summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMinijackson <minijackson@riseup.net>2018-07-13 10:56:47 +0200
committerMinijackson <minijackson@riseup.net>2018-07-13 10:56:47 +0200
commitd0ab46b20b74acef104038cbc5c1b769be863cd3 (patch)
tree2a7caac00f8d3c14635ba9b468bfe9d3c4aed76b
parent17054181f6dc17b90129f75528bae1427176b3aa (diff)
downloadsink-d0ab46b20b74acef104038cbc5c1b769be863cd3.tar.gz
sink-d0ab46b20b74acef104038cbc5c1b769be863cd3.zip
Add versions of readLatest with Identifier as argument in EntityStore
-rw-r--r--common/storage/entitystore.cpp43
-rw-r--r--common/storage/entitystore.h3
2 files changed, 32 insertions, 14 deletions
diff --git a/common/storage/entitystore.cpp b/common/storage/entitystore.cpp
index a1f6108..be51c2c 100644
--- a/common/storage/entitystore.cpp
+++ b/common/storage/entitystore.cpp
@@ -489,29 +489,25 @@ void EntityStore::indexLookup(const QByteArray &type, const QByteArray &property
489 /* }); */ 489 /* }); */
490} 490}
491 491
492void EntityStore::readLatest(const QByteArray &type, const QByteArray &key, const std::function<void(const QByteArray &uid, const EntityBuffer &entity)> callback) 492void EntityStore::readLatest(const QByteArray &type, const Identifier &id, const std::function<void(const QByteArray &uid, const EntityBuffer &entity)> callback)
493{ 493{
494 Q_ASSERT(d); 494 Q_ASSERT(d);
495 Q_ASSERT(!key.isEmpty()); 495 const auto internalKey = id.toInternalByteArray();
496 // TODO: we shouldn't pass whole keys to this function
497 // check the testSingle test from querytest
498 const auto internalKey = [&key]() {
499 if(key.size() == Identifier::DISPLAY_REPR_SIZE) {
500 return Identifier::fromDisplayByteArray(key).toInternalByteArray();
501 } else {
502 return Key::fromDisplayByteArray(key).toInternalByteArray();
503 }
504 }();
505 auto db = DataStore::mainDatabase(d->getTransaction(), type); 496 auto db = DataStore::mainDatabase(d->getTransaction(), type);
506 db.findLatest(internalKey, 497 db.findLatest(internalKey,
507 [=](const QByteArray &key, const QByteArray &value) { 498 [=](const QByteArray &key, const QByteArray &value) {
508 const auto uid = Sink::Storage::Key::fromInternalByteArray(key).identifier().toDisplayByteArray(); 499 const auto uid = Sink::Storage::Key::fromInternalByteArray(key).identifier().toDisplayByteArray();
509 callback(uid, Sink::EntityBuffer(value.data(), value.size())); 500 callback(uid, Sink::EntityBuffer(value.data(), value.size()));
510 }, 501 },
511 [&](const DataStore::Error &error) { SinkWarningCtx(d->logCtx) << "Error during readLatest query: " << error.message << key; }); 502 [&](const DataStore::Error &error) { SinkWarningCtx(d->logCtx) << "Error during readLatest query: " << error.message << id; });
512} 503}
513 504
514void EntityStore::readLatest(const QByteArray &type, const QByteArray &uid, const std::function<void(const ApplicationDomainType &)> callback) 505void EntityStore::readLatest(const QByteArray &type, const QByteArray &key, const std::function<void(const QByteArray &uid, const EntityBuffer &entity)> callback)
506{
507
508}
509
510void EntityStore::readLatest(const QByteArray &type, const Identifier &uid, const std::function<void(const ApplicationDomainType &)> callback)
515{ 511{
516 readLatest(type, uid, [&](const QByteArray &uid, const EntityBuffer &buffer) { 512 readLatest(type, uid, [&](const QByteArray &uid, const EntityBuffer &buffer) {
517 //TODO cache max revision for the duration of the transaction. 513 //TODO cache max revision for the duration of the transaction.
@@ -519,7 +515,12 @@ void EntityStore::readLatest(const QByteArray &type, const QByteArray &uid, cons
519 }); 515 });
520} 516}
521 517
522void EntityStore::readLatest(const QByteArray &type, const QByteArray &uid, const std::function<void(const ApplicationDomainType &, Sink::Operation)> callback) 518void EntityStore::readLatest(const QByteArray &type, const QByteArray &uid, const std::function<void(const ApplicationDomainType &)> callback)
519{
520 readLatest(type, Identifier::fromDisplayByteArray(uid), callback);
521}
522
523void EntityStore::readLatest(const QByteArray &type, const Identifier &uid, const std::function<void(const ApplicationDomainType &, Sink::Operation)> callback)
523{ 524{
524 readLatest(type, uid, [&](const QByteArray &uid, const EntityBuffer &buffer) { 525 readLatest(type, uid, [&](const QByteArray &uid, const EntityBuffer &buffer) {
525 //TODO cache max revision for the duration of the transaction. 526 //TODO cache max revision for the duration of the transaction.
@@ -527,6 +528,20 @@ void EntityStore::readLatest(const QByteArray &type, const QByteArray &uid, cons
527 }); 528 });
528} 529}
529 530
531void EntityStore::readLatest(const QByteArray &type, const QByteArray &uid, const std::function<void(const ApplicationDomainType &, Sink::Operation)> callback)
532{
533 // TODO: we shouldn't pass whole keys to this function
534 // check the testSingle test from querytest
535 const auto id = [&uid]() {
536 if(uid.size() == Identifier::DISPLAY_REPR_SIZE) {
537 return Identifier::fromDisplayByteArray(uid);
538 } else {
539 return Key::fromDisplayByteArray(uid).identifier();
540 }
541 }();
542 readLatest(type, id, callback);
543}
544
530ApplicationDomain::ApplicationDomainType EntityStore::readLatest(const QByteArray &type, const QByteArray &uid) 545ApplicationDomain::ApplicationDomainType EntityStore::readLatest(const QByteArray &type, const QByteArray &uid)
531{ 546{
532 ApplicationDomainType dt; 547 ApplicationDomainType dt;
diff --git a/common/storage/entitystore.h b/common/storage/entitystore.h
index 1dcd285..c23a659 100644
--- a/common/storage/entitystore.h
+++ b/common/storage/entitystore.h
@@ -67,10 +67,13 @@ public:
67 } 67 }
68 68
69 ///Returns the uid and buffer. Note that the memory only remains valid until the next operation or transaction end. 69 ///Returns the uid and buffer. Note that the memory only remains valid until the next operation or transaction end.
70 void readLatest(const QByteArray &type, const Identifier &uid, const std::function<void(const QByteArray &uid, const EntityBuffer &entity)> callback);
70 void readLatest(const QByteArray &type, const QByteArray &uid, const std::function<void(const QByteArray &uid, const EntityBuffer &entity)> callback); 71 void readLatest(const QByteArray &type, const QByteArray &uid, const std::function<void(const QByteArray &uid, const EntityBuffer &entity)> callback);
71 ///Returns an entity. Note that the memory only remains valid until the next operation or transaction end. 72 ///Returns an entity. Note that the memory only remains valid until the next operation or transaction end.
73 void readLatest(const QByteArray &type, const Identifier &uid, const std::function<void(const ApplicationDomainType &entity)> callback);
72 void readLatest(const QByteArray &type, const QByteArray &uid, const std::function<void(const ApplicationDomainType &entity)> callback); 74 void readLatest(const QByteArray &type, const QByteArray &uid, const std::function<void(const ApplicationDomainType &entity)> callback);
73 ///Returns an entity and operation. Note that the memory only remains valid until the next operation or transaction end. 75 ///Returns an entity and operation. Note that the memory only remains valid until the next operation or transaction end.
76 void readLatest(const QByteArray &type, const Identifier &uid, const std::function<void(const ApplicationDomainType &entity, Sink::Operation)> callback);
74 void readLatest(const QByteArray &type, const QByteArray &uid, const std::function<void(const ApplicationDomainType &entity, Sink::Operation)> callback); 77 void readLatest(const QByteArray &type, const QByteArray &uid, const std::function<void(const ApplicationDomainType &entity, Sink::Operation)> callback);
75 78
76 ///Returns a copy 79 ///Returns a copy