0
0
Fork 0
mirror of https://github.com/matrix-construct/construct synced 2024-12-26 15:33:54 +01:00

ircd:Ⓜ️ Remove the query; remove the query from the cursor.

This commit is contained in:
Jason Volk 2018-02-07 21:08:58 -08:00
parent 5c968f0949
commit 230be7d922
3 changed files with 32 additions and 269 deletions

View file

@ -9,12 +9,7 @@
// full license for this software is available in the LICENSE file.
#pragma once
#define HAVE_IRCD_M_VM_CURSOR_H
namespace ircd::m::dbs
{
struct cursor;
}
#define HAVE_IRCD_M_DBS_CURSOR_H
struct ircd::m::dbs::cursor
{
@ -22,11 +17,9 @@ struct ircd::m::dbs::cursor
struct const_reverse_iterator;
struct const_iterator;
template<enum where where = where::noop> using query_type = dbs::query<where>;
using iterator_type = const_iterator;
db::index index;
const query_type<> *query{nullptr};
const_iterator end(const string_view &key = {});
const_iterator begin(const string_view &key = {});
@ -34,9 +27,8 @@ struct ircd::m::dbs::cursor
const_reverse_iterator rend(const string_view &key = {});
const_reverse_iterator rbegin(const string_view &key = {});
cursor(const string_view &index, const query_type<> *const &query = nullptr)
:index{*event::events, index}
,query{query}
cursor(const string_view &index)
:index{*dbs::events, index}
{}
};
@ -48,11 +40,9 @@ struct ircd::m::dbs::cursor::const_iterator_base
using reference = value_type &;
using difference_type = size_t;
using iterator_category = std::bidirectional_iterator_tag;
template<enum where where = where::noop> using query_type = cursor::query_type<where>;
const query_type<> *query{nullptr};
index_iterator idx;
std::array<db::cell, event::size()> cell;
std::array<db::cell, event_columns> cell;
db::row row;
mutable event v;
mutable bool stale{true};
@ -64,23 +54,10 @@ struct ircd::m::dbs::cursor::const_iterator_base
bool operator==(const const_iterator_base &o) const;
bool operator!=(const const_iterator_base &o) const;
value_type &operator*() const
{
if(!stale)
return v;
assign(v, row, row_key());
stale = false;
return v;
}
value_type *operator->() const
{
return &this->operator*();
}
string_view row_key() const;
bool row_valid() const;
string_view row_key() const;
value_type &operator*() const;
value_type *operator->() const;
protected:
bool seek_row();
@ -141,12 +118,11 @@ template<class index_iterator>
ircd::m::dbs::cursor::const_iterator_base<index_iterator>::const_iterator_base(const cursor &c,
index_iterator idx,
const db::gopts &opts)
:query{c.query}
,idx{std::move(idx)}
:idx{std::move(idx)}
,cell{}
,row
{
*event::events,
*dbs::events,
bool(this->idx) && this->idx->second? this->idx->second:
bool(this->idx)? this->idx->first:
string_view{},
@ -162,16 +138,7 @@ ircd::m::dbs::cursor::const_iterator_base<index_iterator>::const_iterator_base(c
{
!this->idx || !row_valid()
}
{
if(invalid)
return;
if(!this->query)
return;
if(!(*this->query)(this->operator*()))
this->operator++();
}
{}
template<class index_iterator>
ircd::m::dbs::cursor::const_iterator_base<index_iterator> &
@ -203,12 +170,30 @@ ircd::m::dbs::cursor::const_iterator_base<index_iterator>::seek_row()
return false;
stale = true;
if(this->query && !(*this->query)(this->operator*()))
return false;
return true;
}
template<class index_iterator>
typename ircd::m::dbs::cursor::const_iterator_base<index_iterator>::value_type *
ircd::m::dbs::cursor::const_iterator_base<index_iterator>::operator->()
const
{
return &this->operator*();
}
template<class index_iterator>
typename ircd::m::dbs::cursor::const_iterator_base<index_iterator>::value_type &
ircd::m::dbs::cursor::const_iterator_base<index_iterator>::operator*()
const
{
if(!stale)
return v;
assign(v, row, row_key());
stale = false;
return v;
}
template<class index_iterator>
bool
ircd::m::dbs::cursor::const_iterator_base<index_iterator>::row_valid()

View file

@ -45,9 +45,8 @@ namespace ircd
#include "error.h"
#include "id.h"
#include "event.h"
#include "query.h"
#include "cursor.h"
#include "dbs.h"
#include "cursor.h"
#include "state.h"
#include "room.h"
#include "user.h"

View file

@ -1,221 +0,0 @@
// 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.
#pragma once
#define HAVE_IRCD_M_VM_QUERY_H
namespace ircd::m::dbs
{
/// 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
/// an event. All queries inherit from query<> which can execute the
/// 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.
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);
extern const query<> noop;
}
struct ircd::m::dbs::query<ircd::m::dbs::where::noop>
{
virtual bool operator()(const event &) const
{
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}
{}
virtual ~query() noexcept;
};
struct ircd::m::dbs::query<ircd::m::dbs::where::test>
:query<>
{
using function = std::function<bool (const event &)>;
function closure;
bool operator()(const event &t) const override
{
return closure(t);
}
query(function closure)
:query<>{where::test}
,closure{std::move(closure)}
{}
};
struct ircd::m::dbs::query<ircd::m::dbs::where::equal>
:query<>
{
event value;
bool operator()(const event &) const override;
query(const event &value)
:query<>{where::equal}
,value{value}
{}
query(const json::members &members)
:query<>{where::equal}
,value{members}
{}
};
inline bool
ircd::m::dbs::query<ircd::m::dbs::where::equal>::operator()(const event &value)
const
{
return json::until(this->value, value, []
(const auto &key, const auto &a, const auto &b)
{
if(!a)
return true;
return a == b;
});
}
struct ircd::m::dbs::query<ircd::m::dbs::where::not_equal>
:query<>
{
event value;
bool operator()(const event &) const override;
query(const event &value)
:query<>{where::not_equal}
,value{value}
{}
query(const json::members &members)
:query<>{where::not_equal}
,value{members}
{}
};
inline bool
ircd::m::dbs::query<ircd::m::dbs::where::not_equal>::operator()(const event &value)
const
{
return !json::until(this->value, value, []
(const auto &key, const auto &a, const auto &b)
{
if(!a)
return true;
return a == b;
});
}
struct ircd::m::dbs::query<ircd::m::dbs::where::logical_or>
:query<>
{
const query<> *a, *b;
bool operator()(const event &t) const override
{
return (*a)(t) || (*b)(t);
}
query(const query<> &a, const query<> &b)
:query<>{where::logical_or}
,a{&a}
,b{&b}
{}
};
struct ircd::m::dbs::query<ircd::m::dbs::where::logical_and>
:query<>
{
const query<> *a, *b;
bool operator()(const event &t) const override
{
return (*a)(t) && (*b)(t);
}
query(const query<> &a, const query<> &b)
:query<>{where::logical_and}
,a{&a}
,b{&b}
{}
};
struct ircd::m::dbs::query<ircd::m::dbs::where::logical_not>
:query<>
{
const query<> *a;
bool operator()(const event &t) const override
{
return !(*a)(t);
}
query(const query<> &a)
:query<>{where::logical_not}
,a{&a}
{}
};
inline ircd::m::dbs::query<ircd::m::dbs::where::logical_or>
ircd::m::dbs::operator||(const query<> &a, const query<> &b)
{
return { a, b };
}
inline ircd::m::dbs::query<ircd::m::dbs::where::logical_and>
ircd::m::dbs::operator&&(const query<> &a, const query<> &b)
{
return { a, b };
}
inline ircd::m::dbs::query<ircd::m::dbs::where::logical_not>
ircd::m::dbs::operator!(const query<> &a)
{
return { a };
}