diff options
author | Minijackson <minijackson@riseup.net> | 2018-06-26 14:10:30 +0200 |
---|---|---|
committer | Minijackson <minijackson@riseup.net> | 2018-07-04 15:37:14 +0200 |
commit | c90ba4a98292a39eb0b3df12fd7e2dec0300e58d (patch) | |
tree | 0c0c252087e6d8ccf31ba521ea76a7153032f20d /common/storage.h | |
parent | 922e0979e2c27ff8dbc765ae151d17c7815b98a0 (diff) | |
download | sink-c90ba4a98292a39eb0b3df12fd7e2dec0300e58d.tar.gz sink-c90ba4a98292a39eb0b3df12fd7e2dec0300e58d.zip |
Move Key API into own files + some fixes
Diffstat (limited to 'common/storage.h')
-rw-r--r-- | common/storage.h | 148 |
1 files changed, 0 insertions, 148 deletions
diff --git a/common/storage.h b/common/storage.h index 3cc5adf..25d0fa6 100644 --- a/common/storage.h +++ b/common/storage.h | |||
@@ -40,151 +40,6 @@ struct SINK_EXPORT DbLayout { | |||
40 | Databases tables; | 40 | Databases tables; |
41 | }; | 41 | }; |
42 | 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 | |||
188 | class SINK_EXPORT DataStore | 43 | class SINK_EXPORT DataStore |
189 | { | 44 | { |
190 | public: | 45 | public: |
@@ -407,6 +262,3 @@ private: | |||
407 | } // namespace Sink | 262 | } // namespace Sink |
408 | 263 | ||
409 | SINK_EXPORT QDebug& operator<<(QDebug &dbg, const Sink::Storage::DataStore::Error &error); | 264 | 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 &); | ||