1
0
Fork 0
mirror of https://github.com/KingDuckZ/incredis synced 2025-08-07 12:59:46 +00:00

Move public headers to the include/incredis dir.

This commit is contained in:
King_DuckZ 2016-07-14 18:13:03 +01:00
parent 6aad99c0f4
commit 8860dc0620
11 changed files with 1 additions and 0 deletions

View file

@ -0,0 +1,91 @@
/* Copyright 2016, Michele Santullo
* This file is part of "incredis".
*
* "incredis" 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.
*
* "incredis" 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 "incredis". If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef id9348909738B047B7B6912D73CB519039
#define id9348909738B047B7B6912D73CB519039
#include "duckhandy/compatibility.h"
#include <cstddef>
#include <boost/utility/string_ref.hpp>
#include <string>
namespace redis {
namespace implem {
template <typename T>
const char* arg_to_bin_safe_char ( const T& parArg );
template <typename T>
std::size_t arg_to_bin_safe_length ( const T& parArg ) a_pure;
template <typename T>
struct MakeCharInfo;
template<>
struct MakeCharInfo<std::string> {
MakeCharInfo ( const std::string& parData ) : m_string(parData) {}
const char* data ( void ) const { return m_string.data(); }
std::size_t size ( void ) const { return m_string.size(); }
private:
const std::string& m_string;
};
template<>
struct MakeCharInfo<boost::string_ref> {
MakeCharInfo ( const boost::string_ref& parData ) : m_data(parData.data()), m_size(parData.size()) {}
const char* data ( void ) const { return m_data; }
std::size_t size ( void ) const { return m_size; }
private:
const char* const m_data;
const std::size_t m_size;
};
template<>
struct MakeCharInfo<char> {
MakeCharInfo ( char parData ) : m_data(parData) {}
const char* data ( void ) const { return &m_data; }
std::size_t size ( void ) const { return 1; }
private:
const char m_data;
};
template <std::size_t N>
struct MakeCharInfo<char[N]> {
static_assert(N > 0, "Given input should have at least one character as it's assumed to be a null-terminated string");
MakeCharInfo ( const char (&parData)[N] ) : m_data(parData, N - 1) {}
const char* data ( void ) const { return m_data.data(); }
std::size_t size ( void ) const { return m_data.size(); }
private:
boost::string_ref m_data;
};
template <typename T>
inline const char* arg_to_bin_safe_char (const T& parArg) {
return MakeCharInfo<T>(parArg).data();
}
template <typename T>
inline std::size_t arg_to_bin_safe_length (const T& parArg) {
return MakeCharInfo<T>(parArg).size();
}
} //namespace implem
} //namespace redis
#endif

View file

@ -0,0 +1,96 @@
/* Copyright 2016, Michele Santullo
* This file is part of "incredis".
*
* "incredis" 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.
*
* "incredis" 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 "incredis". If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef idD81C81D99196491A8C9B68DED8ADD260
#define idD81C81D99196491A8C9B68DED8ADD260
#include "reply.hpp"
#include "arg_to_bin_safe.hpp"
#include <vector>
#include <memory>
namespace std {
template <class R> class future;
template <class T> struct atomic;
} //namespace std
namespace redis {
class Command;
class AsyncConnection;
class Batch {
friend class Command;
public:
Batch ( Batch&& parOther );
Batch ( const Batch& ) = delete;
~Batch ( void ) noexcept;
const std::vector<Reply>& replies ( void );
bool replies_requested ( void ) const;
void throw_if_failed ( void );
template <typename... Args>
Batch& run ( const char* parCommand, Args&&... parArgs );
template <typename... Args>
Batch& operator() ( const char* parCommand, Args&&... parArgs );
void reset ( void ) noexcept;
private:
struct LocalData;
explicit Batch ( AsyncConnection* parConn, std::atomic<std::size_t>& parPendingFutures );
void run_pvt ( int parArgc, const char** parArgv, std::size_t* parLengths );
std::vector<std::future<Reply>> m_futures;
std::vector<Reply> m_replies;
std::unique_ptr<LocalData> m_local_data;
AsyncConnection* m_async_conn;
};
class RedisError : public std::runtime_error {
public:
RedisError ( const char* parMessage, std::size_t parLength );
};
template <typename... Args>
Batch& Batch::run (const char* parCommand, Args&&... parArgs) {
constexpr const std::size_t arg_count = sizeof...(Args) + 1;
using CharPointerArray = std::array<const char*, arg_count>;
using LengthArray = std::array<std::size_t, arg_count>;
using implem::arg_to_bin_safe_char;
using implem::arg_to_bin_safe_length;
using implem::MakeCharInfo;
using boost::string_ref;
this->run_pvt(
static_cast<int>(arg_count),
CharPointerArray{ (arg_to_bin_safe_char(string_ref(parCommand))), MakeCharInfo<typename std::remove_const<typename std::remove_reference<Args>::type>::type>(std::forward<Args>(parArgs)).data()... }.data(),
LengthArray{ arg_to_bin_safe_length(string_ref(parCommand)), arg_to_bin_safe_length(std::forward<Args>(parArgs))... }.data()
);
return *this;
}
template <typename... Args>
Batch& Batch::operator() (const char* parCommand, Args&&... parArgs) {
return this->run(parCommand, std::forward<Args>(parArgs)...);
}
} //namespace redis
#endif

View file

@ -0,0 +1,85 @@
/* Copyright 2016, Michele Santullo
* This file is part of "incredis".
*
* "incredis" 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.
*
* "incredis" 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 "incredis". If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef idD83EEBFC927840C6B9F32D61A1D1E582
#define idD83EEBFC927840C6B9F32D61A1D1E582
#include "reply.hpp"
#include "batch.hpp"
#include "script.hpp"
#include <array>
#include <string>
#include <cstdint>
#include <cstddef>
#include <cassert>
#include <vector>
#include <utility>
#include <boost/utility/string_ref.hpp>
#include <boost/range/iterator_range_core.hpp>
#include <ciso646>
#include <stdexcept>
namespace redis {
class Command {
public:
Command ( std::string&& parAddress, uint16_t parPort );
explicit Command ( std::string&& parSocket );
~Command ( void ) noexcept;
void connect ( void );
void wait_for_connect ( void );
void disconnect ( void );
void wait_for_disconnect ( void );
bool is_connected ( void ) const;
boost::string_ref connection_error ( void ) const;
Batch make_batch ( void );
Script make_script ( const std::string& parScript );
template <typename... Args>
Reply run ( const char* parCommand, Args&&... parArgs );
private:
struct LocalData;
std::unique_ptr<LocalData> m_local_data;
};
template <typename... Args>
Reply Command::run (const char* parCommand, Args&&... parArgs) {
auto batch = make_batch();
batch.run(parCommand, std::forward<Args>(parArgs)...);
batch.throw_if_failed();
return batch.replies().front();
}
template <typename T>
struct StructAdapt;
template <typename AS, typename I>
inline AS range_as (const boost::iterator_range<I>& parRange) {
assert(not boost::empty(parRange));
AS retval;
const auto success = StructAdapt<AS>::decode(parRange, retval);
if (not success)
throw std::runtime_error("Error decoding range");
return retval;
};
} //namespace redis
#endif

View file

@ -0,0 +1,98 @@
/* Copyright 2016, Michele Santullo
* This file is part of "incredis".
*
* "incredis" 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.
*
* "incredis" 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 "incredis". If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef id7D338900114548A890B1EECE0C4D3C4C
#define id7D338900114548A890B1EECE0C4D3C4C
#include "command.hpp"
#include "incredis_batch.hpp"
#include "scan_iterator.hpp"
#include <boost/optional.hpp>
#include <string>
#include <boost/utility/string_ref.hpp>
#include <vector>
#include <boost/range/iterator_range_core.hpp>
#include <boost/range/empty.hpp>
#include <utility>
namespace redis {
class IncRedis {
public:
typedef ScanIterator<ScanSingleValues<std::string>> scan_iterator;
typedef boost::iterator_range<scan_iterator> scan_range;
typedef ScanIterator<ScanPairs<std::pair<std::string, std::string>, ScanCommands::HSCAN>> hscan_iterator;
typedef boost::iterator_range<hscan_iterator> hscan_range;
typedef ScanIterator<ScanSingleValuesInKey<std::string>> sscan_iterator;
typedef boost::iterator_range<sscan_iterator> sscan_range;
typedef ScanIterator<ScanPairs<std::pair<std::string, std::string>, ScanCommands::ZSCAN>> zscan_iterator;
typedef boost::iterator_range<zscan_iterator> zscan_range;
typedef boost::optional<std::string> opt_string;
typedef boost::optional<std::vector<opt_string>> opt_string_list;
IncRedis ( std::string&& parAddress, uint16_t parPort );
explicit IncRedis ( std::string&& parSocket );
~IncRedis ( void ) noexcept = default;
void connect ( void );
void wait_for_connect ( void );
void disconnect ( void );
void wait_for_disconnect ( void );
bool is_connected ( void ) const { return m_command.is_connected(); }
IncRedisBatch make_batch ( void );
Command& command ( void ) { return m_command; }
const Command& command ( void ) const { return m_command; }
//Scan
scan_range scan ( boost::string_ref parPattern=boost::string_ref() );
hscan_range hscan ( boost::string_ref parKey, boost::string_ref parPattern=boost::string_ref() );
sscan_range sscan ( boost::string_ref parKey, boost::string_ref parPattern=boost::string_ref() );
zscan_range zscan ( boost::string_ref parKey, boost::string_ref parPattern=boost::string_ref() );
//Hash
opt_string hget ( boost::string_ref parKey, boost::string_ref parField );
template <typename... Args>
opt_string_list hmget ( boost::string_ref parKey, Args&&... parArgs );
int hincrby ( boost::string_ref parKey, boost::string_ref parField, int parInc );
//Set
opt_string_list srandmember ( boost::string_ref parKey, int parCount );
opt_string srandmember ( boost::string_ref parKey );
opt_string_list smembers ( boost::string_ref parKey );
//Sorted set
opt_string_list zrangebyscore ( boost::string_ref parKey, double parMin, bool parMinIncl, double parMax, bool parMaxIncl, bool parWithScores );
//Script
bool script_flush ( void );
private:
static opt_string_list reply_to_string_list ( const Reply& parReply );
Command m_command;
};
template <typename... Args>
auto IncRedis::hmget (boost::string_ref parKey, Args&&... parArgs) -> opt_string_list {
static_assert(sizeof...(Args) > 0, "No fields specified");
return reply_to_string_list(m_command.run("HMGET", parKey, std::forward<Args>(parArgs)...));
}
} //namespace redis
#endif

View file

@ -0,0 +1,173 @@
/* Copyright 2016, Michele Santullo
* This file is part of "incredis".
*
* "incredis" 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.
*
* "incredis" 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 "incredis". If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef id3C772A92AB0E440DA84DAFD807BC962D
#define id3C772A92AB0E440DA84DAFD807BC962D
#include "batch.hpp"
#include "duckhandy/sequence_bt.hpp"
#include <boost/utility/string_ref.hpp>
#include <boost/lexical_cast.hpp>
#include <type_traits>
namespace redis {
class IncRedisBatch {
public:
enum ZADD_Mode {
ZADD_XX_UpdateOnly,
ZADD_NX_AlwaysAdd,
ZADD_None
};
IncRedisBatch ( void ) = delete;
IncRedisBatch ( IncRedisBatch&& ) = default;
IncRedisBatch ( const Batch& ) = delete;
IncRedisBatch ( Batch&& parBatch );
void reset ( void );
void throw_if_failed ( void );
const std::vector<Reply>& replies ( void ) { return m_batch.replies(); }
Batch& batch ( void ) { return m_batch; }
const Batch& batch ( void ) const { return m_batch; }
//Misc
IncRedisBatch& select ( int parIndex );
IncRedisBatch& client_setname ( boost::string_ref parName );
template <typename... Args>
IncRedisBatch& del ( Args&&... parArgs );
//Hash
IncRedisBatch& hget ( boost::string_ref parKey, boost::string_ref parField );
template <typename... Args>
IncRedisBatch& hmget ( boost::string_ref parKey, Args&&... parArgs );
template <typename... Args>
IncRedisBatch& hmset ( boost::string_ref parKey, Args&&... parArgs );
IncRedisBatch& hincrby ( boost::string_ref parKey, boost::string_ref parField, int parInc );
//Set
IncRedisBatch& srandmember ( boost::string_ref parKey, int parCount );
IncRedisBatch& srandmember ( boost::string_ref parKey );
template <typename... Args>
IncRedisBatch& sadd ( boost::string_ref parKey, Args&&... parArgs );
//Sorted set
template <typename... Args>
IncRedisBatch& zadd ( boost::string_ref parKey, ZADD_Mode parMode, bool parChange, Args&&... parArgs );
IncRedisBatch& zrangebyscore ( boost::string_ref parKey, double parMin, bool parMinIncl, double parMax, bool parMaxIncl, bool parWithScores );
//Script
IncRedisBatch& script_flush ( void );
private:
Batch m_batch;
};
namespace implem {
template <std::size_t... I, typename... Args>
void run_conv_floats_to_strings ( Batch& parBatch, dhandy::bt::index_seq<I...>, Args&&... parArgs );
} //namespace implem
template <typename... Args>
IncRedisBatch& IncRedisBatch::hmget (boost::string_ref parKey, Args&&... parArgs) {
static_assert(sizeof...(Args) > 0, "No fields specified");
m_batch.run("HMGET", parKey, std::forward<Args>(parArgs)...);
return *this;
}
template <typename... Args>
IncRedisBatch& IncRedisBatch::hmset (boost::string_ref parKey, Args&&... parArgs) {
static_assert(sizeof...(Args) >= 1, "No parameters specified");
static_assert(sizeof...(Args) % 2 == 0, "Uneven number of parameters received");
m_batch.run("HMSET", parKey, std::forward<Args>(parArgs)...);
return *this;
}
template <typename... Args>
IncRedisBatch& IncRedisBatch::sadd (boost::string_ref parKey, Args&&... parArgs) {
static_assert(sizeof...(Args) > 0, "No members specified");
m_batch.run("SADD", parKey, std::forward<Args>(parArgs)...);
return *this;
}
template <typename... Args>
IncRedisBatch& IncRedisBatch::del (Args&&... parArgs) {
static_assert(sizeof...(Args) > 0, "No keys specified");
m_batch.run("DEL", std::forward<Args>(parArgs)...);
return *this;
}
template <typename... Args>
IncRedisBatch& IncRedisBatch::zadd (boost::string_ref parKey, ZADD_Mode parMode, bool parChange, Args&&... parArgs) {
static_assert(sizeof...(Args) >= 1, "No score/value pairs specified");
static_assert(sizeof...(Args) % 2 == 0, "Uneven number of parameters received");
using dhandy::bt::index_range;
if (parChange) {
if (ZADD_None == parMode)
implem::run_conv_floats_to_strings(m_batch, index_range<0, sizeof...(Args)>(), "ZADD", parKey, "CH", std::forward<Args>(parArgs)...);
else if (ZADD_NX_AlwaysAdd == parMode)
implem::run_conv_floats_to_strings(m_batch, index_range<0, sizeof...(Args)>(), "ZADD", parKey, "NX", "CH", std::forward<Args>(parArgs)...);
else if (ZADD_XX_UpdateOnly == parMode)
implem::run_conv_floats_to_strings(m_batch, index_range<0, sizeof...(Args)>(), "ZADD", parKey, "XX", "CH", std::forward<Args>(parArgs)...);
}
else {
if (ZADD_None == parMode)
implem::run_conv_floats_to_strings(m_batch, index_range<0, sizeof...(Args)>(), "ZADD", parKey, std::forward<Args>(parArgs)...);
else if (ZADD_NX_AlwaysAdd == parMode)
implem::run_conv_floats_to_strings(m_batch, index_range<0, sizeof...(Args)>(), "ZADD", parKey, "NX", std::forward<Args>(parArgs)...);
else if (ZADD_XX_UpdateOnly == parMode)
implem::run_conv_floats_to_strings(m_batch, index_range<0, sizeof...(Args)>(), "ZADD", parKey, "XX", std::forward<Args>(parArgs)...);
}
return *this;
}
namespace implem {
template <std::size_t IGNORE_COUNT, std::size_t IDX, typename T, bool STRINGIZE=(IDX>=IGNORE_COUNT) && ((IDX-IGNORE_COUNT)%2)==0>
struct stringize_or_forward_impl {
typedef T type;
static T&& do_it ( T&& parT ) { return std::forward<T>(parT); }
};
template <std::size_t IGNORE_COUNT, std::size_t IDX, typename T>
struct stringize_or_forward_impl<IGNORE_COUNT, IDX, T, true> {
static_assert(std::is_floating_point<T>::value, "Scores must be given as floating point values");
typedef std::string type;
static std::string do_it ( T parT ) { return boost::lexical_cast<std::string>(parT); }
};
template <std::size_t IGNORE_COUNT, std::size_t IDX, typename T>
auto stringize_or_forward (T&& parValue) -> typename stringize_or_forward_impl<IGNORE_COUNT, IDX, T>::type {
return stringize_or_forward_impl<IGNORE_COUNT, IDX, T>::do_it(std::forward<T>(parValue));
}
template <std::size_t PreArgsCount, std::size_t... I, typename... Args>
void run_conv_floats_to_strings_impl (Batch& parBatch, dhandy::bt::index_seq<I...>, Args&&... parArgs) {
static_assert(sizeof...(I) == sizeof...(Args), "Wrong number of indices");
static_assert(PreArgsCount <= sizeof...(I), "Can't ignore more arguments than those that were received");
parBatch.run(stringize_or_forward<PreArgsCount, I>(std::forward<Args>(parArgs))...);
}
template <std::size_t... I, typename... Args>
void run_conv_floats_to_strings (Batch& parBatch, dhandy::bt::index_seq<I...>, Args&&... parArgs) {
static_assert(sizeof...(Args) >= sizeof...(I), "Unexpected count, there should be at least as many argument as there are indices");
constexpr const auto pre_args_count = sizeof...(Args) - sizeof...(I);
run_conv_floats_to_strings_impl<pre_args_count>(parBatch, dhandy::bt::index_range<0, sizeof...(Args)>(), std::forward<Args>(parArgs)...);
};
} //namespace implem
} //namespace redis
#endif

View file

@ -0,0 +1,92 @@
/* Copyright 2016, Michele Santullo
* This file is part of "incredis".
*
* "incredis" 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.
*
* "incredis" 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 "incredis". If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef id93FA96E3071745D9A1E45D4D29B9F7D0
#define id93FA96E3071745D9A1E45D4D29B9F7D0
#include <boost/variant/variant.hpp>
#include <string>
#include <vector>
namespace redis {
struct Reply;
class ErrorString {
public:
ErrorString ( const char* parCStr, std::size_t parLen ) :
m_msg(parCStr, parLen)
{ }
const std::string& message ( void ) const noexcept { return m_msg; }
private:
std::string m_msg;
};
class StatusString {
public:
StatusString ( const char* parCStr, std::size_t parLen ) :
m_msg(parCStr, parLen)
{ }
const std::string& message ( void ) const noexcept { return m_msg; }
bool is_ok ( void ) const { return "OK" == m_msg; }
private:
std::string m_msg;
};
namespace implem {
using RedisVariantType = boost::variant<
long long,
std::string,
std::vector<Reply>,
ErrorString,
StatusString,
std::nullptr_t
>;
} //namespace implem
enum RedisVariantTypes {
RedisVariantType_Integer = 0,
RedisVariantType_String,
RedisVariantType_Array,
RedisVariantType_Error,
RedisVariantType_Status,
RedisVariantType_Nil
};
struct Reply : implem::RedisVariantType {
using base_class = implem::RedisVariantType;
Reply ( void ) = default;
Reply ( long long parVal ) : base_class(parVal) {}
Reply ( std::string&& parVal ) : base_class(std::move(parVal)) {}
Reply ( std::vector<Reply>&& parVal ) : base_class(std::move(parVal)) {}
Reply ( ErrorString&& parVal ) : base_class(std::move(parVal)) {}
Reply ( StatusString&& parVal ) : base_class(std::move(parVal)) {}
Reply ( std::nullptr_t parVal ) : base_class(parVal) {}
~Reply ( void ) noexcept = default;
};
const long long& get_integer ( const Reply& parReply );
long long get_integer_autoconv_if_str ( const Reply& parReply );
const std::string& get_string ( const Reply& parReply );
const std::vector<Reply>& get_array ( const Reply& parReply );
const ErrorString& get_error_string ( const Reply& parReply );
template <typename T>
const T& get ( const Reply& parReply );
} //namespace redis
#endif

View file

@ -0,0 +1,144 @@
/* Copyright 2016, Michele Santullo
* This file is part of "incredis".
*
* "incredis" 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.
*
* "incredis" 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 "incredis". If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef id774125B851514A26BD7C2AD1D804D732
#define id774125B851514A26BD7C2AD1D804D732
#include "reply.hpp"
#include "duckhandy/has_method.hpp"
#include "enum.h"
#include <boost/iterator/iterator_facade.hpp>
#include <type_traits>
#include <vector>
#include <cstddef>
#include <boost/utility/string_ref.hpp>
namespace redis {
template <typename ValueFetch>
class ScanIterator;
class Command;
namespace implem {
template <typename ValueFetch>
using ScanIteratorBaseIterator = boost::iterator_facade<ScanIterator<ValueFetch>, const typename ValueFetch::value_type, boost::forward_traversal_tag>;
class ScanIteratorBaseClass {
protected:
explicit ScanIteratorBaseClass ( Command* parCommand );
ScanIteratorBaseClass ( Command* parCommand, boost::string_ref parMatchPattern );
~ScanIteratorBaseClass ( void ) noexcept = default;
bool is_connected ( void ) const;
Reply run ( const char* parCommand, long long parScanContext, std::size_t parCount );
Reply run ( const char* parCommand, const boost::string_ref& parParameter, long long parScanContext, std::size_t parCount );
bool is_equal ( const ScanIteratorBaseClass& parOther ) const { return m_command == parOther.m_command; }
private:
Command* m_command;
boost::string_ref m_match_pattern;
};
} //namespace implem
BETTER_ENUM(ScanCommands, char,
SCAN, SSCAN, ZSCAN, HSCAN
);
template <typename ValueFetch>
class ScanIterator : private implem::ScanIteratorBaseClass, public implem::ScanIteratorBaseIterator<ValueFetch>, private ValueFetch {
friend class boost::iterator_core_access;
typedef implem::ScanIteratorBaseIterator<ValueFetch> base_iterator;
define_has_method(scan_target, ScanTarget);
public:
typedef typename base_iterator::difference_type difference_type;
typedef typename base_iterator::value_type value_type;
typedef typename base_iterator::pointer pointer;
typedef typename base_iterator::reference reference;
typedef typename base_iterator::iterator_category iterator_category;
template <typename Dummy=ValueFetch, typename=typename std::enable_if<not HasScanTargetMethod<Dummy>::value>::type>
ScanIterator ( Command* parCommand, bool parEnd, boost::string_ref parMatchPattern=boost::string_ref() );
template <typename Dummy=ValueFetch, typename=typename std::enable_if<HasScanTargetMethod<Dummy>::value>::type>
ScanIterator ( Command* parCommand, boost::string_ref parKey, bool parEnd, boost::string_ref parMatchPattern=boost::string_ref() );
private:
template <typename T>
Reply forward_scan_command ( typename std::enable_if<HasScanTargetMethod<T>::value, long long>::type parContext );
template <typename T>
Reply forward_scan_command ( typename std::enable_if<not HasScanTargetMethod<T>::value, long long>::type parContext );
bool is_end ( void ) const;
void increment ( void );
bool equal ( const ScanIterator& parOther ) const;
const value_type& dereference ( void ) const;
std::vector<value_type> m_reply;
long long m_scan_context;
std::size_t m_curr_index;
};
template <typename T>
struct ScanSingleValues {
typedef T value_type;
static constexpr const char* command ( void ) { return "SCAN"; }
static constexpr const std::size_t step = 1;
static constexpr const std::size_t work_count = 10;
static const T& make_value ( const Reply* parItem );
};
template <typename T>
struct ScanSingleValuesInKey {
typedef T value_type;
explicit ScanSingleValuesInKey ( boost::string_ref parScanTarget ) : m_scan_target(parScanTarget) {}
static constexpr const char* command ( void ) { return "SSCAN"; }
static constexpr const std::size_t step = 1;
static constexpr const std::size_t work_count = 10;
static const T& make_value ( const Reply* parItem );
boost::string_ref scan_target ( void ) const { return m_scan_target; }
private:
boost::string_ref m_scan_target;
};
template <typename P, char Command, typename A=decltype(P().first), typename B=decltype(P().second)>
struct ScanPairs {
static_assert(Command == ScanCommands::HSCAN or Command == ScanCommands::ZSCAN, "Invalid scan command chosen");
typedef P value_type;
explicit ScanPairs ( boost::string_ref parScanTarget ) : m_scan_target(parScanTarget) {}
static constexpr const char* command ( void ) { return ScanCommands::_from_integral(Command)._to_string(); }
static constexpr const std::size_t step = 2;
static constexpr const std::size_t work_count = 10;
static value_type make_value ( const Reply* parItem );
boost::string_ref scan_target ( void ) const { return m_scan_target; }
private:
boost::string_ref m_scan_target;
};
} //namespace redis
#include "scan_iterator.inl"
#endif

View file

@ -0,0 +1,161 @@
/* Copyright 2016, Michele Santullo
* This file is part of "incredis".
*
* "incredis" 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.
*
* "incredis" 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 "incredis". If not, see <http://www.gnu.org/licenses/>.
*/
#include "command.hpp"
#include <cassert>
#include <ciso646>
namespace redis {
namespace implem {
} //namespace implem
template <typename ValueFetch>
template <typename Dummy, typename>
ScanIterator<ValueFetch>::ScanIterator (Command* parCommand, bool parEnd, boost::string_ref parMatchPattern) :
implem::ScanIteratorBaseClass(parCommand, parMatchPattern),
implem::ScanIteratorBaseIterator<ValueFetch>(),
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>
template <typename Dummy, typename>
ScanIterator<ValueFetch>::ScanIterator (Command* parCommand, boost::string_ref parKey, bool parEnd, boost::string_ref parMatchPattern) :
implem::ScanIteratorBaseClass(parCommand, parMatchPattern),
implem::ScanIteratorBaseIterator<ValueFetch>(),
ValueFetch(parKey),
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>
bool ScanIterator<ValueFetch>::is_end() const {
return not m_curr_index and m_reply.empty() and not m_scan_context;
}
template <typename ValueFetch>
void ScanIterator<ValueFetch>::increment() {
assert(not is_end());
static_assert(ValueFetch::step > 0, "Can't have an increase step of 0");
if (m_curr_index + 1 < m_reply.size()) {
++m_curr_index;
}
else if (m_curr_index + 1 == m_reply.size() and not m_scan_context) {
m_reply.clear();
m_curr_index = 0;
}
else {
std::vector<Reply> array_reply;
long long new_context = m_scan_context;
do {
auto whole_reply = this->forward_scan_command<ValueFetch>(new_context);
array_reply = get_array(whole_reply);
assert(2 == array_reply.size());
assert(array_reply.size() % ValueFetch::step == 0);
new_context = get_integer_autoconv_if_str(array_reply[0]);
} while (new_context and get_array(array_reply[1]).empty());
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
(is_end() and parOther.is_end()) or
(
not (is_end() or parOther.is_end()) and
implem::ScanIteratorBaseClass::is_equal(parOther) and
(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());
return m_reply[m_curr_index];
}
template <typename ValueFetch>
template <typename T>
Reply ScanIterator<ValueFetch>::forward_scan_command (typename std::enable_if<HasScanTargetMethod<T>::value, long long>::type parContext) {
return implem::ScanIteratorBaseClass::run(T::command(), T::scan_target(), parContext, T::work_count);
}
template <typename ValueFetch>
template <typename T>
Reply ScanIterator<ValueFetch>::forward_scan_command (typename std::enable_if<not HasScanTargetMethod<T>::value, long long>::type parContext) {
return implem::ScanIteratorBaseClass::run(T::command(), parContext, T::work_count);
}
template <typename T>
auto ScanSingleValues<T>::make_value (const Reply* parItem) -> const value_type& {
assert(parItem);
return get<T>(*parItem);
}
template <typename T>
auto ScanSingleValuesInKey<T>::make_value (const Reply* parItem) -> const value_type& {
assert(parItem);
return get<T>(*parItem);
}
template <typename P, char Command, typename A, typename B>
auto ScanPairs<P, Command, A, B>::make_value (const Reply* parItem) -> value_type {
assert(parItem);
return value_type(get<A>(parItem[0]), get<B>(parItem[1]));
}
} //namespace redis

View file

@ -0,0 +1,83 @@
/* Copyright 2016, Michele Santullo
* This file is part of "incredis".
*
* "incredis" 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.
*
* "incredis" 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 "incredis". If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef id5B30CDA57F894CD6888093B64F9433DA
#define id5B30CDA57F894CD6888093B64F9433DA
#include "batch.hpp"
#include "duckhandy/lexical_cast.hpp"
#include "duckhandy/sequence_bt.hpp"
#include <boost/utility/string_ref.hpp>
#include <tuple>
#include <cassert>
#include <ciso646>
namespace redis {
class ScriptManager;
class Script {
public:
Script ( void );
Script ( Script&& ) = default;
Script ( boost::string_ref parSha1, ScriptManager& parManager );
~Script ( void ) noexcept = default;
template <typename... Keys, typename... Values>
void run ( Batch& parBatch, const std::tuple<Keys...>& parKeys, const std::tuple<Values...>& parValues );
Script& operator= ( Script&& ) = default;
private:
template <typename... Keys, typename... Values, std::size_t... KeyIndices, std::size_t... ValueIndices>
void run_with_indices ( Batch& parBatch, const std::tuple<Keys...>& parKeys, const std::tuple<Values...>& parValues, dhandy::bt::index_seq<KeyIndices...>, dhandy::bt::index_seq<ValueIndices...> );
boost::string_ref m_sha1;
ScriptManager* m_manager;
};
template <typename... Keys, typename... Values>
void Script::run (Batch& parBatch, const std::tuple<Keys...>& parKeys, const std::tuple<Values...>& parValues) {
this->run_with_indices(
parBatch,
parKeys,
parValues,
::dhandy::bt::index_range<0, sizeof...(Keys)>(),
::dhandy::bt::index_range<0, sizeof...(Values)>()
);
}
template <typename... Keys, typename... Values, std::size_t... KeyIndices, std::size_t... ValueIndices>
void Script::run_with_indices (Batch& parBatch, const std::tuple<Keys...>& parKeys, const std::tuple<Values...>& parValues, dhandy::bt::index_seq<KeyIndices...>, dhandy::bt::index_seq<ValueIndices...>) {
static_assert(sizeof...(Keys) == sizeof...(KeyIndices), "Wrong index count");
static_assert(sizeof...(Values) == sizeof...(ValueIndices), "Wrong value count");
static_assert(sizeof...(Keys) == std::tuple_size<std::tuple<Keys...>>::value, "Wrong key count");
static_assert(sizeof...(Values) == std::tuple_size<std::tuple<Values...>>::value, "Wrong value count");
assert(not m_sha1.empty());
assert(m_manager);
parBatch.run(
"EVALSHA",
m_sha1,
dhandy::lexical_cast<std::string>(sizeof...(Keys)),
std::get<KeyIndices>(parKeys)...,
std::get<ValueIndices>(parValues)...
);
}
} //namespace redis
#endif

View file

@ -0,0 +1,62 @@
/* Copyright 2016, Michele Santullo
* This file is part of "incredis".
*
* "incredis" 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.
*
* "incredis" 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 "incredis". If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef id8E124FF76DF449CDB8FBA806F8EF4E78
#define id8E124FF76DF449CDB8FBA806F8EF4E78
#include "incredisConfig.h"
#if defined(WITH_CRYPTOPP)
# define MAKE_SHA1_WITH_CRYPTOPP
#endif
#include <boost/utility/string_ref.hpp>
#if defined(MAKE_SHA1_WITH_CRYPTOPP)
# include <set>
#else
# include <map>
#endif
#include <string>
#include <array>
#include <boost/utility/string_ref.hpp>
namespace redis {
class Command;
class ScriptManager {
public:
explicit ScriptManager ( Command* parCommand );
boost::string_ref submit_lua_script ( const std::string& parScript );
private:
using Sha1Array = std::array<char, 40>;
boost::string_ref add_lua_script_ifn ( const std::string& parScript );
Command* const m_command;
#if defined(MAKE_SHA1_WITH_CRYPTOPP)
std::set<Sha1Array> m_known_hashes;
#else
std::map<std::string, Sha1Array> m_known_scripts;
#endif
};
inline boost::string_ref ScriptManager::submit_lua_script (const std::string& parScript) {
return add_lua_script_ifn(parScript);
}
} //namespace redis
#endif