From 0a9b39a1f58c1f5b1a424acbe369db520a12df42 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9mi=20Nicole?= Date: Fri, 27 Jul 2018 13:37:32 +0200 Subject: Use Key API in DataStoreQuery Reviewers: cmollekopf Reviewed By: cmollekopf Tags: #sink Differential Revision: https://phabricator.kde.org/D14099 --- common/storage/entitystore.cpp | 62 +++++++++++++++++++++++------------------- common/storage/entitystore.h | 13 +++++---- common/storage/key.cpp | 47 ++++++++++++++++++++++++++++++++ common/storage/key.h | 17 +++++++++++- 4 files changed, 105 insertions(+), 34 deletions(-) (limited to 'common/storage') diff --git a/common/storage/entitystore.cpp b/common/storage/entitystore.cpp index a1f6108..71690fe 100644 --- a/common/storage/entitystore.cpp +++ b/common/storage/entitystore.cpp @@ -461,11 +461,11 @@ QVector EntityStore::indexLookup(const QByteArray &type, const Query return d->typeIndex(type).query(query, appliedFilters, appliedSorting, d->getTransaction(), d->resourceContext.instanceId()); } -QVector EntityStore::indexLookup(const QByteArray &type, const QByteArray &property, const QVariant &value) +QVector EntityStore::indexLookup(const QByteArray &type, const QByteArray &property, const QVariant &value) { if (!d->exists()) { SinkTraceCtx(d->logCtx) << "Database is not existing: " << type; - return QVector(); + return {}; } return d->typeIndex(type).lookup(property, value, d->getTransaction()); } @@ -476,9 +476,9 @@ void EntityStore::indexLookup(const QByteArray &type, const QByteArray &property SinkTraceCtx(d->logCtx) << "Database is not existing: " << type; return; } - auto list = d->typeIndex(type).lookup(property, value, d->getTransaction()); - for (const auto &uid : list) { - callback(uid); + auto list = indexLookup(type, property, value); + for (const auto &id : list) { + callback(id.toDisplayByteArray()); } /* Index index(type + ".index." + property, d->transaction); */ /* index.lookup(value, [&](const QByteArray &sinkId) { */ @@ -489,29 +489,25 @@ void EntityStore::indexLookup(const QByteArray &type, const QByteArray &property /* }); */ } -void EntityStore::readLatest(const QByteArray &type, const QByteArray &key, const std::function callback) +void EntityStore::readLatest(const QByteArray &type, const Identifier &id, const std::function callback) { Q_ASSERT(d); - Q_ASSERT(!key.isEmpty()); - // TODO: we shouldn't pass whole keys to this function - // check the testSingle test from querytest - const auto internalKey = [&key]() { - if(key.size() == Identifier::DISPLAY_REPR_SIZE) { - return Identifier::fromDisplayByteArray(key).toInternalByteArray(); - } else { - return Key::fromDisplayByteArray(key).toInternalByteArray(); - } - }(); + const auto internalKey = id.toInternalByteArray(); auto db = DataStore::mainDatabase(d->getTransaction(), type); db.findLatest(internalKey, [=](const QByteArray &key, const QByteArray &value) { const auto uid = Sink::Storage::Key::fromInternalByteArray(key).identifier().toDisplayByteArray(); callback(uid, Sink::EntityBuffer(value.data(), value.size())); }, - [&](const DataStore::Error &error) { SinkWarningCtx(d->logCtx) << "Error during readLatest query: " << error.message << key; }); + [&](const DataStore::Error &error) { SinkWarningCtx(d->logCtx) << "Error during readLatest query: " << error.message << id; }); } -void EntityStore::readLatest(const QByteArray &type, const QByteArray &uid, const std::function callback) +void EntityStore::readLatest(const QByteArray &type, const QByteArray &uid, const std::function callback) +{ + readLatest(type, Identifier::fromDisplayByteArray(uid), callback); +} + +void EntityStore::readLatest(const QByteArray &type, const Identifier &uid, const std::function callback) { readLatest(type, uid, [&](const QByteArray &uid, const EntityBuffer &buffer) { //TODO cache max revision for the duration of the transaction. @@ -519,7 +515,12 @@ void EntityStore::readLatest(const QByteArray &type, const QByteArray &uid, cons }); } -void EntityStore::readLatest(const QByteArray &type, const QByteArray &uid, const std::function callback) +void EntityStore::readLatest(const QByteArray &type, const QByteArray &uid, const std::function callback) +{ + readLatest(type, Identifier::fromDisplayByteArray(uid), callback); +} + +void EntityStore::readLatest(const QByteArray &type, const Identifier &uid, const std::function callback) { readLatest(type, uid, [&](const QByteArray &uid, const EntityBuffer &buffer) { //TODO cache max revision for the duration of the transaction. @@ -527,6 +528,11 @@ void EntityStore::readLatest(const QByteArray &type, const QByteArray &uid, cons }); } +void EntityStore::readLatest(const QByteArray &type, const QByteArray &uid, const std::function callback) +{ + readLatest(type, Identifier::fromDisplayByteArray(uid), callback); +} + ApplicationDomain::ApplicationDomainType EntityStore::readLatest(const QByteArray &type, const QByteArray &uid) { ApplicationDomainType dt; @@ -573,7 +579,7 @@ void EntityStore::readAll(const QByteArray &type, const std::function &callback) +void EntityStore::readRevisions(qint64 baseRevision, const QByteArray &expectedType, const std::function &callback) { qint64 revisionCounter = baseRevision; const qint64 topRevision = DataStore::maxRevision(d->getTransaction()); @@ -592,15 +598,15 @@ void EntityStore::readRevisions(qint64 baseRevision, const QByteArray &expectedT const auto key = Key(Identifier::fromDisplayByteArray(uid), revisionCounter); revisionCounter++; - callback(key.toDisplayByteArray()); + callback(key); } } -void EntityStore::readPrevious(const QByteArray &type, const QByteArray &uid, qint64 revision, const std::function callback) +void EntityStore::readPrevious(const QByteArray &type, const Identifier &id, qint64 revision, const std::function callback) { auto db = DataStore::mainDatabase(d->getTransaction(), type); qint64 latestRevision = 0; - const auto internalUid = Identifier::fromDisplayByteArray(uid).toInternalByteArray(); + const auto internalUid = id.toInternalByteArray(); db.scan(internalUid, [&latestRevision, revision](const QByteArray &key, const QByteArray &) -> bool { const auto foundRevision = Key::fromInternalByteArray(key).revision().toQint64(); @@ -610,21 +616,21 @@ void EntityStore::readPrevious(const QByteArray &type, const QByteArray &uid, qi return true; }, [&](const DataStore::Error &error) { SinkWarningCtx(d->logCtx) << "Failed to read current value from storage: " << error.message; }, true); - const auto key = Key(Identifier::fromDisplayByteArray(uid), latestRevision); + const auto key = Key(id, latestRevision); readEntity(type, key.toDisplayByteArray(), callback); } -void EntityStore::readPrevious(const QByteArray &type, const QByteArray &uid, qint64 revision, const std::function callback) +void EntityStore::readPrevious(const QByteArray &type, const Identifier &id, qint64 revision, const std::function callback) { - readPrevious(type, uid, revision, [&](const QByteArray &uid, const EntityBuffer &buffer) { + readPrevious(type, id, revision, [&](const QByteArray &uid, const EntityBuffer &buffer) { callback(d->createApplicationDomainType(type, uid, DataStore::maxRevision(d->getTransaction()), buffer)); }); } -ApplicationDomain::ApplicationDomainType EntityStore::readPrevious(const QByteArray &type, const QByteArray &uid, qint64 revision) +ApplicationDomain::ApplicationDomainType EntityStore::readPrevious(const QByteArray &type, const Identifier &id, qint64 revision) { ApplicationDomainType dt; - readPrevious(type, uid, revision, [&](const ApplicationDomainType &entity) { + readPrevious(type, id, revision, [&](const ApplicationDomainType &entity) { dt = *ApplicationDomainType::getInMemoryRepresentation(entity, entity.availableProperties()); }); return dt; diff --git a/common/storage/entitystore.h b/common/storage/entitystore.h index 1dcd285..619b884 100644 --- a/common/storage/entitystore.h +++ b/common/storage/entitystore.h @@ -59,7 +59,7 @@ public: QVector fullScan(const QByteArray &type); QVector indexLookup(const QByteArray &type, const QueryBase &query, QSet &appliedFilters, QByteArray &appliedSorting); - QVector indexLookup(const QByteArray &type, const QByteArray &property, const QVariant &value); + QVector indexLookup(const QByteArray &type, const QByteArray &property, const QVariant &value); void indexLookup(const QByteArray &type, const QByteArray &property, const QVariant &value, const std::function &callback); template void indexLookup(const QVariant &value, const std::function &callback) { @@ -67,10 +67,13 @@ public: } ///Returns the uid and buffer. Note that the memory only remains valid until the next operation or transaction end. + void readLatest(const QByteArray &type, const Identifier &uid, const std::function callback); void readLatest(const QByteArray &type, const QByteArray &uid, const std::function callback); ///Returns an entity. Note that the memory only remains valid until the next operation or transaction end. + void readLatest(const QByteArray &type, const Identifier &uid, const std::function callback); void readLatest(const QByteArray &type, const QByteArray &uid, const std::function callback); ///Returns an entity and operation. Note that the memory only remains valid until the next operation or transaction end. + void readLatest(const QByteArray &type, const Identifier &uid, const std::function callback); void readLatest(const QByteArray &type, const QByteArray &uid, const std::function callback); ///Returns a copy @@ -94,10 +97,10 @@ public: } - void readPrevious(const QByteArray &type, const QByteArray &uid, qint64 revision, const std::function callback); - void readPrevious(const QByteArray &type, const QByteArray &uid, qint64 revision, const std::function callback); + void readPrevious(const QByteArray &type, const Sink::Storage::Identifier &id, qint64 revision, const std::function callback); + void readPrevious(const QByteArray &type, const Sink::Storage::Identifier &id, qint64 revision, const std::function callback); ///Returns a copy - ApplicationDomainType readPrevious(const QByteArray &type, const QByteArray &uid, qint64 revision); + ApplicationDomainType readPrevious(const QByteArray &type, const Sink::Storage::Identifier &id, qint64 revision); template T readPrevious(const QByteArray &uid, qint64 revision) { @@ -115,7 +118,7 @@ public: }); } - void readRevisions(qint64 baseRevision, const QByteArray &type, const std::function &callback); + void readRevisions(qint64 baseRevision, const QByteArray &type, const std::function &callback); ///Db contains entity (but may already be marked as removed bool contains(const QByteArray &type, const QByteArray &uid); diff --git a/common/storage/key.cpp b/common/storage/key.cpp index 01a3e3a..cfeb016 100644 --- a/common/storage/key.cpp +++ b/common/storage/key.cpp @@ -46,8 +46,14 @@ QDebug &operator<<(QDebug &dbg, const Key &key) // Identifier +Identifier Identifier::createIdentifier() +{ + return Identifier(QUuid::createUuid()); +} + QByteArray Identifier::toInternalByteArray() const { + Q_ASSERT(!uid.isNull()); return uid.toRfc4122(); } @@ -73,6 +79,21 @@ Identifier Identifier::fromDisplayByteArray(const QByteArray &bytes) return Identifier(QUuid(bytes)); } +bool Identifier::isNull() const +{ + return uid.isNull(); +} + +bool Identifier::operator==(const Identifier &other) const +{ + return uid == other.uid; +} + +bool Identifier::operator!=(const Identifier &other) const +{ + return !(*this == other); +} + // Revision QByteArray Revision::toInternalByteArray() const @@ -107,6 +128,16 @@ qint64 Revision::toQint64() const return rev; } +bool Revision::operator==(const Revision &other) const +{ + return rev == other.rev; +} + +bool Revision::operator!=(const Revision &other) const +{ + return !(*this == other); +} + // Key QByteArray Key::toInternalByteArray() const @@ -154,3 +185,19 @@ void Key::setRevision(const Revision &newRev) { rev = newRev; } + +bool Key::isNull() const +{ + return id.isNull(); +} + +bool Key::operator==(const Key &other) const +{ + return (id == other.id) && (rev == other.rev); +} + +bool Key::operator!=(const Key &other) const +{ + return !(*this == other); +} + diff --git a/common/storage/key.h b/common/storage/key.h index 76dbd13..a5b92bb 100644 --- a/common/storage/key.h +++ b/common/storage/key.h @@ -37,7 +37,8 @@ public: static const constexpr size_t INTERNAL_REPR_SIZE = 16; static const constexpr size_t DISPLAY_REPR_SIZE = 38; - Identifier() : uid(QUuid::createUuid()){}; + Identifier() = default; + static Identifier createIdentifier(); QByteArray toInternalByteArray() const; static Identifier fromInternalByteArray(const QByteArray &bytes); @@ -45,6 +46,11 @@ public: QByteArray toDisplayByteArray() const; static Identifier fromDisplayByteArray(const QByteArray &bytes); + bool isNull() const; + + bool operator==(const Identifier &other) const; + bool operator!=(const Identifier &other) const; + private: explicit Identifier(const QUuid &uid) : uid(uid) {} QUuid uid; @@ -66,6 +72,9 @@ public: static Revision fromDisplayByteArray(const QByteArray &bytes); qint64 toQint64() const; + bool operator==(const Revision &other) const; + bool operator!=(const Revision &other) const; + private: qint64 rev; }; @@ -76,6 +85,7 @@ public: static const constexpr size_t INTERNAL_REPR_SIZE = Identifier::INTERNAL_REPR_SIZE + Revision::INTERNAL_REPR_SIZE; static const constexpr size_t DISPLAY_REPR_SIZE = Identifier::DISPLAY_REPR_SIZE + Revision::DISPLAY_REPR_SIZE; + Key() : id(), rev(0) {} Key(const Identifier &id, const Revision &rev) : id(id), rev(rev) {} QByteArray toInternalByteArray() const; @@ -87,6 +97,11 @@ public: const Revision &revision() const; void setRevision(const Revision &newRev); + bool isNull() const; + + bool operator==(const Key &other) const; + bool operator!=(const Key &other) const; + private: Identifier id; Revision rev; -- cgit v1.2.3