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;
|
|
|
|
|
|
|
|
template<class R = uint64_t> R property(database &, const string_view &name);
|
|
|
|
template<> uint64_t property(database &, const string_view &name);
|
|
|
|
const std::string &name(const database &);
|
|
|
|
uint64_t sequence(const database &); // Latest sequence number
|
|
|
|
void sync(database &); // Sync the write log (all columns)
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
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;
|
2017-08-23 23:37:06 +02:00
|
|
|
custom_ptr<rocksdb::DB> d;
|
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
|
|
|
};
|