diff options
author | Rémi Nicole <nicole@kolabsystems.com> | 2018-07-27 13:32:40 +0200 |
---|---|---|
committer | Christian Mollekopf <chrigi_1@fastmail.fm> | 2018-07-27 13:47:50 +0200 |
commit | 620c4f551d3de830a516475ad02965695cb25945 (patch) | |
tree | 5839f93daf4718113b8160248cb22db4f0810b15 /common/typeindex.cpp | |
parent | d1838e575baeb6cd08011645609516acbdabd6c8 (diff) | |
download | sink-620c4f551d3de830a516475ad02965695cb25945.tar.gz sink-620c4f551d3de830a516475ad02965695cb25945.zip |
Use Key API in indexes
Summary:
- Only in TypeIndex, not in Index (since we might want to store something other than identifiers as values)
- We might want to do the same in the `SynchronizerStore` for localId ↔ remoteId indexes
Depends on D13735
Some quick benchmarks (against develop and D13735): {F6022279}
Reviewers: cmollekopf
Reviewed By: cmollekopf
Tags: #sink
Differential Revision: https://phabricator.kde.org/D13902
Diffstat (limited to 'common/typeindex.cpp')
-rw-r--r-- | common/typeindex.cpp | 66 |
1 files changed, 42 insertions, 24 deletions
diff --git a/common/typeindex.cpp b/common/typeindex.cpp index 0b78d59..887a146 100644 --- a/common/typeindex.cpp +++ b/common/typeindex.cpp | |||
@@ -27,6 +27,8 @@ | |||
27 | 27 | ||
28 | using namespace Sink; | 28 | using namespace Sink; |
29 | 29 | ||
30 | using Storage::Identifier; | ||
31 | |||
30 | static QByteArray getByteArray(const QVariant &value) | 32 | static QByteArray getByteArray(const QVariant &value) |
31 | { | 33 | { |
32 | if (value.type() == QVariant::DateTime) { | 34 | if (value.type() == QVariant::DateTime) { |
@@ -126,8 +128,8 @@ static void update(TypeIndex::Action action, const QByteArray &indexName, const | |||
126 | 128 | ||
127 | void TypeIndex::addProperty(const QByteArray &property) | 129 | void TypeIndex::addProperty(const QByteArray &property) |
128 | { | 130 | { |
129 | auto indexer = [=](Action action, const QByteArray &identifier, const QVariant &value, Sink::Storage::DataStore::Transaction &transaction) { | 131 | auto indexer = [=](Action action, const Identifier &identifier, const QVariant &value, Sink::Storage::DataStore::Transaction &transaction) { |
130 | update(action, indexName(property), getByteArray(value), identifier, transaction); | 132 | update(action, indexName(property), getByteArray(value), identifier.toInternalByteArray(), transaction); |
131 | }; | 133 | }; |
132 | mIndexer.insert(property, indexer); | 134 | mIndexer.insert(property, indexer); |
133 | mProperties << property; | 135 | mProperties << property; |
@@ -136,9 +138,9 @@ void TypeIndex::addProperty(const QByteArray &property) | |||
136 | template <> | 138 | template <> |
137 | void TypeIndex::addSortedProperty<QDateTime>(const QByteArray &property) | 139 | void TypeIndex::addSortedProperty<QDateTime>(const QByteArray &property) |
138 | { | 140 | { |
139 | auto indexer = [=](Action action, const QByteArray &identifier, const QVariant &value, | 141 | auto indexer = [=](Action action, const Identifier &identifier, const QVariant &value, |
140 | Sink::Storage::DataStore::Transaction &transaction) { | 142 | Sink::Storage::DataStore::Transaction &transaction) { |
141 | update(action, sortedIndexName(property), toSortableByteArray(value), identifier, transaction); | 143 | update(action, sortedIndexName(property), toSortableByteArray(value), identifier.toInternalByteArray(), transaction); |
142 | }; | 144 | }; |
143 | mSortIndexer.insert(property, indexer); | 145 | mSortIndexer.insert(property, indexer); |
144 | mSortedProperties << property; | 146 | mSortedProperties << property; |
@@ -147,10 +149,10 @@ void TypeIndex::addSortedProperty<QDateTime>(const QByteArray &property) | |||
147 | template <> | 149 | template <> |
148 | void TypeIndex::addPropertyWithSorting<QByteArray, QDateTime>(const QByteArray &property, const QByteArray &sortProperty) | 150 | void TypeIndex::addPropertyWithSorting<QByteArray, QDateTime>(const QByteArray &property, const QByteArray &sortProperty) |
149 | { | 151 | { |
150 | auto indexer = [=](Action action, const QByteArray &identifier, const QVariant &value, const QVariant &sortValue, Sink::Storage::DataStore::Transaction &transaction) { | 152 | auto indexer = [=](Action action, const Identifier &identifier, const QVariant &value, const QVariant &sortValue, Sink::Storage::DataStore::Transaction &transaction) { |
151 | const auto date = sortValue.toDateTime(); | 153 | const auto date = sortValue.toDateTime(); |
152 | const auto propertyValue = getByteArray(value); | 154 | const auto propertyValue = getByteArray(value); |
153 | update(action, indexName(property, sortProperty), propertyValue + toSortableByteArray(date), identifier, transaction); | 155 | update(action, indexName(property, sortProperty), propertyValue + toSortableByteArray(date), identifier.toInternalByteArray(), transaction); |
154 | }; | 156 | }; |
155 | mGroupedSortIndexer.insert(property + sortProperty, indexer); | 157 | mGroupedSortIndexer.insert(property + sortProperty, indexer); |
156 | mGroupedSortedProperties.insert(property, sortProperty); | 158 | mGroupedSortedProperties.insert(property, sortProperty); |
@@ -166,7 +168,7 @@ template <> | |||
166 | void TypeIndex::addSampledPeriodIndex<QDateTime, QDateTime>( | 168 | void TypeIndex::addSampledPeriodIndex<QDateTime, QDateTime>( |
167 | const QByteArray &beginProperty, const QByteArray &endProperty) | 169 | const QByteArray &beginProperty, const QByteArray &endProperty) |
168 | { | 170 | { |
169 | auto indexer = [=](Action action, const QByteArray &identifier, const QVariant &begin, | 171 | auto indexer = [=](Action action, const Identifier &identifier, const QVariant &begin, |
170 | const QVariant &end, Sink::Storage::DataStore::Transaction &transaction) { | 172 | const QVariant &end, Sink::Storage::DataStore::Transaction &transaction) { |
171 | SinkTraceCtx(mLogCtx) << "Adding entity to sampled period index"; | 173 | SinkTraceCtx(mLogCtx) << "Adding entity to sampled period index"; |
172 | const auto beginDate = begin.toDateTime(); | 174 | const auto beginDate = begin.toDateTime(); |
@@ -185,10 +187,10 @@ void TypeIndex::addSampledPeriodIndex<QDateTime, QDateTime>( | |||
185 | QByteArray bucketKey = padNumber(bucket); | 187 | QByteArray bucketKey = padNumber(bucket); |
186 | switch (action) { | 188 | switch (action) { |
187 | case TypeIndex::Add: | 189 | case TypeIndex::Add: |
188 | index.add(bucketKey, identifier); | 190 | index.add(bucketKey, identifier.toInternalByteArray()); |
189 | break; | 191 | break; |
190 | case TypeIndex::Remove: | 192 | case TypeIndex::Remove: |
191 | index.remove(bucketKey, identifier); | 193 | index.remove(bucketKey, identifier.toInternalByteArray()); |
192 | break; | 194 | break; |
193 | } | 195 | } |
194 | } | 196 | } |
@@ -198,7 +200,7 @@ void TypeIndex::addSampledPeriodIndex<QDateTime, QDateTime>( | |||
198 | mSampledPeriodIndexer.insert({ beginProperty, endProperty }, indexer); | 200 | mSampledPeriodIndexer.insert({ beginProperty, endProperty }, indexer); |
199 | } | 201 | } |
200 | 202 | ||
201 | void TypeIndex::updateIndex(Action action, const QByteArray &identifier, const Sink::ApplicationDomain::ApplicationDomainType &entity, Sink::Storage::DataStore::Transaction &transaction, const QByteArray &resourceInstanceId) | 203 | void TypeIndex::updateIndex(Action action, const Identifier &identifier, const Sink::ApplicationDomain::ApplicationDomainType &entity, Sink::Storage::DataStore::Transaction &transaction, const QByteArray &resourceInstanceId) |
202 | { | 204 | { |
203 | for (const auto &property : mProperties) { | 205 | for (const auto &property : mProperties) { |
204 | const auto value = entity.getProperty(property); | 206 | const auto value = entity.getProperty(property); |
@@ -239,7 +241,7 @@ void TypeIndex::abortTransaction() | |||
239 | } | 241 | } |
240 | } | 242 | } |
241 | 243 | ||
242 | void TypeIndex::add(const QByteArray &identifier, const Sink::ApplicationDomain::ApplicationDomainType &entity, Sink::Storage::DataStore::Transaction &transaction, const QByteArray &resourceInstanceId) | 244 | void TypeIndex::add(const Identifier &identifier, const Sink::ApplicationDomain::ApplicationDomainType &entity, Sink::Storage::DataStore::Transaction &transaction, const QByteArray &resourceInstanceId) |
243 | { | 245 | { |
244 | updateIndex(Add, identifier, entity, transaction, resourceInstanceId); | 246 | updateIndex(Add, identifier, entity, transaction, resourceInstanceId); |
245 | for (const auto &indexer : mCustomIndexer) { | 247 | for (const auto &indexer : mCustomIndexer) { |
@@ -248,7 +250,7 @@ void TypeIndex::add(const QByteArray &identifier, const Sink::ApplicationDomain: | |||
248 | } | 250 | } |
249 | } | 251 | } |
250 | 252 | ||
251 | void TypeIndex::modify(const QByteArray &identifier, const Sink::ApplicationDomain::ApplicationDomainType &oldEntity, const Sink::ApplicationDomain::ApplicationDomainType &newEntity, Sink::Storage::DataStore::Transaction &transaction, const QByteArray &resourceInstanceId) | 253 | void TypeIndex::modify(const Identifier &identifier, const Sink::ApplicationDomain::ApplicationDomainType &oldEntity, const Sink::ApplicationDomain::ApplicationDomainType &newEntity, Sink::Storage::DataStore::Transaction &transaction, const QByteArray &resourceInstanceId) |
252 | { | 254 | { |
253 | updateIndex(Remove, identifier, oldEntity, transaction, resourceInstanceId); | 255 | updateIndex(Remove, identifier, oldEntity, transaction, resourceInstanceId); |
254 | updateIndex(Add, identifier, newEntity, transaction, resourceInstanceId); | 256 | updateIndex(Add, identifier, newEntity, transaction, resourceInstanceId); |
@@ -258,7 +260,7 @@ void TypeIndex::modify(const QByteArray &identifier, const Sink::ApplicationDoma | |||
258 | } | 260 | } |
259 | } | 261 | } |
260 | 262 | ||
261 | void TypeIndex::remove(const QByteArray &identifier, const Sink::ApplicationDomain::ApplicationDomainType &entity, Sink::Storage::DataStore::Transaction &transaction, const QByteArray &resourceInstanceId) | 263 | void TypeIndex::remove(const Identifier &identifier, const Sink::ApplicationDomain::ApplicationDomainType &entity, Sink::Storage::DataStore::Transaction &transaction, const QByteArray &resourceInstanceId) |
262 | { | 264 | { |
263 | updateIndex(Remove, identifier, entity, transaction, resourceInstanceId); | 265 | updateIndex(Remove, identifier, entity, transaction, resourceInstanceId); |
264 | for (const auto &indexer : mCustomIndexer) { | 266 | for (const auto &indexer : mCustomIndexer) { |
@@ -275,7 +277,7 @@ static QVector<QByteArray> indexLookup(Index &index, QueryBase::Comparator filte | |||
275 | if (filter.comparator == Query::Comparator::Equals) { | 277 | if (filter.comparator == Query::Comparator::Equals) { |
276 | lookupKeys << valueToKey(filter.value); | 278 | lookupKeys << valueToKey(filter.value); |
277 | } else if (filter.comparator == Query::Comparator::In) { | 279 | } else if (filter.comparator == Query::Comparator::In) { |
278 | for(const QVariant &value : filter.value.value<QVariantList>()) { | 280 | for (const QVariant &value : filter.value.value<QVariantList>()) { |
279 | lookupKeys << valueToKey(value); | 281 | lookupKeys << valueToKey(value); |
280 | } | 282 | } |
281 | } else { | 283 | } else { |
@@ -283,7 +285,10 @@ static QVector<QByteArray> indexLookup(Index &index, QueryBase::Comparator filte | |||
283 | } | 285 | } |
284 | 286 | ||
285 | for (const auto &lookupKey : lookupKeys) { | 287 | for (const auto &lookupKey : lookupKeys) { |
286 | index.lookup(lookupKey, [&](const QByteArray &value) { keys << value; }, | 288 | index.lookup(lookupKey, |
289 | [&](const QByteArray &value) { | ||
290 | keys << Identifier::fromInternalByteArray(value).toDisplayByteArray(); | ||
291 | }, | ||
287 | [lookupKey](const Index::Error &error) { | 292 | [lookupKey](const Index::Error &error) { |
288 | SinkWarning() << "Lookup error in index: " << error.message << lookupKey; | 293 | SinkWarning() << "Lookup error in index: " << error.message << lookupKey; |
289 | }, | 294 | }, |
@@ -315,7 +320,10 @@ static QVector<QByteArray> sortedIndexLookup(Index &index, QueryBase::Comparator | |||
315 | upperBound = bounds[1].toByteArray(); | 320 | upperBound = bounds[1].toByteArray(); |
316 | } | 321 | } |
317 | 322 | ||
318 | index.rangeLookup(lowerBound, upperBound, [&](const QByteArray &value) { keys << value; }, | 323 | index.rangeLookup(lowerBound, upperBound, |
324 | [&](const QByteArray &value) { | ||
325 | keys << Identifier::fromInternalByteArray(value).toDisplayByteArray(); | ||
326 | }, | ||
319 | [bounds](const Index::Error &error) { | 327 | [bounds](const Index::Error &error) { |
320 | SinkWarning() << "Lookup error in index:" << error.message | 328 | SinkWarning() << "Lookup error in index:" << error.message |
321 | << "with bounds:" << bounds[0] << bounds[1]; | 329 | << "with bounds:" << bounds[0] << bounds[1]; |
@@ -345,7 +353,7 @@ static QVector<QByteArray> sampledIndexLookup(Index &index, QueryBase::Comparato | |||
345 | 353 | ||
346 | index.rangeLookup(lowerBucket, upperBucket, | 354 | index.rangeLookup(lowerBucket, upperBucket, |
347 | [&](const QByteArray &value) { | 355 | [&](const QByteArray &value) { |
348 | keys << value.data(); | 356 | keys << Identifier::fromInternalByteArray(value).toDisplayByteArray(); |
349 | }, | 357 | }, |
350 | [bounds](const Index::Error &error) { | 358 | [bounds](const Index::Error &error) { |
351 | SinkWarning() << "Lookup error in index:" << error.message | 359 | SinkWarning() << "Lookup error in index:" << error.message |
@@ -419,28 +427,38 @@ QVector<QByteArray> TypeIndex::query(const Sink::QueryBase &query, QSet<QByteArr | |||
419 | return {}; | 427 | return {}; |
420 | } | 428 | } |
421 | 429 | ||
422 | QVector<QByteArray> TypeIndex::lookup(const QByteArray &property, const QVariant &value, Sink::Storage::DataStore::Transaction &transaction) | 430 | QVector<QByteArray> TypeIndex::lookup(const QByteArray &property, const QVariant &value, |
431 | Sink::Storage::DataStore::Transaction &transaction) | ||
423 | { | 432 | { |
424 | SinkTraceCtx(mLogCtx) << "Index lookup on property: " << property << mSecondaryProperties.keys() << mProperties; | 433 | SinkTraceCtx(mLogCtx) << "Index lookup on property: " << property << mSecondaryProperties.keys() << mProperties; |
425 | if (mProperties.contains(property)) { | 434 | if (mProperties.contains(property)) { |
426 | QVector<QByteArray> keys; | 435 | QVector<QByteArray> keys; |
427 | Index index(indexName(property), transaction); | 436 | Index index(indexName(property), transaction); |
428 | const auto lookupKey = getByteArray(value); | 437 | const auto lookupKey = getByteArray(value); |
429 | index.lookup( | 438 | index.lookup(lookupKey, |
430 | lookupKey, [&](const QByteArray &value) { keys << value; }, [property](const Index::Error &error) { SinkWarning() << "Error in index: " << error.message << property; }); | 439 | [&](const QByteArray &value) { |
440 | keys << Identifier::fromInternalByteArray(value).toDisplayByteArray(); | ||
441 | }, | ||
442 | [property](const Index::Error &error) { | ||
443 | SinkWarning() << "Error in index: " << error.message << property; | ||
444 | }); | ||
431 | SinkTraceCtx(mLogCtx) << "Index lookup on " << property << " found " << keys.size() << " keys."; | 445 | SinkTraceCtx(mLogCtx) << "Index lookup on " << property << " found " << keys.size() << " keys."; |
432 | return keys; | 446 | return keys; |
433 | } else if (mSecondaryProperties.contains(property)) { | 447 | } else if (mSecondaryProperties.contains(property)) { |
434 | //Lookups on secondary indexes first lookup the key, and then lookup the results again to resolve to entity id's | 448 | // Lookups on secondary indexes first lookup the key, and then lookup the results again to |
449 | // resolve to entity id's | ||
435 | QVector<QByteArray> keys; | 450 | QVector<QByteArray> keys; |
436 | auto resultProperty = mSecondaryProperties.value(property); | 451 | auto resultProperty = mSecondaryProperties.value(property); |
437 | 452 | ||
438 | QVector<QByteArray> secondaryKeys; | 453 | QVector<QByteArray> secondaryKeys; |
439 | Index index(indexName(property + resultProperty), transaction); | 454 | Index index(indexName(property + resultProperty), transaction); |
440 | const auto lookupKey = getByteArray(value); | 455 | const auto lookupKey = getByteArray(value); |
441 | index.lookup( | 456 | index.lookup(lookupKey, [&](const QByteArray &value) { secondaryKeys << value; }, |
442 | lookupKey, [&](const QByteArray &value) { secondaryKeys << value; }, [property](const Index::Error &error) { SinkWarning() << "Error in index: " << error.message << property; }); | 457 | [property](const Index::Error &error) { |
443 | SinkTraceCtx(mLogCtx) << "Looked up secondary keys for the following lookup key: " << lookupKey << " => " << secondaryKeys; | 458 | SinkWarning() << "Error in index: " << error.message << property; |
459 | }); | ||
460 | SinkTraceCtx(mLogCtx) << "Looked up secondary keys for the following lookup key: " << lookupKey | ||
461 | << " => " << secondaryKeys; | ||
444 | for (const auto &secondary : secondaryKeys) { | 462 | for (const auto &secondary : secondaryKeys) { |
445 | keys += lookup(resultProperty, secondary, transaction); | 463 | keys += lookup(resultProperty, secondary, transaction); |
446 | } | 464 | } |