diff options
author | Christian Mollekopf <chrigi_1@fastmail.fm> | 2016-10-16 14:55:20 +0200 |
---|---|---|
committer | Christian Mollekopf <chrigi_1@fastmail.fm> | 2016-10-21 09:02:21 +0200 |
commit | 237b9ae4113e7a9f489632296941becb71afdb45 (patch) | |
tree | 01cde58f495944f01cad9d282391d4efd2897141 /examples/maildirresource/maildirresource.cpp | |
parent | 95d11bf0be98a4e3c08502fe23417b800233ce14 (diff) | |
download | sink-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 'examples/maildirresource/maildirresource.cpp')
-rw-r--r-- | examples/maildirresource/maildirresource.cpp | 77 |
1 files changed, 29 insertions, 48 deletions
diff --git a/examples/maildirresource/maildirresource.cpp b/examples/maildirresource/maildirresource.cpp index 1ed7fc8..b89d78c 100644 --- a/examples/maildirresource/maildirresource.cpp +++ b/examples/maildirresource/maildirresource.cpp | |||
@@ -85,13 +85,13 @@ class MaildirMimeMessageMover : public Sink::Preprocessor | |||
85 | public: | 85 | public: |
86 | MaildirMimeMessageMover(const QByteArray &resourceInstanceIdentifier, const QString &maildirPath) : mResourceInstanceIdentifier(resourceInstanceIdentifier), mMaildirPath(maildirPath) {} | 86 | MaildirMimeMessageMover(const QByteArray &resourceInstanceIdentifier, const QString &maildirPath) : mResourceInstanceIdentifier(resourceInstanceIdentifier), mMaildirPath(maildirPath) {} |
87 | 87 | ||
88 | QString getPath(const QByteArray &folderIdentifier, Sink::Storage::Transaction &transaction) | 88 | QString getPath(const QByteArray &folderIdentifier, Sink::Storage::DataStore::Transaction &transaction) |
89 | { | 89 | { |
90 | if (folderIdentifier.isEmpty()) { | 90 | if (folderIdentifier.isEmpty()) { |
91 | return mMaildirPath; | 91 | return mMaildirPath; |
92 | } | 92 | } |
93 | QString folderPath; | 93 | QString folderPath; |
94 | auto db = Sink::Storage::mainDatabase(transaction, ENTITY_TYPE_FOLDER); | 94 | auto db = Sink::Storage::DataStore::mainDatabase(transaction, ENTITY_TYPE_FOLDER); |
95 | db.findLatest(folderIdentifier, [&](const QByteArray &, const QByteArray &value) { | 95 | db.findLatest(folderIdentifier, [&](const QByteArray &, const QByteArray &value) { |
96 | Sink::EntityBuffer buffer(value); | 96 | Sink::EntityBuffer buffer(value); |
97 | const Sink::Entity &entity = buffer.entity(); | 97 | const Sink::Entity &entity = buffer.entity(); |
@@ -108,7 +108,7 @@ public: | |||
108 | return folderPath; | 108 | return folderPath; |
109 | } | 109 | } |
110 | 110 | ||
111 | QString moveMessage(const QString &oldPath, const QByteArray &folder, Sink::Storage::Transaction &transaction) | 111 | QString moveMessage(const QString &oldPath, const QByteArray &folder, Sink::Storage::DataStore::Transaction &transaction) |
112 | { | 112 | { |
113 | if (oldPath.startsWith(Sink::temporaryFileLocation())) { | 113 | if (oldPath.startsWith(Sink::temporaryFileLocation())) { |
114 | const auto path = getPath(folder, transaction); | 114 | const auto path = getPath(folder, transaction); |
@@ -141,7 +141,7 @@ public: | |||
141 | } | 141 | } |
142 | } | 142 | } |
143 | 143 | ||
144 | void newEntity(const QByteArray &uid, qint64 revision, Sink::ApplicationDomain::BufferAdaptor &newEntity, Sink::Storage::Transaction &transaction) Q_DECL_OVERRIDE | 144 | void newEntity(const QByteArray &uid, qint64 revision, Sink::ApplicationDomain::BufferAdaptor &newEntity, Sink::Storage::DataStore::Transaction &transaction) Q_DECL_OVERRIDE |
145 | { | 145 | { |
146 | const auto mimeMessage = newEntity.getProperty("mimeMessage"); | 146 | const auto mimeMessage = newEntity.getProperty("mimeMessage"); |
147 | if (mimeMessage.isValid()) { | 147 | if (mimeMessage.isValid()) { |
@@ -150,7 +150,7 @@ public: | |||
150 | } | 150 | } |
151 | 151 | ||
152 | void modifiedEntity(const QByteArray &uid, qint64 revision, const Sink::ApplicationDomain::BufferAdaptor &oldEntity, Sink::ApplicationDomain::BufferAdaptor &newEntity, | 152 | void modifiedEntity(const QByteArray &uid, qint64 revision, const Sink::ApplicationDomain::BufferAdaptor &oldEntity, Sink::ApplicationDomain::BufferAdaptor &newEntity, |
153 | Sink::Storage::Transaction &transaction) Q_DECL_OVERRIDE | 153 | Sink::Storage::DataStore::Transaction &transaction) Q_DECL_OVERRIDE |
154 | { | 154 | { |
155 | const auto mimeMessage = newEntity.getProperty("mimeMessage"); | 155 | const auto mimeMessage = newEntity.getProperty("mimeMessage"); |
156 | const auto newFolder = newEntity.getProperty("folder"); | 156 | const auto newFolder = newEntity.getProperty("folder"); |
@@ -185,7 +185,7 @@ public: | |||
185 | maildir.changeEntryFlags(identifier, flags); | 185 | maildir.changeEntryFlags(identifier, flags); |
186 | } | 186 | } |
187 | 187 | ||
188 | void deletedEntity(const QByteArray &uid, qint64 revision, const Sink::ApplicationDomain::BufferAdaptor &oldEntity, Sink::Storage::Transaction &transaction) Q_DECL_OVERRIDE | 188 | void deletedEntity(const QByteArray &uid, qint64 revision, const Sink::ApplicationDomain::BufferAdaptor &oldEntity, Sink::Storage::DataStore::Transaction &transaction) Q_DECL_OVERRIDE |
189 | { | 189 | { |
190 | const auto filePath = getFilePathFromMimeMessagePath(oldEntity.getProperty("mimeMessage").toString()); | 190 | const auto filePath = getFilePathFromMimeMessagePath(oldEntity.getProperty("mimeMessage").toString()); |
191 | QFile::remove(filePath); | 191 | QFile::remove(filePath); |
@@ -199,7 +199,7 @@ class FolderPreprocessor : public Sink::Preprocessor | |||
199 | public: | 199 | public: |
200 | FolderPreprocessor(const QString maildirPath) : mMaildirPath(maildirPath) {} | 200 | FolderPreprocessor(const QString maildirPath) : mMaildirPath(maildirPath) {} |
201 | 201 | ||
202 | void newEntity(const QByteArray &uid, qint64 revision, Sink::ApplicationDomain::BufferAdaptor &newEntity, Sink::Storage::Transaction &transaction) Q_DECL_OVERRIDE | 202 | void newEntity(const QByteArray &uid, qint64 revision, Sink::ApplicationDomain::BufferAdaptor &newEntity, Sink::Storage::DataStore::Transaction &transaction) Q_DECL_OVERRIDE |
203 | { | 203 | { |
204 | auto folderName = newEntity.getProperty("name").toString(); | 204 | auto folderName = newEntity.getProperty("name").toString(); |
205 | const auto path = mMaildirPath + "/" + folderName; | 205 | const auto path = mMaildirPath + "/" + folderName; |
@@ -208,11 +208,11 @@ public: | |||
208 | } | 208 | } |
209 | 209 | ||
210 | void modifiedEntity(const QByteArray &uid, qint64 revision, const Sink::ApplicationDomain::BufferAdaptor &oldEntity, Sink::ApplicationDomain::BufferAdaptor &newEntity, | 210 | void modifiedEntity(const QByteArray &uid, qint64 revision, const Sink::ApplicationDomain::BufferAdaptor &oldEntity, Sink::ApplicationDomain::BufferAdaptor &newEntity, |
211 | Sink::Storage::Transaction &transaction) Q_DECL_OVERRIDE | 211 | Sink::Storage::DataStore::Transaction &transaction) Q_DECL_OVERRIDE |
212 | { | 212 | { |
213 | } | 213 | } |
214 | 214 | ||
215 | void deletedEntity(const QByteArray &uid, qint64 revision, const Sink::ApplicationDomain::BufferAdaptor &oldEntity, Sink::Storage::Transaction &transaction) Q_DECL_OVERRIDE | 215 | void deletedEntity(const QByteArray &uid, qint64 revision, const Sink::ApplicationDomain::BufferAdaptor &oldEntity, Sink::Storage::DataStore::Transaction &transaction) Q_DECL_OVERRIDE |
216 | { | 216 | { |
217 | } | 217 | } |
218 | QString mMaildirPath; | 218 | QString mMaildirPath; |
@@ -221,8 +221,8 @@ public: | |||
221 | 221 | ||
222 | class MaildirSynchronizer : public Sink::Synchronizer { | 222 | class MaildirSynchronizer : public Sink::Synchronizer { |
223 | public: | 223 | public: |
224 | MaildirSynchronizer(const QByteArray &resourceType, const QByteArray &resourceInstanceIdentifier) | 224 | MaildirSynchronizer(const Sink::ResourceContext &resourceContext) |
225 | : Sink::Synchronizer(resourceType, resourceInstanceIdentifier) | 225 | : Sink::Synchronizer(resourceContext) |
226 | { | 226 | { |
227 | 227 | ||
228 | } | 228 | } |
@@ -278,19 +278,7 @@ public: | |||
278 | const QByteArray bufferType = ENTITY_TYPE_FOLDER; | 278 | const QByteArray bufferType = ENTITY_TYPE_FOLDER; |
279 | QStringList folderList = listAvailableFolders(); | 279 | QStringList folderList = listAvailableFolders(); |
280 | SinkTrace() << "Found folders " << folderList; | 280 | SinkTrace() << "Found folders " << folderList; |
281 | |||
282 | scanForRemovals(bufferType, | 281 | scanForRemovals(bufferType, |
283 | [this, &bufferType](const std::function<void(const QByteArray &)> &callback) { | ||
284 | //TODO Instead of iterating over all entries in the database, which can also pick up the same item multiple times, | ||
285 | //we should rather iterate over an index that contains every uid exactly once. The remoteId index would be such an index, | ||
286 | //but we currently fail to iterate over all entries in an index it seems. | ||
287 | // auto remoteIds = synchronizationTransaction.openDatabase("rid.mapping." + bufferType, std::function<void(const Sink::Storage::Error &)>(), true); | ||
288 | auto mainDatabase = Sink::Storage::mainDatabase(transaction(), bufferType); | ||
289 | mainDatabase.scan("", [&](const QByteArray &key, const QByteArray &) { | ||
290 | callback(key); | ||
291 | return true; | ||
292 | }); | ||
293 | }, | ||
294 | [&folderList](const QByteArray &remoteId) -> bool { | 282 | [&folderList](const QByteArray &remoteId) -> bool { |
295 | return folderList.contains(remoteId); | 283 | return folderList.contains(remoteId); |
296 | } | 284 | } |
@@ -323,16 +311,9 @@ public: | |||
323 | 311 | ||
324 | const auto folderLocalId = syncStore().resolveRemoteId(ENTITY_TYPE_FOLDER, path.toUtf8()); | 312 | const auto folderLocalId = syncStore().resolveRemoteId(ENTITY_TYPE_FOLDER, path.toUtf8()); |
325 | 313 | ||
326 | auto property = "folder"; | ||
327 | scanForRemovals(bufferType, | 314 | scanForRemovals(bufferType, |
328 | [&](const std::function<void(const QByteArray &)> &callback) { | 315 | [&](const std::function<void(const QByteArray &)> &callback) { |
329 | Index index(bufferType + ".index." + property, transaction()); | 316 | store().indexLookup<ApplicationDomain::Mail, ApplicationDomain::Mail::Folder>(folderLocalId, callback); |
330 | index.lookup(folderLocalId, [&](const QByteArray &sinkId) { | ||
331 | callback(sinkId); | ||
332 | }, | ||
333 | [&](const Index::Error &error) { | ||
334 | SinkWarning() << "Error in index: " << error.message << property; | ||
335 | }); | ||
336 | }, | 317 | }, |
337 | [](const QByteArray &remoteId) -> bool { | 318 | [](const QByteArray &remoteId) -> bool { |
338 | return QFile(remoteId).exists(); | 319 | return QFile(remoteId).exists(); |
@@ -392,7 +373,7 @@ public: | |||
392 | class MaildirWriteback : public Sink::SourceWriteBack | 373 | class MaildirWriteback : public Sink::SourceWriteBack |
393 | { | 374 | { |
394 | public: | 375 | public: |
395 | MaildirWriteback(const QByteArray &resourceType, const QByteArray &resourceInstanceIdentifier) : Sink::SourceWriteBack(resourceType, resourceInstanceIdentifier) | 376 | MaildirWriteback(const Sink::ResourceContext &resourceContext) : Sink::SourceWriteBack(resourceContext) |
396 | { | 377 | { |
397 | 378 | ||
398 | } | 379 | } |
@@ -442,24 +423,24 @@ public: | |||
442 | }; | 423 | }; |
443 | 424 | ||
444 | 425 | ||
445 | MaildirResource::MaildirResource(const QByteArray &instanceIdentifier, const QSharedPointer<Sink::Pipeline> &pipeline) | 426 | MaildirResource::MaildirResource(const Sink::ResourceContext &resourceContext, const QSharedPointer<Sink::Pipeline> &pipeline) |
446 | : Sink::GenericResource(PLUGIN_NAME, instanceIdentifier, pipeline) | 427 | : Sink::GenericResource(resourceContext, pipeline) |
447 | { | 428 | { |
448 | auto config = ResourceConfig::getConfiguration(instanceIdentifier); | 429 | auto config = ResourceConfig::getConfiguration(resourceContext.instanceId()); |
449 | mMaildirPath = QDir::cleanPath(QDir::fromNativeSeparators(config.value("path").toString())); | 430 | mMaildirPath = QDir::cleanPath(QDir::fromNativeSeparators(config.value("path").toString())); |
450 | //Chop a trailing slash if necessary | 431 | //Chop a trailing slash if necessary |
451 | if (mMaildirPath.endsWith("/")) { | 432 | if (mMaildirPath.endsWith("/")) { |
452 | mMaildirPath.chop(1); | 433 | mMaildirPath.chop(1); |
453 | } | 434 | } |
454 | 435 | ||
455 | auto synchronizer = QSharedPointer<MaildirSynchronizer>::create(PLUGIN_NAME, instanceIdentifier); | 436 | auto synchronizer = QSharedPointer<MaildirSynchronizer>::create(resourceContext); |
456 | synchronizer->mMaildirPath = mMaildirPath; | 437 | synchronizer->mMaildirPath = mMaildirPath; |
457 | setupSynchronizer(synchronizer); | 438 | setupSynchronizer(synchronizer); |
458 | auto changereplay = QSharedPointer<MaildirWriteback>::create(PLUGIN_NAME, instanceIdentifier); | 439 | auto changereplay = QSharedPointer<MaildirWriteback>::create(resourceContext); |
459 | changereplay->mMaildirPath = mMaildirPath; | 440 | changereplay->mMaildirPath = mMaildirPath; |
460 | setupChangereplay(changereplay); | 441 | setupChangereplay(changereplay); |
461 | 442 | ||
462 | setupPreprocessors(ENTITY_TYPE_MAIL, QVector<Sink::Preprocessor*>() << new SpecialPurposeProcessor(mResourceType, mResourceInstanceIdentifier) << new MaildirMimeMessageMover(mResourceInstanceIdentifier, mMaildirPath) << new MaildirMailPropertyExtractor << new DefaultIndexUpdater<Sink::ApplicationDomain::Mail>); | 443 | setupPreprocessors(ENTITY_TYPE_MAIL, QVector<Sink::Preprocessor*>() << new SpecialPurposeProcessor(resourceContext.resourceType, resourceContext.instanceId()) << new MaildirMimeMessageMover(resourceContext.instanceId(), mMaildirPath) << new MaildirMailPropertyExtractor << new DefaultIndexUpdater<Sink::ApplicationDomain::Mail>); |
463 | setupPreprocessors(ENTITY_TYPE_FOLDER, QVector<Sink::Preprocessor*>() << new FolderPreprocessor(mMaildirPath) << new DefaultIndexUpdater<Sink::ApplicationDomain::Folder>); | 444 | setupPreprocessors(ENTITY_TYPE_FOLDER, QVector<Sink::Preprocessor*>() << new FolderPreprocessor(mMaildirPath) << new DefaultIndexUpdater<Sink::ApplicationDomain::Folder>); |
464 | 445 | ||
465 | KPIM::Maildir dir(mMaildirPath, true); | 446 | KPIM::Maildir dir(mMaildirPath, true); |
@@ -480,24 +461,24 @@ MaildirResource::MaildirResource(const QByteArray &instanceIdentifier, const QSh | |||
480 | void MaildirResource::removeFromDisk(const QByteArray &instanceIdentifier) | 461 | void MaildirResource::removeFromDisk(const QByteArray &instanceIdentifier) |
481 | { | 462 | { |
482 | GenericResource::removeFromDisk(instanceIdentifier); | 463 | GenericResource::removeFromDisk(instanceIdentifier); |
483 | Sink::Storage(Sink::storageLocation(), instanceIdentifier + ".synchronization", Sink::Storage::ReadWrite).removeFromDisk(); | 464 | Sink::Storage::DataStore(Sink::storageLocation(), instanceIdentifier + ".synchronization", Sink::Storage::DataStore::ReadWrite).removeFromDisk(); |
484 | } | 465 | } |
485 | 466 | ||
486 | KAsync::Job<void> MaildirResource::inspect(int inspectionType, const QByteArray &inspectionId, const QByteArray &domainType, const QByteArray &entityId, const QByteArray &property, const QVariant &expectedValue) | 467 | KAsync::Job<void> MaildirResource::inspect(int inspectionType, const QByteArray &inspectionId, const QByteArray &domainType, const QByteArray &entityId, const QByteArray &property, const QVariant &expectedValue) |
487 | { | 468 | { |
488 | auto synchronizationStore = QSharedPointer<Sink::Storage>::create(Sink::storageLocation(), mResourceInstanceIdentifier + ".synchronization", Sink::Storage::ReadOnly); | 469 | auto synchronizationStore = QSharedPointer<Sink::Storage::DataStore>::create(Sink::storageLocation(), mResourceContext.instanceId() + ".synchronization", Sink::Storage::DataStore::ReadOnly); |
489 | auto synchronizationTransaction = synchronizationStore->createTransaction(Sink::Storage::ReadOnly); | 470 | auto synchronizationTransaction = synchronizationStore->createTransaction(Sink::Storage::DataStore::ReadOnly); |
490 | 471 | ||
491 | auto mainStore = QSharedPointer<Sink::Storage>::create(Sink::storageLocation(), mResourceInstanceIdentifier, Sink::Storage::ReadOnly); | 472 | auto mainStore = QSharedPointer<Sink::Storage::DataStore>::create(Sink::storageLocation(), mResourceContext.instanceId(), Sink::Storage::DataStore::ReadOnly); |
492 | auto transaction = mainStore->createTransaction(Sink::Storage::ReadOnly); | 473 | auto transaction = mainStore->createTransaction(Sink::Storage::DataStore::ReadOnly); |
493 | 474 | ||
494 | auto entityStore = QSharedPointer<EntityStore>::create(mResourceType, mResourceInstanceIdentifier, transaction); | 475 | Sink::Storage::EntityStore entityStore(mResourceContext); |
495 | auto syncStore = QSharedPointer<RemoteIdMap>::create(synchronizationTransaction); | 476 | auto syncStore = QSharedPointer<RemoteIdMap>::create(synchronizationTransaction); |
496 | 477 | ||
497 | SinkTrace() << "Inspecting " << inspectionType << domainType << entityId << property << expectedValue; | 478 | SinkTrace() << "Inspecting " << inspectionType << domainType << entityId << property << expectedValue; |
498 | 479 | ||
499 | if (domainType == ENTITY_TYPE_MAIL) { | 480 | if (domainType == ENTITY_TYPE_MAIL) { |
500 | auto mail = entityStore->read<Sink::ApplicationDomain::Mail>(entityId); | 481 | auto mail = entityStore.readLatest<Sink::ApplicationDomain::Mail>(entityId); |
501 | const auto filePath = getFilePathFromMimeMessagePath(mail.getMimeMessagePath()); | 482 | const auto filePath = getFilePathFromMimeMessagePath(mail.getMimeMessagePath()); |
502 | 483 | ||
503 | if (inspectionType == Sink::ResourceControl::Inspection::PropertyInspectionType) { | 484 | if (inspectionType == Sink::ResourceControl::Inspection::PropertyInspectionType) { |
@@ -530,7 +511,7 @@ KAsync::Job<void> MaildirResource::inspect(int inspectionType, const QByteArray | |||
530 | } | 511 | } |
531 | if (domainType == ENTITY_TYPE_FOLDER) { | 512 | if (domainType == ENTITY_TYPE_FOLDER) { |
532 | const auto remoteId = syncStore->resolveLocalId(ENTITY_TYPE_FOLDER, entityId); | 513 | const auto remoteId = syncStore->resolveLocalId(ENTITY_TYPE_FOLDER, entityId); |
533 | auto folder = entityStore->read<Sink::ApplicationDomain::Folder>(entityId); | 514 | auto folder = entityStore.readLatest<Sink::ApplicationDomain::Folder>(entityId); |
534 | 515 | ||
535 | if (inspectionType == Sink::ResourceControl::Inspection::CacheIntegrityInspectionType) { | 516 | if (inspectionType == Sink::ResourceControl::Inspection::CacheIntegrityInspectionType) { |
536 | SinkTrace() << "Inspecting cache integrity" << remoteId; | 517 | SinkTrace() << "Inspecting cache integrity" << remoteId; |
@@ -577,9 +558,9 @@ MaildirResourceFactory::MaildirResourceFactory(QObject *parent) | |||
577 | 558 | ||
578 | } | 559 | } |
579 | 560 | ||
580 | Sink::Resource *MaildirResourceFactory::createResource(const QByteArray &instanceIdentifier) | 561 | Sink::Resource *MaildirResourceFactory::createResource(const ResourceContext &context) |
581 | { | 562 | { |
582 | return new MaildirResource(instanceIdentifier); | 563 | return new MaildirResource(context); |
583 | } | 564 | } |
584 | 565 | ||
585 | void MaildirResourceFactory::registerFacades(Sink::FacadeFactory &factory) | 566 | void MaildirResourceFactory::registerFacades(Sink::FacadeFactory &factory) |