diff options
Diffstat (limited to 'common/remoteidmap.cpp')
-rw-r--r-- | common/remoteidmap.cpp | 75 |
1 files changed, 75 insertions, 0 deletions
diff --git a/common/remoteidmap.cpp b/common/remoteidmap.cpp new file mode 100644 index 0000000..f72369d --- /dev/null +++ b/common/remoteidmap.cpp | |||
@@ -0,0 +1,75 @@ | |||
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 "remoteidmap.h" | ||
21 | |||
22 | #include <QUuid> | ||
23 | #include "index.h" | ||
24 | #include "log.h" | ||
25 | |||
26 | using namespace Sink; | ||
27 | |||
28 | RemoteIdMap::RemoteIdMap(Sink::Storage::Transaction &transaction) | ||
29 | : mTransaction(transaction) | ||
30 | { | ||
31 | |||
32 | } | ||
33 | |||
34 | void RemoteIdMap::recordRemoteId(const QByteArray &bufferType, const QByteArray &localId, const QByteArray &remoteId) | ||
35 | { | ||
36 | Index("rid.mapping." + bufferType, mTransaction).add(remoteId, localId); | ||
37 | Index("localid.mapping." + bufferType, mTransaction).add(localId, remoteId); | ||
38 | } | ||
39 | |||
40 | void RemoteIdMap::removeRemoteId(const QByteArray &bufferType, const QByteArray &localId, const QByteArray &remoteId) | ||
41 | { | ||
42 | Index("rid.mapping." + bufferType, mTransaction).remove(remoteId, localId); | ||
43 | Index("localid.mapping." + bufferType, mTransaction).remove(localId, remoteId); | ||
44 | } | ||
45 | |||
46 | void RemoteIdMap::updateRemoteId(const QByteArray &bufferType, const QByteArray &localId, const QByteArray &remoteId) | ||
47 | { | ||
48 | const auto oldRemoteId = Index("localid.mapping." + bufferType, mTransaction).lookup(localId); | ||
49 | removeRemoteId(bufferType, localId, oldRemoteId); | ||
50 | recordRemoteId(bufferType, localId, remoteId); | ||
51 | } | ||
52 | |||
53 | QByteArray RemoteIdMap::resolveRemoteId(const QByteArray &bufferType, const QByteArray &remoteId) | ||
54 | { | ||
55 | // Lookup local id for remote id, or insert a new pair otherwise | ||
56 | Index index("rid.mapping." + bufferType, mTransaction); | ||
57 | QByteArray sinkId = index.lookup(remoteId); | ||
58 | if (sinkId.isEmpty()) { | ||
59 | sinkId = QUuid::createUuid().toString().toUtf8(); | ||
60 | index.add(remoteId, sinkId); | ||
61 | Index("localid.mapping." + bufferType, mTransaction).add(sinkId, remoteId); | ||
62 | } | ||
63 | return sinkId; | ||
64 | } | ||
65 | |||
66 | QByteArray RemoteIdMap::resolveLocalId(const QByteArray &bufferType, const QByteArray &localId) | ||
67 | { | ||
68 | QByteArray remoteId = Index("localid.mapping." + bufferType, mTransaction).lookup(localId); | ||
69 | if (remoteId.isEmpty()) { | ||
70 | Warning() << "Couldn't find the remote id for " << localId; | ||
71 | return QByteArray(); | ||
72 | } | ||
73 | return remoteId; | ||
74 | } | ||
75 | |||