summaryrefslogtreecommitdiffstats
path: root/common
diff options
context:
space:
mode:
authorMinijackson <minijackson@riseup.net>2018-06-26 14:10:30 +0200
committerMinijackson <minijackson@riseup.net>2018-07-04 15:37:14 +0200
commitc90ba4a98292a39eb0b3df12fd7e2dec0300e58d (patch)
tree0c0c252087e6d8ccf31ba521ea76a7153032f20d /common
parent922e0979e2c27ff8dbc765ae151d17c7815b98a0 (diff)
downloadsink-c90ba4a98292a39eb0b3df12fd7e2dec0300e58d.tar.gz
sink-c90ba4a98292a39eb0b3df12fd7e2dec0300e58d.zip
Move Key API into own files + some fixes
Diffstat (limited to 'common')
-rw-r--r--common/CMakeLists.txt1
-rw-r--r--common/changereplay.cpp1
-rw-r--r--common/storage.h148
-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
-rw-r--r--common/storage_common.cpp18
8 files changed, 262 insertions, 168 deletions
diff --git a/common/CMakeLists.txt b/common/CMakeLists.txt
index 970990f..7c4630b 100644
--- a/common/CMakeLists.txt
+++ b/common/CMakeLists.txt
@@ -73,6 +73,7 @@ add_library(${PROJECT_NAME} SHARED
73 specialpurposepreprocessor.cpp 73 specialpurposepreprocessor.cpp
74 datastorequery.cpp 74 datastorequery.cpp
75 storage/entitystore.cpp 75 storage/entitystore.cpp
76 storage/key.cpp
76 indexer.cpp 77 indexer.cpp
77 mail/threadindexer.cpp 78 mail/threadindexer.cpp
78 mail/fulltextindexer.cpp 79 mail/fulltextindexer.cpp
diff --git a/common/changereplay.cpp b/common/changereplay.cpp
index e81f52c..e94ed80 100644
--- a/common/changereplay.cpp
+++ b/common/changereplay.cpp
@@ -23,6 +23,7 @@
23#include "log.h" 23#include "log.h"
24#include "definitions.h" 24#include "definitions.h"
25#include "bufferutils.h" 25#include "bufferutils.h"
26#include "storage/key.h"
26 27
27#include <QTimer> 28#include <QTimer>
28 29
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 &);
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 &);
diff --git a/common/storage_common.cpp b/common/storage_common.cpp
index b36ffcb..c922d9d 100644
--- a/common/storage_common.cpp
+++ b/common/storage_common.cpp
@@ -32,24 +32,6 @@ QDebug& operator<<(QDebug &dbg, const Sink::Storage::DataStore::Error &error)
32 return dbg; 32 return dbg;
33} 33}
34 34
35QDebug& operator<<(QDebug &dbg, const Sink::Storage::Identifier &id)
36{
37 dbg << id.toDisplayString();
38 return dbg;
39}
40
41QDebug& operator<<(QDebug &dbg, const Sink::Storage::Revision &rev)
42{
43 dbg << rev.toDisplayString();
44 return dbg;
45}
46
47QDebug& operator<<(QDebug &dbg, const Sink::Storage::Key &key)
48{
49 dbg << key.toDisplayString();
50 return dbg;
51}
52
53namespace Sink { 35namespace Sink {
54namespace Storage { 36namespace Storage {
55 37