summaryrefslogtreecommitdiffstats
path: root/common/storage/key.cpp
diff options
context:
space:
mode:
authorRémi Nicole <nicole@kolabsystems.com>2018-07-27 13:32:39 +0200
committerChristian Mollekopf <chrigi_1@fastmail.fm>2018-07-27 13:47:43 +0200
commitd1838e575baeb6cd08011645609516acbdabd6c8 (patch)
tree035b99a4d324231c8c44e0e97905383266bacba6 /common/storage/key.cpp
parent1855b8356e4c427efdba4c932fa9f984e6ae5a43 (diff)
downloadsink-d1838e575baeb6cd08011645609516acbdabd6c8.tar.gz
sink-d1838e575baeb6cd08011645609516acbdabd6c8.zip
New Key API in storage layer
Summary: - Use object oriented paradigm for Keys / Identifiers /Revisions - "Compress" keys by using byte representation of Uuids - Still some cleaning left to do - Also run some benchmarks - I'm questioning whether files other than entitystore (tests excluded) are allowed to access this API Reviewers: cmollekopf Reviewed By: cmollekopf Tags: #sink Differential Revision: https://phabricator.kde.org/D13735
Diffstat (limited to 'common/storage/key.cpp')
-rw-r--r--common/storage/key.cpp156
1 files changed, 156 insertions, 0 deletions
diff --git a/common/storage/key.cpp b/common/storage/key.cpp
new file mode 100644
index 0000000..01a3e3a
--- /dev/null
+++ b/common/storage/key.cpp
@@ -0,0 +1,156 @@
1/*
2 * Copyright (C) 2014 Christian Mollekopf <chrigi_1@fastmail.fm>
3 * Copyright (C) 2018 Rémi Nicole <minijackson@riseup.net>
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) version 3, or any
9 * later version accepted by the membership of KDE e.V. (or its
10 * successor approved by the membership of KDE e.V.), which shall
11 * act as a proxy defined in Section 6 of version 3 of the license.
12 *
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library. If not, see <http://www.gnu.org/licenses/>.
20 */
21
22#include "key.h"
23#include "utils.h"
24
25using Sink::Storage::Identifier;
26using Sink::Storage::Key;
27using Sink::Storage::Revision;
28
29QDebug &operator<<(QDebug &dbg, const Identifier &id)
30{
31 dbg << id.toDisplayString();
32 return dbg;
33}
34
35QDebug &operator<<(QDebug &dbg, const Revision &rev)
36{
37 dbg << rev.toDisplayString();
38 return dbg;
39}
40
41QDebug &operator<<(QDebug &dbg, const Key &key)
42{
43 dbg << key.toDisplayString();
44 return dbg;
45}
46
47// Identifier
48
49QByteArray Identifier::toInternalByteArray() const
50{
51 return uid.toRfc4122();
52}
53
54Identifier Identifier::fromInternalByteArray(const QByteArray &bytes)
55{
56 Q_ASSERT(bytes.size() == INTERNAL_REPR_SIZE);
57 return Identifier(QUuid::fromRfc4122(bytes));
58}
59
60QString Identifier::toDisplayString() const
61{
62 return uid.toString();
63}
64
65QByteArray Identifier::toDisplayByteArray() const
66{
67 return uid.toByteArray();
68}
69
70Identifier Identifier::fromDisplayByteArray(const QByteArray &bytes)
71{
72 Q_ASSERT(bytes.size() == DISPLAY_REPR_SIZE);
73 return Identifier(QUuid(bytes));
74}
75
76// Revision
77
78QByteArray Revision::toInternalByteArray() const
79{
80 return padNumber(rev);
81}
82
83Revision Revision::fromInternalByteArray(const QByteArray &bytes)
84{
85 Q_ASSERT(bytes.size() == INTERNAL_REPR_SIZE);
86 return Revision(bytes.toLongLong());
87}
88
89QString Revision::toDisplayString() const
90{
91 return QString::fromUtf8(toInternalByteArray());
92}
93
94QByteArray Revision::toDisplayByteArray() const
95{
96 return toInternalByteArray();
97}
98
99Revision Revision::fromDisplayByteArray(const QByteArray &bytes)
100{
101 Q_ASSERT(bytes.size() == DISPLAY_REPR_SIZE);
102 return fromInternalByteArray(bytes);
103}
104
105qint64 Revision::toQint64() const
106{
107 return rev;
108}
109
110// Key
111
112QByteArray Key::toInternalByteArray() const
113{
114 return id.toInternalByteArray() + rev.toInternalByteArray();
115}
116
117Key Key::fromInternalByteArray(const QByteArray &bytes)
118{
119 Q_ASSERT(bytes.size() == INTERNAL_REPR_SIZE);
120 auto idBytes = bytes.mid(0, Identifier::INTERNAL_REPR_SIZE);
121 auto revBytes = bytes.mid(Identifier::INTERNAL_REPR_SIZE);
122 return Key(Identifier::fromInternalByteArray(idBytes), Revision::fromInternalByteArray(revBytes));
123}
124
125QString Key::toDisplayString() const
126{
127 return id.toDisplayString() + rev.toDisplayString();
128}
129
130QByteArray Key::toDisplayByteArray() const
131{
132 return id.toDisplayByteArray() + rev.toDisplayByteArray();
133}
134
135Key Key::fromDisplayByteArray(const QByteArray &bytes)
136{
137 Q_ASSERT(bytes.size() == DISPLAY_REPR_SIZE);
138 auto idBytes = bytes.mid(0, Identifier::DISPLAY_REPR_SIZE);
139 auto revBytes = bytes.mid(Identifier::DISPLAY_REPR_SIZE);
140 return Key(Identifier::fromDisplayByteArray(idBytes), Revision::fromDisplayByteArray(revBytes));
141}
142
143const Identifier &Key::identifier() const
144{
145 return id;
146}
147
148const Revision &Key::revision() const
149{
150 return rev;
151}
152
153void Key::setRevision(const Revision &newRev)
154{
155 rev = newRev;
156}