summaryrefslogtreecommitdiffstats
path: root/common/synchronizer.h
diff options
context:
space:
mode:
authorChristian Mollekopf <chrigi_1@fastmail.fm>2016-05-28 02:09:58 +0200
committerChristian Mollekopf <chrigi_1@fastmail.fm>2016-05-28 02:09:58 +0200
commitb441386c4e138d19bbd79d578e0a2ff1b3f54a93 (patch)
tree1110b6ec00ce29a8bcd7f6db0717f4a483f50587 /common/synchronizer.h
parentafb29c153daff23e491a350784ce6af5db5e28af (diff)
downloadsink-b441386c4e138d19bbd79d578e0a2ff1b3f54a93.tar.gz
sink-b441386c4e138d19bbd79d578e0a2ff1b3f54a93.zip
Moved the classes to individual files
Diffstat (limited to 'common/synchronizer.h')
-rw-r--r--common/synchronizer.h95
1 files changed, 95 insertions, 0 deletions
diff --git a/common/synchronizer.h b/common/synchronizer.h
new file mode 100644
index 0000000..61bca7d
--- /dev/null
+++ b/common/synchronizer.h
@@ -0,0 +1,95 @@
1/*
2 * Copyright (C) 2016 Christian Mollekopf <mollekopf@kolabsys.com>
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) version 3, or any
8 * later version accepted by the membership of KDE e.V. (or its
9 * successor approved by the membership of KDE e.V.), which shall
10 * act as a proxy defined in Section 6 of version 3 of the license.
11 *
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library. If not, see <http://www.gnu.org/licenses/>.
19 */
20#pragma once
21
22#include "sink_export.h"
23#include <QObject>
24#include <Async/Async>
25#include <domainadaptor.h>
26
27#include "storage.h"
28
29namespace Sink {
30class EntityStore;
31class RemoteIdMap;
32
33/**
34 * Synchronize and add what we don't already have to local queue
35 */
36class SINK_EXPORT Synchronizer
37{
38public:
39 Synchronizer(const QByteArray &resourceType, const QByteArray &resourceInstanceIdentifier);
40
41 void setup(const std::function<void(int commandId, const QByteArray &data)> &enqueueCommandCallback);
42 KAsync::Job<void> synchronize();
43
44protected:
45 ///Calls the callback to enqueue the command
46 void enqueueCommand(int commandId, const QByteArray &data);
47
48 static void createEntity(const QByteArray &localId, const QByteArray &bufferType, const Sink::ApplicationDomain::ApplicationDomainType &domainObject,
49 DomainTypeAdaptorFactoryInterface &adaptorFactory, std::function<void(const QByteArray &)> callback);
50 static void modifyEntity(const QByteArray &localId, qint64 revision, const QByteArray &bufferType, const Sink::ApplicationDomain::ApplicationDomainType &domainObject,
51 DomainTypeAdaptorFactoryInterface &adaptorFactory, std::function<void(const QByteArray &)> callback);
52 static void deleteEntity(const QByteArray &localId, qint64 revision, const QByteArray &bufferType, std::function<void(const QByteArray &)> callback);
53
54 /**
55 * A synchronous algorithm to remove entities that are no longer existing.
56 *
57 * A list of entities is generated by @param entryGenerator.
58 * The entiry Generator typically iterates over an index to produce all existing entries.
59 * This algorithm calls @param exists for every entity of type @param type, with its remoteId. For every entity where @param exists returns false,
60 * an entity delete command is enqueued.
61 *
62 * All functions are called synchronously, and both @param entryGenerator and @param exists need to be synchronous.
63 */
64 void scanForRemovals(const QByteArray &bufferType,
65 const std::function<void(const std::function<void(const QByteArray &key)> &callback)> &entryGenerator, std::function<bool(const QByteArray &remoteId)> exists);
66
67 /**
68 * An algorithm to create or modify the entity.
69 *
70 * Depending on whether the entity is locally available, or has changed.
71 */
72 void createOrModify(const QByteArray &bufferType, const QByteArray &remoteId, const Sink::ApplicationDomain::ApplicationDomainType &entity);
73
74 //Read only access to main storage
75 EntityStore &store();
76
77 //Read/Write access to sync storage
78 RemoteIdMap &syncStore();
79
80 virtual KAsync::Job<void> synchronizeWithSource() = 0;
81
82private:
83 QSharedPointer<RemoteIdMap> mSyncStore;
84 QSharedPointer<EntityStore> mEntityStore;
85 Sink::Storage mStorage;
86 Sink::Storage mSyncStorage;
87 QByteArray mResourceType;
88 QByteArray mResourceInstanceIdentifier;
89 Sink::Storage::Transaction mTransaction;
90 Sink::Storage::Transaction mSyncTransaction;
91 std::function<void(int commandId, const QByteArray &data)> mEnqueue;
92};
93
94}
95