diff options
author | Christian Mollekopf <chrigi_1@fastmail.fm> | 2015-08-23 13:02:04 +0200 |
---|---|---|
committer | Christian Mollekopf <chrigi_1@fastmail.fm> | 2015-08-23 13:03:50 +0200 |
commit | 4141b5e8e6296ca8ab94553e27257f8c2b107461 (patch) | |
tree | 02c5a45a7e6736c24d76dbb24eeb468f1ced11d9 /common/storage_lmdb.cpp | |
parent | 6746247a49f09287ae4924c5c3df791f9bf61cbc (diff) | |
download | sink-4141b5e8e6296ca8ab94553e27257f8c2b107461.tar.gz sink-4141b5e8e6296ca8ab94553e27257f8c2b107461.zip |
Less noise and better error handling.
Trying to read from non-existant databases no longer prints error
messages.
Diffstat (limited to 'common/storage_lmdb.cpp')
-rw-r--r-- | common/storage_lmdb.cpp | 41 |
1 files changed, 27 insertions, 14 deletions
diff --git a/common/storage_lmdb.cpp b/common/storage_lmdb.cpp index ebb3be3..3e7c962 100644 --- a/common/storage_lmdb.cpp +++ b/common/storage_lmdb.cpp | |||
@@ -71,18 +71,23 @@ public: | |||
71 | std::function<void(const Storage::Error &error)> defaultErrorHandler; | 71 | std::function<void(const Storage::Error &error)> defaultErrorHandler; |
72 | QString name; | 72 | QString name; |
73 | 73 | ||
74 | bool openDatabase(std::function<void(const Storage::Error &error)> errorHandler) | 74 | bool openDatabase(bool readOnly, std::function<void(const Storage::Error &error)> errorHandler) |
75 | { | 75 | { |
76 | unsigned int flags = MDB_CREATE; | 76 | unsigned int flags = 0; |
77 | if (!readOnly) { | ||
78 | flags |= MDB_CREATE; | ||
79 | } | ||
77 | if (allowDuplicates) { | 80 | if (allowDuplicates) { |
78 | flags |= MDB_DUPSORT; | 81 | flags |= MDB_DUPSORT; |
79 | } | 82 | } |
80 | if (const int rc = mdb_dbi_open(transaction, db.constData(), flags, &dbi)) { | 83 | if (const int rc = mdb_dbi_open(transaction, db.constData(), flags, &dbi)) { |
81 | qWarning() << "Failed to open: " << rc << db; | ||
82 | dbi = 0; | 84 | dbi = 0; |
83 | transaction = 0; | 85 | transaction = 0; |
84 | Error error(name.toLatin1(), ErrorCodes::GenericError, "Error while opening database: " + QByteArray(mdb_strerror(rc))); | 86 | //The database is not existing, ignore in read-only mode |
85 | errorHandler ? errorHandler(error) : defaultErrorHandler(error); | 87 | if (!(readOnly && rc == MDB_NOTFOUND)) { |
88 | Error error(name.toLatin1(), ErrorCodes::GenericError, "Error while opening database: " + QByteArray(mdb_strerror(rc))); | ||
89 | errorHandler ? errorHandler(error) : defaultErrorHandler(error); | ||
90 | } | ||
86 | return false; | 91 | return false; |
87 | } | 92 | } |
88 | return true; | 93 | return true; |
@@ -108,7 +113,7 @@ Storage::NamedDatabase::~NamedDatabase() | |||
108 | bool Storage::NamedDatabase::write(const QByteArray &sKey, const QByteArray &sValue, const std::function<void(const Storage::Error &error)> &errorHandler) | 113 | bool Storage::NamedDatabase::write(const QByteArray &sKey, const QByteArray &sValue, const std::function<void(const Storage::Error &error)> &errorHandler) |
109 | { | 114 | { |
110 | if (!d || !d->transaction) { | 115 | if (!d || !d->transaction) { |
111 | Error error(d->name.toLatin1(), ErrorCodes::GenericError, "Not open"); | 116 | Error error("", ErrorCodes::GenericError, "Not open"); |
112 | errorHandler ? errorHandler(error) : d->defaultErrorHandler(error); | 117 | errorHandler ? errorHandler(error) : d->defaultErrorHandler(error); |
113 | return false; | 118 | return false; |
114 | } | 119 | } |
@@ -167,8 +172,7 @@ int Storage::NamedDatabase::scan(const QByteArray &k, | |||
167 | const std::function<void(const Storage::Error &error)> &errorHandler) const | 172 | const std::function<void(const Storage::Error &error)> &errorHandler) const |
168 | { | 173 | { |
169 | if (!d || !d->transaction) { | 174 | if (!d || !d->transaction) { |
170 | // Error error(d->name.toLatin1(), ErrorCodes::NotOpen, "Not open"); | 175 | //Not an error. We rely on this to read nothing from non-existing databases. |
171 | // errorHandler ? errorHandler(error) : d->defaultErrorHandler(error); | ||
172 | return 0; | 176 | return 0; |
173 | } | 177 | } |
174 | 178 | ||
@@ -328,16 +332,20 @@ Storage::NamedDatabase Storage::Transaction::openDatabase(const QByteArray &db, | |||
328 | //We don't now if anything changed | 332 | //We don't now if anything changed |
329 | d->implicitCommit = true; | 333 | d->implicitCommit = true; |
330 | auto p = new Storage::NamedDatabase::Private(db, d->allowDuplicates, d->defaultErrorHandler, d->name, d->transaction); | 334 | auto p = new Storage::NamedDatabase::Private(db, d->allowDuplicates, d->defaultErrorHandler, d->name, d->transaction); |
331 | p->openDatabase(errorHandler); | 335 | if (!p->openDatabase(d->requestedRead, errorHandler)) { |
336 | return Storage::NamedDatabase(); | ||
337 | delete p; | ||
338 | } | ||
332 | return Storage::NamedDatabase(p); | 339 | return Storage::NamedDatabase(p); |
333 | } | 340 | } |
334 | 341 | ||
335 | bool Storage::Transaction::write(const QByteArray &key, const QByteArray &value, const std::function<void(const Storage::Error &error)> &errorHandler) | 342 | bool Storage::Transaction::write(const QByteArray &key, const QByteArray &value, const std::function<void(const Storage::Error &error)> &errorHandler) |
336 | { | 343 | { |
337 | openDatabase().write(key, value, [this, errorHandler](const Storage::Error &error) { | 344 | auto eHandler = [this, errorHandler](const Storage::Error &error) { |
338 | d->error = true; | 345 | d->error = true; |
339 | errorHandler ? errorHandler(error) : d->defaultErrorHandler(error); | 346 | errorHandler ? errorHandler(error) : d->defaultErrorHandler(error); |
340 | }); | 347 | }; |
348 | openDatabase("default", eHandler).write(key, value, eHandler); | ||
341 | d->implicitCommit = true; | 349 | d->implicitCommit = true; |
342 | 350 | ||
343 | return !d->error; | 351 | return !d->error; |
@@ -346,10 +354,11 @@ bool Storage::Transaction::write(const QByteArray &key, const QByteArray &value, | |||
346 | void Storage::Transaction::remove(const QByteArray &k, | 354 | void Storage::Transaction::remove(const QByteArray &k, |
347 | const std::function<void(const Storage::Error &error)> &errorHandler) | 355 | const std::function<void(const Storage::Error &error)> &errorHandler) |
348 | { | 356 | { |
349 | openDatabase().remove(k, [this, errorHandler](const Storage::Error &error) { | 357 | auto eHandler = [this, errorHandler](const Storage::Error &error) { |
350 | d->error = true; | 358 | d->error = true; |
351 | errorHandler ? errorHandler(error) : d->defaultErrorHandler(error); | 359 | errorHandler ? errorHandler(error) : d->defaultErrorHandler(error); |
352 | }); | 360 | }; |
361 | openDatabase("default", eHandler).remove(k, eHandler); | ||
353 | d->implicitCommit = true; | 362 | d->implicitCommit = true; |
354 | } | 363 | } |
355 | 364 | ||
@@ -357,7 +366,11 @@ int Storage::Transaction::scan(const QByteArray &k, | |||
357 | const std::function<bool(const QByteArray &key, const QByteArray &value)> &resultHandler, | 366 | const std::function<bool(const QByteArray &key, const QByteArray &value)> &resultHandler, |
358 | const std::function<void(const Storage::Error &error)> &errorHandler) const | 367 | const std::function<void(const Storage::Error &error)> &errorHandler) const |
359 | { | 368 | { |
360 | return openDatabase().scan(k, resultHandler, errorHandler); | 369 | auto db = openDatabase("default"); |
370 | if (db) { | ||
371 | return db.scan(k, resultHandler, errorHandler); | ||
372 | } | ||
373 | return 0; | ||
361 | } | 374 | } |
362 | 375 | ||
363 | 376 | ||