0
0
Fork 0
mirror of https://github.com/matrix-construct/construct synced 2024-11-17 23:40:57 +01:00

ircd::db: Cleanup comparator related.

This commit is contained in:
Jason Volk 2017-09-22 18:48:35 -07:00
parent 2a17d2b939
commit b29224aa3a
2 changed files with 66 additions and 60 deletions

View file

@ -27,13 +27,57 @@ namespace ircd::db
{ {
struct comparator; struct comparator;
struct cmp_string_view extern const cmp_string_view; struct cmp_int64_t;
struct cmp_int64_t extern const cmp_int64_t; struct cmp_string_view;
} }
struct ircd::db::comparator struct ircd::db::comparator
{ {
std::string name; string_view name;
std::function<bool (const string_view &, const string_view &)> less; std::function<bool (const string_view &, const string_view &)> less;
std::function<bool (const string_view &, const string_view &)> equal; std::function<bool (const string_view &, const string_view &)> equal;
}; };
struct ircd::db::cmp_string_view
:db::comparator
{
static bool less(const string_view &a, const string_view &b)
{
return a < b;
}
static bool equal(const string_view &a, const string_view &b)
{
return a == b;
}
cmp_string_view()
:db::comparator{"string_view", less, equal}
{}
};
struct ircd::db::cmp_int64_t
:db::comparator
{
static bool less(const string_view &sa, const string_view &sb)
{
assert(sa.size() == sizeof(int64_t));
assert(sb.size() == sizeof(int64_t));
const byte_view<int64_t> a{sa};
const byte_view<int64_t> b{sb};
return a < b;
}
static bool equal(const string_view &sa, const string_view &sb)
{
assert(sa.size() == sizeof(int64_t));
assert(sb.size() == sizeof(int64_t));
const byte_view<int64_t> a{sa};
const byte_view<int64_t> b{sb};
return a == b;
}
cmp_int64_t()
:db::comparator{"int64_t", less, equal}
{}
};

View file

@ -710,7 +710,7 @@ ircd::db::database::comparator::Name()
const const
{ {
assert(!user.name.empty()); assert(!user.name.empty());
return user.name.c_str(); return user.name.data();
} }
bool bool
@ -719,9 +719,7 @@ ircd::db::database::comparator::Equal(const Slice &a,
const const
{ {
assert(bool(user.equal)); assert(bool(user.equal));
const string_view sa{slice(a)}; return user.equal(slice(a), slice(b));
const string_view sb{slice(b)};
return user.equal(sa, sb);
} }
int int
@ -730,11 +728,13 @@ ircd::db::database::comparator::Compare(const Slice &a,
const const
{ {
assert(bool(user.less)); assert(bool(user.less));
const string_view sa{slice(a)}; const auto sa{slice(a)};
const string_view sb{slice(b)}; const auto sb{slice(b)};
return user.less(sa, sb)? -1: return user.less(sa, sb)? -1: // less[Y], equal[?], greater[?]
user.less(sb, sa)? 1: user.equal && user.equal(sa, sb)? 0: // less[N], equal[Y], greater[?]
0; user.equal? 1: // less[N], equal[N], greater[Y]
user.less(sb, sa)? 1: // less[N], equal[?], greater[Y]
0; // less[N], equal[Y], greater[N]
} }
void void
@ -751,52 +751,6 @@ const
const string_view limit{_limit.data(), _limit.size()}; const string_view limit{_limit.data(), _limit.size()};
} }
struct ircd::db::cmp_string_view
:db::comparator
{
cmp_string_view()
:db::comparator
{
"string_view"
,[](const string_view &a, const string_view &b)
{
return a < b;
}
,[](const string_view &a, const string_view &b)
{
return a == b;
}
}{}
}
const ircd::db::cmp_string_view;
struct ircd::db::cmp_int64_t
:db::comparator
{
cmp_int64_t()
:db::comparator
{
"int64_t"
,[](const string_view &sa, const string_view &sb)
{
assert(sa.size() == sizeof(int64_t));
assert(sb.size() == sizeof(int64_t));
const auto &a(*reinterpret_cast<const int64_t *>(sa.data()));
const auto &b(*reinterpret_cast<const int64_t *>(sb.data()));
return a < b;
}
,[](const string_view &sa, const string_view &sb)
{
assert(sa.size() == sizeof(int64_t));
assert(sb.size() == sizeof(int64_t));
const auto &a(*reinterpret_cast<const int64_t *>(sa.data()));
const auto &b(*reinterpret_cast<const int64_t *>(sb.data()));
return a == b;
}
}{}
}
const ircd::db::cmp_int64_t;
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////
// //
// database::prefix_transform // database::prefix_transform
@ -919,9 +873,9 @@ ircd::db::database::column::column(database *const &d,
if(!this->descriptor.cmp.less) if(!this->descriptor.cmp.less)
{ {
if(key_type == typeid(string_view)) if(key_type == typeid(string_view))
this->cmp.user = cmp_string_view; this->cmp.user = cmp_string_view{};
else if(key_type == typeid(int64_t)) else if(key_type == typeid(int64_t))
this->cmp.user = cmp_int64_t; this->cmp.user = cmp_int64_t{};
else else
throw error("column '%s' key type[%s] requires user supplied comparator", throw error("column '%s' key type[%s] requires user supplied comparator",
this->name, this->name,
@ -943,6 +897,14 @@ ircd::db::database::column::column(database *const &d,
//log.debug("'%s': Creating new column '%s'", d->name, this->name); //log.debug("'%s': Creating new column '%s'", d->name, this->name);
//throw_on_error(d->d->CreateColumnFamily(this->options, this->name, &this->handle)); //throw_on_error(d->d->CreateColumnFamily(this->options, this->name, &this->handle));
log.debug("schema '%s' declares column [%s => %s] cmp[%s] prefix[%s]: %s",
db::name(*d),
demangle(key_type.name()),
demangle(mapped_type.name()),
this->cmp.Name(),
this->options.prefix_extractor? this->prefix.Name() : "none",
this->descriptor.name);
} }
ircd::db::database::column::~column() ircd::db::database::column::~column()