-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.cpp
123 lines (105 loc) · 3.2 KB
/
main.cpp
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
#include <leveldb/db.h>
#include <iostream>
#include <memory>
#include <stdio.h>
#include <stdlib.h>
#ifdef EMSCRIPTEN
# include <emscripten.h>
#endif
static void dumpAllKeys(leveldb::DB* db, bool keysOnly) {
auto iter = std::unique_ptr<leveldb::Iterator>{db->NewIterator({})};
iter->SeekToFirst();
while (iter->Valid()) {
std::cout << iter->key().ToString();
if (!keysOnly) {
std::cout << " -> " << iter->value().size()
<< " bytes" << std::endl;
}
iter->Next();
}
}
#ifdef EMSCRIPTEN
// note - this is an emscripten specific workaround to write binary to stdout
// for some reason, doing an frwrite to stdout produces garbage, probably
// because of utf-8 conversion ??
EM_JS(void, to_stdout, (const char* buffer, size_t length), {
const array = new Uint8Array(length);
for (let i = 0; i < length; i++) {
array[i] = getValue(buffer + i, 'i8');
}
process.stdout.write(array);
});
#endif
static bool dumpValue(leveldb::DB* db, const std::string& key) {
auto iter = std::unique_ptr<leveldb::Iterator>{db->NewIterator({})};
iter->Seek(key);
if (!iter->Valid() || iter->key() != key) {
std::cerr << "No such key in database: " << key << std::endl;
return false;
}
#ifdef EMSCRIPTEN
to_stdout(iter->value().data(), iter->value().size());
#else
// use fwrite to make sure that \0 bytes are also written
fwrite(iter->value().data(), 1, iter->value().size(), stdout);
#endif
return true;
}
static void printUsage(const char* argv0) {
std::cout << "Usage: " << argv0 << " <db_path> [-k] [key]" << std::endl;
std::cout << " A tool to introspect a leveldb database" << std::endl;
std::cout << std::endl;
std::cout << " <db_path> directory of the cache" << std::endl;
std::cout << " [key] optional - dumps the value for the given key "
"to stdout"
<< std::endl;
std::cout << " [-k] print only keys, one per line" << std::endl;
}
int main(int argc, char* argv[]) {
bool keysOnly = false;
std::string dbPath;
std::string key;
for (int i = 1; i < argc; ++i) {
std::string arg = argv[i];
if (arg == "--help" || arg == "-h") {
printUsage(argv[0]);
return EXIT_SUCCESS;
} else if (arg == "-k") {
keysOnly = true;
} else if (dbPath.empty()) {
dbPath = std::move(arg);
} else {
key = std::move(arg);
}
}
if (dbPath.empty()) {
printUsage(argv[0]);
return EXIT_FAILURE;
}
#ifdef EMSCRIPTEN
// Mount the database directory from NODEFS
EM_ASM({
const destDir = UTF8ToString($0);
try {
FS.mkdir("/data");
FS.mount(NODEFS, { root: destDir }, "/data");
} catch (err) {
console.error("Unable to mount directory", err);
}
}, dbPath.c_str());
dbPath = "/data";
#endif
leveldb::DB* db_ptr;
auto status = leveldb::DB::Open({}, dbPath, &db_ptr);
std::unique_ptr<leveldb::DB> db{db_ptr}; // clean up on exit
if (!status.ok()) {
std::cerr << "Unable to open leveldb: " << status.ToString() << std::endl;
return EXIT_FAILURE;
}
if (!key.empty()) {
if (!dumpValue(db.get(), key)) return EXIT_FAILURE;
} else {
dumpAllKeys(db.get(), keysOnly);
}
return EXIT_SUCCESS;
}