diff options
-rw-r--r-- | common/storage.h | 6 | ||||
-rw-r--r-- | common/storage/entitystore.cpp | 6 | ||||
-rw-r--r-- | common/storage_common.cpp | 12 | ||||
-rw-r--r-- | tests/storagetest.cpp | 29 |
4 files changed, 41 insertions, 12 deletions
diff --git a/common/storage.h b/common/storage.h index 8c129df..c39b904 100644 --- a/common/storage.h +++ b/common/storage.h | |||
@@ -198,9 +198,9 @@ public: | |||
198 | static QByteArray getTypeFromRevision(const Transaction &, qint64 revision); | 198 | static QByteArray getTypeFromRevision(const Transaction &, qint64 revision); |
199 | static void recordRevision(Transaction &, qint64 revision, const QByteArray &uid, const QByteArray &type); | 199 | static void recordRevision(Transaction &, qint64 revision, const QByteArray &uid, const QByteArray &type); |
200 | static void removeRevision(Transaction &, qint64 revision); | 200 | static void removeRevision(Transaction &, qint64 revision); |
201 | static void recordUid(DataStore::Transaction &transaction, const QByteArray &uid); | 201 | static void recordUid(DataStore::Transaction &transaction, const QByteArray &uid, const QByteArray &type); |
202 | static void removeUid(DataStore::Transaction &transaction, const QByteArray &uid); | 202 | static void removeUid(DataStore::Transaction &transaction, const QByteArray &uid, const QByteArray &type); |
203 | static void getUids(const Transaction &, const std::function<void(const QByteArray &uid)> &); | 203 | static void getUids(const QByteArray &type, const Transaction &, const std::function<void(const QByteArray &uid)> &); |
204 | 204 | ||
205 | bool exists() const; | 205 | bool exists() const; |
206 | 206 | ||
diff --git a/common/storage/entitystore.cpp b/common/storage/entitystore.cpp index 1ac87b7..5514e31 100644 --- a/common/storage/entitystore.cpp +++ b/common/storage/entitystore.cpp | |||
@@ -267,7 +267,7 @@ bool EntityStore::add(const QByteArray &type, ApplicationDomain::ApplicationDoma | |||
267 | [&](const DataStore::Error &error) { SinkWarningCtx(d->logCtx) << "Failed to write entity" << entity.identifier() << newRevision; }); | 267 | [&](const DataStore::Error &error) { SinkWarningCtx(d->logCtx) << "Failed to write entity" << entity.identifier() << newRevision; }); |
268 | DataStore::setMaxRevision(d->transaction, newRevision); | 268 | DataStore::setMaxRevision(d->transaction, newRevision); |
269 | DataStore::recordRevision(d->transaction, newRevision, entity.identifier(), type); | 269 | DataStore::recordRevision(d->transaction, newRevision, entity.identifier(), type); |
270 | DataStore::recordUid(d->transaction, entity.identifier()); | 270 | DataStore::recordUid(d->transaction, entity.identifier(), type); |
271 | SinkTraceCtx(d->logCtx) << "Wrote entity: " << entity.identifier() << type << newRevision; | 271 | SinkTraceCtx(d->logCtx) << "Wrote entity: " << entity.identifier() << type << newRevision; |
272 | return true; | 272 | return true; |
273 | } | 273 | } |
@@ -379,7 +379,7 @@ bool EntityStore::remove(const QByteArray &type, const Sink::ApplicationDomain:: | |||
379 | [&](const DataStore::Error &error) { SinkWarningCtx(d->logCtx) << "Failed to write entity" << uid << newRevision; }); | 379 | [&](const DataStore::Error &error) { SinkWarningCtx(d->logCtx) << "Failed to write entity" << uid << newRevision; }); |
380 | DataStore::setMaxRevision(d->transaction, newRevision); | 380 | DataStore::setMaxRevision(d->transaction, newRevision); |
381 | DataStore::recordRevision(d->transaction, newRevision, uid, type); | 381 | DataStore::recordRevision(d->transaction, newRevision, uid, type); |
382 | DataStore::removeUid(d->transaction, uid); | 382 | DataStore::removeUid(d->transaction, uid, type); |
383 | return true; | 383 | return true; |
384 | } | 384 | } |
385 | 385 | ||
@@ -638,7 +638,7 @@ ApplicationDomain::ApplicationDomainType EntityStore::readPrevious(const QByteAr | |||
638 | 638 | ||
639 | void EntityStore::readAllUids(const QByteArray &type, const std::function<void(const QByteArray &uid)> callback) | 639 | void EntityStore::readAllUids(const QByteArray &type, const std::function<void(const QByteArray &uid)> callback) |
640 | { | 640 | { |
641 | DataStore::getUids(d->getTransaction(), callback); | 641 | DataStore::getUids(type, d->getTransaction(), callback); |
642 | } | 642 | } |
643 | 643 | ||
644 | bool EntityStore::contains(const QByteArray &type, const QByteArray &uid) | 644 | bool EntityStore::contains(const QByteArray &type, const QByteArray &uid) |
diff --git a/common/storage_common.cpp b/common/storage_common.cpp index 8603787..630dae9 100644 --- a/common/storage_common.cpp +++ b/common/storage_common.cpp | |||
@@ -156,19 +156,19 @@ void DataStore::removeRevision(DataStore::Transaction &transaction, qint64 revis | |||
156 | transaction.openDatabase("revisionType").remove(QByteArray::number(revision)); | 156 | transaction.openDatabase("revisionType").remove(QByteArray::number(revision)); |
157 | } | 157 | } |
158 | 158 | ||
159 | void DataStore::recordUid(DataStore::Transaction &transaction, const QByteArray &uid) | 159 | void DataStore::recordUid(DataStore::Transaction &transaction, const QByteArray &uid, const QByteArray &type) |
160 | { | 160 | { |
161 | transaction.openDatabase("uids").write(uid, ""); | 161 | transaction.openDatabase(type + "uids").write(uid, ""); |
162 | } | 162 | } |
163 | 163 | ||
164 | void DataStore::removeUid(DataStore::Transaction &transaction, const QByteArray &uid) | 164 | void DataStore::removeUid(DataStore::Transaction &transaction, const QByteArray &uid, const QByteArray &type) |
165 | { | 165 | { |
166 | transaction.openDatabase("uids").remove(uid); | 166 | transaction.openDatabase(type + "uids").remove(uid); |
167 | } | 167 | } |
168 | 168 | ||
169 | void DataStore::getUids(const Transaction &transaction, const std::function<void(const QByteArray &uid)> &callback) | 169 | void DataStore::getUids(const QByteArray &type, const Transaction &transaction, const std::function<void(const QByteArray &uid)> &callback) |
170 | { | 170 | { |
171 | transaction.openDatabase("uids").scan("", [&] (const QByteArray &key, const QByteArray &) { | 171 | transaction.openDatabase(type + "uids").scan("", [&] (const QByteArray &key, const QByteArray &) { |
172 | callback(key); | 172 | callback(key); |
173 | return true; | 173 | return true; |
174 | }); | 174 | }); |
diff --git a/tests/storagetest.cpp b/tests/storagetest.cpp index 9e9bad9..3368549 100644 --- a/tests/storagetest.cpp +++ b/tests/storagetest.cpp | |||
@@ -542,6 +542,35 @@ private slots: | |||
542 | } | 542 | } |
543 | 543 | ||
544 | } | 544 | } |
545 | |||
546 | void testRecordUid() | ||
547 | { | ||
548 | Sink::Storage::DataStore store(testDataPath, dbName, Sink::Storage::DataStore::ReadWrite); | ||
549 | auto transaction = store.createTransaction(Sink::Storage::DataStore::ReadWrite); | ||
550 | Sink::Storage::DataStore::recordUid(transaction, "uid1", "type"); | ||
551 | Sink::Storage::DataStore::recordUid(transaction, "uid2", "type"); | ||
552 | Sink::Storage::DataStore::recordUid(transaction, "uid3", "type2"); | ||
553 | |||
554 | { | ||
555 | QVector<QByteArray> uids; | ||
556 | Sink::Storage::DataStore::getUids("type", transaction, [&](const QByteArray &r) { | ||
557 | uids << r; | ||
558 | }); | ||
559 | QVector<QByteArray> expected{{"uid1"}, {"uid2"}}; | ||
560 | QCOMPARE(uids, expected); | ||
561 | } | ||
562 | |||
563 | Sink::Storage::DataStore::removeUid(transaction, "uid2", "type"); | ||
564 | |||
565 | { | ||
566 | QVector<QByteArray> uids; | ||
567 | Sink::Storage::DataStore::getUids("type", transaction, [&](const QByteArray &r) { | ||
568 | uids << r; | ||
569 | }); | ||
570 | QVector<QByteArray> expected{{"uid1"}}; | ||
571 | QCOMPARE(uids, expected); | ||
572 | } | ||
573 | } | ||
545 | }; | 574 | }; |
546 | 575 | ||
547 | QTEST_MAIN(StorageTest) | 576 | QTEST_MAIN(StorageTest) |