0
0
Fork 0
mirror of https://github.com/matrix-construct/construct synced 2024-11-29 10:12:39 +01:00

ircd::db: Cleanup json::tuple assignment related.

This commit is contained in:
Jason Volk 2017-09-21 16:59:58 -07:00
parent 971496817b
commit 59f9a51404
2 changed files with 130 additions and 56 deletions

View file

@ -146,7 +146,13 @@ ircd::db::cursor<d, tuple>::const_iterator::const_iterator(const cursor &c,
!bool(this->idx) || !row.valid(this->idx->first) !bool(this->idx) || !row.valid(this->idx->first)
} }
{ {
if(!invalid && this->where && !(*this->where)(this->operator*())) if(invalid)
return;
if(!this->where)
return;
if(!(*this->where)(this->operator*()))
seek(pos::NEXT); seek(pos::NEXT);
} }
@ -201,32 +207,11 @@ const typename ircd::db::cursor<d, tuple>::const_iterator::value_type &
ircd::db::cursor<d, tuple>::const_iterator::operator*() ircd::db::cursor<d, tuple>::const_iterator::operator*()
const const
{ {
if(stale) if(!stale)
{ return v;
for(const auto &cell : row)
{
const column &c{cell};
const database::descriptor &desc{describe(c)};
if(desc.type.second == typeid(string_view))
{
if(cell.valid(idx->first))
json::set(v, cell.col(), cell.val());
else
json::set(v, cell.col(), string_view{});
}
else
{
if(cell.valid(idx->first))
json::set(v, cell.col(), byte_view<string_view>{cell.val()});
else
json::set(v, cell.col(), byte_view<string_view>{string_view{}});
}
}
assign(v, row, idx->first);
stale = false; stale = false;
}
return v; return v;
} }

View file

@ -25,44 +25,133 @@
namespace ircd::db namespace ircd::db
{ {
template<class tuple> tuple make_tuple(const row &r); template<class tuple> bool assign(tuple &, const cell &);
template<class it> void set_index(it begin, it end, const string_view &index); template<class tuple> bool assign(tuple &, const cell &, const string_view &keyeq);
template<class... T> void write(database &, const string_view &index, const json::tuple<T...> &, const sopts & = {});
template<class tuple> size_t assign(tuple &, const row &);
template<class tuple> size_t assign(tuple &, const row &, const string_view &keyeq);
template<class tuple, class... args> tuple make_tuple(args&&...);
} }
// namespace ircd::db
// Commit a json::tuple to the database as a single transaction.
//
template<class it>
void
ircd::db::set_index(it b,
it e,
const string_view &index)
{ {
std::for_each(b, e, [&index] template<class tuple> void _assign_invalid(tuple &, const cell &);
(auto &delta) template<class tuple> void _assign_valid(tuple &, const cell &);
{
std::get<2>(delta) = index;
});
} }
template<class... T> template<class tuple,
void class... args>
ircd::db::write(database &database, tuple
const string_view &index, ircd::db::make_tuple(args&&... a)
const json::tuple<T...> &tuple,
const sopts &opts)
{ {
std::array<delta, tuple.size()> deltas; tuple ret;
deltas(index, tuple, begin(deltas)); assign(ret, std::forward<args>(a)...);
database(begin(deltas), end(deltas)); return ret;
} }
template<class tuple> template<class tuple>
tuple size_t
ircd::db::make_tuple(const row &row) ircd::db::assign(tuple &t,
const row &row,
const string_view &keyeq)
{ {
tuple ret; return std::count_if(std::begin(row), std::end(row), [&t, &keyeq]
set(ret, row); (const auto &cell)
return ret; {
return assign(t, cell, keyeq);
});
}
template<class tuple>
size_t
ircd::db::assign(tuple &t,
const row &row)
{
return std::count_if(std::begin(row), std::end(row), [&t]
(const auto &cell)
{
return assign(t, cell);
});
}
template<class tuple>
bool
ircd::db::assign(tuple &t,
const cell &cell)
{
const bool valid
{
cell.valid()
};
if(valid)
_assign_valid(t, cell);
else
_assign_invalid(t, cell);
return valid;
}
template<class tuple>
bool
ircd::db::assign(tuple &t,
const cell &cell,
const string_view &keyeq)
{
const bool valid
{
cell.valid(keyeq)
};
if(valid)
_assign_valid(t, cell);
else
_assign_invalid(t, cell);
return valid;
}
template<class tuple>
void
ircd::db::_assign_invalid(tuple &t,
const cell &cell)
{
const column &c{cell};
const auto &descriptor
{
describe(c)
};
const bool is_string
{
descriptor.type.second == typeid(string_view)
};
if(is_string)
json::set(t, cell.col(), string_view{});
else
json::set(t, cell.col(), byte_view<string_view>{string_view{}});
}
template<class tuple>
void
ircd::db::_assign_valid(tuple &t,
const cell &cell)
{
const column &c{cell};
const auto &descriptor
{
describe(c)
};
const bool is_string
{
descriptor.type.second == typeid(string_view)
};
if(is_string)
json::set(t, cell.col(), cell.val());
else
json::set(t, cell.col(), byte_view<string_view>{cell.val()});
} }