diff options
Diffstat (limited to 'common/entitystore.cpp')
-rw-r--r-- | common/entitystore.cpp | 26 |
1 files changed, 22 insertions, 4 deletions
diff --git a/common/entitystore.cpp b/common/entitystore.cpp index 5f44609..5296d53 100644 --- a/common/entitystore.cpp +++ b/common/entitystore.cpp | |||
@@ -28,17 +28,18 @@ EntityStore::EntityStore(const QByteArray &resourceType, const QByteArray &resou | |||
28 | 28 | ||
29 | } | 29 | } |
30 | 30 | ||
31 | QSharedPointer<Sink::ApplicationDomain::BufferAdaptor> EntityStore::getLatest(const Sink::Storage::NamedDatabase &db, const QByteArray &uid, DomainTypeAdaptorFactoryInterface &adaptorFactory) | 31 | QSharedPointer<Sink::ApplicationDomain::BufferAdaptor> EntityStore::getLatest(const Sink::Storage::NamedDatabase &db, const QByteArray &uid, DomainTypeAdaptorFactoryInterface &adaptorFactory, qint64 &retrievedRevision) |
32 | { | 32 | { |
33 | QSharedPointer<Sink::ApplicationDomain::BufferAdaptor> current; | 33 | QSharedPointer<Sink::ApplicationDomain::BufferAdaptor> current; |
34 | db.findLatest(uid, | 34 | db.findLatest(uid, |
35 | [¤t, &adaptorFactory](const QByteArray &key, const QByteArray &data) -> bool { | 35 | [¤t, &adaptorFactory, &retrievedRevision](const QByteArray &key, const QByteArray &data) -> bool { |
36 | Sink::EntityBuffer buffer(const_cast<const char *>(data.data()), data.size()); | 36 | Sink::EntityBuffer buffer(const_cast<const char *>(data.data()), data.size()); |
37 | if (!buffer.isValid()) { | 37 | if (!buffer.isValid()) { |
38 | Warning() << "Read invalid buffer from disk"; | 38 | Warning() << "Read invalid buffer from disk"; |
39 | } else { | 39 | } else { |
40 | Trace() << "Found value " << key; | 40 | Trace() << "Found value " << key; |
41 | current = adaptorFactory.createAdaptor(buffer.entity()); | 41 | current = adaptorFactory.createAdaptor(buffer.entity()); |
42 | retrievedRevision = Sink::Storage::revisionFromKey(key); | ||
42 | } | 43 | } |
43 | return false; | 44 | return false; |
44 | }, | 45 | }, |
@@ -46,19 +47,36 @@ QSharedPointer<Sink::ApplicationDomain::BufferAdaptor> EntityStore::getLatest(co | |||
46 | return current; | 47 | return current; |
47 | } | 48 | } |
48 | 49 | ||
49 | QSharedPointer<Sink::ApplicationDomain::BufferAdaptor> EntityStore::get(const Sink::Storage::NamedDatabase &db, const QByteArray &key, DomainTypeAdaptorFactoryInterface &adaptorFactory) | 50 | QSharedPointer<Sink::ApplicationDomain::BufferAdaptor> EntityStore::get(const Sink::Storage::NamedDatabase &db, const QByteArray &key, DomainTypeAdaptorFactoryInterface &adaptorFactory, qint64 &retrievedRevision) |
50 | { | 51 | { |
51 | QSharedPointer<Sink::ApplicationDomain::BufferAdaptor> current; | 52 | QSharedPointer<Sink::ApplicationDomain::BufferAdaptor> current; |
52 | db.scan(key, | 53 | db.scan(key, |
53 | [¤t, &adaptorFactory](const QByteArray &key, const QByteArray &data) -> bool { | 54 | [¤t, &adaptorFactory, &retrievedRevision](const QByteArray &key, const QByteArray &data) -> bool { |
54 | Sink::EntityBuffer buffer(const_cast<const char *>(data.data()), data.size()); | 55 | Sink::EntityBuffer buffer(const_cast<const char *>(data.data()), data.size()); |
55 | if (!buffer.isValid()) { | 56 | if (!buffer.isValid()) { |
56 | Warning() << "Read invalid buffer from disk"; | 57 | Warning() << "Read invalid buffer from disk"; |
57 | } else { | 58 | } else { |
58 | current = adaptorFactory.createAdaptor(buffer.entity()); | 59 | current = adaptorFactory.createAdaptor(buffer.entity()); |
60 | retrievedRevision = Sink::Storage::revisionFromKey(key); | ||
59 | } | 61 | } |
60 | return false; | 62 | return false; |
61 | }, | 63 | }, |
62 | [](const Sink::Storage::Error &error) { Warning() << "Failed to read current value from storage: " << error.message; }); | 64 | [](const Sink::Storage::Error &error) { Warning() << "Failed to read current value from storage: " << error.message; }); |
63 | return current; | 65 | return current; |
64 | } | 66 | } |
67 | |||
68 | QSharedPointer<Sink::ApplicationDomain::BufferAdaptor> EntityStore::getPrevious(const Sink::Storage::NamedDatabase &db, const QByteArray &uid, qint64 revision, DomainTypeAdaptorFactoryInterface &adaptorFactory, qint64 &retrievedRevision) | ||
69 | { | ||
70 | QSharedPointer<Sink::ApplicationDomain::BufferAdaptor> current; | ||
71 | qint64 latestRevision = 0; | ||
72 | db.scan(uid, | ||
73 | [¤t, &latestRevision, revision](const QByteArray &key, const QByteArray &) -> bool { | ||
74 | auto foundRevision = Sink::Storage::revisionFromKey(key); | ||
75 | if (foundRevision < revision && foundRevision > latestRevision) { | ||
76 | latestRevision = foundRevision; | ||
77 | } | ||
78 | return true; | ||
79 | }, | ||
80 | [](const Sink::Storage::Error &error) { Warning() << "Failed to read current value from storage: " << error.message; }, true); | ||
81 | return get(db, Sink::Storage::assembleKey(uid, latestRevision), adaptorFactory, retrievedRevision); | ||
82 | } | ||