From 237b9ae4113e7a9f489632296941becb71afdb45 Mon Sep 17 00:00:00 2001 From: Christian Mollekopf Date: Sun, 16 Oct 2016 14:55:20 +0200 Subject: 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. --- common/sourcewriteback.cpp | 31 ++++++++++++++++--------------- 1 file changed, 16 insertions(+), 15 deletions(-) (limited to 'common/sourcewriteback.cpp') diff --git a/common/sourcewriteback.cpp b/common/sourcewriteback.cpp index 702d8e3..204793e 100644 --- a/common/sourcewriteback.cpp +++ b/common/sourcewriteback.cpp @@ -22,6 +22,8 @@ #include "definitions.h" #include "log.h" #include "bufferutils.h" +#include "entitybuffer.h" +#include "entity_generated.h" #define ENTITY_TYPE_MAIL "mail" #define ENTITY_TYPE_FOLDER "folder" @@ -30,21 +32,21 @@ SINK_DEBUG_AREA("sourcewriteback") using namespace Sink; -SourceWriteBack::SourceWriteBack(const QByteArray &resourceType, const QByteArray &resourceInstanceIdentifier) - : ChangeReplay(resourceInstanceIdentifier), - mSyncStorage(Sink::storageLocation(), resourceInstanceIdentifier + ".synchronization", Sink::Storage::ReadWrite), - mResourceType(resourceType), - mResourceInstanceIdentifier(resourceInstanceIdentifier) +SourceWriteBack::SourceWriteBack(const ResourceContext &context) + : ChangeReplay(context), + mResourceContext(context), + mSyncStorage(Sink::storageLocation(), context.instanceId() + ".synchronization", Sink::Storage::DataStore::ReadWrite), + mEntityStore(QSharedPointer::create(mResourceContext)) { } EntityStore &SourceWriteBack::store() { - if (!mEntityStore) { - mEntityStore = QSharedPointer::create(mResourceType, mResourceInstanceIdentifier, mTransaction); + if (!mEntityStoreWrapper) { + mEntityStoreWrapper = QSharedPointer::create(*mEntityStore); } - return *mEntityStore; + return *mEntityStoreWrapper; } RemoteIdMap &SourceWriteBack::syncStore() @@ -76,15 +78,14 @@ KAsync::Job SourceWriteBack::replay(const QByteArray &type, const QByteArr const auto metadataBuffer = Sink::EntityBuffer::readBuffer(entity.metadata()); Q_ASSERT(metadataBuffer); Q_ASSERT(!mSyncStore); - Q_ASSERT(!mEntityStore); - Q_ASSERT(!mTransaction); + Q_ASSERT(!mEntityStoreWrapper); Q_ASSERT(!mSyncTransaction); - mTransaction = mStorage.createTransaction(Sink::Storage::ReadOnly); - mSyncTransaction = mSyncStorage.createTransaction(Sink::Storage::ReadWrite); + mEntityStore->startTransaction(Storage::DataStore::ReadOnly); + mSyncTransaction = mSyncStorage.createTransaction(Sink::Storage::DataStore::ReadWrite); // const qint64 revision = metadataBuffer ? metadataBuffer->revision() : -1; const auto operation = metadataBuffer ? metadataBuffer->operation() : Sink::Operation_Creation; - const auto uid = Sink::Storage::uidFromKey(key); + const auto uid = Sink::Storage::DataStore::uidFromKey(key); const auto modifiedProperties = metadataBuffer->modifiedProperties() ? BufferUtils::fromVector(*metadataBuffer->modifiedProperties()) : QByteArrayList(); QByteArray oldRemoteId; @@ -133,9 +134,9 @@ KAsync::Job SourceWriteBack::replay(const QByteArray &type, const QByteArr SinkWarning() << "Failed to replay change: " << error.errorMessage; } mSyncStore.clear(); - mEntityStore.clear(); - mTransaction.abort(); + mEntityStoreWrapper.clear(); mSyncTransaction.commit(); + mEntityStore->abortTransaction(); }); } -- cgit v1.2.3