mirror of
https://github.com/matrix-construct/construct
synced 2024-12-28 00:14:07 +01:00
ircd::db: Leverage the RocksDB WriteBatch with an initializer_list.
This commit is contained in:
parent
21db4baab3
commit
0d09170507
2 changed files with 78 additions and 0 deletions
|
@ -119,6 +119,27 @@ struct opts
|
|||
template<class... list> opts(list&&... l): optlist<opt>{std::forward<list>(l)...} {}
|
||||
};
|
||||
|
||||
enum op
|
||||
{
|
||||
PUT,
|
||||
MERGE,
|
||||
DELETE,
|
||||
SINGLE_DELETE,
|
||||
DELETE_RANGE,
|
||||
};
|
||||
|
||||
struct delta
|
||||
:std::tuple<op, string_view, string_view>
|
||||
{
|
||||
delta(const enum op &op, const string_view &key, const string_view &val = {})
|
||||
:std::tuple<enum op, string_view, string_view>{op, key, val}
|
||||
{}
|
||||
|
||||
delta(const string_view &key, const string_view &val, const enum op &op = op::PUT)
|
||||
:std::tuple<enum op, string_view, string_view>{op, key, val}
|
||||
{}
|
||||
};
|
||||
|
||||
struct handle
|
||||
{
|
||||
struct const_iterator;
|
||||
|
@ -146,6 +167,10 @@ struct handle
|
|||
void operator()(const string_view &key, const closure &func, const gopts & = {});
|
||||
void operator()(const string_view &key, const gopts &, const closure &func);
|
||||
|
||||
// Perform operations in a sequence as a single transaction.
|
||||
void operator()(const delta &, const sopts & = {});
|
||||
void operator()(const std::initializer_list<delta> &, const sopts & = {});
|
||||
|
||||
// Get data into your buffer. The signed char buffer is null terminated; the unsigned is not.
|
||||
size_t get(const string_view &key, char *const &buf, const size_t &max, const gopts & = {});
|
||||
size_t get(const string_view &key, uint8_t *const &buf, const size_t &max, const gopts & = {});
|
||||
|
|
53
ircd/db.cc
53
ircd/db.cc
|
@ -263,6 +263,59 @@ db::handle::get(const string_view &key,
|
|||
return ret;
|
||||
}
|
||||
|
||||
namespace ircd {
|
||||
namespace db {
|
||||
|
||||
static
|
||||
void append(rocksdb::WriteBatch &batch,
|
||||
const delta &delta)
|
||||
{
|
||||
const rocksdb::Slice k
|
||||
{
|
||||
std::get<1>(delta).data(), std::get<1>(delta).size()
|
||||
};
|
||||
|
||||
const rocksdb::Slice v
|
||||
{
|
||||
std::get<2>(delta).data(), std::get<2>(delta).size()
|
||||
};
|
||||
|
||||
switch(std::get<0>(delta))
|
||||
{
|
||||
case op::PUT: batch.Put(k, v); break;
|
||||
case op::MERGE: batch.Merge(k, v); break;
|
||||
case op::DELETE: batch.Delete(k); break;
|
||||
case op::SINGLE_DELETE: batch.SingleDelete(k); break;
|
||||
case op::DELETE_RANGE: batch.DeleteRange(k, v); break;
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace db
|
||||
} // namespace ircd
|
||||
|
||||
void
|
||||
db::handle::operator()(const std::initializer_list<delta> &deltas,
|
||||
const sopts &sopts)
|
||||
{
|
||||
rocksdb::WriteBatch batch;
|
||||
for(const auto &delta : deltas)
|
||||
append(batch, delta);
|
||||
|
||||
auto opts(make_opts(sopts));
|
||||
throw_on_error(d->Write(opts, &batch));
|
||||
}
|
||||
|
||||
void
|
||||
db::handle::operator()(const delta &delta,
|
||||
const sopts &sopts)
|
||||
{
|
||||
rocksdb::WriteBatch batch;
|
||||
append(batch, delta);
|
||||
|
||||
auto opts(make_opts(sopts));
|
||||
throw_on_error(d->Write(opts, &batch));
|
||||
}
|
||||
|
||||
void
|
||||
db::handle::operator()(const string_view &key,
|
||||
const gopts &gopts,
|
||||
|
|
Loading…
Reference in a new issue