delete command, split up compact and gc into separate commands

This commit is contained in:
Doug Hoyte
2023-02-11 07:36:09 -05:00
parent 6fe1df8e37
commit decc3aea26
4 changed files with 125 additions and 18 deletions

View File

@ -10,15 +10,13 @@
static const char USAGE[] =
R"(
Usage:
compact export <output_file>
compact quad-gc
compact <output_file>
)";
void cmd_compact(const std::vector<std::string> &subArgs) {
std::map<std::string, docopt::value> args = docopt::docopt(USAGE, subArgs, true, "");
if (args["export"].asBool()) {
std::string outputFile = args["<output_file>"].asString();
if (outputFile == "-") {
@ -31,9 +29,4 @@ void cmd_compact(const std::vector<std::string> &subArgs) {
env.copy_fd(::fileno(f));
}
} else if (args["quad-gc"].asBool()) {
auto qdb = getQdbInstance();
quadrableGarbageCollect(qdb, 2);
}
}

93
src/cmd_delete.cpp Normal file
View File

@ -0,0 +1,93 @@
#include <iostream>
#include <docopt.h>
#include "golpe.h"
#include "DBScan.h"
#include "events.h"
#include "gc.h"
static const char USAGE[] =
R"(
Usage:
delete [--age=<age>] [--filter=<filter>] [--dry-run] [--no-gc]
)";
void cmd_delete(const std::vector<std::string> &subArgs) {
std::map<std::string, docopt::value> args = docopt::docopt(USAGE, subArgs, true, "");
uint64_t age = MAX_U64;
if (args["--age"]) age = args["--age"].asLong();
std::string filterStr;
if (args["--filter"]) filterStr = args["--filter"].asString();
bool dryRun = args["--dry-run"].asBool();
bool noGc = args["--no-gc"].asBool();
if (filterStr.size() == 0 && age == MAX_U64) throw herr("must specify --age and/or --filter");
if (filterStr.size() == 0) filterStr = "{}";
auto filter = tao::json::from_string(filterStr);
auto now = hoytech::curr_time_s();
if (age != MAX_U64) {
if (age > now) age = now;
if (filter.optional<uint64_t>("until")) throw herr("--age is not compatible with filter containing 'until'");
filter["until"] = now - age;
}
auto filterGroup = NostrFilterGroup::unwrapped(filter, MAX_U64);
Subscription sub(1, "junkSub", filterGroup);
DBScanQuery query(sub);
btree_set<uint64_t> levIds;
{
auto txn = env.txn_ro();
while (1) {
bool complete = query.process(txn, MAX_U64, false, [&](const auto &sub, uint64_t levId){
levIds.insert(levId);
});
if (complete) break;
}
}
if (dryRun) {
LI << "Would delete " << levIds.size() << " events";
return;
}
auto qdb = getQdbInstance();
LI << "Deleting " << levIds.size() << " events";
{
auto txn = env.txn_rw();
auto changes = qdb.change();
for (auto levId : levIds) {
auto view = env.lookup_Event(txn, levId);
if (!view) continue; // Deleted in between transactions
deleteEvent(txn, changes, *view);
}
changes.apply(txn);
txn.commit();
}
if (!noGc) quadrableGarbageCollect(qdb, 2);
}

22
src/cmd_gc.cpp Normal file
View File

@ -0,0 +1,22 @@
#include <unistd.h>
#include <stdio.h>
#include <docopt.h>
#include "golpe.h"
#include "gc.h"
static const char USAGE[] =
R"(
Usage:
gc
)";
void cmd_gc(const std::vector<std::string> &subArgs) {
std::map<std::string, docopt::value> args = docopt::docopt(USAGE, subArgs, true, "");
auto qdb = getQdbInstance();
quadrableGarbageCollect(qdb, 2);
}

View File

@ -20,8 +20,7 @@ void cmd_scan(const std::vector<std::string> &subArgs) {
uint64_t pause = 0;
if (args["--pause"]) pause = args["--pause"].asLong();
bool metrics = false;
if (args["--metrics"]) metrics = true;
bool metrics = args["--metrics"].asBool();
std::string filterStr = args["<filter>"].asString();