summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorChristian Mollekopf <chrigi_1@fastmail.fm>2017-03-06 18:15:51 +0100
committerChristian Mollekopf <chrigi_1@fastmail.fm>2017-03-06 18:15:51 +0100
commit9237f0d23fb3700244933b36116397b80f466902 (patch)
tree35e1afdd7af91feddb1df843c1d661a6e870c981
parentceac8bebf60025a4da0997a4d73de1328c04e97d (diff)
downloadsink-9237f0d23fb3700244933b36116397b80f466902.tar.gz
sink-9237f0d23fb3700244933b36116397b80f466902.zip
lmdb cleanup
-rw-r--r--common/storage_common.cpp2
-rw-r--r--common/storage_lmdb.cpp74
-rw-r--r--common/typeindex.cpp6
3 files changed, 46 insertions, 36 deletions
diff --git a/common/storage_common.cpp b/common/storage_common.cpp
index d8b1f42..07d0f04 100644
--- a/common/storage_common.cpp
+++ b/common/storage_common.cpp
@@ -28,7 +28,7 @@ SINK_DEBUG_AREA("storage")
28 28
29QDebug& operator<<(QDebug &dbg, const Sink::Storage::DataStore::Error &error) 29QDebug& operator<<(QDebug &dbg, const Sink::Storage::DataStore::Error &error)
30{ 30{
31 dbg << error.message; 31 dbg << error.message << "Code: " << error.code << "Db: " << error.store;
32 return dbg; 32 return dbg;
33} 33}
34 34
diff --git a/common/storage_lmdb.cpp b/common/storage_lmdb.cpp
index e49ecae..08eea37 100644
--- a/common/storage_lmdb.cpp
+++ b/common/storage_lmdb.cpp
@@ -62,6 +62,44 @@ int getErrorCode(int e)
62 return -1; 62 return -1;
63} 63}
64 64
65static QList<QByteArray> getDatabaseNames(MDB_txn *transaction)
66{
67 if (!transaction) {
68 SinkWarning() << "Invalid transaction";
69 return QList<QByteArray>();
70 }
71 int rc;
72 QList<QByteArray> list;
73 MDB_dbi dbi;
74 if ((rc = mdb_dbi_open(transaction, nullptr, 0, &dbi) == 0)) {
75 MDB_val key;
76 MDB_val data;
77 MDB_cursor *cursor;
78
79 mdb_cursor_open(transaction, dbi, &cursor);
80 if ((rc = mdb_cursor_get(cursor, &key, &data, MDB_FIRST)) == 0) {
81 list << QByteArray::fromRawData((char *)key.mv_data, key.mv_size);
82 while ((rc = mdb_cursor_get(cursor, &key, &data, MDB_NEXT)) == 0) {
83 list << QByteArray::fromRawData((char *)key.mv_data, key.mv_size);
84 }
85 } else {
86 //Normal if we don't have any databases yet
87 if (rc == MDB_NOTFOUND) {
88 rc = 0;
89 }
90 if (rc) {
91 SinkWarning() << "Failed to get a value" << rc;
92 }
93 }
94 mdb_cursor_close(cursor);
95 } else {
96 SinkWarning() << "Failed to open db" << rc << QByteArray(mdb_strerror(rc));
97 }
98 return list;
99
100}
101
102
65class DataStore::NamedDatabase::Private 103class DataStore::NamedDatabase::Private
66{ 104{
67public: 105public:
@@ -291,7 +329,8 @@ int DataStore::NamedDatabase::scan(const QByteArray &k, const std::function<bool
291 329
292 rc = mdb_cursor_open(d->transaction, d->dbi, &cursor); 330 rc = mdb_cursor_open(d->transaction, d->dbi, &cursor);
293 if (rc) { 331 if (rc) {
294 Error error(d->name.toLatin1() + d->db, getErrorCode(rc), QByteArray("Error during mdb_cursor open: ") + QByteArray(mdb_strerror(rc))); 332 //Invalid arguments can mean that the transaction doesn't contain the db dbi
333 Error error(d->name.toLatin1() + d->db, getErrorCode(rc), QByteArray("Error during mdb_cursor_open: ") + QByteArray(mdb_strerror(rc)) + ". Key: " + k);
295 errorHandler ? errorHandler(error) : d->defaultErrorHandler(error); 334 errorHandler ? errorHandler(error) : d->defaultErrorHandler(error);
296 return 0; 335 return 0;
297 } 336 }
@@ -459,7 +498,6 @@ public:
459 498
460 MDB_env *env; 499 MDB_env *env;
461 MDB_txn *transaction; 500 MDB_txn *transaction;
462 MDB_dbi dbi;
463 bool requestedRead; 501 bool requestedRead;
464 std::function<void(const DataStore::Error &error)> defaultErrorHandler; 502 std::function<void(const DataStore::Error &error)> defaultErrorHandler;
465 QString name; 503 QString name;
@@ -590,8 +628,7 @@ static bool ensureCorrectDb(DataStore::NamedDatabase &database, const QByteArray
590 } 628 }
591 return false; 629 return false;
592 }, 630 },
593 [](const DataStore::Error &error) -> bool{ 631 [&](const DataStore::Error &) {
594 return false;
595 }, false); 632 }, false);
596 //This is the first time we open this database in a write transaction, write the db name 633 //This is the first time we open this database in a write transaction, write the db name
597 if (!count) { 634 if (!count) {
@@ -649,35 +686,8 @@ QList<QByteArray> DataStore::Transaction::getDatabaseNames() const
649 SinkWarning() << "Invalid transaction"; 686 SinkWarning() << "Invalid transaction";
650 return QList<QByteArray>(); 687 return QList<QByteArray>();
651 } 688 }
689 return Sink::Storage::getDatabaseNames(d->transaction);
652 690
653 int rc;
654 QList<QByteArray> list;
655 Q_ASSERT(d->transaction);
656 if ((rc = mdb_dbi_open(d->transaction, nullptr, 0, &d->dbi) == 0)) {
657 MDB_val key;
658 MDB_val data;
659 MDB_cursor *cursor;
660
661 mdb_cursor_open(d->transaction, d->dbi, &cursor);
662 if ((rc = mdb_cursor_get(cursor, &key, &data, MDB_FIRST)) == 0) {
663 list << QByteArray::fromRawData((char *)key.mv_data, key.mv_size);
664 while ((rc = mdb_cursor_get(cursor, &key, &data, MDB_NEXT)) == 0) {
665 list << QByteArray::fromRawData((char *)key.mv_data, key.mv_size);
666 }
667 } else {
668 //Normal if we don't have any databases yet
669 if (rc == MDB_NOTFOUND) {
670 rc = 0;
671 }
672 if (rc) {
673 SinkWarning() << "Failed to get a value" << rc;
674 }
675 }
676 mdb_cursor_close(cursor);
677 } else {
678 SinkWarning() << "Failed to open db" << rc << QByteArray(mdb_strerror(rc));
679 }
680 return list;
681} 691}
682 692
683 693
diff --git a/common/typeindex.cpp b/common/typeindex.cpp
index 5589e13..153aa43 100644
--- a/common/typeindex.cpp
+++ b/common/typeindex.cpp
@@ -190,7 +190,7 @@ static QVector<QByteArray> indexLookup(Index &index, QueryBase::Comparator filte
190 190
191 for (const auto &lookupKey : lookupKeys) { 191 for (const auto &lookupKey : lookupKeys) {
192 index.lookup(lookupKey, [&](const QByteArray &value) { keys << value; }, 192 index.lookup(lookupKey, [&](const QByteArray &value) { keys << value; },
193 [lookupKey](const Index::Error &error) { SinkWarning() << "Error in index: " << error.message << lookupKey; }, true); 193 [lookupKey](const Index::Error &error) { SinkWarning() << "Lookup error in index: " << error.message << lookupKey; }, true);
194 } 194 }
195 return keys; 195 return keys;
196} 196}
@@ -272,7 +272,7 @@ QVector<QByteArray> TypeIndex::secondaryLookup<QByteArray>(const QByteArray &lef
272 Index index(indexName(leftName + rightName), *mTransaction); 272 Index index(indexName(leftName + rightName), *mTransaction);
273 const auto lookupKey = getByteArray(value); 273 const auto lookupKey = getByteArray(value);
274 index.lookup( 274 index.lookup(
275 lookupKey, [&](const QByteArray &value) { keys << value; }, [=](const Index::Error &error) { SinkWarning() << "Error in index: " << error.message << value; }); 275 lookupKey, [&](const QByteArray &value) { keys << value; }, [=](const Index::Error &error) { SinkWarning() << "Lookup error in secondary index: " << error.message << value << lookupKey; });
276 276
277 return keys; 277 return keys;
278} 278}
@@ -284,7 +284,7 @@ QVector<QByteArray> TypeIndex::secondaryLookup<QString>(const QByteArray &leftNa
284 Index index(indexName(leftName + rightName), *mTransaction); 284 Index index(indexName(leftName + rightName), *mTransaction);
285 const auto lookupKey = getByteArray(value); 285 const auto lookupKey = getByteArray(value);
286 index.lookup( 286 index.lookup(
287 lookupKey, [&](const QByteArray &value) { keys << value; }, [=](const Index::Error &error) { SinkWarning() << "Error in index: " << error.message << value; }); 287 lookupKey, [&](const QByteArray &value) { keys << value; }, [=](const Index::Error &error) { SinkWarning() << "Lookup error in secondary index: " << error.message << value << lookupKey; });
288 288
289 return keys; 289 return keys;
290} 290}