mirror of
https://github.com/KingDuckZ/dindexer.git
synced 2025-07-02 14:04:22 +00:00
New IncRedisBatch class.
Wraps Batch similarly to how IncRedis wraps Command.
This commit is contained in:
parent
e02b0a16f5
commit
e0670ff433
6 changed files with 129 additions and 4 deletions
|
@ -20,6 +20,7 @@ add_library(${PROJECT_NAME} SHARED
|
|||
delete.cpp
|
||||
find.cpp
|
||||
incredis.cpp
|
||||
incredis_batch.cpp
|
||||
)
|
||||
|
||||
target_include_directories(${PROJECT_NAME} SYSTEM
|
||||
|
|
|
@ -107,10 +107,10 @@ namespace dindb {
|
|||
m_redis.connect();
|
||||
m_redis.wait_for_connect();
|
||||
if (m_redis.is_connected()) {
|
||||
auto batch = m_redis.command().make_batch();
|
||||
batch.run("SELECT", lexical_cast<std::string>(m_database));
|
||||
batch.run("CLIENT", "SETNAME", PROGRAM_NAME "_v" STRINGIZE(VERSION_MAJOR) "." STRINGIZE(VERSION_MINOR) "." STRINGIZE(VERSION_PATCH));
|
||||
batch.run("SCRIPT", "FLUSH");
|
||||
auto batch = m_redis.make_batch();
|
||||
batch.select(m_database);
|
||||
batch.client_setname(PROGRAM_NAME "_v" STRINGIZE(VERSION_MAJOR) "." STRINGIZE(VERSION_MINOR) "." STRINGIZE(VERSION_PATCH));
|
||||
batch.script_flush();
|
||||
batch.throw_if_failed();
|
||||
}
|
||||
else {
|
||||
|
|
|
@ -77,6 +77,10 @@ namespace redis {
|
|||
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));
|
||||
}
|
||||
|
|
|
@ -19,6 +19,7 @@
|
|||
#define id7D338900114548A890B1EECE0C4D3C4C
|
||||
|
||||
#include "command.hpp"
|
||||
#include "incredis_batch.hpp"
|
||||
#include "scan_iterator.hpp"
|
||||
#include <boost/optional.hpp>
|
||||
#include <string>
|
||||
|
@ -53,6 +54,8 @@ namespace redis {
|
|||
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; }
|
||||
|
||||
|
|
59
src/backends/redis/incredis_batch.cpp
Normal file
59
src/backends/redis/incredis_batch.cpp
Normal file
|
@ -0,0 +1,59 @@
|
|||
/* 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 "helpers/lexical_cast.hpp"
|
||||
#include <utility>
|
||||
|
||||
namespace redis {
|
||||
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", dinhelp::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, dinhelp::lexical_cast<std::string>(parInc));
|
||||
return *this;
|
||||
}
|
||||
|
||||
IncRedisBatch& IncRedisBatch::script_flush() {
|
||||
m_batch.run("SCRIPT", "FLUSH");
|
||||
return *this;
|
||||
}
|
||||
} //namespace redis
|
58
src/backends/redis/incredis_batch.hpp
Normal file
58
src/backends/redis/incredis_batch.hpp
Normal file
|
@ -0,0 +1,58 @@
|
|||
/* 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 <boost/utility/string_ref.hpp>
|
||||
|
||||
namespace redis {
|
||||
class IncRedisBatch {
|
||||
public:
|
||||
IncRedisBatch ( Batch&& parBatch );
|
||||
|
||||
void reset ( void );
|
||||
void throw_if_failed ( void );
|
||||
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 );
|
||||
|
||||
//Hash
|
||||
IncRedisBatch& hget ( boost::string_ref parKey, boost::string_ref parField );
|
||||
template <typename... Args>
|
||||
IncRedisBatch& hmget ( boost::string_ref parKey, Args&&... parArgs );
|
||||
IncRedisBatch& hincrby ( boost::string_ref parKey, boost::string_ref parField, int parInc );
|
||||
|
||||
//Script
|
||||
IncRedisBatch& script_flush ( void );
|
||||
|
||||
private:
|
||||
Batch m_batch;
|
||||
};
|
||||
|
||||
template <typename... Args>
|
||||
IncRedisBatch& IncRedisBatch::hmget (boost::string_ref parKey, Args&&... parArgs) {
|
||||
m_batch.run("HMGET", parKey, std::forward<Args>(parArgs)...);
|
||||
return *this;
|
||||
}
|
||||
} //namespace redis
|
||||
|
||||
#endif
|
Loading…
Add table
Add a link
Reference in a new issue