mirror of
https://github.com/matrix-construct/construct
synced 2025-02-18 17:50:16 +01:00
ircd::db: Simplify the db::row using vector_view<cell>.
This commit is contained in:
parent
c47be3008a
commit
ade7a138c4
3 changed files with 23 additions and 63 deletions
|
@ -68,6 +68,7 @@ struct ircd::db::cursor<d, tuple>::const_iterator_base
|
|||
|
||||
const query_type<> *query{nullptr};
|
||||
index_iterator idx;
|
||||
std::array<db::cell, tuple::size()> cell;
|
||||
db::row row;
|
||||
mutable tuple v;
|
||||
mutable bool stale{true};
|
||||
|
@ -172,6 +173,7 @@ ircd::db::cursor<d, tuple>::const_iterator_base<index_iterator>::const_iterator_
|
|||
const gopts &opts)
|
||||
:query{c.query}
|
||||
,idx{std::move(idx)}
|
||||
,cell{}
|
||||
,row
|
||||
{
|
||||
*d,
|
||||
|
@ -179,6 +181,7 @@ ircd::db::cursor<d, tuple>::const_iterator_base<index_iterator>::const_iterator_
|
|||
bool(this->idx)? this->idx->first:
|
||||
string_view{},
|
||||
tuple{},
|
||||
cell,
|
||||
opts
|
||||
}
|
||||
,stale
|
||||
|
|
|
@ -42,28 +42,14 @@ namespace ircd::db
|
|||
/// The db::row::iterator iterates over the cells in a row; to iterate over
|
||||
/// multiple rows use the db::cursor
|
||||
struct ircd::db::row
|
||||
:vector_view<cell>
|
||||
{
|
||||
struct delta;
|
||||
|
||||
using vector = std::vector<cell>;
|
||||
using iterator = vector::iterator;
|
||||
using const_iterator = vector::const_iterator;
|
||||
using value_type = vector::value_type;
|
||||
|
||||
vector its;
|
||||
|
||||
public:
|
||||
auto empty() const { return its.empty(); }
|
||||
auto size() const { return its.size(); }
|
||||
bool valid() const; // true on any cell valid; false on empty
|
||||
bool valid(const string_view &) const; // true on any cell valid equal; false on empty
|
||||
|
||||
// [GET] Iterations
|
||||
const_iterator begin() const;
|
||||
const_iterator end() const;
|
||||
iterator begin();
|
||||
iterator end();
|
||||
|
||||
// [GET] Find cell by name
|
||||
const_iterator find(const string_view &column) const;
|
||||
iterator find(const string_view &column);
|
||||
|
@ -77,19 +63,19 @@ struct ircd::db::row
|
|||
// [SET] Perform operation
|
||||
void operator()(const op &, const string_view &col, const string_view &val = {}, const sopts & = {});
|
||||
|
||||
row(std::vector<cell> its = {})
|
||||
:its{std::move(its)}
|
||||
{}
|
||||
using vector_view<cell>::vector_view;
|
||||
|
||||
row(database &,
|
||||
const string_view &key = {},
|
||||
const vector_view<string_view> &columns = {},
|
||||
const vector_view<cell> &buf = {},
|
||||
gopts opts = {});
|
||||
|
||||
template<class... T>
|
||||
row(database &,
|
||||
const string_view &key = {},
|
||||
const json::tuple<T...> & = {},
|
||||
const vector_view<cell> &buf = {},
|
||||
gopts opts = {});
|
||||
|
||||
template<class pos> friend size_t seek(row &, const pos &);
|
||||
|
@ -132,13 +118,14 @@ template<class... T>
|
|||
ircd::db::row::row(database &d,
|
||||
const string_view &key,
|
||||
const json::tuple<T...> &t,
|
||||
const vector_view<cell> &buf,
|
||||
gopts opts)
|
||||
:row{[&d, &key, &t, &opts]
|
||||
:row{[&d, &key, &t, &buf, &opts]
|
||||
() -> row
|
||||
{
|
||||
std::array<string_view, t.size()> cols;
|
||||
json::_key_transform(t, std::begin(cols), std::end(cols));
|
||||
return { d, key, cols, opts };
|
||||
return { d, key, cols, buf, opts };
|
||||
}()}
|
||||
{
|
||||
}
|
||||
|
@ -146,38 +133,12 @@ ircd::db::row::row(database &d,
|
|||
inline ircd::db::cell &
|
||||
ircd::db::row::operator[](const size_t &i)
|
||||
{
|
||||
return its.at(i);
|
||||
return vector_view<cell>::at(i);
|
||||
}
|
||||
|
||||
inline const ircd::db::cell &
|
||||
ircd::db::row::operator[](const size_t &i)
|
||||
const
|
||||
{
|
||||
return its.at(i);
|
||||
}
|
||||
|
||||
inline ircd::db::row::iterator
|
||||
ircd::db::row::end()
|
||||
{
|
||||
return std::end(its);
|
||||
}
|
||||
|
||||
inline ircd::db::row::iterator
|
||||
ircd::db::row::begin()
|
||||
{
|
||||
return std::begin(its);
|
||||
}
|
||||
|
||||
inline ircd::db::row::const_iterator
|
||||
ircd::db::row::end()
|
||||
const
|
||||
{
|
||||
return std::end(its);
|
||||
}
|
||||
|
||||
inline ircd::db::row::const_iterator
|
||||
ircd::db::row::begin()
|
||||
const
|
||||
{
|
||||
return std::begin(its);
|
||||
return vector_view<cell>::at(i);
|
||||
}
|
||||
|
|
26
ircd/db.cc
26
ircd/db.cc
|
@ -2319,13 +2319,13 @@ size_t
|
|||
ircd::db::seek(row &r,
|
||||
const pos &p)
|
||||
{
|
||||
assert(!r.its.empty());
|
||||
assert(!r.empty());
|
||||
const column &c(r[0]);
|
||||
const database &d(c);
|
||||
const ircd::timer timer;
|
||||
const auto ret
|
||||
{
|
||||
std::count_if(begin(r.its), end(r.its), [&p]
|
||||
std::count_if(begin(r), end(r), [&p]
|
||||
(auto &cell)
|
||||
{
|
||||
return seek(cell, p);
|
||||
|
@ -2338,7 +2338,7 @@ ircd::db::seek(row &r,
|
|||
sequence(r[0]),
|
||||
name(c),
|
||||
ret,
|
||||
r.its.size(),
|
||||
r.size(),
|
||||
timer.at<microseconds>().count());
|
||||
|
||||
return ret;
|
||||
|
@ -2353,8 +2353,9 @@ template size_t ircd::db::seek<ircd::string_view>(row &, const string_view &);
|
|||
ircd::db::row::row(database &d,
|
||||
const string_view &key,
|
||||
const vector_view<string_view> &colnames,
|
||||
const vector_view<cell> &buf,
|
||||
gopts opts)
|
||||
:its{[this, &d, &key, &colnames, &opts]
|
||||
:vector_view<cell>{buf}
|
||||
{
|
||||
using std::end;
|
||||
using std::begin;
|
||||
|
@ -2403,16 +2404,11 @@ ircd::db::row::row(database &d,
|
|||
d.d->NewIterators(options, handles, &iterators)
|
||||
};
|
||||
|
||||
std::vector<cell> ret(iterators.size());
|
||||
for(size_t i(0); i < ret.size(); ++i)
|
||||
for(size_t i(0); i < this->size(); ++i)
|
||||
{
|
||||
std::unique_ptr<Iterator> it(iterators.at(i));
|
||||
ret[i] = cell { *colptr.at(i), key, std::move(it), opts };
|
||||
(*this)[i] = cell { *colptr.at(i), key, std::move(it), opts };
|
||||
}
|
||||
|
||||
return ret;
|
||||
}()}
|
||||
{
|
||||
}
|
||||
|
||||
void
|
||||
|
@ -2448,7 +2444,7 @@ const
|
|||
ircd::db::row::iterator
|
||||
ircd::db::row::find(const string_view &col)
|
||||
{
|
||||
return std::find_if(std::begin(its), std::end(its), [&col]
|
||||
return std::find_if(std::begin(*this), std::end(*this), [&col]
|
||||
(const auto &cell)
|
||||
{
|
||||
return name(cell.c) == col;
|
||||
|
@ -2459,7 +2455,7 @@ ircd::db::row::const_iterator
|
|||
ircd::db::row::find(const string_view &col)
|
||||
const
|
||||
{
|
||||
return std::find_if(std::begin(its), std::end(its), [&col]
|
||||
return std::find_if(std::begin(*this), std::end(*this), [&col]
|
||||
(const auto &cell)
|
||||
{
|
||||
return name(cell.c) == col;
|
||||
|
@ -2470,7 +2466,7 @@ bool
|
|||
ircd::db::row::valid()
|
||||
const
|
||||
{
|
||||
return std::any_of(std::begin(its), std::end(its), []
|
||||
return std::any_of(std::begin(*this), std::end(*this), []
|
||||
(const auto &cell)
|
||||
{
|
||||
return cell.valid();
|
||||
|
@ -2481,7 +2477,7 @@ bool
|
|||
ircd::db::row::valid(const string_view &s)
|
||||
const
|
||||
{
|
||||
return std::any_of(std::begin(its), std::end(its), [&s]
|
||||
return std::any_of(std::begin(*this), std::end(*this), [&s]
|
||||
(const auto &cell)
|
||||
{
|
||||
return cell.valid(s);
|
||||
|
|
Loading…
Add table
Reference in a new issue