diff options
author | Minijackson <minijackson@riseup.net> | 2018-06-26 11:44:11 +0200 |
---|---|---|
committer | Minijackson <minijackson@riseup.net> | 2018-07-04 15:37:14 +0200 |
commit | 922e0979e2c27ff8dbc765ae151d17c7815b98a0 (patch) | |
tree | cb031c5d3ccc31ea576f66b4f718c17f5bb0775c /common/storage.h | |
parent | 06f30d0f0d0051df97d4c34cd1a80b14857c9e9c (diff) | |
download | sink-922e0979e2c27ff8dbc765ae151d17c7815b98a0.tar.gz sink-922e0979e2c27ff8dbc765ae151d17c7815b98a0.zip |
[Storage] Implement Key API
Diffstat (limited to 'common/storage.h')
-rw-r--r-- | common/storage.h | 156 |
1 files changed, 153 insertions, 3 deletions
diff --git a/common/storage.h b/common/storage.h index a8c486c..3cc5adf 100644 --- a/common/storage.h +++ b/common/storage.h | |||
@@ -22,8 +22,10 @@ | |||
22 | #pragma once | 22 | #pragma once |
23 | 23 | ||
24 | #include "sink_export.h" | 24 | #include "sink_export.h" |
25 | #include "utils.h" | ||
25 | #include <string> | 26 | #include <string> |
26 | #include <functional> | 27 | #include <functional> |
28 | #include <QUuid> | ||
27 | #include <QString> | 29 | #include <QString> |
28 | #include <QMap> | 30 | #include <QMap> |
29 | 31 | ||
@@ -38,6 +40,151 @@ struct SINK_EXPORT DbLayout { | |||
38 | Databases tables; | 40 | Databases tables; |
39 | }; | 41 | }; |
40 | 42 | ||
43 | class Identifier | ||
44 | { | ||
45 | public: | ||
46 | // RFC 4122 Section 4.1.2 says 128 bits -> 16 bytes | ||
47 | static const constexpr size_t INTERNAL_REPR_SIZE = 16; | ||
48 | static const constexpr size_t DISPLAY_REPR_SIZE = 38; | ||
49 | |||
50 | Identifier() : uid(QUuid::createUuid()) {}; | ||
51 | |||
52 | QByteArray toInternalByteArray() const | ||
53 | { | ||
54 | return uid.toRfc4122(); | ||
55 | } | ||
56 | |||
57 | static Identifier fromInternalByteArray(const QByteArray &bytes) | ||
58 | { | ||
59 | Q_ASSERT(bytes.size() == INTERNAL_REPR_SIZE); | ||
60 | return Identifier(QUuid::fromRfc4122(bytes)); | ||
61 | } | ||
62 | |||
63 | QString toDisplayString() const | ||
64 | { | ||
65 | return uid.toString(); | ||
66 | } | ||
67 | |||
68 | QByteArray toDisplayByteArray() const | ||
69 | { | ||
70 | return uid.toByteArray(); | ||
71 | } | ||
72 | |||
73 | static Identifier fromDisplayByteArray(const QByteArray &bytes) | ||
74 | { | ||
75 | Q_ASSERT(bytes.size() == DISPLAY_REPR_SIZE); | ||
76 | return Identifier(QUuid::fromString(QString::fromUtf8(bytes))); | ||
77 | } | ||
78 | |||
79 | private: | ||
80 | explicit Identifier(const QUuid &uid) : uid(uid) {} | ||
81 | QUuid uid; | ||
82 | }; | ||
83 | |||
84 | class Revision | ||
85 | { | ||
86 | public: | ||
87 | // qint64 has a 19 digit decimal representation | ||
88 | static const constexpr size_t INTERNAL_REPR_SIZE = 19; | ||
89 | static const constexpr size_t DISPLAY_REPR_SIZE = 19; | ||
90 | |||
91 | Revision(qint64 rev) : rev(rev) {} | ||
92 | |||
93 | QByteArray toInternalByteArray() const | ||
94 | { | ||
95 | return padNumber(rev); | ||
96 | } | ||
97 | |||
98 | static Revision fromInternalByteArray(const QByteArray &bytes) | ||
99 | { | ||
100 | Q_ASSERT(bytes.size() == INTERNAL_REPR_SIZE); | ||
101 | return Revision(bytes.toLongLong()); | ||
102 | } | ||
103 | |||
104 | QString toDisplayString() const | ||
105 | { | ||
106 | return QString::fromUtf8(toInternalByteArray()); | ||
107 | } | ||
108 | |||
109 | QByteArray toDisplayByteArray() const | ||
110 | { | ||
111 | return toInternalByteArray(); | ||
112 | } | ||
113 | |||
114 | static Revision fromDisplayByteArray(const QByteArray &bytes) | ||
115 | { | ||
116 | Q_ASSERT(bytes.size() == DISPLAY_REPR_SIZE); | ||
117 | return fromInternalByteArray(bytes); | ||
118 | } | ||
119 | |||
120 | qint64 toQint64() const | ||
121 | { | ||
122 | return rev; | ||
123 | } | ||
124 | |||
125 | private: | ||
126 | qint64 rev; | ||
127 | }; | ||
128 | |||
129 | class Key | ||
130 | { | ||
131 | public: | ||
132 | static const constexpr size_t INTERNAL_REPR_SIZE = Identifier::INTERNAL_REPR_SIZE + Revision::INTERNAL_REPR_SIZE; | ||
133 | static const constexpr size_t DISPLAY_REPR_SIZE = Identifier::DISPLAY_REPR_SIZE + Revision::DISPLAY_REPR_SIZE; | ||
134 | |||
135 | Key(const Identifier &id, const Revision &rev) : id(id), rev(rev) {} | ||
136 | |||
137 | QByteArray toInternalByteArray() const | ||
138 | { | ||
139 | return id.toInternalByteArray() + rev.toInternalByteArray(); | ||
140 | } | ||
141 | |||
142 | static Key fromInternalByteArray(const QByteArray &bytes) | ||
143 | { | ||
144 | Q_ASSERT(bytes.size() == INTERNAL_REPR_SIZE); | ||
145 | auto idBytes = bytes.mid(0, Identifier::INTERNAL_REPR_SIZE); | ||
146 | auto revBytes = bytes.mid(Identifier::INTERNAL_REPR_SIZE); | ||
147 | return Key(Identifier::fromInternalByteArray(idBytes), Revision::fromInternalByteArray(revBytes)); | ||
148 | } | ||
149 | |||
150 | QString toDisplayString() const | ||
151 | { | ||
152 | return id.toDisplayString() + rev.toDisplayString(); | ||
153 | } | ||
154 | |||
155 | QByteArray toDisplayByteArray() const | ||
156 | { | ||
157 | return id.toDisplayByteArray() + rev.toDisplayByteArray(); | ||
158 | } | ||
159 | |||
160 | static Key fromDisplayByteArray(const QByteArray &bytes) | ||
161 | { | ||
162 | Q_ASSERT(bytes.size() == DISPLAY_REPR_SIZE); | ||
163 | auto idBytes = bytes.mid(0, Identifier::DISPLAY_REPR_SIZE); | ||
164 | auto revBytes = bytes.mid(Identifier::DISPLAY_REPR_SIZE); | ||
165 | return Key(Identifier::fromDisplayByteArray(idBytes), Revision::fromDisplayByteArray(revBytes)); | ||
166 | } | ||
167 | |||
168 | const Identifier &identifier() const | ||
169 | { | ||
170 | return id; | ||
171 | } | ||
172 | |||
173 | const Revision &revision() const | ||
174 | { | ||
175 | return rev; | ||
176 | } | ||
177 | |||
178 | void setRevision(const Revision &newRev) | ||
179 | { | ||
180 | rev = newRev; | ||
181 | } | ||
182 | |||
183 | private: | ||
184 | Identifier id; | ||
185 | Revision rev; | ||
186 | }; | ||
187 | |||
41 | class SINK_EXPORT DataStore | 188 | class SINK_EXPORT DataStore |
42 | { | 189 | { |
43 | public: | 190 | public: |
@@ -237,9 +384,9 @@ public: | |||
237 | static bool isInternalKey(void *key, int keySize); | 384 | static bool isInternalKey(void *key, int keySize); |
238 | static bool isInternalKey(const QByteArray &key); | 385 | static bool isInternalKey(const QByteArray &key); |
239 | 386 | ||
240 | static QByteArray assembleKey(const QByteArray &key, qint64 revision); | 387 | //static QByteArray assembleKey(const QByteArray &key, qint64 revision); |
241 | static QByteArray uidFromKey(const QByteArray &key); | 388 | //static Identifier uidFromKey(const QByteArray &key); |
242 | static qint64 revisionFromKey(const QByteArray &key); | 389 | //static qint64 revisionFromKey(const QByteArray &key); |
243 | 390 | ||
244 | static NamedDatabase mainDatabase(const Transaction &, const QByteArray &type); | 391 | static NamedDatabase mainDatabase(const Transaction &, const QByteArray &type); |
245 | 392 | ||
@@ -260,3 +407,6 @@ private: | |||
260 | } // namespace Sink | 407 | } // namespace Sink |
261 | 408 | ||
262 | SINK_EXPORT QDebug& operator<<(QDebug &dbg, const Sink::Storage::DataStore::Error &error); | 409 | SINK_EXPORT QDebug& operator<<(QDebug &dbg, const Sink::Storage::DataStore::Error &error); |
410 | SINK_EXPORT QDebug& operator<<(QDebug &dbg, const Sink::Storage::Identifier &); | ||
411 | SINK_EXPORT QDebug& operator<<(QDebug &dbg, const Sink::Storage::Revision &); | ||
412 | SINK_EXPORT QDebug& operator<<(QDebug &dbg, const Sink::Storage::Key &); | ||