diff --git a/src/cmd_export.cpp b/src/cmd_export.cpp index 3fb5e14..2e62621 100644 --- a/src/cmd_export.cpp +++ b/src/cmd_export.cpp @@ -9,7 +9,7 @@ static const char USAGE[] = R"( Usage: - export [--since=] [--until=] [--include-ephemeral] + export [--since=] [--until=] [--reverse] [--include-ephemeral] )"; @@ -19,6 +19,8 @@ void cmd_export(const std::vector &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 &subArgs) { auto dbVersion = getDBVersion(txn); auto qdb = getQdbInstance(txn); - env.generic_foreachFull(txn, env.dbi_Event__created_at, lmdb::to_sv(since), lmdb::to_sv(0), [&](auto k, auto v) { - if (lmdb::from_sv(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(start), lmdb::to_sv(startDup), [&](auto k, auto v) { + if (reverse) { + if (lmdb::from_sv(k) < since) return false; + } else { + if (lmdb::from_sv(k) > until) return false; + } auto view = env.lookup_Event(txn, lmdb::from_sv(v)); if (!view) throw herr("missing event from index, corrupt DB?"); @@ -41,12 +50,10 @@ void cmd_export(const std::vector &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); }