summaryrefslogtreecommitdiffstats
path: root/common/datastorequery.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'common/datastorequery.cpp')
-rw-r--r--common/datastorequery.cpp66
1 files changed, 37 insertions, 29 deletions
diff --git a/common/datastorequery.cpp b/common/datastorequery.cpp
index aca4bb1..aa315c6 100644
--- a/common/datastorequery.cpp
+++ b/common/datastorequery.cpp
@@ -41,17 +41,20 @@ 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 mIds.reserve(ids.size());
54 for (const auto &id : ids) {
55 mIds.append(Identifier::fromDisplayByteArray(id));
56 }
57 mIt = mIds.constBegin();
55 } 58 }
56 59
57 virtual ~Source(){} 60 virtual ~Source(){}
@@ -63,9 +66,13 @@ class Source : public FilterBase {
63 } 66 }
64 }; 67 };
65 68
66 void add(const QVector<QByteArray> &ids) 69 void add(const QVector<Key> &keys)
67 { 70 {
68 mIncrementalIds = ids; 71 mIncrementalIds.clear();
72 mIncrementalIds.reserve(keys.size());
73 for (const auto &key : keys) {
74 mIncrementalIds.append(key.identifier());
75 }
69 mIncrementalIt = mIncrementalIds.constBegin(); 76 mIncrementalIt = mIncrementalIds.constBegin();
70 } 77 }
71 78
@@ -222,7 +229,7 @@ public:
222 229
223 QSet<QByteArray> mReducedValues; 230 QSet<QByteArray> mReducedValues;
224 QSet<QByteArray> mIncrementallyReducedValues; 231 QSet<QByteArray> mIncrementallyReducedValues;
225 QHash<QByteArray, QByteArray> mSelectedValues; 232 QHash<QByteArray, Identifier> mSelectedValues;
226 QByteArray mReductionProperty; 233 QByteArray mReductionProperty;
227 QByteArray mSelectionProperty; 234 QByteArray mSelectionProperty;
228 QueryBase::Reduce::Selector::Comparator mSelectionComparator; 235 QueryBase::Reduce::Selector::Comparator mSelectionComparator;
@@ -263,7 +270,7 @@ public:
263 } 270 }
264 271
265 struct ReductionResult { 272 struct ReductionResult {
266 QByteArray selection; 273 Identifier selection;
267 QVector<QByteArray> aggregateIds; 274 QVector<QByteArray> aggregateIds;
268 QMap<QByteArray, QVariant> aggregateValues; 275 QMap<QByteArray, QVariant> aggregateValues;
269 }; 276 };
@@ -272,7 +279,7 @@ public:
272 { 279 {
273 QMap<QByteArray, QVariant> aggregateValues; 280 QMap<QByteArray, QVariant> aggregateValues;
274 QVariant selectionResultValue; 281 QVariant selectionResultValue;
275 QByteArray selectionResult; 282 Identifier selectionResult;
276 const auto results = indexLookup(mReductionProperty, reductionValue); 283 const auto results = indexLookup(mReductionProperty, reductionValue);
277 for (auto &aggregator : mAggregators) { 284 for (auto &aggregator : mAggregators) {
278 aggregator.reset(); 285 aggregator.reset();
@@ -284,7 +291,7 @@ public:
284 if (!matchesFilter(entity)) { 291 if (!matchesFilter(entity)) {
285 return; 292 return;
286 } 293 }
287 reducedAndFilteredResults << r; 294 reducedAndFilteredResults << r.toDisplayByteArray();
288 Q_ASSERT(operation != Sink::Operation_Removal); 295 Q_ASSERT(operation != Sink::Operation_Removal);
289 for (auto &aggregator : mAggregators) { 296 for (auto &aggregator : mAggregators) {
290 if (!aggregator.property.isEmpty()) { 297 if (!aggregator.property.isEmpty()) {
@@ -296,7 +303,7 @@ public:
296 auto selectionValue = entity.getProperty(mSelectionProperty); 303 auto selectionValue = entity.getProperty(mSelectionProperty);
297 if (!selectionResultValue.isValid() || compare(selectionValue, selectionResultValue, mSelectionComparator)) { 304 if (!selectionResultValue.isValid() || compare(selectionValue, selectionResultValue, mSelectionComparator)) {
298 selectionResultValue = selectionValue; 305 selectionResultValue = selectionValue;
299 selectionResult = entity.identifier(); 306 selectionResult = Identifier::fromDisplayByteArray(entity.identifier());
300 } 307 }
301 }); 308 });
302 } 309 }
@@ -318,7 +325,8 @@ public:
318 if (v.isNull() && result.operation == Sink::Operation_Removal) { 325 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. 326 //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; 327 QVariant reductionValue;
321 readPrevious(result.entity.identifier(), [&] (const ApplicationDomain::ApplicationDomainType &prev) { 328 const auto id = Identifier::fromDisplayByteArray(result.entity.identifier());
329 readPrevious(id, [&] (const ApplicationDomain::ApplicationDomainType &prev) {
322 Q_ASSERT(result.entity.identifier() == prev.identifier()); 330 Q_ASSERT(result.entity.identifier() == prev.identifier());
323 reductionValue = prev.getProperty(mReductionProperty); 331 reductionValue = prev.getProperty(mReductionProperty);
324 }); 332 });
@@ -341,7 +349,7 @@ public:
341 auto reductionResult = reduceOnValue(reductionValue); 349 auto reductionResult = reduceOnValue(reductionValue);
342 350
343 //This can happen if we get a removal message from a filtered entity and all entites of the reduction are filtered. 351 //This can happen if we get a removal message from a filtered entity and all entites of the reduction are filtered.
344 if (reductionResult.selection.isEmpty()) { 352 if (reductionResult.selection.isNull()) {
345 return; 353 return;
346 } 354 }
347 mSelectedValues.insert(reductionValueBa, reductionResult.selection); 355 mSelectedValues.insert(reductionValueBa, reductionResult.selection);
@@ -362,27 +370,27 @@ public:
362 //If mSelectedValues did not contain the value, oldSelectionResult will be empty.(Happens if entites have been filtered) 370 //If mSelectedValues did not contain the value, oldSelectionResult will be empty.(Happens if entites have been filtered)
363 auto oldSelectionResult = mSelectedValues.take(reductionValueBa); 371 auto oldSelectionResult = mSelectedValues.take(reductionValueBa);
364 SinkTraceCtx(mDatastore->mLogCtx) << "Old selection result: " << oldSelectionResult << " New selection result: " << selectionResult.selection; 372 SinkTraceCtx(mDatastore->mLogCtx) << "Old selection result: " << oldSelectionResult << " New selection result: " << selectionResult.selection;
365 if (selectionResult.selection.isEmpty() && oldSelectionResult.isEmpty()) { 373 if (selectionResult.selection.isNull() && oldSelectionResult.isNull()) {
366 //Nothing to do, the item was filtered before, and still is. 374 //Nothing to do, the item was filtered before, and still is.
367 } else if (oldSelectionResult == selectionResult.selection) { 375 } else if (oldSelectionResult == selectionResult.selection) {
368 mSelectedValues.insert(reductionValueBa, selectionResult.selection); 376 mSelectedValues.insert(reductionValueBa, selectionResult.selection);
369 Q_ASSERT(!selectionResult.selection.isEmpty()); 377 Q_ASSERT(!selectionResult.selection.isNull());
370 readEntity(selectionResult.selection, [&](const Sink::ApplicationDomain::ApplicationDomainType &entity, Sink::Operation) { 378 readEntity(selectionResult.selection, [&](const Sink::ApplicationDomain::ApplicationDomainType &entity, Sink::Operation) {
371 callback({entity, Sink::Operation_Modification, selectionResult.aggregateValues, selectionResult.aggregateIds}); 379 callback({entity, Sink::Operation_Modification, selectionResult.aggregateValues, selectionResult.aggregateIds});
372 }); 380 });
373 } else { 381 } else {
374 //remove old result 382 //remove old result
375 if (!oldSelectionResult.isEmpty()) { 383 if (!oldSelectionResult.isNull()) {
376 readEntity(oldSelectionResult, [&](const Sink::ApplicationDomain::ApplicationDomainType &entity, Sink::Operation) { 384 readEntity(oldSelectionResult, [&](const Sink::ApplicationDomain::ApplicationDomainType &entity, Sink::Operation) {
377 callback({entity, Sink::Operation_Removal}); 385 callback({entity, Sink::Operation_Removal});
378 }); 386 });
379 } 387 }
380 388
381 //If the last item has been removed, then there's nothing to add 389 //If the last item has been removed, then there's nothing to add
382 if (!selectionResult.selection.isEmpty()) { 390 if (!selectionResult.selection.isNull()) {
383 //add new result 391 //add new result
384 mSelectedValues.insert(reductionValueBa, selectionResult.selection); 392 mSelectedValues.insert(reductionValueBa, selectionResult.selection);
385 Q_ASSERT(!selectionResult.selection.isEmpty()); 393 Q_ASSERT(!selectionResult.selection.isNull());
386 readEntity(selectionResult.selection, [&](const Sink::ApplicationDomain::ApplicationDomainType &entity, Sink::Operation) { 394 readEntity(selectionResult.selection, [&](const Sink::ApplicationDomain::ApplicationDomainType &entity, Sink::Operation) {
387 callback({entity, Sink::Operation_Creation, selectionResult.aggregateValues, selectionResult.aggregateIds}); 395 callback({entity, Sink::Operation_Creation, selectionResult.aggregateValues, selectionResult.aggregateIds});
388 }); 396 });
@@ -477,17 +485,17 @@ DataStoreQuery::State::Ptr DataStoreQuery::getState()
477 return state; 485 return state;
478} 486}
479 487
480void DataStoreQuery::readEntity(const QByteArray &key, const BufferCallback &resultCallback) 488void DataStoreQuery::readEntity(const Identifier &id, const BufferCallback &resultCallback)
481{ 489{
482 mStore.readLatest(mType, key, resultCallback); 490 mStore.readLatest(mType, id, resultCallback);
483} 491}
484 492
485void DataStoreQuery::readPrevious(const QByteArray &key, const std::function<void (const ApplicationDomain::ApplicationDomainType &)> &callback) 493void DataStoreQuery::readPrevious(const Identifier &id, const std::function<void (const ApplicationDomain::ApplicationDomainType &)> &callback)
486{ 494{
487 mStore.readPrevious(mType, key, mStore.maxRevision(), callback); 495 mStore.readPrevious(mType, id, mStore.maxRevision(), callback);
488} 496}
489 497
490QVector<QByteArray> DataStoreQuery::indexLookup(const QByteArray &property, const QVariant &value) 498QVector<Identifier> DataStoreQuery::indexLookup(const QByteArray &property, const QVariant &value)
491{ 499{
492 return mStore.indexLookup(mType, property, value); 500 return mStore.indexLookup(mType, property, value);
493} 501}
@@ -654,10 +662,10 @@ void DataStoreQuery::setupQuery(const Sink::QueryBase &query_)
654 mCollector = Collector::Ptr::create(baseSet, this); 662 mCollector = Collector::Ptr::create(baseSet, this);
655} 663}
656 664
657QVector<QByteArray> DataStoreQuery::loadIncrementalResultSet(qint64 baseRevision) 665QVector<Key> DataStoreQuery::loadIncrementalResultSet(qint64 baseRevision)
658{ 666{
659 QVector<QByteArray> changedKeys; 667 QVector<Key> changedKeys;
660 mStore.readRevisions(baseRevision, mType, [&](const QByteArray &key) { 668 mStore.readRevisions(baseRevision, mType, [&](const Key &key) {
661 changedKeys << key; 669 changedKeys << key;
662 }); 670 });
663 return changedKeys; 671 return changedKeys;