diff options
author | Aaron Seigo <aseigo@kde.org> | 2015-02-09 12:56:08 +0100 |
---|---|---|
committer | Aaron Seigo <aseigo@kde.org> | 2015-02-09 12:56:08 +0100 |
commit | 9d2f550759fc9d46e1039368ef77988cdc3a61e4 (patch) | |
tree | b97464e64be30751f4e3f2802d23ed8ce8704f30 /common/storage_unqlite.cpp | |
parent | d2fe5b77e1a0a4931657f5921e7eff099b211c70 (diff) | |
download | sink-9d2f550759fc9d46e1039368ef77988cdc3a61e4.tar.gz sink-9d2f550759fc9d46e1039368ef77988cdc3a61e4.zip |
catch unqlite impl up to current Storage API
Diffstat (limited to 'common/storage_unqlite.cpp')
-rw-r--r-- | common/storage_unqlite.cpp | 47 |
1 files changed, 36 insertions, 11 deletions
diff --git a/common/storage_unqlite.cpp b/common/storage_unqlite.cpp index 263e7e6..6950acd 100644 --- a/common/storage_unqlite.cpp +++ b/common/storage_unqlite.cpp | |||
@@ -41,7 +41,7 @@ static const char *s_unqliteDir = "/unqlite/"; | |||
41 | class Storage::Private | 41 | class Storage::Private |
42 | { | 42 | { |
43 | public: | 43 | public: |
44 | Private(const QString &s, const QString &name, AccessMode m); | 44 | Private(const QString &s, const QString &name, AccessMode m, bool allowDuplicates); |
45 | ~Private(); | 45 | ~Private(); |
46 | 46 | ||
47 | void reportDbError(const char *functionName); | 47 | void reportDbError(const char *functionName); |
@@ -53,14 +53,16 @@ public: | |||
53 | AccessMode mode; | 53 | AccessMode mode; |
54 | 54 | ||
55 | unqlite *db; | 55 | unqlite *db; |
56 | bool allowDuplicates; | ||
56 | bool inTransaction; | 57 | bool inTransaction; |
57 | }; | 58 | }; |
58 | 59 | ||
59 | Storage::Private::Private(const QString &s, const QString &n, AccessMode m) | 60 | Storage::Private::Private(const QString &s, const QString &n, AccessMode m, bool duplicates) |
60 | : storageRoot(s), | 61 | : storageRoot(s), |
61 | name(n), | 62 | name(n), |
62 | mode(m), | 63 | mode(m), |
63 | db(0), | 64 | db(0), |
65 | allowDuplicates(duplicates), //FIXME: currently does nothing ... should do what it says | ||
64 | inTransaction(false) | 66 | inTransaction(false) |
65 | { | 67 | { |
66 | const QString fullPath(storageRoot + s_unqliteDir + name); | 68 | const QString fullPath(storageRoot + s_unqliteDir + name); |
@@ -121,8 +123,8 @@ void Storage::Private::reportDbError(const char *functionName, int errorCode, | |||
121 | errorHandler(error); | 123 | errorHandler(error); |
122 | } | 124 | } |
123 | 125 | ||
124 | Storage::Storage(const QString &storageRoot, const QString &name, AccessMode mode) | 126 | Storage::Storage(const QString &storageRoot, const QString &name, AccessMode mode, bool allowDuplicates) |
125 | : d(new Private(storageRoot, name, mode)) | 127 | : d(new Private(storageRoot, name, mode, allowDuplicates)) |
126 | { | 128 | { |
127 | } | 129 | } |
128 | 130 | ||
@@ -189,18 +191,13 @@ void Storage::abortTransaction() | |||
189 | d->inTransaction = false; | 191 | d->inTransaction = false; |
190 | } | 192 | } |
191 | 193 | ||
192 | bool Storage::write(const char *key, size_t keySize, const char *value, size_t valueSize) | 194 | bool Storage::write(const void *key, size_t keySize, const void *value, size_t valueSize) |
193 | { | ||
194 | return write(std::string(key, keySize), std::string(value, valueSize)); | ||
195 | } | ||
196 | |||
197 | bool Storage::write(const std::string &sKey, const std::string &sValue) | ||
198 | { | 195 | { |
199 | if (!d->db) { | 196 | if (!d->db) { |
200 | return false; | 197 | return false; |
201 | } | 198 | } |
202 | 199 | ||
203 | int rc = unqlite_kv_store(d->db, sKey.data(), -1, sValue.data(), sValue.size()); | 200 | int rc = unqlite_kv_store(d->db, key, keySize, value, valueSize); |
204 | 201 | ||
205 | if (rc != UNQLITE_OK) { | 202 | if (rc != UNQLITE_OK) { |
206 | d->reportDbError("unqlite_kv_store"); | 203 | d->reportDbError("unqlite_kv_store"); |
@@ -209,6 +206,11 @@ bool Storage::write(const std::string &sKey, const std::string &sValue) | |||
209 | return !rc; | 206 | return !rc; |
210 | } | 207 | } |
211 | 208 | ||
209 | bool Storage::write(const std::string &sKey, const std::string &sValue) | ||
210 | { | ||
211 | return write(sKey.data(), sKey.size(), sValue.data(), sKey.size()); | ||
212 | } | ||
213 | |||
212 | void Storage::read(const std::string &sKey, | 214 | void Storage::read(const std::string &sKey, |
213 | const std::function<bool(const std::string &value)> &resultHandler, | 215 | const std::function<bool(const std::string &value)> &resultHandler, |
214 | const std::function<void(const Storage::Error &error)> &errorHandler) | 216 | const std::function<void(const Storage::Error &error)> &errorHandler) |
@@ -233,6 +235,24 @@ void Storage::read(const std::string &sKey, | |||
233 | }, errorHandler); | 235 | }, errorHandler); |
234 | } | 236 | } |
235 | 237 | ||
238 | void Storage::remove(const void *keyData, uint keySize) | ||
239 | { | ||
240 | remove(keyData, keySize, basicErrorHandler()); | ||
241 | } | ||
242 | |||
243 | void Storage::remove(const void *keyData, uint keySize, | ||
244 | const std::function<void(const Storage::Error &error)> &errorHandler) | ||
245 | { | ||
246 | if (!d->db) { | ||
247 | Error error(d->name.toStdString(), -1, "Not open"); | ||
248 | errorHandler(error); | ||
249 | return; | ||
250 | } | ||
251 | |||
252 | unqlite_kv_delete(d->db, keyData, keySize); | ||
253 | } | ||
254 | |||
255 | |||
236 | void fetchCursorData(unqlite_kv_cursor *cursor, | 256 | void fetchCursorData(unqlite_kv_cursor *cursor, |
237 | void **keyBuffer, int *keyBufferLength, void **dataBuffer, unqlite_int64 *dataBufferLength, | 257 | void **keyBuffer, int *keyBufferLength, void **dataBuffer, unqlite_int64 *dataBufferLength, |
238 | const std::function<bool(void *keyPtr, int keySize, void *valuePtr, int valueSize)> &resultHandler) | 258 | const std::function<bool(void *keyPtr, int keySize, void *valuePtr, int valueSize)> &resultHandler) |
@@ -307,6 +327,11 @@ qint64 Storage::diskUsage() const | |||
307 | return info.size(); | 327 | return info.size(); |
308 | } | 328 | } |
309 | 329 | ||
330 | bool Storage::exists() const | ||
331 | { | ||
332 | return d->db != 0; | ||
333 | } | ||
334 | |||
310 | void Storage::removeFromDisk() const | 335 | void Storage::removeFromDisk() const |
311 | { | 336 | { |
312 | QFile::remove(d->storageRoot + s_unqliteDir + d->name); | 337 | QFile::remove(d->storageRoot + s_unqliteDir + d->name); |