2018-01-17 18:14:13 -08:00
|
|
|
// Matrix Construct
|
2017-08-23 15:37:06 -06:00
|
|
|
//
|
2018-01-17 18:14:13 -08:00
|
|
|
// Copyright (C) Matrix Construct Developers, Authors & Contributors
|
|
|
|
// Copyright (C) 2016-2018 Jason Volk <jason@zemos.net>
|
2017-08-23 15:37:06 -06:00
|
|
|
//
|
2018-01-17 18:14:13 -08:00
|
|
|
// 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-01-18 17:59:22 -08:00
|
|
|
// copyright notice and this permission notice is present in all copies. The
|
|
|
|
// full license for this software is available in the LICENSE file.
|
2018-01-17 18:14:13 -08:00
|
|
|
|
|
|
|
#pragma once
|
|
|
|
#define HAVE_IRCD_DB_DATABASE_H
|
|
|
|
|
2017-08-28 14:51:22 -07:00
|
|
|
namespace ircd::db
|
|
|
|
{
|
|
|
|
struct database;
|
|
|
|
|
2018-12-08 15:17:13 -08:00
|
|
|
// Broad conf items
|
|
|
|
extern conf::item<bool> open_check;
|
|
|
|
extern conf::item<std::string> open_recover;
|
2020-01-11 19:14:32 -08:00
|
|
|
extern conf::item<bool> open_repair;
|
2020-06-10 21:49:27 -07:00
|
|
|
extern conf::item<bool> open_slave;
|
2020-06-10 04:29:12 -07:00
|
|
|
extern conf::item<bool> auto_compact;
|
2018-12-08 15:17:13 -08:00
|
|
|
|
2018-04-03 10:58:16 -07:00
|
|
|
// General information
|
2017-08-28 14:51:22 -07:00
|
|
|
const std::string &name(const database &);
|
2018-05-22 17:01:01 -07:00
|
|
|
const std::string &uuid(const database &);
|
2018-03-22 19:35:50 -07:00
|
|
|
uint64_t sequence(const database &); // Latest sequence number
|
2018-10-31 15:29:00 -07:00
|
|
|
const std::vector<std::string> &errors(const database &);
|
2018-04-03 11:44:57 -07:00
|
|
|
std::vector<std::string> files(const database &, uint64_t &msz);
|
|
|
|
std::vector<std::string> files(const database &);
|
2018-12-10 14:04:30 -08:00
|
|
|
std::vector<std::string> wals(const database &);
|
2018-04-17 14:13:36 -07:00
|
|
|
size_t file_count(const database &);
|
|
|
|
size_t bytes(const database &);
|
2018-12-12 09:57:46 -08:00
|
|
|
options getopt(const database &);
|
2018-12-19 12:52:08 -08:00
|
|
|
log::level loglevel(const database &);
|
2018-03-22 19:35:50 -07:00
|
|
|
|
2018-04-03 10:58:16 -07:00
|
|
|
// Property information interface
|
|
|
|
using prop_int = uint64_t;
|
|
|
|
using prop_str = std::string;
|
2018-04-09 12:55:22 -07:00
|
|
|
using prop_map = std::map<std::string, std::string>;
|
2018-04-03 10:58:16 -07:00
|
|
|
template<class R = prop_int> R property(const database &, const string_view &name);
|
|
|
|
template<> prop_int property(const database &, const string_view &name);
|
|
|
|
|
2018-05-14 16:17:43 -07:00
|
|
|
// Access to the database's row cache (see cache.h interface)
|
|
|
|
const rocksdb::Cache *cache(const database &);
|
|
|
|
rocksdb::Cache *cache(database &);
|
|
|
|
|
2018-04-03 10:58:16 -07:00
|
|
|
// Control panel
|
2018-12-19 12:52:08 -08:00
|
|
|
void loglevel(database &, const log::level &);
|
2018-04-20 14:10:59 -07:00
|
|
|
void setopt(database &, const string_view &key, const string_view &val);
|
2018-04-20 13:38:49 -07:00
|
|
|
void fdeletions(database &, const bool &enable, const bool &force = false);
|
2018-05-24 20:01:52 -07:00
|
|
|
uint64_t checkpoint(database &);
|
2018-12-16 18:00:54 -08:00
|
|
|
void bgcancel(database &, const bool &blocking = true);
|
2018-12-14 17:27:48 -08:00
|
|
|
void bgcontinue(database &);
|
|
|
|
void bgpause(database &);
|
2020-06-10 21:49:27 -07:00
|
|
|
void refresh(database &);
|
2018-10-31 15:03:32 -07:00
|
|
|
void resume(database &);
|
2020-01-08 11:23:47 -08:00
|
|
|
void check(database &, const string_view &file);
|
2018-05-22 16:52:34 -07:00
|
|
|
void check(database &);
|
2018-12-12 15:53:16 -08:00
|
|
|
void compact(database &, const std::pair<int, int> &level, const compactor & = {});
|
2018-09-19 16:36:01 -07:00
|
|
|
void compact(database &, const compactor & = {});
|
2018-12-19 13:50:42 -08:00
|
|
|
void sort(database &, const bool &blocking = true, const bool &now = true);
|
2018-04-26 17:19:29 -07:00
|
|
|
void flush(database &, const bool &sync = false);
|
2018-03-22 19:35:50 -07:00
|
|
|
void sync(database &);
|
2017-08-28 14:51:22 -07:00
|
|
|
}
|
|
|
|
|
2018-01-17 18:14:13 -08:00
|
|
|
/// Database instance
|
|
|
|
///
|
|
|
|
/// There can be only one instance of this class for each database, so it is
|
|
|
|
/// always shared and must be make_shared(). The database is open when an
|
|
|
|
/// instance is constructed and closed when the instance destructs.
|
|
|
|
///
|
|
|
|
/// The construction must have the same consistent descriptor set used every
|
|
|
|
/// time otherwise bad things happen.
|
|
|
|
///
|
|
|
|
/// The instance registers and deregisters itself in a global set of open
|
|
|
|
/// databases and can be found that way if necessary.
|
|
|
|
///
|
2018-09-02 17:29:44 -07:00
|
|
|
/// Internal structures declared within this class comprise the backend which
|
|
|
|
/// supports RocksDB; they are not involved in the standard include stack
|
|
|
|
/// beyond this declaration and not meant for IRCd developers merely using the
|
|
|
|
/// ircd::db interface.
|
|
|
|
///
|
2017-08-28 14:51:22 -07:00
|
|
|
struct ircd::db::database
|
2017-08-30 14:21:18 -07:00
|
|
|
:std::enable_shared_from_this<database>
|
2018-05-24 20:01:52 -07:00
|
|
|
,instance_list<database>
|
2017-03-30 15:57:08 -07:00
|
|
|
{
|
|
|
|
struct options;
|
2017-08-23 15:37:06 -06:00
|
|
|
struct events;
|
|
|
|
struct stats;
|
2018-12-10 13:26:46 -08:00
|
|
|
struct logger;
|
2017-08-23 15:37:06 -06:00
|
|
|
struct mergeop;
|
2017-03-30 15:57:08 -07:00
|
|
|
struct snapshot;
|
|
|
|
struct comparator;
|
2017-08-30 22:12:58 -07:00
|
|
|
struct prefix_transform;
|
2018-09-18 15:07:09 -07:00
|
|
|
struct compaction_filter;
|
2017-03-30 15:57:08 -07:00
|
|
|
struct column;
|
2018-01-17 18:34:20 -08:00
|
|
|
struct env;
|
2018-09-02 17:29:44 -07:00
|
|
|
struct cache;
|
2018-09-22 13:13:05 -07:00
|
|
|
struct sst;
|
2018-12-10 14:04:30 -08:00
|
|
|
struct wal;
|
2019-04-20 14:22:08 -07:00
|
|
|
struct wal_filter;
|
2020-05-04 20:30:47 -07:00
|
|
|
struct allocator;
|
2017-11-15 17:29:54 -08:00
|
|
|
|
2017-08-23 15:37:06 -06:00
|
|
|
std::string name;
|
2018-05-24 20:01:52 -07:00
|
|
|
uint64_t checkpoint;
|
|
|
|
std::string path;
|
2018-12-02 17:39:33 -08:00
|
|
|
std::string optstr;
|
2020-06-10 21:49:27 -07:00
|
|
|
bool fsck, slave, read_only;
|
2018-01-17 19:22:35 -08:00
|
|
|
std::shared_ptr<struct env> env;
|
2017-08-23 15:37:06 -06:00
|
|
|
std::shared_ptr<struct stats> stats;
|
2018-12-10 13:26:46 -08:00
|
|
|
std::shared_ptr<struct logger> logger;
|
2017-08-23 15:37:06 -06:00
|
|
|
std::shared_ptr<struct events> events;
|
|
|
|
std::shared_ptr<struct mergeop> mergeop;
|
2019-04-20 14:22:08 -07:00
|
|
|
std::unique_ptr<struct wal_filter> wal_filter;
|
2020-05-04 20:30:47 -07:00
|
|
|
std::shared_ptr<struct allocator> allocator;
|
2018-04-20 15:45:57 -07:00
|
|
|
std::shared_ptr<rocksdb::SstFileManager> ssts;
|
2018-09-02 17:29:44 -07:00
|
|
|
std::shared_ptr<rocksdb::Cache> row_cache;
|
2017-11-15 17:29:54 -08:00
|
|
|
std::vector<descriptor> descriptors;
|
2018-12-02 17:39:33 -08:00
|
|
|
std::unique_ptr<rocksdb::DBOptions> opts;
|
2018-10-22 07:09:55 -07:00
|
|
|
std::unordered_map<string_view, std::shared_ptr<column>> column_names;
|
2018-04-20 16:22:01 -07:00
|
|
|
std::unique_ptr<rocksdb::DB> d;
|
2018-10-22 07:09:55 -07:00
|
|
|
std::vector<std::shared_ptr<column>> column_index; // indexed by cfid
|
|
|
|
std::list<std::shared_ptr<column>> columns; // active only
|
2018-05-22 17:01:01 -07:00
|
|
|
std::string uuid;
|
2018-05-24 20:01:52 -07:00
|
|
|
std::unique_ptr<rocksdb::Checkpoint> checkpointer;
|
2018-10-31 15:29:00 -07:00
|
|
|
std::vector<std::string> errors;
|
2017-04-02 21:02:32 -07:00
|
|
|
|
|
|
|
operator std::shared_ptr<database>() { return shared_from_this(); }
|
2017-03-30 15:57:08 -07:00
|
|
|
operator const rocksdb::DB &() const { return *d; }
|
|
|
|
operator rocksdb::DB &() { return *d; }
|
|
|
|
|
2019-01-16 13:21:36 -08:00
|
|
|
// Lookup a column ID by name; throws not_found or returns -1.
|
|
|
|
int32_t cfid(const std::nothrow_t, const string_view &name) const;
|
|
|
|
uint32_t cfid(const string_view &name) const;
|
|
|
|
|
|
|
|
// Obtain a reference to a column or throw
|
2017-09-18 19:17:36 -07:00
|
|
|
const column &operator[](const uint32_t &id) const;
|
|
|
|
const column &operator[](const string_view &name) const;
|
|
|
|
column &operator[](const uint32_t &id);
|
|
|
|
column &operator[](const string_view &name);
|
2017-03-30 15:57:08 -07:00
|
|
|
|
2017-08-30 14:07:36 -07:00
|
|
|
// [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 &);
|
|
|
|
|
2018-05-24 20:01:52 -07:00
|
|
|
database(const string_view &name,
|
|
|
|
const uint64_t &checkpoint,
|
2018-01-18 17:59:22 -08:00
|
|
|
std::string options,
|
|
|
|
description);
|
2017-08-23 15:37:06 -06:00
|
|
|
|
2018-05-24 20:01:52 -07:00
|
|
|
database(const string_view &name,
|
|
|
|
std::string options,
|
|
|
|
description);
|
|
|
|
|
|
|
|
database(const string_view &name,
|
2018-01-18 17:59:22 -08:00
|
|
|
std::string options = {});
|
2017-03-30 15:57:08 -07:00
|
|
|
|
|
|
|
database() = default;
|
|
|
|
database(database &&) = delete;
|
|
|
|
database(const database &) = delete;
|
2018-01-18 17:59:22 -08:00
|
|
|
~database() noexcept;
|
2017-03-30 16:20:01 -07:00
|
|
|
|
2018-05-24 20:01:52 -07:00
|
|
|
// Find this instance by name (and checkpoint id) in the instance list
|
|
|
|
static database *get(std::nothrow_t, const string_view &name, const uint64_t &checkpoint);
|
|
|
|
static database *get(std::nothrow_t, const string_view &name); // optionally "name:checkpoint"
|
|
|
|
static database &get(const string_view &name, const uint64_t &checkpoint);
|
|
|
|
static database &get(const string_view &name); // optionally "name:checkpoint"
|
|
|
|
|
2017-08-23 15:37:06 -06:00
|
|
|
// Get this instance from any column.
|
2017-03-30 16:20:01 -07:00
|
|
|
static const database &get(const column &);
|
|
|
|
static database &get(column &);
|
2017-03-30 15:57:08 -07:00
|
|
|
};
|