1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
|
/*
* Copyright (C) 2017 Christian Mollekopf <mollekopf@kolabsys.com>
*
* 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 <QDebug>
#include <QObject> // tr()
#include <QTimer>
#include <QDir>
#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] [--showinternal]"));
}
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");
bool showInternal = options.options.contains("showinternal");
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);
if (showInternal) {
//Print internal keys
db.scan("__internal", [&] (const QByteArray &key, const QByteArray &data) {
state.printLine("Internal: " + key + " Value: " + QString::fromUtf8(data));
return true;
},
[&](const Sink::Storage::DataStore::Error &e) {
state.printError(e.message);
},
true, false);
} else {
QByteArray filter;
if (!idFilter.isEmpty()) {
filter = idFilter.first().toUtf8();
}
//Print rest of db
bool findSubstringKeys = !filter.isEmpty();
auto count = db.scan(filter, [&] (const QByteArray &key, const QByteArray &data) {
if (isMainDb) {
Sink::EntityBuffer buffer(const_cast<const char *>(data.data()), data.size());
if (!buffer.isValid()) {
state.printError("Read invalid buffer from disk: " + key);
} else {
const auto metadata = flatbuffers::GetRoot<Sink::Metadata>(buffer.metadataBuffer());
state.printLine("Key: " + key + " Operation: " + QString::number(metadata->operation()));
}
} else {
state.printLine("Key: " + key + " Value: " + QString::fromUtf8(data));
}
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)
}
|