2017-09-08 11:23:11 +02:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2016 Charybdis Development Team
|
|
|
|
* Copyright (C) 2016 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.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
|
|
|
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
|
|
|
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
|
|
* DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
|
|
|
|
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
|
|
|
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
|
|
|
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
|
|
|
|
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
|
|
|
|
* IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
* POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
#define HAVE_IRCD_DB_CURSOR_H
|
|
|
|
|
|
|
|
namespace ircd::db
|
|
|
|
{
|
|
|
|
template<database *const &d, class tuple> struct cursor;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<ircd::db::database *const &d,
|
|
|
|
class tuple>
|
|
|
|
struct ircd::db::cursor
|
|
|
|
{
|
2017-09-22 12:31:44 +02:00
|
|
|
template<class index_iterator> struct const_iterator_base;
|
|
|
|
struct const_reverse_iterator;
|
2017-09-08 11:23:11 +02:00
|
|
|
struct const_iterator;
|
|
|
|
|
|
|
|
using where_type = db::where<tuple>;
|
|
|
|
using iterator_type = const_iterator;
|
|
|
|
|
|
|
|
struct index index;
|
2017-09-17 00:20:13 +02:00
|
|
|
const where_type *where{nullptr};
|
2017-09-08 11:23:11 +02:00
|
|
|
|
2017-09-26 06:37:32 +02:00
|
|
|
const_iterator end(const string_view &key = {});
|
|
|
|
const_iterator begin(const string_view &key = {});
|
2017-09-08 11:23:11 +02:00
|
|
|
|
2017-09-26 06:37:32 +02:00
|
|
|
const_reverse_iterator rend(const string_view &key = {});
|
|
|
|
const_reverse_iterator rbegin(const string_view &key = {});
|
2017-09-22 12:31:44 +02:00
|
|
|
|
|
|
|
cursor(const string_view &index, const where_type *const &where = nullptr)
|
2017-09-08 11:23:11 +02:00
|
|
|
:index{*d, index}
|
2017-09-22 12:31:44 +02:00
|
|
|
,where{where}
|
2017-09-08 11:23:11 +02:00
|
|
|
{}
|
|
|
|
};
|
|
|
|
|
|
|
|
template<ircd::db::database *const &d,
|
|
|
|
class tuple>
|
2017-09-22 12:31:44 +02:00
|
|
|
template<class index_iterator>
|
|
|
|
struct ircd::db::cursor<d, tuple>::const_iterator_base
|
2017-09-08 11:23:11 +02:00
|
|
|
{
|
|
|
|
using value_type = const tuple;
|
|
|
|
using pointer = value_type *;
|
|
|
|
using reference = value_type &;
|
|
|
|
using difference_type = size_t;
|
|
|
|
using iterator_category = std::bidirectional_iterator_tag;
|
|
|
|
using where_type = cursor::where_type;
|
|
|
|
|
|
|
|
const where_type *where{nullptr};
|
|
|
|
index_iterator idx;
|
|
|
|
db::row row;
|
|
|
|
mutable tuple v;
|
|
|
|
mutable bool stale{true};
|
2017-09-22 12:31:44 +02:00
|
|
|
bool invalid{true};
|
2017-09-08 11:23:11 +02:00
|
|
|
|
2017-09-16 20:48:09 +02:00
|
|
|
operator bool() const;
|
|
|
|
bool operator!() const;
|
2017-09-08 11:23:11 +02:00
|
|
|
|
2017-09-22 12:31:44 +02:00
|
|
|
bool operator==(const const_iterator_base &o) const;
|
|
|
|
bool operator!=(const const_iterator_base &o) const;
|
2017-09-08 11:23:11 +02:00
|
|
|
|
2017-09-22 12:31:44 +02:00
|
|
|
value_type &operator*() const
|
|
|
|
{
|
|
|
|
if(!stale)
|
|
|
|
return v;
|
2017-09-08 11:23:11 +02:00
|
|
|
|
2017-09-25 02:59:23 +02:00
|
|
|
assign(v, row, row_key());
|
2017-09-22 12:31:44 +02:00
|
|
|
stale = false;
|
|
|
|
return v;
|
|
|
|
}
|
|
|
|
|
|
|
|
value_type *operator->() const
|
|
|
|
{
|
|
|
|
return &this->operator*();
|
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
2017-09-25 02:59:23 +02:00
|
|
|
string_view row_key() const;
|
|
|
|
bool row_valid() const;
|
2017-09-22 12:31:44 +02:00
|
|
|
bool seek_row();
|
2017-09-08 11:23:11 +02:00
|
|
|
|
|
|
|
public:
|
2017-09-22 12:31:44 +02:00
|
|
|
const_iterator_base &operator++();
|
|
|
|
const_iterator_base &operator--();
|
2017-09-08 11:23:11 +02:00
|
|
|
|
2017-09-22 12:31:44 +02:00
|
|
|
const_iterator_base();
|
|
|
|
const_iterator_base(const cursor &, index_iterator, const gopts & = {});
|
|
|
|
};
|
|
|
|
|
|
|
|
template<ircd::db::database *const &d,
|
|
|
|
class tuple>
|
|
|
|
struct ircd::db::cursor<d, tuple>::const_iterator
|
|
|
|
:const_iterator_base<index::const_iterator>
|
|
|
|
{
|
|
|
|
using const_iterator_base<index::const_iterator>::const_iterator_base;
|
|
|
|
};
|
|
|
|
|
|
|
|
template<ircd::db::database *const &d,
|
|
|
|
class tuple>
|
|
|
|
struct ircd::db::cursor<d, tuple>::const_reverse_iterator
|
|
|
|
:const_iterator_base<index::const_reverse_iterator>
|
|
|
|
{
|
|
|
|
using const_iterator_base<index::const_reverse_iterator>::const_iterator_base;
|
2017-09-08 11:23:11 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
//
|
|
|
|
// cursor
|
|
|
|
//
|
|
|
|
|
|
|
|
template<ircd::db::database *const &d,
|
|
|
|
class tuple>
|
2017-09-22 12:31:44 +02:00
|
|
|
typename ircd::db::cursor<d, tuple>::const_reverse_iterator
|
|
|
|
ircd::db::cursor<d, tuple>::rbegin(const string_view &key)
|
2017-09-08 11:23:11 +02:00
|
|
|
{
|
2017-09-22 12:31:44 +02:00
|
|
|
return const_reverse_iterator { *this, index.rbegin(key), {} };
|
2017-09-08 11:23:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
template<ircd::db::database *const &d,
|
|
|
|
class tuple>
|
2017-09-22 12:31:44 +02:00
|
|
|
typename ircd::db::cursor<d, tuple>::const_reverse_iterator
|
|
|
|
ircd::db::cursor<d, tuple>::rend(const string_view &key)
|
2017-09-08 11:23:11 +02:00
|
|
|
{
|
2017-09-22 12:31:44 +02:00
|
|
|
return const_reverse_iterator { *this, index.rend(key), {} };
|
2017-09-08 11:23:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
template<ircd::db::database *const &d,
|
|
|
|
class tuple>
|
|
|
|
typename ircd::db::cursor<d, tuple>::const_iterator
|
2017-09-22 12:31:44 +02:00
|
|
|
ircd::db::cursor<d, tuple>::begin(const string_view &key)
|
2017-09-08 11:23:11 +02:00
|
|
|
{
|
2017-09-22 12:31:44 +02:00
|
|
|
return const_iterator { *this, index.begin(key), {} };
|
2017-09-08 11:23:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
template<ircd::db::database *const &d,
|
|
|
|
class tuple>
|
2017-09-22 12:31:44 +02:00
|
|
|
typename ircd::db::cursor<d, tuple>::const_iterator
|
|
|
|
ircd::db::cursor<d, tuple>::end(const string_view &key)
|
2017-09-08 11:23:11 +02:00
|
|
|
{
|
2017-09-22 12:31:44 +02:00
|
|
|
return const_iterator { *this, index.end(key), {} };
|
2017-09-08 11:23:11 +02:00
|
|
|
}
|
|
|
|
|
2017-09-22 12:31:44 +02:00
|
|
|
//
|
|
|
|
// const iterator
|
|
|
|
//
|
|
|
|
|
2017-09-08 11:23:11 +02:00
|
|
|
template<ircd::db::database *const &d,
|
|
|
|
class tuple>
|
2017-09-22 12:31:44 +02:00
|
|
|
template<class index_iterator>
|
|
|
|
ircd::db::cursor<d, tuple>::const_iterator_base<index_iterator>::const_iterator_base(const cursor &c,
|
|
|
|
index_iterator idx,
|
|
|
|
const gopts &opts)
|
2017-09-08 11:23:11 +02:00
|
|
|
:where{c.where}
|
|
|
|
,idx{std::move(idx)}
|
|
|
|
,row
|
|
|
|
{
|
2017-09-25 02:59:23 +02:00
|
|
|
*d,
|
|
|
|
bool(this->idx) && this->idx->second? this->idx->second:
|
|
|
|
bool(this->idx)? this->idx->first:
|
|
|
|
string_view{},
|
|
|
|
tuple{},
|
|
|
|
opts
|
|
|
|
}
|
|
|
|
,stale
|
|
|
|
{
|
|
|
|
true
|
2017-09-08 11:23:11 +02:00
|
|
|
}
|
|
|
|
,invalid
|
|
|
|
{
|
2017-09-25 02:59:23 +02:00
|
|
|
!this->idx || !row_valid()
|
2017-09-08 11:23:11 +02:00
|
|
|
}
|
|
|
|
{
|
2017-09-22 01:59:58 +02:00
|
|
|
if(invalid)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if(!this->where)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if(!(*this->where)(this->operator*()))
|
2017-09-22 12:31:44 +02:00
|
|
|
this->operator++();
|
2017-09-08 11:23:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
template<ircd::db::database *const &d,
|
|
|
|
class tuple>
|
2017-09-22 12:31:44 +02:00
|
|
|
template<class index_iterator>
|
|
|
|
ircd::db::cursor<d, tuple>::const_iterator_base<index_iterator> &
|
|
|
|
ircd::db::cursor<d, tuple>::const_iterator_base<index_iterator>::operator++()
|
2017-09-08 11:23:11 +02:00
|
|
|
{
|
2017-09-22 12:31:44 +02:00
|
|
|
while(!(invalid = !bool(++idx)))
|
|
|
|
if(seek_row())
|
|
|
|
break;
|
2017-09-08 11:23:11 +02:00
|
|
|
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<ircd::db::database *const &d,
|
|
|
|
class tuple>
|
2017-09-22 12:31:44 +02:00
|
|
|
template<class index_iterator>
|
|
|
|
ircd::db::cursor<d, tuple>::const_iterator_base<index_iterator> &
|
|
|
|
ircd::db::cursor<d, tuple>::const_iterator_base<index_iterator>::operator--()
|
2017-09-08 11:23:11 +02:00
|
|
|
{
|
2017-09-22 12:31:44 +02:00
|
|
|
while(!(invalid = !bool(--idx)))
|
|
|
|
if(seek_row())
|
|
|
|
break;
|
2017-09-08 11:23:11 +02:00
|
|
|
|
2017-09-22 12:31:44 +02:00
|
|
|
return *this;
|
2017-09-08 11:23:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
template<ircd::db::database *const &d,
|
|
|
|
class tuple>
|
2017-09-22 12:31:44 +02:00
|
|
|
template<class index_iterator>
|
|
|
|
bool
|
|
|
|
ircd::db::cursor<d, tuple>::const_iterator_base<index_iterator>::seek_row()
|
2017-09-08 11:23:11 +02:00
|
|
|
{
|
2017-09-25 02:59:23 +02:00
|
|
|
if(!db::seek(row, row_key()))
|
2017-09-22 12:31:44 +02:00
|
|
|
return false;
|
2017-09-08 11:23:11 +02:00
|
|
|
|
2017-09-22 12:31:44 +02:00
|
|
|
stale = true;
|
|
|
|
if(this->where && !(*this->where)(this->operator*()))
|
|
|
|
return false;
|
2017-09-08 11:23:11 +02:00
|
|
|
|
2017-09-22 12:31:44 +02:00
|
|
|
return true;
|
2017-09-08 11:23:11 +02:00
|
|
|
}
|
|
|
|
|
2017-09-25 02:59:23 +02:00
|
|
|
template<ircd::db::database *const &d,
|
|
|
|
class tuple>
|
|
|
|
template<class index_iterator>
|
|
|
|
bool
|
|
|
|
ircd::db::cursor<d, tuple>::const_iterator_base<index_iterator>::row_valid()
|
|
|
|
const
|
|
|
|
{
|
|
|
|
return row.valid(row_key());
|
|
|
|
}
|
|
|
|
|
|
|
|
template<ircd::db::database *const &d,
|
|
|
|
class tuple>
|
|
|
|
template<class index_iterator>
|
|
|
|
ircd::string_view
|
|
|
|
ircd::db::cursor<d, tuple>::const_iterator_base<index_iterator>::row_key()
|
|
|
|
const
|
|
|
|
{
|
|
|
|
if(!idx)
|
|
|
|
return {};
|
|
|
|
|
|
|
|
if(idx->second)
|
|
|
|
return idx->second;
|
|
|
|
|
|
|
|
assert(bool(idx->first));
|
|
|
|
return idx->first;
|
|
|
|
}
|
|
|
|
|
2017-09-16 20:48:09 +02:00
|
|
|
template<ircd::db::database *const &d,
|
|
|
|
class tuple>
|
2017-09-22 12:31:44 +02:00
|
|
|
template<class index_iterator>
|
2017-09-16 20:48:09 +02:00
|
|
|
bool
|
2017-09-22 12:31:44 +02:00
|
|
|
ircd::db::cursor<d, tuple>::const_iterator_base<index_iterator>::operator!()
|
2017-09-16 20:48:09 +02:00
|
|
|
const
|
|
|
|
{
|
|
|
|
return !static_cast<bool>(*this);
|
|
|
|
}
|
|
|
|
|
|
|
|
template<ircd::db::database *const &d,
|
|
|
|
class tuple>
|
2017-09-22 12:31:44 +02:00
|
|
|
template<class index_iterator>
|
|
|
|
ircd::db::cursor<d, tuple>::const_iterator_base<index_iterator>::operator
|
2017-09-16 20:48:09 +02:00
|
|
|
bool()
|
|
|
|
const
|
|
|
|
{
|
|
|
|
if(invalid)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if(!idx)
|
|
|
|
return false;
|
|
|
|
|
2017-09-25 02:59:23 +02:00
|
|
|
return row_valid();
|
2017-09-16 20:48:09 +02:00
|
|
|
}
|
|
|
|
|
2017-09-08 11:23:11 +02:00
|
|
|
template<ircd::db::database *const &d,
|
|
|
|
class tuple>
|
2017-09-22 12:31:44 +02:00
|
|
|
template<class index_iterator>
|
2017-09-08 11:23:11 +02:00
|
|
|
bool
|
2017-09-22 12:31:44 +02:00
|
|
|
ircd::db::cursor<d, tuple>::const_iterator_base<index_iterator>::operator!=(const const_iterator_base<index_iterator> &o)
|
2017-09-08 11:23:11 +02:00
|
|
|
const
|
|
|
|
{
|
|
|
|
return !(*this == o);
|
|
|
|
}
|
|
|
|
|
|
|
|
template<ircd::db::database *const &d,
|
|
|
|
class tuple>
|
2017-09-22 12:31:44 +02:00
|
|
|
template<class index_iterator>
|
2017-09-08 11:23:11 +02:00
|
|
|
bool
|
2017-09-22 12:31:44 +02:00
|
|
|
ircd::db::cursor<d, tuple>::const_iterator_base<index_iterator>::operator==(const const_iterator_base<index_iterator> &o)
|
2017-09-08 11:23:11 +02:00
|
|
|
const
|
|
|
|
{
|
2017-09-25 02:59:23 +02:00
|
|
|
if(row_key() != o.row_key())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if(!row_valid() && !o.row_valid())
|
2017-09-08 11:23:11 +02:00
|
|
|
return true;
|
|
|
|
|
2017-09-25 02:59:23 +02:00
|
|
|
if(!row_valid() || !o.row_valid())
|
2017-09-08 11:23:11 +02:00
|
|
|
return false;
|
|
|
|
|
2017-09-25 02:59:23 +02:00
|
|
|
return true;
|
2017-09-08 11:23:11 +02:00
|
|
|
}
|