From 2bceabdfa53b3b08840a6d6fd4c6831e5ac15de8 Mon Sep 17 00:00:00 2001 From: Grant Limberg Date: Mon, 8 Jun 2020 11:23:41 -0700 Subject: [PATCH] add redis dependencies for central controller --- .../thirdparty/hiredis-0.14.1/.gitignore | 7 + .../thirdparty/hiredis-0.14.1/.travis.yml | 45 + .../thirdparty/hiredis-0.14.1/CHANGELOG.md | 190 ++ controller/thirdparty/hiredis-0.14.1/COPYING | 29 + controller/thirdparty/hiredis-0.14.1/Makefile | 214 ++ .../thirdparty/hiredis-0.14.1/README.md | 410 +++ .../thirdparty/hiredis-0.14.1/adapters/ae.h | 127 + .../thirdparty/hiredis-0.14.1/adapters/glib.h | 153 ++ .../hiredis-0.14.1/adapters/ivykis.h | 81 + .../hiredis-0.14.1/adapters/libev.h | 147 ++ .../hiredis-0.14.1/adapters/libevent.h | 108 + .../hiredis-0.14.1/adapters/libuv.h | 122 + .../hiredis-0.14.1/adapters/macosx.h | 114 + .../thirdparty/hiredis-0.14.1/adapters/qt.h | 135 + controller/thirdparty/hiredis-0.14.1/alloc.c | 65 + controller/thirdparty/hiredis-0.14.1/alloc.h | 53 + .../thirdparty/hiredis-0.14.1/appveyor.yml | 23 + controller/thirdparty/hiredis-0.14.1/async.c | 717 ++++++ controller/thirdparty/hiredis-0.14.1/async.h | 130 + controller/thirdparty/hiredis-0.14.1/dict.c | 339 +++ controller/thirdparty/hiredis-0.14.1/dict.h | 126 + .../hiredis-0.14.1/examples/example-ae.c | 62 + .../hiredis-0.14.1/examples/example-glib.c | 73 + .../hiredis-0.14.1/examples/example-ivykis.c | 58 + .../hiredis-0.14.1/examples/example-libev.c | 52 + .../examples/example-libevent.c | 53 + .../hiredis-0.14.1/examples/example-libuv.c | 53 + .../hiredis-0.14.1/examples/example-macosx.c | 66 + .../hiredis-0.14.1/examples/example-qt.cpp | 46 + .../hiredis-0.14.1/examples/example-qt.h | 32 + .../hiredis-0.14.1/examples/example.c | 78 + .../thirdparty/hiredis-0.14.1/fmacros.h | 12 + .../thirdparty/hiredis-0.14.1/hiredis.c | 1006 ++++++++ .../thirdparty/hiredis-0.14.1/hiredis.h | 200 ++ controller/thirdparty/hiredis-0.14.1/net.c | 477 ++++ controller/thirdparty/hiredis-0.14.1/net.h | 49 + controller/thirdparty/hiredis-0.14.1/read.c | 598 +++++ controller/thirdparty/hiredis-0.14.1/read.h | 111 + controller/thirdparty/hiredis-0.14.1/sds.c | 1272 ++++++++++ controller/thirdparty/hiredis-0.14.1/sds.h | 273 ++ .../thirdparty/hiredis-0.14.1/sdsalloc.h | 42 + controller/thirdparty/hiredis-0.14.1/test.c | 923 +++++++ controller/thirdparty/hiredis-0.14.1/win32.h | 42 + .../redis-plus-plus-1.1.1/.gitignore | 32 + .../redis-plus-plus-1.1.1/CMakeLists.txt | 50 + .../thirdparty/redis-plus-plus-1.1.1/LICENSE | 201 ++ .../redis-plus-plus-1.1.1/README.md | 1776 +++++++++++++ .../src/sw/redis++/command.cpp | 376 +++ .../src/sw/redis++/command.h | 2233 +++++++++++++++++ .../src/sw/redis++/command_args.h | 180 ++ .../src/sw/redis++/command_options.cpp | 201 ++ .../src/sw/redis++/command_options.h | 211 ++ .../src/sw/redis++/connection.cpp | 305 +++ .../src/sw/redis++/connection.h | 194 ++ .../src/sw/redis++/connection_pool.cpp | 249 ++ .../src/sw/redis++/connection_pool.h | 115 + .../src/sw/redis++/crc16.cpp | 96 + .../src/sw/redis++/errors.cpp | 136 + .../src/sw/redis++/errors.h | 159 ++ .../src/sw/redis++/pipeline.cpp | 35 + .../src/sw/redis++/pipeline.h | 49 + .../src/sw/redis++/queued_redis.h | 1844 ++++++++++++++ .../src/sw/redis++/queued_redis.hpp | 208 ++ .../src/sw/redis++/redis++.h | 25 + .../src/sw/redis++/redis.cpp | 882 +++++++ .../src/sw/redis++/redis.h | 1523 +++++++++++ .../src/sw/redis++/redis.hpp | 1365 ++++++++++ .../src/sw/redis++/redis_cluster.cpp | 769 ++++++ .../src/sw/redis++/redis_cluster.h | 1395 ++++++++++ .../src/sw/redis++/redis_cluster.hpp | 1415 +++++++++++ .../src/sw/redis++/reply.cpp | 150 ++ .../src/sw/redis++/reply.h | 363 +++ .../src/sw/redis++/sentinel.cpp | 361 +++ .../src/sw/redis++/sentinel.h | 138 + .../src/sw/redis++/shards.cpp | 50 + .../src/sw/redis++/shards.h | 115 + .../src/sw/redis++/shards_pool.cpp | 319 +++ .../src/sw/redis++/shards_pool.h | 137 + .../src/sw/redis++/subscriber.cpp | 222 ++ .../src/sw/redis++/subscriber.h | 231 ++ .../src/sw/redis++/transaction.cpp | 123 + .../src/sw/redis++/transaction.h | 77 + .../src/sw/redis++/utils.h | 269 ++ .../redis-plus-plus-1.1.1/test/CMakeLists.txt | 33 + .../test/src/sw/redis++/benchmark_test.h | 83 + .../test/src/sw/redis++/benchmark_test.hpp | 178 ++ .../src/sw/redis++/connection_cmds_test.h | 49 + .../src/sw/redis++/connection_cmds_test.hpp | 50 + .../test/src/sw/redis++/geo_cmds_test.h | 47 + .../test/src/sw/redis++/geo_cmds_test.hpp | 149 ++ .../test/src/sw/redis++/hash_cmds_test.h | 55 + .../test/src/sw/redis++/hash_cmds_test.hpp | 177 ++ .../src/sw/redis++/hyperloglog_cmds_test.h | 47 + .../src/sw/redis++/hyperloglog_cmds_test.hpp | 67 + .../test/src/sw/redis++/keys_cmds_test.h | 55 + .../test/src/sw/redis++/keys_cmds_test.hpp | 166 ++ .../test/src/sw/redis++/list_cmds_test.h | 55 + .../test/src/sw/redis++/list_cmds_test.hpp | 154 ++ .../sw/redis++/pipeline_transaction_test.h | 57 + .../sw/redis++/pipeline_transaction_test.hpp | 184 ++ .../test/src/sw/redis++/pubsub_test.h | 53 + .../test/src/sw/redis++/pubsub_test.hpp | 244 ++ .../test/src/sw/redis++/sanity_test.h | 76 + .../test/src/sw/redis++/sanity_test.hpp | 299 +++ .../test/src/sw/redis++/script_cmds_test.h | 49 + .../test/src/sw/redis++/script_cmds_test.hpp | 97 + .../test/src/sw/redis++/set_cmds_test.h | 53 + .../test/src/sw/redis++/set_cmds_test.hpp | 184 ++ .../test/src/sw/redis++/stream_cmds_test.h | 54 + .../test/src/sw/redis++/stream_cmds_test.hpp | 225 ++ .../test/src/sw/redis++/string_cmds_test.h | 57 + .../test/src/sw/redis++/string_cmds_test.hpp | 247 ++ .../test/src/sw/redis++/test_main.cpp | 303 +++ .../test/src/sw/redis++/threads_test.h | 51 + .../test/src/sw/redis++/threads_test.hpp | 147 ++ .../test/src/sw/redis++/utils.h | 96 + .../test/src/sw/redis++/zset_cmds_test.h | 61 + .../test/src/sw/redis++/zset_cmds_test.hpp | 350 +++ 118 files changed, 31744 insertions(+) create mode 100644 controller/thirdparty/hiredis-0.14.1/.gitignore create mode 100644 controller/thirdparty/hiredis-0.14.1/.travis.yml create mode 100644 controller/thirdparty/hiredis-0.14.1/CHANGELOG.md create mode 100644 controller/thirdparty/hiredis-0.14.1/COPYING create mode 100644 controller/thirdparty/hiredis-0.14.1/Makefile create mode 100644 controller/thirdparty/hiredis-0.14.1/README.md create mode 100644 controller/thirdparty/hiredis-0.14.1/adapters/ae.h create mode 100644 controller/thirdparty/hiredis-0.14.1/adapters/glib.h create mode 100644 controller/thirdparty/hiredis-0.14.1/adapters/ivykis.h create mode 100644 controller/thirdparty/hiredis-0.14.1/adapters/libev.h create mode 100644 controller/thirdparty/hiredis-0.14.1/adapters/libevent.h create mode 100644 controller/thirdparty/hiredis-0.14.1/adapters/libuv.h create mode 100644 controller/thirdparty/hiredis-0.14.1/adapters/macosx.h create mode 100644 controller/thirdparty/hiredis-0.14.1/adapters/qt.h create mode 100644 controller/thirdparty/hiredis-0.14.1/alloc.c create mode 100644 controller/thirdparty/hiredis-0.14.1/alloc.h create mode 100644 controller/thirdparty/hiredis-0.14.1/appveyor.yml create mode 100644 controller/thirdparty/hiredis-0.14.1/async.c create mode 100644 controller/thirdparty/hiredis-0.14.1/async.h create mode 100644 controller/thirdparty/hiredis-0.14.1/dict.c create mode 100644 controller/thirdparty/hiredis-0.14.1/dict.h create mode 100644 controller/thirdparty/hiredis-0.14.1/examples/example-ae.c create mode 100644 controller/thirdparty/hiredis-0.14.1/examples/example-glib.c create mode 100644 controller/thirdparty/hiredis-0.14.1/examples/example-ivykis.c create mode 100644 controller/thirdparty/hiredis-0.14.1/examples/example-libev.c create mode 100644 controller/thirdparty/hiredis-0.14.1/examples/example-libevent.c create mode 100644 controller/thirdparty/hiredis-0.14.1/examples/example-libuv.c create mode 100644 controller/thirdparty/hiredis-0.14.1/examples/example-macosx.c create mode 100644 controller/thirdparty/hiredis-0.14.1/examples/example-qt.cpp create mode 100644 controller/thirdparty/hiredis-0.14.1/examples/example-qt.h create mode 100644 controller/thirdparty/hiredis-0.14.1/examples/example.c create mode 100644 controller/thirdparty/hiredis-0.14.1/fmacros.h create mode 100644 controller/thirdparty/hiredis-0.14.1/hiredis.c create mode 100644 controller/thirdparty/hiredis-0.14.1/hiredis.h create mode 100644 controller/thirdparty/hiredis-0.14.1/net.c create mode 100644 controller/thirdparty/hiredis-0.14.1/net.h create mode 100644 controller/thirdparty/hiredis-0.14.1/read.c create mode 100644 controller/thirdparty/hiredis-0.14.1/read.h create mode 100644 controller/thirdparty/hiredis-0.14.1/sds.c create mode 100644 controller/thirdparty/hiredis-0.14.1/sds.h create mode 100644 controller/thirdparty/hiredis-0.14.1/sdsalloc.h create mode 100644 controller/thirdparty/hiredis-0.14.1/test.c create mode 100644 controller/thirdparty/hiredis-0.14.1/win32.h create mode 100644 controller/thirdparty/redis-plus-plus-1.1.1/.gitignore create mode 100644 controller/thirdparty/redis-plus-plus-1.1.1/CMakeLists.txt create mode 100644 controller/thirdparty/redis-plus-plus-1.1.1/LICENSE create mode 100644 controller/thirdparty/redis-plus-plus-1.1.1/README.md create mode 100644 controller/thirdparty/redis-plus-plus-1.1.1/src/sw/redis++/command.cpp create mode 100644 controller/thirdparty/redis-plus-plus-1.1.1/src/sw/redis++/command.h create mode 100644 controller/thirdparty/redis-plus-plus-1.1.1/src/sw/redis++/command_args.h create mode 100644 controller/thirdparty/redis-plus-plus-1.1.1/src/sw/redis++/command_options.cpp create mode 100644 controller/thirdparty/redis-plus-plus-1.1.1/src/sw/redis++/command_options.h create mode 100644 controller/thirdparty/redis-plus-plus-1.1.1/src/sw/redis++/connection.cpp create mode 100644 controller/thirdparty/redis-plus-plus-1.1.1/src/sw/redis++/connection.h create mode 100644 controller/thirdparty/redis-plus-plus-1.1.1/src/sw/redis++/connection_pool.cpp create mode 100644 controller/thirdparty/redis-plus-plus-1.1.1/src/sw/redis++/connection_pool.h create mode 100644 controller/thirdparty/redis-plus-plus-1.1.1/src/sw/redis++/crc16.cpp create mode 100644 controller/thirdparty/redis-plus-plus-1.1.1/src/sw/redis++/errors.cpp create mode 100644 controller/thirdparty/redis-plus-plus-1.1.1/src/sw/redis++/errors.h create mode 100644 controller/thirdparty/redis-plus-plus-1.1.1/src/sw/redis++/pipeline.cpp create mode 100644 controller/thirdparty/redis-plus-plus-1.1.1/src/sw/redis++/pipeline.h create mode 100644 controller/thirdparty/redis-plus-plus-1.1.1/src/sw/redis++/queued_redis.h create mode 100644 controller/thirdparty/redis-plus-plus-1.1.1/src/sw/redis++/queued_redis.hpp create mode 100644 controller/thirdparty/redis-plus-plus-1.1.1/src/sw/redis++/redis++.h create mode 100644 controller/thirdparty/redis-plus-plus-1.1.1/src/sw/redis++/redis.cpp create mode 100644 controller/thirdparty/redis-plus-plus-1.1.1/src/sw/redis++/redis.h create mode 100644 controller/thirdparty/redis-plus-plus-1.1.1/src/sw/redis++/redis.hpp create mode 100644 controller/thirdparty/redis-plus-plus-1.1.1/src/sw/redis++/redis_cluster.cpp create mode 100644 controller/thirdparty/redis-plus-plus-1.1.1/src/sw/redis++/redis_cluster.h create mode 100644 controller/thirdparty/redis-plus-plus-1.1.1/src/sw/redis++/redis_cluster.hpp create mode 100644 controller/thirdparty/redis-plus-plus-1.1.1/src/sw/redis++/reply.cpp create mode 100644 controller/thirdparty/redis-plus-plus-1.1.1/src/sw/redis++/reply.h create mode 100644 controller/thirdparty/redis-plus-plus-1.1.1/src/sw/redis++/sentinel.cpp create mode 100644 controller/thirdparty/redis-plus-plus-1.1.1/src/sw/redis++/sentinel.h create mode 100644 controller/thirdparty/redis-plus-plus-1.1.1/src/sw/redis++/shards.cpp create mode 100644 controller/thirdparty/redis-plus-plus-1.1.1/src/sw/redis++/shards.h create mode 100644 controller/thirdparty/redis-plus-plus-1.1.1/src/sw/redis++/shards_pool.cpp create mode 100644 controller/thirdparty/redis-plus-plus-1.1.1/src/sw/redis++/shards_pool.h create mode 100644 controller/thirdparty/redis-plus-plus-1.1.1/src/sw/redis++/subscriber.cpp create mode 100644 controller/thirdparty/redis-plus-plus-1.1.1/src/sw/redis++/subscriber.h create mode 100644 controller/thirdparty/redis-plus-plus-1.1.1/src/sw/redis++/transaction.cpp create mode 100644 controller/thirdparty/redis-plus-plus-1.1.1/src/sw/redis++/transaction.h create mode 100644 controller/thirdparty/redis-plus-plus-1.1.1/src/sw/redis++/utils.h create mode 100644 controller/thirdparty/redis-plus-plus-1.1.1/test/CMakeLists.txt create mode 100644 controller/thirdparty/redis-plus-plus-1.1.1/test/src/sw/redis++/benchmark_test.h create mode 100644 controller/thirdparty/redis-plus-plus-1.1.1/test/src/sw/redis++/benchmark_test.hpp create mode 100644 controller/thirdparty/redis-plus-plus-1.1.1/test/src/sw/redis++/connection_cmds_test.h create mode 100644 controller/thirdparty/redis-plus-plus-1.1.1/test/src/sw/redis++/connection_cmds_test.hpp create mode 100644 controller/thirdparty/redis-plus-plus-1.1.1/test/src/sw/redis++/geo_cmds_test.h create mode 100644 controller/thirdparty/redis-plus-plus-1.1.1/test/src/sw/redis++/geo_cmds_test.hpp create mode 100644 controller/thirdparty/redis-plus-plus-1.1.1/test/src/sw/redis++/hash_cmds_test.h create mode 100644 controller/thirdparty/redis-plus-plus-1.1.1/test/src/sw/redis++/hash_cmds_test.hpp create mode 100644 controller/thirdparty/redis-plus-plus-1.1.1/test/src/sw/redis++/hyperloglog_cmds_test.h create mode 100644 controller/thirdparty/redis-plus-plus-1.1.1/test/src/sw/redis++/hyperloglog_cmds_test.hpp create mode 100644 controller/thirdparty/redis-plus-plus-1.1.1/test/src/sw/redis++/keys_cmds_test.h create mode 100644 controller/thirdparty/redis-plus-plus-1.1.1/test/src/sw/redis++/keys_cmds_test.hpp create mode 100644 controller/thirdparty/redis-plus-plus-1.1.1/test/src/sw/redis++/list_cmds_test.h create mode 100644 controller/thirdparty/redis-plus-plus-1.1.1/test/src/sw/redis++/list_cmds_test.hpp create mode 100644 controller/thirdparty/redis-plus-plus-1.1.1/test/src/sw/redis++/pipeline_transaction_test.h create mode 100644 controller/thirdparty/redis-plus-plus-1.1.1/test/src/sw/redis++/pipeline_transaction_test.hpp create mode 100644 controller/thirdparty/redis-plus-plus-1.1.1/test/src/sw/redis++/pubsub_test.h create mode 100644 controller/thirdparty/redis-plus-plus-1.1.1/test/src/sw/redis++/pubsub_test.hpp create mode 100644 controller/thirdparty/redis-plus-plus-1.1.1/test/src/sw/redis++/sanity_test.h create mode 100644 controller/thirdparty/redis-plus-plus-1.1.1/test/src/sw/redis++/sanity_test.hpp create mode 100644 controller/thirdparty/redis-plus-plus-1.1.1/test/src/sw/redis++/script_cmds_test.h create mode 100644 controller/thirdparty/redis-plus-plus-1.1.1/test/src/sw/redis++/script_cmds_test.hpp create mode 100644 controller/thirdparty/redis-plus-plus-1.1.1/test/src/sw/redis++/set_cmds_test.h create mode 100644 controller/thirdparty/redis-plus-plus-1.1.1/test/src/sw/redis++/set_cmds_test.hpp create mode 100644 controller/thirdparty/redis-plus-plus-1.1.1/test/src/sw/redis++/stream_cmds_test.h create mode 100644 controller/thirdparty/redis-plus-plus-1.1.1/test/src/sw/redis++/stream_cmds_test.hpp create mode 100644 controller/thirdparty/redis-plus-plus-1.1.1/test/src/sw/redis++/string_cmds_test.h create mode 100644 controller/thirdparty/redis-plus-plus-1.1.1/test/src/sw/redis++/string_cmds_test.hpp create mode 100644 controller/thirdparty/redis-plus-plus-1.1.1/test/src/sw/redis++/test_main.cpp create mode 100644 controller/thirdparty/redis-plus-plus-1.1.1/test/src/sw/redis++/threads_test.h create mode 100644 controller/thirdparty/redis-plus-plus-1.1.1/test/src/sw/redis++/threads_test.hpp create mode 100644 controller/thirdparty/redis-plus-plus-1.1.1/test/src/sw/redis++/utils.h create mode 100644 controller/thirdparty/redis-plus-plus-1.1.1/test/src/sw/redis++/zset_cmds_test.h create mode 100644 controller/thirdparty/redis-plus-plus-1.1.1/test/src/sw/redis++/zset_cmds_test.hpp diff --git a/controller/thirdparty/hiredis-0.14.1/.gitignore b/controller/thirdparty/hiredis-0.14.1/.gitignore new file mode 100644 index 000000000..c44b5c537 --- /dev/null +++ b/controller/thirdparty/hiredis-0.14.1/.gitignore @@ -0,0 +1,7 @@ +/hiredis-test +/examples/hiredis-example* +/*.o +/*.so +/*.dylib +/*.a +/*.pc diff --git a/controller/thirdparty/hiredis-0.14.1/.travis.yml b/controller/thirdparty/hiredis-0.14.1/.travis.yml new file mode 100644 index 000000000..faf2ce684 --- /dev/null +++ b/controller/thirdparty/hiredis-0.14.1/.travis.yml @@ -0,0 +1,45 @@ +language: c +sudo: false +compiler: + - gcc + - clang + +os: + - linux + - osx + +branches: + only: + - staging + - trying + - master + +before_script: + - if [ "$TRAVIS_OS_NAME" == "osx" ] ; then brew update; brew install redis; fi + +addons: + apt: + packages: + - libc6-dbg + - libc6-dev + - libc6:i386 + - libc6-dev-i386 + - libc6-dbg:i386 + - gcc-multilib + - valgrind + +env: + - CFLAGS="-Werror" + - PRE="valgrind --track-origins=yes --leak-check=full" + - TARGET="32bit" TARGET_VARS="32bit-vars" CFLAGS="-Werror" + - TARGET="32bit" TARGET_VARS="32bit-vars" PRE="valgrind --track-origins=yes --leak-check=full" + +matrix: + exclude: + - os: osx + env: PRE="valgrind --track-origins=yes --leak-check=full" + + - os: osx + env: TARGET="32bit" TARGET_VARS="32bit-vars" PRE="valgrind --track-origins=yes --leak-check=full" + +script: make $TARGET CFLAGS="$CFLAGS" && make check PRE="$PRE" && make $TARGET_VARS hiredis-example diff --git a/controller/thirdparty/hiredis-0.14.1/CHANGELOG.md b/controller/thirdparty/hiredis-0.14.1/CHANGELOG.md new file mode 100644 index 000000000..f8e577369 --- /dev/null +++ b/controller/thirdparty/hiredis-0.14.1/CHANGELOG.md @@ -0,0 +1,190 @@ +**NOTE: BREAKING CHANGES upgrading from 0.13.x to 0.14.x **: + +* Bulk and multi-bulk lengths less than -1 or greater than `LLONG_MAX` are now + protocol errors. This is consistent with the RESP specification. On 32-bit + platforms, the upper bound is lowered to `SIZE_MAX`. + +* Change `redisReply.len` to `size_t`, as it denotes the the size of a string + + User code should compare this to `size_t` values as well. If it was used to + compare to other values, casting might be necessary or can be removed, if + casting was applied before. + +### 0.14.1 (2020-03-13) + +* Adds safe allocation wrappers (CVE-2020-7105, #747, #752) (Michael Grunder) + +### 0.14.0 (2018-09-25) + +* Make string2ll static to fix conflict with Redis (Tom Lee [c3188b]) +* Use -dynamiclib instead of -shared for OSX (Ryan Schmidt [a65537]) +* Use string2ll from Redis w/added tests (Michael Grunder [7bef04, 60f622]) +* Makefile - OSX compilation fixes (Ryan Schmidt [881fcb, 0e9af8]) +* Remove redundant NULL checks (Justin Brewer [54acc8, 58e6b8]) +* Fix bulk and multi-bulk length truncation (Justin Brewer [109197]) +* Fix SIGSEGV in OpenBSD by checking for NULL before calling freeaddrinfo (Justin Brewer [546d94]) +* Several POSIX compatibility fixes (Justin Brewer [bbeab8, 49bbaa, d1c1b6]) +* Makefile - Compatibility fixes (Dimitri Vorobiev [3238cf, 12a9d1]) +* Makefile - Fix make install on FreeBSD (Zach Shipko [a2ef2b]) +* Makefile - don't assume $(INSTALL) is cp (Igor Gnatenko [725a96]) +* Separate side-effect causing function from assert and small cleanup (amallia [b46413, 3c3234]) +* Don't send negative values to `__redisAsyncCommand` (Frederik Deweerdt [706129]) +* Fix leak if setsockopt fails (Frederik Deweerdt [e21c9c]) +* Fix libevent leak (zfz [515228]) +* Clean up GCC warning (Ichito Nagata [2ec774]) +* Keep track of errno in `__redisSetErrorFromErrno()` as snprintf may use it (Jin Qing [25cd88]) +* Solaris compilation fix (Donald Whyte [41b07d]) +* Reorder linker arguments when building examples (Tustfarm-heart [06eedd]) +* Keep track of subscriptions in case of rapid subscribe/unsubscribe (Hyungjin Kim [073dc8, be76c5, d46999]) +* libuv use after free fix (Paul Scott [cbb956]) +* Properly close socket fd on reconnect attempt (WSL [64d1ec]) +* Skip valgrind in OSX tests (Jan-Erik Rediger [9deb78]) +* Various updates for Travis testing OSX (Ted Nyman [fa3774, 16a459, bc0ea5]) +* Update libevent (Chris Xin [386802]) +* Change sds.h for building in C++ projects (Ali Volkan ATLI [f5b32e]) +* Use proper format specifier in redisFormatSdsCommandArgv (Paulino Huerta, Jan-Erik Rediger [360a06, 8655a6]) +* Better handling of NULL reply in example code (Jan-Erik Rediger [1b8ed3]) +* Prevent overflow when formatting an error (Jan-Erik Rediger [0335cb]) +* Compatibility fix for strerror_r (Tom Lee [bb1747]) +* Properly detect integer parse/overflow errors (Justin Brewer [93421f]) +* Adds CI for Windows and cygwin fixes (owent, [6c53d6, 6c3e40]) +* Catch a buffer overflow when formatting the error message +* Import latest upstream sds. This breaks applications that are linked against the old hiredis v0.13 +* Fix warnings, when compiled with -Wshadow +* Make hiredis compile in Cygwin on Windows, now CI-tested + +**BREAKING CHANGES**: + +* Remove backwards compatibility macro's + +This removes the following old function aliases, use the new name now: + +| Old | New | +| --------------------------- | ---------------------- | +| redisReplyReaderCreate | redisReaderCreate | +| redisReplyReaderCreate | redisReaderCreate | +| redisReplyReaderFree | redisReaderFree | +| redisReplyReaderFeed | redisReaderFeed | +| redisReplyReaderGetReply | redisReaderGetReply | +| redisReplyReaderSetPrivdata | redisReaderSetPrivdata | +| redisReplyReaderGetObject | redisReaderGetObject | +| redisReplyReaderGetError | redisReaderGetError | + +* The `DEBUG` variable in the Makefile was renamed to `DEBUG_FLAGS` + +Previously it broke some builds for people that had `DEBUG` set to some arbitrary value, +due to debugging other software. +By renaming we avoid unintentional name clashes. + +Simply rename `DEBUG` to `DEBUG_FLAGS` in your environment to make it working again. + +### 0.13.3 (2015-09-16) + +* Revert "Clear `REDIS_CONNECTED` flag when connection is closed". +* Make tests pass on FreeBSD (Thanks, Giacomo Olgeni) + + +If the `REDIS_CONNECTED` flag is cleared, +the async onDisconnect callback function will never be called. +This causes problems as the disconnect is never reported back to the user. + +### 0.13.2 (2015-08-25) + +* Prevent crash on pending replies in async code (Thanks, @switch-st) +* Clear `REDIS_CONNECTED` flag when connection is closed (Thanks, Jerry Jacobs) +* Add MacOS X addapter (Thanks, @dizzus) +* Add Qt adapter (Thanks, Pietro Cerutti) +* Add Ivykis adapter (Thanks, Gergely Nagy) + +All adapters are provided as is and are only tested where possible. + +### 0.13.1 (2015-05-03) + +This is a bug fix release. +The new `reconnect` method introduced new struct members, which clashed with pre-defined names in pre-C99 code. +Another commit forced C99 compilation just to make it work, but of course this is not desirable for outside projects. +Other non-C99 code can now use hiredis as usual again. +Sorry for the inconvenience. + +* Fix memory leak in async reply handling (Salvatore Sanfilippo) +* Rename struct member to avoid name clash with pre-c99 code (Alex Balashov, ncopa) + +### 0.13.0 (2015-04-16) + +This release adds a minimal Windows compatibility layer. +The parser, standalone since v0.12.0, can now be compiled on Windows +(and thus used in other client libraries as well) + +* Windows compatibility layer for parser code (tzickel) +* Properly escape data printed to PKGCONF file (Dan Skorupski) +* Fix tests when assert() undefined (Keith Bennett, Matt Stancliff) +* Implement a reconnect method for the client context, this changes the structure of `redisContext` (Aaron Bedra) + +### 0.12.1 (2015-01-26) + +* Fix `make install`: DESTDIR support, install all required files, install PKGCONF in proper location +* Fix `make test` as 32 bit build on 64 bit platform + +### 0.12.0 (2015-01-22) + +* Add optional KeepAlive support + +* Try again on EINTR errors + +* Add libuv adapter + +* Add IPv6 support + +* Remove possibility of multiple close on same fd + +* Add ability to bind source address on connect + +* Add redisConnectFd() and redisFreeKeepFd() + +* Fix getaddrinfo() memory leak + +* Free string if it is unused (fixes memory leak) + +* Improve redisAppendCommandArgv performance 2.5x + +* Add support for SO_REUSEADDR + +* Fix redisvFormatCommand format parsing + +* Add GLib 2.0 adapter + +* Refactor reading code into read.c + +* Fix errno error buffers to not clobber errors + +* Generate pkgconf during build + +* Silence _BSD_SOURCE warnings + +* Improve digit counting for multibulk creation + + +### 0.11.0 + +* Increase the maximum multi-bulk reply depth to 7. + +* Increase the read buffer size from 2k to 16k. + +* Use poll(2) instead of select(2) to support large fds (>= 1024). + +### 0.10.1 + +* Makefile overhaul. Important to check out if you override one or more + variables using environment variables or via arguments to the "make" tool. + +* Issue #45: Fix potential memory leak for a multi bulk reply with 0 elements + being created by the default reply object functions. + +* Issue #43: Don't crash in an asynchronous context when Redis returns an error + reply after the connection has been made (this happens when the maximum + number of connections is reached). + +### 0.10.0 + +* See commit log. + diff --git a/controller/thirdparty/hiredis-0.14.1/COPYING b/controller/thirdparty/hiredis-0.14.1/COPYING new file mode 100644 index 000000000..a5fc97395 --- /dev/null +++ b/controller/thirdparty/hiredis-0.14.1/COPYING @@ -0,0 +1,29 @@ +Copyright (c) 2009-2011, Salvatore Sanfilippo +Copyright (c) 2010-2011, Pieter Noordhuis + +All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are met: + +* Redistributions of source code must retain the above copyright notice, + this list of conditions and the following disclaimer. + +* Redistributions in binary form must reproduce the above copyright notice, + this list of conditions and the following disclaimer in the documentation + and/or other materials provided with the distribution. + +* Neither the name of Redis nor the names of its contributors may be used + to endorse or promote products derived from this software without specific + prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND +ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED +WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE +DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR +ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES +(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; +LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON +ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS +SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. diff --git a/controller/thirdparty/hiredis-0.14.1/Makefile b/controller/thirdparty/hiredis-0.14.1/Makefile new file mode 100644 index 000000000..d1f005af4 --- /dev/null +++ b/controller/thirdparty/hiredis-0.14.1/Makefile @@ -0,0 +1,214 @@ +# Hiredis Makefile +# Copyright (C) 2010-2011 Salvatore Sanfilippo +# Copyright (C) 2010-2011 Pieter Noordhuis +# This file is released under the BSD license, see the COPYING file + +OBJ=net.o hiredis.o sds.o async.o read.o alloc.o +EXAMPLES=hiredis-example hiredis-example-libevent hiredis-example-libev hiredis-example-glib +TESTS=hiredis-test +LIBNAME=libhiredis +PKGCONFNAME=hiredis.pc + +HIREDIS_MAJOR=$(shell grep HIREDIS_MAJOR hiredis.h | awk '{print $$3}') +HIREDIS_MINOR=$(shell grep HIREDIS_MINOR hiredis.h | awk '{print $$3}') +HIREDIS_PATCH=$(shell grep HIREDIS_PATCH hiredis.h | awk '{print $$3}') +HIREDIS_SONAME=$(shell grep HIREDIS_SONAME hiredis.h | awk '{print $$3}') + +# Installation related variables and target +PREFIX?=/usr/local +INCLUDE_PATH?=include/hiredis +LIBRARY_PATH?=lib +PKGCONF_PATH?=pkgconfig +INSTALL_INCLUDE_PATH= $(DESTDIR)$(PREFIX)/$(INCLUDE_PATH) +INSTALL_LIBRARY_PATH= $(DESTDIR)$(PREFIX)/$(LIBRARY_PATH) +INSTALL_PKGCONF_PATH= $(INSTALL_LIBRARY_PATH)/$(PKGCONF_PATH) + +# redis-server configuration used for testing +REDIS_PORT=56379 +REDIS_SERVER=redis-server +define REDIS_TEST_CONFIG + daemonize yes + pidfile /tmp/hiredis-test-redis.pid + port $(REDIS_PORT) + bind 127.0.0.1 + unixsocket /tmp/hiredis-test-redis.sock +endef +export REDIS_TEST_CONFIG + +# Fallback to gcc when $CC is not in $PATH. +CC:=$(shell sh -c 'type $${CC%% *} >/dev/null 2>/dev/null && echo $(CC) || echo gcc') +CXX:=$(shell sh -c 'type $${CXX%% *} >/dev/null 2>/dev/null && echo $(CXX) || echo g++') +OPTIMIZATION?=-O3 +WARNINGS=-Wall -W -Wstrict-prototypes -Wwrite-strings +DEBUG_FLAGS?= -g -ggdb +REAL_CFLAGS=$(OPTIMIZATION) -fPIC $(CFLAGS) $(WARNINGS) $(DEBUG_FLAGS) +REAL_LDFLAGS=$(LDFLAGS) + +DYLIBSUFFIX=so +STLIBSUFFIX=a +DYLIB_MINOR_NAME=$(LIBNAME).$(DYLIBSUFFIX).$(HIREDIS_SONAME) +DYLIB_MAJOR_NAME=$(LIBNAME).$(DYLIBSUFFIX).$(HIREDIS_MAJOR) +DYLIBNAME=$(LIBNAME).$(DYLIBSUFFIX) +DYLIB_MAKE_CMD=$(CC) -shared -Wl,-soname,$(DYLIB_MINOR_NAME) -o $(DYLIBNAME) $(LDFLAGS) +STLIBNAME=$(LIBNAME).$(STLIBSUFFIX) +STLIB_MAKE_CMD=ar rcs $(STLIBNAME) + +# Platform-specific overrides +uname_S := $(shell sh -c 'uname -s 2>/dev/null || echo not') +ifeq ($(uname_S),SunOS) + REAL_LDFLAGS+= -ldl -lnsl -lsocket + DYLIB_MAKE_CMD=$(CC) -G -o $(DYLIBNAME) -h $(DYLIB_MINOR_NAME) $(LDFLAGS) +endif +ifeq ($(uname_S),Darwin) + DYLIBSUFFIX=dylib + DYLIB_MINOR_NAME=$(LIBNAME).$(HIREDIS_SONAME).$(DYLIBSUFFIX) + DYLIB_MAKE_CMD=$(CC) -dynamiclib -Wl,-install_name,$(PREFIX)/$(LIBRARY_PATH)/$(DYLIB_MINOR_NAME) -o $(DYLIBNAME) $(LDFLAGS) +endif + +all: $(DYLIBNAME) $(STLIBNAME) hiredis-test $(PKGCONFNAME) + +# Deps (use make dep to generate this) +alloc.o: alloc.c fmacros.h alloc.h +async.o: async.c fmacros.h alloc.h async.h hiredis.h read.h sds.h net.h dict.c dict.h +dict.o: dict.c fmacros.h alloc.h dict.h +hiredis.o: hiredis.c fmacros.h hiredis.h read.h sds.h alloc.h net.h +net.o: net.c fmacros.h net.h hiredis.h read.h sds.h alloc.h +read.o: read.c fmacros.h read.h sds.h +sds.o: sds.c sds.h sdsalloc.h +test.o: test.c fmacros.h hiredis.h read.h sds.h alloc.h net.h + +$(DYLIBNAME): $(OBJ) + $(DYLIB_MAKE_CMD) $(OBJ) + +$(STLIBNAME): $(OBJ) + $(STLIB_MAKE_CMD) $(OBJ) + +dynamic: $(DYLIBNAME) +static: $(STLIBNAME) + +# Binaries: +hiredis-example-libevent: examples/example-libevent.c adapters/libevent.h $(STLIBNAME) + $(CC) -o examples/$@ $(REAL_CFLAGS) $(REAL_LDFLAGS) -I. $< -levent $(STLIBNAME) + +hiredis-example-libev: examples/example-libev.c adapters/libev.h $(STLIBNAME) + $(CC) -o examples/$@ $(REAL_CFLAGS) $(REAL_LDFLAGS) -I. $< -lev $(STLIBNAME) + +hiredis-example-glib: examples/example-glib.c adapters/glib.h $(STLIBNAME) + $(CC) -o examples/$@ $(REAL_CFLAGS) $(REAL_LDFLAGS) -I. $< $(shell pkg-config --cflags --libs glib-2.0) $(STLIBNAME) + +hiredis-example-ivykis: examples/example-ivykis.c adapters/ivykis.h $(STLIBNAME) + $(CC) -o examples/$@ $(REAL_CFLAGS) $(REAL_LDFLAGS) -I. $< -livykis $(STLIBNAME) + +hiredis-example-macosx: examples/example-macosx.c adapters/macosx.h $(STLIBNAME) + $(CC) -o examples/$@ $(REAL_CFLAGS) $(REAL_LDFLAGS) -I. $< -framework CoreFoundation $(STLIBNAME) + +ifndef AE_DIR +hiredis-example-ae: + @echo "Please specify AE_DIR (e.g. /src)" + @false +else +hiredis-example-ae: examples/example-ae.c adapters/ae.h $(STLIBNAME) + $(CC) -o examples/$@ $(REAL_CFLAGS) $(REAL_LDFLAGS) -I. -I$(AE_DIR) $< $(AE_DIR)/ae.o $(AE_DIR)/zmalloc.o $(AE_DIR)/../deps/jemalloc/lib/libjemalloc.a -pthread $(STLIBNAME) +endif + +ifndef LIBUV_DIR +hiredis-example-libuv: + @echo "Please specify LIBUV_DIR (e.g. ../libuv/)" + @false +else +hiredis-example-libuv: examples/example-libuv.c adapters/libuv.h $(STLIBNAME) + $(CC) -o examples/$@ $(REAL_CFLAGS) $(REAL_LDFLAGS) -I. -I$(LIBUV_DIR)/include $< $(LIBUV_DIR)/.libs/libuv.a -lpthread -lrt $(STLIBNAME) +endif + +ifeq ($(and $(QT_MOC),$(QT_INCLUDE_DIR),$(QT_LIBRARY_DIR)),) +hiredis-example-qt: + @echo "Please specify QT_MOC, QT_INCLUDE_DIR AND QT_LIBRARY_DIR" + @false +else +hiredis-example-qt: examples/example-qt.cpp adapters/qt.h $(STLIBNAME) + $(QT_MOC) adapters/qt.h -I. -I$(QT_INCLUDE_DIR) -I$(QT_INCLUDE_DIR)/QtCore | \ + $(CXX) -x c++ -o qt-adapter-moc.o -c - $(REAL_CFLAGS) -I. -I$(QT_INCLUDE_DIR) -I$(QT_INCLUDE_DIR)/QtCore + $(QT_MOC) examples/example-qt.h -I. -I$(QT_INCLUDE_DIR) -I$(QT_INCLUDE_DIR)/QtCore | \ + $(CXX) -x c++ -o qt-example-moc.o -c - $(REAL_CFLAGS) -I. -I$(QT_INCLUDE_DIR) -I$(QT_INCLUDE_DIR)/QtCore + $(CXX) -o examples/$@ $(REAL_CFLAGS) $(REAL_LDFLAGS) -I. -I$(QT_INCLUDE_DIR) -I$(QT_INCLUDE_DIR)/QtCore -L$(QT_LIBRARY_DIR) qt-adapter-moc.o qt-example-moc.o $< -pthread $(STLIBNAME) -lQtCore +endif + +hiredis-example: examples/example.c $(STLIBNAME) + $(CC) -o examples/$@ $(REAL_CFLAGS) $(REAL_LDFLAGS) -I. $< $(STLIBNAME) + +examples: $(EXAMPLES) + +hiredis-test: test.o $(STLIBNAME) + +hiredis-%: %.o $(STLIBNAME) + $(CC) $(REAL_CFLAGS) -o $@ $(REAL_LDFLAGS) $< $(STLIBNAME) + +test: hiredis-test + ./hiredis-test + +check: hiredis-test + @echo "$$REDIS_TEST_CONFIG" | $(REDIS_SERVER) - + $(PRE) ./hiredis-test -h 127.0.0.1 -p $(REDIS_PORT) -s /tmp/hiredis-test-redis.sock || \ + ( kill `cat /tmp/hiredis-test-redis.pid` && false ) + kill `cat /tmp/hiredis-test-redis.pid` + +.c.o: + $(CC) -std=c99 -pedantic -c $(REAL_CFLAGS) $< + +clean: + rm -rf $(DYLIBNAME) $(STLIBNAME) $(TESTS) $(PKGCONFNAME) examples/hiredis-example* *.o *.gcda *.gcno *.gcov + +dep: + $(CC) -MM *.c + +INSTALL?= cp -pPR + +$(PKGCONFNAME): hiredis.h + @echo "Generating $@ for pkgconfig..." + @echo prefix=$(PREFIX) > $@ + @echo exec_prefix=\$${prefix} >> $@ + @echo libdir=$(PREFIX)/$(LIBRARY_PATH) >> $@ + @echo includedir=$(PREFIX)/$(INCLUDE_PATH) >> $@ + @echo >> $@ + @echo Name: hiredis >> $@ + @echo Description: Minimalistic C client library for Redis. >> $@ + @echo Version: $(HIREDIS_MAJOR).$(HIREDIS_MINOR).$(HIREDIS_PATCH) >> $@ + @echo Libs: -L\$${libdir} -lhiredis >> $@ + @echo Cflags: -I\$${includedir} -D_FILE_OFFSET_BITS=64 >> $@ + +install: $(DYLIBNAME) $(STLIBNAME) $(PKGCONFNAME) + mkdir -p $(INSTALL_INCLUDE_PATH) $(INSTALL_INCLUDE_PATH)/adapters $(INSTALL_LIBRARY_PATH) + $(INSTALL) hiredis.h async.h read.h sds.h alloc.h $(INSTALL_INCLUDE_PATH) + $(INSTALL) adapters/*.h $(INSTALL_INCLUDE_PATH)/adapters + $(INSTALL) $(DYLIBNAME) $(INSTALL_LIBRARY_PATH)/$(DYLIB_MINOR_NAME) + cd $(INSTALL_LIBRARY_PATH) && ln -sf $(DYLIB_MINOR_NAME) $(DYLIBNAME) + $(INSTALL) $(STLIBNAME) $(INSTALL_LIBRARY_PATH) + mkdir -p $(INSTALL_PKGCONF_PATH) + $(INSTALL) $(PKGCONFNAME) $(INSTALL_PKGCONF_PATH) + +32bit: + @echo "" + @echo "WARNING: if this fails under Linux you probably need to install libc6-dev-i386" + @echo "" + $(MAKE) CFLAGS="-m32" LDFLAGS="-m32" + +32bit-vars: + $(eval CFLAGS=-m32) + $(eval LDFLAGS=-m32) + +gprof: + $(MAKE) CFLAGS="-pg" LDFLAGS="-pg" + +gcov: + $(MAKE) CFLAGS="-fprofile-arcs -ftest-coverage" LDFLAGS="-fprofile-arcs" + +coverage: gcov + make check + mkdir -p tmp/lcov + lcov -d . -c -o tmp/lcov/hiredis.info + genhtml --legend -o tmp/lcov/report tmp/lcov/hiredis.info + +noopt: + $(MAKE) OPTIMIZATION="" + +.PHONY: all test check clean dep install 32bit 32bit-vars gprof gcov noopt diff --git a/controller/thirdparty/hiredis-0.14.1/README.md b/controller/thirdparty/hiredis-0.14.1/README.md new file mode 100644 index 000000000..50e2e6be6 --- /dev/null +++ b/controller/thirdparty/hiredis-0.14.1/README.md @@ -0,0 +1,410 @@ +[![Build Status](https://travis-ci.org/redis/hiredis.png)](https://travis-ci.org/redis/hiredis) + +**This Readme reflects the latest changed in the master branch. See [v0.14.1](https://github.com/redis/hiredis/tree/v0.14.1) for the Readme and documentation for the latest release.** + +# HIREDIS + +Hiredis is a minimalistic C client library for the [Redis](http://redis.io/) database. + +It is minimalistic because it just adds minimal support for the protocol, but +at the same time it uses a high level printf-alike API in order to make it +much higher level than otherwise suggested by its minimal code base and the +lack of explicit bindings for every Redis command. + +Apart from supporting sending commands and receiving replies, it comes with +a reply parser that is decoupled from the I/O layer. It +is a stream parser designed for easy reusability, which can for instance be used +in higher level language bindings for efficient reply parsing. + +Hiredis only supports the binary-safe Redis protocol, so you can use it with any +Redis version >= 1.2.0. + +The library comes with multiple APIs. There is the +*synchronous API*, the *asynchronous API* and the *reply parsing API*. + +## IMPORTANT: Breaking changes when upgrading from 0.13.x -> 0.14.x + +Bulk and multi-bulk lengths less than -1 or greater than `LLONG_MAX` are now +protocol errors. This is consistent with the RESP specification. On 32-bit +platforms, the upper bound is lowered to `SIZE_MAX`. + +Change `redisReply.len` to `size_t`, as it denotes the the size of a string + +User code should compare this to `size_t` values as well. If it was used to +compare to other values, casting might be necessary or can be removed, if +casting was applied before. + +For a detailed list of changes please view our [Changelog](CHANGELOG.md). + +## Synchronous API + +To consume the synchronous API, there are only a few function calls that need to be introduced: + +```c +redisContext *redisConnect(const char *ip, int port); +void *redisCommand(redisContext *c, const char *format, ...); +void freeReplyObject(void *reply); +``` + +### Connecting + +The function `redisConnect` is used to create a so-called `redisContext`. The +context is where Hiredis holds state for a connection. The `redisContext` +struct has an integer `err` field that is non-zero when the connection is in +an error state. The field `errstr` will contain a string with a description of +the error. More information on errors can be found in the **Errors** section. +After trying to connect to Redis using `redisConnect` you should +check the `err` field to see if establishing the connection was successful: +```c +redisContext *c = redisConnect("127.0.0.1", 6379); +if (c == NULL || c->err) { + if (c) { + printf("Error: %s\n", c->errstr); + // handle error + } else { + printf("Can't allocate redis context\n"); + } +} +``` + +*Note: A `redisContext` is not thread-safe.* + +### Sending commands + +There are several ways to issue commands to Redis. The first that will be introduced is +`redisCommand`. This function takes a format similar to printf. In the simplest form, +it is used like this: +```c +reply = redisCommand(context, "SET foo bar"); +``` + +The specifier `%s` interpolates a string in the command, and uses `strlen` to +determine the length of the string: +```c +reply = redisCommand(context, "SET foo %s", value); +``` +When you need to pass binary safe strings in a command, the `%b` specifier can be +used. Together with a pointer to the string, it requires a `size_t` length argument +of the string: +```c +reply = redisCommand(context, "SET foo %b", value, (size_t) valuelen); +``` +Internally, Hiredis splits the command in different arguments and will +convert it to the protocol used to communicate with Redis. +One or more spaces separates arguments, so you can use the specifiers +anywhere in an argument: +```c +reply = redisCommand(context, "SET key:%s %s", myid, value); +``` + +### Using replies + +The return value of `redisCommand` holds a reply when the command was +successfully executed. When an error occurs, the return value is `NULL` and +the `err` field in the context will be set (see section on **Errors**). +Once an error is returned the context cannot be reused and you should set up +a new connection. + +The standard replies that `redisCommand` are of the type `redisReply`. The +`type` field in the `redisReply` should be used to test what kind of reply +was received: + +* **`REDIS_REPLY_STATUS`**: + * The command replied with a status reply. The status string can be accessed using `reply->str`. + The length of this string can be accessed using `reply->len`. + +* **`REDIS_REPLY_ERROR`**: + * The command replied with an error. The error string can be accessed identical to `REDIS_REPLY_STATUS`. + +* **`REDIS_REPLY_INTEGER`**: + * The command replied with an integer. The integer value can be accessed using the + `reply->integer` field of type `long long`. + +* **`REDIS_REPLY_NIL`**: + * The command replied with a **nil** object. There is no data to access. + +* **`REDIS_REPLY_STRING`**: + * A bulk (string) reply. The value of the reply can be accessed using `reply->str`. + The length of this string can be accessed using `reply->len`. + +* **`REDIS_REPLY_ARRAY`**: + * A multi bulk reply. The number of elements in the multi bulk reply is stored in + `reply->elements`. Every element in the multi bulk reply is a `redisReply` object as well + and can be accessed via `reply->element[..index..]`. + Redis may reply with nested arrays but this is fully supported. + +Replies should be freed using the `freeReplyObject()` function. +Note that this function will take care of freeing sub-reply objects +contained in arrays and nested arrays, so there is no need for the user to +free the sub replies (it is actually harmful and will corrupt the memory). + +**Important:** the current version of hiredis (0.10.0) frees replies when the +asynchronous API is used. This means you should not call `freeReplyObject` when +you use this API. The reply is cleaned up by hiredis _after_ the callback +returns. This behavior will probably change in future releases, so make sure to +keep an eye on the changelog when upgrading (see issue #39). + +### Cleaning up + +To disconnect and free the context the following function can be used: +```c +void redisFree(redisContext *c); +``` +This function immediately closes the socket and then frees the allocations done in +creating the context. + +### Sending commands (cont'd) + +Together with `redisCommand`, the function `redisCommandArgv` can be used to issue commands. +It has the following prototype: +```c +void *redisCommandArgv(redisContext *c, int argc, const char **argv, const size_t *argvlen); +``` +It takes the number of arguments `argc`, an array of strings `argv` and the lengths of the +arguments `argvlen`. For convenience, `argvlen` may be set to `NULL` and the function will +use `strlen(3)` on every argument to determine its length. Obviously, when any of the arguments +need to be binary safe, the entire array of lengths `argvlen` should be provided. + +The return value has the same semantic as `redisCommand`. + +### Pipelining + +To explain how Hiredis supports pipelining in a blocking connection, there needs to be +understanding of the internal execution flow. + +When any of the functions in the `redisCommand` family is called, Hiredis first formats the +command according to the Redis protocol. The formatted command is then put in the output buffer +of the context. This output buffer is dynamic, so it can hold any number of commands. +After the command is put in the output buffer, `redisGetReply` is called. This function has the +following two execution paths: + +1. The input buffer is non-empty: + * Try to parse a single reply from the input buffer and return it + * If no reply could be parsed, continue at *2* +2. The input buffer is empty: + * Write the **entire** output buffer to the socket + * Read from the socket until a single reply could be parsed + +The function `redisGetReply` is exported as part of the Hiredis API and can be used when a reply +is expected on the socket. To pipeline commands, the only things that needs to be done is +filling up the output buffer. For this cause, two commands can be used that are identical +to the `redisCommand` family, apart from not returning a reply: +```c +void redisAppendCommand(redisContext *c, const char *format, ...); +void redisAppendCommandArgv(redisContext *c, int argc, const char **argv, const size_t *argvlen); +``` +After calling either function one or more times, `redisGetReply` can be used to receive the +subsequent replies. The return value for this function is either `REDIS_OK` or `REDIS_ERR`, where +the latter means an error occurred while reading a reply. Just as with the other commands, +the `err` field in the context can be used to find out what the cause of this error is. + +The following examples shows a simple pipeline (resulting in only a single call to `write(2)` and +a single call to `read(2)`): +```c +redisReply *reply; +redisAppendCommand(context,"SET foo bar"); +redisAppendCommand(context,"GET foo"); +redisGetReply(context,&reply); // reply for SET +freeReplyObject(reply); +redisGetReply(context,&reply); // reply for GET +freeReplyObject(reply); +``` +This API can also be used to implement a blocking subscriber: +```c +reply = redisCommand(context,"SUBSCRIBE foo"); +freeReplyObject(reply); +while(redisGetReply(context,&reply) == REDIS_OK) { + // consume message + freeReplyObject(reply); +} +``` +### Errors + +When a function call is not successful, depending on the function either `NULL` or `REDIS_ERR` is +returned. The `err` field inside the context will be non-zero and set to one of the +following constants: + +* **`REDIS_ERR_IO`**: + There was an I/O error while creating the connection, trying to write + to the socket or read from the socket. If you included `errno.h` in your + application, you can use the global `errno` variable to find out what is + wrong. + +* **`REDIS_ERR_EOF`**: + The server closed the connection which resulted in an empty read. + +* **`REDIS_ERR_PROTOCOL`**: + There was an error while parsing the protocol. + +* **`REDIS_ERR_OTHER`**: + Any other error. Currently, it is only used when a specified hostname to connect + to cannot be resolved. + +In every case, the `errstr` field in the context will be set to hold a string representation +of the error. + +## Asynchronous API + +Hiredis comes with an asynchronous API that works easily with any event library. +Examples are bundled that show using Hiredis with [libev](http://software.schmorp.de/pkg/libev.html) +and [libevent](http://monkey.org/~provos/libevent/). + +### Connecting + +The function `redisAsyncConnect` can be used to establish a non-blocking connection to +Redis. It returns a pointer to the newly created `redisAsyncContext` struct. The `err` field +should be checked after creation to see if there were errors creating the connection. +Because the connection that will be created is non-blocking, the kernel is not able to +instantly return if the specified host and port is able to accept a connection. + +*Note: A `redisAsyncContext` is not thread-safe.* + +```c +redisAsyncContext *c = redisAsyncConnect("127.0.0.1", 6379); +if (c->err) { + printf("Error: %s\n", c->errstr); + // handle error +} +``` + +The asynchronous context can hold a disconnect callback function that is called when the +connection is disconnected (either because of an error or per user request). This function should +have the following prototype: +```c +void(const redisAsyncContext *c, int status); +``` +On a disconnect, the `status` argument is set to `REDIS_OK` when disconnection was initiated by the +user, or `REDIS_ERR` when the disconnection was caused by an error. When it is `REDIS_ERR`, the `err` +field in the context can be accessed to find out the cause of the error. + +The context object is always freed after the disconnect callback fired. When a reconnect is needed, +the disconnect callback is a good point to do so. + +Setting the disconnect callback can only be done once per context. For subsequent calls it will +return `REDIS_ERR`. The function to set the disconnect callback has the following prototype: +```c +int redisAsyncSetDisconnectCallback(redisAsyncContext *ac, redisDisconnectCallback *fn); +``` +### Sending commands and their callbacks + +In an asynchronous context, commands are automatically pipelined due to the nature of an event loop. +Therefore, unlike the synchronous API, there is only a single way to send commands. +Because commands are sent to Redis asynchronously, issuing a command requires a callback function +that is called when the reply is received. Reply callbacks should have the following prototype: +```c +void(redisAsyncContext *c, void *reply, void *privdata); +``` +The `privdata` argument can be used to curry arbitrary data to the callback from the point where +the command is initially queued for execution. + +The functions that can be used to issue commands in an asynchronous context are: +```c +int redisAsyncCommand( + redisAsyncContext *ac, redisCallbackFn *fn, void *privdata, + const char *format, ...); +int redisAsyncCommandArgv( + redisAsyncContext *ac, redisCallbackFn *fn, void *privdata, + int argc, const char **argv, const size_t *argvlen); +``` +Both functions work like their blocking counterparts. The return value is `REDIS_OK` when the command +was successfully added to the output buffer and `REDIS_ERR` otherwise. Example: when the connection +is being disconnected per user-request, no new commands may be added to the output buffer and `REDIS_ERR` is +returned on calls to the `redisAsyncCommand` family. + +If the reply for a command with a `NULL` callback is read, it is immediately freed. When the callback +for a command is non-`NULL`, the memory is freed immediately following the callback: the reply is only +valid for the duration of the callback. + +All pending callbacks are called with a `NULL` reply when the context encountered an error. + +### Disconnecting + +An asynchronous connection can be terminated using: +```c +void redisAsyncDisconnect(redisAsyncContext *ac); +``` +When this function is called, the connection is **not** immediately terminated. Instead, new +commands are no longer accepted and the connection is only terminated when all pending commands +have been written to the socket, their respective replies have been read and their respective +callbacks have been executed. After this, the disconnection callback is executed with the +`REDIS_OK` status and the context object is freed. + +### Hooking it up to event library *X* + +There are a few hooks that need to be set on the context object after it is created. +See the `adapters/` directory for bindings to *libev* and *libevent*. + +## Reply parsing API + +Hiredis comes with a reply parsing API that makes it easy for writing higher +level language bindings. + +The reply parsing API consists of the following functions: +```c +redisReader *redisReaderCreate(void); +void redisReaderFree(redisReader *reader); +int redisReaderFeed(redisReader *reader, const char *buf, size_t len); +int redisReaderGetReply(redisReader *reader, void **reply); +``` +The same set of functions are used internally by hiredis when creating a +normal Redis context, the above API just exposes it to the user for a direct +usage. + +### Usage + +The function `redisReaderCreate` creates a `redisReader` structure that holds a +buffer with unparsed data and state for the protocol parser. + +Incoming data -- most likely from a socket -- can be placed in the internal +buffer of the `redisReader` using `redisReaderFeed`. This function will make a +copy of the buffer pointed to by `buf` for `len` bytes. This data is parsed +when `redisReaderGetReply` is called. This function returns an integer status +and a reply object (as described above) via `void **reply`. The returned status +can be either `REDIS_OK` or `REDIS_ERR`, where the latter means something went +wrong (either a protocol error, or an out of memory error). + +The parser limits the level of nesting for multi bulk payloads to 7. If the +multi bulk nesting level is higher than this, the parser returns an error. + +### Customizing replies + +The function `redisReaderGetReply` creates `redisReply` and makes the function +argument `reply` point to the created `redisReply` variable. For instance, if +the response of type `REDIS_REPLY_STATUS` then the `str` field of `redisReply` +will hold the status as a vanilla C string. However, the functions that are +responsible for creating instances of the `redisReply` can be customized by +setting the `fn` field on the `redisReader` struct. This should be done +immediately after creating the `redisReader`. + +For example, [hiredis-rb](https://github.com/pietern/hiredis-rb/blob/master/ext/hiredis_ext/reader.c) +uses customized reply object functions to create Ruby objects. + +### Reader max buffer + +Both when using the Reader API directly or when using it indirectly via a +normal Redis context, the redisReader structure uses a buffer in order to +accumulate data from the server. +Usually this buffer is destroyed when it is empty and is larger than 16 +KiB in order to avoid wasting memory in unused buffers + +However when working with very big payloads destroying the buffer may slow +down performances considerably, so it is possible to modify the max size of +an idle buffer changing the value of the `maxbuf` field of the reader structure +to the desired value. The special value of 0 means that there is no maximum +value for an idle buffer, so the buffer will never get freed. + +For instance if you have a normal Redis context you can set the maximum idle +buffer to zero (unlimited) just with: +```c +context->reader->maxbuf = 0; +``` +This should be done only in order to maximize performances when working with +large payloads. The context should be set back to `REDIS_READER_MAX_BUF` again +as soon as possible in order to prevent allocation of useless memory. + +## AUTHORS + +Hiredis was written by Salvatore Sanfilippo (antirez at gmail) and +Pieter Noordhuis (pcnoordhuis at gmail) and is released under the BSD license. +Hiredis is currently maintained by Matt Stancliff (matt at genges dot com) and +Jan-Erik Rediger (janerik at fnordig dot com) diff --git a/controller/thirdparty/hiredis-0.14.1/adapters/ae.h b/controller/thirdparty/hiredis-0.14.1/adapters/ae.h new file mode 100644 index 000000000..03939928d --- /dev/null +++ b/controller/thirdparty/hiredis-0.14.1/adapters/ae.h @@ -0,0 +1,127 @@ +/* + * Copyright (c) 2010-2011, Pieter Noordhuis + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * * Neither the name of Redis nor the names of its contributors may be used + * to endorse or promote products derived from this software without + * specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +#ifndef __HIREDIS_AE_H__ +#define __HIREDIS_AE_H__ +#include +#include +#include "../hiredis.h" +#include "../async.h" + +typedef struct redisAeEvents { + redisAsyncContext *context; + aeEventLoop *loop; + int fd; + int reading, writing; +} redisAeEvents; + +static void redisAeReadEvent(aeEventLoop *el, int fd, void *privdata, int mask) { + ((void)el); ((void)fd); ((void)mask); + + redisAeEvents *e = (redisAeEvents*)privdata; + redisAsyncHandleRead(e->context); +} + +static void redisAeWriteEvent(aeEventLoop *el, int fd, void *privdata, int mask) { + ((void)el); ((void)fd); ((void)mask); + + redisAeEvents *e = (redisAeEvents*)privdata; + redisAsyncHandleWrite(e->context); +} + +static void redisAeAddRead(void *privdata) { + redisAeEvents *e = (redisAeEvents*)privdata; + aeEventLoop *loop = e->loop; + if (!e->reading) { + e->reading = 1; + aeCreateFileEvent(loop,e->fd,AE_READABLE,redisAeReadEvent,e); + } +} + +static void redisAeDelRead(void *privdata) { + redisAeEvents *e = (redisAeEvents*)privdata; + aeEventLoop *loop = e->loop; + if (e->reading) { + e->reading = 0; + aeDeleteFileEvent(loop,e->fd,AE_READABLE); + } +} + +static void redisAeAddWrite(void *privdata) { + redisAeEvents *e = (redisAeEvents*)privdata; + aeEventLoop *loop = e->loop; + if (!e->writing) { + e->writing = 1; + aeCreateFileEvent(loop,e->fd,AE_WRITABLE,redisAeWriteEvent,e); + } +} + +static void redisAeDelWrite(void *privdata) { + redisAeEvents *e = (redisAeEvents*)privdata; + aeEventLoop *loop = e->loop; + if (e->writing) { + e->writing = 0; + aeDeleteFileEvent(loop,e->fd,AE_WRITABLE); + } +} + +static void redisAeCleanup(void *privdata) { + redisAeEvents *e = (redisAeEvents*)privdata; + redisAeDelRead(privdata); + redisAeDelWrite(privdata); + free(e); +} + +static int redisAeAttach(aeEventLoop *loop, redisAsyncContext *ac) { + redisContext *c = &(ac->c); + redisAeEvents *e; + + /* Nothing should be attached when something is already attached */ + if (ac->ev.data != NULL) + return REDIS_ERR; + + /* Create container for context and r/w events */ + e = (redisAeEvents*)hi_malloc(sizeof(*e)); + e->context = ac; + e->loop = loop; + e->fd = c->fd; + e->reading = e->writing = 0; + + /* Register functions to start/stop listening for events */ + ac->ev.addRead = redisAeAddRead; + ac->ev.delRead = redisAeDelRead; + ac->ev.addWrite = redisAeAddWrite; + ac->ev.delWrite = redisAeDelWrite; + ac->ev.cleanup = redisAeCleanup; + ac->ev.data = e; + + return REDIS_OK; +} +#endif diff --git a/controller/thirdparty/hiredis-0.14.1/adapters/glib.h b/controller/thirdparty/hiredis-0.14.1/adapters/glib.h new file mode 100644 index 000000000..e0a6411d3 --- /dev/null +++ b/controller/thirdparty/hiredis-0.14.1/adapters/glib.h @@ -0,0 +1,153 @@ +#ifndef __HIREDIS_GLIB_H__ +#define __HIREDIS_GLIB_H__ + +#include + +#include "../hiredis.h" +#include "../async.h" + +typedef struct +{ + GSource source; + redisAsyncContext *ac; + GPollFD poll_fd; +} RedisSource; + +static void +redis_source_add_read (gpointer data) +{ + RedisSource *source = (RedisSource *)data; + g_return_if_fail(source); + source->poll_fd.events |= G_IO_IN; + g_main_context_wakeup(g_source_get_context((GSource *)data)); +} + +static void +redis_source_del_read (gpointer data) +{ + RedisSource *source = (RedisSource *)data; + g_return_if_fail(source); + source->poll_fd.events &= ~G_IO_IN; + g_main_context_wakeup(g_source_get_context((GSource *)data)); +} + +static void +redis_source_add_write (gpointer data) +{ + RedisSource *source = (RedisSource *)data; + g_return_if_fail(source); + source->poll_fd.events |= G_IO_OUT; + g_main_context_wakeup(g_source_get_context((GSource *)data)); +} + +static void +redis_source_del_write (gpointer data) +{ + RedisSource *source = (RedisSource *)data; + g_return_if_fail(source); + source->poll_fd.events &= ~G_IO_OUT; + g_main_context_wakeup(g_source_get_context((GSource *)data)); +} + +static void +redis_source_cleanup (gpointer data) +{ + RedisSource *source = (RedisSource *)data; + + g_return_if_fail(source); + + redis_source_del_read(source); + redis_source_del_write(source); + /* + * It is not our responsibility to remove ourself from the + * current main loop. However, we will remove the GPollFD. + */ + if (source->poll_fd.fd >= 0) { + g_source_remove_poll((GSource *)data, &source->poll_fd); + source->poll_fd.fd = -1; + } +} + +static gboolean +redis_source_prepare (GSource *source, + gint *timeout_) +{ + RedisSource *redis = (RedisSource *)source; + *timeout_ = -1; + return !!(redis->poll_fd.events & redis->poll_fd.revents); +} + +static gboolean +redis_source_check (GSource *source) +{ + RedisSource *redis = (RedisSource *)source; + return !!(redis->poll_fd.events & redis->poll_fd.revents); +} + +static gboolean +redis_source_dispatch (GSource *source, + GSourceFunc callback, + gpointer user_data) +{ + RedisSource *redis = (RedisSource *)source; + + if ((redis->poll_fd.revents & G_IO_OUT)) { + redisAsyncHandleWrite(redis->ac); + redis->poll_fd.revents &= ~G_IO_OUT; + } + + if ((redis->poll_fd.revents & G_IO_IN)) { + redisAsyncHandleRead(redis->ac); + redis->poll_fd.revents &= ~G_IO_IN; + } + + if (callback) { + return callback(user_data); + } + + return TRUE; +} + +static void +redis_source_finalize (GSource *source) +{ + RedisSource *redis = (RedisSource *)source; + + if (redis->poll_fd.fd >= 0) { + g_source_remove_poll(source, &redis->poll_fd); + redis->poll_fd.fd = -1; + } +} + +static GSource * +redis_source_new (redisAsyncContext *ac) +{ + static GSourceFuncs source_funcs = { + .prepare = redis_source_prepare, + .check = redis_source_check, + .dispatch = redis_source_dispatch, + .finalize = redis_source_finalize, + }; + redisContext *c = &ac->c; + RedisSource *source; + + g_return_val_if_fail(ac != NULL, NULL); + + source = (RedisSource *)g_source_new(&source_funcs, sizeof *source); + source->ac = ac; + source->poll_fd.fd = c->fd; + source->poll_fd.events = 0; + source->poll_fd.revents = 0; + g_source_add_poll((GSource *)source, &source->poll_fd); + + ac->ev.addRead = redis_source_add_read; + ac->ev.delRead = redis_source_del_read; + ac->ev.addWrite = redis_source_add_write; + ac->ev.delWrite = redis_source_del_write; + ac->ev.cleanup = redis_source_cleanup; + ac->ev.data = source; + + return (GSource *)source; +} + +#endif /* __HIREDIS_GLIB_H__ */ diff --git a/controller/thirdparty/hiredis-0.14.1/adapters/ivykis.h b/controller/thirdparty/hiredis-0.14.1/adapters/ivykis.h new file mode 100644 index 000000000..75616ee24 --- /dev/null +++ b/controller/thirdparty/hiredis-0.14.1/adapters/ivykis.h @@ -0,0 +1,81 @@ +#ifndef __HIREDIS_IVYKIS_H__ +#define __HIREDIS_IVYKIS_H__ +#include +#include "../hiredis.h" +#include "../async.h" + +typedef struct redisIvykisEvents { + redisAsyncContext *context; + struct iv_fd fd; +} redisIvykisEvents; + +static void redisIvykisReadEvent(void *arg) { + redisAsyncContext *context = (redisAsyncContext *)arg; + redisAsyncHandleRead(context); +} + +static void redisIvykisWriteEvent(void *arg) { + redisAsyncContext *context = (redisAsyncContext *)arg; + redisAsyncHandleWrite(context); +} + +static void redisIvykisAddRead(void *privdata) { + redisIvykisEvents *e = (redisIvykisEvents*)privdata; + iv_fd_set_handler_in(&e->fd, redisIvykisReadEvent); +} + +static void redisIvykisDelRead(void *privdata) { + redisIvykisEvents *e = (redisIvykisEvents*)privdata; + iv_fd_set_handler_in(&e->fd, NULL); +} + +static void redisIvykisAddWrite(void *privdata) { + redisIvykisEvents *e = (redisIvykisEvents*)privdata; + iv_fd_set_handler_out(&e->fd, redisIvykisWriteEvent); +} + +static void redisIvykisDelWrite(void *privdata) { + redisIvykisEvents *e = (redisIvykisEvents*)privdata; + iv_fd_set_handler_out(&e->fd, NULL); +} + +static void redisIvykisCleanup(void *privdata) { + redisIvykisEvents *e = (redisIvykisEvents*)privdata; + + iv_fd_unregister(&e->fd); + free(e); +} + +static int redisIvykisAttach(redisAsyncContext *ac) { + redisContext *c = &(ac->c); + redisIvykisEvents *e; + + /* Nothing should be attached when something is already attached */ + if (ac->ev.data != NULL) + return REDIS_ERR; + + /* Create container for context and r/w events */ + e = (redisIvykisEvents*)hi_malloc(sizeof(*e)); + e->context = ac; + + /* Register functions to start/stop listening for events */ + ac->ev.addRead = redisIvykisAddRead; + ac->ev.delRead = redisIvykisDelRead; + ac->ev.addWrite = redisIvykisAddWrite; + ac->ev.delWrite = redisIvykisDelWrite; + ac->ev.cleanup = redisIvykisCleanup; + ac->ev.data = e; + + /* Initialize and install read/write events */ + IV_FD_INIT(&e->fd); + e->fd.fd = c->fd; + e->fd.handler_in = redisIvykisReadEvent; + e->fd.handler_out = redisIvykisWriteEvent; + e->fd.handler_err = NULL; + e->fd.cookie = e->context; + + iv_fd_register(&e->fd); + + return REDIS_OK; +} +#endif diff --git a/controller/thirdparty/hiredis-0.14.1/adapters/libev.h b/controller/thirdparty/hiredis-0.14.1/adapters/libev.h new file mode 100644 index 000000000..abad43634 --- /dev/null +++ b/controller/thirdparty/hiredis-0.14.1/adapters/libev.h @@ -0,0 +1,147 @@ +/* + * Copyright (c) 2010-2011, Pieter Noordhuis + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * * Neither the name of Redis nor the names of its contributors may be used + * to endorse or promote products derived from this software without + * specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +#ifndef __HIREDIS_LIBEV_H__ +#define __HIREDIS_LIBEV_H__ +#include +#include +#include +#include "../hiredis.h" +#include "../async.h" + +typedef struct redisLibevEvents { + redisAsyncContext *context; + struct ev_loop *loop; + int reading, writing; + ev_io rev, wev; +} redisLibevEvents; + +static void redisLibevReadEvent(EV_P_ ev_io *watcher, int revents) { +#if EV_MULTIPLICITY + ((void)loop); +#endif + ((void)revents); + + redisLibevEvents *e = (redisLibevEvents*)watcher->data; + redisAsyncHandleRead(e->context); +} + +static void redisLibevWriteEvent(EV_P_ ev_io *watcher, int revents) { +#if EV_MULTIPLICITY + ((void)loop); +#endif + ((void)revents); + + redisLibevEvents *e = (redisLibevEvents*)watcher->data; + redisAsyncHandleWrite(e->context); +} + +static void redisLibevAddRead(void *privdata) { + redisLibevEvents *e = (redisLibevEvents*)privdata; + struct ev_loop *loop = e->loop; + ((void)loop); + if (!e->reading) { + e->reading = 1; + ev_io_start(EV_A_ &e->rev); + } +} + +static void redisLibevDelRead(void *privdata) { + redisLibevEvents *e = (redisLibevEvents*)privdata; + struct ev_loop *loop = e->loop; + ((void)loop); + if (e->reading) { + e->reading = 0; + ev_io_stop(EV_A_ &e->rev); + } +} + +static void redisLibevAddWrite(void *privdata) { + redisLibevEvents *e = (redisLibevEvents*)privdata; + struct ev_loop *loop = e->loop; + ((void)loop); + if (!e->writing) { + e->writing = 1; + ev_io_start(EV_A_ &e->wev); + } +} + +static void redisLibevDelWrite(void *privdata) { + redisLibevEvents *e = (redisLibevEvents*)privdata; + struct ev_loop *loop = e->loop; + ((void)loop); + if (e->writing) { + e->writing = 0; + ev_io_stop(EV_A_ &e->wev); + } +} + +static void redisLibevCleanup(void *privdata) { + redisLibevEvents *e = (redisLibevEvents*)privdata; + redisLibevDelRead(privdata); + redisLibevDelWrite(privdata); + free(e); +} + +static int redisLibevAttach(EV_P_ redisAsyncContext *ac) { + redisContext *c = &(ac->c); + redisLibevEvents *e; + + /* Nothing should be attached when something is already attached */ + if (ac->ev.data != NULL) + return REDIS_ERR; + + /* Create container for context and r/w events */ + e = (redisLibevEvents*)hi_malloc(sizeof(*e)); + e->context = ac; +#if EV_MULTIPLICITY + e->loop = loop; +#else + e->loop = NULL; +#endif + e->reading = e->writing = 0; + e->rev.data = e; + e->wev.data = e; + + /* Register functions to start/stop listening for events */ + ac->ev.addRead = redisLibevAddRead; + ac->ev.delRead = redisLibevDelRead; + ac->ev.addWrite = redisLibevAddWrite; + ac->ev.delWrite = redisLibevDelWrite; + ac->ev.cleanup = redisLibevCleanup; + ac->ev.data = e; + + /* Initialize read/write events */ + ev_io_init(&e->rev,redisLibevReadEvent,c->fd,EV_READ); + ev_io_init(&e->wev,redisLibevWriteEvent,c->fd,EV_WRITE); + return REDIS_OK; +} + +#endif diff --git a/controller/thirdparty/hiredis-0.14.1/adapters/libevent.h b/controller/thirdparty/hiredis-0.14.1/adapters/libevent.h new file mode 100644 index 000000000..f2330d6f0 --- /dev/null +++ b/controller/thirdparty/hiredis-0.14.1/adapters/libevent.h @@ -0,0 +1,108 @@ +/* + * Copyright (c) 2010-2011, Pieter Noordhuis + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * * Neither the name of Redis nor the names of its contributors may be used + * to endorse or promote products derived from this software without + * specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +#ifndef __HIREDIS_LIBEVENT_H__ +#define __HIREDIS_LIBEVENT_H__ +#include +#include "../hiredis.h" +#include "../async.h" + +typedef struct redisLibeventEvents { + redisAsyncContext *context; + struct event *rev, *wev; +} redisLibeventEvents; + +static void redisLibeventReadEvent(int fd, short event, void *arg) { + ((void)fd); ((void)event); + redisLibeventEvents *e = (redisLibeventEvents*)arg; + redisAsyncHandleRead(e->context); +} + +static void redisLibeventWriteEvent(int fd, short event, void *arg) { + ((void)fd); ((void)event); + redisLibeventEvents *e = (redisLibeventEvents*)arg; + redisAsyncHandleWrite(e->context); +} + +static void redisLibeventAddRead(void *privdata) { + redisLibeventEvents *e = (redisLibeventEvents*)privdata; + event_add(e->rev,NULL); +} + +static void redisLibeventDelRead(void *privdata) { + redisLibeventEvents *e = (redisLibeventEvents*)privdata; + event_del(e->rev); +} + +static void redisLibeventAddWrite(void *privdata) { + redisLibeventEvents *e = (redisLibeventEvents*)privdata; + event_add(e->wev,NULL); +} + +static void redisLibeventDelWrite(void *privdata) { + redisLibeventEvents *e = (redisLibeventEvents*)privdata; + event_del(e->wev); +} + +static void redisLibeventCleanup(void *privdata) { + redisLibeventEvents *e = (redisLibeventEvents*)privdata; + event_free(e->rev); + event_free(e->wev); + free(e); +} + +static int redisLibeventAttach(redisAsyncContext *ac, struct event_base *base) { + redisContext *c = &(ac->c); + redisLibeventEvents *e; + + /* Nothing should be attached when something is already attached */ + if (ac->ev.data != NULL) + return REDIS_ERR; + + /* Create container for context and r/w events */ + e = (redisLibeventEvents*)hi_calloc(1, sizeof(*e)); + e->context = ac; + + /* Register functions to start/stop listening for events */ + ac->ev.addRead = redisLibeventAddRead; + ac->ev.delRead = redisLibeventDelRead; + ac->ev.addWrite = redisLibeventAddWrite; + ac->ev.delWrite = redisLibeventDelWrite; + ac->ev.cleanup = redisLibeventCleanup; + ac->ev.data = e; + + /* Initialize and install read/write events */ + e->rev = event_new(base, c->fd, EV_READ, redisLibeventReadEvent, e); + e->wev = event_new(base, c->fd, EV_WRITE, redisLibeventWriteEvent, e); + event_add(e->rev, NULL); + event_add(e->wev, NULL); + return REDIS_OK; +} +#endif diff --git a/controller/thirdparty/hiredis-0.14.1/adapters/libuv.h b/controller/thirdparty/hiredis-0.14.1/adapters/libuv.h new file mode 100644 index 000000000..ff08c25e1 --- /dev/null +++ b/controller/thirdparty/hiredis-0.14.1/adapters/libuv.h @@ -0,0 +1,122 @@ +#ifndef __HIREDIS_LIBUV_H__ +#define __HIREDIS_LIBUV_H__ +#include +#include +#include "../hiredis.h" +#include "../async.h" +#include + +typedef struct redisLibuvEvents { + redisAsyncContext* context; + uv_poll_t handle; + int events; +} redisLibuvEvents; + + +static void redisLibuvPoll(uv_poll_t* handle, int status, int events) { + redisLibuvEvents* p = (redisLibuvEvents*)handle->data; + + if (status != 0) { + return; + } + + if (p->context != NULL && (events & UV_READABLE)) { + redisAsyncHandleRead(p->context); + } + if (p->context != NULL && (events & UV_WRITABLE)) { + redisAsyncHandleWrite(p->context); + } +} + + +static void redisLibuvAddRead(void *privdata) { + redisLibuvEvents* p = (redisLibuvEvents*)privdata; + + p->events |= UV_READABLE; + + uv_poll_start(&p->handle, p->events, redisLibuvPoll); +} + + +static void redisLibuvDelRead(void *privdata) { + redisLibuvEvents* p = (redisLibuvEvents*)privdata; + + p->events &= ~UV_READABLE; + + if (p->events) { + uv_poll_start(&p->handle, p->events, redisLibuvPoll); + } else { + uv_poll_stop(&p->handle); + } +} + + +static void redisLibuvAddWrite(void *privdata) { + redisLibuvEvents* p = (redisLibuvEvents*)privdata; + + p->events |= UV_WRITABLE; + + uv_poll_start(&p->handle, p->events, redisLibuvPoll); +} + + +static void redisLibuvDelWrite(void *privdata) { + redisLibuvEvents* p = (redisLibuvEvents*)privdata; + + p->events &= ~UV_WRITABLE; + + if (p->events) { + uv_poll_start(&p->handle, p->events, redisLibuvPoll); + } else { + uv_poll_stop(&p->handle); + } +} + + +static void on_close(uv_handle_t* handle) { + redisLibuvEvents* p = (redisLibuvEvents*)handle->data; + + free(p); +} + + +static void redisLibuvCleanup(void *privdata) { + redisLibuvEvents* p = (redisLibuvEvents*)privdata; + + p->context = NULL; // indicate that context might no longer exist + uv_close((uv_handle_t*)&p->handle, on_close); +} + + +static int redisLibuvAttach(redisAsyncContext* ac, uv_loop_t* loop) { + redisContext *c = &(ac->c); + + if (ac->ev.data != NULL) { + return REDIS_ERR; + } + + ac->ev.addRead = redisLibuvAddRead; + ac->ev.delRead = redisLibuvDelRead; + ac->ev.addWrite = redisLibuvAddWrite; + ac->ev.delWrite = redisLibuvDelWrite; + ac->ev.cleanup = redisLibuvCleanup; + + redisLibuvEvents* p = (redisLibuvEvents*)malloc(sizeof(*p)); + + if (!p) { + return REDIS_ERR; + } + + memset(p, 0, sizeof(*p)); + + if (uv_poll_init(loop, &p->handle, c->fd) != 0) { + return REDIS_ERR; + } + + ac->ev.data = p; + p->handle.data = p; + p->context = ac; + + return REDIS_OK; +} +#endif diff --git a/controller/thirdparty/hiredis-0.14.1/adapters/macosx.h b/controller/thirdparty/hiredis-0.14.1/adapters/macosx.h new file mode 100644 index 000000000..72121f606 --- /dev/null +++ b/controller/thirdparty/hiredis-0.14.1/adapters/macosx.h @@ -0,0 +1,114 @@ +// +// Created by Дмитрий Бахвалов on 13.07.15. +// Copyright (c) 2015 Dmitry Bakhvalov. All rights reserved. +// + +#ifndef __HIREDIS_MACOSX_H__ +#define __HIREDIS_MACOSX_H__ + +#include + +#include "../hiredis.h" +#include "../async.h" + +typedef struct { + redisAsyncContext *context; + CFSocketRef socketRef; + CFRunLoopSourceRef sourceRef; +} RedisRunLoop; + +static int freeRedisRunLoop(RedisRunLoop* redisRunLoop) { + if( redisRunLoop != NULL ) { + if( redisRunLoop->sourceRef != NULL ) { + CFRunLoopSourceInvalidate(redisRunLoop->sourceRef); + CFRelease(redisRunLoop->sourceRef); + } + if( redisRunLoop->socketRef != NULL ) { + CFSocketInvalidate(redisRunLoop->socketRef); + CFRelease(redisRunLoop->socketRef); + } + free(redisRunLoop); + } + return REDIS_ERR; +} + +static void redisMacOSAddRead(void *privdata) { + RedisRunLoop *redisRunLoop = (RedisRunLoop*)privdata; + CFSocketEnableCallBacks(redisRunLoop->socketRef, kCFSocketReadCallBack); +} + +static void redisMacOSDelRead(void *privdata) { + RedisRunLoop *redisRunLoop = (RedisRunLoop*)privdata; + CFSocketDisableCallBacks(redisRunLoop->socketRef, kCFSocketReadCallBack); +} + +static void redisMacOSAddWrite(void *privdata) { + RedisRunLoop *redisRunLoop = (RedisRunLoop*)privdata; + CFSocketEnableCallBacks(redisRunLoop->socketRef, kCFSocketWriteCallBack); +} + +static void redisMacOSDelWrite(void *privdata) { + RedisRunLoop *redisRunLoop = (RedisRunLoop*)privdata; + CFSocketDisableCallBacks(redisRunLoop->socketRef, kCFSocketWriteCallBack); +} + +static void redisMacOSCleanup(void *privdata) { + RedisRunLoop *redisRunLoop = (RedisRunLoop*)privdata; + freeRedisRunLoop(redisRunLoop); +} + +static void redisMacOSAsyncCallback(CFSocketRef __unused s, CFSocketCallBackType callbackType, CFDataRef __unused address, const void __unused *data, void *info) { + redisAsyncContext* context = (redisAsyncContext*) info; + + switch (callbackType) { + case kCFSocketReadCallBack: + redisAsyncHandleRead(context); + break; + + case kCFSocketWriteCallBack: + redisAsyncHandleWrite(context); + break; + + default: + break; + } +} + +static int redisMacOSAttach(redisAsyncContext *redisAsyncCtx, CFRunLoopRef runLoop) { + redisContext *redisCtx = &(redisAsyncCtx->c); + + /* Nothing should be attached when something is already attached */ + if( redisAsyncCtx->ev.data != NULL ) return REDIS_ERR; + + RedisRunLoop* redisRunLoop = (RedisRunLoop*) calloc(1, sizeof(RedisRunLoop)); + if( !redisRunLoop ) return REDIS_ERR; + + /* Setup redis stuff */ + redisRunLoop->context = redisAsyncCtx; + + redisAsyncCtx->ev.addRead = redisMacOSAddRead; + redisAsyncCtx->ev.delRead = redisMacOSDelRead; + redisAsyncCtx->ev.addWrite = redisMacOSAddWrite; + redisAsyncCtx->ev.delWrite = redisMacOSDelWrite; + redisAsyncCtx->ev.cleanup = redisMacOSCleanup; + redisAsyncCtx->ev.data = redisRunLoop; + + /* Initialize and install read/write events */ + CFSocketContext socketCtx = { 0, redisAsyncCtx, NULL, NULL, NULL }; + + redisRunLoop->socketRef = CFSocketCreateWithNative(NULL, redisCtx->fd, + kCFSocketReadCallBack | kCFSocketWriteCallBack, + redisMacOSAsyncCallback, + &socketCtx); + if( !redisRunLoop->socketRef ) return freeRedisRunLoop(redisRunLoop); + + redisRunLoop->sourceRef = CFSocketCreateRunLoopSource(NULL, redisRunLoop->socketRef, 0); + if( !redisRunLoop->sourceRef ) return freeRedisRunLoop(redisRunLoop); + + CFRunLoopAddSource(runLoop, redisRunLoop->sourceRef, kCFRunLoopDefaultMode); + + return REDIS_OK; +} + +#endif + diff --git a/controller/thirdparty/hiredis-0.14.1/adapters/qt.h b/controller/thirdparty/hiredis-0.14.1/adapters/qt.h new file mode 100644 index 000000000..5cc02e6ce --- /dev/null +++ b/controller/thirdparty/hiredis-0.14.1/adapters/qt.h @@ -0,0 +1,135 @@ +/*- + * Copyright (C) 2014 Pietro Cerutti + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#ifndef __HIREDIS_QT_H__ +#define __HIREDIS_QT_H__ +#include +#include "../async.h" + +static void RedisQtAddRead(void *); +static void RedisQtDelRead(void *); +static void RedisQtAddWrite(void *); +static void RedisQtDelWrite(void *); +static void RedisQtCleanup(void *); + +class RedisQtAdapter : public QObject { + + Q_OBJECT + + friend + void RedisQtAddRead(void * adapter) { + RedisQtAdapter * a = static_cast(adapter); + a->addRead(); + } + + friend + void RedisQtDelRead(void * adapter) { + RedisQtAdapter * a = static_cast(adapter); + a->delRead(); + } + + friend + void RedisQtAddWrite(void * adapter) { + RedisQtAdapter * a = static_cast(adapter); + a->addWrite(); + } + + friend + void RedisQtDelWrite(void * adapter) { + RedisQtAdapter * a = static_cast(adapter); + a->delWrite(); + } + + friend + void RedisQtCleanup(void * adapter) { + RedisQtAdapter * a = static_cast(adapter); + a->cleanup(); + } + + public: + RedisQtAdapter(QObject * parent = 0) + : QObject(parent), m_ctx(0), m_read(0), m_write(0) { } + + ~RedisQtAdapter() { + if (m_ctx != 0) { + m_ctx->ev.data = NULL; + } + } + + int setContext(redisAsyncContext * ac) { + if (ac->ev.data != NULL) { + return REDIS_ERR; + } + m_ctx = ac; + m_ctx->ev.data = this; + m_ctx->ev.addRead = RedisQtAddRead; + m_ctx->ev.delRead = RedisQtDelRead; + m_ctx->ev.addWrite = RedisQtAddWrite; + m_ctx->ev.delWrite = RedisQtDelWrite; + m_ctx->ev.cleanup = RedisQtCleanup; + return REDIS_OK; + } + + private: + void addRead() { + if (m_read) return; + m_read = new QSocketNotifier(m_ctx->c.fd, QSocketNotifier::Read, 0); + connect(m_read, SIGNAL(activated(int)), this, SLOT(read())); + } + + void delRead() { + if (!m_read) return; + delete m_read; + m_read = 0; + } + + void addWrite() { + if (m_write) return; + m_write = new QSocketNotifier(m_ctx->c.fd, QSocketNotifier::Write, 0); + connect(m_write, SIGNAL(activated(int)), this, SLOT(write())); + } + + void delWrite() { + if (!m_write) return; + delete m_write; + m_write = 0; + } + + void cleanup() { + delRead(); + delWrite(); + } + + private slots: + void read() { redisAsyncHandleRead(m_ctx); } + void write() { redisAsyncHandleWrite(m_ctx); } + + private: + redisAsyncContext * m_ctx; + QSocketNotifier * m_read; + QSocketNotifier * m_write; +}; + +#endif /* !__HIREDIS_QT_H__ */ diff --git a/controller/thirdparty/hiredis-0.14.1/alloc.c b/controller/thirdparty/hiredis-0.14.1/alloc.c new file mode 100644 index 000000000..55c3020e7 --- /dev/null +++ b/controller/thirdparty/hiredis-0.14.1/alloc.c @@ -0,0 +1,65 @@ +/* + * Copyright (c) 2020, Michael Grunder + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * * Neither the name of Redis nor the names of its contributors may be used + * to endorse or promote products derived from this software without + * specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +#include "fmacros.h" +#include "alloc.h" +#include + +void *hi_malloc(size_t size) { + void *ptr = malloc(size); + if (ptr == NULL) + HIREDIS_OOM_HANDLER; + + return ptr; +} + +void *hi_calloc(size_t nmemb, size_t size) { + void *ptr = calloc(nmemb, size); + if (ptr == NULL) + HIREDIS_OOM_HANDLER; + + return ptr; +} + +void *hi_realloc(void *ptr, size_t size) { + void *newptr = realloc(ptr, size); + if (newptr == NULL) + HIREDIS_OOM_HANDLER; + + return newptr; +} + +char *hi_strdup(const char *str) { + char *newstr = strdup(str); + if (newstr == NULL) + HIREDIS_OOM_HANDLER; + + return newstr; +} diff --git a/controller/thirdparty/hiredis-0.14.1/alloc.h b/controller/thirdparty/hiredis-0.14.1/alloc.h new file mode 100644 index 000000000..2c9b04e35 --- /dev/null +++ b/controller/thirdparty/hiredis-0.14.1/alloc.h @@ -0,0 +1,53 @@ +/* + * Copyright (c) 2020, Michael Grunder + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * * Neither the name of Redis nor the names of its contributors may be used + * to endorse or promote products derived from this software without + * specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +#ifndef HIREDIS_ALLOC_H +#define HIREDIS_ALLOC_H + +#include /* for size_t */ + +#ifndef HIREDIS_OOM_HANDLER +#define HIREDIS_OOM_HANDLER abort() +#endif + +#ifdef __cplusplus +extern "C" { +#endif + +void *hi_malloc(size_t size); +void *hi_calloc(size_t nmemb, size_t size); +void *hi_realloc(void *ptr, size_t size); +char *hi_strdup(const char *str); + +#ifdef __cplusplus +} +#endif + +#endif /* HIREDIS_ALLOC_H */ diff --git a/controller/thirdparty/hiredis-0.14.1/appveyor.yml b/controller/thirdparty/hiredis-0.14.1/appveyor.yml new file mode 100644 index 000000000..819efbd58 --- /dev/null +++ b/controller/thirdparty/hiredis-0.14.1/appveyor.yml @@ -0,0 +1,23 @@ +# Appveyor configuration file for CI build of hiredis on Windows (under Cygwin) +environment: + matrix: + - CYG_BASH: C:\cygwin64\bin\bash + CC: gcc + - CYG_BASH: C:\cygwin\bin\bash + CC: gcc + TARGET: 32bit + TARGET_VARS: 32bit-vars + +clone_depth: 1 + +# Attempt to ensure we don't try to convert line endings to Win32 CRLF as this will cause build to fail +init: + - git config --global core.autocrlf input + +# Install needed build dependencies +install: + - '%CYG_BASH% -lc "cygcheck -dc cygwin"' + +build_script: + - 'echo building...' + - '%CYG_BASH% -lc "cd $APPVEYOR_BUILD_FOLDER; exec 0 + * Copyright (c) 2010-2011, Pieter Noordhuis + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * * Neither the name of Redis nor the names of its contributors may be used + * to endorse or promote products derived from this software without + * specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +#include "fmacros.h" +#include "alloc.h" +#include +#include +#include +#include +#include +#include +#include "async.h" +#include "net.h" +#include "dict.c" +#include "sds.h" + +#define _EL_ADD_READ(ctx) do { \ + if ((ctx)->ev.addRead) (ctx)->ev.addRead((ctx)->ev.data); \ + } while(0) +#define _EL_DEL_READ(ctx) do { \ + if ((ctx)->ev.delRead) (ctx)->ev.delRead((ctx)->ev.data); \ + } while(0) +#define _EL_ADD_WRITE(ctx) do { \ + if ((ctx)->ev.addWrite) (ctx)->ev.addWrite((ctx)->ev.data); \ + } while(0) +#define _EL_DEL_WRITE(ctx) do { \ + if ((ctx)->ev.delWrite) (ctx)->ev.delWrite((ctx)->ev.data); \ + } while(0) +#define _EL_CLEANUP(ctx) do { \ + if ((ctx)->ev.cleanup) (ctx)->ev.cleanup((ctx)->ev.data); \ + } while(0); + +/* Forward declaration of function in hiredis.c */ +int __redisAppendCommand(redisContext *c, const char *cmd, size_t len); + +/* Functions managing dictionary of callbacks for pub/sub. */ +static unsigned int callbackHash(const void *key) { + return dictGenHashFunction((const unsigned char *)key, + sdslen((const sds)key)); +} + +static void *callbackValDup(void *privdata, const void *src) { + ((void) privdata); + redisCallback *dup = hi_malloc(sizeof(*dup)); + memcpy(dup,src,sizeof(*dup)); + return dup; +} + +static int callbackKeyCompare(void *privdata, const void *key1, const void *key2) { + int l1, l2; + ((void) privdata); + + l1 = sdslen((const sds)key1); + l2 = sdslen((const sds)key2); + if (l1 != l2) return 0; + return memcmp(key1,key2,l1) == 0; +} + +static void callbackKeyDestructor(void *privdata, void *key) { + ((void) privdata); + sdsfree((sds)key); +} + +static void callbackValDestructor(void *privdata, void *val) { + ((void) privdata); + free(val); +} + +static dictType callbackDict = { + callbackHash, + NULL, + callbackValDup, + callbackKeyCompare, + callbackKeyDestructor, + callbackValDestructor +}; + +static redisAsyncContext *redisAsyncInitialize(redisContext *c) { + redisAsyncContext *ac; + + ac = realloc(c,sizeof(redisAsyncContext)); + if (ac == NULL) + return NULL; + + c = &(ac->c); + + /* The regular connect functions will always set the flag REDIS_CONNECTED. + * For the async API, we want to wait until the first write event is + * received up before setting this flag, so reset it here. */ + c->flags &= ~REDIS_CONNECTED; + + ac->err = 0; + ac->errstr = NULL; + ac->data = NULL; + + ac->ev.data = NULL; + ac->ev.addRead = NULL; + ac->ev.delRead = NULL; + ac->ev.addWrite = NULL; + ac->ev.delWrite = NULL; + ac->ev.cleanup = NULL; + + ac->onConnect = NULL; + ac->onDisconnect = NULL; + + ac->replies.head = NULL; + ac->replies.tail = NULL; + ac->sub.invalid.head = NULL; + ac->sub.invalid.tail = NULL; + ac->sub.channels = dictCreate(&callbackDict,NULL); + ac->sub.patterns = dictCreate(&callbackDict,NULL); + return ac; +} + +/* We want the error field to be accessible directly instead of requiring + * an indirection to the redisContext struct. */ +static void __redisAsyncCopyError(redisAsyncContext *ac) { + if (!ac) + return; + + redisContext *c = &(ac->c); + ac->err = c->err; + ac->errstr = c->errstr; +} + +redisAsyncContext *redisAsyncConnect(const char *ip, int port) { + redisContext *c; + redisAsyncContext *ac; + + c = redisConnectNonBlock(ip,port); + if (c == NULL) + return NULL; + + ac = redisAsyncInitialize(c); + if (ac == NULL) { + redisFree(c); + return NULL; + } + + __redisAsyncCopyError(ac); + return ac; +} + +redisAsyncContext *redisAsyncConnectBind(const char *ip, int port, + const char *source_addr) { + redisContext *c = redisConnectBindNonBlock(ip,port,source_addr); + redisAsyncContext *ac = redisAsyncInitialize(c); + __redisAsyncCopyError(ac); + return ac; +} + +redisAsyncContext *redisAsyncConnectBindWithReuse(const char *ip, int port, + const char *source_addr) { + redisContext *c = redisConnectBindNonBlockWithReuse(ip,port,source_addr); + redisAsyncContext *ac = redisAsyncInitialize(c); + __redisAsyncCopyError(ac); + return ac; +} + +redisAsyncContext *redisAsyncConnectUnix(const char *path) { + redisContext *c; + redisAsyncContext *ac; + + c = redisConnectUnixNonBlock(path); + if (c == NULL) + return NULL; + + ac = redisAsyncInitialize(c); + if (ac == NULL) { + redisFree(c); + return NULL; + } + + __redisAsyncCopyError(ac); + return ac; +} + +int redisAsyncSetConnectCallback(redisAsyncContext *ac, redisConnectCallback *fn) { + if (ac->onConnect == NULL) { + ac->onConnect = fn; + + /* The common way to detect an established connection is to wait for + * the first write event to be fired. This assumes the related event + * library functions are already set. */ + _EL_ADD_WRITE(ac); + return REDIS_OK; + } + return REDIS_ERR; +} + +int redisAsyncSetDisconnectCallback(redisAsyncContext *ac, redisDisconnectCallback *fn) { + if (ac->onDisconnect == NULL) { + ac->onDisconnect = fn; + return REDIS_OK; + } + return REDIS_ERR; +} + +/* Helper functions to push/shift callbacks */ +static int __redisPushCallback(redisCallbackList *list, redisCallback *source) { + redisCallback *cb; + + /* Copy callback from stack to heap */ + cb = malloc(sizeof(*cb)); + if (cb == NULL) + return REDIS_ERR_OOM; + + if (source != NULL) { + memcpy(cb,source,sizeof(*cb)); + cb->next = NULL; + } + + /* Store callback in list */ + if (list->head == NULL) + list->head = cb; + if (list->tail != NULL) + list->tail->next = cb; + list->tail = cb; + return REDIS_OK; +} + +static int __redisShiftCallback(redisCallbackList *list, redisCallback *target) { + redisCallback *cb = list->head; + if (cb != NULL) { + list->head = cb->next; + if (cb == list->tail) + list->tail = NULL; + + /* Copy callback from heap to stack */ + if (target != NULL) + memcpy(target,cb,sizeof(*cb)); + free(cb); + return REDIS_OK; + } + return REDIS_ERR; +} + +static void __redisRunCallback(redisAsyncContext *ac, redisCallback *cb, redisReply *reply) { + redisContext *c = &(ac->c); + if (cb->fn != NULL) { + c->flags |= REDIS_IN_CALLBACK; + cb->fn(ac,reply,cb->privdata); + c->flags &= ~REDIS_IN_CALLBACK; + } +} + +/* Helper function to free the context. */ +static void __redisAsyncFree(redisAsyncContext *ac) { + redisContext *c = &(ac->c); + redisCallback cb; + dictIterator *it; + dictEntry *de; + + /* Execute pending callbacks with NULL reply. */ + while (__redisShiftCallback(&ac->replies,&cb) == REDIS_OK) + __redisRunCallback(ac,&cb,NULL); + + /* Execute callbacks for invalid commands */ + while (__redisShiftCallback(&ac->sub.invalid,&cb) == REDIS_OK) + __redisRunCallback(ac,&cb,NULL); + + /* Run subscription callbacks callbacks with NULL reply */ + it = dictGetIterator(ac->sub.channels); + while ((de = dictNext(it)) != NULL) + __redisRunCallback(ac,dictGetEntryVal(de),NULL); + dictReleaseIterator(it); + dictRelease(ac->sub.channels); + + it = dictGetIterator(ac->sub.patterns); + while ((de = dictNext(it)) != NULL) + __redisRunCallback(ac,dictGetEntryVal(de),NULL); + dictReleaseIterator(it); + dictRelease(ac->sub.patterns); + + /* Signal event lib to clean up */ + _EL_CLEANUP(ac); + + /* Execute disconnect callback. When redisAsyncFree() initiated destroying + * this context, the status will always be REDIS_OK. */ + if (ac->onDisconnect && (c->flags & REDIS_CONNECTED)) { + if (c->flags & REDIS_FREEING) { + ac->onDisconnect(ac,REDIS_OK); + } else { + ac->onDisconnect(ac,(ac->err == 0) ? REDIS_OK : REDIS_ERR); + } + } + + /* Cleanup self */ + redisFree(c); +} + +/* Free the async context. When this function is called from a callback, + * control needs to be returned to redisProcessCallbacks() before actual + * free'ing. To do so, a flag is set on the context which is picked up by + * redisProcessCallbacks(). Otherwise, the context is immediately free'd. */ +void redisAsyncFree(redisAsyncContext *ac) { + redisContext *c = &(ac->c); + c->flags |= REDIS_FREEING; + if (!(c->flags & REDIS_IN_CALLBACK)) + __redisAsyncFree(ac); +} + +/* Helper function to make the disconnect happen and clean up. */ +static void __redisAsyncDisconnect(redisAsyncContext *ac) { + redisContext *c = &(ac->c); + + /* Make sure error is accessible if there is any */ + __redisAsyncCopyError(ac); + + if (ac->err == 0) { + /* For clean disconnects, there should be no pending callbacks. */ + int ret = __redisShiftCallback(&ac->replies,NULL); + assert(ret == REDIS_ERR); + } else { + /* Disconnection is caused by an error, make sure that pending + * callbacks cannot call new commands. */ + c->flags |= REDIS_DISCONNECTING; + } + + /* For non-clean disconnects, __redisAsyncFree() will execute pending + * callbacks with a NULL-reply. */ + __redisAsyncFree(ac); +} + +/* Tries to do a clean disconnect from Redis, meaning it stops new commands + * from being issued, but tries to flush the output buffer and execute + * callbacks for all remaining replies. When this function is called from a + * callback, there might be more replies and we can safely defer disconnecting + * to redisProcessCallbacks(). Otherwise, we can only disconnect immediately + * when there are no pending callbacks. */ +void redisAsyncDisconnect(redisAsyncContext *ac) { + redisContext *c = &(ac->c); + c->flags |= REDIS_DISCONNECTING; + if (!(c->flags & REDIS_IN_CALLBACK) && ac->replies.head == NULL) + __redisAsyncDisconnect(ac); +} + +static int __redisGetSubscribeCallback(redisAsyncContext *ac, redisReply *reply, redisCallback *dstcb) { + redisContext *c = &(ac->c); + dict *callbacks; + redisCallback *cb; + dictEntry *de; + int pvariant; + char *stype; + sds sname; + + /* Custom reply functions are not supported for pub/sub. This will fail + * very hard when they are used... */ + if (reply->type == REDIS_REPLY_ARRAY) { + assert(reply->elements >= 2); + assert(reply->element[0]->type == REDIS_REPLY_STRING); + stype = reply->element[0]->str; + pvariant = (tolower(stype[0]) == 'p') ? 1 : 0; + + if (pvariant) + callbacks = ac->sub.patterns; + else + callbacks = ac->sub.channels; + + /* Locate the right callback */ + assert(reply->element[1]->type == REDIS_REPLY_STRING); + sname = sdsnewlen(reply->element[1]->str,reply->element[1]->len); + de = dictFind(callbacks,sname); + if (de != NULL) { + cb = dictGetEntryVal(de); + + /* If this is an subscribe reply decrease pending counter. */ + if (strcasecmp(stype+pvariant,"subscribe") == 0) { + cb->pending_subs -= 1; + } + + memcpy(dstcb,cb,sizeof(*dstcb)); + + /* If this is an unsubscribe message, remove it. */ + if (strcasecmp(stype+pvariant,"unsubscribe") == 0) { + if (cb->pending_subs == 0) + dictDelete(callbacks,sname); + + /* If this was the last unsubscribe message, revert to + * non-subscribe mode. */ + assert(reply->element[2]->type == REDIS_REPLY_INTEGER); + + /* Unset subscribed flag only when no pipelined pending subscribe. */ + if (reply->element[2]->integer == 0 + && dictSize(ac->sub.channels) == 0 + && dictSize(ac->sub.patterns) == 0) + c->flags &= ~REDIS_SUBSCRIBED; + } + } + sdsfree(sname); + } else { + /* Shift callback for invalid commands. */ + __redisShiftCallback(&ac->sub.invalid,dstcb); + } + return REDIS_OK; +} + +void redisProcessCallbacks(redisAsyncContext *ac) { + redisContext *c = &(ac->c); + redisCallback cb = {NULL, NULL, 0, NULL}; + void *reply = NULL; + int status; + + while((status = redisGetReply(c,&reply)) == REDIS_OK) { + if (reply == NULL) { + /* When the connection is being disconnected and there are + * no more replies, this is the cue to really disconnect. */ + if (c->flags & REDIS_DISCONNECTING && sdslen(c->obuf) == 0 + && ac->replies.head == NULL) { + __redisAsyncDisconnect(ac); + return; + } + + /* If monitor mode, repush callback */ + if(c->flags & REDIS_MONITORING) { + __redisPushCallback(&ac->replies,&cb); + } + + /* When the connection is not being disconnected, simply stop + * trying to get replies and wait for the next loop tick. */ + break; + } + + /* Even if the context is subscribed, pending regular callbacks will + * get a reply before pub/sub messages arrive. */ + if (__redisShiftCallback(&ac->replies,&cb) != REDIS_OK) { + /* + * A spontaneous reply in a not-subscribed context can be the error + * reply that is sent when a new connection exceeds the maximum + * number of allowed connections on the server side. + * + * This is seen as an error instead of a regular reply because the + * server closes the connection after sending it. + * + * To prevent the error from being overwritten by an EOF error the + * connection is closed here. See issue #43. + * + * Another possibility is that the server is loading its dataset. + * In this case we also want to close the connection, and have the + * user wait until the server is ready to take our request. + */ + if (((redisReply*)reply)->type == REDIS_REPLY_ERROR) { + c->err = REDIS_ERR_OTHER; + snprintf(c->errstr,sizeof(c->errstr),"%s",((redisReply*)reply)->str); + c->reader->fn->freeObject(reply); + __redisAsyncDisconnect(ac); + return; + } + /* No more regular callbacks and no errors, the context *must* be subscribed or monitoring. */ + assert((c->flags & REDIS_SUBSCRIBED || c->flags & REDIS_MONITORING)); + if(c->flags & REDIS_SUBSCRIBED) + __redisGetSubscribeCallback(ac,reply,&cb); + } + + if (cb.fn != NULL) { + __redisRunCallback(ac,&cb,reply); + c->reader->fn->freeObject(reply); + + /* Proceed with free'ing when redisAsyncFree() was called. */ + if (c->flags & REDIS_FREEING) { + __redisAsyncFree(ac); + return; + } + } else { + /* No callback for this reply. This can either be a NULL callback, + * or there were no callbacks to begin with. Either way, don't + * abort with an error, but simply ignore it because the client + * doesn't know what the server will spit out over the wire. */ + c->reader->fn->freeObject(reply); + } + } + + /* Disconnect when there was an error reading the reply */ + if (status != REDIS_OK) + __redisAsyncDisconnect(ac); +} + +/* Internal helper function to detect socket status the first time a read or + * write event fires. When connecting was not successful, the connect callback + * is called with a REDIS_ERR status and the context is free'd. */ +static int __redisAsyncHandleConnect(redisAsyncContext *ac) { + redisContext *c = &(ac->c); + + if (redisCheckSocketError(c) == REDIS_ERR) { + /* Try again later when connect(2) is still in progress. */ + if (errno == EINPROGRESS) + return REDIS_OK; + + if (ac->onConnect) ac->onConnect(ac,REDIS_ERR); + __redisAsyncDisconnect(ac); + return REDIS_ERR; + } + + /* Mark context as connected. */ + c->flags |= REDIS_CONNECTED; + if (ac->onConnect) ac->onConnect(ac,REDIS_OK); + return REDIS_OK; +} + +/* This function should be called when the socket is readable. + * It processes all replies that can be read and executes their callbacks. + */ +void redisAsyncHandleRead(redisAsyncContext *ac) { + redisContext *c = &(ac->c); + + if (!(c->flags & REDIS_CONNECTED)) { + /* Abort connect was not successful. */ + if (__redisAsyncHandleConnect(ac) != REDIS_OK) + return; + /* Try again later when the context is still not connected. */ + if (!(c->flags & REDIS_CONNECTED)) + return; + } + + if (redisBufferRead(c) == REDIS_ERR) { + __redisAsyncDisconnect(ac); + } else { + /* Always re-schedule reads */ + _EL_ADD_READ(ac); + redisProcessCallbacks(ac); + } +} + +void redisAsyncHandleWrite(redisAsyncContext *ac) { + redisContext *c = &(ac->c); + int done = 0; + + if (!(c->flags & REDIS_CONNECTED)) { + /* Abort connect was not successful. */ + if (__redisAsyncHandleConnect(ac) != REDIS_OK) + return; + /* Try again later when the context is still not connected. */ + if (!(c->flags & REDIS_CONNECTED)) + return; + } + + if (redisBufferWrite(c,&done) == REDIS_ERR) { + __redisAsyncDisconnect(ac); + } else { + /* Continue writing when not done, stop writing otherwise */ + if (!done) + _EL_ADD_WRITE(ac); + else + _EL_DEL_WRITE(ac); + + /* Always schedule reads after writes */ + _EL_ADD_READ(ac); + } +} + +/* Sets a pointer to the first argument and its length starting at p. Returns + * the number of bytes to skip to get to the following argument. */ +static const char *nextArgument(const char *start, const char **str, size_t *len) { + const char *p = start; + if (p[0] != '$') { + p = strchr(p,'$'); + if (p == NULL) return NULL; + } + + *len = (int)strtol(p+1,NULL,10); + p = strchr(p,'\r'); + assert(p); + *str = p+2; + return p+2+(*len)+2; +} + +/* Helper function for the redisAsyncCommand* family of functions. Writes a + * formatted command to the output buffer and registers the provided callback + * function with the context. */ +static int __redisAsyncCommand(redisAsyncContext *ac, redisCallbackFn *fn, void *privdata, const char *cmd, size_t len) { + redisContext *c = &(ac->c); + redisCallback cb; + struct dict *cbdict; + dictEntry *de; + redisCallback *existcb; + int pvariant, hasnext; + const char *cstr, *astr; + size_t clen, alen; + const char *p; + sds sname; + int ret; + + /* Don't accept new commands when the connection is about to be closed. */ + if (c->flags & (REDIS_DISCONNECTING | REDIS_FREEING)) return REDIS_ERR; + + /* Setup callback */ + cb.fn = fn; + cb.privdata = privdata; + cb.pending_subs = 1; + + /* Find out which command will be appended. */ + p = nextArgument(cmd,&cstr,&clen); + assert(p != NULL); + hasnext = (p[0] == '$'); + pvariant = (tolower(cstr[0]) == 'p') ? 1 : 0; + cstr += pvariant; + clen -= pvariant; + + if (hasnext && strncasecmp(cstr,"subscribe\r\n",11) == 0) { + c->flags |= REDIS_SUBSCRIBED; + + /* Add every channel/pattern to the list of subscription callbacks. */ + while ((p = nextArgument(p,&astr,&alen)) != NULL) { + sname = sdsnewlen(astr,alen); + if (pvariant) + cbdict = ac->sub.patterns; + else + cbdict = ac->sub.channels; + + de = dictFind(cbdict,sname); + + if (de != NULL) { + existcb = dictGetEntryVal(de); + cb.pending_subs = existcb->pending_subs + 1; + } + + ret = dictReplace(cbdict,sname,&cb); + + if (ret == 0) sdsfree(sname); + } + } else if (strncasecmp(cstr,"unsubscribe\r\n",13) == 0) { + /* It is only useful to call (P)UNSUBSCRIBE when the context is + * subscribed to one or more channels or patterns. */ + if (!(c->flags & REDIS_SUBSCRIBED)) return REDIS_ERR; + + /* (P)UNSUBSCRIBE does not have its own response: every channel or + * pattern that is unsubscribed will receive a message. This means we + * should not append a callback function for this command. */ + } else if(strncasecmp(cstr,"monitor\r\n",9) == 0) { + /* Set monitor flag and push callback */ + c->flags |= REDIS_MONITORING; + __redisPushCallback(&ac->replies,&cb); + } else { + if (c->flags & REDIS_SUBSCRIBED) + /* This will likely result in an error reply, but it needs to be + * received and passed to the callback. */ + __redisPushCallback(&ac->sub.invalid,&cb); + else + __redisPushCallback(&ac->replies,&cb); + } + + __redisAppendCommand(c,cmd,len); + + /* Always schedule a write when the write buffer is non-empty */ + _EL_ADD_WRITE(ac); + + return REDIS_OK; +} + +int redisvAsyncCommand(redisAsyncContext *ac, redisCallbackFn *fn, void *privdata, const char *format, va_list ap) { + char *cmd; + int len; + int status; + len = redisvFormatCommand(&cmd,format,ap); + + /* We don't want to pass -1 or -2 to future functions as a length. */ + if (len < 0) + return REDIS_ERR; + + status = __redisAsyncCommand(ac,fn,privdata,cmd,len); + free(cmd); + return status; +} + +int redisAsyncCommand(redisAsyncContext *ac, redisCallbackFn *fn, void *privdata, const char *format, ...) { + va_list ap; + int status; + va_start(ap,format); + status = redisvAsyncCommand(ac,fn,privdata,format,ap); + va_end(ap); + return status; +} + +int redisAsyncCommandArgv(redisAsyncContext *ac, redisCallbackFn *fn, void *privdata, int argc, const char **argv, const size_t *argvlen) { + sds cmd; + int len; + int status; + len = redisFormatSdsCommandArgv(&cmd,argc,argv,argvlen); + if (len < 0) + return REDIS_ERR; + status = __redisAsyncCommand(ac,fn,privdata,cmd,len); + sdsfree(cmd); + return status; +} + +int redisAsyncFormattedCommand(redisAsyncContext *ac, redisCallbackFn *fn, void *privdata, const char *cmd, size_t len) { + int status = __redisAsyncCommand(ac,fn,privdata,cmd,len); + return status; +} diff --git a/controller/thirdparty/hiredis-0.14.1/async.h b/controller/thirdparty/hiredis-0.14.1/async.h new file mode 100644 index 000000000..e69d84090 --- /dev/null +++ b/controller/thirdparty/hiredis-0.14.1/async.h @@ -0,0 +1,130 @@ +/* + * Copyright (c) 2009-2011, Salvatore Sanfilippo + * Copyright (c) 2010-2011, Pieter Noordhuis + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * * Neither the name of Redis nor the names of its contributors may be used + * to endorse or promote products derived from this software without + * specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +#ifndef __HIREDIS_ASYNC_H +#define __HIREDIS_ASYNC_H +#include "hiredis.h" + +#ifdef __cplusplus +extern "C" { +#endif + +struct redisAsyncContext; /* need forward declaration of redisAsyncContext */ +struct dict; /* dictionary header is included in async.c */ + +/* Reply callback prototype and container */ +typedef void (redisCallbackFn)(struct redisAsyncContext*, void*, void*); +typedef struct redisCallback { + struct redisCallback *next; /* simple singly linked list */ + redisCallbackFn *fn; + int pending_subs; + void *privdata; +} redisCallback; + +/* List of callbacks for either regular replies or pub/sub */ +typedef struct redisCallbackList { + redisCallback *head, *tail; +} redisCallbackList; + +/* Connection callback prototypes */ +typedef void (redisDisconnectCallback)(const struct redisAsyncContext*, int status); +typedef void (redisConnectCallback)(const struct redisAsyncContext*, int status); + +/* Context for an async connection to Redis */ +typedef struct redisAsyncContext { + /* Hold the regular context, so it can be realloc'ed. */ + redisContext c; + + /* Setup error flags so they can be used directly. */ + int err; + char *errstr; + + /* Not used by hiredis */ + void *data; + + /* Event library data and hooks */ + struct { + void *data; + + /* Hooks that are called when the library expects to start + * reading/writing. These functions should be idempotent. */ + void (*addRead)(void *privdata); + void (*delRead)(void *privdata); + void (*addWrite)(void *privdata); + void (*delWrite)(void *privdata); + void (*cleanup)(void *privdata); + } ev; + + /* Called when either the connection is terminated due to an error or per + * user request. The status is set accordingly (REDIS_OK, REDIS_ERR). */ + redisDisconnectCallback *onDisconnect; + + /* Called when the first write event was received. */ + redisConnectCallback *onConnect; + + /* Regular command callbacks */ + redisCallbackList replies; + + /* Subscription callbacks */ + struct { + redisCallbackList invalid; + struct dict *channels; + struct dict *patterns; + } sub; +} redisAsyncContext; + +/* Functions that proxy to hiredis */ +redisAsyncContext *redisAsyncConnect(const char *ip, int port); +redisAsyncContext *redisAsyncConnectBind(const char *ip, int port, const char *source_addr); +redisAsyncContext *redisAsyncConnectBindWithReuse(const char *ip, int port, + const char *source_addr); +redisAsyncContext *redisAsyncConnectUnix(const char *path); +int redisAsyncSetConnectCallback(redisAsyncContext *ac, redisConnectCallback *fn); +int redisAsyncSetDisconnectCallback(redisAsyncContext *ac, redisDisconnectCallback *fn); +void redisAsyncDisconnect(redisAsyncContext *ac); +void redisAsyncFree(redisAsyncContext *ac); + +/* Handle read/write events */ +void redisAsyncHandleRead(redisAsyncContext *ac); +void redisAsyncHandleWrite(redisAsyncContext *ac); + +/* Command functions for an async context. Write the command to the + * output buffer and register the provided callback. */ +int redisvAsyncCommand(redisAsyncContext *ac, redisCallbackFn *fn, void *privdata, const char *format, va_list ap); +int redisAsyncCommand(redisAsyncContext *ac, redisCallbackFn *fn, void *privdata, const char *format, ...); +int redisAsyncCommandArgv(redisAsyncContext *ac, redisCallbackFn *fn, void *privdata, int argc, const char **argv, const size_t *argvlen); +int redisAsyncFormattedCommand(redisAsyncContext *ac, redisCallbackFn *fn, void *privdata, const char *cmd, size_t len); + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/controller/thirdparty/hiredis-0.14.1/dict.c b/controller/thirdparty/hiredis-0.14.1/dict.c new file mode 100644 index 000000000..29cdc190f --- /dev/null +++ b/controller/thirdparty/hiredis-0.14.1/dict.c @@ -0,0 +1,339 @@ +/* Hash table implementation. + * + * This file implements in memory hash tables with insert/del/replace/find/ + * get-random-element operations. Hash tables will auto resize if needed + * tables of power of two in size are used, collisions are handled by + * chaining. See the source code for more information... :) + * + * Copyright (c) 2006-2010, Salvatore Sanfilippo + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * * Neither the name of Redis nor the names of its contributors may be used + * to endorse or promote products derived from this software without + * specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +#include "fmacros.h" +#include "alloc.h" +#include +#include +#include +#include "dict.h" + +/* -------------------------- private prototypes ---------------------------- */ + +static int _dictExpandIfNeeded(dict *ht); +static unsigned long _dictNextPower(unsigned long size); +static int _dictKeyIndex(dict *ht, const void *key); +static int _dictInit(dict *ht, dictType *type, void *privDataPtr); + +/* -------------------------- hash functions -------------------------------- */ + +/* Generic hash function (a popular one from Bernstein). + * I tested a few and this was the best. */ +static unsigned int dictGenHashFunction(const unsigned char *buf, int len) { + unsigned int hash = 5381; + + while (len--) + hash = ((hash << 5) + hash) + (*buf++); /* hash * 33 + c */ + return hash; +} + +/* ----------------------------- API implementation ------------------------- */ + +/* Reset an hashtable already initialized with ht_init(). + * NOTE: This function should only called by ht_destroy(). */ +static void _dictReset(dict *ht) { + ht->table = NULL; + ht->size = 0; + ht->sizemask = 0; + ht->used = 0; +} + +/* Create a new hash table */ +static dict *dictCreate(dictType *type, void *privDataPtr) { + dict *ht = hi_malloc(sizeof(*ht)); + _dictInit(ht,type,privDataPtr); + return ht; +} + +/* Initialize the hash table */ +static int _dictInit(dict *ht, dictType *type, void *privDataPtr) { + _dictReset(ht); + ht->type = type; + ht->privdata = privDataPtr; + return DICT_OK; +} + +/* Expand or create the hashtable */ +static int dictExpand(dict *ht, unsigned long size) { + dict n; /* the new hashtable */ + unsigned long realsize = _dictNextPower(size), i; + + /* the size is invalid if it is smaller than the number of + * elements already inside the hashtable */ + if (ht->used > size) + return DICT_ERR; + + _dictInit(&n, ht->type, ht->privdata); + n.size = realsize; + n.sizemask = realsize-1; + n.table = calloc(realsize,sizeof(dictEntry*)); + + /* Copy all the elements from the old to the new table: + * note that if the old hash table is empty ht->size is zero, + * so dictExpand just creates an hash table. */ + n.used = ht->used; + for (i = 0; i < ht->size && ht->used > 0; i++) { + dictEntry *he, *nextHe; + + if (ht->table[i] == NULL) continue; + + /* For each hash entry on this slot... */ + he = ht->table[i]; + while(he) { + unsigned int h; + + nextHe = he->next; + /* Get the new element index */ + h = dictHashKey(ht, he->key) & n.sizemask; + he->next = n.table[h]; + n.table[h] = he; + ht->used--; + /* Pass to the next element */ + he = nextHe; + } + } + assert(ht->used == 0); + free(ht->table); + + /* Remap the new hashtable in the old */ + *ht = n; + return DICT_OK; +} + +/* Add an element to the target hash table */ +static int dictAdd(dict *ht, void *key, void *val) { + int index; + dictEntry *entry; + + /* Get the index of the new element, or -1 if + * the element already exists. */ + if ((index = _dictKeyIndex(ht, key)) == -1) + return DICT_ERR; + + /* Allocates the memory and stores key */ + entry = hi_malloc(sizeof(*entry)); + entry->next = ht->table[index]; + ht->table[index] = entry; + + /* Set the hash entry fields. */ + dictSetHashKey(ht, entry, key); + dictSetHashVal(ht, entry, val); + ht->used++; + return DICT_OK; +} + +/* Add an element, discarding the old if the key already exists. + * Return 1 if the key was added from scratch, 0 if there was already an + * element with such key and dictReplace() just performed a value update + * operation. */ +static int dictReplace(dict *ht, void *key, void *val) { + dictEntry *entry, auxentry; + + /* Try to add the element. If the key + * does not exists dictAdd will succeed. */ + if (dictAdd(ht, key, val) == DICT_OK) + return 1; + /* It already exists, get the entry */ + entry = dictFind(ht, key); + /* Free the old value and set the new one */ + /* Set the new value and free the old one. Note that it is important + * to do that in this order, as the value may just be exactly the same + * as the previous one. In this context, think to reference counting, + * you want to increment (set), and then decrement (free), and not the + * reverse. */ + auxentry = *entry; + dictSetHashVal(ht, entry, val); + dictFreeEntryVal(ht, &auxentry); + return 0; +} + +/* Search and remove an element */ +static int dictDelete(dict *ht, const void *key) { + unsigned int h; + dictEntry *de, *prevde; + + if (ht->size == 0) + return DICT_ERR; + h = dictHashKey(ht, key) & ht->sizemask; + de = ht->table[h]; + + prevde = NULL; + while(de) { + if (dictCompareHashKeys(ht,key,de->key)) { + /* Unlink the element from the list */ + if (prevde) + prevde->next = de->next; + else + ht->table[h] = de->next; + + dictFreeEntryKey(ht,de); + dictFreeEntryVal(ht,de); + free(de); + ht->used--; + return DICT_OK; + } + prevde = de; + de = de->next; + } + return DICT_ERR; /* not found */ +} + +/* Destroy an entire hash table */ +static int _dictClear(dict *ht) { + unsigned long i; + + /* Free all the elements */ + for (i = 0; i < ht->size && ht->used > 0; i++) { + dictEntry *he, *nextHe; + + if ((he = ht->table[i]) == NULL) continue; + while(he) { + nextHe = he->next; + dictFreeEntryKey(ht, he); + dictFreeEntryVal(ht, he); + free(he); + ht->used--; + he = nextHe; + } + } + /* Free the table and the allocated cache structure */ + free(ht->table); + /* Re-initialize the table */ + _dictReset(ht); + return DICT_OK; /* never fails */ +} + +/* Clear & Release the hash table */ +static void dictRelease(dict *ht) { + _dictClear(ht); + free(ht); +} + +static dictEntry *dictFind(dict *ht, const void *key) { + dictEntry *he; + unsigned int h; + + if (ht->size == 0) return NULL; + h = dictHashKey(ht, key) & ht->sizemask; + he = ht->table[h]; + while(he) { + if (dictCompareHashKeys(ht, key, he->key)) + return he; + he = he->next; + } + return NULL; +} + +static dictIterator *dictGetIterator(dict *ht) { + dictIterator *iter = hi_malloc(sizeof(*iter)); + + iter->ht = ht; + iter->index = -1; + iter->entry = NULL; + iter->nextEntry = NULL; + return iter; +} + +static dictEntry *dictNext(dictIterator *iter) { + while (1) { + if (iter->entry == NULL) { + iter->index++; + if (iter->index >= + (signed)iter->ht->size) break; + iter->entry = iter->ht->table[iter->index]; + } else { + iter->entry = iter->nextEntry; + } + if (iter->entry) { + /* We need to save the 'next' here, the iterator user + * may delete the entry we are returning. */ + iter->nextEntry = iter->entry->next; + return iter->entry; + } + } + return NULL; +} + +static void dictReleaseIterator(dictIterator *iter) { + free(iter); +} + +/* ------------------------- private functions ------------------------------ */ + +/* Expand the hash table if needed */ +static int _dictExpandIfNeeded(dict *ht) { + /* If the hash table is empty expand it to the initial size, + * if the table is "full" dobule its size. */ + if (ht->size == 0) + return dictExpand(ht, DICT_HT_INITIAL_SIZE); + if (ht->used == ht->size) + return dictExpand(ht, ht->size*2); + return DICT_OK; +} + +/* Our hash table capability is a power of two */ +static unsigned long _dictNextPower(unsigned long size) { + unsigned long i = DICT_HT_INITIAL_SIZE; + + if (size >= LONG_MAX) return LONG_MAX; + while(1) { + if (i >= size) + return i; + i *= 2; + } +} + +/* Returns the index of a free slot that can be populated with + * an hash entry for the given 'key'. + * If the key already exists, -1 is returned. */ +static int _dictKeyIndex(dict *ht, const void *key) { + unsigned int h; + dictEntry *he; + + /* Expand the hashtable if needed */ + if (_dictExpandIfNeeded(ht) == DICT_ERR) + return -1; + /* Compute the key hash value */ + h = dictHashKey(ht, key) & ht->sizemask; + /* Search if this slot does not already contain the given key */ + he = ht->table[h]; + while(he) { + if (dictCompareHashKeys(ht, key, he->key)) + return -1; + he = he->next; + } + return h; +} + diff --git a/controller/thirdparty/hiredis-0.14.1/dict.h b/controller/thirdparty/hiredis-0.14.1/dict.h new file mode 100644 index 000000000..95fcd280e --- /dev/null +++ b/controller/thirdparty/hiredis-0.14.1/dict.h @@ -0,0 +1,126 @@ +/* Hash table implementation. + * + * This file implements in memory hash tables with insert/del/replace/find/ + * get-random-element operations. Hash tables will auto resize if needed + * tables of power of two in size are used, collisions are handled by + * chaining. See the source code for more information... :) + * + * Copyright (c) 2006-2010, Salvatore Sanfilippo + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * * Neither the name of Redis nor the names of its contributors may be used + * to endorse or promote products derived from this software without + * specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +#ifndef __DICT_H +#define __DICT_H + +#define DICT_OK 0 +#define DICT_ERR 1 + +/* Unused arguments generate annoying warnings... */ +#define DICT_NOTUSED(V) ((void) V) + +typedef struct dictEntry { + void *key; + void *val; + struct dictEntry *next; +} dictEntry; + +typedef struct dictType { + unsigned int (*hashFunction)(const void *key); + void *(*keyDup)(void *privdata, const void *key); + void *(*valDup)(void *privdata, const void *obj); + int (*keyCompare)(void *privdata, const void *key1, const void *key2); + void (*keyDestructor)(void *privdata, void *key); + void (*valDestructor)(void *privdata, void *obj); +} dictType; + +typedef struct dict { + dictEntry **table; + dictType *type; + unsigned long size; + unsigned long sizemask; + unsigned long used; + void *privdata; +} dict; + +typedef struct dictIterator { + dict *ht; + int index; + dictEntry *entry, *nextEntry; +} dictIterator; + +/* This is the initial size of every hash table */ +#define DICT_HT_INITIAL_SIZE 4 + +/* ------------------------------- Macros ------------------------------------*/ +#define dictFreeEntryVal(ht, entry) \ + if ((ht)->type->valDestructor) \ + (ht)->type->valDestructor((ht)->privdata, (entry)->val) + +#define dictSetHashVal(ht, entry, _val_) do { \ + if ((ht)->type->valDup) \ + entry->val = (ht)->type->valDup((ht)->privdata, _val_); \ + else \ + entry->val = (_val_); \ +} while(0) + +#define dictFreeEntryKey(ht, entry) \ + if ((ht)->type->keyDestructor) \ + (ht)->type->keyDestructor((ht)->privdata, (entry)->key) + +#define dictSetHashKey(ht, entry, _key_) do { \ + if ((ht)->type->keyDup) \ + entry->key = (ht)->type->keyDup((ht)->privdata, _key_); \ + else \ + entry->key = (_key_); \ +} while(0) + +#define dictCompareHashKeys(ht, key1, key2) \ + (((ht)->type->keyCompare) ? \ + (ht)->type->keyCompare((ht)->privdata, key1, key2) : \ + (key1) == (key2)) + +#define dictHashKey(ht, key) (ht)->type->hashFunction(key) + +#define dictGetEntryKey(he) ((he)->key) +#define dictGetEntryVal(he) ((he)->val) +#define dictSlots(ht) ((ht)->size) +#define dictSize(ht) ((ht)->used) + +/* API */ +static unsigned int dictGenHashFunction(const unsigned char *buf, int len); +static dict *dictCreate(dictType *type, void *privDataPtr); +static int dictExpand(dict *ht, unsigned long size); +static int dictAdd(dict *ht, void *key, void *val); +static int dictReplace(dict *ht, void *key, void *val); +static int dictDelete(dict *ht, const void *key); +static void dictRelease(dict *ht); +static dictEntry * dictFind(dict *ht, const void *key); +static dictIterator *dictGetIterator(dict *ht); +static dictEntry *dictNext(dictIterator *iter); +static void dictReleaseIterator(dictIterator *iter); + +#endif /* __DICT_H */ diff --git a/controller/thirdparty/hiredis-0.14.1/examples/example-ae.c b/controller/thirdparty/hiredis-0.14.1/examples/example-ae.c new file mode 100644 index 000000000..8efa7306a --- /dev/null +++ b/controller/thirdparty/hiredis-0.14.1/examples/example-ae.c @@ -0,0 +1,62 @@ +#include +#include +#include +#include + +#include +#include +#include + +/* Put event loop in the global scope, so it can be explicitly stopped */ +static aeEventLoop *loop; + +void getCallback(redisAsyncContext *c, void *r, void *privdata) { + redisReply *reply = r; + if (reply == NULL) return; + printf("argv[%s]: %s\n", (char*)privdata, reply->str); + + /* Disconnect after receiving the reply to GET */ + redisAsyncDisconnect(c); +} + +void connectCallback(const redisAsyncContext *c, int status) { + if (status != REDIS_OK) { + printf("Error: %s\n", c->errstr); + aeStop(loop); + return; + } + + printf("Connected...\n"); +} + +void disconnectCallback(const redisAsyncContext *c, int status) { + if (status != REDIS_OK) { + printf("Error: %s\n", c->errstr); + aeStop(loop); + return; + } + + printf("Disconnected...\n"); + aeStop(loop); +} + +int main (int argc, char **argv) { + signal(SIGPIPE, SIG_IGN); + + redisAsyncContext *c = redisAsyncConnect("127.0.0.1", 6379); + if (c->err) { + /* Let *c leak for now... */ + printf("Error: %s\n", c->errstr); + return 1; + } + + loop = aeCreateEventLoop(64); + redisAeAttach(loop, c); + redisAsyncSetConnectCallback(c,connectCallback); + redisAsyncSetDisconnectCallback(c,disconnectCallback); + redisAsyncCommand(c, NULL, NULL, "SET key %b", argv[argc-1], strlen(argv[argc-1])); + redisAsyncCommand(c, getCallback, (char*)"end-1", "GET key"); + aeMain(loop); + return 0; +} + diff --git a/controller/thirdparty/hiredis-0.14.1/examples/example-glib.c b/controller/thirdparty/hiredis-0.14.1/examples/example-glib.c new file mode 100644 index 000000000..d6e10f8e8 --- /dev/null +++ b/controller/thirdparty/hiredis-0.14.1/examples/example-glib.c @@ -0,0 +1,73 @@ +#include + +#include +#include +#include + +static GMainLoop *mainloop; + +static void +connect_cb (const redisAsyncContext *ac G_GNUC_UNUSED, + int status) +{ + if (status != REDIS_OK) { + g_printerr("Failed to connect: %s\n", ac->errstr); + g_main_loop_quit(mainloop); + } else { + g_printerr("Connected...\n"); + } +} + +static void +disconnect_cb (const redisAsyncContext *ac G_GNUC_UNUSED, + int status) +{ + if (status != REDIS_OK) { + g_error("Failed to disconnect: %s", ac->errstr); + } else { + g_printerr("Disconnected...\n"); + g_main_loop_quit(mainloop); + } +} + +static void +command_cb(redisAsyncContext *ac, + gpointer r, + gpointer user_data G_GNUC_UNUSED) +{ + redisReply *reply = r; + + if (reply) { + g_print("REPLY: %s\n", reply->str); + } + + redisAsyncDisconnect(ac); +} + +gint +main (gint argc G_GNUC_UNUSED, + gchar *argv[] G_GNUC_UNUSED) +{ + redisAsyncContext *ac; + GMainContext *context = NULL; + GSource *source; + + ac = redisAsyncConnect("127.0.0.1", 6379); + if (ac->err) { + g_printerr("%s\n", ac->errstr); + exit(EXIT_FAILURE); + } + + source = redis_source_new(ac); + mainloop = g_main_loop_new(context, FALSE); + g_source_attach(source, context); + + redisAsyncSetConnectCallback(ac, connect_cb); + redisAsyncSetDisconnectCallback(ac, disconnect_cb); + redisAsyncCommand(ac, command_cb, NULL, "SET key 1234"); + redisAsyncCommand(ac, command_cb, NULL, "GET key"); + + g_main_loop_run(mainloop); + + return EXIT_SUCCESS; +} diff --git a/controller/thirdparty/hiredis-0.14.1/examples/example-ivykis.c b/controller/thirdparty/hiredis-0.14.1/examples/example-ivykis.c new file mode 100644 index 000000000..67affcef3 --- /dev/null +++ b/controller/thirdparty/hiredis-0.14.1/examples/example-ivykis.c @@ -0,0 +1,58 @@ +#include +#include +#include +#include + +#include +#include +#include + +void getCallback(redisAsyncContext *c, void *r, void *privdata) { + redisReply *reply = r; + if (reply == NULL) return; + printf("argv[%s]: %s\n", (char*)privdata, reply->str); + + /* Disconnect after receiving the reply to GET */ + redisAsyncDisconnect(c); +} + +void connectCallback(const redisAsyncContext *c, int status) { + if (status != REDIS_OK) { + printf("Error: %s\n", c->errstr); + return; + } + printf("Connected...\n"); +} + +void disconnectCallback(const redisAsyncContext *c, int status) { + if (status != REDIS_OK) { + printf("Error: %s\n", c->errstr); + return; + } + printf("Disconnected...\n"); +} + +int main (int argc, char **argv) { + signal(SIGPIPE, SIG_IGN); + + iv_init(); + + redisAsyncContext *c = redisAsyncConnect("127.0.0.1", 6379); + if (c->err) { + /* Let *c leak for now... */ + printf("Error: %s\n", c->errstr); + return 1; + } + + redisIvykisAttach(c); + redisAsyncSetConnectCallback(c,connectCallback); + redisAsyncSetDisconnectCallback(c,disconnectCallback); + redisAsyncCommand(c, NULL, NULL, "SET key %b", argv[argc-1], strlen(argv[argc-1])); + redisAsyncCommand(c, getCallback, (char*)"end-1", "GET key"); + + iv_main(); + + iv_deinit(); + + return 0; +} diff --git a/controller/thirdparty/hiredis-0.14.1/examples/example-libev.c b/controller/thirdparty/hiredis-0.14.1/examples/example-libev.c new file mode 100644 index 000000000..cc8b166ec --- /dev/null +++ b/controller/thirdparty/hiredis-0.14.1/examples/example-libev.c @@ -0,0 +1,52 @@ +#include +#include +#include +#include + +#include +#include +#include + +void getCallback(redisAsyncContext *c, void *r, void *privdata) { + redisReply *reply = r; + if (reply == NULL) return; + printf("argv[%s]: %s\n", (char*)privdata, reply->str); + + /* Disconnect after receiving the reply to GET */ + redisAsyncDisconnect(c); +} + +void connectCallback(const redisAsyncContext *c, int status) { + if (status != REDIS_OK) { + printf("Error: %s\n", c->errstr); + return; + } + printf("Connected...\n"); +} + +void disconnectCallback(const redisAsyncContext *c, int status) { + if (status != REDIS_OK) { + printf("Error: %s\n", c->errstr); + return; + } + printf("Disconnected...\n"); +} + +int main (int argc, char **argv) { + signal(SIGPIPE, SIG_IGN); + + redisAsyncContext *c = redisAsyncConnect("127.0.0.1", 6379); + if (c->err) { + /* Let *c leak for now... */ + printf("Error: %s\n", c->errstr); + return 1; + } + + redisLibevAttach(EV_DEFAULT_ c); + redisAsyncSetConnectCallback(c,connectCallback); + redisAsyncSetDisconnectCallback(c,disconnectCallback); + redisAsyncCommand(c, NULL, NULL, "SET key %b", argv[argc-1], strlen(argv[argc-1])); + redisAsyncCommand(c, getCallback, (char*)"end-1", "GET key"); + ev_loop(EV_DEFAULT_ 0); + return 0; +} diff --git a/controller/thirdparty/hiredis-0.14.1/examples/example-libevent.c b/controller/thirdparty/hiredis-0.14.1/examples/example-libevent.c new file mode 100644 index 000000000..d333c22b7 --- /dev/null +++ b/controller/thirdparty/hiredis-0.14.1/examples/example-libevent.c @@ -0,0 +1,53 @@ +#include +#include +#include +#include + +#include +#include +#include + +void getCallback(redisAsyncContext *c, void *r, void *privdata) { + redisReply *reply = r; + if (reply == NULL) return; + printf("argv[%s]: %s\n", (char*)privdata, reply->str); + + /* Disconnect after receiving the reply to GET */ + redisAsyncDisconnect(c); +} + +void connectCallback(const redisAsyncContext *c, int status) { + if (status != REDIS_OK) { + printf("Error: %s\n", c->errstr); + return; + } + printf("Connected...\n"); +} + +void disconnectCallback(const redisAsyncContext *c, int status) { + if (status != REDIS_OK) { + printf("Error: %s\n", c->errstr); + return; + } + printf("Disconnected...\n"); +} + +int main (int argc, char **argv) { + signal(SIGPIPE, SIG_IGN); + struct event_base *base = event_base_new(); + + redisAsyncContext *c = redisAsyncConnect("127.0.0.1", 6379); + if (c->err) { + /* Let *c leak for now... */ + printf("Error: %s\n", c->errstr); + return 1; + } + + redisLibeventAttach(c,base); + redisAsyncSetConnectCallback(c,connectCallback); + redisAsyncSetDisconnectCallback(c,disconnectCallback); + redisAsyncCommand(c, NULL, NULL, "SET key %b", argv[argc-1], strlen(argv[argc-1])); + redisAsyncCommand(c, getCallback, (char*)"end-1", "GET key"); + event_base_dispatch(base); + return 0; +} diff --git a/controller/thirdparty/hiredis-0.14.1/examples/example-libuv.c b/controller/thirdparty/hiredis-0.14.1/examples/example-libuv.c new file mode 100644 index 000000000..a5462d410 --- /dev/null +++ b/controller/thirdparty/hiredis-0.14.1/examples/example-libuv.c @@ -0,0 +1,53 @@ +#include +#include +#include +#include + +#include +#include +#include + +void getCallback(redisAsyncContext *c, void *r, void *privdata) { + redisReply *reply = r; + if (reply == NULL) return; + printf("argv[%s]: %s\n", (char*)privdata, reply->str); + + /* Disconnect after receiving the reply to GET */ + redisAsyncDisconnect(c); +} + +void connectCallback(const redisAsyncContext *c, int status) { + if (status != REDIS_OK) { + printf("Error: %s\n", c->errstr); + return; + } + printf("Connected...\n"); +} + +void disconnectCallback(const redisAsyncContext *c, int status) { + if (status != REDIS_OK) { + printf("Error: %s\n", c->errstr); + return; + } + printf("Disconnected...\n"); +} + +int main (int argc, char **argv) { + signal(SIGPIPE, SIG_IGN); + uv_loop_t* loop = uv_default_loop(); + + redisAsyncContext *c = redisAsyncConnect("127.0.0.1", 6379); + if (c->err) { + /* Let *c leak for now... */ + printf("Error: %s\n", c->errstr); + return 1; + } + + redisLibuvAttach(c,loop); + redisAsyncSetConnectCallback(c,connectCallback); + redisAsyncSetDisconnectCallback(c,disconnectCallback); + redisAsyncCommand(c, NULL, NULL, "SET key %b", argv[argc-1], strlen(argv[argc-1])); + redisAsyncCommand(c, getCallback, (char*)"end-1", "GET key"); + uv_run(loop, UV_RUN_DEFAULT); + return 0; +} diff --git a/controller/thirdparty/hiredis-0.14.1/examples/example-macosx.c b/controller/thirdparty/hiredis-0.14.1/examples/example-macosx.c new file mode 100644 index 000000000..bc84ed5ba --- /dev/null +++ b/controller/thirdparty/hiredis-0.14.1/examples/example-macosx.c @@ -0,0 +1,66 @@ +// +// Created by Дмитрий Бахвалов on 13.07.15. +// Copyright (c) 2015 Dmitry Bakhvalov. All rights reserved. +// + +#include + +#include +#include +#include + +void getCallback(redisAsyncContext *c, void *r, void *privdata) { + redisReply *reply = r; + if (reply == NULL) return; + printf("argv[%s]: %s\n", (char*)privdata, reply->str); + + /* Disconnect after receiving the reply to GET */ + redisAsyncDisconnect(c); +} + +void connectCallback(const redisAsyncContext *c, int status) { + if (status != REDIS_OK) { + printf("Error: %s\n", c->errstr); + return; + } + printf("Connected...\n"); +} + +void disconnectCallback(const redisAsyncContext *c, int status) { + if (status != REDIS_OK) { + printf("Error: %s\n", c->errstr); + return; + } + CFRunLoopStop(CFRunLoopGetCurrent()); + printf("Disconnected...\n"); +} + +int main (int argc, char **argv) { + signal(SIGPIPE, SIG_IGN); + + CFRunLoopRef loop = CFRunLoopGetCurrent(); + if( !loop ) { + printf("Error: Cannot get current run loop\n"); + return 1; + } + + redisAsyncContext *c = redisAsyncConnect("127.0.0.1", 6379); + if (c->err) { + /* Let *c leak for now... */ + printf("Error: %s\n", c->errstr); + return 1; + } + + redisMacOSAttach(c, loop); + + redisAsyncSetConnectCallback(c,connectCallback); + redisAsyncSetDisconnectCallback(c,disconnectCallback); + + redisAsyncCommand(c, NULL, NULL, "SET key %b", argv[argc-1], strlen(argv[argc-1])); + redisAsyncCommand(c, getCallback, (char*)"end-1", "GET key"); + + CFRunLoopRun(); + + return 0; +} + diff --git a/controller/thirdparty/hiredis-0.14.1/examples/example-qt.cpp b/controller/thirdparty/hiredis-0.14.1/examples/example-qt.cpp new file mode 100644 index 000000000..f524c3f3d --- /dev/null +++ b/controller/thirdparty/hiredis-0.14.1/examples/example-qt.cpp @@ -0,0 +1,46 @@ +#include +using namespace std; + +#include +#include + +#include "example-qt.h" + +void getCallback(redisAsyncContext *, void * r, void * privdata) { + + redisReply * reply = static_cast(r); + ExampleQt * ex = static_cast(privdata); + if (reply == nullptr || ex == nullptr) return; + + cout << "key: " << reply->str << endl; + + ex->finish(); +} + +void ExampleQt::run() { + + m_ctx = redisAsyncConnect("localhost", 6379); + + if (m_ctx->err) { + cerr << "Error: " << m_ctx->errstr << endl; + redisAsyncFree(m_ctx); + emit finished(); + } + + m_adapter.setContext(m_ctx); + + redisAsyncCommand(m_ctx, NULL, NULL, "SET key %s", m_value); + redisAsyncCommand(m_ctx, getCallback, this, "GET key"); +} + +int main (int argc, char **argv) { + + QCoreApplication app(argc, argv); + + ExampleQt example(argv[argc-1]); + + QObject::connect(&example, SIGNAL(finished()), &app, SLOT(quit())); + QTimer::singleShot(0, &example, SLOT(run())); + + return app.exec(); +} diff --git a/controller/thirdparty/hiredis-0.14.1/examples/example-qt.h b/controller/thirdparty/hiredis-0.14.1/examples/example-qt.h new file mode 100644 index 000000000..374f47666 --- /dev/null +++ b/controller/thirdparty/hiredis-0.14.1/examples/example-qt.h @@ -0,0 +1,32 @@ +#ifndef __HIREDIS_EXAMPLE_QT_H +#define __HIREDIS_EXAMPLE_QT_H + +#include + +class ExampleQt : public QObject { + + Q_OBJECT + + public: + ExampleQt(const char * value, QObject * parent = 0) + : QObject(parent), m_value(value) {} + + signals: + void finished(); + + public slots: + void run(); + + private: + void finish() { emit finished(); } + + private: + const char * m_value; + redisAsyncContext * m_ctx; + RedisQtAdapter m_adapter; + + friend + void getCallback(redisAsyncContext *, void *, void *); +}; + +#endif /* !__HIREDIS_EXAMPLE_QT_H */ diff --git a/controller/thirdparty/hiredis-0.14.1/examples/example.c b/controller/thirdparty/hiredis-0.14.1/examples/example.c new file mode 100644 index 000000000..4d494c55a --- /dev/null +++ b/controller/thirdparty/hiredis-0.14.1/examples/example.c @@ -0,0 +1,78 @@ +#include +#include +#include + +#include + +int main(int argc, char **argv) { + unsigned int j; + redisContext *c; + redisReply *reply; + const char *hostname = (argc > 1) ? argv[1] : "127.0.0.1"; + int port = (argc > 2) ? atoi(argv[2]) : 6379; + + struct timeval timeout = { 1, 500000 }; // 1.5 seconds + c = redisConnectWithTimeout(hostname, port, timeout); + if (c == NULL || c->err) { + if (c) { + printf("Connection error: %s\n", c->errstr); + redisFree(c); + } else { + printf("Connection error: can't allocate redis context\n"); + } + exit(1); + } + + /* PING server */ + reply = redisCommand(c,"PING"); + printf("PING: %s\n", reply->str); + freeReplyObject(reply); + + /* Set a key */ + reply = redisCommand(c,"SET %s %s", "foo", "hello world"); + printf("SET: %s\n", reply->str); + freeReplyObject(reply); + + /* Set a key using binary safe API */ + reply = redisCommand(c,"SET %b %b", "bar", (size_t) 3, "hello", (size_t) 5); + printf("SET (binary API): %s\n", reply->str); + freeReplyObject(reply); + + /* Try a GET and two INCR */ + reply = redisCommand(c,"GET foo"); + printf("GET foo: %s\n", reply->str); + freeReplyObject(reply); + + reply = redisCommand(c,"INCR counter"); + printf("INCR counter: %lld\n", reply->integer); + freeReplyObject(reply); + /* again ... */ + reply = redisCommand(c,"INCR counter"); + printf("INCR counter: %lld\n", reply->integer); + freeReplyObject(reply); + + /* Create a list of numbers, from 0 to 9 */ + reply = redisCommand(c,"DEL mylist"); + freeReplyObject(reply); + for (j = 0; j < 10; j++) { + char buf[64]; + + snprintf(buf,64,"%u",j); + reply = redisCommand(c,"LPUSH mylist element-%s", buf); + freeReplyObject(reply); + } + + /* Let's check what we have inside the list */ + reply = redisCommand(c,"LRANGE mylist 0 -1"); + if (reply->type == REDIS_REPLY_ARRAY) { + for (j = 0; j < reply->elements; j++) { + printf("%u) %s\n", j, reply->element[j]->str); + } + } + freeReplyObject(reply); + + /* Disconnects and frees the context */ + redisFree(c); + + return 0; +} diff --git a/controller/thirdparty/hiredis-0.14.1/fmacros.h b/controller/thirdparty/hiredis-0.14.1/fmacros.h new file mode 100644 index 000000000..3227faafd --- /dev/null +++ b/controller/thirdparty/hiredis-0.14.1/fmacros.h @@ -0,0 +1,12 @@ +#ifndef __HIREDIS_FMACRO_H +#define __HIREDIS_FMACRO_H + +#define _XOPEN_SOURCE 600 +#define _POSIX_C_SOURCE 200112L + +#if defined(__APPLE__) && defined(__MACH__) +/* Enable TCP_KEEPALIVE */ +#define _DARWIN_C_SOURCE +#endif + +#endif diff --git a/controller/thirdparty/hiredis-0.14.1/hiredis.c b/controller/thirdparty/hiredis-0.14.1/hiredis.c new file mode 100644 index 000000000..98f43c993 --- /dev/null +++ b/controller/thirdparty/hiredis-0.14.1/hiredis.c @@ -0,0 +1,1006 @@ +/* + * Copyright (c) 2009-2011, Salvatore Sanfilippo + * Copyright (c) 2010-2014, Pieter Noordhuis + * Copyright (c) 2015, Matt Stancliff , + * Jan-Erik Rediger + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * * Neither the name of Redis nor the names of its contributors may be used + * to endorse or promote products derived from this software without + * specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +#include "fmacros.h" +#include +#include +#include +#include +#include +#include + +#include "hiredis.h" +#include "net.h" +#include "sds.h" + +static redisReply *createReplyObject(int type); +static void *createStringObject(const redisReadTask *task, char *str, size_t len); +static void *createArrayObject(const redisReadTask *task, int elements); +static void *createIntegerObject(const redisReadTask *task, long long value); +static void *createNilObject(const redisReadTask *task); + +/* Default set of functions to build the reply. Keep in mind that such a + * function returning NULL is interpreted as OOM. */ +static redisReplyObjectFunctions defaultFunctions = { + createStringObject, + createArrayObject, + createIntegerObject, + createNilObject, + freeReplyObject +}; + +/* Create a reply object */ +static redisReply *createReplyObject(int type) { + redisReply *r = calloc(1,sizeof(*r)); + + if (r == NULL) + return NULL; + + r->type = type; + return r; +} + +/* Free a reply object */ +void freeReplyObject(void *reply) { + redisReply *r = reply; + size_t j; + + if (r == NULL) + return; + + switch(r->type) { + case REDIS_REPLY_INTEGER: + break; /* Nothing to free */ + case REDIS_REPLY_ARRAY: + if (r->element != NULL) { + for (j = 0; j < r->elements; j++) + freeReplyObject(r->element[j]); + free(r->element); + } + break; + case REDIS_REPLY_ERROR: + case REDIS_REPLY_STATUS: + case REDIS_REPLY_STRING: + free(r->str); + break; + } + free(r); +} + +static void *createStringObject(const redisReadTask *task, char *str, size_t len) { + redisReply *r, *parent; + char *buf; + + r = createReplyObject(task->type); + if (r == NULL) + return NULL; + + buf = malloc(len+1); + if (buf == NULL) { + freeReplyObject(r); + return NULL; + } + + assert(task->type == REDIS_REPLY_ERROR || + task->type == REDIS_REPLY_STATUS || + task->type == REDIS_REPLY_STRING); + + /* Copy string value */ + memcpy(buf,str,len); + buf[len] = '\0'; + r->str = buf; + r->len = len; + + if (task->parent) { + parent = task->parent->obj; + assert(parent->type == REDIS_REPLY_ARRAY); + parent->element[task->idx] = r; + } + return r; +} + +static void *createArrayObject(const redisReadTask *task, int elements) { + redisReply *r, *parent; + + r = createReplyObject(REDIS_REPLY_ARRAY); + if (r == NULL) + return NULL; + + if (elements > 0) { + r->element = calloc(elements,sizeof(redisReply*)); + if (r->element == NULL) { + freeReplyObject(r); + return NULL; + } + } + + r->elements = elements; + + if (task->parent) { + parent = task->parent->obj; + assert(parent->type == REDIS_REPLY_ARRAY); + parent->element[task->idx] = r; + } + return r; +} + +static void *createIntegerObject(const redisReadTask *task, long long value) { + redisReply *r, *parent; + + r = createReplyObject(REDIS_REPLY_INTEGER); + if (r == NULL) + return NULL; + + r->integer = value; + + if (task->parent) { + parent = task->parent->obj; + assert(parent->type == REDIS_REPLY_ARRAY); + parent->element[task->idx] = r; + } + return r; +} + +static void *createNilObject(const redisReadTask *task) { + redisReply *r, *parent; + + r = createReplyObject(REDIS_REPLY_NIL); + if (r == NULL) + return NULL; + + if (task->parent) { + parent = task->parent->obj; + assert(parent->type == REDIS_REPLY_ARRAY); + parent->element[task->idx] = r; + } + return r; +} + +/* Return the number of digits of 'v' when converted to string in radix 10. + * Implementation borrowed from link in redis/src/util.c:string2ll(). */ +static uint32_t countDigits(uint64_t v) { + uint32_t result = 1; + for (;;) { + if (v < 10) return result; + if (v < 100) return result + 1; + if (v < 1000) return result + 2; + if (v < 10000) return result + 3; + v /= 10000U; + result += 4; + } +} + +/* Helper that calculates the bulk length given a certain string length. */ +static size_t bulklen(size_t len) { + return 1+countDigits(len)+2+len+2; +} + +int redisvFormatCommand(char **target, const char *format, va_list ap) { + const char *c = format; + char *cmd = NULL; /* final command */ + int pos; /* position in final command */ + sds curarg, newarg; /* current argument */ + int touched = 0; /* was the current argument touched? */ + char **curargv = NULL, **newargv = NULL; + int argc = 0; + int totlen = 0; + int error_type = 0; /* 0 = no error; -1 = memory error; -2 = format error */ + int j; + + /* Abort if there is not target to set */ + if (target == NULL) + return -1; + + /* Build the command string accordingly to protocol */ + curarg = sdsempty(); + if (curarg == NULL) + return -1; + + while(*c != '\0') { + if (*c != '%' || c[1] == '\0') { + if (*c == ' ') { + if (touched) { + newargv = realloc(curargv,sizeof(char*)*(argc+1)); + if (newargv == NULL) goto memory_err; + curargv = newargv; + curargv[argc++] = curarg; + totlen += bulklen(sdslen(curarg)); + + /* curarg is put in argv so it can be overwritten. */ + curarg = sdsempty(); + if (curarg == NULL) goto memory_err; + touched = 0; + } + } else { + newarg = sdscatlen(curarg,c,1); + if (newarg == NULL) goto memory_err; + curarg = newarg; + touched = 1; + } + } else { + char *arg; + size_t size; + + /* Set newarg so it can be checked even if it is not touched. */ + newarg = curarg; + + switch(c[1]) { + case 's': + arg = va_arg(ap,char*); + size = strlen(arg); + if (size > 0) + newarg = sdscatlen(curarg,arg,size); + break; + case 'b': + arg = va_arg(ap,char*); + size = va_arg(ap,size_t); + if (size > 0) + newarg = sdscatlen(curarg,arg,size); + break; + case '%': + newarg = sdscat(curarg,"%"); + break; + default: + /* Try to detect printf format */ + { + static const char intfmts[] = "diouxX"; + static const char flags[] = "#0-+ "; + char _format[16]; + const char *_p = c+1; + size_t _l = 0; + va_list _cpy; + + /* Flags */ + while (*_p != '\0' && strchr(flags,*_p) != NULL) _p++; + + /* Field width */ + while (*_p != '\0' && isdigit(*_p)) _p++; + + /* Precision */ + if (*_p == '.') { + _p++; + while (*_p != '\0' && isdigit(*_p)) _p++; + } + + /* Copy va_list before consuming with va_arg */ + va_copy(_cpy,ap); + + /* Integer conversion (without modifiers) */ + if (strchr(intfmts,*_p) != NULL) { + va_arg(ap,int); + goto fmt_valid; + } + + /* Double conversion (without modifiers) */ + if (strchr("eEfFgGaA",*_p) != NULL) { + va_arg(ap,double); + goto fmt_valid; + } + + /* Size: char */ + if (_p[0] == 'h' && _p[1] == 'h') { + _p += 2; + if (*_p != '\0' && strchr(intfmts,*_p) != NULL) { + va_arg(ap,int); /* char gets promoted to int */ + goto fmt_valid; + } + goto fmt_invalid; + } + + /* Size: short */ + if (_p[0] == 'h') { + _p += 1; + if (*_p != '\0' && strchr(intfmts,*_p) != NULL) { + va_arg(ap,int); /* short gets promoted to int */ + goto fmt_valid; + } + goto fmt_invalid; + } + + /* Size: long long */ + if (_p[0] == 'l' && _p[1] == 'l') { + _p += 2; + if (*_p != '\0' && strchr(intfmts,*_p) != NULL) { + va_arg(ap,long long); + goto fmt_valid; + } + goto fmt_invalid; + } + + /* Size: long */ + if (_p[0] == 'l') { + _p += 1; + if (*_p != '\0' && strchr(intfmts,*_p) != NULL) { + va_arg(ap,long); + goto fmt_valid; + } + goto fmt_invalid; + } + + fmt_invalid: + va_end(_cpy); + goto format_err; + + fmt_valid: + _l = (_p+1)-c; + if (_l < sizeof(_format)-2) { + memcpy(_format,c,_l); + _format[_l] = '\0'; + newarg = sdscatvprintf(curarg,_format,_cpy); + + /* Update current position (note: outer blocks + * increment c twice so compensate here) */ + c = _p-1; + } + + va_end(_cpy); + break; + } + } + + if (newarg == NULL) goto memory_err; + curarg = newarg; + + touched = 1; + c++; + } + c++; + } + + /* Add the last argument if needed */ + if (touched) { + newargv = realloc(curargv,sizeof(char*)*(argc+1)); + if (newargv == NULL) goto memory_err; + curargv = newargv; + curargv[argc++] = curarg; + totlen += bulklen(sdslen(curarg)); + } else { + sdsfree(curarg); + } + + /* Clear curarg because it was put in curargv or was free'd. */ + curarg = NULL; + + /* Add bytes needed to hold multi bulk count */ + totlen += 1+countDigits(argc)+2; + + /* Build the command at protocol level */ + cmd = malloc(totlen+1); + if (cmd == NULL) goto memory_err; + + pos = sprintf(cmd,"*%d\r\n",argc); + for (j = 0; j < argc; j++) { + pos += sprintf(cmd+pos,"$%zu\r\n",sdslen(curargv[j])); + memcpy(cmd+pos,curargv[j],sdslen(curargv[j])); + pos += sdslen(curargv[j]); + sdsfree(curargv[j]); + cmd[pos++] = '\r'; + cmd[pos++] = '\n'; + } + assert(pos == totlen); + cmd[pos] = '\0'; + + free(curargv); + *target = cmd; + return totlen; + +format_err: + error_type = -2; + goto cleanup; + +memory_err: + error_type = -1; + goto cleanup; + +cleanup: + if (curargv) { + while(argc--) + sdsfree(curargv[argc]); + free(curargv); + } + + sdsfree(curarg); + free(cmd); + + return error_type; +} + +/* Format a command according to the Redis protocol. This function + * takes a format similar to printf: + * + * %s represents a C null terminated string you want to interpolate + * %b represents a binary safe string + * + * When using %b you need to provide both the pointer to the string + * and the length in bytes as a size_t. Examples: + * + * len = redisFormatCommand(target, "GET %s", mykey); + * len = redisFormatCommand(target, "SET %s %b", mykey, myval, myvallen); + */ +int redisFormatCommand(char **target, const char *format, ...) { + va_list ap; + int len; + va_start(ap,format); + len = redisvFormatCommand(target,format,ap); + va_end(ap); + + /* The API says "-1" means bad result, but we now also return "-2" in some + * cases. Force the return value to always be -1. */ + if (len < 0) + len = -1; + + return len; +} + +/* Format a command according to the Redis protocol using an sds string and + * sdscatfmt for the processing of arguments. This function takes the + * number of arguments, an array with arguments and an array with their + * lengths. If the latter is set to NULL, strlen will be used to compute the + * argument lengths. + */ +int redisFormatSdsCommandArgv(sds *target, int argc, const char **argv, + const size_t *argvlen) +{ + sds cmd; + unsigned long long totlen; + int j; + size_t len; + + /* Abort on a NULL target */ + if (target == NULL) + return -1; + + /* Calculate our total size */ + totlen = 1+countDigits(argc)+2; + for (j = 0; j < argc; j++) { + len = argvlen ? argvlen[j] : strlen(argv[j]); + totlen += bulklen(len); + } + + /* Use an SDS string for command construction */ + cmd = sdsempty(); + if (cmd == NULL) + return -1; + + /* We already know how much storage we need */ + cmd = sdsMakeRoomFor(cmd, totlen); + if (cmd == NULL) + return -1; + + /* Construct command */ + cmd = sdscatfmt(cmd, "*%i\r\n", argc); + for (j=0; j < argc; j++) { + len = argvlen ? argvlen[j] : strlen(argv[j]); + cmd = sdscatfmt(cmd, "$%u\r\n", len); + cmd = sdscatlen(cmd, argv[j], len); + cmd = sdscatlen(cmd, "\r\n", sizeof("\r\n")-1); + } + + assert(sdslen(cmd)==totlen); + + *target = cmd; + return totlen; +} + +void redisFreeSdsCommand(sds cmd) { + sdsfree(cmd); +} + +/* Format a command according to the Redis protocol. This function takes the + * number of arguments, an array with arguments and an array with their + * lengths. If the latter is set to NULL, strlen will be used to compute the + * argument lengths. + */ +int redisFormatCommandArgv(char **target, int argc, const char **argv, const size_t *argvlen) { + char *cmd = NULL; /* final command */ + int pos; /* position in final command */ + size_t len; + int totlen, j; + + /* Abort on a NULL target */ + if (target == NULL) + return -1; + + /* Calculate number of bytes needed for the command */ + totlen = 1+countDigits(argc)+2; + for (j = 0; j < argc; j++) { + len = argvlen ? argvlen[j] : strlen(argv[j]); + totlen += bulklen(len); + } + + /* Build the command at protocol level */ + cmd = malloc(totlen+1); + if (cmd == NULL) + return -1; + + pos = sprintf(cmd,"*%d\r\n",argc); + for (j = 0; j < argc; j++) { + len = argvlen ? argvlen[j] : strlen(argv[j]); + pos += sprintf(cmd+pos,"$%zu\r\n",len); + memcpy(cmd+pos,argv[j],len); + pos += len; + cmd[pos++] = '\r'; + cmd[pos++] = '\n'; + } + assert(pos == totlen); + cmd[pos] = '\0'; + + *target = cmd; + return totlen; +} + +void redisFreeCommand(char *cmd) { + free(cmd); +} + +void __redisSetError(redisContext *c, int type, const char *str) { + size_t len; + + c->err = type; + if (str != NULL) { + len = strlen(str); + len = len < (sizeof(c->errstr)-1) ? len : (sizeof(c->errstr)-1); + memcpy(c->errstr,str,len); + c->errstr[len] = '\0'; + } else { + /* Only REDIS_ERR_IO may lack a description! */ + assert(type == REDIS_ERR_IO); + strerror_r(errno, c->errstr, sizeof(c->errstr)); + } +} + +redisReader *redisReaderCreate(void) { + return redisReaderCreateWithFunctions(&defaultFunctions); +} + +static redisContext *redisContextInit(void) { + redisContext *c; + + c = calloc(1,sizeof(redisContext)); + if (c == NULL) + return NULL; + + c->obuf = sdsempty(); + c->reader = redisReaderCreate(); + + if (c->obuf == NULL || c->reader == NULL) { + redisFree(c); + return NULL; + } + + return c; +} + +void redisFree(redisContext *c) { + if (c == NULL) + return; + if (c->fd > 0) + close(c->fd); + sdsfree(c->obuf); + redisReaderFree(c->reader); + free(c->tcp.host); + free(c->tcp.source_addr); + free(c->unix_sock.path); + free(c->timeout); + free(c); +} + +int redisFreeKeepFd(redisContext *c) { + int fd = c->fd; + c->fd = -1; + redisFree(c); + return fd; +} + +int redisReconnect(redisContext *c) { + c->err = 0; + memset(c->errstr, '\0', strlen(c->errstr)); + + if (c->fd > 0) { + close(c->fd); + } + + sdsfree(c->obuf); + redisReaderFree(c->reader); + + c->obuf = sdsempty(); + c->reader = redisReaderCreate(); + + if (c->connection_type == REDIS_CONN_TCP) { + return redisContextConnectBindTcp(c, c->tcp.host, c->tcp.port, + c->timeout, c->tcp.source_addr); + } else if (c->connection_type == REDIS_CONN_UNIX) { + return redisContextConnectUnix(c, c->unix_sock.path, c->timeout); + } else { + /* Something bad happened here and shouldn't have. There isn't + enough information in the context to reconnect. */ + __redisSetError(c,REDIS_ERR_OTHER,"Not enough information to reconnect"); + } + + return REDIS_ERR; +} + +/* Connect to a Redis instance. On error the field error in the returned + * context will be set to the return value of the error function. + * When no set of reply functions is given, the default set will be used. */ +redisContext *redisConnect(const char *ip, int port) { + redisContext *c; + + c = redisContextInit(); + if (c == NULL) + return NULL; + + c->flags |= REDIS_BLOCK; + redisContextConnectTcp(c,ip,port,NULL); + return c; +} + +redisContext *redisConnectWithTimeout(const char *ip, int port, const struct timeval tv) { + redisContext *c; + + c = redisContextInit(); + if (c == NULL) + return NULL; + + c->flags |= REDIS_BLOCK; + redisContextConnectTcp(c,ip,port,&tv); + return c; +} + +redisContext *redisConnectNonBlock(const char *ip, int port) { + redisContext *c; + + c = redisContextInit(); + if (c == NULL) + return NULL; + + c->flags &= ~REDIS_BLOCK; + redisContextConnectTcp(c,ip,port,NULL); + return c; +} + +redisContext *redisConnectBindNonBlock(const char *ip, int port, + const char *source_addr) { + redisContext *c = redisContextInit(); + if (c == NULL) + return NULL; + c->flags &= ~REDIS_BLOCK; + redisContextConnectBindTcp(c,ip,port,NULL,source_addr); + return c; +} + +redisContext *redisConnectBindNonBlockWithReuse(const char *ip, int port, + const char *source_addr) { + redisContext *c = redisContextInit(); + if (c == NULL) + return NULL; + c->flags &= ~REDIS_BLOCK; + c->flags |= REDIS_REUSEADDR; + redisContextConnectBindTcp(c,ip,port,NULL,source_addr); + return c; +} + +redisContext *redisConnectUnix(const char *path) { + redisContext *c; + + c = redisContextInit(); + if (c == NULL) + return NULL; + + c->flags |= REDIS_BLOCK; + redisContextConnectUnix(c,path,NULL); + return c; +} + +redisContext *redisConnectUnixWithTimeout(const char *path, const struct timeval tv) { + redisContext *c; + + c = redisContextInit(); + if (c == NULL) + return NULL; + + c->flags |= REDIS_BLOCK; + redisContextConnectUnix(c,path,&tv); + return c; +} + +redisContext *redisConnectUnixNonBlock(const char *path) { + redisContext *c; + + c = redisContextInit(); + if (c == NULL) + return NULL; + + c->flags &= ~REDIS_BLOCK; + redisContextConnectUnix(c,path,NULL); + return c; +} + +redisContext *redisConnectFd(int fd) { + redisContext *c; + + c = redisContextInit(); + if (c == NULL) + return NULL; + + c->fd = fd; + c->flags |= REDIS_BLOCK | REDIS_CONNECTED; + return c; +} + +/* Set read/write timeout on a blocking socket. */ +int redisSetTimeout(redisContext *c, const struct timeval tv) { + if (c->flags & REDIS_BLOCK) + return redisContextSetTimeout(c,tv); + return REDIS_ERR; +} + +/* Enable connection KeepAlive. */ +int redisEnableKeepAlive(redisContext *c) { + if (redisKeepAlive(c, REDIS_KEEPALIVE_INTERVAL) != REDIS_OK) + return REDIS_ERR; + return REDIS_OK; +} + +/* Use this function to handle a read event on the descriptor. It will try + * and read some bytes from the socket and feed them to the reply parser. + * + * After this function is called, you may use redisContextReadReply to + * see if there is a reply available. */ +int redisBufferRead(redisContext *c) { + char buf[1024*16]; + int nread; + + /* Return early when the context has seen an error. */ + if (c->err) + return REDIS_ERR; + + nread = read(c->fd,buf,sizeof(buf)); + if (nread == -1) { + if ((errno == EAGAIN && !(c->flags & REDIS_BLOCK)) || (errno == EINTR)) { + /* Try again later */ + } else { + __redisSetError(c,REDIS_ERR_IO,NULL); + return REDIS_ERR; + } + } else if (nread == 0) { + __redisSetError(c,REDIS_ERR_EOF,"Server closed the connection"); + return REDIS_ERR; + } else { + if (redisReaderFeed(c->reader,buf,nread) != REDIS_OK) { + __redisSetError(c,c->reader->err,c->reader->errstr); + return REDIS_ERR; + } + } + return REDIS_OK; +} + +/* Write the output buffer to the socket. + * + * Returns REDIS_OK when the buffer is empty, or (a part of) the buffer was + * successfully written to the socket. When the buffer is empty after the + * write operation, "done" is set to 1 (if given). + * + * Returns REDIS_ERR if an error occurred trying to write and sets + * c->errstr to hold the appropriate error string. + */ +int redisBufferWrite(redisContext *c, int *done) { + int nwritten; + + /* Return early when the context has seen an error. */ + if (c->err) + return REDIS_ERR; + + if (sdslen(c->obuf) > 0) { + nwritten = write(c->fd,c->obuf,sdslen(c->obuf)); + if (nwritten == -1) { + if ((errno == EAGAIN && !(c->flags & REDIS_BLOCK)) || (errno == EINTR)) { + /* Try again later */ + } else { + __redisSetError(c,REDIS_ERR_IO,NULL); + return REDIS_ERR; + } + } else if (nwritten > 0) { + if (nwritten == (signed)sdslen(c->obuf)) { + sdsfree(c->obuf); + c->obuf = sdsempty(); + } else { + sdsrange(c->obuf,nwritten,-1); + } + } + } + if (done != NULL) *done = (sdslen(c->obuf) == 0); + return REDIS_OK; +} + +/* Internal helper function to try and get a reply from the reader, + * or set an error in the context otherwise. */ +int redisGetReplyFromReader(redisContext *c, void **reply) { + if (redisReaderGetReply(c->reader,reply) == REDIS_ERR) { + __redisSetError(c,c->reader->err,c->reader->errstr); + return REDIS_ERR; + } + return REDIS_OK; +} + +int redisGetReply(redisContext *c, void **reply) { + int wdone = 0; + void *aux = NULL; + + /* Try to read pending replies */ + if (redisGetReplyFromReader(c,&aux) == REDIS_ERR) + return REDIS_ERR; + + /* For the blocking context, flush output buffer and read reply */ + if (aux == NULL && c->flags & REDIS_BLOCK) { + /* Write until done */ + do { + if (redisBufferWrite(c,&wdone) == REDIS_ERR) + return REDIS_ERR; + } while (!wdone); + + /* Read until there is a reply */ + do { + if (redisBufferRead(c) == REDIS_ERR) + return REDIS_ERR; + if (redisGetReplyFromReader(c,&aux) == REDIS_ERR) + return REDIS_ERR; + } while (aux == NULL); + } + + /* Set reply object */ + if (reply != NULL) *reply = aux; + return REDIS_OK; +} + + +/* Helper function for the redisAppendCommand* family of functions. + * + * Write a formatted command to the output buffer. When this family + * is used, you need to call redisGetReply yourself to retrieve + * the reply (or replies in pub/sub). + */ +int __redisAppendCommand(redisContext *c, const char *cmd, size_t len) { + sds newbuf; + + newbuf = sdscatlen(c->obuf,cmd,len); + if (newbuf == NULL) { + __redisSetError(c,REDIS_ERR_OOM,"Out of memory"); + return REDIS_ERR; + } + + c->obuf = newbuf; + return REDIS_OK; +} + +int redisAppendFormattedCommand(redisContext *c, const char *cmd, size_t len) { + + if (__redisAppendCommand(c, cmd, len) != REDIS_OK) { + return REDIS_ERR; + } + + return REDIS_OK; +} + +int redisvAppendCommand(redisContext *c, const char *format, va_list ap) { + char *cmd; + int len; + + len = redisvFormatCommand(&cmd,format,ap); + if (len == -1) { + __redisSetError(c,REDIS_ERR_OOM,"Out of memory"); + return REDIS_ERR; + } else if (len == -2) { + __redisSetError(c,REDIS_ERR_OTHER,"Invalid format string"); + return REDIS_ERR; + } + + if (__redisAppendCommand(c,cmd,len) != REDIS_OK) { + free(cmd); + return REDIS_ERR; + } + + free(cmd); + return REDIS_OK; +} + +int redisAppendCommand(redisContext *c, const char *format, ...) { + va_list ap; + int ret; + + va_start(ap,format); + ret = redisvAppendCommand(c,format,ap); + va_end(ap); + return ret; +} + +int redisAppendCommandArgv(redisContext *c, int argc, const char **argv, const size_t *argvlen) { + sds cmd; + int len; + + len = redisFormatSdsCommandArgv(&cmd,argc,argv,argvlen); + if (len == -1) { + __redisSetError(c,REDIS_ERR_OOM,"Out of memory"); + return REDIS_ERR; + } + + if (__redisAppendCommand(c,cmd,len) != REDIS_OK) { + sdsfree(cmd); + return REDIS_ERR; + } + + sdsfree(cmd); + return REDIS_OK; +} + +/* Helper function for the redisCommand* family of functions. + * + * Write a formatted command to the output buffer. If the given context is + * blocking, immediately read the reply into the "reply" pointer. When the + * context is non-blocking, the "reply" pointer will not be used and the + * command is simply appended to the write buffer. + * + * Returns the reply when a reply was successfully retrieved. Returns NULL + * otherwise. When NULL is returned in a blocking context, the error field + * in the context will be set. + */ +static void *__redisBlockForReply(redisContext *c) { + void *reply; + + if (c->flags & REDIS_BLOCK) { + if (redisGetReply(c,&reply) != REDIS_OK) + return NULL; + return reply; + } + return NULL; +} + +void *redisvCommand(redisContext *c, const char *format, va_list ap) { + if (redisvAppendCommand(c,format,ap) != REDIS_OK) + return NULL; + return __redisBlockForReply(c); +} + +void *redisCommand(redisContext *c, const char *format, ...) { + va_list ap; + va_start(ap,format); + void *reply = redisvCommand(c,format,ap); + va_end(ap); + return reply; +} + +void *redisCommandArgv(redisContext *c, int argc, const char **argv, const size_t *argvlen) { + if (redisAppendCommandArgv(c,argc,argv,argvlen) != REDIS_OK) + return NULL; + return __redisBlockForReply(c); +} diff --git a/controller/thirdparty/hiredis-0.14.1/hiredis.h b/controller/thirdparty/hiredis-0.14.1/hiredis.h new file mode 100644 index 000000000..d945bf204 --- /dev/null +++ b/controller/thirdparty/hiredis-0.14.1/hiredis.h @@ -0,0 +1,200 @@ +/* + * Copyright (c) 2009-2011, Salvatore Sanfilippo + * Copyright (c) 2010-2014, Pieter Noordhuis + * Copyright (c) 2015, Matt Stancliff , + * Jan-Erik Rediger + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * * Neither the name of Redis nor the names of its contributors may be used + * to endorse or promote products derived from this software without + * specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +#ifndef __HIREDIS_H +#define __HIREDIS_H +#include "read.h" +#include /* for va_list */ +#include /* for struct timeval */ +#include /* uintXX_t, etc */ +#include "sds.h" /* for sds */ +#include "alloc.h" /* for allocation wrappers */ + +#define HIREDIS_MAJOR 0 +#define HIREDIS_MINOR 14 +#define HIREDIS_PATCH 1 +#define HIREDIS_SONAME 0.14 + +/* Connection type can be blocking or non-blocking and is set in the + * least significant bit of the flags field in redisContext. */ +#define REDIS_BLOCK 0x1 + +/* Connection may be disconnected before being free'd. The second bit + * in the flags field is set when the context is connected. */ +#define REDIS_CONNECTED 0x2 + +/* The async API might try to disconnect cleanly and flush the output + * buffer and read all subsequent replies before disconnecting. + * This flag means no new commands can come in and the connection + * should be terminated once all replies have been read. */ +#define REDIS_DISCONNECTING 0x4 + +/* Flag specific to the async API which means that the context should be clean + * up as soon as possible. */ +#define REDIS_FREEING 0x8 + +/* Flag that is set when an async callback is executed. */ +#define REDIS_IN_CALLBACK 0x10 + +/* Flag that is set when the async context has one or more subscriptions. */ +#define REDIS_SUBSCRIBED 0x20 + +/* Flag that is set when monitor mode is active */ +#define REDIS_MONITORING 0x40 + +/* Flag that is set when we should set SO_REUSEADDR before calling bind() */ +#define REDIS_REUSEADDR 0x80 + +#define REDIS_KEEPALIVE_INTERVAL 15 /* seconds */ + +/* number of times we retry to connect in the case of EADDRNOTAVAIL and + * SO_REUSEADDR is being used. */ +#define REDIS_CONNECT_RETRIES 10 + +#ifdef __cplusplus +extern "C" { +#endif + +/* This is the reply object returned by redisCommand() */ +typedef struct redisReply { + int type; /* REDIS_REPLY_* */ + long long integer; /* The integer when type is REDIS_REPLY_INTEGER */ + size_t len; /* Length of string */ + char *str; /* Used for both REDIS_REPLY_ERROR and REDIS_REPLY_STRING */ + size_t elements; /* number of elements, for REDIS_REPLY_ARRAY */ + struct redisReply **element; /* elements vector for REDIS_REPLY_ARRAY */ +} redisReply; + +redisReader *redisReaderCreate(void); + +/* Function to free the reply objects hiredis returns by default. */ +void freeReplyObject(void *reply); + +/* Functions to format a command according to the protocol. */ +int redisvFormatCommand(char **target, const char *format, va_list ap); +int redisFormatCommand(char **target, const char *format, ...); +int redisFormatCommandArgv(char **target, int argc, const char **argv, const size_t *argvlen); +int redisFormatSdsCommandArgv(sds *target, int argc, const char ** argv, const size_t *argvlen); +void redisFreeCommand(char *cmd); +void redisFreeSdsCommand(sds cmd); + +enum redisConnectionType { + REDIS_CONN_TCP, + REDIS_CONN_UNIX +}; + +/* Context for a connection to Redis */ +typedef struct redisContext { + int err; /* Error flags, 0 when there is no error */ + char errstr[128]; /* String representation of error when applicable */ + int fd; + int flags; + char *obuf; /* Write buffer */ + redisReader *reader; /* Protocol reader */ + + enum redisConnectionType connection_type; + struct timeval *timeout; + + struct { + char *host; + char *source_addr; + int port; + } tcp; + + struct { + char *path; + } unix_sock; + +} redisContext; + +redisContext *redisConnect(const char *ip, int port); +redisContext *redisConnectWithTimeout(const char *ip, int port, const struct timeval tv); +redisContext *redisConnectNonBlock(const char *ip, int port); +redisContext *redisConnectBindNonBlock(const char *ip, int port, + const char *source_addr); +redisContext *redisConnectBindNonBlockWithReuse(const char *ip, int port, + const char *source_addr); +redisContext *redisConnectUnix(const char *path); +redisContext *redisConnectUnixWithTimeout(const char *path, const struct timeval tv); +redisContext *redisConnectUnixNonBlock(const char *path); +redisContext *redisConnectFd(int fd); + +/** + * Reconnect the given context using the saved information. + * + * This re-uses the exact same connect options as in the initial connection. + * host, ip (or path), timeout and bind address are reused, + * flags are used unmodified from the existing context. + * + * Returns REDIS_OK on successful connect or REDIS_ERR otherwise. + */ +int redisReconnect(redisContext *c); + +int redisSetTimeout(redisContext *c, const struct timeval tv); +int redisEnableKeepAlive(redisContext *c); +void redisFree(redisContext *c); +int redisFreeKeepFd(redisContext *c); +int redisBufferRead(redisContext *c); +int redisBufferWrite(redisContext *c, int *done); + +/* In a blocking context, this function first checks if there are unconsumed + * replies to return and returns one if so. Otherwise, it flushes the output + * buffer to the socket and reads until it has a reply. In a non-blocking + * context, it will return unconsumed replies until there are no more. */ +int redisGetReply(redisContext *c, void **reply); +int redisGetReplyFromReader(redisContext *c, void **reply); + +/* Write a formatted command to the output buffer. Use these functions in blocking mode + * to get a pipeline of commands. */ +int redisAppendFormattedCommand(redisContext *c, const char *cmd, size_t len); + +/* Write a command to the output buffer. Use these functions in blocking mode + * to get a pipeline of commands. */ +int redisvAppendCommand(redisContext *c, const char *format, va_list ap); +int redisAppendCommand(redisContext *c, const char *format, ...); +int redisAppendCommandArgv(redisContext *c, int argc, const char **argv, const size_t *argvlen); + +/* Issue a command to Redis. In a blocking context, it is identical to calling + * redisAppendCommand, followed by redisGetReply. The function will return + * NULL if there was an error in performing the request, otherwise it will + * return the reply. In a non-blocking context, it is identical to calling + * only redisAppendCommand and will always return NULL. */ +void *redisvCommand(redisContext *c, const char *format, va_list ap); +void *redisCommand(redisContext *c, const char *format, ...); +void *redisCommandArgv(redisContext *c, int argc, const char **argv, const size_t *argvlen); + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/controller/thirdparty/hiredis-0.14.1/net.c b/controller/thirdparty/hiredis-0.14.1/net.c new file mode 100644 index 000000000..d71bbcd57 --- /dev/null +++ b/controller/thirdparty/hiredis-0.14.1/net.c @@ -0,0 +1,477 @@ +/* Extracted from anet.c to work properly with Hiredis error reporting. + * + * Copyright (c) 2009-2011, Salvatore Sanfilippo + * Copyright (c) 2010-2014, Pieter Noordhuis + * Copyright (c) 2015, Matt Stancliff , + * Jan-Erik Rediger + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * * Neither the name of Redis nor the names of its contributors may be used + * to endorse or promote products derived from this software without + * specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +#include "fmacros.h" +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "net.h" +#include "sds.h" + +/* Defined in hiredis.c */ +void __redisSetError(redisContext *c, int type, const char *str); + +static void redisContextCloseFd(redisContext *c) { + if (c && c->fd >= 0) { + close(c->fd); + c->fd = -1; + } +} + +static void __redisSetErrorFromErrno(redisContext *c, int type, const char *prefix) { + int errorno = errno; /* snprintf() may change errno */ + char buf[128] = { 0 }; + size_t len = 0; + + if (prefix != NULL) + len = snprintf(buf,sizeof(buf),"%s: ",prefix); + strerror_r(errorno, (char *)(buf + len), sizeof(buf) - len); + __redisSetError(c,type,buf); +} + +static int redisSetReuseAddr(redisContext *c) { + int on = 1; + if (setsockopt(c->fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)) == -1) { + __redisSetErrorFromErrno(c,REDIS_ERR_IO,NULL); + redisContextCloseFd(c); + return REDIS_ERR; + } + return REDIS_OK; +} + +static int redisCreateSocket(redisContext *c, int type) { + int s; + if ((s = socket(type, SOCK_STREAM, 0)) == -1) { + __redisSetErrorFromErrno(c,REDIS_ERR_IO,NULL); + return REDIS_ERR; + } + c->fd = s; + if (type == AF_INET) { + if (redisSetReuseAddr(c) == REDIS_ERR) { + return REDIS_ERR; + } + } + return REDIS_OK; +} + +static int redisSetBlocking(redisContext *c, int blocking) { + int flags; + + /* Set the socket nonblocking. + * Note that fcntl(2) for F_GETFL and F_SETFL can't be + * interrupted by a signal. */ + if ((flags = fcntl(c->fd, F_GETFL)) == -1) { + __redisSetErrorFromErrno(c,REDIS_ERR_IO,"fcntl(F_GETFL)"); + redisContextCloseFd(c); + return REDIS_ERR; + } + + if (blocking) + flags &= ~O_NONBLOCK; + else + flags |= O_NONBLOCK; + + if (fcntl(c->fd, F_SETFL, flags) == -1) { + __redisSetErrorFromErrno(c,REDIS_ERR_IO,"fcntl(F_SETFL)"); + redisContextCloseFd(c); + return REDIS_ERR; + } + return REDIS_OK; +} + +int redisKeepAlive(redisContext *c, int interval) { + int val = 1; + int fd = c->fd; + + if (setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE, &val, sizeof(val)) == -1){ + __redisSetError(c,REDIS_ERR_OTHER,strerror(errno)); + return REDIS_ERR; + } + + val = interval; + +#if defined(__APPLE__) && defined(__MACH__) + if (setsockopt(fd, IPPROTO_TCP, TCP_KEEPALIVE, &val, sizeof(val)) < 0) { + __redisSetError(c,REDIS_ERR_OTHER,strerror(errno)); + return REDIS_ERR; + } +#else +#if defined(__GLIBC__) && !defined(__FreeBSD_kernel__) + if (setsockopt(fd, IPPROTO_TCP, TCP_KEEPIDLE, &val, sizeof(val)) < 0) { + __redisSetError(c,REDIS_ERR_OTHER,strerror(errno)); + return REDIS_ERR; + } + + val = interval/3; + if (val == 0) val = 1; + if (setsockopt(fd, IPPROTO_TCP, TCP_KEEPINTVL, &val, sizeof(val)) < 0) { + __redisSetError(c,REDIS_ERR_OTHER,strerror(errno)); + return REDIS_ERR; + } + + val = 3; + if (setsockopt(fd, IPPROTO_TCP, TCP_KEEPCNT, &val, sizeof(val)) < 0) { + __redisSetError(c,REDIS_ERR_OTHER,strerror(errno)); + return REDIS_ERR; + } +#endif +#endif + + return REDIS_OK; +} + +static int redisSetTcpNoDelay(redisContext *c) { + int yes = 1; + if (setsockopt(c->fd, IPPROTO_TCP, TCP_NODELAY, &yes, sizeof(yes)) == -1) { + __redisSetErrorFromErrno(c,REDIS_ERR_IO,"setsockopt(TCP_NODELAY)"); + redisContextCloseFd(c); + return REDIS_ERR; + } + return REDIS_OK; +} + +#define __MAX_MSEC (((LONG_MAX) - 999) / 1000) + +static int redisContextTimeoutMsec(redisContext *c, long *result) +{ + const struct timeval *timeout = c->timeout; + long msec = -1; + + /* Only use timeout when not NULL. */ + if (timeout != NULL) { + if (timeout->tv_usec > 1000000 || timeout->tv_sec > __MAX_MSEC) { + *result = msec; + return REDIS_ERR; + } + + msec = (timeout->tv_sec * 1000) + ((timeout->tv_usec + 999) / 1000); + + if (msec < 0 || msec > INT_MAX) { + msec = INT_MAX; + } + } + + *result = msec; + return REDIS_OK; +} + +static int redisContextWaitReady(redisContext *c, long msec) { + struct pollfd wfd[1]; + + wfd[0].fd = c->fd; + wfd[0].events = POLLOUT; + + if (errno == EINPROGRESS) { + int res; + + if ((res = poll(wfd, 1, msec)) == -1) { + __redisSetErrorFromErrno(c, REDIS_ERR_IO, "poll(2)"); + redisContextCloseFd(c); + return REDIS_ERR; + } else if (res == 0) { + errno = ETIMEDOUT; + __redisSetErrorFromErrno(c,REDIS_ERR_IO,NULL); + redisContextCloseFd(c); + return REDIS_ERR; + } + + if (redisCheckSocketError(c) != REDIS_OK) + return REDIS_ERR; + + return REDIS_OK; + } + + __redisSetErrorFromErrno(c,REDIS_ERR_IO,NULL); + redisContextCloseFd(c); + return REDIS_ERR; +} + +int redisCheckSocketError(redisContext *c) { + int err = 0; + socklen_t errlen = sizeof(err); + + if (getsockopt(c->fd, SOL_SOCKET, SO_ERROR, &err, &errlen) == -1) { + __redisSetErrorFromErrno(c,REDIS_ERR_IO,"getsockopt(SO_ERROR)"); + return REDIS_ERR; + } + + if (err) { + errno = err; + __redisSetErrorFromErrno(c,REDIS_ERR_IO,NULL); + return REDIS_ERR; + } + + return REDIS_OK; +} + +int redisContextSetTimeout(redisContext *c, const struct timeval tv) { + if (setsockopt(c->fd,SOL_SOCKET,SO_RCVTIMEO,&tv,sizeof(tv)) == -1) { + __redisSetErrorFromErrno(c,REDIS_ERR_IO,"setsockopt(SO_RCVTIMEO)"); + return REDIS_ERR; + } + if (setsockopt(c->fd,SOL_SOCKET,SO_SNDTIMEO,&tv,sizeof(tv)) == -1) { + __redisSetErrorFromErrno(c,REDIS_ERR_IO,"setsockopt(SO_SNDTIMEO)"); + return REDIS_ERR; + } + return REDIS_OK; +} + +static int _redisContextConnectTcp(redisContext *c, const char *addr, int port, + const struct timeval *timeout, + const char *source_addr) { + int s, rv, n; + char _port[6]; /* strlen("65535"); */ + struct addrinfo hints, *servinfo, *bservinfo, *p, *b; + int blocking = (c->flags & REDIS_BLOCK); + int reuseaddr = (c->flags & REDIS_REUSEADDR); + int reuses = 0; + long timeout_msec = -1; + + servinfo = NULL; + c->connection_type = REDIS_CONN_TCP; + c->tcp.port = port; + + /* We need to take possession of the passed parameters + * to make them reusable for a reconnect. + * We also carefully check we don't free data we already own, + * as in the case of the reconnect method. + * + * This is a bit ugly, but atleast it works and doesn't leak memory. + **/ + if (c->tcp.host != addr) { + free(c->tcp.host); + + c->tcp.host = hi_strdup(addr); + } + + if (timeout) { + if (c->timeout != timeout) { + if (c->timeout == NULL) + c->timeout = hi_malloc(sizeof(struct timeval)); + + memcpy(c->timeout, timeout, sizeof(struct timeval)); + } + } else { + free(c->timeout); + c->timeout = NULL; + } + + if (redisContextTimeoutMsec(c, &timeout_msec) != REDIS_OK) { + __redisSetError(c, REDIS_ERR_IO, "Invalid timeout specified"); + goto error; + } + + if (source_addr == NULL) { + free(c->tcp.source_addr); + c->tcp.source_addr = NULL; + } else if (c->tcp.source_addr != source_addr) { + free(c->tcp.source_addr); + c->tcp.source_addr = hi_strdup(source_addr); + } + + snprintf(_port, 6, "%d", port); + memset(&hints,0,sizeof(hints)); + hints.ai_family = AF_INET; + hints.ai_socktype = SOCK_STREAM; + + /* Try with IPv6 if no IPv4 address was found. We do it in this order since + * in a Redis client you can't afford to test if you have IPv6 connectivity + * as this would add latency to every connect. Otherwise a more sensible + * route could be: Use IPv6 if both addresses are available and there is IPv6 + * connectivity. */ + if ((rv = getaddrinfo(c->tcp.host,_port,&hints,&servinfo)) != 0) { + hints.ai_family = AF_INET6; + if ((rv = getaddrinfo(addr,_port,&hints,&servinfo)) != 0) { + __redisSetError(c,REDIS_ERR_OTHER,gai_strerror(rv)); + return REDIS_ERR; + } + } + for (p = servinfo; p != NULL; p = p->ai_next) { +addrretry: + if ((s = socket(p->ai_family,p->ai_socktype,p->ai_protocol)) == -1) + continue; + + c->fd = s; + if (redisSetBlocking(c,0) != REDIS_OK) + goto error; + if (c->tcp.source_addr) { + int bound = 0; + /* Using getaddrinfo saves us from self-determining IPv4 vs IPv6 */ + if ((rv = getaddrinfo(c->tcp.source_addr, NULL, &hints, &bservinfo)) != 0) { + char buf[128]; + snprintf(buf,sizeof(buf),"Can't get addr: %s",gai_strerror(rv)); + __redisSetError(c,REDIS_ERR_OTHER,buf); + goto error; + } + + if (reuseaddr) { + n = 1; + if (setsockopt(s, SOL_SOCKET, SO_REUSEADDR, (char*) &n, + sizeof(n)) < 0) { + freeaddrinfo(bservinfo); + goto error; + } + } + + for (b = bservinfo; b != NULL; b = b->ai_next) { + if (bind(s,b->ai_addr,b->ai_addrlen) != -1) { + bound = 1; + break; + } + } + freeaddrinfo(bservinfo); + if (!bound) { + char buf[128]; + snprintf(buf,sizeof(buf),"Can't bind socket: %s",strerror(errno)); + __redisSetError(c,REDIS_ERR_OTHER,buf); + goto error; + } + } + if (connect(s,p->ai_addr,p->ai_addrlen) == -1) { + if (errno == EHOSTUNREACH) { + redisContextCloseFd(c); + continue; + } else if (errno == EINPROGRESS && !blocking) { + /* This is ok. */ + } else if (errno == EADDRNOTAVAIL && reuseaddr) { + if (++reuses >= REDIS_CONNECT_RETRIES) { + goto error; + } else { + redisContextCloseFd(c); + goto addrretry; + } + } else { + if (redisContextWaitReady(c,timeout_msec) != REDIS_OK) + goto error; + } + } + if (blocking && redisSetBlocking(c,1) != REDIS_OK) + goto error; + if (redisSetTcpNoDelay(c) != REDIS_OK) + goto error; + + c->flags |= REDIS_CONNECTED; + rv = REDIS_OK; + goto end; + } + if (p == NULL) { + char buf[128]; + snprintf(buf,sizeof(buf),"Can't create socket: %s",strerror(errno)); + __redisSetError(c,REDIS_ERR_OTHER,buf); + goto error; + } + +error: + rv = REDIS_ERR; +end: + if(servinfo) { + freeaddrinfo(servinfo); + } + + return rv; // Need to return REDIS_OK if alright +} + +int redisContextConnectTcp(redisContext *c, const char *addr, int port, + const struct timeval *timeout) { + return _redisContextConnectTcp(c, addr, port, timeout, NULL); +} + +int redisContextConnectBindTcp(redisContext *c, const char *addr, int port, + const struct timeval *timeout, + const char *source_addr) { + return _redisContextConnectTcp(c, addr, port, timeout, source_addr); +} + +int redisContextConnectUnix(redisContext *c, const char *path, const struct timeval *timeout) { + int blocking = (c->flags & REDIS_BLOCK); + struct sockaddr_un sa; + long timeout_msec = -1; + + if (redisCreateSocket(c,AF_UNIX) < 0) + return REDIS_ERR; + if (redisSetBlocking(c,0) != REDIS_OK) + return REDIS_ERR; + + c->connection_type = REDIS_CONN_UNIX; + if (c->unix_sock.path != path) + c->unix_sock.path = hi_strdup(path); + + if (timeout) { + if (c->timeout != timeout) { + if (c->timeout == NULL) + c->timeout = hi_malloc(sizeof(struct timeval)); + + memcpy(c->timeout, timeout, sizeof(struct timeval)); + } + } else { + free(c->timeout); + c->timeout = NULL; + } + + if (redisContextTimeoutMsec(c,&timeout_msec) != REDIS_OK) + return REDIS_ERR; + + sa.sun_family = AF_UNIX; + strncpy(sa.sun_path,path,sizeof(sa.sun_path)-1); + if (connect(c->fd, (struct sockaddr*)&sa, sizeof(sa)) == -1) { + if (errno == EINPROGRESS && !blocking) { + /* This is ok. */ + } else { + if (redisContextWaitReady(c,timeout_msec) != REDIS_OK) + return REDIS_ERR; + } + } + + /* Reset socket to be blocking after connect(2). */ + if (blocking && redisSetBlocking(c,1) != REDIS_OK) + return REDIS_ERR; + + c->flags |= REDIS_CONNECTED; + return REDIS_OK; +} diff --git a/controller/thirdparty/hiredis-0.14.1/net.h b/controller/thirdparty/hiredis-0.14.1/net.h new file mode 100644 index 000000000..d9dc36257 --- /dev/null +++ b/controller/thirdparty/hiredis-0.14.1/net.h @@ -0,0 +1,49 @@ +/* Extracted from anet.c to work properly with Hiredis error reporting. + * + * Copyright (c) 2009-2011, Salvatore Sanfilippo + * Copyright (c) 2010-2014, Pieter Noordhuis + * Copyright (c) 2015, Matt Stancliff , + * Jan-Erik Rediger + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * * Neither the name of Redis nor the names of its contributors may be used + * to endorse or promote products derived from this software without + * specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +#ifndef __NET_H +#define __NET_H + +#include "hiredis.h" + +int redisCheckSocketError(redisContext *c); +int redisContextSetTimeout(redisContext *c, const struct timeval tv); +int redisContextConnectTcp(redisContext *c, const char *addr, int port, const struct timeval *timeout); +int redisContextConnectBindTcp(redisContext *c, const char *addr, int port, + const struct timeval *timeout, + const char *source_addr); +int redisContextConnectUnix(redisContext *c, const char *path, const struct timeval *timeout); +int redisKeepAlive(redisContext *c, int interval); + +#endif diff --git a/controller/thirdparty/hiredis-0.14.1/read.c b/controller/thirdparty/hiredis-0.14.1/read.c new file mode 100644 index 000000000..cc2126778 --- /dev/null +++ b/controller/thirdparty/hiredis-0.14.1/read.c @@ -0,0 +1,598 @@ +/* + * Copyright (c) 2009-2011, Salvatore Sanfilippo + * Copyright (c) 2010-2011, Pieter Noordhuis + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * * Neither the name of Redis nor the names of its contributors may be used + * to endorse or promote products derived from this software without + * specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + + +#include "fmacros.h" +#include +#include +#ifndef _MSC_VER +#include +#endif +#include +#include +#include +#include + +#include "read.h" +#include "sds.h" + +static void __redisReaderSetError(redisReader *r, int type, const char *str) { + size_t len; + + if (r->reply != NULL && r->fn && r->fn->freeObject) { + r->fn->freeObject(r->reply); + r->reply = NULL; + } + + /* Clear input buffer on errors. */ + sdsfree(r->buf); + r->buf = NULL; + r->pos = r->len = 0; + + /* Reset task stack. */ + r->ridx = -1; + + /* Set error. */ + r->err = type; + len = strlen(str); + len = len < (sizeof(r->errstr)-1) ? len : (sizeof(r->errstr)-1); + memcpy(r->errstr,str,len); + r->errstr[len] = '\0'; +} + +static size_t chrtos(char *buf, size_t size, char byte) { + size_t len = 0; + + switch(byte) { + case '\\': + case '"': + len = snprintf(buf,size,"\"\\%c\"",byte); + break; + case '\n': len = snprintf(buf,size,"\"\\n\""); break; + case '\r': len = snprintf(buf,size,"\"\\r\""); break; + case '\t': len = snprintf(buf,size,"\"\\t\""); break; + case '\a': len = snprintf(buf,size,"\"\\a\""); break; + case '\b': len = snprintf(buf,size,"\"\\b\""); break; + default: + if (isprint(byte)) + len = snprintf(buf,size,"\"%c\"",byte); + else + len = snprintf(buf,size,"\"\\x%02x\"",(unsigned char)byte); + break; + } + + return len; +} + +static void __redisReaderSetErrorProtocolByte(redisReader *r, char byte) { + char cbuf[8], sbuf[128]; + + chrtos(cbuf,sizeof(cbuf),byte); + snprintf(sbuf,sizeof(sbuf), + "Protocol error, got %s as reply type byte", cbuf); + __redisReaderSetError(r,REDIS_ERR_PROTOCOL,sbuf); +} + +static void __redisReaderSetErrorOOM(redisReader *r) { + __redisReaderSetError(r,REDIS_ERR_OOM,"Out of memory"); +} + +static char *readBytes(redisReader *r, unsigned int bytes) { + char *p; + if (r->len-r->pos >= bytes) { + p = r->buf+r->pos; + r->pos += bytes; + return p; + } + return NULL; +} + +/* Find pointer to \r\n. */ +static char *seekNewline(char *s, size_t len) { + int pos = 0; + int _len = len-1; + + /* Position should be < len-1 because the character at "pos" should be + * followed by a \n. Note that strchr cannot be used because it doesn't + * allow to search a limited length and the buffer that is being searched + * might not have a trailing NULL character. */ + while (pos < _len) { + while(pos < _len && s[pos] != '\r') pos++; + if (pos==_len) { + /* Not found. */ + return NULL; + } else { + if (s[pos+1] == '\n') { + /* Found. */ + return s+pos; + } else { + /* Continue searching. */ + pos++; + } + } + } + return NULL; +} + +/* Convert a string into a long long. Returns REDIS_OK if the string could be + * parsed into a (non-overflowing) long long, REDIS_ERR otherwise. The value + * will be set to the parsed value when appropriate. + * + * Note that this function demands that the string strictly represents + * a long long: no spaces or other characters before or after the string + * representing the number are accepted, nor zeroes at the start if not + * for the string "0" representing the zero number. + * + * Because of its strictness, it is safe to use this function to check if + * you can convert a string into a long long, and obtain back the string + * from the number without any loss in the string representation. */ +static int string2ll(const char *s, size_t slen, long long *value) { + const char *p = s; + size_t plen = 0; + int negative = 0; + unsigned long long v; + + if (plen == slen) + return REDIS_ERR; + + /* Special case: first and only digit is 0. */ + if (slen == 1 && p[0] == '0') { + if (value != NULL) *value = 0; + return REDIS_OK; + } + + if (p[0] == '-') { + negative = 1; + p++; plen++; + + /* Abort on only a negative sign. */ + if (plen == slen) + return REDIS_ERR; + } + + /* First digit should be 1-9, otherwise the string should just be 0. */ + if (p[0] >= '1' && p[0] <= '9') { + v = p[0]-'0'; + p++; plen++; + } else if (p[0] == '0' && slen == 1) { + *value = 0; + return REDIS_OK; + } else { + return REDIS_ERR; + } + + while (plen < slen && p[0] >= '0' && p[0] <= '9') { + if (v > (ULLONG_MAX / 10)) /* Overflow. */ + return REDIS_ERR; + v *= 10; + + if (v > (ULLONG_MAX - (p[0]-'0'))) /* Overflow. */ + return REDIS_ERR; + v += p[0]-'0'; + + p++; plen++; + } + + /* Return if not all bytes were used. */ + if (plen < slen) + return REDIS_ERR; + + if (negative) { + if (v > ((unsigned long long)(-(LLONG_MIN+1))+1)) /* Overflow. */ + return REDIS_ERR; + if (value != NULL) *value = -v; + } else { + if (v > LLONG_MAX) /* Overflow. */ + return REDIS_ERR; + if (value != NULL) *value = v; + } + return REDIS_OK; +} + +static char *readLine(redisReader *r, int *_len) { + char *p, *s; + int len; + + p = r->buf+r->pos; + s = seekNewline(p,(r->len-r->pos)); + if (s != NULL) { + len = s-(r->buf+r->pos); + r->pos += len+2; /* skip \r\n */ + if (_len) *_len = len; + return p; + } + return NULL; +} + +static void moveToNextTask(redisReader *r) { + redisReadTask *cur, *prv; + while (r->ridx >= 0) { + /* Return a.s.a.p. when the stack is now empty. */ + if (r->ridx == 0) { + r->ridx--; + return; + } + + cur = &(r->rstack[r->ridx]); + prv = &(r->rstack[r->ridx-1]); + assert(prv->type == REDIS_REPLY_ARRAY); + if (cur->idx == prv->elements-1) { + r->ridx--; + } else { + /* Reset the type because the next item can be anything */ + assert(cur->idx < prv->elements); + cur->type = -1; + cur->elements = -1; + cur->idx++; + return; + } + } +} + +static int processLineItem(redisReader *r) { + redisReadTask *cur = &(r->rstack[r->ridx]); + void *obj; + char *p; + int len; + + if ((p = readLine(r,&len)) != NULL) { + if (cur->type == REDIS_REPLY_INTEGER) { + if (r->fn && r->fn->createInteger) { + long long v; + if (string2ll(p, len, &v) == REDIS_ERR) { + __redisReaderSetError(r,REDIS_ERR_PROTOCOL, + "Bad integer value"); + return REDIS_ERR; + } + obj = r->fn->createInteger(cur,v); + } else { + obj = (void*)REDIS_REPLY_INTEGER; + } + } else { + /* Type will be error or status. */ + if (r->fn && r->fn->createString) + obj = r->fn->createString(cur,p,len); + else + obj = (void*)(size_t)(cur->type); + } + + if (obj == NULL) { + __redisReaderSetErrorOOM(r); + return REDIS_ERR; + } + + /* Set reply if this is the root object. */ + if (r->ridx == 0) r->reply = obj; + moveToNextTask(r); + return REDIS_OK; + } + + return REDIS_ERR; +} + +static int processBulkItem(redisReader *r) { + redisReadTask *cur = &(r->rstack[r->ridx]); + void *obj = NULL; + char *p, *s; + long long len; + unsigned long bytelen; + int success = 0; + + p = r->buf+r->pos; + s = seekNewline(p,r->len-r->pos); + if (s != NULL) { + p = r->buf+r->pos; + bytelen = s-(r->buf+r->pos)+2; /* include \r\n */ + + if (string2ll(p, bytelen - 2, &len) == REDIS_ERR) { + __redisReaderSetError(r,REDIS_ERR_PROTOCOL, + "Bad bulk string length"); + return REDIS_ERR; + } + + if (len < -1 || (LLONG_MAX > SIZE_MAX && len > (long long)SIZE_MAX)) { + __redisReaderSetError(r,REDIS_ERR_PROTOCOL, + "Bulk string length out of range"); + return REDIS_ERR; + } + + if (len == -1) { + /* The nil object can always be created. */ + if (r->fn && r->fn->createNil) + obj = r->fn->createNil(cur); + else + obj = (void*)REDIS_REPLY_NIL; + success = 1; + } else { + /* Only continue when the buffer contains the entire bulk item. */ + bytelen += len+2; /* include \r\n */ + if (r->pos+bytelen <= r->len) { + if (r->fn && r->fn->createString) + obj = r->fn->createString(cur,s+2,len); + else + obj = (void*)REDIS_REPLY_STRING; + success = 1; + } + } + + /* Proceed when obj was created. */ + if (success) { + if (obj == NULL) { + __redisReaderSetErrorOOM(r); + return REDIS_ERR; + } + + r->pos += bytelen; + + /* Set reply if this is the root object. */ + if (r->ridx == 0) r->reply = obj; + moveToNextTask(r); + return REDIS_OK; + } + } + + return REDIS_ERR; +} + +static int processMultiBulkItem(redisReader *r) { + redisReadTask *cur = &(r->rstack[r->ridx]); + void *obj; + char *p; + long long elements; + int root = 0, len; + + /* Set error for nested multi bulks with depth > 7 */ + if (r->ridx == 8) { + __redisReaderSetError(r,REDIS_ERR_PROTOCOL, + "No support for nested multi bulk replies with depth > 7"); + return REDIS_ERR; + } + + if ((p = readLine(r,&len)) != NULL) { + if (string2ll(p, len, &elements) == REDIS_ERR) { + __redisReaderSetError(r,REDIS_ERR_PROTOCOL, + "Bad multi-bulk length"); + return REDIS_ERR; + } + + root = (r->ridx == 0); + + if (elements < -1 || elements > INT_MAX) { + __redisReaderSetError(r,REDIS_ERR_PROTOCOL, + "Multi-bulk length out of range"); + return REDIS_ERR; + } + + if (elements == -1) { + if (r->fn && r->fn->createNil) + obj = r->fn->createNil(cur); + else + obj = (void*)REDIS_REPLY_NIL; + + if (obj == NULL) { + __redisReaderSetErrorOOM(r); + return REDIS_ERR; + } + + moveToNextTask(r); + } else { + if (r->fn && r->fn->createArray) + obj = r->fn->createArray(cur,elements); + else + obj = (void*)REDIS_REPLY_ARRAY; + + if (obj == NULL) { + __redisReaderSetErrorOOM(r); + return REDIS_ERR; + } + + /* Modify task stack when there are more than 0 elements. */ + if (elements > 0) { + cur->elements = elements; + cur->obj = obj; + r->ridx++; + r->rstack[r->ridx].type = -1; + r->rstack[r->ridx].elements = -1; + r->rstack[r->ridx].idx = 0; + r->rstack[r->ridx].obj = NULL; + r->rstack[r->ridx].parent = cur; + r->rstack[r->ridx].privdata = r->privdata; + } else { + moveToNextTask(r); + } + } + + /* Set reply if this is the root object. */ + if (root) r->reply = obj; + return REDIS_OK; + } + + return REDIS_ERR; +} + +static int processItem(redisReader *r) { + redisReadTask *cur = &(r->rstack[r->ridx]); + char *p; + + /* check if we need to read type */ + if (cur->type < 0) { + if ((p = readBytes(r,1)) != NULL) { + switch (p[0]) { + case '-': + cur->type = REDIS_REPLY_ERROR; + break; + case '+': + cur->type = REDIS_REPLY_STATUS; + break; + case ':': + cur->type = REDIS_REPLY_INTEGER; + break; + case '$': + cur->type = REDIS_REPLY_STRING; + break; + case '*': + cur->type = REDIS_REPLY_ARRAY; + break; + default: + __redisReaderSetErrorProtocolByte(r,*p); + return REDIS_ERR; + } + } else { + /* could not consume 1 byte */ + return REDIS_ERR; + } + } + + /* process typed item */ + switch(cur->type) { + case REDIS_REPLY_ERROR: + case REDIS_REPLY_STATUS: + case REDIS_REPLY_INTEGER: + return processLineItem(r); + case REDIS_REPLY_STRING: + return processBulkItem(r); + case REDIS_REPLY_ARRAY: + return processMultiBulkItem(r); + default: + assert(NULL); + return REDIS_ERR; /* Avoid warning. */ + } +} + +redisReader *redisReaderCreateWithFunctions(redisReplyObjectFunctions *fn) { + redisReader *r; + + r = calloc(1,sizeof(redisReader)); + if (r == NULL) + return NULL; + + r->fn = fn; + r->buf = sdsempty(); + r->maxbuf = REDIS_READER_MAX_BUF; + if (r->buf == NULL) { + free(r); + return NULL; + } + + r->ridx = -1; + return r; +} + +void redisReaderFree(redisReader *r) { + if (r == NULL) + return; + if (r->reply != NULL && r->fn && r->fn->freeObject) + r->fn->freeObject(r->reply); + sdsfree(r->buf); + free(r); +} + +int redisReaderFeed(redisReader *r, const char *buf, size_t len) { + sds newbuf; + + /* Return early when this reader is in an erroneous state. */ + if (r->err) + return REDIS_ERR; + + /* Copy the provided buffer. */ + if (buf != NULL && len >= 1) { + /* Destroy internal buffer when it is empty and is quite large. */ + if (r->len == 0 && r->maxbuf != 0 && sdsavail(r->buf) > r->maxbuf) { + sdsfree(r->buf); + r->buf = sdsempty(); + r->pos = 0; + + /* r->buf should not be NULL since we just free'd a larger one. */ + assert(r->buf != NULL); + } + + newbuf = sdscatlen(r->buf,buf,len); + if (newbuf == NULL) { + __redisReaderSetErrorOOM(r); + return REDIS_ERR; + } + + r->buf = newbuf; + r->len = sdslen(r->buf); + } + + return REDIS_OK; +} + +int redisReaderGetReply(redisReader *r, void **reply) { + /* Default target pointer to NULL. */ + if (reply != NULL) + *reply = NULL; + + /* Return early when this reader is in an erroneous state. */ + if (r->err) + return REDIS_ERR; + + /* When the buffer is empty, there will never be a reply. */ + if (r->len == 0) + return REDIS_OK; + + /* Set first item to process when the stack is empty. */ + if (r->ridx == -1) { + r->rstack[0].type = -1; + r->rstack[0].elements = -1; + r->rstack[0].idx = -1; + r->rstack[0].obj = NULL; + r->rstack[0].parent = NULL; + r->rstack[0].privdata = r->privdata; + r->ridx = 0; + } + + /* Process items in reply. */ + while (r->ridx >= 0) + if (processItem(r) != REDIS_OK) + break; + + /* Return ASAP when an error occurred. */ + if (r->err) + return REDIS_ERR; + + /* Discard part of the buffer when we've consumed at least 1k, to avoid + * doing unnecessary calls to memmove() in sds.c. */ + if (r->pos >= 1024) { + sdsrange(r->buf,r->pos,-1); + r->pos = 0; + r->len = sdslen(r->buf); + } + + /* Emit a reply when there is one. */ + if (r->ridx == -1) { + if (reply != NULL) + *reply = r->reply; + r->reply = NULL; + } + return REDIS_OK; +} diff --git a/controller/thirdparty/hiredis-0.14.1/read.h b/controller/thirdparty/hiredis-0.14.1/read.h new file mode 100644 index 000000000..2988aa453 --- /dev/null +++ b/controller/thirdparty/hiredis-0.14.1/read.h @@ -0,0 +1,111 @@ +/* + * Copyright (c) 2009-2011, Salvatore Sanfilippo + * Copyright (c) 2010-2011, Pieter Noordhuis + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * * Neither the name of Redis nor the names of its contributors may be used + * to endorse or promote products derived from this software without + * specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + + +#ifndef __HIREDIS_READ_H +#define __HIREDIS_READ_H +#include /* for size_t */ + +#define REDIS_ERR -1 +#define REDIS_OK 0 + +/* When an error occurs, the err flag in a context is set to hold the type of + * error that occurred. REDIS_ERR_IO means there was an I/O error and you + * should use the "errno" variable to find out what is wrong. + * For other values, the "errstr" field will hold a description. */ +#define REDIS_ERR_IO 1 /* Error in read or write */ +#define REDIS_ERR_EOF 3 /* End of file */ +#define REDIS_ERR_PROTOCOL 4 /* Protocol error */ +#define REDIS_ERR_OOM 5 /* Out of memory */ +#define REDIS_ERR_OTHER 2 /* Everything else... */ + +#define REDIS_REPLY_STRING 1 +#define REDIS_REPLY_ARRAY 2 +#define REDIS_REPLY_INTEGER 3 +#define REDIS_REPLY_NIL 4 +#define REDIS_REPLY_STATUS 5 +#define REDIS_REPLY_ERROR 6 + +#define REDIS_READER_MAX_BUF (1024*16) /* Default max unused reader buffer. */ + +#ifdef __cplusplus +extern "C" { +#endif + +typedef struct redisReadTask { + int type; + int elements; /* number of elements in multibulk container */ + int idx; /* index in parent (array) object */ + void *obj; /* holds user-generated value for a read task */ + struct redisReadTask *parent; /* parent task */ + void *privdata; /* user-settable arbitrary field */ +} redisReadTask; + +typedef struct redisReplyObjectFunctions { + void *(*createString)(const redisReadTask*, char*, size_t); + void *(*createArray)(const redisReadTask*, int); + void *(*createInteger)(const redisReadTask*, long long); + void *(*createNil)(const redisReadTask*); + void (*freeObject)(void*); +} redisReplyObjectFunctions; + +typedef struct redisReader { + int err; /* Error flags, 0 when there is no error */ + char errstr[128]; /* String representation of error when applicable */ + + char *buf; /* Read buffer */ + size_t pos; /* Buffer cursor */ + size_t len; /* Buffer length */ + size_t maxbuf; /* Max length of unused buffer */ + + redisReadTask rstack[9]; + int ridx; /* Index of current read task */ + void *reply; /* Temporary reply pointer */ + + redisReplyObjectFunctions *fn; + void *privdata; +} redisReader; + +/* Public API for the protocol parser. */ +redisReader *redisReaderCreateWithFunctions(redisReplyObjectFunctions *fn); +void redisReaderFree(redisReader *r); +int redisReaderFeed(redisReader *r, const char *buf, size_t len); +int redisReaderGetReply(redisReader *r, void **reply); + +#define redisReaderSetPrivdata(_r, _p) (int)(((redisReader*)(_r))->privdata = (_p)) +#define redisReaderGetObject(_r) (((redisReader*)(_r))->reply) +#define redisReaderGetError(_r) (((redisReader*)(_r))->errstr) + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/controller/thirdparty/hiredis-0.14.1/sds.c b/controller/thirdparty/hiredis-0.14.1/sds.c new file mode 100644 index 000000000..923ffd82f --- /dev/null +++ b/controller/thirdparty/hiredis-0.14.1/sds.c @@ -0,0 +1,1272 @@ +/* SDSLib 2.0 -- A C dynamic strings library + * + * Copyright (c) 2006-2015, Salvatore Sanfilippo + * Copyright (c) 2015, Oran Agra + * Copyright (c) 2015, Redis Labs, Inc + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * * Neither the name of Redis nor the names of its contributors may be used + * to endorse or promote products derived from this software without + * specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +#include +#include +#include +#include +#include +#include "sds.h" +#include "sdsalloc.h" + +static inline int sdsHdrSize(char type) { + switch(type&SDS_TYPE_MASK) { + case SDS_TYPE_5: + return sizeof(struct sdshdr5); + case SDS_TYPE_8: + return sizeof(struct sdshdr8); + case SDS_TYPE_16: + return sizeof(struct sdshdr16); + case SDS_TYPE_32: + return sizeof(struct sdshdr32); + case SDS_TYPE_64: + return sizeof(struct sdshdr64); + } + return 0; +} + +static inline char sdsReqType(size_t string_size) { + if (string_size < 32) + return SDS_TYPE_5; + if (string_size < 0xff) + return SDS_TYPE_8; + if (string_size < 0xffff) + return SDS_TYPE_16; + if (string_size < 0xffffffff) + return SDS_TYPE_32; + return SDS_TYPE_64; +} + +/* Create a new sds string with the content specified by the 'init' pointer + * and 'initlen'. + * If NULL is used for 'init' the string is initialized with zero bytes. + * + * The string is always null-termined (all the sds strings are, always) so + * even if you create an sds string with: + * + * mystring = sdsnewlen("abc",3); + * + * You can print the string with printf() as there is an implicit \0 at the + * end of the string. However the string is binary safe and can contain + * \0 characters in the middle, as the length is stored in the sds header. */ +sds sdsnewlen(const void *init, size_t initlen) { + void *sh; + sds s; + char type = sdsReqType(initlen); + /* Empty strings are usually created in order to append. Use type 8 + * since type 5 is not good at this. */ + if (type == SDS_TYPE_5 && initlen == 0) type = SDS_TYPE_8; + int hdrlen = sdsHdrSize(type); + unsigned char *fp; /* flags pointer. */ + + sh = s_malloc(hdrlen+initlen+1); + if (sh == NULL) return NULL; + if (!init) + memset(sh, 0, hdrlen+initlen+1); + s = (char*)sh+hdrlen; + fp = ((unsigned char*)s)-1; + switch(type) { + case SDS_TYPE_5: { + *fp = type | (initlen << SDS_TYPE_BITS); + break; + } + case SDS_TYPE_8: { + SDS_HDR_VAR(8,s); + sh->len = initlen; + sh->alloc = initlen; + *fp = type; + break; + } + case SDS_TYPE_16: { + SDS_HDR_VAR(16,s); + sh->len = initlen; + sh->alloc = initlen; + *fp = type; + break; + } + case SDS_TYPE_32: { + SDS_HDR_VAR(32,s); + sh->len = initlen; + sh->alloc = initlen; + *fp = type; + break; + } + case SDS_TYPE_64: { + SDS_HDR_VAR(64,s); + sh->len = initlen; + sh->alloc = initlen; + *fp = type; + break; + } + } + if (initlen && init) + memcpy(s, init, initlen); + s[initlen] = '\0'; + return s; +} + +/* Create an empty (zero length) sds string. Even in this case the string + * always has an implicit null term. */ +sds sdsempty(void) { + return sdsnewlen("",0); +} + +/* Create a new sds string starting from a null terminated C string. */ +sds sdsnew(const char *init) { + size_t initlen = (init == NULL) ? 0 : strlen(init); + return sdsnewlen(init, initlen); +} + +/* Duplicate an sds string. */ +sds sdsdup(const sds s) { + return sdsnewlen(s, sdslen(s)); +} + +/* Free an sds string. No operation is performed if 's' is NULL. */ +void sdsfree(sds s) { + if (s == NULL) return; + s_free((char*)s-sdsHdrSize(s[-1])); +} + +/* Set the sds string length to the length as obtained with strlen(), so + * considering as content only up to the first null term character. + * + * This function is useful when the sds string is hacked manually in some + * way, like in the following example: + * + * s = sdsnew("foobar"); + * s[2] = '\0'; + * sdsupdatelen(s); + * printf("%d\n", sdslen(s)); + * + * The output will be "2", but if we comment out the call to sdsupdatelen() + * the output will be "6" as the string was modified but the logical length + * remains 6 bytes. */ +void sdsupdatelen(sds s) { + int reallen = strlen(s); + sdssetlen(s, reallen); +} + +/* Modify an sds string in-place to make it empty (zero length). + * However all the existing buffer is not discarded but set as free space + * so that next append operations will not require allocations up to the + * number of bytes previously available. */ +void sdsclear(sds s) { + sdssetlen(s, 0); + s[0] = '\0'; +} + +/* Enlarge the free space at the end of the sds string so that the caller + * is sure that after calling this function can overwrite up to addlen + * bytes after the end of the string, plus one more byte for nul term. + * + * Note: this does not change the *length* of the sds string as returned + * by sdslen(), but only the free buffer space we have. */ +sds sdsMakeRoomFor(sds s, size_t addlen) { + void *sh, *newsh; + size_t avail = sdsavail(s); + size_t len, newlen; + char type, oldtype = s[-1] & SDS_TYPE_MASK; + int hdrlen; + + /* Return ASAP if there is enough space left. */ + if (avail >= addlen) return s; + + len = sdslen(s); + sh = (char*)s-sdsHdrSize(oldtype); + newlen = (len+addlen); + if (newlen < SDS_MAX_PREALLOC) + newlen *= 2; + else + newlen += SDS_MAX_PREALLOC; + + type = sdsReqType(newlen); + + /* Don't use type 5: the user is appending to the string and type 5 is + * not able to remember empty space, so sdsMakeRoomFor() must be called + * at every appending operation. */ + if (type == SDS_TYPE_5) type = SDS_TYPE_8; + + hdrlen = sdsHdrSize(type); + if (oldtype==type) { + newsh = s_realloc(sh, hdrlen+newlen+1); + if (newsh == NULL) return NULL; + s = (char*)newsh+hdrlen; + } else { + /* Since the header size changes, need to move the string forward, + * and can't use realloc */ + newsh = s_malloc(hdrlen+newlen+1); + if (newsh == NULL) return NULL; + memcpy((char*)newsh+hdrlen, s, len+1); + s_free(sh); + s = (char*)newsh+hdrlen; + s[-1] = type; + sdssetlen(s, len); + } + sdssetalloc(s, newlen); + return s; +} + +/* Reallocate the sds string so that it has no free space at the end. The + * contained string remains not altered, but next concatenation operations + * will require a reallocation. + * + * After the call, the passed sds string is no longer valid and all the + * references must be substituted with the new pointer returned by the call. */ +sds sdsRemoveFreeSpace(sds s) { + void *sh, *newsh; + char type, oldtype = s[-1] & SDS_TYPE_MASK; + int hdrlen; + size_t len = sdslen(s); + sh = (char*)s-sdsHdrSize(oldtype); + + type = sdsReqType(len); + hdrlen = sdsHdrSize(type); + if (oldtype==type) { + newsh = s_realloc(sh, hdrlen+len+1); + if (newsh == NULL) return NULL; + s = (char*)newsh+hdrlen; + } else { + newsh = s_malloc(hdrlen+len+1); + if (newsh == NULL) return NULL; + memcpy((char*)newsh+hdrlen, s, len+1); + s_free(sh); + s = (char*)newsh+hdrlen; + s[-1] = type; + sdssetlen(s, len); + } + sdssetalloc(s, len); + return s; +} + +/* Return the total size of the allocation of the specifed sds string, + * including: + * 1) The sds header before the pointer. + * 2) The string. + * 3) The free buffer at the end if any. + * 4) The implicit null term. + */ +size_t sdsAllocSize(sds s) { + size_t alloc = sdsalloc(s); + return sdsHdrSize(s[-1])+alloc+1; +} + +/* Return the pointer of the actual SDS allocation (normally SDS strings + * are referenced by the start of the string buffer). */ +void *sdsAllocPtr(sds s) { + return (void*) (s-sdsHdrSize(s[-1])); +} + +/* Increment the sds length and decrements the left free space at the + * end of the string according to 'incr'. Also set the null term + * in the new end of the string. + * + * This function is used in order to fix the string length after the + * user calls sdsMakeRoomFor(), writes something after the end of + * the current string, and finally needs to set the new length. + * + * Note: it is possible to use a negative increment in order to + * right-trim the string. + * + * Usage example: + * + * Using sdsIncrLen() and sdsMakeRoomFor() it is possible to mount the + * following schema, to cat bytes coming from the kernel to the end of an + * sds string without copying into an intermediate buffer: + * + * oldlen = sdslen(s); + * s = sdsMakeRoomFor(s, BUFFER_SIZE); + * nread = read(fd, s+oldlen, BUFFER_SIZE); + * ... check for nread <= 0 and handle it ... + * sdsIncrLen(s, nread); + */ +void sdsIncrLen(sds s, int incr) { + unsigned char flags = s[-1]; + size_t len; + switch(flags&SDS_TYPE_MASK) { + case SDS_TYPE_5: { + unsigned char *fp = ((unsigned char*)s)-1; + unsigned char oldlen = SDS_TYPE_5_LEN(flags); + assert((incr > 0 && oldlen+incr < 32) || (incr < 0 && oldlen >= (unsigned int)(-incr))); + *fp = SDS_TYPE_5 | ((oldlen+incr) << SDS_TYPE_BITS); + len = oldlen+incr; + break; + } + case SDS_TYPE_8: { + SDS_HDR_VAR(8,s); + assert((incr >= 0 && sh->alloc-sh->len >= incr) || (incr < 0 && sh->len >= (unsigned int)(-incr))); + len = (sh->len += incr); + break; + } + case SDS_TYPE_16: { + SDS_HDR_VAR(16,s); + assert((incr >= 0 && sh->alloc-sh->len >= incr) || (incr < 0 && sh->len >= (unsigned int)(-incr))); + len = (sh->len += incr); + break; + } + case SDS_TYPE_32: { + SDS_HDR_VAR(32,s); + assert((incr >= 0 && sh->alloc-sh->len >= (unsigned int)incr) || (incr < 0 && sh->len >= (unsigned int)(-incr))); + len = (sh->len += incr); + break; + } + case SDS_TYPE_64: { + SDS_HDR_VAR(64,s); + assert((incr >= 0 && sh->alloc-sh->len >= (uint64_t)incr) || (incr < 0 && sh->len >= (uint64_t)(-incr))); + len = (sh->len += incr); + break; + } + default: len = 0; /* Just to avoid compilation warnings. */ + } + s[len] = '\0'; +} + +/* Grow the sds to have the specified length. Bytes that were not part of + * the original length of the sds will be set to zero. + * + * if the specified length is smaller than the current length, no operation + * is performed. */ +sds sdsgrowzero(sds s, size_t len) { + size_t curlen = sdslen(s); + + if (len <= curlen) return s; + s = sdsMakeRoomFor(s,len-curlen); + if (s == NULL) return NULL; + + /* Make sure added region doesn't contain garbage */ + memset(s+curlen,0,(len-curlen+1)); /* also set trailing \0 byte */ + sdssetlen(s, len); + return s; +} + +/* Append the specified binary-safe string pointed by 't' of 'len' bytes to the + * end of the specified sds string 's'. + * + * After the call, the passed sds string is no longer valid and all the + * references must be substituted with the new pointer returned by the call. */ +sds sdscatlen(sds s, const void *t, size_t len) { + size_t curlen = sdslen(s); + + s = sdsMakeRoomFor(s,len); + if (s == NULL) return NULL; + memcpy(s+curlen, t, len); + sdssetlen(s, curlen+len); + s[curlen+len] = '\0'; + return s; +} + +/* Append the specified null termianted C string to the sds string 's'. + * + * After the call, the passed sds string is no longer valid and all the + * references must be substituted with the new pointer returned by the call. */ +sds sdscat(sds s, const char *t) { + return sdscatlen(s, t, strlen(t)); +} + +/* Append the specified sds 't' to the existing sds 's'. + * + * After the call, the modified sds string is no longer valid and all the + * references must be substituted with the new pointer returned by the call. */ +sds sdscatsds(sds s, const sds t) { + return sdscatlen(s, t, sdslen(t)); +} + +/* Destructively modify the sds string 's' to hold the specified binary + * safe string pointed by 't' of length 'len' bytes. */ +sds sdscpylen(sds s, const char *t, size_t len) { + if (sdsalloc(s) < len) { + s = sdsMakeRoomFor(s,len-sdslen(s)); + if (s == NULL) return NULL; + } + memcpy(s, t, len); + s[len] = '\0'; + sdssetlen(s, len); + return s; +} + +/* Like sdscpylen() but 't' must be a null-termined string so that the length + * of the string is obtained with strlen(). */ +sds sdscpy(sds s, const char *t) { + return sdscpylen(s, t, strlen(t)); +} + +/* Helper for sdscatlonglong() doing the actual number -> string + * conversion. 's' must point to a string with room for at least + * SDS_LLSTR_SIZE bytes. + * + * The function returns the length of the null-terminated string + * representation stored at 's'. */ +#define SDS_LLSTR_SIZE 21 +int sdsll2str(char *s, long long value) { + char *p, aux; + unsigned long long v; + size_t l; + + /* Generate the string representation, this method produces + * an reversed string. */ + v = (value < 0) ? -value : value; + p = s; + do { + *p++ = '0'+(v%10); + v /= 10; + } while(v); + if (value < 0) *p++ = '-'; + + /* Compute length and add null term. */ + l = p-s; + *p = '\0'; + + /* Reverse the string. */ + p--; + while(s < p) { + aux = *s; + *s = *p; + *p = aux; + s++; + p--; + } + return l; +} + +/* Identical sdsll2str(), but for unsigned long long type. */ +int sdsull2str(char *s, unsigned long long v) { + char *p, aux; + size_t l; + + /* Generate the string representation, this method produces + * an reversed string. */ + p = s; + do { + *p++ = '0'+(v%10); + v /= 10; + } while(v); + + /* Compute length and add null term. */ + l = p-s; + *p = '\0'; + + /* Reverse the string. */ + p--; + while(s < p) { + aux = *s; + *s = *p; + *p = aux; + s++; + p--; + } + return l; +} + +/* Create an sds string from a long long value. It is much faster than: + * + * sdscatprintf(sdsempty(),"%lld\n", value); + */ +sds sdsfromlonglong(long long value) { + char buf[SDS_LLSTR_SIZE]; + int len = sdsll2str(buf,value); + + return sdsnewlen(buf,len); +} + +/* Like sdscatprintf() but gets va_list instead of being variadic. */ +sds sdscatvprintf(sds s, const char *fmt, va_list ap) { + va_list cpy; + char staticbuf[1024], *buf = staticbuf, *t; + size_t buflen = strlen(fmt)*2; + + /* We try to start using a static buffer for speed. + * If not possible we revert to heap allocation. */ + if (buflen > sizeof(staticbuf)) { + buf = s_malloc(buflen); + if (buf == NULL) return NULL; + } else { + buflen = sizeof(staticbuf); + } + + /* Try with buffers two times bigger every time we fail to + * fit the string in the current buffer size. */ + while(1) { + buf[buflen-2] = '\0'; + va_copy(cpy,ap); + vsnprintf(buf, buflen, fmt, cpy); + va_end(cpy); + if (buf[buflen-2] != '\0') { + if (buf != staticbuf) s_free(buf); + buflen *= 2; + buf = s_malloc(buflen); + if (buf == NULL) return NULL; + continue; + } + break; + } + + /* Finally concat the obtained string to the SDS string and return it. */ + t = sdscat(s, buf); + if (buf != staticbuf) s_free(buf); + return t; +} + +/* Append to the sds string 's' a string obtained using printf-alike format + * specifier. + * + * After the call, the modified sds string is no longer valid and all the + * references must be substituted with the new pointer returned by the call. + * + * Example: + * + * s = sdsnew("Sum is: "); + * s = sdscatprintf(s,"%d+%d = %d",a,b,a+b). + * + * Often you need to create a string from scratch with the printf-alike + * format. When this is the need, just use sdsempty() as the target string: + * + * s = sdscatprintf(sdsempty(), "... your format ...", args); + */ +sds sdscatprintf(sds s, const char *fmt, ...) { + va_list ap; + char *t; + va_start(ap, fmt); + t = sdscatvprintf(s,fmt,ap); + va_end(ap); + return t; +} + +/* This function is similar to sdscatprintf, but much faster as it does + * not rely on sprintf() family functions implemented by the libc that + * are often very slow. Moreover directly handling the sds string as + * new data is concatenated provides a performance improvement. + * + * However this function only handles an incompatible subset of printf-alike + * format specifiers: + * + * %s - C String + * %S - SDS string + * %i - signed int + * %I - 64 bit signed integer (long long, int64_t) + * %u - unsigned int + * %U - 64 bit unsigned integer (unsigned long long, uint64_t) + * %% - Verbatim "%" character. + */ +sds sdscatfmt(sds s, char const *fmt, ...) { + const char *f = fmt; + int i; + va_list ap; + + va_start(ap,fmt); + i = sdslen(s); /* Position of the next byte to write to dest str. */ + while(*f) { + char next, *str; + size_t l; + long long num; + unsigned long long unum; + + /* Make sure there is always space for at least 1 char. */ + if (sdsavail(s)==0) { + s = sdsMakeRoomFor(s,1); + } + + switch(*f) { + case '%': + next = *(f+1); + f++; + switch(next) { + case 's': + case 'S': + str = va_arg(ap,char*); + l = (next == 's') ? strlen(str) : sdslen(str); + if (sdsavail(s) < l) { + s = sdsMakeRoomFor(s,l); + } + memcpy(s+i,str,l); + sdsinclen(s,l); + i += l; + break; + case 'i': + case 'I': + if (next == 'i') + num = va_arg(ap,int); + else + num = va_arg(ap,long long); + { + char buf[SDS_LLSTR_SIZE]; + l = sdsll2str(buf,num); + if (sdsavail(s) < l) { + s = sdsMakeRoomFor(s,l); + } + memcpy(s+i,buf,l); + sdsinclen(s,l); + i += l; + } + break; + case 'u': + case 'U': + if (next == 'u') + unum = va_arg(ap,unsigned int); + else + unum = va_arg(ap,unsigned long long); + { + char buf[SDS_LLSTR_SIZE]; + l = sdsull2str(buf,unum); + if (sdsavail(s) < l) { + s = sdsMakeRoomFor(s,l); + } + memcpy(s+i,buf,l); + sdsinclen(s,l); + i += l; + } + break; + default: /* Handle %% and generally %. */ + s[i++] = next; + sdsinclen(s,1); + break; + } + break; + default: + s[i++] = *f; + sdsinclen(s,1); + break; + } + f++; + } + va_end(ap); + + /* Add null-term */ + s[i] = '\0'; + return s; +} + +/* Remove the part of the string from left and from right composed just of + * contiguous characters found in 'cset', that is a null terminted C string. + * + * After the call, the modified sds string is no longer valid and all the + * references must be substituted with the new pointer returned by the call. + * + * Example: + * + * s = sdsnew("AA...AA.a.aa.aHelloWorld :::"); + * s = sdstrim(s,"Aa. :"); + * printf("%s\n", s); + * + * Output will be just "Hello World". + */ +sds sdstrim(sds s, const char *cset) { + char *start, *end, *sp, *ep; + size_t len; + + sp = start = s; + ep = end = s+sdslen(s)-1; + while(sp <= end && strchr(cset, *sp)) sp++; + while(ep > sp && strchr(cset, *ep)) ep--; + len = (sp > ep) ? 0 : ((ep-sp)+1); + if (s != sp) memmove(s, sp, len); + s[len] = '\0'; + sdssetlen(s,len); + return s; +} + +/* Turn the string into a smaller (or equal) string containing only the + * substring specified by the 'start' and 'end' indexes. + * + * start and end can be negative, where -1 means the last character of the + * string, -2 the penultimate character, and so forth. + * + * The interval is inclusive, so the start and end characters will be part + * of the resulting string. + * + * The string is modified in-place. + * + * Example: + * + * s = sdsnew("Hello World"); + * sdsrange(s,1,-1); => "ello World" + */ +void sdsrange(sds s, int start, int end) { + size_t newlen, len = sdslen(s); + + if (len == 0) return; + if (start < 0) { + start = len+start; + if (start < 0) start = 0; + } + if (end < 0) { + end = len+end; + if (end < 0) end = 0; + } + newlen = (start > end) ? 0 : (end-start)+1; + if (newlen != 0) { + if (start >= (signed)len) { + newlen = 0; + } else if (end >= (signed)len) { + end = len-1; + newlen = (start > end) ? 0 : (end-start)+1; + } + } else { + start = 0; + } + if (start && newlen) memmove(s, s+start, newlen); + s[newlen] = 0; + sdssetlen(s,newlen); +} + +/* Apply tolower() to every character of the sds string 's'. */ +void sdstolower(sds s) { + int len = sdslen(s), j; + + for (j = 0; j < len; j++) s[j] = tolower(s[j]); +} + +/* Apply toupper() to every character of the sds string 's'. */ +void sdstoupper(sds s) { + int len = sdslen(s), j; + + for (j = 0; j < len; j++) s[j] = toupper(s[j]); +} + +/* Compare two sds strings s1 and s2 with memcmp(). + * + * Return value: + * + * positive if s1 > s2. + * negative if s1 < s2. + * 0 if s1 and s2 are exactly the same binary string. + * + * If two strings share exactly the same prefix, but one of the two has + * additional characters, the longer string is considered to be greater than + * the smaller one. */ +int sdscmp(const sds s1, const sds s2) { + size_t l1, l2, minlen; + int cmp; + + l1 = sdslen(s1); + l2 = sdslen(s2); + minlen = (l1 < l2) ? l1 : l2; + cmp = memcmp(s1,s2,minlen); + if (cmp == 0) return l1-l2; + return cmp; +} + +/* Split 's' with separator in 'sep'. An array + * of sds strings is returned. *count will be set + * by reference to the number of tokens returned. + * + * On out of memory, zero length string, zero length + * separator, NULL is returned. + * + * Note that 'sep' is able to split a string using + * a multi-character separator. For example + * sdssplit("foo_-_bar","_-_"); will return two + * elements "foo" and "bar". + * + * This version of the function is binary-safe but + * requires length arguments. sdssplit() is just the + * same function but for zero-terminated strings. + */ +sds *sdssplitlen(const char *s, int len, const char *sep, int seplen, int *count) { + int elements = 0, slots = 5, start = 0, j; + sds *tokens; + + if (seplen < 1 || len < 0) return NULL; + + tokens = s_malloc(sizeof(sds)*slots); + if (tokens == NULL) return NULL; + + if (len == 0) { + *count = 0; + return tokens; + } + for (j = 0; j < (len-(seplen-1)); j++) { + /* make sure there is room for the next element and the final one */ + if (slots < elements+2) { + sds *newtokens; + + slots *= 2; + newtokens = s_realloc(tokens,sizeof(sds)*slots); + if (newtokens == NULL) goto cleanup; + tokens = newtokens; + } + /* search the separator */ + if ((seplen == 1 && *(s+j) == sep[0]) || (memcmp(s+j,sep,seplen) == 0)) { + tokens[elements] = sdsnewlen(s+start,j-start); + if (tokens[elements] == NULL) goto cleanup; + elements++; + start = j+seplen; + j = j+seplen-1; /* skip the separator */ + } + } + /* Add the final element. We are sure there is room in the tokens array. */ + tokens[elements] = sdsnewlen(s+start,len-start); + if (tokens[elements] == NULL) goto cleanup; + elements++; + *count = elements; + return tokens; + +cleanup: + { + int i; + for (i = 0; i < elements; i++) sdsfree(tokens[i]); + s_free(tokens); + *count = 0; + return NULL; + } +} + +/* Free the result returned by sdssplitlen(), or do nothing if 'tokens' is NULL. */ +void sdsfreesplitres(sds *tokens, int count) { + if (!tokens) return; + while(count--) + sdsfree(tokens[count]); + s_free(tokens); +} + +/* Append to the sds string "s" an escaped string representation where + * all the non-printable characters (tested with isprint()) are turned into + * escapes in the form "\n\r\a...." or "\x". + * + * After the call, the modified sds string is no longer valid and all the + * references must be substituted with the new pointer returned by the call. */ +sds sdscatrepr(sds s, const char *p, size_t len) { + s = sdscatlen(s,"\"",1); + while(len--) { + switch(*p) { + case '\\': + case '"': + s = sdscatprintf(s,"\\%c",*p); + break; + case '\n': s = sdscatlen(s,"\\n",2); break; + case '\r': s = sdscatlen(s,"\\r",2); break; + case '\t': s = sdscatlen(s,"\\t",2); break; + case '\a': s = sdscatlen(s,"\\a",2); break; + case '\b': s = sdscatlen(s,"\\b",2); break; + default: + if (isprint(*p)) + s = sdscatprintf(s,"%c",*p); + else + s = sdscatprintf(s,"\\x%02x",(unsigned char)*p); + break; + } + p++; + } + return sdscatlen(s,"\"",1); +} + +/* Helper function for sdssplitargs() that returns non zero if 'c' + * is a valid hex digit. */ +int is_hex_digit(char c) { + return (c >= '0' && c <= '9') || (c >= 'a' && c <= 'f') || + (c >= 'A' && c <= 'F'); +} + +/* Helper function for sdssplitargs() that converts a hex digit into an + * integer from 0 to 15 */ +int hex_digit_to_int(char c) { + switch(c) { + case '0': return 0; + case '1': return 1; + case '2': return 2; + case '3': return 3; + case '4': return 4; + case '5': return 5; + case '6': return 6; + case '7': return 7; + case '8': return 8; + case '9': return 9; + case 'a': case 'A': return 10; + case 'b': case 'B': return 11; + case 'c': case 'C': return 12; + case 'd': case 'D': return 13; + case 'e': case 'E': return 14; + case 'f': case 'F': return 15; + default: return 0; + } +} + +/* Split a line into arguments, where every argument can be in the + * following programming-language REPL-alike form: + * + * foo bar "newline are supported\n" and "\xff\x00otherstuff" + * + * The number of arguments is stored into *argc, and an array + * of sds is returned. + * + * The caller should free the resulting array of sds strings with + * sdsfreesplitres(). + * + * Note that sdscatrepr() is able to convert back a string into + * a quoted string in the same format sdssplitargs() is able to parse. + * + * The function returns the allocated tokens on success, even when the + * input string is empty, or NULL if the input contains unbalanced + * quotes or closed quotes followed by non space characters + * as in: "foo"bar or "foo' + */ +sds *sdssplitargs(const char *line, int *argc) { + const char *p = line; + char *current = NULL; + char **vector = NULL; + + *argc = 0; + while(1) { + /* skip blanks */ + while(*p && isspace(*p)) p++; + if (*p) { + /* get a token */ + int inq=0; /* set to 1 if we are in "quotes" */ + int insq=0; /* set to 1 if we are in 'single quotes' */ + int done=0; + + if (current == NULL) current = sdsempty(); + while(!done) { + if (inq) { + if (*p == '\\' && *(p+1) == 'x' && + is_hex_digit(*(p+2)) && + is_hex_digit(*(p+3))) + { + unsigned char byte; + + byte = (hex_digit_to_int(*(p+2))*16)+ + hex_digit_to_int(*(p+3)); + current = sdscatlen(current,(char*)&byte,1); + p += 3; + } else if (*p == '\\' && *(p+1)) { + char c; + + p++; + switch(*p) { + case 'n': c = '\n'; break; + case 'r': c = '\r'; break; + case 't': c = '\t'; break; + case 'b': c = '\b'; break; + case 'a': c = '\a'; break; + default: c = *p; break; + } + current = sdscatlen(current,&c,1); + } else if (*p == '"') { + /* closing quote must be followed by a space or + * nothing at all. */ + if (*(p+1) && !isspace(*(p+1))) goto err; + done=1; + } else if (!*p) { + /* unterminated quotes */ + goto err; + } else { + current = sdscatlen(current,p,1); + } + } else if (insq) { + if (*p == '\\' && *(p+1) == '\'') { + p++; + current = sdscatlen(current,"'",1); + } else if (*p == '\'') { + /* closing quote must be followed by a space or + * nothing at all. */ + if (*(p+1) && !isspace(*(p+1))) goto err; + done=1; + } else if (!*p) { + /* unterminated quotes */ + goto err; + } else { + current = sdscatlen(current,p,1); + } + } else { + switch(*p) { + case ' ': + case '\n': + case '\r': + case '\t': + case '\0': + done=1; + break; + case '"': + inq=1; + break; + case '\'': + insq=1; + break; + default: + current = sdscatlen(current,p,1); + break; + } + } + if (*p) p++; + } + /* add the token to the vector */ + vector = s_realloc(vector,((*argc)+1)*sizeof(char*)); + vector[*argc] = current; + (*argc)++; + current = NULL; + } else { + /* Even on empty input string return something not NULL. */ + if (vector == NULL) vector = s_malloc(sizeof(void*)); + return vector; + } + } + +err: + while((*argc)--) + sdsfree(vector[*argc]); + s_free(vector); + if (current) sdsfree(current); + *argc = 0; + return NULL; +} + +/* Modify the string substituting all the occurrences of the set of + * characters specified in the 'from' string to the corresponding character + * in the 'to' array. + * + * For instance: sdsmapchars(mystring, "ho", "01", 2) + * will have the effect of turning the string "hello" into "0ell1". + * + * The function returns the sds string pointer, that is always the same + * as the input pointer since no resize is needed. */ +sds sdsmapchars(sds s, const char *from, const char *to, size_t setlen) { + size_t j, i, l = sdslen(s); + + for (j = 0; j < l; j++) { + for (i = 0; i < setlen; i++) { + if (s[j] == from[i]) { + s[j] = to[i]; + break; + } + } + } + return s; +} + +/* Join an array of C strings using the specified separator (also a C string). + * Returns the result as an sds string. */ +sds sdsjoin(char **argv, int argc, char *sep) { + sds join = sdsempty(); + int j; + + for (j = 0; j < argc; j++) { + join = sdscat(join, argv[j]); + if (j != argc-1) join = sdscat(join,sep); + } + return join; +} + +/* Like sdsjoin, but joins an array of SDS strings. */ +sds sdsjoinsds(sds *argv, int argc, const char *sep, size_t seplen) { + sds join = sdsempty(); + int j; + + for (j = 0; j < argc; j++) { + join = sdscatsds(join, argv[j]); + if (j != argc-1) join = sdscatlen(join,sep,seplen); + } + return join; +} + +/* Wrappers to the allocators used by SDS. Note that SDS will actually + * just use the macros defined into sdsalloc.h in order to avoid to pay + * the overhead of function calls. Here we define these wrappers only for + * the programs SDS is linked to, if they want to touch the SDS internals + * even if they use a different allocator. */ +void *sds_malloc(size_t size) { return s_malloc(size); } +void *sds_realloc(void *ptr, size_t size) { return s_realloc(ptr,size); } +void sds_free(void *ptr) { s_free(ptr); } + +#if defined(SDS_TEST_MAIN) +#include +#include "testhelp.h" +#include "limits.h" + +#define UNUSED(x) (void)(x) +int sdsTest(void) { + { + sds x = sdsnew("foo"), y; + + test_cond("Create a string and obtain the length", + sdslen(x) == 3 && memcmp(x,"foo\0",4) == 0) + + sdsfree(x); + x = sdsnewlen("foo",2); + test_cond("Create a string with specified length", + sdslen(x) == 2 && memcmp(x,"fo\0",3) == 0) + + x = sdscat(x,"bar"); + test_cond("Strings concatenation", + sdslen(x) == 5 && memcmp(x,"fobar\0",6) == 0); + + x = sdscpy(x,"a"); + test_cond("sdscpy() against an originally longer string", + sdslen(x) == 1 && memcmp(x,"a\0",2) == 0) + + x = sdscpy(x,"xyzxxxxxxxxxxyyyyyyyyyykkkkkkkkkk"); + test_cond("sdscpy() against an originally shorter string", + sdslen(x) == 33 && + memcmp(x,"xyzxxxxxxxxxxyyyyyyyyyykkkkkkkkkk\0",33) == 0) + + sdsfree(x); + x = sdscatprintf(sdsempty(),"%d",123); + test_cond("sdscatprintf() seems working in the base case", + sdslen(x) == 3 && memcmp(x,"123\0",4) == 0) + + sdsfree(x); + x = sdsnew("--"); + x = sdscatfmt(x, "Hello %s World %I,%I--", "Hi!", LLONG_MIN,LLONG_MAX); + test_cond("sdscatfmt() seems working in the base case", + sdslen(x) == 60 && + memcmp(x,"--Hello Hi! World -9223372036854775808," + "9223372036854775807--",60) == 0) + printf("[%s]\n",x); + + sdsfree(x); + x = sdsnew("--"); + x = sdscatfmt(x, "%u,%U--", UINT_MAX, ULLONG_MAX); + test_cond("sdscatfmt() seems working with unsigned numbers", + sdslen(x) == 35 && + memcmp(x,"--4294967295,18446744073709551615--",35) == 0) + + sdsfree(x); + x = sdsnew(" x "); + sdstrim(x," x"); + test_cond("sdstrim() works when all chars match", + sdslen(x) == 0) + + sdsfree(x); + x = sdsnew(" x "); + sdstrim(x," "); + test_cond("sdstrim() works when a single char remains", + sdslen(x) == 1 && x[0] == 'x') + + sdsfree(x); + x = sdsnew("xxciaoyyy"); + sdstrim(x,"xy"); + test_cond("sdstrim() correctly trims characters", + sdslen(x) == 4 && memcmp(x,"ciao\0",5) == 0) + + y = sdsdup(x); + sdsrange(y,1,1); + test_cond("sdsrange(...,1,1)", + sdslen(y) == 1 && memcmp(y,"i\0",2) == 0) + + sdsfree(y); + y = sdsdup(x); + sdsrange(y,1,-1); + test_cond("sdsrange(...,1,-1)", + sdslen(y) == 3 && memcmp(y,"iao\0",4) == 0) + + sdsfree(y); + y = sdsdup(x); + sdsrange(y,-2,-1); + test_cond("sdsrange(...,-2,-1)", + sdslen(y) == 2 && memcmp(y,"ao\0",3) == 0) + + sdsfree(y); + y = sdsdup(x); + sdsrange(y,2,1); + test_cond("sdsrange(...,2,1)", + sdslen(y) == 0 && memcmp(y,"\0",1) == 0) + + sdsfree(y); + y = sdsdup(x); + sdsrange(y,1,100); + test_cond("sdsrange(...,1,100)", + sdslen(y) == 3 && memcmp(y,"iao\0",4) == 0) + + sdsfree(y); + y = sdsdup(x); + sdsrange(y,100,100); + test_cond("sdsrange(...,100,100)", + sdslen(y) == 0 && memcmp(y,"\0",1) == 0) + + sdsfree(y); + sdsfree(x); + x = sdsnew("foo"); + y = sdsnew("foa"); + test_cond("sdscmp(foo,foa)", sdscmp(x,y) > 0) + + sdsfree(y); + sdsfree(x); + x = sdsnew("bar"); + y = sdsnew("bar"); + test_cond("sdscmp(bar,bar)", sdscmp(x,y) == 0) + + sdsfree(y); + sdsfree(x); + x = sdsnew("aar"); + y = sdsnew("bar"); + test_cond("sdscmp(bar,bar)", sdscmp(x,y) < 0) + + sdsfree(y); + sdsfree(x); + x = sdsnewlen("\a\n\0foo\r",7); + y = sdscatrepr(sdsempty(),x,sdslen(x)); + test_cond("sdscatrepr(...data...)", + memcmp(y,"\"\\a\\n\\x00foo\\r\"",15) == 0) + + { + unsigned int oldfree; + char *p; + int step = 10, j, i; + + sdsfree(x); + sdsfree(y); + x = sdsnew("0"); + test_cond("sdsnew() free/len buffers", sdslen(x) == 1 && sdsavail(x) == 0); + + /* Run the test a few times in order to hit the first two + * SDS header types. */ + for (i = 0; i < 10; i++) { + int oldlen = sdslen(x); + x = sdsMakeRoomFor(x,step); + int type = x[-1]&SDS_TYPE_MASK; + + test_cond("sdsMakeRoomFor() len", sdslen(x) == oldlen); + if (type != SDS_TYPE_5) { + test_cond("sdsMakeRoomFor() free", sdsavail(x) >= step); + oldfree = sdsavail(x); + } + p = x+oldlen; + for (j = 0; j < step; j++) { + p[j] = 'A'+j; + } + sdsIncrLen(x,step); + } + test_cond("sdsMakeRoomFor() content", + memcmp("0ABCDEFGHIJABCDEFGHIJABCDEFGHIJABCDEFGHIJABCDEFGHIJABCDEFGHIJABCDEFGHIJABCDEFGHIJABCDEFGHIJABCDEFGHIJ",x,101) == 0); + test_cond("sdsMakeRoomFor() final length",sdslen(x)==101); + + sdsfree(x); + } + } + test_report() + return 0; +} +#endif + +#ifdef SDS_TEST_MAIN +int main(void) { + return sdsTest(); +} +#endif diff --git a/controller/thirdparty/hiredis-0.14.1/sds.h b/controller/thirdparty/hiredis-0.14.1/sds.h new file mode 100644 index 000000000..13be75a9f --- /dev/null +++ b/controller/thirdparty/hiredis-0.14.1/sds.h @@ -0,0 +1,273 @@ +/* SDSLib 2.0 -- A C dynamic strings library + * + * Copyright (c) 2006-2015, Salvatore Sanfilippo + * Copyright (c) 2015, Oran Agra + * Copyright (c) 2015, Redis Labs, Inc + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * * Neither the name of Redis nor the names of its contributors may be used + * to endorse or promote products derived from this software without + * specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +#ifndef __SDS_H +#define __SDS_H + +#define SDS_MAX_PREALLOC (1024*1024) + +#include +#include +#include + +typedef char *sds; + +/* Note: sdshdr5 is never used, we just access the flags byte directly. + * However is here to document the layout of type 5 SDS strings. */ +struct __attribute__ ((__packed__)) sdshdr5 { + unsigned char flags; /* 3 lsb of type, and 5 msb of string length */ + char buf[]; +}; +struct __attribute__ ((__packed__)) sdshdr8 { + uint8_t len; /* used */ + uint8_t alloc; /* excluding the header and null terminator */ + unsigned char flags; /* 3 lsb of type, 5 unused bits */ + char buf[]; +}; +struct __attribute__ ((__packed__)) sdshdr16 { + uint16_t len; /* used */ + uint16_t alloc; /* excluding the header and null terminator */ + unsigned char flags; /* 3 lsb of type, 5 unused bits */ + char buf[]; +}; +struct __attribute__ ((__packed__)) sdshdr32 { + uint32_t len; /* used */ + uint32_t alloc; /* excluding the header and null terminator */ + unsigned char flags; /* 3 lsb of type, 5 unused bits */ + char buf[]; +}; +struct __attribute__ ((__packed__)) sdshdr64 { + uint64_t len; /* used */ + uint64_t alloc; /* excluding the header and null terminator */ + unsigned char flags; /* 3 lsb of type, 5 unused bits */ + char buf[]; +}; + +#define SDS_TYPE_5 0 +#define SDS_TYPE_8 1 +#define SDS_TYPE_16 2 +#define SDS_TYPE_32 3 +#define SDS_TYPE_64 4 +#define SDS_TYPE_MASK 7 +#define SDS_TYPE_BITS 3 +#define SDS_HDR_VAR(T,s) struct sdshdr##T *sh = (struct sdshdr##T *)((s)-(sizeof(struct sdshdr##T))); +#define SDS_HDR(T,s) ((struct sdshdr##T *)((s)-(sizeof(struct sdshdr##T)))) +#define SDS_TYPE_5_LEN(f) ((f)>>SDS_TYPE_BITS) + +static inline size_t sdslen(const sds s) { + unsigned char flags = s[-1]; + switch(flags&SDS_TYPE_MASK) { + case SDS_TYPE_5: + return SDS_TYPE_5_LEN(flags); + case SDS_TYPE_8: + return SDS_HDR(8,s)->len; + case SDS_TYPE_16: + return SDS_HDR(16,s)->len; + case SDS_TYPE_32: + return SDS_HDR(32,s)->len; + case SDS_TYPE_64: + return SDS_HDR(64,s)->len; + } + return 0; +} + +static inline size_t sdsavail(const sds s) { + unsigned char flags = s[-1]; + switch(flags&SDS_TYPE_MASK) { + case SDS_TYPE_5: { + return 0; + } + case SDS_TYPE_8: { + SDS_HDR_VAR(8,s); + return sh->alloc - sh->len; + } + case SDS_TYPE_16: { + SDS_HDR_VAR(16,s); + return sh->alloc - sh->len; + } + case SDS_TYPE_32: { + SDS_HDR_VAR(32,s); + return sh->alloc - sh->len; + } + case SDS_TYPE_64: { + SDS_HDR_VAR(64,s); + return sh->alloc - sh->len; + } + } + return 0; +} + +static inline void sdssetlen(sds s, size_t newlen) { + unsigned char flags = s[-1]; + switch(flags&SDS_TYPE_MASK) { + case SDS_TYPE_5: + { + unsigned char *fp = ((unsigned char*)s)-1; + *fp = SDS_TYPE_5 | (newlen << SDS_TYPE_BITS); + } + break; + case SDS_TYPE_8: + SDS_HDR(8,s)->len = newlen; + break; + case SDS_TYPE_16: + SDS_HDR(16,s)->len = newlen; + break; + case SDS_TYPE_32: + SDS_HDR(32,s)->len = newlen; + break; + case SDS_TYPE_64: + SDS_HDR(64,s)->len = newlen; + break; + } +} + +static inline void sdsinclen(sds s, size_t inc) { + unsigned char flags = s[-1]; + switch(flags&SDS_TYPE_MASK) { + case SDS_TYPE_5: + { + unsigned char *fp = ((unsigned char*)s)-1; + unsigned char newlen = SDS_TYPE_5_LEN(flags)+inc; + *fp = SDS_TYPE_5 | (newlen << SDS_TYPE_BITS); + } + break; + case SDS_TYPE_8: + SDS_HDR(8,s)->len += inc; + break; + case SDS_TYPE_16: + SDS_HDR(16,s)->len += inc; + break; + case SDS_TYPE_32: + SDS_HDR(32,s)->len += inc; + break; + case SDS_TYPE_64: + SDS_HDR(64,s)->len += inc; + break; + } +} + +/* sdsalloc() = sdsavail() + sdslen() */ +static inline size_t sdsalloc(const sds s) { + unsigned char flags = s[-1]; + switch(flags&SDS_TYPE_MASK) { + case SDS_TYPE_5: + return SDS_TYPE_5_LEN(flags); + case SDS_TYPE_8: + return SDS_HDR(8,s)->alloc; + case SDS_TYPE_16: + return SDS_HDR(16,s)->alloc; + case SDS_TYPE_32: + return SDS_HDR(32,s)->alloc; + case SDS_TYPE_64: + return SDS_HDR(64,s)->alloc; + } + return 0; +} + +static inline void sdssetalloc(sds s, size_t newlen) { + unsigned char flags = s[-1]; + switch(flags&SDS_TYPE_MASK) { + case SDS_TYPE_5: + /* Nothing to do, this type has no total allocation info. */ + break; + case SDS_TYPE_8: + SDS_HDR(8,s)->alloc = newlen; + break; + case SDS_TYPE_16: + SDS_HDR(16,s)->alloc = newlen; + break; + case SDS_TYPE_32: + SDS_HDR(32,s)->alloc = newlen; + break; + case SDS_TYPE_64: + SDS_HDR(64,s)->alloc = newlen; + break; + } +} + +sds sdsnewlen(const void *init, size_t initlen); +sds sdsnew(const char *init); +sds sdsempty(void); +sds sdsdup(const sds s); +void sdsfree(sds s); +sds sdsgrowzero(sds s, size_t len); +sds sdscatlen(sds s, const void *t, size_t len); +sds sdscat(sds s, const char *t); +sds sdscatsds(sds s, const sds t); +sds sdscpylen(sds s, const char *t, size_t len); +sds sdscpy(sds s, const char *t); + +sds sdscatvprintf(sds s, const char *fmt, va_list ap); +#ifdef __GNUC__ +sds sdscatprintf(sds s, const char *fmt, ...) + __attribute__((format(printf, 2, 3))); +#else +sds sdscatprintf(sds s, const char *fmt, ...); +#endif + +sds sdscatfmt(sds s, char const *fmt, ...); +sds sdstrim(sds s, const char *cset); +void sdsrange(sds s, int start, int end); +void sdsupdatelen(sds s); +void sdsclear(sds s); +int sdscmp(const sds s1, const sds s2); +sds *sdssplitlen(const char *s, int len, const char *sep, int seplen, int *count); +void sdsfreesplitres(sds *tokens, int count); +void sdstolower(sds s); +void sdstoupper(sds s); +sds sdsfromlonglong(long long value); +sds sdscatrepr(sds s, const char *p, size_t len); +sds *sdssplitargs(const char *line, int *argc); +sds sdsmapchars(sds s, const char *from, const char *to, size_t setlen); +sds sdsjoin(char **argv, int argc, char *sep); +sds sdsjoinsds(sds *argv, int argc, const char *sep, size_t seplen); + +/* Low level functions exposed to the user API */ +sds sdsMakeRoomFor(sds s, size_t addlen); +void sdsIncrLen(sds s, int incr); +sds sdsRemoveFreeSpace(sds s); +size_t sdsAllocSize(sds s); +void *sdsAllocPtr(sds s); + +/* Export the allocator used by SDS to the program using SDS. + * Sometimes the program SDS is linked to, may use a different set of + * allocators, but may want to allocate or free things that SDS will + * respectively free or allocate. */ +void *sds_malloc(size_t size); +void *sds_realloc(void *ptr, size_t size); +void sds_free(void *ptr); + +#ifdef REDIS_TEST +int sdsTest(int argc, char *argv[]); +#endif + +#endif diff --git a/controller/thirdparty/hiredis-0.14.1/sdsalloc.h b/controller/thirdparty/hiredis-0.14.1/sdsalloc.h new file mode 100644 index 000000000..f43023c48 --- /dev/null +++ b/controller/thirdparty/hiredis-0.14.1/sdsalloc.h @@ -0,0 +1,42 @@ +/* SDSLib 2.0 -- A C dynamic strings library + * + * Copyright (c) 2006-2015, Salvatore Sanfilippo + * Copyright (c) 2015, Oran Agra + * Copyright (c) 2015, Redis Labs, Inc + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * * Neither the name of Redis nor the names of its contributors may be used + * to endorse or promote products derived from this software without + * specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +/* SDS allocator selection. + * + * This file is used in order to change the SDS allocator at compile time. + * Just define the following defines to what you want to use. Also add + * the include of your alternate allocator if needed (not needed in order + * to use the default libc allocator). */ + +#define s_malloc malloc +#define s_realloc realloc +#define s_free free diff --git a/controller/thirdparty/hiredis-0.14.1/test.c b/controller/thirdparty/hiredis-0.14.1/test.c new file mode 100644 index 000000000..0f5bfe572 --- /dev/null +++ b/controller/thirdparty/hiredis-0.14.1/test.c @@ -0,0 +1,923 @@ +#include "fmacros.h" +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "hiredis.h" +#include "net.h" + +enum connection_type { + CONN_TCP, + CONN_UNIX, + CONN_FD +}; + +struct config { + enum connection_type type; + + struct { + const char *host; + int port; + struct timeval timeout; + } tcp; + + struct { + const char *path; + } unix_sock; +}; + +/* The following lines make up our testing "framework" :) */ +static int tests = 0, fails = 0; +#define test(_s) { printf("#%02d ", ++tests); printf(_s); } +#define test_cond(_c) if(_c) printf("\033[0;32mPASSED\033[0;0m\n"); else {printf("\033[0;31mFAILED\033[0;0m\n"); fails++;} + +static long long usec(void) { + struct timeval tv; + gettimeofday(&tv,NULL); + return (((long long)tv.tv_sec)*1000000)+tv.tv_usec; +} + +/* The assert() calls below have side effects, so we need assert() + * even if we are compiling without asserts (-DNDEBUG). */ +#ifdef NDEBUG +#undef assert +#define assert(e) (void)(e) +#endif + +static redisContext *select_database(redisContext *c) { + redisReply *reply; + + /* Switch to DB 9 for testing, now that we know we can chat. */ + reply = redisCommand(c,"SELECT 9"); + assert(reply != NULL); + freeReplyObject(reply); + + /* Make sure the DB is emtpy */ + reply = redisCommand(c,"DBSIZE"); + assert(reply != NULL); + if (reply->type == REDIS_REPLY_INTEGER && reply->integer == 0) { + /* Awesome, DB 9 is empty and we can continue. */ + freeReplyObject(reply); + } else { + printf("Database #9 is not empty, test can not continue\n"); + exit(1); + } + + return c; +} + +static int disconnect(redisContext *c, int keep_fd) { + redisReply *reply; + + /* Make sure we're on DB 9. */ + reply = redisCommand(c,"SELECT 9"); + assert(reply != NULL); + freeReplyObject(reply); + reply = redisCommand(c,"FLUSHDB"); + assert(reply != NULL); + freeReplyObject(reply); + + /* Free the context as well, but keep the fd if requested. */ + if (keep_fd) + return redisFreeKeepFd(c); + redisFree(c); + return -1; +} + +static redisContext *connect(struct config config) { + redisContext *c = NULL; + + if (config.type == CONN_TCP) { + c = redisConnect(config.tcp.host, config.tcp.port); + } else if (config.type == CONN_UNIX) { + c = redisConnectUnix(config.unix_sock.path); + } else if (config.type == CONN_FD) { + /* Create a dummy connection just to get an fd to inherit */ + redisContext *dummy_ctx = redisConnectUnix(config.unix_sock.path); + if (dummy_ctx) { + int fd = disconnect(dummy_ctx, 1); + printf("Connecting to inherited fd %d\n", fd); + c = redisConnectFd(fd); + } + } else { + assert(NULL); + } + + if (c == NULL) { + printf("Connection error: can't allocate redis context\n"); + exit(1); + } else if (c->err) { + printf("Connection error: %s\n", c->errstr); + redisFree(c); + exit(1); + } + + return select_database(c); +} + +static void test_format_commands(void) { + char *cmd; + int len; + + test("Format command without interpolation: "); + len = redisFormatCommand(&cmd,"SET foo bar"); + test_cond(strncmp(cmd,"*3\r\n$3\r\nSET\r\n$3\r\nfoo\r\n$3\r\nbar\r\n",len) == 0 && + len == 4+4+(3+2)+4+(3+2)+4+(3+2)); + free(cmd); + + test("Format command with %%s string interpolation: "); + len = redisFormatCommand(&cmd,"SET %s %s","foo","bar"); + test_cond(strncmp(cmd,"*3\r\n$3\r\nSET\r\n$3\r\nfoo\r\n$3\r\nbar\r\n",len) == 0 && + len == 4+4+(3+2)+4+(3+2)+4+(3+2)); + free(cmd); + + test("Format command with %%s and an empty string: "); + len = redisFormatCommand(&cmd,"SET %s %s","foo",""); + test_cond(strncmp(cmd,"*3\r\n$3\r\nSET\r\n$3\r\nfoo\r\n$0\r\n\r\n",len) == 0 && + len == 4+4+(3+2)+4+(3+2)+4+(0+2)); + free(cmd); + + test("Format command with an empty string in between proper interpolations: "); + len = redisFormatCommand(&cmd,"SET %s %s","","foo"); + test_cond(strncmp(cmd,"*3\r\n$3\r\nSET\r\n$0\r\n\r\n$3\r\nfoo\r\n",len) == 0 && + len == 4+4+(3+2)+4+(0+2)+4+(3+2)); + free(cmd); + + test("Format command with %%b string interpolation: "); + len = redisFormatCommand(&cmd,"SET %b %b","foo",(size_t)3,"b\0r",(size_t)3); + test_cond(strncmp(cmd,"*3\r\n$3\r\nSET\r\n$3\r\nfoo\r\n$3\r\nb\0r\r\n",len) == 0 && + len == 4+4+(3+2)+4+(3+2)+4+(3+2)); + free(cmd); + + test("Format command with %%b and an empty string: "); + len = redisFormatCommand(&cmd,"SET %b %b","foo",(size_t)3,"",(size_t)0); + test_cond(strncmp(cmd,"*3\r\n$3\r\nSET\r\n$3\r\nfoo\r\n$0\r\n\r\n",len) == 0 && + len == 4+4+(3+2)+4+(3+2)+4+(0+2)); + free(cmd); + + test("Format command with literal %%: "); + len = redisFormatCommand(&cmd,"SET %% %%"); + test_cond(strncmp(cmd,"*3\r\n$3\r\nSET\r\n$1\r\n%\r\n$1\r\n%\r\n",len) == 0 && + len == 4+4+(3+2)+4+(1+2)+4+(1+2)); + free(cmd); + + /* Vararg width depends on the type. These tests make sure that the + * width is correctly determined using the format and subsequent varargs + * can correctly be interpolated. */ +#define INTEGER_WIDTH_TEST(fmt, type) do { \ + type value = 123; \ + test("Format command with printf-delegation (" #type "): "); \ + len = redisFormatCommand(&cmd,"key:%08" fmt " str:%s", value, "hello"); \ + test_cond(strncmp(cmd,"*2\r\n$12\r\nkey:00000123\r\n$9\r\nstr:hello\r\n",len) == 0 && \ + len == 4+5+(12+2)+4+(9+2)); \ + free(cmd); \ +} while(0) + +#define FLOAT_WIDTH_TEST(type) do { \ + type value = 123.0; \ + test("Format command with printf-delegation (" #type "): "); \ + len = redisFormatCommand(&cmd,"key:%08.3f str:%s", value, "hello"); \ + test_cond(strncmp(cmd,"*2\r\n$12\r\nkey:0123.000\r\n$9\r\nstr:hello\r\n",len) == 0 && \ + len == 4+5+(12+2)+4+(9+2)); \ + free(cmd); \ +} while(0) + + INTEGER_WIDTH_TEST("d", int); + INTEGER_WIDTH_TEST("hhd", char); + INTEGER_WIDTH_TEST("hd", short); + INTEGER_WIDTH_TEST("ld", long); + INTEGER_WIDTH_TEST("lld", long long); + INTEGER_WIDTH_TEST("u", unsigned int); + INTEGER_WIDTH_TEST("hhu", unsigned char); + INTEGER_WIDTH_TEST("hu", unsigned short); + INTEGER_WIDTH_TEST("lu", unsigned long); + INTEGER_WIDTH_TEST("llu", unsigned long long); + FLOAT_WIDTH_TEST(float); + FLOAT_WIDTH_TEST(double); + + test("Format command with invalid printf format: "); + len = redisFormatCommand(&cmd,"key:%08p %b",(void*)1234,"foo",(size_t)3); + test_cond(len == -1); + + const char *argv[3]; + argv[0] = "SET"; + argv[1] = "foo\0xxx"; + argv[2] = "bar"; + size_t lens[3] = { 3, 7, 3 }; + int argc = 3; + + test("Format command by passing argc/argv without lengths: "); + len = redisFormatCommandArgv(&cmd,argc,argv,NULL); + test_cond(strncmp(cmd,"*3\r\n$3\r\nSET\r\n$3\r\nfoo\r\n$3\r\nbar\r\n",len) == 0 && + len == 4+4+(3+2)+4+(3+2)+4+(3+2)); + free(cmd); + + test("Format command by passing argc/argv with lengths: "); + len = redisFormatCommandArgv(&cmd,argc,argv,lens); + test_cond(strncmp(cmd,"*3\r\n$3\r\nSET\r\n$7\r\nfoo\0xxx\r\n$3\r\nbar\r\n",len) == 0 && + len == 4+4+(3+2)+4+(7+2)+4+(3+2)); + free(cmd); + + sds sds_cmd; + + sds_cmd = sdsempty(); + test("Format command into sds by passing argc/argv without lengths: "); + len = redisFormatSdsCommandArgv(&sds_cmd,argc,argv,NULL); + test_cond(strncmp(sds_cmd,"*3\r\n$3\r\nSET\r\n$3\r\nfoo\r\n$3\r\nbar\r\n",len) == 0 && + len == 4+4+(3+2)+4+(3+2)+4+(3+2)); + sdsfree(sds_cmd); + + sds_cmd = sdsempty(); + test("Format command into sds by passing argc/argv with lengths: "); + len = redisFormatSdsCommandArgv(&sds_cmd,argc,argv,lens); + test_cond(strncmp(sds_cmd,"*3\r\n$3\r\nSET\r\n$7\r\nfoo\0xxx\r\n$3\r\nbar\r\n",len) == 0 && + len == 4+4+(3+2)+4+(7+2)+4+(3+2)); + sdsfree(sds_cmd); +} + +static void test_append_formatted_commands(struct config config) { + redisContext *c; + redisReply *reply; + char *cmd; + int len; + + c = connect(config); + + test("Append format command: "); + + len = redisFormatCommand(&cmd, "SET foo bar"); + + test_cond(redisAppendFormattedCommand(c, cmd, len) == REDIS_OK); + + assert(redisGetReply(c, (void*)&reply) == REDIS_OK); + + free(cmd); + freeReplyObject(reply); + + disconnect(c, 0); +} + +static void test_reply_reader(void) { + redisReader *reader; + void *reply; + int ret; + int i; + + test("Error handling in reply parser: "); + reader = redisReaderCreate(); + redisReaderFeed(reader,(char*)"@foo\r\n",6); + ret = redisReaderGetReply(reader,NULL); + test_cond(ret == REDIS_ERR && + strcasecmp(reader->errstr,"Protocol error, got \"@\" as reply type byte") == 0); + redisReaderFree(reader); + + /* when the reply already contains multiple items, they must be free'd + * on an error. valgrind will bark when this doesn't happen. */ + test("Memory cleanup in reply parser: "); + reader = redisReaderCreate(); + redisReaderFeed(reader,(char*)"*2\r\n",4); + redisReaderFeed(reader,(char*)"$5\r\nhello\r\n",11); + redisReaderFeed(reader,(char*)"@foo\r\n",6); + ret = redisReaderGetReply(reader,NULL); + test_cond(ret == REDIS_ERR && + strcasecmp(reader->errstr,"Protocol error, got \"@\" as reply type byte") == 0); + redisReaderFree(reader); + + test("Set error on nested multi bulks with depth > 7: "); + reader = redisReaderCreate(); + + for (i = 0; i < 9; i++) { + redisReaderFeed(reader,(char*)"*1\r\n",4); + } + + ret = redisReaderGetReply(reader,NULL); + test_cond(ret == REDIS_ERR && + strncasecmp(reader->errstr,"No support for",14) == 0); + redisReaderFree(reader); + + test("Correctly parses LLONG_MAX: "); + reader = redisReaderCreate(); + redisReaderFeed(reader, ":9223372036854775807\r\n",22); + ret = redisReaderGetReply(reader,&reply); + test_cond(ret == REDIS_OK && + ((redisReply*)reply)->type == REDIS_REPLY_INTEGER && + ((redisReply*)reply)->integer == LLONG_MAX); + freeReplyObject(reply); + redisReaderFree(reader); + + test("Set error when > LLONG_MAX: "); + reader = redisReaderCreate(); + redisReaderFeed(reader, ":9223372036854775808\r\n",22); + ret = redisReaderGetReply(reader,&reply); + test_cond(ret == REDIS_ERR && + strcasecmp(reader->errstr,"Bad integer value") == 0); + freeReplyObject(reply); + redisReaderFree(reader); + + test("Correctly parses LLONG_MIN: "); + reader = redisReaderCreate(); + redisReaderFeed(reader, ":-9223372036854775808\r\n",23); + ret = redisReaderGetReply(reader,&reply); + test_cond(ret == REDIS_OK && + ((redisReply*)reply)->type == REDIS_REPLY_INTEGER && + ((redisReply*)reply)->integer == LLONG_MIN); + freeReplyObject(reply); + redisReaderFree(reader); + + test("Set error when < LLONG_MIN: "); + reader = redisReaderCreate(); + redisReaderFeed(reader, ":-9223372036854775809\r\n",23); + ret = redisReaderGetReply(reader,&reply); + test_cond(ret == REDIS_ERR && + strcasecmp(reader->errstr,"Bad integer value") == 0); + freeReplyObject(reply); + redisReaderFree(reader); + + test("Set error when array < -1: "); + reader = redisReaderCreate(); + redisReaderFeed(reader, "*-2\r\n+asdf\r\n",12); + ret = redisReaderGetReply(reader,&reply); + test_cond(ret == REDIS_ERR && + strcasecmp(reader->errstr,"Multi-bulk length out of range") == 0); + freeReplyObject(reply); + redisReaderFree(reader); + + test("Set error when bulk < -1: "); + reader = redisReaderCreate(); + redisReaderFeed(reader, "$-2\r\nasdf\r\n",11); + ret = redisReaderGetReply(reader,&reply); + test_cond(ret == REDIS_ERR && + strcasecmp(reader->errstr,"Bulk string length out of range") == 0); + freeReplyObject(reply); + redisReaderFree(reader); + + test("Set error when array > INT_MAX: "); + reader = redisReaderCreate(); + redisReaderFeed(reader, "*9223372036854775807\r\n+asdf\r\n",29); + ret = redisReaderGetReply(reader,&reply); + test_cond(ret == REDIS_ERR && + strcasecmp(reader->errstr,"Multi-bulk length out of range") == 0); + freeReplyObject(reply); + redisReaderFree(reader); + +#if LLONG_MAX > SIZE_MAX + test("Set error when bulk > SIZE_MAX: "); + reader = redisReaderCreate(); + redisReaderFeed(reader, "$9223372036854775807\r\nasdf\r\n",28); + ret = redisReaderGetReply(reader,&reply); + test_cond(ret == REDIS_ERR && + strcasecmp(reader->errstr,"Bulk string length out of range") == 0); + freeReplyObject(reply); + redisReaderFree(reader); +#endif + + test("Works with NULL functions for reply: "); + reader = redisReaderCreate(); + reader->fn = NULL; + redisReaderFeed(reader,(char*)"+OK\r\n",5); + ret = redisReaderGetReply(reader,&reply); + test_cond(ret == REDIS_OK && reply == (void*)REDIS_REPLY_STATUS); + redisReaderFree(reader); + + test("Works when a single newline (\\r\\n) covers two calls to feed: "); + reader = redisReaderCreate(); + reader->fn = NULL; + redisReaderFeed(reader,(char*)"+OK\r",4); + ret = redisReaderGetReply(reader,&reply); + assert(ret == REDIS_OK && reply == NULL); + redisReaderFeed(reader,(char*)"\n",1); + ret = redisReaderGetReply(reader,&reply); + test_cond(ret == REDIS_OK && reply == (void*)REDIS_REPLY_STATUS); + redisReaderFree(reader); + + test("Don't reset state after protocol error: "); + reader = redisReaderCreate(); + reader->fn = NULL; + redisReaderFeed(reader,(char*)"x",1); + ret = redisReaderGetReply(reader,&reply); + assert(ret == REDIS_ERR); + ret = redisReaderGetReply(reader,&reply); + test_cond(ret == REDIS_ERR && reply == NULL); + redisReaderFree(reader); + + /* Regression test for issue #45 on GitHub. */ + test("Don't do empty allocation for empty multi bulk: "); + reader = redisReaderCreate(); + redisReaderFeed(reader,(char*)"*0\r\n",4); + ret = redisReaderGetReply(reader,&reply); + test_cond(ret == REDIS_OK && + ((redisReply*)reply)->type == REDIS_REPLY_ARRAY && + ((redisReply*)reply)->elements == 0); + freeReplyObject(reply); + redisReaderFree(reader); +} + +static void test_free_null(void) { + void *redisCtx = NULL; + void *reply = NULL; + + test("Don't fail when redisFree is passed a NULL value: "); + redisFree(redisCtx); + test_cond(redisCtx == NULL); + + test("Don't fail when freeReplyObject is passed a NULL value: "); + freeReplyObject(reply); + test_cond(reply == NULL); +} + +static void test_blocking_connection_errors(void) { + redisContext *c; + + test("Returns error when host cannot be resolved: "); + c = redisConnect((char*)"idontexist.test", 6379); + test_cond(c->err == REDIS_ERR_OTHER && + (strcmp(c->errstr,"Name or service not known") == 0 || + strcmp(c->errstr,"Can't resolve: idontexist.test") == 0 || + strcmp(c->errstr,"nodename nor servname provided, or not known") == 0 || + strcmp(c->errstr,"No address associated with hostname") == 0 || + strcmp(c->errstr,"Temporary failure in name resolution") == 0 || + strcmp(c->errstr,"hostname nor servname provided, or not known") == 0 || + strcmp(c->errstr,"no address associated with name") == 0)); + redisFree(c); + + test("Returns error when the port is not open: "); + c = redisConnect((char*)"localhost", 1); + test_cond(c->err == REDIS_ERR_IO && + strcmp(c->errstr,"Connection refused") == 0); + redisFree(c); + + test("Returns error when the unix_sock socket path doesn't accept connections: "); + c = redisConnectUnix((char*)"/tmp/idontexist.sock"); + test_cond(c->err == REDIS_ERR_IO); /* Don't care about the message... */ + redisFree(c); +} + +static void test_blocking_connection(struct config config) { + redisContext *c; + redisReply *reply; + + c = connect(config); + + test("Is able to deliver commands: "); + reply = redisCommand(c,"PING"); + test_cond(reply->type == REDIS_REPLY_STATUS && + strcasecmp(reply->str,"pong") == 0) + freeReplyObject(reply); + + test("Is a able to send commands verbatim: "); + reply = redisCommand(c,"SET foo bar"); + test_cond (reply->type == REDIS_REPLY_STATUS && + strcasecmp(reply->str,"ok") == 0) + freeReplyObject(reply); + + test("%%s String interpolation works: "); + reply = redisCommand(c,"SET %s %s","foo","hello world"); + freeReplyObject(reply); + reply = redisCommand(c,"GET foo"); + test_cond(reply->type == REDIS_REPLY_STRING && + strcmp(reply->str,"hello world") == 0); + freeReplyObject(reply); + + test("%%b String interpolation works: "); + reply = redisCommand(c,"SET %b %b","foo",(size_t)3,"hello\x00world",(size_t)11); + freeReplyObject(reply); + reply = redisCommand(c,"GET foo"); + test_cond(reply->type == REDIS_REPLY_STRING && + memcmp(reply->str,"hello\x00world",11) == 0) + + test("Binary reply length is correct: "); + test_cond(reply->len == 11) + freeReplyObject(reply); + + test("Can parse nil replies: "); + reply = redisCommand(c,"GET nokey"); + test_cond(reply->type == REDIS_REPLY_NIL) + freeReplyObject(reply); + + /* test 7 */ + test("Can parse integer replies: "); + reply = redisCommand(c,"INCR mycounter"); + test_cond(reply->type == REDIS_REPLY_INTEGER && reply->integer == 1) + freeReplyObject(reply); + + test("Can parse multi bulk replies: "); + freeReplyObject(redisCommand(c,"LPUSH mylist foo")); + freeReplyObject(redisCommand(c,"LPUSH mylist bar")); + reply = redisCommand(c,"LRANGE mylist 0 -1"); + test_cond(reply->type == REDIS_REPLY_ARRAY && + reply->elements == 2 && + !memcmp(reply->element[0]->str,"bar",3) && + !memcmp(reply->element[1]->str,"foo",3)) + freeReplyObject(reply); + + /* m/e with multi bulk reply *before* other reply. + * specifically test ordering of reply items to parse. */ + test("Can handle nested multi bulk replies: "); + freeReplyObject(redisCommand(c,"MULTI")); + freeReplyObject(redisCommand(c,"LRANGE mylist 0 -1")); + freeReplyObject(redisCommand(c,"PING")); + reply = (redisCommand(c,"EXEC")); + test_cond(reply->type == REDIS_REPLY_ARRAY && + reply->elements == 2 && + reply->element[0]->type == REDIS_REPLY_ARRAY && + reply->element[0]->elements == 2 && + !memcmp(reply->element[0]->element[0]->str,"bar",3) && + !memcmp(reply->element[0]->element[1]->str,"foo",3) && + reply->element[1]->type == REDIS_REPLY_STATUS && + strcasecmp(reply->element[1]->str,"pong") == 0); + freeReplyObject(reply); + + disconnect(c, 0); +} + +static void test_blocking_connection_timeouts(struct config config) { + redisContext *c; + redisReply *reply; + ssize_t s; + const char *cmd = "DEBUG SLEEP 3\r\n"; + struct timeval tv; + + c = connect(config); + test("Successfully completes a command when the timeout is not exceeded: "); + reply = redisCommand(c,"SET foo fast"); + freeReplyObject(reply); + tv.tv_sec = 0; + tv.tv_usec = 10000; + redisSetTimeout(c, tv); + reply = redisCommand(c, "GET foo"); + test_cond(reply != NULL && reply->type == REDIS_REPLY_STRING && memcmp(reply->str, "fast", 4) == 0); + freeReplyObject(reply); + disconnect(c, 0); + + c = connect(config); + test("Does not return a reply when the command times out: "); + s = write(c->fd, cmd, strlen(cmd)); + tv.tv_sec = 0; + tv.tv_usec = 10000; + redisSetTimeout(c, tv); + reply = redisCommand(c, "GET foo"); + test_cond(s > 0 && reply == NULL && c->err == REDIS_ERR_IO && strcmp(c->errstr, "Resource temporarily unavailable") == 0); + freeReplyObject(reply); + + test("Reconnect properly reconnects after a timeout: "); + redisReconnect(c); + reply = redisCommand(c, "PING"); + test_cond(reply != NULL && reply->type == REDIS_REPLY_STATUS && strcmp(reply->str, "PONG") == 0); + freeReplyObject(reply); + + test("Reconnect properly uses owned parameters: "); + config.tcp.host = "foo"; + config.unix_sock.path = "foo"; + redisReconnect(c); + reply = redisCommand(c, "PING"); + test_cond(reply != NULL && reply->type == REDIS_REPLY_STATUS && strcmp(reply->str, "PONG") == 0); + freeReplyObject(reply); + + disconnect(c, 0); +} + +static void test_blocking_io_errors(struct config config) { + redisContext *c; + redisReply *reply; + void *_reply; + int major, minor; + + /* Connect to target given by config. */ + c = connect(config); + { + /* Find out Redis version to determine the path for the next test */ + const char *field = "redis_version:"; + char *p, *eptr; + + reply = redisCommand(c,"INFO"); + p = strstr(reply->str,field); + major = strtol(p+strlen(field),&eptr,10); + p = eptr+1; /* char next to the first "." */ + minor = strtol(p,&eptr,10); + freeReplyObject(reply); + } + + test("Returns I/O error when the connection is lost: "); + reply = redisCommand(c,"QUIT"); + if (major > 2 || (major == 2 && minor > 0)) { + /* > 2.0 returns OK on QUIT and read() should be issued once more + * to know the descriptor is at EOF. */ + test_cond(strcasecmp(reply->str,"OK") == 0 && + redisGetReply(c,&_reply) == REDIS_ERR); + freeReplyObject(reply); + } else { + test_cond(reply == NULL); + } + + /* On 2.0, QUIT will cause the connection to be closed immediately and + * the read(2) for the reply on QUIT will set the error to EOF. + * On >2.0, QUIT will return with OK and another read(2) needed to be + * issued to find out the socket was closed by the server. In both + * conditions, the error will be set to EOF. */ + assert(c->err == REDIS_ERR_EOF && + strcmp(c->errstr,"Server closed the connection") == 0); + redisFree(c); + + c = connect(config); + test("Returns I/O error on socket timeout: "); + struct timeval tv = { 0, 1000 }; + assert(redisSetTimeout(c,tv) == REDIS_OK); + test_cond(redisGetReply(c,&_reply) == REDIS_ERR && + c->err == REDIS_ERR_IO && errno == EAGAIN); + redisFree(c); +} + +static void test_invalid_timeout_errors(struct config config) { + redisContext *c; + + test("Set error when an invalid timeout usec value is given to redisConnectWithTimeout: "); + + config.tcp.timeout.tv_sec = 0; + config.tcp.timeout.tv_usec = 10000001; + + c = redisConnectWithTimeout(config.tcp.host, config.tcp.port, config.tcp.timeout); + + test_cond(c->err == REDIS_ERR_IO && strcmp(c->errstr, "Invalid timeout specified") == 0); + redisFree(c); + + test("Set error when an invalid timeout sec value is given to redisConnectWithTimeout: "); + + config.tcp.timeout.tv_sec = (((LONG_MAX) - 999) / 1000) + 1; + config.tcp.timeout.tv_usec = 0; + + c = redisConnectWithTimeout(config.tcp.host, config.tcp.port, config.tcp.timeout); + + test_cond(c->err == REDIS_ERR_IO && strcmp(c->errstr, "Invalid timeout specified") == 0); + redisFree(c); +} + +static void test_throughput(struct config config) { + redisContext *c = connect(config); + redisReply **replies; + int i, num; + long long t1, t2; + + test("Throughput:\n"); + for (i = 0; i < 500; i++) + freeReplyObject(redisCommand(c,"LPUSH mylist foo")); + + num = 1000; + replies = malloc(sizeof(redisReply*)*num); + t1 = usec(); + for (i = 0; i < num; i++) { + replies[i] = redisCommand(c,"PING"); + assert(replies[i] != NULL && replies[i]->type == REDIS_REPLY_STATUS); + } + t2 = usec(); + for (i = 0; i < num; i++) freeReplyObject(replies[i]); + free(replies); + printf("\t(%dx PING: %.3fs)\n", num, (t2-t1)/1000000.0); + + replies = malloc(sizeof(redisReply*)*num); + t1 = usec(); + for (i = 0; i < num; i++) { + replies[i] = redisCommand(c,"LRANGE mylist 0 499"); + assert(replies[i] != NULL && replies[i]->type == REDIS_REPLY_ARRAY); + assert(replies[i] != NULL && replies[i]->elements == 500); + } + t2 = usec(); + for (i = 0; i < num; i++) freeReplyObject(replies[i]); + free(replies); + printf("\t(%dx LRANGE with 500 elements: %.3fs)\n", num, (t2-t1)/1000000.0); + + replies = malloc(sizeof(redisReply*)*num); + t1 = usec(); + for (i = 0; i < num; i++) { + replies[i] = redisCommand(c, "INCRBY incrkey %d", 1000000); + assert(replies[i] != NULL && replies[i]->type == REDIS_REPLY_INTEGER); + } + t2 = usec(); + for (i = 0; i < num; i++) freeReplyObject(replies[i]); + free(replies); + printf("\t(%dx INCRBY: %.3fs)\n", num, (t2-t1)/1000000.0); + + num = 10000; + replies = malloc(sizeof(redisReply*)*num); + for (i = 0; i < num; i++) + redisAppendCommand(c,"PING"); + t1 = usec(); + for (i = 0; i < num; i++) { + assert(redisGetReply(c, (void*)&replies[i]) == REDIS_OK); + assert(replies[i] != NULL && replies[i]->type == REDIS_REPLY_STATUS); + } + t2 = usec(); + for (i = 0; i < num; i++) freeReplyObject(replies[i]); + free(replies); + printf("\t(%dx PING (pipelined): %.3fs)\n", num, (t2-t1)/1000000.0); + + replies = malloc(sizeof(redisReply*)*num); + for (i = 0; i < num; i++) + redisAppendCommand(c,"LRANGE mylist 0 499"); + t1 = usec(); + for (i = 0; i < num; i++) { + assert(redisGetReply(c, (void*)&replies[i]) == REDIS_OK); + assert(replies[i] != NULL && replies[i]->type == REDIS_REPLY_ARRAY); + assert(replies[i] != NULL && replies[i]->elements == 500); + } + t2 = usec(); + for (i = 0; i < num; i++) freeReplyObject(replies[i]); + free(replies); + printf("\t(%dx LRANGE with 500 elements (pipelined): %.3fs)\n", num, (t2-t1)/1000000.0); + + replies = malloc(sizeof(redisReply*)*num); + for (i = 0; i < num; i++) + redisAppendCommand(c,"INCRBY incrkey %d", 1000000); + t1 = usec(); + for (i = 0; i < num; i++) { + assert(redisGetReply(c, (void*)&replies[i]) == REDIS_OK); + assert(replies[i] != NULL && replies[i]->type == REDIS_REPLY_INTEGER); + } + t2 = usec(); + for (i = 0; i < num; i++) freeReplyObject(replies[i]); + free(replies); + printf("\t(%dx INCRBY (pipelined): %.3fs)\n", num, (t2-t1)/1000000.0); + + disconnect(c, 0); +} + +// static long __test_callback_flags = 0; +// static void __test_callback(redisContext *c, void *privdata) { +// ((void)c); +// /* Shift to detect execution order */ +// __test_callback_flags <<= 8; +// __test_callback_flags |= (long)privdata; +// } +// +// static void __test_reply_callback(redisContext *c, redisReply *reply, void *privdata) { +// ((void)c); +// /* Shift to detect execution order */ +// __test_callback_flags <<= 8; +// __test_callback_flags |= (long)privdata; +// if (reply) freeReplyObject(reply); +// } +// +// static redisContext *__connect_nonblock() { +// /* Reset callback flags */ +// __test_callback_flags = 0; +// return redisConnectNonBlock("127.0.0.1", port, NULL); +// } +// +// static void test_nonblocking_connection() { +// redisContext *c; +// int wdone = 0; +// +// test("Calls command callback when command is issued: "); +// c = __connect_nonblock(); +// redisSetCommandCallback(c,__test_callback,(void*)1); +// redisCommand(c,"PING"); +// test_cond(__test_callback_flags == 1); +// redisFree(c); +// +// test("Calls disconnect callback on redisDisconnect: "); +// c = __connect_nonblock(); +// redisSetDisconnectCallback(c,__test_callback,(void*)2); +// redisDisconnect(c); +// test_cond(__test_callback_flags == 2); +// redisFree(c); +// +// test("Calls disconnect callback and free callback on redisFree: "); +// c = __connect_nonblock(); +// redisSetDisconnectCallback(c,__test_callback,(void*)2); +// redisSetFreeCallback(c,__test_callback,(void*)4); +// redisFree(c); +// test_cond(__test_callback_flags == ((2 << 8) | 4)); +// +// test("redisBufferWrite against empty write buffer: "); +// c = __connect_nonblock(); +// test_cond(redisBufferWrite(c,&wdone) == REDIS_OK && wdone == 1); +// redisFree(c); +// +// test("redisBufferWrite against not yet connected fd: "); +// c = __connect_nonblock(); +// redisCommand(c,"PING"); +// test_cond(redisBufferWrite(c,NULL) == REDIS_ERR && +// strncmp(c->error,"write:",6) == 0); +// redisFree(c); +// +// test("redisBufferWrite against closed fd: "); +// c = __connect_nonblock(); +// redisCommand(c,"PING"); +// redisDisconnect(c); +// test_cond(redisBufferWrite(c,NULL) == REDIS_ERR && +// strncmp(c->error,"write:",6) == 0); +// redisFree(c); +// +// test("Process callbacks in the right sequence: "); +// c = __connect_nonblock(); +// redisCommandWithCallback(c,__test_reply_callback,(void*)1,"PING"); +// redisCommandWithCallback(c,__test_reply_callback,(void*)2,"PING"); +// redisCommandWithCallback(c,__test_reply_callback,(void*)3,"PING"); +// +// /* Write output buffer */ +// wdone = 0; +// while(!wdone) { +// usleep(500); +// redisBufferWrite(c,&wdone); +// } +// +// /* Read until at least one callback is executed (the 3 replies will +// * arrive in a single packet, causing all callbacks to be executed in +// * a single pass). */ +// while(__test_callback_flags == 0) { +// assert(redisBufferRead(c) == REDIS_OK); +// redisProcessCallbacks(c); +// } +// test_cond(__test_callback_flags == 0x010203); +// redisFree(c); +// +// test("redisDisconnect executes pending callbacks with NULL reply: "); +// c = __connect_nonblock(); +// redisSetDisconnectCallback(c,__test_callback,(void*)1); +// redisCommandWithCallback(c,__test_reply_callback,(void*)2,"PING"); +// redisDisconnect(c); +// test_cond(__test_callback_flags == 0x0201); +// redisFree(c); +// } + +int main(int argc, char **argv) { + struct config cfg = { + .tcp = { + .host = "127.0.0.1", + .port = 6379 + }, + .unix_sock = { + .path = "/tmp/redis.sock" + } + }; + int throughput = 1; + int test_inherit_fd = 1; + + /* Ignore broken pipe signal (for I/O error tests). */ + signal(SIGPIPE, SIG_IGN); + + /* Parse command line options. */ + argv++; argc--; + while (argc) { + if (argc >= 2 && !strcmp(argv[0],"-h")) { + argv++; argc--; + cfg.tcp.host = argv[0]; + } else if (argc >= 2 && !strcmp(argv[0],"-p")) { + argv++; argc--; + cfg.tcp.port = atoi(argv[0]); + } else if (argc >= 2 && !strcmp(argv[0],"-s")) { + argv++; argc--; + cfg.unix_sock.path = argv[0]; + } else if (argc >= 1 && !strcmp(argv[0],"--skip-throughput")) { + throughput = 0; + } else if (argc >= 1 && !strcmp(argv[0],"--skip-inherit-fd")) { + test_inherit_fd = 0; + } else { + fprintf(stderr, "Invalid argument: %s\n", argv[0]); + exit(1); + } + argv++; argc--; + } + + test_format_commands(); + test_reply_reader(); + test_blocking_connection_errors(); + test_free_null(); + + printf("\nTesting against TCP connection (%s:%d):\n", cfg.tcp.host, cfg.tcp.port); + cfg.type = CONN_TCP; + test_blocking_connection(cfg); + test_blocking_connection_timeouts(cfg); + test_blocking_io_errors(cfg); + test_invalid_timeout_errors(cfg); + test_append_formatted_commands(cfg); + if (throughput) test_throughput(cfg); + + printf("\nTesting against Unix socket connection (%s):\n", cfg.unix_sock.path); + cfg.type = CONN_UNIX; + test_blocking_connection(cfg); + test_blocking_connection_timeouts(cfg); + test_blocking_io_errors(cfg); + if (throughput) test_throughput(cfg); + + if (test_inherit_fd) { + printf("\nTesting against inherited fd (%s):\n", cfg.unix_sock.path); + cfg.type = CONN_FD; + test_blocking_connection(cfg); + } + + + if (fails) { + printf("*** %d TESTS FAILED ***\n", fails); + return 1; + } + + printf("ALL TESTS PASSED\n"); + return 0; +} diff --git a/controller/thirdparty/hiredis-0.14.1/win32.h b/controller/thirdparty/hiredis-0.14.1/win32.h new file mode 100644 index 000000000..1a27c18f2 --- /dev/null +++ b/controller/thirdparty/hiredis-0.14.1/win32.h @@ -0,0 +1,42 @@ +#ifndef _WIN32_HELPER_INCLUDE +#define _WIN32_HELPER_INCLUDE +#ifdef _MSC_VER + +#ifndef inline +#define inline __inline +#endif + +#ifndef va_copy +#define va_copy(d,s) ((d) = (s)) +#endif + +#ifndef snprintf +#define snprintf c99_snprintf + +__inline int c99_vsnprintf(char* str, size_t size, const char* format, va_list ap) +{ + int count = -1; + + if (size != 0) + count = _vsnprintf_s(str, size, _TRUNCATE, format, ap); + if (count == -1) + count = _vscprintf(format, ap); + + return count; +} + +__inline int c99_snprintf(char* str, size_t size, const char* format, ...) +{ + int count; + va_list ap; + + va_start(ap, format); + count = c99_vsnprintf(str, size, format, ap); + va_end(ap); + + return count; +} +#endif + +#endif +#endif \ No newline at end of file diff --git a/controller/thirdparty/redis-plus-plus-1.1.1/.gitignore b/controller/thirdparty/redis-plus-plus-1.1.1/.gitignore new file mode 100644 index 000000000..259148fa1 --- /dev/null +++ b/controller/thirdparty/redis-plus-plus-1.1.1/.gitignore @@ -0,0 +1,32 @@ +# Prerequisites +*.d + +# Compiled Object files +*.slo +*.lo +*.o +*.obj + +# Precompiled Headers +*.gch +*.pch + +# Compiled Dynamic libraries +*.so +*.dylib +*.dll + +# Fortran module files +*.mod +*.smod + +# Compiled Static libraries +*.lai +*.la +*.a +*.lib + +# Executables +*.exe +*.out +*.app diff --git a/controller/thirdparty/redis-plus-plus-1.1.1/CMakeLists.txt b/controller/thirdparty/redis-plus-plus-1.1.1/CMakeLists.txt new file mode 100644 index 000000000..fb160b5df --- /dev/null +++ b/controller/thirdparty/redis-plus-plus-1.1.1/CMakeLists.txt @@ -0,0 +1,50 @@ +project(redis++) + +if (${CMAKE_SYSTEM_NAME} MATCHES "Darwin") + cmake_minimum_required(VERSION 3.0.0) +else() + cmake_minimum_required(VERSION 2.8.0) +endif() + +set(CMAKE_CXX_FLAGS "-std=c++11 -Wall -W -Werror -fPIC") + +set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib) + +set(PROJECT_SOURCE_DIR ${PROJECT_SOURCE_DIR}/src/sw/redis++) + +file(GLOB PROJECT_SOURCE_FILES "${PROJECT_SOURCE_DIR}/*.cpp") + +set(STATIC_LIB static) +set(SHARED_LIB shared) + +add_library(${STATIC_LIB} STATIC ${PROJECT_SOURCE_FILES}) +add_library(${SHARED_LIB} SHARED ${PROJECT_SOURCE_FILES}) + +# hiredis dependency +find_path(HIREDIS_HEADER hiredis) +target_include_directories(${STATIC_LIB} PUBLIC ${HIREDIS_HEADER}) +target_include_directories(${SHARED_LIB} PUBLIC ${HIREDIS_HEADER}) + +find_library(HIREDIS_LIB hiredis) +target_link_libraries(${SHARED_LIB} ${HIREDIS_LIB}) + +set_target_properties(${STATIC_LIB} PROPERTIES OUTPUT_NAME ${PROJECT_NAME}) +set_target_properties(${SHARED_LIB} PROPERTIES OUTPUT_NAME ${PROJECT_NAME}) + +set_target_properties(${STATIC_LIB} PROPERTIES CLEAN_DIRECT_OUTPUT 1) +set_target_properties(${SHARED_LIB} PROPERTIES CLEAN_DIRECT_OUTPUT 1) + +add_subdirectory(test) + +# Install static lib. +install(TARGETS ${STATIC_LIB} + ARCHIVE DESTINATION lib) + +# Install shared lib. +install(TARGETS ${SHARED_LIB} + LIBRARY DESTINATION lib) + +#Install headers. +set(HEADER_PATH "sw/redis++") +file(GLOB HEADERS "${PROJECT_SOURCE_DIR}/*.h*") +install(FILES ${HEADERS} DESTINATION ${CMAKE_INSTALL_PREFIX}/include/${HEADER_PATH}) diff --git a/controller/thirdparty/redis-plus-plus-1.1.1/LICENSE b/controller/thirdparty/redis-plus-plus-1.1.1/LICENSE new file mode 100644 index 000000000..261eeb9e9 --- /dev/null +++ b/controller/thirdparty/redis-plus-plus-1.1.1/LICENSE @@ -0,0 +1,201 @@ + Apache License + Version 2.0, January 2004 + http://www.apache.org/licenses/ + + TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + + 1. Definitions. + + "License" shall mean the terms and conditions for use, reproduction, + and distribution as defined by Sections 1 through 9 of this document. + + "Licensor" shall mean the copyright owner or entity authorized by + the copyright owner that is granting the License. + + "Legal Entity" shall mean the union of the acting entity and all + other entities that control, are controlled by, or are under common + control with that entity. For the purposes of this definition, + "control" means (i) the power, direct or indirect, to cause the + direction or management of such entity, whether by contract or + otherwise, or (ii) ownership of fifty percent (50%) or more of the + outstanding shares, or (iii) beneficial ownership of such entity. + + "You" (or "Your") shall mean an individual or Legal Entity + exercising permissions granted by this License. + + "Source" form shall mean the preferred form for making modifications, + including but not limited to software source code, documentation + source, and configuration files. + + "Object" form shall mean any form resulting from mechanical + transformation or translation of a Source form, including but + not limited to compiled object code, generated documentation, + and conversions to other media types. + + "Work" shall mean the work of authorship, whether in Source or + Object form, made available under the License, as indicated by a + copyright notice that is included in or attached to the work + (an example is provided in the Appendix below). + + "Derivative Works" shall mean any work, whether in Source or Object + form, that is based on (or derived from) the Work and for which the + editorial revisions, annotations, elaborations, or other modifications + represent, as a whole, an original work of authorship. For the purposes + of this License, Derivative Works shall not include works that remain + separable from, or merely link (or bind by name) to the interfaces of, + the Work and Derivative Works thereof. + + "Contribution" shall mean any work of authorship, including + the original version of the Work and any modifications or additions + to that Work or Derivative Works thereof, that is intentionally + submitted to Licensor for inclusion in the Work by the copyright owner + or by an individual or Legal Entity authorized to submit on behalf of + the copyright owner. For the purposes of this definition, "submitted" + means any form of electronic, verbal, or written communication sent + to the Licensor or its representatives, including but not limited to + communication on electronic mailing lists, source code control systems, + and issue tracking systems that are managed by, or on behalf of, the + Licensor for the purpose of discussing and improving the Work, but + excluding communication that is conspicuously marked or otherwise + designated in writing by the copyright owner as "Not a Contribution." + + "Contributor" shall mean Licensor and any individual or Legal Entity + on behalf of whom a Contribution has been received by Licensor and + subsequently incorporated within the Work. + + 2. Grant of Copyright License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + copyright license to reproduce, prepare Derivative Works of, + publicly display, publicly perform, sublicense, and distribute the + Work and such Derivative Works in Source or Object form. + + 3. Grant of Patent License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + (except as stated in this section) patent license to make, have made, + use, offer to sell, sell, import, and otherwise transfer the Work, + where such license applies only to those patent claims licensable + by such Contributor that are necessarily infringed by their + Contribution(s) alone or by combination of their Contribution(s) + with the Work to which such Contribution(s) was submitted. If You + institute patent litigation against any entity (including a + cross-claim or counterclaim in a lawsuit) alleging that the Work + or a Contribution incorporated within the Work constitutes direct + or contributory patent infringement, then any patent licenses + granted to You under this License for that Work shall terminate + as of the date such litigation is filed. + + 4. Redistribution. You may reproduce and distribute copies of the + Work or Derivative Works thereof in any medium, with or without + modifications, and in Source or Object form, provided that You + meet the following conditions: + + (a) You must give any other recipients of the Work or + Derivative Works a copy of this License; and + + (b) You must cause any modified files to carry prominent notices + stating that You changed the files; and + + (c) You must retain, in the Source form of any Derivative Works + that You distribute, all copyright, patent, trademark, and + attribution notices from the Source form of the Work, + excluding those notices that do not pertain to any part of + the Derivative Works; and + + (d) If the Work includes a "NOTICE" text file as part of its + distribution, then any Derivative Works that You distribute must + include a readable copy of the attribution notices contained + within such NOTICE file, excluding those notices that do not + pertain to any part of the Derivative Works, in at least one + of the following places: within a NOTICE text file distributed + as part of the Derivative Works; within the Source form or + documentation, if provided along with the Derivative Works; or, + within a display generated by the Derivative Works, if and + wherever such third-party notices normally appear. The contents + of the NOTICE file are for informational purposes only and + do not modify the License. You may add Your own attribution + notices within Derivative Works that You distribute, alongside + or as an addendum to the NOTICE text from the Work, provided + that such additional attribution notices cannot be construed + as modifying the License. + + You may add Your own copyright statement to Your modifications and + may provide additional or different license terms and conditions + for use, reproduction, or distribution of Your modifications, or + for any such Derivative Works as a whole, provided Your use, + reproduction, and distribution of the Work otherwise complies with + the conditions stated in this License. + + 5. Submission of Contributions. Unless You explicitly state otherwise, + any Contribution intentionally submitted for inclusion in the Work + by You to the Licensor shall be under the terms and conditions of + this License, without any additional terms or conditions. + Notwithstanding the above, nothing herein shall supersede or modify + the terms of any separate license agreement you may have executed + with Licensor regarding such Contributions. + + 6. Trademarks. This License does not grant permission to use the trade + names, trademarks, service marks, or product names of the Licensor, + except as required for reasonable and customary use in describing the + origin of the Work and reproducing the content of the NOTICE file. + + 7. Disclaimer of Warranty. Unless required by applicable law or + agreed to in writing, Licensor provides the Work (and each + Contributor provides its Contributions) on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + implied, including, without limitation, any warranties or conditions + of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A + PARTICULAR PURPOSE. You are solely responsible for determining the + appropriateness of using or redistributing the Work and assume any + risks associated with Your exercise of permissions under this License. + + 8. Limitation of Liability. In no event and under no legal theory, + whether in tort (including negligence), contract, or otherwise, + unless required by applicable law (such as deliberate and grossly + negligent acts) or agreed to in writing, shall any Contributor be + liable to You for damages, including any direct, indirect, special, + incidental, or consequential damages of any character arising as a + result of this License or out of the use or inability to use the + Work (including but not limited to damages for loss of goodwill, + work stoppage, computer failure or malfunction, or any and all + other commercial damages or losses), even if such Contributor + has been advised of the possibility of such damages. + + 9. Accepting Warranty or Additional Liability. While redistributing + the Work or Derivative Works thereof, You may choose to offer, + and charge a fee for, acceptance of support, warranty, indemnity, + or other liability obligations and/or rights consistent with this + License. However, in accepting such obligations, You may act only + on Your own behalf and on Your sole responsibility, not on behalf + of any other Contributor, and only if You agree to indemnify, + defend, and hold each Contributor harmless for any liability + incurred by, or claims asserted against, such Contributor by reason + of your accepting any such warranty or additional liability. + + END OF TERMS AND CONDITIONS + + APPENDIX: How to apply the Apache License to your work. + + To apply the Apache License to your work, attach the following + boilerplate notice, with the fields enclosed by brackets "[]" + replaced with your own identifying information. (Don't include + the brackets!) The text should be enclosed in the appropriate + comment syntax for the file format. We also recommend that a + file or class name and description of purpose be included on the + same "printed page" as the copyright notice for easier + identification within third-party archives. + + Copyright [yyyy] [name of copyright owner] + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. diff --git a/controller/thirdparty/redis-plus-plus-1.1.1/README.md b/controller/thirdparty/redis-plus-plus-1.1.1/README.md new file mode 100644 index 000000000..8d2fd7d28 --- /dev/null +++ b/controller/thirdparty/redis-plus-plus-1.1.1/README.md @@ -0,0 +1,1776 @@ +# redis-plus-plus + +- [Overview](#overview) + - [Features](#features) +- [Installation](#installation) + - [Install hiredis](#install-hiredis) + - [Install redis-plus-plus](#install-redis-plus-plus) + - [Run Tests (Optional)](#run-tests-optional) + - [Use redis-plus-plus In Your Project](#use-redis-plus-plus-in-your-project) +- [Getting Started](#getting-started) +- [API Reference](#api-reference) + - [Connection](#connection) + - [Send Command to Redis Server](#send-command-to-redis-server) + - [Generic Command Interface](#generic-command-interface) + - [Publish/Subscribe](#publishsubscribe) + - [Pipeline](#pipeline) + - [Transaction](#transaction) + - [Redis Cluster](#redis-cluster) + - [Redis Sentinel](#redis-sentinel) + - [Redis Stream](#redis-stream) +- [Author](#author) + +## Overview + +This is a C++ client for Redis. It's based on [hiredis](https://github.com/redis/hiredis), and written in C++ 11. + +**NOTE**: I'm not a native speaker. So if the documentation is unclear, please feel free to open an issue or pull request. I'll response ASAP. + +### Features +- Most commands for Redis. +- Connection pool. +- Redis scripting. +- Thread safe unless otherwise stated. +- Redis publish/subscribe. +- Redis pipeline. +- Redis transaction. +- Redis Cluster. +- Redis Sentinel. +- STL-like interfaces. +- Generic command interface. + +## Installation + +### Install hiredis + +Since *redis-plus-plus* is based on *hiredis*, you should install *hiredis* first. The minimum version requirement for *hiredis* is **v0.12.1**, and you'd better use the latest release of *hiredis*. + +``` +git clone https://github.com/redis/hiredis.git + +cd hiredis + +make + +make install +``` + +By default, *hiredis* is installed at */usr/local*. If you want to install *hiredis* at non-default location, use the following commands to specify the installation path. + +``` +make PREFIX=/non/default/path + +make PREFIX=/non/default/path install +``` + +### Install redis-plus-plus + +*redis-plus-plus* is built with [CMAKE](https://cmake.org). + +``` +git clone https://github.com/sewenew/redis-plus-plus.git + +cd redis-plus-plus + +mkdir compile + +cd compile + +cmake -DCMAKE_BUILD_TYPE=Release .. + +make + +make install + +cd .. +``` + +If *hiredis* is installed at non-default location, you should use `CMAKE_PREFIX_PATH` to specify the installation path of *hiredis*. By default, *redis-plus-plus* is installed at */usr/local*. However, you can use `CMAKE_INSTALL_PREFIX` to install *redis-plus-plus* at non-default location. + +``` +cmake -DCMAKE_BUILD_TYPE=Release -DCMAKE_PREFIX_PATH=/path/to/hiredis -DCMAKE_INSTALL_PREFIX=/path/to/install/redis-plus-plus .. +``` + +### Run Tests (Optional) + +*redis-plus-plus* has been fully tested with the following compilers: + +``` +gcc version 4.8.5 20150623 (Red Hat 4.8.5-39) (GCC) +gcc version 5.5.0 20171010 (Ubuntu 5.5.0-12ubuntu1) +gcc version 6.5.0 20181026 (Ubuntu 6.5.0-2ubuntu1~18.04) +gcc version 7.4.0 (Ubuntu 7.4.0-1ubuntu1~18.04.1) +gcc version 8.3.0 (Ubuntu 8.3.0-6ubuntu1~18.04.1) +clang version 3.9.1-19ubuntu1 (tags/RELEASE_391/rc2) +clang version 4.0.1-10 (tags/RELEASE_401/final) +clang version 5.0.1-4 (tags/RELEASE_501/final) +clang version 6.0.0-1ubuntu2 (tags/RELEASE_600/final) +clang version 7.0.0-3~ubuntu0.18.04.1 (tags/RELEASE_700/final) +Apple clang version 11.0.0 (clang-1100.0.33.8) +``` + +After compiling with cmake, you'll get a test program in *compile/test* directory: *compile/test/test_redis++*. + +In order to run the tests, you need to set up a Redis instance, and a Redis Cluster. Since the test program will send most of Redis commands to the server and cluster, you need to set up Redis of the latest version (by now, it's 5.0). Otherwise, the tests might fail. For example, if you set up Redis 4.0 for testing, the test program will fail when it tries to send the `ZPOPMAX` command (a Redis 5.0 command) to the server. If you want to run the tests with other Redis versions, you have to comment out commands that haven't been supported by your Redis, from test source files in *redis-plus-plus/test/src/sw/redis++/* directory. Sorry for the inconvenience, and I'll fix this problem to make the test program work with any version of Redis in the future. + +**NOTE**: The latest version of Redis is only a requirement for running the tests. In fact, you can use *redis-plus-plus* with Redis of any version, e.g. Redis 2.0, Redis 3.0, Redis 4.0, Redis 5.0. + +**NEVER** run the test program in production envronment, since the keys, which the test program reads or writes, might conflict with your application. + +In order to run tests with both Redis and Redis Cluster, you can run the test program with the following command: + +``` +./compile/test/test_redis++ -h host -p port -a auth -n cluster_node -c cluster_port +``` + +- *host* and *port* are the host and port number of the Redis instance. +- *cluster_node* and *cluster_port* are the host and port number of Redis Cluster. You only need to set the host and port number of a single node in the cluster, *redis-plus-plus* will find other nodes automatically. +- *auth* is the password of the Redis instance and Redis Cluster. The Redis instance and Redis Cluster must be configured with the same password. If there's no password configured, don't set this option. + +If you only want to run tests with Redis, you only need to specify *host*, *port* and *auth* options: + +``` +./compile/test/test_redis++ -h host -p port -a auth +``` + +Similarly, if you only want to run tests with Redis Cluster, just specify *cluster_node*, *cluster_port* and *auth* options: + +``` +./compile/test/test_redis++ -a auth -n cluster_node -c cluster_port +``` + +The test program will test running *redis-plus-plus* in multi-threads environment, and this test will cost a long time. If you want to skip it (not recommended), just comment out the following lines in *test/src/sw/redis++/test_main.cpp* file. + +```C++ +sw::redis::test::ThreadsTest threads_test(opts, cluster_node_opts); +threads_test.run(); +``` + +If all tests have been passed, the test program will print the following message: + +``` +Pass all tests +``` + +Otherwise, it prints the error message. + +#### Performance + +*redis-plus-plus* runs as fast as *hiredis*, since it's a wrapper of *hiredis*. You can run *test_redis++* in benchmark mode to check the performance in your environment. + +``` +./compile/test/test_redis++ -h host -p port -a auth -n cluster_node -c cluster_port -b -t thread_num -s connection_pool_size -r request_num -k key_len -v val_len +``` + +- *-b* option turns the test program into benchmark mode. +- *thread_num* specifies the number of worker threads. `10` by default. +- *connection_pool_size* specifies the size of the connection pool. `5` by default. +- *request_num* specifies the total number of requests sent to server for each test. `100000` by default. +- *key_len* specifies the length of the key for each operation. `10` by default. +- *val_len* specifies the length of the value. `10` by default. + +The bechmark will generate `100` random binary keys for testing, and the size of these keys is specified by *key_len*. When the benchmark runs, it will read/write with these keys. So **NEVER** run the test program in your production environment, otherwise, it might inaccidently delete your data. + +### Use redis-plus-plus In Your Project + +After compiling the code, you'll get both shared library and static library. Since *redis-plus-plus* depends on *hiredis*, you need to link both libraries to your Application. Also don't forget to specify the `-std=c++11` and thread-related option. + +#### Use Static Libraries + +Take gcc as an example. + +``` +g++ -std=c++11 -o app app.cpp /path/to/libredis++.a /path/to/libhiredis.a -pthread +``` + +If *hiredis* and *redis-plus-plus* are installed at non-default location, you should use `-I` option to specify the header path. + +``` +g++ -std=c++11 -I/non-default/install/include/path -o app app.cpp /path/to/libredis++.a /path/to/libhiredis.a -pthread +``` + +#### Use Shared Libraries + +``` +g++ -std=c++11 -o app app.cpp -lredis++ -lhiredis -pthread +``` + +If *hiredis* and *redis-plus-plus* are installed at non-default location, you should use `-I` and `-L` options to specify the header and library paths. + +``` +g++ -std=c++11 -I/non-default/install/include/path -L/non-default/install/lib/path -o app app.cpp -lredis++ -lhiredis -pthread +``` + +When linking with shared libraries, and running your application, you might get the following error message: + +``` +error while loading shared libraries: xxx: cannot open shared object file: No such file or directory. +``` + +That's because the linker cannot find the shared libraries. In order to solve the problem, you can add the path where you installed *hiredis* and *redis-plus-plus* libraries, to `LD_LIBRARY_PATH` environment variable. For example: + +``` +export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/lib +``` + +Check [this StackOverflow question](https://stackoverflow.com/questions/480764) for details on how to solve the problem. + +#### Build With Cmake + +If you're using cmake to build your application, you need to add *hiredis* and *redis-plus-plus* dependencies in your *CMakeLists.txt*: + +```CMake +# <------------ add hiredis dependency ---------------> +find_path(HIREDIS_HEADER hiredis) +target_include_directories(target PUBLIC ${HIREDIS_HEADER}) + +find_library(HIREDIS_LIB hiredis) +target_link_libraries(target ${HIREDIS_LIB}) + +# <------------ add redis-plus-plus dependency --------------> +# NOTE: this should be *sw* NOT *redis++* +find_path(REDIS_PLUS_PLUS_HEADER sw) +target_include_directories(target PUBLIC ${REDIS_PLUS_PLUS_HEADER}) + +find_library(REDIS_PLUS_PLUS_LIB redis++) +target_link_libraries(target ${REDIS_PLUS_PLUS_LIB}) +``` + +See [this issue](https://github.com/sewenew/redis-plus-plus/issues/5) for a complete example of *CMakeLists.txt*. + +Also, if you installed *hiredis* and *redis-plus-plus* at non-default location, you need to run cmake with `CMAKE_PREFIX_PATH` option to specify the installation path of these two libraries. + +``` +cmake -DCMAKE_PREFIX_PATH=/installation/path/to/the/two/libs .. +``` + +## Getting Started + +```C++ +#include + +using namespace sw::redis; + +try { + // Create an Redis object, which is movable but NOT copyable. + auto redis = Redis("tcp://127.0.0.1:6379"); + + // ***** STRING commands ***** + + redis.set("key", "val"); + auto val = redis.get("key"); // val is of type OptionalString. See 'API Reference' section for details. + if (val) { + // Dereference val to get the returned value of std::string type. + std::cout << *val << std::endl; + } // else key doesn't exist. + + // ***** LIST commands ***** + + // std::vector to Redis LIST. + std::vector vec = {"a", "b", "c"}; + redis.rpush("list", vec.begin(), vec.end()); + + // std::initializer_list to Redis LIST. + redis.rpush("list", {"a", "b", "c"}); + + // Redis LIST to std::vector. + vec.clear(); + redis.lrange("list", 0, -1, std::back_inserter(vec)); + + // ***** HASH commands ***** + + redis.hset("hash", "field", "val"); + + // Another way to do the same job. + redis.hset("hash", std::make_pair("field", "val")); + + // std::unordered_map to Redis HASH. + std::unordered_map m = { + {"field1", "val1"}, + {"field2", "val2"} + }; + redis.hmset("hash", m.begin(), m.end()); + + // Redis HASH to std::unordered_map. + m.clear(); + redis.hgetall("hash", std::inserter(m, m.begin())); + + // Get value only. + // NOTE: since field might NOT exist, so we need to parse it to OptionalString. + std::vector vals; + redis.hmget("hash", {"field1", "field2"}, std::back_inserter(vals)); + + // ***** SET commands ***** + + redis.sadd("set", "m1"); + + // std::unordered_set to Redis SET. + std::unordered_set set = {"m2", "m3"}; + redis.sadd("set", set.begin(), set.end()); + + // std::initializer_list to Redis SET. + redis.sadd("set", {"m2", "m3"}); + + // Redis SET to std::unordered_set. + set.clear(); + redis.smembers("set", std::inserter(set, set.begin())); + + if (redis.sismember("set", "m1")) { + std::cout << "m1 exists" << std::endl; + } // else NOT exist. + + // ***** SORTED SET commands ***** + + redis.zadd("sorted_set", "m1", 1.3); + + // std::unordered_map to Redis SORTED SET. + std::unordered_map scores = { + {"m2", 2.3}, + {"m3", 4.5} + }; + redis.zadd("sorted_set", scores.begin(), scores.end()); + + // Redis SORTED SET to std::unordered_map. + scores.clear(); + redis.zrangebyscore("sorted_set", + UnboundedInterval{}, // (-inf, +inf) + std::inserter(scores, scores.begin())); + + // Only get member names: + // pass an inserter of std::vector type as output parameter. + std::vector without_score; + redis.zrangebyscore("sorted_set", + BoundedInterval(1.5, 3.4, BoundType::CLOSED), // [1.5, 3.4] + std::back_inserter(without_score)); + + // Get both member names and scores: + // pass an inserter of std::unordered_map as output parameter. + std::unordered_map with_score; + redis.zrangebyscore("sorted_set", + BoundedInterval(1.5, 3.4, BoundType::LEFT_OPEN), // (1.5, 3.4] + std::inserter(with_score, with_score.end())); + + // ***** SCRIPTING commands ***** + + // Script returns a single element. + auto num = redis.eval("return 1", {}, {}); + + // Script returns an array of elements. + std::vector nums; + redis.eval("return {ARGV[1], ARGV[2]}", {}, {"1", "2"}, std::back_inserter(nums)); + + // ***** Pipeline ***** + + // Create a pipeline. + auto pipe = redis.pipeline(); + + // Send mulitple commands and get all replies. + auto pipe_replies = pipe.set("key", "value") + .get("key") + .rename("key", "new-key") + .rpush("list", {"a", "b", "c"}) + .lrange("list", 0, -1) + .exec(); + + // Parse reply with reply type and index. + auto set_cmd_result = pipe_replies.get(0); + + auto get_cmd_result = pipe_replies.get(1); + + // rename command result + pipe_replies.get(2); + + auto rpush_cmd_result = pipe_replies.get(3); + + std::vector lrange_cmd_result; + pipe_replies.get(4, back_inserter(lrange_cmd_result)); + + // ***** Transaction ***** + + // Create a transaction. + auto tx = redis.transaction(); + + // Run multiple commands in a transaction, and get all replies. + auto tx_replies = tx.incr("num0") + .incr("num1") + .mget({"num0", "num1"}) + .exec(); + + // Parse reply with reply type and index. + auto incr_result0 = tx_replies.get(0); + + auto incr_result1 = tx_replies.get(1); + + std::vector mget_cmd_result; + tx_replies.get(2, back_inserter(mget_cmd_result)); + + // ***** Generic Command Interface ***** + + // There's no *Redis::client_getname* interface. + // But you can use *Redis::command* to get the client name. + val = redis.command("client", "getname"); + if (val) { + std::cout << *val << std::endl; + } + + // Same as above. + auto getname_cmd_str = {"client", "getname"}; + val = redis.command(getname_cmd_str.begin(), getname_cmd_str.end()); + + // There's no *Redis::sort* interface. + // But you can use *Redis::command* to send sort the list. + std::vector sorted_list; + redis.command("sort", "list", "ALPHA", std::back_inserter(sorted_list)); + + // Another *Redis::command* to do the same work. + auto sort_cmd_str = {"sort", "list", "ALPHA"}; + redis.command(sort_cmd_str.begin(), sort_cmd_str.end(), std::back_inserter(sorted_list)); + + // ***** Redis Cluster ***** + + // Create a RedisCluster object, which is movable but NOT copyable. + auto redis_cluster = RedisCluster("tcp://127.0.0.1:7000"); + + // RedisCluster has similar interfaces as Redis. + redis_cluster.set("key", "value"); + val = redis_cluster.get("key"); + if (val) { + std::cout << *val << std::endl; + } // else key doesn't exist. + + // Keys with hash-tag. + redis_cluster.set("key{tag}1", "val1"); + redis_cluster.set("key{tag}2", "val2"); + redis_cluster.set("key{tag}3", "val3"); + + std::vector hash_tag_res; + redis_cluster.mget({"key{tag}1", "key{tag}2", "key{tag}3"}, + std::back_inserter(hash_tag_res)); + +} catch (const Error &e) { + // Error handling. +} +``` + +## API Reference + +### Connection + +`Redis` class maintains a connection pool to Redis server. If the connection is broken, `Redis` reconnects to Redis server automatically. + +You can initialize a `Redis` instance with `ConnectionOptions` and `ConnectionPoolOptions`. `ConnectionOptions` specifies options for connection to Redis server, and `ConnectionPoolOptions` specifies options for conneciton pool. `ConnectionPoolOptions` is optional. If not specified, `Redis` maintains a single connection to Redis server. + +```C++ +ConnectionOptions connection_options; +connection_options.host = "127.0.0.1"; // Required. +connection_options.port = 6666; // Optional. The default port is 6379. +connection_options.password = "auth"; // Optional. No password by default. +connection_options.db = 1; // Optional. Use the 0th database by default. + +// Optional. Timeout before we successfully send request to or receive response from redis. +// By default, the timeout is 0ms, i.e. never timeout and block until we send or receive successfuly. +// NOTE: if any command is timed out, we throw a TimeoutError exception. +connection_options.socket_timeout = std::chrono::milliseconds(200); + +// Connect to Redis server with a single connection. +Redis redis1(connection_options); + +ConnectionPoolOptions pool_options; +pool_options.size = 3; // Pool size, i.e. max number of connections. + +// Connect to Redis server with a connection pool. +Redis redis2(connection_options, pool_options); +``` + +See [ConnectionOptions](https://github.com/sewenew/redis-plus-plus/blob/master/src/sw/redis%2B%2B/connection.h#L40) and [ConnectionPoolOptions](https://github.com/sewenew/redis-plus-plus/blob/master/src/sw/redis%2B%2B/connection_pool.h#L30) for more options. + +**NOTE**: `Redis` class is movable but NOT copyable. + +```C++ +// auto redis3 = redis1; // this won't compile. + +// But it's movable. +auto redis3 = std::move(redis1); +``` + +*redis-plus-plus* also supports connecting to Redis server with Unix Domain Socket. + +```C++ +ConnectionOptions options; +options.type = ConnectionType::UNIX; +options.path = "/path/to/socket"; +Redis redis(options); +``` + +You can also connect to Redis server with a URI. However, in this case, you can only specify *host* and *port*, or *Unix Domain Socket path*. In order to specify other options, you need to use `ConnectionOptions` and `ConnectionPoolOptions`. + +```C++ +// Single connection to the given host and port. +Redis redis1("tcp://127.0.0.1:6666"); + +// Use default port, i.e. 6379. +Redis redis2("tcp://127.0.0.1"); + +// Connect to Unix Domain Socket. +Redis redis3("unix://path/to/socket"); +``` + +#### Lazily Create Connection + +Connections in the pool are lazily created. When the connection pool is initialized, i.e. the constructor of `Redis`, `Redis` does NOT connect to the server. Instead, it connects to the server only when you try to send command. In this way, we can avoid unnecessary connections. So if the pool size is 5, but the number of max concurrent connections is 3, there will be only 3 connections in the pool. + +#### Connection Failure + +You don't need to check whether `Redis` object connects to server successfully. If `Redis` fails to create a connection to Redis server, or the connection is broken at some time, it throws an exception of type `Error` when you try to send command with `Redis`. Even when you get an exception, i.e. the connection is broken, you don't need to create a new `Redis` object. You can reuse the `Redis` object to send commands, and the `Redis` object will try to reconnect to server automatically. If it reconnects successfully, it sends command to server. Otherwise, it throws an exception again. + +See the [Exception section](#exception) for details on exceptions. + +#### Reuse Redis object As Much As Possible + +It's NOT cheap to create a `Redis` object, since it will create new connections to Redis server. So you'd better reuse `Redis` object as much as possible. Also, it's safe to call `Redis`' member functions in multi-thread environment, and you can share `Redis` object in multiple threads. + +```C++ +// This is GOOD practice. +auto redis = Redis("tcp://127.0.0.1"); +for (auto idx = 0; idx < 100; ++idx) { + // Reuse the Redis object in the loop. + redis.set("key", "val"); +} + +// This is VERY BAD! It's very inefficient. +// NEVER DO IT!!! +for (auto idx = 0; idx < 100; ++idx) { + // Create a new Redis object for each iteration. + auto redis = Redis("tcp://127.0.0.1"); + redis.set("key", "val"); +} +``` + +### Send Command to Redis Server + +You can send [Redis commands](https://redis.io/commands) through `Redis` object. `Redis` has one or more (overloaded) methods for each Redis command. The method has the same (lowercased) name as the corresponding command. For example, we have 3 overload methods for the `DEL key [key ...]` command: + +```C++ +// Delete a single key. +long long Redis::del(const StringView &key); + +// Delete a batch of keys: [first, last). +template +long long Redis::del(Input first, Input last); + +// Delete keys in the initializer_list. +template +long long Redis::del(std::initializer_list il); +``` + +With input parameters, these methods build a Redis command based on [Redis protocol](https://redis.io/topics/protocol), and send the command to Redis server. Then synchronously receive the reply, parse it, and return to the caller. + +Let's take a closer look at these methods' parameters and return values. + +#### Parameter Type + +Most of these methods have the same parameters as the corresponding commands. The following is a list of parameter types: + +| Parameter Type | Explaination | Example | Note | +| :------------: | ------------ | ------- | ---- | +| **StringView** | Parameters of string type. Normally used for key, value, member name, field name and so on | ***bool Redis::hset(const StringView &key, const StringView &field, const StringView &val)*** | See the [StringView section](#stringview) for details on `StringView` | +| **long long** | Parameters of integer type. Normally used for index (e.g. list commands) or integer | ***void ltrim(const StringView &key, long long start, long long stop)***
***long long decrby(const StringView &key, long long decrement)*** | | +| **double** | Parameters of floating-point type. Normally used for score (e.g. sorted set commands) or number of floating-point type | ***double incrbyfloat(const StringView &key, double increment)*** | | +| **std::chrono::duration**
**std::chrono::time_point** | Time-related parameters | ***bool expire(const StringView &key, const std::chrono::seconds &timeout)***
***bool expireat(const StringView &key, const std::chrono::time_point &tp)*** | | +| **std::pair** | Used for Redis hash's (field, value) pair | ***bool hset(const StringView &key, const std::pair &item)*** | | +| **std::pair** | Used for Redis geo's (longitude, latitude) pair | ***OptionalLongLong georadius(const StringView &key, const std::pair &location, double radius, GeoUnit unit, const StringView &destination, bool store_dist, long long count)*** | | +| **pair of iterators** | Use a pair of iterators to specify a range of input, so that we can pass the data in a STL container to these methods | ***template < typename Input >***
***long long del(Input first, Input last)*** | Throw an exception, if it's an empty range, i.e. *first == last* | +| **std::initializer_list< T >** | Use an initializer list to specify a batch of input | ***template < typename T >***
***long long del(std::initializer_list< T > il)*** | | +| **some options** | Options for some commands | ***UpdateType***, ***template < typename T > class BoundedInterval*** | See [command_options.h](https://github.com/sewenew/redis-plus-plus/blob/master/src/sw/redis%2B%2B/command_options.h) for details | + +##### StringView + +[std::string_view](http://en.cppreference.com/w/cpp/string/basic_string_view) is a good option for the type of string parameters. However, by now, not all compilers support `std::string_view`. So we wrote a [simple version](https://github.com/sewenew/redis-plus-plus/blob/master/src/sw/redis%2B%2B/utils.h#L48), i.e. `StringView`. Since there are conversions from `std::string` and c-style string to `StringView`, you can just pass `std::string` or c-style string to methods that need a `StringView` parameter. + +```C++ +// bool Redis::hset(const StringView &key, const StringView &field, const StringView &val) + +// Pass c-style string to StringView. +redis.hset("key", "field", "value"); + +// Pass std::string to StringView. +std::string key = "key"; +std::string field = "field"; +std::string val = "val"; +redis.hset(key, field, val); + +// Mix std::string and c-style string. +redis.hset(key, field, "value"); +``` + +#### Return Type + +[Redis protocol](https://redis.io/topics/protocol) defines 5 kinds of replies: +- *Status Reply*: Also known as *Simple String Reply*. It's a non-binary string reply. +- *Bulk String Reply*: Binary safe string reply. +- *Integer Reply*: Signed integer reply. Large enough to hold `long long`. +- *Array Reply*: (Nested) Array reply. +- *Error Reply*: Non-binary string reply that gives error info. + +Also these replies might be *NULL*. For instance, when you try to `GET` the value of a nonexistent key, Redis returns a *NULL Bulk String Reply*. + +As we mentioned above, replies are parsed into return values of these methods. The following is a list of return types: + +| Return Type | Explaination | Example | Note | +| :---------: | ------------ | ------- | ---- | +| **void** | *Status Reply* that should always return a string of "OK" | *RENAME*, *SETEX* | | +| **std::string** | *Status Reply* that NOT always return "OK", and *Bulk String Reply* | *PING*, *INFO* | | +| **bool** | *Integer Reply* that always returns 0 or 1 | *EXPIRE*, *HSET* | See the [Boolean Return Value section](#boolean-return-value) for the meaning of a boolean return value | +| **long long** | *Integer Reply* that not always return 0 or 1 | *DEL*, *APPEND* | | +| **double** | *Bulk String Reply* that represents a double | *INCRBYFLOAT*, *ZINCRBY* | | +| **std::pair** | *Array Reply* with exactly 2 elements. Since the return value is always an array of 2 elements, we return the 2 elements as a `std::pair`'s first and second elements | *BLPOP* | | +| **std::tuple** | *Array Reply* with fixed length and has more than 2 elements. Since length of the returned array is fixed, we return the array as a `std::tuple` | *BZPOPMAX* | | +| **output iterator** | General *Array Reply* with non-fixed/dynamic length. We use STL-like interface to return this kind of array replies, so that you can insert the return value into a STL container easily | *MGET*, *LRANGE* | Also, sometimes the type of output iterator decides which options to send with the command. See the [Examples section](#command-overloads) for details | +| **Optional< T >** | For any reply of type `T` that might be *NULL* | *GET*, *LPOP*, *BLPOP*, *BZPOPMAX* | See the [Optional section](#optional) for details on `Optional` | + +##### Boolean Return Value + +The return type of some methods, e.g. `EXPIRE`, `HSET`, is `bool`. If the method returns `false`, it DOES NOT mean that `Redis` failed to send the command to Redis server. Instead, it means that Redis server returns an *Integer Reply*, and the value of the reply is `0`. Accordingly, if the method returns `true`, it means that Redis server returns an *Integer Reply*, and the value of the reply is `1`. You can +check [Redis commands manual](http://redis.io/commands) for what do `0` and `1` stand for. + +For example, when we send `EXPIRE` command to Redis server, it returns `1` if the timeout was set, and it returns `0` if the key doesn't exist. Accordingly, if the timeout was set, `Redis::expire` returns `true`, and if the key doesn't exist, `Redis::expire` returns `false`. + +So, never use the return value to check if the command has been successfully sent to Redis server. Instead, if `Redis` failed to send command to server, it throws an exception of type `Error`. See the [Exception section](#exception) for details on exceptions. + +##### Optional + +[std::optional](http://en.cppreference.com/w/cpp/utility/optional) is a good option for return type, if Redis might return *NULL REPLY*. Again, since not all compilers support `std::optional` so far, we implement our own [simple version](https://github.com/sewenew/redis-plus-plus/blob/master/src/sw/redis%2B%2B/utils.h#L85), i.e. `Optional`. + +Take the [GET](https://redis.io/commands/get) and [MGET](https://redis.io/commands/mget) commands for example: + +```C++ +// Or just: auto val = redis.get("key"); +Optional val = redis.get("key"); + +// Optional has a conversion to bool. +// If it's NOT a null Optional object, it's converted to true. +// Otherwise, it's converted to false. +if (val) { + // Key exists. Dereference val to get the string result. + std::cout << *val << std::endl; +} else { + // Redis server returns a NULL Bulk String Reply. + // It's invalid to dereference a null Optional object. + std::cout << "key doesn't exist." << std::endl; +} + +std::vector> values; +redis.mget({"key1", "key2", "key3"}, std::back_inserter(values)); +for (const auto &val : values) { + if (val) { + // Key exist, process the value. + } +} +``` + +We also have some typedefs for some commonly used `Optional`: + +```C++ +using OptionalString = Optional; + +using OptionalLongLong = Optional; + +using OptionalDouble = Optional; + +using OptionalStringPair = Optional>; +``` + +#### Exception + +`Redis` throws exceptions if it receives an *Error Reply* or something bad happens, e.g. failed to create a connection to server, or connection to server is broken. All exceptions derived from `Error` class. See [errors.h](https://github.com/sewenew/redis-plus-plus/blob/master/src/sw/redis%2B%2B/errors.h) for details. + +- `Error`: Generic error. It's also the base class of other exceptions. +- `IoError`: There's some IO error with the connection. +- `TimeoutError`: Read or write operation was timed out. It's a derived class of `IoError`. +- `ClosedError`: Redis server closed the connection. +- `ProtoError`: The command or reply is invalid, and we cannot process it with Redis protocol. +- `OomError`: *hiredis* library got an out-of-memory error. +- `ReplyError`: Redis server returned an error reply, e.g. we try to call `redis::lrange` on a Redis hash. +- `WatchError`: Watched key has been modified. See [Watch section](#watch) for details. + +**NOTE**: *NULL REPLY*` is not taken as an exception. For example, if we try to `GET` a non-existent key, we'll get a *NULL Bulk String Reply*. Instead of throwing an exception, we return the *NULL REPLY* as a null `Optional` object. Also see [Optional section](#optional). + +#### Examples + +Let's see some examples on how to send commands to Redis server. + +##### Various Parameter Types + +```C++ +// ***** Parameters of StringView type ***** + +// Implicitly construct StringView with c-style string. +redis.set("key", "value"); + +// Implicitly construct StringView with std::string. +std::string key("key"); +std::string val("value"); +redis.set(key, val); + +// Explicitly pass StringView as parameter. +std::vector large_data; +// Avoid copying. +redis.set("key", StringView(large_data.data(), large_data.size())); + +// ***** Parameters of long long type ***** + +// For index. +redis.bitcount(key, 1, 3); + +// For number. +redis.incrby("num", 100); + +// ***** Parameters of double type ***** + +// For score. +redis.zadd("zset", "m1", 2.5); +redis.zadd("zset", "m2", 3.5); +redis.zadd("zset", "m3", 5); + +// For (longitude, latitude). +redis.geoadd("geo", std::make_tuple("member", 13.5, 15.6)); + +// ***** Time-related parameters ***** + +using namespace std::chrono; + +redis.expire(key, seconds(1000)); + +auto tp = time_point_cast(system_clock::now() + seconds(100)); +redis.expireat(key, tp); + +// ***** Some options for commands ***** + +if (redis.set(key, "value", milliseconds(100), UpdateType::NOT_EXIST)) { + std::cout << "set OK" << std::endl; +} + +redis.linsert("list", InsertPosition::BEFORE, "pivot", "val"); + +std::vector res; + +// (-inf, inf) +redis.zrangebyscore("zset", UnboundedInterval{}, std::back_inserter(res)); + +// [3, 6] +redis.zrangebyscore("zset", + BoundedInterval(3, 6, BoundType::CLOSED), + std::back_inserter(res)); + +// (3, 6] +redis.zrangebyscore("zset", + BoundedInterval(3, 6, BoundType::LEFT_OPEN), + std::back_inserter(res)); + +// (3, 6) +redis.zrangebyscore("zset", + BoundedInterval(3, 6, BoundType::OPEN), + std::back_inserter(res)); + +// [3, 6) +redis.zrangebyscore("zset", + BoundedInterval(3, 6, BoundType::RIGHT_OPEN), + std::back_inserter(res)); + +// [3, +inf) +redis.zrangebyscore("zset", + LeftBoundedInterval(3, BoundType::RIGHT_OPEN), + std::back_inserter(res)); + +// (3, +inf) +redis.zrangebyscore("zset", + LeftBoundedInterval(3, BoundType::OPEN), + std::back_inserter(res)); + +// (-inf, 6] +redis.zrangebyscore("zset", + RightBoundedInterval(6, BoundType::LEFT_OPEN), + std::back_inserter(res)); + +// (-inf, 6) +redis.zrangebyscore("zset", + RightBoundedInterval(6, BoundType::OPEN), + std::back_inserter(res)); + +// ***** Pair of iterators ***** + +std::vector> kvs = {{"k1", "v1"}, {"k2", "v2"}, {"k3", "v3"}}; +redis.mset(kvs.begin(), kvs.end()); + +std::unordered_map kv_map = {{"k1", "v1"}, {"k2", "v2"}, {"k3", "v3"}}; +redis.mset(kv_map.begin(), kv_map.end()); + +std::unordered_map str_map = {{"f1", "v1"}, {"f2", "v2"}, {"f3", "v3"}}; +redis.hmset("hash", str_map.begin(), str_map.end()); + +std::unordered_map score_map = {{"m1", 20}, {"m2", 12.5}, {"m3", 3.14}}; +redis.zadd("zset", score_map.begin(), score_map.end()); + +std::vector keys = {"k1", "k2", "k3"}; +redis.del(keys.begin(), keys.end()); + +// ***** Parameters of initializer_list type ***** + +redis.mset({ + std::make_pair("k1", "v1"), + std::make_pair("k2", "v2"), + std::make_pair("k3", "v3") +}); + +redis.hmset("hash", + { + std::make_pair("f1", "v1"), + std::make_pair("f2", "v2"), + std::make_pair("f3", "v3") + }); + +redis.zadd("zset", + { + std::make_pair("m1", 20.0), + std::make_pair("m2", 34.5), + std::make_pair("m3", 23.4) + }); + +redis.del({"k1", "k2", "k3"}); +``` + +##### Various Return Types + +```C++ +// ***** Return void ***** + +redis.save(); + +// ***** Return std::string ***** + +auto info = redis.info(); + +// ***** Return bool ***** + +if (!redis.expire("nonexistent", std::chrono::seconds(100))) { + std::cerr << "key doesn't exist" << std::endl; +} + +if (redis.setnx("key", "val")) { + std::cout << "set OK" << std::endl; +} + +// ***** Return long long ***** + +auto len = redis.strlen("key"); +auto num = redis.del({"a", "b", "c"}); +num = redis.incr("a"); + +// ***** Return double ***** + +auto real = redis.incrbyfloat("b", 23.4); +real = redis.hincrbyfloat("c", "f", 34.5); + +// ***** Return Optional, i.e. OptionalString ***** + +auto os = redis.get("kk"); +if (os) { + std::cout << *os << std::endl; +} else { + std::cerr << "key doesn't exist" << std::endl; +} + +os = redis.spop("set"); +if (os) { + std::cout << *os << std::endl; +} else { + std::cerr << "set is empty" << std::endl; +} + +// ***** Return Optional, i.e. OptionalLongLong ***** + +auto oll = redis.zrank("zset", "mem"); +if (oll) { + std::cout << "rank is " << *oll << std::endl; +} else { + std::cerr << "member doesn't exist" << std::endl; +} + +// ***** Return Optional, i.e. OptionalDouble ***** + +auto ob = redis.zscore("zset", "m1"); +if (ob) { + std::cout << "score is " << *ob << std::endl; +} else { + std::cerr << "member doesn't exist" << std::endl; +} + +// ***** Return Optional> ***** + +auto op = redis.blpop({"list1", "list2"}, std::chrono::seconds(2)); +if (op) { + std::cout << "key is " << op->first << ", value is " << op->second << std::endl; +} else { + std::cerr << "timeout" << std::endl; +} + +// ***** Output iterators ***** + +std::vector os_vec; +redis.mget({"k1", "k2", "k3"}, std::back_inserter(os_vec)); + +std::vector s_vec; +redis.lrange("list", 0, -1, std::back_inserter(s_vec)); + +std::unordered_map hash; +redis.hgetall("hash", std::inserter(hash, hash.end())); +// You can also save the result in a vecotr of string pair. +std::vector> hash_vec; +redis.hgetall("hash", std::back_inserter(hash_vec)); + +std::unordered_set str_set; +redis.smembers("s1", std::inserter(str_set, str_set.end())); +// You can also save the result in a vecotr of string. +s_vec.clear(); +redis.smembers("s1", std::back_inserter(s_vec)); +``` + +##### SCAN Commands + +```C++ +auto cursor = 0LL; +auto pattern = "*pattern*"; +auto count = 5; +std::vector scan_vec; +while (true) { + cursor = redis.scan(cursor, pattern, count, std::back_inserter(scan_vec)); + // Default pattern is "*", and default count is 10 + // cursor = redis.scan(cursor, std::back_inserter(scan_vec)); + + if (cursor == 0) { + break; + } +} +``` + +##### Command Overloads + +Sometimes the type of output iterator decides which options to send with the command. + +```C++ +// If the output iterator is an iterator of a container of string, +// we send *ZRANGE* command without the *WITHSCORES* option. +std::vector members; +redis.zrange("list", 0, -1, std::back_inserter(members)); + +// If it's an iterator of a container of a pair, +// we send *ZRANGE* command with *WITHSCORES* option. +std::unordered_map res_with_score; +redis.zrange("list", 0, -1, std::inserter(res_with_score, res_with_score.end())); + +// The above examples also apply to other command with the *WITHSCORES* options, +// e.g. *ZRANGEBYSCORE*, *ZREVRANGE*, *ZREVRANGEBYSCORE*. + +// Another example is the *GEORADIUS* command. + +// Only get members. +members.clear(); +redis.georadius("geo", + std::make_pair(10.1, 11.1), + 100, + GeoUnit::KM, + 10, + true, + std::back_inserter(members)); + +// If the iterator is an iterator of a container of tuple, +// we send the *GEORADIUS* command with *WITHDIST* option. +std::vector> mem_with_dist; +redis.georadius("geo", + std::make_pair(10.1, 11.1), + 100, + GeoUnit::KM, + 10, + true, + std::back_inserter(mem_with_dist)); + +// If the iterator is an iterator of a container of tuple, +// we send the *GEORADIUS* command with *WITHDIST* and *WITHHASH* options. +std::vector> mem_with_dist_hash; +redis.georadius("geo", + std::make_pair(10.1, 11.1), + 100, + GeoUnit::KM, + 10, + true, + std::back_inserter(mem_with_dist_hash)); + +// If the iterator is an iterator of a container of +// tuple, double>, +// we send the *GEORADIUS* command with *WITHHASH*, *WITHCOORD* and *WITHDIST* options. +std::vector> mem_with_hash_coord_dist; +redis.georadius("geo", + std::make_pair(10.1, 11.1), + 100, + GeoUnit::KM, + 10, + true, + std::back_inserter(mem_with_hash_coord_dist)); +``` + +Please see [redis.h](https://github.com/sewenew/redis-plus-plus/blob/master/src/sw/redis%2B%2B/redis.h) for more API references, and see the [tests](https://github.com/sewenew/redis-plus-plus/tree/master/test/src/sw/redis%2B%2B) for more examples. + +### Generic Command Interface + +There're too many Redis commands, we haven't implemented all of them. However, you can use the generic `Redis::command` methods to send any commands to Redis. Unlike other client libraries, `Redis::command` doesn't use format string to combine command arguments into a command string. Instead, you can directly pass command arguments of `StringView` type or arithmetic type as parameters of `Redis::command`. For the reason why we don't use format string, please see [this discussion](https://github.com/sewenew/redis-plus-plus/pull/2). + +```C++ +auto redis = Redis("tcp://127.0.0.1"); + +// Redis class doesn't have built-in *CLIENT SETNAME* method. +// However, you can use Redis::command to send the command manually. +redis.command("client", "setname", "name"); +auto val = redis.command("client", "getname"); +if (val) { + std::cout << *val << std::endl; +} + +// NOTE: the following code is for example only. In fact, Redis has built-in +// methods for the following commands. + +// Arguments of the command can be strings. +// NOTE: for SET command, the return value is NOT always void, I'll explain latter. +redis.command("set", "key", "100"); + +// Arguments of the command can be a combination of strings and integers. +auto num = redis.command("incrby", "key", 1); + +// Argument can also be double. +auto real = redis.command("incrbyfloat", "key", 2.3); + +// Even the key of the command can be of arithmetic type. +redis.command("set", 100, "value"); + +val = redis.command("get", 100); + +// If the command returns an array of elements. +std::vector result; +redis.command("mget", "k1", "k2", "k3", std::back_inserter(result)); + +// Or just parse it into a vector. +result = redis.command>("mget", "k1", "k2", "k3"); + +// Arguments of the command can be a range of strings. +auto set_cmd_strs = {"set", "key", "value"}; +redis.command(set_cmd_strs.begin(), set_cmd_strs.end()); + +auto get_cmd_strs = {"get", "key"}; +val = redis.command(get_cmd_strs.begin(), get_cmd_strs.end()); + +// If it returns an array of elements. +result.clear(); +auto mget_cmd_strs = {"mget", "key1", "key2"}; +redis.command(mget_cmd_strs.begin(), mget_cmd_strs.end(), std::back_inserter(result)); +``` + +**NOTE**: The name of some Redis commands is composed with two strings, e.g. *CLIENT SETNAME*. In this case, you need to pass these two strings as two arguments for `Redis::command`. + +```C++ +// This is GOOD. +redis.command("client", "setname", "name"); + +// This is BAD, and will fail to send command to Redis server. +// redis.command("client setname", "name"); +``` + +As I mentioned in the comments, the `SET` command not always returns `void`. Because if you try to set a (key, value) pair with *NX* or *XX* option, you might fail, and Redis will return a *NULL REPLY*. Besides the `SET` command, there're other commands whose return value is NOT a fixed type, you need to parse it by yourself. For example, `Redis::set` method rewrite the reply of `SET` command, and make it return `bool` type, i.e. if no *NX* or *XX* option specified, Redis server will always return an "OK" string, and `Redis::set` returns `true`; if *NX* or *XX* specified, and Redis server returns a *NULL REPLY*, `Redis::set` returns `false`. + +So `Redis` class also has other overloaded `command` methods, these methods return a `ReplyUPtr`, i.e. `std::unique_ptr`, object. Normally you don't need to parse it manually. Instead, you only need to pass the reply to `template T reply::parse(redisReply &)` to get a value of type `T`. Check the [Return Type section](#return-type) for valid `T` types. If the command returns an array of elements, besides calling `reply::parse` to parse the reply to an STL container, you can also call `template reply::to_array(redisReply &reply, Output output)` to parse the result into an array or STL container with an output iterator. + +Let's rewrite the above examples: + +```C++ +auto redis = Redis("tcp://127.0.0.1"); + +redis.command("client", "setname", "name"); +auto r = redis.command("client", "getname"); +assert(r); + +// If the command returns a single element, +// use `reply::parse(redisReply&)` to parse it. +auto val = reply::parse(*r); +if (val) { + std::cout << *val << std::endl; +} + +// Arguments of the command can be strings. +redis.command("set", "key", "100"); + +// Arguments of the command can be a combination of strings and integers. +r = redis.command("incrby", "key", 1); +auto num = reply::parse(*r); + +// Argument can also be double. +r = redis.command("incrbyfloat", "key", 2.3); +auto real = reply::parse(*r); + +// Even the key of the command can be of arithmetic type. +redis.command("set", 100, "value"); + +r = redis.command("get", 100); +val = reply::parse(*r); + +// If the command returns an array of elements. +r = redis.command("mget", "k1", "k2", "k3"); +// Use `reply::to_array(redisReply&, OutputIterator)` to parse the result into an STL container. +std::vector result; +reply::to_array(*r, std::back_inserter(result)); + +// Or just call `reply::parse` to parse it into vector. +result = reply::parse>(*r); + +// Arguments of the command can be a range of strings. +auto get_cmd_strs = {"get", "key"}; +r = redis.command(get_cmd_strs.begin(), get_cmd_strs.end()); +val = reply::parse(*r); + +// If it returns an array of elements. +result.clear(); +auto mget_cmd_strs = {"mget", "key1", "key2"}; +r = redis.command(mget_cmd_strs.begin(), mget_cmd_strs.end()); +reply::to_array(*r, std::back_inserter(result)); +``` + +In fact, there's one more `Redis::command` method: + +```C++ +template +auto command(Cmd cmd, Args &&...args) + -> typename std::enable_if::value, ReplyUPtr>::type; +``` + +However, this method exposes some implementation details, and is only for internal use. You should NOT use this method. + +### Publish/Subscribe + +You can use `Redis::publish` to publish messages to channels. `Redis` randomly picks a connection from the underlying connection pool, and publishes message with that connection. So you might publish two messages with two different connections. + +When you subscribe to a channel with a connection, all messages published to the channel are sent back to that connection. So there's NO `Redis::subscribe` method. Instead, you can call `Redis::subscriber` to create a `Subscriber` and the `Subscriber` maintains a connection to Redis. The underlying connection is a new connection, NOT picked from the connection pool. This new connection has the same `ConnectionOptions` as the `Redis` object. + +With `Subscriber`, you can call `Subscriber::subscribe`, `Subscriber::unsubscribe`, `Subscriber::psubscribe` and `Subscriber::punsubscribe` to send *SUBSCRIBE*, *UNSUBSCRIBE*, *PSUBSCRIBE* and *PUNSUBSCRIBE* commands to Redis. + +#### Thread Safety + +`Subscriber` is NOT thread-safe. If you want to call its member functions in multi-thread environment, you need to synchronize between threads manually. + +#### Subscriber Callbacks + +There are 6 kinds of messages: +- *MESSAGE*: message sent to a channel. +- *PMESSAGE*: message sent to channels of a given pattern. +- *SUBSCRIBE*: message sent when we successfully subscribe to a channel. +- *UNSUBSCRIBE*: message sent when we successfully unsubscribe to a channel. +- *PSUBSCRIBE*: message sent when we successfully subscribe to a channel pattern. +- *PUNSUBSCRIBE*: message sent when we successfully unsubscribe to a channel pattern. + +We call messages of *SUBSCRIBE*, *UNSUBSCRIBE*, *PSUBSCRIBE* and *PUNSUBSCRIBE* types as *META MESSAGE*s. + +In order to process these messages, you can set callback functions on `Subscriber`: +- `Subscriber::on_message(MsgCallback)`: set callback function for messages of *MESSAGE* type, and the callback interface is: `void (std::string channel, std::string msg)`. +- `Subscriber::on_pmessage(PatternMsgCallback)`: set the callback function for messages of *PMESSAGE* type, and the callback interface is: `void (std::string pattern, std::string channel, std::string msg)`. +- `Subscriber::on_meta(MetaCallback)`: set callback function for messages of *META MESSAGE* type, and the callback interface is: `void (Subscriber::MsgType type, OptionalString channel, long long num)`. `type` is an enum, it can be one of the following enum: `Subscriber::MsgType::SUBSCRIBE`, `Subscriber::MsgType::UNSUBSCRIBE`, `Subscriber::MsgType::PSUBSCRIBE`, `Subscriber::MsgType::PUNSUBSCRIBE`, `Subscriber::MsgType::MESSAGE`, and `Subscriber::MsgType::PMESSAGE`. If you haven't subscribe/psubscribe to any channel/pattern, and try to unsubscribe/punsubscribe without any parameter, i.e. unsubscribe/punsubscribe all channels/patterns, *channel* will be null. So the second parameter of meta callback is of type `OptionalString`. + +All these callback interfaces pass `std::string` by value, and you can take their ownership (i.e. `std::move`) safely. + +#### Consume Messages + +You can call `Subscriber::consume` to consume messages published to channels/patterns that the `Subscriber` has been subscribed. + +`Subscriber::consume` waits for message from the underlying connection. If the `ConnectionOptions::socket_timeout` is reached, and there's no message sent to this connection, `Subscriber::consume` throws a `TimeoutError` exception. If `ConnectionOptions::socket_timeout` is `0ms`, `Subscriber::consume` blocks until it receives a message. + +After receiving the message, `Subscriber::consume` calls the callback function to process the message based on message type. However, if you don't set callback for a specific kind of message, `Subscriber::consume` will ignore the received message, i.e. no callback will be called. + +#### Examples + +The following example is a common pattern for using `Subscriber`: + +```C++ +// Create a Subscriber. +auto sub = redis.subscriber(); + +// Set callback functions. +sub.on_message([](std::string channel, std::string msg) { + // Process message of MESSAGE type. + }); + +sub.on_pmessage([](std::string pattern, std::string channel, std::string msg) { + // Process message of PMESSAGE type. + }); + +sub.on_meta([](Subscriber::MsgType type, OptionalString channel, long long num) { + // Process message of META type. + }); + +// Subscribe to channels and patterns. +sub.subscribe("channel1"); +sub.subscribe({"channel2", "channel3"}); + +sub.psubscribe("pattern1*"); + +// Consume messages in a loop. +while (true) { + try { + sub.consume(); + } catch (const Error &err) { + // Handle exceptions. + } +} +``` + +If `ConnectionOptions::socket_timeout` is set, you might get `TimeoutError` exception before receiving a message: + +```C++ +while (true) { + try { + sub.consume(); + } catch (const TimeoutError &e) { + // Try again. + continue; + } catch (const Error &err) { + // Handle other exceptions. + } +} +``` + +The above examples use lambda as callback. If you're not familiar with lambda, you can also set a free function as callback. Check [this issue](https://github.com/sewenew/redis-plus-plus/issues/16) for detail. + +### Pipeline + +[Pipeline](https://redis.io/topics/pipelining) is used to reduce *RTT* (Round Trip Time), and speed up Redis queries. *redis-plus-plus* supports pipeline with the `Pipeline` class. + +#### Create Pipeline + +You can create a pipeline with `Redis::pipeline` method, which returns a `Pipeline` object. + +```C++ +ConnectionOptions connection_options; +ConnectionPoolOptions pool_options; + +Redis redis(connection_options, pool_options); + +auto pipe = redis.pipeline(); +``` + +When creating a `Pipeline` object, `Redis::pipeline` method creates a new connection to Redis server. This connection is NOT picked from the connection pool, but a newly created connection. This connection has the same `ConnectionOptions` as other connections in the connection pool. `Pipeline` object maintains the new connection, and all piped commands are sent through this connection. + +**NOTE**: Creating a `Pipeline` object is NOT cheap, since it creates a new connection. So you'd better reuse the `Pipeline` object as much as possible. + +#### Send Commands + +You can send Redis commands through the `Pipeline` object. Just like the `Redis` class, `Pipeline` has one or more (overloaded) methods for each Redis command. However, you CANNOT get the replies until you call `Pipeline::exec`. So these methods do NOT return the reply, instead they return the `Pipeline` object itself. And you can chain these methods calls. + +```C++ +pipe.set("key", "val").incr("num").rpush("list", {0, 1, 2}).command("hset", "key", "field", "value"); +``` + +#### Get Replies + +Once you finish sending commands to Redis, you can call `Pipeline::exec` to get replies of these commands. You can also chain `Pipeline::exec` with other commands. + +```C++ +pipe.set("key", "val").incr("num"); +auto replies = pipe.exec(); + +// The same as: +replies = pipe.set("key", "val").incr("num).exec(); +``` + +In fact, these commands won't be sent to Redis, until you call `Pipeline::exec`. So `Pipeline::exec` does 2 work in order: send all piped commands, then get all replies from Redis. + +Also you can call `Pipeline::discard` to discard those piped commands. + +```C++ +pipe.set("key", "val").incr("num"); + +pipe.discard(); +``` + +#### Parse Replies + +`Pipeline::exec` returns a `QueuedReplies` object, which contains replies of all commands that have been sent to Redis. You can use `QueuedReplies::get` method to get and parse the `ith` reply. It has 3 overloads: + +- `template Result get(std::size_t idx)`: Return the `ith` reply as a return value, and you need to specify the return type as tempalte parameter. +- `template void get(std::size_t idx, Output output)`: If the reply is of type *Array Reply*, you can call this method to write the `ith` reply to an output iterator. Normally, compiler will deduce the type of the output iterator, and you don't need to specify the type parameter explicitly. +- `redisReply& get(std::size_t idx)`: If the reply is NOT a fixed type, call this method to get a reference to `redisReply` object. In this case, you need to call `template T reply::parse(redisReply &)` to parse the reply manually. + +Check the [Return Type section](#return-type) for details on the return types of the result. + +```C++ +auto replies = pipe.set("key", "val").incr("num").lrange("list", 0, -1).exec(); + +auto set_cmd_result = replies.get(0); + +auto incr_cmd_result = replies.get(1); + +std::vector list_cmd_result; +replies.get(2, std::back_inserter(list_cmd_result)); +``` + +#### Exception + +If any of `Pipeline`'s method throws an exception, the `Pipeline` object enters an invalid state. You CANNOT use it any more, but only destroy the object, and create a new one. + +#### Thread Safety + +`Pipeline` is NOT thread-safe. If you want to call its member functions in multi-thread environment, you need to synchronize between threads manually. + +### Transaction + +[Transaction](https://redis.io/topics/transactions) is used to make multiple commands runs atomically. + +#### Create Transaction + +You can create a transaction with `Redis::transaction` method, which returns a `Transaction` object. + +```C++ +ConnectionOptions connection_options; +ConnectionPoolOptions pool_options; + +Redis redis(connection_options, pool_options); + +auto tx = redis.transaction(); +``` + +As the `Pipeline` class, `Transaction` maintains a newly created connection to Redis. This connection has the same `ConnectionOptions` as the `Redis` object. + +**NOTE**: Creating a `Transaction` object is NOT cheap, since it creates a new connection. So you'd better reuse the `Transaction` as much as possible. + +Also you don't need to send [MULTI](https://redis.io/commands/multi) command to Redis. `Transaction` will do that for you automatically. + +#### Send Commands + +`Transaction` shares most of implementation with `Pipeline`. It has the same interfaces as `Pipeline`. You can send commands as what you do with `Pipeline` object. + +```C++ +tx.set("key", "val").incr("num").lpush("list", {0, 1, 2}).command("hset", "key", "field", "val"); +``` + +#### Execute Transaction + +When you call `Transaction::exec`, you explicitly ask Redis to execute those queued commands, and return the replies. Otherwise, these commands won't be executed. Also, you can call `Transaction::discard` to discard the execution, i.e. no command will be executed. Both `Transaction::exec` and `Transaction::discard` can be chained with other commands. + +```C++ +auto replies = tx.set("key", "val").incr("num").exec(); + +tx.set("key", "val").incr("num"); + +// Discard the transaction. +tx.discard(); +``` + +#### Parse Replies + +See [Pipeline's Parse Replies section](#parse-replies) for how to parse the replies. + +#### Piped Transaction + +Normally, we always send multiple commnds in a transaction. In order to improve the performance, you can send these commands in a pipeline. You can create a piped transaction by passing `true` as parameter of `Redis::transaction` method. + +```C++ +// Create a piped transaction +auto tx = redis.transaction(true); +``` + +With this piped transaction, all commands are sent to Redis in a pipeline. + +#### Exception + +If any of `Transaction`'s method throws an exception other than `WatchError`, the `Transaction` object enters an invalid state. You CANNOT use it any more, but only destroy the object and create a new one. + +#### Thread Safety + +`Transacation` is NOT thread-safe. If you want to call its member functions in multi-thread environment, you need to synchronize between threads manually. + +#### Watch + +[WATCH is used to provide a check-and-set(CAS) behavior to Redis transactions](https://redis.io/topics/transactions#optimistic-locking-using-check-and-set). + +The `WATCH` command must be sent in the same connection as the transaction. And normally after the `WATCH` command, we also need to send some other commands to get data from Redis before executing the transaction. Take the following check-and-set case as an example: + +``` +WATCH key // watch a key +val = GET key // get value of the key +new_val = val + 1 // incr the value +MULTI // begin the transaction +SET key new_val // set value only if the value is NOT modified by others +EXEC // try to execute the transaction. + // if val has been modified, the transaction won't be executed. +``` + +However, with `Transaction` object, you CANNOT get the result of commands until the whole transaction has been finished. Instead, you need to create a `Redis` object from the `Transaction` object. The created `Redis` object shares the connection with `Transaction` object. With this created `Redis` object, you can send `WATCH` command and any other Redis commands to Redis server, and get the result immediately. + +Let's see how to implement the above example with *redis-plus-plus*: + +```C++ +auto redis = Redis("tcp://127.0.0.1"); + +// Create a transaction. +auto tx = redis.transaction(); + +// Create a Redis object from the Transaction object. Both objects share the same connection. +auto r = tx.redis(); + +// If the watched key has been modified by other clients, the transaction might fail. +// So we need to retry the transaction in a loop. +while (true) { + try { + // Watch a key. + r.watch("key"); + + // Get the old value. + auto val = r.get("key"); + auto num = 0; + if (val) { + num = std::stoi(*val); + } // else use default value, i.e. 0. + + // Incr value. + ++num; + + // Execute the transaction. + auto replies = tx.set("key", std::to_string(num)).exec(); + + // Transaction has been executed successfully. Check the result and break. + + assert(replies.size() == 1 && replies.get(0) == true); + + break; + } catch (const WatchError &err) { + // Key has been modified by other clients, retry. + continue; + } catch (const Error &err) { + // Something bad happens, and the Transaction object is no longer valid. + throw; + } +} +``` + +### Redis Cluster + +*redis-plus-plus* supports [Redis Cluster](https://redis.io/topics/cluster-tutorial). You can use `RedisCluster` class to send commands to Redis Cluster. It has similar interfaces as `Redis` class. + +#### Connection + +`RedisCluster` connects to all master nodes in the cluster. For each master node, it maintains a connection pool. By now, it doesn't connect to slave nodes. + +You can initialize a `RedisCluster` instance with `ConnectionOptions` and `ConnectionPoolOptions`. You only need to set one master node's host & port in `ConnectionOptions`, and `RedisCluster` will get other nodes' info automatically (with the *CLUSTER SLOTS* command). For each master node, it creates a connection pool with the specified `ConnectionPoolOptions`. If `ConnectionPoolOptions` is not specified, `RedisCluster` maintains a single connection to every master node. + +```C++ +// Set a master node's host & port. +ConnectionOptions connection_options; +connection_options.host = "127.0.0.1"; // Required. +connection_options.port = 7000; // Optional. The default port is 6379. +connection_options.password = "auth"; // Optional. No password by default. + +// Automatically get other nodes' info, +// and connect to every master node with a single connection. +RedisCluster cluster1(connection_options); + +ConnectionPoolOptions pool_options; +pool_options.size = 3; + +// For each master node, maintains a connection pool of size 3. +RedisCluster cluster2(connection_options, pool_options); +``` + +You can also specify connection option with an URI. However, in this way, you can only use default `ConnectionPoolOptions`, i.e. pool of size 1, and CANNOT specify password. + +```C++ +// Specify a master node's host & port. +RedisCluster cluster3("tcp://127.0.0.1:7000"); + +// Use default port, i.e. 6379. +RedisCluster cluster4("tcp://127.0.0.1"); +``` + +##### Note + +- `RedisCluster` only works with tcp connection. It CANNOT connect to Unix Domain Socket. If you specify Unix Domain Socket in `ConnectionOptions`, it throws an exception. +- All nodes in the cluster should have the same password. +- Since [Redis Cluster does NOT support multiple databses](https://redis.io/topics/cluster-spec#implemented-subset), `ConnectionOptions::db` is ignored. + +#### Interfaces + +As we mentioned above, `RedisCluster`'s interfaces are similar to `Redis`. It supports most of `Redis`' interfaces, including the [generic command interface](#generic-command-interface) (see `Redis`' [API Reference section](#api-reference) for details), except the following: + +- Not support commands without key as argument, e.g. `PING`, `INFO`. +- Not support Lua script without key parameters. + +Since there's no key parameter, `RedisCluster` has no idea on to which node these commands should be sent. However there're 2 workarounds for this problem: + +- If you want to send these commands to a specific node, you can create a `Redis` object with that node's host and port, and use the `Redis` object to do the work. +- Instead of host and port, you can also call `Redis RedisCluster::redis(const StringView &hash_tag)` to create a `Redis` object with a hash-tag specifying the node. In this case, the returned `Redis` object creates a new connection to Redis server. + +Also you can use the [hash tags](https://redis.io/topics/cluster-spec#keys-hash-tags) to send multiple-key commands. + +See the [example section](#examples-2) for details. + +##### Publish/Subscribe + +You can publish and subscribe messages with `RedisCluster`. The interfaces are exactly the same as `Redis`, i.e. use `RedisCluster::publish` to publish messages, and use `RedisCluster::subscriber` to create a subscriber to consume messages. See [Publish/Subscribe section](#publishsubscribe) for details. + +##### Pipeline and Transaction + +You can also create `Pipeline` and `Transaction` objects with `RedisCluster`, but the interfaces are different from `Redis`. Since all commands in the pipeline and transaction should be sent to a single node in a single connection, we need to tell `RedisCluster` with which node the pipeline or transaction should be created. + +Instead of specifing the node's IP and port, `RedisCluster`'s pipeline and transaction interfaces allow you to specify the node with a *hash tag*. `RedisCluster` will calculate the slot number with the given *hash tag*, and create a pipeline or transaction with the node holding the slot. + +```C++ +Pipeline RedisCluster::pipeline(const StringView &hash_tag); + +Transaction RedisCluster::transaction(const StringView &hash_tag, bool piped = false); +``` + +With the created `Pipeline` or `Transaction` object, you can send commands with keys located on the same node as the given *hash_tag*. See [Examples section](#examples-2) for an example. + +#### Examples + +```C++ +#include + +using namespace sw::redis; + +auto redis_cluster = RedisCluster("tcp://127.0.0.1:7000"); + +redis_cluster.set("key", "value"); +auto val = redis_cluster.get("key"); +if (val) { + std::cout << *val << std::endl; +} + +// With hash-tag. +redis_cluster.set("key{tag}1", "val1"); +redis_cluster.set("key{tag}2", "val2"); +redis_cluster.set("key{tag}3", "val3"); +std::vector hash_tag_res; +redis_cluster.mget({"key{tag}1", "key{tag}2", "key{tag}3"}, + std::back_inserter(hash_tag_res)); + +redis_cluster.lpush("list", {"1", "2", "3"}); +std::vector list; +redis_cluster.lrange("list", 0, -1, std::back_inserter(list)); + +// Pipline. +auto pipe = redis_cluster.pipeline("counter"); +auto replies = pipe.incr("{counter}:1").incr("{counter}:2").exec(); + +// Transaction. +auto tx = redis_cluster.transaction("key"); +replies = tx.incr("key").get("key").exec(); + +// Create a Redis object with hash-tag. +// It connects to the Redis instance that holds the given key, i.e. hash-tag. +auto r = redis_cluster.redis("hash-tag"); + +// And send command without key parameter to the server. +r.command("client", "setname", "connection-name"); +``` + +**NOTE**: When you use `RedisCluster::redis(const StringView &hash_tag)` to create a `Redis` object, instead of picking a connection from the underlying connection pool, it creates a new connection to the corresponding Redis server. So this is NOT a cheap operation, and you should try to reuse this newly created `Redis` object as much as possible. + +```C++ +// This is BAD! It's very inefficient. +// NEVER DO IT!!! +// After sending PING command, the newly created Redis object will be destroied. +cluster.redis("key").ping(); + +// Then it creates a connection to Redis, and closes the connection after sending the command. +cluster.redis("key").command("client", "setname", "hello"); + +// Instead you should reuse the Redis object. +// This is GOOD! +auto redis = cluster.redis("key"); + +redis.ping(); +redis.command("client", "setname", "hello"); +``` + +#### Details + +`RedisCluster` maintains the newest slot-node mapping, and sends command directly to the right node. Normally it works as fast as `Redis`. If the cluster reshards, `RedisCluster` will follow the redirection, and it will finally update the slot-node mapping. It can correctly handle the following resharding cases: + +- Data migration between exist nodes. +- Add new node to the cluster. +- Remove node from the cluster. + +`redis-plus-plus` is able to handle both [MOVED](https://redis.io/topics/cluster-spec#moved-redirection) and [ASK](https://redis.io/topics/cluster-spec#ask-redirection) redirections, so it's a complete Redis Cluster client. + +If master is down, the cluster will promote one of its replicas to be the new master. *redis-plus-plus* can also handle this case: + +- When the master is down, *redis-plus-plus* losts connection to it. In this case, if you try to send commands to this master, *redis-plus-plus* will try to update slot-node mapping from other nodes. If the mapping remains unchanged, i.e. new master hasn't been elected yet, it fails to send command to Redis Cluster and throws exception. +- When the new master has been elected, the slot-node mapping will be updated by the cluster. In this case, if you send commands to the cluster, *redis-plus-plus* can get an update-to-date mapping, and sends commands to the new master. + +### Redis Sentinel + +[Redis Sentinel provides high availability for Redis](https://redis.io/topics/sentinel). If Redis master is down, Redis Sentinels will elect a new master from slaves, i.e. failover. Besides, Redis Sentinel can also act like a configuration provider for clients, and clients can query master or slave address from Redis Sentinel. So that if a failover occurs, clients can ask the new master address from Redis Sentinel. + +*redis-plus-plus* supports getting Redis master or slave's IP and port from Redis Sentinel. In order to use this feature, you only need to initialize `Redis` object with Redis Sentinel info, which is composed with 3 parts: `std::shared_ptr`, master name and role (master or slave). + +Before using Redis Sentinel with *redis-plus-plus*, ensure that you have read Redis Sentinel's [doc](https://redis.io/topics/sentinel). + +#### Sentinel + +You can create a `std::shared_ptr` object with `SentinelOptions`. + +```C++ +SentinelOptions sentinel_opts; +sentinel_opts.nodes = {{"127.0.0.1", 9000}, + {"127.0.0.1", 9001}, + {"127.0.0.1", 9002}}; // Required. List of Redis Sentinel nodes. + +// Optional. Timeout before we successfully connect to Redis Sentinel. +// By default, the timeout is 100ms. +sentinel_opts.connect_timeout = std::chrono::milliseconds(200); + +// Optional. Timeout before we successfully send request to or receive response from Redis Sentinel. +// By default, the timeout is 100ms. +sentinel_opts.socket_timeout = std::chrono::milliseconds(200); + +auto sentinel = std::make_shared(sentinel_opts); +``` + +`SentinelOptions::connect_timeout` and `SentinelOptions::socket_timeout` CANNOT be 0ms, i.e. no timeout and block forever. Otherwise, *redis-plus-plus* will throw an exception. + +See [SentinelOptions](https://github.com/sewenew/redis-plus-plus/blob/master/src/sw/redis%2B%2B/sentinel.h#L33) for more options. + +#### Role + +Besides `std::shared_ptr` and master name, you also need to specify a role. There are two roles: `Role::MASTER`, and `Role::SLAVE`. + +With `Role::MASTER`, *redis-plus-plus* will always connect to current master instance, even if a failover occurs. Each time when *redis-plus-plus* needs to create a new connection to master, or a connection is broken, and it needs to reconnect to master, *redis-plus-plus* will ask master address from Redis Sentinel, and connects to current master. If a failover occurs, *redis-plus-plus* can automatically get the address of the new master, and refresh all connections in the underlying connection pool. + +Similarly, with `Role::SLAVE`, *redis-plus-plus* will always connect to a slave instance. A master might have several slaves, *redis-plus-plus* will randomly pick one, and connect to it, i.e. all connections in the underlying connection pool, connect to the same slave instance. If the connection is broken, while this slave instance is still an alive slave, *redis-plus-plus* will reconnect to this slave. However, if this slave instance is down, or it has been promoted to be the master, *redis-plus-plus* will randomly connect to another slave. If there's no slave alive, it throws an exception. + +#### Create Redis With Sentinel + +When creating a `Redis` object with sentinel, besides the sentinel info, you should also provide `ConnectionOptions` and `ConnectionPoolOptions`. These two options are used to connect to Redis instance. `ConnectionPoolOptions` is optional, if not specified, it creates a single connection the instance. + +```C++ +ConnectionOptions connection_opts; +connection_opts.password = "auth"; // Optional. No password by default. +connection_opts.connect_timeout = std::chrono::milliseconds(100); // Required. +connection_opts.socket_timeout = std::chrono::milliseconds(100); // Required. + +ConnectionPoolOptions pool_opts; +pool_opts.size = 3; // Optional. The default size is 1. + +auto redis = Redis(sentinel, "master_name", Role::MASTER, connection_opts, pool_opts); +``` + +You might have noticed that we didn't specify the `host` and `port` fields for `ConnectionOptions`. Because, `Redis` will get these info from Redis Sentinel. Also, in this case, `ConnectionOptions::connect_timeout` and `ConnectionOptions::socket_timeout` CANNOT be 0ms, otherwise, it throws an exception. So you always need to specify these two timeouts manually. + +After creating the `Redis` object with sentinel, you can send commands with it, just like an ordinary `Redis` object. + +If you want to write to master, and scale read with slaves. You can use the following pattern: + +```C++ +auto sentinel = std::make_shared(sentinel_opts); + +auto master = Redis(sentinel, "master_name", Role::MASTER, connection_opts, pool_opts); + +auto slave = Redis(sentinel, "master_name", Role::SLAVE, connection_opts, pool_opts); + +// Write to master. +master.set("key", "value"); + +// Read from slave. +slave.get("key"); +``` + +### Redis Stream + +Since Redis 5.0, it introduces a new data type: *Redis Stream*. *redis-plus-plus* has built-in methods for all stream commands except the *XINFO* command (of course, you can use the [Generic Command Interface](#generic-command-interface) to send *XINFO* command). + +However, the replies of some streams commands, i.e. *XPENDING*, *XREAD*, are complex. So I'll give some examples to show you how to work with these built-in methods. + +#### Examples + +```C++ +auto redis = Redis("tcp://127.0.0.1"); + +using Attrs = std::vector>; + +// You can also use std::unordered_map, if you don't care the order of attributes: +// using Attrs = std::unordered_map; + +Attrs attrs = { {"f1", "v1"}, {"f2", "v2"} }; + +// Add an item into the stream. This method returns the auto generated id. +auto id = redis.xadd("key", "*", attrs.begin(), attrs.end()); + +// Each item is assigned with an id: pair. +using Item = std::pair; +using ItemStream = std::vector; + +// If you don't care the order of items in the stream, you can also use unordered_map: +// using ItemStream = std::unordered_map; + +// Read items from a stream, and return at most 10 items. +// You need to specify a key and an id (timestamp + offset). +std::unordered_map result; +redis.xread("key", id, 10, std::inserter(result, result.end())); + +// Read from multiple streams. For each stream, you need to specify a key and an id. +std::unordered_map keys = { {"key", id}, {"another-key", "0-0"} }; +redis.xread(keys.begin(), keys.end(), 10, std::inserter(result, result.end())); + +// Block for at most 1 second if currently there's no data in the stream. +redis.xread("key", id, std::chrono::seconds(1), 10, std::inserter(result, result.end())); + +// Block for multiple streams. +redis.xread(keys.begin(), keys.end(), std::chrono::seconds(1), 10, std::inserter(result, result.end())); + +// Read items in a range: +ItemStream item_stream; +redis.xrange("key", "-", "+", std::back_inserter(item_stream)); + +// Trim the stream to a given number of items. After the operation, the stream length is NOT exactly +// 10. Instead, it might be much larger than 10. +// `XTRIM key MAXLEN 10` +redis.xtrim("key", 10); + +// In order to trim the stream to exactly 10 items, specify the third argument, i.e. approx, as false. +// `XTRIM key MAXLEN ~ 10` +redis.xtrim("key", 10, false); + +// Delete an item from the stream. +redis.xdel("key", id); + +// Create a consumer group. +redis.xgroup_create("key", "group", "$"); + +// If the stream doesn't exist, you can set the fourth argument, i.e. MKSTREAM, to be true. +// redis.xgroup_create("key", "group", "$", true); + +id = redis.xadd("key", "*", attrs.begin(), attrs.end()); + +// Read item by a consumer of a consumer group. +redis.xreadgroup("group", "consumer", "key", ">", 1, std::inserter(result, result.end())); + +using PendingItem = std::tuple; +std::vector pending_items; + +// Get pending items of a speicified consumer. +redis.xpending("key", "group", "-", "+", 1, "consumer", std::back_inserter(pending_items)); + +redis.xack("key", "group", id); + +redis.xgroup_delconsumer("key", "group", "consumer"); +redis.xgroup_destroy("key", "group"); +``` + +If you have any problem on sending stream commands to Redis, please feel free to let me know. + +## Author + +*redis-plus-plus* is written by sewenew, who is also active on [StackOverflow](https://stackoverflow.com/users/5384363/for-stack). diff --git a/controller/thirdparty/redis-plus-plus-1.1.1/src/sw/redis++/command.cpp b/controller/thirdparty/redis-plus-plus-1.1.1/src/sw/redis++/command.cpp new file mode 100644 index 000000000..bb1afb2a9 --- /dev/null +++ b/controller/thirdparty/redis-plus-plus-1.1.1/src/sw/redis++/command.cpp @@ -0,0 +1,376 @@ +/************************************************************************** + Copyright (c) 2017 sewenew + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. + *************************************************************************/ + +#include "command.h" +#include + +namespace sw { + +namespace redis { + +namespace cmd { + +// KEY commands. + +void restore(Connection &connection, + const StringView &key, + const StringView &val, + long long ttl, + bool replace) { + CmdArgs args; + args << "RESTORE" << key << ttl << val; + + if (replace) { + args << "REPLACE"; + } + + connection.send(args); +} + +// STRING commands. + +void bitop(Connection &connection, + BitOp op, + const StringView &destination, + const StringView &key) { + CmdArgs args; + + detail::set_bitop(args, op); + + args << destination << key; + + connection.send(args); +} + +void set(Connection &connection, + const StringView &key, + const StringView &val, + long long ttl, + UpdateType type) { + CmdArgs args; + args << "SET" << key << val; + + if (ttl > 0) { + args << "PX" << ttl; + } + + detail::set_update_type(args, type); + + connection.send(args); +} + +// LIST commands. + +void linsert(Connection &connection, + const StringView &key, + InsertPosition position, + const StringView &pivot, + const StringView &val) { + std::string pos; + switch (position) { + case InsertPosition::BEFORE: + pos = "BEFORE"; + break; + + case InsertPosition::AFTER: + pos = "AFTER"; + break; + + default: + assert(false); + } + + connection.send("LINSERT %b %s %b %b", + key.data(), key.size(), + pos.c_str(), + pivot.data(), pivot.size(), + val.data(), val.size()); +} + +// GEO commands. + +void geodist(Connection &connection, + const StringView &key, + const StringView &member1, + const StringView &member2, + GeoUnit unit) { + CmdArgs args; + args << "GEODIST" << key << member1 << member2; + + detail::set_geo_unit(args, unit); + + connection.send(args); +} + +void georadius_store(Connection &connection, + const StringView &key, + const std::pair &loc, + double radius, + GeoUnit unit, + const StringView &destination, + bool store_dist, + long long count) { + CmdArgs args; + args << "GEORADIUS" << key << loc.first << loc.second; + + detail::set_georadius_store_parameters(args, + radius, + unit, + destination, + store_dist, + count); + + connection.send(args); +} + +void georadius(Connection &connection, + const StringView &key, + const std::pair &loc, + double radius, + GeoUnit unit, + long long count, + bool asc, + bool with_coord, + bool with_dist, + bool with_hash) { + CmdArgs args; + args << "GEORADIUS" << key << loc.first << loc.second; + + detail::set_georadius_parameters(args, + radius, + unit, + count, + asc, + with_coord, + with_dist, + with_hash); + + connection.send(args); +} + +void georadiusbymember(Connection &connection, + const StringView &key, + const StringView &member, + double radius, + GeoUnit unit, + long long count, + bool asc, + bool with_coord, + bool with_dist, + bool with_hash) { + CmdArgs args; + args << "GEORADIUSBYMEMBER" << key << member; + + detail::set_georadius_parameters(args, + radius, + unit, + count, + asc, + with_coord, + with_dist, + with_hash); + + connection.send(args); +} + +void georadiusbymember_store(Connection &connection, + const StringView &key, + const StringView &member, + double radius, + GeoUnit unit, + const StringView &destination, + bool store_dist, + long long count) { + CmdArgs args; + args << "GEORADIUSBYMEMBER" << key << member; + + detail::set_georadius_store_parameters(args, + radius, + unit, + destination, + store_dist, + count); + + connection.send(args); +} + +// Stream commands. + +void xtrim(Connection &connection, const StringView &key, long long count, bool approx) { + CmdArgs args; + args << "XTRIM" << key << "MAXLEN"; + + if (approx) { + args << "~"; + } + + args << count; + + connection.send(args); +} + +namespace detail { + +void set_bitop(CmdArgs &args, BitOp op) { + args << "BITOP"; + + switch (op) { + case BitOp::AND: + args << "AND"; + break; + + case BitOp::OR: + args << "OR"; + break; + + case BitOp::XOR: + args << "XOR"; + break; + + case BitOp::NOT: + args << "NOT"; + break; + + default: + throw Error("Unknown bit operations"); + } +} + +void set_update_type(CmdArgs &args, UpdateType type) { + switch (type) { + case UpdateType::EXIST: + args << "XX"; + break; + + case UpdateType::NOT_EXIST: + args << "NX"; + break; + + case UpdateType::ALWAYS: + // Do nothing. + break; + + default: + throw Error("Unknown update type"); + } +} + +void set_aggregation_type(CmdArgs &args, Aggregation aggr) { + args << "AGGREGATE"; + + switch (aggr) { + case Aggregation::SUM: + args << "SUM"; + break; + + case Aggregation::MIN: + args << "MIN"; + break; + + case Aggregation::MAX: + args << "MAX"; + break; + + default: + throw Error("Unknown aggregation type"); + } +} + +void set_geo_unit(CmdArgs &args, GeoUnit unit) { + switch (unit) { + case GeoUnit::M: + args << "m"; + break; + + case GeoUnit::KM: + args << "km"; + break; + + case GeoUnit::MI: + args << "mi"; + break; + + case GeoUnit::FT: + args << "ft"; + break; + + default: + throw Error("Unknown geo unit type"); + break; + } +} + +void set_georadius_store_parameters(CmdArgs &args, + double radius, + GeoUnit unit, + const StringView &destination, + bool store_dist, + long long count) { + args << radius; + + detail::set_geo_unit(args, unit); + + args << "COUNT" << count; + + if (store_dist) { + args << "STOREDIST"; + } else { + args << "STORE"; + } + + args << destination; +} + +void set_georadius_parameters(CmdArgs &args, + double radius, + GeoUnit unit, + long long count, + bool asc, + bool with_coord, + bool with_dist, + bool with_hash) { + args << radius; + + detail::set_geo_unit(args, unit); + + if (with_coord) { + args << "WITHCOORD"; + } + + if (with_dist) { + args << "WITHDIST"; + } + + if (with_hash) { + args << "WITHHASH"; + } + + args << "COUNT" << count; + + if (asc) { + args << "ASC"; + } else { + args << "DESC"; + } +} + +} + +} + +} + +} diff --git a/controller/thirdparty/redis-plus-plus-1.1.1/src/sw/redis++/command.h b/controller/thirdparty/redis-plus-plus-1.1.1/src/sw/redis++/command.h new file mode 100644 index 000000000..3a4b24c5e --- /dev/null +++ b/controller/thirdparty/redis-plus-plus-1.1.1/src/sw/redis++/command.h @@ -0,0 +1,2233 @@ +/************************************************************************** + Copyright (c) 2017 sewenew + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. + *************************************************************************/ + +#ifndef SEWENEW_REDISPLUSPLUS_COMMAND_H +#define SEWENEW_REDISPLUSPLUS_COMMAND_H + +#include +#include +#include +#include +#include "connection.h" +#include "command_options.h" +#include "command_args.h" +#include "utils.h" + +namespace sw { + +namespace redis { + +namespace cmd { + +// CONNECTION command. +inline void auth(Connection &connection, const StringView &password) { + connection.send("AUTH %b", password.data(), password.size()); +} + +inline void echo(Connection &connection, const StringView &msg) { + connection.send("ECHO %b", msg.data(), msg.size()); +} + +inline void ping(Connection &connection) { + connection.send("PING"); +} + +inline void quit(Connection &connection) { + connection.send("QUIT"); +} + +inline void ping(Connection &connection, const StringView &msg) { + // If *msg* is empty, Redis returns am empty reply of REDIS_REPLY_STRING type. + connection.send("PING %b", msg.data(), msg.size()); +} + +inline void select(Connection &connection, long long idx) { + connection.send("SELECT %lld", idx); +} + +inline void swapdb(Connection &connection, long long idx1, long long idx2) { + connection.send("SWAPDB %lld %lld", idx1, idx2); +} + +// SERVER commands. + +inline void bgrewriteaof(Connection &connection) { + connection.send("BGREWRITEAOF"); +} + +inline void bgsave(Connection &connection) { + connection.send("BGSAVE"); +} + +inline void dbsize(Connection &connection) { + connection.send("DBSIZE"); +} + +inline void flushall(Connection &connection, bool async) { + if (async) { + connection.send("FLUSHALL ASYNC"); + } else { + connection.send("FLUSHALL"); + } +} + +inline void flushdb(Connection &connection, bool async) { + if (async) { + connection.send("FLUSHDB ASYNC"); + } else { + connection.send("FLUSHDB"); + } +} + +inline void info(Connection &connection) { + connection.send("INFO"); +} + +inline void info(Connection &connection, const StringView §ion) { + connection.send("INFO %b", section.data(), section.size()); +} + +inline void lastsave(Connection &connection) { + connection.send("LASTSAVE"); +} + +inline void save(Connection &connection) { + connection.send("SAVE"); +} + +// KEY commands. + +inline void del(Connection &connection, const StringView &key) { + connection.send("DEL %b", key.data(), key.size()); +} + +template +inline void del_range(Connection &connection, Input first, Input last) { + assert(first != last); + + CmdArgs args; + args << "DEL" << std::make_pair(first, last); + + connection.send(args); +} + +inline void dump(Connection &connection, const StringView &key) { + connection.send("DUMP %b", key.data(), key.size()); +} + +inline void exists(Connection &connection, const StringView &key) { + connection.send("EXISTS %b", key.data(), key.size()); +} + +template +inline void exists_range(Connection &connection, Input first, Input last) { + assert(first != last); + + CmdArgs args; + args << "EXISTS" << std::make_pair(first, last); + + connection.send(args); +} + +inline void expire(Connection &connection, + const StringView &key, + long long timeout) { + connection.send("EXPIRE %b %lld", + key.data(), key.size(), + timeout); +} + +inline void expireat(Connection &connection, + const StringView &key, + long long timestamp) { + connection.send("EXPIREAT %b %lld", + key.data(), key.size(), + timestamp); +} + +inline void keys(Connection &connection, const StringView &pattern) { + connection.send("KEYS %b", pattern.data(), pattern.size()); +} + +inline void move(Connection &connection, const StringView &key, long long db) { + connection.send("MOVE %b %lld", + key.data(), key.size(), + db); +} + +inline void persist(Connection &connection, const StringView &key) { + connection.send("PERSIST %b", key.data(), key.size()); +} + +inline void pexpire(Connection &connection, + const StringView &key, + long long timeout) { + connection.send("PEXPIRE %b %lld", + key.data(), key.size(), + timeout); +} + +inline void pexpireat(Connection &connection, + const StringView &key, + long long timestamp) { + connection.send("PEXPIREAT %b %lld", + key.data(), key.size(), + timestamp); +} + +inline void pttl(Connection &connection, const StringView &key) { + connection.send("PTTL %b", key.data(), key.size()); +} + +inline void randomkey(Connection &connection) { + connection.send("RANDOMKEY"); +} + +inline void rename(Connection &connection, + const StringView &key, + const StringView &newkey) { + connection.send("RENAME %b %b", + key.data(), key.size(), + newkey.data(), newkey.size()); +} + +inline void renamenx(Connection &connection, + const StringView &key, + const StringView &newkey) { + connection.send("RENAMENX %b %b", + key.data(), key.size(), + newkey.data(), newkey.size()); +} + +void restore(Connection &connection, + const StringView &key, + const StringView &val, + long long ttl, + bool replace); + +inline void scan(Connection &connection, + long long cursor, + const StringView &pattern, + long long count) { + connection.send("SCAN %lld MATCH %b COUNT %lld", + cursor, + pattern.data(), pattern.size(), + count); +} + +inline void touch(Connection &connection, const StringView &key) { + connection.send("TOUCH %b", key.data(), key.size()); +} + +template +inline void touch_range(Connection &connection, Input first, Input last) { + assert(first != last); + + CmdArgs args; + args << "TOUCH" << std::make_pair(first, last); + + connection.send(args); +} + +inline void ttl(Connection &connection, const StringView &key) { + connection.send("TTL %b", key.data(), key.size()); +} + +inline void type(Connection &connection, const StringView &key) { + connection.send("TYPE %b", key.data(), key.size()); +} + +inline void unlink(Connection &connection, const StringView &key) { + connection.send("UNLINK %b", key.data(), key.size()); +} + +template +inline void unlink_range(Connection &connection, Input first, Input last) { + assert(first != last); + + CmdArgs args; + args << "UNLINK" << std::make_pair(first, last); + + connection.send(args); +} + +inline void wait(Connection &connection, long long numslave, long long timeout) { + connection.send("WAIT %lld %lld", numslave, timeout); +} + +// STRING commands. + +inline void append(Connection &connection, const StringView &key, const StringView &str) { + connection.send("APPEND %b %b", + key.data(), key.size(), + str.data(), str.size()); +} + +inline void bitcount(Connection &connection, + const StringView &key, + long long start, + long long end) { + connection.send("BITCOUNT %b %lld %lld", + key.data(), key.size(), + start, end); +} + +void bitop(Connection &connection, + BitOp op, + const StringView &destination, + const StringView &key); + +template +void bitop_range(Connection &connection, + BitOp op, + const StringView &destination, + Input first, + Input last); + +inline void bitpos(Connection &connection, + const StringView &key, + long long bit, + long long start, + long long end) { + connection.send("BITPOS %b %lld %lld %lld", + key.data(), key.size(), + bit, + start, + end); +} + +inline void decr(Connection &connection, const StringView &key) { + connection.send("DECR %b", key.data(), key.size()); +} + +inline void decrby(Connection &connection, const StringView &key, long long decrement) { + connection.send("DECRBY %b %lld", + key.data(), key.size(), + decrement); +} + +inline void get(Connection &connection, const StringView &key) { + connection.send("GET %b", + key.data(), key.size()); +} + +inline void getbit(Connection &connection, const StringView &key, long long offset) { + connection.send("GETBIT %b %lld", + key.data(), key.size(), + offset); +} + +inline void getrange(Connection &connection, + const StringView &key, + long long start, + long long end) { + connection.send("GETRANGE %b %lld %lld", + key.data(), key.size(), + start, + end); +} + +inline void getset(Connection &connection, + const StringView &key, + const StringView &val) { + connection.send("GETSET %b %b", + key.data(), key.size(), + val.data(), val.size()); +} + +inline void incr(Connection &connection, const StringView &key) { + connection.send("INCR %b", key.data(), key.size()); +} + +inline void incrby(Connection &connection, const StringView &key, long long increment) { + connection.send("INCRBY %b %lld", + key.data(), key.size(), + increment); +} + +inline void incrbyfloat(Connection &connection, const StringView &key, double increment) { + connection.send("INCRBYFLOAT %b %f", + key.data(), key.size(), + increment); +} + +template +inline void mget(Connection &connection, Input first, Input last) { + assert(first != last); + + CmdArgs args; + args << "MGET" << std::make_pair(first, last); + + connection.send(args); +} + +template +inline void mset(Connection &connection, Input first, Input last) { + assert(first != last); + + CmdArgs args; + args << "MSET" << std::make_pair(first, last); + + connection.send(args); +} + +template +inline void msetnx(Connection &connection, Input first, Input last) { + assert(first != last); + + CmdArgs args; + args << "MSETNX" << std::make_pair(first, last); + + connection.send(args); +} + +inline void psetex(Connection &connection, + const StringView &key, + long long ttl, + const StringView &val) { + connection.send("PSETEX %b %lld %b", + key.data(), key.size(), + ttl, + val.data(), val.size()); +} + +void set(Connection &connection, + const StringView &key, + const StringView &val, + long long ttl, + UpdateType type); + +inline void setex(Connection &connection, + const StringView &key, + long long ttl, + const StringView &val) { + connection.send("SETEX %b %lld %b", + key.data(), key.size(), + ttl, + val.data(), val.size()); +} + +inline void setnx(Connection &connection, + const StringView &key, + const StringView &val) { + connection.send("SETNX %b %b", + key.data(), key.size(), + val.data(), val.size()); +} + +inline void setrange(Connection &connection, + const StringView &key, + long long offset, + const StringView &val) { + connection.send("SETRANGE %b %lld %b", + key.data(), key.size(), + offset, + val.data(), val.size()); +} + +inline void strlen(Connection &connection, const StringView &key) { + connection.send("STRLEN %b", key.data(), key.size()); +} + +// LIST commands. + +inline void blpop(Connection &connection, const StringView &key, long long timeout) { + connection.send("BLPOP %b %lld", + key.data(), key.size(), + timeout); +} + +template +inline void blpop_range(Connection &connection, + Input first, + Input last, + long long timeout) { + assert(first != last); + + CmdArgs args; + args << "BLPOP" << std::make_pair(first, last) << timeout; + + connection.send(args); +} + +inline void brpop(Connection &connection, const StringView &key, long long timeout) { + connection.send("BRPOP %b %lld", + key.data(), key.size(), + timeout); +} + +template +inline void brpop_range(Connection &connection, + Input first, + Input last, + long long timeout) { + assert(first != last); + + CmdArgs args; + args << "BRPOP" << std::make_pair(first, last) << timeout; + + connection.send(args); +} + +inline void brpoplpush(Connection &connection, + const StringView &source, + const StringView &destination, + long long timeout) { + connection.send("BRPOPLPUSH %b %b %lld", + source.data(), source.size(), + destination.data(), destination.size(), + timeout); +} + +inline void lindex(Connection &connection, const StringView &key, long long index) { + connection.send("LINDEX %b %lld", + key.data(), key.size(), + index); +} + +void linsert(Connection &connection, + const StringView &key, + InsertPosition position, + const StringView &pivot, + const StringView &val); + +inline void llen(Connection &connection, + const StringView &key) { + connection.send("LLEN %b", key.data(), key.size()); +} + +inline void lpop(Connection &connection, const StringView &key) { + connection.send("LPOP %b", + key.data(), key.size()); +} + +inline void lpush(Connection &connection, const StringView &key, const StringView &val) { + connection.send("LPUSH %b %b", + key.data(), key.size(), + val.data(), val.size()); +} + +template +inline void lpush_range(Connection &connection, + const StringView &key, + Input first, + Input last) { + assert(first != last); + + CmdArgs args; + args << "LPUSH" << key << std::make_pair(first, last); + + connection.send(args); +} + +inline void lpushx(Connection &connection, const StringView &key, const StringView &val) { + connection.send("LPUSHX %b %b", + key.data(), key.size(), + val.data(), val.size()); +} + +inline void lrange(Connection &connection, + const StringView &key, + long long start, + long long stop) { + connection.send("LRANGE %b %lld %lld", + key.data(), key.size(), + start, + stop); +} + +inline void lrem(Connection &connection, + const StringView &key, + long long count, + const StringView &val) { + connection.send("LREM %b %lld %b", + key.data(), key.size(), + count, + val.data(), val.size()); +} + +inline void lset(Connection &connection, + const StringView &key, + long long index, + const StringView &val) { + connection.send("LSET %b %lld %b", + key.data(), key.size(), + index, + val.data(), val.size()); +} + +inline void ltrim(Connection &connection, + const StringView &key, + long long start, + long long stop) { + connection.send("LTRIM %b %lld %lld", + key.data(), key.size(), + start, + stop); +} + +inline void rpop(Connection &connection, const StringView &key) { + connection.send("RPOP %b", key.data(), key.size()); +} + +inline void rpoplpush(Connection &connection, + const StringView &source, + const StringView &destination) { + connection.send("RPOPLPUSH %b %b", + source.data(), source.size(), + destination.data(), destination.size()); +} + +inline void rpush(Connection &connection, const StringView &key, const StringView &val) { + connection.send("RPUSH %b %b", + key.data(), key.size(), + val.data(), val.size()); +} + +template +inline void rpush_range(Connection &connection, + const StringView &key, + Input first, + Input last) { + assert(first != last); + + CmdArgs args; + args << "RPUSH" << key << std::make_pair(first, last); + + connection.send(args); +} + +inline void rpushx(Connection &connection, const StringView &key, const StringView &val) { + connection.send("RPUSHX %b %b", + key.data(), key.size(), + val.data(), val.size()); +} + +// HASH commands. + +inline void hdel(Connection &connection, const StringView &key, const StringView &field) { + connection.send("HDEL %b %b", + key.data(), key.size(), + field.data(), field.size()); +} + +template +inline void hdel_range(Connection &connection, + const StringView &key, + Input first, + Input last) { + assert(first != last); + + CmdArgs args; + args << "HDEL" << key << std::make_pair(first, last); + + connection.send(args); +} + +inline void hexists(Connection &connection, const StringView &key, const StringView &field) { + connection.send("HEXISTS %b %b", + key.data(), key.size(), + field.data(), field.size()); +} + +inline void hget(Connection &connection, const StringView &key, const StringView &field) { + connection.send("HGET %b %b", + key.data(), key.size(), + field.data(), field.size()); +} + +inline void hgetall(Connection &connection, const StringView &key) { + connection.send("HGETALL %b", key.data(), key.size()); +} + +inline void hincrby(Connection &connection, + const StringView &key, + const StringView &field, + long long increment) { + connection.send("HINCRBY %b %b %lld", + key.data(), key.size(), + field.data(), field.size(), + increment); +} + +inline void hincrbyfloat(Connection &connection, + const StringView &key, + const StringView &field, + double increment) { + connection.send("HINCRBYFLOAT %b %b %f", + key.data(), key.size(), + field.data(), field.size(), + increment); +} + +inline void hkeys(Connection &connection, const StringView &key) { + connection.send("HKEYS %b", key.data(), key.size()); +} + +inline void hlen(Connection &connection, const StringView &key) { + connection.send("HLEN %b", key.data(), key.size()); +} + +template +inline void hmget(Connection &connection, + const StringView &key, + Input first, + Input last) { + assert(first != last); + + CmdArgs args; + args << "HMGET" << key << std::make_pair(first, last); + + connection.send(args); +} + +template +inline void hmset(Connection &connection, + const StringView &key, + Input first, + Input last) { + assert(first != last); + + CmdArgs args; + args << "HMSET" << key << std::make_pair(first, last); + + connection.send(args); +} + +inline void hscan(Connection &connection, + const StringView &key, + long long cursor, + const StringView &pattern, + long long count) { + connection.send("HSCAN %b %lld MATCH %b COUNT %lld", + key.data(), key.size(), + cursor, + pattern.data(), pattern.size(), + count); +} + +inline void hset(Connection &connection, + const StringView &key, + const StringView &field, + const StringView &val) { + connection.send("HSET %b %b %b", + key.data(), key.size(), + field.data(), field.size(), + val.data(), val.size()); +} + +inline void hsetnx(Connection &connection, + const StringView &key, + const StringView &field, + const StringView &val) { + connection.send("HSETNX %b %b %b", + key.data(), key.size(), + field.data(), field.size(), + val.data(), val.size()); +} + +inline void hstrlen(Connection &connection, + const StringView &key, + const StringView &field) { + connection.send("HSTRLEN %b %b", + key.data(), key.size(), + field.data(), field.size()); +} + +inline void hvals(Connection &connection, const StringView &key) { + connection.send("HVALS %b", key.data(), key.size()); +} + +// SET commands + +inline void sadd(Connection &connection, + const StringView &key, + const StringView &member) { + connection.send("SADD %b %b", + key.data(), key.size(), + member.data(), member.size()); +} + +template +inline void sadd_range(Connection &connection, + const StringView &key, + Input first, + Input last) { + assert(first != last); + + CmdArgs args; + args << "SADD" << key << std::make_pair(first, last); + + connection.send(args); +} + +inline void scard(Connection &connection, const StringView &key) { + connection.send("SCARD %b", key.data(), key.size()); +} + +template +inline void sdiff(Connection &connection, Input first, Input last) { + assert(first != last); + + CmdArgs args; + args << "SDIFF" << std::make_pair(first, last); + + connection.send(args); +} + +inline void sdiffstore(Connection &connection, + const StringView &destination, + const StringView &key) { + connection.send("SDIFFSTORE %b %b", + destination.data(), destination.size(), + key.data(), key.size()); +} + +template +inline void sdiffstore_range(Connection &connection, + const StringView &destination, + Input first, + Input last) { + assert(first != last); + + CmdArgs args; + args << "SDIFFSTORE" << destination << std::make_pair(first, last); + + connection.send(args); +} + +template +inline void sinter(Connection &connection, Input first, Input last) { + assert(first != last); + + CmdArgs args; + args << "SINTER" << std::make_pair(first, last); + + connection.send(args); +} + +inline void sinterstore(Connection &connection, + const StringView &destination, + const StringView &key) { + connection.send("SINTERSTORE %b %b", + destination.data(), destination.size(), + key.data(), key.size()); +} + +template +inline void sinterstore_range(Connection &connection, + const StringView &destination, + Input first, + Input last) { + assert(first != last); + + CmdArgs args; + args << "SINTERSTORE" << destination << std::make_pair(first, last); + + connection.send(args); +} + +inline void sismember(Connection &connection, + const StringView &key, + const StringView &member) { + connection.send("SISMEMBER %b %b", + key.data(), key.size(), + member.data(), member.size()); +} + +inline void smembers(Connection &connection, const StringView &key) { + connection.send("SMEMBERS %b", key.data(), key.size()); +} + +inline void smove(Connection &connection, + const StringView &source, + const StringView &destination, + const StringView &member) { + connection.send("SMOVE %b %b %b", + source.data(), source.size(), + destination.data(), destination.size(), + member.data(), member.size()); +} + +inline void spop(Connection &connection, const StringView &key) { + connection.send("SPOP %b", key.data(), key.size()); +} + +inline void spop_range(Connection &connection, const StringView &key, long long count) { + connection.send("SPOP %b %lld", + key.data(), key.size(), + count); +} + +inline void srandmember(Connection &connection, const StringView &key) { + connection.send("SRANDMEMBER %b", key.data(), key.size()); +} + +inline void srandmember_range(Connection &connection, + const StringView &key, + long long count) { + connection.send("SRANDMEMBER %b %lld", + key.data(), key.size(), + count); +} + +inline void srem(Connection &connection, + const StringView &key, + const StringView &member) { + connection.send("SREM %b %b", + key.data(), key.size(), + member.data(), member.size()); +} + +template +inline void srem_range(Connection &connection, + const StringView &key, + Input first, + Input last) { + assert(first != last); + + CmdArgs args; + args << "SREM" << key << std::make_pair(first, last); + + connection.send(args); +} + +inline void sscan(Connection &connection, + const StringView &key, + long long cursor, + const StringView &pattern, + long long count) { + connection.send("SSCAN %b %lld MATCH %b COUNT %lld", + key.data(), key.size(), + cursor, + pattern.data(), pattern.size(), + count); +} + +template +inline void sunion(Connection &connection, Input first, Input last) { + assert(first != last); + + CmdArgs args; + args << "SUNION" << std::make_pair(first, last); + + connection.send(args); +} + +inline void sunionstore(Connection &connection, + const StringView &destination, + const StringView &key) { + connection.send("SUNIONSTORE %b %b", + destination.data(), destination.size(), + key.data(), key.size()); +} + +template +inline void sunionstore_range(Connection &connection, + const StringView &destination, + Input first, + Input last) { + assert(first != last); + + CmdArgs args; + args << "SUNIONSTORE" << destination << std::make_pair(first, last); + + connection.send(args); +} + +// Sorted Set commands. + +inline void bzpopmax(Connection &connection, const StringView &key, long long timeout) { + connection.send("BZPOPMAX %b %lld", key.data(), key.size(), timeout); +} + +template +void bzpopmax_range(Connection &connection, + Input first, + Input last, + long long timeout) { + assert(first != last); + + CmdArgs args; + args << "BZPOPMAX" << std::make_pair(first, last) << timeout; + + connection.send(args); +} + +inline void bzpopmin(Connection &connection, const StringView &key, long long timeout) { + connection.send("BZPOPMIN %b %lld", key.data(), key.size(), timeout); +} + +template +void bzpopmin_range(Connection &connection, + Input first, + Input last, + long long timeout) { + assert(first != last); + + CmdArgs args; + args << "BZPOPMIN" << std::make_pair(first, last) << timeout; + + connection.send(args); +} + +template +void zadd_range(Connection &connection, + const StringView &key, + Input first, + Input last, + UpdateType type, + bool changed); + +inline void zadd(Connection &connection, + const StringView &key, + const StringView &member, + double score, + UpdateType type, + bool changed) { + auto tmp = {std::make_pair(member, score)}; + + zadd_range(connection, key, tmp.begin(), tmp.end(), type, changed); +} + +inline void zcard(Connection &connection, const StringView &key) { + connection.send("ZCARD %b", key.data(), key.size()); +} + +template +inline void zcount(Connection &connection, + const StringView &key, + const Interval &interval) { + connection.send("ZCOUNT %b %s %s", + key.data(), key.size(), + interval.min().c_str(), + interval.max().c_str()); +} + +inline void zincrby(Connection &connection, + const StringView &key, + double increment, + const StringView &member) { + connection.send("ZINCRBY %b %f %b", + key.data(), key.size(), + increment, + member.data(), member.size()); +} + +inline void zinterstore(Connection &connection, + const StringView &destination, + const StringView &key, + double weight) { + connection.send("ZINTERSTORE %b 1 %b WEIGHTS %f", + destination.data(), destination.size(), + key.data(), key.size(), + weight); +} + +template +void zinterstore_range(Connection &connection, + const StringView &destination, + Input first, + Input last, + Aggregation aggr); + +template +inline void zlexcount(Connection &connection, + const StringView &key, + const Interval &interval) { + const auto &min = interval.min(); + const auto &max = interval.max(); + + connection.send("ZLEXCOUNT %b %b %b", + key.data(), key.size(), + min.data(), min.size(), + max.data(), max.size()); +} + +inline void zpopmax(Connection &connection, const StringView &key, long long count) { + connection.send("ZPOPMAX %b %lld", + key.data(), key.size(), + count); +} + +inline void zpopmin(Connection &connection, const StringView &key, long long count) { + connection.send("ZPOPMIN %b %lld", + key.data(), key.size(), + count); +} + +inline void zrange(Connection &connection, + const StringView &key, + long long start, + long long stop, + bool with_scores) { + if (with_scores) { + connection.send("ZRANGE %b %lld %lld WITHSCORES", + key.data(), key.size(), + start, + stop); + } else { + connection.send("ZRANGE %b %lld %lld", + key.data(), key.size(), + start, + stop); + } +} + +template +inline void zrangebylex(Connection &connection, + const StringView &key, + const Interval &interval, + const LimitOptions &opts) { + const auto &min = interval.min(); + const auto &max = interval.max(); + + connection.send("ZRANGEBYLEX %b %b %b LIMIT %lld %lld", + key.data(), key.size(), + min.data(), min.size(), + max.data(), max.size(), + opts.offset, + opts.count); +} + +template +void zrangebyscore(Connection &connection, + const StringView &key, + const Interval &interval, + const LimitOptions &opts, + bool with_scores) { + const auto &min = interval.min(); + const auto &max = interval.max(); + + if (with_scores) { + connection.send("ZRANGEBYSCORE %b %b %b WITHSCORES LIMIT %lld %lld", + key.data(), key.size(), + min.data(), min.size(), + max.data(), max.size(), + opts.offset, + opts.count); + } else { + connection.send("ZRANGEBYSCORE %b %b %b LIMIT %lld %lld", + key.data(), key.size(), + min.data(), min.size(), + max.data(), max.size(), + opts.offset, + opts.count); + } +} + +inline void zrank(Connection &connection, + const StringView &key, + const StringView &member) { + connection.send("ZRANK %b %b", + key.data(), key.size(), + member.data(), member.size()); +} + +inline void zrem(Connection &connection, + const StringView &key, + const StringView &member) { + connection.send("ZREM %b %b", + key.data(), key.size(), + member.data(), member.size()); +} + +template +inline void zrem_range(Connection &connection, + const StringView &key, + Input first, + Input last) { + assert(first != last); + + CmdArgs args; + args << "ZREM" << key << std::make_pair(first, last); + + connection.send(args); +} + +template +inline void zremrangebylex(Connection &connection, + const StringView &key, + const Interval &interval) { + const auto &min = interval.min(); + const auto &max = interval.max(); + + connection.send("ZREMRANGEBYLEX %b %b %b", + key.data(), key.size(), + min.data(), min.size(), + max.data(), max.size()); +} + +inline void zremrangebyrank(Connection &connection, + const StringView &key, + long long start, + long long stop) { + connection.send("zremrangebyrank %b %lld %lld", + key.data(), key.size(), + start, + stop); +} + +template +inline void zremrangebyscore(Connection &connection, + const StringView &key, + const Interval &interval) { + const auto &min = interval.min(); + const auto &max = interval.max(); + + connection.send("ZREMRANGEBYSCORE %b %b %b", + key.data(), key.size(), + min.data(), min.size(), + max.data(), max.size()); +} + +inline void zrevrange(Connection &connection, + const StringView &key, + long long start, + long long stop, + bool with_scores) { + if (with_scores) { + connection.send("ZREVRANGE %b %lld %lld WITHSCORES", + key.data(), key.size(), + start, + stop); + } else { + connection.send("ZREVRANGE %b %lld %lld", + key.data(), key.size(), + start, + stop); + } +} + +template +inline void zrevrangebylex(Connection &connection, + const StringView &key, + const Interval &interval, + const LimitOptions &opts) { + const auto &min = interval.min(); + const auto &max = interval.max(); + + connection.send("ZREVRANGEBYLEX %b %b %b LIMIT %lld %lld", + key.data(), key.size(), + max.data(), max.size(), + min.data(), min.size(), + opts.offset, + opts.count); +} + +template +void zrevrangebyscore(Connection &connection, + const StringView &key, + const Interval &interval, + const LimitOptions &opts, + bool with_scores) { + const auto &min = interval.min(); + const auto &max = interval.max(); + + if (with_scores) { + connection.send("ZREVRANGEBYSCORE %b %b %b WITHSCORES LIMIT %lld %lld", + key.data(), key.size(), + max.data(), max.size(), + min.data(), min.size(), + opts.offset, + opts.count); + } else { + connection.send("ZREVRANGEBYSCORE %b %b %b LIMIT %lld %lld", + key.data(), key.size(), + max.data(), max.size(), + min.data(), min.size(), + opts.offset, + opts.count); + } +} + +inline void zrevrank(Connection &connection, + const StringView &key, + const StringView &member) { + connection.send("ZREVRANK %b %b", + key.data(), key.size(), + member.data(), member.size()); +} + +inline void zscan(Connection &connection, + const StringView &key, + long long cursor, + const StringView &pattern, + long long count) { + connection.send("ZSCAN %b %lld MATCH %b COUNT %lld", + key.data(), key.size(), + cursor, + pattern.data(), pattern.size(), + count); +} + +inline void zscore(Connection &connection, + const StringView &key, + const StringView &member) { + connection.send("ZSCORE %b %b", + key.data(), key.size(), + member.data(), member.size()); +} + +inline void zunionstore(Connection &connection, + const StringView &destination, + const StringView &key, + double weight) { + connection.send("ZUNIONSTORE %b 1 %b WEIGHTS %f", + destination.data(), destination.size(), + key.data(), key.size(), + weight); +} + +template +void zunionstore_range(Connection &connection, + const StringView &destination, + Input first, + Input last, + Aggregation aggr); + +// HYPERLOGLOG commands. + +inline void pfadd(Connection &connection, + const StringView &key, + const StringView &element) { + connection.send("PFADD %b %b", + key.data(), key.size(), + element.data(), element.size()); +} + +template +inline void pfadd_range(Connection &connection, + const StringView &key, + Input first, + Input last) { + assert(first != last); + + CmdArgs args; + args << "PFADD" << key << std::make_pair(first, last); + + connection.send(args); +} + +inline void pfcount(Connection &connection, const StringView &key) { + connection.send("PFCOUNT %b", key.data(), key.size()); +} + +template +inline void pfcount_range(Connection &connection, + Input first, + Input last) { + assert(first != last); + + CmdArgs args; + args << "PFCOUNT" << std::make_pair(first, last); + + connection.send(args); +} + +inline void pfmerge(Connection &connection, const StringView &destination, const StringView &key) { + connection.send("PFMERGE %b %b", + destination.data(), destination.size(), + key.data(), key.size()); +} + +template +inline void pfmerge_range(Connection &connection, + const StringView &destination, + Input first, + Input last) { + assert(first != last); + + CmdArgs args; + args << "PFMERGE" << destination << std::make_pair(first, last); + + connection.send(args); +} + +// GEO commands. + +inline void geoadd(Connection &connection, + const StringView &key, + const std::tuple &member) { + const auto &mem = std::get<0>(member); + + connection.send("GEOADD %b %f %f %b", + key.data(), key.size(), + std::get<1>(member), + std::get<2>(member), + mem.data(), mem.size()); +} + +template +inline void geoadd_range(Connection &connection, + const StringView &key, + Input first, + Input last) { + assert(first != last); + + CmdArgs args; + args << "GEOADD" << key; + + while (first != last) { + const auto &member = *first; + args << std::get<1>(member) << std::get<2>(member) << std::get<0>(member); + ++first; + } + + connection.send(args); +} + +void geodist(Connection &connection, + const StringView &key, + const StringView &member1, + const StringView &member2, + GeoUnit unit); + +template +inline void geohash_range(Connection &connection, + const StringView &key, + Input first, + Input last) { + assert(first != last); + + CmdArgs args; + args << "GEOHASH" << key << std::make_pair(first, last); + + connection.send(args); +} + +template +inline void geopos_range(Connection &connection, + const StringView &key, + Input first, + Input last) { + assert(first != last); + + CmdArgs args; + args << "GEOPOS" << key << std::make_pair(first, last); + + connection.send(args); +} + +void georadius(Connection &connection, + const StringView &key, + const std::pair &loc, + double radius, + GeoUnit unit, + long long count, + bool asc, + bool with_coord, + bool with_dist, + bool with_hash); + +void georadius_store(Connection &connection, + const StringView &key, + const std::pair &loc, + double radius, + GeoUnit unit, + const StringView &destination, + bool store_dist, + long long count); + +void georadiusbymember(Connection &connection, + const StringView &key, + const StringView &member, + double radius, + GeoUnit unit, + long long count, + bool asc, + bool with_coord, + bool with_dist, + bool with_hash); + +void georadiusbymember_store(Connection &connection, + const StringView &key, + const StringView &member, + double radius, + GeoUnit unit, + const StringView &destination, + bool store_dist, + long long count); + +// SCRIPTING commands. + +inline void eval(Connection &connection, + const StringView &script, + std::initializer_list keys, + std::initializer_list args) { + CmdArgs cmd_args; + + cmd_args << "EVAL" << script << keys.size() + << std::make_pair(keys.begin(), keys.end()) + << std::make_pair(args.begin(), args.end()); + + connection.send(cmd_args); +} + +inline void evalsha(Connection &connection, + const StringView &script, + std::initializer_list keys, + std::initializer_list args) { + CmdArgs cmd_args; + + cmd_args << "EVALSHA" << script << keys.size() + << std::make_pair(keys.begin(), keys.end()) + << std::make_pair(args.begin(), args.end()); + + connection.send(cmd_args); +} + +inline void script_exists(Connection &connection, const StringView &sha) { + connection.send("SCRIPT EXISTS %b", sha.data(), sha.size()); +} + +template +inline void script_exists_range(Connection &connection, Input first, Input last) { + assert(first != last); + + CmdArgs args; + args << "SCRIPT" << "EXISTS" << std::make_pair(first, last); + + connection.send(args); +} + +inline void script_flush(Connection &connection) { + connection.send("SCRIPT FLUSH"); +} + +inline void script_kill(Connection &connection) { + connection.send("SCRIPT KILL"); +} + +inline void script_load(Connection &connection, const StringView &script) { + connection.send("SCRIPT LOAD %b", script.data(), script.size()); +} + +// PUBSUB commands. + +inline void psubscribe(Connection &connection, const StringView &pattern) { + connection.send("PSUBSCRIBE %b", pattern.data(), pattern.size()); +} + +template +inline void psubscribe_range(Connection &connection, Input first, Input last) { + if (first == last) { + throw Error("PSUBSCRIBE: no key specified"); + } + + CmdArgs args; + args << "PSUBSCRIBE" << std::make_pair(first, last); + + connection.send(args); +} + +inline void publish(Connection &connection, + const StringView &channel, + const StringView &message) { + connection.send("PUBLISH %b %b", + channel.data(), channel.size(), + message.data(), message.size()); +} + +inline void punsubscribe(Connection &connection) { + connection.send("PUNSUBSCRIBE"); +} + +inline void punsubscribe(Connection &connection, const StringView &pattern) { + connection.send("PUNSUBSCRIBE %b", pattern.data(), pattern.size()); +} + +template +inline void punsubscribe_range(Connection &connection, Input first, Input last) { + if (first == last) { + throw Error("PUNSUBSCRIBE: no key specified"); + } + + CmdArgs args; + args << "PUNSUBSCRIBE" << std::make_pair(first, last); + + connection.send(args); +} + +inline void subscribe(Connection &connection, const StringView &channel) { + connection.send("SUBSCRIBE %b", channel.data(), channel.size()); +} + +template +inline void subscribe_range(Connection &connection, Input first, Input last) { + if (first == last) { + throw Error("SUBSCRIBE: no key specified"); + } + + CmdArgs args; + args << "SUBSCRIBE" << std::make_pair(first, last); + + connection.send(args); +} + +inline void unsubscribe(Connection &connection) { + connection.send("UNSUBSCRIBE"); +} + +inline void unsubscribe(Connection &connection, const StringView &channel) { + connection.send("UNSUBSCRIBE %b", channel.data(), channel.size()); +} + +template +inline void unsubscribe_range(Connection &connection, Input first, Input last) { + if (first == last) { + throw Error("UNSUBSCRIBE: no key specified"); + } + + CmdArgs args; + args << "UNSUBSCRIBE" << std::make_pair(first, last); + + connection.send(args); +} + +// Transaction commands. + +inline void discard(Connection &connection) { + connection.send("DISCARD"); +} + +inline void exec(Connection &connection) { + connection.send("EXEC"); +} + +inline void multi(Connection &connection) { + connection.send("MULTI"); +} + +inline void unwatch(Connection &connection, const StringView &key) { + connection.send("UNWATCH %b", key.data(), key.size()); +} + +template +inline void unwatch_range(Connection &connection, Input first, Input last) { + if (first == last) { + throw Error("UNWATCH: no key specified"); + } + + CmdArgs args; + args << "UNWATCH" << std::make_pair(first, last); + + connection.send(args); +} + +inline void watch(Connection &connection, const StringView &key) { + connection.send("WATCH %b", key.data(), key.size()); +} + +template +inline void watch_range(Connection &connection, Input first, Input last) { + if (first == last) { + throw Error("WATCH: no key specified"); + } + + CmdArgs args; + args << "WATCH" << std::make_pair(first, last); + + connection.send(args); +} + +// Stream commands. + +inline void xack(Connection &connection, + const StringView &key, + const StringView &group, + const StringView &id) { + connection.send("XACK %b %b %b", + key.data(), key.size(), + group.data(), group.size(), + id.data(), id.size()); +} + +template +void xack_range(Connection &connection, + const StringView &key, + const StringView &group, + Input first, + Input last) { + CmdArgs args; + args << "XACK" << key << group << std::make_pair(first, last); + + connection.send(args); +} + +template +void xadd_range(Connection &connection, + const StringView &key, + const StringView &id, + Input first, + Input last) { + CmdArgs args; + args << "XADD" << key << id << std::make_pair(first, last); + + connection.send(args); +} + +template +void xadd_maxlen_range(Connection &connection, + const StringView &key, + const StringView &id, + Input first, + Input last, + long long count, + bool approx) { + CmdArgs args; + args << "XADD" << key << "MAXLEN"; + + if (approx) { + args << "~"; + } + + args << count << id << std::make_pair(first, last); + + connection.send(args); +} + +inline void xclaim(Connection &connection, + const StringView &key, + const StringView &group, + const StringView &consumer, + long long min_idle_time, + const StringView &id) { + connection.send("XCLAIM %b %b %b %lld %b", + key.data(), key.size(), + group.data(), group.size(), + consumer.data(), consumer.size(), + min_idle_time, + id.data(), id.size()); +} + +template +void xclaim_range(Connection &connection, + const StringView &key, + const StringView &group, + const StringView &consumer, + long long min_idle_time, + Input first, + Input last) { + CmdArgs args; + args << "XCLAIM" << key << group << consumer << min_idle_time << std::make_pair(first, last); + + connection.send(args); +} + +inline void xdel(Connection &connection, const StringView &key, const StringView &id) { + connection.send("XDEL %b %b", key.data(), key.size(), id.data(), id.size()); +} + +template +void xdel_range(Connection &connection, const StringView &key, Input first, Input last) { + CmdArgs args; + args << "XDEL" << key << std::make_pair(first, last); + + connection.send(args); +} + +inline void xgroup_create(Connection &connection, + const StringView &key, + const StringView &group, + const StringView &id, + bool mkstream) { + CmdArgs args; + args << "XGROUP" << "CREATE" << key << group << id; + + if (mkstream) { + args << "MKSTREAM"; + } + + connection.send(args); +} + +inline void xgroup_setid(Connection &connection, + const StringView &key, + const StringView &group, + const StringView &id) { + connection.send("XGROUP SETID %b %b %b", + key.data(), key.size(), + group.data(), group.size(), + id.data(), id.size()); +} + +inline void xgroup_destroy(Connection &connection, + const StringView &key, + const StringView &group) { + connection.send("XGROUP DESTROY %b %b", + key.data(), key.size(), + group.data(), group.size()); +} + +inline void xgroup_delconsumer(Connection &connection, + const StringView &key, + const StringView &group, + const StringView &consumer) { + connection.send("XGROUP DELCONSUMER %b %b %b", + key.data(), key.size(), + group.data(), group.size(), + consumer.data(), consumer.size()); +} + +inline void xlen(Connection &connection, const StringView &key) { + connection.send("XLEN %b", key.data(), key.size()); +} + +inline void xpending(Connection &connection, const StringView &key, const StringView &group) { + connection.send("XPENDING %b %b", + key.data(), key.size(), + group.data(), group.size()); +} + +inline void xpending_detail(Connection &connection, + const StringView &key, + const StringView &group, + const StringView &start, + const StringView &end, + long long count) { + connection.send("XPENDING %b %b %b %b %lld", + key.data(), key.size(), + group.data(), group.size(), + start.data(), start.size(), + end.data(), end.size(), + count); +} + +inline void xpending_per_consumer(Connection &connection, + const StringView &key, + const StringView &group, + const StringView &start, + const StringView &end, + long long count, + const StringView &consumer) { + connection.send("XPENDING %b %b %b %b %lld %b", + key.data(), key.size(), + group.data(), group.size(), + start.data(), start.size(), + end.data(), end.size(), + count, + consumer.data(), consumer.size()); +} + +inline void xrange(Connection &connection, + const StringView &key, + const StringView &start, + const StringView &end) { + connection.send("XRANGE %b %b %b", + key.data(), key.size(), + start.data(), start.size(), + end.data(), end.size()); +} + +inline void xrange_count(Connection &connection, + const StringView &key, + const StringView &start, + const StringView &end, + long long count) { + connection.send("XRANGE %b %b %b COUNT %lld", + key.data(), key.size(), + start.data(), start.size(), + end.data(), end.size(), + count); +} + +inline void xread(Connection &connection, + const StringView &key, + const StringView &id, + long long count) { + connection.send("XREAD COUNT %lld STREAMS %b %b", + count, + key.data(), key.size(), + id.data(), id.size()); +} + +template +void xread_range(Connection &connection, Input first, Input last, long long count) { + CmdArgs args; + args << "XREAD" << "COUNT" << count << "STREAMS"; + + for (auto iter = first; iter != last; ++iter) { + args << iter->first; + } + + for (auto iter = first; iter != last; ++iter) { + args << iter->second; + } + + connection.send(args); +} + +inline void xread_block(Connection &connection, + const StringView &key, + const StringView &id, + long long timeout, + long long count) { + connection.send("XREAD COUNT %lld BLOCK %lld STREAMS %b %b", + count, + timeout, + key.data(), key.size(), + id.data(), id.size()); +} + +template +void xread_block_range(Connection &connection, + Input first, + Input last, + long long timeout, + long long count) { + CmdArgs args; + args << "XREAD" << "COUNT" << count << "BLOCK" << timeout << "STREAMS"; + + for (auto iter = first; iter != last; ++iter) { + args << iter->first; + } + + for (auto iter = first; iter != last; ++iter) { + args << iter->second; + } + + connection.send(args); +} + +inline void xreadgroup(Connection &connection, + const StringView &group, + const StringView &consumer, + const StringView &key, + const StringView &id, + long long count, + bool noack) { + CmdArgs args; + args << "XREADGROUP" << "GROUP" << group << consumer << "COUNT" << count; + + if (noack) { + args << "NOACK"; + } + + args << "STREAMS" << key << id; + + connection.send(args); +} + +template +void xreadgroup_range(Connection &connection, + const StringView &group, + const StringView &consumer, + Input first, + Input last, + long long count, + bool noack) { + CmdArgs args; + args << "XREADGROUP" << "GROUP" << group << consumer << "COUNT" << count; + + if (noack) { + args << "NOACK"; + } + + args << "STREAMS"; + + for (auto iter = first; iter != last; ++iter) { + args << iter->first; + } + + for (auto iter = first; iter != last; ++iter) { + args << iter->second; + } + + connection.send(args); +} + +inline void xreadgroup_block(Connection &connection, + const StringView &group, + const StringView &consumer, + const StringView &key, + const StringView &id, + long long timeout, + long long count, + bool noack) { + CmdArgs args; + args << "XREADGROUP" << "GROUP" << group << consumer + << "COUNT" << count << "BLOCK" << timeout; + + if (noack) { + args << "NOACK"; + } + + args << "STREAMS" << key << id; + + connection.send(args); +} + +template +void xreadgroup_block_range(Connection &connection, + const StringView &group, + const StringView &consumer, + Input first, + Input last, + long long timeout, + long long count, + bool noack) { + CmdArgs args; + args << "XREADGROUP" << "GROUP" << group << consumer + << "COUNT" << count << "BLOCK" << timeout; + + if (noack) { + args << "NOACK"; + } + + args << "STREAMS"; + + for (auto iter = first; iter != last; ++iter) { + args << iter->first; + } + + for (auto iter = first; iter != last; ++iter) { + args << iter->second; + } + + connection.send(args); +} + +inline void xrevrange(Connection &connection, + const StringView &key, + const StringView &end, + const StringView &start) { + connection.send("XREVRANGE %b %b %b", + key.data(), key.size(), + end.data(), end.size(), + start.data(), start.size()); +} + +inline void xrevrange_count(Connection &connection, + const StringView &key, + const StringView &end, + const StringView &start, + long long count) { + connection.send("XREVRANGE %b %b %b COUNT %lld", + key.data(), key.size(), + end.data(), end.size(), + start.data(), start.size(), + count); +} + +void xtrim(Connection &connection, const StringView &key, long long count, bool approx); + +namespace detail { + +void set_bitop(CmdArgs &args, BitOp op); + +void set_update_type(CmdArgs &args, UpdateType type); + +void set_aggregation_type(CmdArgs &args, Aggregation type); + +template +void zinterstore(std::false_type, + Connection &connection, + const StringView &destination, + Input first, + Input last, + Aggregation aggr) { + CmdArgs args; + args << "ZINTERSTORE" << destination << std::distance(first, last) + << std::make_pair(first, last); + + set_aggregation_type(args, aggr); + + connection.send(args); +} + +template +void zinterstore(std::true_type, + Connection &connection, + const StringView &destination, + Input first, + Input last, + Aggregation aggr) { + CmdArgs args; + args << "ZINTERSTORE" << destination << std::distance(first, last); + + for (auto iter = first; iter != last; ++iter) { + args << iter->first; + } + + args << "WEIGHTS"; + + for (auto iter = first; iter != last; ++iter) { + args << iter->second; + } + + set_aggregation_type(args, aggr); + + connection.send(args); +} + +template +void zunionstore(std::false_type, + Connection &connection, + const StringView &destination, + Input first, + Input last, + Aggregation aggr) { + CmdArgs args; + args << "ZUNIONSTORE" << destination << std::distance(first, last) + << std::make_pair(first, last); + + set_aggregation_type(args, aggr); + + connection.send(args); +} + +template +void zunionstore(std::true_type, + Connection &connection, + const StringView &destination, + Input first, + Input last, + Aggregation aggr) { + CmdArgs args; + args << "ZUNIONSTORE" << destination << std::distance(first, last); + + for (auto iter = first; iter != last; ++iter) { + args << iter->first; + } + + args << "WEIGHTS"; + + for (auto iter = first; iter != last; ++iter) { + args << iter->second; + } + + set_aggregation_type(args, aggr); + + connection.send(args); +} + +void set_geo_unit(CmdArgs &args, GeoUnit unit); + +void set_georadius_store_parameters(CmdArgs &args, + double radius, + GeoUnit unit, + const StringView &destination, + bool store_dist, + long long count); + +void set_georadius_parameters(CmdArgs &args, + double radius, + GeoUnit unit, + long long count, + bool asc, + bool with_coord, + bool with_dist, + bool with_hash); + +} + +} + +} + +} + +namespace sw { + +namespace redis { + +namespace cmd { + +template +void bitop_range(Connection &connection, + BitOp op, + const StringView &destination, + Input first, + Input last) { + assert(first != last); + + CmdArgs args; + + detail::set_bitop(args, op); + + args << destination << std::make_pair(first, last); + + connection.send(args); +} + +template +void zadd_range(Connection &connection, + const StringView &key, + Input first, + Input last, + UpdateType type, + bool changed) { + assert(first != last); + + CmdArgs args; + + args << "ZADD" << key; + + detail::set_update_type(args, type); + + if (changed) { + args << "CH"; + } + + while (first != last) { + // Swap the pair to pair. + args << first->second << first->first; + ++first; + } + + connection.send(args); +} + +template +void zinterstore_range(Connection &connection, + const StringView &destination, + Input first, + Input last, + Aggregation aggr) { + assert(first != last); + + detail::zinterstore(typename IsKvPairIter::type(), + connection, + destination, + first, + last, + aggr); +} + +template +void zunionstore_range(Connection &connection, + const StringView &destination, + Input first, + Input last, + Aggregation aggr) { + assert(first != last); + + detail::zunionstore(typename IsKvPairIter::type(), + connection, + destination, + first, + last, + aggr); +} + +} + +} + +} + +#endif // end SEWENEW_REDISPLUSPLUS_COMMAND_H diff --git a/controller/thirdparty/redis-plus-plus-1.1.1/src/sw/redis++/command_args.h b/controller/thirdparty/redis-plus-plus-1.1.1/src/sw/redis++/command_args.h new file mode 100644 index 000000000..0beb71e5c --- /dev/null +++ b/controller/thirdparty/redis-plus-plus-1.1.1/src/sw/redis++/command_args.h @@ -0,0 +1,180 @@ +/************************************************************************** + Copyright (c) 2017 sewenew + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. + *************************************************************************/ + +#ifndef SEWENEW_REDISPLUSPLUS_COMMAND_ARGS_H +#define SEWENEW_REDISPLUSPLUS_COMMAND_ARGS_H + +#include +#include +#include +#include +#include "utils.h" + +namespace sw { + +namespace redis { + +class CmdArgs { +public: + template + CmdArgs& append(Arg &&arg); + + template + CmdArgs& append(Arg &&arg, Args &&...args); + + // All overloads of operator<< are for internal use only. + CmdArgs& operator<<(const StringView &arg); + + template ::type>::value, + int>::type = 0> + CmdArgs& operator<<(T &&arg); + + template + CmdArgs& operator<<(const std::pair &range); + + template + auto operator<<(const std::tuple &) -> + typename std::enable_if::type { + return *this; + } + + template + auto operator<<(const std::tuple &arg) -> + typename std::enable_if::type; + + const char** argv() { + return _argv.data(); + } + + const std::size_t* argv_len() { + return _argv_len.data(); + } + + std::size_t size() const { + return _argv.size(); + } + +private: + // Deep copy. + CmdArgs& _append(std::string arg); + + // Shallow copy. + CmdArgs& _append(const StringView &arg); + + // Shallow copy. + CmdArgs& _append(const char *arg); + + template ::type>::value, + int>::type = 0> + CmdArgs& _append(T &&arg) { + return operator<<(std::forward(arg)); + } + + template + CmdArgs& _append(std::true_type, const std::pair &range); + + template + CmdArgs& _append(std::false_type, const std::pair &range); + + std::vector _argv; + std::vector _argv_len; + + std::list _args; +}; + +template +inline CmdArgs& CmdArgs::append(Arg &&arg) { + return _append(std::forward(arg)); +} + +template +inline CmdArgs& CmdArgs::append(Arg &&arg, Args &&...args) { + _append(std::forward(arg)); + + return append(std::forward(args)...); +} + +inline CmdArgs& CmdArgs::operator<<(const StringView &arg) { + _argv.push_back(arg.data()); + _argv_len.push_back(arg.size()); + + return *this; +} + +template +inline CmdArgs& CmdArgs::operator<<(const std::pair &range) { + return _append(IsKvPair())>::type>(), range); +} + +template ::type>::value, + int>::type> +inline CmdArgs& CmdArgs::operator<<(T &&arg) { + return _append(std::to_string(std::forward(arg))); +} + +template +auto CmdArgs::operator<<(const std::tuple &arg) -> + typename std::enable_if::type { + operator<<(std::get(arg)); + + return operator<<(arg); +} + +inline CmdArgs& CmdArgs::_append(std::string arg) { + _args.push_back(std::move(arg)); + return operator<<(_args.back()); +} + +inline CmdArgs& CmdArgs::_append(const StringView &arg) { + return operator<<(arg); +} + +inline CmdArgs& CmdArgs::_append(const char *arg) { + return operator<<(arg); +} + +template +CmdArgs& CmdArgs::_append(std::false_type, const std::pair &range) { + auto first = range.first; + auto last = range.second; + while (first != last) { + *this << *first; + ++first; + } + + return *this; +} + +template +CmdArgs& CmdArgs::_append(std::true_type, const std::pair &range) { + auto first = range.first; + auto last = range.second; + while (first != last) { + *this << first->first << first->second; + ++first; + } + + return *this; +} + +} + +} + +#endif // end SEWENEW_REDISPLUSPLUS_COMMAND_ARGS_H diff --git a/controller/thirdparty/redis-plus-plus-1.1.1/src/sw/redis++/command_options.cpp b/controller/thirdparty/redis-plus-plus-1.1.1/src/sw/redis++/command_options.cpp new file mode 100644 index 000000000..0c9254e86 --- /dev/null +++ b/controller/thirdparty/redis-plus-plus-1.1.1/src/sw/redis++/command_options.cpp @@ -0,0 +1,201 @@ +/************************************************************************** + Copyright (c) 2017 sewenew + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. + *************************************************************************/ + +#include "command_options.h" +#include "errors.h" + +namespace { + +const std::string NEGATIVE_INFINITY_NUMERIC = "-inf"; +const std::string POSITIVE_INFINITY_NUMERIC = "+inf"; + +const std::string NEGATIVE_INFINITY_STRING = "-"; +const std::string POSITIVE_INFINITY_STRING = "+"; + +std::string unbound(const std::string &bnd); + +std::string bound(const std::string &bnd); + +} + +namespace sw { + +namespace redis { + +const std::string& UnboundedInterval::min() const { + return NEGATIVE_INFINITY_NUMERIC; +} + +const std::string& UnboundedInterval::max() const { + return POSITIVE_INFINITY_NUMERIC; +} + +BoundedInterval::BoundedInterval(double min, double max, BoundType type) : + _min(std::to_string(min)), + _max(std::to_string(max)) { + switch (type) { + case BoundType::CLOSED: + // Do nothing + break; + + case BoundType::OPEN: + _min = unbound(_min); + _max = unbound(_max); + break; + + case BoundType::LEFT_OPEN: + _min = unbound(_min); + break; + + case BoundType::RIGHT_OPEN: + _max = unbound(_max); + break; + + default: + throw Error("Unknow BoundType"); + } +} + +LeftBoundedInterval::LeftBoundedInterval(double min, BoundType type) : + _min(std::to_string(min)) { + switch (type) { + case BoundType::OPEN: + _min = unbound(_min); + break; + + case BoundType::RIGHT_OPEN: + // Do nothing. + break; + + default: + throw Error("Bound type can only be OPEN or RIGHT_OPEN"); + } +} + +const std::string& LeftBoundedInterval::max() const { + return POSITIVE_INFINITY_NUMERIC; +} + +RightBoundedInterval::RightBoundedInterval(double max, BoundType type) : + _max(std::to_string(max)) { + switch (type) { + case BoundType::OPEN: + _max = unbound(_max); + break; + + case BoundType::LEFT_OPEN: + // Do nothing. + break; + + default: + throw Error("Bound type can only be OPEN or LEFT_OPEN"); + } +} + +const std::string& RightBoundedInterval::min() const { + return NEGATIVE_INFINITY_NUMERIC; +} + +const std::string& UnboundedInterval::min() const { + return NEGATIVE_INFINITY_STRING; +} + +const std::string& UnboundedInterval::max() const { + return POSITIVE_INFINITY_STRING; +} + +BoundedInterval::BoundedInterval(const std::string &min, + const std::string &max, + BoundType type) { + switch (type) { + case BoundType::CLOSED: + _min = bound(min); + _max = bound(max); + break; + + case BoundType::OPEN: + _min = unbound(min); + _max = unbound(max); + break; + + case BoundType::LEFT_OPEN: + _min = unbound(min); + _max = bound(max); + break; + + case BoundType::RIGHT_OPEN: + _min = bound(min); + _max = unbound(max); + break; + + default: + throw Error("Unknow BoundType"); + } +} + +LeftBoundedInterval::LeftBoundedInterval(const std::string &min, BoundType type) { + switch (type) { + case BoundType::OPEN: + _min = unbound(min); + break; + + case BoundType::RIGHT_OPEN: + _min = bound(min); + break; + + default: + throw Error("Bound type can only be OPEN or RIGHT_OPEN"); + } +} + +const std::string& LeftBoundedInterval::max() const { + return POSITIVE_INFINITY_STRING; +} + +RightBoundedInterval::RightBoundedInterval(const std::string &max, BoundType type) { + switch (type) { + case BoundType::OPEN: + _max = unbound(max); + break; + + case BoundType::LEFT_OPEN: + _max = bound(max); + break; + + default: + throw Error("Bound type can only be OPEN or LEFT_OPEN"); + } +} + +const std::string& RightBoundedInterval::min() const { + return NEGATIVE_INFINITY_STRING; +} + +} + +} + +namespace { + +std::string unbound(const std::string &bnd) { + return "(" + bnd; +} + +std::string bound(const std::string &bnd) { + return "[" + bnd; +} + +} diff --git a/controller/thirdparty/redis-plus-plus-1.1.1/src/sw/redis++/command_options.h b/controller/thirdparty/redis-plus-plus-1.1.1/src/sw/redis++/command_options.h new file mode 100644 index 000000000..ca766c086 --- /dev/null +++ b/controller/thirdparty/redis-plus-plus-1.1.1/src/sw/redis++/command_options.h @@ -0,0 +1,211 @@ +/************************************************************************** + Copyright (c) 2017 sewenew + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. + *************************************************************************/ + +#ifndef SEWENEW_REDISPLUSPLUS_COMMAND_OPTIONS_H +#define SEWENEW_REDISPLUSPLUS_COMMAND_OPTIONS_H + +#include +#include "utils.h" + +namespace sw { + +namespace redis { + +enum class UpdateType { + EXIST, + NOT_EXIST, + ALWAYS +}; + +enum class InsertPosition { + BEFORE, + AFTER +}; + +enum class BoundType { + CLOSED, + OPEN, + LEFT_OPEN, + RIGHT_OPEN +}; + +// (-inf, +inf) +template +class UnboundedInterval; + +// [min, max], (min, max), (min, max], [min, max) +template +class BoundedInterval; + +// [min, +inf), (min, +inf) +template +class LeftBoundedInterval; + +// (-inf, max], (-inf, max) +template +class RightBoundedInterval; + +template <> +class UnboundedInterval { +public: + const std::string& min() const; + + const std::string& max() const; +}; + +template <> +class BoundedInterval { +public: + BoundedInterval(double min, double max, BoundType type); + + const std::string& min() const { + return _min; + } + + const std::string& max() const { + return _max; + } + +private: + std::string _min; + std::string _max; +}; + +template <> +class LeftBoundedInterval { +public: + LeftBoundedInterval(double min, BoundType type); + + const std::string& min() const { + return _min; + } + + const std::string& max() const; + +private: + std::string _min; +}; + +template <> +class RightBoundedInterval { +public: + RightBoundedInterval(double max, BoundType type); + + const std::string& min() const; + + const std::string& max() const { + return _max; + } + +private: + std::string _max; +}; + +template <> +class UnboundedInterval { +public: + const std::string& min() const; + + const std::string& max() const; +}; + +template <> +class BoundedInterval { +public: + BoundedInterval(const std::string &min, const std::string &max, BoundType type); + + const std::string& min() const { + return _min; + } + + const std::string& max() const { + return _max; + } + +private: + std::string _min; + std::string _max; +}; + +template <> +class LeftBoundedInterval { +public: + LeftBoundedInterval(const std::string &min, BoundType type); + + const std::string& min() const { + return _min; + } + + const std::string& max() const; + +private: + std::string _min; +}; + +template <> +class RightBoundedInterval { +public: + RightBoundedInterval(const std::string &max, BoundType type); + + const std::string& min() const; + + const std::string& max() const { + return _max; + } + +private: + std::string _max; +}; + +struct LimitOptions { + long long offset = 0; + long long count = -1; +}; + +enum class Aggregation { + SUM, + MIN, + MAX +}; + +enum class BitOp { + AND, + OR, + XOR, + NOT +}; + +enum class GeoUnit { + M, + KM, + MI, + FT +}; + +template +struct WithCoord : TupleWithType, T> {}; + +template +struct WithDist : TupleWithType {}; + +template +struct WithHash : TupleWithType {}; + +} + +} + +#endif // end SEWENEW_REDISPLUSPLUS_COMMAND_OPTIONS_H diff --git a/controller/thirdparty/redis-plus-plus-1.1.1/src/sw/redis++/connection.cpp b/controller/thirdparty/redis-plus-plus-1.1.1/src/sw/redis++/connection.cpp new file mode 100644 index 000000000..87c204084 --- /dev/null +++ b/controller/thirdparty/redis-plus-plus-1.1.1/src/sw/redis++/connection.cpp @@ -0,0 +1,305 @@ +/************************************************************************** + Copyright (c) 2017 sewenew + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. + *************************************************************************/ + +#include "connection.h" +#include +#include "reply.h" +#include "command.h" +#include "command_args.h" + +namespace sw { + +namespace redis { + +ConnectionOptions::ConnectionOptions(const std::string &uri) : + ConnectionOptions(_parse_options(uri)) {} + +ConnectionOptions ConnectionOptions::_parse_options(const std::string &uri) const { + std::string type; + std::string path; + std::tie(type, path) = _split_string(uri, "://"); + + if (path.empty()) { + throw Error("Invalid URI: no path"); + } + + if (type == "tcp") { + return _parse_tcp_options(path); + } else if (type == "unix") { + return _parse_unix_options(path); + } else { + throw Error("Invalid URI: invalid type"); + } +} + +ConnectionOptions ConnectionOptions::_parse_tcp_options(const std::string &path) const { + ConnectionOptions options; + + options.type = ConnectionType::TCP; + + std::string host; + std::string port; + std::tie(host, port) = _split_string(path, ":"); + + options.host = host; + try { + if (!port.empty()) { + options.port = std::stoi(port); + } // else use default port, i.e. 6379. + } catch (const std::exception &) { + throw Error("Invalid URL: invalid port"); + } + + return options; +} + +ConnectionOptions ConnectionOptions::_parse_unix_options(const std::string &path) const { + ConnectionOptions options; + + options.type = ConnectionType::UNIX; + options.path = path; + + return options; +} + +auto ConnectionOptions::_split_string(const std::string &str, const std::string &delimiter) const -> + std::pair { + auto pos = str.rfind(delimiter); + if (pos == std::string::npos) { + return {str, ""}; + } + + return {str.substr(0, pos), str.substr(pos + delimiter.size())}; +} + +class Connection::Connector { +public: + explicit Connector(const ConnectionOptions &opts); + + ContextUPtr connect() const; + +private: + ContextUPtr _connect() const; + + redisContext* _connect_tcp() const; + + redisContext* _connect_unix() const; + + void _set_socket_timeout(redisContext &ctx) const; + + void _enable_keep_alive(redisContext &ctx) const; + + timeval _to_timeval(const std::chrono::milliseconds &dur) const; + + const ConnectionOptions &_opts; +}; + +Connection::Connector::Connector(const ConnectionOptions &opts) : _opts(opts) {} + +Connection::ContextUPtr Connection::Connector::connect() const { + auto ctx = _connect(); + + assert(ctx); + + if (ctx->err != REDIS_OK) { + throw_error(*ctx, "Failed to connect to Redis"); + } + + _set_socket_timeout(*ctx); + + _enable_keep_alive(*ctx); + + return ctx; +} + +Connection::ContextUPtr Connection::Connector::_connect() const { + redisContext *context = nullptr; + switch (_opts.type) { + case ConnectionType::TCP: + context = _connect_tcp(); + break; + + case ConnectionType::UNIX: + context = _connect_unix(); + break; + + default: + // Never goes here. + throw Error("Unkonw connection type"); + } + + if (context == nullptr) { + throw Error("Failed to allocate memory for connection."); + } + + return ContextUPtr(context); +} + +redisContext* Connection::Connector::_connect_tcp() const { + if (_opts.connect_timeout > std::chrono::milliseconds(0)) { + return redisConnectWithTimeout(_opts.host.c_str(), + _opts.port, + _to_timeval(_opts.connect_timeout)); + } else { + return redisConnect(_opts.host.c_str(), _opts.port); + } +} + +redisContext* Connection::Connector::_connect_unix() const { + if (_opts.connect_timeout > std::chrono::milliseconds(0)) { + return redisConnectUnixWithTimeout( + _opts.path.c_str(), + _to_timeval(_opts.connect_timeout)); + } else { + return redisConnectUnix(_opts.path.c_str()); + } +} + +void Connection::Connector::_set_socket_timeout(redisContext &ctx) const { + if (_opts.socket_timeout <= std::chrono::milliseconds(0)) { + return; + } + + if (redisSetTimeout(&ctx, _to_timeval(_opts.socket_timeout)) != REDIS_OK) { + throw_error(ctx, "Failed to set socket timeout"); + } +} + +void Connection::Connector::_enable_keep_alive(redisContext &ctx) const { + if (!_opts.keep_alive) { + return; + } + + if (redisEnableKeepAlive(&ctx) != REDIS_OK) { + throw_error(ctx, "Failed to enable keep alive option"); + } +} + +timeval Connection::Connector::_to_timeval(const std::chrono::milliseconds &dur) const { + auto sec = std::chrono::duration_cast(dur); + auto msec = std::chrono::duration_cast(dur - sec); + + return { + static_cast(sec.count()), + static_cast(msec.count()) + }; +} + +void swap(Connection &lhs, Connection &rhs) noexcept { + std::swap(lhs._ctx, rhs._ctx); + std::swap(lhs._last_active, rhs._last_active); + std::swap(lhs._opts, rhs._opts); +} + +Connection::Connection(const ConnectionOptions &opts) : + _ctx(Connector(opts).connect()), + _last_active(std::chrono::steady_clock::now()), + _opts(opts) { + assert(_ctx && !broken()); + + _set_options(); +} + +void Connection::reconnect() { + Connection connection(_opts); + + swap(*this, connection); +} + +void Connection::send(int argc, const char **argv, const std::size_t *argv_len) { + auto ctx = _context(); + + assert(ctx != nullptr); + + if (redisAppendCommandArgv(ctx, + argc, + argv, + argv_len) != REDIS_OK) { + throw_error(*ctx, "Failed to send command"); + } + + assert(!broken()); +} + +void Connection::send(CmdArgs &args) { + auto ctx = _context(); + + assert(ctx != nullptr); + + if (redisAppendCommandArgv(ctx, + args.size(), + args.argv(), + args.argv_len()) != REDIS_OK) { + throw_error(*ctx, "Failed to send command"); + } + + assert(!broken()); +} + +ReplyUPtr Connection::recv() { + auto *ctx = _context(); + + assert(ctx != nullptr); + + void *r = nullptr; + if (redisGetReply(ctx, &r) != REDIS_OK) { + throw_error(*ctx, "Failed to get reply"); + } + + assert(!broken() && r != nullptr); + + auto reply = ReplyUPtr(static_cast(r)); + + if (reply::is_error(*reply)) { + throw_error(*reply); + } + + return reply; +} + +void Connection::_set_options() { + _auth(); + + _select_db(); +} + +void Connection::_auth() { + if (_opts.password.empty()) { + return; + } + + cmd::auth(*this, _opts.password); + + auto reply = recv(); + + reply::parse(*reply); +} + +void Connection::_select_db() { + if (_opts.db == 0) { + return; + } + + cmd::select(*this, _opts.db); + + auto reply = recv(); + + reply::parse(*reply); +} + +} + +} diff --git a/controller/thirdparty/redis-plus-plus-1.1.1/src/sw/redis++/connection.h b/controller/thirdparty/redis-plus-plus-1.1.1/src/sw/redis++/connection.h new file mode 100644 index 000000000..5ad419225 --- /dev/null +++ b/controller/thirdparty/redis-plus-plus-1.1.1/src/sw/redis++/connection.h @@ -0,0 +1,194 @@ +/************************************************************************** + Copyright (c) 2017 sewenew + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. + *************************************************************************/ + +#ifndef SEWENEW_REDISPLUSPLUS_CONNECTION_H +#define SEWENEW_REDISPLUSPLUS_CONNECTION_H + +#include +#include +#include +#include +#include +#include +#include +#include "errors.h" +#include "reply.h" +#include "utils.h" + +namespace sw { + +namespace redis { + +enum class ConnectionType { + TCP = 0, + UNIX +}; + +struct ConnectionOptions { +public: + ConnectionOptions() = default; + + explicit ConnectionOptions(const std::string &uri); + + ConnectionOptions(const ConnectionOptions &) = default; + ConnectionOptions& operator=(const ConnectionOptions &) = default; + + ConnectionOptions(ConnectionOptions &&) = default; + ConnectionOptions& operator=(ConnectionOptions &&) = default; + + ~ConnectionOptions() = default; + + ConnectionType type = ConnectionType::TCP; + + std::string host; + + int port = 6379; + + std::string path; + + std::string password; + + int db = 0; + + bool keep_alive = false; + + std::chrono::milliseconds connect_timeout{0}; + + std::chrono::milliseconds socket_timeout{0}; + +private: + ConnectionOptions _parse_options(const std::string &uri) const; + + ConnectionOptions _parse_tcp_options(const std::string &path) const; + + ConnectionOptions _parse_unix_options(const std::string &path) const; + + auto _split_string(const std::string &str, const std::string &delimiter) const -> + std::pair; +}; + +class CmdArgs; + +class Connection { +public: + explicit Connection(const ConnectionOptions &opts); + + Connection(const Connection &) = delete; + Connection& operator=(const Connection &) = delete; + + Connection(Connection &&) = default; + Connection& operator=(Connection &&) = default; + + ~Connection() = default; + + // Check if the connection is broken. Client needs to do this check + // before sending some command to the connection. If it's broken, + // client needs to reconnect it. + bool broken() const noexcept { + return _ctx->err != REDIS_OK; + } + + void reset() noexcept { + _ctx->err = 0; + } + + void reconnect(); + + auto last_active() const + -> std::chrono::time_point { + return _last_active; + } + + template + void send(const char *format, Args &&...args); + + void send(int argc, const char **argv, const std::size_t *argv_len); + + void send(CmdArgs &args); + + ReplyUPtr recv(); + + const ConnectionOptions& options() const { + return _opts; + } + + friend void swap(Connection &lhs, Connection &rhs) noexcept; + +private: + class Connector; + + struct ContextDeleter { + void operator()(redisContext *context) const { + if (context != nullptr) { + redisFree(context); + } + }; + }; + + using ContextUPtr = std::unique_ptr; + + void _set_options(); + + void _auth(); + + void _select_db(); + + redisContext* _context(); + + ContextUPtr _ctx; + + // The time that the connection is created or the time that + // the connection is used, i.e. *context()* is called. + std::chrono::time_point _last_active{}; + + ConnectionOptions _opts; +}; + +using ConnectionSPtr = std::shared_ptr; + +enum class Role { + MASTER, + SLAVE +}; + +// Inline implementaions. + +template +inline void Connection::send(const char *format, Args &&...args) { + auto ctx = _context(); + + assert(ctx != nullptr); + + if (redisAppendCommand(ctx, + format, + std::forward(args)...) != REDIS_OK) { + throw_error(*ctx, "Failed to send command"); + } + + assert(!broken()); +} + +inline redisContext* Connection::_context() { + _last_active = std::chrono::steady_clock::now(); + + return _ctx.get(); +} + +} + +} + +#endif // end SEWENEW_REDISPLUSPLUS_CONNECTION_H diff --git a/controller/thirdparty/redis-plus-plus-1.1.1/src/sw/redis++/connection_pool.cpp b/controller/thirdparty/redis-plus-plus-1.1.1/src/sw/redis++/connection_pool.cpp new file mode 100644 index 000000000..f00edb027 --- /dev/null +++ b/controller/thirdparty/redis-plus-plus-1.1.1/src/sw/redis++/connection_pool.cpp @@ -0,0 +1,249 @@ +/************************************************************************** + Copyright (c) 2017 sewenew + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. + *************************************************************************/ + +#include "connection_pool.h" +#include +#include "errors.h" + +namespace sw { + +namespace redis { + +ConnectionPool::ConnectionPool(const ConnectionPoolOptions &pool_opts, + const ConnectionOptions &connection_opts) : + _opts(connection_opts), + _pool_opts(pool_opts) { + if (_pool_opts.size == 0) { + throw Error("CANNOT create an empty pool"); + } + + // Lazily create connections. +} + +ConnectionPool::ConnectionPool(SimpleSentinel sentinel, + const ConnectionPoolOptions &pool_opts, + const ConnectionOptions &connection_opts) : + _opts(connection_opts), + _pool_opts(pool_opts), + _sentinel(std::move(sentinel)) { + // In this case, the connection must be of TCP type. + if (_opts.type != ConnectionType::TCP) { + throw Error("Sentinel only supports TCP connection"); + } + + if (_opts.connect_timeout == std::chrono::milliseconds(0) + || _opts.socket_timeout == std::chrono::milliseconds(0)) { + throw Error("With sentinel, connection timeout and socket timeout cannot be 0"); + } + + // Cleanup connection options. + _update_connection_opts("", -1); + + assert(_sentinel); +} + +ConnectionPool::ConnectionPool(ConnectionPool &&that) { + std::lock_guard lock(that._mutex); + + _move(std::move(that)); +} + +ConnectionPool& ConnectionPool::operator=(ConnectionPool &&that) { + if (this != &that) { + std::lock(_mutex, that._mutex); + std::lock_guard lock_this(_mutex, std::adopt_lock); + std::lock_guard lock_that(that._mutex, std::adopt_lock); + + _move(std::move(that)); + } + + return *this; +} + +Connection ConnectionPool::fetch() { + std::unique_lock lock(_mutex); + + if (_pool.empty()) { + if (_used_connections == _pool_opts.size) { + _wait_for_connection(lock); + } else { + // Lazily create a new connection. + auto connection = _create(); + + ++_used_connections; + + return connection; + } + } + + // _pool is NOT empty. + auto connection = _fetch(); + + auto connection_lifetime = _pool_opts.connection_lifetime; + + if (_sentinel) { + auto opts = _opts; + auto role_changed = _role_changed(connection.options()); + auto sentinel = _sentinel; + + lock.unlock(); + + if (role_changed || _need_reconnect(connection, connection_lifetime)) { + try { + connection = _create(sentinel, opts, false); + } catch (const Error &e) { + // Failed to reconnect, return it to the pool, and retry latter. + release(std::move(connection)); + throw; + } + } + + return connection; + } + + lock.unlock(); + + if (_need_reconnect(connection, connection_lifetime)) { + try { + connection.reconnect(); + } catch (const Error &e) { + // Failed to reconnect, return it to the pool, and retry latter. + release(std::move(connection)); + throw; + } + } + + return connection; +} + +ConnectionOptions ConnectionPool::connection_options() { + std::lock_guard lock(_mutex); + + return _opts; +} + +void ConnectionPool::release(Connection connection) { + { + std::lock_guard lock(_mutex); + + _pool.push_back(std::move(connection)); + } + + _cv.notify_one(); +} + +Connection ConnectionPool::create() { + std::unique_lock lock(_mutex); + + auto opts = _opts; + + if (_sentinel) { + auto sentinel = _sentinel; + + lock.unlock(); + + return _create(sentinel, opts, false); + } else { + lock.unlock(); + + return Connection(opts); + } +} + +void ConnectionPool::_move(ConnectionPool &&that) { + _opts = std::move(that._opts); + _pool_opts = std::move(that._pool_opts); + _pool = std::move(that._pool); + _used_connections = that._used_connections; + _sentinel = std::move(that._sentinel); +} + +Connection ConnectionPool::_create() { + if (_sentinel) { + // Get Redis host and port info from sentinel. + return _create(_sentinel, _opts, true); + } + + return Connection(_opts); +} + +Connection ConnectionPool::_create(SimpleSentinel &sentinel, + const ConnectionOptions &opts, + bool locked) { + try { + auto connection = sentinel.create(opts); + + std::unique_lock lock(_mutex, std::defer_lock); + if (!locked) { + lock.lock(); + } + + const auto &connection_opts = connection.options(); + if (_role_changed(connection_opts)) { + // Master/Slave has been changed, reconnect all connections. + _update_connection_opts(connection_opts.host, connection_opts.port); + } + + return connection; + } catch (const StopIterError &e) { + throw Error("Failed to create connection with sentinel"); + } +} + +Connection ConnectionPool::_fetch() { + assert(!_pool.empty()); + + auto connection = std::move(_pool.front()); + _pool.pop_front(); + + return connection; +} + +void ConnectionPool::_wait_for_connection(std::unique_lock &lock) { + auto timeout = _pool_opts.wait_timeout; + if (timeout > std::chrono::milliseconds(0)) { + // Wait until _pool is no longer empty or timeout. + if (!_cv.wait_for(lock, + timeout, + [this] { return !(this->_pool).empty(); })) { + throw Error("Failed to fetch a connection in " + + std::to_string(timeout.count()) + " milliseconds"); + } + } else { + // Wait forever. + _cv.wait(lock, [this] { return !(this->_pool).empty(); }); + } +} + +bool ConnectionPool::_need_reconnect(const Connection &connection, + const std::chrono::milliseconds &connection_lifetime) const { + if (connection.broken()) { + return true; + } + + if (connection_lifetime > std::chrono::milliseconds(0)) { + auto now = std::chrono::steady_clock::now(); + if (now - connection.last_active() > connection_lifetime) { + return true; + } + } + + return false; +} + +} + +} diff --git a/controller/thirdparty/redis-plus-plus-1.1.1/src/sw/redis++/connection_pool.h b/controller/thirdparty/redis-plus-plus-1.1.1/src/sw/redis++/connection_pool.h new file mode 100644 index 000000000..6f2663ad7 --- /dev/null +++ b/controller/thirdparty/redis-plus-plus-1.1.1/src/sw/redis++/connection_pool.h @@ -0,0 +1,115 @@ +/************************************************************************** + Copyright (c) 2017 sewenew + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. + *************************************************************************/ + +#ifndef SEWENEW_REDISPLUSPLUS_CONNECTION_POOL_H +#define SEWENEW_REDISPLUSPLUS_CONNECTION_POOL_H + +#include +#include +#include +#include +#include +#include "connection.h" +#include "sentinel.h" + +namespace sw { + +namespace redis { + +struct ConnectionPoolOptions { + // Max number of connections, including both in-use and idle ones. + std::size_t size = 1; + + // Max time to wait for a connection. 0ms means client waits forever. + std::chrono::milliseconds wait_timeout{0}; + + // Max lifetime of a connection. 0ms means we never expire the connection. + std::chrono::milliseconds connection_lifetime{0}; +}; + +class ConnectionPool { +public: + ConnectionPool(const ConnectionPoolOptions &pool_opts, + const ConnectionOptions &connection_opts); + + ConnectionPool(SimpleSentinel sentinel, + const ConnectionPoolOptions &pool_opts, + const ConnectionOptions &connection_opts); + + ConnectionPool() = default; + + ConnectionPool(ConnectionPool &&that); + ConnectionPool& operator=(ConnectionPool &&that); + + ConnectionPool(const ConnectionPool &) = delete; + ConnectionPool& operator=(const ConnectionPool &) = delete; + + ~ConnectionPool() = default; + + // Fetch a connection from pool. + Connection fetch(); + + ConnectionOptions connection_options(); + + void release(Connection connection); + + // Create a new connection. + Connection create(); + +private: + void _move(ConnectionPool &&that); + + // NOT thread-safe + Connection _create(); + + Connection _create(SimpleSentinel &sentinel, const ConnectionOptions &opts, bool locked); + + Connection _fetch(); + + void _wait_for_connection(std::unique_lock &lock); + + bool _need_reconnect(const Connection &connection, + const std::chrono::milliseconds &connection_lifetime) const; + + void _update_connection_opts(const std::string &host, int port) { + _opts.host = host; + _opts.port = port; + } + + bool _role_changed(const ConnectionOptions &opts) const { + return opts.port != _opts.port || opts.host != _opts.host; + } + + ConnectionOptions _opts; + + ConnectionPoolOptions _pool_opts; + + std::deque _pool; + + std::size_t _used_connections = 0; + + std::mutex _mutex; + + std::condition_variable _cv; + + SimpleSentinel _sentinel; +}; + +} + +} + +#endif // end SEWENEW_REDISPLUSPLUS_CONNECTION_POOL_H diff --git a/controller/thirdparty/redis-plus-plus-1.1.1/src/sw/redis++/crc16.cpp b/controller/thirdparty/redis-plus-plus-1.1.1/src/sw/redis++/crc16.cpp new file mode 100644 index 000000000..c94a08d30 --- /dev/null +++ b/controller/thirdparty/redis-plus-plus-1.1.1/src/sw/redis++/crc16.cpp @@ -0,0 +1,96 @@ +/* + * Copyright 2001-2010 Georges Menie (www.menie.org) + * Copyright 2010-2012 Salvatore Sanfilippo (adapted to Redis coding style) + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * * Neither the name of the University of California, Berkeley nor the + * names of its contributors may be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND ANY + * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE REGENTS AND CONTRIBUTORS BE LIABLE FOR ANY + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +/* CRC16 implementation according to CCITT standards. + * + * Note by @antirez: this is actually the XMODEM CRC 16 algorithm, using the + * following parameters: + * + * Name : "XMODEM", also known as "ZMODEM", "CRC-16/ACORN" + * Width : 16 bit + * Poly : 1021 (That is actually x^16 + x^12 + x^5 + 1) + * Initialization : 0000 + * Reflect Input byte : False + * Reflect Output CRC : False + * Xor constant to output CRC : 0000 + * Output for "123456789" : 31C3 + */ + +#include + +namespace sw { + +namespace redis { + +static const uint16_t crc16tab[256]= { + 0x0000,0x1021,0x2042,0x3063,0x4084,0x50a5,0x60c6,0x70e7, + 0x8108,0x9129,0xa14a,0xb16b,0xc18c,0xd1ad,0xe1ce,0xf1ef, + 0x1231,0x0210,0x3273,0x2252,0x52b5,0x4294,0x72f7,0x62d6, + 0x9339,0x8318,0xb37b,0xa35a,0xd3bd,0xc39c,0xf3ff,0xe3de, + 0x2462,0x3443,0x0420,0x1401,0x64e6,0x74c7,0x44a4,0x5485, + 0xa56a,0xb54b,0x8528,0x9509,0xe5ee,0xf5cf,0xc5ac,0xd58d, + 0x3653,0x2672,0x1611,0x0630,0x76d7,0x66f6,0x5695,0x46b4, + 0xb75b,0xa77a,0x9719,0x8738,0xf7df,0xe7fe,0xd79d,0xc7bc, + 0x48c4,0x58e5,0x6886,0x78a7,0x0840,0x1861,0x2802,0x3823, + 0xc9cc,0xd9ed,0xe98e,0xf9af,0x8948,0x9969,0xa90a,0xb92b, + 0x5af5,0x4ad4,0x7ab7,0x6a96,0x1a71,0x0a50,0x3a33,0x2a12, + 0xdbfd,0xcbdc,0xfbbf,0xeb9e,0x9b79,0x8b58,0xbb3b,0xab1a, + 0x6ca6,0x7c87,0x4ce4,0x5cc5,0x2c22,0x3c03,0x0c60,0x1c41, + 0xedae,0xfd8f,0xcdec,0xddcd,0xad2a,0xbd0b,0x8d68,0x9d49, + 0x7e97,0x6eb6,0x5ed5,0x4ef4,0x3e13,0x2e32,0x1e51,0x0e70, + 0xff9f,0xefbe,0xdfdd,0xcffc,0xbf1b,0xaf3a,0x9f59,0x8f78, + 0x9188,0x81a9,0xb1ca,0xa1eb,0xd10c,0xc12d,0xf14e,0xe16f, + 0x1080,0x00a1,0x30c2,0x20e3,0x5004,0x4025,0x7046,0x6067, + 0x83b9,0x9398,0xa3fb,0xb3da,0xc33d,0xd31c,0xe37f,0xf35e, + 0x02b1,0x1290,0x22f3,0x32d2,0x4235,0x5214,0x6277,0x7256, + 0xb5ea,0xa5cb,0x95a8,0x8589,0xf56e,0xe54f,0xd52c,0xc50d, + 0x34e2,0x24c3,0x14a0,0x0481,0x7466,0x6447,0x5424,0x4405, + 0xa7db,0xb7fa,0x8799,0x97b8,0xe75f,0xf77e,0xc71d,0xd73c, + 0x26d3,0x36f2,0x0691,0x16b0,0x6657,0x7676,0x4615,0x5634, + 0xd94c,0xc96d,0xf90e,0xe92f,0x99c8,0x89e9,0xb98a,0xa9ab, + 0x5844,0x4865,0x7806,0x6827,0x18c0,0x08e1,0x3882,0x28a3, + 0xcb7d,0xdb5c,0xeb3f,0xfb1e,0x8bf9,0x9bd8,0xabbb,0xbb9a, + 0x4a75,0x5a54,0x6a37,0x7a16,0x0af1,0x1ad0,0x2ab3,0x3a92, + 0xfd2e,0xed0f,0xdd6c,0xcd4d,0xbdaa,0xad8b,0x9de8,0x8dc9, + 0x7c26,0x6c07,0x5c64,0x4c45,0x3ca2,0x2c83,0x1ce0,0x0cc1, + 0xef1f,0xff3e,0xcf5d,0xdf7c,0xaf9b,0xbfba,0x8fd9,0x9ff8, + 0x6e17,0x7e36,0x4e55,0x5e74,0x2e93,0x3eb2,0x0ed1,0x1ef0 +}; + +uint16_t crc16(const char *buf, int len) { + int counter; + uint16_t crc = 0; + for (counter = 0; counter < len; counter++) + crc = (crc<<8) ^ crc16tab[((crc>>8) ^ *buf++)&0x00FF]; + return crc; +} + +} + +} diff --git a/controller/thirdparty/redis-plus-plus-1.1.1/src/sw/redis++/errors.cpp b/controller/thirdparty/redis-plus-plus-1.1.1/src/sw/redis++/errors.cpp new file mode 100644 index 000000000..5d5fe7dc9 --- /dev/null +++ b/controller/thirdparty/redis-plus-plus-1.1.1/src/sw/redis++/errors.cpp @@ -0,0 +1,136 @@ +/************************************************************************** + Copyright (c) 2017 sewenew + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. + *************************************************************************/ + +#include "errors.h" +#include +#include +#include +#include +#include "shards.h" + +namespace { + +using namespace sw::redis; + +std::pair parse_error(const std::string &msg); + +std::unordered_map error_map = { + {"MOVED", ReplyErrorType::MOVED}, + {"ASK", ReplyErrorType::ASK} +}; + +} + +namespace sw { + +namespace redis { + +void throw_error(redisContext &context, const std::string &err_info) { + auto err_code = context.err; + const auto *err_str = context.errstr; + if (err_str == nullptr) { + throw Error(err_info + ": null error message: " + std::to_string(err_code)); + } + + auto err_msg = err_info + ": " + err_str; + + switch (err_code) { + case REDIS_ERR_IO: + if (errno == EAGAIN || errno == EINTR) { + throw TimeoutError(err_msg); + } else { + throw IoError(err_msg); + } + break; + + case REDIS_ERR_EOF: + throw ClosedError(err_msg); + break; + + case REDIS_ERR_PROTOCOL: + throw ProtoError(err_msg); + break; + + case REDIS_ERR_OOM: + throw OomError(err_msg); + break; + + case REDIS_ERR_OTHER: + throw Error(err_msg); + break; + + default: + throw Error(err_info + ": Unknown error code"); + } +} + +void throw_error(const redisReply &reply) { + assert(reply.type == REDIS_REPLY_ERROR); + + if (reply.str == nullptr) { + throw Error("Null error reply"); + } + + auto err_str = std::string(reply.str, reply.len); + + auto err_type = ReplyErrorType::ERR; + std::string err_msg; + std::tie(err_type, err_msg) = parse_error(err_str); + + switch (err_type) { + case ReplyErrorType::MOVED: + throw MovedError(err_msg); + break; + + case ReplyErrorType::ASK: + throw AskError(err_msg); + break; + + default: + throw ReplyError(err_str); + break; + } +} + +} + +} + +namespace { + +using namespace sw::redis; + +std::pair parse_error(const std::string &err) { + // The error contains an Error Prefix, and an optional error message. + auto idx = err.find_first_of(" \n"); + + if (idx == std::string::npos) { + throw ProtoError("No Error Prefix: " + err); + } + + auto err_prefix = err.substr(0, idx); + auto err_type = ReplyErrorType::ERR; + + auto iter = error_map.find(err_prefix); + if (iter != error_map.end()) { + // Specific error. + err_type = iter->second; + } // else Generic error. + + return {err_type, err.substr(idx + 1)}; +} + +} diff --git a/controller/thirdparty/redis-plus-plus-1.1.1/src/sw/redis++/errors.h b/controller/thirdparty/redis-plus-plus-1.1.1/src/sw/redis++/errors.h new file mode 100644 index 000000000..44d629e50 --- /dev/null +++ b/controller/thirdparty/redis-plus-plus-1.1.1/src/sw/redis++/errors.h @@ -0,0 +1,159 @@ +/************************************************************************** + Copyright (c) 2017 sewenew + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. + *************************************************************************/ + +#ifndef SEWENEW_REDISPLUSPLUS_ERRORS_H +#define SEWENEW_REDISPLUSPLUS_ERRORS_H + +#include +#include +#include + +namespace sw { + +namespace redis { + +enum ReplyErrorType { + ERR, + MOVED, + ASK +}; + +class Error : public std::exception { +public: + explicit Error(const std::string &msg) : _msg(msg) {} + + Error(const Error &) = default; + Error& operator=(const Error &) = default; + + Error(Error &&) = default; + Error& operator=(Error &&) = default; + + virtual ~Error() = default; + + virtual const char* what() const noexcept { + return _msg.data(); + } + +private: + std::string _msg; +}; + +class IoError : public Error { +public: + explicit IoError(const std::string &msg) : Error(msg) {} + + IoError(const IoError &) = default; + IoError& operator=(const IoError &) = default; + + IoError(IoError &&) = default; + IoError& operator=(IoError &&) = default; + + virtual ~IoError() = default; +}; + +class TimeoutError : public IoError { +public: + explicit TimeoutError(const std::string &msg) : IoError(msg) {} + + TimeoutError(const TimeoutError &) = default; + TimeoutError& operator=(const TimeoutError &) = default; + + TimeoutError(TimeoutError &&) = default; + TimeoutError& operator=(TimeoutError &&) = default; + + virtual ~TimeoutError() = default; +}; + +class ClosedError : public Error { +public: + explicit ClosedError(const std::string &msg) : Error(msg) {} + + ClosedError(const ClosedError &) = default; + ClosedError& operator=(const ClosedError &) = default; + + ClosedError(ClosedError &&) = default; + ClosedError& operator=(ClosedError &&) = default; + + virtual ~ClosedError() = default; +}; + +class ProtoError : public Error { +public: + explicit ProtoError(const std::string &msg) : Error(msg) {} + + ProtoError(const ProtoError &) = default; + ProtoError& operator=(const ProtoError &) = default; + + ProtoError(ProtoError &&) = default; + ProtoError& operator=(ProtoError &&) = default; + + virtual ~ProtoError() = default; +}; + +class OomError : public Error { +public: + explicit OomError(const std::string &msg) : Error(msg) {} + + OomError(const OomError &) = default; + OomError& operator=(const OomError &) = default; + + OomError(OomError &&) = default; + OomError& operator=(OomError &&) = default; + + virtual ~OomError() = default; +}; + +class ReplyError : public Error { +public: + explicit ReplyError(const std::string &msg) : Error(msg) {} + + ReplyError(const ReplyError &) = default; + ReplyError& operator=(const ReplyError &) = default; + + ReplyError(ReplyError &&) = default; + ReplyError& operator=(ReplyError &&) = default; + + virtual ~ReplyError() = default; +}; + +class WatchError : public Error { +public: + explicit WatchError() : Error("Watched key has been modified") {} + + WatchError(const WatchError &) = default; + WatchError& operator=(const WatchError &) = default; + + WatchError(WatchError &&) = default; + WatchError& operator=(WatchError &&) = default; + + virtual ~WatchError() = default; +}; + + +// MovedError and AskError are defined in shards.h +class MovedError; + +class AskError; + +void throw_error(redisContext &context, const std::string &err_info); + +void throw_error(const redisReply &reply); + +} + +} + +#endif // end SEWENEW_REDISPLUSPLUS_ERRORS_H diff --git a/controller/thirdparty/redis-plus-plus-1.1.1/src/sw/redis++/pipeline.cpp b/controller/thirdparty/redis-plus-plus-1.1.1/src/sw/redis++/pipeline.cpp new file mode 100644 index 000000000..141f96ea2 --- /dev/null +++ b/controller/thirdparty/redis-plus-plus-1.1.1/src/sw/redis++/pipeline.cpp @@ -0,0 +1,35 @@ +/************************************************************************** + Copyright (c) 2017 sewenew + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. + *************************************************************************/ + +#include "pipeline.h" + +namespace sw { + +namespace redis { + +std::vector PipelineImpl::exec(Connection &connection, std::size_t cmd_num) { + std::vector replies; + while (cmd_num > 0) { + replies.push_back(connection.recv()); + --cmd_num; + } + + return replies; +} + +} + +} diff --git a/controller/thirdparty/redis-plus-plus-1.1.1/src/sw/redis++/pipeline.h b/controller/thirdparty/redis-plus-plus-1.1.1/src/sw/redis++/pipeline.h new file mode 100644 index 000000000..52b01253f --- /dev/null +++ b/controller/thirdparty/redis-plus-plus-1.1.1/src/sw/redis++/pipeline.h @@ -0,0 +1,49 @@ +/************************************************************************** + Copyright (c) 2017 sewenew + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. + *************************************************************************/ + +#ifndef SEWENEW_REDISPLUSPLUS_PIPELINE_H +#define SEWENEW_REDISPLUSPLUS_PIPELINE_H + +#include +#include +#include "connection.h" + +namespace sw { + +namespace redis { + +class PipelineImpl { +public: + template + void command(Connection &connection, Cmd cmd, Args &&...args) { + assert(!connection.broken()); + + cmd(connection, std::forward(args)...); + } + + std::vector exec(Connection &connection, std::size_t cmd_num); + + void discard(Connection &connection, std::size_t /*cmd_num*/) { + // Reconnect to Redis to discard all commands. + connection.reconnect(); + } +}; + +} + +} + +#endif // end SEWENEW_REDISPLUSPLUS_PIPELINE_H diff --git a/controller/thirdparty/redis-plus-plus-1.1.1/src/sw/redis++/queued_redis.h b/controller/thirdparty/redis-plus-plus-1.1.1/src/sw/redis++/queued_redis.h new file mode 100644 index 000000000..71d975ee3 --- /dev/null +++ b/controller/thirdparty/redis-plus-plus-1.1.1/src/sw/redis++/queued_redis.h @@ -0,0 +1,1844 @@ +/************************************************************************** + Copyright (c) 2017 sewenew + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. + *************************************************************************/ + +#ifndef SEWENEW_REDISPLUSPLUS_QUEUED_REDIS_H +#define SEWENEW_REDISPLUSPLUS_QUEUED_REDIS_H + +#include +#include +#include +#include +#include "connection.h" +#include "utils.h" +#include "reply.h" +#include "command.h" +#include "redis.h" + +namespace sw { + +namespace redis { + +class QueuedReplies; + +// If any command throws, QueuedRedis resets the connection, and becomes invalid. +// In this case, the only thing we can do is to destory the QueuedRedis object. +template +class QueuedRedis { +public: + QueuedRedis(QueuedRedis &&) = default; + QueuedRedis& operator=(QueuedRedis &&) = default; + + // When it destructs, the underlying *Connection* will be closed, + // and any command that has NOT been executed will be ignored. + ~QueuedRedis() = default; + + Redis redis(); + + template + auto command(Cmd cmd, Args &&...args) + -> typename std::enable_if::value, + QueuedRedis&>::type; + + template + QueuedRedis& command(const StringView &cmd_name, Args &&...args); + + template + auto command(Input first, Input last) + -> typename std::enable_if::value, QueuedRedis&>::type; + + QueuedReplies exec(); + + void discard(); + + // CONNECTION commands. + + QueuedRedis& auth(const StringView &password) { + return command(cmd::auth, password); + } + + QueuedRedis& echo(const StringView &msg) { + return command(cmd::echo, msg); + } + + QueuedRedis& ping() { + return command(cmd::ping); + } + + QueuedRedis& ping(const StringView &msg) { + return command(cmd::ping, msg); + } + + // We DO NOT support the QUIT command. See *Redis::quit* doc for details. + // + // QueuedRedis& quit(); + + QueuedRedis& select(long long idx) { + return command(cmd::select, idx); + } + + QueuedRedis& swapdb(long long idx1, long long idx2) { + return command(cmd::swapdb, idx1, idx2); + } + + // SERVER commands. + + QueuedRedis& bgrewriteaof() { + return command(cmd::bgrewriteaof); + } + + QueuedRedis& bgsave() { + return command(cmd::bgsave); + } + + QueuedRedis& dbsize() { + return command(cmd::dbsize); + } + + QueuedRedis& flushall(bool async = false) { + return command(cmd::flushall, async); + } + + QueuedRedis& flushdb(bool async = false) { + return command(cmd::flushdb, async); + } + + QueuedRedis& info() { + return command(cmd::info); + } + + QueuedRedis& info(const StringView §ion) { + return command(cmd::info, section); + } + + QueuedRedis& lastsave() { + return command(cmd::lastsave); + } + + QueuedRedis& save() { + return command(cmd::save); + } + + // KEY commands. + + QueuedRedis& del(const StringView &key) { + return command(cmd::del, key); + } + + template + QueuedRedis& del(Input first, Input last) { + return command(cmd::del_range, first, last); + } + + template + QueuedRedis& del(std::initializer_list il) { + return del(il.begin(), il.end()); + } + + QueuedRedis& dump(const StringView &key) { + return command(cmd::dump, key); + } + + QueuedRedis& exists(const StringView &key) { + return command(cmd::exists, key); + } + + template + QueuedRedis& exists(Input first, Input last) { + return command(cmd::exists_range, first, last); + } + + template + QueuedRedis& exists(std::initializer_list il) { + return exists(il.begin(), il.end()); + } + + QueuedRedis& expire(const StringView &key, long long timeout) { + return command(cmd::expire, key, timeout); + } + + QueuedRedis& expire(const StringView &key, + const std::chrono::seconds &timeout) { + return expire(key, timeout.count()); + } + + QueuedRedis& expireat(const StringView &key, long long timestamp) { + return command(cmd::expireat, key, timestamp); + } + + QueuedRedis& expireat(const StringView &key, + const std::chrono::time_point &tp) { + return expireat(key, tp.time_since_epoch().count()); + } + + QueuedRedis& keys(const StringView &pattern) { + return command(cmd::keys, pattern); + } + + QueuedRedis& move(const StringView &key, long long db) { + return command(cmd::move, key, db); + } + + QueuedRedis& persist(const StringView &key) { + return command(cmd::persist, key); + } + + QueuedRedis& pexpire(const StringView &key, long long timeout) { + return command(cmd::pexpire, key, timeout); + } + + QueuedRedis& pexpire(const StringView &key, + const std::chrono::milliseconds &timeout) { + return pexpire(key, timeout.count()); + } + + QueuedRedis& pexpireat(const StringView &key, long long timestamp) { + return command(cmd::pexpireat, key, timestamp); + } + + QueuedRedis& pexpireat(const StringView &key, + const std::chrono::time_point &tp) { + return pexpireat(key, tp.time_since_epoch().count()); + } + + QueuedRedis& pttl(const StringView &key) { + return command(cmd::pttl, key); + } + + QueuedRedis& randomkey() { + return command(cmd::randomkey); + } + + QueuedRedis& rename(const StringView &key, const StringView &newkey) { + return command(cmd::rename, key, newkey); + } + + QueuedRedis& renamenx(const StringView &key, const StringView &newkey) { + return command(cmd::renamenx, key, newkey); + } + + QueuedRedis& restore(const StringView &key, + const StringView &val, + long long ttl, + bool replace = false) { + return command(cmd::restore, key, val, ttl, replace); + } + + QueuedRedis& restore(const StringView &key, + const StringView &val, + const std::chrono::milliseconds &ttl = std::chrono::milliseconds{0}, + bool replace = false) { + return restore(key, val, ttl.count(), replace); + } + + // TODO: sort + + QueuedRedis& scan(long long cursor, + const StringView &pattern, + long long count) { + return command(cmd::scan, cursor, pattern, count); + } + + QueuedRedis& scan(long long cursor) { + return scan(cursor, "*", 10); + } + + QueuedRedis& scan(long long cursor, + const StringView &pattern) { + return scan(cursor, pattern, 10); + } + + QueuedRedis& scan(long long cursor, + long long count) { + return scan(cursor, "*", count); + } + + QueuedRedis& touch(const StringView &key) { + return command(cmd::touch, key); + } + + template + QueuedRedis& touch(Input first, Input last) { + return command(cmd::touch_range, first, last); + } + + template + QueuedRedis& touch(std::initializer_list il) { + return touch(il.begin(), il.end()); + } + + QueuedRedis& ttl(const StringView &key) { + return command(cmd::ttl, key); + } + + QueuedRedis& type(const StringView &key) { + return command(cmd::type, key); + } + + QueuedRedis& unlink(const StringView &key) { + return command(cmd::unlink, key); + } + + template + QueuedRedis& unlink(Input first, Input last) { + return command(cmd::unlink_range, first, last); + } + + template + QueuedRedis& unlink(std::initializer_list il) { + return unlink(il.begin(), il.end()); + } + + QueuedRedis& wait(long long numslaves, long long timeout) { + return command(cmd::wait, numslaves, timeout); + } + + QueuedRedis& wait(long long numslaves, const std::chrono::milliseconds &timeout) { + return wait(numslaves, timeout.count()); + } + + // STRING commands. + + QueuedRedis& append(const StringView &key, const StringView &str) { + return command(cmd::append, key, str); + } + + QueuedRedis& bitcount(const StringView &key, + long long start = 0, + long long end = -1) { + return command(cmd::bitcount, key, start, end); + } + + QueuedRedis& bitop(BitOp op, + const StringView &destination, + const StringView &key) { + return command(cmd::bitop, op, destination, key); + } + + template + QueuedRedis& bitop(BitOp op, + const StringView &destination, + Input first, + Input last) { + return command(cmd::bitop_range, op, destination, first, last); + } + + template + QueuedRedis& bitop(BitOp op, + const StringView &destination, + std::initializer_list il) { + return bitop(op, destination, il.begin(), il.end()); + } + + QueuedRedis& bitpos(const StringView &key, + long long bit, + long long start = 0, + long long end = -1) { + return command(cmd::bitpos, key, bit, start, end); + } + + QueuedRedis& decr(const StringView &key) { + return command(cmd::decr, key); + } + + QueuedRedis& decrby(const StringView &key, long long decrement) { + return command(cmd::decrby, key, decrement); + } + + QueuedRedis& get(const StringView &key) { + return command(cmd::get, key); + } + + QueuedRedis& getbit(const StringView &key, long long offset) { + return command(cmd::getbit, key, offset); + } + + QueuedRedis& getrange(const StringView &key, long long start, long long end) { + return command(cmd::getrange, key, start, end); + } + + QueuedRedis& getset(const StringView &key, const StringView &val) { + return command(cmd::getset, key, val); + } + + QueuedRedis& incr(const StringView &key) { + return command(cmd::incr, key); + } + + QueuedRedis& incrby(const StringView &key, long long increment) { + return command(cmd::incrby, key, increment); + } + + QueuedRedis& incrbyfloat(const StringView &key, double increment) { + return command(cmd::incrbyfloat, key, increment); + } + + template + QueuedRedis& mget(Input first, Input last) { + return command(cmd::mget, first, last); + } + + template + QueuedRedis& mget(std::initializer_list il) { + return mget(il.begin(), il.end()); + } + + template + QueuedRedis& mset(Input first, Input last) { + return command(cmd::mset, first, last); + } + + template + QueuedRedis& mset(std::initializer_list il) { + return mset(il.begin(), il.end()); + } + + template + QueuedRedis& msetnx(Input first, Input last) { + return command(cmd::msetnx, first, last); + } + + template + QueuedRedis& msetnx(std::initializer_list il) { + return msetnx(il.begin(), il.end()); + } + + QueuedRedis& psetex(const StringView &key, + long long ttl, + const StringView &val) { + return command(cmd::psetex, key, ttl, val); + } + + QueuedRedis& psetex(const StringView &key, + const std::chrono::milliseconds &ttl, + const StringView &val) { + return psetex(key, ttl.count(), val); + } + + QueuedRedis& set(const StringView &key, + const StringView &val, + const std::chrono::milliseconds &ttl = std::chrono::milliseconds(0), + UpdateType type = UpdateType::ALWAYS) { + _set_cmd_indexes.push_back(_cmd_num); + + return command(cmd::set, key, val, ttl.count(), type); + } + + QueuedRedis& setex(const StringView &key, + long long ttl, + const StringView &val) { + return command(cmd::setex, key, ttl, val); + } + + QueuedRedis& setex(const StringView &key, + const std::chrono::seconds &ttl, + const StringView &val) { + return setex(key, ttl.count(), val); + } + + QueuedRedis& setnx(const StringView &key, const StringView &val) { + return command(cmd::setnx, key, val); + } + + QueuedRedis& setrange(const StringView &key, + long long offset, + const StringView &val) { + return command(cmd::setrange, key, offset, val); + } + + QueuedRedis& strlen(const StringView &key) { + return command(cmd::strlen, key); + } + + // LIST commands. + + QueuedRedis& blpop(const StringView &key, long long timeout) { + return command(cmd::blpop, key, timeout); + } + + QueuedRedis& blpop(const StringView &key, + const std::chrono::seconds &timeout = std::chrono::seconds{0}) { + return blpop(key, timeout.count()); + } + + template + QueuedRedis& blpop(Input first, Input last, long long timeout) { + return command(cmd::blpop_range, first, last, timeout); + } + + template + QueuedRedis& blpop(std::initializer_list il, long long timeout) { + return blpop(il.begin(), il.end(), timeout); + } + + template + QueuedRedis& blpop(Input first, + Input last, + const std::chrono::seconds &timeout = std::chrono::seconds{0}) { + return blpop(first, last, timeout.count()); + } + + template + QueuedRedis& blpop(std::initializer_list il, + const std::chrono::seconds &timeout = std::chrono::seconds{0}) { + return blpop(il.begin(), il.end(), timeout); + } + + QueuedRedis& brpop(const StringView &key, long long timeout) { + return command(cmd::brpop, key, timeout); + } + + QueuedRedis& brpop(const StringView &key, + const std::chrono::seconds &timeout = std::chrono::seconds{0}) { + return brpop(key, timeout.count()); + } + + template + QueuedRedis& brpop(Input first, Input last, long long timeout) { + return command(cmd::brpop_range, first, last, timeout); + } + + template + QueuedRedis& brpop(std::initializer_list il, long long timeout) { + return brpop(il.begin(), il.end(), timeout); + } + + template + QueuedRedis& brpop(Input first, + Input last, + const std::chrono::seconds &timeout = std::chrono::seconds{0}) { + return brpop(first, last, timeout.count()); + } + + template + QueuedRedis& brpop(std::initializer_list il, + const std::chrono::seconds &timeout = std::chrono::seconds{0}) { + return brpop(il.begin(), il.end(), timeout); + } + + QueuedRedis& brpoplpush(const StringView &source, + const StringView &destination, + long long timeout) { + return command(cmd::brpoplpush, source, destination, timeout); + } + + QueuedRedis& brpoplpush(const StringView &source, + const StringView &destination, + const std::chrono::seconds &timeout = std::chrono::seconds{0}) { + return brpoplpush(source, destination, timeout.count()); + } + + QueuedRedis& lindex(const StringView &key, long long index) { + return command(cmd::lindex, key, index); + } + + QueuedRedis& linsert(const StringView &key, + InsertPosition position, + const StringView &pivot, + const StringView &val) { + return command(cmd::linsert, key, position, pivot, val); + } + + QueuedRedis& llen(const StringView &key) { + return command(cmd::llen, key); + } + + QueuedRedis& lpop(const StringView &key) { + return command(cmd::lpop, key); + } + + QueuedRedis& lpush(const StringView &key, const StringView &val) { + return command(cmd::lpush, key, val); + } + + template + QueuedRedis& lpush(const StringView &key, Input first, Input last) { + return command(cmd::lpush_range, key, first, last); + } + + template + QueuedRedis& lpush(const StringView &key, std::initializer_list il) { + return lpush(key, il.begin(), il.end()); + } + + QueuedRedis& lpushx(const StringView &key, const StringView &val) { + return command(cmd::lpushx, key, val); + } + + QueuedRedis& lrange(const StringView &key, + long long start, + long long stop) { + return command(cmd::lrange, key, start, stop); + } + + QueuedRedis& lrem(const StringView &key, long long count, const StringView &val) { + return command(cmd::lrem, key, count, val); + } + + QueuedRedis& lset(const StringView &key, long long index, const StringView &val) { + return command(cmd::lset, key, index, val); + } + + QueuedRedis& ltrim(const StringView &key, long long start, long long stop) { + return command(cmd::ltrim, key, start, stop); + } + + QueuedRedis& rpop(const StringView &key) { + return command(cmd::rpop, key); + } + + QueuedRedis& rpoplpush(const StringView &source, const StringView &destination) { + return command(cmd::rpoplpush, source, destination); + } + + QueuedRedis& rpush(const StringView &key, const StringView &val) { + return command(cmd::rpush, key, val); + } + + template + QueuedRedis& rpush(const StringView &key, Input first, Input last) { + return command(cmd::rpush_range, key, first, last); + } + + template + QueuedRedis& rpush(const StringView &key, std::initializer_list il) { + return rpush(key, il.begin(), il.end()); + } + + QueuedRedis& rpushx(const StringView &key, const StringView &val) { + return command(cmd::rpushx, key, val); + } + + // HASH commands. + + QueuedRedis& hdel(const StringView &key, const StringView &field) { + return command(cmd::hdel, key, field); + } + + template + QueuedRedis& hdel(const StringView &key, Input first, Input last) { + return command(cmd::hdel_range, key, first, last); + } + + template + QueuedRedis& hdel(const StringView &key, std::initializer_list il) { + return hdel(key, il.begin(), il.end()); + } + + QueuedRedis& hexists(const StringView &key, const StringView &field) { + return command(cmd::hexists, key, field); + } + + QueuedRedis& hget(const StringView &key, const StringView &field) { + return command(cmd::hget, key, field); + } + + QueuedRedis& hgetall(const StringView &key) { + return command(cmd::hgetall, key); + } + + QueuedRedis& hincrby(const StringView &key, + const StringView &field, + long long increment) { + return command(cmd::hincrby, key, field, increment); + } + + QueuedRedis& hincrbyfloat(const StringView &key, + const StringView &field, + double increment) { + return command(cmd::hincrbyfloat, key, field, increment); + } + + QueuedRedis& hkeys(const StringView &key) { + return command(cmd::hkeys, key); + } + + QueuedRedis& hlen(const StringView &key) { + return command(cmd::hlen, key); + } + + template + QueuedRedis& hmget(const StringView &key, Input first, Input last) { + return command(cmd::hmget, key, first, last); + } + + template + QueuedRedis& hmget(const StringView &key, std::initializer_list il) { + return hmget(key, il.begin(), il.end()); + } + + template + QueuedRedis& hmset(const StringView &key, Input first, Input last) { + return command(cmd::hmset, key, first, last); + } + + template + QueuedRedis& hmset(const StringView &key, std::initializer_list il) { + return hmset(key, il.begin(), il.end()); + } + + QueuedRedis& hscan(const StringView &key, + long long cursor, + const StringView &pattern, + long long count) { + return command(cmd::hscan, key, cursor, pattern, count); + } + + QueuedRedis& hscan(const StringView &key, + long long cursor, + const StringView &pattern) { + return hscan(key, cursor, pattern, 10); + } + + QueuedRedis& hscan(const StringView &key, + long long cursor, + long long count) { + return hscan(key, cursor, "*", count); + } + + QueuedRedis& hscan(const StringView &key, + long long cursor) { + return hscan(key, cursor, "*", 10); + } + + QueuedRedis& hset(const StringView &key, const StringView &field, const StringView &val) { + return command(cmd::hset, key, field, val); + } + + QueuedRedis& hset(const StringView &key, const std::pair &item) { + return hset(key, item.first, item.second); + } + + QueuedRedis& hsetnx(const StringView &key, const StringView &field, const StringView &val) { + return command(cmd::hsetnx, key, field, val); + } + + QueuedRedis& hsetnx(const StringView &key, const std::pair &item) { + return hsetnx(key, item.first, item.second); + } + + QueuedRedis& hstrlen(const StringView &key, const StringView &field) { + return command(cmd::hstrlen, key, field); + } + + QueuedRedis& hvals(const StringView &key) { + return command(cmd::hvals, key); + } + + // SET commands. + + QueuedRedis& sadd(const StringView &key, const StringView &member) { + return command(cmd::sadd, key, member); + } + + template + QueuedRedis& sadd(const StringView &key, Input first, Input last) { + return command(cmd::sadd_range, key, first, last); + } + + template + QueuedRedis& sadd(const StringView &key, std::initializer_list il) { + return sadd(key, il.begin(), il.end()); + } + + QueuedRedis& scard(const StringView &key) { + return command(cmd::scard, key); + } + + template + QueuedRedis& sdiff(Input first, Input last) { + return command(cmd::sdiff, first, last); + } + + template + QueuedRedis& sdiff(std::initializer_list il) { + return sdiff(il.begin(), il.end()); + } + + QueuedRedis& sdiffstore(const StringView &destination, const StringView &key) { + return command(cmd::sdiffstore, destination, key); + } + + template + QueuedRedis& sdiffstore(const StringView &destination, + Input first, + Input last) { + return command(cmd::sdiffstore_range, destination, first, last); + } + + template + QueuedRedis& sdiffstore(const StringView &destination, std::initializer_list il) { + return sdiffstore(destination, il.begin(), il.end()); + } + + template + QueuedRedis& sinter(Input first, Input last) { + return command(cmd::sinter, first, last); + } + + template + QueuedRedis& sinter(std::initializer_list il) { + return sinter(il.begin(), il.end()); + } + + QueuedRedis& sinterstore(const StringView &destination, const StringView &key) { + return command(cmd::sinterstore, destination, key); + } + + template + QueuedRedis& sinterstore(const StringView &destination, + Input first, + Input last) { + return command(cmd::sinterstore_range, destination, first, last); + } + + template + QueuedRedis& sinterstore(const StringView &destination, std::initializer_list il) { + return sinterstore(destination, il.begin(), il.end()); + } + + QueuedRedis& sismember(const StringView &key, const StringView &member) { + return command(cmd::sismember, key, member); + } + + QueuedRedis& smembers(const StringView &key) { + return command(cmd::smembers, key); + } + + QueuedRedis& smove(const StringView &source, + const StringView &destination, + const StringView &member) { + return command(cmd::smove, source, destination, member); + } + + QueuedRedis& spop(const StringView &key) { + return command(cmd::spop, key); + } + + QueuedRedis& spop(const StringView &key, long long count) { + return command(cmd::spop_range, key, count); + } + + QueuedRedis& srandmember(const StringView &key) { + return command(cmd::srandmember, key); + } + + QueuedRedis& srandmember(const StringView &key, long long count) { + return command(cmd::srandmember_range, key, count); + } + + QueuedRedis& srem(const StringView &key, const StringView &member) { + return command(cmd::srem, key, member); + } + + template + QueuedRedis& srem(const StringView &key, Input first, Input last) { + return command(cmd::srem_range, key, first, last); + } + + template + QueuedRedis& srem(const StringView &key, std::initializer_list il) { + return srem(key, il.begin(), il.end()); + } + + QueuedRedis& sscan(const StringView &key, + long long cursor, + const StringView &pattern, + long long count) { + return command(cmd::sscan, key, cursor, pattern, count); + } + + QueuedRedis& sscan(const StringView &key, + long long cursor, + const StringView &pattern) { + return sscan(key, cursor, pattern, 10); + } + + QueuedRedis& sscan(const StringView &key, + long long cursor, + long long count) { + return sscan(key, cursor, "*", count); + } + + QueuedRedis& sscan(const StringView &key, + long long cursor) { + return sscan(key, cursor, "*", 10); + } + + template + QueuedRedis& sunion(Input first, Input last) { + return command(cmd::sunion, first, last); + } + + template + QueuedRedis& sunion(std::initializer_list il) { + return sunion(il.begin(), il.end()); + } + + QueuedRedis& sunionstore(const StringView &destination, const StringView &key) { + return command(cmd::sunionstore, destination, key); + } + + template + QueuedRedis& sunionstore(const StringView &destination, Input first, Input last) { + return command(cmd::sunionstore_range, destination, first, last); + } + + template + QueuedRedis& sunionstore(const StringView &destination, std::initializer_list il) { + return sunionstore(destination, il.begin(), il.end()); + } + + // SORTED SET commands. + + QueuedRedis& bzpopmax(const StringView &key, long long timeout) { + return command(cmd::bzpopmax, key, timeout); + } + + QueuedRedis& bzpopmax(const StringView &key, + const std::chrono::seconds &timeout = std::chrono::seconds{0}) { + return bzpopmax(key, timeout.count()); + } + + template + QueuedRedis& bzpopmax(Input first, Input last, long long timeout) { + return command(cmd::bzpopmax_range, first, last, timeout); + } + + template + QueuedRedis& bzpopmax(Input first, + Input last, + const std::chrono::seconds &timeout = std::chrono::seconds{0}) { + return bzpopmax(first, last, timeout.count()); + } + + template + QueuedRedis& bzpopmax(std::initializer_list il, long long timeout) { + return bzpopmax(il.begin(), il.end(), timeout); + } + + template + QueuedRedis& bzpopmax(std::initializer_list il, + const std::chrono::seconds &timeout = std::chrono::seconds{0}) { + return bzpopmax(il.begin(), il.end(), timeout); + } + + QueuedRedis& bzpopmin(const StringView &key, long long timeout) { + return command(cmd::bzpopmin, key, timeout); + } + + QueuedRedis& bzpopmin(const StringView &key, + const std::chrono::seconds &timeout = std::chrono::seconds{0}) { + return bzpopmin(key, timeout.count()); + } + + template + QueuedRedis& bzpopmin(Input first, Input last, long long timeout) { + return command(cmd::bzpopmin_range, first, last, timeout); + } + + template + QueuedRedis& bzpopmin(Input first, + Input last, + const std::chrono::seconds &timeout = std::chrono::seconds{0}) { + return bzpopmin(first, last, timeout.count()); + } + + template + QueuedRedis& bzpopmin(std::initializer_list il, long long timeout) { + return bzpopmin(il.begin(), il.end(), timeout); + } + + template + QueuedRedis& bzpopmin(std::initializer_list il, + const std::chrono::seconds &timeout = std::chrono::seconds{0}) { + return bzpopmin(il.begin(), il.end(), timeout); + } + + // We don't support the INCR option, since you can always use ZINCRBY instead. + QueuedRedis& zadd(const StringView &key, + const StringView &member, + double score, + UpdateType type = UpdateType::ALWAYS, + bool changed = false) { + return command(cmd::zadd, key, member, score, type, changed); + } + + template + QueuedRedis& zadd(const StringView &key, + Input first, + Input last, + UpdateType type = UpdateType::ALWAYS, + bool changed = false) { + return command(cmd::zadd_range, key, first, last, type, changed); + } + + QueuedRedis& zcard(const StringView &key) { + return command(cmd::zcard, key); + } + + template + QueuedRedis& zcount(const StringView &key, const Interval &interval) { + return command(cmd::zcount, key, interval); + } + + QueuedRedis& zincrby(const StringView &key, double increment, const StringView &member) { + return command(cmd::zincrby, key, increment, member); + } + + QueuedRedis& zinterstore(const StringView &destination, + const StringView &key, + double weight) { + return command(cmd::zinterstore, destination, key, weight); + } + + template + QueuedRedis& zinterstore(const StringView &destination, + Input first, + Input last, + Aggregation type = Aggregation::SUM) { + return command(cmd::zinterstore_range, destination, first, last, type); + } + + template + QueuedRedis& zinterstore(const StringView &destination, + std::initializer_list il, + Aggregation type = Aggregation::SUM) { + return zinterstore(destination, il.begin(), il.end(), type); + } + + template + QueuedRedis& zlexcount(const StringView &key, const Interval &interval) { + return command(cmd::zlexcount, key, interval); + } + + QueuedRedis& zpopmax(const StringView &key) { + return command(cmd::zpopmax, key, 1); + } + + QueuedRedis& zpopmax(const StringView &key, long long count) { + return command(cmd::zpopmax, key, count); + } + + QueuedRedis& zpopmin(const StringView &key) { + return command(cmd::zpopmin, key, 1); + } + + QueuedRedis& zpopmin(const StringView &key, long long count) { + return command(cmd::zpopmin, key, count); + } + + // NOTE: *QueuedRedis::zrange*'s parameters are different from *Redis::zrange*. + // *Redis::zrange* is overloaded by the output iterator, however, there's no such + // iterator in *QueuedRedis::zrange*. So we have to use an extra parameter: *with_scores*, + // to decide whether we should send *WITHSCORES* option to Redis. This also applies to + // other commands with the *WITHSCORES* option, e.g. *ZRANGEBYSCORE*, *ZREVRANGE*, + // *ZREVRANGEBYSCORE*. + QueuedRedis& zrange(const StringView &key, + long long start, + long long stop, + bool with_scores = false) { + return command(cmd::zrange, key, start, stop, with_scores); + } + + template + QueuedRedis& zrangebylex(const StringView &key, + const Interval &interval, + const LimitOptions &opts) { + return command(cmd::zrangebylex, key, interval, opts); + } + + template + QueuedRedis& zrangebylex(const StringView &key, const Interval &interval) { + return zrangebylex(key, interval, {}); + } + + // See comments on *ZRANGE*. + template + QueuedRedis& zrangebyscore(const StringView &key, + const Interval &interval, + const LimitOptions &opts, + bool with_scores = false) { + return command(cmd::zrangebyscore, key, interval, opts, with_scores); + } + + // See comments on *ZRANGE*. + template + QueuedRedis& zrangebyscore(const StringView &key, + const Interval &interval, + bool with_scores = false) { + return zrangebyscore(key, interval, {}, with_scores); + } + + QueuedRedis& zrank(const StringView &key, const StringView &member) { + return command(cmd::zrank, key, member); + } + + QueuedRedis& zrem(const StringView &key, const StringView &member) { + return command(cmd::zrem, key, member); + } + + template + QueuedRedis& zrem(const StringView &key, Input first, Input last) { + return command(cmd::zrem_range, key, first, last); + } + + template + QueuedRedis& zrem(const StringView &key, std::initializer_list il) { + return zrem(key, il.begin(), il.end()); + } + + template + QueuedRedis& zremrangebylex(const StringView &key, const Interval &interval) { + return command(cmd::zremrangebylex, key, interval); + } + + QueuedRedis& zremrangebyrank(const StringView &key, long long start, long long stop) { + return command(cmd::zremrangebyrank, key, start, stop); + } + + template + QueuedRedis& zremrangebyscore(const StringView &key, const Interval &interval) { + return command(cmd::zremrangebyscore, key, interval); + } + + // See comments on *ZRANGE*. + QueuedRedis& zrevrange(const StringView &key, + long long start, + long long stop, + bool with_scores = false) { + return command(cmd::zrevrange, key, start, stop, with_scores); + } + + template + QueuedRedis& zrevrangebylex(const StringView &key, + const Interval &interval, + const LimitOptions &opts) { + return command(cmd::zrevrangebylex, key, interval, opts); + } + + template + QueuedRedis& zrevrangebylex(const StringView &key, const Interval &interval) { + return zrevrangebylex(key, interval, {}); + } + + // See comments on *ZRANGE*. + template + QueuedRedis& zrevrangebyscore(const StringView &key, + const Interval &interval, + const LimitOptions &opts, + bool with_scores = false) { + return command(cmd::zrevrangebyscore, key, interval, opts, with_scores); + } + + // See comments on *ZRANGE*. + template + QueuedRedis& zrevrangebyscore(const StringView &key, + const Interval &interval, + bool with_scores = false) { + return zrevrangebyscore(key, interval, {}, with_scores); + } + + QueuedRedis& zrevrank(const StringView &key, const StringView &member) { + return command(cmd::zrevrank, key, member); + } + + QueuedRedis& zscan(const StringView &key, + long long cursor, + const StringView &pattern, + long long count) { + return command(cmd::zscan, key, cursor, pattern, count); + } + + QueuedRedis& zscan(const StringView &key, + long long cursor, + const StringView &pattern) { + return zscan(key, cursor, pattern, 10); + } + + QueuedRedis& zscan(const StringView &key, + long long cursor, + long long count) { + return zscan(key, cursor, "*", count); + } + + QueuedRedis& zscan(const StringView &key, + long long cursor) { + return zscan(key, cursor, "*", 10); + } + + QueuedRedis& zscore(const StringView &key, const StringView &member) { + return command(cmd::zscore, key, member); + } + + QueuedRedis& zunionstore(const StringView &destination, + const StringView &key, + double weight) { + return command(cmd::zunionstore, destination, key, weight); + } + + template + QueuedRedis& zunionstore(const StringView &destination, + Input first, + Input last, + Aggregation type = Aggregation::SUM) { + return command(cmd::zunionstore_range, destination, first, last, type); + } + + template + QueuedRedis& zunionstore(const StringView &destination, + std::initializer_list il, + Aggregation type = Aggregation::SUM) { + return zunionstore(destination, il.begin(), il.end(), type); + } + + // HYPERLOGLOG commands. + + QueuedRedis& pfadd(const StringView &key, const StringView &element) { + return command(cmd::pfadd, key, element); + } + + template + QueuedRedis& pfadd(const StringView &key, Input first, Input last) { + return command(cmd::pfadd_range, key, first, last); + } + + template + QueuedRedis& pfadd(const StringView &key, std::initializer_list il) { + return pfadd(key, il.begin(), il.end()); + } + + QueuedRedis& pfcount(const StringView &key) { + return command(cmd::pfcount, key); + } + + template + QueuedRedis& pfcount(Input first, Input last) { + return command(cmd::pfcount_range, first, last); + } + + template + QueuedRedis& pfcount(std::initializer_list il) { + return pfcount(il.begin(), il.end()); + } + + QueuedRedis& pfmerge(const StringView &destination, const StringView &key) { + return command(cmd::pfmerge, destination, key); + } + + template + QueuedRedis& pfmerge(const StringView &destination, Input first, Input last) { + return command(cmd::pfmerge_range, destination, first, last); + } + + template + QueuedRedis& pfmerge(const StringView &destination, std::initializer_list il) { + return pfmerge(destination, il.begin(), il.end()); + } + + // GEO commands. + + QueuedRedis& geoadd(const StringView &key, + const std::tuple &member) { + return command(cmd::geoadd, key, member); + } + + template + QueuedRedis& geoadd(const StringView &key, + Input first, + Input last) { + return command(cmd::geoadd_range, key, first, last); + } + + template + QueuedRedis& geoadd(const StringView &key, std::initializer_list il) { + return geoadd(key, il.begin(), il.end()); + } + + QueuedRedis& geodist(const StringView &key, + const StringView &member1, + const StringView &member2, + GeoUnit unit = GeoUnit::M) { + return command(cmd::geodist, key, member1, member2, unit); + } + + template + QueuedRedis& geohash(const StringView &key, Input first, Input last) { + return command(cmd::geohash_range, key, first, last); + } + + template + QueuedRedis& geohash(const StringView &key, std::initializer_list il) { + return geohash(key, il.begin(), il.end()); + } + + template + QueuedRedis& geopos(const StringView &key, Input first, Input last) { + return command(cmd::geopos_range, key, first, last); + } + + template + QueuedRedis& geopos(const StringView &key, std::initializer_list il) { + return geopos(key, il.begin(), il.end()); + } + + // TODO: + // 1. since we have different overloads for georadius and georadius-store, + // we might use the GEORADIUS_RO command in the future. + // 2. there're too many parameters for this method, we might refactor it. + QueuedRedis& georadius(const StringView &key, + const std::pair &loc, + double radius, + GeoUnit unit, + const StringView &destination, + bool store_dist, + long long count) { + _georadius_cmd_indexes.push_back(_cmd_num); + + return command(cmd::georadius_store, + key, + loc, + radius, + unit, + destination, + store_dist, + count); + } + + // NOTE: *QueuedRedis::georadius*'s parameters are different from *Redis::georadius*. + // *Redis::georadius* is overloaded by the output iterator, however, there's no such + // iterator in *QueuedRedis::georadius*. So we have to use extra parameters to decide + // whether we should send options to Redis. This also applies to *GEORADIUSBYMEMBER*. + QueuedRedis& georadius(const StringView &key, + const std::pair &loc, + double radius, + GeoUnit unit, + long long count, + bool asc, + bool with_coord, + bool with_dist, + bool with_hash) { + return command(cmd::georadius, + key, + loc, + radius, + unit, + count, + asc, + with_coord, + with_dist, + with_hash); + } + + QueuedRedis& georadiusbymember(const StringView &key, + const StringView &member, + double radius, + GeoUnit unit, + const StringView &destination, + bool store_dist, + long long count) { + _georadius_cmd_indexes.push_back(_cmd_num); + + return command(cmd::georadiusbymember, + key, + member, + radius, + unit, + destination, + store_dist, + count); + } + + // See the comments on *GEORADIUS*. + QueuedRedis& georadiusbymember(const StringView &key, + const StringView &member, + double radius, + GeoUnit unit, + long long count, + bool asc, + bool with_coord, + bool with_dist, + bool with_hash) { + return command(cmd::georadiusbymember, + key, + member, + radius, + unit, + count, + asc, + with_coord, + with_dist, + with_hash); + } + + // SCRIPTING commands. + + QueuedRedis& eval(const StringView &script, + std::initializer_list keys, + std::initializer_list args) { + return command(cmd::eval, script, keys, args); + } + + QueuedRedis& evalsha(const StringView &script, + std::initializer_list keys, + std::initializer_list args) { + return command(cmd::evalsha, script, keys, args); + } + + template + QueuedRedis& script_exists(Input first, Input last) { + return command(cmd::script_exists_range, first, last); + } + + template + QueuedRedis& script_exists(std::initializer_list il) { + return script_exists(il.begin(), il.end()); + } + + QueuedRedis& script_flush() { + return command(cmd::script_flush); + } + + QueuedRedis& script_kill() { + return command(cmd::script_kill); + } + + QueuedRedis& script_load(const StringView &script) { + return command(cmd::script_load, script); + } + + // PUBSUB commands. + + QueuedRedis& publish(const StringView &channel, const StringView &message) { + return command(cmd::publish, channel, message); + } + + // Stream commands. + + QueuedRedis& xack(const StringView &key, const StringView &group, const StringView &id) { + return command(cmd::xack, key, group, id); + } + + template + QueuedRedis& xack(const StringView &key, const StringView &group, Input first, Input last) { + return command(cmd::xack_range, key, group, first, last); + } + + template + QueuedRedis& xack(const StringView &key, const StringView &group, std::initializer_list il) { + return xack(key, group, il.begin(), il.end()); + } + + template + QueuedRedis& xadd(const StringView &key, const StringView &id, Input first, Input last) { + return command(cmd::xadd_range, key, id, first, last); + } + + template + QueuedRedis& xadd(const StringView &key, const StringView &id, std::initializer_list il) { + return xadd(key, id, il.begin(), il.end()); + } + + template + QueuedRedis& xadd(const StringView &key, + const StringView &id, + Input first, + Input last, + long long count, + bool approx = true) { + return command(cmd::xadd_maxlen_range, key, id, first, last, count, approx); + } + + template + QueuedRedis& xadd(const StringView &key, + const StringView &id, + std::initializer_list il, + long long count, + bool approx = true) { + return xadd(key, id, il.begin(), il.end(), count, approx); + } + + QueuedRedis& xclaim(const StringView &key, + const StringView &group, + const StringView &consumer, + const std::chrono::milliseconds &min_idle_time, + const StringView &id) { + return command(cmd::xclaim, key, group, consumer, min_idle_time.count(), id); + } + + template + QueuedRedis& xclaim(const StringView &key, + const StringView &group, + const StringView &consumer, + const std::chrono::milliseconds &min_idle_time, + Input first, + Input last) { + return command(cmd::xclaim_range, + key, + group, + consumer, + min_idle_time.count(), + first, + last); + } + + template + QueuedRedis& xclaim(const StringView &key, + const StringView &group, + const StringView &consumer, + const std::chrono::milliseconds &min_idle_time, + std::initializer_list il) { + return xclaim(key, group, consumer, min_idle_time, il.begin(), il.end()); + } + + QueuedRedis& xdel(const StringView &key, const StringView &id) { + return command(cmd::xdel, key, id); + } + + template + QueuedRedis& xdel(const StringView &key, Input first, Input last) { + return command(cmd::xdel_range, key, first, last); + } + + template + QueuedRedis& xdel(const StringView &key, std::initializer_list il) { + return xdel(key, il.begin(), il.end()); + } + + QueuedRedis& xgroup_create(const StringView &key, + const StringView &group, + const StringView &id, + bool mkstream = false) { + return command(cmd::xgroup_create, key, group, id, mkstream); + } + + QueuedRedis& xgroup_setid(const StringView &key, + const StringView &group, + const StringView &id) { + return command(cmd::xgroup_setid, key, group, id); + } + + QueuedRedis& xgroup_destroy(const StringView &key, const StringView &group) { + return command(cmd::xgroup_destroy, key, group); + } + + QueuedRedis& xgroup_delconsumer(const StringView &key, + const StringView &group, + const StringView &consumer) { + return command(cmd::xgroup_delconsumer, key, group, consumer); + } + + QueuedRedis& xlen(const StringView &key) { + return command(cmd::xlen, key); + } + + QueuedRedis& xpending(const StringView &key, const StringView &group) { + return command(cmd::xpending, key, group); + } + + QueuedRedis& xpending(const StringView &key, + const StringView &group, + const StringView &start, + const StringView &end, + long long count) { + return command(cmd::xpending_detail, key, group, start, end, count); + } + + QueuedRedis& xpending(const StringView &key, + const StringView &group, + const StringView &start, + const StringView &end, + long long count, + const StringView &consumer) { + return command(cmd::xpending_per_consumer, key, group, start, end, count, consumer); + } + + QueuedRedis& xrange(const StringView &key, + const StringView &start, + const StringView &end) { + return command(cmd::xrange, key, start, end); + } + + QueuedRedis& xrange(const StringView &key, + const StringView &start, + const StringView &end, + long long count) { + return command(cmd::xrange, key, start, end, count); + } + + QueuedRedis& xread(const StringView &key, const StringView &id, long long count) { + return command(cmd::xread, key, id, count); + } + + QueuedRedis& xread(const StringView &key, const StringView &id) { + return xread(key, id, 0); + } + + template + auto xread(Input first, Input last, long long count) + -> typename std::enable_if::value, + QueuedRedis&>::type { + return command(cmd::xread_range, first, last, count); + } + + template + auto xread(Input first, Input last) + -> typename std::enable_if::value, + QueuedRedis&>::type { + return xread(first, last, 0); + } + + QueuedRedis& xread(const StringView &key, + const StringView &id, + const std::chrono::milliseconds &timeout, + long long count) { + return command(cmd::xread_block, key, id, timeout.count(), count); + } + + QueuedRedis& xread(const StringView &key, + const StringView &id, + const std::chrono::milliseconds &timeout) { + return xread(key, id, timeout, 0); + } + + template + auto xread(Input first, + Input last, + const std::chrono::milliseconds &timeout, + long long count) + -> typename std::enable_if::value, + QueuedRedis&>::type { + return command(cmd::xread_block_range, first, last, timeout.count(), count); + } + + template + auto xread(Input first, + Input last, + const std::chrono::milliseconds &timeout) + -> typename std::enable_if::value, + QueuedRedis&>::type { + return xread(first, last, timeout, 0); + } + + QueuedRedis& xreadgroup(const StringView &group, + const StringView &consumer, + const StringView &key, + const StringView &id, + long long count, + bool noack) { + return command(cmd::xreadgroup, group, consumer, key, id, count, noack); + } + + QueuedRedis& xreadgroup(const StringView &group, + const StringView &consumer, + const StringView &key, + const StringView &id, + long long count) { + return xreadgroup(group, consumer, key, id, count, false); + } + + template + auto xreadgroup(const StringView &group, + const StringView &consumer, + Input first, + Input last, + long long count, + bool noack) + -> typename std::enable_if::value, + QueuedRedis&>::type { + return command(cmd::xreadgroup_range, group, consumer, first, last, count, noack); + } + + template + auto xreadgroup(const StringView &group, + const StringView &consumer, + Input first, + Input last, + long long count) + -> typename std::enable_if::value, + QueuedRedis&>::type { + return xreadgroup(group, consumer, first ,last, count, false); + } + + template + auto xreadgroup(const StringView &group, + const StringView &consumer, + Input first, + Input last) + -> typename std::enable_if::value, + QueuedRedis&>::type { + return xreadgroup(group, consumer, first ,last, 0, false); + } + + QueuedRedis& xreadgroup(const StringView &group, + const StringView &consumer, + const StringView &key, + const StringView &id, + const std::chrono::milliseconds &timeout, + long long count, + bool noack) { + return command(cmd::xreadgroup_block, + group, + consumer, + key, + id, + timeout.count(), + count, + noack); + } + + QueuedRedis& xreadgroup(const StringView &group, + const StringView &consumer, + const StringView &key, + const StringView &id, + const std::chrono::milliseconds &timeout, + long long count) { + return xreadgroup(group, consumer, key, id, timeout, count, false); + } + + QueuedRedis& xreadgroup(const StringView &group, + const StringView &consumer, + const StringView &key, + const StringView &id, + const std::chrono::milliseconds &timeout) { + return xreadgroup(group, consumer, key, id, timeout, 0, false); + } + + template + auto xreadgroup(const StringView &group, + const StringView &consumer, + Input first, + Input last, + const std::chrono::milliseconds &timeout, + long long count, + bool noack) + -> typename std::enable_if::value, + QueuedRedis&>::type { + return command(cmd::xreadgroup_block_range, + group, + consumer, + first, + last, + timeout.count(), + count, + noack); + } + + template + auto xreadgroup(const StringView &group, + const StringView &consumer, + Input first, + Input last, + const std::chrono::milliseconds &timeout, + long long count) + -> typename std::enable_if::value, + QueuedRedis&>::type { + return xreadgroup(group, consumer, first, last, timeout, count, false); + } + + template + auto xreadgroup(const StringView &group, + const StringView &consumer, + Input first, + Input last, + const std::chrono::milliseconds &timeout) + -> typename std::enable_if::value, + QueuedRedis&>::type { + return xreadgroup(group, consumer, first, last, timeout, 0, false); + } + + QueuedRedis& xrevrange(const StringView &key, + const StringView &end, + const StringView &start) { + return command(cmd::xrevrange, key, end, start); + } + + QueuedRedis& xrevrange(const StringView &key, + const StringView &end, + const StringView &start, + long long count) { + return command(cmd::xrevrange, key, end, start, count); + } + + QueuedRedis& xtrim(const StringView &key, long long count, bool approx = true) { + return command(cmd::xtrim, key, count, approx); + } + +private: + friend class Redis; + + friend class RedisCluster; + + template + QueuedRedis(const ConnectionSPtr &connection, Args &&...args); + + void _sanity_check() const; + + void _reset(); + + void _invalidate(); + + void _rewrite_replies(std::vector &replies) const; + + template + void _rewrite_replies(const std::vector &indexes, + Func rewriter, + std::vector &replies) const; + + ConnectionSPtr _connection; + + Impl _impl; + + std::size_t _cmd_num = 0; + + std::vector _set_cmd_indexes; + + std::vector _georadius_cmd_indexes; + + bool _valid = true; +}; + +class QueuedReplies { +public: + std::size_t size() const; + + redisReply& get(std::size_t idx); + + template + Result get(std::size_t idx); + + template + void get(std::size_t idx, Output output); + +private: + template + friend class QueuedRedis; + + explicit QueuedReplies(std::vector replies) : _replies(std::move(replies)) {} + + void _index_check(std::size_t idx) const; + + std::vector _replies; +}; + +} + +} + +#include "queued_redis.hpp" + +#endif // end SEWENEW_REDISPLUSPLUS_QUEUED_REDIS_H diff --git a/controller/thirdparty/redis-plus-plus-1.1.1/src/sw/redis++/queued_redis.hpp b/controller/thirdparty/redis-plus-plus-1.1.1/src/sw/redis++/queued_redis.hpp new file mode 100644 index 000000000..409f48aca --- /dev/null +++ b/controller/thirdparty/redis-plus-plus-1.1.1/src/sw/redis++/queued_redis.hpp @@ -0,0 +1,208 @@ +/************************************************************************** + Copyright (c) 2017 sewenew + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. + *************************************************************************/ + +#ifndef SEWENEW_REDISPLUSPLUS_QUEUED_REDIS_HPP +#define SEWENEW_REDISPLUSPLUS_QUEUED_REDIS_HPP + +namespace sw { + +namespace redis { + +template +template +QueuedRedis::QueuedRedis(const ConnectionSPtr &connection, Args &&...args) : + _connection(connection), + _impl(std::forward(args)...) { + assert(_connection); +} + +template +Redis QueuedRedis::redis() { + return Redis(_connection); +} + +template +template +auto QueuedRedis::command(Cmd cmd, Args &&...args) + -> typename std::enable_if::value, + QueuedRedis&>::type { + try { + _sanity_check(); + + _impl.command(*_connection, cmd, std::forward(args)...); + + ++_cmd_num; + } catch (const Error &e) { + _invalidate(); + throw; + } + + return *this; +} + +template +template +QueuedRedis& QueuedRedis::command(const StringView &cmd_name, Args &&...args) { + auto cmd = [](Connection &connection, const StringView &cmd_name, Args &&...args) { + CmdArgs cmd_args; + cmd_args.append(cmd_name, std::forward(args)...); + connection.send(cmd_args); + }; + + return command(cmd, cmd_name, std::forward(args)...); +} + +template +template +auto QueuedRedis::command(Input first, Input last) + -> typename std::enable_if::value, QueuedRedis&>::type { + if (first == last) { + throw Error("command: empty range"); + } + + auto cmd = [](Connection &connection, Input first, Input last) { + CmdArgs cmd_args; + while (first != last) { + cmd_args.append(*first); + ++first; + } + connection.send(cmd_args); + }; + + return command(cmd, first, last); +} + +template +QueuedReplies QueuedRedis::exec() { + try { + _sanity_check(); + + auto replies = _impl.exec(*_connection, _cmd_num); + + _rewrite_replies(replies); + + _reset(); + + return QueuedReplies(std::move(replies)); + } catch (const Error &e) { + _invalidate(); + throw; + } +} + +template +void QueuedRedis::discard() { + try { + _sanity_check(); + + _impl.discard(*_connection, _cmd_num); + + _reset(); + } catch (const Error &e) { + _invalidate(); + throw; + } +} + +template +void QueuedRedis::_sanity_check() const { + if (!_valid) { + throw Error("Not in valid state"); + } + + if (_connection->broken()) { + throw Error("Connection is broken"); + } +} + +template +inline void QueuedRedis::_reset() { + _cmd_num = 0; + + _set_cmd_indexes.clear(); + + _georadius_cmd_indexes.clear(); +} + +template +void QueuedRedis::_invalidate() { + _valid = false; + + _reset(); +} + +template +void QueuedRedis::_rewrite_replies(std::vector &replies) const { + _rewrite_replies(_set_cmd_indexes, reply::rewrite_set_reply, replies); + + _rewrite_replies(_georadius_cmd_indexes, reply::rewrite_georadius_reply, replies); +} + +template +template +void QueuedRedis::_rewrite_replies(const std::vector &indexes, + Func rewriter, + std::vector &replies) const { + for (auto idx : indexes) { + assert(idx < replies.size()); + + auto &reply = replies[idx]; + + assert(reply); + + rewriter(*reply); + } +} + +inline std::size_t QueuedReplies::size() const { + return _replies.size(); +} + +inline redisReply& QueuedReplies::get(std::size_t idx) { + _index_check(idx); + + auto &reply = _replies[idx]; + + assert(reply); + + return *reply; +} + +template +inline Result QueuedReplies::get(std::size_t idx) { + auto &reply = get(idx); + + return reply::parse(reply); +} + +template +inline void QueuedReplies::get(std::size_t idx, Output output) { + auto &reply = get(idx); + + reply::to_array(reply, output); +} + +inline void QueuedReplies::_index_check(std::size_t idx) const { + if (idx >= size()) { + throw Error("Out of range"); + } +} + +} + +} + +#endif // end SEWENEW_REDISPLUSPLUS_QUEUED_REDIS_HPP diff --git a/controller/thirdparty/redis-plus-plus-1.1.1/src/sw/redis++/redis++.h b/controller/thirdparty/redis-plus-plus-1.1.1/src/sw/redis++/redis++.h new file mode 100644 index 000000000..0da0ebb16 --- /dev/null +++ b/controller/thirdparty/redis-plus-plus-1.1.1/src/sw/redis++/redis++.h @@ -0,0 +1,25 @@ +/************************************************************************** + Copyright (c) 2017 sewenew + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. + *************************************************************************/ + +#ifndef SEWENEW_REDISPLUSPLUS_REDISPLUSPLUS_H +#define SEWENEW_REDISPLUSPLUS_REDISPLUSPLUS_H + +#include "redis.h" +#include "redis_cluster.h" +#include "queued_redis.h" +#include "sentinel.h" + +#endif // end SEWENEW_REDISPLUSPLUS_REDISPLUSPLUS_H diff --git a/controller/thirdparty/redis-plus-plus-1.1.1/src/sw/redis++/redis.cpp b/controller/thirdparty/redis-plus-plus-1.1.1/src/sw/redis++/redis.cpp new file mode 100644 index 000000000..be96967fe --- /dev/null +++ b/controller/thirdparty/redis-plus-plus-1.1.1/src/sw/redis++/redis.cpp @@ -0,0 +1,882 @@ +/************************************************************************** + Copyright (c) 2017 sewenew + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. + *************************************************************************/ + +#include "redis.h" +#include +#include "command.h" +#include "errors.h" +#include "queued_redis.h" + +namespace sw { + +namespace redis { + +Redis::Redis(const std::string &uri) : Redis(ConnectionOptions(uri)) {} + +Redis::Redis(const ConnectionSPtr &connection) : _connection(connection) { + assert(_connection); +} + +Pipeline Redis::pipeline() { + return Pipeline(std::make_shared(_pool.create())); +} + +Transaction Redis::transaction(bool piped) { + return Transaction(std::make_shared(_pool.create()), piped); +} + +Subscriber Redis::subscriber() { + return Subscriber(_pool.create()); +} + +// CONNECTION commands. + +void Redis::auth(const StringView &password) { + auto reply = command(cmd::auth, password); + + reply::parse(*reply); +} + +std::string Redis::echo(const StringView &msg) { + auto reply = command(cmd::echo, msg); + + return reply::parse(*reply); +} + +std::string Redis::ping() { + auto reply = command(cmd::ping); + + return reply::to_status(*reply); +} + +std::string Redis::ping(const StringView &msg) { + auto reply = command(cmd::ping, msg); + + return reply::parse(*reply); +} + +void Redis::swapdb(long long idx1, long long idx2) { + auto reply = command(cmd::swapdb, idx1, idx2); + + reply::parse(*reply); +} + +// SERVER commands. + +void Redis::bgrewriteaof() { + auto reply = command(cmd::bgrewriteaof); + + reply::parse(*reply); +} + +void Redis::bgsave() { + auto reply = command(cmd::bgsave); + + reply::parse(*reply); +} + +long long Redis::dbsize() { + auto reply = command(cmd::dbsize); + + return reply::parse(*reply); +} + +void Redis::flushall(bool async) { + auto reply = command(cmd::flushall, async); + + reply::parse(*reply); +} + +void Redis::flushdb(bool async) { + auto reply = command(cmd::flushdb, async); + + reply::parse(*reply); +} + +std::string Redis::info() { + auto reply = command(cmd::info); + + return reply::parse(*reply); +} + +std::string Redis::info(const StringView §ion) { + auto reply = command(cmd::info, section); + + return reply::parse(*reply); +} + +long long Redis::lastsave() { + auto reply = command(cmd::lastsave); + + return reply::parse(*reply); +} + +void Redis::save() { + auto reply = command(cmd::save); + + reply::parse(*reply); +} + +// KEY commands. + +long long Redis::del(const StringView &key) { + auto reply = command(cmd::del, key); + + return reply::parse(*reply); +} + +OptionalString Redis::dump(const StringView &key) { + auto reply = command(cmd::dump, key); + + return reply::parse(*reply); +} + +long long Redis::exists(const StringView &key) { + auto reply = command(cmd::exists, key); + + return reply::parse(*reply); +} + +bool Redis::expire(const StringView &key, long long timeout) { + auto reply = command(cmd::expire, key, timeout); + + return reply::parse(*reply); +} + +bool Redis::expireat(const StringView &key, long long timestamp) { + auto reply = command(cmd::expireat, key, timestamp); + + return reply::parse(*reply); +} + +bool Redis::move(const StringView &key, long long db) { + auto reply = command(cmd::move, key, db); + + return reply::parse(*reply); +} + +bool Redis::persist(const StringView &key) { + auto reply = command(cmd::persist, key); + + return reply::parse(*reply); +} + +bool Redis::pexpire(const StringView &key, long long timeout) { + auto reply = command(cmd::pexpire, key, timeout); + + return reply::parse(*reply); +} + +bool Redis::pexpireat(const StringView &key, long long timestamp) { + auto reply = command(cmd::pexpireat, key, timestamp); + + return reply::parse(*reply); +} + +long long Redis::pttl(const StringView &key) { + auto reply = command(cmd::pttl, key); + + return reply::parse(*reply); +} + +OptionalString Redis::randomkey() { + auto reply = command(cmd::randomkey); + + return reply::parse(*reply); +} + +void Redis::rename(const StringView &key, const StringView &newkey) { + auto reply = command(cmd::rename, key, newkey); + + reply::parse(*reply); +} + +bool Redis::renamenx(const StringView &key, const StringView &newkey) { + auto reply = command(cmd::renamenx, key, newkey); + + return reply::parse(*reply); +} + +void Redis::restore(const StringView &key, + const StringView &val, + long long ttl, + bool replace) { + auto reply = command(cmd::restore, key, val, ttl, replace); + + reply::parse(*reply); +} + +long long Redis::touch(const StringView &key) { + auto reply = command(cmd::touch, key); + + return reply::parse(*reply); +} + +long long Redis::ttl(const StringView &key) { + auto reply = command(cmd::ttl, key); + + return reply::parse(*reply); +} + +std::string Redis::type(const StringView &key) { + auto reply = command(cmd::type, key); + + return reply::parse(*reply); +} + +long long Redis::unlink(const StringView &key) { + auto reply = command(cmd::unlink, key); + + return reply::parse(*reply); +} + +long long Redis::wait(long long numslaves, long long timeout) { + auto reply = command(cmd::wait, numslaves, timeout); + + return reply::parse(*reply); +} + +// STRING commands. + +long long Redis::append(const StringView &key, const StringView &val) { + auto reply = command(cmd::append, key, val); + + return reply::parse(*reply); +} + +long long Redis::bitcount(const StringView &key, long long start, long long end) { + auto reply = command(cmd::bitcount, key, start, end); + + return reply::parse(*reply); +} + +long long Redis::bitop(BitOp op, const StringView &destination, const StringView &key) { + auto reply = command(cmd::bitop, op, destination, key); + + return reply::parse(*reply); +} + +long long Redis::bitpos(const StringView &key, + long long bit, + long long start, + long long end) { + auto reply = command(cmd::bitpos, key, bit, start, end); + + return reply::parse(*reply); +} + +long long Redis::decr(const StringView &key) { + auto reply = command(cmd::decr, key); + + return reply::parse(*reply); +} + +long long Redis::decrby(const StringView &key, long long decrement) { + auto reply = command(cmd::decrby, key, decrement); + + return reply::parse(*reply); +} + +OptionalString Redis::get(const StringView &key) { + auto reply = command(cmd::get, key); + + return reply::parse(*reply); +} + +long long Redis::getbit(const StringView &key, long long offset) { + auto reply = command(cmd::getbit, key, offset); + + return reply::parse(*reply); +} + +std::string Redis::getrange(const StringView &key, long long start, long long end) { + auto reply = command(cmd::getrange, key, start, end); + + return reply::parse(*reply); +} + +OptionalString Redis::getset(const StringView &key, const StringView &val) { + auto reply = command(cmd::getset, key, val); + + return reply::parse(*reply); +} + +long long Redis::incr(const StringView &key) { + auto reply = command(cmd::incr, key); + + return reply::parse(*reply); +} + +long long Redis::incrby(const StringView &key, long long increment) { + auto reply = command(cmd::incrby, key, increment); + + return reply::parse(*reply); +} + +double Redis::incrbyfloat(const StringView &key, double increment) { + auto reply = command(cmd::incrbyfloat, key, increment); + + return reply::parse(*reply); +} + +void Redis::psetex(const StringView &key, + long long ttl, + const StringView &val) { + auto reply = command(cmd::psetex, key, ttl, val); + + reply::parse(*reply); +} + +bool Redis::set(const StringView &key, + const StringView &val, + const std::chrono::milliseconds &ttl, + UpdateType type) { + auto reply = command(cmd::set, key, val, ttl.count(), type); + + reply::rewrite_set_reply(*reply); + + return reply::parse(*reply); +} + +void Redis::setex(const StringView &key, + long long ttl, + const StringView &val) { + auto reply = command(cmd::setex, key, ttl, val); + + reply::parse(*reply); +} + +bool Redis::setnx(const StringView &key, const StringView &val) { + auto reply = command(cmd::setnx, key, val); + + return reply::parse(*reply); +} + +long long Redis::setrange(const StringView &key, long long offset, const StringView &val) { + auto reply = command(cmd::setrange, key, offset, val); + + return reply::parse(*reply); +} + +long long Redis::strlen(const StringView &key) { + auto reply = command(cmd::strlen, key); + + return reply::parse(*reply); +} + +// LIST commands. + +OptionalStringPair Redis::blpop(const StringView &key, long long timeout) { + auto reply = command(cmd::blpop, key, timeout); + + return reply::parse(*reply); +} + +OptionalStringPair Redis::blpop(const StringView &key, const std::chrono::seconds &timeout) { + return blpop(key, timeout.count()); +} + +OptionalStringPair Redis::brpop(const StringView &key, long long timeout) { + auto reply = command(cmd::brpop, key, timeout); + + return reply::parse(*reply); +} + +OptionalStringPair Redis::brpop(const StringView &key, const std::chrono::seconds &timeout) { + return brpop(key, timeout.count()); +} + +OptionalString Redis::brpoplpush(const StringView &source, + const StringView &destination, + long long timeout) { + auto reply = command(cmd::brpoplpush, source, destination, timeout); + + return reply::parse(*reply); +} + +OptionalString Redis::lindex(const StringView &key, long long index) { + auto reply = command(cmd::lindex, key, index); + + return reply::parse(*reply); +} + +long long Redis::linsert(const StringView &key, + InsertPosition position, + const StringView &pivot, + const StringView &val) { + auto reply = command(cmd::linsert, key, position, pivot, val); + + return reply::parse(*reply); +} + +long long Redis::llen(const StringView &key) { + auto reply = command(cmd::llen, key); + + return reply::parse(*reply); +} + +OptionalString Redis::lpop(const StringView &key) { + auto reply = command(cmd::lpop, key); + + return reply::parse(*reply); +} + +long long Redis::lpush(const StringView &key, const StringView &val) { + auto reply = command(cmd::lpush, key, val); + + return reply::parse(*reply); +} + +long long Redis::lpushx(const StringView &key, const StringView &val) { + auto reply = command(cmd::lpushx, key, val); + + return reply::parse(*reply); +} + +long long Redis::lrem(const StringView &key, long long count, const StringView &val) { + auto reply = command(cmd::lrem, key, count, val); + + return reply::parse(*reply); +} + +void Redis::lset(const StringView &key, long long index, const StringView &val) { + auto reply = command(cmd::lset, key, index, val); + + reply::parse(*reply); +} + +void Redis::ltrim(const StringView &key, long long start, long long stop) { + auto reply = command(cmd::ltrim, key, start, stop); + + reply::parse(*reply); +} + +OptionalString Redis::rpop(const StringView &key) { + auto reply = command(cmd::rpop, key); + + return reply::parse(*reply); +} + +OptionalString Redis::rpoplpush(const StringView &source, const StringView &destination) { + auto reply = command(cmd::rpoplpush, source, destination); + + return reply::parse(*reply); +} + +long long Redis::rpush(const StringView &key, const StringView &val) { + auto reply = command(cmd::rpush, key, val); + + return reply::parse(*reply); +} + +long long Redis::rpushx(const StringView &key, const StringView &val) { + auto reply = command(cmd::rpushx, key, val); + + return reply::parse(*reply); +} + +long long Redis::hdel(const StringView &key, const StringView &field) { + auto reply = command(cmd::hdel, key, field); + + return reply::parse(*reply); +} + +bool Redis::hexists(const StringView &key, const StringView &field) { + auto reply = command(cmd::hexists, key, field); + + return reply::parse(*reply); +} + +OptionalString Redis::hget(const StringView &key, const StringView &field) { + auto reply = command(cmd::hget, key, field); + + return reply::parse(*reply); +} + +long long Redis::hincrby(const StringView &key, const StringView &field, long long increment) { + auto reply = command(cmd::hincrby, key, field, increment); + + return reply::parse(*reply); +} + +double Redis::hincrbyfloat(const StringView &key, const StringView &field, double increment) { + auto reply = command(cmd::hincrbyfloat, key, field, increment); + + return reply::parse(*reply); +} + +long long Redis::hlen(const StringView &key) { + auto reply = command(cmd::hlen, key); + + return reply::parse(*reply); +} + +bool Redis::hset(const StringView &key, const StringView &field, const StringView &val) { + auto reply = command(cmd::hset, key, field, val); + + return reply::parse(*reply); +} + +bool Redis::hset(const StringView &key, const std::pair &item) { + return hset(key, item.first, item.second); +} + +bool Redis::hsetnx(const StringView &key, const StringView &field, const StringView &val) { + auto reply = command(cmd::hsetnx, key, field, val); + + return reply::parse(*reply); +} + +bool Redis::hsetnx(const StringView &key, const std::pair &item) { + return hsetnx(key, item.first, item.second); +} + +long long Redis::hstrlen(const StringView &key, const StringView &field) { + auto reply = command(cmd::hstrlen, key, field); + + return reply::parse(*reply); +} + +// SET commands. + +long long Redis::sadd(const StringView &key, const StringView &member) { + auto reply = command(cmd::sadd, key, member); + + return reply::parse(*reply); +} + +long long Redis::scard(const StringView &key) { + auto reply = command(cmd::scard, key); + + return reply::parse(*reply); +} + +long long Redis::sdiffstore(const StringView &destination, const StringView &key) { + auto reply = command(cmd::sdiffstore, destination, key); + + return reply::parse(*reply); +} + +long long Redis::sinterstore(const StringView &destination, const StringView &key) { + auto reply = command(cmd::sinterstore, destination, key); + + return reply::parse(*reply); +} + +bool Redis::sismember(const StringView &key, const StringView &member) { + auto reply = command(cmd::sismember, key, member); + + return reply::parse(*reply); +} + +bool Redis::smove(const StringView &source, + const StringView &destination, + const StringView &member) { + auto reply = command(cmd::smove, source, destination, member); + + return reply::parse(*reply); +} + +OptionalString Redis::spop(const StringView &key) { + auto reply = command(cmd::spop, key); + + return reply::parse(*reply); +} + +OptionalString Redis::srandmember(const StringView &key) { + auto reply = command(cmd::srandmember, key); + + return reply::parse(*reply); +} + +long long Redis::srem(const StringView &key, const StringView &member) { + auto reply = command(cmd::srem, key, member); + + return reply::parse(*reply); +} + +long long Redis::sunionstore(const StringView &destination, const StringView &key) { + auto reply = command(cmd::sunionstore, destination, key); + + return reply::parse(*reply); +} + +// SORTED SET commands. + +auto Redis::bzpopmax(const StringView &key, long long timeout) + -> Optional> { + auto reply = command(cmd::bzpopmax, key, timeout); + + return reply::parse>>(*reply); +} + +auto Redis::bzpopmin(const StringView &key, long long timeout) + -> Optional> { + auto reply = command(cmd::bzpopmin, key, timeout); + + return reply::parse>>(*reply); +} + +long long Redis::zadd(const StringView &key, + const StringView &member, + double score, + UpdateType type, + bool changed) { + auto reply = command(cmd::zadd, key, member, score, type, changed); + + return reply::parse(*reply); +} + +long long Redis::zcard(const StringView &key) { + auto reply = command(cmd::zcard, key); + + return reply::parse(*reply); +} + +double Redis::zincrby(const StringView &key, double increment, const StringView &member) { + auto reply = command(cmd::zincrby, key, increment, member); + + return reply::parse(*reply); +} + +long long Redis::zinterstore(const StringView &destination, const StringView &key, double weight) { + auto reply = command(cmd::zinterstore, destination, key, weight); + + return reply::parse(*reply); +} + +Optional> Redis::zpopmax(const StringView &key) { + auto reply = command(cmd::zpopmax, key, 1); + + return reply::parse>>(*reply); +} + +Optional> Redis::zpopmin(const StringView &key) { + auto reply = command(cmd::zpopmin, key, 1); + + return reply::parse>>(*reply); +} + +OptionalLongLong Redis::zrank(const StringView &key, const StringView &member) { + auto reply = command(cmd::zrank, key, member); + + return reply::parse(*reply); +} + +long long Redis::zrem(const StringView &key, const StringView &member) { + auto reply = command(cmd::zrem, key, member); + + return reply::parse(*reply); +} + +long long Redis::zremrangebyrank(const StringView &key, long long start, long long stop) { + auto reply = command(cmd::zremrangebyrank, key, start, stop); + + return reply::parse(*reply); +} + +OptionalLongLong Redis::zrevrank(const StringView &key, const StringView &member) { + auto reply = command(cmd::zrevrank, key, member); + + return reply::parse(*reply); +} + +OptionalDouble Redis::zscore(const StringView &key, const StringView &member) { + auto reply = command(cmd::zscore, key, member); + + return reply::parse(*reply); +} + +long long Redis::zunionstore(const StringView &destination, const StringView &key, double weight) { + auto reply = command(cmd::zunionstore, destination, key, weight); + + return reply::parse(*reply); +} + +// HYPERLOGLOG commands. + +bool Redis::pfadd(const StringView &key, const StringView &element) { + auto reply = command(cmd::pfadd, key, element); + + return reply::parse(*reply); +} + +long long Redis::pfcount(const StringView &key) { + auto reply = command(cmd::pfcount, key); + + return reply::parse(*reply); +} + +void Redis::pfmerge(const StringView &destination, const StringView &key) { + auto reply = command(cmd::pfmerge, destination, key); + + reply::parse(*reply); +} + +// GEO commands. + +long long Redis::geoadd(const StringView &key, + const std::tuple &member) { + auto reply = command(cmd::geoadd, key, member); + + return reply::parse(*reply); +} + +OptionalDouble Redis::geodist(const StringView &key, + const StringView &member1, + const StringView &member2, + GeoUnit unit) { + auto reply = command(cmd::geodist, key, member1, member2, unit); + + return reply::parse(*reply); +} + +OptionalLongLong Redis::georadius(const StringView &key, + const std::pair &loc, + double radius, + GeoUnit unit, + const StringView &destination, + bool store_dist, + long long count) { + auto reply = command(cmd::georadius_store, + key, + loc, + radius, + unit, + destination, + store_dist, + count); + + reply::rewrite_georadius_reply(*reply); + + return reply::parse(*reply); +} + +OptionalLongLong Redis::georadiusbymember(const StringView &key, + const StringView &member, + double radius, + GeoUnit unit, + const StringView &destination, + bool store_dist, + long long count) { + auto reply = command(cmd::georadiusbymember_store, + key, + member, + radius, + unit, + destination, + store_dist, + count); + + reply::rewrite_georadius_reply(*reply); + + return reply::parse(*reply); +} + +// SCRIPTING commands. + +void Redis::script_flush() { + auto reply = command(cmd::script_flush); + + reply::parse(*reply); +} + +void Redis::script_kill() { + auto reply = command(cmd::script_kill); + + reply::parse(*reply); +} + +std::string Redis::script_load(const StringView &script) { + auto reply = command(cmd::script_load, script); + + return reply::parse(*reply); +} + +// PUBSUB commands. + +long long Redis::publish(const StringView &channel, const StringView &message) { + auto reply = command(cmd::publish, channel, message); + + return reply::parse(*reply); +} + +// Transaction commands. + +void Redis::watch(const StringView &key) { + auto reply = command(cmd::watch, key); + + reply::parse(*reply); +} + +// Stream commands. + +long long Redis::xack(const StringView &key, const StringView &group, const StringView &id) { + auto reply = command(cmd::xack, key, group, id); + + return reply::parse(*reply); +} + +long long Redis::xdel(const StringView &key, const StringView &id) { + auto reply = command(cmd::xdel, key, id); + + return reply::parse(*reply); +} + +void Redis::xgroup_create(const StringView &key, + const StringView &group, + const StringView &id, + bool mkstream) { + auto reply = command(cmd::xgroup_create, key, group, id, mkstream); + + reply::parse(*reply); +} + +void Redis::xgroup_setid(const StringView &key, const StringView &group, const StringView &id) { + auto reply = command(cmd::xgroup_setid, key, group, id); + + reply::parse(*reply); +} + +long long Redis::xgroup_destroy(const StringView &key, const StringView &group) { + auto reply = command(cmd::xgroup_destroy, key, group); + + return reply::parse(*reply); +} + +long long Redis::xgroup_delconsumer(const StringView &key, + const StringView &group, + const StringView &consumer) { + auto reply = command(cmd::xgroup_delconsumer, key, group, consumer); + + return reply::parse(*reply); +} + +long long Redis::xlen(const StringView &key) { + auto reply = command(cmd::xlen, key); + + return reply::parse(*reply); +} + +long long Redis::xtrim(const StringView &key, long long count, bool approx) { + auto reply = command(cmd::xtrim, key, count, approx); + + return reply::parse(*reply); +} + +} + +} diff --git a/controller/thirdparty/redis-plus-plus-1.1.1/src/sw/redis++/redis.h b/controller/thirdparty/redis-plus-plus-1.1.1/src/sw/redis++/redis.h new file mode 100644 index 000000000..b54afb96b --- /dev/null +++ b/controller/thirdparty/redis-plus-plus-1.1.1/src/sw/redis++/redis.h @@ -0,0 +1,1523 @@ +/************************************************************************** + Copyright (c) 2017 sewenew + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. + *************************************************************************/ + +#ifndef SEWENEW_REDISPLUSPLUS_REDIS_H +#define SEWENEW_REDISPLUSPLUS_REDIS_H + +#include +#include +#include +#include +#include +#include "connection_pool.h" +#include "reply.h" +#include "command_options.h" +#include "utils.h" +#include "subscriber.h" +#include "pipeline.h" +#include "transaction.h" +#include "sentinel.h" + +namespace sw { + +namespace redis { + +template +class QueuedRedis; + +using Transaction = QueuedRedis; + +using Pipeline = QueuedRedis; + +class Redis { +public: + Redis(const ConnectionOptions &connection_opts, + const ConnectionPoolOptions &pool_opts = {}) : _pool(pool_opts, connection_opts) {} + + // Construct Redis instance with URI: + // "tcp://127.0.0.1", "tcp://127.0.0.1:6379", or "unix://path/to/socket" + explicit Redis(const std::string &uri); + + Redis(const std::shared_ptr &sentinel, + const std::string &master_name, + Role role, + const ConnectionOptions &connection_opts, + const ConnectionPoolOptions &pool_opts = {}) : + _pool(SimpleSentinel(sentinel, master_name, role), pool_opts, connection_opts) {} + + Redis(const Redis &) = delete; + Redis& operator=(const Redis &) = delete; + + Redis(Redis &&) = default; + Redis& operator=(Redis &&) = default; + + Pipeline pipeline(); + + Transaction transaction(bool piped = false); + + Subscriber subscriber(); + + template + auto command(Cmd cmd, Args &&...args) + -> typename std::enable_if::value, ReplyUPtr>::type; + + template + auto command(const StringView &cmd_name, Args &&...args) + -> typename std::enable_if::type>::value, + ReplyUPtr>::type; + + template + auto command(const StringView &cmd_name, Args &&...args) + -> typename std::enable_if::type>::value, void>::type; + + template + Result command(const StringView &cmd_name, Args &&...args); + + template + auto command(Input first, Input last) + -> typename std::enable_if::value, ReplyUPtr>::type; + + template + auto command(Input first, Input last) + -> typename std::enable_if::value, Result>::type; + + template + auto command(Input first, Input last, Output output) + -> typename std::enable_if::value, void>::type; + + // CONNECTION commands. + + void auth(const StringView &password); + + std::string echo(const StringView &msg); + + std::string ping(); + + std::string ping(const StringView &msg); + + // After sending QUIT, only the current connection will be close, while + // other connections in the pool is still open. This is a strange behavior. + // So we DO NOT support the QUIT command. If you want to quit connection to + // server, just destroy the Redis object. + // + // void quit(); + + // We get a connection from the pool, and send the SELECT command to switch + // to a specified DB. However, when we try to send other commands to the + // given DB, we might get a different connection from the pool, and these + // commands, in fact, work on other DB. e.g. + // + // redis.select(1); // get a connection from the pool and switch to the 1th DB + // redis.get("key"); // might get another connection from the pool, + // // and try to get 'key' on the default DB + // + // Obviously, this is NOT what we expect. So we DO NOT support SELECT command. + // In order to select a DB, we can specify the DB index with the ConnectionOptions. + // + // However, since Pipeline and Transaction always send multiple commands on a + // single connection, these two classes have a *select* method. + // + // void select(long long idx); + + void swapdb(long long idx1, long long idx2); + + // SERVER commands. + + void bgrewriteaof(); + + void bgsave(); + + long long dbsize(); + + void flushall(bool async = false); + + void flushdb(bool async = false); + + std::string info(); + + std::string info(const StringView §ion); + + long long lastsave(); + + void save(); + + // KEY commands. + + long long del(const StringView &key); + + template + long long del(Input first, Input last); + + template + long long del(std::initializer_list il) { + return del(il.begin(), il.end()); + } + + OptionalString dump(const StringView &key); + + long long exists(const StringView &key); + + template + long long exists(Input first, Input last); + + template + long long exists(std::initializer_list il) { + return exists(il.begin(), il.end()); + } + + bool expire(const StringView &key, long long timeout); + + bool expire(const StringView &key, const std::chrono::seconds &timeout); + + bool expireat(const StringView &key, long long timestamp); + + bool expireat(const StringView &key, + const std::chrono::time_point &tp); + + template + void keys(const StringView &pattern, Output output); + + bool move(const StringView &key, long long db); + + bool persist(const StringView &key); + + bool pexpire(const StringView &key, long long timeout); + + bool pexpire(const StringView &key, const std::chrono::milliseconds &timeout); + + bool pexpireat(const StringView &key, long long timestamp); + + bool pexpireat(const StringView &key, + const std::chrono::time_point &tp); + + long long pttl(const StringView &key); + + OptionalString randomkey(); + + void rename(const StringView &key, const StringView &newkey); + + bool renamenx(const StringView &key, const StringView &newkey); + + void restore(const StringView &key, + const StringView &val, + long long ttl, + bool replace = false); + + void restore(const StringView &key, + const StringView &val, + const std::chrono::milliseconds &ttl = std::chrono::milliseconds{0}, + bool replace = false); + + // TODO: sort + + template + long long scan(long long cursor, + const StringView &pattern, + long long count, + Output output); + + template + long long scan(long long cursor, + Output output); + + template + long long scan(long long cursor, + const StringView &pattern, + Output output); + + template + long long scan(long long cursor, + long long count, + Output output); + + long long touch(const StringView &key); + + template + long long touch(Input first, Input last); + + template + long long touch(std::initializer_list il) { + return touch(il.begin(), il.end()); + } + + long long ttl(const StringView &key); + + std::string type(const StringView &key); + + long long unlink(const StringView &key); + + template + long long unlink(Input first, Input last); + + template + long long unlink(std::initializer_list il) { + return unlink(il.begin(), il.end()); + } + + long long wait(long long numslaves, long long timeout); + + long long wait(long long numslaves, const std::chrono::milliseconds &timeout); + + // STRING commands. + + long long append(const StringView &key, const StringView &str); + + long long bitcount(const StringView &key, long long start = 0, long long end = -1); + + long long bitop(BitOp op, const StringView &destination, const StringView &key); + + template + long long bitop(BitOp op, const StringView &destination, Input first, Input last); + + template + long long bitop(BitOp op, const StringView &destination, std::initializer_list il) { + return bitop(op, destination, il.begin(), il.end()); + } + + long long bitpos(const StringView &key, + long long bit, + long long start = 0, + long long end = -1); + + long long decr(const StringView &key); + + long long decrby(const StringView &key, long long decrement); + + OptionalString get(const StringView &key); + + long long getbit(const StringView &key, long long offset); + + std::string getrange(const StringView &key, long long start, long long end); + + OptionalString getset(const StringView &key, const StringView &val); + + long long incr(const StringView &key); + + long long incrby(const StringView &key, long long increment); + + double incrbyfloat(const StringView &key, double increment); + + template + void mget(Input first, Input last, Output output); + + template + void mget(std::initializer_list il, Output output) { + mget(il.begin(), il.end(), output); + } + + template + void mset(Input first, Input last); + + template + void mset(std::initializer_list il) { + mset(il.begin(), il.end()); + } + + template + bool msetnx(Input first, Input last); + + template + bool msetnx(std::initializer_list il) { + return msetnx(il.begin(), il.end()); + } + + void psetex(const StringView &key, + long long ttl, + const StringView &val); + + void psetex(const StringView &key, + const std::chrono::milliseconds &ttl, + const StringView &val); + + bool set(const StringView &key, + const StringView &val, + const std::chrono::milliseconds &ttl = std::chrono::milliseconds(0), + UpdateType type = UpdateType::ALWAYS); + + void setex(const StringView &key, + long long ttl, + const StringView &val); + + void setex(const StringView &key, + const std::chrono::seconds &ttl, + const StringView &val); + + bool setnx(const StringView &key, const StringView &val); + + long long setrange(const StringView &key, long long offset, const StringView &val); + + long long strlen(const StringView &key); + + // LIST commands. + + OptionalStringPair blpop(const StringView &key, long long timeout); + + OptionalStringPair blpop(const StringView &key, + const std::chrono::seconds &timeout = std::chrono::seconds{0}); + + template + OptionalStringPair blpop(Input first, Input last, long long timeout); + + template + OptionalStringPair blpop(std::initializer_list il, long long timeout) { + return blpop(il.begin(), il.end(), timeout); + } + + template + OptionalStringPair blpop(Input first, + Input last, + const std::chrono::seconds &timeout = std::chrono::seconds{0}); + + template + OptionalStringPair blpop(std::initializer_list il, + const std::chrono::seconds &timeout = std::chrono::seconds{0}) { + return blpop(il.begin(), il.end(), timeout); + } + + OptionalStringPair brpop(const StringView &key, long long timeout); + + OptionalStringPair brpop(const StringView &key, + const std::chrono::seconds &timeout = std::chrono::seconds{0}); + + template + OptionalStringPair brpop(Input first, Input last, long long timeout); + + template + OptionalStringPair brpop(std::initializer_list il, long long timeout) { + return brpop(il.begin(), il.end(), timeout); + } + + template + OptionalStringPair brpop(Input first, + Input last, + const std::chrono::seconds &timeout = std::chrono::seconds{0}); + + template + OptionalStringPair brpop(std::initializer_list il, + const std::chrono::seconds &timeout = std::chrono::seconds{0}) { + return brpop(il.begin(), il.end(), timeout); + } + + OptionalString brpoplpush(const StringView &source, + const StringView &destination, + long long timeout); + + OptionalString brpoplpush(const StringView &source, + const StringView &destination, + const std::chrono::seconds &timeout = std::chrono::seconds{0}); + + OptionalString lindex(const StringView &key, long long index); + + long long linsert(const StringView &key, + InsertPosition position, + const StringView &pivot, + const StringView &val); + + long long llen(const StringView &key); + + OptionalString lpop(const StringView &key); + + long long lpush(const StringView &key, const StringView &val); + + template + long long lpush(const StringView &key, Input first, Input last); + + template + long long lpush(const StringView &key, std::initializer_list il) { + return lpush(key, il.begin(), il.end()); + } + + long long lpushx(const StringView &key, const StringView &val); + + template + void lrange(const StringView &key, long long start, long long stop, Output output); + + long long lrem(const StringView &key, long long count, const StringView &val); + + void lset(const StringView &key, long long index, const StringView &val); + + void ltrim(const StringView &key, long long start, long long stop); + + OptionalString rpop(const StringView &key); + + OptionalString rpoplpush(const StringView &source, const StringView &destination); + + long long rpush(const StringView &key, const StringView &val); + + template + long long rpush(const StringView &key, Input first, Input last); + + template + long long rpush(const StringView &key, std::initializer_list il) { + return rpush(key, il.begin(), il.end()); + } + + long long rpushx(const StringView &key, const StringView &val); + + // HASH commands. + + long long hdel(const StringView &key, const StringView &field); + + template + long long hdel(const StringView &key, Input first, Input last); + + template + long long hdel(const StringView &key, std::initializer_list il) { + return hdel(key, il.begin(), il.end()); + } + + bool hexists(const StringView &key, const StringView &field); + + OptionalString hget(const StringView &key, const StringView &field); + + template + void hgetall(const StringView &key, Output output); + + long long hincrby(const StringView &key, const StringView &field, long long increment); + + double hincrbyfloat(const StringView &key, const StringView &field, double increment); + + template + void hkeys(const StringView &key, Output output); + + long long hlen(const StringView &key); + + template + void hmget(const StringView &key, Input first, Input last, Output output); + + template + void hmget(const StringView &key, std::initializer_list il, Output output) { + hmget(key, il.begin(), il.end(), output); + } + + template + void hmset(const StringView &key, Input first, Input last); + + template + void hmset(const StringView &key, std::initializer_list il) { + hmset(key, il.begin(), il.end()); + } + + template + long long hscan(const StringView &key, + long long cursor, + const StringView &pattern, + long long count, + Output output); + + template + long long hscan(const StringView &key, + long long cursor, + const StringView &pattern, + Output output); + + template + long long hscan(const StringView &key, + long long cursor, + long long count, + Output output); + + template + long long hscan(const StringView &key, + long long cursor, + Output output); + + bool hset(const StringView &key, const StringView &field, const StringView &val); + + bool hset(const StringView &key, const std::pair &item); + + bool hsetnx(const StringView &key, const StringView &field, const StringView &val); + + bool hsetnx(const StringView &key, const std::pair &item); + + long long hstrlen(const StringView &key, const StringView &field); + + template + void hvals(const StringView &key, Output output); + + // SET commands. + + long long sadd(const StringView &key, const StringView &member); + + template + long long sadd(const StringView &key, Input first, Input last); + + template + long long sadd(const StringView &key, std::initializer_list il) { + return sadd(key, il.begin(), il.end()); + } + + long long scard(const StringView &key); + + template + void sdiff(Input first, Input last, Output output); + + template + void sdiff(std::initializer_list il, Output output) { + sdiff(il.begin(), il.end(), output); + } + + long long sdiffstore(const StringView &destination, const StringView &key); + + template + long long sdiffstore(const StringView &destination, + Input first, + Input last); + + template + long long sdiffstore(const StringView &destination, + std::initializer_list il) { + return sdiffstore(destination, il.begin(), il.end()); + } + + template + void sinter(Input first, Input last, Output output); + + template + void sinter(std::initializer_list il, Output output) { + sinter(il.begin(), il.end(), output); + } + + long long sinterstore(const StringView &destination, const StringView &key); + + template + long long sinterstore(const StringView &destination, + Input first, + Input last); + + template + long long sinterstore(const StringView &destination, + std::initializer_list il) { + return sinterstore(destination, il.begin(), il.end()); + } + + bool sismember(const StringView &key, const StringView &member); + + template + void smembers(const StringView &key, Output output); + + bool smove(const StringView &source, + const StringView &destination, + const StringView &member); + + OptionalString spop(const StringView &key); + + template + void spop(const StringView &key, long long count, Output output); + + OptionalString srandmember(const StringView &key); + + template + void srandmember(const StringView &key, long long count, Output output); + + long long srem(const StringView &key, const StringView &member); + + template + long long srem(const StringView &key, Input first, Input last); + + template + long long srem(const StringView &key, std::initializer_list il) { + return srem(key, il.begin(), il.end()); + } + + template + long long sscan(const StringView &key, + long long cursor, + const StringView &pattern, + long long count, + Output output); + + template + long long sscan(const StringView &key, + long long cursor, + const StringView &pattern, + Output output); + + template + long long sscan(const StringView &key, + long long cursor, + long long count, + Output output); + + template + long long sscan(const StringView &key, + long long cursor, + Output output); + + template + void sunion(Input first, Input last, Output output); + + template + void sunion(std::initializer_list il, Output output) { + sunion(il.begin(), il.end(), output); + } + + long long sunionstore(const StringView &destination, const StringView &key); + + template + long long sunionstore(const StringView &destination, Input first, Input last); + + template + long long sunionstore(const StringView &destination, std::initializer_list il) { + return sunionstore(destination, il.begin(), il.end()); + } + + // SORTED SET commands. + + auto bzpopmax(const StringView &key, long long timeout) + -> Optional>; + + auto bzpopmax(const StringView &key, + const std::chrono::seconds &timeout = std::chrono::seconds{0}) + -> Optional>; + + template + auto bzpopmax(Input first, Input last, long long timeout) + -> Optional>; + + template + auto bzpopmax(Input first, + Input last, + const std::chrono::seconds &timeout = std::chrono::seconds{0}) + -> Optional>; + + template + auto bzpopmax(std::initializer_list il, long long timeout) + -> Optional> { + return bzpopmax(il.begin(), il.end(), timeout); + } + + template + auto bzpopmax(std::initializer_list il, + const std::chrono::seconds &timeout = std::chrono::seconds{0}) + -> Optional> { + return bzpopmax(il.begin(), il.end(), timeout); + } + + auto bzpopmin(const StringView &key, long long timeout) + -> Optional>; + + auto bzpopmin(const StringView &key, + const std::chrono::seconds &timeout = std::chrono::seconds{0}) + -> Optional>; + + template + auto bzpopmin(Input first, Input last, long long timeout) + -> Optional>; + + template + auto bzpopmin(Input first, + Input last, + const std::chrono::seconds &timeout = std::chrono::seconds{0}) + -> Optional>; + + template + auto bzpopmin(std::initializer_list il, long long timeout) + -> Optional> { + return bzpopmin(il.begin(), il.end(), timeout); + } + + template + auto bzpopmin(std::initializer_list il, + const std::chrono::seconds &timeout = std::chrono::seconds{0}) + -> Optional> { + return bzpopmin(il.begin(), il.end(), timeout); + } + + // We don't support the INCR option, since you can always use ZINCRBY instead. + long long zadd(const StringView &key, + const StringView &member, + double score, + UpdateType type = UpdateType::ALWAYS, + bool changed = false); + + template + long long zadd(const StringView &key, + Input first, + Input last, + UpdateType type = UpdateType::ALWAYS, + bool changed = false); + + template + long long zadd(const StringView &key, + std::initializer_list il, + UpdateType type = UpdateType::ALWAYS, + bool changed = false) { + return zadd(key, il.begin(), il.end(), type, changed); + } + + long long zcard(const StringView &key); + + template + long long zcount(const StringView &key, const Interval &interval); + + double zincrby(const StringView &key, double increment, const StringView &member); + + // There's no aggregation type parameter for single key overload, since these 3 types + // have the same effect. + long long zinterstore(const StringView &destination, const StringView &key, double weight); + + // If *Input* is an iterator of a container of string, + // we use the default weight, i.e. 1, and send + // *ZINTERSTORE destination numkeys key [key ...] [AGGREGATE SUM|MIN|MAX]* command. + // If *Input* is an iterator of a container of pair, i.e. key-weight pair, + // we send the command with the given weights: + // *ZINTERSTORE destination numkeys key [key ...] + // [WEIGHTS weight [weight ...]] [AGGREGATE SUM|MIN|MAX]* + // + // The following code use the default weight: + // + // vector keys = {"k1", "k2", "k3"}; + // redis.zinterstore(destination, keys.begin(), keys.end()); + // + // On the other hand, the following code use the given weights: + // + // vector> keys_with_weights = {{"k1", 1}, {"k2", 2}, {"k3", 3}}; + // redis.zinterstore(destination, keys_with_weights.begin(), keys_with_weights.end()); + // + // NOTE: `keys_with_weights` can also be of type `unordered_map`. + // However, it will be slower than vector>, since we use + // `distance(first, last)` to calculate the *numkeys* parameter. + // + // This also applies to *ZUNIONSTORE* command. + template + long long zinterstore(const StringView &destination, + Input first, + Input last, + Aggregation type = Aggregation::SUM); + + template + long long zinterstore(const StringView &destination, + std::initializer_list il, + Aggregation type = Aggregation::SUM) { + return zinterstore(destination, il.begin(), il.end(), type); + } + + template + long long zlexcount(const StringView &key, const Interval &interval); + + Optional> zpopmax(const StringView &key); + + template + void zpopmax(const StringView &key, long long count, Output output); + + Optional> zpopmin(const StringView &key); + + template + void zpopmin(const StringView &key, long long count, Output output); + + // If *output* is an iterator of a container of string, + // we send *ZRANGE key start stop* command. + // If it's an iterator of a container of pair, + // we send *ZRANGE key start stop WITHSCORES* command. + // + // The following code sends *ZRANGE* without the *WITHSCORES* option: + // + // vector result; + // redis.zrange("key", 0, -1, back_inserter(result)); + // + // On the other hand, the following code sends command with *WITHSCORES* option: + // + // unordered_map with_score; + // redis.zrange("key", 0, -1, inserter(with_score, with_score.end())); + // + // This also applies to other commands with the *WITHSCORES* option, + // e.g. *ZRANGEBYSCORE*, *ZREVRANGE*, *ZREVRANGEBYSCORE*. + template + void zrange(const StringView &key, long long start, long long stop, Output output); + + template + void zrangebylex(const StringView &key, const Interval &interval, Output output); + + template + void zrangebylex(const StringView &key, + const Interval &interval, + const LimitOptions &opts, + Output output); + + // See *zrange* comment on how to send command with *WITHSCORES* option. + template + void zrangebyscore(const StringView &key, const Interval &interval, Output output); + + // See *zrange* comment on how to send command with *WITHSCORES* option. + template + void zrangebyscore(const StringView &key, + const Interval &interval, + const LimitOptions &opts, + Output output); + + OptionalLongLong zrank(const StringView &key, const StringView &member); + + long long zrem(const StringView &key, const StringView &member); + + template + long long zrem(const StringView &key, Input first, Input last); + + template + long long zrem(const StringView &key, std::initializer_list il) { + return zrem(key, il.begin(), il.end()); + } + + template + long long zremrangebylex(const StringView &key, const Interval &interval); + + long long zremrangebyrank(const StringView &key, long long start, long long stop); + + template + long long zremrangebyscore(const StringView &key, const Interval &interval); + + // See *zrange* comment on how to send command with *WITHSCORES* option. + template + void zrevrange(const StringView &key, long long start, long long stop, Output output); + + template + void zrevrangebylex(const StringView &key, const Interval &interval, Output output); + + template + void zrevrangebylex(const StringView &key, + const Interval &interval, + const LimitOptions &opts, + Output output); + + // See *zrange* comment on how to send command with *WITHSCORES* option. + template + void zrevrangebyscore(const StringView &key, const Interval &interval, Output output); + + // See *zrange* comment on how to send command with *WITHSCORES* option. + template + void zrevrangebyscore(const StringView &key, + const Interval &interval, + const LimitOptions &opts, + Output output); + + OptionalLongLong zrevrank(const StringView &key, const StringView &member); + + template + long long zscan(const StringView &key, + long long cursor, + const StringView &pattern, + long long count, + Output output); + + template + long long zscan(const StringView &key, + long long cursor, + const StringView &pattern, + Output output); + + template + long long zscan(const StringView &key, + long long cursor, + long long count, + Output output); + + template + long long zscan(const StringView &key, + long long cursor, + Output output); + + OptionalDouble zscore(const StringView &key, const StringView &member); + + // There's no aggregation type parameter for single key overload, since these 3 types + // have the same effect. + long long zunionstore(const StringView &destination, const StringView &key, double weight); + + // See *zinterstore* comment for how to use this method. + template + long long zunionstore(const StringView &destination, + Input first, + Input last, + Aggregation type = Aggregation::SUM); + + template + long long zunionstore(const StringView &destination, + std::initializer_list il, + Aggregation type = Aggregation::SUM) { + return zunionstore(destination, il.begin(), il.end(), type); + } + + // HYPERLOGLOG commands. + + bool pfadd(const StringView &key, const StringView &element); + + template + bool pfadd(const StringView &key, Input first, Input last); + + template + bool pfadd(const StringView &key, std::initializer_list il) { + return pfadd(key, il.begin(), il.end()); + } + + long long pfcount(const StringView &key); + + template + long long pfcount(Input first, Input last); + + template + long long pfcount(std::initializer_list il) { + return pfcount(il.begin(), il.end()); + } + + void pfmerge(const StringView &destination, const StringView &key); + + template + void pfmerge(const StringView &destination, Input first, Input last); + + template + void pfmerge(const StringView &destination, std::initializer_list il) { + pfmerge(destination, il.begin(), il.end()); + } + + // GEO commands. + + long long geoadd(const StringView &key, + const std::tuple &member); + + template + long long geoadd(const StringView &key, + Input first, + Input last); + + template + long long geoadd(const StringView &key, + std::initializer_list il) { + return geoadd(key, il.begin(), il.end()); + } + + OptionalDouble geodist(const StringView &key, + const StringView &member1, + const StringView &member2, + GeoUnit unit = GeoUnit::M); + + template + void geohash(const StringView &key, Input first, Input last, Output output); + + template + void geohash(const StringView &key, std::initializer_list il, Output output) { + geohash(key, il.begin(), il.end(), output); + } + + template + void geopos(const StringView &key, Input first, Input last, Output output); + + template + void geopos(const StringView &key, std::initializer_list il, Output output) { + geopos(key, il.begin(), il.end(), output); + } + + // TODO: + // 1. since we have different overloads for georadius and georadius-store, + // we might use the GEORADIUS_RO command in the future. + // 2. there're too many parameters for this method, we might refactor it. + OptionalLongLong georadius(const StringView &key, + const std::pair &loc, + double radius, + GeoUnit unit, + const StringView &destination, + bool store_dist, + long long count); + + // If *output* is an iterator of a container of string, we send *GEORADIUS* command + // without any options and only get the members in the specified geo range. + // If *output* is an iterator of a container of a tuple, the type of the tuple decides + // options we send with the *GEORADIUS* command. If the tuple has an element of type + // double, we send the *WITHDIST* option. If it has an element of type string, we send + // the *WITHHASH* option. If it has an element of type pair, we send + // the *WITHCOORD* option. For example: + // + // The following code only gets the members in range, i.e. without any option. + // + // vector members; + // redis.georadius("key", make_pair(10.1, 10.2), 10, GeoUnit::KM, 10, true, + // back_inserter(members)) + // + // The following code sends the command with *WITHDIST* option. + // + // vector> with_dist; + // redis.georadius("key", make_pair(10.1, 10.2), 10, GeoUnit::KM, 10, true, + // back_inserter(with_dist)) + // + // The following code sends the command with *WITHDIST* and *WITHHASH* options. + // + // vector> with_dist_hash; + // redis.georadius("key", make_pair(10.1, 10.2), 10, GeoUnit::KM, 10, true, + // back_inserter(with_dist_hash)) + // + // The following code sends the command with *WITHDIST*, *WITHCOORD* and *WITHHASH* options. + // + // vector, string>> with_dist_coord_hash; + // redis.georadius("key", make_pair(10.1, 10.2), 10, GeoUnit::KM, 10, true, + // back_inserter(with_dist_coord_hash)) + // + // This also applies to *GEORADIUSBYMEMBER*. + template + void georadius(const StringView &key, + const std::pair &loc, + double radius, + GeoUnit unit, + long long count, + bool asc, + Output output); + + OptionalLongLong georadiusbymember(const StringView &key, + const StringView &member, + double radius, + GeoUnit unit, + const StringView &destination, + bool store_dist, + long long count); + + // See comments on *GEORADIUS*. + template + void georadiusbymember(const StringView &key, + const StringView &member, + double radius, + GeoUnit unit, + long long count, + bool asc, + Output output); + + // SCRIPTING commands. + + template + Result eval(const StringView &script, + std::initializer_list keys, + std::initializer_list args); + + template + void eval(const StringView &script, + std::initializer_list keys, + std::initializer_list args, + Output output); + + template + Result evalsha(const StringView &script, + std::initializer_list keys, + std::initializer_list args); + + template + void evalsha(const StringView &script, + std::initializer_list keys, + std::initializer_list args, + Output output); + + template + void script_exists(Input first, Input last, Output output); + + template + void script_exists(std::initializer_list il, Output output) { + script_exists(il.begin(), il.end(), output); + } + + void script_flush(); + + void script_kill(); + + std::string script_load(const StringView &script); + + // PUBSUB commands. + + long long publish(const StringView &channel, const StringView &message); + + // Transaction commands. + void watch(const StringView &key); + + template + void watch(Input first, Input last); + + template + void watch(std::initializer_list il) { + watch(il.begin(), il.end()); + } + + // Stream commands. + + long long xack(const StringView &key, const StringView &group, const StringView &id); + + template + long long xack(const StringView &key, const StringView &group, Input first, Input last); + + template + long long xack(const StringView &key, const StringView &group, std::initializer_list il) { + return xack(key, group, il.begin(), il.end()); + } + + template + std::string xadd(const StringView &key, const StringView &id, Input first, Input last); + + template + std::string xadd(const StringView &key, const StringView &id, std::initializer_list il) { + return xadd(key, id, il.begin(), il.end()); + } + + template + std::string xadd(const StringView &key, + const StringView &id, + Input first, + Input last, + long long count, + bool approx = true); + + template + std::string xadd(const StringView &key, + const StringView &id, + std::initializer_list il, + long long count, + bool approx = true) { + return xadd(key, id, il.begin(), il.end(), count, approx); + } + + template + void xclaim(const StringView &key, + const StringView &group, + const StringView &consumer, + const std::chrono::milliseconds &min_idle_time, + const StringView &id, + Output output); + + template + void xclaim(const StringView &key, + const StringView &group, + const StringView &consumer, + const std::chrono::milliseconds &min_idle_time, + Input first, + Input last, + Output output); + + template + void xclaim(const StringView &key, + const StringView &group, + const StringView &consumer, + const std::chrono::milliseconds &min_idle_time, + std::initializer_list il, + Output output) { + xclaim(key, group, consumer, min_idle_time, il.begin(), il.end(), output); + } + + long long xdel(const StringView &key, const StringView &id); + + template + long long xdel(const StringView &key, Input first, Input last); + + template + long long xdel(const StringView &key, std::initializer_list il) { + return xdel(key, il.begin(), il.end()); + } + + void xgroup_create(const StringView &key, + const StringView &group, + const StringView &id, + bool mkstream = false); + + void xgroup_setid(const StringView &key, const StringView &group, const StringView &id); + + long long xgroup_destroy(const StringView &key, const StringView &group); + + long long xgroup_delconsumer(const StringView &key, + const StringView &group, + const StringView &consumer); + + long long xlen(const StringView &key); + + template + auto xpending(const StringView &key, const StringView &group, Output output) + -> std::tuple; + + template + void xpending(const StringView &key, + const StringView &group, + const StringView &start, + const StringView &end, + long long count, + Output output); + + template + void xpending(const StringView &key, + const StringView &group, + const StringView &start, + const StringView &end, + long long count, + const StringView &consumer, + Output output); + + template + void xrange(const StringView &key, + const StringView &start, + const StringView &end, + Output output); + + template + void xrange(const StringView &key, + const StringView &start, + const StringView &end, + long long count, + Output output); + + template + void xread(const StringView &key, + const StringView &id, + long long count, + Output output); + + template + void xread(const StringView &key, + const StringView &id, + Output output) { + xread(key, id, 0, output); + } + + template + auto xread(Input first, Input last, long long count, Output output) + -> typename std::enable_if::value>::type; + + template + auto xread(Input first, Input last, Output output) + -> typename std::enable_if::value>::type { + xread(first ,last, 0, output); + } + + template + void xread(const StringView &key, + const StringView &id, + const std::chrono::milliseconds &timeout, + long long count, + Output output); + + template + void xread(const StringView &key, + const StringView &id, + const std::chrono::milliseconds &timeout, + Output output) { + xread(key, id, timeout, 0, output); + } + + template + auto xread(Input first, + Input last, + const std::chrono::milliseconds &timeout, + long long count, + Output output) + -> typename std::enable_if::value>::type; + + template + auto xread(Input first, + Input last, + const std::chrono::milliseconds &timeout, + Output output) + -> typename std::enable_if::value>::type { + xread(first, last, timeout, 0, output); + } + + template + void xreadgroup(const StringView &group, + const StringView &consumer, + const StringView &key, + const StringView &id, + long long count, + bool noack, + Output output); + + template + void xreadgroup(const StringView &group, + const StringView &consumer, + const StringView &key, + const StringView &id, + long long count, + Output output) { + xreadgroup(group, consumer, key, id, count, false, output); + } + + template + void xreadgroup(const StringView &group, + const StringView &consumer, + const StringView &key, + const StringView &id, + Output output) { + xreadgroup(group, consumer, key, id, 0, false, output); + } + + template + auto xreadgroup(const StringView &group, + const StringView &consumer, + Input first, + Input last, + long long count, + bool noack, + Output output) + -> typename std::enable_if::value>::type; + + template + auto xreadgroup(const StringView &group, + const StringView &consumer, + Input first, + Input last, + long long count, + Output output) + -> typename std::enable_if::value>::type { + xreadgroup(group, consumer, first ,last, count, false, output); + } + + template + auto xreadgroup(const StringView &group, + const StringView &consumer, + Input first, + Input last, + Output output) + -> typename std::enable_if::value>::type { + xreadgroup(group, consumer, first ,last, 0, false, output); + } + + template + void xreadgroup(const StringView &group, + const StringView &consumer, + const StringView &key, + const StringView &id, + const std::chrono::milliseconds &timeout, + long long count, + bool noack, + Output output); + + template + void xreadgroup(const StringView &group, + const StringView &consumer, + const StringView &key, + const StringView &id, + const std::chrono::milliseconds &timeout, + long long count, + Output output) { + xreadgroup(group, consumer, key, id, timeout, count, false, output); + } + + template + void xreadgroup(const StringView &group, + const StringView &consumer, + const StringView &key, + const StringView &id, + const std::chrono::milliseconds &timeout, + Output output) { + xreadgroup(group, consumer, key, id, timeout, 0, false, output); + } + + template + auto xreadgroup(const StringView &group, + const StringView &consumer, + Input first, + Input last, + const std::chrono::milliseconds &timeout, + long long count, + bool noack, + Output output) + -> typename std::enable_if::value>::type; + + template + auto xreadgroup(const StringView &group, + const StringView &consumer, + Input first, + Input last, + const std::chrono::milliseconds &timeout, + long long count, + Output output) + -> typename std::enable_if::value>::type { + xreadgroup(group, consumer, first, last, timeout, count, false, output); + } + + template + auto xreadgroup(const StringView &group, + const StringView &consumer, + Input first, + Input last, + const std::chrono::milliseconds &timeout, + Output output) + -> typename std::enable_if::value>::type { + xreadgroup(group, consumer, first, last, timeout, 0, false, output); + } + + template + void xrevrange(const StringView &key, + const StringView &end, + const StringView &start, + Output output); + + template + void xrevrange(const StringView &key, + const StringView &end, + const StringView &start, + long long count, + Output output); + + long long xtrim(const StringView &key, long long count, bool approx = true); + +private: + class ConnectionPoolGuard { + public: + ConnectionPoolGuard(ConnectionPool &pool, + Connection &connection) : _pool(pool), _connection(connection) {} + + ~ConnectionPoolGuard() { + _pool.release(std::move(_connection)); + } + + private: + ConnectionPool &_pool; + Connection &_connection; + }; + + template + friend class QueuedRedis; + + friend class RedisCluster; + + // For internal use. + explicit Redis(const ConnectionSPtr &connection); + + template + ReplyUPtr _command(const StringView &cmd_name, const IndexSequence &, Args &&...args) { + return command(cmd_name, NthValue(std::forward(args)...)...); + } + + template + ReplyUPtr _command(Connection &connection, Cmd cmd, Args &&...args); + + template + ReplyUPtr _score_command(std::true_type, Cmd cmd, Args &&... args); + + template + ReplyUPtr _score_command(std::false_type, Cmd cmd, Args &&... args); + + template + ReplyUPtr _score_command(Cmd cmd, Args &&... args); + + // Pool Mode. + // Public constructors create a *Redis* instance with a pool. + // In this case, *_connection* is a null pointer, and is never used. + ConnectionPool _pool; + + // Single Connection Mode. + // Private constructor creats a *Redis* instance with a single connection. + // This is used when we create Transaction, Pipeline and Subscriber. + // In this case, *_pool* is empty, and is never used. + ConnectionSPtr _connection; +}; + +} + +} + +#include "redis.hpp" + +#endif // end SEWENEW_REDISPLUSPLUS_REDIS_H diff --git a/controller/thirdparty/redis-plus-plus-1.1.1/src/sw/redis++/redis.hpp b/controller/thirdparty/redis-plus-plus-1.1.1/src/sw/redis++/redis.hpp new file mode 100644 index 000000000..3a227a6f1 --- /dev/null +++ b/controller/thirdparty/redis-plus-plus-1.1.1/src/sw/redis++/redis.hpp @@ -0,0 +1,1365 @@ +/************************************************************************** + Copyright (c) 2017 sewenew + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. + *************************************************************************/ + +#ifndef SEWENEW_REDISPLUSPLUS_REDIS_HPP +#define SEWENEW_REDISPLUSPLUS_REDIS_HPP + +#include "command.h" +#include "reply.h" +#include "utils.h" +#include "errors.h" + +namespace sw { + +namespace redis { + +template +auto Redis::command(Cmd cmd, Args &&...args) + -> typename std::enable_if::value, ReplyUPtr>::type { + if (_connection) { + // Single Connection Mode. + // TODO: In this case, should we reconnect? + if (_connection->broken()) { + throw Error("Connection is broken"); + } + + return _command(*_connection, cmd, std::forward(args)...); + } else { + // Pool Mode, i.e. get connection from pool. + auto connection = _pool.fetch(); + + assert(!connection.broken()); + + ConnectionPoolGuard guard(_pool, connection); + + return _command(connection, cmd, std::forward(args)...); + } +} + +template +auto Redis::command(const StringView &cmd_name, Args &&...args) + -> typename std::enable_if::type>::value, ReplyUPtr>::type { + auto cmd = [](Connection &connection, const StringView &cmd_name, Args &&...args) { + CmdArgs cmd_args; + cmd_args.append(cmd_name, std::forward(args)...); + connection.send(cmd_args); + }; + + return command(cmd, cmd_name, std::forward(args)...); +} + +template +auto Redis::command(Input first, Input last) + -> typename std::enable_if::value, ReplyUPtr>::type { + if (first == last) { + throw Error("command: empty range"); + } + + auto cmd = [](Connection &connection, Input first, Input last) { + CmdArgs cmd_args; + while (first != last) { + cmd_args.append(*first); + ++first; + } + connection.send(cmd_args); + }; + + return command(cmd, first, last); +} + +template +Result Redis::command(const StringView &cmd_name, Args &&...args) { + auto r = command(cmd_name, std::forward(args)...); + + assert(r); + + return reply::parse(*r); +} + +template +auto Redis::command(const StringView &cmd_name, Args &&...args) + -> typename std::enable_if::type>::value, void>::type { + auto r = _command(cmd_name, + MakeIndexSequence(), + std::forward(args)...); + + assert(r); + + reply::to_array(*r, LastValue(std::forward(args)...)); +} + +template +auto Redis::command(Input first, Input last) + -> typename std::enable_if::value, Result>::type { + auto r = command(first, last); + + assert(r); + + return reply::parse(*r); +} + +template +auto Redis::command(Input first, Input last, Output output) + -> typename std::enable_if::value, void>::type { + auto r = command(first, last); + + assert(r); + + reply::to_array(*r, output); +} + +// KEY commands. + +template +long long Redis::del(Input first, Input last) { + if (first == last) { + throw Error("DEL: no key specified"); + } + + auto reply = command(cmd::del_range, first, last); + + return reply::parse(*reply); +} + +template +long long Redis::exists(Input first, Input last) { + if (first == last) { + throw Error("EXISTS: no key specified"); + } + + auto reply = command(cmd::exists_range, first, last); + + return reply::parse(*reply); +} + +inline bool Redis::expire(const StringView &key, const std::chrono::seconds &timeout) { + return expire(key, timeout.count()); +} + +inline bool Redis::expireat(const StringView &key, + const std::chrono::time_point &tp) { + return expireat(key, tp.time_since_epoch().count()); +} + +template +void Redis::keys(const StringView &pattern, Output output) { + auto reply = command(cmd::keys, pattern); + + reply::to_array(*reply, output); +} + +inline bool Redis::pexpire(const StringView &key, const std::chrono::milliseconds &timeout) { + return pexpire(key, timeout.count()); +} + +inline bool Redis::pexpireat(const StringView &key, + const std::chrono::time_point &tp) { + return pexpireat(key, tp.time_since_epoch().count()); +} + +inline void Redis::restore(const StringView &key, + const StringView &val, + const std::chrono::milliseconds &ttl, + bool replace) { + return restore(key, val, ttl.count(), replace); +} + +template +long long Redis::scan(long long cursor, + const StringView &pattern, + long long count, + Output output) { + auto reply = command(cmd::scan, cursor, pattern, count); + + return reply::parse_scan_reply(*reply, output); +} + +template +inline long long Redis::scan(long long cursor, + const StringView &pattern, + Output output) { + return scan(cursor, pattern, 10, output); +} + +template +inline long long Redis::scan(long long cursor, + long long count, + Output output) { + return scan(cursor, "*", count, output); +} + +template +inline long long Redis::scan(long long cursor, + Output output) { + return scan(cursor, "*", 10, output); +} + +template +long long Redis::touch(Input first, Input last) { + if (first == last) { + throw Error("TOUCH: no key specified"); + } + + auto reply = command(cmd::touch_range, first, last); + + return reply::parse(*reply); +} + +template +long long Redis::unlink(Input first, Input last) { + if (first == last) { + throw Error("UNLINK: no key specified"); + } + + auto reply = command(cmd::unlink_range, first, last); + + return reply::parse(*reply); +} + +inline long long Redis::wait(long long numslaves, const std::chrono::milliseconds &timeout) { + return wait(numslaves, timeout.count()); +} + +// STRING commands. + +template +long long Redis::bitop(BitOp op, const StringView &destination, Input first, Input last) { + if (first == last) { + throw Error("BITOP: no key specified"); + } + + auto reply = command(cmd::bitop_range, op, destination, first, last); + + return reply::parse(*reply); +} + +template +void Redis::mget(Input first, Input last, Output output) { + if (first == last) { + throw Error("MGET: no key specified"); + } + + auto reply = command(cmd::mget, first, last); + + reply::to_array(*reply, output); +} + +template +void Redis::mset(Input first, Input last) { + if (first == last) { + throw Error("MSET: no key specified"); + } + + auto reply = command(cmd::mset, first, last); + + reply::parse(*reply); +} + +template +bool Redis::msetnx(Input first, Input last) { + if (first == last) { + throw Error("MSETNX: no key specified"); + } + + auto reply = command(cmd::msetnx, first, last); + + return reply::parse(*reply); +} + +inline void Redis::psetex(const StringView &key, + const std::chrono::milliseconds &ttl, + const StringView &val) { + return psetex(key, ttl.count(), val); +} + +inline void Redis::setex(const StringView &key, + const std::chrono::seconds &ttl, + const StringView &val) { + setex(key, ttl.count(), val); +} + +// LIST commands. + +template +OptionalStringPair Redis::blpop(Input first, Input last, long long timeout) { + if (first == last) { + throw Error("BLPOP: no key specified"); + } + + auto reply = command(cmd::blpop_range, first, last, timeout); + + return reply::parse(*reply); +} + +template +OptionalStringPair Redis::blpop(Input first, + Input last, + const std::chrono::seconds &timeout) { + return blpop(first, last, timeout.count()); +} + +template +OptionalStringPair Redis::brpop(Input first, Input last, long long timeout) { + if (first == last) { + throw Error("BRPOP: no key specified"); + } + + auto reply = command(cmd::brpop_range, first, last, timeout); + + return reply::parse(*reply); +} + +template +OptionalStringPair Redis::brpop(Input first, + Input last, + const std::chrono::seconds &timeout) { + return brpop(first, last, timeout.count()); +} + +inline OptionalString Redis::brpoplpush(const StringView &source, + const StringView &destination, + const std::chrono::seconds &timeout) { + return brpoplpush(source, destination, timeout.count()); +} + +template +inline long long Redis::lpush(const StringView &key, Input first, Input last) { + if (first == last) { + throw Error("LPUSH: no key specified"); + } + + auto reply = command(cmd::lpush_range, key, first, last); + + return reply::parse(*reply); +} + +template +inline void Redis::lrange(const StringView &key, long long start, long long stop, Output output) { + auto reply = command(cmd::lrange, key, start, stop); + + reply::to_array(*reply, output); +} + +template +inline long long Redis::rpush(const StringView &key, Input first, Input last) { + if (first == last) { + throw Error("RPUSH: no key specified"); + } + + auto reply = command(cmd::rpush_range, key, first, last); + + return reply::parse(*reply); +} + +// HASH commands. + +template +inline long long Redis::hdel(const StringView &key, Input first, Input last) { + if (first == last) { + throw Error("HDEL: no key specified"); + } + + auto reply = command(cmd::hdel_range, key, first, last); + + return reply::parse(*reply); +} + +template +inline void Redis::hgetall(const StringView &key, Output output) { + auto reply = command(cmd::hgetall, key); + + reply::to_array(*reply, output); +} + +template +inline void Redis::hkeys(const StringView &key, Output output) { + auto reply = command(cmd::hkeys, key); + + reply::to_array(*reply, output); +} + +template +inline void Redis::hmget(const StringView &key, Input first, Input last, Output output) { + if (first == last) { + throw Error("HMGET: no key specified"); + } + + auto reply = command(cmd::hmget, key, first, last); + + reply::to_array(*reply, output); +} + +template +inline void Redis::hmset(const StringView &key, Input first, Input last) { + if (first == last) { + throw Error("HMSET: no key specified"); + } + + auto reply = command(cmd::hmset, key, first, last); + + reply::parse(*reply); +} + +template +long long Redis::hscan(const StringView &key, + long long cursor, + const StringView &pattern, + long long count, + Output output) { + auto reply = command(cmd::hscan, key, cursor, pattern, count); + + return reply::parse_scan_reply(*reply, output); +} + +template +inline long long Redis::hscan(const StringView &key, + long long cursor, + const StringView &pattern, + Output output) { + return hscan(key, cursor, pattern, 10, output); +} + +template +inline long long Redis::hscan(const StringView &key, + long long cursor, + long long count, + Output output) { + return hscan(key, cursor, "*", count, output); +} + +template +inline long long Redis::hscan(const StringView &key, + long long cursor, + Output output) { + return hscan(key, cursor, "*", 10, output); +} + +template +inline void Redis::hvals(const StringView &key, Output output) { + auto reply = command(cmd::hvals, key); + + reply::to_array(*reply, output); +} + +// SET commands. + +template +long long Redis::sadd(const StringView &key, Input first, Input last) { + if (first == last) { + throw Error("SADD: no key specified"); + } + + auto reply = command(cmd::sadd_range, key, first, last); + + return reply::parse(*reply); +} + +template +void Redis::sdiff(Input first, Input last, Output output) { + if (first == last) { + throw Error("SDIFF: no key specified"); + } + + auto reply = command(cmd::sdiff, first, last); + + reply::to_array(*reply, output); +} + +template +long long Redis::sdiffstore(const StringView &destination, + Input first, + Input last) { + if (first == last) { + throw Error("SDIFFSTORE: no key specified"); + } + + auto reply = command(cmd::sdiffstore_range, destination, first, last); + + return reply::parse(*reply); +} + +template +void Redis::sinter(Input first, Input last, Output output) { + if (first == last) { + throw Error("SINTER: no key specified"); + } + + auto reply = command(cmd::sinter, first, last); + + reply::to_array(*reply, output); +} + +template +long long Redis::sinterstore(const StringView &destination, + Input first, + Input last) { + if (first == last) { + throw Error("SINTERSTORE: no key specified"); + } + + auto reply = command(cmd::sinterstore_range, destination, first, last); + + return reply::parse(*reply); +} + +template +void Redis::smembers(const StringView &key, Output output) { + auto reply = command(cmd::smembers, key); + + reply::to_array(*reply, output); +} + +template +void Redis::spop(const StringView &key, long long count, Output output) { + auto reply = command(cmd::spop_range, key, count); + + reply::to_array(*reply, output); +} + +template +void Redis::srandmember(const StringView &key, long long count, Output output) { + auto reply = command(cmd::srandmember_range, key, count); + + reply::to_array(*reply, output); +} + +template +long long Redis::srem(const StringView &key, Input first, Input last) { + if (first == last) { + throw Error("SREM: no key specified"); + } + + auto reply = command(cmd::srem_range, key, first, last); + + return reply::parse(*reply); +} + +template +long long Redis::sscan(const StringView &key, + long long cursor, + const StringView &pattern, + long long count, + Output output) { + auto reply = command(cmd::sscan, key, cursor, pattern, count); + + return reply::parse_scan_reply(*reply, output); +} + +template +inline long long Redis::sscan(const StringView &key, + long long cursor, + const StringView &pattern, + Output output) { + return sscan(key, cursor, pattern, 10, output); +} + +template +inline long long Redis::sscan(const StringView &key, + long long cursor, + long long count, + Output output) { + return sscan(key, cursor, "*", count, output); +} + +template +inline long long Redis::sscan(const StringView &key, + long long cursor, + Output output) { + return sscan(key, cursor, "*", 10, output); +} + +template +void Redis::sunion(Input first, Input last, Output output) { + if (first == last) { + throw Error("SUNION: no key specified"); + } + + auto reply = command(cmd::sunion, first, last); + + reply::to_array(*reply, output); +} + +template +long long Redis::sunionstore(const StringView &destination, Input first, Input last) { + if (first == last) { + throw Error("SUNIONSTORE: no key specified"); + } + + auto reply = command(cmd::sunionstore_range, destination, first, last); + + return reply::parse(*reply); +} + +// SORTED SET commands. + +inline auto Redis::bzpopmax(const StringView &key, const std::chrono::seconds &timeout) + -> Optional> { + return bzpopmax(key, timeout.count()); +} + +template +auto Redis::bzpopmax(Input first, Input last, long long timeout) + -> Optional> { + auto reply = command(cmd::bzpopmax_range, first, last, timeout); + + return reply::parse>>(*reply); +} + +template +inline auto Redis::bzpopmax(Input first, + Input last, + const std::chrono::seconds &timeout) + -> Optional> { + return bzpopmax(first, last, timeout.count()); +} + +inline auto Redis::bzpopmin(const StringView &key, const std::chrono::seconds &timeout) + -> Optional> { + return bzpopmin(key, timeout.count()); +} + +template +auto Redis::bzpopmin(Input first, Input last, long long timeout) + -> Optional> { + auto reply = command(cmd::bzpopmin_range, first, last, timeout); + + return reply::parse>>(*reply); +} + +template +inline auto Redis::bzpopmin(Input first, + Input last, + const std::chrono::seconds &timeout) + -> Optional> { + return bzpopmin(first, last, timeout.count()); +} + +template +long long Redis::zadd(const StringView &key, + Input first, + Input last, + UpdateType type, + bool changed) { + if (first == last) { + throw Error("ZADD: no key specified"); + } + + auto reply = command(cmd::zadd_range, key, first, last, type, changed); + + return reply::parse(*reply); +} + +template +long long Redis::zcount(const StringView &key, const Interval &interval) { + auto reply = command(cmd::zcount, key, interval); + + return reply::parse(*reply); +} + +template +long long Redis::zinterstore(const StringView &destination, + Input first, + Input last, + Aggregation type) { + if (first == last) { + throw Error("ZINTERSTORE: no key specified"); + } + + auto reply = command(cmd::zinterstore_range, + destination, + first, + last, + type); + + return reply::parse(*reply); +} + +template +long long Redis::zlexcount(const StringView &key, const Interval &interval) { + auto reply = command(cmd::zlexcount, key, interval); + + return reply::parse(*reply); +} + +template +void Redis::zpopmax(const StringView &key, long long count, Output output) { + auto reply = command(cmd::zpopmax, key, count); + + reply::to_array(*reply, output); +} + +template +void Redis::zpopmin(const StringView &key, long long count, Output output) { + auto reply = command(cmd::zpopmin, key, count); + + reply::to_array(*reply, output); +} + +template +void Redis::zrange(const StringView &key, long long start, long long stop, Output output) { + auto reply = _score_command(cmd::zrange, key, start, stop); + + reply::to_array(*reply, output); +} + +template +void Redis::zrangebylex(const StringView &key, const Interval &interval, Output output) { + zrangebylex(key, interval, {}, output); +} + +template +void Redis::zrangebylex(const StringView &key, + const Interval &interval, + const LimitOptions &opts, + Output output) { + auto reply = command(cmd::zrangebylex, key, interval, opts); + + reply::to_array(*reply, output); +} + +template +void Redis::zrangebyscore(const StringView &key, + const Interval &interval, + Output output) { + zrangebyscore(key, interval, {}, output); +} + +template +void Redis::zrangebyscore(const StringView &key, + const Interval &interval, + const LimitOptions &opts, + Output output) { + auto reply = _score_command(cmd::zrangebyscore, + key, + interval, + opts); + + reply::to_array(*reply, output); +} + +template +long long Redis::zrem(const StringView &key, Input first, Input last) { + if (first == last) { + throw Error("ZREM: no key specified"); + } + + auto reply = command(cmd::zrem_range, key, first, last); + + return reply::parse(*reply); +} + +template +long long Redis::zremrangebylex(const StringView &key, const Interval &interval) { + auto reply = command(cmd::zremrangebylex, key, interval); + + return reply::parse(*reply); +} + +template +long long Redis::zremrangebyscore(const StringView &key, const Interval &interval) { + auto reply = command(cmd::zremrangebyscore, key, interval); + + return reply::parse(*reply); +} + +template +void Redis::zrevrange(const StringView &key, long long start, long long stop, Output output) { + auto reply = _score_command(cmd::zrevrange, key, start, stop); + + reply::to_array(*reply, output); +} + +template +inline void Redis::zrevrangebylex(const StringView &key, + const Interval &interval, + Output output) { + zrevrangebylex(key, interval, {}, output); +} + +template +void Redis::zrevrangebylex(const StringView &key, + const Interval &interval, + const LimitOptions &opts, + Output output) { + auto reply = command(cmd::zrevrangebylex, key, interval, opts); + + reply::to_array(*reply, output); +} + +template +void Redis::zrevrangebyscore(const StringView &key, const Interval &interval, Output output) { + zrevrangebyscore(key, interval, {}, output); +} + +template +void Redis::zrevrangebyscore(const StringView &key, + const Interval &interval, + const LimitOptions &opts, + Output output) { + auto reply = _score_command(cmd::zrevrangebyscore, key, interval, opts); + + reply::to_array(*reply, output); +} + +template +long long Redis::zscan(const StringView &key, + long long cursor, + const StringView &pattern, + long long count, + Output output) { + auto reply = command(cmd::zscan, key, cursor, pattern, count); + + return reply::parse_scan_reply(*reply, output); +} + +template +inline long long Redis::zscan(const StringView &key, + long long cursor, + const StringView &pattern, + Output output) { + return zscan(key, cursor, pattern, 10, output); +} + +template +inline long long Redis::zscan(const StringView &key, + long long cursor, + long long count, + Output output) { + return zscan(key, cursor, "*", count, output); +} + +template +inline long long Redis::zscan(const StringView &key, + long long cursor, + Output output) { + return zscan(key, cursor, "*", 10, output); +} + +template +long long Redis::zunionstore(const StringView &destination, + Input first, + Input last, + Aggregation type) { + if (first == last) { + throw Error("ZUNIONSTORE: no key specified"); + } + + auto reply = command(cmd::zunionstore_range, + destination, + first, + last, + type); + + return reply::parse(*reply); +} + +// HYPERLOGLOG commands. + +template +bool Redis::pfadd(const StringView &key, Input first, Input last) { + if (first == last) { + throw Error("PFADD: no key specified"); + } + + auto reply = command(cmd::pfadd_range, key, first, last); + + return reply::parse(*reply); +} + +template +long long Redis::pfcount(Input first, Input last) { + if (first == last) { + throw Error("PFCOUNT: no key specified"); + } + + auto reply = command(cmd::pfcount_range, first, last); + + return reply::parse(*reply); +} + +template +void Redis::pfmerge(const StringView &destination, + Input first, + Input last) { + if (first == last) { + throw Error("PFMERGE: no key specified"); + } + + auto reply = command(cmd::pfmerge_range, destination, first, last); + + reply::parse(*reply); +} + +// GEO commands. + +template +inline long long Redis::geoadd(const StringView &key, + Input first, + Input last) { + if (first == last) { + throw Error("GEOADD: no key specified"); + } + + auto reply = command(cmd::geoadd_range, key, first, last); + + return reply::parse(*reply); +} + +template +void Redis::geohash(const StringView &key, Input first, Input last, Output output) { + if (first == last) { + throw Error("GEOHASH: no key specified"); + } + + auto reply = command(cmd::geohash_range, key, first, last); + + reply::to_array(*reply, output); +} + +template +void Redis::geopos(const StringView &key, Input first, Input last, Output output) { + if (first == last) { + throw Error("GEOPOS: no key specified"); + } + + auto reply = command(cmd::geopos_range, key, first, last); + + reply::to_array(*reply, output); +} + +template +void Redis::georadius(const StringView &key, + const std::pair &loc, + double radius, + GeoUnit unit, + long long count, + bool asc, + Output output) { + auto reply = command(cmd::georadius, + key, + loc, + radius, + unit, + count, + asc, + WithCoord::type>::value, + WithDist::type>::value, + WithHash::type>::value); + + reply::to_array(*reply, output); +} + +template +void Redis::georadiusbymember(const StringView &key, + const StringView &member, + double radius, + GeoUnit unit, + long long count, + bool asc, + Output output) { + auto reply = command(cmd::georadiusbymember, + key, + member, + radius, + unit, + count, + asc, + WithCoord::type>::value, + WithDist::type>::value, + WithHash::type>::value); + + reply::to_array(*reply, output); +} + +// SCRIPTING commands. + +template +Result Redis::eval(const StringView &script, + std::initializer_list keys, + std::initializer_list args) { + auto reply = command(cmd::eval, script, keys, args); + + return reply::parse(*reply); +} + +template +void Redis::eval(const StringView &script, + std::initializer_list keys, + std::initializer_list args, + Output output) { + auto reply = command(cmd::eval, script, keys, args); + + reply::to_array(*reply, output); +} + +template +Result Redis::evalsha(const StringView &script, + std::initializer_list keys, + std::initializer_list args) { + auto reply = command(cmd::evalsha, script, keys, args); + + return reply::parse(*reply); +} + +template +void Redis::evalsha(const StringView &script, + std::initializer_list keys, + std::initializer_list args, + Output output) { + auto reply = command(cmd::evalsha, script, keys, args); + + reply::to_array(*reply, output); +} + +template +void Redis::script_exists(Input first, Input last, Output output) { + if (first == last) { + throw Error("SCRIPT EXISTS: no key specified"); + } + + auto reply = command(cmd::script_exists_range, first, last); + + reply::to_array(*reply, output); +} + +// Transaction commands. + +template +void Redis::watch(Input first, Input last) { + auto reply = command(cmd::watch_range, first, last); + + reply::parse(*reply); +} + +// Stream commands. + +template +long long Redis::xack(const StringView &key, const StringView &group, Input first, Input last) { + auto reply = command(cmd::xack_range, key, group, first, last); + + return reply::parse(*reply); +} + +template +std::string Redis::xadd(const StringView &key, const StringView &id, Input first, Input last) { + auto reply = command(cmd::xadd_range, key, id, first, last); + + return reply::parse(*reply); +} + +template +std::string Redis::xadd(const StringView &key, + const StringView &id, + Input first, + Input last, + long long count, + bool approx) { + auto reply = command(cmd::xadd_maxlen_range, key, id, first, last, count, approx); + + return reply::parse(*reply); +} + +template +void Redis::xclaim(const StringView &key, + const StringView &group, + const StringView &consumer, + const std::chrono::milliseconds &min_idle_time, + const StringView &id, + Output output) { + auto reply = command(cmd::xclaim, key, group, consumer, min_idle_time.count(), id); + + reply::to_array(*reply, output); +} + +template +void Redis::xclaim(const StringView &key, + const StringView &group, + const StringView &consumer, + const std::chrono::milliseconds &min_idle_time, + Input first, + Input last, + Output output) { + auto reply = command(cmd::xclaim_range, + key, + group, + consumer, + min_idle_time.count(), + first, + last); + + reply::to_array(*reply, output); +} + +template +long long Redis::xdel(const StringView &key, Input first, Input last) { + auto reply = command(cmd::xdel_range, key, first, last); + + return reply::parse(*reply); +} + +template +auto Redis::xpending(const StringView &key, const StringView &group, Output output) + -> std::tuple { + auto reply = command(cmd::xpending, key, group); + + return reply::parse_xpending_reply(*reply, output); +} + +template +void Redis::xpending(const StringView &key, + const StringView &group, + const StringView &start, + const StringView &end, + long long count, + Output output) { + auto reply = command(cmd::xpending_detail, key, group, start, end, count); + + reply::to_array(*reply, output); +} + +template +void Redis::xpending(const StringView &key, + const StringView &group, + const StringView &start, + const StringView &end, + long long count, + const StringView &consumer, + Output output) { + auto reply = command(cmd::xpending_per_consumer, key, group, start, end, count, consumer); + + reply::to_array(*reply, output); +} + +template +void Redis::xrange(const StringView &key, + const StringView &start, + const StringView &end, + Output output) { + auto reply = command(cmd::xrange, key, start, end); + + reply::to_array(*reply, output); +} + +template +void Redis::xrange(const StringView &key, + const StringView &start, + const StringView &end, + long long count, + Output output) { + auto reply = command(cmd::xrange_count, key, start, end, count); + + reply::to_array(*reply, output); +} + +template +void Redis::xread(const StringView &key, + const StringView &id, + long long count, + Output output) { + auto reply = command(cmd::xread, key, id, count); + + if (!reply::is_nil(*reply)) { + reply::to_array(*reply, output); + } +} + +template +auto Redis::xread(Input first, Input last, long long count, Output output) + -> typename std::enable_if::value>::type { + if (first == last) { + throw Error("XREAD: no key specified"); + } + + auto reply = command(cmd::xread_range, first, last, count); + + if (!reply::is_nil(*reply)) { + reply::to_array(*reply, output); + } +} + +template +void Redis::xread(const StringView &key, + const StringView &id, + const std::chrono::milliseconds &timeout, + long long count, + Output output) { + auto reply = command(cmd::xread_block, key, id, timeout.count(), count); + + if (!reply::is_nil(*reply)) { + reply::to_array(*reply, output); + } +} + +template +auto Redis::xread(Input first, + Input last, + const std::chrono::milliseconds &timeout, + long long count, + Output output) + -> typename std::enable_if::value>::type { + if (first == last) { + throw Error("XREAD: no key specified"); + } + + auto reply = command(cmd::xread_block_range, first, last, timeout.count(), count); + + if (!reply::is_nil(*reply)) { + reply::to_array(*reply, output); + } +} + +template +void Redis::xreadgroup(const StringView &group, + const StringView &consumer, + const StringView &key, + const StringView &id, + long long count, + bool noack, + Output output) { + auto reply = command(cmd::xreadgroup, group, consumer, key, id, count, noack); + + if (!reply::is_nil(*reply)) { + reply::to_array(*reply, output); + } +} + +template +auto Redis::xreadgroup(const StringView &group, + const StringView &consumer, + Input first, + Input last, + long long count, + bool noack, + Output output) + -> typename std::enable_if::value>::type { + if (first == last) { + throw Error("XREADGROUP: no key specified"); + } + + auto reply = command(cmd::xreadgroup_range, group, consumer, first, last, count, noack); + + if (!reply::is_nil(*reply)) { + reply::to_array(*reply, output); + } +} + +template +void Redis::xreadgroup(const StringView &group, + const StringView &consumer, + const StringView &key, + const StringView &id, + const std::chrono::milliseconds &timeout, + long long count, + bool noack, + Output output) { + auto reply = command(cmd::xreadgroup_block, + group, + consumer, + key, + id, + timeout.count(), + count, + noack); + + if (!reply::is_nil(*reply)) { + reply::to_array(*reply, output); + } +} + +template +auto Redis::xreadgroup(const StringView &group, + const StringView &consumer, + Input first, + Input last, + const std::chrono::milliseconds &timeout, + long long count, + bool noack, + Output output) + -> typename std::enable_if::value>::type { + if (first == last) { + throw Error("XREADGROUP: no key specified"); + } + + auto reply = command(cmd::xreadgroup_block_range, + group, + consumer, + first, + last, + timeout.count(), + count, + noack); + + if (!reply::is_nil(*reply)) { + reply::to_array(*reply, output); + } +} + +template +void Redis::xrevrange(const StringView &key, + const StringView &end, + const StringView &start, + Output output) { + auto reply = command(cmd::xrevrange, key, end, start); + + reply::to_array(*reply, output); +} + +template +void Redis::xrevrange(const StringView &key, + const StringView &end, + const StringView &start, + long long count, + Output output) { + auto reply = command(cmd::xrevrange_count, key, end, start, count); + + reply::to_array(*reply, output); +} + +template +ReplyUPtr Redis::_command(Connection &connection, Cmd cmd, Args &&...args) { + assert(!connection.broken()); + + cmd(connection, std::forward(args)...); + + auto reply = connection.recv(); + + return reply; +} + +template +inline ReplyUPtr Redis::_score_command(std::true_type, Cmd cmd, Args &&... args) { + return command(cmd, std::forward(args)..., true); +} + +template +inline ReplyUPtr Redis::_score_command(std::false_type, Cmd cmd, Args &&... args) { + return command(cmd, std::forward(args)..., false); +} + +template +inline ReplyUPtr Redis::_score_command(Cmd cmd, Args &&... args) { + return _score_command(typename IsKvPairIter::type(), + cmd, + std::forward(args)...); +} + +} + +} + +#endif // end SEWENEW_REDISPLUSPLUS_REDIS_HPP diff --git a/controller/thirdparty/redis-plus-plus-1.1.1/src/sw/redis++/redis_cluster.cpp b/controller/thirdparty/redis-plus-plus-1.1.1/src/sw/redis++/redis_cluster.cpp new file mode 100644 index 000000000..6950f5730 --- /dev/null +++ b/controller/thirdparty/redis-plus-plus-1.1.1/src/sw/redis++/redis_cluster.cpp @@ -0,0 +1,769 @@ +/************************************************************************** + Copyright (c) 2017 sewenew + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. + *************************************************************************/ + +#include "redis_cluster.h" +#include +#include "command.h" +#include "errors.h" +#include "queued_redis.h" + +namespace sw { + +namespace redis { + +RedisCluster::RedisCluster(const std::string &uri) : RedisCluster(ConnectionOptions(uri)) {} + +Redis RedisCluster::redis(const StringView &hash_tag) { + auto opts = _pool.connection_options(hash_tag); + return Redis(std::make_shared(opts)); +} + +Pipeline RedisCluster::pipeline(const StringView &hash_tag) { + auto opts = _pool.connection_options(hash_tag); + return Pipeline(std::make_shared(opts)); +} + +Transaction RedisCluster::transaction(const StringView &hash_tag, bool piped) { + auto opts = _pool.connection_options(hash_tag); + return Transaction(std::make_shared(opts), piped); +} + +Subscriber RedisCluster::subscriber() { + auto opts = _pool.connection_options(); + return Subscriber(Connection(opts)); +} + +// KEY commands. + +long long RedisCluster::del(const StringView &key) { + auto reply = command(cmd::del, key); + + return reply::parse(*reply); +} + +OptionalString RedisCluster::dump(const StringView &key) { + auto reply = command(cmd::dump, key); + + return reply::parse(*reply); +} + +long long RedisCluster::exists(const StringView &key) { + auto reply = command(cmd::exists, key); + + return reply::parse(*reply); +} + +bool RedisCluster::expire(const StringView &key, long long timeout) { + auto reply = command(cmd::expire, key, timeout); + + return reply::parse(*reply); +} + +bool RedisCluster::expireat(const StringView &key, long long timestamp) { + auto reply = command(cmd::expireat, key, timestamp); + + return reply::parse(*reply); +} + +bool RedisCluster::persist(const StringView &key) { + auto reply = command(cmd::persist, key); + + return reply::parse(*reply); +} + +bool RedisCluster::pexpire(const StringView &key, long long timeout) { + auto reply = command(cmd::pexpire, key, timeout); + + return reply::parse(*reply); +} + +bool RedisCluster::pexpireat(const StringView &key, long long timestamp) { + auto reply = command(cmd::pexpireat, key, timestamp); + + return reply::parse(*reply); +} + +long long RedisCluster::pttl(const StringView &key) { + auto reply = command(cmd::pttl, key); + + return reply::parse(*reply); +} + +void RedisCluster::rename(const StringView &key, const StringView &newkey) { + auto reply = command(cmd::rename, key, newkey); + + reply::parse(*reply); +} + +bool RedisCluster::renamenx(const StringView &key, const StringView &newkey) { + auto reply = command(cmd::renamenx, key, newkey); + + return reply::parse(*reply); +} + +void RedisCluster::restore(const StringView &key, + const StringView &val, + long long ttl, + bool replace) { + auto reply = command(cmd::restore, key, val, ttl, replace); + + reply::parse(*reply); +} + +long long RedisCluster::touch(const StringView &key) { + auto reply = command(cmd::touch, key); + + return reply::parse(*reply); +} + +long long RedisCluster::ttl(const StringView &key) { + auto reply = command(cmd::ttl, key); + + return reply::parse(*reply); +} + +std::string RedisCluster::type(const StringView &key) { + auto reply = command(cmd::type, key); + + return reply::parse(*reply); +} + +long long RedisCluster::unlink(const StringView &key) { + auto reply = command(cmd::unlink, key); + + return reply::parse(*reply); +} + +// STRING commands. + +long long RedisCluster::append(const StringView &key, const StringView &val) { + auto reply = command(cmd::append, key, val); + + return reply::parse(*reply); +} + +long long RedisCluster::bitcount(const StringView &key, long long start, long long end) { + auto reply = command(cmd::bitcount, key, start, end); + + return reply::parse(*reply); +} + +long long RedisCluster::bitop(BitOp op, const StringView &destination, const StringView &key) { + auto reply = _command(cmd::bitop, destination, op, destination, key); + + return reply::parse(*reply); +} + +long long RedisCluster::bitpos(const StringView &key, + long long bit, + long long start, + long long end) { + auto reply = command(cmd::bitpos, key, bit, start, end); + + return reply::parse(*reply); +} + +long long RedisCluster::decr(const StringView &key) { + auto reply = command(cmd::decr, key); + + return reply::parse(*reply); +} + +long long RedisCluster::decrby(const StringView &key, long long decrement) { + auto reply = command(cmd::decrby, key, decrement); + + return reply::parse(*reply); +} + +OptionalString RedisCluster::get(const StringView &key) { + auto reply = command(cmd::get, key); + + return reply::parse(*reply); +} + +long long RedisCluster::getbit(const StringView &key, long long offset) { + auto reply = command(cmd::getbit, key, offset); + + return reply::parse(*reply); +} + +std::string RedisCluster::getrange(const StringView &key, long long start, long long end) { + auto reply = command(cmd::getrange, key, start, end); + + return reply::parse(*reply); +} + +OptionalString RedisCluster::getset(const StringView &key, const StringView &val) { + auto reply = command(cmd::getset, key, val); + + return reply::parse(*reply); +} + +long long RedisCluster::incr(const StringView &key) { + auto reply = command(cmd::incr, key); + + return reply::parse(*reply); +} + +long long RedisCluster::incrby(const StringView &key, long long increment) { + auto reply = command(cmd::incrby, key, increment); + + return reply::parse(*reply); +} + +double RedisCluster::incrbyfloat(const StringView &key, double increment) { + auto reply = command(cmd::incrbyfloat, key, increment); + + return reply::parse(*reply); +} + +void RedisCluster::psetex(const StringView &key, + long long ttl, + const StringView &val) { + auto reply = command(cmd::psetex, key, ttl, val); + + reply::parse(*reply); +} + +bool RedisCluster::set(const StringView &key, + const StringView &val, + const std::chrono::milliseconds &ttl, + UpdateType type) { + auto reply = command(cmd::set, key, val, ttl.count(), type); + + reply::rewrite_set_reply(*reply); + + return reply::parse(*reply); +} + +void RedisCluster::setex(const StringView &key, + long long ttl, + const StringView &val) { + auto reply = command(cmd::setex, key, ttl, val); + + reply::parse(*reply); +} + +bool RedisCluster::setnx(const StringView &key, const StringView &val) { + auto reply = command(cmd::setnx, key, val); + + return reply::parse(*reply); +} + +long long RedisCluster::setrange(const StringView &key, long long offset, const StringView &val) { + auto reply = command(cmd::setrange, key, offset, val); + + return reply::parse(*reply); +} + +long long RedisCluster::strlen(const StringView &key) { + auto reply = command(cmd::strlen, key); + + return reply::parse(*reply); +} + +// LIST commands. + +OptionalStringPair RedisCluster::blpop(const StringView &key, long long timeout) { + auto reply = command(cmd::blpop, key, timeout); + + return reply::parse(*reply); +} + +OptionalStringPair RedisCluster::blpop(const StringView &key, const std::chrono::seconds &timeout) { + return blpop(key, timeout.count()); +} + +OptionalStringPair RedisCluster::brpop(const StringView &key, long long timeout) { + auto reply = command(cmd::brpop, key, timeout); + + return reply::parse(*reply); +} + +OptionalStringPair RedisCluster::brpop(const StringView &key, const std::chrono::seconds &timeout) { + return brpop(key, timeout.count()); +} + +OptionalString RedisCluster::brpoplpush(const StringView &source, + const StringView &destination, + long long timeout) { + auto reply = command(cmd::brpoplpush, source, destination, timeout); + + return reply::parse(*reply); +} + +OptionalString RedisCluster::lindex(const StringView &key, long long index) { + auto reply = command(cmd::lindex, key, index); + + return reply::parse(*reply); +} + +long long RedisCluster::linsert(const StringView &key, + InsertPosition position, + const StringView &pivot, + const StringView &val) { + auto reply = command(cmd::linsert, key, position, pivot, val); + + return reply::parse(*reply); +} + +long long RedisCluster::llen(const StringView &key) { + auto reply = command(cmd::llen, key); + + return reply::parse(*reply); +} + +OptionalString RedisCluster::lpop(const StringView &key) { + auto reply = command(cmd::lpop, key); + + return reply::parse(*reply); +} + +long long RedisCluster::lpush(const StringView &key, const StringView &val) { + auto reply = command(cmd::lpush, key, val); + + return reply::parse(*reply); +} + +long long RedisCluster::lpushx(const StringView &key, const StringView &val) { + auto reply = command(cmd::lpushx, key, val); + + return reply::parse(*reply); +} + +long long RedisCluster::lrem(const StringView &key, long long count, const StringView &val) { + auto reply = command(cmd::lrem, key, count, val); + + return reply::parse(*reply); +} + +void RedisCluster::lset(const StringView &key, long long index, const StringView &val) { + auto reply = command(cmd::lset, key, index, val); + + reply::parse(*reply); +} + +void RedisCluster::ltrim(const StringView &key, long long start, long long stop) { + auto reply = command(cmd::ltrim, key, start, stop); + + reply::parse(*reply); +} + +OptionalString RedisCluster::rpop(const StringView &key) { + auto reply = command(cmd::rpop, key); + + return reply::parse(*reply); +} + +OptionalString RedisCluster::rpoplpush(const StringView &source, const StringView &destination) { + auto reply = command(cmd::rpoplpush, source, destination); + + return reply::parse(*reply); +} + +long long RedisCluster::rpush(const StringView &key, const StringView &val) { + auto reply = command(cmd::rpush, key, val); + + return reply::parse(*reply); +} + +long long RedisCluster::rpushx(const StringView &key, const StringView &val) { + auto reply = command(cmd::rpushx, key, val); + + return reply::parse(*reply); +} + +long long RedisCluster::hdel(const StringView &key, const StringView &field) { + auto reply = command(cmd::hdel, key, field); + + return reply::parse(*reply); +} + +bool RedisCluster::hexists(const StringView &key, const StringView &field) { + auto reply = command(cmd::hexists, key, field); + + return reply::parse(*reply); +} + +OptionalString RedisCluster::hget(const StringView &key, const StringView &field) { + auto reply = command(cmd::hget, key, field); + + return reply::parse(*reply); +} + +long long RedisCluster::hincrby(const StringView &key, const StringView &field, long long increment) { + auto reply = command(cmd::hincrby, key, field, increment); + + return reply::parse(*reply); +} + +double RedisCluster::hincrbyfloat(const StringView &key, const StringView &field, double increment) { + auto reply = command(cmd::hincrbyfloat, key, field, increment); + + return reply::parse(*reply); +} + +long long RedisCluster::hlen(const StringView &key) { + auto reply = command(cmd::hlen, key); + + return reply::parse(*reply); +} + +bool RedisCluster::hset(const StringView &key, const StringView &field, const StringView &val) { + auto reply = command(cmd::hset, key, field, val); + + return reply::parse(*reply); +} + +bool RedisCluster::hset(const StringView &key, const std::pair &item) { + return hset(key, item.first, item.second); +} + +bool RedisCluster::hsetnx(const StringView &key, const StringView &field, const StringView &val) { + auto reply = command(cmd::hsetnx, key, field, val); + + return reply::parse(*reply); +} + +bool RedisCluster::hsetnx(const StringView &key, const std::pair &item) { + return hsetnx(key, item.first, item.second); +} + +long long RedisCluster::hstrlen(const StringView &key, const StringView &field) { + auto reply = command(cmd::hstrlen, key, field); + + return reply::parse(*reply); +} + +// SET commands. + +long long RedisCluster::sadd(const StringView &key, const StringView &member) { + auto reply = command(cmd::sadd, key, member); + + return reply::parse(*reply); +} + +long long RedisCluster::scard(const StringView &key) { + auto reply = command(cmd::scard, key); + + return reply::parse(*reply); +} + +long long RedisCluster::sdiffstore(const StringView &destination, const StringView &key) { + auto reply = command(cmd::sdiffstore, destination, key); + + return reply::parse(*reply); +} + +long long RedisCluster::sinterstore(const StringView &destination, const StringView &key) { + auto reply = command(cmd::sinterstore, destination, key); + + return reply::parse(*reply); +} + +bool RedisCluster::sismember(const StringView &key, const StringView &member) { + auto reply = command(cmd::sismember, key, member); + + return reply::parse(*reply); +} + +bool RedisCluster::smove(const StringView &source, + const StringView &destination, + const StringView &member) { + auto reply = command(cmd::smove, source, destination, member); + + return reply::parse(*reply); +} + +OptionalString RedisCluster::spop(const StringView &key) { + auto reply = command(cmd::spop, key); + + return reply::parse(*reply); +} + +OptionalString RedisCluster::srandmember(const StringView &key) { + auto reply = command(cmd::srandmember, key); + + return reply::parse(*reply); +} + +long long RedisCluster::srem(const StringView &key, const StringView &member) { + auto reply = command(cmd::srem, key, member); + + return reply::parse(*reply); +} + +long long RedisCluster::sunionstore(const StringView &destination, const StringView &key) { + auto reply = command(cmd::sunionstore, destination, key); + + return reply::parse(*reply); +} + +// SORTED SET commands. + +auto RedisCluster::bzpopmax(const StringView &key, long long timeout) + -> Optional> { + auto reply = command(cmd::bzpopmax, key, timeout); + + return reply::parse>>(*reply); +} + +auto RedisCluster::bzpopmin(const StringView &key, long long timeout) + -> Optional> { + auto reply = command(cmd::bzpopmin, key, timeout); + + return reply::parse>>(*reply); +} + +long long RedisCluster::zadd(const StringView &key, + const StringView &member, + double score, + UpdateType type, + bool changed) { + auto reply = command(cmd::zadd, key, member, score, type, changed); + + return reply::parse(*reply); +} + +long long RedisCluster::zcard(const StringView &key) { + auto reply = command(cmd::zcard, key); + + return reply::parse(*reply); +} + +double RedisCluster::zincrby(const StringView &key, double increment, const StringView &member) { + auto reply = command(cmd::zincrby, key, increment, member); + + return reply::parse(*reply); +} + +long long RedisCluster::zinterstore(const StringView &destination, + const StringView &key, + double weight) { + auto reply = command(cmd::zinterstore, destination, key, weight); + + return reply::parse(*reply); +} + +Optional> RedisCluster::zpopmax(const StringView &key) { + auto reply = command(cmd::zpopmax, key, 1); + + return reply::parse>>(*reply); +} + +Optional> RedisCluster::zpopmin(const StringView &key) { + auto reply = command(cmd::zpopmin, key, 1); + + return reply::parse>>(*reply); +} + +OptionalLongLong RedisCluster::zrank(const StringView &key, const StringView &member) { + auto reply = command(cmd::zrank, key, member); + + return reply::parse(*reply); +} + +long long RedisCluster::zrem(const StringView &key, const StringView &member) { + auto reply = command(cmd::zrem, key, member); + + return reply::parse(*reply); +} + +long long RedisCluster::zremrangebyrank(const StringView &key, long long start, long long stop) { + auto reply = command(cmd::zremrangebyrank, key, start, stop); + + return reply::parse(*reply); +} + +OptionalLongLong RedisCluster::zrevrank(const StringView &key, const StringView &member) { + auto reply = command(cmd::zrevrank, key, member); + + return reply::parse(*reply); +} + +OptionalDouble RedisCluster::zscore(const StringView &key, const StringView &member) { + auto reply = command(cmd::zscore, key, member); + + return reply::parse(*reply); +} + +long long RedisCluster::zunionstore(const StringView &destination, + const StringView &key, + double weight) { + auto reply = command(cmd::zunionstore, destination, key, weight); + + return reply::parse(*reply); +} + +// HYPERLOGLOG commands. + +bool RedisCluster::pfadd(const StringView &key, const StringView &element) { + auto reply = command(cmd::pfadd, key, element); + + return reply::parse(*reply); +} + +long long RedisCluster::pfcount(const StringView &key) { + auto reply = command(cmd::pfcount, key); + + return reply::parse(*reply); +} + +void RedisCluster::pfmerge(const StringView &destination, const StringView &key) { + auto reply = command(cmd::pfmerge, destination, key); + + reply::parse(*reply); +} + +// GEO commands. + +long long RedisCluster::geoadd(const StringView &key, + const std::tuple &member) { + auto reply = command(cmd::geoadd, key, member); + + return reply::parse(*reply); +} + +OptionalDouble RedisCluster::geodist(const StringView &key, + const StringView &member1, + const StringView &member2, + GeoUnit unit) { + auto reply = command(cmd::geodist, key, member1, member2, unit); + + return reply::parse(*reply); +} + +OptionalLongLong RedisCluster::georadius(const StringView &key, + const std::pair &loc, + double radius, + GeoUnit unit, + const StringView &destination, + bool store_dist, + long long count) { + auto reply = command(cmd::georadius_store, + key, + loc, + radius, + unit, + destination, + store_dist, + count); + + reply::rewrite_georadius_reply(*reply); + + return reply::parse(*reply); +} + +OptionalLongLong RedisCluster::georadiusbymember(const StringView &key, + const StringView &member, + double radius, + GeoUnit unit, + const StringView &destination, + bool store_dist, + long long count) { + auto reply = command(cmd::georadiusbymember_store, + key, + member, + radius, + unit, + destination, + store_dist, + count); + + reply::rewrite_georadius_reply(*reply); + + return reply::parse(*reply); +} + +// PUBSUB commands. + +long long RedisCluster::publish(const StringView &channel, const StringView &message) { + auto reply = command(cmd::publish, channel, message); + + return reply::parse(*reply); +} + +// Stream commands. + +long long RedisCluster::xack(const StringView &key, const StringView &group, const StringView &id) { + auto reply = command(cmd::xack, key, group, id); + + return reply::parse(*reply); +} + +long long RedisCluster::xdel(const StringView &key, const StringView &id) { + auto reply = command(cmd::xdel, key, id); + + return reply::parse(*reply); +} + +void RedisCluster::xgroup_create(const StringView &key, + const StringView &group, + const StringView &id, + bool mkstream) { + auto reply = command(cmd::xgroup_create, key, group, id, mkstream); + + reply::parse(*reply); +} + +void RedisCluster::xgroup_setid(const StringView &key, + const StringView &group, + const StringView &id) { + auto reply = command(cmd::xgroup_setid, key, group, id); + + reply::parse(*reply); +} + +long long RedisCluster::xgroup_destroy(const StringView &key, const StringView &group) { + auto reply = command(cmd::xgroup_destroy, key, group); + + return reply::parse(*reply); +} + +long long RedisCluster::xgroup_delconsumer(const StringView &key, + const StringView &group, + const StringView &consumer) { + auto reply = command(cmd::xgroup_delconsumer, key, group, consumer); + + return reply::parse(*reply); +} + +long long RedisCluster::xlen(const StringView &key) { + auto reply = command(cmd::xlen, key); + + return reply::parse(*reply); +} + +long long RedisCluster::xtrim(const StringView &key, long long count, bool approx) { + auto reply = command(cmd::xtrim, key, count, approx); + + return reply::parse(*reply); +} + +void RedisCluster::_asking(Connection &connection) { + // Send ASKING command. + connection.send("ASKING"); + + auto reply = connection.recv(); + + assert(reply); + + reply::parse(*reply); +} + +} + +} diff --git a/controller/thirdparty/redis-plus-plus-1.1.1/src/sw/redis++/redis_cluster.h b/controller/thirdparty/redis-plus-plus-1.1.1/src/sw/redis++/redis_cluster.h new file mode 100644 index 000000000..50a221367 --- /dev/null +++ b/controller/thirdparty/redis-plus-plus-1.1.1/src/sw/redis++/redis_cluster.h @@ -0,0 +1,1395 @@ +/************************************************************************** + Copyright (c) 2017 sewenew + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. + *************************************************************************/ + +#ifndef SEWENEW_REDISPLUSPLUS_REDIS_CLUSTER_H +#define SEWENEW_REDISPLUSPLUS_REDIS_CLUSTER_H + +#include +#include +#include +#include +#include "shards_pool.h" +#include "reply.h" +#include "command_options.h" +#include "utils.h" +#include "subscriber.h" +#include "pipeline.h" +#include "transaction.h" +#include "redis.h" + +namespace sw { + +namespace redis { + +template +class QueuedRedis; + +using Transaction = QueuedRedis; + +using Pipeline = QueuedRedis; + +class RedisCluster { +public: + RedisCluster(const ConnectionOptions &connection_opts, + const ConnectionPoolOptions &pool_opts = {}) : + _pool(pool_opts, connection_opts) {} + + // Construct RedisCluster with URI: + // "tcp://127.0.0.1" or "tcp://127.0.0.1:6379" + // Only need to specify one URI. + explicit RedisCluster(const std::string &uri); + + RedisCluster(const RedisCluster &) = delete; + RedisCluster& operator=(const RedisCluster &) = delete; + + RedisCluster(RedisCluster &&) = default; + RedisCluster& operator=(RedisCluster &&) = default; + + Redis redis(const StringView &hash_tag); + + Pipeline pipeline(const StringView &hash_tag); + + Transaction transaction(const StringView &hash_tag, bool piped = false); + + Subscriber subscriber(); + + template + auto command(Cmd cmd, Key &&key, Args &&...args) + -> typename std::enable_if::value, ReplyUPtr>::type; + + template + auto command(const StringView &cmd_name, Key &&key, Args &&...args) + -> typename std::enable_if<(std::is_convertible::value + || std::is_arithmetic::type>::value) + && !IsIter::type>::value, ReplyUPtr>::type; + + template + auto command(const StringView &cmd_name, Key &&key, Args &&...args) + -> typename std::enable_if<(std::is_convertible::value + || std::is_arithmetic::type>::value) + && IsIter::type>::value, void>::type; + + template + auto command(const StringView &cmd_name, Key &&key, Args &&...args) + -> typename std::enable_if::value + || std::is_arithmetic::type>::value, Result>::type; + + template + auto command(Input first, Input last) + -> typename std::enable_if::value, ReplyUPtr>::type; + + template + auto command(Input first, Input last) + -> typename std::enable_if::value, Result>::type; + + template + auto command(Input first, Input last, Output output) + -> typename std::enable_if::value, void>::type; + + // KEY commands. + + long long del(const StringView &key); + + template + long long del(Input first, Input last); + + template + long long del(std::initializer_list il) { + return del(il.begin(), il.end()); + } + + OptionalString dump(const StringView &key); + + long long exists(const StringView &key); + + template + long long exists(Input first, Input last); + + template + long long exists(std::initializer_list il) { + return exists(il.begin(), il.end()); + } + + bool expire(const StringView &key, long long timeout); + + bool expire(const StringView &key, const std::chrono::seconds &timeout); + + bool expireat(const StringView &key, long long timestamp); + + bool expireat(const StringView &key, + const std::chrono::time_point &tp); + + bool persist(const StringView &key); + + bool pexpire(const StringView &key, long long timeout); + + bool pexpire(const StringView &key, const std::chrono::milliseconds &timeout); + + bool pexpireat(const StringView &key, long long timestamp); + + bool pexpireat(const StringView &key, + const std::chrono::time_point &tp); + + long long pttl(const StringView &key); + + void rename(const StringView &key, const StringView &newkey); + + bool renamenx(const StringView &key, const StringView &newkey); + + void restore(const StringView &key, + const StringView &val, + long long ttl, + bool replace = false); + + void restore(const StringView &key, + const StringView &val, + const std::chrono::milliseconds &ttl = std::chrono::milliseconds{0}, + bool replace = false); + + // TODO: sort + + long long touch(const StringView &key); + + template + long long touch(Input first, Input last); + + template + long long touch(std::initializer_list il) { + return touch(il.begin(), il.end()); + } + + long long ttl(const StringView &key); + + std::string type(const StringView &key); + + long long unlink(const StringView &key); + + template + long long unlink(Input first, Input last); + + template + long long unlink(std::initializer_list il) { + return unlink(il.begin(), il.end()); + } + + // STRING commands. + + long long append(const StringView &key, const StringView &str); + + long long bitcount(const StringView &key, long long start = 0, long long end = -1); + + long long bitop(BitOp op, const StringView &destination, const StringView &key); + + template + long long bitop(BitOp op, const StringView &destination, Input first, Input last); + + template + long long bitop(BitOp op, const StringView &destination, std::initializer_list il) { + return bitop(op, destination, il.begin(), il.end()); + } + + long long bitpos(const StringView &key, + long long bit, + long long start = 0, + long long end = -1); + + long long decr(const StringView &key); + + long long decrby(const StringView &key, long long decrement); + + OptionalString get(const StringView &key); + + long long getbit(const StringView &key, long long offset); + + std::string getrange(const StringView &key, long long start, long long end); + + OptionalString getset(const StringView &key, const StringView &val); + + long long incr(const StringView &key); + + long long incrby(const StringView &key, long long increment); + + double incrbyfloat(const StringView &key, double increment); + + template + void mget(Input first, Input last, Output output); + + template + void mget(std::initializer_list il, Output output) { + mget(il.begin(), il.end(), output); + } + + template + void mset(Input first, Input last); + + template + void mset(std::initializer_list il) { + mset(il.begin(), il.end()); + } + + template + bool msetnx(Input first, Input last); + + template + bool msetnx(std::initializer_list il) { + return msetnx(il.begin(), il.end()); + } + + void psetex(const StringView &key, + long long ttl, + const StringView &val); + + void psetex(const StringView &key, + const std::chrono::milliseconds &ttl, + const StringView &val); + + bool set(const StringView &key, + const StringView &val, + const std::chrono::milliseconds &ttl = std::chrono::milliseconds(0), + UpdateType type = UpdateType::ALWAYS); + + void setex(const StringView &key, + long long ttl, + const StringView &val); + + void setex(const StringView &key, + const std::chrono::seconds &ttl, + const StringView &val); + + bool setnx(const StringView &key, const StringView &val); + + long long setrange(const StringView &key, long long offset, const StringView &val); + + long long strlen(const StringView &key); + + // LIST commands. + + OptionalStringPair blpop(const StringView &key, long long timeout); + + OptionalStringPair blpop(const StringView &key, + const std::chrono::seconds &timeout = std::chrono::seconds{0}); + + template + OptionalStringPair blpop(Input first, Input last, long long timeout); + + template + OptionalStringPair blpop(std::initializer_list il, long long timeout) { + return blpop(il.begin(), il.end(), timeout); + } + + template + OptionalStringPair blpop(Input first, + Input last, + const std::chrono::seconds &timeout = std::chrono::seconds{0}); + + template + OptionalStringPair blpop(std::initializer_list il, + const std::chrono::seconds &timeout = std::chrono::seconds{0}) { + return blpop(il.begin(), il.end(), timeout); + } + + OptionalStringPair brpop(const StringView &key, long long timeout); + + OptionalStringPair brpop(const StringView &key, + const std::chrono::seconds &timeout = std::chrono::seconds{0}); + + template + OptionalStringPair brpop(Input first, Input last, long long timeout); + + template + OptionalStringPair brpop(std::initializer_list il, long long timeout) { + return brpop(il.begin(), il.end(), timeout); + } + + template + OptionalStringPair brpop(Input first, + Input last, + const std::chrono::seconds &timeout = std::chrono::seconds{0}); + + template + OptionalStringPair brpop(std::initializer_list il, + const std::chrono::seconds &timeout = std::chrono::seconds{0}) { + return brpop(il.begin(), il.end(), timeout); + } + + OptionalString brpoplpush(const StringView &source, + const StringView &destination, + long long timeout); + + OptionalString brpoplpush(const StringView &source, + const StringView &destination, + const std::chrono::seconds &timeout = std::chrono::seconds{0}); + + OptionalString lindex(const StringView &key, long long index); + + long long linsert(const StringView &key, + InsertPosition position, + const StringView &pivot, + const StringView &val); + + long long llen(const StringView &key); + + OptionalString lpop(const StringView &key); + + long long lpush(const StringView &key, const StringView &val); + + template + long long lpush(const StringView &key, Input first, Input last); + + template + long long lpush(const StringView &key, std::initializer_list il) { + return lpush(key, il.begin(), il.end()); + } + + long long lpushx(const StringView &key, const StringView &val); + + template + void lrange(const StringView &key, long long start, long long stop, Output output); + + long long lrem(const StringView &key, long long count, const StringView &val); + + void lset(const StringView &key, long long index, const StringView &val); + + void ltrim(const StringView &key, long long start, long long stop); + + OptionalString rpop(const StringView &key); + + OptionalString rpoplpush(const StringView &source, const StringView &destination); + + long long rpush(const StringView &key, const StringView &val); + + template + long long rpush(const StringView &key, Input first, Input last); + + template + long long rpush(const StringView &key, std::initializer_list il) { + return rpush(key, il.begin(), il.end()); + } + + long long rpushx(const StringView &key, const StringView &val); + + // HASH commands. + + long long hdel(const StringView &key, const StringView &field); + + template + long long hdel(const StringView &key, Input first, Input last); + + template + long long hdel(const StringView &key, std::initializer_list il) { + return hdel(key, il.begin(), il.end()); + } + + bool hexists(const StringView &key, const StringView &field); + + OptionalString hget(const StringView &key, const StringView &field); + + template + void hgetall(const StringView &key, Output output); + + long long hincrby(const StringView &key, const StringView &field, long long increment); + + double hincrbyfloat(const StringView &key, const StringView &field, double increment); + + template + void hkeys(const StringView &key, Output output); + + long long hlen(const StringView &key); + + template + void hmget(const StringView &key, Input first, Input last, Output output); + + template + void hmget(const StringView &key, std::initializer_list il, Output output) { + hmget(key, il.begin(), il.end(), output); + } + + template + void hmset(const StringView &key, Input first, Input last); + + template + void hmset(const StringView &key, std::initializer_list il) { + hmset(key, il.begin(), il.end()); + } + + template + long long hscan(const StringView &key, + long long cursor, + const StringView &pattern, + long long count, + Output output); + + template + long long hscan(const StringView &key, + long long cursor, + const StringView &pattern, + Output output); + + template + long long hscan(const StringView &key, + long long cursor, + long long count, + Output output); + + template + long long hscan(const StringView &key, + long long cursor, + Output output); + + bool hset(const StringView &key, const StringView &field, const StringView &val); + + bool hset(const StringView &key, const std::pair &item); + + bool hsetnx(const StringView &key, const StringView &field, const StringView &val); + + bool hsetnx(const StringView &key, const std::pair &item); + + long long hstrlen(const StringView &key, const StringView &field); + + template + void hvals(const StringView &key, Output output); + + // SET commands. + + long long sadd(const StringView &key, const StringView &member); + + template + long long sadd(const StringView &key, Input first, Input last); + + template + long long sadd(const StringView &key, std::initializer_list il) { + return sadd(key, il.begin(), il.end()); + } + + long long scard(const StringView &key); + + template + void sdiff(Input first, Input last, Output output); + + template + void sdiff(std::initializer_list il, Output output) { + sdiff(il.begin(), il.end(), output); + } + + long long sdiffstore(const StringView &destination, const StringView &key); + + template + long long sdiffstore(const StringView &destination, + Input first, + Input last); + + template + long long sdiffstore(const StringView &destination, + std::initializer_list il) { + return sdiffstore(destination, il.begin(), il.end()); + } + + template + void sinter(Input first, Input last, Output output); + + template + void sinter(std::initializer_list il, Output output) { + sinter(il.begin(), il.end(), output); + } + + long long sinterstore(const StringView &destination, const StringView &key); + + template + long long sinterstore(const StringView &destination, + Input first, + Input last); + + template + long long sinterstore(const StringView &destination, + std::initializer_list il) { + return sinterstore(destination, il.begin(), il.end()); + } + + bool sismember(const StringView &key, const StringView &member); + + template + void smembers(const StringView &key, Output output); + + bool smove(const StringView &source, + const StringView &destination, + const StringView &member); + + OptionalString spop(const StringView &key); + + template + void spop(const StringView &key, long long count, Output output); + + OptionalString srandmember(const StringView &key); + + template + void srandmember(const StringView &key, long long count, Output output); + + long long srem(const StringView &key, const StringView &member); + + template + long long srem(const StringView &key, Input first, Input last); + + template + long long srem(const StringView &key, std::initializer_list il) { + return srem(key, il.begin(), il.end()); + } + + template + long long sscan(const StringView &key, + long long cursor, + const StringView &pattern, + long long count, + Output output); + + template + long long sscan(const StringView &key, + long long cursor, + const StringView &pattern, + Output output); + + template + long long sscan(const StringView &key, + long long cursor, + long long count, + Output output); + + template + long long sscan(const StringView &key, + long long cursor, + Output output); + + template + void sunion(Input first, Input last, Output output); + + template + void sunion(std::initializer_list il, Output output) { + sunion(il.begin(), il.end(), output); + } + + long long sunionstore(const StringView &destination, const StringView &key); + + template + long long sunionstore(const StringView &destination, Input first, Input last); + + template + long long sunionstore(const StringView &destination, std::initializer_list il) { + return sunionstore(destination, il.begin(), il.end()); + } + + // SORTED SET commands. + + auto bzpopmax(const StringView &key, long long timeout) + -> Optional>; + + auto bzpopmax(const StringView &key, + const std::chrono::seconds &timeout = std::chrono::seconds{0}) + -> Optional>; + + template + auto bzpopmax(Input first, Input last, long long timeout) + -> Optional>; + + template + auto bzpopmax(Input first, + Input last, + const std::chrono::seconds &timeout = std::chrono::seconds{0}) + -> Optional>; + + template + auto bzpopmax(std::initializer_list il, long long timeout) + -> Optional> { + return bzpopmax(il.begin(), il.end(), timeout); + } + + template + auto bzpopmax(std::initializer_list il, + const std::chrono::seconds &timeout = std::chrono::seconds{0}) + -> Optional> { + return bzpopmax(il.begin(), il.end(), timeout); + } + + auto bzpopmin(const StringView &key, long long timeout) + -> Optional>; + + auto bzpopmin(const StringView &key, + const std::chrono::seconds &timeout = std::chrono::seconds{0}) + -> Optional>; + + template + auto bzpopmin(Input first, Input last, long long timeout) + -> Optional>; + + template + auto bzpopmin(Input first, + Input last, + const std::chrono::seconds &timeout = std::chrono::seconds{0}) + -> Optional>; + + template + auto bzpopmin(std::initializer_list il, long long timeout) + -> Optional> { + return bzpopmin(il.begin(), il.end(), timeout); + } + + template + auto bzpopmin(std::initializer_list il, + const std::chrono::seconds &timeout = std::chrono::seconds{0}) + -> Optional> { + return bzpopmin(il.begin(), il.end(), timeout); + } + + // We don't support the INCR option, since you can always use ZINCRBY instead. + long long zadd(const StringView &key, + const StringView &member, + double score, + UpdateType type = UpdateType::ALWAYS, + bool changed = false); + + template + long long zadd(const StringView &key, + Input first, + Input last, + UpdateType type = UpdateType::ALWAYS, + bool changed = false); + + template + long long zadd(const StringView &key, + std::initializer_list il, + UpdateType type = UpdateType::ALWAYS, + bool changed = false) { + return zadd(key, il.begin(), il.end(), type, changed); + } + + long long zcard(const StringView &key); + + template + long long zcount(const StringView &key, const Interval &interval); + + double zincrby(const StringView &key, double increment, const StringView &member); + + long long zinterstore(const StringView &destination, const StringView &key, double weight); + + template + long long zinterstore(const StringView &destination, + Input first, + Input last, + Aggregation type = Aggregation::SUM); + + template + long long zinterstore(const StringView &destination, + std::initializer_list il, + Aggregation type = Aggregation::SUM) { + return zinterstore(destination, il.begin(), il.end(), type); + } + + template + long long zlexcount(const StringView &key, const Interval &interval); + + Optional> zpopmax(const StringView &key); + + template + void zpopmax(const StringView &key, long long count, Output output); + + Optional> zpopmin(const StringView &key); + + template + void zpopmin(const StringView &key, long long count, Output output); + + // If *output* is an iterator of a container of string, + // we send *ZRANGE key start stop* command. + // If it's an iterator of a container of pair, + // we send *ZRANGE key start stop WITHSCORES* command. + // + // The following code sends *ZRANGE* without the *WITHSCORES* option: + // + // vector result; + // redis.zrange("key", 0, -1, back_inserter(result)); + // + // On the other hand, the following code sends command with *WITHSCORES* option: + // + // unordered_map with_score; + // redis.zrange("key", 0, -1, inserter(with_score, with_score.end())); + // + // This also applies to other commands with the *WITHSCORES* option, + // e.g. *ZRANGEBYSCORE*, *ZREVRANGE*, *ZREVRANGEBYSCORE*. + template + void zrange(const StringView &key, long long start, long long stop, Output output); + + template + void zrangebylex(const StringView &key, const Interval &interval, Output output); + + template + void zrangebylex(const StringView &key, + const Interval &interval, + const LimitOptions &opts, + Output output); + + // See *zrange* comment on how to send command with *WITHSCORES* option. + template + void zrangebyscore(const StringView &key, const Interval &interval, Output output); + + // See *zrange* comment on how to send command with *WITHSCORES* option. + template + void zrangebyscore(const StringView &key, + const Interval &interval, + const LimitOptions &opts, + Output output); + + OptionalLongLong zrank(const StringView &key, const StringView &member); + + long long zrem(const StringView &key, const StringView &member); + + template + long long zrem(const StringView &key, Input first, Input last); + + template + long long zrem(const StringView &key, std::initializer_list il) { + return zrem(key, il.begin(), il.end()); + } + + template + long long zremrangebylex(const StringView &key, const Interval &interval); + + long long zremrangebyrank(const StringView &key, long long start, long long stop); + + template + long long zremrangebyscore(const StringView &key, const Interval &interval); + + // See *zrange* comment on how to send command with *WITHSCORES* option. + template + void zrevrange(const StringView &key, long long start, long long stop, Output output); + + template + void zrevrangebylex(const StringView &key, const Interval &interval, Output output); + + template + void zrevrangebylex(const StringView &key, + const Interval &interval, + const LimitOptions &opts, + Output output); + + // See *zrange* comment on how to send command with *WITHSCORES* option. + template + void zrevrangebyscore(const StringView &key, const Interval &interval, Output output); + + // See *zrange* comment on how to send command with *WITHSCORES* option. + template + void zrevrangebyscore(const StringView &key, + const Interval &interval, + const LimitOptions &opts, + Output output); + + OptionalLongLong zrevrank(const StringView &key, const StringView &member); + + template + long long zscan(const StringView &key, + long long cursor, + const StringView &pattern, + long long count, + Output output); + + template + long long zscan(const StringView &key, + long long cursor, + const StringView &pattern, + Output output); + + template + long long zscan(const StringView &key, + long long cursor, + long long count, + Output output); + + template + long long zscan(const StringView &key, + long long cursor, + Output output); + + OptionalDouble zscore(const StringView &key, const StringView &member); + + long long zunionstore(const StringView &destination, const StringView &key, double weight); + + template + long long zunionstore(const StringView &destination, + Input first, + Input last, + Aggregation type = Aggregation::SUM); + + template + long long zunionstore(const StringView &destination, + std::initializer_list il, + Aggregation type = Aggregation::SUM) { + return zunionstore(destination, il.begin(), il.end(), type); + } + + // HYPERLOGLOG commands. + + bool pfadd(const StringView &key, const StringView &element); + + template + bool pfadd(const StringView &key, Input first, Input last); + + template + bool pfadd(const StringView &key, std::initializer_list il) { + return pfadd(key, il.begin(), il.end()); + } + + long long pfcount(const StringView &key); + + template + long long pfcount(Input first, Input last); + + template + long long pfcount(std::initializer_list il) { + return pfcount(il.begin(), il.end()); + } + + void pfmerge(const StringView &destination, const StringView &key); + + template + void pfmerge(const StringView &destination, Input first, Input last); + + template + void pfmerge(const StringView &destination, std::initializer_list il) { + pfmerge(destination, il.begin(), il.end()); + } + + // GEO commands. + + long long geoadd(const StringView &key, + const std::tuple &member); + + template + long long geoadd(const StringView &key, + Input first, + Input last); + + template + long long geoadd(const StringView &key, + std::initializer_list il) { + return geoadd(key, il.begin(), il.end()); + } + + OptionalDouble geodist(const StringView &key, + const StringView &member1, + const StringView &member2, + GeoUnit unit = GeoUnit::M); + + template + void geohash(const StringView &key, Input first, Input last, Output output); + + template + void geohash(const StringView &key, std::initializer_list il, Output output) { + geohash(key, il.begin(), il.end(), output); + } + + template + void geopos(const StringView &key, Input first, Input last, Output output); + + template + void geopos(const StringView &key, std::initializer_list il, Output output) { + geopos(key, il.begin(), il.end(), output); + } + + // TODO: + // 1. since we have different overloads for georadius and georadius-store, + // we might use the GEORADIUS_RO command in the future. + // 2. there're too many parameters for this method, we might refactor it. + OptionalLongLong georadius(const StringView &key, + const std::pair &loc, + double radius, + GeoUnit unit, + const StringView &destination, + bool store_dist, + long long count); + + // If *output* is an iterator of a container of string, we send *GEORADIUS* command + // without any options and only get the members in the specified geo range. + // If *output* is an iterator of a container of a tuple, the type of the tuple decides + // options we send with the *GEORADIUS* command. If the tuple has an element of type + // double, we send the *WITHDIST* option. If it has an element of type string, we send + // the *WITHHASH* option. If it has an element of type pair, we send + // the *WITHCOORD* option. For example: + // + // The following code only gets the members in range, i.e. without any option. + // + // vector members; + // redis.georadius("key", make_pair(10.1, 10.2), 10, GeoUnit::KM, 10, true, + // back_inserter(members)) + // + // The following code sends the command with *WITHDIST* option. + // + // vector> with_dist; + // redis.georadius("key", make_pair(10.1, 10.2), 10, GeoUnit::KM, 10, true, + // back_inserter(with_dist)) + // + // The following code sends the command with *WITHDIST* and *WITHHASH* options. + // + // vector> with_dist_hash; + // redis.georadius("key", make_pair(10.1, 10.2), 10, GeoUnit::KM, 10, true, + // back_inserter(with_dist_hash)) + // + // The following code sends the command with *WITHDIST*, *WITHCOORD* and *WITHHASH* options. + // + // vector, string>> with_dist_coord_hash; + // redis.georadius("key", make_pair(10.1, 10.2), 10, GeoUnit::KM, 10, true, + // back_inserter(with_dist_coord_hash)) + // + // This also applies to *GEORADIUSBYMEMBER*. + template + void georadius(const StringView &key, + const std::pair &loc, + double radius, + GeoUnit unit, + long long count, + bool asc, + Output output); + + OptionalLongLong georadiusbymember(const StringView &key, + const StringView &member, + double radius, + GeoUnit unit, + const StringView &destination, + bool store_dist, + long long count); + + // See comments on *GEORADIUS*. + template + void georadiusbymember(const StringView &key, + const StringView &member, + double radius, + GeoUnit unit, + long long count, + bool asc, + Output output); + + // SCRIPTING commands. + + template + Result eval(const StringView &script, + std::initializer_list keys, + std::initializer_list args); + + template + void eval(const StringView &script, + std::initializer_list keys, + std::initializer_list args, + Output output); + + template + Result evalsha(const StringView &script, + std::initializer_list keys, + std::initializer_list args); + + template + void evalsha(const StringView &script, + std::initializer_list keys, + std::initializer_list args, + Output output); + + // PUBSUB commands. + + long long publish(const StringView &channel, const StringView &message); + + // Stream commands. + + long long xack(const StringView &key, const StringView &group, const StringView &id); + + template + long long xack(const StringView &key, const StringView &group, Input first, Input last); + + template + long long xack(const StringView &key, const StringView &group, std::initializer_list il) { + return xack(key, group, il.begin(), il.end()); + } + + template + std::string xadd(const StringView &key, const StringView &id, Input first, Input last); + + template + std::string xadd(const StringView &key, const StringView &id, std::initializer_list il) { + return xadd(key, id, il.begin(), il.end()); + } + + template + std::string xadd(const StringView &key, + const StringView &id, + Input first, + Input last, + long long count, + bool approx = true); + + template + std::string xadd(const StringView &key, + const StringView &id, + std::initializer_list il, + long long count, + bool approx = true) { + return xadd(key, id, il.begin(), il.end(), count, approx); + } + + template + void xclaim(const StringView &key, + const StringView &group, + const StringView &consumer, + const std::chrono::milliseconds &min_idle_time, + const StringView &id, + Output output); + + template + void xclaim(const StringView &key, + const StringView &group, + const StringView &consumer, + const std::chrono::milliseconds &min_idle_time, + Input first, + Input last, + Output output); + + template + void xclaim(const StringView &key, + const StringView &group, + const StringView &consumer, + const std::chrono::milliseconds &min_idle_time, + std::initializer_list il, + Output output) { + xclaim(key, group, consumer, min_idle_time, il.begin(), il.end(), output); + } + + long long xdel(const StringView &key, const StringView &id); + + template + long long xdel(const StringView &key, Input first, Input last); + + template + long long xdel(const StringView &key, std::initializer_list il) { + return xdel(key, il.begin(), il.end()); + } + + void xgroup_create(const StringView &key, + const StringView &group, + const StringView &id, + bool mkstream = false); + + void xgroup_setid(const StringView &key, const StringView &group, const StringView &id); + + long long xgroup_destroy(const StringView &key, const StringView &group); + + long long xgroup_delconsumer(const StringView &key, + const StringView &group, + const StringView &consumer); + + long long xlen(const StringView &key); + + template + auto xpending(const StringView &key, const StringView &group, Output output) + -> std::tuple; + + template + void xpending(const StringView &key, + const StringView &group, + const StringView &start, + const StringView &end, + long long count, + Output output); + + template + void xpending(const StringView &key, + const StringView &group, + const StringView &start, + const StringView &end, + long long count, + const StringView &consumer, + Output output); + + template + void xrange(const StringView &key, + const StringView &start, + const StringView &end, + Output output); + + template + void xrange(const StringView &key, + const StringView &start, + const StringView &end, + long long count, + Output output); + + template + void xread(const StringView &key, + const StringView &id, + long long count, + Output output); + + template + void xread(const StringView &key, + const StringView &id, + Output output) { + xread(key, id, 0, output); + } + + template + auto xread(Input first, Input last, long long count, Output output) + -> typename std::enable_if::value>::type; + + template + auto xread(Input first, Input last, Output output) + -> typename std::enable_if::value>::type { + xread(first, last, 0, output); + } + + template + void xread(const StringView &key, + const StringView &id, + const std::chrono::milliseconds &timeout, + long long count, + Output output); + + template + void xread(const StringView &key, + const StringView &id, + const std::chrono::milliseconds &timeout, + Output output) { + xread(key, id, timeout, 0, output); + } + + template + auto xread(Input first, + Input last, + const std::chrono::milliseconds &timeout, + long long count, + Output output) + -> typename std::enable_if::value>::type; + + template + auto xread(Input first, + Input last, + const std::chrono::milliseconds &timeout, + Output output) + -> typename std::enable_if::value>::type { + xread(first, last, timeout, 0, output); + } + + template + void xreadgroup(const StringView &group, + const StringView &consumer, + const StringView &key, + const StringView &id, + long long count, + bool noack, + Output output); + + template + void xreadgroup(const StringView &group, + const StringView &consumer, + const StringView &key, + const StringView &id, + long long count, + Output output) { + xreadgroup(group, consumer, key, id, count, false, output); + } + + template + void xreadgroup(const StringView &group, + const StringView &consumer, + const StringView &key, + const StringView &id, + Output output) { + xreadgroup(group, consumer, key, id, 0, false, output); + } + + template + auto xreadgroup(const StringView &group, + const StringView &consumer, + Input first, + Input last, + long long count, + bool noack, + Output output) + -> typename std::enable_if::value>::type; + + template + auto xreadgroup(const StringView &group, + const StringView &consumer, + Input first, + Input last, + long long count, + Output output) + -> typename std::enable_if::value>::type { + xreadgroup(group, consumer, first ,last, count, false, output); + } + + template + auto xreadgroup(const StringView &group, + const StringView &consumer, + Input first, + Input last, + Output output) + -> typename std::enable_if::value>::type { + xreadgroup(group, consumer, first ,last, 0, false, output); + } + + template + void xreadgroup(const StringView &group, + const StringView &consumer, + const StringView &key, + const StringView &id, + const std::chrono::milliseconds &timeout, + long long count, + bool noack, + Output output); + + template + void xreadgroup(const StringView &group, + const StringView &consumer, + const StringView &key, + const StringView &id, + const std::chrono::milliseconds &timeout, + long long count, + Output output) { + return xreadgroup(group, consumer, key, id, timeout, count, false, output); + } + + template + void xreadgroup(const StringView &group, + const StringView &consumer, + const StringView &key, + const StringView &id, + const std::chrono::milliseconds &timeout, + Output output) { + return xreadgroup(group, consumer, key, id, timeout, 0, false, output); + } + + template + auto xreadgroup(const StringView &group, + const StringView &consumer, + Input first, + Input last, + const std::chrono::milliseconds &timeout, + long long count, + bool noack, + Output output) + -> typename std::enable_if::value>::type; + + template + auto xreadgroup(const StringView &group, + const StringView &consumer, + Input first, + Input last, + const std::chrono::milliseconds &timeout, + long long count, + Output output) + -> typename std::enable_if::value>::type { + xreadgroup(group, consumer, first, last, timeout, count, false, output); + } + + template + auto xreadgroup(const StringView &group, + const StringView &consumer, + Input first, + Input last, + const std::chrono::milliseconds &timeout, + Output output) + -> typename std::enable_if::value>::type { + xreadgroup(group, consumer, first, last, timeout, 0, false, output); + } + + template + void xrevrange(const StringView &key, + const StringView &end, + const StringView &start, + Output output); + + template + void xrevrange(const StringView &key, + const StringView &end, + const StringView &start, + long long count, + Output output); + + long long xtrim(const StringView &key, long long count, bool approx = true); + +private: + class Command { + public: + explicit Command(const StringView &cmd_name) : _cmd_name(cmd_name) {} + + template + void operator()(Connection &connection, Args &&...args) const { + CmdArgs cmd_args; + cmd_args.append(_cmd_name, std::forward(args)...); + connection.send(cmd_args); + } + + private: + StringView _cmd_name; + }; + + template + auto _generic_command(Cmd cmd, Key &&key, Args &&...args) + -> typename std::enable_if::value, + ReplyUPtr>::type; + + template + auto _generic_command(Cmd cmd, Key &&key, Args &&...args) + -> typename std::enable_if::type>::value, + ReplyUPtr>::type; + + template + ReplyUPtr _command(Cmd cmd, Connection &connection, Args &&...args); + + template + ReplyUPtr _command(Cmd cmd, const StringView &key, Args &&...args); + + template + ReplyUPtr _command(Cmd cmd, std::true_type, const StringView &key, Args &&...args); + + template + ReplyUPtr _command(Cmd cmd, std::false_type, Input &&first, Args &&...args); + + template + ReplyUPtr _command(const StringView &cmd_name, const IndexSequence &, Args &&...args) { + return command(cmd_name, NthValue(std::forward(args)...)...); + } + + template + ReplyUPtr _range_command(Cmd cmd, std::true_type, Input input, Args &&...args); + + template + ReplyUPtr _range_command(Cmd cmd, std::false_type, Input input, Args &&...args); + + void _asking(Connection &connection); + + template + ReplyUPtr _score_command(std::true_type, Cmd cmd, Args &&... args); + + template + ReplyUPtr _score_command(std::false_type, Cmd cmd, Args &&... args); + + template + ReplyUPtr _score_command(Cmd cmd, Args &&... args); + + ShardsPool _pool; +}; + +} + +} + +#include "redis_cluster.hpp" + +#endif // end SEWENEW_REDISPLUSPLUS_REDIS_CLUSTER_H diff --git a/controller/thirdparty/redis-plus-plus-1.1.1/src/sw/redis++/redis_cluster.hpp b/controller/thirdparty/redis-plus-plus-1.1.1/src/sw/redis++/redis_cluster.hpp new file mode 100644 index 000000000..61da3f062 --- /dev/null +++ b/controller/thirdparty/redis-plus-plus-1.1.1/src/sw/redis++/redis_cluster.hpp @@ -0,0 +1,1415 @@ +/************************************************************************** + Copyright (c) 2017 sewenew + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. + *************************************************************************/ + +#ifndef SEWENEW_REDISPLUSPLUS_REDIS_CLUSTER_HPP +#define SEWENEW_REDISPLUSPLUS_REDIS_CLUSTER_HPP + +#include +#include "command.h" +#include "reply.h" +#include "utils.h" +#include "errors.h" +#include "shards_pool.h" + +namespace sw { + +namespace redis { + +template +auto RedisCluster::command(Cmd cmd, Key &&key, Args &&...args) + -> typename std::enable_if::value, ReplyUPtr>::type { + return _command(cmd, + std::is_convertible::type, StringView>(), + std::forward(key), + std::forward(args)...); +} + +template +auto RedisCluster::command(const StringView &cmd_name, Key &&key, Args &&...args) + -> typename std::enable_if<(std::is_convertible::value + || std::is_arithmetic::type>::value) + && !IsIter::type>::value, ReplyUPtr>::type { + auto cmd = Command(cmd_name); + + return _generic_command(cmd, std::forward(key), std::forward(args)...); +} + +template +auto RedisCluster::command(const StringView &cmd_name, Key &&key, Args &&...args) + -> typename std::enable_if::value + || std::is_arithmetic::type>::value, Result>::type { + auto r = command(cmd_name, std::forward(key), std::forward(args)...); + + assert(r); + + return reply::parse(*r); +} + +template +auto RedisCluster::command(const StringView &cmd_name, Key &&key, Args &&...args) + -> typename std::enable_if<(std::is_convertible::value + || std::is_arithmetic::type>::value) + && IsIter::type>::value, void>::type { + auto r = _command(cmd_name, + MakeIndexSequence(), + std::forward(key), + std::forward(args)...); + + assert(r); + + reply::to_array(*r, LastValue(std::forward(args)...)); +} + +template +auto RedisCluster::command(Input first, Input last) + -> typename std::enable_if::value, ReplyUPtr>::type { + if (first == last || std::next(first) == last) { + throw Error("command: invalid range"); + } + + const auto &key = *first; + ++first; + + auto cmd = [&key](Connection &connection, Input first, Input last) { + CmdArgs cmd_args; + cmd_args.append(key); + while (first != last) { + cmd_args.append(*first); + ++first; + } + connection.send(cmd_args); + }; + + return command(cmd, first, last); +} + +template +auto RedisCluster::command(Input first, Input last) + -> typename std::enable_if::value, Result>::type { + auto r = command(first, last); + + assert(r); + + return reply::parse(*r); +} + +template +auto RedisCluster::command(Input first, Input last, Output output) + -> typename std::enable_if::value, void>::type { + auto r = command(first, last); + + assert(r); + + reply::to_array(*r, output); +} + +// KEY commands. + +template +long long RedisCluster::del(Input first, Input last) { + if (first == last) { + throw Error("DEL: no key specified"); + } + + auto reply = command(cmd::del_range, first, last); + + return reply::parse(*reply); +} + +template +long long RedisCluster::exists(Input first, Input last) { + if (first == last) { + throw Error("EXISTS: no key specified"); + } + + auto reply = command(cmd::exists_range, first, last); + + return reply::parse(*reply); +} + +inline bool RedisCluster::expire(const StringView &key, const std::chrono::seconds &timeout) { + return expire(key, timeout.count()); +} + +inline bool RedisCluster::expireat(const StringView &key, + const std::chrono::time_point &tp) { + return expireat(key, tp.time_since_epoch().count()); +} + +inline bool RedisCluster::pexpire(const StringView &key, const std::chrono::milliseconds &timeout) { + return pexpire(key, timeout.count()); +} + +inline bool RedisCluster::pexpireat(const StringView &key, + const std::chrono::time_point &tp) { + return pexpireat(key, tp.time_since_epoch().count()); +} + +inline void RedisCluster::restore(const StringView &key, + const StringView &val, + const std::chrono::milliseconds &ttl, + bool replace) { + return restore(key, val, ttl.count(), replace); +} + +template +long long RedisCluster::touch(Input first, Input last) { + if (first == last) { + throw Error("TOUCH: no key specified"); + } + + auto reply = command(cmd::touch_range, first, last); + + return reply::parse(*reply); +} + +template +long long RedisCluster::unlink(Input first, Input last) { + if (first == last) { + throw Error("UNLINK: no key specified"); + } + + auto reply = command(cmd::unlink_range, first, last); + + return reply::parse(*reply); +} + +// STRING commands. + +template +long long RedisCluster::bitop(BitOp op, const StringView &destination, Input first, Input last) { + if (first == last) { + throw Error("BITOP: no key specified"); + } + + auto reply = _command(cmd::bitop_range, destination, op, destination, first, last); + + return reply::parse(*reply); +} + +template +void RedisCluster::mget(Input first, Input last, Output output) { + if (first == last) { + throw Error("MGET: no key specified"); + } + + auto reply = command(cmd::mget, first, last); + + reply::to_array(*reply, output); +} + +template +void RedisCluster::mset(Input first, Input last) { + if (first == last) { + throw Error("MSET: no key specified"); + } + + auto reply = command(cmd::mset, first, last); + + reply::parse(*reply); +} + +template +bool RedisCluster::msetnx(Input first, Input last) { + if (first == last) { + throw Error("MSETNX: no key specified"); + } + + auto reply = command(cmd::msetnx, first, last); + + return reply::parse(*reply); +} + +inline void RedisCluster::psetex(const StringView &key, + const std::chrono::milliseconds &ttl, + const StringView &val) { + return psetex(key, ttl.count(), val); +} + +inline void RedisCluster::setex(const StringView &key, + const std::chrono::seconds &ttl, + const StringView &val) { + setex(key, ttl.count(), val); +} + +// LIST commands. + +template +OptionalStringPair RedisCluster::blpop(Input first, Input last, long long timeout) { + if (first == last) { + throw Error("BLPOP: no key specified"); + } + + auto reply = command(cmd::blpop_range, first, last, timeout); + + return reply::parse(*reply); +} + +template +OptionalStringPair RedisCluster::blpop(Input first, + Input last, + const std::chrono::seconds &timeout) { + return blpop(first, last, timeout.count()); +} + +template +OptionalStringPair RedisCluster::brpop(Input first, Input last, long long timeout) { + if (first == last) { + throw Error("BRPOP: no key specified"); + } + + auto reply = command(cmd::brpop_range, first, last, timeout); + + return reply::parse(*reply); +} + +template +OptionalStringPair RedisCluster::brpop(Input first, + Input last, + const std::chrono::seconds &timeout) { + return brpop(first, last, timeout.count()); +} + +inline OptionalString RedisCluster::brpoplpush(const StringView &source, + const StringView &destination, + const std::chrono::seconds &timeout) { + return brpoplpush(source, destination, timeout.count()); +} + +template +inline long long RedisCluster::lpush(const StringView &key, Input first, Input last) { + if (first == last) { + throw Error("LPUSH: no key specified"); + } + + auto reply = command(cmd::lpush_range, key, first, last); + + return reply::parse(*reply); +} + +template +inline void RedisCluster::lrange(const StringView &key, long long start, long long stop, Output output) { + auto reply = command(cmd::lrange, key, start, stop); + + reply::to_array(*reply, output); +} + +template +inline long long RedisCluster::rpush(const StringView &key, Input first, Input last) { + if (first == last) { + throw Error("RPUSH: no key specified"); + } + + auto reply = command(cmd::rpush_range, key, first, last); + + return reply::parse(*reply); +} + +// HASH commands. + +template +inline long long RedisCluster::hdel(const StringView &key, Input first, Input last) { + if (first == last) { + throw Error("HDEL: no key specified"); + } + + auto reply = command(cmd::hdel_range, key, first, last); + + return reply::parse(*reply); +} + +template +inline void RedisCluster::hgetall(const StringView &key, Output output) { + auto reply = command(cmd::hgetall, key); + + reply::to_array(*reply, output); +} + +template +inline void RedisCluster::hkeys(const StringView &key, Output output) { + auto reply = command(cmd::hkeys, key); + + reply::to_array(*reply, output); +} + +template +inline void RedisCluster::hmget(const StringView &key, Input first, Input last, Output output) { + if (first == last) { + throw Error("HMGET: no key specified"); + } + + auto reply = command(cmd::hmget, key, first, last); + + reply::to_array(*reply, output); +} + +template +inline void RedisCluster::hmset(const StringView &key, Input first, Input last) { + if (first == last) { + throw Error("HMSET: no key specified"); + } + + auto reply = command(cmd::hmset, key, first, last); + + reply::parse(*reply); +} + +template +long long RedisCluster::hscan(const StringView &key, + long long cursor, + const StringView &pattern, + long long count, + Output output) { + auto reply = command(cmd::hscan, key, cursor, pattern, count); + + return reply::parse_scan_reply(*reply, output); +} + +template +inline long long RedisCluster::hscan(const StringView &key, + long long cursor, + const StringView &pattern, + Output output) { + return hscan(key, cursor, pattern, 10, output); +} + +template +inline long long RedisCluster::hscan(const StringView &key, + long long cursor, + long long count, + Output output) { + return hscan(key, cursor, "*", count, output); +} + +template +inline long long RedisCluster::hscan(const StringView &key, + long long cursor, + Output output) { + return hscan(key, cursor, "*", 10, output); +} + +template +inline void RedisCluster::hvals(const StringView &key, Output output) { + auto reply = command(cmd::hvals, key); + + reply::to_array(*reply, output); +} + +// SET commands. + +template +long long RedisCluster::sadd(const StringView &key, Input first, Input last) { + if (first == last) { + throw Error("SADD: no key specified"); + } + + auto reply = command(cmd::sadd_range, key, first, last); + + return reply::parse(*reply); +} + +template +void RedisCluster::sdiff(Input first, Input last, Output output) { + if (first == last) { + throw Error("SDIFF: no key specified"); + } + + auto reply = command(cmd::sdiff, first, last); + + reply::to_array(*reply, output); +} + +template +long long RedisCluster::sdiffstore(const StringView &destination, + Input first, + Input last) { + if (first == last) { + throw Error("SDIFFSTORE: no key specified"); + } + + auto reply = command(cmd::sdiffstore_range, destination, first, last); + + return reply::parse(*reply); +} + +template +void RedisCluster::sinter(Input first, Input last, Output output) { + if (first == last) { + throw Error("SINTER: no key specified"); + } + + auto reply = command(cmd::sinter, first, last); + + reply::to_array(*reply, output); +} + +template +long long RedisCluster::sinterstore(const StringView &destination, + Input first, + Input last) { + if (first == last) { + throw Error("SINTERSTORE: no key specified"); + } + + auto reply = command(cmd::sinterstore_range, destination, first, last); + + return reply::parse(*reply); +} + +template +void RedisCluster::smembers(const StringView &key, Output output) { + auto reply = command(cmd::smembers, key); + + reply::to_array(*reply, output); +} + +template +void RedisCluster::spop(const StringView &key, long long count, Output output) { + auto reply = command(cmd::spop_range, key, count); + + reply::to_array(*reply, output); +} + +template +void RedisCluster::srandmember(const StringView &key, long long count, Output output) { + auto reply = command(cmd::srandmember_range, key, count); + + reply::to_array(*reply, output); +} + +template +long long RedisCluster::srem(const StringView &key, Input first, Input last) { + if (first == last) { + throw Error("SREM: no key specified"); + } + + auto reply = command(cmd::srem_range, key, first, last); + + return reply::parse(*reply); +} + +template +long long RedisCluster::sscan(const StringView &key, + long long cursor, + const StringView &pattern, + long long count, + Output output) { + auto reply = command(cmd::sscan, key, cursor, pattern, count); + + return reply::parse_scan_reply(*reply, output); +} + +template +inline long long RedisCluster::sscan(const StringView &key, + long long cursor, + const StringView &pattern, + Output output) { + return sscan(key, cursor, pattern, 10, output); +} + +template +inline long long RedisCluster::sscan(const StringView &key, + long long cursor, + long long count, + Output output) { + return sscan(key, cursor, "*", count, output); +} + +template +inline long long RedisCluster::sscan(const StringView &key, + long long cursor, + Output output) { + return sscan(key, cursor, "*", 10, output); +} + +template +void RedisCluster::sunion(Input first, Input last, Output output) { + if (first == last) { + throw Error("SUNION: no key specified"); + } + + auto reply = command(cmd::sunion, first, last); + + reply::to_array(*reply, output); +} + +template +long long RedisCluster::sunionstore(const StringView &destination, Input first, Input last) { + if (first == last) { + throw Error("SUNIONSTORE: no key specified"); + } + + auto reply = command(cmd::sunionstore_range, destination, first, last); + + return reply::parse(*reply); +} + +// SORTED SET commands. + +inline auto RedisCluster::bzpopmax(const StringView &key, const std::chrono::seconds &timeout) + -> Optional> { + return bzpopmax(key, timeout.count()); +} + +template +auto RedisCluster::bzpopmax(Input first, Input last, long long timeout) + -> Optional> { + auto reply = command(cmd::bzpopmax_range, first, last, timeout); + + return reply::parse>>(*reply); +} + +template +inline auto RedisCluster::bzpopmax(Input first, + Input last, + const std::chrono::seconds &timeout) + -> Optional> { + return bzpopmax(first, last, timeout.count()); +} + +inline auto RedisCluster::bzpopmin(const StringView &key, const std::chrono::seconds &timeout) + -> Optional> { + return bzpopmin(key, timeout.count()); +} + +template +auto RedisCluster::bzpopmin(Input first, Input last, long long timeout) + -> Optional> { + auto reply = command(cmd::bzpopmin_range, first, last, timeout); + + return reply::parse>>(*reply); +} + +template +inline auto RedisCluster::bzpopmin(Input first, + Input last, + const std::chrono::seconds &timeout) + -> Optional> { + return bzpopmin(first, last, timeout.count()); +} + +template +long long RedisCluster::zadd(const StringView &key, + Input first, + Input last, + UpdateType type, + bool changed) { + if (first == last) { + throw Error("ZADD: no key specified"); + } + + auto reply = command(cmd::zadd_range, key, first, last, type, changed); + + return reply::parse(*reply); +} + +template +long long RedisCluster::zcount(const StringView &key, const Interval &interval) { + auto reply = command(cmd::zcount, key, interval); + + return reply::parse(*reply); +} + +template +long long RedisCluster::zinterstore(const StringView &destination, + Input first, + Input last, + Aggregation type) { + if (first == last) { + throw Error("ZINTERSTORE: no key specified"); + } + + auto reply = command(cmd::zinterstore_range, + destination, + first, + last, + type); + + return reply::parse(*reply); +} + +template +long long RedisCluster::zlexcount(const StringView &key, const Interval &interval) { + auto reply = command(cmd::zlexcount, key, interval); + + return reply::parse(*reply); +} + +template +void RedisCluster::zpopmax(const StringView &key, long long count, Output output) { + auto reply = command(cmd::zpopmax, key, count); + + reply::to_array(*reply, output); +} + +template +void RedisCluster::zpopmin(const StringView &key, long long count, Output output) { + auto reply = command(cmd::zpopmin, key, count); + + reply::to_array(*reply, output); +} + +template +void RedisCluster::zrange(const StringView &key, long long start, long long stop, Output output) { + auto reply = _score_command(cmd::zrange, key, start, stop); + + reply::to_array(*reply, output); +} + +template +void RedisCluster::zrangebylex(const StringView &key, const Interval &interval, Output output) { + zrangebylex(key, interval, {}, output); +} + +template +void RedisCluster::zrangebylex(const StringView &key, + const Interval &interval, + const LimitOptions &opts, + Output output) { + auto reply = command(cmd::zrangebylex, key, interval, opts); + + reply::to_array(*reply, output); +} + +template +void RedisCluster::zrangebyscore(const StringView &key, + const Interval &interval, + Output output) { + zrangebyscore(key, interval, {}, output); +} + +template +void RedisCluster::zrangebyscore(const StringView &key, + const Interval &interval, + const LimitOptions &opts, + Output output) { + auto reply = _score_command(cmd::zrangebyscore, + key, + interval, + opts); + + reply::to_array(*reply, output); +} + +template +long long RedisCluster::zrem(const StringView &key, Input first, Input last) { + if (first == last) { + throw Error("ZREM: no key specified"); + } + + auto reply = command(cmd::zrem_range, key, first, last); + + return reply::parse(*reply); +} + +template +long long RedisCluster::zremrangebylex(const StringView &key, const Interval &interval) { + auto reply = command(cmd::zremrangebylex, key, interval); + + return reply::parse(*reply); +} + +template +long long RedisCluster::zremrangebyscore(const StringView &key, const Interval &interval) { + auto reply = command(cmd::zremrangebyscore, key, interval); + + return reply::parse(*reply); +} + +template +void RedisCluster::zrevrange(const StringView &key, long long start, long long stop, Output output) { + auto reply = _score_command(cmd::zrevrange, key, start, stop); + + reply::to_array(*reply, output); +} + +template +inline void RedisCluster::zrevrangebylex(const StringView &key, + const Interval &interval, + Output output) { + zrevrangebylex(key, interval, {}, output); +} + +template +void RedisCluster::zrevrangebylex(const StringView &key, + const Interval &interval, + const LimitOptions &opts, + Output output) { + auto reply = command(cmd::zrevrangebylex, key, interval, opts); + + reply::to_array(*reply, output); +} + +template +void RedisCluster::zrevrangebyscore(const StringView &key, const Interval &interval, Output output) { + zrevrangebyscore(key, interval, {}, output); +} + +template +void RedisCluster::zrevrangebyscore(const StringView &key, + const Interval &interval, + const LimitOptions &opts, + Output output) { + auto reply = _score_command(cmd::zrevrangebyscore, key, interval, opts); + + reply::to_array(*reply, output); +} + +template +long long RedisCluster::zscan(const StringView &key, + long long cursor, + const StringView &pattern, + long long count, + Output output) { + auto reply = command(cmd::zscan, key, cursor, pattern, count); + + return reply::parse_scan_reply(*reply, output); +} + +template +inline long long RedisCluster::zscan(const StringView &key, + long long cursor, + const StringView &pattern, + Output output) { + return zscan(key, cursor, pattern, 10, output); +} + +template +inline long long RedisCluster::zscan(const StringView &key, + long long cursor, + long long count, + Output output) { + return zscan(key, cursor, "*", count, output); +} + +template +inline long long RedisCluster::zscan(const StringView &key, + long long cursor, + Output output) { + return zscan(key, cursor, "*", 10, output); +} + +template +long long RedisCluster::zunionstore(const StringView &destination, + Input first, + Input last, + Aggregation type) { + if (first == last) { + throw Error("ZUNIONSTORE: no key specified"); + } + + auto reply = command(cmd::zunionstore_range, + destination, + first, + last, + type); + + return reply::parse(*reply); +} + +// HYPERLOGLOG commands. + +template +bool RedisCluster::pfadd(const StringView &key, Input first, Input last) { + if (first == last) { + throw Error("PFADD: no key specified"); + } + + auto reply = command(cmd::pfadd_range, key, first, last); + + return reply::parse(*reply); +} + +template +long long RedisCluster::pfcount(Input first, Input last) { + if (first == last) { + throw Error("PFCOUNT: no key specified"); + } + + auto reply = command(cmd::pfcount_range, first, last); + + return reply::parse(*reply); +} + +template +void RedisCluster::pfmerge(const StringView &destination, + Input first, + Input last) { + if (first == last) { + throw Error("PFMERGE: no key specified"); + } + + auto reply = command(cmd::pfmerge_range, destination, first, last); + + reply::parse(*reply); +} + +// GEO commands. + +template +inline long long RedisCluster::geoadd(const StringView &key, + Input first, + Input last) { + if (first == last) { + throw Error("GEOADD: no key specified"); + } + + auto reply = command(cmd::geoadd_range, key, first, last); + + return reply::parse(*reply); +} + +template +void RedisCluster::geohash(const StringView &key, Input first, Input last, Output output) { + if (first == last) { + throw Error("GEOHASH: no key specified"); + } + + auto reply = command(cmd::geohash_range, key, first, last); + + reply::to_array(*reply, output); +} + +template +void RedisCluster::geopos(const StringView &key, Input first, Input last, Output output) { + if (first == last) { + throw Error("GEOPOS: no key specified"); + } + + auto reply = command(cmd::geopos_range, key, first, last); + + reply::to_array(*reply, output); +} + +template +void RedisCluster::georadius(const StringView &key, + const std::pair &loc, + double radius, + GeoUnit unit, + long long count, + bool asc, + Output output) { + auto reply = command(cmd::georadius, + key, + loc, + radius, + unit, + count, + asc, + WithCoord::type>::value, + WithDist::type>::value, + WithHash::type>::value); + + reply::to_array(*reply, output); +} + +template +void RedisCluster::georadiusbymember(const StringView &key, + const StringView &member, + double radius, + GeoUnit unit, + long long count, + bool asc, + Output output) { + auto reply = command(cmd::georadiusbymember, + key, + member, + radius, + unit, + count, + asc, + WithCoord::type>::value, + WithDist::type>::value, + WithHash::type>::value); + + reply::to_array(*reply, output); +} + +// SCRIPTING commands. + +template +Result RedisCluster::eval(const StringView &script, + std::initializer_list keys, + std::initializer_list args) { + if (keys.size() == 0) { + throw Error("DO NOT support Lua script without key"); + } + + auto reply = _command(cmd::eval, *keys.begin(), script, keys, args); + + return reply::parse(*reply); +} + +template +void RedisCluster::eval(const StringView &script, + std::initializer_list keys, + std::initializer_list args, + Output output) { + if (keys.size() == 0) { + throw Error("DO NOT support Lua script without key"); + } + + auto reply = _command(cmd::eval, *keys.begin(), script, keys, args); + + reply::to_array(*reply, output); +} + +template +Result RedisCluster::evalsha(const StringView &script, + std::initializer_list keys, + std::initializer_list args) { + if (keys.size() == 0) { + throw Error("DO NOT support Lua script without key"); + } + + auto reply = _command(cmd::evalsha, *keys.begin(), script, keys, args); + + return reply::parse(*reply); +} + +template +void RedisCluster::evalsha(const StringView &script, + std::initializer_list keys, + std::initializer_list args, + Output output) { + if (keys.size() == 0) { + throw Error("DO NOT support Lua script without key"); + } + + auto reply = command(cmd::evalsha, *keys.begin(), script, keys, args); + + reply::to_array(*reply, output); +} + +// Stream commands. + +template +long long RedisCluster::xack(const StringView &key, + const StringView &group, + Input first, + Input last) { + auto reply = command(cmd::xack_range, key, group, first, last); + + return reply::parse(*reply); +} + +template +std::string RedisCluster::xadd(const StringView &key, + const StringView &id, + Input first, + Input last) { + auto reply = command(cmd::xadd_range, key, id, first, last); + + return reply::parse(*reply); +} + +template +std::string RedisCluster::xadd(const StringView &key, + const StringView &id, + Input first, + Input last, + long long count, + bool approx) { + auto reply = command(cmd::xadd_maxlen_range, key, id, first, last, count, approx); + + return reply::parse(*reply); +} + +template +void RedisCluster::xclaim(const StringView &key, + const StringView &group, + const StringView &consumer, + const std::chrono::milliseconds &min_idle_time, + const StringView &id, + Output output) { + auto reply = command(cmd::xclaim, key, group, consumer, min_idle_time.count(), id); + + reply::to_array(*reply, output); +} + +template +void RedisCluster::xclaim(const StringView &key, + const StringView &group, + const StringView &consumer, + const std::chrono::milliseconds &min_idle_time, + Input first, + Input last, + Output output) { + auto reply = command(cmd::xclaim_range, + key, + group, + consumer, + min_idle_time.count(), + first, + last); + + reply::to_array(*reply, output); +} + +template +long long RedisCluster::xdel(const StringView &key, Input first, Input last) { + auto reply = command(cmd::xdel_range, key, first, last); + + return reply::parse(*reply); +} + +template +auto RedisCluster::xpending(const StringView &key, const StringView &group, Output output) + -> std::tuple { + auto reply = command(cmd::xpending, key, group); + + return reply::parse_xpending_reply(*reply, output); +} + +template +void RedisCluster::xpending(const StringView &key, + const StringView &group, + const StringView &start, + const StringView &end, + long long count, + Output output) { + auto reply = command(cmd::xpending_detail, key, group, start, end, count); + + reply::to_array(*reply, output); +} + +template +void RedisCluster::xpending(const StringView &key, + const StringView &group, + const StringView &start, + const StringView &end, + long long count, + const StringView &consumer, + Output output) { + auto reply = command(cmd::xpending_per_consumer, key, group, start, end, count, consumer); + + reply::to_array(*reply, output); +} + +template +void RedisCluster::xrange(const StringView &key, + const StringView &start, + const StringView &end, + Output output) { + auto reply = command(cmd::xrange, key, start, end); + + reply::to_array(*reply, output); +} + +template +void RedisCluster::xrange(const StringView &key, + const StringView &start, + const StringView &end, + long long count, + Output output) { + auto reply = command(cmd::xrange_count, key, start, end, count); + + reply::to_array(*reply, output); +} + +template +void RedisCluster::xread(const StringView &key, + const StringView &id, + long long count, + Output output) { + auto reply = command(cmd::xread, key, id, count); + + if (!reply::is_nil(*reply)) { + reply::to_array(*reply, output); + } +} + +template +auto RedisCluster::xread(Input first, Input last, long long count, Output output) + -> typename std::enable_if::value>::type { + if (first == last) { + throw Error("XREAD: no key specified"); + } + + auto reply = command(cmd::xread_range, first, last, count); + + if (!reply::is_nil(*reply)) { + reply::to_array(*reply, output); + } +} + +template +void RedisCluster::xread(const StringView &key, + const StringView &id, + const std::chrono::milliseconds &timeout, + long long count, + Output output) { + auto reply = command(cmd::xread_block, key, id, timeout.count(), count); + + if (!reply::is_nil(*reply)) { + reply::to_array(*reply, output); + } +} + +template +auto RedisCluster::xread(Input first, + Input last, + const std::chrono::milliseconds &timeout, + long long count, + Output output) + -> typename std::enable_if::value>::type { + if (first == last) { + throw Error("XREAD: no key specified"); + } + + auto reply = command(cmd::xread_block_range, first, last, timeout.count(), count); + + if (!reply::is_nil(*reply)) { + reply::to_array(*reply, output); + } +} + +template +void RedisCluster::xreadgroup(const StringView &group, + const StringView &consumer, + const StringView &key, + const StringView &id, + long long count, + bool noack, + Output output) { + auto reply = _command(cmd::xreadgroup, key, group, consumer, key, id, count, noack); + + if (!reply::is_nil(*reply)) { + reply::to_array(*reply, output); + } +} + +template +auto RedisCluster::xreadgroup(const StringView &group, + const StringView &consumer, + Input first, + Input last, + long long count, + bool noack, + Output output) + -> typename std::enable_if::value>::type { + if (first == last) { + throw Error("XREADGROUP: no key specified"); + } + + auto reply = _command(cmd::xreadgroup_range, + first->first, + group, + consumer, + first, + last, + count, + noack); + + if (!reply::is_nil(*reply)) { + reply::to_array(*reply, output); + } +} + +template +void RedisCluster::xreadgroup(const StringView &group, + const StringView &consumer, + const StringView &key, + const StringView &id, + const std::chrono::milliseconds &timeout, + long long count, + bool noack, + Output output) { + auto reply = _command(cmd::xreadgroup_block, + key, + group, + consumer, + key, + id, + timeout.count(), + count, + noack); + + if (!reply::is_nil(*reply)) { + reply::to_array(*reply, output); + } +} + +template +auto RedisCluster::xreadgroup(const StringView &group, + const StringView &consumer, + Input first, + Input last, + const std::chrono::milliseconds &timeout, + long long count, + bool noack, + Output output) + -> typename std::enable_if::value>::type { + if (first == last) { + throw Error("XREADGROUP: no key specified"); + } + + auto reply = _command(cmd::xreadgroup_block_range, + first->first, + group, + consumer, + first, + last, + timeout.count(), + count, + noack); + + if (!reply::is_nil(*reply)) { + reply::to_array(*reply, output); + } +} + +template +void RedisCluster::xrevrange(const StringView &key, + const StringView &end, + const StringView &start, + Output output) { + auto reply = command(cmd::xrevrange, key, end, start); + + reply::to_array(*reply, output); +} + +template +void RedisCluster::xrevrange(const StringView &key, + const StringView &end, + const StringView &start, + long long count, + Output output) { + auto reply = command(cmd::xrevrange_count, key, end, start, count); + + reply::to_array(*reply, output); +} + +template +auto RedisCluster::_generic_command(Cmd cmd, Key &&key, Args &&...args) + -> typename std::enable_if::value, + ReplyUPtr>::type { + return command(cmd, std::forward(key), std::forward(args)...); +} + +template +auto RedisCluster::_generic_command(Cmd cmd, Key &&key, Args &&...args) + -> typename std::enable_if::type>::value, + ReplyUPtr>::type { + auto k = std::to_string(std::forward(key)); + return command(cmd, k, std::forward(args)...); +} + +template +ReplyUPtr RedisCluster::_command(Cmd cmd, std::true_type, const StringView &key, Args &&...args) { + return _command(cmd, key, key, std::forward(args)...); +} + +template +ReplyUPtr RedisCluster::_command(Cmd cmd, std::false_type, Input &&first, Args &&...args) { + return _range_command(cmd, + std::is_convertible< + typename std::decay< + decltype(*std::declval())>::type, StringView>(), + std::forward(first), + std::forward(args)...); +} + +template +ReplyUPtr RedisCluster::_range_command(Cmd cmd, std::true_type, Input input, Args &&...args) { + return _command(cmd, *input, input, std::forward(args)...); +} + +template +ReplyUPtr RedisCluster::_range_command(Cmd cmd, std::false_type, Input input, Args &&...args) { + return _command(cmd, std::get<0>(*input), input, std::forward(args)...); +} + +template +ReplyUPtr RedisCluster::_command(Cmd cmd, Connection &connection, Args &&...args) { + assert(!connection.broken()); + + cmd(connection, std::forward(args)...); + + return connection.recv(); +} + +template +ReplyUPtr RedisCluster::_command(Cmd cmd, const StringView &key, Args &&...args) { + for (auto idx = 0; idx < 2; ++idx) { + try { + auto guarded_connection = _pool.fetch(key); + + return _command(cmd, guarded_connection.connection(), std::forward(args)...); + } catch (const IoError &err) { + // When master is down, one of its replicas will be promoted to be the new master. + // If we try to send command to the old master, we'll get an *IoError*. + // In this case, we need to update the slots mapping. + _pool.update(); + } catch (const ClosedError &err) { + // Node might be removed. + // 1. Get up-to-date slot mapping to check if the node still exists. + _pool.update(); + + // TODO: + // 2. If it's NOT exist, update slot mapping, and retry. + // 3. If it's still exist, that means the node is down, NOT removed, throw exception. + } catch (const MovedError &err) { + // Slot mapping has been changed, update it and try again. + _pool.update(); + } catch (const AskError &err) { + auto guarded_connection = _pool.fetch(err.node()); + auto &connection = guarded_connection.connection(); + + // 1. send ASKING command. + _asking(connection); + + // 2. resend last command. + try { + return _command(cmd, connection, std::forward(args)...); + } catch (const MovedError &err) { + throw Error("Slot migrating... ASKING node hasn't been set to IMPORTING state"); + } + } // For other exceptions, just throw it. + } + + // Possible failures: + // 1. Source node has already run 'CLUSTER SETSLOT xxx NODE xxx', + // while the destination node has NOT run it. + // In this case, client will be redirected by both nodes with MovedError. + // 2. Other failures... + throw Error("Failed to send command with key: " + std::string(key.data(), key.size())); +} + +template +inline ReplyUPtr RedisCluster::_score_command(std::true_type, Cmd cmd, Args &&... args) { + return command(cmd, std::forward(args)..., true); +} + +template +inline ReplyUPtr RedisCluster::_score_command(std::false_type, Cmd cmd, Args &&... args) { + return command(cmd, std::forward(args)..., false); +} + +template +inline ReplyUPtr RedisCluster::_score_command(Cmd cmd, Args &&... args) { + return _score_command(typename IsKvPairIter::type(), + cmd, + std::forward(args)...); +} + +} + +} + +#endif // end SEWENEW_REDISPLUSPLUS_REDIS_CLUSTER_HPP diff --git a/controller/thirdparty/redis-plus-plus-1.1.1/src/sw/redis++/reply.cpp b/controller/thirdparty/redis-plus-plus-1.1.1/src/sw/redis++/reply.cpp new file mode 100644 index 000000000..dc70d30cc --- /dev/null +++ b/controller/thirdparty/redis-plus-plus-1.1.1/src/sw/redis++/reply.cpp @@ -0,0 +1,150 @@ +/************************************************************************** + Copyright (c) 2017 sewenew + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. + *************************************************************************/ + +#include "reply.h" + +namespace sw { + +namespace redis { + +namespace reply { + +std::string to_status(redisReply &reply) { + if (!reply::is_status(reply)) { + throw ProtoError("Expect STATUS reply"); + } + + if (reply.str == nullptr) { + throw ProtoError("A null status reply"); + } + + // Old version hiredis' *redisReply::len* is of type int. + // So we CANNOT have something like: *return {reply.str, reply.len}*. + return std::string(reply.str, reply.len); +} + +std::string parse(ParseTag, redisReply &reply) { + if (!reply::is_string(reply) && !reply::is_status(reply)) { + throw ProtoError("Expect STRING reply"); + } + + if (reply.str == nullptr) { + throw ProtoError("A null string reply"); + } + + // Old version hiredis' *redisReply::len* is of type int. + // So we CANNOT have something like: *return {reply.str, reply.len}*. + return std::string(reply.str, reply.len); +} + +long long parse(ParseTag, redisReply &reply) { + if (!reply::is_integer(reply)) { + throw ProtoError("Expect INTEGER reply"); + } + + return reply.integer; +} + +double parse(ParseTag, redisReply &reply) { + return std::stod(parse(reply)); +} + +bool parse(ParseTag, redisReply &reply) { + auto ret = parse(reply); + + if (ret == 1) { + return true; + } else if (ret == 0) { + return false; + } else { + throw ProtoError("Invalid bool reply: " + std::to_string(ret)); + } +} + +void parse(ParseTag, redisReply &reply) { + if (!reply::is_status(reply)) { + throw ProtoError("Expect STATUS reply"); + } + + if (reply.str == nullptr) { + throw ProtoError("A null status reply"); + } + + static const std::string OK = "OK"; + + // Old version hiredis' *redisReply::len* is of type int. + // So we have to cast it to an unsigned int. + if (static_cast(reply.len) != OK.size() + || OK.compare(0, OK.size(), reply.str, reply.len) != 0) { + throw ProtoError("NOT ok status reply: " + reply::to_status(reply)); + } +} + +void rewrite_set_reply(redisReply &reply) { + if (is_nil(reply)) { + // Failed to set, and make it a FALSE reply. + reply.type = REDIS_REPLY_INTEGER; + reply.integer = 0; + + return; + } + + // Check if it's a "OK" status reply. + reply::parse(reply); + + assert(is_status(reply) && reply.str != nullptr); + + free(reply.str); + + // Make it a TRUE reply. + reply.type = REDIS_REPLY_INTEGER; + reply.integer = 1; +} + +void rewrite_georadius_reply(redisReply &reply) { + if (is_array(reply) && reply.element == nullptr) { + // Make it a nil reply. + reply.type = REDIS_REPLY_NIL; + } +} + +namespace detail { + +bool is_flat_array(redisReply &reply) { + assert(reply::is_array(reply)); + + // Empty array reply. + if (reply.element == nullptr || reply.elements == 0) { + return false; + } + + auto *sub_reply = reply.element[0]; + + // Null element. + if (sub_reply == nullptr) { + return false; + } + + return !reply::is_array(*sub_reply); +} + +} + +} + +} + +} diff --git a/controller/thirdparty/redis-plus-plus-1.1.1/src/sw/redis++/reply.h b/controller/thirdparty/redis-plus-plus-1.1.1/src/sw/redis++/reply.h new file mode 100644 index 000000000..b309de5bb --- /dev/null +++ b/controller/thirdparty/redis-plus-plus-1.1.1/src/sw/redis++/reply.h @@ -0,0 +1,363 @@ +/************************************************************************** + Copyright (c) 2017 sewenew + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. + *************************************************************************/ + +#ifndef SEWENEW_REDISPLUSPLUS_REPLY_H +#define SEWENEW_REDISPLUSPLUS_REPLY_H + +#include +#include +#include +#include +#include +#include +#include "errors.h" +#include "utils.h" + +namespace sw { + +namespace redis { + +struct ReplyDeleter { + void operator()(redisReply *reply) const { + if (reply != nullptr) { + freeReplyObject(reply); + } + } +}; + +using ReplyUPtr = std::unique_ptr; + +namespace reply { + +template +struct ParseTag {}; + +template +inline T parse(redisReply &reply) { + return parse(ParseTag(), reply); +} + +void parse(ParseTag, redisReply &reply); + +std::string parse(ParseTag, redisReply &reply); + +long long parse(ParseTag, redisReply &reply); + +double parse(ParseTag, redisReply &reply); + +bool parse(ParseTag, redisReply &reply); + +template +Optional parse(ParseTag>, redisReply &reply); + +template +std::pair parse(ParseTag>, redisReply &reply); + +template +std::tuple parse(ParseTag>, redisReply &reply); + +template ::value, int>::type = 0> +T parse(ParseTag, redisReply &reply); + +template ::value, int>::type = 0> +T parse(ParseTag, redisReply &reply); + +template +long long parse_scan_reply(redisReply &reply, Output output); + +inline bool is_error(redisReply &reply) { + return reply.type == REDIS_REPLY_ERROR; +} + +inline bool is_nil(redisReply &reply) { + return reply.type == REDIS_REPLY_NIL; +} + +inline bool is_string(redisReply &reply) { + return reply.type == REDIS_REPLY_STRING; +} + +inline bool is_status(redisReply &reply) { + return reply.type == REDIS_REPLY_STATUS; +} + +inline bool is_integer(redisReply &reply) { + return reply.type == REDIS_REPLY_INTEGER; +} + +inline bool is_array(redisReply &reply) { + return reply.type == REDIS_REPLY_ARRAY; +} + +std::string to_status(redisReply &reply); + +template +void to_array(redisReply &reply, Output output); + +// Rewrite set reply to bool type +void rewrite_set_reply(redisReply &reply); + +// Rewrite georadius reply to OptionalLongLong type +void rewrite_georadius_reply(redisReply &reply); + +template +auto parse_xpending_reply(redisReply &reply, Output output) + -> std::tuple; + +} + +// Inline implementations. + +namespace reply { + +namespace detail { + +template +void to_array(redisReply &reply, Output output) { + if (!is_array(reply)) { + throw ProtoError("Expect ARRAY reply"); + } + + if (reply.element == nullptr) { + // Empty array. + return; + } + + for (std::size_t idx = 0; idx != reply.elements; ++idx) { + auto *sub_reply = reply.element[idx]; + if (sub_reply == nullptr) { + throw ProtoError("Null array element reply"); + } + + *output = parse::type>(*sub_reply); + + ++output; + } +} + +bool is_flat_array(redisReply &reply); + +template +void to_flat_array(redisReply &reply, Output output) { + if (reply.element == nullptr) { + // Empty array. + return; + } + + if (reply.elements % 2 != 0) { + throw ProtoError("Not string pair array reply"); + } + + for (std::size_t idx = 0; idx != reply.elements; idx += 2) { + auto *key_reply = reply.element[idx]; + auto *val_reply = reply.element[idx + 1]; + if (key_reply == nullptr || val_reply == nullptr) { + throw ProtoError("Null string array reply"); + } + + using Pair = typename IterType::type; + using FirstType = typename std::decay::type; + using SecondType = typename std::decay::type; + *output = std::make_pair(parse(*key_reply), + parse(*val_reply)); + + ++output; + } +} + +template +void to_array(std::true_type, redisReply &reply, Output output) { + if (is_flat_array(reply)) { + to_flat_array(reply, output); + } else { + to_array(reply, output); + } +} + +template +void to_array(std::false_type, redisReply &reply, Output output) { + to_array(reply, output); +} + +template +std::tuple parse_tuple(redisReply **reply, std::size_t idx) { + assert(reply != nullptr); + + auto *sub_reply = reply[idx]; + if (sub_reply == nullptr) { + throw ProtoError("Null reply"); + } + + return std::make_tuple(parse(*sub_reply)); +} + +template +auto parse_tuple(redisReply **reply, std::size_t idx) -> + typename std::enable_if>::type { + assert(reply != nullptr); + + return std::tuple_cat(parse_tuple(reply, idx), + parse_tuple(reply, idx + 1)); +} + +} + +template +Optional parse(ParseTag>, redisReply &reply) { + if (reply::is_nil(reply)) { + return {}; + } + + return Optional(parse(reply)); +} + +template +std::pair parse(ParseTag>, redisReply &reply) { + if (!is_array(reply)) { + throw ProtoError("Expect ARRAY reply"); + } + + if (reply.elements != 2) { + throw ProtoError("NOT key-value PAIR reply"); + } + + if (reply.element == nullptr) { + throw ProtoError("Null PAIR reply"); + } + + auto *first = reply.element[0]; + auto *second = reply.element[1]; + if (first == nullptr || second == nullptr) { + throw ProtoError("Null pair reply"); + } + + return std::make_pair(parse::type>(*first), + parse::type>(*second)); +} + +template +std::tuple parse(ParseTag>, redisReply &reply) { + constexpr auto size = sizeof...(Args); + + static_assert(size > 0, "DO NOT support parsing tuple with 0 element"); + + if (!is_array(reply)) { + throw ProtoError("Expect ARRAY reply"); + } + + if (reply.elements != size) { + throw ProtoError("Expect tuple reply with " + std::to_string(size) + "elements"); + } + + if (reply.element == nullptr) { + throw ProtoError("Null TUPLE reply"); + } + + return detail::parse_tuple(reply.element, 0); +} + +template ::value, int>::type> +T parse(ParseTag, redisReply &reply) { + if (!is_array(reply)) { + throw ProtoError("Expect ARRAY reply"); + } + + T container; + + to_array(reply, std::back_inserter(container)); + + return container; +} + +template ::value, int>::type> +T parse(ParseTag, redisReply &reply) { + if (!is_array(reply)) { + throw ProtoError("Expect ARRAY reply"); + } + + T container; + + to_array(reply, std::inserter(container, container.end())); + + return container; +} + +template +long long parse_scan_reply(redisReply &reply, Output output) { + if (reply.elements != 2 || reply.element == nullptr) { + throw ProtoError("Invalid scan reply"); + } + + auto *cursor_reply = reply.element[0]; + auto *data_reply = reply.element[1]; + if (cursor_reply == nullptr || data_reply == nullptr) { + throw ProtoError("Invalid cursor reply or data reply"); + } + + auto cursor_str = reply::parse(*cursor_reply); + auto new_cursor = 0; + try { + new_cursor = std::stoll(cursor_str); + } catch (const std::exception &e) { + throw ProtoError("Invalid cursor reply: " + cursor_str); + } + + reply::to_array(*data_reply, output); + + return new_cursor; +} + +template +void to_array(redisReply &reply, Output output) { + if (!is_array(reply)) { + throw ProtoError("Expect ARRAY reply"); + } + + detail::to_array(typename IsKvPairIter::type(), reply, output); +} + +template +auto parse_xpending_reply(redisReply &reply, Output output) + -> std::tuple { + if (!is_array(reply) || reply.elements != 4) { + throw ProtoError("expect array reply with 4 elements"); + } + + for (std::size_t idx = 0; idx != reply.elements; ++idx) { + if (reply.element[idx] == nullptr) { + throw ProtoError("null array reply"); + } + } + + auto num = parse(*(reply.element[0])); + auto start = parse(*(reply.element[1])); + auto end = parse(*(reply.element[2])); + + auto &entry_reply = *(reply.element[3]); + if (!is_nil(entry_reply)) { + to_array(entry_reply, output); + } + + return std::make_tuple(num, std::move(start), std::move(end)); +} + +} + +} + +} + +#endif // end SEWENEW_REDISPLUSPLUS_REPLY_H diff --git a/controller/thirdparty/redis-plus-plus-1.1.1/src/sw/redis++/sentinel.cpp b/controller/thirdparty/redis-plus-plus-1.1.1/src/sw/redis++/sentinel.cpp new file mode 100644 index 000000000..a7d88a181 --- /dev/null +++ b/controller/thirdparty/redis-plus-plus-1.1.1/src/sw/redis++/sentinel.cpp @@ -0,0 +1,361 @@ +/************************************************************************** + Copyright (c) 2017 sewenew + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. + *************************************************************************/ + +#include "sentinel.h" +#include +#include +#include +#include +#include "redis.h" +#include "errors.h" + +namespace sw { + +namespace redis { + +class Sentinel::Iterator { +public: + Iterator(std::list &healthy_sentinels, + std::list &broken_sentinels) : + _healthy_sentinels(healthy_sentinels), + _broken_sentinels(broken_sentinels) { + reset(); + } + + Connection& next(); + + void reset(); + +private: + std::list &_healthy_sentinels; + + std::size_t _healthy_size = 0; + + std::list &_broken_sentinels; + + std::size_t _broken_size = 0; +}; + +Connection& Sentinel::Iterator::next() { + while (_healthy_size > 0) { + assert(_healthy_sentinels.size() >= _healthy_size); + + --_healthy_size; + + auto &connection = _healthy_sentinels.front(); + if (connection.broken()) { + _broken_sentinels.push_front(connection.options()); + ++_broken_size; + + _healthy_sentinels.pop_front(); + } else { + _healthy_sentinels.splice(_healthy_sentinels.end(), + _healthy_sentinels, + _healthy_sentinels.begin()); + + return _healthy_sentinels.back(); + } + } + + while (_broken_size > 0) { + assert(_broken_sentinels.size() >= _broken_size); + + --_broken_size; + + try { + const auto &opt = _broken_sentinels.front(); + Connection connection(opt); + _healthy_sentinels.push_back(std::move(connection)); + + _broken_sentinels.pop_front(); + + return _healthy_sentinels.back(); + } catch (const Error &e) { + // Failed to connect to sentinel. + _broken_sentinels.splice(_broken_sentinels.end(), + _broken_sentinels, + _broken_sentinels.begin()); + } + } + + throw StopIterError(); +} + +void Sentinel::Iterator::reset() { + _healthy_size = _healthy_sentinels.size(); + _broken_size = _broken_sentinels.size(); +} + +Sentinel::Sentinel(const SentinelOptions &sentinel_opts) : + _broken_sentinels(_parse_options(sentinel_opts)), + _sentinel_opts(sentinel_opts) { + if (_sentinel_opts.connect_timeout == std::chrono::milliseconds(0) + || _sentinel_opts.socket_timeout == std::chrono::milliseconds(0)) { + throw Error("With sentinel, connection timeout and socket timeout cannot be 0"); + } +} + +Connection Sentinel::master(const std::string &master_name, const ConnectionOptions &opts) { + std::lock_guard lock(_mutex); + + Iterator iter(_healthy_sentinels, _broken_sentinels); + std::size_t retries = 0; + while (true) { + try { + auto &sentinel = iter.next(); + + auto master = _get_master_addr_by_name(sentinel, master_name); + if (!master) { + // Try the next sentinel. + continue; + } + + auto connection = _connect_redis(*master, opts); + if (_get_role(connection) != Role::MASTER) { + // Retry the whole process at most SentinelOptions::max_retry times. + ++retries; + if (retries > _sentinel_opts.max_retry) { + throw Error("Failed to get master from sentinel"); + } + + std::this_thread::sleep_for(_sentinel_opts.retry_interval); + + // Restart the iteration. + iter.reset(); + continue; + } + + return connection; + } catch (const StopIterError &e) { + throw; + } catch (const Error &e) { + continue; + } + } +} + +Connection Sentinel::slave(const std::string &master_name, const ConnectionOptions &opts) { + std::lock_guard lock(_mutex); + + Iterator iter(_healthy_sentinels, _broken_sentinels); + std::size_t retries = 0; + while (true) { + try { + auto &sentinel = iter.next(); + + auto slaves = _get_slave_addr_by_name(sentinel, master_name); + if (slaves.empty()) { + // Try the next sentinel. + continue; + } + + // Normally slaves list is NOT very large, so there won't be a performance problem. + auto slave_iter = std::find(slaves.begin(), + slaves.end(), + Node{opts.host, opts.port}); + if (slave_iter != slaves.end() && slave_iter != slaves.begin()) { + // The given node is still a valid slave. Try it first. + std::swap(*(slaves.begin()), *slave_iter); + } + + for (const auto &slave : slaves) { + try { + auto connection = _connect_redis(slave, opts); + if (_get_role(connection) != Role::SLAVE) { + // Retry the whole process at most SentinelOptions::max_retry times. + ++retries; + if (retries > _sentinel_opts.max_retry) { + throw Error("Failed to get slave from sentinel"); + } + + std::this_thread::sleep_for(_sentinel_opts.retry_interval); + + // Restart the iteration. + iter.reset(); + break; + } + + return connection; + } catch (const Error &e) { + // Try the next slave. + continue; + } + } + } catch (const StopIterError &e) { + throw; + } catch (const Error &e) { + continue; + } + } +} + +Optional Sentinel::_get_master_addr_by_name(Connection &connection, const StringView &name) { + connection.send("SENTINEL GET-MASTER-ADDR-BY-NAME %b", name.data(), name.size()); + + auto reply = connection.recv(); + + assert(reply); + + auto master = reply::parse>>(*reply); + if (!master) { + return {}; + } + + int port = 0; + try { + port = std::stoi(master->second); + } catch (const std::exception &) { + throw ProtoError("Master port is invalid: " + master->second); + } + + return Optional{Node{master->first, port}}; +} + +std::vector Sentinel::_get_slave_addr_by_name(Connection &connection, + const StringView &name) { + try { + connection.send("SENTINEL SLAVES %b", name.data(), name.size()); + + auto reply = connection.recv(); + + assert(reply); + + auto slaves = _parse_slave_info(*reply); + + // Make slave list random. + std::mt19937 gen(std::random_device{}()); + std::shuffle(slaves.begin(), slaves.end(), gen); + + return slaves; + } catch (const ReplyError &e) { + // Unknown master name. + return {}; + } +} + +std::vector Sentinel::_parse_slave_info(redisReply &reply) const { + using SlaveInfo = std::unordered_map; + + auto slaves = reply::parse>(reply); + + std::vector nodes; + for (const auto &slave : slaves) { + auto flags_iter = slave.find("flags"); + auto ip_iter = slave.find("ip"); + auto port_iter = slave.find("port"); + if (flags_iter == slave.end() || ip_iter == slave.end() || port_iter == slave.end()) { + throw ProtoError("Invalid slave info"); + } + + // This slave is down, e.g. 's_down,slave,disconnected' + if (flags_iter->second != "slave") { + continue; + } + + int port = 0; + try { + port = std::stoi(port_iter->second); + } catch (const std::exception &) { + throw ProtoError("Slave port is invalid: " + port_iter->second); + } + + nodes.push_back(Node{ip_iter->second, port}); + } + + return nodes; +} + +Connection Sentinel::_connect_redis(const Node &node, ConnectionOptions opts) { + opts.host = node.host; + opts.port = node.port; + + return Connection(opts); +} + +Role Sentinel::_get_role(Connection &connection) { + connection.send("INFO REPLICATION"); + auto reply = connection.recv(); + + assert(reply); + auto info = reply::parse(*reply); + + auto start = info.find("role:"); + if (start == std::string::npos) { + throw ProtoError("Invalid INFO REPLICATION reply"); + } + start += 5; + auto stop = info.find("\r\n", start); + if (stop == std::string::npos) { + throw ProtoError("Invalid INFO REPLICATION reply"); + } + + auto role = info.substr(start, stop - start); + if (role == "master") { + return Role::MASTER; + } else if (role == "slave") { + return Role::SLAVE; + } else { + throw Error("Invalid role: " + role); + } +} + +std::list Sentinel::_parse_options(const SentinelOptions &opts) const { + std::list options; + for (const auto &node : opts.nodes) { + ConnectionOptions opt; + opt.host = node.first; + opt.port = node.second; + opt.password = opts.password; + opt.keep_alive = opts.keep_alive; + opt.connect_timeout = opts.connect_timeout; + opt.socket_timeout = opts.socket_timeout; + + options.push_back(opt); + } + + return options; +} + +SimpleSentinel::SimpleSentinel(const std::shared_ptr &sentinel, + const std::string &master_name, + Role role) : + _sentinel(sentinel), + _master_name(master_name), + _role(role) { + if (!_sentinel) { + throw Error("Sentinel cannot be null"); + } + + if (_role != Role::MASTER && _role != Role::SLAVE) { + throw Error("Role must be Role::MASTER or Role::SLAVE"); + } +} + +Connection SimpleSentinel::create(const ConnectionOptions &opts) { + assert(_sentinel); + + if (_role == Role::MASTER) { + return _sentinel->master(_master_name, opts); + } + + assert(_role == Role::SLAVE); + + return _sentinel->slave(_master_name, opts); +} + +} + +} diff --git a/controller/thirdparty/redis-plus-plus-1.1.1/src/sw/redis++/sentinel.h b/controller/thirdparty/redis-plus-plus-1.1.1/src/sw/redis++/sentinel.h new file mode 100644 index 000000000..e80d1e56a --- /dev/null +++ b/controller/thirdparty/redis-plus-plus-1.1.1/src/sw/redis++/sentinel.h @@ -0,0 +1,138 @@ +/************************************************************************** + Copyright (c) 2017 sewenew + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. + *************************************************************************/ + +#ifndef SEWENEW_REDISPLUSPLUS_SENTINEL_H +#define SEWENEW_REDISPLUSPLUS_SENTINEL_H + +#include +#include +#include +#include +#include +#include "connection.h" +#include "shards.h" +#include "reply.h" + +namespace sw { + +namespace redis { + +struct SentinelOptions { + std::vector> nodes; + + std::string password; + + bool keep_alive = true; + + std::chrono::milliseconds connect_timeout{100}; + + std::chrono::milliseconds socket_timeout{100}; + + std::chrono::milliseconds retry_interval{100}; + + std::size_t max_retry = 2; +}; + +class Sentinel { +public: + explicit Sentinel(const SentinelOptions &sentinel_opts); + + Sentinel(const Sentinel &) = delete; + Sentinel& operator=(const Sentinel &) = delete; + + Sentinel(Sentinel &&) = delete; + Sentinel& operator=(Sentinel &&) = delete; + + ~Sentinel() = default; + +private: + Connection master(const std::string &master_name, const ConnectionOptions &opts); + + Connection slave(const std::string &master_name, const ConnectionOptions &opts); + + class Iterator; + + friend class SimpleSentinel; + + std::list _parse_options(const SentinelOptions &opts) const; + + Optional _get_master_addr_by_name(Connection &connection, const StringView &name); + + std::vector _get_slave_addr_by_name(Connection &connection, const StringView &name); + + Connection _connect_redis(const Node &node, ConnectionOptions opts); + + Role _get_role(Connection &connection); + + std::vector _parse_slave_info(redisReply &reply) const; + + std::list _healthy_sentinels; + + std::list _broken_sentinels; + + SentinelOptions _sentinel_opts; + + std::mutex _mutex; +}; + +class SimpleSentinel { +public: + SimpleSentinel(const std::shared_ptr &sentinel, + const std::string &master_name, + Role role); + + SimpleSentinel() = default; + + SimpleSentinel(const SimpleSentinel &) = default; + SimpleSentinel& operator=(const SimpleSentinel &) = default; + + SimpleSentinel(SimpleSentinel &&) = default; + SimpleSentinel& operator=(SimpleSentinel &&) = default; + + ~SimpleSentinel() = default; + + explicit operator bool() const { + return bool(_sentinel); + } + + Connection create(const ConnectionOptions &opts); + +private: + std::shared_ptr _sentinel; + + std::string _master_name; + + Role _role = Role::MASTER; +}; + +class StopIterError : public Error { +public: + StopIterError() : Error("StopIterError") {} + + StopIterError(const StopIterError &) = default; + StopIterError& operator=(const StopIterError &) = default; + + StopIterError(StopIterError &&) = default; + StopIterError& operator=(StopIterError &&) = default; + + virtual ~StopIterError() = default; +}; + +} + +} + +#endif // end SEWENEW_REDISPLUSPLUS_SENTINEL_H diff --git a/controller/thirdparty/redis-plus-plus-1.1.1/src/sw/redis++/shards.cpp b/controller/thirdparty/redis-plus-plus-1.1.1/src/sw/redis++/shards.cpp new file mode 100644 index 000000000..e06d2a732 --- /dev/null +++ b/controller/thirdparty/redis-plus-plus-1.1.1/src/sw/redis++/shards.cpp @@ -0,0 +1,50 @@ +/************************************************************************** + Copyright (c) 2017 sewenew + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. + *************************************************************************/ + +#include "shards.h" + +namespace sw { + +namespace redis { + +RedirectionError::RedirectionError(const std::string &msg): ReplyError(msg) { + std::tie(_slot, _node) = _parse_error(msg); +} + +std::pair RedirectionError::_parse_error(const std::string &msg) const { + // "slot ip:port" + auto space_pos = msg.find(" "); + auto colon_pos = msg.find(":"); + if (space_pos == std::string::npos + || colon_pos == std::string::npos + || colon_pos < space_pos) { + throw ProtoError("Invalid ASK error message: " + msg); + } + + try { + auto slot = std::stoull(msg.substr(0, space_pos)); + auto host = msg.substr(space_pos + 1, colon_pos - space_pos - 1); + auto port = std::stoi(msg.substr(colon_pos + 1)); + + return {slot, {host, port}}; + } catch (const std::exception &e) { + throw ProtoError("Invalid ASK error message: " + msg); + } +} + +} + +} diff --git a/controller/thirdparty/redis-plus-plus-1.1.1/src/sw/redis++/shards.h b/controller/thirdparty/redis-plus-plus-1.1.1/src/sw/redis++/shards.h new file mode 100644 index 000000000..a0593acbc --- /dev/null +++ b/controller/thirdparty/redis-plus-plus-1.1.1/src/sw/redis++/shards.h @@ -0,0 +1,115 @@ +/************************************************************************** + Copyright (c) 2017 sewenew + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. + *************************************************************************/ + +#ifndef SEWENEW_REDISPLUSPLUS_SHARDS_H +#define SEWENEW_REDISPLUSPLUS_SHARDS_H + +#include +#include +#include "errors.h" + +namespace sw { + +namespace redis { + +using Slot = std::size_t; + +struct SlotRange { + Slot min; + Slot max; +}; + +inline bool operator<(const SlotRange &lhs, const SlotRange &rhs) { + return lhs.max < rhs.max; +} + +struct Node { + std::string host; + int port; +}; + +inline bool operator==(const Node &lhs, const Node &rhs) { + return lhs.host == rhs.host && lhs.port == rhs.port; +} + +struct NodeHash { + std::size_t operator()(const Node &node) const noexcept { + auto host_hash = std::hash{}(node.host); + auto port_hash = std::hash{}(node.port); + return host_hash ^ (port_hash << 1); + } +}; + +using Shards = std::map; + +class RedirectionError : public ReplyError { +public: + RedirectionError(const std::string &msg); + + RedirectionError(const RedirectionError &) = default; + RedirectionError& operator=(const RedirectionError &) = default; + + RedirectionError(RedirectionError &&) = default; + RedirectionError& operator=(RedirectionError &&) = default; + + virtual ~RedirectionError() = default; + + Slot slot() const { + return _slot; + } + + const Node& node() const { + return _node; + } + +private: + std::pair _parse_error(const std::string &msg) const; + + Slot _slot = 0; + Node _node; +}; + +class MovedError : public RedirectionError { +public: + explicit MovedError(const std::string &msg) : RedirectionError(msg) {} + + MovedError(const MovedError &) = default; + MovedError& operator=(const MovedError &) = default; + + MovedError(MovedError &&) = default; + MovedError& operator=(MovedError &&) = default; + + virtual ~MovedError() = default; +}; + +class AskError : public RedirectionError { +public: + explicit AskError(const std::string &msg) : RedirectionError(msg) {} + + AskError(const AskError &) = default; + AskError& operator=(const AskError &) = default; + + AskError(AskError &&) = default; + AskError& operator=(AskError &&) = default; + + virtual ~AskError() = default; +}; + +} + +} + +#endif // end SEWENEW_REDISPLUSPLUS_SHARDS_H diff --git a/controller/thirdparty/redis-plus-plus-1.1.1/src/sw/redis++/shards_pool.cpp b/controller/thirdparty/redis-plus-plus-1.1.1/src/sw/redis++/shards_pool.cpp new file mode 100644 index 000000000..436cc265c --- /dev/null +++ b/controller/thirdparty/redis-plus-plus-1.1.1/src/sw/redis++/shards_pool.cpp @@ -0,0 +1,319 @@ +/************************************************************************** + Copyright (c) 2017 sewenew + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. + *************************************************************************/ + +#include "shards_pool.h" +#include +#include "errors.h" + +namespace sw { + +namespace redis { + +const std::size_t ShardsPool::SHARDS; + +ShardsPool::ShardsPool(const ConnectionPoolOptions &pool_opts, + const ConnectionOptions &connection_opts) : + _pool_opts(pool_opts), + _connection_opts(connection_opts) { + if (_connection_opts.type != ConnectionType::TCP) { + throw Error("Only support TCP connection for Redis Cluster"); + } + + Connection connection(_connection_opts); + + _shards = _cluster_slots(connection); + + _init_pool(_shards); +} + +ShardsPool::ShardsPool(ShardsPool &&that) { + std::lock_guard lock(that._mutex); + + _move(std::move(that)); +} + +ShardsPool& ShardsPool::operator=(ShardsPool &&that) { + if (this != &that) { + std::lock(_mutex, that._mutex); + std::lock_guard lock_this(_mutex, std::adopt_lock); + std::lock_guard lock_that(that._mutex, std::adopt_lock); + + _move(std::move(that)); + } + + return *this; +} + +GuardedConnection ShardsPool::fetch(const StringView &key) { + auto slot = _slot(key); + + return _fetch(slot); +} + +GuardedConnection ShardsPool::fetch() { + auto slot = _slot(); + + return _fetch(slot); +} + +GuardedConnection ShardsPool::fetch(const Node &node) { + std::lock_guard lock(_mutex); + + auto iter = _pools.find(node); + if (iter == _pools.end()) { + // Node doesn't exist, and it should be a newly created node. + // So add a new connection pool. + iter = _add_node(node); + } + + assert(iter != _pools.end()); + + return GuardedConnection(iter->second); +} + +void ShardsPool::update() { + // My might send command to a removed node. + // Try at most 3 times. + for (auto idx = 0; idx < 3; ++idx) { + try { + // Randomly pick a connection. + auto guarded_connection = fetch(); + auto shards = _cluster_slots(guarded_connection.connection()); + + std::unordered_set nodes; + for (const auto &shard : shards) { + nodes.insert(shard.second); + } + + std::lock_guard lock(_mutex); + + // TODO: If shards is unchanged, no need to update, and return immediately. + + _shards = std::move(shards); + + // Remove non-existent nodes. + for (auto iter = _pools.begin(); iter != _pools.end(); ) { + if (nodes.find(iter->first) == nodes.end()) { + // Node has been removed. + _pools.erase(iter++); + } else { + ++iter; + } + } + + // Add connection pool for new nodes. + // In fact, connections will be created lazily. + for (const auto &node : nodes) { + if (_pools.find(node) == _pools.end()) { + _add_node(node); + } + } + + // Update successfully. + return; + } catch (const Error &) { + // continue; + } + } + + throw Error("Failed to update shards info"); +} + +ConnectionOptions ShardsPool::connection_options(const StringView &key) { + auto slot = _slot(key); + + return _connection_options(slot); +} + +ConnectionOptions ShardsPool::connection_options() { + auto slot = _slot(); + + return _connection_options(slot); +} +void ShardsPool::_move(ShardsPool &&that) { + _pool_opts = that._pool_opts; + _connection_opts = that._connection_opts; + _shards = std::move(that._shards); + _pools = std::move(that._pools); +} + +void ShardsPool::_init_pool(const Shards &shards) { + for (const auto &shard : shards) { + _add_node(shard.second); + } +} + +Shards ShardsPool::_cluster_slots(Connection &connection) const { + auto reply = _cluster_slots_command(connection); + + assert(reply); + + return _parse_reply(*reply); +} + +ReplyUPtr ShardsPool::_cluster_slots_command(Connection &connection) const { + connection.send("CLUSTER SLOTS"); + + return connection.recv(); +} + +Shards ShardsPool::_parse_reply(redisReply &reply) const { + if (!reply::is_array(reply)) { + throw ProtoError("Expect ARRAY reply"); + } + + if (reply.element == nullptr || reply.elements == 0) { + throw Error("Empty slots"); + } + + Shards shards; + for (std::size_t idx = 0; idx != reply.elements; ++idx) { + auto *sub_reply = reply.element[idx]; + if (sub_reply == nullptr) { + throw ProtoError("Null slot info"); + } + + shards.emplace(_parse_slot_info(*sub_reply)); + } + + return shards; +} + +std::pair ShardsPool::_parse_slot_info(redisReply &reply) const { + if (reply.elements < 3 || reply.element == nullptr) { + throw ProtoError("Invalid slot info"); + } + + // Min slot id + auto *min_slot_reply = reply.element[0]; + if (min_slot_reply == nullptr) { + throw ProtoError("Invalid min slot"); + } + std::size_t min_slot = reply::parse(*min_slot_reply); + + // Max slot id + auto *max_slot_reply = reply.element[1]; + if (max_slot_reply == nullptr) { + throw ProtoError("Invalid max slot"); + } + std::size_t max_slot = reply::parse(*max_slot_reply); + + if (min_slot > max_slot) { + throw ProtoError("Invalid slot range"); + } + + // Master node info + auto *node_reply = reply.element[2]; + if (node_reply == nullptr + || !reply::is_array(*node_reply) + || node_reply->element == nullptr + || node_reply->elements < 2) { + throw ProtoError("Invalid node info"); + } + + auto master_host = reply::parse(*(node_reply->element[0])); + int master_port = reply::parse(*(node_reply->element[1])); + + // By now, we ignore node id and other replicas' info. + + return {SlotRange{min_slot, max_slot}, Node{master_host, master_port}}; +} + +Slot ShardsPool::_slot(const StringView &key) const { + // The following code is copied from: https://redis.io/topics/cluster-spec + // And I did some minor changes. + + const auto *k = key.data(); + auto keylen = key.size(); + + // start-end indexes of { and }. + std::size_t s = 0; + std::size_t e = 0; + + // Search the first occurrence of '{'. + for (s = 0; s < keylen; s++) + if (k[s] == '{') break; + + // No '{' ? Hash the whole key. This is the base case. + if (s == keylen) return crc16(k, keylen) & SHARDS; + + // '{' found? Check if we have the corresponding '}'. + for (e = s + 1; e < keylen; e++) + if (k[e] == '}') break; + + // No '}' or nothing between {} ? Hash the whole key. + if (e == keylen || e == s + 1) return crc16(k, keylen) & SHARDS; + + // If we are here there is both a { and a } on its right. Hash + // what is in the middle between { and }. + return crc16(k + s + 1, e - s - 1) & SHARDS; +} + +Slot ShardsPool::_slot() const { + static thread_local std::default_random_engine engine; + + std::uniform_int_distribution uniform_dist(0, SHARDS); + + return uniform_dist(engine); +} + +ConnectionPoolSPtr& ShardsPool::_get_pool(Slot slot) { + auto shards_iter = _shards.lower_bound(SlotRange{slot, slot}); + if (shards_iter == _shards.end() || slot < shards_iter->first.min) { + throw Error("Slot is out of range: " + std::to_string(slot)); + } + + const auto &node = shards_iter->second; + + auto node_iter = _pools.find(node); + if (node_iter == _pools.end()) { + throw Error("Slot is NOT covered: " + std::to_string(slot)); + } + + return node_iter->second; +} + +GuardedConnection ShardsPool::_fetch(Slot slot) { + std::lock_guard lock(_mutex); + + auto &pool = _get_pool(slot); + + assert(pool); + + return GuardedConnection(pool); +} + +ConnectionOptions ShardsPool::_connection_options(Slot slot) { + std::lock_guard lock(_mutex); + + auto &pool = _get_pool(slot); + + assert(pool); + + return pool->connection_options(); +} + +auto ShardsPool::_add_node(const Node &node) -> NodeMap::iterator { + auto opts = _connection_opts; + opts.host = node.host; + opts.port = node.port; + + return _pools.emplace(node, std::make_shared(_pool_opts, opts)).first; +} + +} + +} diff --git a/controller/thirdparty/redis-plus-plus-1.1.1/src/sw/redis++/shards_pool.h b/controller/thirdparty/redis-plus-plus-1.1.1/src/sw/redis++/shards_pool.h new file mode 100644 index 000000000..1184806e9 --- /dev/null +++ b/controller/thirdparty/redis-plus-plus-1.1.1/src/sw/redis++/shards_pool.h @@ -0,0 +1,137 @@ +/************************************************************************** + Copyright (c) 2017 sewenew + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. + *************************************************************************/ + +#ifndef SEWENEW_REDISPLUSPLUS_SHARDS_POOL_H +#define SEWENEW_REDISPLUSPLUS_SHARDS_POOL_H + +#include +#include +#include +#include +#include +#include "reply.h" +#include "connection_pool.h" +#include "shards.h" + +namespace sw { + +namespace redis { + +using ConnectionPoolSPtr = std::shared_ptr; + +class GuardedConnection { +public: + GuardedConnection(const ConnectionPoolSPtr &pool) : _pool(pool), + _connection(_pool->fetch()) { + assert(!_connection.broken()); + } + + GuardedConnection(const GuardedConnection &) = delete; + GuardedConnection& operator=(const GuardedConnection &) = delete; + + GuardedConnection(GuardedConnection &&) = default; + GuardedConnection& operator=(GuardedConnection &&) = default; + + ~GuardedConnection() { + _pool->release(std::move(_connection)); + } + + Connection& connection() { + return _connection; + } + +private: + ConnectionPoolSPtr _pool; + Connection _connection; +}; + +class ShardsPool { +public: + ShardsPool() = default; + + ShardsPool(const ShardsPool &that) = delete; + ShardsPool& operator=(const ShardsPool &that) = delete; + + ShardsPool(ShardsPool &&that); + ShardsPool& operator=(ShardsPool &&that); + + ~ShardsPool() = default; + + ShardsPool(const ConnectionPoolOptions &pool_opts, + const ConnectionOptions &connection_opts); + + // Fetch a connection by key. + GuardedConnection fetch(const StringView &key); + + // Randomly pick a connection. + GuardedConnection fetch(); + + // Fetch a connection by node. + GuardedConnection fetch(const Node &node); + + void update(); + + ConnectionOptions connection_options(const StringView &key); + + ConnectionOptions connection_options(); + +private: + void _move(ShardsPool &&that); + + void _init_pool(const Shards &shards); + + Shards _cluster_slots(Connection &connection) const; + + ReplyUPtr _cluster_slots_command(Connection &connection) const; + + Shards _parse_reply(redisReply &reply) const; + + std::pair _parse_slot_info(redisReply &reply) const; + + // Get slot by key. + std::size_t _slot(const StringView &key) const; + + // Randomly pick a slot. + std::size_t _slot() const; + + ConnectionPoolSPtr& _get_pool(Slot slot); + + GuardedConnection _fetch(Slot slot); + + ConnectionOptions _connection_options(Slot slot); + + using NodeMap = std::unordered_map; + + NodeMap::iterator _add_node(const Node &node); + + ConnectionPoolOptions _pool_opts; + + ConnectionOptions _connection_opts; + + Shards _shards; + + NodeMap _pools; + + std::mutex _mutex; + + static const std::size_t SHARDS = 16383; +}; + +} + +} + +#endif // end SEWENEW_REDISPLUSPLUS_SHARDS_POOL_H diff --git a/controller/thirdparty/redis-plus-plus-1.1.1/src/sw/redis++/subscriber.cpp b/controller/thirdparty/redis-plus-plus-1.1.1/src/sw/redis++/subscriber.cpp new file mode 100644 index 000000000..b699b02f0 --- /dev/null +++ b/controller/thirdparty/redis-plus-plus-1.1.1/src/sw/redis++/subscriber.cpp @@ -0,0 +1,222 @@ +/************************************************************************** + Copyright (c) 2017 sewenew + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. + *************************************************************************/ + +#include "subscriber.h" +#include + +namespace sw { + +namespace redis { + +const Subscriber::TypeIndex Subscriber::_msg_type_index = { + {"message", MsgType::MESSAGE}, + {"pmessage", MsgType::PMESSAGE}, + {"subscribe", MsgType::SUBSCRIBE}, + {"unsubscribe", MsgType::UNSUBSCRIBE}, + {"psubscribe", MsgType::PSUBSCRIBE}, + {"punsubscribe", MsgType::PUNSUBSCRIBE} +}; + +Subscriber::Subscriber(Connection connection) : _connection(std::move(connection)) {} + +void Subscriber::subscribe(const StringView &channel) { + _check_connection(); + + // TODO: cmd::subscribe DOES NOT send the subscribe message to Redis. + // In fact, it puts the command to network buffer. + // So we need a queue to record these sub or unsub commands, and + // ensure that before stopping the subscriber, all these commands + // have really been sent to Redis. + cmd::subscribe(_connection, channel); +} + +void Subscriber::unsubscribe() { + _check_connection(); + + cmd::unsubscribe(_connection); +} + +void Subscriber::unsubscribe(const StringView &channel) { + _check_connection(); + + cmd::unsubscribe(_connection, channel); +} + +void Subscriber::psubscribe(const StringView &pattern) { + _check_connection(); + + cmd::psubscribe(_connection, pattern); +} + +void Subscriber::punsubscribe() { + _check_connection(); + + cmd::punsubscribe(_connection); +} + +void Subscriber::punsubscribe(const StringView &pattern) { + _check_connection(); + + cmd::punsubscribe(_connection, pattern); +} + +void Subscriber::consume() { + _check_connection(); + + ReplyUPtr reply; + try { + reply = _connection.recv(); + } catch (const TimeoutError &) { + _connection.reset(); + throw; + } + + assert(reply); + + if (!reply::is_array(*reply) || reply->elements < 1 || reply->element == nullptr) { + throw ProtoError("Invalid subscribe message"); + } + + auto type = _msg_type(reply->element[0]); + switch (type) { + case MsgType::MESSAGE: + _handle_message(*reply); + break; + + case MsgType::PMESSAGE: + _handle_pmessage(*reply); + break; + + case MsgType::SUBSCRIBE: + case MsgType::UNSUBSCRIBE: + case MsgType::PSUBSCRIBE: + case MsgType::PUNSUBSCRIBE: + _handle_meta(type, *reply); + break; + + default: + assert(false); + } +} + +Subscriber::MsgType Subscriber::_msg_type(redisReply *reply) const { + if (reply == nullptr) { + throw ProtoError("Null type reply."); + } + + auto type = reply::parse(*reply); + + auto iter = _msg_type_index.find(type); + if (iter == _msg_type_index.end()) { + throw ProtoError("Invalid message type."); + } + + return iter->second; +} + +void Subscriber::_check_connection() { + if (_connection.broken()) { + throw Error("Connection is broken"); + } +} + +void Subscriber::_handle_message(redisReply &reply) { + if (_msg_callback == nullptr) { + return; + } + + if (reply.elements != 3) { + throw ProtoError("Expect 3 sub replies"); + } + + assert(reply.element != nullptr); + + auto *channel_reply = reply.element[1]; + if (channel_reply == nullptr) { + throw ProtoError("Null channel reply"); + } + auto channel = reply::parse(*channel_reply); + + auto *msg_reply = reply.element[2]; + if (msg_reply == nullptr) { + throw ProtoError("Null message reply"); + } + auto msg = reply::parse(*msg_reply); + + _msg_callback(std::move(channel), std::move(msg)); +} + +void Subscriber::_handle_pmessage(redisReply &reply) { + if (_pmsg_callback == nullptr) { + return; + } + + if (reply.elements != 4) { + throw ProtoError("Expect 4 sub replies"); + } + + assert(reply.element != nullptr); + + auto *pattern_reply = reply.element[1]; + if (pattern_reply == nullptr) { + throw ProtoError("Null pattern reply"); + } + auto pattern = reply::parse(*pattern_reply); + + auto *channel_reply = reply.element[2]; + if (channel_reply == nullptr) { + throw ProtoError("Null channel reply"); + } + auto channel = reply::parse(*channel_reply); + + auto *msg_reply = reply.element[3]; + if (msg_reply == nullptr) { + throw ProtoError("Null message reply"); + } + auto msg = reply::parse(*msg_reply); + + _pmsg_callback(std::move(pattern), std::move(channel), std::move(msg)); +} + +void Subscriber::_handle_meta(MsgType type, redisReply &reply) { + if (_meta_callback == nullptr) { + return; + } + + if (reply.elements != 3) { + throw ProtoError("Expect 3 sub replies"); + } + + assert(reply.element != nullptr); + + auto *channel_reply = reply.element[1]; + if (channel_reply == nullptr) { + throw ProtoError("Null channel reply"); + } + auto channel = reply::parse(*channel_reply); + + auto *num_reply = reply.element[2]; + if (num_reply == nullptr) { + throw ProtoError("Null num reply"); + } + auto num = reply::parse(*num_reply); + + _meta_callback(type, std::move(channel), num); +} + +} + +} diff --git a/controller/thirdparty/redis-plus-plus-1.1.1/src/sw/redis++/subscriber.h b/controller/thirdparty/redis-plus-plus-1.1.1/src/sw/redis++/subscriber.h new file mode 100644 index 000000000..8b7c5cfb4 --- /dev/null +++ b/controller/thirdparty/redis-plus-plus-1.1.1/src/sw/redis++/subscriber.h @@ -0,0 +1,231 @@ +/************************************************************************** + Copyright (c) 2017 sewenew + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. + *************************************************************************/ + +#ifndef SEWENEW_REDISPLUSPLUS_SUBSCRIBER_H +#define SEWENEW_REDISPLUSPLUS_SUBSCRIBER_H + +#include +#include +#include +#include "connection.h" +#include "reply.h" +#include "command.h" +#include "utils.h" + +namespace sw { + +namespace redis { + +// @NOTE: Subscriber is NOT thread-safe. +// Subscriber uses callbacks to handle messages. There are 6 kinds of messages: +// 1) MESSAGE: message sent to a channel. +// 2) PMESSAGE: message sent to channels of a given pattern. +// 3) SUBSCRIBE: meta message sent when we successfully subscribe to a channel. +// 4) UNSUBSCRIBE: meta message sent when we successfully unsubscribe to a channel. +// 5) PSUBSCRIBE: meta message sent when we successfully subscribe to a channel pattern. +// 6) PUNSUBSCRIBE: meta message sent when we successfully unsubscribe to a channel pattern. +// +// Use Subscriber::on_message(MsgCallback) to set the callback function for message of +// *MESSAGE* type, and the callback interface is: +// void (std::string channel, std::string msg) +// +// Use Subscriber::on_pmessage(PatternMsgCallback) to set the callback function for message of +// *PMESSAGE* type, and the callback interface is: +// void (std::string pattern, std::string channel, std::string msg) +// +// Messages of other types are called *META MESSAGE*, they have the same callback interface. +// Use Subscriber::on_meta(MetaCallback) to set the callback function: +// void (Subscriber::MsgType type, OptionalString channel, long long num) +// +// NOTE: If we haven't subscribe/psubscribe to any channel/pattern, and try to +// unsubscribe/punsubscribe without any parameter, i.e. unsubscribe/punsubscribe all +// channels/patterns, *channel* will be null. So the second parameter of meta callback +// is of type *OptionalString*. +// +// All these callback interfaces pass std::string by value, and you can take their ownership +// (i.e. std::move) safely. +// +// If you don't set callback for a specific kind of message, Subscriber::consume() will +// receive the message, and ignore it, i.e. no callback will be called. +class Subscriber { +public: + Subscriber(const Subscriber &) = delete; + Subscriber& operator=(const Subscriber &) = delete; + + Subscriber(Subscriber &&) = default; + Subscriber& operator=(Subscriber &&) = default; + + ~Subscriber() = default; + + enum class MsgType { + SUBSCRIBE, + UNSUBSCRIBE, + PSUBSCRIBE, + PUNSUBSCRIBE, + MESSAGE, + PMESSAGE + }; + + template + void on_message(MsgCb msg_callback); + + template + void on_pmessage(PMsgCb pmsg_callback); + + template + void on_meta(MetaCb meta_callback); + + void subscribe(const StringView &channel); + + template + void subscribe(Input first, Input last); + + template + void subscribe(std::initializer_list channels) { + subscribe(channels.begin(), channels.end()); + } + + void unsubscribe(); + + void unsubscribe(const StringView &channel); + + template + void unsubscribe(Input first, Input last); + + template + void unsubscribe(std::initializer_list channels) { + unsubscribe(channels.begin(), channels.end()); + } + + void psubscribe(const StringView &pattern); + + template + void psubscribe(Input first, Input last); + + template + void psubscribe(std::initializer_list channels) { + psubscribe(channels.begin(), channels.end()); + } + + void punsubscribe(); + + void punsubscribe(const StringView &channel); + + template + void punsubscribe(Input first, Input last); + + template + void punsubscribe(std::initializer_list channels) { + punsubscribe(channels.begin(), channels.end()); + } + + void consume(); + +private: + friend class Redis; + + friend class RedisCluster; + + explicit Subscriber(Connection connection); + + MsgType _msg_type(redisReply *reply) const; + + void _check_connection(); + + void _handle_message(redisReply &reply); + + void _handle_pmessage(redisReply &reply); + + void _handle_meta(MsgType type, redisReply &reply); + + using MsgCallback = std::function; + + using PatternMsgCallback = std::function; + + using MetaCallback = std::function; + + using TypeIndex = std::unordered_map; + static const TypeIndex _msg_type_index; + + Connection _connection; + + MsgCallback _msg_callback = nullptr; + + PatternMsgCallback _pmsg_callback = nullptr; + + MetaCallback _meta_callback = nullptr; +}; + +template +void Subscriber::on_message(MsgCb msg_callback) { + _msg_callback = msg_callback; +} + +template +void Subscriber::on_pmessage(PMsgCb pmsg_callback) { + _pmsg_callback = pmsg_callback; +} + +template +void Subscriber::on_meta(MetaCb meta_callback) { + _meta_callback = meta_callback; +} + +template +void Subscriber::subscribe(Input first, Input last) { + if (first == last) { + return; + } + + _check_connection(); + + cmd::subscribe_range(_connection, first, last); +} + +template +void Subscriber::unsubscribe(Input first, Input last) { + _check_connection(); + + cmd::unsubscribe_range(_connection, first, last); +} + +template +void Subscriber::psubscribe(Input first, Input last) { + if (first == last) { + return; + } + + _check_connection(); + + cmd::psubscribe_range(_connection, first, last); +} + +template +void Subscriber::punsubscribe(Input first, Input last) { + _check_connection(); + + cmd::punsubscribe_range(_connection, first, last); +} + +} + +} + +#endif // end SEWENEW_REDISPLUSPLUS_SUBSCRIBER_H diff --git a/controller/thirdparty/redis-plus-plus-1.1.1/src/sw/redis++/transaction.cpp b/controller/thirdparty/redis-plus-plus-1.1.1/src/sw/redis++/transaction.cpp new file mode 100644 index 000000000..faa1bd178 --- /dev/null +++ b/controller/thirdparty/redis-plus-plus-1.1.1/src/sw/redis++/transaction.cpp @@ -0,0 +1,123 @@ +/************************************************************************** + Copyright (c) 2017 sewenew + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. + *************************************************************************/ + +#include "transaction.h" +#include "command.h" + +namespace sw { + +namespace redis { + +std::vector TransactionImpl::exec(Connection &connection, std::size_t cmd_num) { + _close_transaction(); + + _get_queued_replies(connection, cmd_num); + + return _exec(connection); +} + +void TransactionImpl::discard(Connection &connection, std::size_t cmd_num) { + _close_transaction(); + + _get_queued_replies(connection, cmd_num); + + _discard(connection); +} + +void TransactionImpl::_open_transaction(Connection &connection) { + assert(!_in_transaction); + + cmd::multi(connection); + auto reply = connection.recv(); + auto status = reply::to_status(*reply); + if (status != "OK") { + throw Error("Failed to open transaction: " + status); + } + + _in_transaction = true; +} + +void TransactionImpl::_close_transaction() { + if (!_in_transaction) { + throw Error("No command in transaction"); + } + + _in_transaction = false; +} + +void TransactionImpl::_get_queued_reply(Connection &connection) { + auto reply = connection.recv(); + auto status = reply::to_status(*reply); + if (status != "QUEUED") { + throw Error("Invalid QUEUED reply: " + status); + } +} + +void TransactionImpl::_get_queued_replies(Connection &connection, std::size_t cmd_num) { + if (_piped) { + // Get all QUEUED reply + while (cmd_num > 0) { + _get_queued_reply(connection); + + --cmd_num; + } + } +} + +std::vector TransactionImpl::_exec(Connection &connection) { + cmd::exec(connection); + + auto reply = connection.recv(); + + if (reply::is_nil(*reply)) { + // Execution has been aborted, i.e. watched key has been modified. + throw WatchError(); + } + + if (!reply::is_array(*reply)) { + throw ProtoError("Expect ARRAY reply"); + } + + if (reply->element == nullptr || reply->elements == 0) { + // Since we don't allow EXEC without any command, this ARRAY reply + // should NOT be null or empty. + throw ProtoError("Null ARRAY reply"); + } + + std::vector replies; + for (std::size_t idx = 0; idx != reply->elements; ++idx) { + auto *sub_reply = reply->element[idx]; + if (sub_reply == nullptr) { + throw ProtoError("Null sub reply"); + } + + auto r = ReplyUPtr(sub_reply); + reply->element[idx] = nullptr; + replies.push_back(std::move(r)); + } + + return replies; +} + +void TransactionImpl::_discard(Connection &connection) { + cmd::discard(connection); + auto reply = connection.recv(); + reply::parse(*reply); +} + +} + +} diff --git a/controller/thirdparty/redis-plus-plus-1.1.1/src/sw/redis++/transaction.h b/controller/thirdparty/redis-plus-plus-1.1.1/src/sw/redis++/transaction.h new file mode 100644 index 000000000..f19f24889 --- /dev/null +++ b/controller/thirdparty/redis-plus-plus-1.1.1/src/sw/redis++/transaction.h @@ -0,0 +1,77 @@ +/************************************************************************** + Copyright (c) 2017 sewenew + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. + *************************************************************************/ + +#ifndef SEWENEW_REDISPLUSPLUS_TRANSACTION_H +#define SEWENEW_REDISPLUSPLUS_TRANSACTION_H + +#include +#include +#include "connection.h" +#include "errors.h" + +namespace sw { + +namespace redis { + +class TransactionImpl { +public: + explicit TransactionImpl(bool piped) : _piped(piped) {} + + template + void command(Connection &connection, Cmd cmd, Args &&...args); + + std::vector exec(Connection &connection, std::size_t cmd_num); + + void discard(Connection &connection, std::size_t cmd_num); + +private: + void _open_transaction(Connection &connection); + + void _close_transaction(); + + void _get_queued_reply(Connection &connection); + + void _get_queued_replies(Connection &connection, std::size_t cmd_num); + + std::vector _exec(Connection &connection); + + void _discard(Connection &connection); + + bool _in_transaction = false; + + bool _piped; +}; + +template +void TransactionImpl::command(Connection &connection, Cmd cmd, Args &&...args) { + assert(!connection.broken()); + + if (!_in_transaction) { + _open_transaction(connection); + } + + cmd(connection, std::forward(args)...); + + if (!_piped) { + _get_queued_reply(connection); + } +} + +} + +} + +#endif // end SEWENEW_REDISPLUSPLUS_TRANSACTION_H diff --git a/controller/thirdparty/redis-plus-plus-1.1.1/src/sw/redis++/utils.h b/controller/thirdparty/redis-plus-plus-1.1.1/src/sw/redis++/utils.h new file mode 100644 index 000000000..e29e64e14 --- /dev/null +++ b/controller/thirdparty/redis-plus-plus-1.1.1/src/sw/redis++/utils.h @@ -0,0 +1,269 @@ +/************************************************************************** + Copyright (c) 2017 sewenew + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. + *************************************************************************/ + +#ifndef SEWENEW_REDISPLUSPLUS_UTILS_H +#define SEWENEW_REDISPLUSPLUS_UTILS_H + +#include +#include +#include + +namespace sw { + +namespace redis { + +// By now, not all compilers support std::string_view, +// so we make our own implementation. +class StringView { +public: + constexpr StringView() noexcept = default; + + constexpr StringView(const char *data, std::size_t size) : _data(data), _size(size) {} + + StringView(const char *data) : _data(data), _size(std::strlen(data)) {} + + StringView(const std::string &str) : _data(str.data()), _size(str.size()) {} + + constexpr StringView(const StringView &) noexcept = default; + + StringView& operator=(const StringView &) noexcept = default; + + constexpr const char* data() const noexcept { + return _data; + } + + constexpr std::size_t size() const noexcept { + return _size; + } + +private: + const char *_data = nullptr; + std::size_t _size = 0; +}; + +template +class Optional { +public: + Optional() = default; + + Optional(const Optional &) = default; + Optional& operator=(const Optional &) = default; + + Optional(Optional &&) = default; + Optional& operator=(Optional &&) = default; + + ~Optional() = default; + + template + explicit Optional(Args &&...args) : _value(true, T(std::forward(args)...)) {} + + explicit operator bool() const { + return _value.first; + } + + T& value() { + return _value.second; + } + + const T& value() const { + return _value.second; + } + + T* operator->() { + return &(_value.second); + } + + const T* operator->() const { + return &(_value.second); + } + + T& operator*() { + return _value.second; + } + + const T& operator*() const { + return _value.second; + } + +private: + std::pair _value; +}; + +using OptionalString = Optional; + +using OptionalLongLong = Optional; + +using OptionalDouble = Optional; + +using OptionalStringPair = Optional>; + +template +struct IsKvPair : std::false_type {}; + +template +struct IsKvPair> : std::true_type {}; + +template +using Void = void; + +template > +struct IsInserter : std::false_type {}; + +template +//struct IsInserter> : std::true_type {}; +struct IsInserter::value>::type> + : std::true_type {}; + +template > +struct IterType { + using type = typename std::iterator_traits::value_type; +}; + +template +//struct IterType> { +struct IterType::value>::type> { + typename std::enable_if::value>::type> { + using type = typename std::decay::type; +}; + +template > +struct IsIter : std::false_type {}; + +template +struct IsIter::value>::type> : std::true_type {}; + +template +//struct IsIter::iterator_category>> +struct IsIter::value_type>::value>::type> + : std::integral_constant::value> {}; + +template +struct IsKvPairIter : IsKvPair::type> {}; + +template +struct TupleWithType : std::false_type {}; + +template +struct TupleWithType> : std::false_type {}; + +template +struct TupleWithType> : TupleWithType> {}; + +template +struct TupleWithType> : std::true_type {}; + +template +struct IndexSequence {}; + +template +struct MakeIndexSequence : MakeIndexSequence {}; + +template +struct MakeIndexSequence<0, Is...> : IndexSequence {}; + +// NthType and NthValue are taken from +// https://stackoverflow.com/questions/14261183 +template +struct NthType {}; + +template +struct NthType<0, Arg, Args...> { + using type = Arg; +}; + +template +struct NthType { + using type = typename NthType::type; +}; + +template +struct LastType { + using type = typename NthType::type; +}; + +struct InvalidLastType {}; + +template <> +struct LastType<> { + using type = InvalidLastType; +}; + +template +auto NthValue(Arg &&arg, Args &&...) + -> typename std::enable_if<(I == 0), decltype(std::forward(arg))>::type { + return std::forward(arg); +} + +template +auto NthValue(Arg &&, Args &&...args) + -> typename std::enable_if<(I > 0), + decltype(std::forward::type>( + std::declval::type>()))>::type { + return std::forward::type>( + NthValue(std::forward(args)...)); +} + +template +auto LastValue(Args &&...args) + -> decltype(std::forward::type>( + std::declval::type>())) { + return std::forward::type>( + NthValue(std::forward(args)...)); +} + +template > +struct HasPushBack : std::false_type {}; + +template +struct HasPushBack().push_back(std::declval()) + )>::value>::type> : std::true_type {}; + +template > +struct HasInsert : std::false_type {}; + +template +struct HasInsert().insert(std::declval(), + std::declval())), + typename T::iterator>::value>::type> : std::true_type {}; + +template +struct IsSequenceContainer + : std::integral_constant::value + && !std::is_same::type, std::string>::value> {}; + +template +struct IsAssociativeContainer + : std::integral_constant::value && !HasPushBack::value> {}; + +uint16_t crc16(const char *buf, int len); + +} + +} + +#endif // end SEWENEW_REDISPLUSPLUS_UTILS_H diff --git a/controller/thirdparty/redis-plus-plus-1.1.1/test/CMakeLists.txt b/controller/thirdparty/redis-plus-plus-1.1.1/test/CMakeLists.txt new file mode 100644 index 000000000..861e6ff88 --- /dev/null +++ b/controller/thirdparty/redis-plus-plus-1.1.1/test/CMakeLists.txt @@ -0,0 +1,33 @@ +project(test_redis++) + +if (${CMAKE_SYSTEM_NAME} MATCHES "Darwin") + cmake_minimum_required(VERSION 3.0.0) +else() + cmake_minimum_required(VERSION 2.8.0) +endif() + +set(PROJECT_SOURCE_DIR ${PROJECT_SOURCE_DIR}/src/sw/redis++) + +file(GLOB PROJECT_SOURCE_FILES "${PROJECT_SOURCE_DIR}/*.cpp") + +add_executable(${PROJECT_NAME} ${PROJECT_SOURCE_FILES}) + +# hiredis dependency +find_path(HIREDIS_HEADER hiredis) +target_include_directories(${PROJECT_NAME} PUBLIC ${HIREDIS_HEADER}) + +find_library(HIREDIS_STATIC_LIB libhiredis.a) +target_link_libraries(${PROJECT_NAME} ${HIREDIS_STATIC_LIB}) + +# redis++ dependency +target_include_directories(${PROJECT_NAME} PUBLIC ../src) +set(REDIS_PLUS_PLUS_LIB ${CMAKE_CURRENT_BINARY_DIR}/../lib/libredis++.a) + +## solaris socket dependency +IF (CMAKE_SYSTEM_NAME MATCHES "(Solaris|SunOS)" ) + target_link_libraries(${PROJECT_NAME} -lsocket) +ENDIF(CMAKE_SYSTEM_NAME MATCHES "(Solaris|SunOS)" ) + +find_package(Threads REQUIRED) + +target_link_libraries(${PROJECT_NAME} ${REDIS_PLUS_PLUS_LIB} ${CMAKE_THREAD_LIBS_INIT}) diff --git a/controller/thirdparty/redis-plus-plus-1.1.1/test/src/sw/redis++/benchmark_test.h b/controller/thirdparty/redis-plus-plus-1.1.1/test/src/sw/redis++/benchmark_test.h new file mode 100644 index 000000000..309bc6863 --- /dev/null +++ b/controller/thirdparty/redis-plus-plus-1.1.1/test/src/sw/redis++/benchmark_test.h @@ -0,0 +1,83 @@ +/************************************************************************** + Copyright (c) 2017 sewenew + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. + *************************************************************************/ + +#ifndef SEWENEW_REDISPLUSPLUS_TEST_BENCHMARK_TEST_H +#define SEWENEW_REDISPLUSPLUS_TEST_BENCHMARK_TEST_H + +#include + +namespace sw { + +namespace redis { + +namespace test { + +struct BenchmarkOptions { + std::size_t pool_size = 5; + std::size_t thread_num = 10; + std::size_t total_request_num = 100000; + std::size_t key_len = 10; + std::size_t val_len = 10; +}; + +template +class BenchmarkTest { +public: + BenchmarkTest(const BenchmarkOptions &opts, RedisInstance &instance); + + ~BenchmarkTest() { + _cleanup(); + } + + void run(); + +private: + template + void _run(const std::string &title, Func &&func); + + template + std::size_t _run(Func &&func, std::size_t request_num); + + void _test_get(); + + std::vector _gen_keys() const; + + std::string _gen_value() const; + + void _cleanup(); + + const std::string& _key(std::size_t idx) const { + return _keys[idx % _keys.size()]; + } + + BenchmarkOptions _opts; + + RedisInstance &_redis; + + std::vector _keys; + + std::string _value; +}; + +} + +} + +} + +#include "benchmark_test.hpp" + +#endif // end SEWENEW_REDISPLUSPLUS_TEST_BENCHMARK_TEST_H diff --git a/controller/thirdparty/redis-plus-plus-1.1.1/test/src/sw/redis++/benchmark_test.hpp b/controller/thirdparty/redis-plus-plus-1.1.1/test/src/sw/redis++/benchmark_test.hpp new file mode 100644 index 000000000..eaf50ec50 --- /dev/null +++ b/controller/thirdparty/redis-plus-plus-1.1.1/test/src/sw/redis++/benchmark_test.hpp @@ -0,0 +1,178 @@ +/************************************************************************** + Copyright (c) 2017 sewenew + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. + *************************************************************************/ + +#ifndef SEWENEW_REDISPLUSPLUS_TEST_BENCHMARK_TEST_HPP +#define SEWENEW_REDISPLUSPLUS_TEST_BENCHMARK_TEST_HPP + +#include +#include +#include +#include +#include "utils.h" + +namespace sw { + +namespace redis { + +namespace test { + +template +BenchmarkTest::BenchmarkTest(const BenchmarkOptions &opts, + RedisInstance &instance) : _opts(opts), _redis(instance) { + REDIS_ASSERT(_opts.pool_size > 0 + && _opts.thread_num > 0 + && _opts.total_request_num > 0 + && _opts.key_len > 0 + && _opts.val_len > 0, + "Invalid benchmark test options."); + + _keys = _gen_keys(); + _value = _gen_value(); +} + +template +void BenchmarkTest::run() { + _cleanup(); + + _run("SET key value", [this](std::size_t idx) { this->_redis.set(this->_key(idx), _value); }); + + _run("GET key", [this](std::size_t idx) { + auto res = this->_redis.get(this->_key(idx)); + (void)res; + }); + + _cleanup(); + + _run("LPUSH key value", [this](std::size_t idx) { + this->_redis.lpush(this->_key(idx), _value); + }); + + _run("LRANGE key 0 10", [this](std::size_t idx) { + std::vector res; + res.reserve(10); + this->_redis.lrange(this->_key(idx), 0, 10, std::back_inserter(res)); + }); + + _run("LPOP key", [this](std::size_t idx) { + auto res = this->_redis.lpop(this->_key(idx)); + (void)res; + }); + + _cleanup(); + + _run("INCR key", [this](std::size_t idx) { + auto num = this->_redis.incr(this->_key(idx)); + (void)num; + }); + + _cleanup(); + + _run("SADD key member", [this](std::size_t idx) { + auto num = this->_redis.sadd(this->_key(idx), _value); + (void)num; + }); + + _run("SPOP key", [this](std::size_t idx) { + auto res = this->_redis.spop(this->_key(idx)); + (void)res; + }); + + _cleanup(); +} + +template +template +void BenchmarkTest::_run(const std::string &title, Func &&func) { + auto thread_num = _opts.thread_num; + auto requests_per_thread = _opts.total_request_num / thread_num; + auto total_request_num = requests_per_thread * thread_num; + std::vector> res; + res.reserve(thread_num); + res.push_back(std::async(std::launch::async, + [this](Func &&func, std::size_t request_num) { + return this->_run(std::forward(func), request_num); + }, + std::forward(func), + requests_per_thread)); + + auto total_in_msec = 0; + for (auto &fut : res) { + total_in_msec += fut.get(); + } + + auto total_in_sec = total_in_msec * 1.0 / 1000; + + auto avg = total_in_msec * 1.0 / total_request_num; + + auto ops = static_cast(1000 / avg); + + std::cout << "-----" << title << "-----" << std::endl; + std::cout << total_request_num << " requests cost " << total_in_sec << " seconds" << std::endl; + std::cout << ops << " requests per second" << std::endl; +} + +template +template +std::size_t BenchmarkTest::_run(Func &&func, std::size_t request_num) { + auto start = std::chrono::steady_clock::now(); + + for (auto idx = 0U; idx != request_num; ++idx) { + func(idx); + } + + auto stop = std::chrono::steady_clock::now(); + + return std::chrono::duration_cast(stop - start).count(); +} + +template +std::vector BenchmarkTest::_gen_keys() const { + const auto KEY_NUM = 100; + std::vector res; + res.reserve(KEY_NUM); + std::default_random_engine engine(std::random_device{}()); + std::uniform_int_distribution uniform_dist(0, 255); + for (auto i = 0; i != KEY_NUM; ++i) { + std::string str; + str.reserve(_opts.key_len); + for (std::size_t j = 0; j != _opts.key_len; ++j) { + str.push_back(static_cast(uniform_dist(engine))); + } + res.push_back(str); + } + + return res; +} + +template +std::string BenchmarkTest::_gen_value() const { + return std::string(_opts.val_len, 'x'); +} + +template +void BenchmarkTest::_cleanup() { + for (const auto &key : _keys) { + _redis.del(key); + } +} + +} + +} + +} + +#endif // end SEWENEW_REDISPLUSPLUS_TEST_BENCHMARK_TEST_HPP diff --git a/controller/thirdparty/redis-plus-plus-1.1.1/test/src/sw/redis++/connection_cmds_test.h b/controller/thirdparty/redis-plus-plus-1.1.1/test/src/sw/redis++/connection_cmds_test.h new file mode 100644 index 000000000..2127257c3 --- /dev/null +++ b/controller/thirdparty/redis-plus-plus-1.1.1/test/src/sw/redis++/connection_cmds_test.h @@ -0,0 +1,49 @@ +/************************************************************************** + Copyright (c) 2017 sewenew + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. + *************************************************************************/ + +#ifndef SEWENEW_REDISPLUSPLUS_TEST_CONNECTION_CMDS_TEST_H +#define SEWENEW_REDISPLUSPLUS_TEST_CONNECTION_CMDS_TEST_H + +#include + +namespace sw { + +namespace redis { + +namespace test { + +template +class ConnectionCmdTest { +public: + explicit ConnectionCmdTest(RedisInstance &instance) : _redis(instance) {} + + void run(); + +private: + void _run(Redis &redis); + + RedisInstance &_redis; +}; + +} + +} + +} + +#include "connection_cmds_test.hpp" + +#endif // end SEWENEW_REDISPLUSPLUS_TEST_CONNECTION_CMDS_TEST_H diff --git a/controller/thirdparty/redis-plus-plus-1.1.1/test/src/sw/redis++/connection_cmds_test.hpp b/controller/thirdparty/redis-plus-plus-1.1.1/test/src/sw/redis++/connection_cmds_test.hpp new file mode 100644 index 000000000..90e7c313b --- /dev/null +++ b/controller/thirdparty/redis-plus-plus-1.1.1/test/src/sw/redis++/connection_cmds_test.hpp @@ -0,0 +1,50 @@ +/************************************************************************** + Copyright (c) 2017 sewenew + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. + *************************************************************************/ + +#ifndef SEWENEW_REDISPLUSPLUS_TEST_CONNECTION_CMDS_TEST_HPP +#define SEWENEW_REDISPLUSPLUS_TEST_CONNECTION_CMDS_TEST_HPP + +#include "utils.h" + +namespace sw { + +namespace redis { + +namespace test { + +template +void ConnectionCmdTest::run() { + cluster_specializing_test(*this, &ConnectionCmdTest::_run, _redis); +} + +template +void ConnectionCmdTest::_run(Redis &instance) { + auto message = std::string("hello"); + + REDIS_ASSERT(instance.echo(message) == message, "failed to test echo"); + + REDIS_ASSERT(instance.ping() == "PONG", "failed to test ping"); + + REDIS_ASSERT(instance.ping(message) == message, "failed to test ping"); +} + +} + +} + +} + +#endif // end SEWENEW_REDISPLUSPLUS_TEST_CONNECTION_CMDS_TEST_HPP diff --git a/controller/thirdparty/redis-plus-plus-1.1.1/test/src/sw/redis++/geo_cmds_test.h b/controller/thirdparty/redis-plus-plus-1.1.1/test/src/sw/redis++/geo_cmds_test.h new file mode 100644 index 000000000..41b9795fa --- /dev/null +++ b/controller/thirdparty/redis-plus-plus-1.1.1/test/src/sw/redis++/geo_cmds_test.h @@ -0,0 +1,47 @@ +/************************************************************************** + Copyright (c) 2017 sewenew + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. + *************************************************************************/ + +#ifndef SEWENEW_REDISPLUSPLUS_TEST_GEO_CMDS_TEST_H +#define SEWENEW_REDISPLUSPLUS_TEST_GEO_CMDS_TEST_H + +#include + +namespace sw { + +namespace redis { + +namespace test { + +template +class GeoCmdTest { +public: + explicit GeoCmdTest(RedisInstance &instance) : _redis(instance) {} + + void run(); + +private: + RedisInstance &_redis; +}; + +} + +} + +} + +#include "geo_cmds_test.hpp" + +#endif // end SEWENEW_REDISPLUSPLUS_TEST_GEO_CMDS_TEST_H diff --git a/controller/thirdparty/redis-plus-plus-1.1.1/test/src/sw/redis++/geo_cmds_test.hpp b/controller/thirdparty/redis-plus-plus-1.1.1/test/src/sw/redis++/geo_cmds_test.hpp new file mode 100644 index 000000000..866fafe10 --- /dev/null +++ b/controller/thirdparty/redis-plus-plus-1.1.1/test/src/sw/redis++/geo_cmds_test.hpp @@ -0,0 +1,149 @@ +/************************************************************************** + Copyright (c) 2017 sewenew + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. + *************************************************************************/ + +#ifndef SEWENEW_REDISPLUSPLUS_TEST_GEO_CMDS_TEST_HPP +#define SEWENEW_REDISPLUSPLUS_TEST_GEO_CMDS_TEST_HPP + +#include +#include +#include "utils.h" + +namespace sw { + +namespace redis { + +namespace test { + +template +void GeoCmdTest::run() { + auto key = test_key("geo"); + auto dest = test_key("dest"); + + KeyDeleter deleter(_redis, {key, dest}); + + auto members = { + std::make_tuple("m1", 10.0, 11.0), + std::make_tuple("m2", 10.1, 11.1), + std::make_tuple("m3", 10.2, 11.2) + }; + + REDIS_ASSERT(_redis.geoadd(key, std::make_tuple("m1", 10.0, 11.0)) == 1, + "failed to test geoadd"); + REDIS_ASSERT(_redis.geoadd(key, members) == 2, "failed to test geoadd"); + + auto dist = _redis.geodist(key, "m1", "m4", GeoUnit::KM); + REDIS_ASSERT(!dist, "failed to test geodist with nonexistent member"); + + std::vector hashes; + _redis.geohash(key, {"m1", "m4"}, std::back_inserter(hashes)); + REDIS_ASSERT(hashes.size() == 2, "failed to test geohash"); + REDIS_ASSERT(bool(hashes[0]) && *(hashes[0]) == "s1zned3z8u0" && !(hashes[1]), + "failed to test geohash"); + hashes.clear(); + _redis.geohash(key, {"m4"}, std::back_inserter(hashes)); + REDIS_ASSERT(hashes.size() == 1 && !(hashes[0]), "failed to test geohash"); + + std::vector>> pos; + _redis.geopos(key, {"m4"}, std::back_inserter(pos)); + REDIS_ASSERT(pos.size() == 1 && !(pos[0]), "failed to test geopos"); + + auto num = _redis.georadius(key, + std::make_pair(10.1, 11.1), + 100, + GeoUnit::KM, + dest, + false, + 10); + REDIS_ASSERT(bool(num) && *num == 3, "failed to test georadius with store option"); + + std::vector mems; + _redis.georadius(key, + std::make_pair(10.1, 11.1), + 100, + GeoUnit::KM, + 10, + true, + std::back_inserter(mems)); + REDIS_ASSERT(mems.size() == 3, "failed to test georadius with no option"); + + std::vector> with_dist; + _redis.georadius(key, + std::make_pair(10.1, 11.1), + 100, + GeoUnit::KM, + 10, + true, + std::back_inserter(with_dist)); + REDIS_ASSERT(with_dist.size() == 3, "failed to test georadius with dist"); + + std::vector>> with_dist_coord; + _redis.georadius(key, + std::make_pair(10.1, 11.1), + 100, + GeoUnit::KM, + 10, + true, + std::back_inserter(with_dist_coord)); + REDIS_ASSERT(with_dist_coord.size() == 3, "failed to test georadius with dist and coord"); + + num = _redis.georadiusbymember(key, + "m1", + 100, + GeoUnit::KM, + dest, + false, + 10); + REDIS_ASSERT(bool(num) && *num == 3, "failed to test georadiusbymember with store option"); + + mems.clear(); + _redis.georadiusbymember(key, + "m1", + 100, + GeoUnit::KM, + 10, + true, + std::back_inserter(mems)); + REDIS_ASSERT(mems.size() == 3, "failed to test georadiusbymember with no option"); + + with_dist.clear(); + _redis.georadiusbymember(key, + "m1", + 100, + GeoUnit::KM, + 10, + true, + std::back_inserter(with_dist)); + REDIS_ASSERT(with_dist.size() == 3, "failed to test georadiusbymember with dist"); + + with_dist_coord.clear(); + _redis.georadiusbymember(key, + "m1", + 100, + GeoUnit::KM, + 10, + true, + std::back_inserter(with_dist_coord)); + REDIS_ASSERT(with_dist_coord.size() == 3, + "failed to test georadiusbymember with dist and coord"); +} + +} + +} + +} + +#endif // end SEWENEW_REDISPLUSPLUS_TEST_GEO_CMDS_TEST_HPP diff --git a/controller/thirdparty/redis-plus-plus-1.1.1/test/src/sw/redis++/hash_cmds_test.h b/controller/thirdparty/redis-plus-plus-1.1.1/test/src/sw/redis++/hash_cmds_test.h new file mode 100644 index 000000000..60791c8b9 --- /dev/null +++ b/controller/thirdparty/redis-plus-plus-1.1.1/test/src/sw/redis++/hash_cmds_test.h @@ -0,0 +1,55 @@ +/************************************************************************** + Copyright (c) 2017 sewenew + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. + *************************************************************************/ + +#ifndef SEWENEW_REDISPLUSPLUS_TEST_HASH_CMDS_TEST_H +#define SEWENEW_REDISPLUSPLUS_TEST_HASH_CMDS_TEST_H + +#include + +namespace sw { + +namespace redis { + +namespace test { + +template +class HashCmdTest { +public: + explicit HashCmdTest(RedisInstance &instance) : _redis(instance) {} + + void run(); + +private: + void _test_hash(); + + void _test_hash_batch(); + + void _test_numeric(); + + void _test_hscan(); + + RedisInstance &_redis; +}; + +} + +} + +} + +#include "hash_cmds_test.hpp" + +#endif // end SEWENEW_REDISPLUSPLUS_TEST_HASH_CMDS_TEST_H diff --git a/controller/thirdparty/redis-plus-plus-1.1.1/test/src/sw/redis++/hash_cmds_test.hpp b/controller/thirdparty/redis-plus-plus-1.1.1/test/src/sw/redis++/hash_cmds_test.hpp new file mode 100644 index 000000000..42c309f4e --- /dev/null +++ b/controller/thirdparty/redis-plus-plus-1.1.1/test/src/sw/redis++/hash_cmds_test.hpp @@ -0,0 +1,177 @@ +/************************************************************************** + Copyright (c) 2017 sewenew + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. + *************************************************************************/ + +#ifndef SEWENEW_REDISPLUSPLUS_TEST_HASH_CMDS_TEST_HPP +#define SEWENEW_REDISPLUSPLUS_TEST_HASH_CMDS_TEST_HPP + +#include +#include "utils.h" + +namespace sw { + +namespace redis { + +namespace test { + +template +void HashCmdTest::run() { + _test_hash(); + + _test_hash_batch(); + + _test_numeric(); + + _test_hscan(); +} + +template +void HashCmdTest::_test_hash() { + auto key = test_key("hash"); + + KeyDeleter deleter(_redis, key); + + auto f1 = std::string("f1"); + auto v1 = std::string("v1"); + auto f2 = std::string("f2"); + auto v2 = std::string("v2"); + auto f3 = std::string("f3"); + auto v3 = std::string("v3"); + + REDIS_ASSERT(_redis.hset(key, f1, v1), "failed to test hset"); + REDIS_ASSERT(!_redis.hset(key, f1, v2), "failed to test hset with exist field"); + + auto res = _redis.hget(key, f1); + REDIS_ASSERT(res && *res == v2, "failed to test hget"); + + REDIS_ASSERT(_redis.hsetnx(key, f2, v1), "failed to test hsetnx"); + REDIS_ASSERT(!_redis.hsetnx(key, f2, v2), "failed to test hsetnx with exist field"); + + res = _redis.hget(key, f2); + REDIS_ASSERT(res && *res == v1, "failed to test hget"); + + REDIS_ASSERT(!_redis.hexists(key, f3), "failed to test hexists"); + REDIS_ASSERT(_redis.hset(key, std::make_pair(f3, v3)), "failed to test hset"); + REDIS_ASSERT(_redis.hexists(key, f3), "failed to test hexists"); + + REDIS_ASSERT(_redis.hlen(key) == 3, "failed to test hlen"); + REDIS_ASSERT(_redis.hstrlen(key, f1) == static_cast(v1.size()), + "failed to test hstrlen"); + + REDIS_ASSERT(_redis.hdel(key, f1) == 1, "failed to test hdel"); + REDIS_ASSERT(_redis.hdel(key, {f1, f2, f3}) == 2, "failed to test hdel range"); +} + +template +void HashCmdTest::_test_hash_batch() { + auto key = test_key("hash"); + + KeyDeleter deleter(_redis, key); + + auto f1 = std::string("f1"); + auto v1 = std::string("v1"); + auto f2 = std::string("f2"); + auto v2 = std::string("v2"); + auto f3 = std::string("f3"); + + _redis.hmset(key, {std::make_pair(f1, v1), + std::make_pair(f2, v2)}); + + std::vector fields; + _redis.hkeys(key, std::back_inserter(fields)); + REDIS_ASSERT(fields.size() == 2, "failed to test hkeys"); + + std::vector vals; + _redis.hvals(key, std::back_inserter(vals)); + REDIS_ASSERT(vals.size() == 2, "failed to test hvals"); + + std::unordered_map items; + _redis.hgetall(key, std::inserter(items, items.end())); + REDIS_ASSERT(items.size() == 2 && items[f1] == v1 && items[f2] == v2, + "failed to test hgetall"); + + std::vector> item_vec; + _redis.hgetall(key, std::back_inserter(item_vec)); + REDIS_ASSERT(item_vec.size() == 2, "failed to test hgetall"); + + std::vector res; + _redis.hmget(key, {f1, f2, f3}, std::back_inserter(res)); + REDIS_ASSERT(res.size() == 3 + && bool(res[0]) && *(res[0]) == v1 + && bool(res[1]) && *(res[1]) == v2 + && !res[2], + "failed to test hmget"); +} + +template +void HashCmdTest::_test_numeric() { + auto key = test_key("numeric"); + + KeyDeleter deleter(_redis, key); + + auto field = "field"; + + REDIS_ASSERT(_redis.hincrby(key, field, 1) == 1, "failed to test hincrby"); + REDIS_ASSERT(_redis.hincrby(key, field, -1) == 0, "failed to test hincrby"); + REDIS_ASSERT(_redis.hincrbyfloat(key, field, 1.5) == 1.5, "failed to test hincrbyfloat"); +} + +template +void HashCmdTest::_test_hscan() { + auto key = test_key("hscan"); + + KeyDeleter deleter(_redis, key); + + auto items = std::unordered_map{ + std::make_pair("f1", "v1"), + std::make_pair("f2", "v2"), + std::make_pair("f3", "v3"), + }; + + _redis.hmset(key, items.begin(), items.end()); + + std::unordered_map item_map; + auto cursor = 0; + while (true) { + cursor = _redis.hscan(key, cursor, "f*", 2, std::inserter(item_map, item_map.end())); + if (cursor == 0) { + break; + } + } + + REDIS_ASSERT(item_map == items, "failed to test hscan with pattern and count"); + + std::vector> item_vec; + cursor = 0; + while (true) { + cursor = _redis.hscan(key, cursor, std::back_inserter(item_vec)); + if (cursor == 0) { + break; + } + } + + REDIS_ASSERT(item_vec.size() == items.size(), "failed to test hscan"); + for (const auto &ele : item_vec) { + REDIS_ASSERT(items.find(ele.first) != items.end(), "failed to test hscan"); + } +} + +} + +} + +} + +#endif // end SEWENEW_REDISPLUSPLUS_TEST_HASH_CMDS_TEST_HPP diff --git a/controller/thirdparty/redis-plus-plus-1.1.1/test/src/sw/redis++/hyperloglog_cmds_test.h b/controller/thirdparty/redis-plus-plus-1.1.1/test/src/sw/redis++/hyperloglog_cmds_test.h new file mode 100644 index 000000000..50e454b23 --- /dev/null +++ b/controller/thirdparty/redis-plus-plus-1.1.1/test/src/sw/redis++/hyperloglog_cmds_test.h @@ -0,0 +1,47 @@ +/************************************************************************** + Copyright (c) 2017 sewenew + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. + *************************************************************************/ + +#ifndef SEWENEW_REDISPLUSPLUS_TEST_HYPERLOGLOG_CMDS_TEST_H +#define SEWENEW_REDISPLUSPLUS_TEST_HYPERLOGLOG_CMDS_TEST_H + +#include + +namespace sw { + +namespace redis { + +namespace test { + +template +class HyperloglogCmdTest { +public: + explicit HyperloglogCmdTest(RedisInstance &instance) : _redis(instance) {} + + void run(); + +private: + RedisInstance &_redis; +}; + +} + +} + +} + +#include "hyperloglog_cmds_test.hpp" + +#endif // end SEWENEW_REDISPLUSPLUS_TEST_HYPERLOGLOG_CMDS_TEST_H diff --git a/controller/thirdparty/redis-plus-plus-1.1.1/test/src/sw/redis++/hyperloglog_cmds_test.hpp b/controller/thirdparty/redis-plus-plus-1.1.1/test/src/sw/redis++/hyperloglog_cmds_test.hpp new file mode 100644 index 000000000..09775255f --- /dev/null +++ b/controller/thirdparty/redis-plus-plus-1.1.1/test/src/sw/redis++/hyperloglog_cmds_test.hpp @@ -0,0 +1,67 @@ +/************************************************************************** + Copyright (c) 2017 sewenew + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. + *************************************************************************/ + +#ifndef SEWENEW_REDISPLUSPLUS_TEST_HYPERLOGLOG_CMDS_TEST_HPP +#define SEWENEW_REDISPLUSPLUS_TEST_HYPERLOGLOG_CMDS_TEST_HPP + +#include "utils.h" + +namespace sw { + +namespace redis { + +namespace test { + +template +void HyperloglogCmdTest::run() { + auto k1 = test_key("k1"); + auto k2 = test_key("k2"); + auto k3 = test_key("k3"); + + KeyDeleter deleter(_redis, {k1, k2, k3}); + + _redis.pfadd(k1, "a"); + auto members1 = {"b", "c", "d", "e", "f", "g"}; + _redis.pfadd(k1, members1); + + auto cnt = _redis.pfcount(k1); + auto err = cnt * 1.0 / (1 + members1.size()); + REDIS_ASSERT(err < 1.02 && err > 0.98, "failed to test pfadd and pfcount"); + + auto members2 = {"a", "b", "c", "h", "i", "j", "k"}; + _redis.pfadd(k2, members2); + auto total = 1 + members1.size() + members2.size() - 3; + + cnt = _redis.pfcount({k1, k2}); + err = cnt * 1.0 / total; + REDIS_ASSERT(err < 1.02 && err > 0.98, "failed to test pfcount"); + + _redis.pfmerge(k3, {k1, k2}); + cnt = _redis.pfcount(k3); + err = cnt * 1.0 / total; + REDIS_ASSERT(err < 1.02 && err > 0.98, "failed to test pfcount"); + + _redis.pfmerge(k3, k1); + REDIS_ASSERT(cnt == _redis.pfcount(k3), "failed to test pfmerge"); +} + +} + +} + +} + +#endif // end SEWENEW_REDISPLUSPLUS_TEST_HYPERLOGLOG_CMDS_TEST_HPP diff --git a/controller/thirdparty/redis-plus-plus-1.1.1/test/src/sw/redis++/keys_cmds_test.h b/controller/thirdparty/redis-plus-plus-1.1.1/test/src/sw/redis++/keys_cmds_test.h new file mode 100644 index 000000000..54d25ca28 --- /dev/null +++ b/controller/thirdparty/redis-plus-plus-1.1.1/test/src/sw/redis++/keys_cmds_test.h @@ -0,0 +1,55 @@ +/************************************************************************** + Copyright (c) 2017 sewenew + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. + *************************************************************************/ + +#ifndef SEWENEW_REDISPLUSPLUS_TEST_KEYS_CMDS_TEST_H +#define SEWENEW_REDISPLUSPLUS_TEST_KEYS_CMDS_TEST_H + +#include + +namespace sw { + +namespace redis { + +namespace test { + +template +class KeysCmdTest { +public: + explicit KeysCmdTest(RedisInstance &instance) : _redis(instance) {} + + void run(); + +private: + void _test_key(); + + void _test_randomkey(Redis &instance); + + void _test_ttl(); + + void _test_scan(Redis &instance); + + RedisInstance &_redis; +}; + +} + +} + +} + +#include "keys_cmds_test.hpp" + +#endif // end SEWENEW_REDISPLUSPLUS_TEST_KEYS_CMDS_TEST_H diff --git a/controller/thirdparty/redis-plus-plus-1.1.1/test/src/sw/redis++/keys_cmds_test.hpp b/controller/thirdparty/redis-plus-plus-1.1.1/test/src/sw/redis++/keys_cmds_test.hpp new file mode 100644 index 000000000..de21f8d18 --- /dev/null +++ b/controller/thirdparty/redis-plus-plus-1.1.1/test/src/sw/redis++/keys_cmds_test.hpp @@ -0,0 +1,166 @@ +/************************************************************************** + Copyright (c) 2017 sewenew + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. + *************************************************************************/ + +#ifndef SEWENEW_REDISPLUSPLUS_TEST_KEYS_CMDS_TEST_HPP +#define SEWENEW_REDISPLUSPLUS_TEST_KEYS_CMDS_TEST_HPP + +#include +#include +#include "utils.h" + +namespace sw { + +namespace redis { + +namespace test { + +template +void KeysCmdTest::run() { + _test_key(); + + cluster_specializing_test(*this, &KeysCmdTest::_test_randomkey, _redis); + + _test_ttl(); + + cluster_specializing_test(*this, &KeysCmdTest::_test_scan, _redis); +} + +template +void KeysCmdTest::_test_key() { + auto key = test_key("key"); + auto dest = test_key("dest"); + auto new_key_name = test_key("new-key"); + auto not_exist_key = test_key("not-exist"); + + KeyDeleter deleter(_redis, {key, dest, new_key_name}); + + REDIS_ASSERT(_redis.exists(key) == 0, "failed to test exists"); + + auto val = std::string("val"); + _redis.set(key, val); + + REDIS_ASSERT(_redis.exists({key, not_exist_key}) == 1, "failed to test exists"); + + auto new_val = _redis.dump(key); + REDIS_ASSERT(bool(new_val), "failed to test dump"); + + _redis.restore(dest, *new_val, std::chrono::seconds(1000)); + + new_val = _redis.get(dest); + REDIS_ASSERT(bool(new_val) && *new_val == val, "failed to test dump and restore"); + + _redis.rename(dest, new_key_name); + + bool not_exist = false; + try { + _redis.rename(not_exist_key, new_key_name); + } catch (const Error &e) { + not_exist = true; + } + REDIS_ASSERT(not_exist, "failed to test rename with nonexistent key"); + + REDIS_ASSERT(_redis.renamenx(new_key_name, dest), "failed to test renamenx"); + + REDIS_ASSERT(_redis.touch(not_exist_key) == 0, "failed to test touch"); + REDIS_ASSERT(_redis.touch({key, dest, new_key_name}) == 2, "failed to test touch"); + + REDIS_ASSERT(_redis.type(key) == "string", "failed to test type"); + + REDIS_ASSERT(_redis.del({new_key_name, dest}) == 1, "failed to test del"); + REDIS_ASSERT(_redis.unlink({new_key_name, key}) == 1, "failed to test unlink"); +} + +template +void KeysCmdTest::_test_randomkey(Redis &instance) { + auto key = test_key("randomkey"); + + KeyDeleter deleter(instance, key); + + instance.set(key, "value"); + + auto rand_key = instance.randomkey(); + REDIS_ASSERT(bool(rand_key), "failed to test randomkey"); +} + +template +void KeysCmdTest::_test_ttl() { + using namespace std::chrono; + + auto key = test_key("ttl"); + + KeyDeleter deleter(_redis, key); + + _redis.set(key, "val", seconds(100)); + auto ttl = _redis.ttl(key); + REDIS_ASSERT(ttl > 0 && ttl <= 100, "failed to test ttl"); + + REDIS_ASSERT(_redis.persist(key), "failed to test persist"); + ttl = _redis.ttl(key); + REDIS_ASSERT(ttl == -1, "failed to test ttl"); + + REDIS_ASSERT(_redis.expire(key, seconds(100)), + "failed to test expire"); + + auto tp = time_point_cast(system_clock::now() + seconds(100)); + REDIS_ASSERT(_redis.expireat(key, tp), "failed to test expireat"); + ttl = _redis.ttl(key); + REDIS_ASSERT(ttl > 0, "failed to test expireat"); + + REDIS_ASSERT(_redis.pexpire(key, milliseconds(100000)), "failed to test expire"); + + auto pttl = _redis.pttl(key); + REDIS_ASSERT(pttl > 0 && pttl <= 100000, "failed to test pttl"); + + auto tp_milli = time_point_cast(system_clock::now() + milliseconds(100000)); + REDIS_ASSERT(_redis.pexpireat(key, tp_milli), "failed to test pexpireat"); + pttl = _redis.pttl(key); + REDIS_ASSERT(pttl > 0, "failed to test pexpireat"); +} + +template +void KeysCmdTest::_test_scan(Redis &instance) { + std::string key_pattern = "!@#$%^&()_+alseufoawhnlkszd"; + auto k1 = test_key(key_pattern + "k1"); + auto k2 = test_key(key_pattern + "k2"); + auto k3 = test_key(key_pattern + "k3"); + + auto keys = {k1, k2, k3}; + + KeyDeleter deleter(instance, keys); + + instance.set(k1, "v"); + instance.set(k2, "v"); + instance.set(k3, "v"); + + auto cursor = 0; + std::unordered_set res; + while (true) { + cursor = instance.scan(cursor, "*" + key_pattern + "*", 2, std::inserter(res, res.end())); + if (cursor == 0) { + break; + } + } + REDIS_ASSERT(res == std::unordered_set(keys), + "failed to test scan"); +} + +} + +} + +} + +#endif // end SEWENEW_REDISPLUSPLUS_TEST_KEYS_CMDS_TEST_HPP diff --git a/controller/thirdparty/redis-plus-plus-1.1.1/test/src/sw/redis++/list_cmds_test.h b/controller/thirdparty/redis-plus-plus-1.1.1/test/src/sw/redis++/list_cmds_test.h new file mode 100644 index 000000000..2092fe9e4 --- /dev/null +++ b/controller/thirdparty/redis-plus-plus-1.1.1/test/src/sw/redis++/list_cmds_test.h @@ -0,0 +1,55 @@ +/************************************************************************** + Copyright (c) 2017 sewenew + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. + *************************************************************************/ + +#ifndef SEWENEW_REDISPLUSPLUS_TEST_LIST_CMDS_TEST_H +#define SEWENEW_REDISPLUSPLUS_TEST_LIST_CMDS_TEST_H + +#include + +namespace sw { + +namespace redis { + +namespace test { + +template +class ListCmdTest { +public: + explicit ListCmdTest(RedisInstance &instance) : _redis(instance) {} + + void run(); + +private: + void _test_lpoppush(); + + void _test_rpoppush(); + + void _test_list(); + + void _test_blocking(); + + RedisInstance &_redis; +}; + +} + +} + +} + +#include "list_cmds_test.hpp" + +#endif // end SEWENEW_REDISPLUSPLUS_TEST_LIST_CMDS_TEST_H diff --git a/controller/thirdparty/redis-plus-plus-1.1.1/test/src/sw/redis++/list_cmds_test.hpp b/controller/thirdparty/redis-plus-plus-1.1.1/test/src/sw/redis++/list_cmds_test.hpp new file mode 100644 index 000000000..fd26d8fde --- /dev/null +++ b/controller/thirdparty/redis-plus-plus-1.1.1/test/src/sw/redis++/list_cmds_test.hpp @@ -0,0 +1,154 @@ +/************************************************************************** + Copyright (c) 2017 sewenew + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. + *************************************************************************/ + +#ifndef SEWENEW_REDISPLUSPLUS_TEST_LIST_CMDS_TEST_HPP +#define SEWENEW_REDISPLUSPLUS_TEST_LIST_CMDS_TEST_HPP + +#include "utils.h" + +namespace sw { + +namespace redis { + +namespace test { + +template +void ListCmdTest::run() { + _test_lpoppush(); + + _test_rpoppush(); + + _test_list(); + + _test_blocking(); +} + +template +void ListCmdTest::_test_lpoppush() { + auto key = test_key("lpoppush"); + + KeyDeleter deleter(_redis, key); + + auto item = _redis.lpop(key); + REDIS_ASSERT(!item, "failed to test lpop"); + + REDIS_ASSERT(_redis.lpushx(key, "1") == 0, "failed to test lpushx"); + REDIS_ASSERT(_redis.lpush(key, "1") == 1, "failed to test lpush"); + REDIS_ASSERT(_redis.lpushx(key, "2") == 2, "failed to test lpushx"); + REDIS_ASSERT(_redis.lpush(key, {"3", "4", "5"}) == 5, "failed to test lpush"); + + item = _redis.lpop(key); + REDIS_ASSERT(item && *item == "5", "failed to test lpop"); +} + +template +void ListCmdTest::_test_rpoppush() { + auto key = test_key("rpoppush"); + + KeyDeleter deleter(_redis, key); + + auto item = _redis.rpop(key); + REDIS_ASSERT(!item, "failed to test rpop"); + + REDIS_ASSERT(_redis.rpushx(key, "1") == 0, "failed to test rpushx"); + REDIS_ASSERT(_redis.rpush(key, "1") == 1, "failed to test rpush"); + REDIS_ASSERT(_redis.rpushx(key, "2") == 2, "failed to test rpushx"); + REDIS_ASSERT(_redis.rpush(key, {"3", "4", "5"}) == 5, "failed to test rpush"); + + item = _redis.rpop(key); + REDIS_ASSERT(item && *item == "5", "failed to test rpop"); +} + +template +void ListCmdTest::_test_list() { + auto key = test_key("list"); + + KeyDeleter deleter(_redis, key); + + auto item = _redis.lindex(key, 0); + REDIS_ASSERT(!item, "failed to test lindex"); + + _redis.lpush(key, {"1", "2", "3", "4", "5"}); + + REDIS_ASSERT(_redis.lrem(key, 0, "3") == 1, "failed to test lrem"); + + REDIS_ASSERT(_redis.linsert(key, InsertPosition::BEFORE, "2", "3") == 5, + "failed to test lindex"); + + REDIS_ASSERT(_redis.llen(key) == 5, "failed to test llen"); + + _redis.lset(key, 0, "6"); + item = _redis.lindex(key, 0); + REDIS_ASSERT(item && *item == "6", "failed to test lindex"); + + _redis.ltrim(key, 0, 2); + + std::vector res; + _redis.lrange(key, 0, -1, std::back_inserter(res)); + REDIS_ASSERT(res == std::vector({"6", "4", "3"}), "failed to test ltrim"); +} + +template +void ListCmdTest::_test_blocking() { + auto k1 = test_key("k1"); + auto k2 = test_key("k2"); + auto k3 = test_key("k3"); + + auto keys = {k1, k2, k3}; + + KeyDeleter deleter(_redis, keys); + + std::string val("value"); + _redis.lpush(k1, val); + + auto res = _redis.blpop(keys.begin(), keys.end()); + REDIS_ASSERT(res && *res == std::make_pair(k1, val), "failed to test blpop"); + + res = _redis.brpop(keys, std::chrono::seconds(1)); + REDIS_ASSERT(!res, "failed to test brpop with timeout"); + + _redis.lpush(k1, val); + res = _redis.blpop(k1); + REDIS_ASSERT(res && *res == std::make_pair(k1, val), "failed to test blpop"); + + res = _redis.blpop(k1, std::chrono::seconds(1)); + REDIS_ASSERT(!res, "failed to test blpop with timeout"); + + _redis.lpush(k1, val); + res = _redis.brpop(k1); + REDIS_ASSERT(res && *res == std::make_pair(k1, val), "failed to test brpop"); + + res = _redis.brpop(k1, std::chrono::seconds(1)); + REDIS_ASSERT(!res, "failed to test brpop with timeout"); + + auto str = _redis.brpoplpush(k2, k3, std::chrono::seconds(1)); + REDIS_ASSERT(!str, "failed to test brpoplpush with timeout"); + + _redis.lpush(k2, val); + str = _redis.brpoplpush(k2, k3); + REDIS_ASSERT(str && *str == val, "failed to test brpoplpush"); + + str = _redis.rpoplpush(k3, k2); + REDIS_ASSERT(str && *str == val, "failed to test rpoplpush"); +} + +} + +} + +} + +#endif // end SEWENEW_REDISPLUSPLUS_TEST_LIST_CMDS_TEST_HPP diff --git a/controller/thirdparty/redis-plus-plus-1.1.1/test/src/sw/redis++/pipeline_transaction_test.h b/controller/thirdparty/redis-plus-plus-1.1.1/test/src/sw/redis++/pipeline_transaction_test.h new file mode 100644 index 000000000..fc6b80dde --- /dev/null +++ b/controller/thirdparty/redis-plus-plus-1.1.1/test/src/sw/redis++/pipeline_transaction_test.h @@ -0,0 +1,57 @@ +/************************************************************************** + Copyright (c) 2017 sewenew + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. + *************************************************************************/ + +#ifndef SEWENEW_REDISPLUSPLUS_TEST_PIPELINE_TRANSACTION_TEST_H +#define SEWENEW_REDISPLUSPLUS_TEST_PIPELINE_TRANSACTION_TEST_H + +#include + +namespace sw { + +namespace redis { + +namespace test { + +template +class PipelineTransactionTest { +public: + explicit PipelineTransactionTest(RedisInstance &instance) : _redis(instance) {} + + void run(); + +private: + Pipeline _pipeline(const StringView &key); + + Transaction _transaction(const StringView &key, bool piped); + + void _test_pipeline(const StringView &key, Pipeline &pipe); + + void _test_transaction(const StringView &key, Transaction &tx); + + void _test_watch(); + + RedisInstance &_redis; +}; + +} + +} + +} + +#include "pipeline_transaction_test.hpp" + +#endif // end SEWENEW_REDISPLUSPLUS_TEST_PIPELINE_TRANSACTION_TEST_H diff --git a/controller/thirdparty/redis-plus-plus-1.1.1/test/src/sw/redis++/pipeline_transaction_test.hpp b/controller/thirdparty/redis-plus-plus-1.1.1/test/src/sw/redis++/pipeline_transaction_test.hpp new file mode 100644 index 000000000..4e6a745cb --- /dev/null +++ b/controller/thirdparty/redis-plus-plus-1.1.1/test/src/sw/redis++/pipeline_transaction_test.hpp @@ -0,0 +1,184 @@ +/************************************************************************** + Copyright (c) 2017 sewenew + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. + *************************************************************************/ + +#ifndef SEWENEW_REDISPLUSPLUS_TEST_PIPELINE_TRANSACTION_TEST_HPP +#define SEWENEW_REDISPLUSPLUS_TEST_PIPELINE_TRANSACTION_TEST_HPP + +#include +#include "utils.h" + +namespace sw { + +namespace redis { + +namespace test { + +template +void PipelineTransactionTest::run() { + { + auto key = test_key("pipeline"); + KeyDeleter deleter(_redis, key); + auto pipe = _pipeline(key); + _test_pipeline(key, pipe); + } + + { + auto key = test_key("transaction"); + KeyDeleter deleter(_redis, key); + auto tx = _transaction(key, true); + _test_transaction(key, tx); + } + + { + auto key = test_key("transaction"); + KeyDeleter deleter(_redis, key); + auto tx = _transaction(key, false); + _test_transaction(key, tx); + } + + _test_watch(); +} + +template +Pipeline PipelineTransactionTest::_pipeline(const StringView &) { + return _redis.pipeline(); +} + +template <> +inline Pipeline PipelineTransactionTest::_pipeline(const StringView &key) { + return _redis.pipeline(key); +} + +template +Transaction PipelineTransactionTest::_transaction(const StringView &, bool piped) { + return _redis.transaction(piped); +} + +template <> +inline Transaction PipelineTransactionTest::_transaction(const StringView &key, + bool piped) { + return _redis.transaction(key, piped); +} + +template +void PipelineTransactionTest::_test_pipeline(const StringView &key, + Pipeline &pipe) { + std::string val("value"); + auto replies = pipe.set(key, val) + .get(key) + .strlen(key) + .exec(); + + REDIS_ASSERT(replies.get(0), "failed to test pipeline with set operation"); + + auto new_val = replies.get(1); + std::size_t len = replies.get(2); + REDIS_ASSERT(bool(new_val) && *new_val == val && len == val.size(), + "failed to test pipeline with string operations"); + + REDIS_ASSERT(reply::parse(replies.get(0)), "failed to test pipeline with set operation"); + + new_val = reply::parse(replies.get(1)); + len = reply::parse(replies.get(2)); + REDIS_ASSERT(bool(new_val) && *new_val == val && len == val.size(), + "failed to test pipeline with string operations"); +} + +template +void PipelineTransactionTest::_test_transaction(const StringView &key, + Transaction &tx) { + std::unordered_map m = { + std::make_pair("f1", "v1"), + std::make_pair("f2", "v2"), + std::make_pair("f3", "v3") + }; + auto replies = tx.hmset(key, m.begin(), m.end()) + .hgetall(key) + .hdel(key, "f1") + .exec(); + + replies.get(0); + + decltype(m) mm; + replies.get(1, std::inserter(mm, mm.end())); + REDIS_ASSERT(mm == m, "failed to test transaction"); + + REDIS_ASSERT(replies.get(2) == 1, "failed to test transaction"); + + tx.set(key, "value") + .get(key) + .incr(key); + + tx.discard(); + + replies = tx.del(key) + .set(key, "value") + .exec(); + + REDIS_ASSERT(replies.get(0) == 1, "failed to test transaction"); + + REDIS_ASSERT(replies.get(1), "failed to test transaction"); +} + +template +void PipelineTransactionTest::_test_watch() { + auto key = test_key("watch"); + + KeyDeleter deleter(_redis, key); + + { + auto tx = _transaction(key, false); + + auto redis = tx.redis(); + + redis.watch(key); + + auto replies = tx.set(key, "1").get(key).exec(); + + REDIS_ASSERT(replies.size() == 2 + && replies.template get(0) == true, "failed to test watch"); + + auto val = replies.template get(1); + + REDIS_ASSERT(val && *val == "1", "failed to test watch"); + } + + try { + auto tx = _transaction(key, false); + + auto redis = tx.redis(); + + redis.watch(key); + + // Key has been modified by other client. + _redis.set(key, "val"); + + // Transaction should fail, and throw WatchError + tx.set(key, "1").exec(); + + REDIS_ASSERT(false, "failed to test watch"); + } catch (const sw::redis::WatchError &err) { + // Catch the error. + } +} + +} + +} + +} + +#endif // end SEWENEW_REDISPLUSPLUS_TEST_PIPELINE_TRANSACTION_TEST_HPP diff --git a/controller/thirdparty/redis-plus-plus-1.1.1/test/src/sw/redis++/pubsub_test.h b/controller/thirdparty/redis-plus-plus-1.1.1/test/src/sw/redis++/pubsub_test.h new file mode 100644 index 000000000..f816eaecc --- /dev/null +++ b/controller/thirdparty/redis-plus-plus-1.1.1/test/src/sw/redis++/pubsub_test.h @@ -0,0 +1,53 @@ +/************************************************************************** + Copyright (c) 2017 sewenew + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. + *************************************************************************/ + +#ifndef SEWENEW_REDISPLUSPLUS_TEST_SUBPUB_TEST_H +#define SEWENEW_REDISPLUSPLUS_TEST_SUBPUB_TEST_H + +#include + +namespace sw { + +namespace redis { + +namespace test { + +template +class PubSubTest { +public: + explicit PubSubTest(RedisInstance &instance) : _redis(instance) {} + + void run(); + +private: + void _test_sub_channel(); + + void _test_sub_pattern(); + + void _test_unsubscribe(); + + RedisInstance &_redis; +}; + +} + +} + +} + +#include "pubsub_test.hpp" + +#endif // end SEWENEW_REDISPLUSPLUS_TEST_SUBPUB_TEST_H diff --git a/controller/thirdparty/redis-plus-plus-1.1.1/test/src/sw/redis++/pubsub_test.hpp b/controller/thirdparty/redis-plus-plus-1.1.1/test/src/sw/redis++/pubsub_test.hpp new file mode 100644 index 000000000..4530c0788 --- /dev/null +++ b/controller/thirdparty/redis-plus-plus-1.1.1/test/src/sw/redis++/pubsub_test.hpp @@ -0,0 +1,244 @@ +/************************************************************************** + Copyright (c) 2017 sewenew + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. + *************************************************************************/ + +#ifndef SEWENEW_REDISPLUSPLUS_TEST_SUBPUB_TEST_HPP +#define SEWENEW_REDISPLUSPLUS_TEST_SUBPUB_TEST_HPP + +#include +#include +#include "utils.h" + +namespace sw { + +namespace redis { + +namespace test { + +template +void PubSubTest::run() { + _test_sub_channel(); + + _test_sub_pattern(); + + _test_unsubscribe(); +} + +template +void PubSubTest::_test_sub_channel() { + auto sub = _redis.subscriber(); + + auto msgs = {"msg1", "msg2"}; + auto channel1 = test_key("c1"); + sub.on_message([&msgs, &channel1](std::string channel, std::string msg) { + static std::size_t idx = 0; + REDIS_ASSERT(channel == channel1 && msg == *(msgs.begin() + idx), + "failed to test subscribe"); + ++idx; + }); + + sub.subscribe(channel1); + + // Consume the SUBSCRIBE message. + sub.consume(); + + for (const auto &msg : msgs) { + _redis.publish(channel1, msg); + sub.consume(); + } + + sub.unsubscribe(channel1); + + // Consume the UNSUBSCRIBE message. + sub.consume(); + + auto channel2 = test_key("c2"); + auto channel3 = test_key("c3"); + auto channel4 = test_key("c4"); + std::unordered_set channels; + sub.on_meta([&channels](Subscriber::MsgType type, + OptionalString channel, + long long num) { + REDIS_ASSERT(bool(channel), "failed to test subscribe"); + + if (type == Subscriber::MsgType::SUBSCRIBE) { + auto iter = channels.find(*channel); + REDIS_ASSERT(iter == channels.end(), "failed to test subscribe"); + channels.insert(*channel); + REDIS_ASSERT(static_cast(num) == channels.size(), + "failed to test subscribe"); + } else if (type == Subscriber::MsgType::UNSUBSCRIBE) { + auto iter = channels.find(*channel); + REDIS_ASSERT(iter != channels.end(), "failed to test subscribe"); + channels.erase(*channel); + REDIS_ASSERT(static_cast(num) == channels.size(), + "failed to test subscribe"); + } else { + REDIS_ASSERT(false, "Unknown message type"); + } + }); + + std::unordered_map messages = { + {channel2, "msg2"}, + {channel3, "msg3"}, + {channel4, "msg4"}, + }; + sub.on_message([&messages](std::string channel, std::string msg) { + REDIS_ASSERT(messages.find(channel) != messages.end(), + "failed to test subscribe"); + REDIS_ASSERT(messages[channel] == msg, "failed to test subscribe"); + }); + + sub.subscribe({channel2, channel3, channel4}); + + for (std::size_t idx = 0; idx != channels.size(); ++idx) { + sub.consume(); + } + + for (const auto &ele : messages) { + _redis.publish(ele.first, ele.second); + sub.consume(); + } + + auto tmp = {channel2, channel3, channel4}; + sub.unsubscribe(tmp); + + for (std::size_t idx = 0; idx != tmp.size(); ++idx) { + sub.consume(); + } +} + +template +void PubSubTest::_test_sub_pattern() { + auto sub = _redis.subscriber(); + + auto msgs = {"msg1", "msg2"}; + auto pattern1 = test_key("pattern*"); + std::string channel1 = test_key("pattern1"); + sub.on_pmessage([&msgs, &pattern1, &channel1](std::string pattern, + std::string channel, + std::string msg) { + static std::size_t idx = 0; + REDIS_ASSERT(pattern == pattern1 + && channel == channel1 + && msg == *(msgs.begin() + idx), + "failed to test psubscribe"); + ++idx; + }); + + sub.psubscribe(pattern1); + + // Consume the PSUBSCRIBE message. + sub.consume(); + + for (const auto &msg : msgs) { + _redis.publish(channel1, msg); + sub.consume(); + } + + sub.punsubscribe(pattern1); + + // Consume the PUNSUBSCRIBE message. + sub.consume(); + + auto channel2 = test_key("pattern22"); + auto channel3 = test_key("pattern33"); + auto channel4 = test_key("pattern44"); + std::unordered_set channels; + sub.on_meta([&channels](Subscriber::MsgType type, + OptionalString channel, + long long num) { + REDIS_ASSERT(bool(channel), "failed to test psubscribe"); + + if (type == Subscriber::MsgType::PSUBSCRIBE) { + auto iter = channels.find(*channel); + REDIS_ASSERT(iter == channels.end(), "failed to test psubscribe"); + channels.insert(*channel); + REDIS_ASSERT(static_cast(num) == channels.size(), + "failed to test psubscribe"); + } else if (type == Subscriber::MsgType::PUNSUBSCRIBE) { + auto iter = channels.find(*channel); + REDIS_ASSERT(iter != channels.end(), "failed to test psubscribe"); + channels.erase(*channel); + REDIS_ASSERT(static_cast(num) == channels.size(), + "failed to test psubscribe"); + } else { + REDIS_ASSERT(false, "Unknown message type"); + } + }); + + auto pattern2 = test_key("pattern2*"); + auto pattern3 = test_key("pattern3*"); + auto pattern4 = test_key("pattern4*"); + std::unordered_set patterns = {pattern2, pattern3, pattern4}; + + std::unordered_map messages = { + {channel2, "msg2"}, + {channel3, "msg3"}, + {channel4, "msg4"}, + }; + sub.on_pmessage([&patterns, &messages](std::string pattern, + std::string channel, + std::string msg) { + REDIS_ASSERT(patterns.find(pattern) != patterns.end(), + "failed to test psubscribe"); + REDIS_ASSERT(messages[channel] == msg, "failed to test psubscribe"); + }); + + sub.psubscribe({pattern2, pattern3, pattern4}); + + for (std::size_t idx = 0; idx != channels.size(); ++idx) { + sub.consume(); + } + + for (const auto &ele : messages) { + _redis.publish(ele.first, ele.second); + sub.consume(); + } + + auto tmp = {pattern2, pattern3, pattern4}; + sub.punsubscribe(tmp); + + for (std::size_t idx = 0; idx != tmp.size(); ++idx) { + sub.consume(); + } +} + +template +void PubSubTest::_test_unsubscribe() { + auto sub = _redis.subscriber(); + + sub.on_meta([](Subscriber::MsgType type, + OptionalString channel, + long long num) { + REDIS_ASSERT(type == Subscriber::MsgType::UNSUBSCRIBE, + "failed to test unsub"); + + REDIS_ASSERT(!channel, "failed to test unsub"); + + REDIS_ASSERT(num == 0, "failed to test unsub"); + }); + + sub.unsubscribe(); + sub.consume(); +} + +} + +} + +} + +#endif // end SEWENEW_REDISPLUSPLUS_TEST_SUBPUB_TEST_HPP diff --git a/controller/thirdparty/redis-plus-plus-1.1.1/test/src/sw/redis++/sanity_test.h b/controller/thirdparty/redis-plus-plus-1.1.1/test/src/sw/redis++/sanity_test.h new file mode 100644 index 000000000..e41fdef9d --- /dev/null +++ b/controller/thirdparty/redis-plus-plus-1.1.1/test/src/sw/redis++/sanity_test.h @@ -0,0 +1,76 @@ +/************************************************************************** + Copyright (c) 2017 sewenew + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. + *************************************************************************/ + +#ifndef SEWENEW_REDISPLUSPLUS_TEST_SANITY_TEST_H +#define SEWENEW_REDISPLUSPLUS_TEST_SANITY_TEST_H + +#include + +namespace sw { + +namespace redis { + +namespace test { + +template +class SanityTest { +public: + SanityTest(const ConnectionOptions &opts, RedisInstance &instance) + : _opts(opts), _redis(instance) {} + + void run(); + +private: + void _test_uri_ctor(); + + void _ping(Redis &instance); + + void _test_move_ctor(); + + void _test_cmdargs(); + + void _test_generic_command(); + + void _test_hash_tag(); + + void _test_hash_tag(std::initializer_list keys); + + std::string _test_key(const std::string &key); + + void _test_ping(Redis &instance); + + void _test_pipeline(const StringView &key, Pipeline &pipeline); + + void _test_transaction(const StringView &key, Transaction &transaction); + + Pipeline _pipeline(const StringView &key); + + Transaction _transaction(const StringView &key); + + ConnectionOptions _opts; + + RedisInstance &_redis; +}; + +} + +} + +} + +#include "sanity_test.hpp" + +#endif // end SEWENEW_REDISPLUSPLUS_TEST_SANITY_TEST_H diff --git a/controller/thirdparty/redis-plus-plus-1.1.1/test/src/sw/redis++/sanity_test.hpp b/controller/thirdparty/redis-plus-plus-1.1.1/test/src/sw/redis++/sanity_test.hpp new file mode 100644 index 000000000..cff992ecb --- /dev/null +++ b/controller/thirdparty/redis-plus-plus-1.1.1/test/src/sw/redis++/sanity_test.hpp @@ -0,0 +1,299 @@ +/************************************************************************** + Copyright (c) 2017 sewenew + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. + *************************************************************************/ + +#ifndef SEWENEW_REDISPLUSPLUS_TEST_SANITY_TEST_HPP +#define SEWENEW_REDISPLUSPLUS_TEST_SANITY_TEST_HPP + +#include "utils.h" + +namespace sw { + +namespace redis { + +namespace test { + +template +void SanityTest::run() { + _test_uri_ctor(); + + _test_move_ctor(); + + cluster_specializing_test(*this, &SanityTest::_test_ping, _redis); + + auto pipe_key = test_key("pipeline"); + auto tx_key = test_key("transaction"); + + KeyDeleter deleter(_redis, {pipe_key, tx_key}); + + auto pipeline = _pipeline(pipe_key); + _test_pipeline(pipe_key, pipeline); + + auto transaction = _transaction(tx_key); + _test_transaction(tx_key, transaction); + + _test_cmdargs(); + + _test_generic_command(); +} + +template +void SanityTest::_test_uri_ctor() { + std::string uri; + switch (_opts.type) { + case sw::redis::ConnectionType::TCP: + uri = "tcp://" + _opts.host + ":" + std::to_string(_opts.port); + break; + + case sw::redis::ConnectionType::UNIX: + REDIS_ASSERT(false, "NO test for UNIX Domain Socket"); + break; + + default: + REDIS_ASSERT(false, "Unknown connection type"); + } + + auto instance = RedisInstance(uri); + + cluster_specializing_test(*this, &SanityTest::_ping, instance); +} + +template +void SanityTest::_ping(Redis &instance) { + try { + auto pong = instance.ping(); + REDIS_ASSERT(pong == "PONG", "Failed to test constructing Redis with uri"); + } catch (const sw::redis::ReplyError &e) { + REDIS_ASSERT(e.what() == std::string("NOAUTH Authentication required."), + "Failed to test constructing Redis with uri"); + } +} + +template +void SanityTest::_test_move_ctor() { + auto test_move_ctor = std::move(_redis); + + _redis = std::move(test_move_ctor); +} + +template +void SanityTest::_test_cmdargs() { + auto lpush_num = [](Connection &connection, const StringView &key, long long num) { + connection.send("LPUSH %b %lld", + key.data(), key.size(), + num); + }; + + auto lpush_nums = [](Connection &connection, + const StringView &key, + const std::vector &nums) { + CmdArgs args; + args.append("LPUSH").append(key); + for (auto num : nums) { + args.append(std::to_string(num)); + } + + connection.send(args); + }; + + auto key = test_key("lpush_num"); + + KeyDeleter deleter(_redis, key); + + auto reply = _redis.command(lpush_num, key, 1); + REDIS_ASSERT(reply::parse(*reply) == 1, "failed to test cmdargs"); + + std::vector nums = {2, 3, 4, 5}; + reply = _redis.command(lpush_nums, key, nums); + REDIS_ASSERT(reply::parse(*reply) == 5, "failed to test cmdargs"); + + std::vector res; + _redis.lrange(key, 0, -1, std::back_inserter(res)); + REDIS_ASSERT((res == std::vector{"5", "4", "3", "2", "1"}), + "failed to test cmdargs"); +} + +template +void SanityTest::_test_generic_command() { + auto key = test_key("key"); + auto not_exist_key = test_key("not_exist_key"); + auto k1 = test_key("k1"); + auto k2 = test_key("k2"); + + KeyDeleter deleter(_redis, {key, not_exist_key, k1, k2}); + + std::string cmd("set"); + _redis.command(cmd, key, 123); + auto reply = _redis.command("get", key); + auto val = reply::parse(*reply); + REDIS_ASSERT(val && *val == "123", "failed to test generic command"); + + val = _redis.template command("get", key); + REDIS_ASSERT(val && *val == "123", "failed to test generic command"); + + std::vector res; + _redis.command("mget", key, not_exist_key, std::back_inserter(res)); + REDIS_ASSERT(res.size() == 2 && res[0] && *res[0] == "123" && !res[1], + "failed to test generic command"); + + reply = _redis.command("incr", key); + REDIS_ASSERT(reply::parse(*reply) == 124, "failed to test generic command"); + + _redis.command("mset", k1.c_str(), "v", k2.c_str(), "v"); + reply = _redis.command("mget", k1, k2); + res.clear(); + reply::to_array(*reply, std::back_inserter(res)); + REDIS_ASSERT(res.size() == 2 && res[0] && *(res[0]) == "v" && res[1] && *(res[1]) == "v", + "failed to test generic command"); + + res = _redis.template command>("mget", k1, k2); + REDIS_ASSERT(res.size() == 2 && res[0] && *(res[0]) == "v" && res[1] && *(res[1]) == "v", + "failed to test generic command"); + + res.clear(); + _redis.command("mget", k1, k2, std::back_inserter(res)); + REDIS_ASSERT(res.size() == 2 && res[0] && *(res[0]) == "v" && res[1] && *(res[1]) == "v", + "failed to test generic command"); + + auto set_cmd_str = {"set", key.c_str(), "new_value"}; + _redis.command(set_cmd_str.begin(), set_cmd_str.end()); + + auto get_cmd_str = {"get", key.c_str()}; + reply = _redis.command(get_cmd_str.begin(), get_cmd_str.end()); + val = reply::parse(*reply); + REDIS_ASSERT(val && *val == "new_value", "failed to test generic command"); + + val = _redis.template command(get_cmd_str.begin(), get_cmd_str.end()); + REDIS_ASSERT(val && *val == "new_value", "failed to test generic command"); + + auto mget_cmd_str = {"mget", key.c_str(), not_exist_key.c_str()}; + res.clear(); + _redis.command(mget_cmd_str.begin(), mget_cmd_str.end(), std::back_inserter(res)); + REDIS_ASSERT(res.size() == 2 && res[0] && *res[0] == "new_value" && !res[1], + "failed to test generic command"); +} + +template +void SanityTest::_test_hash_tag() { + _test_hash_tag({_test_key("{tag}postfix1"), + _test_key("{tag}postfix2"), + _test_key("{tag}postfix3")}); + + _test_hash_tag({_test_key("prefix1{tag}postfix1"), + _test_key("prefix2{tag}postfix2"), + _test_key("prefix3{tag}postfix3")}); + + _test_hash_tag({_test_key("prefix1{tag}"), + _test_key("prefix2{tag}"), + _test_key("prefix3{tag}")}); + + _test_hash_tag({_test_key("prefix{}postfix"), + _test_key("prefix{}postfix"), + _test_key("prefix{}postfix")}); + + _test_hash_tag({_test_key("prefix1{tag}post}fix1"), + _test_key("prefix2{tag}pos}tfix2"), + _test_key("prefix3{tag}postfi}x3")}); + + _test_hash_tag({_test_key("prefix1{t{ag}postfix1"), + _test_key("prefix2{t{ag}postfix2"), + _test_key("prefix3{t{ag}postfix3")}); + + _test_hash_tag({_test_key("prefix1{t{ag}postfi}x1"), + _test_key("prefix2{t{ag}post}fix2"), + _test_key("prefix3{t{ag}po}stfix3")}); +} + +template +void SanityTest::_test_hash_tag(std::initializer_list keys) { + KeyDeleter deleter(_redis, keys.begin(), keys.end()); + + std::string value = "value"; + std::vector> kvs; + for (const auto &key : keys) { + kvs.emplace_back(key, value); + } + + _redis.mset(kvs.begin(), kvs.end()); + + std::vector res; + res.reserve(keys.size()); + _redis.mget(keys.begin(), keys.end(), std::back_inserter(res)); + + REDIS_ASSERT(res.size() == keys.size(), "failed to test hash tag"); + + for (const auto &ele : res) { + REDIS_ASSERT(ele && *ele == value, "failed to test hash tag"); + } +} + +template +std::string SanityTest::_test_key(const std::string &key) { + REDIS_ASSERT(key.size() > 1, "failed to generate key"); + + // Ensure that key prefix has NO hash tag. Also see the implementation of test_key. + return key.substr(1); +} + +template +void SanityTest::_test_ping(Redis &instance) { + auto reply = instance.command("ping"); + REDIS_ASSERT(reply && reply::parse(*reply) == "PONG", + "failed to test generic command"); + + auto pong = instance.command("ping"); + REDIS_ASSERT(pong == "PONG", "failed to test generic command"); +} + +template +void SanityTest::_test_pipeline(const StringView &key, Pipeline &pipeline) { + auto pipe_replies = pipeline.command("set", key, "value").command("get", key).exec(); + auto val = pipe_replies.get(1); + REDIS_ASSERT(val && *val == "value", "failed to test generic command"); +} + +template +void SanityTest::_test_transaction(const StringView &key, Transaction &transaction) { + auto tx_replies = transaction.command("set", key, 456).command("incr", key).exec(); + REDIS_ASSERT(tx_replies.get(1) == 457, "failed to test generic command"); +} + +template +Pipeline SanityTest::_pipeline(const StringView &) { + return _redis.pipeline(); +} + +template <> +inline Pipeline SanityTest::_pipeline(const StringView &key) { + return _redis.pipeline(key); +} + +template +Transaction SanityTest::_transaction(const StringView &) { + return _redis.transaction(); +} + +template <> +inline Transaction SanityTest::_transaction(const StringView &key) { + return _redis.transaction(key); +} + +} + +} + +} + +#endif // end SEWENEW_REDISPLUSPLUS_TEST_SANITY_TEST_HPP diff --git a/controller/thirdparty/redis-plus-plus-1.1.1/test/src/sw/redis++/script_cmds_test.h b/controller/thirdparty/redis-plus-plus-1.1.1/test/src/sw/redis++/script_cmds_test.h new file mode 100644 index 000000000..f7adb11b6 --- /dev/null +++ b/controller/thirdparty/redis-plus-plus-1.1.1/test/src/sw/redis++/script_cmds_test.h @@ -0,0 +1,49 @@ +/************************************************************************** + Copyright (c) 2017 sewenew + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. + *************************************************************************/ + +#ifndef SEWENEW_REDISPLUSPLUS_TEST_SCRIPT_CMDS_TEST_H +#define SEWENEW_REDISPLUSPLUS_TEST_SCRIPT_CMDS_TEST_H + +#include + +namespace sw { + +namespace redis { + +namespace test { + +template +class ScriptCmdTest { +public: + explicit ScriptCmdTest(RedisInstance &instance) : _redis(instance) {} + + void run(); + +private: + void _run(Redis &instance); + + RedisInstance &_redis; +}; + +} + +} + +} + +#include "script_cmds_test.hpp" + +#endif // end SEWENEW_REDISPLUSPLUS_TEST_SCRIPT_CMDS_TEST_H diff --git a/controller/thirdparty/redis-plus-plus-1.1.1/test/src/sw/redis++/script_cmds_test.hpp b/controller/thirdparty/redis-plus-plus-1.1.1/test/src/sw/redis++/script_cmds_test.hpp new file mode 100644 index 000000000..76e07fda2 --- /dev/null +++ b/controller/thirdparty/redis-plus-plus-1.1.1/test/src/sw/redis++/script_cmds_test.hpp @@ -0,0 +1,97 @@ +/************************************************************************** + Copyright (c) 2017 sewenew + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. + *************************************************************************/ + +#ifndef SEWENEW_REDISPLUSPLUS_TEST_SCRIPT_CMDS_TEST_HPP +#define SEWENEW_REDISPLUSPLUS_TEST_SCRIPT_CMDS_TEST_HPP + +#include +#include +#include "utils.h" + +namespace sw { + +namespace redis { + +namespace test { + +template +void ScriptCmdTest::run() { + cluster_specializing_test(*this, + &ScriptCmdTest::_run, + _redis); +} + +template +void ScriptCmdTest::_run(Redis &instance) { + auto key1 = test_key("k1"); + auto key2 = test_key("k2"); + + KeyDeleter deleter(instance, {key1, key2}); + + std::string script = "redis.call('set', KEYS[1], 1);" + "redis.call('set', KEYS[2], 2);" + "local first = redis.call('get', KEYS[1]);" + "local second = redis.call('get', KEYS[2]);" + "return first + second"; + + auto num = instance.eval(script, {key1, key2}, {}); + REDIS_ASSERT(num == 3, "failed to test scripting for cluster"); + + script = "return 1"; + num = instance.eval(script, {}, {}); + REDIS_ASSERT(num == 1, "failed to test eval"); + + auto script_with_args = "return {ARGV[1] + 1, ARGV[2] + 2, ARGV[3] + 3}"; + std::vector res; + instance.eval(script_with_args, + {"k"}, + {"1", "2", "3"}, + std::back_inserter(res)); + REDIS_ASSERT(res == std::vector({2, 4, 6}), + "failed to test eval with array reply"); + + auto sha1 = instance.script_load(script); + num = instance.evalsha(sha1, {}, {}); + REDIS_ASSERT(num == 1, "failed to test evalsha"); + + auto sha2 = instance.script_load(script_with_args); + res.clear(); + instance.evalsha(sha2, + {"k"}, + {"1", "2", "3"}, + std::back_inserter(res)); + REDIS_ASSERT(res == std::vector({2, 4, 6}), + "failed to test evalsha with array reply"); + + std::list exist_res; + instance.script_exists({sha1, sha2, std::string("not exist")}, std::back_inserter(exist_res)); + REDIS_ASSERT(exist_res == std::list({true, true, false}), + "failed to test script exists"); + + instance.script_flush(); + exist_res.clear(); + instance.script_exists({sha1, sha2, std::string("not exist")}, std::back_inserter(exist_res)); + REDIS_ASSERT(exist_res == std::list({false, false, false}), + "failed to test script flush"); +} + +} + +} + +} + +#endif // end SEWENEW_REDISPLUSPLUS_TEST_SCRIPT_CMDS_TEST_HPP diff --git a/controller/thirdparty/redis-plus-plus-1.1.1/test/src/sw/redis++/set_cmds_test.h b/controller/thirdparty/redis-plus-plus-1.1.1/test/src/sw/redis++/set_cmds_test.h new file mode 100644 index 000000000..c5320d793 --- /dev/null +++ b/controller/thirdparty/redis-plus-plus-1.1.1/test/src/sw/redis++/set_cmds_test.h @@ -0,0 +1,53 @@ +/************************************************************************** + Copyright (c) 2017 sewenew + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. + *************************************************************************/ + +#ifndef SEWENEW_REDISPLUSPLUS_TEST_SET_CMDS_TEST_H +#define SEWENEW_REDISPLUSPLUS_TEST_SET_CMDS_TEST_H + +#include + +namespace sw { + +namespace redis { + +namespace test { + +template +class SetCmdTest { +public: + explicit SetCmdTest(RedisInstance &instance) : _redis(instance) {} + + void run(); + +private: + void _test_set(); + + void _test_multi_set(); + + void _test_sscan(); + + RedisInstance &_redis; +}; + +} + +} + +} + +#include "set_cmds_test.hpp" + +#endif // end SEWENEW_REDISPLUSPLUS_TEST_SET_CMDS_TEST_H diff --git a/controller/thirdparty/redis-plus-plus-1.1.1/test/src/sw/redis++/set_cmds_test.hpp b/controller/thirdparty/redis-plus-plus-1.1.1/test/src/sw/redis++/set_cmds_test.hpp new file mode 100644 index 000000000..1a4c24bfd --- /dev/null +++ b/controller/thirdparty/redis-plus-plus-1.1.1/test/src/sw/redis++/set_cmds_test.hpp @@ -0,0 +1,184 @@ +/************************************************************************** + Copyright (c) 2017 sewenew + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. + *************************************************************************/ + +#ifndef SEWENEW_REDISPLUSPLUS_TEST_SET_CMDS_TEST_HPP +#define SEWENEW_REDISPLUSPLUS_TEST_SET_CMDS_TEST_HPP + +#include +#include +#include "utils.h" + +namespace sw { + +namespace redis { + +namespace test { + +template +void SetCmdTest::run() { + _test_set(); + + _test_multi_set(); + + _test_sscan(); +} + +template +void SetCmdTest::_test_set() { + auto key = test_key("set"); + + KeyDeleter deleter(_redis, key); + + std::string m1("m1"); + std::string m2("m2"); + std::string m3("m3"); + + REDIS_ASSERT(_redis.sadd(key, m1) == 1, "failed to test sadd"); + + auto members = {m1, m2, m3}; + REDIS_ASSERT(_redis.sadd(key, members) == 2, "failed to test sadd with multiple members"); + + REDIS_ASSERT(_redis.scard(key) == 3, "failed to test scard"); + + REDIS_ASSERT(_redis.sismember(key, m1), "failed to test sismember"); + + std::unordered_set res; + _redis.smembers(key, std::inserter(res, res.end())); + REDIS_ASSERT(res.find(m1) != res.end() + && res.find(m2) != res.end() + && res.find(m3) != res.end(), + "failed to test smembers"); + + auto ele = _redis.srandmember(key); + REDIS_ASSERT(bool(ele) && res.find(*ele) != res.end(), "failed to test srandmember"); + + std::vector rand_members; + _redis.srandmember(key, 2, std::back_inserter(rand_members)); + REDIS_ASSERT(rand_members.size() == 2, "failed to test srandmember"); + + ele = _redis.spop(key); + REDIS_ASSERT(bool(ele) && res.find(*ele) != res.end(), "failed to test spop"); + + rand_members.clear(); + _redis.spop(key, 3, std::back_inserter(rand_members)); + REDIS_ASSERT(rand_members.size() == 2, "failed to test srandmember"); + + rand_members.clear(); + _redis.srandmember(key, 2, std::back_inserter(rand_members)); + REDIS_ASSERT(rand_members.empty(), "failed to test srandmember with empty set"); + + _redis.spop(key, 2, std::back_inserter(rand_members)); + REDIS_ASSERT(rand_members.empty(), "failed to test spop with empty set"); + + _redis.sadd(key, members); + REDIS_ASSERT(_redis.srem(key, m1) == 1, "failed to test srem"); + REDIS_ASSERT(_redis.srem(key, members) == 2, "failed to test srem with mulitple members"); + REDIS_ASSERT(_redis.srem(key, members) == 0, "failed to test srem with mulitple members"); +} + +template +void SetCmdTest::_test_multi_set() { + auto k1 = test_key("s1"); + auto k2 = test_key("s2"); + auto k3 = test_key("s3"); + auto k4 = test_key("s4"); + auto k5 = test_key("s5"); + auto k6 = test_key("s6"); + + KeyDeleter keys(_redis, {k1, k2, k3, k4, k5, k6}); + + _redis.sadd(k1, {"a", "c"}); + _redis.sadd(k2, {"a", "b"}); + std::vector sdiff; + _redis.sdiff({k1, k1}, std::back_inserter(sdiff)); + REDIS_ASSERT(sdiff.empty(), "failed to test sdiff"); + + _redis.sdiff({k1, k2}, std::back_inserter(sdiff)); + REDIS_ASSERT(sdiff == std::vector({"c"}), "failed to test sdiff"); + + _redis.sdiffstore(k3, {k1, k2}); + sdiff.clear(); + _redis.smembers(k3, std::back_inserter(sdiff)); + REDIS_ASSERT(sdiff == std::vector({"c"}), "failed to test sdiffstore"); + + REDIS_ASSERT(_redis.sdiffstore(k3, k1) == 2, "failed to test sdiffstore"); + + REDIS_ASSERT(_redis.sinterstore(k3, k1) == 2, "failed to test sinterstore"); + + REDIS_ASSERT(_redis.sunionstore(k3, k1) == 2, "failed to test sunionstore"); + + std::vector sinter; + _redis.sinter({k1, k2}, std::back_inserter(sinter)); + REDIS_ASSERT(sinter == std::vector({"a"}), "failed to test sinter"); + + _redis.sinterstore(k4, {k1, k2}); + sinter.clear(); + _redis.smembers(k4, std::back_inserter(sinter)); + REDIS_ASSERT(sinter == std::vector({"a"}), "failed to test sinterstore"); + + std::unordered_set sunion; + _redis.sunion({k1, k2}, std::inserter(sunion, sunion.end())); + REDIS_ASSERT(sunion == std::unordered_set({"a", "b", "c"}), + "failed to test sunion"); + + _redis.sunionstore(k5, {k1, k2}); + sunion.clear(); + _redis.smembers(k5, std::inserter(sunion, sunion.end())); + REDIS_ASSERT(sunion == std::unordered_set({"a", "b", "c"}), + "failed to test sunionstore"); + + REDIS_ASSERT(_redis.smove(k5, k6, "a"), "failed to test smove"); +} + +template +void SetCmdTest::_test_sscan() { + auto key = test_key("sscan"); + + KeyDeleter deleter(_redis, key); + + std::unordered_set members = {"m1", "m2", "m3"}; + _redis.sadd(key, members.begin(), members.end()); + + std::unordered_set res; + long long cursor = 0; + while (true) { + cursor = _redis.sscan(key, cursor, "m*", 1, std::inserter(res, res.end())); + if (cursor == 0) { + break; + } + } + + REDIS_ASSERT(res == members, "failed to test sscan"); + + res.clear(); + cursor = 0; + while (true) { + cursor = _redis.sscan(key, cursor, std::inserter(res, res.end())); + if (cursor == 0) { + break; + } + } + + REDIS_ASSERT(res == members, "failed to test sscan"); +} + +} + +} + +} + +#endif // end SEWENEW_REDISPLUSPLUS_TEST_SET_CMDS_TEST_HPP diff --git a/controller/thirdparty/redis-plus-plus-1.1.1/test/src/sw/redis++/stream_cmds_test.h b/controller/thirdparty/redis-plus-plus-1.1.1/test/src/sw/redis++/stream_cmds_test.h new file mode 100644 index 000000000..24873a8b4 --- /dev/null +++ b/controller/thirdparty/redis-plus-plus-1.1.1/test/src/sw/redis++/stream_cmds_test.h @@ -0,0 +1,54 @@ +/************************************************************************** + Copyright (c) 2017 sewenew + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. + *************************************************************************/ + +#ifndef SEWENEW_REDISPLUSPLUS_TEST_STREAM_CMDS_TEST_H +#define SEWENEW_REDISPLUSPLUS_TEST_STREAM_CMDS_TEST_H + +#include + +namespace sw { + +namespace redis { + +namespace test { + +template +class StreamCmdsTest { +public: + explicit StreamCmdsTest(RedisInstance &instance) : _redis(instance) {} + + void run(); + +private: + using Item = std::pair>; + using Result = std::unordered_map>; + + void _test_stream_cmds(); + + void _test_group_cmds(); + + RedisInstance &_redis; +}; + +} + +} + +} + +#include "stream_cmds_test.hpp" + +#endif // end SEWENEW_REDISPLUSPLUS_TEST_STREAM_CMDS_TEST_H diff --git a/controller/thirdparty/redis-plus-plus-1.1.1/test/src/sw/redis++/stream_cmds_test.hpp b/controller/thirdparty/redis-plus-plus-1.1.1/test/src/sw/redis++/stream_cmds_test.hpp new file mode 100644 index 000000000..df6f57690 --- /dev/null +++ b/controller/thirdparty/redis-plus-plus-1.1.1/test/src/sw/redis++/stream_cmds_test.hpp @@ -0,0 +1,225 @@ +/************************************************************************** + Copyright (c) 2017 sewenew + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. + *************************************************************************/ + +#ifndef SEWENEW_REDISPLUSPLUS_TEST_STREAM_CMDS_TEST_HPP +#define SEWENEW_REDISPLUSPLUS_TEST_STREAM_CMDS_TEST_HPP + +#include +#include +#include +#include +#include +#include "utils.h" + +namespace sw { + +namespace redis { + +namespace test { + +template +void StreamCmdsTest::run() { + _test_stream_cmds(); + + _test_group_cmds(); +} + +template +void StreamCmdsTest::_test_stream_cmds() { + auto key = test_key("stream"); + + KeyDeleter deleter(_redis, key); + + std::vector> attrs = { + {"f1", "v1"}, + {"f2", "v2"} + }; + auto id = "1565427842-0"; + REDIS_ASSERT(_redis.xadd(key, id, attrs.begin(), attrs.end()) == id, + "failed to test xadd"); + + std::vector> keys = {std::make_pair(key, "0-0")}; + Result result; + _redis.xread(keys.begin(), keys.end(), 1, std::inserter(result, result.end())); + + REDIS_ASSERT(result.size() == 1 + && result.find(key) != result.end() + && result[key].size() == 1 + && result[key][0].first == id + && result[key][0].second.size() == 2, + "failed to test xread"); + + result.clear(); + _redis.xread(key, std::string("0-0"), 1, std::inserter(result, result.end())); + + REDIS_ASSERT(result.size() == 1 + && result.find(key) != result.end() + && result[key].size() == 1 + && result[key][0].first == id + && result[key][0].second.size() == 2, + "failed to test xread"); + + result.clear(); + keys = {std::make_pair(key, id)}; + _redis.xread(keys.begin(), + keys.end(), + std::chrono::seconds(1), + 2, + std::inserter(result, result.end())); + REDIS_ASSERT(result.size() == 0, "failed to test xread"); + + _redis.xread(key, + id, + std::chrono::seconds(1), + 2, + std::inserter(result, result.end())); + REDIS_ASSERT(result.size() == 0, "failed to test xread"); + + id = "1565427842-1"; + REDIS_ASSERT(_redis.xadd(key, id, attrs.begin(), attrs.end()) == id, + "failed to test xadd"); + + REDIS_ASSERT(_redis.xlen(key) == 2, "failed to test xlen"); + + REDIS_ASSERT(_redis.xtrim(key, 1, false) == 1, "failed to test xtrim"); + + std::vector items; + _redis.xrange(key, "-", "+", std::back_inserter(items)); + REDIS_ASSERT(items.size() == 1 && items[0].first == id, "failed to test xrange"); + + items.clear(); + _redis.xrevrange(key, "+", "-", std::back_inserter(items)); + REDIS_ASSERT(items.size() == 1 && items[0].first == id, "failed to test xrevrange"); + + REDIS_ASSERT(_redis.xdel(key, {id, "111-111"}) == 1, "failed to test xdel"); +} + +template +void StreamCmdsTest::_test_group_cmds() { + auto key = test_key("stream"); + + KeyDeleter deleter(_redis, key); + + auto group = "group"; + auto consumer1 = "consumer1"; + + _redis.xgroup_create(key, group, "$", true); + + std::vector> attrs = { + {"f1", "v1"}, + {"f2", "v2"} + }; + auto id = _redis.xadd(key, "*", attrs.begin(), attrs.end()); + auto keys = {std::make_pair(key, ">")}; + + Result result; + _redis.xreadgroup(group, + consumer1, + keys.begin(), + keys.end(), + 1, + std::inserter(result, result.end())); + REDIS_ASSERT(result.size() == 1 + && result.find(key) != result.end() + && result[key].size() == 1 + && result[key][0].first == id, + "failed to test xreadgroup"); + + result.clear(); + _redis.xreadgroup(group, + consumer1, + key, + std::string(">"), + 1, + std::inserter(result, result.end())); + REDIS_ASSERT(result.size() == 0, "failed to test xreadgroup"); + + result.clear(); + _redis.xreadgroup(group, + "not-exist-consumer", + keys.begin(), + keys.end(), + 1, + std::inserter(result, result.end())); + REDIS_ASSERT(result.size() == 0, "failed to test xreadgroup"); + + result.clear(); + _redis.xreadgroup(group, + consumer1, + keys.begin(), + keys.end(), + std::chrono::seconds(1), + 1, + std::inserter(result, result.end())); + REDIS_ASSERT(result.size() == 0, "failed to test xreadgroup"); + + result.clear(); + _redis.xreadgroup(group, + consumer1, + key, + ">", + std::chrono::seconds(1), + 1, + std::inserter(result, result.end())); + REDIS_ASSERT(result.size() == 0, "failed to test xreadgroup"); + + using PendingResult = std::vector>; + PendingResult pending_result; + _redis.xpending(key, group, "-", "+", 1, consumer1, std::back_inserter(pending_result)); + + REDIS_ASSERT(pending_result.size() == 1 + && std::get<0>(pending_result[0]) == id + && std::get<1>(pending_result[0]) == consumer1, + "failed to test xpending"); + + std::this_thread::sleep_for(std::chrono::seconds(1)); + + auto consumer2 = "consumer2"; + std::vector items; + auto ids = {id}; + _redis.xclaim(key, + group, + consumer2, + std::chrono::milliseconds(10), + ids, + std::back_inserter(items)); + REDIS_ASSERT(items.size() == 1 && items[0].first == id, "failed to test xclaim"); + + std::this_thread::sleep_for(std::chrono::seconds(1)); + + items.clear(); + _redis.xclaim(key, group, consumer1, std::chrono::milliseconds(10), id, std::back_inserter(items)); + REDIS_ASSERT(items.size() == 1 && items[0].first == id, "failed to test xclaim: " + std::to_string(items.size())); + + _redis.xack(key, group, id); + + REDIS_ASSERT(_redis.xgroup_delconsumer(key, group, consumer1) == 0, + "failed to test xgroup_delconsumer"); + + REDIS_ASSERT(_redis.xgroup_delconsumer(key, group, consumer2) == 0, + "failed to test xgroup_delconsumer"); + + REDIS_ASSERT(_redis.xgroup_destroy(key, group) == 1, + "failed to test xgroup_destroy"); +} + +} + +} + +} + +#endif // end SEWENEW_REDISPLUSPLUS_TEST_STREAM_CMDS_TEST_HPP diff --git a/controller/thirdparty/redis-plus-plus-1.1.1/test/src/sw/redis++/string_cmds_test.h b/controller/thirdparty/redis-plus-plus-1.1.1/test/src/sw/redis++/string_cmds_test.h new file mode 100644 index 000000000..86788b2bc --- /dev/null +++ b/controller/thirdparty/redis-plus-plus-1.1.1/test/src/sw/redis++/string_cmds_test.h @@ -0,0 +1,57 @@ +/************************************************************************** + Copyright (c) 2017 sewenew + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. + *************************************************************************/ + +#ifndef SEWENEW_REDISPLUSPLUS_TEST_STRING_CMDS_TEST_H +#define SEWENEW_REDISPLUSPLUS_TEST_STRING_CMDS_TEST_H + +#include + +namespace sw { + +namespace redis { + +namespace test { + +template +class StringCmdTest { +public: + explicit StringCmdTest(RedisInstance &instance) : _redis(instance) {} + + void run(); + +private: + void _test_str(); + + void _test_bit(); + + void _test_numeric(); + + void _test_getset(); + + void _test_mgetset(); + + RedisInstance &_redis; +}; + +} + +} + +} + +#include "string_cmds_test.hpp" + +#endif // end SEWENEW_REDISPLUSPLUS_TEST_STRING_CMDS_TEST_H diff --git a/controller/thirdparty/redis-plus-plus-1.1.1/test/src/sw/redis++/string_cmds_test.hpp b/controller/thirdparty/redis-plus-plus-1.1.1/test/src/sw/redis++/string_cmds_test.hpp new file mode 100644 index 000000000..b8a0aa3c1 --- /dev/null +++ b/controller/thirdparty/redis-plus-plus-1.1.1/test/src/sw/redis++/string_cmds_test.hpp @@ -0,0 +1,247 @@ +/************************************************************************** + Copyright (c) 2017 sewenew + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. + *************************************************************************/ + +#ifndef SEWENEW_REDISPLUSPLUS_TEST_STRING_CMDS_TEST_HPP +#define SEWENEW_REDISPLUSPLUS_TEST_STRING_CMDS_TEST_HPP + +#include +#include "utils.h" + +namespace sw { + +namespace redis { + +namespace test { + +template +void StringCmdTest::run() { + _test_str(); + + _test_bit(); + + _test_numeric(); + + _test_getset(); + + _test_mgetset(); +} + +template +void StringCmdTest::_test_str() { + auto key = test_key("str"); + + KeyDeleter deleter(_redis, key); + + std::string val("value"); + + long long val_size = val.size(); + + auto len1 = _redis.append(key, val); + REDIS_ASSERT(len1 == val_size, "failed to append to non-existent key"); + + auto len2 = _redis.append(key, val); + REDIS_ASSERT(len2 == len1 + val_size, "failed to append to non-empty string"); + + auto len3 = _redis.append(key, {}); + REDIS_ASSERT(len3 == len2, "failed to append empty string"); + + auto len4 = _redis.strlen(key); + REDIS_ASSERT(len4 == len3, "failed to test strlen"); + + REDIS_ASSERT(_redis.del(key) == 1, "failed to remove key"); + + auto len5 = _redis.append(key, {}); + REDIS_ASSERT(len5 == 0, "failed to append empty string to non-existent key"); + + _redis.del(key); + + REDIS_ASSERT(_redis.getrange(key, 0, 2) == "", "failed to test getrange on non-existent key"); + + _redis.set(key, val); + + REDIS_ASSERT(_redis.getrange(key, 1, 2) == val.substr(1, 2), "failed to test getrange"); + + long long new_size = val.size() * 2; + REDIS_ASSERT(_redis.setrange(key, val.size(), val) == new_size, "failed to test setrange"); + REDIS_ASSERT(_redis.getrange(key, 0, -1) == val + val, "failed to test setrange"); +} + +template +void StringCmdTest::_test_bit() { + auto key = test_key("bit"); + + KeyDeleter deleter(_redis, key); + + REDIS_ASSERT(_redis.bitcount(key) == 0, "failed to test bitcount on non-existent key"); + + REDIS_ASSERT(_redis.getbit(key, 5) == 0, "failed to test getbit"); + + REDIS_ASSERT(_redis.template command("SETBIT", key, 1, 1) == 0, + "failed to test setbit"); + REDIS_ASSERT(_redis.template command("SETBIT", key, 3, 1) == 0, + "failed to test setbit"); + REDIS_ASSERT(_redis.template command("SETBIT", key, 7, 1) == 0, + "failed to test setbit"); + REDIS_ASSERT(_redis.template command("SETBIT", key, 10, 1) == 0, + "failed to test setbit"); + REDIS_ASSERT(_redis.template command("SETBIT", key, 10, 0) == 1, + "failed to test setbit"); + REDIS_ASSERT(_redis.template command("SETBIT", key, 11, 1) == 0, + "failed to test setbit"); + REDIS_ASSERT(_redis.template command("SETBIT", key, 21, 1) == 0, + "failed to test setbit"); + + // key -> 01010001, 00010000, 00000100 + + REDIS_ASSERT(_redis.getbit(key, 1) == 1, "failed to test getbit"); + REDIS_ASSERT(_redis.getbit(key, 2) == 0, "failed to test getbit"); + REDIS_ASSERT(_redis.getbit(key, 7) == 1, "failed to test getbit"); + REDIS_ASSERT(_redis.getbit(key, 10) == 0, "failed to test getbit"); + REDIS_ASSERT(_redis.getbit(key, 100) == 0, "failed to test getbit"); + + REDIS_ASSERT(_redis.bitcount(key) == 5, "failed to test bitcount"); + REDIS_ASSERT(_redis.bitcount(key, 0, 0) == 3, "failed to test bitcount"); + REDIS_ASSERT(_redis.bitcount(key, 0, 1) == 4, "failed to test bitcount"); + REDIS_ASSERT(_redis.bitcount(key, -2, -1) == 2, "failed to test bitcount"); + + REDIS_ASSERT(_redis.bitpos(key, 1) == 1, "failed to test bitpos"); + REDIS_ASSERT(_redis.bitpos(key, 0) == 0, "failed to test bitpos"); + REDIS_ASSERT(_redis.bitpos(key, 1, 1, 1) == 11, "failed to test bitpos"); + REDIS_ASSERT(_redis.bitpos(key, 0, 1, 1) == 8, "failed to test bitpos"); + REDIS_ASSERT(_redis.bitpos(key, 1, -1, -1) == 21, "failed to test bitpos"); + REDIS_ASSERT(_redis.bitpos(key, 0, -1, -1) == 16, "failed to test bitpos"); + + auto dest_key = test_key("bitop_dest"); + auto src_key1 = test_key("bitop_src1"); + auto src_key2 = test_key("bitop_src2"); + + KeyDeleter deleters(_redis, {dest_key, src_key1, src_key2}); + + // src_key1 -> 00010000 + _redis.template command("SETBIT", src_key1, 3, 1); + + // src_key2 -> 00000000, 00001000 + _redis.template command("SETBIT", src_key2, 12, 1); + + REDIS_ASSERT(_redis.bitop(BitOp::AND, dest_key, {src_key1, src_key2}) == 2, + "failed to test bitop"); + + // dest_key -> 00000000, 00000000 + auto v = _redis.get(dest_key); + REDIS_ASSERT(v && *v == std::string(2, 0), "failed to test bitop"); + + REDIS_ASSERT(_redis.bitop(BitOp::NOT, dest_key, src_key1) == 1, + "failed to test bitop"); + + // dest_key -> 11101111 + v = _redis.get(dest_key); + REDIS_ASSERT(v && *v == std::string(1, 0xEF), "failed to test bitop"); +} + +template +void StringCmdTest::_test_numeric() { + auto key = test_key("numeric"); + + KeyDeleter deleter(_redis, key); + + REDIS_ASSERT(_redis.incr(key) == 1, "failed to test incr"); + REDIS_ASSERT(_redis.decr(key) == 0, "failed to test decr"); + REDIS_ASSERT(_redis.incrby(key, 3) == 3, "failed to test incrby"); + REDIS_ASSERT(_redis.decrby(key, 3) == 0, "failed to test decrby"); + REDIS_ASSERT(_redis.incrby(key, -3) == -3, "failed to test incrby"); + REDIS_ASSERT(_redis.decrby(key, -3) == 0, "failed to test incrby"); + REDIS_ASSERT(_redis.incrbyfloat(key, 1.5) == 1.5, "failed to test incrbyfloat"); +} + +template +void StringCmdTest::_test_getset() { + auto key = test_key("getset"); + auto non_exist_key = test_key("non-existent"); + + KeyDeleter deleter(_redis, {key, non_exist_key}); + + std::string val("value"); + REDIS_ASSERT(_redis.set(key, val), "failed to test set"); + + auto v = _redis.get(key); + REDIS_ASSERT(v && *v == val, "failed to test get"); + + v = _redis.getset(key, val + val); + REDIS_ASSERT(v && *v == val, "failed to test get"); + + REDIS_ASSERT(!_redis.set(key, val, std::chrono::milliseconds(0), UpdateType::NOT_EXIST), + "failed to test set with NOT_EXIST type"); + REDIS_ASSERT(!_redis.set(non_exist_key, val, std::chrono::milliseconds(0), UpdateType::EXIST), + "failed to test set with EXIST type"); + + REDIS_ASSERT(!_redis.setnx(key, val), "failed to test setnx"); + REDIS_ASSERT(_redis.setnx(non_exist_key, val), "failed to test setnx"); + + auto ttl = std::chrono::seconds(10); + + _redis.set(key, val, ttl); + REDIS_ASSERT(_redis.ttl(key) <= ttl.count(), "failed to test set key with ttl"); + + _redis.setex(key, ttl, val); + REDIS_ASSERT(_redis.ttl(key) <= ttl.count(), "failed to test setex"); + + auto pttl = std::chrono::milliseconds(10000); + + _redis.psetex(key, ttl, val); + REDIS_ASSERT(_redis.pttl(key) <= pttl.count(), "failed to test psetex"); +} + +template +void StringCmdTest::_test_mgetset() { + auto kvs = {std::make_pair(test_key("k1"), "v1"), + std::make_pair(test_key("k2"), "v2"), + std::make_pair(test_key("k3"), "v3")}; + + std::vector keys; + std::vector vals; + for (const auto &kv : kvs) { + keys.push_back(kv.first); + vals.push_back(kv.second); + } + + KeyDeleter deleter(_redis, keys.begin(), keys.end()); + + _redis.mset(kvs); + + std::vector res; + _redis.mget(keys.begin(), keys.end(), std::back_inserter(res)); + + REDIS_ASSERT(res.size() == kvs.size(), "failed to test mget"); + + std::vector res_vals; + for (const auto &ele : res) { + REDIS_ASSERT(bool(ele), "failed to test mget"); + + res_vals.push_back(*ele); + } + + REDIS_ASSERT(vals == res_vals, "failed to test mget"); + + REDIS_ASSERT(!_redis.msetnx(kvs), "failed to test msetnx"); +} + +} + +} + +} + +#endif // end SEWENEW_REDISPLUSPLUS_TEST_STRING_CMDS_TEST_HPP diff --git a/controller/thirdparty/redis-plus-plus-1.1.1/test/src/sw/redis++/test_main.cpp b/controller/thirdparty/redis-plus-plus-1.1.1/test/src/sw/redis++/test_main.cpp new file mode 100644 index 000000000..dd2880d1a --- /dev/null +++ b/controller/thirdparty/redis-plus-plus-1.1.1/test/src/sw/redis++/test_main.cpp @@ -0,0 +1,303 @@ +/************************************************************************** + Copyright (c) 2017 sewenew + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. + *************************************************************************/ + +#include +#include +#include +#include +#include +#include "sanity_test.h" +#include "connection_cmds_test.h" +#include "keys_cmds_test.h" +#include "string_cmds_test.h" +#include "list_cmds_test.h" +#include "hash_cmds_test.h" +#include "set_cmds_test.h" +#include "zset_cmds_test.h" +#include "hyperloglog_cmds_test.h" +#include "geo_cmds_test.h" +#include "script_cmds_test.h" +#include "pubsub_test.h" +#include "pipeline_transaction_test.h" +#include "threads_test.h" +#include "stream_cmds_test.h" +#include "benchmark_test.h" + +namespace { + +void print_help(); + +auto parse_options(int argc, char **argv) + -> std::tuple, + sw::redis::Optional, + sw::redis::Optional>; + +template +void run_test(const sw::redis::ConnectionOptions &opts); + +template +void run_benchmark(const sw::redis::ConnectionOptions &opts, + const sw::redis::test::BenchmarkOptions &benchmark_opts); + +} + +int main(int argc, char **argv) { + try { + sw::redis::Optional opts; + sw::redis::Optional cluster_node_opts; + sw::redis::Optional benchmark_opts; + std::tie(opts, cluster_node_opts, benchmark_opts) = parse_options(argc, argv); + + if (opts) { + std::cout << "Testing Redis..." << std::endl; + + if (benchmark_opts) { + run_benchmark(*opts, *benchmark_opts); + } else { + run_test(*opts); + } + } + + if (cluster_node_opts) { + std::cout << "Testing RedisCluster..." << std::endl; + + if (benchmark_opts) { + run_benchmark(*cluster_node_opts, *benchmark_opts); + } else { + run_test(*cluster_node_opts); + } + } + + std::cout << "Pass all tests" << std::endl; + } catch (const sw::redis::Error &e) { + std::cerr << "Test failed: " << e.what() << std::endl; + return -1; + } + + return 0; +} + +namespace { + +void print_help() { + std::cerr << "Usage: test_redis++ -h host -p port" + << " -n cluster_node -c cluster_port [-a auth] [-b]\n\n"; + std::cerr << "See https://github.com/sewenew/redis-plus-plus#run-tests-optional" + << " for details on how to run test" << std::endl; +} + +auto parse_options(int argc, char **argv) + -> std::tuple, + sw::redis::Optional, + sw::redis::Optional> { + std::string host; + int port = 0; + std::string auth; + std::string cluster_node; + int cluster_port = 0; + bool benchmark = false; + sw::redis::test::BenchmarkOptions tmp_benchmark_opts; + + int opt = 0; + while ((opt = getopt(argc, argv, "h:p:a:n:c:k:v:r:t:bs:")) != -1) { + try { + switch (opt) { + case 'h': + host = optarg; + break; + + case 'p': + port = std::stoi(optarg); + break; + + case 'a': + auth = optarg; + break; + + case 'n': + cluster_node = optarg; + break; + + case 'c': + cluster_port = std::stoi(optarg); + break; + + case 'b': + benchmark = true; + break; + + case 'k': + tmp_benchmark_opts.key_len = std::stoi(optarg); + break; + + case 'v': + tmp_benchmark_opts.val_len = std::stoi(optarg); + break; + + case 'r': + tmp_benchmark_opts.total_request_num = std::stoi(optarg); + break; + + case 't': + tmp_benchmark_opts.thread_num = std::stoi(optarg); + break; + + case 's': + tmp_benchmark_opts.pool_size = std::stoi(optarg); + break; + + default: + throw sw::redis::Error("Unknow command line option"); + break; + } + } catch (const sw::redis::Error &e) { + print_help(); + throw; + } catch (const std::exception &e) { + print_help(); + throw sw::redis::Error("Invalid command line option"); + } + } + + sw::redis::Optional opts; + if (!host.empty() && port > 0) { + sw::redis::ConnectionOptions tmp; + tmp.host = host; + tmp.port = port; + tmp.password = auth; + + opts = sw::redis::Optional(tmp); + } + + sw::redis::Optional cluster_opts; + if (!cluster_node.empty() && cluster_port > 0) { + sw::redis::ConnectionOptions tmp; + tmp.host = cluster_node; + tmp.port = cluster_port; + tmp.password = auth; + + cluster_opts = sw::redis::Optional(tmp); + } + + if (!opts && !cluster_opts) { + print_help(); + throw sw::redis::Error("Invalid connection options"); + } + + sw::redis::Optional benchmark_opts; + if (benchmark) { + benchmark_opts = sw::redis::Optional(tmp_benchmark_opts); + } + + return std::make_tuple(std::move(opts), std::move(cluster_opts), std::move(benchmark_opts)); +} + +template +void run_test(const sw::redis::ConnectionOptions &opts) { + auto instance = RedisInstance(opts); + + sw::redis::test::SanityTest sanity_test(opts, instance); + sanity_test.run(); + + std::cout << "Pass sanity tests" << std::endl; + + sw::redis::test::ConnectionCmdTest connection_test(instance); + connection_test.run(); + + std::cout << "Pass connection commands tests" << std::endl; + + sw::redis::test::KeysCmdTest keys_test(instance); + keys_test.run(); + + std::cout << "Pass keys commands tests" << std::endl; + + sw::redis::test::StringCmdTest string_test(instance); + string_test.run(); + + std::cout << "Pass string commands tests" << std::endl; + + sw::redis::test::ListCmdTest list_test(instance); + list_test.run(); + + std::cout << "Pass list commands tests" << std::endl; + + sw::redis::test::HashCmdTest hash_test(instance); + hash_test.run(); + + std::cout << "Pass hash commands tests" << std::endl; + + sw::redis::test::SetCmdTest set_test(instance); + set_test.run(); + + std::cout << "Pass set commands tests" << std::endl; + + sw::redis::test::ZSetCmdTest zset_test(instance); + zset_test.run(); + + std::cout << "Pass zset commands tests" << std::endl; + + sw::redis::test::HyperloglogCmdTest hll_test(instance); + hll_test.run(); + + std::cout << "Pass hyperloglog commands tests" << std::endl; + + sw::redis::test::GeoCmdTest geo_test(instance); + geo_test.run(); + + std::cout << "Pass geo commands tests" << std::endl; + + sw::redis::test::ScriptCmdTest script_test(instance); + script_test.run(); + + std::cout << "Pass script commands tests" << std::endl; + + sw::redis::test::PubSubTest pubsub_test(instance); + pubsub_test.run(); + + std::cout << "Pass pubsub tests" << std::endl; + + sw::redis::test::PipelineTransactionTest pipe_tx_test(instance); + pipe_tx_test.run(); + + std::cout << "Pass pipeline and transaction tests" << std::endl; + + sw::redis::test::ThreadsTest threads_test(opts); + threads_test.run(); + + std::cout << "Pass threads tests" << std::endl; + + sw::redis::test::StreamCmdsTest stream_test(instance); + stream_test.run(); + + std::cout << "Pass stream commands tests" << std::endl; +} + +template +void run_benchmark(const sw::redis::ConnectionOptions &opts, + const sw::redis::test::BenchmarkOptions &benchmark_opts) { + std::cout << "Benchmark test options:" << std::endl; + std::cout << " Thread number: " << benchmark_opts.thread_num << std::endl; + std::cout << " Connection pool size: " << benchmark_opts.pool_size << std::endl; + std::cout << " Length of key: " << benchmark_opts.key_len << std::endl; + std::cout << " Length of value: " << benchmark_opts.val_len << std::endl; + + auto instance = RedisInstance(opts); + + sw::redis::test::BenchmarkTest benchmark_test(benchmark_opts, instance); + benchmark_test.run(); +} + +} diff --git a/controller/thirdparty/redis-plus-plus-1.1.1/test/src/sw/redis++/threads_test.h b/controller/thirdparty/redis-plus-plus-1.1.1/test/src/sw/redis++/threads_test.h new file mode 100644 index 000000000..aee307ec2 --- /dev/null +++ b/controller/thirdparty/redis-plus-plus-1.1.1/test/src/sw/redis++/threads_test.h @@ -0,0 +1,51 @@ +/************************************************************************** + Copyright (c) 2017 sewenew + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. + *************************************************************************/ + +#ifndef SEWENEW_REDISPLUSPLUS_TEST_THREADS_TEST_H +#define SEWENEW_REDISPLUSPLUS_TEST_THREADS_TEST_H + +#include + +namespace sw { + +namespace redis { + +namespace test { + +template +class ThreadsTest { +public: + explicit ThreadsTest(const ConnectionOptions &opts) : _opts(opts) {} + + void run(); + +private: + void _test_multithreads(RedisInstance redis, int threads_num, int times); + + void _test_timeout(); + + ConnectionOptions _opts; +}; + +} + +} + +} + +#include "threads_test.hpp" + +#endif // end SEWENEW_REDISPLUSPLUS_TEST_THREADS_TEST_H diff --git a/controller/thirdparty/redis-plus-plus-1.1.1/test/src/sw/redis++/threads_test.hpp b/controller/thirdparty/redis-plus-plus-1.1.1/test/src/sw/redis++/threads_test.hpp new file mode 100644 index 000000000..24bee9454 --- /dev/null +++ b/controller/thirdparty/redis-plus-plus-1.1.1/test/src/sw/redis++/threads_test.hpp @@ -0,0 +1,147 @@ +/************************************************************************** + Copyright (c) 2017 sewenew + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. + *************************************************************************/ + +#ifndef SEWENEW_REDISPLUSPLUS_TEST_THREADS_TEST_HPP +#define SEWENEW_REDISPLUSPLUS_TEST_THREADS_TEST_HPP + +#include +#include +#include +#include +#include "utils.h" + +namespace sw { + +namespace redis { + +namespace test { + +template +void ThreadsTest::run() { + // 100 * 10000 = 1 million writes + auto thread_num = 100; + auto times = 10000; + + // Default pool options: single connection and wait forever. + _test_multithreads(RedisInstance(_opts), thread_num, times); + + // Pool with 10 connections. + ConnectionPoolOptions pool_opts; + pool_opts.size = 10; + _test_multithreads(RedisInstance(_opts, pool_opts), thread_num, times); + + _test_timeout(); +} + +template +void ThreadsTest::_test_multithreads(RedisInstance redis, + int thread_num, + int times) { + std::vector keys; + keys.reserve(thread_num); + for (auto idx = 0; idx != thread_num; ++idx) { + auto key = test_key("multi-threads::" + std::to_string(idx)); + keys.push_back(key); + } + + using DeleterUPtr = std::unique_ptr>; + std::vector deleters; + for (const auto &key : keys) { + deleters.emplace_back(new KeyDeleter(redis, key)); + } + + std::vector workers; + workers.reserve(thread_num); + for (const auto &key : keys) { + workers.emplace_back([&redis, key, times]() { + try { + for (auto i = 0; i != times; ++i) { + redis.incr(key); + } + } catch (...) { + // Something bad happens. + return; + } + }); + } + + for (auto &worker : workers) { + worker.join(); + } + + for (const auto &key : keys) { + auto val = redis.get(key); + REDIS_ASSERT(bool(val), "failed to test multithreads, cannot get value of " + key); + + auto num = std::stoi(*val); + REDIS_ASSERT(num == times, "failed to test multithreads, num: " + + *val + ", times: " + std::to_string(times)); + } +} + +template +void ThreadsTest::_test_timeout() { + using namespace std::chrono; + + ConnectionPoolOptions pool_opts; + pool_opts.size = 1; + pool_opts.wait_timeout = milliseconds(100); + + auto redis = RedisInstance(_opts, pool_opts); + + auto key = test_key("key"); + + std::atomic slow_get_is_running{false}; + auto slow_get = [&slow_get_is_running](Connection &connection, const StringView &key) { + slow_get_is_running = true; + + // Sleep a while to simulate a slow get. + std::this_thread::sleep_for(seconds(5)); + + connection.send("GET %b", key.data(), key.size()); + }; + auto slow_get_thread = std::thread([&redis, slow_get, &key]() { + redis.command(slow_get, key); + }); + + auto get_thread = std::thread([&redis, &slow_get_is_running, &key]() { + try { + while (!slow_get_is_running) { + std::this_thread::sleep_for(milliseconds(10)); + } + + redis.get(key); + + // Slow get is running, this thread should + // timeout before obtaining the connection. + // So it never reaches here. + REDIS_ASSERT(false, "failed to test pool timeout"); + } catch (const Error &err) { + // This thread timeout. + } + }); + + slow_get_thread.join(); + get_thread.join(); +} + +} + +} + +} + +#endif // end SEWENEW_REDISPLUSPLUS_TEST_THREADS_TEST_HPP diff --git a/controller/thirdparty/redis-plus-plus-1.1.1/test/src/sw/redis++/utils.h b/controller/thirdparty/redis-plus-plus-1.1.1/test/src/sw/redis++/utils.h new file mode 100644 index 000000000..7c430e2d4 --- /dev/null +++ b/controller/thirdparty/redis-plus-plus-1.1.1/test/src/sw/redis++/utils.h @@ -0,0 +1,96 @@ +/************************************************************************** + Copyright (c) 2017 sewenew + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. + *************************************************************************/ + +#ifndef SEWENEW_REDISPLUSPLUS_TEST_UTILS_H +#define SEWENEW_REDISPLUSPLUS_TEST_UTILS_H + +#include +#include +#include + +#define REDIS_ASSERT(condition, msg) \ + sw::redis::test::redis_assert((condition), (msg), __FILE__, __LINE__) + +namespace sw { + +namespace redis { + +namespace test { + +inline void redis_assert(bool condition, + const std::string &msg, + const std::string &file, + int line) { + if (!condition) { + auto err_msg = "ASSERT: " + msg + ". " + file + ":" + std::to_string(line); + throw Error(err_msg); + } +} + +inline std::string test_key(const std::string &k) { + // Key prefix with hash tag, + // so that we can call multiple-key commands on RedisCluster. + return "{sw::redis::test}::" + k; +} + +template +void cluster_specializing_test(Test &test, void (Test::*func)(Redis &instance), Redis &instance) { + (test.*func)(instance); +} + +template +void cluster_specializing_test(Test &test, + void (Test::*func)(Redis &instance), + RedisCluster &cluster) { + auto instance = cluster.redis("hash-tag"); + (test.*func)(instance); +} + +template +class KeyDeleter { +public: + template + KeyDeleter(RedisInstance &redis, Input first, Input last) : _redis(redis), _keys(first, last) { + _delete(); + } + + KeyDeleter(RedisInstance &redis, std::initializer_list il) : + KeyDeleter(redis, il.begin(), il.end()) {} + + KeyDeleter(RedisInstance &redis, const std::string &key) : KeyDeleter(redis, {key}) {} + + ~KeyDeleter() { + _delete(); + } + +private: + void _delete() { + if (!_keys.empty()) { + _redis.del(_keys.begin(), _keys.end()); + } + } + + RedisInstance &_redis; + std::vector _keys; +}; + +} + +} + +} + +#endif // end SEWENEW_REDISPLUSPLUS_TEST_UTILS_H diff --git a/controller/thirdparty/redis-plus-plus-1.1.1/test/src/sw/redis++/zset_cmds_test.h b/controller/thirdparty/redis-plus-plus-1.1.1/test/src/sw/redis++/zset_cmds_test.h new file mode 100644 index 000000000..56fd0b96f --- /dev/null +++ b/controller/thirdparty/redis-plus-plus-1.1.1/test/src/sw/redis++/zset_cmds_test.h @@ -0,0 +1,61 @@ +/************************************************************************** + Copyright (c) 2017 sewenew + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. + *************************************************************************/ + +#ifndef SEWENEW_REDISPLUSPLUS_TEST_ZSET_CMDS_TEST_H +#define SEWENEW_REDISPLUSPLUS_TEST_ZSET_CMDS_TEST_H + +#include + +namespace sw { + +namespace redis { + +namespace test { + +template +class ZSetCmdTest { +public: + explicit ZSetCmdTest(RedisInstance &instance) : _redis(instance) {} + + void run(); + +private: + void _test_zset(); + + void _test_zscan(); + + void _test_range(); + + void _test_lex(); + + void _test_multi_zset(); + + void _test_zpop(); + + void _test_bzpop(); + + RedisInstance &_redis; +}; + +} + +} + +} + +#include "zset_cmds_test.hpp" + +#endif // end SEWENEW_REDISPLUSPLUS_TEST_ZSET_CMDS_TEST_H diff --git a/controller/thirdparty/redis-plus-plus-1.1.1/test/src/sw/redis++/zset_cmds_test.hpp b/controller/thirdparty/redis-plus-plus-1.1.1/test/src/sw/redis++/zset_cmds_test.hpp new file mode 100644 index 000000000..fb331bb0a --- /dev/null +++ b/controller/thirdparty/redis-plus-plus-1.1.1/test/src/sw/redis++/zset_cmds_test.hpp @@ -0,0 +1,350 @@ +/************************************************************************** + Copyright (c) 2017 sewenew + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. + *************************************************************************/ + +#ifndef SEWENEW_REDISPLUSPLUS_TEST_ZSET_CMDS_TEST_HPP +#define SEWENEW_REDISPLUSPLUS_TEST_ZSET_CMDS_TEST_HPP + +#include +#include +#include +#include +#include "utils.h" + +namespace sw { + +namespace redis { + +namespace test { + +template +void ZSetCmdTest::run() { + _test_zset(); + + _test_zscan(); + + _test_range(); + + _test_lex(); + + _test_multi_zset(); + + _test_zpop(); + + _test_bzpop(); +} + +template +void ZSetCmdTest::_test_zset() { + auto key = test_key("zset"); + + KeyDeleter deleter(_redis, key); + + std::map s = { + std::make_pair("m1", 1.2), + std::make_pair("m2", 2), + std::make_pair("m3", 3), + }; + + const auto &ele = *(s.begin()); + REDIS_ASSERT(_redis.zadd(key, ele.first, ele.second, UpdateType::EXIST) == 0, + "failed to test zadd with noexistent member"); + + REDIS_ASSERT(_redis.zadd(key, s.begin(), s.end()) == 3, "failed to test zadd"); + + REDIS_ASSERT(_redis.zadd(key, ele.first, ele.second, UpdateType::NOT_EXIST) == 0, + "failed to test zadd with exist member"); + + REDIS_ASSERT(_redis.zadd(key, s.begin(), s.end(), UpdateType::ALWAYS, true) == 0, + "failed to test zadd"); + + REDIS_ASSERT(_redis.zcard(key) == 3, "failed to test zcard"); + + auto rank = _redis.zrank(key, "m2"); + REDIS_ASSERT(bool(rank) && *rank == 1, "failed to test zrank"); + rank = _redis.zrevrank(key, "m4"); + REDIS_ASSERT(!rank, "failed to test zrevrank with nonexistent member"); + + auto score = _redis.zscore(key, "m4"); + REDIS_ASSERT(!score, "failed to test zscore with nonexistent member"); + + REDIS_ASSERT(_redis.zincrby(key, 1, "m3") == 4, "failed to test zincrby"); + + score = _redis.zscore(key, "m3"); + REDIS_ASSERT(score && *score == 4, "failed to test zscore"); + + REDIS_ASSERT(_redis.zrem(key, "m1") == 1, "failed to test zrem"); + REDIS_ASSERT(_redis.zrem(key, {"m1", "m2", "m3", "m4"}) == 2, "failed to test zrem"); +} + +template +void ZSetCmdTest::_test_zscan() { + auto key = test_key("zscan"); + + KeyDeleter deleter(_redis, key); + + std::map s = { + std::make_pair("m1", 1.2), + std::make_pair("m2", 2), + std::make_pair("m3", 3), + }; + _redis.zadd(key, s.begin(), s.end()); + + std::map res; + auto cursor = 0; + while (true) { + cursor = _redis.zscan(key, cursor, "m*", 2, std::inserter(res, res.end())); + if (cursor == 0) { + break; + } + } + REDIS_ASSERT(res == s, "failed to test zscan"); +} + +template +void ZSetCmdTest::_test_range() { + auto key = test_key("range"); + + KeyDeleter deleter(_redis, key); + + std::map s = { + std::make_pair("m1", 1), + std::make_pair("m2", 2), + std::make_pair("m3", 3), + }; + _redis.zadd(key, s.begin(), s.end()); + + REDIS_ASSERT(_redis.zcount(key, UnboundedInterval{}) == 3, "failed to test zcount"); + + std::vector members; + _redis.zrange(key, 0, -1, std::back_inserter(members)); + REDIS_ASSERT(members.size() == s.size(), "failed to test zrange"); + for (const auto &mem : {"m1", "m2", "m3"}) { + REDIS_ASSERT(std::find(members.begin(), members.end(), mem) != members.end(), + "failed to test zrange"); + } + + std::map res; + _redis.zrange(key, 0, -1, std::inserter(res, res.end())); + REDIS_ASSERT(s == res, "failed to test zrange with score"); + + members.clear(); + _redis.zrevrange(key, 0, 0, std::back_inserter(members)); + REDIS_ASSERT(members.size() == 1 && members[0] == "m3", "failed to test zrevrange"); + + res.clear(); + _redis.zrevrange(key, 0, 0, std::inserter(res, res.end())); + REDIS_ASSERT(res.size() == 1 && res.find("m3") != res.end() && res["m3"] == 3, + "failed to test zrevrange with score"); + + members.clear(); + _redis.zrangebyscore(key, UnboundedInterval{}, std::back_inserter(members)); + REDIS_ASSERT(members.size() == s.size(), "failed to test zrangebyscore"); + for (const auto &mem : {"m1", "m2", "m3"}) { + REDIS_ASSERT(std::find(members.begin(), members.end(), mem) != members.end(), + "failed to test zrangebyscore"); + } + + members.clear(); + _redis.zrangebyscore(key, + BoundedInterval(1, 2, BoundType::RIGHT_OPEN), + std::back_inserter(members)); + REDIS_ASSERT(members.size() == 1 && members[0] == "m1", "failed to test zrangebyscore"); + + res.clear(); + _redis.zrangebyscore(key, + LeftBoundedInterval(2, BoundType::OPEN), + std::inserter(res, res.end())); + REDIS_ASSERT(res.size() == 1 && res.find("m3") != res.end() && res["m3"] == 3, + "failed to test zrangebyscore"); + + members.clear(); + _redis.zrevrangebyscore(key, + BoundedInterval(1, 3, BoundType::CLOSED), + std::back_inserter(members)); + REDIS_ASSERT(members == std::vector({"m3", "m2", "m1"}), + "failed to test zrevrangebyscore"); + + res.clear(); + _redis.zrevrangebyscore(key, + RightBoundedInterval(1, BoundType::LEFT_OPEN), + std::inserter(res, res.end())); + REDIS_ASSERT(res.size() == 1 && res.find("m1") != res.end() && res["m1"] == 1, + "failed to test zrevrangebyscore"); + + REDIS_ASSERT(_redis.zremrangebyrank(key, 0, 0) == 1, "failed to test zremrangebyrank"); + + REDIS_ASSERT(_redis.zremrangebyscore(key, + BoundedInterval(2, 3, BoundType::LEFT_OPEN)) == 1, + "failed to test zremrangebyscore"); +} + +template +void ZSetCmdTest::_test_lex() { + auto key = test_key("lex"); + + KeyDeleter deleter(_redis, key); + + auto s = { + std::make_pair("m1", 0), + std::make_pair("m2", 0), + std::make_pair("m3", 0), + }; + _redis.zadd(key, s.begin(), s.end()); + + REDIS_ASSERT(_redis.zlexcount(key, UnboundedInterval{}) == 3, + "failed to test zlexcount"); + + std::vector members; + _redis.zrangebylex(key, + LeftBoundedInterval("m2", BoundType::OPEN), + std::back_inserter(members)); + REDIS_ASSERT(members.size() == 1 && members[0] == "m3", + "failed to test zrangebylex"); + + members.clear(); + _redis.zrevrangebylex(key, + RightBoundedInterval("m1", BoundType::LEFT_OPEN), + std::back_inserter(members)); + REDIS_ASSERT(members.size() == 1 && members[0] == "m1", + "failed to test zrevrangebylex"); + + REDIS_ASSERT(_redis.zremrangebylex(key, + BoundedInterval("m1", "m3", BoundType::OPEN)) == 1, + "failed to test zremrangebylex"); +} + +template +void ZSetCmdTest::_test_multi_zset() { + auto k1 = test_key("k1"); + auto k2 = test_key("k2"); + auto k3 = test_key("k3"); + + KeyDeleter deleter(_redis, {k1, k2, k3}); + + _redis.zadd(k1, {std::make_pair("a", 1), std::make_pair("b", 2)}); + _redis.zadd(k2, {std::make_pair("a", 2), std::make_pair("c", 3)}); + + REDIS_ASSERT(_redis.zinterstore(k3, {k1, k2}) == 1, "failed to test zinterstore"); + auto score = _redis.zscore(k3, "a"); + REDIS_ASSERT(bool(score) && *score == 3, "failed to test zinterstore"); + + REDIS_ASSERT(_redis.zinterstore(k3, k1, 2) == 2, "failed to test zinterstore"); + + _redis.del(k3); + + REDIS_ASSERT(_redis.zinterstore(k3, {k1, k2}, Aggregation::MAX) == 1, + "failed to test zinterstore"); + score = _redis.zscore(k3, "a"); + REDIS_ASSERT(bool(score) && *score == 2, "failed to test zinterstore"); + + _redis.del(k3); + + REDIS_ASSERT(_redis.zunionstore(k3, + {std::make_pair(k1, 1), std::make_pair(k2, 2)}, + Aggregation::MIN) == 3, + "failed to test zunionstore"); + std::vector> res; + _redis.zrange(k3, 0, -1, std::back_inserter(res)); + for (const auto &ele : res) { + if (ele.first == "a") { + REDIS_ASSERT(ele.second == 1, "failed to test zunionstore"); + } else if (ele.first == "b") { + REDIS_ASSERT(ele.second == 2, "failed to test zunionstore"); + } else if (ele.first == "c") { + REDIS_ASSERT(ele.second == 6, "failed to test zunionstore"); + } else { + REDIS_ASSERT(false, "failed to test zuionstore"); + } + } + + REDIS_ASSERT(_redis.zunionstore(k3, k1, 2) == 2, "failed to test zunionstore"); +} + +template +void ZSetCmdTest::_test_zpop() { + auto key = test_key("zpop"); + + KeyDeleter deleter(_redis, key); + + _redis.zadd(key, {std::make_pair("m1", 1.1), + std::make_pair("m2", 2.2), + std::make_pair("m3", 3.3), + std::make_pair("m4", 4.4), + std::make_pair("m5", 5.5), + std::make_pair("m6", 6.6)}); + + auto item = _redis.zpopmax(key); + REDIS_ASSERT(item && item->first == "m6", "failed to test zpopmax"); + + item = _redis.zpopmin(key); + REDIS_ASSERT(item && item->first == "m1", "failed to test zpopmin"); + + std::vector> vec; + _redis.zpopmax(key, 2, std::back_inserter(vec)); + REDIS_ASSERT(vec.size() == 2 && vec[0].first == "m5" && vec[1].first == "m4", + "failed to test zpopmax"); + + std::unordered_map m; + _redis.zpopmin(key, 2, std::inserter(m, m.end())); + REDIS_ASSERT(m.size() == 2 && m.find("m3") != m.end() && m.find("m2") != m.end(), + "failed to test zpopmin"); +} + +template +void ZSetCmdTest::_test_bzpop() { + auto key1 = test_key("bzpop1"); + auto key2 = test_key("bzpop2"); + + KeyDeleter deleter(_redis, {key1, key2}); + + _redis.zadd(key1, {std::make_pair("m1", 1.1), + std::make_pair("m2", 2.2), + std::make_pair("m3", 3.3), + std::make_pair("m4", 4.4), + std::make_pair("m5", 5.5), + std::make_pair("m6", 6.6)}); + + _redis.zadd(key2, {std::make_pair("m1", 1.1), + std::make_pair("m2", 2.2), + std::make_pair("m3", 3.3), + std::make_pair("m4", 4.4), + std::make_pair("m5", 5.5), + std::make_pair("m6", 6.6)}); + + auto item = _redis.bzpopmax(key1); + REDIS_ASSERT(item && std::get<0>(*item) == key1 && std::get<1>(*item) == "m6", + "failed to test bzpopmax"); + + item = _redis.bzpopmin(key1, std::chrono::seconds(1)); + REDIS_ASSERT(item && std::get<0>(*item) == key1 && std::get<1>(*item) == "m1", + "failed to test zpopmin"); + + item = _redis.bzpopmax({key1, key2}, std::chrono::seconds(1)); + REDIS_ASSERT(item && std::get<0>(*item) == key1 && std::get<1>(*item) == "m5", + "failed to test zpopmax"); + + item = _redis.bzpopmin({key2, key1}); + REDIS_ASSERT(item && std::get<0>(*item) == key2 && std::get<1>(*item) == "m1", + "failed to test zpopmin"); +} + +} + +} + +} + +#endif // end SEWENEW_REDISPLUSPLUS_TEST_ZSET_CMDS_TEST_HPP