From b632a46e30e2159573c1decfbcbf13cb04fc6fe4 Mon Sep 17 00:00:00 2001 From: Christian Mollekopf Date: Wed, 8 Feb 2017 20:31:34 +0100 Subject: sinksh inspect --- sinksh/syntax_modules/sink_inspect.cpp | 112 +++++++++++++++++++++++++++++++++ sinksh/syntax_modules/sink_list.cpp | 7 ++- 2 files changed, 118 insertions(+), 1 deletion(-) create mode 100644 sinksh/syntax_modules/sink_inspect.cpp (limited to 'sinksh/syntax_modules') diff --git a/sinksh/syntax_modules/sink_inspect.cpp b/sinksh/syntax_modules/sink_inspect.cpp new file mode 100644 index 0000000..175bb9d --- /dev/null +++ b/sinksh/syntax_modules/sink_inspect.cpp @@ -0,0 +1,112 @@ +/* + * Copyright (C) 2017 Christian Mollekopf + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the + * Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#include +#include // tr() +#include +#include + +#include "common/resource.h" +#include "common/storage.h" +#include "common/domain/event.h" +#include "common/domain/folder.h" +#include "common/resourceconfig.h" +#include "common/log.h" +#include "common/storage.h" +#include "common/definitions.h" +#include "common/entitybuffer.h" +#include "common/metadata_generated.h" + +#include "sinksh_utils.h" +#include "state.h" +#include "syntaxtree.h" + +namespace SinkInspect +{ + +bool inspect(const QStringList &args, State &state) +{ + if (args.isEmpty()) { + state.printError(QObject::tr("Options: $type [--resource $resource] [--db $db] [--filter $id]")); + } + auto options = SyntaxTree::parseOptions(args); + auto resource = options.options.value("resource").value(0); + + Sink::Storage::DataStore storage(Sink::storageLocation(), resource, Sink::Storage::DataStore::ReadOnly); + auto transaction = storage.createTransaction(Sink::Storage::DataStore::ReadOnly); + + auto dbs = options.options.value("db"); + auto idFilter = options.options.value("filter"); + + auto databases = transaction.getDatabaseNames(); + if (dbs.isEmpty()) { + state.printLine(QString("Available databases: ") + databases.join(", ")); + return false; + } + auto dbName = dbs.value(0).toUtf8(); + auto isMainDb = dbName.contains(".main"); + if (!databases.contains(dbName)) { + state.printError(QString("Database not available: ") + dbName); + } + + state.printLine(QString("Opening: ") + dbName); + auto db = transaction.openDatabase(dbName, + [&] (const Sink::Storage::DataStore::Error &e) { + Q_ASSERT(false); + state.printError(e.message); + }, false); + QByteArray filter; + if (!idFilter.isEmpty()) { + filter = idFilter.first().toUtf8(); + } + bool findSubstringKeys = !filter.isEmpty(); + auto count = db.scan(filter, [&] (const QByteArray &key, const QByteArray &data) { + if (isMainDb) { + Sink::EntityBuffer buffer(const_cast(data.data()), data.size()); + if (!buffer.isValid()) { + state.printError("Read invalid buffer from disk: " + key); + } else { + const auto metadata = flatbuffers::GetRoot(buffer.metadataBuffer()); + state.printLine("Key: " + key + " Operation: " + QString::number(metadata->operation())); + } + } else { + state.printLine("Key: " + key); + } + return true; + }, + [&](const Sink::Storage::DataStore::Error &e) { + state.printError(e.message); + }, + findSubstringKeys); + + state.printLine("Found " + QString::number(count) + " entries"); + return false; +} + +Syntax::List syntax() +{ + Syntax state("inspect", QObject::tr("Inspect database for the resource requested"), &SinkInspect::inspect, Syntax::NotInteractive); + state.completer = &SinkshUtils::resourceCompleter; + + return Syntax::List() << state; +} + +REGISTER_SYNTAX(SinkInspect) + +} diff --git a/sinksh/syntax_modules/sink_list.cpp b/sinksh/syntax_modules/sink_list.cpp index feb582e..f6cae6b 100644 --- a/sinksh/syntax_modules/sink_list.cpp +++ b/sinksh/syntax_modules/sink_list.cpp @@ -64,7 +64,7 @@ QByteArray baIfAvailable(const QStringList &list) bool list(const QStringList &args_, State &state) { if (args_.isEmpty()) { - state.printError(QObject::tr("Options: $type [--resource $resource] [--compact] [--filter $property=$value] [--showall|--show $property]")); + state.printError(QObject::tr("Options: $type [--resource $resource] [--compact] [--filter $property=$value] [--id $id] [--showall|--show $property]")); return false; } @@ -84,6 +84,11 @@ bool list(const QStringList &args_, State &state) query.filter(filter.at(0).toLatin1(), QVariant::fromValue(Sink::ApplicationDomain::Reference{filter.at(1).toLatin1()})); } } + if (options.options.contains("id")) { + for (const auto &f : options.options.value("id")) { + query.filter(f.toUtf8()); + } + } auto compact = options.options.contains("compact"); if (!options.options.contains("showall")) { if (options.options.contains("show")) { -- cgit v1.2.3