32 virtual size_t numBytesOnDisk()
const = 0;
34 virtual bool write(ostream& os)
const = 0;
36 virtual bool read(istream& is) = 0;
55 return sizeof(size_t) + data.size() *
sizeof(T);
59 size_t n = data.size();
60 os.write((
char *)&n,
sizeof(
size_t));
62 os.write((
char *)&data[0], n *
sizeof(T));
69 is.read((
char *)&n,
sizeof(
size_t));
72 is.read((
char *)&data[0], n *
sizeof(T));
84 std::vector<std::vector<T> > data;
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);
102 size_t n = data.size();
103 os.write((
char *)&n,
sizeof(
size_t));
104 for (
unsigned i = 0; i < data.size(); i++) {
106 os.write((
char *)&n,
sizeof(
size_t));
108 os.write((
char *)&data[i][0], n *
sizeof(T));
116 is.read((
char *)&n,
sizeof(
size_t));
118 for (
unsigned i = 0; i < data.size(); i++) {
119 is.read((
char *)&n,
sizeof(
size_t));
122 is.read((
char *)&data[i][0], n *
sizeof(T));
181 #ifdef DRWN_USE_PTHREADS
182 static pthread_mutex_t _gmutex;
183 pthread_mutex_t _mutex;
192 bool open(
const char *indexFile,
const char *dataFile);
194 bool open(
const char *fileStem);
201 bool isOpen()
const {
return (_fsdata != NULL) || _bSuspended; }
203 bool canReopen()
const {
return (!_indexFilename.empty() && !_dataFilename.empty() && (_fsdata == NULL)); }
206 int numRecords()
const {
return (
int)_recordMapping.size(); }
208 size_t numTotalBytes()
const;
210 size_t numUsedBytes()
const;
212 size_t numFreeBytes()
const;
215 bool hasKey(
const char *key)
const {
return _recordMapping.find(
string(key)) != _recordMapping.end(); }
217 set<string> getKeys()
const;
220 bool erase(
const char *key);
226 size_t bytes(
const char *key)
const;
237 bool atomic_reopen(
bool locked);
238 bool atomic_close(
bool locked);
239 void atomic_suspend(
bool locked);
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