2018-01-18 03:14:13 +01:00
|
|
|
// Matrix Construct
|
2017-08-23 23:37:06 +02:00
|
|
|
//
|
2018-01-18 03:14:13 +01:00
|
|
|
// Copyright (C) Matrix Construct Developers, Authors & Contributors
|
|
|
|
// Copyright (C) 2016-2018 Jason Volk <jason@zemos.net>
|
2017-08-23 23:37:06 +02:00
|
|
|
//
|
2018-01-18 03:14:13 +01: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-19 02:59:22 +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.
|
2018-01-18 03:14:13 +01:00
|
|
|
|
|
|
|
#pragma once
|
|
|
|
#define HAVE_IRCD_DB_DATABASE_H
|
|
|
|
|
2017-08-28 23:51:22 +02:00
|
|
|
namespace ircd::db
|
|
|
|
{
|
|
|
|
struct database;
|
|
|
|
|
2018-04-03 19:58:16 +02:00
|
|
|
// General information
|
2017-08-28 23:51:22 +02:00
|
|
|
const std::string &name(const database &);
|
2018-03-23 03:35:50 +01:00
|
|
|
uint64_t sequence(const database &); // Latest sequence number
|
2018-04-03 20:44:57 +02:00
|
|
|
std::vector<std::string> files(const database &, uint64_t &msz);
|
|
|
|
std::vector<std::string> files(const database &);
|
2018-04-17 23:13:36 +02:00
|
|
|
size_t file_count(const database &);
|
|
|
|
size_t bytes(const database &);
|
2018-03-23 03:35:50 +01:00
|
|
|
|
2018-04-03 19:58:16 +02:00
|
|
|
// Property information interface
|
|
|
|
using prop_int = uint64_t;
|
|
|
|
using prop_str = std::string;
|
2018-04-09 21:55:22 +02:00
|
|
|
using prop_map = std::map<std::string, std::string>;
|
2018-04-03 19:58:16 +02: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-04-15 13:45:31 +02:00
|
|
|
// Ticker
|
|
|
|
extern const uint32_t ticker_max;
|
|
|
|
string_view ticker_id(const uint32_t &id);
|
|
|
|
uint32_t ticker_id(const string_view &key);
|
|
|
|
uint64_t ticker(const database &, const uint32_t &id);
|
|
|
|
uint64_t ticker(const database &, const string_view &key);
|
|
|
|
|
|
|
|
// Histogram
|
|
|
|
extern const uint32_t histogram_max;
|
|
|
|
string_view histogram_id(const uint32_t &id);
|
|
|
|
uint32_t histogram_id(const string_view &key);
|
|
|
|
|
2018-04-03 19:58:16 +02:00
|
|
|
// Control panel
|
2018-04-20 23:10:59 +02:00
|
|
|
void setopt(database &, const string_view &key, const string_view &val);
|
2018-04-15 11:42:57 +02:00
|
|
|
void compact(database &);
|
2018-04-20 22:38:49 +02:00
|
|
|
void fdeletions(database &, const bool &enable, const bool &force = false);
|
2018-03-23 03:56:08 +01:00
|
|
|
void checkpoint(database &, const string_view &dir);
|
2018-03-23 03:41:50 +01:00
|
|
|
void flush(database &, const bool &blocking = true);
|
2018-03-23 03:35:50 +01:00
|
|
|
void sync(database &);
|
2017-08-28 23:51:22 +02:00
|
|
|
}
|
|
|
|
|
2018-01-18 03:14:13 +01: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.
|
|
|
|
///
|
2017-08-28 23:51:22 +02:00
|
|
|
struct ircd::db::database
|
2017-08-30 23:21:18 +02:00
|
|
|
:std::enable_shared_from_this<database>
|
2017-03-31 00:57:08 +02:00
|
|
|
{
|
2017-08-23 23:37:06 +02:00
|
|
|
struct descriptor;
|
2017-03-31 00:57:08 +02:00
|
|
|
struct options;
|
2017-08-23 23:37:06 +02:00
|
|
|
struct events;
|
|
|
|
struct stats;
|
|
|
|
struct logs;
|
|
|
|
struct mergeop;
|
2017-03-31 00:57:08 +02:00
|
|
|
struct snapshot;
|
|
|
|
struct comparator;
|
2017-08-31 07:12:58 +02:00
|
|
|
struct prefix_transform;
|
2017-03-31 00:57:08 +02:00
|
|
|
struct column;
|
2018-01-18 03:34:20 +01:00
|
|
|
struct env;
|
2017-11-16 02:29:54 +01:00
|
|
|
|
2017-09-19 05:53:45 +02:00
|
|
|
using description = std::vector<descriptor>;
|
2017-08-23 23:37:06 +02:00
|
|
|
|
|
|
|
// central collection of open databases for iteration (non-owning)
|
|
|
|
static std::map<string_view, database *> dbs;
|
|
|
|
|
|
|
|
std::string name;
|
|
|
|
std::string path;
|
2017-09-19 04:17:36 +02:00
|
|
|
std::string optstr;
|
2018-01-18 04:22:35 +01:00
|
|
|
std::shared_ptr<struct env> env;
|
2017-08-23 23:37:06 +02:00
|
|
|
std::shared_ptr<struct logs> logs;
|
|
|
|
std::shared_ptr<struct stats> stats;
|
|
|
|
std::shared_ptr<struct events> events;
|
|
|
|
std::shared_ptr<struct mergeop> mergeop;
|
2018-04-21 00:45:57 +02:00
|
|
|
std::shared_ptr<rocksdb::SstFileManager> ssts;
|
2017-08-23 23:37:06 +02:00
|
|
|
std::shared_ptr<rocksdb::Cache> cache;
|
2017-11-16 02:29:54 +01:00
|
|
|
std::vector<descriptor> descriptors;
|
|
|
|
std::vector<string_view> column_names;
|
|
|
|
std::unordered_map<string_view, size_t> column_index;
|
2017-09-19 04:17:36 +02:00
|
|
|
std::vector<std::shared_ptr<column>> columns;
|
2018-04-21 01:22:01 +02:00
|
|
|
std::unique_ptr<rocksdb::DB> d;
|
2018-03-23 03:56:08 +01:00
|
|
|
std::unique_ptr<rocksdb::Checkpoint> checkpoint;
|
2017-08-23 22:37:47 +02:00
|
|
|
unique_const_iterator<decltype(dbs)> dbs_it;
|
2017-04-03 06:02:32 +02:00
|
|
|
|
|
|
|
operator std::shared_ptr<database>() { return shared_from_this(); }
|
2017-03-31 00:57:08 +02:00
|
|
|
operator const rocksdb::DB &() const { return *d; }
|
|
|
|
operator rocksdb::DB &() { return *d; }
|
|
|
|
|
2017-09-19 04:17:36 +02: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-31 00:57:08 +02:00
|
|
|
|
2017-08-30 23:07:36 +02: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-01-19 02:59:22 +01:00
|
|
|
database(std::string name,
|
|
|
|
std::string options,
|
|
|
|
description);
|
2017-08-23 23:37:06 +02:00
|
|
|
|
2018-01-19 02:59:22 +01:00
|
|
|
database(std::string name,
|
|
|
|
std::string options = {});
|
2017-03-31 00:57:08 +02:00
|
|
|
|
|
|
|
database() = default;
|
|
|
|
database(database &&) = delete;
|
|
|
|
database(const database &) = delete;
|
2018-01-19 02:59:22 +01:00
|
|
|
~database() noexcept;
|
2017-03-31 01:20:01 +02:00
|
|
|
|
2017-08-23 23:37:06 +02:00
|
|
|
// Get this instance from any column.
|
2017-03-31 01:20:01 +02:00
|
|
|
static const database &get(const column &);
|
|
|
|
static database &get(column &);
|
2017-03-31 00:57:08 +02:00
|
|
|
};
|