0
0
Fork 0
mirror of https://github.com/matrix-construct/construct synced 2024-11-17 23:40:57 +01:00

ircd::db: Add generalized database delta interface.

This commit is contained in:
Jason Volk 2017-08-30 14:07:36 -07:00
parent f729fd0917
commit 19c5e6c83b
4 changed files with 92 additions and 0 deletions

View file

@ -30,6 +30,8 @@
namespace ircd::db
{
struct init;
struct gopts;
struct sopts;
struct cell;
struct row;
struct column;

View file

@ -83,6 +83,14 @@ struct ircd::db::database
const column &operator[](const string_view &) const;
column &operator[](const string_view &);
// [SET] Perform operations in a sequence as a single transaction.
void operator()(const sopts &, const delta *const &begin, const delta *const &end);
void operator()(const sopts &, const std::initializer_list<delta> &);
void operator()(const sopts &, const delta &);
void operator()(const delta *const &begin, const delta *const &end);
void operator()(const std::initializer_list<delta> &);
void operator()(const delta &);
database(std::string name,
std::string options,
description);

View file

@ -43,9 +43,24 @@ namespace ircd::db
using merge_closure = std::function<std::string (const string_view &key, const merge_delta &)>;
using update_closure = std::function<std::string (const string_view &key, merge_delta &)>;
struct delta;
struct comparator;
}
struct ircd::db::delta
:std::tuple<op, string_view, string_view, string_view>
{
delta(const string_view &col, const string_view &key, const string_view &val, const enum op &op = op::SET)
:std::tuple<enum op, string_view, string_view, string_view>{op, col, key, val}
{}
delta(const enum op &op, const string_view &col, const string_view &key, const string_view &val = {})
:std::tuple<enum op, string_view, string_view, string_view>{op, col, key, val}
{}
delta() = default;
};
struct ircd::db::comparator
{
std::string name;

View file

@ -536,6 +536,73 @@ noexcept
background_errors);
}
void
ircd::db::database::operator()(const delta &delta)
{
operator()(sopts{}, delta);
}
void
ircd::db::database::operator()(const std::initializer_list<delta> &deltas)
{
operator()(sopts{}, deltas);
}
void
ircd::db::database::operator()(const delta *const &begin,
const delta *const &end)
{
operator()(sopts{}, begin, end);
}
void
ircd::db::database::operator()(const sopts &sopts,
const delta &delta)
{
operator()(sopts, &delta, &delta + 1);
}
void
ircd::db::database::operator()(const sopts &sopts,
const std::initializer_list<delta> &deltas)
{
operator()(sopts, std::begin(deltas), std::end(deltas));
}
void
ircd::db::database::operator()(const sopts &sopts,
const delta *const &begin,
const delta *const &end)
{
rocksdb::WriteBatch batch;
std::for_each(begin, end, [this, &batch]
(const delta &delta)
{
const auto &op(std::get<op>(delta));
const auto &col(std::get<1>(delta));
const auto &key(std::get<2>(delta));
const auto &val(std::get<3>(delta));
db::column column(operator[](col));
append(batch, column, db::column::delta
{
op,
key,
val
});
});
auto opts(make_opts(sopts));
log.debug("'%s' @%lu PUT %zu column deltas",
name,
sequence(*this),
std::distance(begin, end));
throw_on_error
{
d->Write(opts, &batch)
};
}
ircd::db::database::column &
ircd::db::database::operator[](const string_view &name)
try