From 83bdb7161c74a8c9318a25c4bb6c48799be5294f Mon Sep 17 00:00:00 2001 From: Jason Volk Date: Thu, 22 Mar 2018 20:49:11 -0700 Subject: [PATCH] ircd::db: Add interface to iterate the database's txn log. --- include/ircd/db/txn.h | 5 +++++ ircd/db.cc | 50 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 55 insertions(+) diff --git a/include/ircd/db/txn.h b/include/ircd/db/txn.h index e5c44562c..9d5013d42 100644 --- a/include/ircd/db/txn.h +++ b/include/ircd/db/txn.h @@ -19,6 +19,11 @@ namespace ircd::db bool until(const txn &, const std::function &); void for_each(const txn &, const std::function &); std::string debug(const txn &); + + using seq_closure_bool = std::function; + using seq_closure = std::function; + bool for_each(database &d, const uint64_t &seq, const seq_closure_bool &); + void for_each(database &d, const uint64_t &seq, const seq_closure &); } struct ircd::db::txn diff --git a/ircd/db.cc b/ircd/db.cc index 26aa68122..27ad52fae 100644 --- a/ircd/db.cc +++ b/ircd/db.cc @@ -2248,6 +2248,56 @@ noexcept // db/txn.h // +void +ircd::db::for_each(database &d, + const uint64_t &seq, + const seq_closure &closure) +{ + for_each(d, seq, seq_closure_bool{[&closure] + (txn &txn, const uint64_t &seq) + { + closure(txn, seq); + return true; + }}); +} + +bool +ircd::db::for_each(database &d, + const uint64_t &seq, + const seq_closure_bool &closure) +{ + std::unique_ptr tit; + throw_on_error + { + d.d->GetUpdatesSince(seq, &tit) + }; + + assert(bool(tit)); + for(; tit->Valid(); tit->Next()) + { + auto batchres + { + tit->GetBatch() + }; + + throw_on_error + { + tit->status() + }; + + db::txn txn + { + d, std::move(batchres.writeBatchPtr) + }; + + assert(bool(txn.wb)); + if(!closure(txn, batchres.sequence)) + return false; + } + + return true; +} + std::string ircd::db::debug(const txn &t) {