diff options
-rw-r--r-- | common/storage.h | 1 | ||||
-rw-r--r-- | common/storage/entitystore.cpp | 5 | ||||
-rw-r--r-- | common/storage_common.cpp | 12 | ||||
-rw-r--r-- | tests/entitystoretest.cpp | 47 |
4 files changed, 62 insertions, 3 deletions
diff --git a/common/storage.h b/common/storage.h index 22d8046..ac6509d 100644 --- a/common/storage.h +++ b/common/storage.h | |||
@@ -258,6 +258,7 @@ public: | |||
258 | static void recordUid(DataStore::Transaction &transaction, const QByteArray &uid, const QByteArray &type); | 258 | static void recordUid(DataStore::Transaction &transaction, const QByteArray &uid, const QByteArray &type); |
259 | static void removeUid(DataStore::Transaction &transaction, const QByteArray &uid, const QByteArray &type); | 259 | static void removeUid(DataStore::Transaction &transaction, const QByteArray &uid, const QByteArray &type); |
260 | static void getUids(const QByteArray &type, const Transaction &, const std::function<void(const QByteArray &uid)> &); | 260 | static void getUids(const QByteArray &type, const Transaction &, const std::function<void(const QByteArray &uid)> &); |
261 | static bool hasUid(const QByteArray &type, const Transaction &, const QByteArray &uid); | ||
261 | 262 | ||
262 | bool exists() const; | 263 | bool exists() const; |
263 | static bool exists(const QString &storageRoot, const QString &name); | 264 | static bool exists(const QString &storageRoot, const QString &name); |
diff --git a/common/storage/entitystore.cpp b/common/storage/entitystore.cpp index a92de40..454e25a 100644 --- a/common/storage/entitystore.cpp +++ b/common/storage/entitystore.cpp | |||
@@ -630,11 +630,10 @@ void EntityStore::readAllUids(const QByteArray &type, const std::function<void(c | |||
630 | DataStore::getUids(type, d->getTransaction(), callback); | 630 | DataStore::getUids(type, d->getTransaction(), callback); |
631 | } | 631 | } |
632 | 632 | ||
633 | bool EntityStore::contains(const QByteArray &type, const QByteArray &uid) | 633 | bool EntityStore::contains(const QByteArray & /* type */, const QByteArray &uid) |
634 | { | 634 | { |
635 | Q_ASSERT(!uid.isEmpty()); | 635 | Q_ASSERT(!uid.isEmpty()); |
636 | const auto internalUid = Identifier::fromDisplayByteArray(uid).toInternalByteArray(); | 636 | return !DataStore::getRevisionsFromUid(d->getTransaction(), uid).isEmpty(); |
637 | return DataStore::mainDatabase(d->getTransaction(), type).contains(internalUid); | ||
638 | } | 637 | } |
639 | 638 | ||
640 | bool EntityStore::exists(const QByteArray &type, const QByteArray &uid) | 639 | bool EntityStore::exists(const QByteArray &type, const QByteArray &uid) |
diff --git a/common/storage_common.cpp b/common/storage_common.cpp index 09b7707..7c794c3 100644 --- a/common/storage_common.cpp +++ b/common/storage_common.cpp | |||
@@ -233,6 +233,18 @@ void DataStore::getUids(const QByteArray &type, const Transaction &transaction, | |||
233 | }); | 233 | }); |
234 | } | 234 | } |
235 | 235 | ||
236 | bool DataStore::hasUid(const QByteArray &type, const Transaction &transaction, const QByteArray &uid) | ||
237 | { | ||
238 | bool hasTheUid = false; | ||
239 | transaction.openDatabase(type + "uids").scan(uid, [&](const QByteArray &key, const QByteArray &) { | ||
240 | Q_ASSERT(uid == key); | ||
241 | hasTheUid = true; | ||
242 | return false; | ||
243 | }); | ||
244 | |||
245 | return hasTheUid; | ||
246 | } | ||
247 | |||
236 | bool DataStore::isInternalKey(const char *key) | 248 | bool DataStore::isInternalKey(const char *key) |
237 | { | 249 | { |
238 | return key && strncmp(key, s_internalPrefix, s_internalPrefixSize) == 0; | 250 | return key && strncmp(key, s_internalPrefix, s_internalPrefixSize) == 0; |
diff --git a/tests/entitystoretest.cpp b/tests/entitystoretest.cpp index aab5b3b..608de4c 100644 --- a/tests/entitystoretest.cpp +++ b/tests/entitystoretest.cpp | |||
@@ -18,6 +18,7 @@ private slots: | |||
18 | void initTestCase() | 18 | void initTestCase() |
19 | { | 19 | { |
20 | Sink::AdaptorFactoryRegistry::instance().registerFactory<Sink::ApplicationDomain::Mail, TestMailAdaptorFactory>("test"); | 20 | Sink::AdaptorFactoryRegistry::instance().registerFactory<Sink::ApplicationDomain::Mail, TestMailAdaptorFactory>("test"); |
21 | Sink::AdaptorFactoryRegistry::instance().registerFactory<Sink::ApplicationDomain::Event, TestEventAdaptorFactory>("test"); | ||
21 | } | 22 | } |
22 | 23 | ||
23 | void cleanup() | 24 | void cleanup() |
@@ -77,6 +78,52 @@ private slots: | |||
77 | } | 78 | } |
78 | } | 79 | } |
79 | 80 | ||
81 | void testExistsAndContains() | ||
82 | { | ||
83 | |||
84 | using namespace Sink; | ||
85 | ResourceContext resourceContext{resourceInstanceIdentifier.toUtf8(), "dummy", AdaptorFactoryRegistry::instance().getFactories("test")}; | ||
86 | Storage::EntityStore store(resourceContext, {}); | ||
87 | |||
88 | auto mail = ApplicationDomain::ApplicationDomainType::createEntity<ApplicationDomain::Mail>("res1"); | ||
89 | mail.setExtractedMessageId("messageid"); | ||
90 | mail.setExtractedSubject("boo"); | ||
91 | |||
92 | auto mail2 = ApplicationDomain::ApplicationDomainType::createEntity<ApplicationDomain::Mail>("res1"); | ||
93 | mail2.setExtractedMessageId("messageid2"); | ||
94 | mail2.setExtractedSubject("foo"); | ||
95 | |||
96 | auto mail3 = ApplicationDomain::ApplicationDomainType::createEntity<ApplicationDomain::Mail>("res1"); | ||
97 | mail3.setExtractedMessageId("messageid2"); | ||
98 | mail3.setExtractedSubject("foo"); | ||
99 | |||
100 | auto event = ApplicationDomain::ApplicationDomainType::createEntity<ApplicationDomain::Event>("res1"); | ||
101 | event.setExtractedUid("messageid2"); | ||
102 | event.setExtractedSummary("foo"); | ||
103 | |||
104 | store.startTransaction(Storage::DataStore::ReadWrite); | ||
105 | store.add("mail", mail, false); | ||
106 | store.add("mail", mail2, false); | ||
107 | store.add("mail", mail3, false); | ||
108 | store.add("event", event, false); | ||
109 | |||
110 | mail.setExtractedSubject("foo"); | ||
111 | |||
112 | store.modify("mail", mail, QByteArrayList{}, false); | ||
113 | store.remove("mail", mail3, false); | ||
114 | store.commitTransaction(); | ||
115 | |||
116 | QVERIFY(store.contains("mail", mail.identifier())); | ||
117 | QVERIFY(store.contains("mail", mail2.identifier())); | ||
118 | QVERIFY(store.contains("mail", mail3.identifier())); | ||
119 | QVERIFY(store.contains("event", event.identifier())); | ||
120 | |||
121 | QVERIFY(store.exists("mail", mail.identifier())); | ||
122 | QVERIFY(store.exists("mail", mail2.identifier())); | ||
123 | QVERIFY(!store.exists("mail", mail3.identifier())); | ||
124 | QVERIFY(store.exists("event", event.identifier())); | ||
125 | } | ||
126 | |||
80 | void readAll() | 127 | void readAll() |
81 | { | 128 | { |
82 | using namespace Sink; | 129 | using namespace Sink; |