From 1b5499770273913ad26b213406df4208b10e2b3c Mon Sep 17 00:00:00 2001 From: Minijackson Date: Fri, 13 Jul 2018 17:15:53 +0200 Subject: Finish converting DataStoreQuery to new Key API --- common/storage/entitystore.cpp | 39 +++++++++++++++------------------------ common/storage/entitystore.h | 10 +++++----- common/storage/key.h | 3 +++ 3 files changed, 23 insertions(+), 29 deletions(-) (limited to 'common/storage') diff --git a/common/storage/entitystore.cpp b/common/storage/entitystore.cpp index be51c2c..672eea1 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) { */ @@ -530,16 +530,7 @@ void EntityStore::readLatest(const QByteArray &type, const Identifier &uid, cons void EntityStore::readLatest(const QByteArray &type, const QByteArray &uid, const std::function callback) { - // TODO: we shouldn't pass whole keys to this function - // check the testSingle test from querytest - const auto id = [&uid]() { - if(uid.size() == Identifier::DISPLAY_REPR_SIZE) { - return Identifier::fromDisplayByteArray(uid); - } else { - return Key::fromDisplayByteArray(uid).identifier(); - } - }(); - readLatest(type, id, callback); + readLatest(type, Identifier::fromDisplayByteArray(uid), callback); } ApplicationDomain::ApplicationDomainType EntityStore::readLatest(const QByteArray &type, const QByteArray &uid) @@ -588,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()); @@ -607,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(); @@ -625,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 c23a659..f3aa07d 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) { @@ -97,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 &uid, 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) { @@ -118,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.h b/common/storage/key.h index 76dbd13..40f4aca 100644 --- a/common/storage/key.h +++ b/common/storage/key.h @@ -76,6 +76,9 @@ 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; + // Just to be able to store keys into Qt containers + [[deprecated("Don't use the default constructor")]] + Key() : id(), rev(0) {} Key(const Identifier &id, const Revision &rev) : id(id), rev(rev) {} QByteArray toInternalByteArray() const; -- cgit v1.2.3