From f925c9e5dfae14ad5b7eb00b7a1fa24f371557bc Mon Sep 17 00:00:00 2001 From: Jason Volk Date: Tue, 6 Feb 2018 15:14:14 -0800 Subject: [PATCH] modules/console: Add command to backup the events database as json::vector to file. This same vector can be replayed into a new database with an "exec" command. --- modules/console.cc | 57 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) diff --git a/modules/console.cc b/modules/console.cc index a1b313c13..80e77dfaa 100644 --- a/modules/console.cc +++ b/modules/console.cc @@ -299,6 +299,7 @@ console_cmd__key__default(const string_view &line) // static bool console_cmd__event__default(const string_view &line); +static bool console_cmd__event__dump(const string_view &line); static bool console_cmd__event__fetch(const string_view &line); bool @@ -314,11 +315,67 @@ console_cmd__event(const string_view &line) case hash("fetch"): return console_cmd__event__fetch(args); + case hash("dump"): + return console_cmd__event__dump(args); + default: return console_cmd__event__default(line); } } +bool +console_cmd__event__dump(const string_view &line) +{ + const auto filename + { + token(line, ' ', 0) + }; + + m::dbs::cursor cursor + { + "event_id" + }; + + static char buf[512_KiB]; + char *pos{buf}; + size_t foff{0}, ecount{0}, acount{0}; + for(auto it(begin(cursor)); it != end(cursor); ++it, ++ecount) + { + const auto remain + { + size_t(buf + sizeof(buf) - pos) + }; + + assert(remain >= 64_KiB && remain <= sizeof(buf)); + const mutable_buffer mb{pos, remain}; + const m::event event{*it}; + pos += json::print(mb, event); + + if(pos + 64_KiB > buf + sizeof(buf)) + { + const const_buffer cb{buf, pos}; + foff += size(fs::append(filename, cb)); + pos = buf; + ++acount; + } + } + + if(pos > buf) + { + const const_buffer cb{buf, pos}; + foff += size(fs::append(filename, cb)); + ++acount; + } + + out << "Dumped " << ecount << " events" + << " using " << foff << " bytes" + << " in " << acount << " writes" + << " to " << filename + << std::endl; + + return true; +} + bool console_cmd__event__fetch(const string_view &line) {