diff options
Diffstat (limited to 'common/storage/entitystore.cpp')
-rw-r--r-- | common/storage/entitystore.cpp | 73 |
1 files changed, 50 insertions, 23 deletions
diff --git a/common/storage/entitystore.cpp b/common/storage/entitystore.cpp index dd6bbf0..3addf94 100644 --- a/common/storage/entitystore.cpp +++ b/common/storage/entitystore.cpp | |||
@@ -237,8 +237,10 @@ bool EntityStore::add(const QByteArray &type, ApplicationDomainType entity, bool | |||
237 | flatbuffers::FlatBufferBuilder fbb; | 237 | flatbuffers::FlatBufferBuilder fbb; |
238 | d->resourceContext.adaptorFactory(type).createBuffer(entity, fbb, metadataFbb.GetBufferPointer(), metadataFbb.GetSize()); | 238 | d->resourceContext.adaptorFactory(type).createBuffer(entity, fbb, metadataFbb.GetBufferPointer(), metadataFbb.GetSize()); |
239 | 239 | ||
240 | const auto key = Key(Identifier::fromDisplayByteArray(entity.identifier()), newRevision); | ||
241 | |||
240 | DataStore::mainDatabase(d->transaction, type) | 242 | DataStore::mainDatabase(d->transaction, type) |
241 | .write(DataStore::assembleKey(entity.identifier(), newRevision), BufferUtils::extractBuffer(fbb), | 243 | .write(key.toInternalByteArray(), BufferUtils::extractBuffer(fbb), |
242 | [&](const DataStore::Error &error) { SinkWarningCtx(d->logCtx) << "Failed to write entity" << entity.identifier() << newRevision; }); | 244 | [&](const DataStore::Error &error) { SinkWarningCtx(d->logCtx) << "Failed to write entity" << entity.identifier() << newRevision; }); |
243 | DataStore::setMaxRevision(d->transaction, newRevision); | 245 | DataStore::setMaxRevision(d->transaction, newRevision); |
244 | DataStore::recordRevision(d->transaction, newRevision, entity.identifier(), type); | 246 | DataStore::recordRevision(d->transaction, newRevision, entity.identifier(), type); |
@@ -311,8 +313,10 @@ bool EntityStore::modify(const QByteArray &type, const ApplicationDomainType &cu | |||
311 | flatbuffers::FlatBufferBuilder fbb; | 313 | flatbuffers::FlatBufferBuilder fbb; |
312 | d->resourceContext.adaptorFactory(type).createBuffer(newEntity, fbb, metadataFbb.GetBufferPointer(), metadataFbb.GetSize()); | 314 | d->resourceContext.adaptorFactory(type).createBuffer(newEntity, fbb, metadataFbb.GetBufferPointer(), metadataFbb.GetSize()); |
313 | 315 | ||
316 | const auto key = Key(Identifier::fromDisplayByteArray(newEntity.identifier()), newRevision); | ||
317 | |||
314 | DataStore::mainDatabase(d->transaction, type) | 318 | DataStore::mainDatabase(d->transaction, type) |
315 | .write(DataStore::assembleKey(newEntity.identifier(), newRevision), BufferUtils::extractBuffer(fbb), | 319 | .write(key.toInternalByteArray(), BufferUtils::extractBuffer(fbb), |
316 | [&](const DataStore::Error &error) { SinkWarningCtx(d->logCtx) << "Failed to write entity" << newEntity.identifier() << newRevision; }); | 320 | [&](const DataStore::Error &error) { SinkWarningCtx(d->logCtx) << "Failed to write entity" << newEntity.identifier() << newRevision; }); |
317 | DataStore::setMaxRevision(d->transaction, newRevision); | 321 | DataStore::setMaxRevision(d->transaction, newRevision); |
318 | DataStore::recordRevision(d->transaction, newRevision, newEntity.identifier(), type); | 322 | DataStore::recordRevision(d->transaction, newRevision, newEntity.identifier(), type); |
@@ -346,8 +350,10 @@ bool EntityStore::remove(const QByteArray &type, const ApplicationDomainType &cu | |||
346 | flatbuffers::FlatBufferBuilder fbb; | 350 | flatbuffers::FlatBufferBuilder fbb; |
347 | EntityBuffer::assembleEntityBuffer(fbb, metadataFbb.GetBufferPointer(), metadataFbb.GetSize(), 0, 0, 0, 0); | 351 | EntityBuffer::assembleEntityBuffer(fbb, metadataFbb.GetBufferPointer(), metadataFbb.GetSize(), 0, 0, 0, 0); |
348 | 352 | ||
353 | const auto key = Key(Identifier::fromDisplayByteArray(uid), newRevision); | ||
354 | |||
349 | DataStore::mainDatabase(d->transaction, type) | 355 | DataStore::mainDatabase(d->transaction, type) |
350 | .write(DataStore::assembleKey(uid, newRevision), BufferUtils::extractBuffer(fbb), | 356 | .write(key.toInternalByteArray(), BufferUtils::extractBuffer(fbb), |
351 | [&](const DataStore::Error &error) { SinkWarningCtx(d->logCtx) << "Failed to write entity" << uid << newRevision; }); | 357 | [&](const DataStore::Error &error) { SinkWarningCtx(d->logCtx) << "Failed to write entity" << uid << newRevision; }); |
352 | DataStore::setMaxRevision(d->transaction, newRevision); | 358 | DataStore::setMaxRevision(d->transaction, newRevision); |
353 | DataStore::recordRevision(d->transaction, newRevision, uid, type); | 359 | DataStore::recordRevision(d->transaction, newRevision, uid, type); |
@@ -365,8 +371,9 @@ void EntityStore::cleanupEntityRevisionsUntil(qint64 revision) | |||
365 | return; | 371 | return; |
366 | } | 372 | } |
367 | SinkTraceCtx(d->logCtx) << "Cleaning up revision " << revision << uid << bufferType; | 373 | SinkTraceCtx(d->logCtx) << "Cleaning up revision " << revision << uid << bufferType; |
374 | const auto internalUid = Identifier::fromDisplayByteArray(uid).toInternalByteArray(); | ||
368 | DataStore::mainDatabase(d->transaction, bufferType) | 375 | DataStore::mainDatabase(d->transaction, bufferType) |
369 | .scan(uid, | 376 | .scan(internalUid, |
370 | [&](const QByteArray &key, const QByteArray &data) -> bool { | 377 | [&](const QByteArray &key, const QByteArray &data) -> bool { |
371 | EntityBuffer buffer(const_cast<const char *>(data.data()), data.size()); | 378 | EntityBuffer buffer(const_cast<const char *>(data.data()), data.size()); |
372 | if (!buffer.isValid()) { | 379 | if (!buffer.isValid()) { |
@@ -428,7 +435,7 @@ QVector<QByteArray> EntityStore::fullScan(const QByteArray &type) | |||
428 | DataStore::mainDatabase(d->getTransaction(), type) | 435 | DataStore::mainDatabase(d->getTransaction(), type) |
429 | .scan(QByteArray(), | 436 | .scan(QByteArray(), |
430 | [&](const QByteArray &key, const QByteArray &value) -> bool { | 437 | [&](const QByteArray &key, const QByteArray &value) -> bool { |
431 | const auto uid = DataStore::uidFromKey(key); | 438 | const auto uid = Sink::Storage::Key::fromInternalByteArray(key).identifier().toDisplayByteArray(); |
432 | if (keys.contains(uid)) { | 439 | if (keys.contains(uid)) { |
433 | //Not something that should persist if the replay works, so we keep a message for now. | 440 | //Not something that should persist if the replay works, so we keep a message for now. |
434 | SinkTraceCtx(d->logCtx) << "Multiple revisions for key: " << key; | 441 | SinkTraceCtx(d->logCtx) << "Multiple revisions for key: " << key; |
@@ -479,16 +486,26 @@ void EntityStore::indexLookup(const QByteArray &type, const QByteArray &property | |||
479 | /* }); */ | 486 | /* }); */ |
480 | } | 487 | } |
481 | 488 | ||
482 | void EntityStore::readLatest(const QByteArray &type, const QByteArray &uid, const std::function<void(const QByteArray &uid, const EntityBuffer &entity)> callback) | 489 | void EntityStore::readLatest(const QByteArray &type, const QByteArray &key, const std::function<void(const QByteArray &uid, const EntityBuffer &entity)> callback) |
483 | { | 490 | { |
484 | Q_ASSERT(d); | 491 | Q_ASSERT(d); |
485 | Q_ASSERT(!uid.isEmpty()); | 492 | Q_ASSERT(!key.isEmpty()); |
493 | // TODO: we shouldn't pass whole keys to this function | ||
494 | // check the testSingle test from querytest | ||
495 | const auto internalKey = [&key]() { | ||
496 | if(key.size() == Identifier::DISPLAY_REPR_SIZE) { | ||
497 | return Identifier::fromDisplayByteArray(key).toInternalByteArray(); | ||
498 | } else { | ||
499 | return Key::fromDisplayByteArray(key).toInternalByteArray(); | ||
500 | } | ||
501 | }(); | ||
486 | auto db = DataStore::mainDatabase(d->getTransaction(), type); | 502 | auto db = DataStore::mainDatabase(d->getTransaction(), type); |
487 | db.findLatest(uid, | 503 | db.findLatest(internalKey, |
488 | [=](const QByteArray &key, const QByteArray &value) { | 504 | [=](const QByteArray &key, const QByteArray &value) { |
489 | callback(DataStore::uidFromKey(key), Sink::EntityBuffer(value.data(), value.size())); | 505 | const auto uid = Sink::Storage::Key::fromInternalByteArray(key).identifier().toDisplayByteArray(); |
506 | callback(uid, Sink::EntityBuffer(value.data(), value.size())); | ||
490 | }, | 507 | }, |
491 | [&](const DataStore::Error &error) { SinkWarningCtx(d->logCtx) << "Error during readLatest query: " << error.message << uid; }); | 508 | [&](const DataStore::Error &error) { SinkWarningCtx(d->logCtx) << "Error during readLatest query: " << error.message << key; }); |
492 | } | 509 | } |
493 | 510 | ||
494 | void EntityStore::readLatest(const QByteArray &type, const QByteArray &uid, const std::function<void(const ApplicationDomainType &)> callback) | 511 | void EntityStore::readLatest(const QByteArray &type, const QByteArray &uid, const std::function<void(const ApplicationDomainType &)> callback) |
@@ -516,12 +533,14 @@ ApplicationDomain::ApplicationDomainType EntityStore::readLatest(const QByteArra | |||
516 | return dt; | 533 | return dt; |
517 | } | 534 | } |
518 | 535 | ||
519 | void EntityStore::readEntity(const QByteArray &type, const QByteArray &key, const std::function<void(const QByteArray &uid, const EntityBuffer &entity)> callback) | 536 | void EntityStore::readEntity(const QByteArray &type, const QByteArray &displayKey, const std::function<void(const QByteArray &uid, const EntityBuffer &entity)> callback) |
520 | { | 537 | { |
538 | const auto key = Key::fromDisplayByteArray(displayKey); | ||
521 | auto db = DataStore::mainDatabase(d->getTransaction(), type); | 539 | auto db = DataStore::mainDatabase(d->getTransaction(), type); |
522 | db.scan(key, | 540 | db.scan(key.toInternalByteArray(), |
523 | [=](const QByteArray &key, const QByteArray &value) -> bool { | 541 | [=](const QByteArray &key, const QByteArray &value) -> bool { |
524 | callback(DataStore::uidFromKey(key), Sink::EntityBuffer(value.data(), value.size())); | 542 | const auto uid = Sink::Storage::Key::fromInternalByteArray(key).identifier().toDisplayByteArray(); |
543 | callback(uid, Sink::EntityBuffer(value.data(), value.size())); | ||
525 | return false; | 544 | return false; |
526 | }, | 545 | }, |
527 | [&](const DataStore::Error &error) { SinkWarningCtx(d->logCtx) << "Error during readEntity query: " << error.message << key; }); | 546 | [&](const DataStore::Error &error) { SinkWarningCtx(d->logCtx) << "Error during readEntity query: " << error.message << key; }); |
@@ -567,9 +586,10 @@ void EntityStore::readRevisions(qint64 baseRevision, const QByteArray &expectedT | |||
567 | revisionCounter++; | 586 | revisionCounter++; |
568 | continue; | 587 | continue; |
569 | } | 588 | } |
570 | const auto key = DataStore::assembleKey(uid, revisionCounter); | 589 | const auto key = Key(Identifier::fromDisplayByteArray(uid), revisionCounter); |
590 | |||
571 | revisionCounter++; | 591 | revisionCounter++; |
572 | callback(key); | 592 | callback(key.toDisplayByteArray()); |
573 | } | 593 | } |
574 | } | 594 | } |
575 | 595 | ||
@@ -577,16 +597,18 @@ void EntityStore::readPrevious(const QByteArray &type, const QByteArray &uid, qi | |||
577 | { | 597 | { |
578 | auto db = DataStore::mainDatabase(d->getTransaction(), type); | 598 | auto db = DataStore::mainDatabase(d->getTransaction(), type); |
579 | qint64 latestRevision = 0; | 599 | qint64 latestRevision = 0; |
580 | db.scan(uid, | 600 | const auto internalUid = Identifier::fromDisplayByteArray(uid).toInternalByteArray(); |
601 | db.scan(internalUid, | ||
581 | [&latestRevision, revision](const QByteArray &key, const QByteArray &) -> bool { | 602 | [&latestRevision, revision](const QByteArray &key, const QByteArray &) -> bool { |
582 | const auto foundRevision = DataStore::revisionFromKey(key); | 603 | const auto foundRevision = Key::fromInternalByteArray(key).revision().toQint64(); |
583 | if (foundRevision < revision && foundRevision > latestRevision) { | 604 | if (foundRevision < revision && foundRevision > latestRevision) { |
584 | latestRevision = foundRevision; | 605 | latestRevision = foundRevision; |
585 | } | 606 | } |
586 | return true; | 607 | return true; |
587 | }, | 608 | }, |
588 | [&](const DataStore::Error &error) { SinkWarningCtx(d->logCtx) << "Failed to read current value from storage: " << error.message; }, true); | 609 | [&](const DataStore::Error &error) { SinkWarningCtx(d->logCtx) << "Failed to read current value from storage: " << error.message; }, true); |
589 | readEntity(type, DataStore::assembleKey(uid, latestRevision), callback); | 610 | const auto key = Key(Identifier::fromDisplayByteArray(uid), latestRevision); |
611 | readEntity(type, key.toDisplayByteArray(), callback); | ||
590 | } | 612 | } |
591 | 613 | ||
592 | void EntityStore::readPrevious(const QByteArray &type, const QByteArray &uid, qint64 revision, const std::function<void(const ApplicationDomainType &)> callback) | 614 | void EntityStore::readPrevious(const QByteArray &type, const QByteArray &uid, qint64 revision, const std::function<void(const ApplicationDomainType &)> callback) |
@@ -612,15 +634,18 @@ void EntityStore::readAllUids(const QByteArray &type, const std::function<void(c | |||
612 | 634 | ||
613 | bool EntityStore::contains(const QByteArray &type, const QByteArray &uid) | 635 | bool EntityStore::contains(const QByteArray &type, const QByteArray &uid) |
614 | { | 636 | { |
615 | return DataStore::mainDatabase(d->getTransaction(), type).contains(uid); | 637 | Q_ASSERT(!uid.isEmpty()); |
638 | const auto internalUid = Identifier::fromDisplayByteArray(uid).toInternalByteArray(); | ||
639 | return DataStore::mainDatabase(d->getTransaction(), type).contains(internalUid); | ||
616 | } | 640 | } |
617 | 641 | ||
618 | bool EntityStore::exists(const QByteArray &type, const QByteArray &uid) | 642 | bool EntityStore::exists(const QByteArray &type, const QByteArray &uid) |
619 | { | 643 | { |
620 | bool found = false; | 644 | bool found = false; |
621 | bool alreadyRemoved = false; | 645 | bool alreadyRemoved = false; |
646 | const auto internalUid = Identifier::fromDisplayByteArray(uid).toInternalByteArray(); | ||
622 | DataStore::mainDatabase(d->transaction, type) | 647 | DataStore::mainDatabase(d->transaction, type) |
623 | .findLatest(uid, | 648 | .findLatest(internalUid, |
624 | [&found, &alreadyRemoved](const QByteArray &key, const QByteArray &data) { | 649 | [&found, &alreadyRemoved](const QByteArray &key, const QByteArray &data) { |
625 | auto entity = GetEntity(data.data()); | 650 | auto entity = GetEntity(data.data()); |
626 | if (entity && entity->metadata()) { | 651 | if (entity && entity->metadata()) { |
@@ -647,12 +672,14 @@ void EntityStore::readRevisions(const QByteArray &type, const QByteArray &uid, q | |||
647 | { | 672 | { |
648 | Q_ASSERT(d); | 673 | Q_ASSERT(d); |
649 | Q_ASSERT(!uid.isEmpty()); | 674 | Q_ASSERT(!uid.isEmpty()); |
675 | const auto internalUid = Identifier::fromDisplayByteArray(uid).toInternalByteArray(); | ||
650 | DataStore::mainDatabase(d->transaction, type) | 676 | DataStore::mainDatabase(d->transaction, type) |
651 | .scan(uid, | 677 | .scan(internalUid, |
652 | [&](const QByteArray &key, const QByteArray &value) -> bool { | 678 | [&](const QByteArray &key, const QByteArray &value) -> bool { |
653 | const auto revision = DataStore::revisionFromKey(key); | 679 | const auto parsedKey = Key::fromInternalByteArray(key); |
680 | const auto revision = parsedKey.revision().toQint64(); | ||
654 | if (revision >= startingRevision) { | 681 | if (revision >= startingRevision) { |
655 | callback(DataStore::uidFromKey(key), revision, Sink::EntityBuffer(value.data(), value.size())); | 682 | callback(parsedKey.identifier().toDisplayByteArray(), revision, Sink::EntityBuffer(value.data(), value.size())); |
656 | } | 683 | } |
657 | return true; | 684 | return true; |
658 | }, | 685 | }, |