2018-01-18 03:14:13 +01:00
|
|
|
// Matrix Construct
|
|
|
|
//
|
|
|
|
// Copyright (C) Matrix Construct Developers, Authors & Contributors
|
|
|
|
// Copyright (C) 2016-2018 Jason Volk <jason@zemos.net>
|
|
|
|
//
|
|
|
|
// Permission to use, copy, modify, and/or distribute this software for any
|
|
|
|
// purpose with or without fee is hereby granted, provided that the above
|
2018-02-04 03:22:01 +01:00
|
|
|
// copyright notice and this permission notice is present in all copies. The
|
|
|
|
// full license for this software is available in the LICENSE file.
|
2017-09-19 04:19:02 +02:00
|
|
|
|
|
|
|
#pragma once
|
2018-01-30 18:58:36 +01:00
|
|
|
#define HAVE_IRCD_DB_TXN_H
|
2017-09-19 04:19:02 +02:00
|
|
|
|
|
|
|
namespace ircd::db
|
|
|
|
{
|
2018-01-30 18:58:36 +01:00
|
|
|
struct txn;
|
2017-09-19 04:19:02 +02:00
|
|
|
|
2018-01-30 18:58:36 +01:00
|
|
|
bool test(const txn &, const std::function<bool (const delta &)> &);
|
|
|
|
bool until(const txn &, const std::function<bool (const delta &)> &);
|
|
|
|
void for_each(const txn &, const std::function<void (const delta &)> &);
|
|
|
|
std::string debug(const txn &);
|
2017-09-19 04:19:02 +02:00
|
|
|
}
|
|
|
|
|
2018-01-30 18:58:36 +01:00
|
|
|
class ircd::db::txn
|
2017-09-19 04:19:02 +02:00
|
|
|
{
|
|
|
|
database *d {nullptr};
|
|
|
|
std::unique_ptr<rocksdb::WriteBatch> wb;
|
|
|
|
|
|
|
|
public:
|
|
|
|
struct opts;
|
2017-09-19 05:43:32 +02:00
|
|
|
struct checkpoint;
|
2017-09-19 04:19:02 +02:00
|
|
|
struct append;
|
|
|
|
struct handler;
|
|
|
|
|
|
|
|
explicit operator const rocksdb::WriteBatch &() const;
|
|
|
|
explicit operator const database &() const;
|
|
|
|
explicit operator rocksdb::WriteBatch &();
|
|
|
|
explicit operator database &();
|
|
|
|
|
2017-09-19 05:40:13 +02:00
|
|
|
string_view get(const op &, const string_view &col, const string_view &key) const;
|
|
|
|
string_view at(const op &, const string_view &col, const string_view &key) const;
|
|
|
|
bool has(const op &, const string_view &col, const string_view &key) const;
|
|
|
|
|
|
|
|
delta get(const op &, const string_view &col) const;
|
|
|
|
delta at(const op &, const string_view &col) const;
|
|
|
|
bool has(const op &, const string_view &col) const;
|
2017-09-19 04:19:02 +02:00
|
|
|
bool has(const op &) const;
|
2017-09-19 05:40:13 +02:00
|
|
|
|
2017-09-19 04:19:02 +02:00
|
|
|
size_t bytes() const;
|
|
|
|
size_t size() const;
|
|
|
|
|
|
|
|
// commit
|
|
|
|
void operator()(database &, const sopts & = {});
|
|
|
|
void operator()(const sopts & = {});
|
|
|
|
|
|
|
|
// clear
|
|
|
|
void clear();
|
|
|
|
|
2018-01-30 18:58:36 +01:00
|
|
|
txn() = default;
|
|
|
|
txn(database &);
|
|
|
|
txn(database &, const opts &);
|
|
|
|
~txn() noexcept;
|
2017-09-19 04:19:02 +02:00
|
|
|
};
|
|
|
|
|
2018-01-30 18:58:36 +01:00
|
|
|
struct ircd::db::txn::append
|
2017-09-19 04:19:02 +02:00
|
|
|
{
|
2018-01-30 18:58:36 +01:00
|
|
|
append(txn &, database &, const delta &);
|
|
|
|
append(txn &, column &, const column::delta &);
|
|
|
|
append(txn &, const cell::delta &);
|
|
|
|
append(txn &, const row::delta &);
|
|
|
|
append(txn &, const delta &);
|
|
|
|
append(txn &, const string_view &key, const json::iov &);
|
|
|
|
template<class... T> append(txn &, const string_view &key, const json::tuple<T...> &);
|
2017-09-19 04:19:02 +02:00
|
|
|
};
|
|
|
|
|
2018-01-30 18:58:36 +01:00
|
|
|
struct ircd::db::txn::checkpoint
|
2017-09-19 04:19:02 +02:00
|
|
|
{
|
2018-01-30 18:58:36 +01:00
|
|
|
txn &t;
|
2017-09-19 05:43:32 +02:00
|
|
|
|
2018-01-30 18:58:36 +01:00
|
|
|
checkpoint(txn &);
|
2017-09-19 05:43:32 +02:00
|
|
|
~checkpoint() noexcept;
|
2017-09-19 04:19:02 +02:00
|
|
|
};
|
|
|
|
|
2018-01-30 18:58:36 +01:00
|
|
|
struct ircd::db::txn::opts
|
2017-09-19 04:19:02 +02:00
|
|
|
{
|
|
|
|
size_t reserve_bytes = 0;
|
|
|
|
size_t max_bytes = 0;
|
|
|
|
};
|
2017-10-16 06:13:44 +02:00
|
|
|
|
|
|
|
template<class... T>
|
2018-01-30 18:58:36 +01:00
|
|
|
ircd::db::txn::append::append(txn &txn,
|
2017-10-16 06:13:44 +02:00
|
|
|
const string_view &key,
|
|
|
|
const json::tuple<T...> &tuple)
|
|
|
|
{
|
2018-01-30 18:58:36 +01:00
|
|
|
for_each(tuple, [&txn, &key](const auto &col, auto&& val)
|
2017-10-16 06:13:44 +02:00
|
|
|
{
|
|
|
|
if(defined(val)) append
|
|
|
|
{
|
2018-01-30 18:58:36 +01:00
|
|
|
txn, delta
|
2017-10-16 06:13:44 +02:00
|
|
|
{
|
|
|
|
col, key, byte_view<string_view>{val}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
});
|
|
|
|
}
|