summaryrefslogtreecommitdiffstats
path: root/common/storage
diff options
context:
space:
mode:
Diffstat (limited to 'common/storage')
-rw-r--r--common/storage/entitystore.cpp5
-rw-r--r--common/storage/entitystore.h1
-rw-r--r--common/storage/key.cpp156
-rw-r--r--common/storage/key.h100
4 files changed, 260 insertions, 2 deletions
diff --git a/common/storage/entitystore.cpp b/common/storage/entitystore.cpp
index f74d3df..efafe8a 100644
--- a/common/storage/entitystore.cpp
+++ b/common/storage/entitystore.cpp
@@ -678,9 +678,10 @@ void EntityStore::readRevisions(const QByteArray &type, const QByteArray &uid, q
678 DataStore::mainDatabase(d->transaction, type) 678 DataStore::mainDatabase(d->transaction, type)
679 .scan(uid, 679 .scan(uid,
680 [&](const QByteArray &key, const QByteArray &value) -> bool { 680 [&](const QByteArray &key, const QByteArray &value) -> bool {
681 const auto revision = DataStore::revisionFromKey(key); 681 const auto parsedKey = Key::fromDisplayByteArray(key);
682 const auto revision = parsedKey.revision().toQint64();
682 if (revision >= startingRevision) { 683 if (revision >= startingRevision) {
683 callback(DataStore::uidFromKey(key), revision, Sink::EntityBuffer(value.data(), value.size())); 684 callback(parsedKey.identifier().toDisplayByteArray(), revision, Sink::EntityBuffer(value.data(), value.size()));
684 } 685 }
685 return true; 686 return true;
686 }, 687 },
diff --git a/common/storage/entitystore.h b/common/storage/entitystore.h
index 69de76c..1dcd285 100644
--- a/common/storage/entitystore.h
+++ b/common/storage/entitystore.h
@@ -25,6 +25,7 @@
25#include "domaintypeadaptorfactoryinterface.h" 25#include "domaintypeadaptorfactoryinterface.h"
26#include "query.h" 26#include "query.h"
27#include "storage.h" 27#include "storage.h"
28#include "key.h"
28#include "resourcecontext.h" 29#include "resourcecontext.h"
29#include "metadata_generated.h" 30#include "metadata_generated.h"
30 31
diff --git a/common/storage/key.cpp b/common/storage/key.cpp
new file mode 100644
index 0000000..5d26722
--- /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
25using Sink::Storage::Identifier;
26using Sink::Storage::Key;
27using Sink::Storage::Revision;
28
29QDebug &operator<<(QDebug &dbg, const Identifier &id)
30{
31 dbg << id.toDisplayString();
32 return dbg;
33}
34
35QDebug &operator<<(QDebug &dbg, const Revision &rev)
36{
37 dbg << rev.toDisplayString();
38 return dbg;
39}
40
41QDebug &operator<<(QDebug &dbg, const Key &key)
42{
43 dbg << key.toDisplayString();
44 return dbg;
45}
46
47// Identifier
48
49QByteArray Identifier::toInternalByteArray() const
50{
51 return uid.toRfc4122();
52}
53
54Identifier Identifier::fromInternalByteArray(const QByteArray &bytes)
55{
56 Q_ASSERT(bytes.size() == INTERNAL_REPR_SIZE);
57 return Identifier(QUuid::fromRfc4122(bytes));
58}
59
60QString Identifier::toDisplayString() const
61{
62 return uid.toString();
63}
64
65QByteArray Identifier::toDisplayByteArray() const
66{
67 return uid.toByteArray();
68}
69
70Identifier Identifier::fromDisplayByteArray(const QByteArray &bytes)
71{
72 Q_ASSERT(bytes.size() == DISPLAY_REPR_SIZE);
73 return Identifier(QUuid::fromString(QString::fromUtf8(bytes)));
74}
75
76// Revision
77
78QByteArray Revision::toInternalByteArray() const
79{
80 return padNumber(rev);
81}
82
83Revision Revision::fromInternalByteArray(const QByteArray &bytes)
84{
85 Q_ASSERT(bytes.size() == INTERNAL_REPR_SIZE);
86 return Revision(bytes.toLongLong());
87}
88
89QString Revision::toDisplayString() const
90{
91 return QString::fromUtf8(toInternalByteArray());
92}
93
94QByteArray Revision::toDisplayByteArray() const
95{
96 return toInternalByteArray();
97}
98
99Revision Revision::fromDisplayByteArray(const QByteArray &bytes)
100{
101 Q_ASSERT(bytes.size() == DISPLAY_REPR_SIZE);
102 return fromInternalByteArray(bytes);
103}
104
105qint64 Revision::toQint64() const
106{
107 return rev;
108}
109
110// Key
111
112QByteArray Key::toInternalByteArray() const
113{
114 return id.toInternalByteArray() + rev.toInternalByteArray();
115}
116
117Key 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
125QString Key::toDisplayString() const
126{
127 return id.toDisplayString() + rev.toDisplayString();
128}
129
130QByteArray Key::toDisplayByteArray() const
131{
132 return id.toDisplayByteArray() + rev.toDisplayByteArray();
133}
134
135Key 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
143const Identifier &Key::identifier() const
144{
145 return id;
146}
147
148const Revision &Key::revision() const
149{
150 return rev;
151}
152
153void Key::setRevision(const Revision &newRev)
154{
155 rev = newRev;
156}
diff --git a/common/storage/key.h b/common/storage/key.h
new file mode 100644
index 0000000..76dbd13
--- /dev/null
+++ b/common/storage/key.h
@@ -0,0 +1,100 @@
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#pragma once
23
24#include "sink_export.h"
25
26#include <QByteArray>
27#include <QDebug>
28#include <QUuid>
29
30namespace Sink {
31namespace Storage {
32
33class Identifier
34{
35public:
36 // RFC 4122 Section 4.1.2 says 128 bits -> 16 bytes
37 static const constexpr size_t INTERNAL_REPR_SIZE = 16;
38 static const constexpr size_t DISPLAY_REPR_SIZE = 38;
39
40 Identifier() : uid(QUuid::createUuid()){};
41
42 QByteArray toInternalByteArray() const;
43 static Identifier fromInternalByteArray(const QByteArray &bytes);
44 QString toDisplayString() const;
45 QByteArray toDisplayByteArray() const;
46 static Identifier fromDisplayByteArray(const QByteArray &bytes);
47
48private:
49 explicit Identifier(const QUuid &uid) : uid(uid) {}
50 QUuid uid;
51};
52
53class Revision
54{
55public:
56 // qint64 has a 19 digit decimal representation
57 static const constexpr size_t INTERNAL_REPR_SIZE = 19;
58 static const constexpr size_t DISPLAY_REPR_SIZE = 19;
59
60 Revision(qint64 rev) : rev(rev) {}
61
62 QByteArray toInternalByteArray() const;
63 static Revision fromInternalByteArray(const QByteArray &bytes);
64 QString toDisplayString() const;
65 QByteArray toDisplayByteArray() const;
66 static Revision fromDisplayByteArray(const QByteArray &bytes);
67 qint64 toQint64() const;
68
69private:
70 qint64 rev;
71};
72
73class Key
74{
75public:
76 static const constexpr size_t INTERNAL_REPR_SIZE = Identifier::INTERNAL_REPR_SIZE + Revision::INTERNAL_REPR_SIZE;
77 static const constexpr size_t DISPLAY_REPR_SIZE = Identifier::DISPLAY_REPR_SIZE + Revision::DISPLAY_REPR_SIZE;
78
79 Key(const Identifier &id, const Revision &rev) : id(id), rev(rev) {}
80
81 QByteArray toInternalByteArray() const;
82 static Key fromInternalByteArray(const QByteArray &bytes);
83 QString toDisplayString() const;
84 QByteArray toDisplayByteArray() const;
85 static Key fromDisplayByteArray(const QByteArray &bytes);
86 const Identifier &identifier() const;
87 const Revision &revision() const;
88 void setRevision(const Revision &newRev);
89
90private:
91 Identifier id;
92 Revision rev;
93};
94
95} // namespace Storage
96} // namespace Sink
97
98SINK_EXPORT QDebug& operator<<(QDebug &dbg, const Sink::Storage::Identifier &);
99SINK_EXPORT QDebug& operator<<(QDebug &dbg, const Sink::Storage::Revision &);
100SINK_EXPORT QDebug& operator<<(QDebug &dbg, const Sink::Storage::Key &);