1
0
Fork 0
mirror of https://github.com/KingDuckZ/dindexer.git synced 2025-02-19 12:04:54 +00:00

Implement zadd().

This commit is contained in:
King_DuckZ 2016-07-13 01:08:17 +01:00
parent d7c254e1a0
commit a9b979dd03
2 changed files with 79 additions and 2 deletions

View file

@ -174,11 +174,10 @@ namespace dindb {
std::vector<GroupIDType> find_all_sets (redis::IncRedis& parRedis) {
using dincore::split_and_trim;
using dinhelp::lexical_cast;
std::vector<GroupIDType> retval;
for (const auto& itm : parRedis.scan(PROGRAM_NAME ":set:*")) {
retval.push_back(lexical_cast<GroupIDType>(split_and_trim(itm, ':').back()));
retval.push_back(dinhelp::lexical_cast<GroupIDType>(split_and_trim(itm, ':').back()));
}
return retval;
}

View file

@ -19,11 +19,20 @@
#define id3C772A92AB0E440DA84DAFD807BC962D
#include "batch.hpp"
#include "helpers/sequence_bt.hpp"
#include <boost/utility/string_ref.hpp>
#include <boost/lexical_cast.hpp>
#include <type_traits>
namespace redis {
class IncRedisBatch {
public:
enum ZADD_Mode {
ZADD_XX_UpdateOnly,
ZADD_NX_AlwaysAdd,
ZADD_None
};
IncRedisBatch ( void ) = delete;
IncRedisBatch ( IncRedisBatch&& ) = default;
IncRedisBatch ( const Batch& ) = delete;
@ -55,6 +64,10 @@ namespace redis {
template <typename... Args>
IncRedisBatch& sadd ( boost::string_ref parKey, Args&&... parArgs );
//Sorted set
template <typename... Args>
IncRedisBatch& zadd ( boost::string_ref parKey, ZADD_Mode parMode, bool parChange, Args&&... parArgs );
//Script
IncRedisBatch& script_flush ( void );
@ -62,6 +75,11 @@ namespace redis {
Batch m_batch;
};
namespace implem {
template <std::size_t... I, typename... Args>
void run_conv_floats_to_strings ( Batch& parBatch, dinhelp::bt::index_seq<I...>, Args&&... parArgs );
} //namespace implem
template <typename... Args>
IncRedisBatch& IncRedisBatch::hmget (boost::string_ref parKey, Args&&... parArgs) {
static_assert(sizeof...(Args) > 0, "No fields specified");
@ -71,6 +89,7 @@ namespace redis {
template <typename... Args>
IncRedisBatch& IncRedisBatch::hmset (boost::string_ref parKey, Args&&... parArgs) {
static_assert(sizeof...(Args) >= 1, "No parameters specified");
static_assert(sizeof...(Args) % 2 == 0, "Uneven number of parameters received");
m_batch.run("HMSET", parKey, std::forward<Args>(parArgs)...);
return *this;
@ -89,6 +108,65 @@ namespace redis {
m_batch.run("DEL", std::forward<Args>(parArgs)...);
return *this;
}
template <typename... Args>
IncRedisBatch& IncRedisBatch::zadd (boost::string_ref parKey, ZADD_Mode parMode, bool parChange, Args&&... parArgs) {
static_assert(sizeof...(Args) >= 1, "No score/value pairs specified");
static_assert(sizeof...(Args) % 2 == 0, "Uneven number of parameters received");
using dinhelp::bt::index_range;
if (parChange) {
if (ZADD_None == parMode)
implem::run_conv_floats_to_strings(m_batch, index_range<0, sizeof...(Args)>(), "ZADD", parKey, "CH", std::forward<Args>(parArgs)...);
else if (ZADD_NX_AlwaysAdd == parMode)
implem::run_conv_floats_to_strings(m_batch, index_range<0, sizeof...(Args)>(), "ZADD", parKey, "NX", "CH", std::forward<Args>(parArgs)...);
else if (ZADD_XX_UpdateOnly == parMode)
implem::run_conv_floats_to_strings(m_batch, index_range<0, sizeof...(Args)>(), "ZADD", parKey, "XX", "CH", std::forward<Args>(parArgs)...);
}
else {
if (ZADD_None == parMode)
implem::run_conv_floats_to_strings(m_batch, index_range<0, sizeof...(Args)>(), "ZADD", parKey, std::forward<Args>(parArgs)...);
else if (ZADD_NX_AlwaysAdd == parMode)
implem::run_conv_floats_to_strings(m_batch, index_range<0, sizeof...(Args)>(), "ZADD", parKey, "NX", std::forward<Args>(parArgs)...);
else if (ZADD_XX_UpdateOnly == parMode)
implem::run_conv_floats_to_strings(m_batch, index_range<0, sizeof...(Args)>(), "ZADD", parKey, "XX", std::forward<Args>(parArgs)...);
}
return *this;
}
namespace implem {
template <std::size_t IGNORE_COUNT, std::size_t IDX, typename T, bool STRINGIZE=(IDX>=IGNORE_COUNT) && ((IDX-IGNORE_COUNT)%2)==0>
struct stringize_or_forward_impl {
typedef T type;
static T&& do_it ( T&& parT ) { return std::forward<T>(parT); }
};
template <std::size_t IGNORE_COUNT, std::size_t IDX, typename T>
struct stringize_or_forward_impl<IGNORE_COUNT, IDX, T, true> {
static_assert(std::is_floating_point<T>::value, "Scores must be given as floating point values");
typedef std::string type;
static std::string do_it ( T parT ) { return boost::lexical_cast<std::string>(parT); }
};
template <std::size_t IGNORE_COUNT, std::size_t IDX, typename T>
auto stringize_or_forward (T&& parValue) -> typename stringize_or_forward_impl<IGNORE_COUNT, IDX, T>::type {
return stringize_or_forward_impl<IGNORE_COUNT, IDX, T>::do_it(std::forward<T>(parValue));
}
template <std::size_t PreArgsCount, std::size_t... I, typename... Args>
void run_conv_floats_to_strings_impl (Batch& parBatch, dinhelp::bt::index_seq<I...>, Args&&... parArgs) {
static_assert(sizeof...(I) == sizeof...(Args), "Wrong number of indices");
static_assert(PreArgsCount <= sizeof...(I), "Can't ignore more arguments than those that were received");
parBatch.run(stringize_or_forward<PreArgsCount, I>(std::forward<Args>(parArgs))...);
}
template <std::size_t... I, typename... Args>
void run_conv_floats_to_strings (Batch& parBatch, dinhelp::bt::index_seq<I...>, Args&&... parArgs) {
static_assert(sizeof...(Args) >= sizeof...(I), "Unexpected count, there should be at least as many argument as there are indices");
constexpr const auto pre_args_count = sizeof...(Args) - sizeof...(I);
run_conv_floats_to_strings_impl<pre_args_count>(parBatch, dinhelp::bt::index_range<0, sizeof...(Args)>(), std::forward<Args>(parArgs)...);
};
} //namespace implem
} //namespace redis
#endif