~drizzle-trunk/drizzle/development

1089.2.1 by David Shrewsbury
Add a new CachedDirectory class that handles opendir/readdir operations for all systems.
1
/* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2
 *  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
3
 *
1999.6.1 by kalebral at gmail
update Copyright strings to a more common format to help with creating the master debian copyright file
4
 *  Copyright (C) 2008 Sun Microsystems, Inc.
1089.2.1 by David Shrewsbury
Add a new CachedDirectory class that handles opendir/readdir operations for all systems.
5
 *
6
 *  This program is free software; you can redistribute it and/or modify
7
 *  it under the terms of the GNU General Public License as published by
8
 *  the Free Software Foundation; version 2 of the License.
9
 *
10
 *  This program is distributed in the hope that it will be useful,
11
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 *  GNU General Public License for more details.
14
 *
15
 *  You should have received a copy of the GNU General Public License
16
 *  along with this program; if not, write to the Free Software
17
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
18
 */
19
20
/**
21
 * @file
22
 *   cached_directory.h
23
 *
24
 * @brief
25
 *   Defines the interface to the CachedDirectory class.
26
 */
27
1241.9.44 by Monty Taylor
Made magic with cached_directory.
28
#ifndef DRIZZLED_CACHED_DIRECTORY_H
29
#define DRIZZLED_CACHED_DIRECTORY_H
30
31
#include <dirent.h>
1089.2.1 by David Shrewsbury
Add a new CachedDirectory class that handles opendir/readdir operations for all systems.
32
1183.1.5 by Brian Aker
Reworked getTableNames() interface. Way simpler, less mallocs....
33
#include <iostream>
1089.2.1 by David Shrewsbury
Add a new CachedDirectory class that handles opendir/readdir operations for all systems.
34
#include <vector>
1183.1.27 by Brian Aker
Fix issue where there are too many files in data directory. We now only
35
#include <set>
1089.2.1 by David Shrewsbury
Add a new CachedDirectory class that handles opendir/readdir operations for all systems.
36
#include <string>
1241.9.44 by Monty Taylor
Made magic with cached_directory.
37
#include <cstdlib>
38
#include <cerrno>
39
40
namespace drizzled
41
{
1089.2.1 by David Shrewsbury
Add a new CachedDirectory class that handles opendir/readdir operations for all systems.
42
43
/**
44
 * A utility class to handle processing the entries/files within a directory.
45
 *
46
 * This class will allow the user to either get a list of the entry names 
1108.4.1 by David Shrewsbury
Corrected comments
47
 * within a given directory.
1089.2.1 by David Shrewsbury
Add a new CachedDirectory class that handles opendir/readdir operations for all systems.
48
 */
49
class CachedDirectory
50
{
51
public:
1273.13.9 by Brian Aker
Updating test cases + added Drizzle specific schema_names and schema_info.
52
  enum FILTER {
53
    NONE,
54
    DIRECTORY,
55
    FILE,
56
    MAX
57
  };
58
1089.2.1 by David Shrewsbury
Add a new CachedDirectory class that handles opendir/readdir operations for all systems.
59
  class Entry
60
  {
1259.3.4 by Monty Taylor
Just a little bit of a cleanup.
61
    Entry();
1089.2.1 by David Shrewsbury
Add a new CachedDirectory class that handles opendir/readdir operations for all systems.
62
  public:
63
    std::string filename;
1259.3.4 by Monty Taylor
Just a little bit of a cleanup.
64
    explicit Entry(std::string in_name)
1089.2.1 by David Shrewsbury
Add a new CachedDirectory class that handles opendir/readdir operations for all systems.
65
      : filename(in_name)
66
    {}
67
  };
68
  typedef std::vector<Entry *> Entries;
69
  /**
1200.2.2 by Jay Pipes
Cleans up a few things in CachedDirectory class:
70
   * Empty Constructor.
1089.2.1 by David Shrewsbury
Add a new CachedDirectory class that handles opendir/readdir operations for all systems.
71
   */
1183.1.5 by Brian Aker
Reworked getTableNames() interface. Way simpler, less mallocs....
72
  CachedDirectory();
1089.2.1 by David Shrewsbury
Add a new CachedDirectory class that handles opendir/readdir operations for all systems.
73
      
74
  /**
75
   * Constructor taking full directory path as sole parameter.
76
   *
77
   * @param[in] Path to the directory to open
1200.2.2 by Jay Pipes
Cleans up a few things in CachedDirectory class:
78
   * @param[in] File extensions to allow
79
   */
80
  CachedDirectory(const std::string& in_path); 
81
82
  /**
83
   * Constructor taking full directory path as sole parameter.
84
   *
85
   * @param[in] Path to the directory to open
86
   * @param[in] File extensions to allow
87
   */
88
  CachedDirectory(const std::string& in_path, std::set<std::string>& allowed_exts);
1960.1.9 by Brian Aker
Merge in lock testing code/additional fix for tests.
89
  CachedDirectory(const std::string& in_path, enum CachedDirectory::FILTER filter, bool use_full_path= false);
1200.2.2 by Jay Pipes
Cleans up a few things in CachedDirectory class:
90
91
  /**
92
   * Destructor.  Cleans up any resources we've taken 
93
   */
1089.2.1 by David Shrewsbury
Add a new CachedDirectory class that handles opendir/readdir operations for all systems.
94
  ~CachedDirectory();
95
96
  /**
97
   * Returns whether the CachedDirectory object is in a failed state
98
   */
99
  inline bool fail() const 
100
  {
101
    return error != 0;
102
  }
103
104
  /** 
105
   * Returns the stored error code of the last action the directory
106
   * object took (open, read, etc)
107
   */
108
  inline int getError() const
109
  {
110
    return error;
111
  }
112
1183.1.5 by Brian Aker
Reworked getTableNames() interface. Way simpler, less mallocs....
113
  /** 
114
   * Returns the current path for the cached directory
115
   */
116
  inline const char *getPath() const
117
  {
118
    return path.c_str();
119
  }
120
1089.2.1 by David Shrewsbury
Add a new CachedDirectory class that handles opendir/readdir operations for all systems.
121
  /**
1108.4.1 by David Shrewsbury
Corrected comments
122
   * Return the list of entries read from the directory
1089.2.1 by David Shrewsbury
Add a new CachedDirectory class that handles opendir/readdir operations for all systems.
123
   *
124
   * @returns
125
   *   A vector of strings containing the directory entry names.
126
   */
127
  inline const Entries &getEntries()
128
  {
129
    return entries;
130
  }
1200.2.2 by Jay Pipes
Cleans up a few things in CachedDirectory class:
131
private:
132
  std::string path; ///< Path to the directory
133
  int error; ///< Error code stored from various syscalls
1960.1.9 by Brian Aker
Merge in lock testing code/additional fix for tests.
134
  bool use_full_path;
1200.2.2 by Jay Pipes
Cleans up a few things in CachedDirectory class:
135
  Entries entries; ///< Entries in the directory
1183.1.5 by Brian Aker
Reworked getTableNames() interface. Way simpler, less mallocs....
136
137
  /**
138
   * Encapsulate the logic to open the directory.
1200.2.2 by Jay Pipes
Cleans up a few things in CachedDirectory class:
139
   * @param[in] The path to the directory to open and read
140
   *
1183.1.5 by Brian Aker
Reworked getTableNames() interface. Way simpler, less mallocs....
141
   * @retval true Success
142
   * @retval false Failure
143
   */
1200.2.2 by Jay Pipes
Cleans up a few things in CachedDirectory class:
144
  bool open(const std::string &in_path);
1183.1.5 by Brian Aker
Reworked getTableNames() interface. Way simpler, less mallocs....
145
1200.2.2 by Jay Pipes
Cleans up a few things in CachedDirectory class:
146
  /**
147
   * Encapsulate the logic to open the directory with a set of allowed
148
   * file extensions to filter for.
149
   *
150
   * @param[in] The path to the directory to open and read
151
   * @param[in] File extensions to allow
152
   *
153
   * @retval true Success
154
   * @retval false Failure
155
   */
156
  bool open(const std::string &in_path, std::set<std::string> &allowable_exts);
1273.13.9 by Brian Aker
Updating test cases + added Drizzle specific schema_names and schema_info.
157
  bool open(const std::string &in_path, std::set<std::string> &allowed_exts, enum CachedDirectory::FILTER filter);
158
1387 by Brian Aker
Fix for cases where not all files are removed during a deletion of a schema.
159
  friend std::ostream& operator<<(std::ostream& output, CachedDirectory &directory)
160
  {
161
    output << "CachedDirectory:(Path: " << directory.getPath() << ")\n";
162
163
    CachedDirectory::Entries files= directory.getEntries();
164
165
    for (CachedDirectory::Entries::iterator fileIter= files.begin();
166
         fileIter != files.end(); fileIter++)
167
    {
168
      CachedDirectory::Entry *entry= *fileIter;
169
      output << "\t(" << entry->filename << ")\n";
170
    }
171
172
    return output;  // for multiple << operators.
173
  }
174
1089.2.1 by David Shrewsbury
Add a new CachedDirectory class that handles opendir/readdir operations for all systems.
175
};
176
1241.9.44 by Monty Taylor
Made magic with cached_directory.
177
} /* namespace drizzled */
178
179
#endif /* DRIZZLED_CACHED_DIRECTORY_H */