1
0
Fork 0
mirror of https://github.com/KingDuckZ/dindexer.git synced 2025-02-23 12:54:56 +00:00
dindexer/src/backends/redis/scan_iterator.inl

162 lines
5 KiB
Text
Raw Normal View History

/* Copyright 2015, 2016, Michele Santullo
* This file is part of "dindexer".
*
* "dindexer" is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* "dindexer" is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with "dindexer". If not, see <http://www.gnu.org/licenses/>.
*/
#include "command.hpp"
#include <cassert>
#include <ciso646>
namespace redis {
namespace implem {
} //namespace implem
template <typename ValueFetch>
2016-06-13 15:14:10 +01:00
template <typename Dummy, typename>
ScanIterator<ValueFetch>::ScanIterator (Command* parCommand, bool parEnd) :
implem::ScanIteratorBaseClass(parCommand),
implem::ScanIteratorBaseIterator<ValueFetch>(),
2016-06-13 15:14:10 +01:00
ValueFetch(),
m_reply(),
m_scan_context(0),
m_curr_index(0)
{
if (not parEnd) {
m_curr_index = 1; //Some arbitrary value so is_end()==false
assert(not is_end());
this->increment();
}
else {
assert(is_end());
}
}
template <typename ValueFetch>
2016-06-13 15:14:10 +01:00
template <typename Dummy, typename>
ScanIterator<ValueFetch>::ScanIterator (Command* parCommand, boost::string_ref parKey, bool parEnd) :
2016-06-13 15:14:10 +01:00
implem::ScanIteratorBaseClass(parCommand),
implem::ScanIteratorBaseIterator<ValueFetch>(),
2016-06-13 15:14:10 +01:00
ValueFetch(parKey),
m_reply(),
m_scan_context(0),
m_curr_index(0)
{
2016-06-13 10:57:42 +01:00
if (not parEnd) {
m_curr_index = 1; //Some arbitrary value so is_end()==false
assert(not is_end());
this->increment();
2016-06-13 10:57:42 +01:00
}
else {
assert(is_end());
}
}
template <typename ValueFetch>
bool ScanIterator<ValueFetch>::is_end() const {
2016-06-13 10:57:42 +01:00
return not m_curr_index and m_reply.empty() and not m_scan_context;
}
template <typename ValueFetch>
void ScanIterator<ValueFetch>::increment() {
2016-06-13 10:57:42 +01:00
assert(not is_end());
static_assert(ValueFetch::step > 0, "Can't have an increase step of 0");
2016-06-13 10:57:42 +01:00
2016-06-13 15:14:10 +01:00
if (m_curr_index + 1 < m_reply.size()) {
++m_curr_index;
}
2016-06-13 15:14:10 +01:00
else if (m_curr_index + 1 == m_reply.size() and not m_scan_context) {
2016-06-13 10:57:42 +01:00
m_reply.clear();
m_curr_index = 0;
}
else {
std::vector<RedisReplyType> array_reply;
long long new_context;
do {
auto whole_reply = this->forward_scan_command<ValueFetch>(0);
array_reply = get_array(whole_reply);
assert(2 == array_reply.size());
assert(array_reply.size() % ValueFetch::step == 0);
2016-06-13 10:57:42 +01:00
new_context = get_integer_autoconv_if_str(array_reply[0]);
} while (new_context and array_reply.empty());
2016-06-13 15:14:10 +01:00
const auto variant_array = get_array(array_reply[1]);
assert(variant_array.size() % ValueFetch::step == 0);
const std::size_t expected_reply_count = variant_array.size() / ValueFetch::step;
m_reply.clear();
m_reply.reserve(expected_reply_count);
for (std::size_t z = 0; z < variant_array.size(); z += ValueFetch::step) {
m_reply.push_back(ValueFetch::make_value(variant_array.data() + z));
}
assert(expected_reply_count == m_reply.size());
m_scan_context = new_context;
m_curr_index = 0;
}
}
template <typename ValueFetch>
bool ScanIterator<ValueFetch>::equal (const ScanIterator& parOther) const {
return
(&parOther == this) or
2016-06-13 10:57:42 +01:00
(is_end() and parOther.is_end()) or
(
2016-06-13 10:57:42 +01:00
not (is_end() or parOther.is_end()) and
implem::ScanIteratorBaseClass::is_equal(parOther) and
2016-06-13 10:57:42 +01:00
(m_scan_context == parOther.m_scan_context) and
(m_curr_index == parOther.m_curr_index) and
(m_reply.size() == parOther.m_reply.size())
);
}
template <typename ValueFetch>
auto ScanIterator<ValueFetch>::dereference() const -> const value_type& {
assert(not m_reply.empty());
assert(m_curr_index < m_reply.size());
2016-06-13 15:14:10 +01:00
return m_reply[m_curr_index];
}
template <typename ValueFetch>
template <typename T>
RedisReplyType ScanIterator<ValueFetch>::forward_scan_command (typename std::enable_if<HasScanTargetMethod<T>::value, int>::type) {
return implem::ScanIteratorBaseClass::run(T::command(), T::scan_target(), m_scan_context);
}
template <typename ValueFetch>
template <typename T>
RedisReplyType ScanIterator<ValueFetch>::forward_scan_command (typename std::enable_if<not HasScanTargetMethod<T>::value, int>::type) {
return implem::ScanIteratorBaseClass::run(T::command(), m_scan_context);
}
template <typename T>
auto ScanSingleValues<T>::make_value (const RedisReplyType* parItem) -> const value_type& {
assert(parItem);
return get<T>(*parItem);
}
2016-06-13 15:14:10 +01:00
2016-06-13 15:38:18 +01:00
template <typename T>
auto ScanSingleValuesInKey<T>::make_value (const RedisReplyType* parItem) -> const value_type& {
2016-06-13 15:14:10 +01:00
assert(parItem);
2016-06-13 15:38:18 +01:00
return get<T>(*parItem);
2016-06-13 15:14:10 +01:00
}
template <typename P, typename A, typename B>
2016-06-13 15:38:18 +01:00
auto ScanPairs<P, A, B>::make_value (const RedisReplyType* parItem) -> value_type {
assert(parItem);
return value_type(get<A>(parItem[0]), get<B>(parItem[1]));
2016-06-13 15:14:10 +01:00
}
} //namespace redis