2017-03-31 00:57:08 +02:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2016 Charybdis Development Team
|
|
|
|
* Copyright (C) 2016 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
|
|
|
|
* copyright notice and this permission notice is present in all copies.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
|
|
|
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
|
|
|
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
|
|
* DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
|
|
|
|
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
|
|
|
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
|
|
|
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
|
|
|
|
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
|
|
|
|
* IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
* POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
#define HAVE_IRCD_DB_COLUMN_H
|
|
|
|
|
2017-08-23 23:37:06 +02:00
|
|
|
// Columns add the ability to run multiple LevelDB's in synchrony under the same
|
|
|
|
// database (directory). Each column is a fully distinct key/value store; they
|
|
|
|
// are merely joined for consistency and possible performance advantages for
|
|
|
|
// concurrent multi-column lookups of the same key.
|
2017-03-31 00:57:08 +02:00
|
|
|
//
|
2017-08-23 23:37:06 +02:00
|
|
|
// This class is a handle to the real column instance `database::column` because the
|
|
|
|
// real column instance has to have a lifetime congruent to the open database. But
|
|
|
|
// that makes this object easier to work with, pass around, and construct. It will
|
|
|
|
// find the real `database::column` at any time.
|
2017-03-31 01:20:01 +02:00
|
|
|
//
|
2017-08-23 23:37:06 +02:00
|
|
|
// [GET] If the data is not cached, your ircd::context will yield. Note that the
|
|
|
|
// request may be posted to a separate thread which incurs the time of IO. This is
|
|
|
|
// because RocksDB has minimalist origins and is not yet asynchronous.
|
|
|
|
//
|
|
|
|
// + In the future, your ircd::context will still yield but the internals here will
|
|
|
|
// interleave pending contexts. If RocksDB is clever enough to expose actual file
|
|
|
|
// descriptors or something we can interleave on, similar to the pgsql API, we can
|
|
|
|
// remove the IO/offload thread as well.
|
|
|
|
//
|
|
|
|
// [SET] usually occur without yielding your context because the DB is oriented
|
2017-08-18 20:14:22 +02:00
|
|
|
// around write-log appends. It deals with the heavier tasks later in background.
|
|
|
|
//
|
|
|
|
// NOTE that the column and cell structs are type-agnostic. The database is capable of
|
|
|
|
// storing binary data in the key or the value for a cell. The string_view will work
|
|
|
|
// with both a normal string and binary data, so this class is not a template and
|
|
|
|
// offers no conversions at this level. see: value.h/object.h
|
2017-03-31 01:20:01 +02:00
|
|
|
//
|
2017-08-28 23:51:22 +02:00
|
|
|
namespace ircd::db
|
|
|
|
{
|
|
|
|
struct column;
|
|
|
|
|
|
|
|
// Get property data of a db column. R can optionally be uint64_t for some
|
|
|
|
// values. Refer to RocksDB documentation for more info.
|
|
|
|
template<class R = std::string> R property(column &, const string_view &name);
|
|
|
|
template<> std::string property(column &, const string_view &name);
|
|
|
|
template<> uint64_t property(column &, const string_view &name);
|
|
|
|
|
|
|
|
// Information about a column
|
|
|
|
const std::string &name(const column &);
|
|
|
|
size_t file_count(column &);
|
|
|
|
size_t bytes(column &);
|
|
|
|
|
|
|
|
// [GET] Tests if key exists
|
|
|
|
bool has(column &, const string_view &key, const gopts & = {});
|
|
|
|
|
|
|
|
// [GET] Convenience functions to copy data into your buffer.
|
|
|
|
// The signed char buffer is null terminated; the unsigned is not.
|
|
|
|
size_t read(column &, const string_view &key, uint8_t *const &buf, const size_t &max, const gopts & = {});
|
|
|
|
string_view read(column &, const string_view &key, char *const &buf, const size_t &max, const gopts & = {});
|
|
|
|
std::string read(column &, const string_view &key, const gopts & = {});
|
|
|
|
|
|
|
|
// [SET] Write data to the db
|
|
|
|
void write(column &, const string_view &key, const string_view &value, const sopts & = {});
|
|
|
|
void write(column &, const string_view &key, const uint8_t *const &buf, const size_t &size, const sopts & = {});
|
|
|
|
|
|
|
|
// [SET] Remove data from the db. not_found is never thrown.
|
|
|
|
void del(column &, const string_view &key, const sopts & = {});
|
|
|
|
|
|
|
|
// [SET] Flush memory tables to disk (this column only).
|
|
|
|
void flush(column &, const bool &blocking = false);
|
|
|
|
}
|
|
|
|
|
|
|
|
struct ircd::db::column
|
2017-03-31 00:57:08 +02:00
|
|
|
{
|
2017-04-03 06:02:32 +02:00
|
|
|
struct delta;
|
2017-03-31 00:57:08 +02:00
|
|
|
struct const_iterator;
|
|
|
|
using key_type = string_view;
|
|
|
|
using mapped_type = string_view;
|
|
|
|
using value_type = std::pair<key_type, mapped_type>;
|
|
|
|
using pointer = value_type *;
|
|
|
|
using reference = value_type &;
|
|
|
|
using difference_type = size_t;
|
|
|
|
using iterator = const_iterator;
|
|
|
|
|
|
|
|
protected:
|
2017-08-31 07:13:50 +02:00
|
|
|
database::column *c;
|
2017-03-31 00:57:08 +02:00
|
|
|
|
|
|
|
public:
|
2017-04-03 06:02:32 +02:00
|
|
|
explicit operator const database &() const;
|
|
|
|
explicit operator const database::column &() const;
|
2017-09-08 10:33:41 +02:00
|
|
|
explicit operator const database::descriptor &() const;
|
2017-03-31 00:57:08 +02:00
|
|
|
|
2017-04-03 06:02:32 +02:00
|
|
|
explicit operator database &();
|
|
|
|
explicit operator database::column &();
|
2017-03-31 00:57:08 +02:00
|
|
|
|
2017-03-31 01:20:01 +02:00
|
|
|
operator bool() const { return bool(c); }
|
|
|
|
bool operator!() const { return !c; }
|
2017-03-31 00:57:08 +02:00
|
|
|
|
|
|
|
// [GET] Iterations
|
|
|
|
const_iterator cbegin(const gopts & = {});
|
|
|
|
const_iterator cend(const gopts & = {});
|
|
|
|
const_iterator begin(const gopts & = {});
|
|
|
|
const_iterator end(const gopts & = {});
|
|
|
|
const_iterator find(const string_view &key, const gopts & = {});
|
|
|
|
const_iterator lower_bound(const string_view &key, const gopts & = {});
|
|
|
|
const_iterator upper_bound(const string_view &key, const gopts & = {});
|
|
|
|
|
2017-04-03 06:02:32 +02:00
|
|
|
// [GET] Get cell
|
|
|
|
cell operator[](const string_view &key) const;
|
|
|
|
|
2017-03-31 00:57:08 +02:00
|
|
|
// [GET] Perform a get into a closure. This offers a reference to the data with zero-copy.
|
2017-04-03 06:02:32 +02:00
|
|
|
using view_closure = std::function<void (const string_view &)>;
|
2017-03-31 00:57:08 +02:00
|
|
|
void operator()(const string_view &key, const view_closure &func, const gopts & = {});
|
|
|
|
void operator()(const string_view &key, const gopts &, const view_closure &func);
|
|
|
|
|
2017-08-23 22:37:47 +02:00
|
|
|
// [SET] Perform operations in a sequence as a single transaction. No template iterators
|
|
|
|
// supported yet, just a ContiguousContainer iteration (and derived convenience overloads)
|
|
|
|
void operator()(const delta *const &begin, const delta *const &end, const sopts & = {});
|
2017-03-31 00:57:08 +02:00
|
|
|
void operator()(const std::initializer_list<delta> &, const sopts & = {});
|
2017-04-03 06:02:32 +02:00
|
|
|
void operator()(const sopts &, const std::initializer_list<delta> &);
|
2017-08-23 22:37:47 +02:00
|
|
|
void operator()(const delta &, const sopts & = {});
|
2017-03-31 00:57:08 +02:00
|
|
|
|
2017-04-03 06:02:32 +02:00
|
|
|
column(database::column &c);
|
2017-03-31 00:57:08 +02:00
|
|
|
column(database &, const string_view &column);
|
2017-04-03 06:02:32 +02:00
|
|
|
column() = default;
|
|
|
|
};
|
|
|
|
|
2017-08-18 20:14:22 +02:00
|
|
|
//
|
|
|
|
// Delta is an element of a transaction. Use column::delta's to atomically
|
|
|
|
// commit to multiple keys in the same column. Refer to delta.h for the `enum op`
|
2017-08-23 22:37:47 +02:00
|
|
|
// choices. Refer to cell::delta to transact with multiple cells across different
|
|
|
|
// columns. Refer to row::delta to transact with entire rows.
|
|
|
|
//
|
|
|
|
// Note, for now, unlike cell::delta and row::delta, the column::delta has
|
|
|
|
// no reference to the column in its tuple. This is why these deltas are executed
|
|
|
|
// through the member column::operator() and not an overload of db::write().
|
|
|
|
//
|
|
|
|
// It is unlikely you will need to work with column deltas directly because
|
|
|
|
// you may decohere one column from the others participating in a row.
|
2017-08-18 20:14:22 +02:00
|
|
|
//
|
2017-08-28 23:51:22 +02:00
|
|
|
struct ircd::db::column::delta
|
2017-04-03 06:02:32 +02:00
|
|
|
:std::tuple<op, string_view, string_view>
|
|
|
|
{
|
2017-08-23 22:37:47 +02:00
|
|
|
delta(const string_view &key, const string_view &val, const enum op &op = op::SET)
|
2017-04-03 06:02:32 +02:00
|
|
|
:std::tuple<enum op, string_view, string_view>{op, key, val}
|
2017-03-31 01:20:01 +02:00
|
|
|
{}
|
|
|
|
|
2017-08-23 22:37:47 +02:00
|
|
|
delta(const enum op &op, const string_view &key, const string_view &val = {})
|
2017-04-03 06:02:32 +02:00
|
|
|
:std::tuple<enum op, string_view, string_view>{op, key, val}
|
|
|
|
{}
|
|
|
|
};
|
|
|
|
|
2017-08-18 20:14:22 +02:00
|
|
|
//
|
|
|
|
// Iteration over all keys down a column. Default construction is an invalid
|
|
|
|
// iterator, which could be compared against in the style of STL algorithms.
|
|
|
|
// Otherwise, construct an iterator by having it returned from the appropriate
|
|
|
|
// function in column::.
|
|
|
|
//
|
2017-08-28 23:51:22 +02:00
|
|
|
struct ircd::db::column::const_iterator
|
2017-04-03 06:02:32 +02:00
|
|
|
{
|
|
|
|
using value_type = column::value_type;
|
|
|
|
using iterator_category = std::bidirectional_iterator_tag;
|
|
|
|
|
2017-09-08 10:33:41 +02:00
|
|
|
protected:
|
2017-08-31 07:13:50 +02:00
|
|
|
database::column *c;
|
2017-09-08 10:33:41 +02:00
|
|
|
database::snapshot ss;
|
2017-04-03 06:02:32 +02:00
|
|
|
std::unique_ptr<rocksdb::Iterator> it;
|
|
|
|
mutable value_type val;
|
2017-09-08 10:33:41 +02:00
|
|
|
bool all_prefix;
|
2017-04-03 06:02:32 +02:00
|
|
|
|
|
|
|
friend class column;
|
2017-09-08 10:33:41 +02:00
|
|
|
const_iterator(database::column *const &, std::unique_ptr<rocksdb::Iterator> &&, const gopts & = {});
|
2017-04-03 06:02:32 +02:00
|
|
|
|
|
|
|
public:
|
2017-08-23 22:37:47 +02:00
|
|
|
explicit operator const database::snapshot &() const;
|
|
|
|
explicit operator const database::column &() const;
|
2017-04-03 06:02:32 +02:00
|
|
|
|
2017-08-23 22:37:47 +02:00
|
|
|
explicit operator database::snapshot &();
|
|
|
|
explicit operator database::column &();
|
2017-04-03 06:02:32 +02:00
|
|
|
|
|
|
|
operator bool() const;
|
|
|
|
bool operator!() const;
|
|
|
|
|
|
|
|
const value_type *operator->() const;
|
|
|
|
const value_type &operator*() const;
|
|
|
|
|
|
|
|
const_iterator &operator++();
|
|
|
|
const_iterator &operator--();
|
|
|
|
|
|
|
|
const_iterator();
|
|
|
|
const_iterator(const_iterator &&) noexcept;
|
|
|
|
const_iterator &operator=(const_iterator &&) noexcept;
|
|
|
|
~const_iterator() noexcept;
|
|
|
|
|
|
|
|
friend bool operator==(const const_iterator &, const const_iterator &);
|
|
|
|
friend bool operator!=(const const_iterator &, const const_iterator &);
|
|
|
|
friend bool operator<(const const_iterator &, const const_iterator &);
|
|
|
|
friend bool operator>(const const_iterator &, const const_iterator &);
|
|
|
|
|
2017-08-30 23:05:15 +02:00
|
|
|
template<class pos> friend bool seek(column::const_iterator &, const pos &);
|
2017-03-31 00:57:08 +02:00
|
|
|
};
|
|
|
|
|
2017-08-23 22:37:47 +02:00
|
|
|
inline ircd::db::column::const_iterator::operator
|
|
|
|
database::column &()
|
|
|
|
{
|
|
|
|
return *c;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline ircd::db::column::const_iterator::operator
|
|
|
|
database::snapshot &()
|
|
|
|
{
|
2017-09-08 10:33:41 +02:00
|
|
|
return ss;
|
2017-08-23 22:37:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
inline ircd::db::column::const_iterator::operator
|
|
|
|
const database::column &()
|
|
|
|
const
|
|
|
|
{
|
|
|
|
return *c;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline ircd::db::column::const_iterator::operator
|
|
|
|
const database::snapshot &()
|
|
|
|
const
|
|
|
|
{
|
2017-09-08 10:33:41 +02:00
|
|
|
return ss;
|
2017-08-23 22:37:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
inline ircd::db::column::operator
|
|
|
|
database::column &()
|
2017-03-31 01:20:01 +02:00
|
|
|
{
|
2017-04-03 06:02:32 +02:00
|
|
|
return *c;
|
2017-03-31 01:20:01 +02:00
|
|
|
}
|
2017-04-03 06:02:32 +02:00
|
|
|
|
2017-08-23 22:37:47 +02:00
|
|
|
inline ircd::db::column::operator
|
|
|
|
database &()
|
2017-03-31 01:20:01 +02:00
|
|
|
{
|
2017-04-03 06:02:32 +02:00
|
|
|
return database::get(*c);
|
2017-03-31 01:20:01 +02:00
|
|
|
}
|
2017-04-03 06:02:32 +02:00
|
|
|
|
2017-08-23 22:37:47 +02:00
|
|
|
inline ircd::db::column::operator
|
|
|
|
const database::column &()
|
2017-04-03 06:02:32 +02:00
|
|
|
const
|
|
|
|
{
|
|
|
|
return *c;
|
|
|
|
}
|
|
|
|
|
2017-08-23 22:37:47 +02:00
|
|
|
inline ircd::db::column::operator
|
|
|
|
const database &()
|
2017-04-03 06:02:32 +02:00
|
|
|
const
|
2017-03-31 01:20:01 +02:00
|
|
|
{
|
2017-04-03 06:02:32 +02:00
|
|
|
return database::get(*c);
|
2017-03-31 01:20:01 +02:00
|
|
|
}
|