From 230be7d922e1f7f072ab135eda8c655ce842b8ac Mon Sep 17 00:00:00 2001 From: Jason Volk Date: Wed, 7 Feb 2018 21:08:58 -0800 Subject: [PATCH] ircd::m: Remove the query; remove the query from the cursor. --- include/ircd/m/cursor.h | 77 ++++++-------- include/ircd/m/m.h | 3 +- include/ircd/m/query.h | 221 ---------------------------------------- 3 files changed, 32 insertions(+), 269 deletions(-) delete mode 100644 include/ircd/m/query.h diff --git a/include/ircd/m/cursor.h b/include/ircd/m/cursor.h index dee584e08..9b2a720bf 100644 --- a/include/ircd/m/cursor.h +++ b/include/ircd/m/cursor.h @@ -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 using query_type = dbs::query; 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 using query_type = cursor::query_type; - const query_type<> *query{nullptr}; index_iterator idx; - std::array cell; + std::array 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 ircd::m::dbs::cursor::const_iterator_base::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::const_iterator_base(c { !this->idx || !row_valid() } -{ - if(invalid) - return; - - if(!this->query) - return; - - if(!(*this->query)(this->operator*())) - this->operator++(); -} +{} template ircd::m::dbs::cursor::const_iterator_base & @@ -203,12 +170,30 @@ ircd::m::dbs::cursor::const_iterator_base::seek_row() return false; stale = true; - if(this->query && !(*this->query)(this->operator*())) - return false; - return true; } +template +typename ircd::m::dbs::cursor::const_iterator_base::value_type * +ircd::m::dbs::cursor::const_iterator_base::operator->() +const +{ + return &this->operator*(); +} + +template +typename ircd::m::dbs::cursor::const_iterator_base::value_type & +ircd::m::dbs::cursor::const_iterator_base::operator*() +const +{ + if(!stale) + return v; + + assign(v, row, row_key()); + stale = false; + return v; +} + template bool ircd::m::dbs::cursor::const_iterator_base::row_valid() diff --git a/include/ircd/m/m.h b/include/ircd/m/m.h index b76ca9ac2..8196bd9c8 100644 --- a/include/ircd/m/m.h +++ b/include/ircd/m/m.h @@ -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" diff --git a/include/ircd/m/query.h b/include/ircd/m/query.h deleted file mode 100644 index 58c36d0b6..000000000 --- a/include/ircd/m/query.h +++ /dev/null @@ -1,221 +0,0 @@ -// Matrix Construct -// -// Copyright (C) Matrix Construct Developers, Authors & Contributors -// Copyright (C) 2016-2018 Jason Volk -// -// 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 struct query; - - struct query; - struct query; - struct query; - struct query; - struct query; - struct query; - struct query; - - query operator||(const query<> &a, const query<> &b); - query operator&&(const query<> &a, const query<> &b); - query operator!(const query<> &a); - - extern const query<> noop; -} - -struct ircd::m::dbs::query -{ - 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 -:query<> -{ - using function = std::function; - - 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 -: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::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 -: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::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 -: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 -: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 -: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::operator||(const query<> &a, const query<> &b) -{ - return { a, b }; -} - -inline ircd::m::dbs::query -ircd::m::dbs::operator&&(const query<> &a, const query<> &b) -{ - return { a, b }; -} - -inline ircd::m::dbs::query -ircd::m::dbs::operator!(const query<> &a) -{ - return { a }; -}