From 8d7d4c555578bc9785b4b1802411151576a91f2b Mon Sep 17 00:00:00 2001 From: King_DuckZ Date: Thu, 30 Jun 2016 11:27:15 +0100 Subject: [PATCH] Implement Script class. --- src/backends/redis/backend_redis.cpp | 1 - src/backends/redis/command.cpp | 5 +-- src/backends/redis/command.hpp | 4 +-- src/backends/redis/script.cpp | 5 +++ src/backends/redis/script.hpp | 49 ++++++++++++++++++++++++++- src/backends/redis/script_manager.cpp | 4 --- src/backends/redis/script_manager.hpp | 7 +++- 7 files changed, 64 insertions(+), 11 deletions(-) diff --git a/src/backends/redis/backend_redis.cpp b/src/backends/redis/backend_redis.cpp index 427aec2..ca87f91 100644 --- a/src/backends/redis/backend_redis.cpp +++ b/src/backends/redis/backend_redis.cpp @@ -158,7 +158,6 @@ namespace dindb { batch.run("SELECT", lexical_cast(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; diff --git a/src/backends/redis/command.cpp b/src/backends/redis/command.cpp index 00ca1ca..e58707f 100644 --- a/src/backends/redis/command.cpp +++ b/src/backends/redis/command.cpp @@ -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 diff --git a/src/backends/redis/command.hpp b/src/backends/redis/command.hpp index 32b37f3..d9c6fbf 100644 --- a/src/backends/redis/command.hpp +++ b/src/backends/redis/command.hpp @@ -22,6 +22,7 @@ #include "reply.hpp" #include "batch.hpp" #include "redisConfig.h" +#include "script.hpp" #include #include #include @@ -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 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; diff --git a/src/backends/redis/script.cpp b/src/backends/redis/script.cpp index 2e1502c..af7eeac 100644 --- a/src/backends/redis/script.cpp +++ b/src/backends/redis/script.cpp @@ -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 diff --git a/src/backends/redis/script.hpp b/src/backends/redis/script.hpp index f547df3..e169bf6 100644 --- a/src/backends/redis/script.hpp +++ b/src/backends/redis/script.hpp @@ -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 +#include namespace redis { + class ScriptManager; + class Script { + public: + Script ( Script&& ) = default; + Script ( boost::string_ref parSha1, ScriptManager& parManager ); + ~Script ( void ) noexcept = default; + + template + void run ( Batch& parBatch, const std::tuple& parKeys, const std::tuple& parValues ); + + private: + template + void run_with_indices ( Batch& parBatch, const std::tuple& parKeys, const std::tuple& parValues, dinhelp::bt::index_seq, dinhelp::bt::index_seq ); + + boost::string_ref m_sha1; + ScriptManager& m_manager; }; + + template + void Script::run (Batch& parBatch, const std::tuple& parKeys, const std::tuple& parValues) { + this->run_with_indices( + parBatch, + parKeys, + parValues, + ::dinhelp::bt::index_range<0, sizeof...(Keys)>(), + ::dinhelp::bt::index_range<0, sizeof...(Values)>() + ); + } + + template + void Script::run_with_indices (Batch& parBatch, const std::tuple& parKeys, const std::tuple& parValues, dinhelp::bt::index_seq, dinhelp::bt::index_seq) { + 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::value, "Wrong key count"); + static_assert(sizeof...(Values) == std::tuple_size::value, "Wrong value count"); + + parBatch.run( + "EVALSHA", + m_sha1, + dinhelp::lexical_cast(sizeof...(Keys)), + std::get(parKeys)..., + std::get(parValues)... + ); + } } //namespace redis #endif diff --git a/src/backends/redis/script_manager.cpp b/src/backends/redis/script_manager.cpp index 0f7c98e..1e86f54 100644 --- a/src/backends/redis/script_manager.cpp +++ b/src/backends/redis/script_manager.cpp @@ -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 diff --git a/src/backends/redis/script_manager.hpp b/src/backends/redis/script_manager.hpp index 08b744a..5cec852 100644 --- a/src/backends/redis/script_manager.hpp +++ b/src/backends/redis/script_manager.hpp @@ -27,6 +27,7 @@ #endif #include #include +#include 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; @@ -49,6 +50,10 @@ namespace redis { std::map 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