diff options
Diffstat (limited to 'common/synchronizerstore.cpp')
-rw-r--r-- | common/synchronizerstore.cpp | 103 |
1 files changed, 103 insertions, 0 deletions
diff --git a/common/synchronizerstore.cpp b/common/synchronizerstore.cpp new file mode 100644 index 0000000..fcb3ed0 --- /dev/null +++ b/common/synchronizerstore.cpp | |||
@@ -0,0 +1,103 @@ | |||
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 | #include "synchronizerstore.h" | ||
21 | |||
22 | #include <QUuid> | ||
23 | #include "index.h" | ||
24 | #include "log.h" | ||
25 | |||
26 | using namespace Sink; | ||
27 | |||
28 | SINK_DEBUG_AREA("synchronizerstore") | ||
29 | |||
30 | SynchronizerStore::SynchronizerStore(Sink::Storage::DataStore::Transaction &transaction) | ||
31 | : mTransaction(transaction) | ||
32 | { | ||
33 | |||
34 | } | ||
35 | |||
36 | void SynchronizerStore::recordRemoteId(const QByteArray &bufferType, const QByteArray &localId, const QByteArray &remoteId) | ||
37 | { | ||
38 | Index("rid.mapping." + bufferType, mTransaction).add(remoteId, localId); | ||
39 | Index("localid.mapping." + bufferType, mTransaction).add(localId, remoteId); | ||
40 | } | ||
41 | |||
42 | void SynchronizerStore::removeRemoteId(const QByteArray &bufferType, const QByteArray &localId, const QByteArray &remoteId) | ||
43 | { | ||
44 | Index("rid.mapping." + bufferType, mTransaction).remove(remoteId, localId); | ||
45 | Index("localid.mapping." + bufferType, mTransaction).remove(localId, remoteId); | ||
46 | } | ||
47 | |||
48 | void SynchronizerStore::updateRemoteId(const QByteArray &bufferType, const QByteArray &localId, const QByteArray &remoteId) | ||
49 | { | ||
50 | const auto oldRemoteId = Index("localid.mapping." + bufferType, mTransaction).lookup(localId); | ||
51 | removeRemoteId(bufferType, localId, oldRemoteId); | ||
52 | recordRemoteId(bufferType, localId, remoteId); | ||
53 | } | ||
54 | |||
55 | QByteArray SynchronizerStore::resolveRemoteId(const QByteArray &bufferType, const QByteArray &remoteId) | ||
56 | { | ||
57 | // Lookup local id for remote id, or insert a new pair otherwise | ||
58 | Index index("rid.mapping." + bufferType, mTransaction); | ||
59 | QByteArray sinkId = index.lookup(remoteId); | ||
60 | if (sinkId.isEmpty()) { | ||
61 | sinkId = Sink::Storage::DataStore::generateUid(); | ||
62 | index.add(remoteId, sinkId); | ||
63 | Index("localid.mapping." + bufferType, mTransaction).add(sinkId, remoteId); | ||
64 | } | ||
65 | return sinkId; | ||
66 | } | ||
67 | |||
68 | QByteArray SynchronizerStore::resolveLocalId(const QByteArray &bufferType, const QByteArray &localId) | ||
69 | { | ||
70 | QByteArray remoteId = Index("localid.mapping." + bufferType, mTransaction).lookup(localId); | ||
71 | if (remoteId.isEmpty()) { | ||
72 | SinkWarning() << "Couldn't find the remote id for " << localId; | ||
73 | return QByteArray(); | ||
74 | } | ||
75 | return remoteId; | ||
76 | } | ||
77 | |||
78 | QByteArrayList SynchronizerStore::resolveLocalIds(const QByteArray &bufferType, const QByteArrayList &localIds) | ||
79 | { | ||
80 | QByteArrayList result; | ||
81 | for (const auto &l : localIds) { | ||
82 | result << resolveLocalId(bufferType, l); | ||
83 | } | ||
84 | return result; | ||
85 | } | ||
86 | |||
87 | QByteArray SynchronizerStore::readValue(const QByteArray &key) | ||
88 | { | ||
89 | QByteArray value; | ||
90 | mTransaction.openDatabase("values").scan(key, [&value](const QByteArray &, const QByteArray &v) { | ||
91 | value = v; | ||
92 | return false; | ||
93 | }, [](const Sink::Storage::DataStore::Error &) { | ||
94 | //Ignore errors because we may not find the value | ||
95 | }); | ||
96 | return value; | ||
97 | } | ||
98 | |||
99 | void SynchronizerStore::writeValue(const QByteArray &key, const QByteArray &value) | ||
100 | { | ||
101 | mTransaction.openDatabase("values").write(key, value); | ||
102 | } | ||
103 | |||