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_ROW_H
|
|
|
|
|
2017-08-28 23:51:22 +02:00
|
|
|
namespace ircd::db
|
|
|
|
{
|
|
|
|
struct row;
|
|
|
|
|
|
|
|
// [SET] Delete row from DB (convenience to an op::DELETE delta)
|
|
|
|
void del(row &, const sopts & = {});
|
|
|
|
}
|
2017-03-31 00:57:08 +02:00
|
|
|
|
2017-09-17 00:20:13 +02:00
|
|
|
/// A `row` is a collection of cells from different columns which all share the same
|
|
|
|
/// key. This is an interface for dealing with those cells in the aggregate.
|
|
|
|
///
|
|
|
|
/// Note that in a `row` each `cell` comes from a different `column`, but `cell::key()`
|
|
|
|
/// will all return the same index value across the whole `row`. To get the names
|
|
|
|
/// of the columns themselves to build ex. the key name of a JSON key-value pair,
|
|
|
|
/// use `cell::col()`, which will be different for each `cell` across the `row`.
|
|
|
|
///
|
|
|
|
/// The db::row::iterator iterates over the cells in a row; to iterate over
|
|
|
|
/// multiple rows use the db::cursor
|
2017-08-28 23:51:22 +02:00
|
|
|
struct ircd::db::row
|
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
|
|
|
|
2017-09-17 00:20:13 +02:00
|
|
|
using vector = std::vector<cell>;
|
|
|
|
using iterator = vector::iterator;
|
|
|
|
using const_iterator = vector::const_iterator;
|
|
|
|
using value_type = vector::value_type;
|
|
|
|
|
|
|
|
vector its;
|
2017-03-31 00:57:08 +02:00
|
|
|
|
|
|
|
public:
|
2017-08-30 23:22:19 +02:00
|
|
|
auto empty() const { return its.empty(); }
|
|
|
|
auto size() const { return its.size(); }
|
|
|
|
bool valid() const; // true on any cell valid; false on empty
|
2017-09-16 20:48:09 +02:00
|
|
|
bool valid(const string_view &) const; // true on any cell valid equal; false on empty
|
2017-08-30 23:22:19 +02:00
|
|
|
|
2017-04-03 06:02:32 +02:00
|
|
|
// [GET] Iterations
|
|
|
|
const_iterator begin() const;
|
|
|
|
const_iterator end() const;
|
|
|
|
iterator begin();
|
|
|
|
iterator end();
|
|
|
|
|
|
|
|
// [GET] Get iterator to cell
|
|
|
|
const_iterator find(const string_view &column) const;
|
|
|
|
iterator find(const string_view &column);
|
|
|
|
|
|
|
|
// [GET] Get cell
|
|
|
|
const cell &operator[](const string_view &column) const;
|
|
|
|
cell &operator[](const string_view &column);
|
|
|
|
|
|
|
|
// [SET] Perform operation
|
|
|
|
void operator()(const op &, const string_view &col, const string_view &val = {}, const sopts & = {});
|
|
|
|
|
2017-09-17 00:20:13 +02:00
|
|
|
row(std::vector<cell> its = {})
|
|
|
|
:its{std::move(its)}
|
2017-04-03 06:02:32 +02:00
|
|
|
{}
|
|
|
|
|
|
|
|
row(database &,
|
|
|
|
const string_view &key = {},
|
|
|
|
const vector_view<string_view> &columns = {},
|
2017-08-23 22:37:47 +02:00
|
|
|
gopts opts = {});
|
2017-04-03 06:02:32 +02:00
|
|
|
|
2017-08-30 23:22:19 +02:00
|
|
|
template<class... T>
|
|
|
|
row(database &,
|
|
|
|
const string_view &key = {},
|
|
|
|
const json::tuple<T...> & = {},
|
|
|
|
gopts opts = {});
|
|
|
|
|
2017-09-12 21:03:13 +02:00
|
|
|
template<class pos> friend size_t seek(row &, const pos &);
|
2017-04-03 06:02:32 +02:00
|
|
|
};
|
|
|
|
|
2017-08-28 23:51:22 +02:00
|
|
|
namespace ircd::db
|
|
|
|
{
|
|
|
|
// [SET] Perform operations in a sequence as a single transaction. No template
|
|
|
|
// iterators supported yet, just a ptr range good for contiguous sequences.
|
|
|
|
void write(const row::delta *const &begin, const row::delta *const &end, const sopts & = {});
|
|
|
|
void write(const std::initializer_list<row::delta> &, const sopts & = {});
|
|
|
|
void write(const sopts &, const std::initializer_list<row::delta> &);
|
|
|
|
void write(const row::delta &, const sopts & = {});
|
|
|
|
}
|
|
|
|
|
2017-08-23 22:37:47 +02:00
|
|
|
//
|
|
|
|
// A delta is an element of a database transaction. You can use this to make
|
|
|
|
// an all-succeed-or-all-fail commitment to multiple rows at once. It is also
|
|
|
|
// useful to make a commitment on a single row as a convenient way to compose
|
|
|
|
// all of a row's cells together.
|
|
|
|
//
|
2017-08-28 23:51:22 +02:00
|
|
|
struct ircd::db::row::delta
|
2017-08-29 03:44:27 +02:00
|
|
|
:std::tuple<op, row *>
|
2017-08-23 22:37:47 +02:00
|
|
|
{
|
2017-09-19 05:51:53 +02:00
|
|
|
enum
|
|
|
|
{
|
|
|
|
OP, ROW
|
|
|
|
};
|
|
|
|
|
2017-08-23 22:37:47 +02:00
|
|
|
delta(row &r, const enum op &op = op::SET)
|
2017-08-29 03:44:27 +02:00
|
|
|
:std::tuple<enum op, row *>{op, &r}
|
2017-08-23 22:37:47 +02:00
|
|
|
{}
|
|
|
|
|
|
|
|
delta(const enum op &op, row &r)
|
2017-08-29 03:44:27 +02:00
|
|
|
:std::tuple<enum op, row *>{op, &r}
|
2017-08-23 22:37:47 +02:00
|
|
|
{}
|
|
|
|
};
|
|
|
|
|
2017-08-30 23:22:19 +02:00
|
|
|
template<class... T>
|
|
|
|
ircd::db::row::row(database &d,
|
|
|
|
const string_view &key,
|
|
|
|
const json::tuple<T...> &t,
|
|
|
|
gopts opts)
|
|
|
|
:row{[&d, &key, &t, &opts]
|
|
|
|
() -> row
|
|
|
|
{
|
|
|
|
std::array<string_view, t.size()> cols;
|
|
|
|
json::_key_transform(t, std::begin(cols), std::end(cols));
|
|
|
|
return { d, key, cols, opts };
|
|
|
|
}()}
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2017-04-03 06:02:32 +02:00
|
|
|
inline ircd::db::cell &
|
|
|
|
ircd::db::row::operator[](const string_view &column)
|
|
|
|
{
|
|
|
|
const auto it(find(column));
|
|
|
|
if(unlikely(it == end()))
|
2017-08-19 23:31:46 +02:00
|
|
|
throw schema_error("column '%s' not specified in the descriptor schema", column);
|
2017-04-03 06:02:32 +02:00
|
|
|
|
|
|
|
return *it;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline const ircd::db::cell &
|
|
|
|
ircd::db::row::operator[](const string_view &column)
|
|
|
|
const
|
|
|
|
{
|
|
|
|
const auto it(find(column));
|
|
|
|
if(unlikely(it == end()))
|
2017-08-19 23:31:46 +02:00
|
|
|
throw schema_error("column '%s' not specified in the descriptor schema", column);
|
2017-04-03 06:02:32 +02:00
|
|
|
|
|
|
|
return *it;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline ircd::db::row::iterator
|
|
|
|
ircd::db::row::end()
|
|
|
|
{
|
2017-09-17 00:20:13 +02:00
|
|
|
return std::end(its);
|
2017-04-03 06:02:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
inline ircd::db::row::iterator
|
|
|
|
ircd::db::row::begin()
|
|
|
|
{
|
2017-09-17 00:20:13 +02:00
|
|
|
return std::begin(its);
|
2017-04-03 06:02:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
inline ircd::db::row::const_iterator
|
|
|
|
ircd::db::row::end()
|
|
|
|
const
|
|
|
|
{
|
2017-09-17 00:20:13 +02:00
|
|
|
return std::end(its);
|
2017-04-03 06:02:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
inline ircd::db::row::const_iterator
|
|
|
|
ircd::db::row::begin()
|
|
|
|
const
|
|
|
|
{
|
2017-09-17 00:20:13 +02:00
|
|
|
return std::begin(its);
|
2017-04-03 06:02:32 +02:00
|
|
|
}
|