0
0
Fork 0
mirror of https://github.com/matrix-construct/construct synced 2024-07-01 08:18:20 +02:00

ircd::db: Add json::tuple related and utils to row.

This commit is contained in:
Jason Volk 2017-08-30 14:22:19 -07:00
parent 0ca1ebba61
commit 69917f153f
2 changed files with 75 additions and 7 deletions

View file

@ -51,13 +51,14 @@ struct ircd::db::row
using pointer = cell *;
using difference_type = size_t;
private:
private: public:
std::vector<cell> its;
template<class pos> friend void seek(row &, const pos &);
friend void seek(row &, const string_view &key);
public:
auto empty() const { return its.empty(); }
auto size() const { return its.size(); }
bool valid() const; // true on any cell valid; false on empty
// [GET] Iterations
const_iterator begin() const;
const_iterator end() const;
@ -68,16 +69,20 @@ struct ircd::db::row
const_iterator find(const string_view &column) const;
iterator find(const string_view &column);
auto empty() const { return its.empty(); }
auto size() const { return its.size(); }
// [GET] Get cell
const cell &operator[](const string_view &column) const;
cell &operator[](const string_view &column);
// [GET] Object conversion
template<class tuple> explicit operator tuple() const;
template<class tuple> explicit operator tuple();
// [SET] Perform operation
void operator()(const op &, const string_view &col, const string_view &val = {}, const sopts & = {});
// All cells
void load(const gopts & = {}); // !DANGER! not atomic
row(std::vector<cell> cells = {})
:its{std::move(cells)}
{}
@ -87,6 +92,12 @@ struct ircd::db::row
const vector_view<string_view> &columns = {},
gopts opts = {});
template<class... T>
row(database &,
const string_view &key = {},
const json::tuple<T...> & = {},
gopts opts = {});
template<class pos> friend bool seeka(row &, const pos &);
template<class pos> friend bool seek(row &, const pos &);
friend size_t trim(row &, const std::function<bool (cell &)> &);
@ -180,6 +191,42 @@ struct ircd::db::row::delta
{}
};
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 };
}()}
{
}
template<class tuple>
ircd::db::row::operator tuple()
{
tuple ret;
for(auto &cell : *this)
json::set(ret, cell.col(), cell.val());
return ret;
}
template<class tuple>
ircd::db::row::operator tuple()
const
{
tuple ret;
for(const auto &cell : *this)
json::set(ret, cell.col(), cell.val());
return ret;
}
inline ircd::db::cell &
ircd::db::row::operator[](const string_view &column)
{

View file

@ -1733,6 +1733,27 @@ const
return ret;
}
bool
ircd::db::row::valid()
const
{
return std::any_of(std::begin(its), std::end(its), []
(const auto &cell)
{
return cell.valid();
});
}
void
ircd::db::row::load(const gopts &opts)
{
std::for_each(std::begin(its), std::end(its), [&opts]
(auto &cell)
{
cell.load(opts);
});
}
///////////////////////////////////////////////////////////////////////////////
//
// db/column.h