acl/lib_acl_cpp/include/acl_cpp/redis/redis_string.hpp

535 lines
23 KiB
C++
Raw Normal View History

#pragma once
2017-06-02 14:47:24 +08:00
#include "../acl_cpp_define.hpp"
2015-01-14 23:35:23 +08:00
#include <map>
#include <vector>
2017-06-02 14:47:24 +08:00
#include "redis_command.hpp"
2015-01-14 23:35:23 +08:00
#if !defined(ACL_CLIENT_ONLY) && !defined(ACL_REDIS_DISABLE)
2015-01-14 23:35:23 +08:00
namespace acl
{
class string;
class redis_client;
class redis_result;
2015-01-29 00:38:29 +08:00
/**
*
2015-04-14 17:34:24 +08:00
* all the commands in redis Strings are be implemented.
2015-01-29 00:38:29 +08:00
*/
class ACL_CPP_API redis_string : virtual public redis_command
2015-01-14 23:35:23 +08:00
{
public:
2015-03-29 19:27:41 +08:00
/**
* see redis_command::redis_command()
*/
redis_string(void);
2015-03-29 19:27:41 +08:00
/**
* see redis_command::redis_command(redis_client*)
*/
redis_string(redis_client* conn);
2015-03-29 19:27:41 +08:00
/**
2016-05-22 09:10:34 +08:00
* see redis_command::redis_command(redis_client_cluster*, size_t)
2015-03-29 19:27:41 +08:00
*/
redis_string(redis_client_cluster* cluster, size_t max_conns = 0);
virtual ~redis_string(void);
2015-01-14 23:35:23 +08:00
/////////////////////////////////////////////////////////////////////
2015-01-29 00:38:29 +08:00
/**
* value key
2015-04-14 17:34:24 +08:00
* set the string value of a key
* @param key {const char*} key
2015-04-14 17:34:24 +08:00
* the key of a string
* @param value {const char*} value
2015-04-14 17:34:24 +08:00
* the value of a string
* @return {bool} false key
2015-04-14 17:34:24 +08:00
* true if SET was executed correctly, false if error happened or
* the key's object isn't a string.
2015-01-29 00:38:29 +08:00
*/
2015-01-14 23:35:23 +08:00
bool set(const char* key, const char* value);
bool set(const char* key, size_t key_len,
const char* value, size_t value_len);
2015-01-29 00:38:29 +08:00
/**
* value key key timeout ()
* key SETEX
2015-04-14 17:34:24 +08:00
* set key to hold the strnig value, and set key to timeout after
* a given number of seconds.
* @param key {const char*} key
2015-04-14 17:34:24 +08:00
* the key of a string
* @param value {const char*} value
2015-04-14 17:34:24 +08:00
* the value of a string
* @param timeout {int}
2015-04-14 17:34:24 +08:00
* the timeout in seconds of a string
* @return {bool} false key
2015-04-14 17:34:24 +08:00
* true if SETEX was executed correctly, false if error happened
* or the object specified by the key is not a string
2015-01-29 00:38:29 +08:00
*/
2015-01-14 23:35:23 +08:00
bool setex(const char* key, const char* value, int timeout);
bool setex(const char* key, size_t key_len, const char* value,
size_t value_len, int timeout);
/**
* value key key timeout ()
* key SETEX
2015-04-14 17:34:24 +08:00
* set key to hold the string value, and set key to timeout after
* a given number of milliseconds.
* @param key {const char*} key
2015-04-14 17:34:24 +08:00
* the key of a string
* @param value {const char*} value
2015-04-14 17:34:24 +08:00
* the value of a string
* @param timeout {int}
2015-04-14 17:34:24 +08:00
* the timeout in milliseconds of a string
* @return {bool} false key
2015-04-14 17:34:24 +08:00
* true if SETEX was executed correctly, false if error happened
* or the object specified by the key is not a string
*/
bool psetex(const char* key, const char* value, int timeout);
bool psetex(const char* key, size_t key_len, const char* value,
size_t value_len, int timeout);
2015-01-29 00:38:29 +08:00
/**
* key value key key
* SETNX
2015-04-14 17:34:24 +08:00
* set the value of a key, only if the key does not exist.
* @param key {const char*} key
2015-04-14 17:34:24 +08:00
* the key of the string
* @param value {const char*} value
2015-04-14 17:34:24 +08:00
* the value of the string
* @return {int}
2015-04-14 17:34:24 +08:00
* return the value as below:
* -1 key
2015-04-14 17:34:24 +08:00
* error happened or the object by the key isn't a string
* 0 key
2015-04-14 17:34:24 +08:00
* the string of the key already exists
* 1
2015-04-14 17:34:24 +08:00
* the command was executed correctly
2015-01-29 00:38:29 +08:00
*/
2015-01-14 23:35:23 +08:00
int setnx(const char* key, const char* value);
int setnx(const char* key, size_t key_len,
const char* value, size_t value_len);
2015-01-29 00:38:29 +08:00
/**
* key APPEND value key
* key APPEND key value
2015-04-14 17:34:24 +08:00
* append a value to a key
* @param key {const char*} key
2015-04-14 17:34:24 +08:00
* the key of a string
* @param value {const char*}
2015-04-14 17:34:24 +08:00
* the value to be appended to a key
* @return {int} -1 key
2015-04-14 17:34:24 +08:00
* return the length of the string after appending, -1 if error
* happened or the key's object isn't a string
2015-01-29 00:38:29 +08:00
*/
2015-01-14 23:35:23 +08:00
int append(const char* key, const char* value);
int append(const char* key, const char* value, size_t size);
2015-01-29 00:38:29 +08:00
/**
* key
2015-04-14 17:34:24 +08:00
* get the value of a key
* @param key {const char*} key
2015-04-14 17:34:24 +08:00
* the key of a string
* @param buf {string&} true
* key
* store the value of a key after GET executed correctly, key not
* exist if the buf is empty when return true
* @return {bool} false key
2015-04-14 17:34:24 +08:00
* if the GET was executed correctly, false if error happened or
* is is not a string of the key
2015-01-29 00:38:29 +08:00
*/
2015-01-14 23:35:23 +08:00
bool get(const char* key, string& buf);
bool get(const char* key, size_t len, string& buf);
2015-01-29 00:38:29 +08:00
/**
* key
* 使
* redis_result::get(size_t, size_t*)
* redis_result::get_size()
* @param key {const char*} key
* @param buf {string&}
* @return {bool} false key
2015-01-29 00:38:29 +08:00
*/
2015-01-14 23:35:23 +08:00
const redis_result* get(const char* key);
const redis_result* get(const char* key, size_t len);
2015-01-14 23:35:23 +08:00
2015-01-29 00:38:29 +08:00
/**
* key value key key
*
2015-04-14 22:03:56 +08:00
* set the string value of a key and and return its old value
* @param key {const char*} key
2015-04-14 22:03:56 +08:00
* the key of string
* @param value {const char*}
2015-04-14 22:03:56 +08:00
* the new string value of the key
* @param buf {string&}
2015-04-14 22:03:56 +08:00
* store the old string value of the key
* @return {bool}
2015-04-14 22:03:56 +08:00
* if GETSET was executed correctly.
2015-01-29 00:38:29 +08:00
*/
2015-01-14 23:35:23 +08:00
bool getset(const char* key, const char* value, string& buf);
2015-01-29 00:38:29 +08:00
bool getset(const char* key, size_t key_len, const char* value,
size_t value_len, string& buf);
2015-01-14 23:35:23 +08:00
/////////////////////////////////////////////////////////////////////
2015-01-29 00:38:29 +08:00
/**
* key
2015-04-14 22:03:56 +08:00
* get the length of value stored in a key
* @param key {const char*} key
2015-04-14 22:03:56 +08:00
* the key of the string
* @return {int}
2015-04-14 22:03:56 +08:00
* return value as below:
* -1
2015-04-14 22:03:56 +08:00
* error happened or the it isn't a string of the key
* 0 key
2015-04-14 22:03:56 +08:00
* the key doesn't exist
* >0
2015-04-14 22:03:56 +08:00
* the length of the value stored in a key
2015-01-29 00:38:29 +08:00
*/
int get_strlen(const char* key);
2015-04-14 22:03:56 +08:00
int get_strlen(const char* key, size_t key_len);
2015-01-29 00:38:29 +08:00
/**
* value (overwrite) key offset
* key
2015-04-14 22:03:56 +08:00
* overwrite part of a string at key starting at the specified offset
* @param key {const char*} key
2015-04-14 22:03:56 +08:00
* the key of a string
* @param offset {unsigned}
* \0
2015-04-14 22:03:56 +08:00
* the specified offset of the string
* @param value {const char*}
2015-04-14 22:03:56 +08:00
* the value to be set
* @return {int}
2015-04-14 22:03:56 +08:00
* the length of the string after SETRANGE
2015-01-29 00:38:29 +08:00
*/
2015-01-14 23:35:23 +08:00
int setrange(const char* key, unsigned offset, const char* value);
int setrange(const char* key, size_t key_len, unsigned offset,
const char* value, size_t value_len);
2015-01-29 00:38:29 +08:00
/**
* key start end
* ( start end )
2015-04-14 22:03:56 +08:00
* get substring of the string stored at a key
* @param key {const char*} key
2015-04-14 22:03:56 +08:00
* the key of string
* @param start {int}
2015-04-14 22:03:56 +08:00
* the starting offset of the string
* @param end {int}
2015-04-14 22:03:56 +08:00
* the ending offset of the string
* @param buf {string&}
2015-04-14 22:03:56 +08:00
* store the substring result
* @return {bool}
2015-04-14 22:03:56 +08:00
* if GETRANGE was executed correctly.
* -1
2015-01-29 00:38:29 +08:00
*/
2015-01-14 23:35:23 +08:00
bool getrange(const char* key, int start, int end, string& buf);
bool getrange(const char* key, size_t key_len,
int start, int end, string& buf);
/////////////////////////////////////////////////////////////////////
2015-01-29 00:38:29 +08:00
/**
* key (bit)
* value 0 1
2015-04-14 22:03:56 +08:00
* set or clear the bit at offset in the string value stored at key
* @param key {const char*} key
2015-04-14 22:03:56 +08:00
* the key of the string
* @param offset {unsigned}
2015-04-14 22:03:56 +08:00
* the offset at the string value
* @param bit {bool} true
2015-04-14 22:03:56 +08:00
* set bit if true, or clear bit if false at the specified offset
* @return {bool}
2015-04-14 22:03:56 +08:00
* if the command was executed correctly
2015-01-29 00:38:29 +08:00
*/
2015-08-22 18:05:46 +08:00
bool setbit_(const char* key, unsigned offset, bool bit);
bool setbit_(const char* key, size_t len, unsigned offset, bool bit);
2015-01-29 00:38:29 +08:00
/**
* key (bit) offset
* key 0
2015-04-14 22:03:56 +08:00
* get the bit at offset in the string value stored at key
* @param key {const char*} key
2015-04-14 22:03:56 +08:00
* the key of the string
* @param offset {unsigned}
2015-04-14 22:03:56 +08:00
* the offset in the string value
* @param bit {int&}
2015-04-14 22:03:56 +08:00
* on success it will stored the bit at the specified offset
* @return {bool} false key
2015-04-14 22:03:56 +08:00
* if the GETBIT was executed correctly, false if error happened,
* or the key doesn't store a string object
2015-01-29 00:38:29 +08:00
*/
2015-01-14 23:35:23 +08:00
bool getbit(const char* key, unsigned offset, int& bit);
bool getbit(const char* key, size_t len, unsigned offset, int& bit);
2015-01-29 00:38:29 +08:00
/**
* 1 start/end
*
2015-04-14 22:03:56 +08:00
* count set bits in a string
* @param key {const char*} key
2015-04-14 22:03:56 +08:00
* the key of a string
* @return {int} 1 -1
2015-04-14 22:03:56 +08:00
* the count of bits been set, -1 if error happened or it's not
* a string
2015-01-29 00:38:29 +08:00
*/
2015-01-14 23:35:23 +08:00
int bitcount(const char* key);
int bitcount(const char* key, size_t len);
int bitcount(const char* key, int start, int end);
int bitcount(const char* key, size_t len, int start, int end);
2015-01-29 00:38:29 +08:00
/**
* key destkey
2015-04-14 22:03:56 +08:00
* BITOP AND on multiple source keys and save the result to another key
* @param destkey {const char*} key
2015-04-14 22:03:56 +08:00
* the key storing the result
* @param keys
2015-04-14 22:03:56 +08:00
* the source keys
* @return {int} key
2015-04-14 22:03:56 +08:00
* the size of the string stored in the destination key, that is
* equal to the size of the longest input string
2015-01-29 00:38:29 +08:00
*/
2015-01-14 23:35:23 +08:00
int bitop_and(const char* destkey, const std::vector<string>& keys);
int bitop_and(const char* destkey, const std::vector<const char*>& keys);
int bitop_and(const char* destkey, const char* key, ...);
int bitop_and(const char* destkey, const char* keys[], size_t size);
2015-01-29 00:38:29 +08:00
/**
* key destkey
2015-04-14 22:03:56 +08:00
* BITOP OR on multiple source keys and save the result to another key
* @param destkey {const char*} key
2015-04-14 22:03:56 +08:00
* the destination key
* @param keys
2015-04-14 22:03:56 +08:00
* the source keys
2015-01-29 00:38:29 +08:00
* @return {int}
2015-04-14 22:03:56 +08:00
* the size of the string stored in the destination key
2015-01-29 00:38:29 +08:00
*/
int bitop_or(const char* destkey, const std::vector<string>& keys);
int bitop_or(const char* destkey, const std::vector<const char*>& keys);
int bitop_or(const char* destkey, const char* key, ...);
2015-01-14 23:35:23 +08:00
int bitop_or(const char* destkey, const char* keys[], size_t size);
2015-01-29 00:38:29 +08:00
/**
* key destkey
2015-04-14 22:03:56 +08:00
* BITOP XOR on multiple source keys and save the result to another key
* @param destkey {const char*} key
2015-04-14 22:03:56 +08:00
* the destination key
* @param keys
2015-04-14 22:03:56 +08:00
* the source keys
2015-01-29 00:38:29 +08:00
* @return {int}
2015-04-14 22:03:56 +08:00
* the size of the string stored in the destination key
2015-01-29 00:38:29 +08:00
*/
int bitop_xor(const char* destkey, const std::vector<string>& keys);
int bitop_xor(const char* destkey, const std::vector<const char*>& keys);
int bitop_xor(const char* destkey, const char* key, ...);
2015-01-14 23:35:23 +08:00
int bitop_xor(const char* destkey, const char* keys[], size_t size);
/////////////////////////////////////////////////////////////////////
2015-01-29 00:38:29 +08:00
/**
* key-value
2015-04-14 22:03:56 +08:00
* set multiple key-value pair
* @param objs key-value
2015-04-14 22:03:56 +08:00
* the collection of multiple key-value pair
* @return {bool}
2015-04-14 22:03:56 +08:00
* if the command was executed correctly
2015-01-29 00:38:29 +08:00
*/
2015-01-14 23:35:23 +08:00
bool mset(const std::map<string, string>& objs);
bool mset(const std::vector<string>& keys,
const std::vector<string>& values);
bool mset(const char* keys[], const char* values[], size_t argc);
bool mset(const char* keys[], const size_t keys_len[],
const char* values[], const size_t values_len[], size_t argc);
/////////////////////////////////////////////////////////////////////
2015-01-29 00:38:29 +08:00
/**
* key key-value
2015-04-14 22:03:56 +08:00
* set multiple keys to multiple values only if none of the keys exist
* @param objs key-value
2015-04-14 22:03:56 +08:00
* the collection of multile key-value pair
* @return {int}
2015-04-14 22:03:56 +08:00
* return value as below:
* -1
2015-04-14 22:03:56 +08:00
* error happened or there were a object of not a string.
* 0 key
2015-04-14 22:03:56 +08:00
* none be set because some of the keys already exist
* 1
2015-04-14 22:03:56 +08:00
* add ok.
2015-01-29 00:38:29 +08:00
*/
2015-01-14 23:35:23 +08:00
int msetnx(const std::map<string, string>& objs);
int msetnx(const std::vector<string>& keys,
const std::vector<string>& values);
int msetnx(const char* keys[], const char* values[], size_t argc);
int msetnx(const char* keys[], const size_t keys_len[],
const char* values[], const size_t values_len[], size_t argc);
/////////////////////////////////////////////////////////////////////
2015-01-29 00:38:29 +08:00
/**
* () key key key
* key
2015-04-14 22:03:56 +08:00
* get the values of the given keys
* @param keys {const std::vector<string>&} key
2015-04-14 22:03:56 +08:00
* the given keys
* @param out {std::vector<acl::string>*}
* key
2015-04-14 22:03:56 +08:00
* acl::string array storing the result. if one key not exists,
* a empty string "" will also be stored in the array.
* @return {bool}
2015-04-14 22:03:56 +08:00
* if successul, one of below ways can be used to get the result:
*
* 1
2015-04-14 22:03:56 +08:00
* input the no-NULL result parameter when call hmget, when
* success, the result will store the values of the given fileds
*
* 2 get_value
2015-04-14 22:03:56 +08:00
* call redis_command::result_value with the specified subscript
*
* 3 get_child (redis_result
* redis_result::argv_to_string
* redis_result::argv_to_string
2015-04-14 22:03:56 +08:00
* call redis_command::result_child with specified subscript to
* get redis_result object, then call redis_result::argv_to_string
* with above result to get the values of the give fileds
*
* 4 get_result redis_result
* redis_result::get_child 2
*
2015-04-14 22:03:56 +08:00
* call redis_command::get_result with the specified subscript to
* get redis_result object, and use redis_result::get_child to
* get one result object, then call redis_result::argv_to_string
* to get the value of one filed.
*
* 5 get_children redis_result
* argv_to_string
2015-04-14 22:03:56 +08:00
* use redis_command::get_children to get the redis_result array,
* then use redis_result::argv_to_string to get every value of
* the given fileds
2015-01-29 00:38:29 +08:00
*/
2015-01-14 23:35:23 +08:00
bool mget(const std::vector<string>& keys,
std::vector<string>* out = NULL);
2015-01-14 23:35:23 +08:00
bool mget(const std::vector<const char*>& keys,
std::vector<string>* out = NULL);
2015-01-14 23:35:23 +08:00
2015-01-30 22:58:13 +08:00
bool mget(std::vector<string>* result, const char* first_key, ...);
2015-01-14 23:35:23 +08:00
bool mget(const char* keys[], size_t argc,
std::vector<string>* out = NULL);
2015-01-14 23:35:23 +08:00
bool mget(const char* keys[], const size_t keys_len[], size_t argc,
std::vector<string>* out = NULL);
2015-01-14 23:35:23 +08:00
/////////////////////////////////////////////////////////////////////
2015-01-29 00:38:29 +08:00
/**
* key
* 1 key key 0 INCR
* 2
* 3 64 (bit)
2015-04-14 22:03:56 +08:00
* increment the integer value of a key by one
* 1) if key not exists, the key's value will be set 0 and INCR
* 2) if key's value is not a number an error will be returned
* 3) the number is a 64 signed integer
* @param key {const char*} key
2015-04-14 22:03:56 +08:00
* the given key
* @param result {long long int*}
2015-04-14 22:03:56 +08:00
* store the result after INCR if it isn't NULL
* @return {bool}
2015-04-14 22:03:56 +08:00
* if the INCR was executed correctly
2015-01-29 00:38:29 +08:00
*/
2015-01-14 23:35:23 +08:00
bool incr(const char* key, long long int* result = NULL);
2015-01-29 00:38:29 +08:00
/**
* key increment
* 1 key key 0 INCRBY
* 2
* 3 64 (bit)
2015-04-14 22:03:56 +08:00
* increment the integer value of a key by a given amount
* 1) if key not exists, the key's value will be set 0 and INCRBY
* 2) if key's value is not a number an error will be returned
* 3) the number is a 64 signed integer
* @param key {const char*} key
2015-04-14 22:03:56 +08:00
* the given key
* @param inc {long long int}
2015-04-14 22:03:56 +08:00
* the given amount
* @param result {long long int*}
2015-04-14 22:03:56 +08:00
* store the result after INCR if it isn't NULL
* @return {bool}
2015-04-14 22:03:56 +08:00
* if the INCRBY was executed correctly
2015-01-29 00:38:29 +08:00
*/
2015-01-14 23:35:23 +08:00
bool incrby(const char* key, long long int inc,
long long int* result = NULL);
2015-01-29 00:38:29 +08:00
/**
* key
* 1) key INCRBYFLOAT key 0
* 2) key
*
* 3)
2015-04-14 22:03:56 +08:00
* increment the float value of a key by the given amount
* 1) if key not exists, the key's value will be set 0 and INCRBYFLOAT
* 2) if key's value is not a float an error will be returned
* @param key {const char*} key
2015-04-14 22:03:56 +08:00
* the given key
* @param inc {double}
2015-04-14 22:03:56 +08:00
* the given amount
* @param result {double*}
2015-04-14 22:03:56 +08:00
* store the result after INCR if it isn't NULL
* @return {bool}
2015-04-14 22:03:56 +08:00
* if the INCRBYFLOAT was executed correctly
2015-01-29 00:38:29 +08:00
*/
2015-01-14 23:35:23 +08:00
bool incrbyfloat(const char* key, double inc, double* result = NULL);
2015-01-29 00:38:29 +08:00
/**
* key
* 1) key key 0 DECR
* 2)
* 3) 64 (bit)
2015-04-14 22:03:56 +08:00
* decrement the integer value of a key by one
* 1) if key not exists, the key's value will be set 0 and DECR
* 2) if key's value is not a number an error will be returned
* 3) the number is a 64 signed integer
* @param key {const char*} key
2015-04-14 22:03:56 +08:00
* the given key
* @param result {long long int*}
2015-04-14 22:03:56 +08:00
* store the result after INCR if it isn't NULL
* @return {bool}
2015-04-14 22:03:56 +08:00
* if the DECR was executed correctly
2015-01-29 00:38:29 +08:00
*/
2015-01-14 23:35:23 +08:00
bool decr(const char* key, long long int* result = NULL);
2015-01-29 00:38:29 +08:00
/**
* key decrement
* 1) key key 0 DECRBY
* 2)
* 3) 64 (bit)
2015-04-14 22:03:56 +08:00
* decrement the integer value of a key by the given amount
* @param key {const char*} key
2015-04-14 22:03:56 +08:00
* the given key
* @param dec {long long int}
2015-04-14 22:03:56 +08:00
* the given amount
* @param result {long long int*}
2015-04-14 22:03:56 +08:00
* store the result after INCR if it isn't NULL
* @return {bool}
2015-04-14 22:03:56 +08:00
* if the DECRBY was executed correctly
2015-01-29 00:38:29 +08:00
*/
2015-01-14 23:35:23 +08:00
bool decrby(const char* key, long long int dec,
long long int* result = NULL);
private:
int bitop(const char* op, const char* destkey,
const std::vector<string>& keys);
int bitop(const char* op, const char* destkey,
const std::vector<const char*>& keys);
int bitop(const char* op, const char* destkey,
const char* keys[], size_t size);
2015-01-29 00:38:29 +08:00
bool incoper(const char* cmd, const char* key, long long int* inc,
2015-01-29 00:38:29 +08:00
long long int* result);
2015-01-14 23:35:23 +08:00
};
} // namespace acl
#endif // !defined(ACL_CLIENT_ONLY) && !defined(ACL_REDIS_DISABLE)