From d0a1d32488f45de9362a71da225e401c3b33b390 Mon Sep 17 00:00:00 2001 From: Jason Volk Date: Wed, 17 Nov 2021 17:07:24 -0800 Subject: [PATCH] ircd::db: Add SST scan interface w/ console suite. --- include/ircd/db/sst.h | 9 +++++ ircd/db.h | 1 + ircd/db_database.cc | 93 ++++++++++++++++++++++++++++++++++++++++++- modules/console.cc | 70 ++++++++++++++++++++++++++++++++ 4 files changed, 172 insertions(+), 1 deletion(-) diff --git a/include/ircd/db/sst.h b/include/ircd/db/sst.h index cef528357..9d0e97ba5 100644 --- a/include/ircd/db/sst.h +++ b/include/ircd/db/sst.h @@ -15,6 +15,7 @@ struct ircd::db::database::sst { struct info; struct dump; + struct scan; [[deprecated]] static void tool(const vector_view &args); @@ -97,3 +98,11 @@ struct ircd::db::database::sst::dump dump(dump &&) = delete; dump(const dump &) = delete; }; + +struct ircd::db::database::sst::scan +{ + using closure = std::function; + + scan(db::database &, const string_view &path, const closure &); + scan(db::database &, const string_view &path); // VerifyChecksum +}; diff --git a/ircd/db.h b/ircd/db.h index 974e227d3..b0790e7d2 100644 --- a/ircd/db.h +++ b/ircd/db.h @@ -50,6 +50,7 @@ #include #include #include +#include #include #include #include diff --git a/ircd/db_database.cc b/ircd/db_database.cc index bf93f41bd..17de80ea8 100644 --- a/ircd/db_database.cc +++ b/ircd/db_database.cc @@ -3615,7 +3615,98 @@ ircd::db::database::sst::tool(const vector_view &args) } // -// sst::dump::dump +// sst::scan +// + +ircd::db::database::sst::scan::scan(database &d, + const string_view &fpath) +{ + const auto &opts + { + d.d->GetOptions() + }; + + rocksdb::SstFileReader reader + { + opts + }; + + const string_view path_parts[] + { + fs::base::db, db::name(d), fpath + }; + + const auto path + { + fs::path_string(path_parts) + }; + + throw_on_error + { + reader.Open(path) + }; + + db::gopts gopts; + throw_on_error + { + reader.VerifyChecksum(make_opts(gopts)) + }; +} + +ircd::db::database::sst::scan::scan(database &d, + const string_view &fpath, + const closure &call) +{ + const auto &opts + { + d.d->GetOptions() + }; + + rocksdb::SstFileReader reader + { + opts + }; + + const string_view path_parts[] + { + fs::base::db, db::name(d), fpath + }; + + const auto path + { + fs::path_string(path_parts) + }; + + throw_on_error + { + reader.Open(path) + }; + + db::gopts gopts; + std::unique_ptr it + { + reader.NewIterator(make_opts(gopts)) + }; + + for(it->SeekToFirst(); db::valid(*it); it->Next()) + { + const auto &key + { + slice(it->key()) + }; + + const auto &val + { + slice(it->value()) + }; + + if(!call(key, val)) + break; + } +} + +// +// sst::dump // ircd::db::database::sst::dump::dump(db::column column, diff --git a/modules/console.cc b/modules/console.cc index 412fa7282..2b13929ad 100644 --- a/modules/console.cc +++ b/modules/console.cc @@ -4711,6 +4711,76 @@ console_cmd__db__sst__dump(opt &out, const string_view &line) return true; } +bool +console_cmd__db__sst__scan(opt &out, const string_view &line) +{ + const params param{line, " ", + { + "dbname", "path", + }}; + + const auto dbname + { + param.at("dbname") + }; + + const auto path + { + param.at("path") + }; + + auto &database + { + db::database::get(dbname) + }; + + db::database::sst::scan + { + database, path + }; + + out << "Completed without error." << std::endl; + return true; +} + +bool +console_cmd__db__sst__scan__count(opt &out, const string_view &line) +{ + const params param{line, " ", + { + "dbname", "path", + }}; + + const auto dbname + { + param.at("dbname") + }; + + const auto path + { + param.at("path") + }; + + auto &database + { + db::database::get(dbname) + }; + + size_t i(0); + db::database::sst::scan + { + database, path, [&out, &i] + (const auto &key, const auto &val) + { + ++i; + return true; + } + }; + + out << "Found " << i << " entries." << std::endl; + return true; +} + bool console_cmd__db__wal(opt &out, const string_view &line) try