summaryrefslogtreecommitdiffstats
path: root/common/storage
diff options
context:
space:
mode:
Diffstat (limited to 'common/storage')
-rw-r--r--common/storage/entitystore.cpp62
-rw-r--r--common/storage/entitystore.h13
-rw-r--r--common/storage/key.cpp47
-rw-r--r--common/storage/key.h17
4 files changed, 105 insertions, 34 deletions
diff --git a/common/storage/entitystore.cpp b/common/storage/entitystore.cpp
index a1f6108..71690fe 100644
--- a/common/storage/entitystore.cpp
+++ b/common/storage/entitystore.cpp
@@ -461,11 +461,11 @@ QVector<QByteArray> EntityStore::indexLookup(const QByteArray &type, const Query
461 return d->typeIndex(type).query(query, appliedFilters, appliedSorting, d->getTransaction(), d->resourceContext.instanceId()); 461 return d->typeIndex(type).query(query, appliedFilters, appliedSorting, d->getTransaction(), d->resourceContext.instanceId());
462} 462}
463 463
464QVector<QByteArray> EntityStore::indexLookup(const QByteArray &type, const QByteArray &property, const QVariant &value) 464QVector<Identifier> EntityStore::indexLookup(const QByteArray &type, const QByteArray &property, const QVariant &value)
465{ 465{
466 if (!d->exists()) { 466 if (!d->exists()) {
467 SinkTraceCtx(d->logCtx) << "Database is not existing: " << type; 467 SinkTraceCtx(d->logCtx) << "Database is not existing: " << type;
468 return QVector<QByteArray>(); 468 return {};
469 } 469 }
470 return d->typeIndex(type).lookup(property, value, d->getTransaction()); 470 return d->typeIndex(type).lookup(property, value, d->getTransaction());
471} 471}
@@ -476,9 +476,9 @@ void EntityStore::indexLookup(const QByteArray &type, const QByteArray &property
476 SinkTraceCtx(d->logCtx) << "Database is not existing: " << type; 476 SinkTraceCtx(d->logCtx) << "Database is not existing: " << type;
477 return; 477 return;
478 } 478 }
479 auto list = d->typeIndex(type).lookup(property, value, d->getTransaction()); 479 auto list = indexLookup(type, property, value);
480 for (const auto &uid : list) { 480 for (const auto &id : list) {
481 callback(uid); 481 callback(id.toDisplayByteArray());
482 } 482 }
483 /* Index index(type + ".index." + property, d->transaction); */ 483 /* Index index(type + ".index." + property, d->transaction); */
484 /* index.lookup(value, [&](const QByteArray &sinkId) { */ 484 /* index.lookup(value, [&](const QByteArray &sinkId) { */
@@ -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 &uid, const std::function<void(const QByteArray &uid, const EntityBuffer &entity)> callback)
506{
507 readLatest(type, Identifier::fromDisplayByteArray(uid), callback);
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,11 @@ 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 readLatest(type, Identifier::fromDisplayByteArray(uid), callback);
534}
535
530ApplicationDomain::ApplicationDomainType EntityStore::readLatest(const QByteArray &type, const QByteArray &uid) 536ApplicationDomain::ApplicationDomainType EntityStore::readLatest(const QByteArray &type, const QByteArray &uid)
531{ 537{
532 ApplicationDomainType dt; 538 ApplicationDomainType dt;
@@ -573,7 +579,7 @@ void EntityStore::readAll(const QByteArray &type, const std::function<void(const
573 }); 579 });
574} 580}
575 581
576void EntityStore::readRevisions(qint64 baseRevision, const QByteArray &expectedType, const std::function<void(const QByteArray &key)> &callback) 582void EntityStore::readRevisions(qint64 baseRevision, const QByteArray &expectedType, const std::function<void(const Key &key)> &callback)
577{ 583{
578 qint64 revisionCounter = baseRevision; 584 qint64 revisionCounter = baseRevision;
579 const qint64 topRevision = DataStore::maxRevision(d->getTransaction()); 585 const qint64 topRevision = DataStore::maxRevision(d->getTransaction());
@@ -592,15 +598,15 @@ void EntityStore::readRevisions(qint64 baseRevision, const QByteArray &expectedT
592 const auto key = Key(Identifier::fromDisplayByteArray(uid), revisionCounter); 598 const auto key = Key(Identifier::fromDisplayByteArray(uid), revisionCounter);
593 599
594 revisionCounter++; 600 revisionCounter++;
595 callback(key.toDisplayByteArray()); 601 callback(key);
596 } 602 }
597} 603}
598 604
599void EntityStore::readPrevious(const QByteArray &type, const QByteArray &uid, qint64 revision, const std::function<void(const QByteArray &uid, const EntityBuffer &entity)> callback) 605void EntityStore::readPrevious(const QByteArray &type, const Identifier &id, qint64 revision, const std::function<void(const QByteArray &uid, const EntityBuffer &entity)> callback)
600{ 606{
601 auto db = DataStore::mainDatabase(d->getTransaction(), type); 607 auto db = DataStore::mainDatabase(d->getTransaction(), type);
602 qint64 latestRevision = 0; 608 qint64 latestRevision = 0;
603 const auto internalUid = Identifier::fromDisplayByteArray(uid).toInternalByteArray(); 609 const auto internalUid = id.toInternalByteArray();
604 db.scan(internalUid, 610 db.scan(internalUid,
605 [&latestRevision, revision](const QByteArray &key, const QByteArray &) -> bool { 611 [&latestRevision, revision](const QByteArray &key, const QByteArray &) -> bool {
606 const auto foundRevision = Key::fromInternalByteArray(key).revision().toQint64(); 612 const auto foundRevision = Key::fromInternalByteArray(key).revision().toQint64();
@@ -610,21 +616,21 @@ void EntityStore::readPrevious(const QByteArray &type, const QByteArray &uid, qi
610 return true; 616 return true;
611 }, 617 },
612 [&](const DataStore::Error &error) { SinkWarningCtx(d->logCtx) << "Failed to read current value from storage: " << error.message; }, true); 618 [&](const DataStore::Error &error) { SinkWarningCtx(d->logCtx) << "Failed to read current value from storage: " << error.message; }, true);
613 const auto key = Key(Identifier::fromDisplayByteArray(uid), latestRevision); 619 const auto key = Key(id, latestRevision);
614 readEntity(type, key.toDisplayByteArray(), callback); 620 readEntity(type, key.toDisplayByteArray(), callback);
615} 621}
616 622
617void EntityStore::readPrevious(const QByteArray &type, const QByteArray &uid, qint64 revision, const std::function<void(const ApplicationDomainType &)> callback) 623void EntityStore::readPrevious(const QByteArray &type, const Identifier &id, qint64 revision, const std::function<void(const ApplicationDomainType &)> callback)
618{ 624{
619 readPrevious(type, uid, revision, [&](const QByteArray &uid, const EntityBuffer &buffer) { 625 readPrevious(type, id, revision, [&](const QByteArray &uid, const EntityBuffer &buffer) {
620 callback(d->createApplicationDomainType(type, uid, DataStore::maxRevision(d->getTransaction()), buffer)); 626 callback(d->createApplicationDomainType(type, uid, DataStore::maxRevision(d->getTransaction()), buffer));
621 }); 627 });
622} 628}
623 629
624ApplicationDomain::ApplicationDomainType EntityStore::readPrevious(const QByteArray &type, const QByteArray &uid, qint64 revision) 630ApplicationDomain::ApplicationDomainType EntityStore::readPrevious(const QByteArray &type, const Identifier &id, qint64 revision)
625{ 631{
626 ApplicationDomainType dt; 632 ApplicationDomainType dt;
627 readPrevious(type, uid, revision, [&](const ApplicationDomainType &entity) { 633 readPrevious(type, id, revision, [&](const ApplicationDomainType &entity) {
628 dt = *ApplicationDomainType::getInMemoryRepresentation<ApplicationDomainType>(entity, entity.availableProperties()); 634 dt = *ApplicationDomainType::getInMemoryRepresentation<ApplicationDomainType>(entity, entity.availableProperties());
629 }); 635 });
630 return dt; 636 return dt;
diff --git a/common/storage/entitystore.h b/common/storage/entitystore.h
index 1dcd285..619b884 100644
--- a/common/storage/entitystore.h
+++ b/common/storage/entitystore.h
@@ -59,7 +59,7 @@ public:
59 59
60 QVector<QByteArray> fullScan(const QByteArray &type); 60 QVector<QByteArray> fullScan(const QByteArray &type);
61 QVector<QByteArray> indexLookup(const QByteArray &type, const QueryBase &query, QSet<QByteArrayList> &appliedFilters, QByteArray &appliedSorting); 61 QVector<QByteArray> indexLookup(const QByteArray &type, const QueryBase &query, QSet<QByteArrayList> &appliedFilters, QByteArray &appliedSorting);
62 QVector<QByteArray> indexLookup(const QByteArray &type, const QByteArray &property, const QVariant &value); 62 QVector<Sink::Storage::Identifier> indexLookup(const QByteArray &type, const QByteArray &property, const QVariant &value);
63 void indexLookup(const QByteArray &type, const QByteArray &property, const QVariant &value, const std::function<void(const QByteArray &uid)> &callback); 63 void indexLookup(const QByteArray &type, const QByteArray &property, const QVariant &value, const std::function<void(const QByteArray &uid)> &callback);
64 template<typename EntityType, typename PropertyType> 64 template<typename EntityType, typename PropertyType>
65 void indexLookup(const QVariant &value, const std::function<void(const QByteArray &uid)> &callback) { 65 void indexLookup(const QVariant &value, const std::function<void(const QByteArray &uid)> &callback) {
@@ -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
@@ -94,10 +97,10 @@ public:
94 } 97 }
95 98
96 99
97 void readPrevious(const QByteArray &type, const QByteArray &uid, qint64 revision, const std::function<void(const QByteArray &uid, const EntityBuffer &entity)> callback); 100 void readPrevious(const QByteArray &type, const Sink::Storage::Identifier &id, qint64 revision, const std::function<void(const QByteArray &uid, const EntityBuffer &entity)> callback);
98 void readPrevious(const QByteArray &type, const QByteArray &uid, qint64 revision, const std::function<void(const ApplicationDomainType &entity)> callback); 101 void readPrevious(const QByteArray &type, const Sink::Storage::Identifier &id, qint64 revision, const std::function<void(const ApplicationDomainType &entity)> callback);
99 ///Returns a copy 102 ///Returns a copy
100 ApplicationDomainType readPrevious(const QByteArray &type, const QByteArray &uid, qint64 revision); 103 ApplicationDomainType readPrevious(const QByteArray &type, const Sink::Storage::Identifier &id, qint64 revision);
101 104
102 template<typename T> 105 template<typename T>
103 T readPrevious(const QByteArray &uid, qint64 revision) { 106 T readPrevious(const QByteArray &uid, qint64 revision) {
@@ -115,7 +118,7 @@ public:
115 }); 118 });
116 } 119 }
117 120
118 void readRevisions(qint64 baseRevision, const QByteArray &type, const std::function<void(const QByteArray &key)> &callback); 121 void readRevisions(qint64 baseRevision, const QByteArray &type, const std::function<void(const Key &key)> &callback);
119 122
120 ///Db contains entity (but may already be marked as removed 123 ///Db contains entity (but may already be marked as removed
121 bool contains(const QByteArray &type, const QByteArray &uid); 124 bool contains(const QByteArray &type, const QByteArray &uid);
diff --git a/common/storage/key.cpp b/common/storage/key.cpp
index 01a3e3a..cfeb016 100644
--- a/common/storage/key.cpp
+++ b/common/storage/key.cpp
@@ -46,8 +46,14 @@ QDebug &operator<<(QDebug &dbg, const Key &key)
46 46
47// Identifier 47// Identifier
48 48
49Identifier Identifier::createIdentifier()
50{
51 return Identifier(QUuid::createUuid());
52}
53
49QByteArray Identifier::toInternalByteArray() const 54QByteArray Identifier::toInternalByteArray() const
50{ 55{
56 Q_ASSERT(!uid.isNull());
51 return uid.toRfc4122(); 57 return uid.toRfc4122();
52} 58}
53 59
@@ -73,6 +79,21 @@ Identifier Identifier::fromDisplayByteArray(const QByteArray &bytes)
73 return Identifier(QUuid(bytes)); 79 return Identifier(QUuid(bytes));
74} 80}
75 81
82bool Identifier::isNull() const
83{
84 return uid.isNull();
85}
86
87bool Identifier::operator==(const Identifier &other) const
88{
89 return uid == other.uid;
90}
91
92bool Identifier::operator!=(const Identifier &other) const
93{
94 return !(*this == other);
95}
96
76// Revision 97// Revision
77 98
78QByteArray Revision::toInternalByteArray() const 99QByteArray Revision::toInternalByteArray() const
@@ -107,6 +128,16 @@ qint64 Revision::toQint64() const
107 return rev; 128 return rev;
108} 129}
109 130
131bool Revision::operator==(const Revision &other) const
132{
133 return rev == other.rev;
134}
135
136bool Revision::operator!=(const Revision &other) const
137{
138 return !(*this == other);
139}
140
110// Key 141// Key
111 142
112QByteArray Key::toInternalByteArray() const 143QByteArray Key::toInternalByteArray() const
@@ -154,3 +185,19 @@ void Key::setRevision(const Revision &newRev)
154{ 185{
155 rev = newRev; 186 rev = newRev;
156} 187}
188
189bool Key::isNull() const
190{
191 return id.isNull();
192}
193
194bool Key::operator==(const Key &other) const
195{
196 return (id == other.id) && (rev == other.rev);
197}
198
199bool Key::operator!=(const Key &other) const
200{
201 return !(*this == other);
202}
203
diff --git a/common/storage/key.h b/common/storage/key.h
index 76dbd13..a5b92bb 100644
--- a/common/storage/key.h
+++ b/common/storage/key.h
@@ -37,7 +37,8 @@ public:
37 static const constexpr size_t INTERNAL_REPR_SIZE = 16; 37 static const constexpr size_t INTERNAL_REPR_SIZE = 16;
38 static const constexpr size_t DISPLAY_REPR_SIZE = 38; 38 static const constexpr size_t DISPLAY_REPR_SIZE = 38;
39 39
40 Identifier() : uid(QUuid::createUuid()){}; 40 Identifier() = default;
41 static Identifier createIdentifier();
41 42
42 QByteArray toInternalByteArray() const; 43 QByteArray toInternalByteArray() const;
43 static Identifier fromInternalByteArray(const QByteArray &bytes); 44 static Identifier fromInternalByteArray(const QByteArray &bytes);
@@ -45,6 +46,11 @@ public:
45 QByteArray toDisplayByteArray() const; 46 QByteArray toDisplayByteArray() const;
46 static Identifier fromDisplayByteArray(const QByteArray &bytes); 47 static Identifier fromDisplayByteArray(const QByteArray &bytes);
47 48
49 bool isNull() const;
50
51 bool operator==(const Identifier &other) const;
52 bool operator!=(const Identifier &other) const;
53
48private: 54private:
49 explicit Identifier(const QUuid &uid) : uid(uid) {} 55 explicit Identifier(const QUuid &uid) : uid(uid) {}
50 QUuid uid; 56 QUuid uid;
@@ -66,6 +72,9 @@ public:
66 static Revision fromDisplayByteArray(const QByteArray &bytes); 72 static Revision fromDisplayByteArray(const QByteArray &bytes);
67 qint64 toQint64() const; 73 qint64 toQint64() const;
68 74
75 bool operator==(const Revision &other) const;
76 bool operator!=(const Revision &other) const;
77
69private: 78private:
70 qint64 rev; 79 qint64 rev;
71}; 80};
@@ -76,6 +85,7 @@ public:
76 static const constexpr size_t INTERNAL_REPR_SIZE = Identifier::INTERNAL_REPR_SIZE + Revision::INTERNAL_REPR_SIZE; 85 static const constexpr size_t INTERNAL_REPR_SIZE = Identifier::INTERNAL_REPR_SIZE + Revision::INTERNAL_REPR_SIZE;
77 static const constexpr size_t DISPLAY_REPR_SIZE = Identifier::DISPLAY_REPR_SIZE + Revision::DISPLAY_REPR_SIZE; 86 static const constexpr size_t DISPLAY_REPR_SIZE = Identifier::DISPLAY_REPR_SIZE + Revision::DISPLAY_REPR_SIZE;
78 87
88 Key() : id(), rev(0) {}
79 Key(const Identifier &id, const Revision &rev) : id(id), rev(rev) {} 89 Key(const Identifier &id, const Revision &rev) : id(id), rev(rev) {}
80 90
81 QByteArray toInternalByteArray() const; 91 QByteArray toInternalByteArray() const;
@@ -87,6 +97,11 @@ public:
87 const Revision &revision() const; 97 const Revision &revision() const;
88 void setRevision(const Revision &newRev); 98 void setRevision(const Revision &newRev);
89 99
100 bool isNull() const;
101
102 bool operator==(const Key &other) const;
103 bool operator!=(const Key &other) const;
104
90private: 105private:
91 Identifier id; 106 Identifier id;
92 Revision rev; 107 Revision rev;