From 59eff8ff5669bf33c2b87f8717df47b1b7ee79f6 Mon Sep 17 00:00:00 2001 From: King_DuckZ Date: Wed, 9 Aug 2017 10:41:18 +0100 Subject: [PATCH] Allow sock connections to redis. --- src/backends/redis/backend_redis.cpp | 50 +++++++++++++++++++++++----- src/backends/redis/backend_redis.hpp | 1 + 2 files changed, 42 insertions(+), 9 deletions(-) diff --git a/src/backends/redis/backend_redis.cpp b/src/backends/redis/backend_redis.cpp index ec3383a..adc6330 100644 --- a/src/backends/redis/backend_redis.cpp +++ b/src/backends/redis/backend_redis.cpp @@ -41,6 +41,7 @@ namespace dindb { std::string address; uint16_t port; uint16_t database; + bool sock_mode; }; std::string read_script (const dincore::SearchPaths& parSearch, const char* parName) { @@ -70,20 +71,31 @@ namespace YAML { node["address"] = parSettings.address; node["port"] = parSettings.port; node["database"] = parSettings.database; + node["sock_mode"] = parSettings.sock_mode; return node; } static bool decode (const Node& parNode, dindb::RedisConnectionSettings& parSettings) { - if (not parNode.IsMap() or parNode.size() != 2) { + if (not parNode.IsMap() or parNode.size() < 2) { return false; } + if (parNode["sock_mode"]) + parSettings.sock_mode = parNode["sock_mode"].as(); + else + parSettings.sock_mode = false; + parSettings.address = parNode["address"].as(); - parSettings.port = parNode["port"].as(); + if (parNode["port"]) + parSettings.port = parNode["port"].as(); + else + parSettings.port = 6379; + if (parNode["database"]) parSettings.database = parNode["database"].as(); else parSettings.database = 0; + return true; } }; @@ -100,6 +112,16 @@ namespace dindb { this->connect(); } + BackendRedis::BackendRedis(std::string&& parPath, uint16_t parDatabase, bool parConnect, dincore::SearchPaths&& parLuaPaths) : + m_redis(std::move(parPath)), + m_tag_if_in_set(), + m_lua_script_paths(std::move(parLuaPaths)), + m_database(parDatabase) + { + if (parConnect) + this->connect(); + } + BackendRedis::~BackendRedis() noexcept { } @@ -299,13 +321,23 @@ extern "C" dindb::Backend* dindexer_create_backend (const YAML::Node* parConfig) dincore::SearchPaths lua_paths(std::move(vec)); lua_paths.add_path(REDIS_SCRIPTS_PATH); - return new dindb::BackendRedis( - std::move(config.address), - config.port, - config.database, - true, - std::move(lua_paths) - ); + if (config.sock_mode) { + return new dindb::BackendRedis( + std::move(config.address), + config.database, + true, + std::move(lua_paths) + ); + } + else { + return new dindb::BackendRedis( + std::move(config.address), + config.port, + config.database, + true, + std::move(lua_paths) + ); + } } extern "C" void dindexer_destroy_backend (dindb::Backend* parDele) { diff --git a/src/backends/redis/backend_redis.hpp b/src/backends/redis/backend_redis.hpp index 7e13626..358cf59 100644 --- a/src/backends/redis/backend_redis.hpp +++ b/src/backends/redis/backend_redis.hpp @@ -30,6 +30,7 @@ namespace dindb { public: BackendRedis ( BackendRedis&& ) = default; BackendRedis ( std::string&& parAddress, uint16_t parPort, uint16_t parDatabase, bool parConnect, dincore::SearchPaths&& parLuaPaths ); + BackendRedis ( std::string&& parPath, uint16_t parDatabase, bool parConnect, dincore::SearchPaths&& parLuaPaths ); virtual ~BackendRedis ( void ) noexcept; virtual void connect ( void ) override;