summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRémi Nicole <nicole@kolabsystems.com>2018-07-27 13:40:15 +0200
committerChristian Mollekopf <chrigi_1@fastmail.fm>2018-07-27 13:48:34 +0200
commit6fcb97ad464a346663184b27d587668c59b54361 (patch)
tree02685a81d0101d339dc2815f3471004502467830
parentc741d7fbb21ff959ca4b6d4f7681aead277044b4 (diff)
downloadsink-6fcb97ad464a346663184b27d587668c59b54361.tar.gz
sink-6fcb97ad464a346663184b27d587668c59b54361.zip
Use Key API in SinkSH
Summary: Depends on D14289 - Fixes the `sinksh inspect …` command - Introduces `isValid`, `isValidInternal` and `isValidDisplay` static functions in Key, Identifier and Revision I still have to do a more extensive search for induced bugs in other commands Reviewers: cmollekopf Reviewed By: cmollekopf Tags: #sink Differential Revision: https://phabricator.kde.org/D14404
-rw-r--r--common/storage/key.cpp76
-rw-r--r--common/storage/key.h18
-rw-r--r--sinksh/syntax_modules/sink_inspect.cpp25
3 files changed, 111 insertions, 8 deletions
diff --git a/common/storage/key.cpp b/common/storage/key.cpp
index cfeb016..215e155 100644
--- a/common/storage/key.cpp
+++ b/common/storage/key.cpp
@@ -84,6 +84,27 @@ bool Identifier::isNull() const
84 return uid.isNull(); 84 return uid.isNull();
85} 85}
86 86
87bool Identifier::isValidInternal(const QByteArray &bytes)
88{
89 return !QUuid::fromRfc4122(bytes).isNull();
90}
91
92bool Identifier::isValidDisplay(const QByteArray &bytes)
93{
94 return !QUuid(bytes).isNull();
95}
96
97bool Identifier::isValid(const QByteArray &bytes)
98{
99 switch (bytes.size()) {
100 case Identifier::INTERNAL_REPR_SIZE:
101 return isValidInternal(bytes);
102 case Identifier::DISPLAY_REPR_SIZE:
103 return isValidDisplay(bytes);
104 }
105 return false;
106}
107
87bool Identifier::operator==(const Identifier &other) const 108bool Identifier::operator==(const Identifier &other) const
88{ 109{
89 return uid == other.uid; 110 return uid == other.uid;
@@ -128,6 +149,27 @@ qint64 Revision::toQint64() const
128 return rev; 149 return rev;
129} 150}
130 151
152bool Revision::isValidInternal(const QByteArray &bytes)
153{
154 if (bytes.size() != Revision::INTERNAL_REPR_SIZE) {
155 return false;
156 }
157
158 bool ok;
159 bytes.toLongLong(&ok);
160 return ok;
161}
162
163bool Revision::isValidDisplay(const QByteArray &bytes)
164{
165 isValidInternal(bytes);
166}
167
168bool Revision::isValid(const QByteArray &bytes)
169{
170 isValidInternal(bytes);
171}
172
131bool Revision::operator==(const Revision &other) const 173bool Revision::operator==(const Revision &other) const
132{ 174{
133 return rev == other.rev; 175 return rev == other.rev;
@@ -191,6 +233,39 @@ bool Key::isNull() const
191 return id.isNull(); 233 return id.isNull();
192} 234}
193 235
236bool Key::isValidInternal(const QByteArray &bytes)
237{
238 if (bytes.size() != Key::INTERNAL_REPR_SIZE) {
239 return false;
240 }
241
242 auto idBytes = bytes.mid(0, Identifier::INTERNAL_REPR_SIZE);
243 auto revBytes = bytes.mid(Identifier::INTERNAL_REPR_SIZE);
244 return Identifier::isValidInternal(idBytes) && Revision::isValidInternal(revBytes);
245}
246
247bool Key::isValidDisplay(const QByteArray &bytes)
248{
249 if (bytes.size() != Key::DISPLAY_REPR_SIZE) {
250 return false;
251 }
252
253 auto idBytes = bytes.mid(0, Identifier::DISPLAY_REPR_SIZE);
254 auto revBytes = bytes.mid(Identifier::DISPLAY_REPR_SIZE);
255 return Key::isValidDisplay(idBytes) && Revision::isValidDisplay(revBytes);
256}
257
258bool Key::isValid(const QByteArray &bytes)
259{
260 switch (bytes.size()) {
261 case Key::INTERNAL_REPR_SIZE:
262 return isValidInternal(bytes);
263 case Key::DISPLAY_REPR_SIZE:
264 return isValidDisplay(bytes);
265 }
266 return false;
267}
268
194bool Key::operator==(const Key &other) const 269bool Key::operator==(const Key &other) const
195{ 270{
196 return (id == other.id) && (rev == other.rev); 271 return (id == other.id) && (rev == other.rev);
@@ -200,4 +275,3 @@ bool Key::operator!=(const Key &other) const
200{ 275{
201 return !(*this == other); 276 return !(*this == other);
202} 277}
203
diff --git a/common/storage/key.h b/common/storage/key.h
index a5b92bb..211aea7 100644
--- a/common/storage/key.h
+++ b/common/storage/key.h
@@ -48,6 +48,10 @@ public:
48 48
49 bool isNull() const; 49 bool isNull() const;
50 50
51 static bool isValidInternal(const QByteArray &);
52 static bool isValidDisplay(const QByteArray &);
53 static bool isValid(const QByteArray &);
54
51 bool operator==(const Identifier &other) const; 55 bool operator==(const Identifier &other) const;
52 bool operator!=(const Identifier &other) const; 56 bool operator!=(const Identifier &other) const;
53 57
@@ -72,6 +76,10 @@ public:
72 static Revision fromDisplayByteArray(const QByteArray &bytes); 76 static Revision fromDisplayByteArray(const QByteArray &bytes);
73 qint64 toQint64() const; 77 qint64 toQint64() const;
74 78
79 static bool isValidInternal(const QByteArray &);
80 static bool isValidDisplay(const QByteArray &);
81 static bool isValid(const QByteArray &);
82
75 bool operator==(const Revision &other) const; 83 bool operator==(const Revision &other) const;
76 bool operator!=(const Revision &other) const; 84 bool operator!=(const Revision &other) const;
77 85
@@ -99,6 +107,10 @@ public:
99 107
100 bool isNull() const; 108 bool isNull() const;
101 109
110 static bool isValidInternal(const QByteArray &);
111 static bool isValidDisplay(const QByteArray &);
112 static bool isValid(const QByteArray &);
113
102 bool operator==(const Key &other) const; 114 bool operator==(const Key &other) const;
103 bool operator!=(const Key &other) const; 115 bool operator!=(const Key &other) const;
104 116
@@ -110,6 +122,6 @@ private:
110} // namespace Storage 122} // namespace Storage
111} // namespace Sink 123} // namespace Sink
112 124
113SINK_EXPORT QDebug& operator<<(QDebug &dbg, const Sink::Storage::Identifier &); 125SINK_EXPORT QDebug &operator<<(QDebug &dbg, const Sink::Storage::Identifier &);
114SINK_EXPORT QDebug& operator<<(QDebug &dbg, const Sink::Storage::Revision &); 126SINK_EXPORT QDebug &operator<<(QDebug &dbg, const Sink::Storage::Revision &);
115SINK_EXPORT QDebug& operator<<(QDebug &dbg, const Sink::Storage::Key &); 127SINK_EXPORT QDebug &operator<<(QDebug &dbg, const Sink::Storage::Key &);
diff --git a/sinksh/syntax_modules/sink_inspect.cpp b/sinksh/syntax_modules/sink_inspect.cpp
index 7a41ef0..f9cc50a 100644
--- a/sinksh/syntax_modules/sink_inspect.cpp
+++ b/sinksh/syntax_modules/sink_inspect.cpp
@@ -41,6 +41,20 @@
41namespace SinkInspect 41namespace SinkInspect
42{ 42{
43 43
44using Sink::Storage::Key;
45using Sink::Storage::Identifier;
46
47QString parse(const QByteArray &bytes)
48{
49 if (Key::isValidInternal(bytes)) {
50 return Key::fromInternalByteArray(bytes).toDisplayString();
51 } else if (Identifier::isValidInternal(bytes)) {
52 return Identifier::fromInternalByteArray(bytes).toDisplayString();
53 } else {
54 return QString::fromUtf8(bytes);
55 }
56}
57
44bool inspect(const QStringList &args, State &state) 58bool inspect(const QStringList &args, State &state)
45{ 59{
46 if (args.isEmpty()) { 60 if (args.isEmpty()) {
@@ -90,7 +104,7 @@ bool inspect(const QStringList &args, State &state)
90 104
91 QSet<QByteArray> uids; 105 QSet<QByteArray> uids;
92 db.scan("", [&] (const QByteArray &key, const QByteArray &data) { 106 db.scan("", [&] (const QByteArray &key, const QByteArray &data) {
93 uids.insert(Sink::Storage::Key::fromInternalByteArray(key).identifier().toDisplayByteArray()); 107 uids.insert(Key::fromInternalByteArray(key).identifier().toDisplayByteArray());
94 return true; 108 return true;
95 }, 109 },
96 [&](const Sink::Storage::DataStore::Error &e) { 110 [&](const Sink::Storage::DataStore::Error &e) {
@@ -187,13 +201,16 @@ bool inspect(const QStringList &args, State &state)
187 auto count = db.scan(filter, [&] (const QByteArray &key, const QByteArray &data) { 201 auto count = db.scan(filter, [&] (const QByteArray &key, const QByteArray &data) {
188 keySizeTotal += key.size(); 202 keySizeTotal += key.size();
189 valueSizeTotal += data.size(); 203 valueSizeTotal += data.size();
204
205 const auto parsedKey = parse(key);
206
190 if (isMainDb) { 207 if (isMainDb) {
191 Sink::EntityBuffer buffer(const_cast<const char *>(data.data()), data.size()); 208 Sink::EntityBuffer buffer(const_cast<const char *>(data.data()), data.size());
192 if (!buffer.isValid()) { 209 if (!buffer.isValid()) {
193 state.printError("Read invalid buffer from disk: " + key); 210 state.printError("Read invalid buffer from disk: " + parsedKey);
194 } else { 211 } else {
195 const auto metadata = flatbuffers::GetRoot<Sink::Metadata>(buffer.metadataBuffer()); 212 const auto metadata = flatbuffers::GetRoot<Sink::Metadata>(buffer.metadataBuffer());
196 state.printLine("Key: " + key 213 state.printLine("Key: " + parsedKey
197 + " Operation: " + QString::number(metadata->operation()) 214 + " Operation: " + QString::number(metadata->operation())
198 + " Replay: " + (metadata->replayToSource() ? "true" : "false") 215 + " Replay: " + (metadata->replayToSource() ? "true" : "false")
199 + ((metadata->modifiedProperties() && metadata->modifiedProperties()->size() != 0) ? (" [" + Sink::BufferUtils::fromVector(*metadata->modifiedProperties()).join(", ")) + "]": "") 216 + ((metadata->modifiedProperties() && metadata->modifiedProperties()->size() != 0) ? (" [" + Sink::BufferUtils::fromVector(*metadata->modifiedProperties()).join(", ")) + "]": "")
@@ -201,7 +218,7 @@ bool inspect(const QStringList &args, State &state)
201 ); 218 );
202 } 219 }
203 } else { 220 } else {
204 state.printLine("Key: " + key + "\tValue: " + QString::fromUtf8(data)); 221 state.printLine("Key: " + parsedKey + "\tValue: " + parse(data));
205 } 222 }
206 return true; 223 return true;
207 }, 224 },