summaryrefslogtreecommitdiffstats
path: root/tests/pipelinetest.cpp
diff options
context:
space:
mode:
authorChristian Mollekopf <chrigi_1@fastmail.fm>2016-10-16 14:55:20 +0200
committerChristian Mollekopf <chrigi_1@fastmail.fm>2016-10-21 09:02:21 +0200
commit237b9ae4113e7a9f489632296941becb71afdb45 (patch)
tree01cde58f495944f01cad9d282391d4efd2897141 /tests/pipelinetest.cpp
parent95d11bf0be98a4e3c08502fe23417b800233ce14 (diff)
downloadsink-237b9ae4113e7a9f489632296941becb71afdb45.tar.gz
sink-237b9ae4113e7a9f489632296941becb71afdb45.zip
Refactor how the storage is used.
This is the initial refactoring to improve how we deal with the storage. It does a couple of things: * Rename Sink::Storage to Sink::Storage::DataStore to free up the Sink::Storage namespace * Introduce a Sink::ResourceContext to have a single object that can be passed around containing everything that is necessary to operate on a resource. This is a lot better than the multiple separate parameters that we used to pass around all over the place, while still allowing for dependency injection for tests. * Tie storage access together using the new EntityStore that directly works with ApplicationDomainTypes. This gives us a central place where main storage, indexes and buffer adaptors are tied together, which will also give us a place to implement external indexes, such as a fulltextindex using xapian. * Use ApplicationDomainTypes as the default way to pass around entities. Instead of using various ways to pass around entities (buffers, buffer adaptors, ApplicationDomainTypes), only use a single way. The old approach was confusing, and was only done as: * optimization; really shouldn't be necessary and otherwise I'm sure we can find better ways to optimize ApplicationDomainType itself. * a way to account for entities that have multiple buffers, a concept that I no longer deem relevant. While this commit does the bulk of the work to get there, the following commits will refactor more stuff to get things back to normal.
Diffstat (limited to 'tests/pipelinetest.cpp')
-rw-r--r--tests/pipelinetest.cpp49
1 files changed, 22 insertions, 27 deletions
diff --git a/tests/pipelinetest.cpp b/tests/pipelinetest.cpp
index 7216f62..112453e 100644
--- a/tests/pipelinetest.cpp
+++ b/tests/pipelinetest.cpp
@@ -23,14 +23,14 @@
23 23
24static void removeFromDisk(const QString &name) 24static void removeFromDisk(const QString &name)
25{ 25{
26 Sink::Storage store(Sink::Store::storageLocation(), name, Sink::Storage::ReadWrite); 26 Sink::Storage::DataStore store(Sink::Store::storageLocation(), name, Sink::Storage::DataStore::ReadWrite);
27 store.removeFromDisk(); 27 store.removeFromDisk();
28} 28}
29 29
30static QList<QByteArray> getKeys(const QByteArray &dbEnv, const QByteArray &name) 30static QList<QByteArray> getKeys(const QByteArray &dbEnv, const QByteArray &name)
31{ 31{
32 Sink::Storage store(Sink::storageLocation(), dbEnv, Sink::Storage::ReadOnly); 32 Sink::Storage::DataStore store(Sink::storageLocation(), dbEnv, Sink::Storage::DataStore::ReadOnly);
33 auto transaction = store.createTransaction(Sink::Storage::ReadOnly); 33 auto transaction = store.createTransaction(Sink::Storage::DataStore::ReadOnly);
34 auto db = transaction.openDatabase(name, nullptr, false); 34 auto db = transaction.openDatabase(name, nullptr, false);
35 QList<QByteArray> result; 35 QList<QByteArray> result;
36 db.scan("", [&](const QByteArray &key, const QByteArray &value) { 36 db.scan("", [&](const QByteArray &key, const QByteArray &value) {
@@ -42,8 +42,8 @@ static QList<QByteArray> getKeys(const QByteArray &dbEnv, const QByteArray &name
42 42
43static QByteArray getEntity(const QByteArray &dbEnv, const QByteArray &name, const QByteArray &uid) 43static QByteArray getEntity(const QByteArray &dbEnv, const QByteArray &name, const QByteArray &uid)
44{ 44{
45 Sink::Storage store(Sink::storageLocation(), dbEnv, Sink::Storage::ReadOnly); 45 Sink::Storage::DataStore store(Sink::storageLocation(), dbEnv, Sink::Storage::DataStore::ReadOnly);
46 auto transaction = store.createTransaction(Sink::Storage::ReadOnly); 46 auto transaction = store.createTransaction(Sink::Storage::DataStore::ReadOnly);
47 auto db = transaction.openDatabase(name, nullptr, false); 47 auto db = transaction.openDatabase(name, nullptr, false);
48 QByteArray result; 48 QByteArray result;
49 db.scan(uid, [&](const QByteArray &key, const QByteArray &value) { 49 db.scan(uid, [&](const QByteArray &key, const QByteArray &value) {
@@ -152,20 +152,20 @@ QByteArray deleteEntityCommand(const QByteArray &uid, qint64 revision)
152class TestProcessor : public Sink::Preprocessor 152class TestProcessor : public Sink::Preprocessor
153{ 153{
154public: 154public:
155 void newEntity(const QByteArray &uid, qint64 revision, Sink::ApplicationDomain::BufferAdaptor &newEntity, Sink::Storage::Transaction &transaction) Q_DECL_OVERRIDE 155 void newEntity(const QByteArray &uid, qint64 revision, Sink::ApplicationDomain::BufferAdaptor &newEntity, Sink::Storage::DataStore::Transaction &transaction) Q_DECL_OVERRIDE
156 { 156 {
157 newUids << uid; 157 newUids << uid;
158 newRevisions << revision; 158 newRevisions << revision;
159 } 159 }
160 160
161 void modifiedEntity(const QByteArray &uid, qint64 revision, const Sink::ApplicationDomain::BufferAdaptor &oldEntity, Sink::ApplicationDomain::BufferAdaptor &newEntity, 161 void modifiedEntity(const QByteArray &uid, qint64 revision, const Sink::ApplicationDomain::BufferAdaptor &oldEntity, Sink::ApplicationDomain::BufferAdaptor &newEntity,
162 Sink::Storage::Transaction &transaction) Q_DECL_OVERRIDE 162 Sink::Storage::DataStore::Transaction &transaction) Q_DECL_OVERRIDE
163 { 163 {
164 modifiedUids << uid; 164 modifiedUids << uid;
165 modifiedRevisions << revision; 165 modifiedRevisions << revision;
166 } 166 }
167 167
168 void deletedEntity(const QByteArray &uid, qint64 revision, const Sink::ApplicationDomain::BufferAdaptor &oldEntity, Sink::Storage::Transaction &transaction) Q_DECL_OVERRIDE 168 void deletedEntity(const QByteArray &uid, qint64 revision, const Sink::ApplicationDomain::BufferAdaptor &oldEntity, Sink::Storage::DataStore::Transaction &transaction) Q_DECL_OVERRIDE
169 { 169 {
170 deletedUids << uid; 170 deletedUids << uid;
171 deletedRevisions << revision; 171 deletedRevisions << revision;
@@ -203,8 +203,7 @@ private slots:
203 flatbuffers::FlatBufferBuilder entityFbb; 203 flatbuffers::FlatBufferBuilder entityFbb;
204 auto command = createEntityCommand(createEvent(entityFbb)); 204 auto command = createEntityCommand(createEvent(entityFbb));
205 205
206 Sink::Pipeline pipeline("sink.pipelinetest.instance1"); 206 Sink::Pipeline pipeline(Sink::ResourceContext{"sink.pipelinetest.instance1", "test"});
207 pipeline.setResourceType("test");
208 207
209 pipeline.startTransaction(); 208 pipeline.startTransaction();
210 pipeline.newEntity(command.constData(), command.size()); 209 pipeline.newEntity(command.constData(), command.size());
@@ -220,8 +219,7 @@ private slots:
220 flatbuffers::FlatBufferBuilder entityFbb; 219 flatbuffers::FlatBufferBuilder entityFbb;
221 auto command = createEntityCommand(createEvent(entityFbb, "summary", "description")); 220 auto command = createEntityCommand(createEvent(entityFbb, "summary", "description"));
222 221
223 Sink::Pipeline pipeline("sink.pipelinetest.instance1"); 222 Sink::Pipeline pipeline(Sink::ResourceContext{"sink.pipelinetest.instance1", "test"});
224 pipeline.setResourceType("test");
225 223
226 auto adaptorFactory = QSharedPointer<TestEventAdaptorFactory>::create(); 224 auto adaptorFactory = QSharedPointer<TestEventAdaptorFactory>::create();
227 225
@@ -234,7 +232,7 @@ private slots:
234 auto keys = getKeys("sink.pipelinetest.instance1", "event.main"); 232 auto keys = getKeys("sink.pipelinetest.instance1", "event.main");
235 QCOMPARE(keys.size(), 1); 233 QCOMPARE(keys.size(), 1);
236 const auto key = keys.first(); 234 const auto key = keys.first();
237 const auto uid = Sink::Storage::uidFromKey(key); 235 const auto uid = Sink::Storage::DataStore::uidFromKey(key);
238 236
239 // Execute the modification 237 // Execute the modification
240 entityFbb.Clear(); 238 entityFbb.Clear();
@@ -244,7 +242,7 @@ private slots:
244 pipeline.commit(); 242 pipeline.commit();
245 243
246 // Ensure we've got the new revision with the modification 244 // Ensure we've got the new revision with the modification
247 auto buffer = getEntity("sink.pipelinetest.instance1", "event.main", Sink::Storage::assembleKey(uid, 2)); 245 auto buffer = getEntity("sink.pipelinetest.instance1", "event.main", Sink::Storage::DataStore::assembleKey(uid, 2));
248 QVERIFY(!buffer.isEmpty()); 246 QVERIFY(!buffer.isEmpty());
249 Sink::EntityBuffer entityBuffer(buffer.data(), buffer.size()); 247 Sink::EntityBuffer entityBuffer(buffer.data(), buffer.size());
250 auto adaptor = adaptorFactory->createAdaptor(entityBuffer.entity()); 248 auto adaptor = adaptorFactory->createAdaptor(entityBuffer.entity());
@@ -269,8 +267,7 @@ private slots:
269 flatbuffers::FlatBufferBuilder entityFbb; 267 flatbuffers::FlatBufferBuilder entityFbb;
270 auto command = createEntityCommand(createEvent(entityFbb)); 268 auto command = createEntityCommand(createEvent(entityFbb));
271 269
272 Sink::Pipeline pipeline("sink.pipelinetest.instance1"); 270 Sink::Pipeline pipeline(Sink::ResourceContext{"sink.pipelinetest.instance1", "test"});
273 pipeline.setResourceType("test");
274 271
275 auto adaptorFactory = QSharedPointer<TestEventAdaptorFactory>::create(); 272 auto adaptorFactory = QSharedPointer<TestEventAdaptorFactory>::create();
276 273
@@ -282,7 +279,7 @@ private slots:
282 // Get uid of written entity 279 // Get uid of written entity
283 auto keys = getKeys("sink.pipelinetest.instance1", "event.main"); 280 auto keys = getKeys("sink.pipelinetest.instance1", "event.main");
284 QCOMPARE(keys.size(), 1); 281 QCOMPARE(keys.size(), 1);
285 const auto uid = Sink::Storage::uidFromKey(keys.first()); 282 const auto uid = Sink::Storage::DataStore::uidFromKey(keys.first());
286 283
287 284
288 // Create another operation inbetween 285 // Create another operation inbetween
@@ -302,7 +299,7 @@ private slots:
302 pipeline.commit(); 299 pipeline.commit();
303 300
304 // Ensure we've got the new revision with the modification 301 // Ensure we've got the new revision with the modification
305 auto buffer = getEntity("sink.pipelinetest.instance1", "event.main", Sink::Storage::assembleKey(uid, 3)); 302 auto buffer = getEntity("sink.pipelinetest.instance1", "event.main", Sink::Storage::DataStore::assembleKey(uid, 3));
306 QVERIFY(!buffer.isEmpty()); 303 QVERIFY(!buffer.isEmpty());
307 Sink::EntityBuffer entityBuffer(buffer.data(), buffer.size()); 304 Sink::EntityBuffer entityBuffer(buffer.data(), buffer.size());
308 auto adaptor = adaptorFactory->createAdaptor(entityBuffer.entity()); 305 auto adaptor = adaptorFactory->createAdaptor(entityBuffer.entity());
@@ -313,8 +310,7 @@ private slots:
313 { 310 {
314 flatbuffers::FlatBufferBuilder entityFbb; 311 flatbuffers::FlatBufferBuilder entityFbb;
315 auto command = createEntityCommand(createEvent(entityFbb)); 312 auto command = createEntityCommand(createEvent(entityFbb));
316 Sink::Pipeline pipeline("sink.pipelinetest.instance1"); 313 Sink::Pipeline pipeline(Sink::ResourceContext{"sink.pipelinetest.instance1", "test"});
317 pipeline.setResourceType("test");
318 314
319 // Create the initial revision 315 // Create the initial revision
320 pipeline.startTransaction(); 316 pipeline.startTransaction();
@@ -324,7 +320,7 @@ private slots:
324 auto result = getKeys("sink.pipelinetest.instance1", "event.main"); 320 auto result = getKeys("sink.pipelinetest.instance1", "event.main");
325 QCOMPARE(result.size(), 1); 321 QCOMPARE(result.size(), 1);
326 322
327 const auto uid = Sink::Storage::uidFromKey(result.first()); 323 const auto uid = Sink::Storage::DataStore::uidFromKey(result.first());
328 324
329 // Delete entity 325 // Delete entity
330 auto deleteCommand = deleteEntityCommand(uid, 1); 326 auto deleteCommand = deleteEntityCommand(uid, 1);
@@ -350,8 +346,7 @@ private slots:
350 346
351 auto testProcessor = new TestProcessor; 347 auto testProcessor = new TestProcessor;
352 348
353 Sink::Pipeline pipeline("sink.pipelinetest.instance1"); 349 Sink::Pipeline pipeline(Sink::ResourceContext{"sink.pipelinetest.instance1", "test"});
354 pipeline.setResourceType("test");
355 pipeline.setPreprocessors("event", QVector<Sink::Preprocessor *>() << testProcessor); 350 pipeline.setPreprocessors("event", QVector<Sink::Preprocessor *>() << testProcessor);
356 pipeline.startTransaction(); 351 pipeline.startTransaction();
357 // pipeline.setAdaptorFactory("event", QSharedPointer<TestEventAdaptorFactory>::create()); 352 // pipeline.setAdaptorFactory("event", QSharedPointer<TestEventAdaptorFactory>::create());
@@ -363,21 +358,21 @@ private slots:
363 QCOMPARE(testProcessor->newUids.size(), 1); 358 QCOMPARE(testProcessor->newUids.size(), 1);
364 QCOMPARE(testProcessor->newRevisions.size(), 1); 359 QCOMPARE(testProcessor->newRevisions.size(), 1);
365 // Key doesn't contain revision and is just the uid 360 // Key doesn't contain revision and is just the uid
366 QCOMPARE(testProcessor->newUids.at(0), Sink::Storage::uidFromKey(testProcessor->newUids.at(0))); 361 QCOMPARE(testProcessor->newUids.at(0), Sink::Storage::DataStore::uidFromKey(testProcessor->newUids.at(0)));
367 } 362 }
368 pipeline.commit(); 363 pipeline.commit();
369 entityFbb.Clear(); 364 entityFbb.Clear();
370 pipeline.startTransaction(); 365 pipeline.startTransaction();
371 auto keys = getKeys("sink.pipelinetest.instance1", "event.main"); 366 auto keys = getKeys("sink.pipelinetest.instance1", "event.main");
372 QCOMPARE(keys.size(), 1); 367 QCOMPARE(keys.size(), 1);
373 const auto uid = Sink::Storage::uidFromKey(keys.first()); 368 const auto uid = Sink::Storage::DataStore::uidFromKey(keys.first());
374 { 369 {
375 auto modifyCommand = modifyEntityCommand(createEvent(entityFbb, "summary2"), uid, 1); 370 auto modifyCommand = modifyEntityCommand(createEvent(entityFbb, "summary2"), uid, 1);
376 pipeline.modifiedEntity(modifyCommand.constData(), modifyCommand.size()); 371 pipeline.modifiedEntity(modifyCommand.constData(), modifyCommand.size());
377 QCOMPARE(testProcessor->modifiedUids.size(), 1); 372 QCOMPARE(testProcessor->modifiedUids.size(), 1);
378 QCOMPARE(testProcessor->modifiedRevisions.size(), 1); 373 QCOMPARE(testProcessor->modifiedRevisions.size(), 1);
379 // Key doesn't contain revision and is just the uid 374 // Key doesn't contain revision and is just the uid
380 QCOMPARE(testProcessor->modifiedUids.at(0), Sink::Storage::uidFromKey(testProcessor->modifiedUids.at(0))); 375 QCOMPARE(testProcessor->modifiedUids.at(0), Sink::Storage::DataStore::uidFromKey(testProcessor->modifiedUids.at(0)));
381 } 376 }
382 pipeline.commit(); 377 pipeline.commit();
383 entityFbb.Clear(); 378 entityFbb.Clear();
@@ -389,7 +384,7 @@ private slots:
389 QCOMPARE(testProcessor->deletedUids.size(), 1); 384 QCOMPARE(testProcessor->deletedUids.size(), 1);
390 QCOMPARE(testProcessor->deletedSummaries.size(), 1); 385 QCOMPARE(testProcessor->deletedSummaries.size(), 1);
391 // Key doesn't contain revision and is just the uid 386 // Key doesn't contain revision and is just the uid
392 QCOMPARE(testProcessor->deletedUids.at(0), Sink::Storage::uidFromKey(testProcessor->deletedUids.at(0))); 387 QCOMPARE(testProcessor->deletedUids.at(0), Sink::Storage::DataStore::uidFromKey(testProcessor->deletedUids.at(0)));
393 QCOMPARE(testProcessor->deletedSummaries.at(0), QByteArray("summary2")); 388 QCOMPARE(testProcessor->deletedSummaries.at(0), QByteArray("summary2"));
394 } 389 }
395 } 390 }