From 77562cdae63e0ec7b09e8ece6af97165ba9e48dd Mon Sep 17 00:00:00 2001 From: Christian Mollekopf Date: Fri, 3 Jun 2016 12:41:07 +0200 Subject: A way to retrieve the last revision during changereplay. --- common/entitystore.cpp | 26 ++++++++++++++++++++++---- 1 file changed, 22 insertions(+), 4 deletions(-) (limited to 'common/entitystore.cpp') 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 } -QSharedPointer EntityStore::getLatest(const Sink::Storage::NamedDatabase &db, const QByteArray &uid, DomainTypeAdaptorFactoryInterface &adaptorFactory) +QSharedPointer EntityStore::getLatest(const Sink::Storage::NamedDatabase &db, const QByteArray &uid, DomainTypeAdaptorFactoryInterface &adaptorFactory, qint64 &retrievedRevision) { QSharedPointer current; db.findLatest(uid, - [¤t, &adaptorFactory](const QByteArray &key, const QByteArray &data) -> bool { + [¤t, &adaptorFactory, &retrievedRevision](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 { Trace() << "Found value " << key; current = adaptorFactory.createAdaptor(buffer.entity()); + retrievedRevision = Sink::Storage::revisionFromKey(key); } return false; }, @@ -46,19 +47,36 @@ QSharedPointer EntityStore::getLatest(co return current; } -QSharedPointer EntityStore::get(const Sink::Storage::NamedDatabase &db, const QByteArray &key, DomainTypeAdaptorFactoryInterface &adaptorFactory) +QSharedPointer EntityStore::get(const Sink::Storage::NamedDatabase &db, const QByteArray &key, DomainTypeAdaptorFactoryInterface &adaptorFactory, qint64 &retrievedRevision) { QSharedPointer current; db.scan(key, - [¤t, &adaptorFactory](const QByteArray &key, const QByteArray &data) -> bool { + [¤t, &adaptorFactory, &retrievedRevision](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()); + retrievedRevision = Sink::Storage::revisionFromKey(key); } return false; }, [](const Sink::Storage::Error &error) { Warning() << "Failed to read current value from storage: " << error.message; }); return current; } + +QSharedPointer EntityStore::getPrevious(const Sink::Storage::NamedDatabase &db, const QByteArray &uid, qint64 revision, DomainTypeAdaptorFactoryInterface &adaptorFactory, qint64 &retrievedRevision) +{ + QSharedPointer current; + qint64 latestRevision = 0; + db.scan(uid, + [¤t, &latestRevision, revision](const QByteArray &key, const QByteArray &) -> bool { + auto foundRevision = Sink::Storage::revisionFromKey(key); + if (foundRevision < revision && foundRevision > latestRevision) { + latestRevision = foundRevision; + } + return true; + }, + [](const Sink::Storage::Error &error) { Warning() << "Failed to read current value from storage: " << error.message; }, true); + return get(db, Sink::Storage::assembleKey(uid, latestRevision), adaptorFactory, retrievedRevision); +} -- cgit v1.2.3