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

366 lines
16 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 "../stdlib/string.hpp"
#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 redis_client;
2015-01-26 19:58:02 +08:00
/**
* redis Hash()
2015-04-12 21:37:17 +08:00
* redis Hash class, include commands as below:
2015-01-26 19:58:02 +08:00
* HDEL/HEXISTS/HGET/HGETALL/HINCRBY/HINCRBYFLOAT/HKEYS/HLEN/HMGET/HMSET
* HSET/HSETNX/HVALS/HSCAN
*/
class ACL_CPP_API redis_hash : 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_hash(void);
2015-03-29 19:27:41 +08:00
/**
* see redis_command::redis_command(redis_client*)
*/
redis_hash(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_hash(redis_client_cluster* cluster, size_t max_conns = 0);
2015-03-29 19:27:41 +08:00
virtual ~redis_hash(void);
2015-01-14 23:35:23 +08:00
/////////////////////////////////////////////////////////////////////
/**
* "域-值" KEY
2015-04-12 21:37:17 +08:00
* HMSET: set the key's multiple fileds in redis-server
* @param key {const char*} key
2015-04-12 21:37:17 +08:00
* the hash key for Hash class
* @param attrs {const std::map<acl::string, ...>&} the fileds in map
* @return {bool}
2015-04-12 21:37:17 +08:00
* if successful for HMSET command
2015-01-14 23:35:23 +08:00
*/
bool hmset(const char* key, const std::map<string, string>& attrs);
bool hmset(const char* key, const std::map<string, const char*>& attrs);
bool hmset(const char* key, const std::vector<string>& names,
const std::vector<string>& values);
bool hmset(const char* key, const std::vector<const char*>& names,
const std::vector<const char*>& values);
bool hmset(const char* key, const char* names[], const char* values[],
size_t argc);
bool hmset(const char* key, const char* names[], const size_t names_len[],
const char* values[], const size_t values_len[], size_t argc);
2015-01-14 23:35:23 +08:00
/////////////////////////////////////////////////////////////////////
/**
* KEY "域-值"
2015-04-12 21:37:17 +08:00
* get the values associated with the specified fields
* in the hash stored at key
* @param key {const char*} key
2015-04-12 21:37:17 +08:00
* the hash key
* @param names key
2015-04-12 21:37:17 +08:00
* the given hash fileds
* @param result {std::vector<acl::string>*}
* NULL result_/get_
2015-04-12 21:37:17 +08:00
* store the result of the given hash files if not NULL.
* If NULL, the base class's method like result_/get can be used
* to get the values
* @return {bool}
2015-04-12 21:37:17 +08:00
* if successul, one of below ways can be used to get the result:
*
* 1
2015-04-12 21:37:17 +08:00
* input the no-NULL result parameter when call hmget, when
* success, the result will store the values of the given fileds
*
* 2 result_value
2015-04-12 21:37:17 +08:00
* call redis_command::result_value with the specified subscript
*
* 3 result_child (redis_result
* redis_result::argv_to_string
2015-04-12 21:37:17 +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-12 21:37:17 +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-12 21:37:17 +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-14 23:35:23 +08:00
*/
bool hmget(const char* key, const std::vector<string>& names,
std::vector<string>* result = NULL);
bool hmget(const char* key, const std::vector<const char*>& names,
std::vector<string>* result = NULL);
2015-01-14 23:35:23 +08:00
bool hmget(const char* key, const char* names[], size_t argc,
std::vector<string>* result = NULL);
bool hmget(const char* key, const char* names[], const size_t lens[],
size_t argc, std::vector<string>* result = NULL);
2015-01-14 23:35:23 +08:00
/////////////////////////////////////////////////////////////////////
2015-01-26 19:58:02 +08:00
/**
* key
2015-04-12 21:37:17 +08:00
* set one field's value in the hash stored at key.
* @param key {const char*} key
2015-04-12 21:37:17 +08:00
* the hash key
* @param name {const char*} key
2015-04-12 21:37:17 +08:00
* the filed name of the hash key
* @param value {const char*} key
2015-04-12 21:37:17 +08:00
* the filed value of the hash key
* @return {int}
* 1 --
* 0 --
* -1 -- key
2015-04-12 21:37:17 +08:00
* return int value as below:
* 1 -- this is a new filed and set ok
* 0 -- thie is a old filed and set ok
* -1 -- error happend or the key is not a Hash type
2015-01-26 19:58:02 +08:00
*/
2015-01-14 23:35:23 +08:00
int hset(const char* key, const char* name, const char* value);
int hset(const char* key, const char* name,
const char* value, size_t value_len);
int hset(const char* key, const char* name, size_t name_len,
const char* value, size_t value_len);
2015-01-26 19:58:02 +08:00
/**
* key
2015-04-12 21:37:17 +08:00
* set one new field of one key in hash only when the filed isn't
* existing.
* @param key {const char*} key
2015-04-12 21:37:17 +08:00
* the hash key
* @param name {const char*} key
2015-04-12 21:37:17 +08:00
* the field name
* @param value {const char*} key
* the field value
* @return {int}
* 1 --
* 0 --
* -1 -- key
2015-04-12 21:37:17 +08:00
*
* return int value as below:
* 1 -- this is a new filed and set ok
* 0 -- thie is a old filed and not set
* -1 -- error happend or the key is not a Hash type
2015-01-26 19:58:02 +08:00
*/
2015-01-14 23:35:23 +08:00
int hsetnx(const char* key, const char* name, const char* value);
int hsetnx(const char* key, const char* name,
const char* value, size_t value_len);
int hsetnx(const char* key, const char* name, size_t name_len,
const char* value, size_t value_len);
2015-01-26 19:58:02 +08:00
/**
* redis key
2015-04-12 21:37:17 +08:00
* get the value assosiated with field in the hash stored at key
* @param key {const char*} key
2015-04-12 21:37:17 +08:00
* the hash key
* @param name {const char*} key
2015-04-12 21:37:17 +08:00
* the field's name
* @param result {acl::string&} ( string )
2015-04-12 21:37:17 +08:00
* store the value result of the given field
* @return {bool}
* true -- result为空时表示 KEY
2016-12-08 13:13:30 +08:00
* get the value associated with field; if result is empty then
* the key or the name field doesn't exist
* false -- key
2015-04-12 21:37:17 +08:00
* the field not exists, or error happened,
* or the key isn't a hash key
2015-01-26 19:58:02 +08:00
*/
2015-01-14 23:35:23 +08:00
bool hget(const char* key, const char* name, string& result);
bool hget(const char* key, const char* name,
size_t name_len, string& result);
2015-01-26 19:58:02 +08:00
/**
* redis key
2015-04-12 21:37:17 +08:00
* get all the fields and values in hash stored at key
* @param key {const char*} key
2015-04-12 21:37:17 +08:00
* the hash key
* @param result {std::map<string, string>&} -
2015-04-12 21:37:17 +08:00
* store the result of all the fileds and values
* @return {bool}
2015-04-12 21:37:17 +08:00
* if ok, show below:
* true -- result
* result.size()
2015-04-12 21:37:17 +08:00
* successful if the key is a hash key or the key not exists
* false -- key
2015-04-12 21:37:17 +08:00
* error happened or the key isn't a hash key
2015-01-26 19:58:02 +08:00
*/
2015-01-14 23:35:23 +08:00
bool hgetall(const char* key, std::map<string, string>& result);
bool hgetall(const char* key, std::vector<string>& names,
std::vector<string>& values);
bool hgetall(const char* key, std::vector<const char*>& names,
std::vector<const char*>& values);
2015-01-26 19:58:02 +08:00
/**
* redis key
2015-04-12 21:37:17 +08:00
* remove one or more fields from hash stored at key
* @param key {const char*} key
2015-04-12 21:37:17 +08:00
* the hash key
* @param first_name {const char*} NULL
2015-04-12 21:37:17 +08:00
* the first field of the fields list, the last field must be NULL
* indicating the end of vary parameters
* @return {int} -1 key
2015-04-12 21:37:17 +08:00
* return the number of fields be removed successfully, or -1 when
* error happened or operating on a no hash key
2015-01-26 19:58:02 +08:00
*/
int hdel(const char* key, const char* first_name);
2015-01-14 23:35:23 +08:00
int hdel(const char* key, const char* names[], size_t argc);
int hdel(const char* key, const char* names[],
const size_t names_len[], size_t argc);
int hdel(const char* key, const std::vector<string>& names);
int hdel(const char* key, const std::vector<const char*>& names);
int hdel_fields(const char* key, const char* names[], size_t argc);
int hdel_fields(const char* key, const char* names[],
const size_t names_len[], size_t argc);
int hdel_fields(const char* key, const std::vector<string>& names);
int hdel_fields(const char* key, const std::vector<const char*>& names);
int hdel_fields(const char* key, const char* first_name, ...);
2015-01-14 23:35:23 +08:00
2015-01-26 19:58:02 +08:00
/**
* key
2015-04-12 21:37:17 +08:00
* inc(+n) or dec(-n) on a integer filed in hash stored at key
* @param key {const char*} key
2015-04-12 21:37:17 +08:00
* the hash key
* @param name {const char*} key
2015-04-12 21:37:17 +08:00
* the filed name of integer type
* @param inc {long long int}
2015-04-12 21:37:17 +08:00
* the integer value to be inc or dec on the field's value
* @param result {long long int*} NULL
2015-04-12 21:37:17 +08:00
* store the result if non-NULL
* @return {bool} false key
*
2015-04-12 21:37:17 +08:00
* if successful: false when error, not a hash, or the field isn't
* integer type
2015-01-26 19:58:02 +08:00
*/
2015-01-14 23:35:23 +08:00
bool hincrby(const char* key, const char* name,
long long int inc, long long int* result = NULL);
2015-01-26 19:58:02 +08:00
/**
* key
2015-04-12 21:37:17 +08:00
* inc(+n) or dec(-n) on a float filed in hash stored at key
* @param key {const char*} key
2015-04-12 21:37:17 +08:00
* the hash key
* @param name {const char*} key
2015-04-12 21:37:17 +08:00
* the filed name of float type
* @param inc {double}
2015-04-12 21:37:17 +08:00
* the float value to be inc or dec on the field's value
* @param result {double*} NULL
2015-04-12 21:37:17 +08:00
* store the result if non-NULL
* @return {bool} false key
*
2015-04-12 21:37:17 +08:00
* if successful: false when error, not a hash, or the field isn't
* float type
2015-01-26 19:58:02 +08:00
*/
2015-01-14 23:35:23 +08:00
bool hincrbyfloat(const char* key, const char* name,
double inc, double* result = NULL);
2015-01-26 19:58:02 +08:00
/**
* key
2015-04-12 21:37:17 +08:00
* get all the fields in hash stored at key
* @param key {const char*} key
2015-04-12 21:37:17 +08:00
* the hash key
* @param names {std::vector<string>&} key
2015-04-12 21:37:17 +08:00
* store all the names of all fileds
* @return {bool} false key
2015-04-12 21:37:17 +08:00
* return true on success, false if error happened or the
* key wasn't a hash key
2015-01-26 19:58:02 +08:00
*/
2015-01-14 23:35:23 +08:00
bool hkeys(const char* key, std::vector<string>& names);
2015-01-26 19:58:02 +08:00
/**
* key
2015-04-12 21:37:17 +08:00
* check if the field exists in hash stored at key
* @param key {const char*} key
2015-04-12 21:37:17 +08:00
* the hash key
* @param name {const char*} key
2015-04-12 21:37:17 +08:00
* the filed's name of the key
* @return {bool} false key
*
2015-04-12 21:37:17 +08:00
* return true on success, false if error happened or the
* key wasn't a hash key
2015-01-26 19:58:02 +08:00
*/
2015-01-14 23:35:23 +08:00
bool hexists(const char* key, const char* name);
bool hexists(const char* key, const char* name, size_t name_len);
2015-01-26 19:58:02 +08:00
/**
* key
2015-04-12 21:37:17 +08:00
* get the count of fields in hash stored at key
* @param key {const char*} key
2015-04-12 21:37:17 +08:00
* the hash key
* @return {int}
2015-04-12 21:37:17 +08:00
* return int value as below:
* -1 -- key
2015-04-12 21:37:17 +08:00
* error or not a hash key
* >0 --
2015-04-12 21:37:17 +08:00
* the count of fields
* 0 -- key 0
2015-04-12 21:37:17 +08:00
* key not exists or no fields in hash stored at key
2015-01-26 19:58:02 +08:00
*/
2015-01-14 23:35:23 +08:00
int hlen(const char* key);
2015-01-23 17:23:04 +08:00
2016-09-20 19:55:38 +08:00
/**
* key
2016-09-20 19:55:38 +08:00
* Returns the string length of the value associated with field
* in the hash stored at key
* @param key {const char*} key
2016-09-20 19:55:38 +08:00
* the hash key
* @param name {const char*} key
2016-09-20 19:55:38 +08:00
* the field's name
* @return {int} key name 0 key
* -1
2016-09-20 19:55:38 +08:00
* If the key or the field do not exist, 0 is returned; If the key is
* not the hash key or error happened, -1 is returned.
*/
int hstrlen(const char* key, const char* name, size_t name_len);
int hstrlen(const char* key, const char *name);
2015-01-23 17:23:04 +08:00
/**
*
2015-04-12 21:37:17 +08:00
* scan the name and value of all fields in hash stored at key
* @param key {const char*}
2015-04-12 21:37:17 +08:00
* the hash key
* @param cursor {int} 0
2015-04-12 21:37:17 +08:00
* the cursor value, which is 0 at begin
* @param out {std::map<acl::string>&}
*
*
2015-04-12 21:37:17 +08:00
* store scaning result in appending mode
* @param pattern {const char*} glob
2015-04-12 21:37:17 +08:00
* match pattern, effective only on no-NULL
* @param count {const size_t*}
2015-04-12 21:37:17 +08:00
* the max count of one scan process, effective only on no-NULL
* @return {int}
2015-04-12 21:37:17 +08:00
* return the next cursor position, as below:
* 0
2015-04-12 21:37:17 +08:00
* scan finish
* -1:
2015-04-12 21:37:17 +08:00
* some error happened
* >0: 使 out
2015-04-12 21:37:17 +08:00
* the next cursor postion to scan
2015-01-23 17:23:04 +08:00
*/
int hscan(const char* key, int cursor, std::map<string, string>& out,
const char* pattern = NULL, const size_t* count = NULL);
2015-01-14 23:35:23 +08:00
};
} // namespace acl
#endif // !defined(ACL_CLIENT_ONLY) && !defined(ACL_REDIS_DISABLE)