diff options
author | Christian Mollekopf <chrigi_1@fastmail.fm> | 2016-06-02 13:32:51 +0200 |
---|---|---|
committer | Christian Mollekopf <chrigi_1@fastmail.fm> | 2016-06-02 13:32:51 +0200 |
commit | c950e43e8f06324c6ba95dffe59ca0e6d6b840a8 (patch) | |
tree | f64f27a9e9a8e5a762bbf64a1029ea3c1a8fe85a | |
parent | c8587bca69546a3a5fd1b2f6c09aff89095a90f8 (diff) | |
download | sink-c950e43e8f06324c6ba95dffe59ca0e6d6b840a8.tar.gz sink-c950e43e8f06324c6ba95dffe59ca0e6d6b840a8.zip |
Reading of individual revisions from the store
-rw-r--r-- | common/entitystore.cpp | 16 | ||||
-rw-r--r-- | common/entitystore.h | 18 |
2 files changed, 33 insertions, 1 deletions
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<Sink::ApplicationDomain::BufferAdaptor> EntityStore::getLatest(co | |||
46 | return current; | 46 | return current; |
47 | } | 47 | } |
48 | 48 | ||
49 | QSharedPointer<Sink::ApplicationDomain::BufferAdaptor> EntityStore::get(const Sink::Storage::NamedDatabase &db, const QByteArray &key, DomainTypeAdaptorFactoryInterface &adaptorFactory) | ||
50 | { | ||
51 | QSharedPointer<Sink::ApplicationDomain::BufferAdaptor> current; | ||
52 | db.scan(key, | ||
53 | [¤t, &adaptorFactory](const QByteArray &key, const QByteArray &data) -> bool { | ||
54 | Sink::EntityBuffer buffer(const_cast<const char *>(data.data()), data.size()); | ||
55 | if (!buffer.isValid()) { | ||
56 | Warning() << "Read invalid buffer from disk"; | ||
57 | } else { | ||
58 | current = adaptorFactory.createAdaptor(buffer.entity()); | ||
59 | } | ||
60 | return false; | ||
61 | }, | ||
62 | [](const Sink::Storage::Error &error) { Warning() << "Failed to read current value from storage: " << error.message; }); | ||
63 | return current; | ||
64 | } | ||
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: | |||
38 | auto typeName = ApplicationDomain::getTypeName<T>(); | 38 | auto typeName = ApplicationDomain::getTypeName<T>(); |
39 | auto mainDatabase = Storage::mainDatabase(mTransaction, typeName); | 39 | auto mainDatabase = Storage::mainDatabase(mTransaction, typeName); |
40 | auto bufferAdaptor = getLatest(mainDatabase, identifier, *Sink::AdaptorFactoryRegistry::instance().getFactory<T>(mResourceType)); | 40 | auto bufferAdaptor = getLatest(mainDatabase, identifier, *Sink::AdaptorFactoryRegistry::instance().getFactory<T>(mResourceType)); |
41 | Q_ASSERT(bufferAdaptor); | 41 | if (!bufferAdaptor) { |
42 | return T(); | ||
43 | } | ||
44 | return T(mResourceInstanceIdentifier, identifier, 0, bufferAdaptor); | ||
45 | } | ||
46 | |||
47 | template<typename T> | ||
48 | T readFromKey(const QByteArray &key) const | ||
49 | { | ||
50 | auto typeName = ApplicationDomain::getTypeName<T>(); | ||
51 | auto mainDatabase = Storage::mainDatabase(mTransaction, typeName); | ||
52 | auto bufferAdaptor = get(mainDatabase, key, *Sink::AdaptorFactoryRegistry::instance().getFactory<T>(mResourceType)); | ||
53 | const auto identifier = Storage::uidFromKey(key); | ||
54 | if (!bufferAdaptor) { | ||
55 | return T(); | ||
56 | } | ||
42 | return T(mResourceInstanceIdentifier, identifier, 0, bufferAdaptor); | 57 | return T(mResourceInstanceIdentifier, identifier, 0, bufferAdaptor); |
43 | } | 58 | } |
44 | 59 | ||
45 | 60 | ||
46 | static QSharedPointer<Sink::ApplicationDomain::BufferAdaptor> getLatest(const Sink::Storage::NamedDatabase &db, const QByteArray &uid, DomainTypeAdaptorFactoryInterface &adaptorFactory); | 61 | static QSharedPointer<Sink::ApplicationDomain::BufferAdaptor> getLatest(const Sink::Storage::NamedDatabase &db, const QByteArray &uid, DomainTypeAdaptorFactoryInterface &adaptorFactory); |
62 | static QSharedPointer<Sink::ApplicationDomain::BufferAdaptor> get(const Sink::Storage::NamedDatabase &db, const QByteArray &key, DomainTypeAdaptorFactoryInterface &adaptorFactory); | ||
47 | private: | 63 | private: |
48 | QByteArray mResourceType; | 64 | QByteArray mResourceType; |
49 | QByteArray mResourceInstanceIdentifier; | 65 | QByteArray mResourceInstanceIdentifier; |