diff options
author | Rémi Nicole <nicole@kolabsystems.com> | 2018-07-27 13:32:39 +0200 |
---|---|---|
committer | Christian Mollekopf <chrigi_1@fastmail.fm> | 2018-07-27 13:47:43 +0200 |
commit | d1838e575baeb6cd08011645609516acbdabd6c8 (patch) | |
tree | 035b99a4d324231c8c44e0e97905383266bacba6 /common/storage/key.h | |
parent | 1855b8356e4c427efdba4c932fa9f984e6ae5a43 (diff) | |
download | sink-d1838e575baeb6cd08011645609516acbdabd6c8.tar.gz sink-d1838e575baeb6cd08011645609516acbdabd6c8.zip |
New Key API in storage layer
Summary:
- Use object oriented paradigm for Keys / Identifiers /Revisions
- "Compress" keys by using byte representation of Uuids
- Still some cleaning left to do
- Also run some benchmarks
- I'm questioning whether files other than entitystore (tests excluded) are allowed to access this API
Reviewers: cmollekopf
Reviewed By: cmollekopf
Tags: #sink
Differential Revision: https://phabricator.kde.org/D13735
Diffstat (limited to 'common/storage/key.h')
-rw-r--r-- | common/storage/key.h | 100 |
1 files changed, 100 insertions, 0 deletions
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 | |||
30 | namespace Sink { | ||
31 | namespace Storage { | ||
32 | |||
33 | class Identifier | ||
34 | { | ||
35 | public: | ||
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 | |||
48 | private: | ||
49 | explicit Identifier(const QUuid &uid) : uid(uid) {} | ||
50 | QUuid uid; | ||
51 | }; | ||
52 | |||
53 | class Revision | ||
54 | { | ||
55 | public: | ||
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 | |||
69 | private: | ||
70 | qint64 rev; | ||
71 | }; | ||
72 | |||
73 | class Key | ||
74 | { | ||
75 | public: | ||
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 | |||
90 | private: | ||
91 | Identifier id; | ||
92 | Revision rev; | ||
93 | }; | ||
94 | |||
95 | } // namespace Storage | ||
96 | } // namespace Sink | ||
97 | |||
98 | SINK_EXPORT QDebug& operator<<(QDebug &dbg, const Sink::Storage::Identifier &); | ||
99 | SINK_EXPORT QDebug& operator<<(QDebug &dbg, const Sink::Storage::Revision &); | ||
100 | SINK_EXPORT QDebug& operator<<(QDebug &dbg, const Sink::Storage::Key &); | ||