From 9237f0d23fb3700244933b36116397b80f466902 Mon Sep 17 00:00:00 2001 From: Christian Mollekopf Date: Mon, 6 Mar 2017 18:15:51 +0100 Subject: lmdb cleanup --- common/storage_common.cpp | 2 +- common/storage_lmdb.cpp | 74 +++++++++++++++++++++++++++-------------------- common/typeindex.cpp | 6 ++-- 3 files changed, 46 insertions(+), 36 deletions(-) (limited to 'common') 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") QDebug& operator<<(QDebug &dbg, const Sink::Storage::DataStore::Error &error) { - dbg << error.message; + dbg << error.message << "Code: " << error.code << "Db: " << error.store; return dbg; } 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) return -1; } +static QList getDatabaseNames(MDB_txn *transaction) +{ + if (!transaction) { + SinkWarning() << "Invalid transaction"; + return QList(); + } + int rc; + QList list; + MDB_dbi dbi; + if ((rc = mdb_dbi_open(transaction, nullptr, 0, &dbi) == 0)) { + MDB_val key; + MDB_val data; + MDB_cursor *cursor; + + mdb_cursor_open(transaction, dbi, &cursor); + if ((rc = mdb_cursor_get(cursor, &key, &data, MDB_FIRST)) == 0) { + list << QByteArray::fromRawData((char *)key.mv_data, key.mv_size); + while ((rc = mdb_cursor_get(cursor, &key, &data, MDB_NEXT)) == 0) { + list << QByteArray::fromRawData((char *)key.mv_data, key.mv_size); + } + } else { + //Normal if we don't have any databases yet + if (rc == MDB_NOTFOUND) { + rc = 0; + } + if (rc) { + SinkWarning() << "Failed to get a value" << rc; + } + } + mdb_cursor_close(cursor); + } else { + SinkWarning() << "Failed to open db" << rc << QByteArray(mdb_strerror(rc)); + } + return list; + +} + + class DataStore::NamedDatabase::Private { public: @@ -291,7 +329,8 @@ int DataStore::NamedDatabase::scan(const QByteArray &k, const std::functiontransaction, d->dbi, &cursor); if (rc) { - Error error(d->name.toLatin1() + d->db, getErrorCode(rc), QByteArray("Error during mdb_cursor open: ") + QByteArray(mdb_strerror(rc))); + //Invalid arguments can mean that the transaction doesn't contain the db dbi + Error error(d->name.toLatin1() + d->db, getErrorCode(rc), QByteArray("Error during mdb_cursor_open: ") + QByteArray(mdb_strerror(rc)) + ". Key: " + k); errorHandler ? errorHandler(error) : d->defaultErrorHandler(error); return 0; } @@ -459,7 +498,6 @@ public: MDB_env *env; MDB_txn *transaction; - MDB_dbi dbi; bool requestedRead; std::function defaultErrorHandler; QString name; @@ -590,8 +628,7 @@ static bool ensureCorrectDb(DataStore::NamedDatabase &database, const QByteArray } return false; }, - [](const DataStore::Error &error) -> bool{ - return false; + [&](const DataStore::Error &) { }, false); //This is the first time we open this database in a write transaction, write the db name if (!count) { @@ -649,35 +686,8 @@ QList DataStore::Transaction::getDatabaseNames() const SinkWarning() << "Invalid transaction"; return QList(); } + return Sink::Storage::getDatabaseNames(d->transaction); - int rc; - QList list; - Q_ASSERT(d->transaction); - if ((rc = mdb_dbi_open(d->transaction, nullptr, 0, &d->dbi) == 0)) { - MDB_val key; - MDB_val data; - MDB_cursor *cursor; - - mdb_cursor_open(d->transaction, d->dbi, &cursor); - if ((rc = mdb_cursor_get(cursor, &key, &data, MDB_FIRST)) == 0) { - list << QByteArray::fromRawData((char *)key.mv_data, key.mv_size); - while ((rc = mdb_cursor_get(cursor, &key, &data, MDB_NEXT)) == 0) { - list << QByteArray::fromRawData((char *)key.mv_data, key.mv_size); - } - } else { - //Normal if we don't have any databases yet - if (rc == MDB_NOTFOUND) { - rc = 0; - } - if (rc) { - SinkWarning() << "Failed to get a value" << rc; - } - } - mdb_cursor_close(cursor); - } else { - SinkWarning() << "Failed to open db" << rc << QByteArray(mdb_strerror(rc)); - } - return list; } 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 indexLookup(Index &index, QueryBase::Comparator filte for (const auto &lookupKey : lookupKeys) { index.lookup(lookupKey, [&](const QByteArray &value) { keys << value; }, - [lookupKey](const Index::Error &error) { SinkWarning() << "Error in index: " << error.message << lookupKey; }, true); + [lookupKey](const Index::Error &error) { SinkWarning() << "Lookup error in index: " << error.message << lookupKey; }, true); } return keys; } @@ -272,7 +272,7 @@ QVector TypeIndex::secondaryLookup(const QByteArray &lef Index index(indexName(leftName + rightName), *mTransaction); const auto lookupKey = getByteArray(value); index.lookup( - lookupKey, [&](const QByteArray &value) { keys << value; }, [=](const Index::Error &error) { SinkWarning() << "Error in index: " << error.message << value; }); + lookupKey, [&](const QByteArray &value) { keys << value; }, [=](const Index::Error &error) { SinkWarning() << "Lookup error in secondary index: " << error.message << value << lookupKey; }); return keys; } @@ -284,7 +284,7 @@ QVector TypeIndex::secondaryLookup(const QByteArray &leftNa Index index(indexName(leftName + rightName), *mTransaction); const auto lookupKey = getByteArray(value); index.lookup( - lookupKey, [&](const QByteArray &value) { keys << value; }, [=](const Index::Error &error) { SinkWarning() << "Error in index: " << error.message << value; }); + lookupKey, [&](const QByteArray &value) { keys << value; }, [=](const Index::Error &error) { SinkWarning() << "Lookup error in secondary index: " << error.message << value << lookupKey; }); return keys; } -- cgit v1.2.3