feat: add reset_storage function

This commit is contained in:
erhant 2026-07-02 14:09:35 +03:00
parent e17bc549f4
commit 731bcc4a41
2 changed files with 57 additions and 0 deletions

View File

@ -5,6 +5,7 @@
#include <cstdint>
#include <cstdio>
#include <fstream>
#include <string>
using namespace marshalling;
@ -83,6 +84,54 @@ int64_t LezIndexerModuleImpl::stop_indexer() {
return 0;
}
int64_t LezIndexerModuleImpl::reset_storage(const std::string& config_path) {
// Stop first so RocksDB is closed before its files are deleted
const int64_t stop_code = stop_indexer();
if (stop_code != 0) {
error("reset_storage", "could not stop indexer before wiping storage");
return stop_code;
}
const std::string& storage = instancePersistencePath();
if (storage.empty()) {
error("reset_storage", "no instance persistence path; refusing to wipe (nothing to target)");
return -1;
}
// FFI stores RocksDB at <storage>/rocksdb-{channel_id}
std::string channel_id;
try {
std::ifstream in(config_path);
if (!in) {
error("reset_storage", "could not open config " + config_path);
return -1;
}
channel_id = nlohmann::json::parse(in).at("channel_id").get<std::string>();
} catch (const std::exception& e) {
error("reset_storage", std::string("could not read channel_id from config: ") + e.what());
return -1;
}
if (channel_id.empty()) {
error("reset_storage", "config has an empty channel_id");
return -1;
}
const std::filesystem::path store = std::filesystem::path(storage) / ("rocksdb-" + channel_id);
std::error_code ec;
if (!std::filesystem::exists(store, ec)) {
info("reset_storage", "no store at " + store.string() + "; nothing to wipe");
return 0;
}
std::filesystem::remove_all(store, ec);
if (ec) {
error("reset_storage", "failed to remove " + store.string() + ": " + ec.message());
return -1;
}
info("reset_storage", "wiped rocksdb store " + store.string());
return 0;
}
// === Indexer Queries ===
//
// Each method calls the matching query_* FFI function on the handle we hold,

View File

@ -41,6 +41,14 @@ public:
/// own). Returns 0 on success, else the FFI OperationStatus code.
int64_t stop_indexer();
/// Stop the indexer (if running) and delete the RocksDB store for the config's
/// channel — `<instancePersistencePath>/rocksdb-<channel_id>`, the exact path the
/// FFI uses — so the next start_indexer re-indexes from scratch. The recovery
/// path when the store is stale against a different/reset chain. Pass the same
/// `config_path` given to start_indexer; does NOT restart. Returns 0 on success,
/// else non-zero. int64_t for the same codegen reason as start_indexer.
int64_t reset_storage(const std::string& config_path);
/// Account by id, accepting Base58 (canonical) or 32-byte hex. The returned
/// JSON omits the id; callers inject the queried id themselves.
std::string getAccount(const std::string& account_id);