summaryrefslogtreecommitdiffstats
path: root/common/storage.h
diff options
context:
space:
mode:
Diffstat (limited to 'common/storage.h')
-rw-r--r--common/storage.h148
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
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
188class SINK_EXPORT DataStore 43class SINK_EXPORT DataStore
189{ 44{
190public: 45public:
@@ -407,6 +262,3 @@ private:
407} // namespace Sink 262} // namespace Sink
408 263
409SINK_EXPORT QDebug& operator<<(QDebug &dbg, const Sink::Storage::DataStore::Error &error); 264SINK_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 &);