1
0
Fork 0
mirror of https://github.com/KingDuckZ/dindexer.git synced 2024-11-25 00:53:43 +00:00

Remove incredis and import it as a submodule.

This commit is contained in:
King_DuckZ 2016-07-14 17:30:45 +01:00
parent 7a3438def6
commit 2f6c4f3dab
29 changed files with 6 additions and 2543 deletions

3
.gitmodules vendored
View file

@ -13,3 +13,6 @@
[submodule "lib/duckhandy"]
path = lib/duckhandy
url = https://github.com/KingDuckZ/duckhandy.git
[submodule "lib/incredis"]
path = lib/incredis
url = ../incredis.git

View file

@ -107,7 +107,6 @@ target_compile_features(${PROJECT_NAME}
target_include_directories(${bare_name}-inc
INTERFACE ${PROJECT_BINARY_DIR}
INTERFACE ${CMAKE_SOURCE_DIR}/include
INTERFACE ${CMAKE_SOURCE_DIR}/lib/sprout
)
@ -132,6 +131,7 @@ add_subdirectory(lib/glob2regex)
add_subdirectory(src/backends)
add_subdirectory(src/core)
add_shared_git_project(lib/duckhandy)
add_shared_git_project(lib/incredis EXCLUDE_FROM_ALL)
#Actions
add_subdirectory(src/main)

View file

@ -1,136 +0,0 @@
# Module for locating the Crypto++ encryption library.
#
# Customizable variables:
# CryptoPP_ROOT_DIR
# This variable points to the CryptoPP root directory. On Windows the
# library location typically will have to be provided explicitly using the
# -D command-line option. The directory should include the include/cryptopp,
# lib and/or bin sub-directories.
#
# Read-only variables:
# CryptoPP_FOUND
# Indicates whether the library has been found.
#
# CryptoPP_INCLUDE_DIRS
# Points to the CryptoPP include directory.
#
# CryptoPP_LIBRARIES
# Points to the CryptoPP libraries that should be passed to
# target_link_libararies.
#
#
# Copyright (c) 2012 Sergiu Dotenco
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
INCLUDE (FindPackageHandleStandardArgs)
FIND_PATH (
CryptoPP_ROOT_DIR
NAMES cryptopp/cryptlib.h include/cryptopp/cryptlib.h
PATHS ENV CRYPTOPPROOT
DOC "CryptoPP root directory"
)
# Re-use the previous path:
FIND_PATH (
CRYPTOPP_INCLUDE_DIR
NAMES cryptopp/cryptlib.h
HINTS ${CryptoPP_ROOT_DIR}
PATH_SUFFIXES include
DOC "CryptoPP include directory"
)
FIND_LIBRARY (
CRYPTOPP_LIBRARY_DEBUG
NAMES cryptlibd cryptoppd
HINTS ${CryptoPP_ROOT_DIR}
PATH_SUFFIXES lib
DOC "CryptoPP debug library"
)
FIND_LIBRARY (
CRYPTOPP_LIBRARY_RELEASE
NAMES cryptlib cryptopp
HINTS ${CryptoPP_ROOT_DIR}
PATH_SUFFIXES lib
DOC "CryptoPP release library"
)
IF (CRYPTOPP_LIBRARY_DEBUG AND CRYPTOPP_LIBRARY_RELEASE)
SET (
CRYPTOPP_LIBRARY
optimized ${CRYPTOPP_LIBRARY_RELEASE}
debug ${CRYPTOPP_LIBRARY_DEBUG} CACHE DOC "CryptoPP library"
)
ELSEIF (CRYPTOPP_LIBRARY_RELEASE)
SET (
CRYPTOPP_LIBRARY ${CRYPTOPP_LIBRARY_RELEASE} CACHE DOC
"CryptoPP library"
)
ENDIF (CRYPTOPP_LIBRARY_DEBUG AND CRYPTOPP_LIBRARY_RELEASE)
IF (CRYPTOPP_INCLUDE_DIR)
SET (_CRYPTOPP_VERSION_HEADER ${CRYPTOPP_INCLUDE_DIR}/cryptopp/config.h)
IF (EXISTS ${_CRYPTOPP_VERSION_HEADER})
FILE (
STRINGS ${_CRYPTOPP_VERSION_HEADER} _CRYPTOPP_VERSION_TMP REGEX
"^#define CRYPTOPP_VERSION[ \t]+[0-9]+$"
)
STRING (
REGEX REPLACE
"^#define CRYPTOPP_VERSION[ \t]+([0-9]+)" "\\1" _CRYPTOPP_VERSION_TMP
${_CRYPTOPP_VERSION_TMP}
)
STRING (
REGEX REPLACE "([0-9]+)[0-9][0-9]" "\\1" CRYPTOPP_VERSION_MAJOR
${_CRYPTOPP_VERSION_TMP}
)
STRING (
REGEX REPLACE "[0-9]([0-9])[0-9]" "\\1" CRYPTOPP_VERSION_MINOR
${_CRYPTOPP_VERSION_TMP}
)
STRING (
REGEX REPLACE "[0-9][0-9]([0-9])" "\\1" CRYPTOPP_VERSION_PATCH
${_CRYPTOPP_VERSION_TMP}
)
SET (CRYPTOPP_VERSION_COUNT 3)
SET (
CRYPTOPP_VERSION
${CRYPTOPP_VERSION_MAJOR}.${CRYPTOPP_VERSION_MINOR}.${CRYPTOPP_VERSION_PATCH}
)
ENDIF (EXISTS ${_CRYPTOPP_VERSION_HEADER})
ENDIF (CRYPTOPP_INCLUDE_DIR)
SET (CryptoPP_INCLUDE_DIRS ${CRYPTOPP_INCLUDE_DIR})
SET (CryptoPP_LIBRARIES ${CRYPTOPP_LIBRARY})
MARK_AS_ADVANCED (
CRYPTOPP_INCLUDE_DIR CRYPTOPP_LIBRARY CRYPTOPP_LIBRARY_DEBUG
CRYPTOPP_LIBRARY_RELEASE
)
FIND_PACKAGE_HANDLE_STANDARD_ARGS (
CryptoPP REQUIRED_VARS CryptoPP_ROOT_DIR
CRYPTOPP_INCLUDE_DIR CRYPTOPP_LIBRARY VERSION_VAR CRYPTOPP_VERSION
)

View file

@ -1,55 +0,0 @@
#Find libhiredis
#
# HIREDIS_INCLUDE_DIRS - where to find hiredis/hiredis.h
# HIREDIS_LIBRARIES - list of libraries when using libhiredis
# HIREDIS_FOUND - true if libhiredis found
# HIREDIS_VERSION_STRING - the version of the libhiredis found
#
find_path(HIREDIS_INCLUDE_DIR NAMES hiredis/hiredis.h)
find_library(HIREDIS_LIBRARY NAMES
hiredis
)
if(HIREDIS_INCLUDE_DIR)
if(EXISTS "${HIREDIS_INCLUDE_DIR}/hiredis/hiredis.h")
set(hiredis_ver_tmp_major 0)
set(hiredis_ver_tmp_minor 0)
set(hiredis_ver_tmp_patch 0)
file(STRINGS "${HIREDIS_INCLUDE_DIR}/hiredis/hiredis.h" hiredis_version_nums REGEX "^#define[\t ]+HIREDIS_(MAJOR|MINOR|PATCH)[\t ]+[0-9]+")
foreach(hiredis_version_def IN LISTS hiredis_version_nums)
if ("${hiredis_version_def}" MATCHES "HIREDIS_MAJOR")
string(REGEX REPLACE "^#define[\t ]+HIREDIS_MAJOR[\t ]+([0-9]+).*" "\\1" hiredis_ver_tmp_major "${hiredis_version_def}")
elseif ("${hiredis_version_def}" MATCHES "HIREDIS_MINOR")
string(REGEX REPLACE "^#define[\t ]+HIREDIS_MINOR[\t ]+([0-9]+).*" "\\1" hiredis_ver_tmp_minor "${hiredis_version_def}")
elseif ("${hiredis_version_def}" MATCHES "HIREDIS_PATCH")
string(REGEX REPLACE "^#define[\t ]+HIREDIS_PATCH[\t ]+([0-9]+).*" "\\1" hiredis_ver_tmp_patch "${hiredis_version_def}")
endif()
endforeach()
unset(hiredis_version_nums)
unset(hiredis_version_def)
set(HIREDIS_VERSION_STRING "${hiredis_ver_tmp_major}.${hiredis_ver_tmp_minor}.${hiredis_ver_tmp_patch}")
unset(hiredis_ver_tmp_major)
unset(hiredis_ver_tmp_minor)
unset(hiredis_ver_tmp_patch)
endif()
endif()
include(FindPackageHandleStandardArgs)
FIND_PACKAGE_HANDLE_STANDARD_ARGS(hiredis
REQUIRED_VARS HIREDIS_LIBRARY HIREDIS_INCLUDE_DIR
VERSION_VAR HIREDIS_VERSION_STRING
)
if(HIREDIS_FOUND)
set(HIREDIS_LIBRARIES ${HIREDIS_LIBRARY})
set(HIREDIS_INCLUDE_DIRS ${HIREDIS_INCLUDE_DIR})
endif()
mark_as_advanced(
HIREDIS_ROOT_DIR
HIREDIS_INCLUDE_DIR
HIREDIS_LIBRARY
)

View file

@ -1,31 +0,0 @@
# Try to find libev
# Once done, this will define
#
# LIBEV_FOUND - system has libev
# LIBEV_INCLUDE_DIRS - libev include directories
# LIBEV_LIBRARIES - libraries needed to use libev
if(LIBEV_INCLUDE_DIRS AND LIBEV_LIBRARIES)
set(LIBEV_FIND_QUIETLY TRUE)
else()
find_path(
LIBEV_INCLUDE_DIR
NAMES ev.h
HINTS ${LIBEV_ROOT_DIR}
PATH_SUFFIXES include)
find_library(
LIBEV_LIBRARY
NAME ev
HINTS ${LIBEV_ROOT_DIR}
PATH_SUFFIXES ${CMAKE_INSTALL_LIBDIR})
set(LIBEV_INCLUDE_DIRS ${LIBEV_INCLUDE_DIR})
set(LIBEV_LIBRARIES ${LIBEV_LIBRARY})
include(FindPackageHandleStandardArgs)
find_package_handle_standard_args(
libev DEFAULT_MSG LIBEV_LIBRARY LIBEV_INCLUDE_DIR)
mark_as_advanced(LIBEV_LIBRARY LIBEV_INCLUDE_DIR)
endif()

View file

@ -26,7 +26,7 @@ function (add_shared_git_project SUBMODULE_PATH)
message(FATAL_ERROR "Couldn't make up a name for given project in \"${submod_path}\"")
endif()
string(MAKE_C_IDENTIFIER "${submod_path}" proj_name_c_id)
string(MAKE_C_IDENTIFIER "${proj_name_orig}" proj_name_c_id)
string(TOUPPER ${proj_name_c_id} proj_name)
get_property(shared_projects_list GLOBAL PROPERTY SHARED_PROJECTS_LIST)

1
lib/incredis Submodule

@ -0,0 +1 @@
Subproject commit 6aad99c0f460da5c09b719f6112ab38e4fea2ce7

View file

@ -1,74 +0,0 @@
project(incredis CXX)
find_package(hiredis 0.11.0 REQUIRED)
find_package(CryptoPP 5.6)
find_package(libev 4.0 REQUIRED)
find_package(Boost 1.53.0 REQUIRED)
add_library(${PROJECT_NAME} SHARED
command.cpp
scan_iterator.cpp
reply.cpp
batch.cpp
script.cpp
script_manager.cpp
async_connection.cpp
incredis.cpp
incredis_batch.cpp
)
target_include_directories(${PROJECT_NAME} SYSTEM
PUBLIC ${Boost_INCLUDE_DIRS}
PRIVATE ${HIREDIS_INCLUDE_DIRS}
PUBLIC ${CMAKE_SOURCE_DIR}/lib/better-enums
PRIVATE ${LIBEV_INCLUDE_DIRS}
PRIVATE ${Boost_INCLUDE_DIRS}
)
target_include_directories(${PROJECT_NAME}
PUBLIC ${CMAKE_SOURCE_DIR}/include
PRIVATE ${CMAKE_CURRENT_BINARY_DIR}
PUBLIC ${CMAKE_SOURCE_DIR}/lib/sprout
)
target_link_libraries(${PROJECT_NAME}
PRIVATE ${HIREDIS_LIBRARIES}
PRIVATE ${LIBEV_LIBRARIES}
PRIVATE ${Boost_LIBRARIES}
)
configure_file(
incredisConfig.h.in
${CMAKE_CURRENT_BINARY_DIR}/incredisConfig.h
)
target_compile_definitions(${PROJECT_NAME}
PRIVATE EV_COMPAT3=0
)
if (CryptoPP_FOUND)
target_link_libraries(${PROJECT_NAME} PRIVATE ${CryptoPP_LIBRARIES})
target_include_directories(${PROJECT_NAME} SYSTEM PRIVATE ${CryptoPP_INCLUDE_DIRS})
set (has_cryptopp_lib ON)
else()
set (has_cryptopp_lib OFF)
endif()
install(TARGETS ${PROJECT_NAME}
LIBRARY DESTINATION lib
RUNTIME DESTINATION bin
ARCHIVE DESTINATION lib/static
)
target_compile_features(${PROJECT_NAME}
PUBLIC cxx_nullptr
PUBLIC cxx_range_for
PUBLIC cxx_lambdas
PUBLIC cxx_decltype_auto
PUBLIC cxx_defaulted_functions
PUBLIC cxx_deleted_functions
PUBLIC cxx_auto_type
PUBLIC cxx_decltype_incomplete_return_types
PUBLIC cxx_defaulted_move_initializers
PUBLIC cxx_noexcept
PUBLIC cxx_rvalue_references
)

View file

@ -1,91 +0,0 @@
/* 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/>.
*/
#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

@ -1,219 +0,0 @@
/* 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 "async_connection.hpp"
#include <hiredis/async.h>
#include <hiredis/adapters/libev.h>
#include <ev.h>
#include <thread>
#include <condition_variable>
#include <atomic>
#include <mutex>
#include <signal.h>
#include <cassert>
#include <sstream>
namespace redis {
namespace {
void async_callback (ev_loop* /*parLoop*/, ev_async* /*parObject*/, int /*parRevents*/) {
}
void async_halt_loop (ev_loop* parLoop, ev_async* /*parObject*/, int /*parRevents*/) {
ev_break(parLoop, EVBREAK_ALL);
}
void lock_mutex_libev (ev_loop* parLoop) noexcept {
std::mutex* mtx = static_cast<std::mutex*>(ev_userdata(parLoop));
assert(mtx);
try {
mtx->lock();
}
catch (const std::system_error&) {
assert(false);
}
}
void unlock_mutex_libev (ev_loop* parLoop) noexcept {
std::mutex* mtx = static_cast<std::mutex*>(ev_userdata(parLoop));
assert(mtx);
mtx->unlock();
}
} //unnamed namespace
struct AsyncConnection::LocalData {
LocalData() :
redis_poll_thread(),
connect_processed(false),
disconnect_processed(true)
{
}
ev_async watcher_wakeup;
ev_async watcher_halt;
std::thread redis_poll_thread;
std::mutex hiredis_mutex;
std::mutex libev_mutex;
std::condition_variable condition_connected;
std::condition_variable condition_disconnected;
std::string connect_err_msg;
std::atomic_bool connect_processed;
std::atomic_bool disconnect_processed;
};
void on_connect (const redisAsyncContext* parContext, int parStatus) {
assert(parContext and parContext->data);
AsyncConnection& self = *static_cast<AsyncConnection*>(parContext->data);
assert(parContext == self.m_conn.get());
assert(not self.m_local_data->connect_processed);
self.m_connection_lost = false;
self.m_connected = (parStatus == REDIS_OK);
self.m_local_data->connect_processed = true;
self.m_local_data->connect_err_msg = parContext->errstr;
self.m_local_data->condition_connected.notify_one();
}
void on_disconnect (const redisAsyncContext* parContext, int parStatus) {
assert(parContext and parContext->data);
AsyncConnection& self = *static_cast<AsyncConnection*>(parContext->data);
assert(self.m_connected);
assert(not self.m_local_data->disconnect_processed);
self.m_connection_lost = (REDIS_ERR == parStatus);
self.m_connected = false;
self.m_local_data->disconnect_processed = true;
self.m_local_data->connect_err_msg.clear();
self.m_local_data->condition_disconnected.notify_one();
};
AsyncConnection::AsyncConnection(std::string&& parAddress, uint16_t parPort) :
m_conn(nullptr, &redisAsyncDisconnect),
m_local_data(new LocalData()),
m_libev_loop_thread(ev_loop_new(EVFLAG_NOINOTIFY), &ev_loop_destroy),
m_address(std::move(parAddress)),
m_port(parPort),
m_connected(false),
m_connection_lost(false)
{
//Init libev stuff
{
signal(SIGPIPE, SIG_IGN);
//See: http://pod.tst.eu/http://cvs.schmorp.de/libev/ev.pod#THREAD_LOCKING_EXAMPLE
ev_async_init(&m_local_data->watcher_wakeup, &async_callback);
ev_async_start(m_libev_loop_thread.get(), &m_local_data->watcher_wakeup);
ev_async_init(&m_local_data->watcher_halt, &async_halt_loop);
ev_async_start(m_libev_loop_thread.get(), &m_local_data->watcher_halt);
ev_set_userdata(m_libev_loop_thread.get(), &m_local_data->libev_mutex);
ev_set_loop_release_cb(m_libev_loop_thread.get(), &unlock_mutex_libev, &lock_mutex_libev);
}
}
AsyncConnection::~AsyncConnection() noexcept {
this->disconnect();
this->wait_for_disconnect();
}
void AsyncConnection::connect() {
if (not m_conn) {
m_local_data->disconnect_processed = false;
RedisConnection conn(
(is_socket_connection() ?
redisAsyncConnectUnix(m_address.c_str())
:
redisAsyncConnect(m_address.c_str(), m_port)
),
&redisAsyncDisconnect
);
if (not conn) {
std::ostringstream oss;
oss << "Unable to connect to Redis server at " << m_address << ':' << m_port;
throw std::runtime_error(oss.str());
}
else {
conn->data = this;
}
if (REDIS_OK != redisLibevAttach(m_libev_loop_thread.get(), conn.get()))
throw std::runtime_error("Unable to set event loop");
if (REDIS_OK != redisAsyncSetConnectCallback(conn.get(), &on_connect))
throw std::runtime_error("Unable to set \"on_connect()\" callback");
if (REDIS_OK != redisAsyncSetDisconnectCallback(conn.get(), &on_disconnect))
throw std::runtime_error("Unable to set \"on_disconnect()\" callback");
std::swap(conn, m_conn);
m_local_data->redis_poll_thread = std::thread([this]() {
m_local_data->libev_mutex.lock();
ev_run(m_libev_loop_thread.get(), 0);
m_local_data->libev_mutex.unlock();
});
wakeup_event_thread();
}
}
void AsyncConnection::wait_for_connect() {
if (not m_local_data->connect_processed) {
std::unique_lock<std::mutex> lk(m_local_data->hiredis_mutex);
m_local_data->condition_connected.wait(lk, [this]() { return m_local_data->connect_processed.load(); });
assert(true == m_local_data->connect_processed);
}
}
void AsyncConnection::disconnect() {
if (not m_local_data->connect_processed)
return;
assert(m_local_data->redis_poll_thread.joinable());
m_local_data->connect_processed = false;
{
std::lock_guard<std::mutex> lock(m_local_data->libev_mutex);
assert(not ev_async_pending(&m_local_data->watcher_halt));
ev_async_send(m_libev_loop_thread.get(), &m_local_data->watcher_halt);
m_conn.reset();
}
m_local_data->redis_poll_thread.join();
}
void AsyncConnection::wait_for_disconnect() {
if (not m_local_data->disconnect_processed) {
std::unique_lock<std::mutex> lk(m_local_data->hiredis_mutex);
m_local_data->condition_disconnected.wait(lk, [this]() { return m_local_data->disconnect_processed.load(); });
assert(true == m_local_data->disconnect_processed);
}
}
bool AsyncConnection::is_connected() const {
const bool connected = m_conn and not m_conn->err and m_connected;
assert(not m_connection_lost or connected);
return connected;
}
boost::string_ref AsyncConnection::connection_error() const {
return m_local_data->connect_err_msg;
}
void AsyncConnection::wakeup_event_thread() {
std::lock_guard<std::mutex> lock(m_local_data->libev_mutex);
if (ev_async_pending(&m_local_data->watcher_wakeup) == false)
ev_async_send(m_libev_loop_thread.get(), &m_local_data->watcher_wakeup);
}
std::mutex& AsyncConnection::event_mutex() {
return m_local_data->libev_mutex;
}
bool AsyncConnection::is_socket_connection() const {
return not (m_port or m_address.empty());
}
} //namespace redis

View file

@ -1,74 +0,0 @@
/* 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/>.
*/
#ifndef id982A651A9BC34F6E9BA935A804B3A0A4
#define id982A651A9BC34F6E9BA935A804B3A0A4
#include <memory>
#include <string>
#include <cstdint>
#include <boost/utility/string_ref.hpp>
struct redisAsyncContext;
struct ev_loop;
namespace std {
class mutex;
} //namespace std
namespace redis {
class AsyncConnection {
friend void on_connect ( const redisAsyncContext*, int );
friend void on_disconnect ( const redisAsyncContext*, int );
public:
AsyncConnection ( std::string&& parAddress, uint16_t parPort );
~AsyncConnection ( 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;
void wakeup_event_thread ( void );
std::mutex& event_mutex ( void );
redisAsyncContext* connection ( void );
private:
using RedisConnection = std::unique_ptr<redisAsyncContext, void(*)(redisAsyncContext*)>;
using LibevLoop = std::unique_ptr<ev_loop, void(*)(ev_loop*)>;
bool is_socket_connection ( void ) const;
struct LocalData;
RedisConnection m_conn;
std::unique_ptr<LocalData> m_local_data;
LibevLoop m_libev_loop_thread;
std::string m_address;
uint16_t m_port;
volatile bool m_connected;
volatile bool m_connection_lost;
};
inline redisAsyncContext* AsyncConnection::connection() {
return m_conn.get();
}
} //namespace redis
#endif

View file

@ -1,229 +0,0 @@
/* 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 "batch.hpp"
#include "async_connection.hpp"
#include <hiredis/hiredis.h>
#include <hiredis/async.h>
#include <cassert>
#include <future>
#include <ciso646>
#include <boost/iterator/transform_iterator.hpp>
#include <atomic>
#include <mutex>
#include <condition_variable>
#include <sstream>
//#define VERBOSE_HIREDIS_COMM
#if defined(VERBOSE_HIREDIS_COMM)
# include <iostream>
#endif
namespace redis {
namespace {
const std::size_t g_max_redis_unanswered_commands = 1000;
struct HiredisCallbackData {
HiredisCallbackData ( std::atomic_size_t& parPendingFutures, std::condition_variable& parSendCmdCond ) :
promise(),
pending_futures(parPendingFutures),
send_command_condition(parSendCmdCond)
{
}
std::promise<Reply> promise;
std::atomic_size_t& pending_futures;
std::condition_variable& send_command_condition;
};
Reply make_redis_reply_type (redisReply* parReply) {
using boost::transform_iterator;
using PtrToReplyIterator = transform_iterator<Reply(*)(redisReply*), redisReply**>;
switch (parReply->type) {
case REDIS_REPLY_INTEGER:
return parReply->integer;
case REDIS_REPLY_STRING:
return std::string(parReply->str, parReply->len);
case REDIS_REPLY_ARRAY:
return std::vector<Reply>(
PtrToReplyIterator(parReply->element, &make_redis_reply_type),
PtrToReplyIterator(parReply->element + parReply->elements, &make_redis_reply_type)
);
case REDIS_REPLY_ERROR:
return ErrorString(parReply->str, parReply->len);
case REDIS_REPLY_STATUS:
return StatusString(parReply->str, parReply->len);
case REDIS_REPLY_NIL:
return nullptr;
default:
assert(false); //not reached
return Reply();
};
}
void hiredis_run_callback (redisAsyncContext*, void* parReply, void* parPrivData) {
assert(parPrivData);
auto* data = static_cast<HiredisCallbackData*>(parPrivData);
const auto old_count = data->pending_futures.fetch_add(-1);
assert(old_count > 0);
if (old_count == g_max_redis_unanswered_commands)
data->send_command_condition.notify_one();
if (parReply) {
auto reply = make_redis_reply_type(static_cast<redisReply*>(parReply));
data->promise.set_value(std::move(reply));
}
else {
assert(false); //Should this case also be managed?
}
delete data;
}
int array_throw_if_failed (int parErrCount, int parMaxReportedErrors, const std::vector<Reply>& parReplies, std::ostream& parStream) {
int err_count = 0;
for (const auto& rep : parReplies) {
if (rep.which() == RedisVariantType_Error) {
++err_count;
if (err_count + parErrCount <= parMaxReportedErrors)
parStream << '"' << get_error_string(rep).message() << "\" ";
}
else if (rep.which() == RedisVariantType_Array) {
err_count += array_throw_if_failed(err_count + parErrCount, parMaxReportedErrors, get_array(rep), parStream);
}
}
return err_count;
}
} //unnamed namespace
struct Batch::LocalData {
explicit LocalData ( std::atomic_size_t& parPendingFutures ) :
free_cmd_slot(),
futures_mutex(),
pending_futures(parPendingFutures)
{
}
std::condition_variable free_cmd_slot;
std::mutex futures_mutex;
std::atomic_size_t& pending_futures;
};
Batch::Batch (Batch&&) = default;
Batch::Batch (AsyncConnection* parConn, std::atomic_size_t& parPendingFutures) :
m_futures(),
m_replies(),
m_local_data(new LocalData(parPendingFutures)),
m_async_conn(parConn)
{
assert(m_async_conn);
assert(m_async_conn->connection());
}
Batch::~Batch() noexcept {
if (m_local_data)
this->reset();
}
void Batch::run_pvt (int parArgc, const char** parArgv, std::size_t* parLengths) {
assert(not replies_requested());
assert(parArgc >= 1);
assert(parArgv);
assert(parLengths); //This /could/ be null, but I don't see why it should
assert(m_local_data);
const auto pending_futures = m_local_data->pending_futures.fetch_add(1);
auto* data = new HiredisCallbackData(m_local_data->pending_futures, m_local_data->free_cmd_slot);
#if defined(VERBOSE_HIREDIS_COMM)
std::cout << "run_pvt(), " << pending_futures << " items pending... ";
#endif
if (pending_futures >= g_max_redis_unanswered_commands) {
#if defined(VERBOSE_HIREDIS_COMM)
std::cout << " waiting... ";
#endif
std::unique_lock<std::mutex> u_lock(m_local_data->futures_mutex);
m_local_data->free_cmd_slot.wait(u_lock, [this]() { return m_local_data->pending_futures < g_max_redis_unanswered_commands; });
}
#if defined(VERBOSE_HIREDIS_COMM)
std::cout << " emplace_back(future)... ";
#endif
m_futures.emplace_back(data->promise.get_future());
{
std::lock_guard<std::mutex> lock(m_async_conn->event_mutex());
const int command_added = redisAsyncCommandArgv(m_async_conn->connection(), &hiredis_run_callback, data, parArgc, parArgv, parLengths);
assert(REDIS_OK == command_added); // REDIS_ERR if error
static_cast<void>(command_added);
}
#if defined(VERBOSE_HIREDIS_COMM)
std::cout << "command sent to hiredis" << std::endl;
#endif
m_async_conn->wakeup_event_thread();
}
bool Batch::replies_requested() const {
return not m_replies.empty();
}
const std::vector<Reply>& Batch::replies() {
if (not replies_requested()) {
m_replies.reserve(m_futures.size());
for (auto& fut : m_futures) {
m_replies.emplace_back(fut.get());
}
auto empty_vec = std::move(m_futures);
}
return m_replies;
}
void Batch::throw_if_failed() {
std::ostringstream oss;
const int max_reported_errors = 3;
oss << "Error in reply: ";
const int err_count = array_throw_if_failed(0, max_reported_errors, replies(), oss);
if (err_count) {
oss << " (showing " << err_count << '/' << max_reported_errors << " errors on " << replies().size() << " total replies)";
throw std::runtime_error(oss.str());
}
}
void Batch::reset() noexcept {
try {
this->replies(); //force waiting for any pending jobs
}
catch (...) {
assert(false);
}
assert(m_local_data);
assert(0 == m_local_data->pending_futures);
m_futures.clear();
m_replies.clear();
}
RedisError::RedisError (const char* parMessage, std::size_t parLength) :
std::runtime_error(std::string(parMessage, parLength))
{
}
} //namespace redis

View file

@ -1,96 +0,0 @@
/* 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/>.
*/
#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

@ -1,92 +0,0 @@
/* 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 "script_manager.hpp"
#include "async_connection.hpp"
#include <hiredis/hiredis.h>
#include <ciso646>
#include <cassert>
#include <algorithm>
#include <stdexcept>
#include <atomic>
//See docs directory for info about hiredis/libev with multithreading
namespace redis {
namespace {
} //unnamed namespace
struct Command::LocalData {
explicit LocalData (Command* parCommand, std::string&& parAddress, uint16_t parPort) :
async_connection(std::move(parAddress), parPort),
lua_scripts(parCommand),
pending_futures(0)
{
}
AsyncConnection async_connection;
ScriptManager lua_scripts;
std::atomic_size_t pending_futures;
};
Command::Command (std::string&& parAddress, uint16_t parPort) :
m_local_data(new LocalData(this, std::move(parAddress), parPort))
{
}
Command::Command (std::string&& parSocket) :
Command(std::move(parSocket), 0)
{
}
Command::~Command() noexcept = default;
void Command::connect() {
m_local_data->async_connection.connect();
}
void Command::wait_for_connect() {
m_local_data->async_connection.wait_for_connect();
}
void Command::disconnect() {
m_local_data->async_connection.disconnect();
}
void Command::wait_for_disconnect() {
m_local_data->async_connection.wait_for_disconnect();
}
bool Command::is_connected() const {
return m_local_data->async_connection.is_connected();
}
boost::string_ref Command::connection_error() const {
return m_local_data->async_connection.connection_error();
}
Batch Command::make_batch() {
assert(is_connected());
return Batch(&m_local_data->async_connection, m_local_data->pending_futures);
}
Script Command::make_script (const std::string &parScript) {
auto sha1 = m_local_data->lua_scripts.submit_lua_script(parScript);
return Script(sha1, m_local_data->lua_scripts);
}
} //namespace redis

View file

@ -1,84 +0,0 @@
/* 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/>.
*/
#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 <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

@ -1,137 +0,0 @@
/* 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 "incredis.hpp"
#include "duckhandy/compatibility.h"
#include "duckhandy/lexical_cast.hpp"
#include <cassert>
#include <ciso646>
namespace redis {
namespace {
inline IncRedis::opt_string optional_string ( const Reply& parReply ) a_always_inline;
IncRedis::opt_string_list optional_string_list ( const Reply& parReply );
IncRedis::opt_string optional_string (const Reply& parReply) {
assert(parReply.which() == RedisVariantType_Nil or parReply.which() == RedisVariantType_String);
if (RedisVariantType_Nil == parReply.which())
return boost::none;
else
return get_string(parReply);
}
IncRedis::opt_string_list optional_string_list (const Reply& parReply) {
assert(parReply.which() == RedisVariantType_Nil or parReply.which() == RedisVariantType_Array);
if (RedisVariantType_Nil == parReply.which()) {
return boost::none;
}
else {
auto replies = get_array(parReply);
IncRedis::opt_string_list::value_type retval;
retval.reserve(replies.size());
for (const auto& rep : replies) {
retval.emplace_back(optional_string(rep));
}
return IncRedis::opt_string_list(std::move(retval));
}
}
} //unnamed namespace
IncRedis::IncRedis (std::string &&parAddress, uint16_t parPort) :
m_command(std::move(parAddress), parPort)
{
}
IncRedis::IncRedis (std::string&& parSocket) :
m_command(std::move(parSocket))
{
}
void IncRedis::connect() {
m_command.connect();
}
void IncRedis::wait_for_connect() {
m_command.wait_for_connect();
}
void IncRedis::disconnect() {
m_command.disconnect();
}
void IncRedis::wait_for_disconnect() {
m_command.wait_for_disconnect();
}
IncRedisBatch IncRedis::make_batch() {
return m_command.make_batch();
}
auto IncRedis::scan (boost::string_ref parPattern) -> scan_range {
return scan_range(scan_iterator(&m_command, false, parPattern), scan_iterator(&m_command, true));
}
auto IncRedis::hscan (boost::string_ref parKey, boost::string_ref parPattern) -> hscan_range {
return hscan_range(hscan_iterator(&m_command, parKey, false, parPattern), hscan_iterator(&m_command, parKey, true));
}
auto IncRedis::sscan (boost::string_ref parKey, boost::string_ref parPattern) -> sscan_range {
return sscan_range(sscan_iterator(&m_command, parKey, false, parPattern), sscan_iterator(&m_command, parKey, true));
}
auto IncRedis::zscan (boost::string_ref parKey, boost::string_ref parPattern) -> zscan_range {
return zscan_range(zscan_iterator(&m_command, parKey, false, parPattern), zscan_iterator(&m_command, parKey, true));
}
auto IncRedis::hget (boost::string_ref parKey, boost::string_ref parField) -> opt_string {
return optional_string(m_command.run("HGET", parKey, parField));
}
int IncRedis::hincrby (boost::string_ref parKey, boost::string_ref parField, int parInc) {
const auto inc = dhandy::lexical_cast<std::string>(parInc);
auto reply = m_command.run("HINCRBY", parKey, parField, inc);
return get_integer(reply);
}
auto IncRedis::srandmember (boost::string_ref parKey, int parCount) -> opt_string_list {
return optional_string_list(m_command.run("SRANDMEMBER", parKey, dhandy::lexical_cast<std::string>(parCount)));
}
auto IncRedis::srandmember (boost::string_ref parKey) -> opt_string {
return optional_string(m_command.run("SRANDMEMBER", parKey));
}
auto IncRedis::smembers (boost::string_ref parKey) -> opt_string_list {
return optional_string_list(m_command.run("SMEMBERS", parKey));
}
auto IncRedis::zrangebyscore (boost::string_ref parKey, double parMin, bool parMinIncl, double parMax, bool parMaxIncl, bool parWithScores) -> opt_string_list {
auto batch = make_batch();
batch.zrangebyscore(parKey, parMin, parMinIncl, parMax, parMaxIncl, parWithScores);
assert(batch.replies().size() == 1);
return optional_string_list(batch.replies().front());
}
bool IncRedis::script_flush() {
const auto ret = get<StatusString>(m_command.run("SCRIPT", "FLUSH"));
return ret.is_ok();
}
auto IncRedis::reply_to_string_list (const Reply& parReply) -> opt_string_list {
return optional_string_list(parReply);
}
} //namespace redis

View file

@ -1,98 +0,0 @@
/* 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/>.
*/
#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

@ -1,27 +0,0 @@
/* 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/>.
*/
#ifndef id2F97AF7626CE45F08742867A2A737482
#define id2F97AF7626CE45F08742867A2A737482
#include "duckhandy/cmake_on_off.h"
#if CMAKE_@has_cryptopp_lib@
# define WITH_CRYPTOPP
#endif
#endif

View file

@ -1,92 +0,0 @@
/* 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 "incredis_batch.hpp"
#include "duckhandy/lexical_cast.hpp"
#include <sstream>
#include <utility>
#include <ciso646>
namespace redis {
namespace {
std::string make_boundary (double parValue, bool parExclude) {
std::ostringstream oss;
if (parExclude)
oss << '(';
oss << parValue;
return oss.str();
}
} //unnamed namespace
IncRedisBatch::IncRedisBatch (Batch&& parBatch) :
m_batch(std::move(parBatch))
{
}
void IncRedisBatch::reset() {
m_batch.reset();
}
void IncRedisBatch::throw_if_failed() {
m_batch.throw_if_failed();
}
IncRedisBatch& IncRedisBatch::select (int parIndex) {
m_batch.run("SELECT", dhandy::lexical_cast<std::string>(parIndex));
return *this;
}
IncRedisBatch& IncRedisBatch::client_setname (boost::string_ref parName) {
m_batch.run("CLIENT", "SETNAME", parName);
return *this;
}
IncRedisBatch& IncRedisBatch::hget (boost::string_ref parKey, boost::string_ref parField) {
m_batch.run("HGET", parKey, parField);
return *this;
}
IncRedisBatch& IncRedisBatch::hincrby (boost::string_ref parKey, boost::string_ref parField, int parInc) {
m_batch.run("HINCRBY", parKey, parField, dhandy::lexical_cast<std::string>(parInc));
return *this;
}
IncRedisBatch& IncRedisBatch::srandmember (boost::string_ref parKey, int parCount) {
m_batch.run("SRANDMEMBER", parKey, dhandy::lexical_cast<std::string>(parCount));
return *this;
}
IncRedisBatch& IncRedisBatch::srandmember (boost::string_ref parKey) {
m_batch.run("SRANDMEMBER", parKey);
return *this;
}
IncRedisBatch& IncRedisBatch::zrangebyscore (boost::string_ref parKey, double parMin, bool parMinIncl, double parMax, bool parMaxIncl, bool parWithScores) {
auto lower_bound = make_boundary(parMin, not parMinIncl);
auto upper_bound = make_boundary(parMax, not parMaxIncl);
if (parWithScores)
m_batch.run("ZRANGEBYSCORE", parKey, lower_bound, upper_bound, "WITHSCORES");
else
m_batch.run("ZRANGEBYSCORE", parKey, lower_bound, upper_bound);
return *this;
}
IncRedisBatch& IncRedisBatch::script_flush() {
m_batch.run("SCRIPT", "FLUSH");
return *this;
}
} //namespace redis

View file

@ -1,173 +0,0 @@
/* 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/>.
*/
#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

@ -1,86 +0,0 @@
/* 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 "reply.hpp"
#include "duckhandy/lexical_cast.hpp"
#include <boost/variant/get.hpp>
namespace redis {
const long long& get_integer (const Reply& parReply) {
assert(RedisVariantType_Integer == parReply.which());
return boost::get<long long>(parReply);
}
const std::string& get_string (const Reply& parReply) {
static const std::string empty_str;
if (RedisVariantType_Nil == parReply.which())
return empty_str;
assert(RedisVariantType_String == parReply.which());
return boost::get<std::string>(parReply);
}
long long get_integer_autoconv_if_str (const Reply &parReply) {
using dhandy::lexical_cast;
const auto type = parReply.which();
switch (type) {
case RedisVariantType_Integer:
return get_integer(parReply);
case RedisVariantType_String:
return lexical_cast<long long>(get_string(parReply));
default:
assert(false);
return 0;
}
}
const std::vector<Reply>& get_array (const Reply& parReply) {
assert(RedisVariantType_Array == parReply.which());
return boost::get<std::vector<Reply>>(parReply);
}
const ErrorString& get_error_string (const Reply& parReply) {
assert(RedisVariantType_Error == parReply.which());
return boost::get<ErrorString>(parReply);
}
template <>
const std::string& get<std::string> (const Reply& parReply) {
return get_string(parReply);
}
template <>
const std::vector<Reply>& get<std::vector<Reply>> (const Reply& parReply) {
return get_array(parReply);
}
template <>
const long long& get<long long> (const Reply& parReply) {
return get_integer(parReply);
}
template <>
const ErrorString& get<ErrorString> (const Reply& parReply) {
return get_error_string(parReply);
}
template const std::string& get<std::string> ( const Reply& parReply );
template const std::vector<Reply>& get<std::vector<Reply>> ( const Reply& parReply );
template const long long& get<long long> ( const Reply& parReply );
template const ErrorString& get<ErrorString> ( const Reply& parReply );
} //namespace redis

View file

@ -1,92 +0,0 @@
/* 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/>.
*/
#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

@ -1,62 +0,0 @@
/* 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 "scan_iterator.hpp"
#include "duckhandy/lexical_cast.hpp"
#include "command.hpp"
#include <cassert>
#include <ciso646>
#include <string>
namespace redis {
namespace implem {
ScanIteratorBaseClass::ScanIteratorBaseClass (Command* parCommand) :
ScanIteratorBaseClass(parCommand, boost::string_ref())
{
}
ScanIteratorBaseClass::ScanIteratorBaseClass (Command* parCommand, boost::string_ref parMatchPattern) :
m_command(parCommand),
m_match_pattern(parMatchPattern)
{
assert(m_command);
assert(m_command->is_connected());
}
bool ScanIteratorBaseClass::is_connected() const {
return m_command and m_command->is_connected();
}
Reply ScanIteratorBaseClass::run (const char* parCommand, long long parScanContext, std::size_t parCount) {
const auto scan_context = dhandy::lexical_cast<std::string>(parScanContext);
const auto count_hint = dhandy::lexical_cast<std::string>(parCount);
if (m_match_pattern.empty())
return m_command->run(parCommand, scan_context, "COUNT", count_hint);
else
return m_command->run(parCommand, scan_context, "MATCH", m_match_pattern, "COUNT", count_hint);
}
Reply ScanIteratorBaseClass::run (const char* parCommand, const boost::string_ref& parParameter, long long parScanContext, std::size_t parCount) {
const auto scan_context = dhandy::lexical_cast<std::string>(parScanContext);
const auto count_hint = dhandy::lexical_cast<std::string>(parCount);
if (m_match_pattern.empty())
return m_command->run(parCommand, parParameter, scan_context, "COUNT", count_hint);
else
return m_command->run(parCommand, parParameter, scan_context, "MATCH", m_match_pattern, "COUNT", count_hint);
}
} //namespace implem
} //namespace redis

View file

@ -1,144 +0,0 @@
/* 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/>.
*/
#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

@ -1,161 +0,0 @@
/* 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>
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

@ -1,32 +0,0 @@
/* 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 "script.hpp"
namespace redis {
Script::Script() :
m_sha1(),
m_manager(nullptr)
{
}
Script::Script (boost::string_ref parSha1, ScriptManager& parManager) :
m_sha1(parSha1),
m_manager(&parManager)
{
}
} //namespace redis

View file

@ -1,83 +0,0 @@
/* 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/>.
*/
#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

@ -1,111 +0,0 @@
/* 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 "script_manager.hpp"
#include "duckhandy/lexical_cast.hpp"
#include "command.hpp"
#include <cassert>
#if defined(MAKE_SHA1_WITH_CRYPTOPP)
# include <crypto++/sha.h>
#endif
namespace redis {
namespace {
#if defined(MAKE_SHA1_WITH_CRYPTOPP)
struct LuaScriptHash {
union {
struct {
uint64_t part_a, part_b;
uint32_t part_c;
};
uint8_t raw_bytes[20];
};
};
#endif
} //unnamed namespace
ScriptManager::ScriptManager (Command* parCommand) :
m_command(parCommand)
{
assert(m_command);
}
#if defined(MAKE_SHA1_WITH_CRYPTOPP)
boost::string_ref ScriptManager::add_lua_script_ifn (const std::string& parScript) {
assert(m_command->is_connected());
if (parScript.empty())
return boost::string_ref();
using dhandy::lexical_cast;
static_assert(20 == CryptoPP::SHA1::DIGESTSIZE, "Unexpected SHA1 digest size");
static_assert(sizeof(LuaScriptHash) >= CryptoPP::SHA1::DIGESTSIZE, "Wrong SHA1 struct size");
static_assert(Sha1Array().size() == CryptoPP::SHA1::DIGESTSIZE * 2, "Wrong array size");
LuaScriptHash digest;
CryptoPP::SHA1().CalculateDigest(digest.raw_bytes, reinterpret_cast<const uint8_t*>(parScript.data()), parScript.size());
//TODO: change when lexical_cast will support arrays
auto sha1_str_parta = lexical_cast<std::string, dhandy::tags::hexl>(__builtin_bswap64(digest.part_a));
auto sha1_str_partb = lexical_cast<std::string, dhandy::tags::hexl>(__builtin_bswap64(digest.part_b));
auto sha1_str_partc = lexical_cast<std::string, dhandy::tags::hexl>(__builtin_bswap32(digest.part_c));
const std::string sha1_str =
std::string(sizeof(digest.part_a) * 2 - sha1_str_parta.size(), '0') + sha1_str_parta +
std::string(sizeof(digest.part_b) * 2 - sha1_str_partb.size(), '0') + sha1_str_partb +
std::string(sizeof(digest.part_c) * 2 - sha1_str_partc.size(), '0') + sha1_str_partc
;
Sha1Array sha1_array;
assert(sha1_str.size() == sha1_array.size());
std::copy(sha1_str.begin(), sha1_str.end(), sha1_array.begin());
auto it_found = m_known_hashes.find(sha1_array);
const bool was_present = (m_known_hashes.end() != it_found);
if (was_present) {
return boost::string_ref(it_found->data(), it_found->size());
}
auto reply = m_command->run("SCRIPT", "LOAD", parScript);
assert(not was_present);
assert(get_string(reply) == sha1_str);
const auto it_inserted = m_known_hashes.insert(it_found, sha1_array);
(void)reply;
return boost::string_ref(it_inserted->data(), it_inserted->size());
}
#else
boost::string_ref ScriptManager::add_lua_script_ifn (const std::string& parScript) {
assert(m_command->is_connected());
auto it_found = m_known_scripts.find(parScript);
const bool was_present = (m_known_scripts.end() != it_found);
if (was_present) {
return boost::string_ref(it_found->second.data(), it_found->second.size());
}
auto reply = m_command->run("SCRIPT", "LOAD", parScript);
assert(not was_present);
const auto sha1_str = get_string(reply);
Sha1Array sha1_array;
std::copy(sha1_str.begin(), sha1_str.end(), sha1_array.begin());
auto it_inserted = m_known_scripts.insert(it_found, std::make_pair(parScript, sha1_array));
return boost::string_ref(it_inserted->second.data(), it_inserted->second.size());
}
#endif
} //namespace redis

View file

@ -1,62 +0,0 @@
/* 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/>.
*/
#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