From c950e43e8f06324c6ba95dffe59ca0e6d6b840a8 Mon Sep 17 00:00:00 2001 From: Christian Mollekopf Date: Thu, 2 Jun 2016 13:32:51 +0200 Subject: Reading of individual revisions from the store --- common/entitystore.cpp | 16 ++++++++++++++++ common/entitystore.h | 18 +++++++++++++++++- 2 files changed, 33 insertions(+), 1 deletion(-) diff --git a/common/entitystore.cpp b/common/entitystore.cpp index 2c15abf..5f44609 100644 --- a/common/entitystore.cpp +++ b/common/entitystore.cpp @@ -46,3 +46,19 @@ QSharedPointer EntityStore::getLatest(co return current; } +QSharedPointer EntityStore::get(const Sink::Storage::NamedDatabase &db, const QByteArray &key, DomainTypeAdaptorFactoryInterface &adaptorFactory) +{ + QSharedPointer current; + db.scan(key, + [¤t, &adaptorFactory](const QByteArray &key, const QByteArray &data) -> bool { + Sink::EntityBuffer buffer(const_cast(data.data()), data.size()); + if (!buffer.isValid()) { + Warning() << "Read invalid buffer from disk"; + } else { + current = adaptorFactory.createAdaptor(buffer.entity()); + } + return false; + }, + [](const Sink::Storage::Error &error) { Warning() << "Failed to read current value from storage: " << error.message; }); + return current; +} diff --git a/common/entitystore.h b/common/entitystore.h index 17156ec..b6f8713 100644 --- a/common/entitystore.h +++ b/common/entitystore.h @@ -38,12 +38,28 @@ public: auto typeName = ApplicationDomain::getTypeName(); auto mainDatabase = Storage::mainDatabase(mTransaction, typeName); auto bufferAdaptor = getLatest(mainDatabase, identifier, *Sink::AdaptorFactoryRegistry::instance().getFactory(mResourceType)); - Q_ASSERT(bufferAdaptor); + if (!bufferAdaptor) { + return T(); + } + return T(mResourceInstanceIdentifier, identifier, 0, bufferAdaptor); + } + + template + T readFromKey(const QByteArray &key) const + { + auto typeName = ApplicationDomain::getTypeName(); + auto mainDatabase = Storage::mainDatabase(mTransaction, typeName); + auto bufferAdaptor = get(mainDatabase, key, *Sink::AdaptorFactoryRegistry::instance().getFactory(mResourceType)); + const auto identifier = Storage::uidFromKey(key); + if (!bufferAdaptor) { + return T(); + } return T(mResourceInstanceIdentifier, identifier, 0, bufferAdaptor); } static QSharedPointer getLatest(const Sink::Storage::NamedDatabase &db, const QByteArray &uid, DomainTypeAdaptorFactoryInterface &adaptorFactory); + static QSharedPointer get(const Sink::Storage::NamedDatabase &db, const QByteArray &key, DomainTypeAdaptorFactoryInterface &adaptorFactory); private: QByteArray mResourceType; QByteArray mResourceInstanceIdentifier; -- cgit v1.2.3