summaryrefslogtreecommitdiffstats
path: root/common/storage.h
diff options
context:
space:
mode:
Diffstat (limited to 'common/storage.h')
-rw-r--r--common/storage.h156
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
43class Identifier
44{
45public:
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
79private:
80 explicit Identifier(const QUuid &uid) : uid(uid) {}
81 QUuid uid;
82};
83
84class Revision
85{
86public:
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
125private:
126 qint64 rev;
127};
128
129class Key
130{
131public:
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
183private:
184 Identifier id;
185 Revision rev;
186};
187
41class SINK_EXPORT DataStore 188class SINK_EXPORT DataStore
42{ 189{
43public: 190public:
@@ -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
262SINK_EXPORT QDebug& operator<<(QDebug &dbg, const Sink::Storage::DataStore::Error &error); 409SINK_EXPORT QDebug& operator<<(QDebug &dbg, const Sink::Storage::DataStore::Error &error);
410SINK_EXPORT QDebug& operator<<(QDebug &dbg, const Sink::Storage::Identifier &);
411SINK_EXPORT QDebug& operator<<(QDebug &dbg, const Sink::Storage::Revision &);
412SINK_EXPORT QDebug& operator<<(QDebug &dbg, const Sink::Storage::Key &);