From 7b1931bca0db59b650ad153ec7a463774db77bfe Mon Sep 17 00:00:00 2001 From: King_DuckZ Date: Wed, 21 Jun 2017 21:09:46 +0100 Subject: [PATCH] Accept lua scripts in a string_view rather than in a string. --- include/incredis/command.hpp | 2 +- include/incredis/script_manager.hpp | 6 +++--- src/command.cpp | 2 +- src/script_manager.cpp | 9 +++++---- 4 files changed, 10 insertions(+), 9 deletions(-) diff --git a/include/incredis/command.hpp b/include/incredis/command.hpp index 0b7a7e3..ab83641 100644 --- a/include/incredis/command.hpp +++ b/include/incredis/command.hpp @@ -50,7 +50,7 @@ namespace redis { boost::string_view connection_error ( void ) const; Batch make_batch ( void ); - Script make_script ( const std::string& parScript ); + Script make_script ( const boost::string_view& parScript ); template Reply run ( const char* parCommand, Args&&... parArgs ); diff --git a/include/incredis/script_manager.hpp b/include/incredis/script_manager.hpp index 2a098d5..560ac20 100644 --- a/include/incredis/script_manager.hpp +++ b/include/incredis/script_manager.hpp @@ -38,13 +38,13 @@ namespace redis { public: explicit ScriptManager ( Command* parCommand ); - boost::string_view submit_lua_script ( const std::string& parScript ); + boost::string_view submit_lua_script ( const boost::string_view& parScript ); void update_command_ptr (Command* parNewPtr); private: using Sha1Array = std::array; - boost::string_view add_lua_script_ifn ( const std::string& parScript ); + boost::string_view add_lua_script_ifn ( const boost::string_view& parScript ); Command* m_command; #if defined(MAKE_SHA1_WITH_CRYPTOPP) @@ -54,7 +54,7 @@ namespace redis { #endif }; - inline boost::string_view ScriptManager::submit_lua_script (const std::string& parScript) { + inline boost::string_view ScriptManager::submit_lua_script (const boost::string_view& parScript) { return add_lua_script_ifn(parScript); } } //namespace redis diff --git a/src/command.cpp b/src/command.cpp index 37293c0..c9229d2 100644 --- a/src/command.cpp +++ b/src/command.cpp @@ -90,7 +90,7 @@ namespace redis { return Batch(&m_local_data->async_connection, m_local_data->thread_context); } - Script Command::make_script (const std::string &parScript) { + Script Command::make_script (const boost::string_view &parScript) { auto sha1 = m_local_data->lua_scripts.submit_lua_script(parScript); return Script(sha1, m_local_data->lua_scripts); } diff --git a/src/script_manager.cpp b/src/script_manager.cpp index 763dee3..56037f7 100644 --- a/src/script_manager.cpp +++ b/src/script_manager.cpp @@ -45,7 +45,7 @@ namespace redis { } #if defined(MAKE_SHA1_WITH_CRYPTOPP) - boost::string_view ScriptManager::add_lua_script_ifn (const std::string& parScript) { + boost::string_view ScriptManager::add_lua_script_ifn (const boost::string_view& parScript) { assert(m_command->is_connected()); if (parScript.empty()) @@ -88,10 +88,11 @@ namespace redis { return boost::string_view(it_inserted->data(), it_inserted->size()); } #else - boost::string_view ScriptManager::add_lua_script_ifn (const std::string& parScript) { + boost::string_view ScriptManager::add_lua_script_ifn (const boost::string_view& parScript) { assert(m_command->is_connected()); - auto it_found = m_known_scripts.find(parScript); + std::string script_cpy = std::string(parScript); + auto it_found = m_known_scripts.find(script_cpy); const bool was_present = (m_known_scripts.end() != it_found); if (was_present) { return boost::string_view(it_found->second.data(), it_found->second.size()); @@ -103,7 +104,7 @@ namespace redis { 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)); + auto it_inserted = m_known_scripts.insert(it_found, std::make_pair(std::move(script_cpy), sha1_array)); return boost::string_view(it_inserted->second.data(), it_inserted->second.size()); }