summaryrefslogtreecommitdiffstats
path: root/common/datastorequery.cpp
diff options
context:
space:
mode:
authorMinijackson <minijackson@riseup.net>2018-07-13 17:15:53 +0200
committerMinijackson <minijackson@riseup.net>2018-07-13 17:15:53 +0200
commit1b5499770273913ad26b213406df4208b10e2b3c (patch)
tree85e13a65e7c6e702db3e6c433b95e718e7072148 /common/datastorequery.cpp
parentd0ab46b20b74acef104038cbc5c1b769be863cd3 (diff)
downloadsink-1b5499770273913ad26b213406df4208b10e2b3c.tar.gz
sink-1b5499770273913ad26b213406df4208b10e2b3c.zip
Finish converting DataStoreQuery to new Key API
Diffstat (limited to 'common/datastorequery.cpp')
-rw-r--r--common/datastorequery.cpp59
1 files changed, 36 insertions, 23 deletions
diff --git a/common/datastorequery.cpp b/common/datastorequery.cpp
index fd910f8..8bfc49b 100644
--- a/common/datastorequery.cpp
+++ b/common/datastorequery.cpp
@@ -41,17 +41,23 @@ class Source : public FilterBase {
41 public: 41 public:
42 typedef QSharedPointer<Source> Ptr; 42 typedef QSharedPointer<Source> Ptr;
43 43
44 QVector<QByteArray> mIds; 44 QVector<Identifier> mIds;
45 QVector<QByteArray>::ConstIterator mIt; 45 QVector<Identifier>::ConstIterator mIt;
46 QVector<QByteArray> mIncrementalIds; 46 QVector<Identifier> mIncrementalIds;
47 QVector<QByteArray>::ConstIterator mIncrementalIt; 47 QVector<Identifier>::ConstIterator mIncrementalIt;
48 48
49 Source (const QVector<QByteArray> &ids, DataStoreQuery *store) 49 Source (const QVector<QByteArray> &ids, DataStoreQuery *store)
50 : FilterBase(store), 50 : FilterBase(store),
51 mIds(ids), 51 mIds()
52 mIt(mIds.constBegin())
53 { 52 {
54 53 // Use reserve + append instead of QVector constructor with size here
54 // to avoid default construction, which would generate a new random
55 // Uuid
56 mIds.reserve(ids.size());
57 for (const auto &id : ids) {
58 mIds.append(Identifier::fromDisplayByteArray(id));
59 }
60 mIt = mIds.constBegin();
55 } 61 }
56 62
57 virtual ~Source(){} 63 virtual ~Source(){}
@@ -63,9 +69,15 @@ class Source : public FilterBase {
63 } 69 }
64 }; 70 };
65 71
66 void add(const QVector<QByteArray> &ids) 72 void add(const QVector<Key> &keys)
67 { 73 {
68 mIncrementalIds = ids; 74 // Use clear + reserve + append instead of resize here to avoid default
75 // construction
76 mIncrementalIds.clear();
77 mIncrementalIds.reserve(keys.size());
78 for (const auto &key : keys) {
79 mIncrementalIds.append(key.identifier());
80 }
69 mIncrementalIt = mIncrementalIds.constBegin(); 81 mIncrementalIt = mIncrementalIds.constBegin();
70 } 82 }
71 83
@@ -284,7 +296,7 @@ public:
284 if (!matchesFilter(entity)) { 296 if (!matchesFilter(entity)) {
285 return; 297 return;
286 } 298 }
287 reducedAndFilteredResults << r; 299 reducedAndFilteredResults << r.toDisplayByteArray();
288 Q_ASSERT(operation != Sink::Operation_Removal); 300 Q_ASSERT(operation != Sink::Operation_Removal);
289 for (auto &aggregator : mAggregators) { 301 for (auto &aggregator : mAggregators) {
290 if (!aggregator.property.isEmpty()) { 302 if (!aggregator.property.isEmpty()) {
@@ -318,7 +330,8 @@ public:
318 if (v.isNull() && result.operation == Sink::Operation_Removal) { 330 if (v.isNull() && result.operation == Sink::Operation_Removal) {
319 //For removals we have to read the last revision to get a value, and thus be able to find the correct thread. 331 //For removals we have to read the last revision to get a value, and thus be able to find the correct thread.
320 QVariant reductionValue; 332 QVariant reductionValue;
321 readPrevious(result.entity.identifier(), [&] (const ApplicationDomain::ApplicationDomainType &prev) { 333 const auto id = Identifier::fromDisplayByteArray(result.entity.identifier());
334 readPrevious(id, [&] (const ApplicationDomain::ApplicationDomainType &prev) {
322 Q_ASSERT(result.entity.identifier() == prev.identifier()); 335 Q_ASSERT(result.entity.identifier() == prev.identifier());
323 reductionValue = prev.getProperty(mReductionProperty); 336 reductionValue = prev.getProperty(mReductionProperty);
324 }); 337 });
@@ -345,7 +358,7 @@ public:
345 return; 358 return;
346 } 359 }
347 mSelectedValues.insert(reductionValueBa, reductionResult.selection); 360 mSelectedValues.insert(reductionValueBa, reductionResult.selection);
348 readEntity(reductionResult.selection, [&](const Sink::ApplicationDomain::ApplicationDomainType &entity, Sink::Operation operation) { 361 readEntity(Identifier::fromDisplayByteArray(reductionResult.selection), [&](const Sink::ApplicationDomain::ApplicationDomainType &entity, Sink::Operation operation) {
349 callback({entity, operation, reductionResult.aggregateValues, reductionResult.aggregateIds}); 362 callback({entity, operation, reductionResult.aggregateValues, reductionResult.aggregateIds});
350 foundValue = true; 363 foundValue = true;
351 }); 364 });
@@ -368,13 +381,13 @@ public:
368 if (oldSelectionResult == selectionResult.selection) { 381 if (oldSelectionResult == selectionResult.selection) {
369 mSelectedValues.insert(reductionValueBa, selectionResult.selection); 382 mSelectedValues.insert(reductionValueBa, selectionResult.selection);
370 Q_ASSERT(!selectionResult.selection.isEmpty()); 383 Q_ASSERT(!selectionResult.selection.isEmpty());
371 readEntity(selectionResult.selection, [&](const Sink::ApplicationDomain::ApplicationDomainType &entity, Sink::Operation) { 384 readEntity(Identifier::fromDisplayByteArray(selectionResult.selection), [&](const Sink::ApplicationDomain::ApplicationDomainType &entity, Sink::Operation) {
372 callback({entity, Sink::Operation_Modification, selectionResult.aggregateValues, selectionResult.aggregateIds}); 385 callback({entity, Sink::Operation_Modification, selectionResult.aggregateValues, selectionResult.aggregateIds});
373 }); 386 });
374 } else { 387 } else {
375 //remove old result 388 //remove old result
376 Q_ASSERT(!oldSelectionResult.isEmpty()); 389 Q_ASSERT(!oldSelectionResult.isEmpty());
377 readEntity(oldSelectionResult, [&](const Sink::ApplicationDomain::ApplicationDomainType &entity, Sink::Operation) { 390 readEntity(Identifier::fromDisplayByteArray(oldSelectionResult), [&](const Sink::ApplicationDomain::ApplicationDomainType &entity, Sink::Operation) {
378 callback({entity, Sink::Operation_Removal}); 391 callback({entity, Sink::Operation_Removal});
379 }); 392 });
380 393
@@ -383,7 +396,7 @@ public:
383 //add new result 396 //add new result
384 mSelectedValues.insert(reductionValueBa, selectionResult.selection); 397 mSelectedValues.insert(reductionValueBa, selectionResult.selection);
385 Q_ASSERT(!selectionResult.selection.isEmpty()); 398 Q_ASSERT(!selectionResult.selection.isEmpty());
386 readEntity(selectionResult.selection, [&](const Sink::ApplicationDomain::ApplicationDomainType &entity, Sink::Operation) { 399 readEntity(Identifier::fromDisplayByteArray(selectionResult.selection), [&](const Sink::ApplicationDomain::ApplicationDomainType &entity, Sink::Operation) {
387 callback({entity, Sink::Operation_Creation, selectionResult.aggregateValues, selectionResult.aggregateIds}); 400 callback({entity, Sink::Operation_Creation, selectionResult.aggregateValues, selectionResult.aggregateIds});
388 }); 401 });
389 } 402 }
@@ -477,17 +490,17 @@ DataStoreQuery::State::Ptr DataStoreQuery::getState()
477 return state; 490 return state;
478} 491}
479 492
480void DataStoreQuery::readEntity(const QByteArray &key, const BufferCallback &resultCallback) 493void DataStoreQuery::readEntity(const Identifier &id, const BufferCallback &resultCallback)
481{ 494{
482 mStore.readLatest(mType, key, resultCallback); 495 mStore.readLatest(mType, id, resultCallback);
483} 496}
484 497
485void DataStoreQuery::readPrevious(const QByteArray &key, const std::function<void (const ApplicationDomain::ApplicationDomainType &)> &callback) 498void DataStoreQuery::readPrevious(const Identifier &id, const std::function<void (const ApplicationDomain::ApplicationDomainType &)> &callback)
486{ 499{
487 mStore.readPrevious(mType, key, mStore.maxRevision(), callback); 500 mStore.readPrevious(mType, id, mStore.maxRevision(), callback);
488} 501}
489 502
490QVector<QByteArray> DataStoreQuery::indexLookup(const QByteArray &property, const QVariant &value) 503QVector<Identifier> DataStoreQuery::indexLookup(const QByteArray &property, const QVariant &value)
491{ 504{
492 return mStore.indexLookup(mType, property, value); 505 return mStore.indexLookup(mType, property, value);
493} 506}
@@ -654,10 +667,10 @@ void DataStoreQuery::setupQuery(const Sink::QueryBase &query_)
654 mCollector = Collector::Ptr::create(baseSet, this); 667 mCollector = Collector::Ptr::create(baseSet, this);
655} 668}
656 669
657QVector<QByteArray> DataStoreQuery::loadIncrementalResultSet(qint64 baseRevision) 670QVector<Key> DataStoreQuery::loadIncrementalResultSet(qint64 baseRevision)
658{ 671{
659 QVector<QByteArray> changedKeys; 672 QVector<Key> changedKeys;
660 mStore.readRevisions(baseRevision, mType, [&](const QByteArray &key) { 673 mStore.readRevisions(baseRevision, mType, [&](const Key &key) {
661 changedKeys << key; 674 changedKeys << key;
662 }); 675 });
663 return changedKeys; 676 return changedKeys;