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

97 lines
3 KiB
C++
Raw Normal View History

2016-06-20 18:19:06 +01:00
/* 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>
struct redisAsyncContext;
namespace std {
template <class R> class future;
template <class T> struct atomic;
} //namespace std
namespace redis {
class Command;
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 );
private:
struct LocalData;
Batch ( redisAsyncContext* parContext, Command* parCommand );
2016-06-20 18:19:06 +01:00
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;
Command* m_command;
2016-06-20 18:19:06 +01:00
redisAsyncContext* m_context;
};
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