diff options
author | Rémi Nicole <nicole@kolabsystems.com> | 2018-07-27 13:37:32 +0200 |
---|---|---|
committer | Christian Mollekopf <chrigi_1@fastmail.fm> | 2018-07-27 13:47:54 +0200 |
commit | 0a9b39a1f58c1f5b1a424acbe369db520a12df42 (patch) | |
tree | 4c64e5680150f50088bf114cbab39f79f781d242 /common/storage/entitystore.cpp | |
parent | 620c4f551d3de830a516475ad02965695cb25945 (diff) | |
download | sink-0a9b39a1f58c1f5b1a424acbe369db520a12df42.tar.gz sink-0a9b39a1f58c1f5b1a424acbe369db520a12df42.zip |
Use Key API in DataStoreQuery
Reviewers: cmollekopf
Reviewed By: cmollekopf
Tags: #sink
Differential Revision: https://phabricator.kde.org/D14099
Diffstat (limited to 'common/storage/entitystore.cpp')
-rw-r--r-- | common/storage/entitystore.cpp | 62 |
1 files changed, 34 insertions, 28 deletions
diff --git a/common/storage/entitystore.cpp b/common/storage/entitystore.cpp index a1f6108..71690fe 100644 --- a/common/storage/entitystore.cpp +++ b/common/storage/entitystore.cpp | |||
@@ -461,11 +461,11 @@ QVector<QByteArray> EntityStore::indexLookup(const QByteArray &type, const Query | |||
461 | return d->typeIndex(type).query(query, appliedFilters, appliedSorting, d->getTransaction(), d->resourceContext.instanceId()); | 461 | return d->typeIndex(type).query(query, appliedFilters, appliedSorting, d->getTransaction(), d->resourceContext.instanceId()); |
462 | } | 462 | } |
463 | 463 | ||
464 | QVector<QByteArray> EntityStore::indexLookup(const QByteArray &type, const QByteArray &property, const QVariant &value) | 464 | QVector<Identifier> EntityStore::indexLookup(const QByteArray &type, const QByteArray &property, const QVariant &value) |
465 | { | 465 | { |
466 | if (!d->exists()) { | 466 | if (!d->exists()) { |
467 | SinkTraceCtx(d->logCtx) << "Database is not existing: " << type; | 467 | SinkTraceCtx(d->logCtx) << "Database is not existing: " << type; |
468 | return QVector<QByteArray>(); | 468 | return {}; |
469 | } | 469 | } |
470 | return d->typeIndex(type).lookup(property, value, d->getTransaction()); | 470 | return d->typeIndex(type).lookup(property, value, d->getTransaction()); |
471 | } | 471 | } |
@@ -476,9 +476,9 @@ void EntityStore::indexLookup(const QByteArray &type, const QByteArray &property | |||
476 | SinkTraceCtx(d->logCtx) << "Database is not existing: " << type; | 476 | SinkTraceCtx(d->logCtx) << "Database is not existing: " << type; |
477 | return; | 477 | return; |
478 | } | 478 | } |
479 | auto list = d->typeIndex(type).lookup(property, value, d->getTransaction()); | 479 | auto list = indexLookup(type, property, value); |
480 | for (const auto &uid : list) { | 480 | for (const auto &id : list) { |
481 | callback(uid); | 481 | callback(id.toDisplayByteArray()); |
482 | } | 482 | } |
483 | /* Index index(type + ".index." + property, d->transaction); */ | 483 | /* Index index(type + ".index." + property, d->transaction); */ |
484 | /* index.lookup(value, [&](const QByteArray &sinkId) { */ | 484 | /* index.lookup(value, [&](const QByteArray &sinkId) { */ |
@@ -489,29 +489,25 @@ void EntityStore::indexLookup(const QByteArray &type, const QByteArray &property | |||
489 | /* }); */ | 489 | /* }); */ |
490 | } | 490 | } |
491 | 491 | ||
492 | void EntityStore::readLatest(const QByteArray &type, const QByteArray &key, const std::function<void(const QByteArray &uid, const EntityBuffer &entity)> callback) | 492 | void EntityStore::readLatest(const QByteArray &type, const Identifier &id, const std::function<void(const QByteArray &uid, const EntityBuffer &entity)> callback) |
493 | { | 493 | { |
494 | Q_ASSERT(d); | 494 | Q_ASSERT(d); |
495 | Q_ASSERT(!key.isEmpty()); | 495 | const auto internalKey = id.toInternalByteArray(); |
496 | // TODO: we shouldn't pass whole keys to this function | ||
497 | // check the testSingle test from querytest | ||
498 | const auto internalKey = [&key]() { | ||
499 | if(key.size() == Identifier::DISPLAY_REPR_SIZE) { | ||
500 | return Identifier::fromDisplayByteArray(key).toInternalByteArray(); | ||
501 | } else { | ||
502 | return Key::fromDisplayByteArray(key).toInternalByteArray(); | ||
503 | } | ||
504 | }(); | ||
505 | auto db = DataStore::mainDatabase(d->getTransaction(), type); | 496 | auto db = DataStore::mainDatabase(d->getTransaction(), type); |
506 | db.findLatest(internalKey, | 497 | db.findLatest(internalKey, |
507 | [=](const QByteArray &key, const QByteArray &value) { | 498 | [=](const QByteArray &key, const QByteArray &value) { |
508 | const auto uid = Sink::Storage::Key::fromInternalByteArray(key).identifier().toDisplayByteArray(); | 499 | const auto uid = Sink::Storage::Key::fromInternalByteArray(key).identifier().toDisplayByteArray(); |
509 | callback(uid, Sink::EntityBuffer(value.data(), value.size())); | 500 | callback(uid, Sink::EntityBuffer(value.data(), value.size())); |
510 | }, | 501 | }, |
511 | [&](const DataStore::Error &error) { SinkWarningCtx(d->logCtx) << "Error during readLatest query: " << error.message << key; }); | 502 | [&](const DataStore::Error &error) { SinkWarningCtx(d->logCtx) << "Error during readLatest query: " << error.message << id; }); |
512 | } | 503 | } |
513 | 504 | ||
514 | void EntityStore::readLatest(const QByteArray &type, const QByteArray &uid, const std::function<void(const ApplicationDomainType &)> callback) | 505 | void EntityStore::readLatest(const QByteArray &type, const QByteArray &uid, const std::function<void(const QByteArray &uid, const EntityBuffer &entity)> callback) |
506 | { | ||
507 | readLatest(type, Identifier::fromDisplayByteArray(uid), callback); | ||
508 | } | ||
509 | |||
510 | void EntityStore::readLatest(const QByteArray &type, const Identifier &uid, const std::function<void(const ApplicationDomainType &)> callback) | ||
515 | { | 511 | { |
516 | readLatest(type, uid, [&](const QByteArray &uid, const EntityBuffer &buffer) { | 512 | readLatest(type, uid, [&](const QByteArray &uid, const EntityBuffer &buffer) { |
517 | //TODO cache max revision for the duration of the transaction. | 513 | //TODO cache max revision for the duration of the transaction. |
@@ -519,7 +515,12 @@ void EntityStore::readLatest(const QByteArray &type, const QByteArray &uid, cons | |||
519 | }); | 515 | }); |
520 | } | 516 | } |
521 | 517 | ||
522 | void EntityStore::readLatest(const QByteArray &type, const QByteArray &uid, const std::function<void(const ApplicationDomainType &, Sink::Operation)> callback) | 518 | void EntityStore::readLatest(const QByteArray &type, const QByteArray &uid, const std::function<void(const ApplicationDomainType &)> callback) |
519 | { | ||
520 | readLatest(type, Identifier::fromDisplayByteArray(uid), callback); | ||
521 | } | ||
522 | |||
523 | void EntityStore::readLatest(const QByteArray &type, const Identifier &uid, const std::function<void(const ApplicationDomainType &, Sink::Operation)> callback) | ||
523 | { | 524 | { |
524 | readLatest(type, uid, [&](const QByteArray &uid, const EntityBuffer &buffer) { | 525 | readLatest(type, uid, [&](const QByteArray &uid, const EntityBuffer &buffer) { |
525 | //TODO cache max revision for the duration of the transaction. | 526 | //TODO cache max revision for the duration of the transaction. |
@@ -527,6 +528,11 @@ void EntityStore::readLatest(const QByteArray &type, const QByteArray &uid, cons | |||
527 | }); | 528 | }); |
528 | } | 529 | } |
529 | 530 | ||
531 | void EntityStore::readLatest(const QByteArray &type, const QByteArray &uid, const std::function<void(const ApplicationDomainType &, Sink::Operation)> callback) | ||
532 | { | ||
533 | readLatest(type, Identifier::fromDisplayByteArray(uid), callback); | ||
534 | } | ||
535 | |||
530 | ApplicationDomain::ApplicationDomainType EntityStore::readLatest(const QByteArray &type, const QByteArray &uid) | 536 | ApplicationDomain::ApplicationDomainType EntityStore::readLatest(const QByteArray &type, const QByteArray &uid) |
531 | { | 537 | { |
532 | ApplicationDomainType dt; | 538 | ApplicationDomainType dt; |
@@ -573,7 +579,7 @@ void EntityStore::readAll(const QByteArray &type, const std::function<void(const | |||
573 | }); | 579 | }); |
574 | } | 580 | } |
575 | 581 | ||
576 | void EntityStore::readRevisions(qint64 baseRevision, const QByteArray &expectedType, const std::function<void(const QByteArray &key)> &callback) | 582 | void EntityStore::readRevisions(qint64 baseRevision, const QByteArray &expectedType, const std::function<void(const Key &key)> &callback) |
577 | { | 583 | { |
578 | qint64 revisionCounter = baseRevision; | 584 | qint64 revisionCounter = baseRevision; |
579 | const qint64 topRevision = DataStore::maxRevision(d->getTransaction()); | 585 | const qint64 topRevision = DataStore::maxRevision(d->getTransaction()); |
@@ -592,15 +598,15 @@ void EntityStore::readRevisions(qint64 baseRevision, const QByteArray &expectedT | |||
592 | const auto key = Key(Identifier::fromDisplayByteArray(uid), revisionCounter); | 598 | const auto key = Key(Identifier::fromDisplayByteArray(uid), revisionCounter); |
593 | 599 | ||
594 | revisionCounter++; | 600 | revisionCounter++; |
595 | callback(key.toDisplayByteArray()); | 601 | callback(key); |
596 | } | 602 | } |
597 | } | 603 | } |
598 | 604 | ||
599 | void EntityStore::readPrevious(const QByteArray &type, const QByteArray &uid, qint64 revision, const std::function<void(const QByteArray &uid, const EntityBuffer &entity)> callback) | 605 | void EntityStore::readPrevious(const QByteArray &type, const Identifier &id, qint64 revision, const std::function<void(const QByteArray &uid, const EntityBuffer &entity)> callback) |
600 | { | 606 | { |
601 | auto db = DataStore::mainDatabase(d->getTransaction(), type); | 607 | auto db = DataStore::mainDatabase(d->getTransaction(), type); |
602 | qint64 latestRevision = 0; | 608 | qint64 latestRevision = 0; |
603 | const auto internalUid = Identifier::fromDisplayByteArray(uid).toInternalByteArray(); | 609 | const auto internalUid = id.toInternalByteArray(); |
604 | db.scan(internalUid, | 610 | db.scan(internalUid, |
605 | [&latestRevision, revision](const QByteArray &key, const QByteArray &) -> bool { | 611 | [&latestRevision, revision](const QByteArray &key, const QByteArray &) -> bool { |
606 | const auto foundRevision = Key::fromInternalByteArray(key).revision().toQint64(); | 612 | const auto foundRevision = Key::fromInternalByteArray(key).revision().toQint64(); |
@@ -610,21 +616,21 @@ void EntityStore::readPrevious(const QByteArray &type, const QByteArray &uid, qi | |||
610 | return true; | 616 | return true; |
611 | }, | 617 | }, |
612 | [&](const DataStore::Error &error) { SinkWarningCtx(d->logCtx) << "Failed to read current value from storage: " << error.message; }, true); | 618 | [&](const DataStore::Error &error) { SinkWarningCtx(d->logCtx) << "Failed to read current value from storage: " << error.message; }, true); |
613 | const auto key = Key(Identifier::fromDisplayByteArray(uid), latestRevision); | 619 | const auto key = Key(id, latestRevision); |
614 | readEntity(type, key.toDisplayByteArray(), callback); | 620 | readEntity(type, key.toDisplayByteArray(), callback); |
615 | } | 621 | } |
616 | 622 | ||
617 | void EntityStore::readPrevious(const QByteArray &type, const QByteArray &uid, qint64 revision, const std::function<void(const ApplicationDomainType &)> callback) | 623 | void EntityStore::readPrevious(const QByteArray &type, const Identifier &id, qint64 revision, const std::function<void(const ApplicationDomainType &)> callback) |
618 | { | 624 | { |
619 | readPrevious(type, uid, revision, [&](const QByteArray &uid, const EntityBuffer &buffer) { | 625 | readPrevious(type, id, revision, [&](const QByteArray &uid, const EntityBuffer &buffer) { |
620 | callback(d->createApplicationDomainType(type, uid, DataStore::maxRevision(d->getTransaction()), buffer)); | 626 | callback(d->createApplicationDomainType(type, uid, DataStore::maxRevision(d->getTransaction()), buffer)); |
621 | }); | 627 | }); |
622 | } | 628 | } |
623 | 629 | ||
624 | ApplicationDomain::ApplicationDomainType EntityStore::readPrevious(const QByteArray &type, const QByteArray &uid, qint64 revision) | 630 | ApplicationDomain::ApplicationDomainType EntityStore::readPrevious(const QByteArray &type, const Identifier &id, qint64 revision) |
625 | { | 631 | { |
626 | ApplicationDomainType dt; | 632 | ApplicationDomainType dt; |
627 | readPrevious(type, uid, revision, [&](const ApplicationDomainType &entity) { | 633 | readPrevious(type, id, revision, [&](const ApplicationDomainType &entity) { |
628 | dt = *ApplicationDomainType::getInMemoryRepresentation<ApplicationDomainType>(entity, entity.availableProperties()); | 634 | dt = *ApplicationDomainType::getInMemoryRepresentation<ApplicationDomainType>(entity, entity.availableProperties()); |
629 | }); | 635 | }); |
630 | return dt; | 636 | return dt; |