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

229 lines
6.6 KiB
C++
Raw Normal View History

2015-01-14 23:35:23 +08:00
#pragma once
2017-06-02 14:47:24 +08:00
#include "../acl_cpp_define.hpp"
#include "../stdlib/noncopyable.hpp"
2015-01-14 23:35:23 +08:00
#include <vector>
#if !defined(ACL_CLIENT_ONLY) && !defined(ACL_REDIS_DISABLE)
2015-01-14 23:35:23 +08:00
namespace acl
{
typedef enum
{
REDIS_RESULT_UNKOWN,
2015-01-14 23:35:23 +08:00
REDIS_RESULT_NIL,
REDIS_RESULT_ERROR,
REDIS_RESULT_STATUS,
REDIS_RESULT_INTEGER,
REDIS_RESULT_STRING,
REDIS_RESULT_ARRAY,
} redis_result_t;
class string;
2015-01-14 23:35:23 +08:00
class dbuf_pool;
class redis_client;
2015-03-29 19:27:41 +08:00
/**
* <EFBFBD><EFBFBD> redis-server <EFBFBD><EFBFBD><EFBFBD>ؽ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> redis-server <EFBFBD><EFBFBD><EFBFBD>ص<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݽ<EFBFBD><EFBFBD>з<EFBFBD><EFBFBD><EFBFBD><EFBFBD>󴴽<EFBFBD>
* redis_result <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
2015-04-12 21:37:17 +08:00
* the redis result for redis-server's reply
2015-03-29 19:27:41 +08:00
*/
class ACL_CPP_API redis_result : public noncopyable
2015-01-14 23:35:23 +08:00
{
public:
redis_result(dbuf_pool* dbuf);
2015-01-14 23:35:23 +08:00
/**
* <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> new/delete <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> new <EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʱ<EFBFBD><EFBFBD>ʹ<EFBFBD>ڴ<EFBFBD><EFBFBD>ķ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
* <EFBFBD>ڴ<EFBFBD><EFBFBD>ؽ<EFBFBD><EFBFBD>з<EFBFBD><EFBFBD><EFBFBD>
2015-04-12 21:37:17 +08:00
* override new/delete operator, when the new object was created,
* memory was alloc in dbuf_pool, which is a memroy pool allocator
2015-01-14 23:35:23 +08:00
*/
void *operator new(size_t size, dbuf_pool* pool);
void operator delete(void* ptr, dbuf_pool* pool);
/**
* <EFBFBD><EFBFBD><EFBFBD>õ<EFBFBD>ǰ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
2015-04-12 21:37:17 +08:00
* get the data type of the reply from redis-server
2015-01-14 23:35:23 +08:00
* @return {redis_result_t}
2015-04-12 21:37:17 +08:00
* defined above REDIS_RESULT_
2015-01-14 23:35:23 +08:00
*/
redis_result_t get_type(void) const
2015-01-14 23:35:23 +08:00
{
return result_type_;
}
/**
* <EFBFBD><EFBFBD><EFBFBD>õ<EFBFBD>ǰ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ķ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ĸ<EFBFBD><EFBFBD><EFBFBD>
2015-04-12 21:37:17 +08:00
* get the number of objects from redis-server
2015-01-14 23:35:23 +08:00
* @return {size_t} <EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֵ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>͵Ķ<EFBFBD>Ӧ<EFBFBD><EFBFBD>ϵ<EFBFBD><EFBFBD><EFBFBD>£<EFBFBD>
2015-04-12 21:37:17 +08:00
* the relation between returned value and result type show below:
2015-01-14 23:35:23 +08:00
* REDIS_RESULT_ERROR: 1
* REDIS_RESULT_STATUS: 1
* REDIS_RESULT_INTEGER: 1
* REDIS_RESULT_STRING: > 0 ʱ<EFBFBD><EFBFBD>ʾ<EFBFBD><EFBFBD><EFBFBD>ַ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݱ<EFBFBD><EFBFBD>зֳɷ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ڴ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ĸ<EFBFBD><EFBFBD><EFBFBD>
* REDIS_RESULT_ARRAY: children_->size()
*/
size_t get_size(void) const;
2015-01-14 23:35:23 +08:00
/**
* <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֵΪ REDIS_RESULT_INTEGER <EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʱ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ض<EFBFBD>Ӧ<EFBFBD><EFBFBD> 32 λ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֵ
2015-04-12 21:37:17 +08:00
* get the 32 bits integer for REDIS_RESULT_INTEGER result
2015-01-14 23:35:23 +08:00
* @param success {bool*} <EFBFBD><EFBFBD>ָ<EFBFBD><EFBFBD><EFBFBD><EFBFBD> NULL ʱ<EFBFBD><EFBFBD>¼<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ƿ<EFBFBD><EFBFBD>ɹ<EFBFBD>
2015-04-12 21:37:17 +08:00
* when not NULL, storing the status of success
2015-01-14 23:35:23 +08:00
* @return {int}
*/
int get_integer(bool* success = NULL) const;
/**
* <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֵΪ REDIS_RESULT_INTEGER <EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʱ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ض<EFBFBD>Ӧ<EFBFBD><EFBFBD> 64 λ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֵ
2015-04-12 21:37:17 +08:00
* get the 64 bits integer for REDIS_RESULT_INTEGER result
2015-01-14 23:35:23 +08:00
* @param success {bool*} <EFBFBD><EFBFBD>ָ<EFBFBD><EFBFBD><EFBFBD><EFBFBD> NULL ʱ<EFBFBD><EFBFBD>¼<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ƿ<EFBFBD><EFBFBD>ɹ<EFBFBD>
2015-04-12 21:37:17 +08:00
* when not NULL, storing the status of success
2015-01-14 23:35:23 +08:00
* @return {long long int}
*/
long long int get_integer64(bool* success = NULL) const;
/**
* <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֵΪ REDIS_RESULT_STRING <EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʱ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ض<EFBFBD>Ӧ<EFBFBD><EFBFBD> double <EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֵ
* get the double value for REDIS_RESULT_STRING result
* @param success {bool*} <EFBFBD><EFBFBD>ָ<EFBFBD><EFBFBD><EFBFBD><EFBFBD> NULL ʱ<EFBFBD><EFBFBD>¼<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ƿ<EFBFBD><EFBFBD>ɹ<EFBFBD>
* when not NULL, storing the status of success
* @return {double}
*/
double get_double(bool* success = NULL) const;
2015-01-14 23:35:23 +08:00
/**
* <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֵΪ REDIS_RESULT_STATUS <EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʱ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>״̬<EFBFBD><EFBFBD>Ϣ
2015-04-12 21:37:17 +08:00
* get operation status for REDIS_RESULT_STATUS result
2015-02-07 22:55:24 +08:00
* @return {const char*} <EFBFBD><EFBFBD><EFBFBD><EFBFBD> "" <EFBFBD><EFBFBD>ʾ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>
2015-04-12 21:37:17 +08:00
* error if empty string returned
2015-01-14 23:35:23 +08:00
*/
const char* get_status() const;
2015-02-07 22:55:24 +08:00
/**
* <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʱ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֵΪ REDIS_RESULT_ERROR <EFBFBD><EFBFBD><EFBFBD>ͣ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>س<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϣ
2015-04-12 21:37:17 +08:00
* when some error happened, this can get the error information
2015-02-07 22:55:24 +08:00
* @return {const char*} <EFBFBD><EFBFBD><EFBFBD>ؿմ<EFBFBD> "" <EFBFBD><EFBFBD>ʾû<EFBFBD>г<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϣ
2015-04-12 21:37:17 +08:00
* there was no error information if empty string returned
2015-02-07 22:55:24 +08:00
*/
const char* get_error(void) const;
2015-02-07 22:55:24 +08:00
2015-01-14 23:35:23 +08:00
/**
* <EFBFBD><EFBFBD><EFBFBD>ض<EFBFBD>Ӧ<EFBFBD>±<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>(<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͷ<EFBFBD> REDIS_RESULT_ARRAY ʱ<EFBFBD><EFBFBD>
2015-04-12 21:37:17 +08:00
* get the string data of associated subscript(just for the type
* of no REDIS_RESULT_ARRAY)
2015-01-14 23:35:23 +08:00
* @param i {size_t} <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>±<EFBFBD>
2015-04-12 21:37:17 +08:00
* the array's subscript
2015-01-14 23:35:23 +08:00
* @param len {size_t*} <EFBFBD><EFBFBD>Ϊ<EFBFBD><EFBFBD> NULL ָ<EFBFBD><EFBFBD>ʱ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݵij<EFBFBD><EFBFBD><EFBFBD>
2015-04-12 21:37:17 +08:00
* when not NULL, the parameter will store the length of the result
2015-02-07 22:55:24 +08:00
* @return {const char*} <EFBFBD><EFBFBD><EFBFBD><EFBFBD> NULL <EFBFBD><EFBFBD>ʾ<EFBFBD>±<EFBFBD>Խ<EFBFBD><EFBFBD>
2015-04-12 21:37:17 +08:00
* NULL if nothing exists or the subscript is out of bounds
2015-01-14 23:35:23 +08:00
*/
const char* get(size_t i, size_t* len = NULL) const;
/**
* <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>е<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>(<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͷ<EFBFBD> REDIS_RESULT_ARRAY ʱ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ַ
2015-04-12 21:37:17 +08:00
* return all data's array if the type isn't REDIS_RESULT_ARRAY
2015-01-14 23:35:23 +08:00
* @return {const char**}
*/
const char** gets_argv(void) const
2015-01-14 23:35:23 +08:00
{
return (const char**) argv_;
}
/**
* <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>е<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݳ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>(<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͷ<EFBFBD> REDIS_RESULT_ARRAY ʱ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ַ
2015-04-12 21:37:17 +08:00
* return all length's array if the type isn't REDIS_RESULT_ARRAY
2015-01-14 23:35:23 +08:00
* @return {const size_t*}
*/
const size_t* get_lens(void) const
2015-01-14 23:35:23 +08:00
{
return lens_;
}
/**
* <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݵ<EFBFBD><EFBFBD>ܳ<EFBFBD><EFBFBD><EFBFBD>(<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͷ<EFBFBD> REDIS_RESULT_ARRAY ʱ<EFBFBD><EFBFBD>
2015-04-12 21:37:17 +08:00
* return the total length of all data for no REDIS_RESULT_ARRAY
2015-01-14 23:35:23 +08:00
* @return {size_t}
*/
size_t get_length(void) const;
2015-01-14 23:35:23 +08:00
/**
* <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϊ REDIS_RESULT_STRING <EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʱ<EFBFBD><EFBFBD><EFBFBD>ú<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ڴ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ŵ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
* <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ڴ<EFBFBD><EFBFBD>У<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ҫע<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֹ<EFBFBD>ڴ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
2015-04-12 21:37:17 +08:00
* compose a continus data for the slicing chunk data internal
2015-01-27 17:52:50 +08:00
* @param buf {string&} <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݣ<EFBFBD><EFBFBD>ڲ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ȵ<EFBFBD><EFBFBD><EFBFBD> buf.clear()
2015-04-12 21:37:17 +08:00
* store the result
* @return {int} <EFBFBD><EFBFBD><EFBFBD>ݵ<EFBFBD><EFBFBD>ܳ<EFBFBD><EFBFBD>ȣ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֵ 0 <EFBFBD><EFBFBD>ʾ<EFBFBD>ڲ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϊ<EFBFBD><EFBFBD>
* return the total length of data, 0 if data array has no elements
2015-01-14 23:35:23 +08:00
*/
2015-01-29 00:38:29 +08:00
int argv_to_string(string& buf) const;
int argv_to_string(char* buf, size_t size) const;
2015-01-14 23:35:23 +08:00
/**
* <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϊ REDIS_RESULT_ARRAY <EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʱ<EFBFBD><EFBFBD><EFBFBD>ú<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>е<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
2015-04-12 21:37:17 +08:00
* return the objects array when result type is REDIS_RESULT_ARRAY
* @param size {size_t*} <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ǿ<EFBFBD>ʱ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>õ<EFBFBD>ַ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
2015-04-12 21:37:17 +08:00
* store the array's length if size isn't NULL
2015-01-14 23:35:23 +08:00
* @return {const const redis_result*}
*/
const redis_result** get_children(size_t* size) const;
2015-01-14 23:35:23 +08:00
/**
* <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϊ REDIS_RESULT_ARRAY <EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʱ<EFBFBD><EFBFBD><EFBFBD>ú<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ض<EFBFBD>Ӧ<EFBFBD>±<EFBFBD><EFBFBD>Ľ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
2015-04-12 21:37:17 +08:00
* get one object of the given subscript from objects array
2015-01-14 23:35:23 +08:00
* @param i {size_t} <EFBFBD>±<EFBFBD>ֵ
2015-04-12 21:37:17 +08:00
* the given subscript
2015-01-14 23:35:23 +08:00
* @return {const redis_result*} <EFBFBD><EFBFBD><EFBFBD>±<EFBFBD>ֵԽ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʱ<EFBFBD><EFBFBD><EFBFBD>򷵻<EFBFBD> NULL
2015-04-12 21:37:17 +08:00
* NULL if subscript is out of bounds or object not exist
2015-01-14 23:35:23 +08:00
*/
const redis_result* get_child(size_t i) const;
/**
* <EFBFBD><EFBFBD><EFBFBD>ع<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ڴ<EFBFBD><EFBFBD>ض<EFBFBD><EFBFBD><EFBFBD>
2015-04-12 21:37:17 +08:00
* get the memory pool object set in constructor
2015-01-14 23:35:23 +08:00
* @return {dbuf_pool*}
*/
dbuf_pool* get_dbuf(void)
2015-01-14 23:35:23 +08:00
{
return dbuf_;
2015-01-14 23:35:23 +08:00
}
/**
* <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ת<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ַ<EFBFBD><EFBFBD><EFBFBD>
* @param out {string&} <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>(<EFBFBD><EFBFBD>׷<EFBFBD>ӷ<EFBFBD>ʽ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>)
* @return {const string&}
*/
const string& to_string(string& out) const;
2015-01-14 23:35:23 +08:00
private:
~redis_result(void);
2015-01-14 23:35:23 +08:00
friend class redis_client;
void clear(void);
2015-01-14 23:35:23 +08:00
redis_result& set_type(redis_result_t type);
redis_result& set_size(size_t size);
redis_result& put(const char* buf, size_t len);
redis_result& put(const redis_result* rr, size_t idx);
private:
redis_result_t result_type_;
dbuf_pool* dbuf_;
2015-01-14 23:35:23 +08:00
size_t size_;
size_t idx_;
const char** argv_;
size_t* lens_;
//std::vector<const redis_result*>* children_;
const redis_result** children_;
size_t children_size_;
size_t children_idx_;
};
} // namespace acl
#endif // !defined(ACL_CLIENT_ONLY) && !defined(ACL_REDIS_DISABLE)