Darwin  1.10(beta)
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
drwnPersistentStorage.h
1 /*****************************************************************************
2 ** DARWIN: A FRAMEWORK FOR MACHINE LEARNING RESEARCH AND DEVELOPMENT
3 ** Distributed under the terms of the BSD license (see the LICENSE file)
4 ** Copyright (c) 2007-2015, Stephen Gould
5 ** All rights reserved.
6 **
7 ******************************************************************************
8 ** FILENAME: drwnPersistentStorage.h
9 ** AUTHOR(S): Stephen Gould <stephen.gould@anu.edu.au>
10 **
11 *****************************************************************************/
12 
13 #pragma once
14 
15 #include <cstdlib>
16 #include <map>
17 #include <list>
18 #include <vector>
19 
20 using namespace std;
21 
22 // drwnPersistentRecord ------------------------------------------------------
24 
26  public:
27  drwnPersistentRecord() { /* do nothing */ };
28  virtual ~drwnPersistentRecord() { /* do nothing */ };
29 
32  virtual size_t numBytesOnDisk() const = 0;
34  virtual bool write(ostream& os) const = 0;
36  virtual bool read(istream& is) = 0;
37 };
38 
39 // drwnPersistentVectorRecord ------------------------------------------------
41 
42 template <class T>
44  public:
45  std::vector<T> data; //<! the data
46 
47  public:
49  drwnPersistentVectorRecord() { /* do nothing */ }
51  ~drwnPersistentVectorRecord() { /* do nothing */ }
52 
53  // i/o (from drwnPersistentRecord)
54  size_t numBytesOnDisk() const {
55  return sizeof(size_t) + data.size() * sizeof(T);
56  }
57 
58  bool write(ostream& os) const {
59  size_t n = data.size();
60  os.write((char *)&n, sizeof(size_t));
61  if (n != 0) {
62  os.write((char *)&data[0], n * sizeof(T));
63  }
64  return true;
65  }
66 
67  bool read(istream& is) {
68  size_t n = 0;
69  is.read((char *)&n, sizeof(size_t));
70  data.resize(n);
71  if (n > 0) {
72  is.read((char *)&data[0], n * sizeof(T));
73  }
74  return true;
75  }
76 };
77 
78 // drwnPersistentVectorVectorRecord ------------------------------------------
80 
81 template <class T>
83  public:
84  std::vector<std::vector<T> > data; //<! the data
85 
86  public:
88  drwnPersistentVectorVectorRecord() { /* do nothing */ }
90  ~drwnPersistentVectorVectorRecord() { /* do nothing */ }
91 
92  // i/o (from drwnPersistentRecord)
93  size_t numBytesOnDisk() const {
94  size_t n = sizeof(size_t) * (data.size() + 1);
95  for (unsigned i = 0; i < data.size(); i++) {
96  n += data[i].size() * sizeof(T);
97  }
98  return n;
99  }
100 
101  bool write(ostream& os) const {
102  size_t n = data.size();
103  os.write((char *)&n, sizeof(size_t));
104  for (unsigned i = 0; i < data.size(); i++) {
105  n = data[i].size();
106  os.write((char *)&n, sizeof(size_t));
107  if (n != 0) {
108  os.write((char *)&data[i][0], n * sizeof(T));
109  }
110  }
111  return true;
112  }
113 
114  bool read(istream& is) {
115  size_t n = 0;
116  is.read((char *)&n, sizeof(size_t));
117  data.resize(n);
118  for (unsigned i = 0; i < data.size(); i++) {
119  is.read((char *)&n, sizeof(size_t));
120  data[i].resize(n);
121  if (n > 0) {
122  is.read((char *)&data[i][0], n * sizeof(T));
123  }
124  }
125  return true;
126  }
127 };
128 
129 // drwnPersistentBlock -------------------------------------------------------
131 
133  public:
134  size_t start;
135  size_t length;
136 
137  public:
138  drwnPersistentBlock() { /* do nothing */ };
139  drwnPersistentBlock(size_t strt, size_t len) : start(strt), length(len) { /* do nothing */ };
140  ~drwnPersistentBlock() { /* do nothing */ };
141 };
142 
143 // drwnPersistentStorage -----------------------------------------------------
161 
163  public:
164  static int MAX_OPEN;
165  static string DEFAULT_INDEX_EXT;
166  static string DEFAULT_DATA_EXT;
167 
168  protected:
169  static list<drwnPersistentStorage *> _openList;
170 
171  string _indexFilename;
172  string _dataFilename;
173  map<string, drwnPersistentBlock> _recordMapping;
174  list<drwnPersistentBlock> _freeSpace;
175 
177  fstream *_fsdata;
178  bool _bSuspended;
179  bool _bDirty;
180 
181 #ifdef DRWN_USE_PTHREADS
182  static pthread_mutex_t _gmutex;
183  pthread_mutex_t _mutex;
184 #endif
185 
186  public:
187  drwnPersistentStorage(bool bCompressed = false);
189  virtual ~drwnPersistentStorage();
190 
192  bool open(const char *indexFile, const char *dataFile);
194  bool open(const char *fileStem);
196  bool reopen();
198  bool close();
199 
201  bool isOpen() const { return (_fsdata != NULL) || _bSuspended; }
203  bool canReopen() const { return (!_indexFilename.empty() && !_dataFilename.empty() && (_fsdata == NULL)); }
204 
206  int numRecords() const { return (int)_recordMapping.size(); }
208  size_t numTotalBytes() const;
210  size_t numUsedBytes() const;
212  size_t numFreeBytes() const;
213 
215  bool hasKey(const char *key) const { return _recordMapping.find(string(key)) != _recordMapping.end(); }
217  set<string> getKeys() const;
218 
220  bool erase(const char *key);
222  bool read(const char *key, drwnPersistentRecord *record);
224  bool write(const char *key, const drwnPersistentRecord *record);
226  size_t bytes(const char *key) const;
227 
229  bool clear();
231  bool defragment();
232 
233  protected:
234  void suspend();
235  void resume();
236 
237  bool atomic_reopen(bool locked);
238  bool atomic_close(bool locked);
239  void atomic_suspend(bool locked);
240 };
bool hasKey(const char *key) const
returns true if a record with given key exists in the persistent storage
Definition: drwnPersistentStorage.h:215
bool _bDirty
data has been written to the storage since opened
Definition: drwnPersistentStorage.h:179
map< string, drwnPersistentBlock > _recordMapping
key, start, length
Definition: drwnPersistentStorage.h:173
size_t start
start of block on disk
Definition: drwnPersistentStorage.h:134
drwnPersistentVectorVectorRecord()
default constructor
Definition: drwnPersistentStorage.h:88
bool read(istream &is)
read the object from an input stream
Definition: drwnPersistentStorage.h:114
string _indexFilename
name of index file
Definition: drwnPersistentStorage.h:171
bool _bSuspended
self-suspended vs. closed (will reopen on any operation)
Definition: drwnPersistentStorage.h:178
string _dataFilename
name of data file
Definition: drwnPersistentStorage.h:172
static int MAX_OPEN
max. number of open files
Definition: drwnPersistentStorage.h:164
int numRecords() const
number of drwnPersistentRecord records stored on disk
Definition: drwnPersistentStorage.h:206
static list< drwnPersistentStorage * > _openList
list of open storage
Definition: drwnPersistentStorage.h:169
static string DEFAULT_INDEX_EXT
default extension for index files
Definition: drwnPersistentStorage.h:165
list< drwnPersistentBlock > _freeSpace
start, length
Definition: drwnPersistentStorage.h:174
~drwnPersistentVectorRecord()
destructor
Definition: drwnPersistentStorage.h:51
Templated class for storing vector records.
Definition: drwnPersistentStorage.h:43
bool read(istream &is)
read the object from an input stream
Definition: drwnPersistentStorage.h:67
bool canReopen() const
returns true if the persistent storage object has valid filenames but is currently closed ...
Definition: drwnPersistentStorage.h:203
Provides indexed storage for multiple records using two files (a binary data file and a text index fi...
Definition: drwnPersistentStorage.h:162
bool write(ostream &os) const
write the object to an output stream
Definition: drwnPersistentStorage.h:58
bool isOpen() const
returns true if the persistent storage has been opened and not closed
Definition: drwnPersistentStorage.h:201
Templated class for storing vector-of-vector records.
Definition: drwnPersistentStorage.h:82
Interface class for drwnPersistentStorage.
Definition: drwnPersistentStorage.h:25
size_t numBytesOnDisk() const
number of bytes required to store object on disk or in a character stream (without compression) ...
Definition: drwnPersistentStorage.h:93
bool write(ostream &os) const
write the object to an output stream
Definition: drwnPersistentStorage.h:101
bool _bCompressed
data in storage is compressed
Definition: drwnPersistentStorage.h:176
drwnPersistentVectorRecord()
default constructor
Definition: drwnPersistentStorage.h:49
Persistent storage block used internally by drwnPersistentStorage.
Definition: drwnPersistentStorage.h:132
size_t numBytesOnDisk() const
number of bytes required to store object on disk or in a character stream (without compression) ...
Definition: drwnPersistentStorage.h:54
~drwnPersistentVectorVectorRecord()
destructor
Definition: drwnPersistentStorage.h:90
size_t length
length of block on disk
Definition: drwnPersistentStorage.h:135
fstream * _fsdata
data file stream (if open)
Definition: drwnPersistentStorage.h:177
static string DEFAULT_DATA_EXT
default extension for data files
Definition: drwnPersistentStorage.h:166