From de5bd0955ac50fe17aaf0a02055cc70836b7e4ae Mon Sep 17 00:00:00 2001 From: Minijackson Date: Wed, 22 Aug 2018 13:45:49 +0200 Subject: Fix "contains" function + write tests for contains and exists --- common/storage.h | 1 + common/storage/entitystore.cpp | 5 ++--- common/storage_common.cpp | 12 ++++++++++++ 3 files changed, 15 insertions(+), 3 deletions(-) (limited to 'common') diff --git a/common/storage.h b/common/storage.h index 22d8046..ac6509d 100644 --- a/common/storage.h +++ b/common/storage.h @@ -258,6 +258,7 @@ public: static void recordUid(DataStore::Transaction &transaction, const QByteArray &uid, const QByteArray &type); static void removeUid(DataStore::Transaction &transaction, const QByteArray &uid, const QByteArray &type); static void getUids(const QByteArray &type, const Transaction &, const std::function &); + static bool hasUid(const QByteArray &type, const Transaction &, const QByteArray &uid); bool exists() const; static bool exists(const QString &storageRoot, const QString &name); diff --git a/common/storage/entitystore.cpp b/common/storage/entitystore.cpp index a92de40..454e25a 100644 --- a/common/storage/entitystore.cpp +++ b/common/storage/entitystore.cpp @@ -630,11 +630,10 @@ void EntityStore::readAllUids(const QByteArray &type, const std::functiongetTransaction(), callback); } -bool EntityStore::contains(const QByteArray &type, const QByteArray &uid) +bool EntityStore::contains(const QByteArray & /* type */, const QByteArray &uid) { Q_ASSERT(!uid.isEmpty()); - const auto internalUid = Identifier::fromDisplayByteArray(uid).toInternalByteArray(); - return DataStore::mainDatabase(d->getTransaction(), type).contains(internalUid); + return !DataStore::getRevisionsFromUid(d->getTransaction(), uid).isEmpty(); } bool EntityStore::exists(const QByteArray &type, const QByteArray &uid) diff --git a/common/storage_common.cpp b/common/storage_common.cpp index 09b7707..7c794c3 100644 --- a/common/storage_common.cpp +++ b/common/storage_common.cpp @@ -233,6 +233,18 @@ void DataStore::getUids(const QByteArray &type, const Transaction &transaction, }); } +bool DataStore::hasUid(const QByteArray &type, const Transaction &transaction, const QByteArray &uid) +{ + bool hasTheUid = false; + transaction.openDatabase(type + "uids").scan(uid, [&](const QByteArray &key, const QByteArray &) { + Q_ASSERT(uid == key); + hasTheUid = true; + return false; + }); + + return hasTheUid; +} + bool DataStore::isInternalKey(const char *key) { return key && strncmp(key, s_internalPrefix, s_internalPrefixSize) == 0; -- cgit v1.2.3