diff options
author | Minijackson <minijackson@riseup.net> | 2018-07-04 16:12:58 +0200 |
---|---|---|
committer | Minijackson <minijackson@riseup.net> | 2018-07-04 16:12:58 +0200 |
commit | be09c96b977db014932a3d28b5ee6643ed5eff84 (patch) | |
tree | a460c06d845282c3e7beafc422e695a134c662e0 | |
parent | 456d18b45a825a4d236ccc10c576da1fcbc4f74d (diff) | |
download | sink-be09c96b977db014932a3d28b5ee6643ed5eff84.tar.gz sink-be09c96b977db014932a3d28b5ee6643ed5eff84.zip |
Use key API in indexes
-rw-r--r-- | common/index.cpp | 12 | ||||
-rw-r--r-- | common/index.h | 3 | ||||
-rw-r--r-- | common/storage/entitystore.cpp | 17 | ||||
-rw-r--r-- | common/typeindex.cpp | 66 | ||||
-rw-r--r-- | common/typeindex.h | 17 |
5 files changed, 76 insertions, 39 deletions
diff --git a/common/index.cpp b/common/index.cpp index 86a2dd5..238a745 100644 --- a/common/index.cpp +++ b/common/index.cpp | |||
@@ -2,6 +2,8 @@ | |||
2 | 2 | ||
3 | #include "log.h" | 3 | #include "log.h" |
4 | 4 | ||
5 | using Sink::Storage::Identifier; | ||
6 | |||
5 | Index::Index(const QString &storageRoot, const QString &dbName, const QString &indexName, Sink::Storage::DataStore::AccessMode mode) | 7 | Index::Index(const QString &storageRoot, const QString &dbName, const QString &indexName, Sink::Storage::DataStore::AccessMode mode) |
6 | : mTransaction(Sink::Storage::DataStore(storageRoot, dbName, mode).createTransaction(mode)), | 8 | : mTransaction(Sink::Storage::DataStore(storageRoot, dbName, mode).createTransaction(mode)), |
7 | mDb(mTransaction.openDatabase(indexName.toLatin1(), std::function<void(const Sink::Storage::DataStore::Error &)>(), true)), | 9 | mDb(mTransaction.openDatabase(indexName.toLatin1(), std::function<void(const Sink::Storage::DataStore::Error &)>(), true)), |
@@ -32,6 +34,11 @@ Index::Index(const QByteArray &name, Sink::Storage::DataStore::Transaction &tran | |||
32 | { | 34 | { |
33 | } | 35 | } |
34 | 36 | ||
37 | void Index::add(const Identifier &key, const QByteArray &value) | ||
38 | { | ||
39 | add(key.toInternalByteArray(), value); | ||
40 | } | ||
41 | |||
35 | void Index::add(const QByteArray &key, const QByteArray &value) | 42 | void Index::add(const QByteArray &key, const QByteArray &value) |
36 | { | 43 | { |
37 | Q_ASSERT(!key.isEmpty()); | 44 | Q_ASSERT(!key.isEmpty()); |
@@ -40,6 +47,11 @@ void Index::add(const QByteArray &key, const QByteArray &value) | |||
40 | }); | 47 | }); |
41 | } | 48 | } |
42 | 49 | ||
50 | void Index::remove(const Identifier &key, const QByteArray &value) | ||
51 | { | ||
52 | remove(key.toInternalByteArray(), value); | ||
53 | } | ||
54 | |||
43 | void Index::remove(const QByteArray &key, const QByteArray &value) | 55 | void Index::remove(const QByteArray &key, const QByteArray &value) |
44 | { | 56 | { |
45 | mDb.remove(key, value, [&] (const Sink::Storage::DataStore::Error &error) { | 57 | mDb.remove(key, value, [&] (const Sink::Storage::DataStore::Error &error) { |
diff --git a/common/index.h b/common/index.h index 492319e..833701e 100644 --- a/common/index.h +++ b/common/index.h | |||
@@ -6,6 +6,7 @@ | |||
6 | #include <QString> | 6 | #include <QString> |
7 | #include "storage.h" | 7 | #include "storage.h" |
8 | #include "log.h" | 8 | #include "log.h" |
9 | #include "storage/key.h" | ||
9 | 10 | ||
10 | /** | 11 | /** |
11 | * An index for value pairs. | 12 | * An index for value pairs. |
@@ -35,7 +36,9 @@ public: | |||
35 | Index(const QByteArray &name, Sink::Storage::DataStore::Transaction &); | 36 | Index(const QByteArray &name, Sink::Storage::DataStore::Transaction &); |
36 | 37 | ||
37 | void add(const QByteArray &key, const QByteArray &value); | 38 | void add(const QByteArray &key, const QByteArray &value); |
39 | void add(const Sink::Storage::Identifier &key, const QByteArray &value); | ||
38 | void remove(const QByteArray &key, const QByteArray &value); | 40 | void remove(const QByteArray &key, const QByteArray &value); |
41 | void remove(const Sink::Storage::Identifier &key, const QByteArray &value); | ||
39 | 42 | ||
40 | void lookup(const QByteArray &key, const std::function<void(const QByteArray &value)> &resultHandler, const std::function<void(const Error &error)> &errorHandler, | 43 | void lookup(const QByteArray &key, const std::function<void(const QByteArray &value)> &resultHandler, const std::function<void(const Error &error)> &errorHandler, |
41 | bool matchSubStringKeys = false); | 44 | bool matchSubStringKeys = false); |
diff --git a/common/storage/entitystore.cpp b/common/storage/entitystore.cpp index 3addf94..a1f6108 100644 --- a/common/storage/entitystore.cpp +++ b/common/storage/entitystore.cpp | |||
@@ -220,7 +220,9 @@ bool EntityStore::add(const QByteArray &type, ApplicationDomainType entity, bool | |||
220 | 220 | ||
221 | SinkTraceCtx(d->logCtx) << "New entity " << entity; | 221 | SinkTraceCtx(d->logCtx) << "New entity " << entity; |
222 | 222 | ||
223 | d->typeIndex(type).add(entity.identifier(), entity, d->transaction, d->resourceContext.instanceId()); | 223 | const auto identifier = Identifier::fromDisplayByteArray(entity.identifier()); |
224 | |||
225 | d->typeIndex(type).add(identifier, entity, d->transaction, d->resourceContext.instanceId()); | ||
224 | 226 | ||
225 | //The maxRevision may have changed meanwhile if the entity created sub-entities | 227 | //The maxRevision may have changed meanwhile if the entity created sub-entities |
226 | const qint64 newRevision = maxRevision() + 1; | 228 | const qint64 newRevision = maxRevision() + 1; |
@@ -237,7 +239,7 @@ bool EntityStore::add(const QByteArray &type, ApplicationDomainType entity, bool | |||
237 | flatbuffers::FlatBufferBuilder fbb; | 239 | flatbuffers::FlatBufferBuilder fbb; |
238 | d->resourceContext.adaptorFactory(type).createBuffer(entity, fbb, metadataFbb.GetBufferPointer(), metadataFbb.GetSize()); | 240 | d->resourceContext.adaptorFactory(type).createBuffer(entity, fbb, metadataFbb.GetBufferPointer(), metadataFbb.GetSize()); |
239 | 241 | ||
240 | const auto key = Key(Identifier::fromDisplayByteArray(entity.identifier()), newRevision); | 242 | const auto key = Key(identifier, newRevision); |
241 | 243 | ||
242 | DataStore::mainDatabase(d->transaction, type) | 244 | DataStore::mainDatabase(d->transaction, type) |
243 | .write(key.toInternalByteArray(), BufferUtils::extractBuffer(fbb), | 245 | .write(key.toInternalByteArray(), BufferUtils::extractBuffer(fbb), |
@@ -289,7 +291,8 @@ bool EntityStore::modify(const QByteArray &type, const ApplicationDomainType &cu | |||
289 | { | 291 | { |
290 | SinkTraceCtx(d->logCtx) << "Modified entity: " << newEntity; | 292 | SinkTraceCtx(d->logCtx) << "Modified entity: " << newEntity; |
291 | 293 | ||
292 | d->typeIndex(type).modify(newEntity.identifier(), current, newEntity, d->transaction, d->resourceContext.instanceId()); | 294 | const auto identifier = Identifier::fromDisplayByteArray(newEntity.identifier()); |
295 | d->typeIndex(type).modify(identifier, current, newEntity, d->transaction, d->resourceContext.instanceId()); | ||
293 | 296 | ||
294 | const qint64 newRevision = DataStore::maxRevision(d->transaction) + 1; | 297 | const qint64 newRevision = DataStore::maxRevision(d->transaction) + 1; |
295 | 298 | ||
@@ -313,7 +316,7 @@ bool EntityStore::modify(const QByteArray &type, const ApplicationDomainType &cu | |||
313 | flatbuffers::FlatBufferBuilder fbb; | 316 | flatbuffers::FlatBufferBuilder fbb; |
314 | d->resourceContext.adaptorFactory(type).createBuffer(newEntity, fbb, metadataFbb.GetBufferPointer(), metadataFbb.GetSize()); | 317 | d->resourceContext.adaptorFactory(type).createBuffer(newEntity, fbb, metadataFbb.GetBufferPointer(), metadataFbb.GetSize()); |
315 | 318 | ||
316 | const auto key = Key(Identifier::fromDisplayByteArray(newEntity.identifier()), newRevision); | 319 | const auto key = Key(identifier, newRevision); |
317 | 320 | ||
318 | DataStore::mainDatabase(d->transaction, type) | 321 | DataStore::mainDatabase(d->transaction, type) |
319 | .write(key.toInternalByteArray(), BufferUtils::extractBuffer(fbb), | 322 | .write(key.toInternalByteArray(), BufferUtils::extractBuffer(fbb), |
@@ -331,8 +334,8 @@ bool EntityStore::remove(const QByteArray &type, const ApplicationDomainType &cu | |||
331 | SinkWarningCtx(d->logCtx) << "Remove: Entity is already removed " << uid; | 334 | SinkWarningCtx(d->logCtx) << "Remove: Entity is already removed " << uid; |
332 | return false; | 335 | return false; |
333 | } | 336 | } |
334 | 337 | const auto identifier = Identifier::fromDisplayByteArray(uid); | |
335 | d->typeIndex(type).remove(current.identifier(), current, d->transaction, d->resourceContext.instanceId()); | 338 | d->typeIndex(type).remove(identifier, current, d->transaction, d->resourceContext.instanceId()); |
336 | 339 | ||
337 | SinkTraceCtx(d->logCtx) << "Removed entity " << current; | 340 | SinkTraceCtx(d->logCtx) << "Removed entity " << current; |
338 | 341 | ||
@@ -350,7 +353,7 @@ bool EntityStore::remove(const QByteArray &type, const ApplicationDomainType &cu | |||
350 | flatbuffers::FlatBufferBuilder fbb; | 353 | flatbuffers::FlatBufferBuilder fbb; |
351 | EntityBuffer::assembleEntityBuffer(fbb, metadataFbb.GetBufferPointer(), metadataFbb.GetSize(), 0, 0, 0, 0); | 354 | EntityBuffer::assembleEntityBuffer(fbb, metadataFbb.GetBufferPointer(), metadataFbb.GetSize(), 0, 0, 0, 0); |
352 | 355 | ||
353 | const auto key = Key(Identifier::fromDisplayByteArray(uid), newRevision); | 356 | const auto key = Key(identifier, newRevision); |
354 | 357 | ||
355 | DataStore::mainDatabase(d->transaction, type) | 358 | DataStore::mainDatabase(d->transaction, type) |
356 | .write(key.toInternalByteArray(), BufferUtils::extractBuffer(fbb), | 359 | .write(key.toInternalByteArray(), BufferUtils::extractBuffer(fbb), |
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 | } |
diff --git a/common/typeindex.h b/common/typeindex.h index 4e5a555..a701e9c 100644 --- a/common/typeindex.h +++ b/common/typeindex.h | |||
@@ -23,6 +23,7 @@ | |||
23 | #include "query.h" | 23 | #include "query.h" |
24 | #include "log.h" | 24 | #include "log.h" |
25 | #include "indexer.h" | 25 | #include "indexer.h" |
26 | #include "storage/key.h" | ||
26 | #include <QByteArray> | 27 | #include <QByteArray> |
27 | 28 | ||
28 | namespace Sink { | 29 | namespace Sink { |
@@ -89,9 +90,9 @@ public: | |||
89 | addSampledPeriodIndex<typename Begin::Type, typename End::Type>(Begin::name, End::name); | 90 | addSampledPeriodIndex<typename Begin::Type, typename End::Type>(Begin::name, End::name); |
90 | } | 91 | } |
91 | 92 | ||
92 | void add(const QByteArray &identifier, const Sink::ApplicationDomain::ApplicationDomainType &entity, Sink::Storage::DataStore::Transaction &transaction, const QByteArray &resourceInstanceId); | 93 | void add(const Sink::Storage::Identifier &identifier, const Sink::ApplicationDomain::ApplicationDomainType &entity, Sink::Storage::DataStore::Transaction &transaction, const QByteArray &resourceInstanceId); |
93 | void modify(const QByteArray &identifier, const Sink::ApplicationDomain::ApplicationDomainType &oldEntity, const Sink::ApplicationDomain::ApplicationDomainType &newEntity, Sink::Storage::DataStore::Transaction &transaction, const QByteArray &resourceInstanceId); | 94 | void modify(const Sink::Storage::Identifier &identifier, const Sink::ApplicationDomain::ApplicationDomainType &oldEntity, const Sink::ApplicationDomain::ApplicationDomainType &newEntity, Sink::Storage::DataStore::Transaction &transaction, const QByteArray &resourceInstanceId); |
94 | void remove(const QByteArray &identifier, const Sink::ApplicationDomain::ApplicationDomainType &entity, Sink::Storage::DataStore::Transaction &transaction, const QByteArray &resourceInstanceId); | 95 | void remove(const Sink::Storage::Identifier &identifier, const Sink::ApplicationDomain::ApplicationDomainType &entity, Sink::Storage::DataStore::Transaction &transaction, const QByteArray &resourceInstanceId); |
95 | 96 | ||
96 | QVector<QByteArray> query(const Sink::QueryBase &query, QSet<QByteArrayList> &appliedFilters, QByteArray &appliedSorting, Sink::Storage::DataStore::Transaction &transaction, const QByteArray &resourceInstanceId); | 97 | QVector<QByteArray> query(const Sink::QueryBase &query, QSet<QByteArrayList> &appliedFilters, QByteArray &appliedSorting, Sink::Storage::DataStore::Transaction &transaction, const QByteArray &resourceInstanceId); |
97 | QVector<QByteArray> lookup(const QByteArray &property, const QVariant &value, Sink::Storage::DataStore::Transaction &transaction); | 98 | QVector<QByteArray> lookup(const QByteArray &property, const QVariant &value, Sink::Storage::DataStore::Transaction &transaction); |
@@ -133,7 +134,7 @@ public: | |||
133 | 134 | ||
134 | private: | 135 | private: |
135 | friend class Sink::Storage::EntityStore; | 136 | friend class Sink::Storage::EntityStore; |
136 | void updateIndex(Action action, const QByteArray &identifier, const Sink::ApplicationDomain::ApplicationDomainType &entity, Sink::Storage::DataStore::Transaction &transaction, const QByteArray &resourceInstanceId); | 137 | void updateIndex(Action action, const Sink::Storage::Identifier &identifier, const Sink::ApplicationDomain::ApplicationDomainType &entity, Sink::Storage::DataStore::Transaction &transaction, const QByteArray &resourceInstanceId); |
137 | QByteArray indexName(const QByteArray &property, const QByteArray &sortProperty = QByteArray()) const; | 138 | QByteArray indexName(const QByteArray &property, const QByteArray &sortProperty = QByteArray()) const; |
138 | QByteArray sortedIndexName(const QByteArray &property) const; | 139 | QByteArray sortedIndexName(const QByteArray &property) const; |
139 | QByteArray sampledPeriodIndexName(const QByteArray &rangeBeginProperty, const QByteArray &rangeEndProperty) const; | 140 | QByteArray sampledPeriodIndexName(const QByteArray &rangeBeginProperty, const QByteArray &rangeEndProperty) const; |
@@ -147,8 +148,8 @@ private: | |||
147 | QSet<QPair<QByteArray, QByteArray>> mSampledPeriodProperties; | 148 | QSet<QPair<QByteArray, QByteArray>> mSampledPeriodProperties; |
148 | QList<Sink::Indexer::Ptr> mCustomIndexer; | 149 | QList<Sink::Indexer::Ptr> mCustomIndexer; |
149 | Sink::Storage::DataStore::Transaction *mTransaction; | 150 | Sink::Storage::DataStore::Transaction *mTransaction; |
150 | QHash<QByteArray, std::function<void(Action, const QByteArray &identifier, const QVariant &value, Sink::Storage::DataStore::Transaction &transaction)>> mIndexer; | 151 | QHash<QByteArray, std::function<void(Action, const Sink::Storage::Identifier &identifier, const QVariant &value, Sink::Storage::DataStore::Transaction &transaction)>> mIndexer; |
151 | QHash<QByteArray, std::function<void(Action, const QByteArray &identifier, const QVariant &value, Sink::Storage::DataStore::Transaction &transaction)>> mSortIndexer; | 152 | QHash<QByteArray, std::function<void(Action, const Sink::Storage::Identifier &identifier, const QVariant &value, Sink::Storage::DataStore::Transaction &transaction)>> mSortIndexer; |
152 | QHash<QByteArray, std::function<void(Action, const QByteArray &identifier, const QVariant &value, const QVariant &sortValue, Sink::Storage::DataStore::Transaction &transaction)>> mGroupedSortIndexer; | 153 | QHash<QByteArray, std::function<void(Action, const Sink::Storage::Identifier &identifier, const QVariant &value, const QVariant &sortValue, Sink::Storage::DataStore::Transaction &transaction)>> mGroupedSortIndexer; |
153 | QHash<QPair<QByteArray, QByteArray>, std::function<void(Action, const QByteArray &identifier, const QVariant &begin, const QVariant &end, Sink::Storage::DataStore::Transaction &transaction)>> mSampledPeriodIndexer; | 154 | QHash<QPair<QByteArray, QByteArray>, std::function<void(Action, const Sink::Storage::Identifier &identifier, const QVariant &begin, const QVariant &end, Sink::Storage::DataStore::Transaction &transaction)>> mSampledPeriodIndexer; |
154 | }; | 155 | }; |