0
0
Fork 0
mirror of https://github.com/matrix-construct/construct synced 2024-11-29 10:12:39 +01:00

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.
This commit is contained in:
Jason Volk 2018-02-06 15:14:14 -08:00
parent da38c9164f
commit f925c9e5df

View file

@ -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)
{