reverse export

This commit is contained in:
Doug Hoyte
2023-02-19 15:11:30 -05:00
parent 188a7ff6f1
commit 54f467cc1c

View File

@ -9,7 +9,7 @@
static const char USAGE[] =
R"(
Usage:
export [--since=<since>] [--until=<until>] [--include-ephemeral]
export [--since=<since>] [--until=<until>] [--reverse] [--include-ephemeral]
)";
@ -19,6 +19,8 @@ void cmd_export(const std::vector<std::string> &subArgs) {
uint64_t since = 0, until = MAX_U64;
if (args["--since"]) since = args["--since"].asLong();
if (args["--until"]) until = args["--until"].asLong();
bool includeEphemeral = args["--include-ephemeral"].asBool();
bool reverse = args["--reverse"].asBool();
Decompressor decomp;
@ -27,8 +29,15 @@ void cmd_export(const std::vector<std::string> &subArgs) {
auto dbVersion = getDBVersion(txn);
auto qdb = getQdbInstance(txn);
env.generic_foreachFull(txn, env.dbi_Event__created_at, lmdb::to_sv<uint64_t>(since), lmdb::to_sv<uint64_t>(0), [&](auto k, auto v) {
if (lmdb::from_sv<uint64_t>(k) > until) return false;
uint64_t start = reverse ? until : since;
uint64_t startDup = reverse ? MAX_U64 : 0;
env.generic_foreachFull(txn, env.dbi_Event__created_at, lmdb::to_sv<uint64_t>(start), lmdb::to_sv<uint64_t>(startDup), [&](auto k, auto v) {
if (reverse) {
if (lmdb::from_sv<uint64_t>(k) < since) return false;
} else {
if (lmdb::from_sv<uint64_t>(k) > until) return false;
}
auto view = env.lookup_Event(txn, lmdb::from_sv<uint64_t>(v));
if (!view) throw herr("missing event from index, corrupt DB?");
@ -41,12 +50,10 @@ void cmd_export(const std::vector<std::string> &subArgs) {
return true;
}
if (!args["--include-ephemeral"].asBool()) {
if (isEphemeralEvent(view->flat_nested()->kind())) return true;
}
if (!includeEphemeral && isEphemeralEvent(view->flat_nested()->kind())) return true;
std::cout << getEventJson(txn, decomp, view->primaryKeyId) << "\n";
return true;
});
}, reverse);
}