diff options
Diffstat (limited to 'common/storage/key.cpp')
-rw-r--r-- | common/storage/key.cpp | 156 |
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 | |||
25 | using Sink::Storage::Identifier; | ||
26 | using Sink::Storage::Key; | ||
27 | using Sink::Storage::Revision; | ||
28 | |||
29 | QDebug &operator<<(QDebug &dbg, const Identifier &id) | ||
30 | { | ||
31 | dbg << id.toDisplayString(); | ||
32 | return dbg; | ||
33 | } | ||
34 | |||
35 | QDebug &operator<<(QDebug &dbg, const Revision &rev) | ||
36 | { | ||
37 | dbg << rev.toDisplayString(); | ||
38 | return dbg; | ||
39 | } | ||
40 | |||
41 | QDebug &operator<<(QDebug &dbg, const Key &key) | ||
42 | { | ||
43 | dbg << key.toDisplayString(); | ||
44 | return dbg; | ||
45 | } | ||
46 | |||
47 | // Identifier | ||
48 | |||
49 | QByteArray Identifier::toInternalByteArray() const | ||
50 | { | ||
51 | return uid.toRfc4122(); | ||
52 | } | ||
53 | |||
54 | Identifier Identifier::fromInternalByteArray(const QByteArray &bytes) | ||
55 | { | ||
56 | Q_ASSERT(bytes.size() == INTERNAL_REPR_SIZE); | ||
57 | return Identifier(QUuid::fromRfc4122(bytes)); | ||
58 | } | ||
59 | |||
60 | QString Identifier::toDisplayString() const | ||
61 | { | ||
62 | return uid.toString(); | ||
63 | } | ||
64 | |||
65 | QByteArray Identifier::toDisplayByteArray() const | ||
66 | { | ||
67 | return uid.toByteArray(); | ||
68 | } | ||
69 | |||
70 | Identifier Identifier::fromDisplayByteArray(const QByteArray &bytes) | ||
71 | { | ||
72 | Q_ASSERT(bytes.size() == DISPLAY_REPR_SIZE); | ||
73 | return Identifier(QUuid(bytes)); | ||
74 | } | ||
75 | |||
76 | // Revision | ||
77 | |||
78 | QByteArray Revision::toInternalByteArray() const | ||
79 | { | ||
80 | return padNumber(rev); | ||
81 | } | ||
82 | |||
83 | Revision Revision::fromInternalByteArray(const QByteArray &bytes) | ||
84 | { | ||
85 | Q_ASSERT(bytes.size() == INTERNAL_REPR_SIZE); | ||
86 | return Revision(bytes.toLongLong()); | ||
87 | } | ||
88 | |||
89 | QString Revision::toDisplayString() const | ||
90 | { | ||
91 | return QString::fromUtf8(toInternalByteArray()); | ||
92 | } | ||
93 | |||
94 | QByteArray Revision::toDisplayByteArray() const | ||
95 | { | ||
96 | return toInternalByteArray(); | ||
97 | } | ||
98 | |||
99 | Revision Revision::fromDisplayByteArray(const QByteArray &bytes) | ||
100 | { | ||
101 | Q_ASSERT(bytes.size() == DISPLAY_REPR_SIZE); | ||
102 | return fromInternalByteArray(bytes); | ||
103 | } | ||
104 | |||
105 | qint64 Revision::toQint64() const | ||
106 | { | ||
107 | return rev; | ||
108 | } | ||
109 | |||
110 | // Key | ||
111 | |||
112 | QByteArray Key::toInternalByteArray() const | ||
113 | { | ||
114 | return id.toInternalByteArray() + rev.toInternalByteArray(); | ||
115 | } | ||
116 | |||
117 | Key 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 | |||
125 | QString Key::toDisplayString() const | ||
126 | { | ||
127 | return id.toDisplayString() + rev.toDisplayString(); | ||
128 | } | ||
129 | |||
130 | QByteArray Key::toDisplayByteArray() const | ||
131 | { | ||
132 | return id.toDisplayByteArray() + rev.toDisplayByteArray(); | ||
133 | } | ||
134 | |||
135 | Key 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 | |||
143 | const Identifier &Key::identifier() const | ||
144 | { | ||
145 | return id; | ||
146 | } | ||
147 | |||
148 | const Revision &Key::revision() const | ||
149 | { | ||
150 | return rev; | ||
151 | } | ||
152 | |||
153 | void Key::setRevision(const Revision &newRev) | ||
154 | { | ||
155 | rev = newRev; | ||
156 | } | ||