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

Implement Script class.

This commit is contained in:
King_DuckZ 2016-06-30 11:27:15 +01:00
parent 0efd71a2fa
commit 8d7d4c5555
7 changed files with 64 additions and 11 deletions

View file

@ -158,7 +158,6 @@ namespace dindb {
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.throw_if_failed();
m_redis.submit_lua_script("return 42;");
}
else {
std::ostringstream oss;

View file

@ -98,7 +98,8 @@ namespace redis {
return Batch(&m_local_data->async_connection);
}
void Command::submit_lua_script (const std::string& parScript) {
m_local_data->lua_scripts.submit_lua_script(parScript);
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

@ -22,6 +22,7 @@
#include "reply.hpp"
#include "batch.hpp"
#include "redisConfig.h"
#include "script.hpp"
#include <array>
#include <string>
#include <cstdint>
@ -58,6 +59,7 @@ namespace redis {
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 );
@ -68,8 +70,6 @@ namespace redis {
sscan_range sscan ( boost::string_ref parKey );
zscan_range zscan ( boost::string_ref parKey );
void submit_lua_script ( const std::string& parScript );
private:
struct LocalData;

View file

@ -18,4 +18,9 @@
#include "script.hpp"
namespace redis {
Script::Script (boost::string_ref parSha1, ScriptManager& parManager) :
m_sha1(parSha1),
m_manager(parManager)
{
}
} //namespace redis

View file

@ -18,11 +18,58 @@
#ifndef id5B30CDA57F894CD6888093B64F9433DA
#define id5B30CDA57F894CD6888093B64F9433DA
#include "script_manager.hpp"
#include "batch.hpp"
#include "helpers/lexical_cast.hpp"
#include "helpers/sequence_bt.hpp"
#include <boost/utility/string_ref.hpp>
#include <tuple>
namespace redis {
class ScriptManager;
class Script {
public:
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 );
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, dinhelp::bt::index_seq<KeyIndices...>, dinhelp::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,
::dinhelp::bt::index_range<0, sizeof...(Keys)>(),
::dinhelp::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, dinhelp::bt::index_seq<KeyIndices...>, dinhelp::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<decltype(parKeys)>::value, "Wrong key count");
static_assert(sizeof...(Values) == std::tuple_size<decltype(parValues)>::value, "Wrong value count");
parBatch.run(
"EVALSHA",
m_sha1,
dinhelp::lexical_cast<std::string>(sizeof...(Keys)),
std::get<KeyIndices>(parKeys)...,
std::get<ValueIndices>(parValues)...
);
}
} //namespace redis
#endif

View file

@ -108,8 +108,4 @@ namespace redis {
return boost::string_ref(it_inserted->second.data(), it_inserted->second.size());
}
#endif
void ScriptManager::submit_lua_script (const std::string& parScript) {
add_lua_script_ifn(parScript);
}
} //namespace redis

View file

@ -27,6 +27,7 @@
#endif
#include <string>
#include <array>
#include <boost/utility/string_ref.hpp>
namespace redis {
class Command;
@ -35,7 +36,7 @@ namespace redis {
public:
explicit ScriptManager ( Command* parCommand );
void submit_lua_script ( const std::string& parScript );
boost::string_ref submit_lua_script ( const std::string& parScript );
private:
using Sha1Array = std::array<char, 40>;
@ -49,6 +50,10 @@ namespace redis {
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