From 6c5d7a65899f3b322184628c2be68fd3f3fdd5da Mon Sep 17 00:00:00 2001 From: Minijackson Date: Thu, 26 Jul 2018 17:00:03 +0200 Subject: Parse Keys in SinkSH inspect --- common/storage/key.cpp | 76 +++++++++++++++++++++++++++++++++- common/storage/key.h | 18 ++++++-- sinksh/syntax_modules/sink_inspect.cpp | 25 +++++++++-- 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 return uid.isNull(); } +bool Identifier::isValidInternal(const QByteArray &bytes) +{ + return !QUuid::fromRfc4122(bytes).isNull(); +} + +bool Identifier::isValidDisplay(const QByteArray &bytes) +{ + return !QUuid(bytes).isNull(); +} + +bool Identifier::isValid(const QByteArray &bytes) +{ + switch (bytes.size()) { + case Identifier::INTERNAL_REPR_SIZE: + return isValidInternal(bytes); + case Identifier::DISPLAY_REPR_SIZE: + return isValidDisplay(bytes); + } + return false; +} + bool Identifier::operator==(const Identifier &other) const { return uid == other.uid; @@ -128,6 +149,27 @@ qint64 Revision::toQint64() const return rev; } +bool Revision::isValidInternal(const QByteArray &bytes) +{ + if (bytes.size() != Revision::INTERNAL_REPR_SIZE) { + return false; + } + + bool ok; + bytes.toLongLong(&ok); + return ok; +} + +bool Revision::isValidDisplay(const QByteArray &bytes) +{ + isValidInternal(bytes); +} + +bool Revision::isValid(const QByteArray &bytes) +{ + isValidInternal(bytes); +} + bool Revision::operator==(const Revision &other) const { return rev == other.rev; @@ -191,6 +233,39 @@ bool Key::isNull() const return id.isNull(); } +bool Key::isValidInternal(const QByteArray &bytes) +{ + if (bytes.size() != Key::INTERNAL_REPR_SIZE) { + return false; + } + + auto idBytes = bytes.mid(0, Identifier::INTERNAL_REPR_SIZE); + auto revBytes = bytes.mid(Identifier::INTERNAL_REPR_SIZE); + return Identifier::isValidInternal(idBytes) && Revision::isValidInternal(revBytes); +} + +bool Key::isValidDisplay(const QByteArray &bytes) +{ + if (bytes.size() != Key::DISPLAY_REPR_SIZE) { + return false; + } + + auto idBytes = bytes.mid(0, Identifier::DISPLAY_REPR_SIZE); + auto revBytes = bytes.mid(Identifier::DISPLAY_REPR_SIZE); + return Key::isValidDisplay(idBytes) && Revision::isValidDisplay(revBytes); +} + +bool Key::isValid(const QByteArray &bytes) +{ + switch (bytes.size()) { + case Key::INTERNAL_REPR_SIZE: + return isValidInternal(bytes); + case Key::DISPLAY_REPR_SIZE: + return isValidDisplay(bytes); + } + return false; +} + bool Key::operator==(const Key &other) const { return (id == other.id) && (rev == other.rev); @@ -200,4 +275,3 @@ bool Key::operator!=(const Key &other) const { return !(*this == other); } - 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: bool isNull() const; + static bool isValidInternal(const QByteArray &); + static bool isValidDisplay(const QByteArray &); + static bool isValid(const QByteArray &); + bool operator==(const Identifier &other) const; bool operator!=(const Identifier &other) const; @@ -72,6 +76,10 @@ public: static Revision fromDisplayByteArray(const QByteArray &bytes); qint64 toQint64() const; + static bool isValidInternal(const QByteArray &); + static bool isValidDisplay(const QByteArray &); + static bool isValid(const QByteArray &); + bool operator==(const Revision &other) const; bool operator!=(const Revision &other) const; @@ -99,6 +107,10 @@ public: bool isNull() const; + static bool isValidInternal(const QByteArray &); + static bool isValidDisplay(const QByteArray &); + static bool isValid(const QByteArray &); + bool operator==(const Key &other) const; bool operator!=(const Key &other) const; @@ -110,6 +122,6 @@ private: } // namespace Storage } // namespace Sink -SINK_EXPORT QDebug& operator<<(QDebug &dbg, const Sink::Storage::Identifier &); -SINK_EXPORT QDebug& operator<<(QDebug &dbg, const Sink::Storage::Revision &); -SINK_EXPORT QDebug& operator<<(QDebug &dbg, const Sink::Storage::Key &); +SINK_EXPORT QDebug &operator<<(QDebug &dbg, const Sink::Storage::Identifier &); +SINK_EXPORT QDebug &operator<<(QDebug &dbg, const Sink::Storage::Revision &); +SINK_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 1b3d77c..646a8cc 100644 --- a/sinksh/syntax_modules/sink_inspect.cpp +++ b/sinksh/syntax_modules/sink_inspect.cpp @@ -43,6 +43,20 @@ namespace SinkInspect { +using Sink::Storage::Key; +using Sink::Storage::Identifier; + +QString parse(const QByteArray &bytes) +{ + if (Key::isValidInternal(bytes)) { + return Key::fromInternalByteArray(bytes).toDisplayString(); + } else if (Identifier::isValidInternal(bytes)) { + return Identifier::fromInternalByteArray(bytes).toDisplayString(); + } else { + return QString::fromUtf8(bytes); + } +} + bool inspect(const QStringList &args, State &state) { if (args.isEmpty()) { @@ -92,7 +106,7 @@ bool inspect(const QStringList &args, State &state) QSet uids; db.scan("", [&] (const QByteArray &key, const QByteArray &data) { - uids.insert(Sink::Storage::Key::fromInternalByteArray(key).identifier().toDisplayByteArray()); + uids.insert(Key::fromInternalByteArray(key).identifier().toDisplayByteArray()); return true; }, [&](const Sink::Storage::DataStore::Error &e) { @@ -202,13 +216,16 @@ bool inspect(const QStringList &args, State &state) auto count = db.scan(filter, [&] (const QByteArray &key, const QByteArray &data) { keySizeTotal += key.size(); valueSizeTotal += data.size(); + + const auto parsedKey = parse(key); + if (isMainDb) { Sink::EntityBuffer buffer(const_cast(data.data()), data.size()); if (!buffer.isValid()) { - state.printError("Read invalid buffer from disk: " + key); + state.printError("Read invalid buffer from disk: " + parsedKey); } else { const auto metadata = flatbuffers::GetRoot(buffer.metadataBuffer()); - state.printLine("Key: " + key + state.printLine("Key: " + parsedKey + " Operation: " + QString::number(metadata->operation()) + " Replay: " + (metadata->replayToSource() ? "true" : "false") + ((metadata->modifiedProperties() && metadata->modifiedProperties()->size() != 0) ? (" [" + Sink::BufferUtils::fromVector(*metadata->modifiedProperties()).join(", ")) + "]": "") @@ -216,7 +233,7 @@ bool inspect(const QStringList &args, State &state) ); } } else { - state.printLine("Key: " + key + "\tValue: " + QString::fromUtf8(data)); + state.printLine("Key: " + parsedKey + "\tValue: " + parse(data)); } return true; }, -- cgit v1.2.3