2018-01-17 18:14:13 -08:00
|
|
|
// Matrix Construct
|
|
|
|
//
|
|
|
|
// Copyright (C) Matrix Construct Developers, Authors & Contributors
|
|
|
|
// Copyright (C) 2016-2018 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
|
2018-02-03 18:22:01 -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.
|
2017-03-30 15:57:08 -07:00
|
|
|
|
|
|
|
#pragma once
|
|
|
|
#define HAVE_IRCD_DB_ROW_H
|
|
|
|
|
2017-08-28 14:51:22 -07:00
|
|
|
namespace ircd::db
|
|
|
|
{
|
|
|
|
struct row;
|
|
|
|
|
2019-01-24 10:53:07 -08:00
|
|
|
// [GET] Seek all cells to key
|
|
|
|
size_t seek(row &, const string_view &, const gopts &opts = {});
|
|
|
|
|
2017-08-28 14:51:22 -07:00
|
|
|
// [SET] Delete row from DB (convenience to an op::DELETE delta)
|
|
|
|
void del(row &, const sopts & = {});
|
|
|
|
}
|
2017-03-30 15:57:08 -07:00
|
|
|
|
2017-09-16 15:20:13 -07: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 14:51:22 -07:00
|
|
|
struct ircd::db::row
|
2017-09-28 21:19:02 -07:00
|
|
|
:vector_view<cell>
|
2017-03-30 15:57:08 -07:00
|
|
|
{
|
2017-04-02 21:02:32 -07:00
|
|
|
struct delta;
|
2017-03-30 15:57:08 -07:00
|
|
|
|
|
|
|
public:
|
2017-08-30 14:22:19 -07:00
|
|
|
bool valid() const; // true on any cell valid; false on empty
|
2020-06-07 03:26:31 -07:00
|
|
|
bool valid_all() const; // true on all cell valid; false_ on empty
|
2017-09-16 11:48:09 -07:00
|
|
|
bool valid(const string_view &) const; // true on any cell valid equal; false on empty
|
2020-06-07 03:26:31 -07:00
|
|
|
bool valid_all(const string_view &) const; // true on all cell valid equal; false on empty
|
2017-08-30 14:22:19 -07:00
|
|
|
|
2018-12-26 19:45:04 -08:00
|
|
|
bool cached() const; // true on all cell cached
|
|
|
|
bool cached(const string_view &) const; // true on all columns cached key
|
|
|
|
|
2017-09-21 17:00:48 -07:00
|
|
|
// [GET] Find cell by name
|
2017-04-02 21:02:32 -07:00
|
|
|
const_iterator find(const string_view &column) const;
|
|
|
|
iterator find(const string_view &column);
|
|
|
|
|
2017-09-21 17:00:48 -07:00
|
|
|
// [GET] Get cell (or throw)
|
|
|
|
const cell &operator[](const size_t &column) const;
|
2017-04-02 21:02:32 -07:00
|
|
|
const cell &operator[](const string_view &column) const;
|
2019-01-24 10:53:07 -08:00
|
|
|
|
2017-09-21 17:00:48 -07:00
|
|
|
cell &operator[](const size_t &column);
|
2017-04-02 21:02:32 -07:00
|
|
|
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-28 21:19:02 -07:00
|
|
|
using vector_view<cell>::vector_view;
|
2017-04-02 21:02:32 -07:00
|
|
|
|
|
|
|
row(database &,
|
|
|
|
const string_view &key = {},
|
2018-05-12 18:26:49 -07:00
|
|
|
const vector_view<const string_view> &columns = {},
|
2017-09-28 21:19:02 -07:00
|
|
|
const vector_view<cell> &buf = {},
|
2019-05-07 23:05:18 -07:00
|
|
|
gopts opts = {});
|
2017-04-02 21:02:32 -07:00
|
|
|
};
|
|
|
|
|
2017-08-28 14:51:22 -07: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 14:37:47 -06: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 14:51:22 -07:00
|
|
|
struct ircd::db::row::delta
|
2017-08-28 18:44:27 -07:00
|
|
|
:std::tuple<op, row *>
|
2017-08-23 14:37:47 -06:00
|
|
|
{
|
2017-09-18 20:51:53 -07:00
|
|
|
enum
|
|
|
|
{
|
|
|
|
OP, ROW
|
|
|
|
};
|
|
|
|
|
2017-08-23 14:37:47 -06:00
|
|
|
delta(row &r, const enum op &op = op::SET)
|
2017-08-28 18:44:27 -07:00
|
|
|
:std::tuple<enum op, row *>{op, &r}
|
2017-08-23 14:37:47 -06:00
|
|
|
{}
|
|
|
|
|
|
|
|
delta(const enum op &op, row &r)
|
2017-08-28 18:44:27 -07:00
|
|
|
:std::tuple<enum op, row *>{op, &r}
|
2017-08-23 14:37:47 -06:00
|
|
|
{}
|
|
|
|
};
|
|
|
|
|
2017-04-02 21:02:32 -07:00
|
|
|
inline ircd::db::cell &
|
2017-09-21 17:00:48 -07:00
|
|
|
ircd::db::row::operator[](const size_t &i)
|
2017-04-02 21:02:32 -07:00
|
|
|
{
|
2017-09-28 21:19:02 -07:00
|
|
|
return vector_view<cell>::at(i);
|
2017-04-02 21:02:32 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
inline const ircd::db::cell &
|
2017-09-21 17:00:48 -07:00
|
|
|
ircd::db::row::operator[](const size_t &i)
|
2017-04-02 21:02:32 -07:00
|
|
|
const
|
|
|
|
{
|
2017-09-28 21:19:02 -07:00
|
|
|
return vector_view<cell>::at(i);
|
2017-04-02 21:02:32 -07:00
|
|
|
}
|