2018-02-04 03:22:01 +01:00
|
|
|
// Matrix Construct
|
|
|
|
//
|
|
|
|
// Copyright (C) Matrix Construct Developers, Authors & Contributors
|
|
|
|
// Copyright (C) 2016-2018 Jason Volk <jason@zemos.net>
|
|
|
|
//
|
|
|
|
// Permission to use, copy, modify, and/or distribute this software for any
|
|
|
|
// purpose with or without fee is hereby granted, provided that the above
|
|
|
|
// copyright notice and this permission notice is present in all copies. The
|
|
|
|
// full license for this software is available in the LICENSE file.
|
2017-09-28 03:24:36 +02:00
|
|
|
|
|
|
|
#pragma once
|
2017-11-16 02:48:25 +01:00
|
|
|
#define HAVE_IRCD_M_VM_QUERY_H
|
2017-09-28 03:24:36 +02:00
|
|
|
|
2018-01-26 21:26:09 +01:00
|
|
|
namespace ircd::m::dbs
|
2017-09-28 03:24:36 +02:00
|
|
|
{
|
|
|
|
/// Types of query clauses.
|
|
|
|
enum class where
|
|
|
|
{
|
|
|
|
noop,
|
|
|
|
test,
|
|
|
|
equal,
|
|
|
|
not_equal,
|
|
|
|
logical_or,
|
|
|
|
logical_and,
|
|
|
|
logical_not,
|
|
|
|
};
|
|
|
|
|
|
|
|
string_view reflect(const where &);
|
|
|
|
|
|
|
|
/// The query provides a decision tree oriented around the structure of
|
2017-11-16 02:48:25 +01:00
|
|
|
/// an event. All queries inherit from query<> which can execute the
|
2017-09-28 03:24:36 +02:00
|
|
|
/// derived's test via the virtual operator(). The abstract query instance
|
|
|
|
/// stores the type of its derived portion for downcasting. Downcasting is
|
|
|
|
/// used to get more information from the query to get a result faster, ex.
|
|
|
|
/// where::equal, the keys being tested might impact the db fetch pattern.
|
|
|
|
/// or searching a logic tree of where::logical_* for the most efficient
|
|
|
|
/// db fetches to make next.
|
2017-11-16 02:48:25 +01:00
|
|
|
template<enum where = where::noop> struct query;
|
|
|
|
|
|
|
|
struct query<where::noop>;
|
|
|
|
struct query<where::test>;
|
|
|
|
struct query<where::equal>;
|
|
|
|
struct query<where::not_equal>;
|
|
|
|
struct query<where::logical_or>;
|
|
|
|
struct query<where::logical_and>;
|
|
|
|
struct query<where::logical_not>;
|
|
|
|
|
|
|
|
query<where::logical_or> operator||(const query<> &a, const query<> &b);
|
|
|
|
query<where::logical_and> operator&&(const query<> &a, const query<> &b);
|
|
|
|
query<where::logical_not> operator!(const query<> &a);
|
2018-01-26 21:26:09 +01:00
|
|
|
|
|
|
|
extern const query<> noop;
|
2017-09-28 03:24:36 +02:00
|
|
|
}
|
|
|
|
|
2018-01-26 21:26:09 +01:00
|
|
|
struct ircd::m::dbs::query<ircd::m::dbs::where::noop>
|
2017-09-28 03:24:36 +02:00
|
|
|
{
|
2017-11-16 02:48:25 +01:00
|
|
|
virtual bool operator()(const event &) const
|
2017-09-28 03:24:36 +02:00
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Stores the type of the derived class for downcasting. This is important
|
|
|
|
// in order to evaluate details of the query.
|
|
|
|
where type;
|
|
|
|
|
|
|
|
query(const enum where &type = where::noop)
|
|
|
|
:type{type}
|
|
|
|
{}
|
|
|
|
|
2017-11-16 02:48:25 +01:00
|
|
|
virtual ~query() noexcept;
|
2017-09-28 03:24:36 +02:00
|
|
|
};
|
|
|
|
|
2018-01-26 21:26:09 +01:00
|
|
|
struct ircd::m::dbs::query<ircd::m::dbs::where::test>
|
2017-11-16 02:48:25 +01:00
|
|
|
:query<>
|
2017-09-28 03:24:36 +02:00
|
|
|
{
|
2017-11-16 02:48:25 +01:00
|
|
|
using function = std::function<bool (const event &)>;
|
2017-09-28 03:24:36 +02:00
|
|
|
|
|
|
|
function closure;
|
|
|
|
|
2017-11-16 02:48:25 +01:00
|
|
|
bool operator()(const event &t) const override
|
2017-09-28 03:24:36 +02:00
|
|
|
{
|
|
|
|
return closure(t);
|
|
|
|
}
|
|
|
|
|
|
|
|
query(function closure)
|
2017-11-16 02:48:25 +01:00
|
|
|
:query<>{where::test}
|
2017-09-28 03:24:36 +02:00
|
|
|
,closure{std::move(closure)}
|
|
|
|
{}
|
|
|
|
};
|
|
|
|
|
2018-01-26 21:26:09 +01:00
|
|
|
struct ircd::m::dbs::query<ircd::m::dbs::where::equal>
|
2017-11-16 02:48:25 +01:00
|
|
|
:query<>
|
2017-09-28 03:24:36 +02:00
|
|
|
{
|
2017-11-16 02:48:25 +01:00
|
|
|
event value;
|
2017-09-28 03:24:36 +02:00
|
|
|
|
2017-11-16 02:48:25 +01:00
|
|
|
bool operator()(const event &) const override;
|
2017-09-28 03:24:36 +02:00
|
|
|
|
2017-11-16 02:48:25 +01:00
|
|
|
query(const event &value)
|
|
|
|
:query<>{where::equal}
|
2017-09-28 03:24:36 +02:00
|
|
|
,value{value}
|
|
|
|
{}
|
|
|
|
|
|
|
|
query(const json::members &members)
|
2017-11-16 02:48:25 +01:00
|
|
|
:query<>{where::equal}
|
2017-09-28 03:24:36 +02:00
|
|
|
,value{members}
|
|
|
|
{}
|
|
|
|
};
|
|
|
|
|
2017-11-16 02:48:25 +01:00
|
|
|
inline bool
|
2018-01-26 21:26:09 +01:00
|
|
|
ircd::m::dbs::query<ircd::m::dbs::where::equal>::operator()(const event &value)
|
2017-09-28 03:24:36 +02:00
|
|
|
const
|
|
|
|
{
|
|
|
|
return json::until(this->value, value, []
|
|
|
|
(const auto &key, const auto &a, const auto &b)
|
|
|
|
{
|
|
|
|
if(!a)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
return a == b;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-01-26 21:26:09 +01:00
|
|
|
struct ircd::m::dbs::query<ircd::m::dbs::where::not_equal>
|
2017-11-16 02:48:25 +01:00
|
|
|
:query<>
|
2017-09-28 03:24:36 +02:00
|
|
|
{
|
2017-11-16 02:48:25 +01:00
|
|
|
event value;
|
2017-09-28 03:24:36 +02:00
|
|
|
|
2017-11-16 02:48:25 +01:00
|
|
|
bool operator()(const event &) const override;
|
2017-09-28 03:24:36 +02:00
|
|
|
|
2017-11-16 02:48:25 +01:00
|
|
|
query(const event &value)
|
|
|
|
:query<>{where::not_equal}
|
2017-09-28 03:24:36 +02:00
|
|
|
,value{value}
|
|
|
|
{}
|
|
|
|
|
|
|
|
query(const json::members &members)
|
2017-11-16 02:48:25 +01:00
|
|
|
:query<>{where::not_equal}
|
2017-09-28 03:24:36 +02:00
|
|
|
,value{members}
|
|
|
|
{}
|
|
|
|
};
|
|
|
|
|
2017-11-16 02:48:25 +01:00
|
|
|
inline bool
|
2018-01-26 21:26:09 +01:00
|
|
|
ircd::m::dbs::query<ircd::m::dbs::where::not_equal>::operator()(const event &value)
|
2017-09-28 03:24:36 +02:00
|
|
|
const
|
|
|
|
{
|
|
|
|
return !json::until(this->value, value, []
|
|
|
|
(const auto &key, const auto &a, const auto &b)
|
|
|
|
{
|
|
|
|
if(!a)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
return a == b;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-01-26 21:26:09 +01:00
|
|
|
struct ircd::m::dbs::query<ircd::m::dbs::where::logical_or>
|
2017-11-16 02:48:25 +01:00
|
|
|
:query<>
|
2017-09-28 03:24:36 +02:00
|
|
|
{
|
2017-11-16 02:48:25 +01:00
|
|
|
const query<> *a, *b;
|
2017-09-28 03:24:36 +02:00
|
|
|
|
2017-11-16 02:48:25 +01:00
|
|
|
bool operator()(const event &t) const override
|
2017-09-28 03:24:36 +02:00
|
|
|
{
|
|
|
|
return (*a)(t) || (*b)(t);
|
|
|
|
}
|
|
|
|
|
2017-11-16 02:48:25 +01:00
|
|
|
query(const query<> &a, const query<> &b)
|
|
|
|
:query<>{where::logical_or}
|
2017-09-28 03:24:36 +02:00
|
|
|
,a{&a}
|
|
|
|
,b{&b}
|
|
|
|
{}
|
|
|
|
};
|
|
|
|
|
2018-01-26 21:26:09 +01:00
|
|
|
struct ircd::m::dbs::query<ircd::m::dbs::where::logical_and>
|
2017-11-16 02:48:25 +01:00
|
|
|
:query<>
|
2017-09-28 03:24:36 +02:00
|
|
|
{
|
2017-11-16 02:48:25 +01:00
|
|
|
const query<> *a, *b;
|
2017-09-28 03:24:36 +02:00
|
|
|
|
2017-11-16 02:48:25 +01:00
|
|
|
bool operator()(const event &t) const override
|
2017-09-28 03:24:36 +02:00
|
|
|
{
|
|
|
|
return (*a)(t) && (*b)(t);
|
|
|
|
}
|
|
|
|
|
2017-11-16 02:48:25 +01:00
|
|
|
query(const query<> &a, const query<> &b)
|
|
|
|
:query<>{where::logical_and}
|
2017-09-28 03:24:36 +02:00
|
|
|
,a{&a}
|
|
|
|
,b{&b}
|
|
|
|
{}
|
|
|
|
};
|
|
|
|
|
2018-01-26 21:26:09 +01:00
|
|
|
struct ircd::m::dbs::query<ircd::m::dbs::where::logical_not>
|
2017-11-16 02:48:25 +01:00
|
|
|
:query<>
|
2017-09-28 03:24:36 +02:00
|
|
|
{
|
2017-11-16 02:48:25 +01:00
|
|
|
const query<> *a;
|
2017-09-28 03:24:36 +02:00
|
|
|
|
2017-11-16 02:48:25 +01:00
|
|
|
bool operator()(const event &t) const override
|
2017-09-28 03:24:36 +02:00
|
|
|
{
|
|
|
|
return !(*a)(t);
|
|
|
|
}
|
|
|
|
|
2017-11-16 02:48:25 +01:00
|
|
|
query(const query<> &a)
|
|
|
|
:query<>{where::logical_not}
|
2017-09-28 03:24:36 +02:00
|
|
|
,a{&a}
|
|
|
|
{}
|
|
|
|
};
|
|
|
|
|
2018-01-26 21:26:09 +01:00
|
|
|
inline ircd::m::dbs::query<ircd::m::dbs::where::logical_or>
|
|
|
|
ircd::m::dbs::operator||(const query<> &a, const query<> &b)
|
2017-09-28 03:24:36 +02:00
|
|
|
{
|
2017-11-16 02:48:25 +01:00
|
|
|
return { a, b };
|
|
|
|
}
|
2017-09-28 03:24:36 +02:00
|
|
|
|
2018-01-26 21:26:09 +01:00
|
|
|
inline ircd::m::dbs::query<ircd::m::dbs::where::logical_and>
|
|
|
|
ircd::m::dbs::operator&&(const query<> &a, const query<> &b)
|
2017-11-16 02:48:25 +01:00
|
|
|
{
|
|
|
|
return { a, b };
|
|
|
|
}
|
2017-09-28 03:24:36 +02:00
|
|
|
|
2018-01-26 21:26:09 +01:00
|
|
|
inline ircd::m::dbs::query<ircd::m::dbs::where::logical_not>
|
|
|
|
ircd::m::dbs::operator!(const query<> &a)
|
2017-11-16 02:48:25 +01:00
|
|
|
{
|
|
|
|
return { a };
|
2017-09-28 03:24:36 +02:00
|
|
|
}
|