diff options
author | Christian Mollekopf <chrigi_1@fastmail.fm> | 2016-12-04 10:36:58 +0100 |
---|---|---|
committer | Christian Mollekopf <chrigi_1@fastmail.fm> | 2016-12-04 10:36:58 +0100 |
commit | 59aa460cf704d5f1a1fb1fe6b8ede4457da083ff (patch) | |
tree | d1cf394099e4897d2b8abf6817191c488c754667 | |
parent | 41006b5cab7b0260f8abc42aa2d2e959a013764e (diff) | |
download | sink-59aa460cf704d5f1a1fb1fe6b8ede4457da083ff.tar.gz sink-59aa460cf704d5f1a1fb1fe6b8ede4457da083ff.zip |
Copy command and proper move
-rw-r--r-- | common/facade.cpp | 12 | ||||
-rw-r--r-- | common/facade.h | 1 | ||||
-rw-r--r-- | common/facadeinterface.h | 12 | ||||
-rw-r--r-- | common/pipeline.cpp | 14 | ||||
-rw-r--r-- | common/resourcefacade.cpp | 6 | ||||
-rw-r--r-- | common/resourcefacade.h | 1 | ||||
-rw-r--r-- | common/storage/entitystore.cpp | 4 | ||||
-rw-r--r-- | common/storage_lmdb.cpp | 1 | ||||
-rw-r--r-- | common/store.cpp | 10 | ||||
-rw-r--r-- | common/store.h | 6 | ||||
-rw-r--r-- | common/test.cpp | 5 | ||||
-rw-r--r-- | tests/clientapitest.cpp | 4 |
12 files changed, 73 insertions, 3 deletions
diff --git a/common/facade.cpp b/common/facade.cpp index c618d4e..4f03073 100644 --- a/common/facade.cpp +++ b/common/facade.cpp | |||
@@ -80,6 +80,18 @@ KAsync::Job<void> GenericFacade<DomainType>::move(const DomainType &domainObject | |||
80 | SinkWarning() << "No domain type adaptor factory available"; | 80 | SinkWarning() << "No domain type adaptor factory available"; |
81 | return KAsync::error<void>(); | 81 | return KAsync::error<void>(); |
82 | } | 82 | } |
83 | return mResourceAccess->sendModifyCommand(domainObject.identifier(), domainObject.revision(), bufferTypeForDomainType(), QByteArrayList(), BufferUtils::extractBuffer(entityFbb), domainObject.changedProperties(), newResource, true); | ||
84 | } | ||
85 | |||
86 | template <class DomainType> | ||
87 | KAsync::Job<void> GenericFacade<DomainType>::copy(const DomainType &domainObject, const QByteArray &newResource) | ||
88 | { | ||
89 | SinkTrace() << "Copying entity: " << domainObject.identifier() << domainObject.changedProperties() << newResource; | ||
90 | flatbuffers::FlatBufferBuilder entityFbb; | ||
91 | if (!mResourceContext.adaptorFactory<DomainType>().createBuffer(domainObject, entityFbb)) { | ||
92 | SinkWarning() << "No domain type adaptor factory available"; | ||
93 | return KAsync::error<void>(); | ||
94 | } | ||
83 | return mResourceAccess->sendModifyCommand(domainObject.identifier(), domainObject.revision(), bufferTypeForDomainType(), QByteArrayList(), BufferUtils::extractBuffer(entityFbb), domainObject.changedProperties(), newResource, false); | 95 | return mResourceAccess->sendModifyCommand(domainObject.identifier(), domainObject.revision(), bufferTypeForDomainType(), QByteArrayList(), BufferUtils::extractBuffer(entityFbb), domainObject.changedProperties(), newResource, false); |
84 | } | 96 | } |
85 | 97 | ||
diff --git a/common/facade.h b/common/facade.h index 0fba34a..c10886f 100644 --- a/common/facade.h +++ b/common/facade.h | |||
@@ -64,6 +64,7 @@ public: | |||
64 | KAsync::Job<void> create(const DomainType &domainObject) Q_DECL_OVERRIDE; | 64 | KAsync::Job<void> create(const DomainType &domainObject) Q_DECL_OVERRIDE; |
65 | KAsync::Job<void> modify(const DomainType &domainObject) Q_DECL_OVERRIDE; | 65 | KAsync::Job<void> modify(const DomainType &domainObject) Q_DECL_OVERRIDE; |
66 | KAsync::Job<void> move(const DomainType &domainObject, const QByteArray &newResource) Q_DECL_OVERRIDE; | 66 | KAsync::Job<void> move(const DomainType &domainObject, const QByteArray &newResource) Q_DECL_OVERRIDE; |
67 | KAsync::Job<void> copy(const DomainType &domainObject, const QByteArray &newResource) Q_DECL_OVERRIDE; | ||
67 | KAsync::Job<void> remove(const DomainType &domainObject) Q_DECL_OVERRIDE; | 68 | KAsync::Job<void> remove(const DomainType &domainObject) Q_DECL_OVERRIDE; |
68 | virtual QPair<KAsync::Job<void>, typename ResultEmitter<typename DomainType::Ptr>::Ptr> load(const Sink::Query &query) Q_DECL_OVERRIDE; | 69 | virtual QPair<KAsync::Job<void>, typename ResultEmitter<typename DomainType::Ptr>::Ptr> load(const Sink::Query &query) Q_DECL_OVERRIDE; |
69 | 70 | ||
diff --git a/common/facadeinterface.h b/common/facadeinterface.h index 136791e..5d12360 100644 --- a/common/facadeinterface.h +++ b/common/facadeinterface.h | |||
@@ -70,6 +70,13 @@ public: | |||
70 | virtual KAsync::Job<void> move(const DomainType &domainObject, const QByteArray &newResource) = 0; | 70 | virtual KAsync::Job<void> move(const DomainType &domainObject, const QByteArray &newResource) = 0; |
71 | 71 | ||
72 | /** | 72 | /** |
73 | * Copy an entity to a new resource. | ||
74 | * | ||
75 | * The job returns succefully once the task has been successfully placed in the queue | ||
76 | */ | ||
77 | virtual KAsync::Job<void> copy(const DomainType &domainObject, const QByteArray &newResource) = 0; | ||
78 | |||
79 | /** | ||
73 | * Remove an entity from the store. | 80 | * Remove an entity from the store. |
74 | * | 81 | * |
75 | * The job returns succefully once the task has been successfully placed in the queue | 82 | * The job returns succefully once the task has been successfully placed in the queue |
@@ -102,6 +109,11 @@ public: | |||
102 | return KAsync::error<void>(-1, "Failed to create a facade"); | 109 | return KAsync::error<void>(-1, "Failed to create a facade"); |
103 | } | 110 | } |
104 | 111 | ||
112 | KAsync::Job<void> copy(const DomainType &domainObject, const QByteArray &newResource) | ||
113 | { | ||
114 | return KAsync::error<void>(-1, "Failed to create a facade"); | ||
115 | } | ||
116 | |||
105 | KAsync::Job<void> remove(const DomainType &domainObject) | 117 | KAsync::Job<void> remove(const DomainType &domainObject) |
106 | { | 118 | { |
107 | return KAsync::error<void>(-1, "Failed to create a facade"); | 119 | return KAsync::error<void>(-1, "Failed to create a facade"); |
diff --git a/common/pipeline.cpp b/common/pipeline.cpp index 8ace855..b94e3f0 100644 --- a/common/pipeline.cpp +++ b/common/pipeline.cpp | |||
@@ -249,6 +249,7 @@ KAsync::Job<qint64> Pipeline::modifiedEntity(void const *command, size_t size) | |||
249 | } | 249 | } |
250 | 250 | ||
251 | if (modifyEntity->targetResource()) { | 251 | if (modifyEntity->targetResource()) { |
252 | auto isMove = modifyEntity->removeEntity(); | ||
252 | auto targetResource = BufferUtils::extractBuffer(modifyEntity->targetResource()); | 253 | auto targetResource = BufferUtils::extractBuffer(modifyEntity->targetResource()); |
253 | auto changeset = diff.changedProperties(); | 254 | auto changeset = diff.changedProperties(); |
254 | const auto current = d->entityStore.readLatest(bufferType, diff.identifier()); | 255 | const auto current = d->entityStore.readLatest(bufferType, diff.identifier()); |
@@ -276,9 +277,20 @@ KAsync::Job<qint64> Pipeline::modifiedEntity(void const *command, size_t size) | |||
276 | 277 | ||
277 | SinkTrace() << "Moving entity to new resource " << newEntity.identifier() << newEntity.resourceInstanceIdentifier() << targetResource; | 278 | SinkTrace() << "Moving entity to new resource " << newEntity.identifier() << newEntity.resourceInstanceIdentifier() << targetResource; |
278 | auto job = TypeHelper<CreateHelper>{bufferType}.operator()<KAsync::Job<void>, ApplicationDomain::ApplicationDomainType&>(newEntity); | 279 | auto job = TypeHelper<CreateHelper>{bufferType}.operator()<KAsync::Job<void>, ApplicationDomain::ApplicationDomainType&>(newEntity); |
279 | job = job.syncThen<void>([=](const KAsync::Error &error) { | 280 | job = job.syncThen<void>([this, newEntity, isMove, targetResource, bufferType](const KAsync::Error &error) { |
280 | if (!error) { | 281 | if (!error) { |
281 | SinkTrace() << "Move of " << newEntity.identifier() << "was successfull"; | 282 | SinkTrace() << "Move of " << newEntity.identifier() << "was successfull"; |
283 | if (isMove) { | ||
284 | startTransaction(); | ||
285 | flatbuffers::FlatBufferBuilder fbb; | ||
286 | auto entityId = fbb.CreateString(newEntity.identifier()); | ||
287 | auto type = fbb.CreateString(bufferType); | ||
288 | auto location = Sink::Commands::CreateDeleteEntity(fbb, newEntity.revision(), entityId, type, true); | ||
289 | Sink::Commands::FinishDeleteEntityBuffer(fbb, location); | ||
290 | const auto data = BufferUtils::extractBuffer(fbb); | ||
291 | deletedEntity(data, data.size()).exec(); | ||
292 | commit(); | ||
293 | } | ||
282 | } else { | 294 | } else { |
283 | SinkError() << "Failed to move entity " << targetResource << " to resource " << newEntity.identifier(); | 295 | SinkError() << "Failed to move entity " << targetResource << " to resource " << newEntity.identifier(); |
284 | } | 296 | } |
diff --git a/common/resourcefacade.cpp b/common/resourcefacade.cpp index 0a05bd9..c4e16b1 100644 --- a/common/resourcefacade.cpp +++ b/common/resourcefacade.cpp | |||
@@ -225,6 +225,12 @@ KAsync::Job<void> LocalStorageFacade<DomainType>::move(const DomainType &, const | |||
225 | } | 225 | } |
226 | 226 | ||
227 | template <typename DomainType> | 227 | template <typename DomainType> |
228 | KAsync::Job<void> LocalStorageFacade<DomainType>::copy(const DomainType &, const QByteArray &) | ||
229 | { | ||
230 | return KAsync::error<void>(1, "Resources and Accounts cannot be copied."); | ||
231 | } | ||
232 | |||
233 | template <typename DomainType> | ||
228 | KAsync::Job<void> LocalStorageFacade<DomainType>::remove(const DomainType &domainObject) | 234 | KAsync::Job<void> LocalStorageFacade<DomainType>::remove(const DomainType &domainObject) |
229 | { | 235 | { |
230 | auto configStoreIdentifier = mIdentifier; | 236 | auto configStoreIdentifier = mIdentifier; |
diff --git a/common/resourcefacade.h b/common/resourcefacade.h index ea552c2..4575e72 100644 --- a/common/resourcefacade.h +++ b/common/resourcefacade.h | |||
@@ -82,6 +82,7 @@ public: | |||
82 | virtual KAsync::Job<void> create(const DomainType &resource) Q_DECL_OVERRIDE; | 82 | virtual KAsync::Job<void> create(const DomainType &resource) Q_DECL_OVERRIDE; |
83 | virtual KAsync::Job<void> modify(const DomainType &resource) Q_DECL_OVERRIDE; | 83 | virtual KAsync::Job<void> modify(const DomainType &resource) Q_DECL_OVERRIDE; |
84 | virtual KAsync::Job<void> move(const DomainType &resource, const QByteArray &) Q_DECL_OVERRIDE; | 84 | virtual KAsync::Job<void> move(const DomainType &resource, const QByteArray &) Q_DECL_OVERRIDE; |
85 | virtual KAsync::Job<void> copy(const DomainType &resource, const QByteArray &) Q_DECL_OVERRIDE; | ||
85 | virtual KAsync::Job<void> remove(const DomainType &resource) Q_DECL_OVERRIDE; | 86 | virtual KAsync::Job<void> remove(const DomainType &resource) Q_DECL_OVERRIDE; |
86 | virtual QPair<KAsync::Job<void>, typename Sink::ResultEmitter<typename DomainType::Ptr>::Ptr> load(const Sink::Query &query) Q_DECL_OVERRIDE; | 87 | virtual QPair<KAsync::Job<void>, typename Sink::ResultEmitter<typename DomainType::Ptr>::Ptr> load(const Sink::Query &query) Q_DECL_OVERRIDE; |
87 | 88 | ||
diff --git a/common/storage/entitystore.cpp b/common/storage/entitystore.cpp index b6251b6..1417861 100644 --- a/common/storage/entitystore.cpp +++ b/common/storage/entitystore.cpp | |||
@@ -239,11 +239,11 @@ bool EntityStore::remove(const QByteArray &type, const QByteArray &uid, bool rep | |||
239 | [](const DataStore::Error &error) { SinkWarning() << "Failed to read old revision from storage: " << error.message; }); | 239 | [](const DataStore::Error &error) { SinkWarning() << "Failed to read old revision from storage: " << error.message; }); |
240 | 240 | ||
241 | if (!found) { | 241 | if (!found) { |
242 | SinkWarning() << "Failed to find entity " << uid; | 242 | SinkWarning() << "Remove: Failed to find entity " << uid; |
243 | return false; | 243 | return false; |
244 | } | 244 | } |
245 | if (alreadyRemoved) { | 245 | if (alreadyRemoved) { |
246 | SinkWarning() << "Entity is already removed " << uid; | 246 | SinkWarning() << "Remove: Entity is already removed " << uid; |
247 | return false; | 247 | return false; |
248 | } | 248 | } |
249 | 249 | ||
diff --git a/common/storage_lmdb.cpp b/common/storage_lmdb.cpp index e418472..fa99a80 100644 --- a/common/storage_lmdb.cpp +++ b/common/storage_lmdb.cpp | |||
@@ -520,6 +520,7 @@ bool DataStore::Transaction::validateNamedDatabases() | |||
520 | DataStore::NamedDatabase DataStore::Transaction::openDatabase(const QByteArray &db, const std::function<void(const DataStore::Error &error)> &errorHandler, bool allowDuplicates) const | 520 | DataStore::NamedDatabase DataStore::Transaction::openDatabase(const QByteArray &db, const std::function<void(const DataStore::Error &error)> &errorHandler, bool allowDuplicates) const |
521 | { | 521 | { |
522 | if (!d) { | 522 | if (!d) { |
523 | SinkError() << "Tried to open database on invalid transaction: " << db; | ||
523 | return DataStore::NamedDatabase(); | 524 | return DataStore::NamedDatabase(); |
524 | } | 525 | } |
525 | Q_ASSERT(d->transaction); | 526 | Q_ASSERT(d->transaction); |
diff --git a/common/store.cpp b/common/store.cpp index fad8c5e..ef8a593 100644 --- a/common/store.cpp +++ b/common/store.cpp | |||
@@ -225,6 +225,15 @@ KAsync::Job<void> Store::move(const DomainType &domainObject, const QByteArray & | |||
225 | } | 225 | } |
226 | 226 | ||
227 | template <class DomainType> | 227 | template <class DomainType> |
228 | KAsync::Job<void> Store::copy(const DomainType &domainObject, const QByteArray &newResource) | ||
229 | { | ||
230 | SinkTrace() << "Copy: " << domainObject << newResource; | ||
231 | // Potentially copy to separate thread as well | ||
232 | auto facade = getFacade<DomainType>(domainObject.resourceInstanceIdentifier()); | ||
233 | return facade->copy(domainObject, newResource).addToContext(std::shared_ptr<void>(facade)).onError([](const KAsync::Error &error) { SinkWarning() << "Failed to copy"; }); | ||
234 | } | ||
235 | |||
236 | template <class DomainType> | ||
228 | KAsync::Job<void> Store::remove(const DomainType &domainObject) | 237 | KAsync::Job<void> Store::remove(const DomainType &domainObject) |
229 | { | 238 | { |
230 | SinkTrace() << "Remove: " << domainObject; | 239 | SinkTrace() << "Remove: " << domainObject; |
@@ -396,6 +405,7 @@ QList<DomainType> Store::read(const Sink::Query &q) | |||
396 | template KAsync::Job<void> Store::create<T>(const T &domainObject); \ | 405 | template KAsync::Job<void> Store::create<T>(const T &domainObject); \ |
397 | template KAsync::Job<void> Store::modify<T>(const T &domainObject); \ | 406 | template KAsync::Job<void> Store::modify<T>(const T &domainObject); \ |
398 | template KAsync::Job<void> Store::move<T>(const T &domainObject, const QByteArray &newResource); \ | 407 | template KAsync::Job<void> Store::move<T>(const T &domainObject, const QByteArray &newResource); \ |
408 | template KAsync::Job<void> Store::copy<T>(const T &domainObject, const QByteArray &newResource); \ | ||
399 | template QSharedPointer<QAbstractItemModel> Store::loadModel<T>(Query query); \ | 409 | template QSharedPointer<QAbstractItemModel> Store::loadModel<T>(Query query); \ |
400 | template KAsync::Job<T> Store::fetchOne<T>(const Query &); \ | 410 | template KAsync::Job<T> Store::fetchOne<T>(const Query &); \ |
401 | template KAsync::Job<QList<T::Ptr>> Store::fetchAll<T>(const Query &); \ | 411 | template KAsync::Job<QList<T::Ptr>> Store::fetchAll<T>(const Query &); \ |
diff --git a/common/store.h b/common/store.h index 7c7d2fe..b261f47 100644 --- a/common/store.h +++ b/common/store.h | |||
@@ -88,6 +88,12 @@ template <class DomainType> | |||
88 | KAsync::Job<void> SINK_EXPORT move(const DomainType &domainObject, const QByteArray &newResource); | 88 | KAsync::Job<void> SINK_EXPORT move(const DomainType &domainObject, const QByteArray &newResource); |
89 | 89 | ||
90 | /** | 90 | /** |
91 | * Copy an entity to a new resource. | ||
92 | */ | ||
93 | template <class DomainType> | ||
94 | KAsync::Job<void> SINK_EXPORT copy(const DomainType &domainObject, const QByteArray &newResource); | ||
95 | |||
96 | /** | ||
91 | * Synchronize data to local cache. | 97 | * Synchronize data to local cache. |
92 | */ | 98 | */ |
93 | KAsync::Job<void> SINK_EXPORT synchronize(const Sink::Query &query); | 99 | KAsync::Job<void> SINK_EXPORT synchronize(const Sink::Query &query); |
diff --git a/common/test.cpp b/common/test.cpp index 74c499c..0c7ba10 100644 --- a/common/test.cpp +++ b/common/test.cpp | |||
@@ -130,6 +130,11 @@ public: | |||
130 | // mTestAccount->moveEntity<T>(domainObject, newResource); | 130 | // mTestAccount->moveEntity<T>(domainObject, newResource); |
131 | return KAsync::null<void>(); | 131 | return KAsync::null<void>(); |
132 | }; | 132 | }; |
133 | KAsync::Job<void> copy(const T &domainObject, const QByteArray &newResource) Q_DECL_OVERRIDE | ||
134 | { | ||
135 | // mTestAccount->copyEntity<T>(domainObject, newResource); | ||
136 | return KAsync::null<void>(); | ||
137 | }; | ||
133 | KAsync::Job<void> remove(const T &domainObject) Q_DECL_OVERRIDE | 138 | KAsync::Job<void> remove(const T &domainObject) Q_DECL_OVERRIDE |
134 | { | 139 | { |
135 | //FIXME | 140 | //FIXME |
diff --git a/tests/clientapitest.cpp b/tests/clientapitest.cpp index 71be514..4a33d17 100644 --- a/tests/clientapitest.cpp +++ b/tests/clientapitest.cpp | |||
@@ -45,6 +45,10 @@ public: | |||
45 | { | 45 | { |
46 | return KAsync::null<void>(); | 46 | return KAsync::null<void>(); |
47 | }; | 47 | }; |
48 | KAsync::Job<void> copy(const T &domainObject, const QByteArray &) Q_DECL_OVERRIDE | ||
49 | { | ||
50 | return KAsync::null<void>(); | ||
51 | }; | ||
48 | KAsync::Job<void> remove(const T &domainObject) Q_DECL_OVERRIDE | 52 | KAsync::Job<void> remove(const T &domainObject) Q_DECL_OVERRIDE |
49 | { | 53 | { |
50 | return KAsync::null<void>(); | 54 | return KAsync::null<void>(); |