diff options
-rw-r--r-- | store/database.cpp | 36 | ||||
-rw-r--r-- | store/database.h | 5 |
2 files changed, 33 insertions, 8 deletions
diff --git a/store/database.cpp b/store/database.cpp index fd3aa8f..c58379c 100644 --- a/store/database.cpp +++ b/store/database.cpp | |||
@@ -14,9 +14,11 @@ | |||
14 | class Database::Private | 14 | class Database::Private |
15 | { | 15 | { |
16 | public: | 16 | public: |
17 | Private(const QString &path); | 17 | Private(const QString &s, const QString &name); |
18 | ~Private(); | 18 | ~Private(); |
19 | 19 | ||
20 | QString storageRoot; | ||
21 | QString name; | ||
20 | MDB_dbi dbi; | 22 | MDB_dbi dbi; |
21 | MDB_env *env; | 23 | MDB_env *env; |
22 | MDB_txn *transaction; | 24 | MDB_txn *transaction; |
@@ -24,19 +26,21 @@ public: | |||
24 | bool firstOpen; | 26 | bool firstOpen; |
25 | }; | 27 | }; |
26 | 28 | ||
27 | Database::Private::Private(const QString &path) | 29 | Database::Private::Private(const QString &s, const QString &n) |
28 | : transaction(0), | 30 | : transaction(0), |
29 | readTransaction(false), | 31 | readTransaction(false), |
30 | firstOpen(true) | 32 | firstOpen(true), |
33 | storageRoot(s), | ||
34 | name(n) | ||
31 | { | 35 | { |
32 | QDir dir; | 36 | QDir dir; |
33 | dir.mkdir(path); | 37 | dir.mkdir(storageRoot); |
34 | 38 | ||
35 | //create file | 39 | //create file |
36 | if (mdb_env_create(&env)) { | 40 | if (mdb_env_create(&env)) { |
37 | // TODO: handle error | 41 | // TODO: handle error |
38 | } else { | 42 | } else { |
39 | int rc = mdb_env_open(env, path.toStdString().data(), 0, 0664); | 43 | int rc = mdb_env_open(env, (storageRoot+name).toStdString().data(), 0, 0664); |
40 | 44 | ||
41 | if (rc) { | 45 | if (rc) { |
42 | std::cerr << "mdb_env_open: " << rc << " " << mdb_strerror(rc) << std::endl; | 46 | std::cerr << "mdb_env_open: " << rc << " " << mdb_strerror(rc) << std::endl; |
@@ -60,8 +64,8 @@ Database::Private::~Private() | |||
60 | mdb_env_close(env); | 64 | mdb_env_close(env); |
61 | } | 65 | } |
62 | 66 | ||
63 | Database::Database(const QString &path) | 67 | Database::Database(const QString &storageRoot, const QString &name) |
64 | : d(new Private(path)) | 68 | : d(new Private(storageRoot, name)) |
65 | { | 69 | { |
66 | } | 70 | } |
67 | 71 | ||
@@ -141,6 +145,11 @@ void Database::abortTransaction() | |||
141 | d->transaction = 0; | 145 | d->transaction = 0; |
142 | } | 146 | } |
143 | 147 | ||
148 | bool Database::write(const char *key, size_t keySize, const char *value, size_t valueSize) | ||
149 | { | ||
150 | write(std::string(key, keySize), std::string(value, valueSize)); | ||
151 | } | ||
152 | |||
144 | bool Database::write(const std::string &sKey, const std::string &sValue) | 153 | bool Database::write(const std::string &sKey, const std::string &sValue) |
145 | { | 154 | { |
146 | if (!d->env) { | 155 | if (!d->env) { |
@@ -249,3 +258,16 @@ void Database::read(const std::string &sKey, const std::function<void(void *ptr, | |||
249 | */ | 258 | */ |
250 | } | 259 | } |
251 | 260 | ||
261 | qint64 Database::diskUsage() const | ||
262 | { | ||
263 | QFileInfo info(d->storageRoot+d->name); | ||
264 | return info.size(); | ||
265 | } | ||
266 | |||
267 | void Database::removeFromDisk() const | ||
268 | { | ||
269 | QFileInfo info(d->storageRoot+d->name); | ||
270 | QDir dir = info.dir(); | ||
271 | dir.remove("data.mdb"); | ||
272 | dir.remove("lock.mdb"); | ||
273 | } | ||
diff --git a/store/database.h b/store/database.h index 2bf0556..e752ff5 100644 --- a/store/database.h +++ b/store/database.h | |||
@@ -7,17 +7,20 @@ class Database { | |||
7 | public: | 7 | public: |
8 | enum TransactionType { ReadOnly, ReadWrite }; | 8 | enum TransactionType { ReadOnly, ReadWrite }; |
9 | 9 | ||
10 | Database(const QString &path); | 10 | Database(const QString &storageRoot, const QString &name); |
11 | ~Database(); | 11 | ~Database(); |
12 | bool isInTransaction() const; | 12 | bool isInTransaction() const; |
13 | bool startTransaction(TransactionType type = ReadWrite); | 13 | bool startTransaction(TransactionType type = ReadWrite); |
14 | bool commitTransaction(); | 14 | bool commitTransaction(); |
15 | void abortTransaction(); | 15 | void abortTransaction(); |
16 | bool write(const char *key, size_t keySize, const char *value, size_t valueSize); | ||
16 | bool write(const std::string &sKey, const std::string &sValue); | 17 | bool write(const std::string &sKey, const std::string &sValue); |
17 | //Perhaps prefer iterators (assuming we need to be able to match multiple values | 18 | //Perhaps prefer iterators (assuming we need to be able to match multiple values |
18 | void read(const std::string &sKey, const std::function<void(const std::string &value)> &); | 19 | void read(const std::string &sKey, const std::function<void(const std::string &value)> &); |
19 | void read(const std::string &sKey, const std::function<void(void *ptr, int size)> &); | 20 | void read(const std::string &sKey, const std::function<void(void *ptr, int size)> &); |
20 | 21 | ||
22 | qint64 diskUsage() const; | ||
23 | void removeFromDisk() const; | ||
21 | private: | 24 | private: |
22 | class Private; | 25 | class Private; |
23 | Private * const d; | 26 | Private * const d; |