From 9a84f63e549428759715b91869da12640b9b573e Mon Sep 17 00:00:00 2001 From: King_DuckZ Date: Wed, 13 Jul 2016 01:47:38 +0100 Subject: [PATCH] Implement zrangebyscore(). --- src/backends/redis/incredis.cpp | 7 +++++++ src/backends/redis/incredis.hpp | 3 +++ src/backends/redis/incredis_batch.cpp | 23 +++++++++++++++++++++++ src/backends/redis/incredis_batch.hpp | 1 + 4 files changed, 34 insertions(+) diff --git a/src/backends/redis/incredis.cpp b/src/backends/redis/incredis.cpp index 2e7ca88..26999b8 100644 --- a/src/backends/redis/incredis.cpp +++ b/src/backends/redis/incredis.cpp @@ -119,6 +119,13 @@ namespace redis { return optional_string_list(m_command.run("SMEMBERS", parKey)); } + auto IncRedis::zrangebyscore (boost::string_ref parKey, double parMin, bool parMinIncl, double parMax, bool parMaxIncl, bool parWithScores) -> opt_string_list { + auto batch = make_batch(); + batch.zrangebyscore(parKey, parMin, parMinIncl, parMax, parMaxIncl, parWithScores); + assert(batch.replies().size() == 1); + return optional_string_list(batch.replies().front()); + } + bool IncRedis::script_flush() { const auto ret = get(m_command.run("SCRIPT", "FLUSH")); return ret.is_ok(); diff --git a/src/backends/redis/incredis.hpp b/src/backends/redis/incredis.hpp index 5ea6727..bcd45e6 100644 --- a/src/backends/redis/incredis.hpp +++ b/src/backends/redis/incredis.hpp @@ -76,6 +76,9 @@ namespace redis { opt_string srandmember ( boost::string_ref parKey ); opt_string_list smembers ( boost::string_ref parKey ); + //Sorted set + opt_string_list zrangebyscore ( boost::string_ref parKey, double parMin, bool parMinIncl, double parMax, bool parMaxIncl, bool parWithScores ); + //Script bool script_flush ( void ); diff --git a/src/backends/redis/incredis_batch.cpp b/src/backends/redis/incredis_batch.cpp index 2cae9de..0f625b9 100644 --- a/src/backends/redis/incredis_batch.cpp +++ b/src/backends/redis/incredis_batch.cpp @@ -17,9 +17,21 @@ #include "incredis_batch.hpp" #include "helpers/lexical_cast.hpp" +#include #include +#include namespace redis { + namespace { + std::string make_boundary (double parValue, bool parExclude) { + std::ostringstream oss; + if (parExclude) + oss << '('; + oss << parValue; + return oss.str(); + } + } //unnamed namespace + IncRedisBatch::IncRedisBatch (Batch&& parBatch) : m_batch(std::move(parBatch)) { @@ -62,6 +74,17 @@ namespace redis { return *this; } + IncRedisBatch& IncRedisBatch::zrangebyscore (boost::string_ref parKey, double parMin, bool parMinIncl, double parMax, bool parMaxIncl, bool parWithScores) { + auto lower_bound = make_boundary(parMin, not parMinIncl); + auto upper_bound = make_boundary(parMax, not parMaxIncl); + + if (parWithScores) + m_batch.run("ZRANGEBYSCORE", parKey, lower_bound, upper_bound, "WITHSCORES"); + else + m_batch.run("ZRANGEBYSCORE", parKey, lower_bound, upper_bound); + return *this; + } + IncRedisBatch& IncRedisBatch::script_flush() { m_batch.run("SCRIPT", "FLUSH"); return *this; diff --git a/src/backends/redis/incredis_batch.hpp b/src/backends/redis/incredis_batch.hpp index 2112892..db3f256 100644 --- a/src/backends/redis/incredis_batch.hpp +++ b/src/backends/redis/incredis_batch.hpp @@ -67,6 +67,7 @@ namespace redis { //Sorted set template IncRedisBatch& zadd ( boost::string_ref parKey, ZADD_Mode parMode, bool parChange, Args&&... parArgs ); + IncRedisBatch& zrangebyscore ( boost::string_ref parKey, double parMin, bool parMinIncl, double parMax, bool parMaxIncl, bool parWithScores ); //Script IncRedisBatch& script_flush ( void );