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 /common/sourcewriteback.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 'common/sourcewriteback.cpp')
-rw-r--r-- | common/sourcewriteback.cpp | 31 |
1 files changed, 16 insertions, 15 deletions
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 @@ | |||
22 | #include "definitions.h" | 22 | #include "definitions.h" |
23 | #include "log.h" | 23 | #include "log.h" |
24 | #include "bufferutils.h" | 24 | #include "bufferutils.h" |
25 | #include "entitybuffer.h" | ||
26 | #include "entity_generated.h" | ||
25 | 27 | ||
26 | #define ENTITY_TYPE_MAIL "mail" | 28 | #define ENTITY_TYPE_MAIL "mail" |
27 | #define ENTITY_TYPE_FOLDER "folder" | 29 | #define ENTITY_TYPE_FOLDER "folder" |
@@ -30,21 +32,21 @@ SINK_DEBUG_AREA("sourcewriteback") | |||
30 | 32 | ||
31 | using namespace Sink; | 33 | using namespace Sink; |
32 | 34 | ||
33 | SourceWriteBack::SourceWriteBack(const QByteArray &resourceType, const QByteArray &resourceInstanceIdentifier) | 35 | SourceWriteBack::SourceWriteBack(const ResourceContext &context) |
34 | : ChangeReplay(resourceInstanceIdentifier), | 36 | : ChangeReplay(context), |
35 | mSyncStorage(Sink::storageLocation(), resourceInstanceIdentifier + ".synchronization", Sink::Storage::ReadWrite), | 37 | mResourceContext(context), |
36 | mResourceType(resourceType), | 38 | mSyncStorage(Sink::storageLocation(), context.instanceId() + ".synchronization", Sink::Storage::DataStore::ReadWrite), |
37 | mResourceInstanceIdentifier(resourceInstanceIdentifier) | 39 | mEntityStore(QSharedPointer<Storage::EntityStore>::create(mResourceContext)) |
38 | { | 40 | { |
39 | 41 | ||
40 | } | 42 | } |
41 | 43 | ||
42 | EntityStore &SourceWriteBack::store() | 44 | EntityStore &SourceWriteBack::store() |
43 | { | 45 | { |
44 | if (!mEntityStore) { | 46 | if (!mEntityStoreWrapper) { |
45 | mEntityStore = QSharedPointer<EntityStore>::create(mResourceType, mResourceInstanceIdentifier, mTransaction); | 47 | mEntityStoreWrapper = QSharedPointer<EntityStore>::create(*mEntityStore); |
46 | } | 48 | } |
47 | return *mEntityStore; | 49 | return *mEntityStoreWrapper; |
48 | } | 50 | } |
49 | 51 | ||
50 | RemoteIdMap &SourceWriteBack::syncStore() | 52 | RemoteIdMap &SourceWriteBack::syncStore() |
@@ -76,15 +78,14 @@ KAsync::Job<void> SourceWriteBack::replay(const QByteArray &type, const QByteArr | |||
76 | const auto metadataBuffer = Sink::EntityBuffer::readBuffer<Sink::Metadata>(entity.metadata()); | 78 | const auto metadataBuffer = Sink::EntityBuffer::readBuffer<Sink::Metadata>(entity.metadata()); |
77 | Q_ASSERT(metadataBuffer); | 79 | Q_ASSERT(metadataBuffer); |
78 | Q_ASSERT(!mSyncStore); | 80 | Q_ASSERT(!mSyncStore); |
79 | Q_ASSERT(!mEntityStore); | 81 | Q_ASSERT(!mEntityStoreWrapper); |
80 | Q_ASSERT(!mTransaction); | ||
81 | Q_ASSERT(!mSyncTransaction); | 82 | Q_ASSERT(!mSyncTransaction); |
82 | mTransaction = mStorage.createTransaction(Sink::Storage::ReadOnly); | 83 | mEntityStore->startTransaction(Storage::DataStore::ReadOnly); |
83 | mSyncTransaction = mSyncStorage.createTransaction(Sink::Storage::ReadWrite); | 84 | mSyncTransaction = mSyncStorage.createTransaction(Sink::Storage::DataStore::ReadWrite); |
84 | 85 | ||
85 | // const qint64 revision = metadataBuffer ? metadataBuffer->revision() : -1; | 86 | // const qint64 revision = metadataBuffer ? metadataBuffer->revision() : -1; |
86 | const auto operation = metadataBuffer ? metadataBuffer->operation() : Sink::Operation_Creation; | 87 | const auto operation = metadataBuffer ? metadataBuffer->operation() : Sink::Operation_Creation; |
87 | const auto uid = Sink::Storage::uidFromKey(key); | 88 | const auto uid = Sink::Storage::DataStore::uidFromKey(key); |
88 | const auto modifiedProperties = metadataBuffer->modifiedProperties() ? BufferUtils::fromVector(*metadataBuffer->modifiedProperties()) : QByteArrayList(); | 89 | const auto modifiedProperties = metadataBuffer->modifiedProperties() ? BufferUtils::fromVector(*metadataBuffer->modifiedProperties()) : QByteArrayList(); |
89 | QByteArray oldRemoteId; | 90 | QByteArray oldRemoteId; |
90 | 91 | ||
@@ -133,9 +134,9 @@ KAsync::Job<void> SourceWriteBack::replay(const QByteArray &type, const QByteArr | |||
133 | SinkWarning() << "Failed to replay change: " << error.errorMessage; | 134 | SinkWarning() << "Failed to replay change: " << error.errorMessage; |
134 | } | 135 | } |
135 | mSyncStore.clear(); | 136 | mSyncStore.clear(); |
136 | mEntityStore.clear(); | 137 | mEntityStoreWrapper.clear(); |
137 | mTransaction.abort(); | ||
138 | mSyncTransaction.commit(); | 138 | mSyncTransaction.commit(); |
139 | mEntityStore->abortTransaction(); | ||
139 | }); | 140 | }); |
140 | } | 141 | } |
141 | 142 | ||