0
0
Fork 0
mirror of https://github.com/matrix-construct/construct synced 2024-06-02 18:18:56 +02:00

ircd::db: Improve valid checking and valid() suite.

This commit is contained in:
Jason Volk 2017-09-16 11:48:09 -07:00
parent 1f8fb24e8d
commit 276a902a62
4 changed files with 125 additions and 43 deletions

View file

@ -65,7 +65,9 @@ struct ircd::db::cell
explicit operator column &() { return c; }
bool valid() const; // cell exists
bool valid(const string_view &eq) const; // valid_equal
bool valid(const string_view &) const; // valid equal key
bool valid_gt(const string_view &) const; // valid greater than key
bool valid_lte(const string_view &) const; // valid less than or equal key
operator bool() const { return valid(); }
bool operator!() const { return !valid(); }

View file

@ -71,8 +71,8 @@ struct ircd::db::cursor<d, tuple>::const_iterator
mutable bool stale{true};
bool invalid{false};
bool operator!() const { return invalid || !row.valid(); }
operator bool() const { return !operator!(); }
operator bool() const;
bool operator!() const;
bool operator==(const const_iterator &o) const;
bool operator!=(const const_iterator &o) const;
@ -143,7 +143,7 @@ ircd::db::cursor<d, tuple>::const_iterator::const_iterator(const cursor &c,
}
,invalid
{
!this->idx || !row.valid()
!bool(this->idx) || !row.valid(this->idx->first)
}
{
if(!invalid && this->where && !(*this->where)(this->operator*()))
@ -215,6 +215,30 @@ const
return v;
}
template<ircd::db::database *const &d,
class tuple>
bool
ircd::db::cursor<d, tuple>::const_iterator::operator!()
const
{
return !static_cast<bool>(*this);
}
template<ircd::db::database *const &d,
class tuple>
ircd::db::cursor<d, tuple>::const_iterator::operator
bool()
const
{
if(invalid)
return false;
if(!idx)
return false;
return row.valid(idx->first);
}
template<ircd::db::database *const &d,
class tuple>
bool

View file

@ -58,6 +58,7 @@ struct ircd::db::row
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;

View file

@ -60,9 +60,13 @@ namespace ircd::db
// Validation functors
bool valid(const rocksdb::Iterator &);
bool operator!(const rocksdb::Iterator &);
using valid_proffer = std::function<bool (const rocksdb::Iterator &)>;
bool valid(const rocksdb::Iterator &, const valid_proffer &);
bool valid_eq(const rocksdb::Iterator &, const string_view &);
bool valid_lte(const rocksdb::Iterator &, const string_view &);
bool valid_gt(const rocksdb::Iterator &, const string_view &);
void valid_or_throw(const rocksdb::Iterator &);
bool valid_equal(const rocksdb::Iterator &, const string_view &);
void valid_equal_or_throw(const rocksdb::Iterator &, const string_view &);
void valid_eq_or_throw(const rocksdb::Iterator &, const string_view &);
// [GET] seek suite
template<class pos> bool seek(database::column &, const pos &, rocksdb::ReadOptions &, std::unique_ptr<rocksdb::Iterator> &it);
@ -1409,7 +1413,7 @@ ircd::db::cell::cell(column column,
,it{ss? seek(this->c, index, opts) : std::unique_ptr<rocksdb::Iterator>{}}
{
if(bool(this->it))
if(!valid_equal(*this->it, index))
if(!valid_eq(*this->it, index))
this->it.reset();
}
@ -1422,7 +1426,7 @@ ircd::db::cell::cell(column column,
,it{std::move(it)}
{
seek(*this, index);
if(!valid_equal(*this->it, index))
if(!valid_eq(*this->it, index))
this->it.reset();
}
@ -1580,13 +1584,6 @@ const
return likely(valid())? db::key(*it) : string_view{};
}
bool
ircd::db::cell::valid(const string_view &eq)
const
{
return bool(it) && db::valid_equal(*it, eq);
}
bool
ircd::db::cell::valid()
const
@ -1594,6 +1591,27 @@ const
return bool(it) && db::valid(*it);
}
bool
ircd::db::cell::valid(const string_view &s)
const
{
return bool(it) && db::valid_eq(*it, s);
}
bool
ircd::db::cell::valid_gt(const string_view &s)
const
{
return bool(it) && db::valid_gt(*it, s);
}
bool
ircd::db::cell::valid_lte(const string_view &s)
const
{
return bool(it) && db::valid_lte(*it, s);
}
///////////////////////////////////////////////////////////////////////////////
//
// db/row.h
@ -1702,7 +1720,7 @@ ircd::db::trim(row &r,
return trim(r, [&index]
(const auto &cell)
{
return !valid_equal(*cell.it, index);
return !valid_eq(*cell.it, index);
});
}
@ -1860,6 +1878,17 @@ const
});
}
bool
ircd::db::row::valid(const string_view &s)
const
{
return std::any_of(std::begin(its), std::end(its), [&s]
(const auto &cell)
{
return cell.valid(s);
});
}
///////////////////////////////////////////////////////////////////////////////
//
// db/column.h
@ -2168,7 +2197,7 @@ ircd::db::column::operator()(const string_view &key,
const gopts &gopts)
{
const auto it(seek(*this, key, gopts));
valid_equal_or_throw(*it, key);
valid_eq_or_throw(*it, key);
func(val(*it));
}
@ -2324,7 +2353,7 @@ const ircd::db::column::const_iterator::value_type &
ircd::db::column::const_iterator::operator*()
const
{
assert(valid(*it));
assert(it && valid(*it));
val.first = db::key(*it);
val.second = db::val(*it);
return val;
@ -2341,19 +2370,19 @@ bool
ircd::db::column::const_iterator::operator!()
const
{
if(!it)
return true;
if(!valid(*it))
return true;
return false;
return !static_cast<bool>(*this);
}
ircd::db::column::const_iterator::operator bool()
const
{
return !!*this;
if(!it)
return false;
if(!valid(*it))
return false;
return true;
}
bool
@ -2994,25 +3023,14 @@ ircd::db::make_opts(const sopts &opts)
}
void
ircd::db::valid_equal_or_throw(const rocksdb::Iterator &it,
const string_view &sv)
ircd::db::valid_eq_or_throw(const rocksdb::Iterator &it,
const string_view &sv)
{
valid_or_throw(it);
if(it.key().compare(slice(sv)) != 0)
if(!valid_eq(it, sv))
{
throw_on_error(it.status());
throw not_found();
}
bool
ircd::db::valid_equal(const rocksdb::Iterator &it,
const string_view &sv)
{
if(!valid(it))
return false;
if(it.key().compare(slice(sv)) != 0)
return false;
return true;
}
}
void
@ -3026,6 +3044,43 @@ ircd::db::valid_or_throw(const rocksdb::Iterator &it)
}
}
bool
ircd::db::valid_lte(const rocksdb::Iterator &it,
const string_view &sv)
{
return valid(it, [&sv](const auto &it)
{
return it.key().compare(slice(sv)) <= 0;
});
}
bool
ircd::db::valid_gt(const rocksdb::Iterator &it,
const string_view &sv)
{
return valid(it, [&sv](const auto &it)
{
return it.key().compare(slice(sv)) > 0;
});
}
bool
ircd::db::valid_eq(const rocksdb::Iterator &it,
const string_view &sv)
{
return valid(it, [&sv](const auto &it)
{
return it.key().compare(slice(sv)) == 0;
});
}
bool
ircd::db::valid(const rocksdb::Iterator &it,
const valid_proffer &proffer)
{
return valid(it)? proffer(it) : false;
}
bool
ircd::db::operator!(const rocksdb::Iterator &it)
{