mirror of
https://gitee.com/acl-dev/acl.git
synced 2024-11-30 10:57:34 +08:00
optimize redis client
This commit is contained in:
parent
4323441df7
commit
730ff1c3fb
@ -104,7 +104,7 @@ public:
|
||||
/**
|
||||
* 当数据类型为 REDIS_RESULT_STRING 类型时,该函数将按内存块存放的数据存储至
|
||||
* 连接内存中,但需要注意防止内存溢出
|
||||
* @param buf {string&} 存储结果数据
|
||||
* @param buf {string&} 存储结果数据,内部会先调用 buf.clear()
|
||||
* @return {size_t} 数据的总长度
|
||||
*/
|
||||
size_t argv_to_string(string& buf) const;
|
||||
|
@ -27,15 +27,15 @@ public:
|
||||
* -1:表示出错或 key 对象非有序集对象
|
||||
* >0:新添加的成员数量
|
||||
*/
|
||||
int zadd(const char* key, std::map<string, double>& members);
|
||||
int zadd(const char* key, const std::map<string, double>& members);
|
||||
int zadd(const char* key,
|
||||
std::vector<std::pair<string, double> >&members);
|
||||
const std::vector<std::pair<string, double> >&members);
|
||||
int zadd(const char* key,
|
||||
std::vector<std::pair<const char*, double> >&members);
|
||||
int zadd(const char* key, std::vector<string>& members,
|
||||
std::vector<double>& scores);
|
||||
int zadd(const char* key, std::vector<const char*>& members,
|
||||
std::vector<double>& scores);
|
||||
const std::vector<std::pair<const char*, double> >&members);
|
||||
int zadd(const char* key, const std::vector<string>& members,
|
||||
const std::vector<double>& scores);
|
||||
int zadd(const char* key, const std::vector<const char*>& members,
|
||||
const std::vector<double>& scores);
|
||||
int zadd(const char* key, const char* members[], double scores[],
|
||||
size_t size);
|
||||
int zadd(const char* key, const char* members[], size_t members_len[],
|
||||
@ -80,7 +80,8 @@ public:
|
||||
* @param key {const char*} 有序集键值
|
||||
* @param start {int} 起始下标位置
|
||||
* @param stop {int} 结束下标位置(结果集同时含该位置)
|
||||
* @param result {std::vector<string>&} 存储结果集
|
||||
* @param result {std::vector<string>&} 存储结果集,内部先调用
|
||||
* result.clear() 清除其中的元素
|
||||
* @return {int} 结果集中成员的数量
|
||||
* 0: 表示结果集为空或 key 不存在
|
||||
* -1: 表示出错或 key 对象非有序集对象
|
||||
@ -96,7 +97,7 @@ public:
|
||||
* @param key {const char*} 有序集键值
|
||||
* @param start {int} 起始下标位置
|
||||
* @param stop {int} 结束下标位置(结果集同时含该位置)
|
||||
* @param result 存储 "成员名-分值对"结果集
|
||||
* @param result 存储 "成员名-分值对"结果集,内部先调用 out.clear()
|
||||
* @return {int} 结果集中成员的数量
|
||||
* 0: 表示结果集为空或 key 不存在
|
||||
* -1: 表示出错或 key 对象非有序集对象
|
||||
@ -113,7 +114,7 @@ public:
|
||||
* @param key {const char*} 有序集键值
|
||||
* @param min {double} 最小分值
|
||||
* @param max {double} 最大分值
|
||||
* @param out 存储“分值-成员名”对的结果集
|
||||
* @param out 存储“分值-成员名”对的结果集,内部先调用 out.clear()
|
||||
* @param offset {const int*} 非空时表示结果集的起始下标
|
||||
* @param count {const int*} 非空时表示截取的结果集中成员个数
|
||||
* @return {int} 结果集中成员的数量
|
||||
@ -132,7 +133,7 @@ public:
|
||||
* @param key {const char*} 有序集键值
|
||||
* @param min {const char*} 以字符串表示最小分值
|
||||
* @param max {const char*} 以字符串表示最大分值
|
||||
* @param out 存储“分值-成员名”对的结果集
|
||||
* @param out 存储“分值-成员名”对的结果集,内部先调用 out.clear()
|
||||
* @param offset {const int*} 非空时表示结果集的起始下标
|
||||
* @param count {const int*} 非空时表示截取的结果集中成员个数
|
||||
* @return {int} 结果集中成员的数量
|
||||
@ -155,6 +156,8 @@ public:
|
||||
* 返回有序集 key 中,所有 score 值介于 min 和 max 之间(包括等于 min 或 max )
|
||||
* 的成员及分值。有序集成员按 score 值递增(从小到大)次序排列;分值(min/max)使用
|
||||
* 浮点数表示
|
||||
* @param out 存储结果集,内部先调用 out.clear()
|
||||
* @return {int} 结果集中成员的数量
|
||||
*/
|
||||
int zrangebyscore_with_scores(const char* key, double min, double max,
|
||||
std::vector<std::pair<string, double> >& out,
|
||||
@ -164,6 +167,8 @@ public:
|
||||
* 返回有序集 key 中,所有 score 值介于 min 和 max 之间(包括等于 min 或 max )
|
||||
* 的成员及分值。有序集成员按 score 值递增(从小到大)次序排列;分值(min/max)使用
|
||||
* 字符串表示
|
||||
* @param out 存储结果集,内部先调用 out.clear()
|
||||
* @return {int} 结果集中成员的数量
|
||||
*/
|
||||
int zrangebyscore_with_scores(const char* key, const char* min,
|
||||
const char* max, std::vector<std::pair<string, double> >& out,
|
||||
@ -240,6 +245,7 @@ public:
|
||||
* @param result {std::vector<string>&} 存储结果集
|
||||
* 注:对于下标位置,0 表示第一个成员,1 表示第二个成员;-1 表示最后一个成员,
|
||||
* -2 表示倒数第二个成员,以此类推
|
||||
* @return {int} 结果集数量,-1 表示出错
|
||||
*/
|
||||
int zrevrange(const char* key, int start, int stop,
|
||||
std::vector<string>& result);
|
||||
@ -249,9 +255,10 @@ public:
|
||||
* @param key {const char*} 有序集键值
|
||||
* @param start {int} 起始下标位置
|
||||
* @param stop {int} 结束下标位置(结果集同时含该位置)
|
||||
* @param result 存储 "成员名-分值对"结果集
|
||||
* @param result 存储 "成员名-分值对"结果集,内部先调用 out.clear()
|
||||
* 注:对于下标位置,0 表示第一个成员,1 表示第二个成员;-1 表示最后一个成员,
|
||||
* -2 表示倒数第二个成员,以此类推
|
||||
* @return {int} 结果集数量,-1 表示出错
|
||||
*/
|
||||
int zrevrange_with_scores(const char* key, int start, int stop,
|
||||
std::vector<std::pair<string, double> >& out);
|
||||
@ -262,7 +269,7 @@ public:
|
||||
* @param key {const char*} 有序集键值
|
||||
* @param min {const char*} 以字符串表示最小分值
|
||||
* @param max {const char*} 以字符串表示最大分值
|
||||
* @param out 存储“分值-成员名”对的结果集
|
||||
* @param out 存储“分值-成员名”对的结果集,内部先调用 out.clear()
|
||||
* @param offset {const int*} 非空时表示结果集的起始下标
|
||||
* @param count {const int*} 非空时表示截取的结果集中成员个数
|
||||
* @return {int} 结果集中成员的数量
|
||||
@ -285,6 +292,8 @@ public:
|
||||
* 返回有序集 key 中,所有 score 值介于 min 和 max 之间(包括等于 min 或 max )
|
||||
* 的成员及分值。有序集成员按 score 值递减(从小到大)次序排列;分值(min/max)使用
|
||||
* 浮点数表示
|
||||
* @param out 存储“分值-成员名”对的结果集,内部先调用 out.clear()
|
||||
* @param count {const int*} 非空时表示截取的结果集中成员个数
|
||||
*/
|
||||
int zrevrangebyscore_with_scores(const char* key, double min,
|
||||
double max, std::vector<std::pair<string, double> >& out,
|
||||
@ -330,7 +339,7 @@ public:
|
||||
* MIN: 将所有集合中某个成员的 最小 score 值作为结果集中该成员的 score 值
|
||||
* MAX: 将所有集合中某个成员的 最大 score 值作为结果集中该成员的 score 值
|
||||
* @return {int} 新保存到目标有序集的结果集中的元素(成员)数量,如果源有序集
|
||||
* 集合中存在相同的成员,则只新增一个成员
|
||||
* 集合中存在相同的成员,则只新增一个成员;返回 -1 表示出错
|
||||
*/
|
||||
int zunionstore(const char* dst, const std::map<string, double>& keys,
|
||||
const char* aggregate = "SUM");
|
||||
@ -351,6 +360,21 @@ public:
|
||||
int zinterstore(const char* dst, const std::vector<string>& keys,
|
||||
const std::vector<double>* weights = NULL,
|
||||
const char* aggregate = "SUM");
|
||||
|
||||
/**
|
||||
* 命令用于迭代有序集合中的元素(包括元素成员和元素分值)
|
||||
* @param cursor {int} 游标值,开始遍历时该值写 0
|
||||
* @param out 存储结果集,内部先 out.clear()
|
||||
* @param pattern {const char*} 匹配模式,glob 风格,非空时有效
|
||||
* @param count {const size_t*} 限定的结果集数量,非空指针时有效
|
||||
* @return {int} 下一个游标位置,含义如下:
|
||||
* 0:遍历结束
|
||||
* -1: 出错
|
||||
* >0: 游标的下一个位置
|
||||
*/
|
||||
int zscan(const char* key, int cursor,
|
||||
std::vector<std::pair<string, double> >& out,
|
||||
const char* pattern = NULL, const size_t* count = NULL);
|
||||
|
||||
/**
|
||||
* 当有序集合的所有成员都具有相同的分值时, 有序集合的元素会根据成员的字典序
|
||||
@ -358,6 +382,7 @@ public:
|
||||
* 有序集合键 key 中, 值介于 min 和 max 之间的成员
|
||||
* @param min {const char*} 区间最小值
|
||||
* @param max {const char*} 区间最大值
|
||||
* @param out {std::map<string, double>&} 存储结果集,内部先 out.clear()
|
||||
* @param offset {const int*} 非空时有效,从结果集中选取的下标起始值
|
||||
* @param count {const int*} 非空时有效,从结果集中的指定下标位置起选取的数量
|
||||
* @return {int} 结果集中成员的数量
|
||||
@ -378,31 +403,17 @@ public:
|
||||
/**
|
||||
* 于一个所有成员的分值都相同的有序集合键 key 来说, 这个命令会返回该集合中,
|
||||
* 成员介于 min 和 max 范围内的元素数量
|
||||
* @return {int}
|
||||
* @return {int} 符合条件的元素数量
|
||||
*/
|
||||
int zlexcount(const char* key, const char* min, const char* max);
|
||||
|
||||
/**
|
||||
* 对于一个所有成员的分值都相同的有序集合键 key 来说, 这个命令会移除该集合中,
|
||||
* 成员介于 min 和 max 范围内的所有元素
|
||||
* @return {int}
|
||||
* @return {int} 被移除的元素数量
|
||||
*/
|
||||
int zremrangebylex(const char* key, const char* min, const char* max);
|
||||
|
||||
/**
|
||||
* 命令用于迭代有序集合中的元素(包括元素成员和元素分值)
|
||||
* @param cursor {int} 游标值,开始遍历时该值写 0
|
||||
* @param out {std::map<string, double>&} 结果集
|
||||
* @param pattern {const char*} 匹配模式,glob 风格,非空时有效
|
||||
* @param count {const size_t*} 限定的结果集数量,非空指针时有效
|
||||
* @return {int} 下一个游标位置,含义如下:
|
||||
* 0:遍历结束
|
||||
* -1: 出错
|
||||
* >0: 游标的下一个位置
|
||||
*/
|
||||
int zscan(const char* key, int cursor, std::map<string, double>& out,
|
||||
const char* pattern = NULL, const size_t* count = NULL);
|
||||
|
||||
private:
|
||||
int zrange_get(const char* cmd, const char* key, int start,
|
||||
int stop, std::vector<string>& result);
|
||||
|
@ -5,6 +5,7 @@ all:
|
||||
@(cd redis_pool; make)
|
||||
@(cd redis_manager; make)
|
||||
@(cd redis_pubsub; make)
|
||||
@(cd redis_zset; make)
|
||||
|
||||
clean:
|
||||
@(cd redis_key; make clean)
|
||||
@ -13,3 +14,4 @@ clean:
|
||||
@(cd redis_pool; make clean)
|
||||
@(cd redis_manager; make clean)
|
||||
@(cd redis_pubsub; make clean)
|
||||
@(cd redis_zset; make clean)
|
||||
|
@ -120,20 +120,21 @@ static void test_zrange(acl::redis_zset& option, int n)
|
||||
printf("zrange error, key: %s\r\n", key.c_str());
|
||||
break;
|
||||
}
|
||||
else if (i < 10)
|
||||
else if (i >= 10)
|
||||
{
|
||||
printf("zrange ok, key: %s, ret: %d\r\n",
|
||||
key.c_str(), ret);
|
||||
std::vector<acl::string>::const_iterator cit;
|
||||
for (cit = result.begin(); cit != result.end(); ++cit)
|
||||
{
|
||||
if (cit != result.begin())
|
||||
printf(", ");
|
||||
printf("%s", (*cit).c_str());
|
||||
}
|
||||
printf("\r\n");
|
||||
result.clear();
|
||||
continue;
|
||||
}
|
||||
|
||||
printf("zrange ok, key: %s, ret: %d\r\n", key.c_str(), ret);
|
||||
std::vector<acl::string>::const_iterator cit;
|
||||
for (cit = result.begin(); cit != result.end(); ++cit)
|
||||
{
|
||||
if (cit != result.begin())
|
||||
printf(", ");
|
||||
printf("%s", (*cit).c_str());
|
||||
}
|
||||
printf("\r\n");
|
||||
result.clear();
|
||||
}
|
||||
|
||||
@ -153,21 +154,22 @@ static void test_zrange(acl::redis_zset& option, int n)
|
||||
printf("zrange error, key: %s\r\n", key.c_str());
|
||||
break;
|
||||
}
|
||||
else if (i < 10)
|
||||
else if (i >= 10)
|
||||
{
|
||||
printf("zrange ok, key: %s, ret: %d\r\n",
|
||||
key.c_str(), ret);
|
||||
|
||||
std::vector<std::pair<acl::string, double> >::const_iterator cit;
|
||||
for (cit = result2.begin(); cit != result2.end(); ++cit)
|
||||
{
|
||||
if (cit != result2.begin())
|
||||
printf(", ");
|
||||
printf("%s: %.2f", cit->first.c_str(), cit->second);
|
||||
}
|
||||
printf("\r\n");
|
||||
result2.clear();
|
||||
continue;
|
||||
}
|
||||
|
||||
printf("zrange ok, key: %s, ret: %d\r\n", key.c_str(), ret);
|
||||
|
||||
std::vector<std::pair<acl::string, double> >::const_iterator cit;
|
||||
for (cit = result2.begin(); cit != result2.end(); ++cit)
|
||||
{
|
||||
if (cit != result2.begin())
|
||||
printf(", ");
|
||||
printf("%s: %.2f", cit->first.c_str(), cit->second);
|
||||
}
|
||||
printf("\r\n");
|
||||
result2.clear();
|
||||
}
|
||||
}
|
||||
@ -192,20 +194,22 @@ static void test_zrangebyscore(acl::redis_zset& option, int n)
|
||||
printf("zrangebyscore error, key: %s\r\n", key.c_str());
|
||||
break;
|
||||
}
|
||||
else if (i < 10)
|
||||
else if (i >= 10)
|
||||
{
|
||||
printf("zrangebyscore ok, key: %s, ret: %d\r\n",
|
||||
key.c_str(), ret);
|
||||
std::vector<acl::string>::const_iterator cit;
|
||||
for (cit = result.begin(); cit != result.end(); ++cit)
|
||||
{
|
||||
if (cit != result.begin())
|
||||
printf(", ");
|
||||
printf("%s", (*cit).c_str());
|
||||
}
|
||||
printf("\r\n");
|
||||
result.clear();
|
||||
continue;
|
||||
}
|
||||
|
||||
printf("zrangebyscore ok, key: %s, ret: %d\r\n",
|
||||
key.c_str(), ret);
|
||||
std::vector<acl::string>::const_iterator cit;
|
||||
for (cit = result.begin(); cit != result.end(); ++cit)
|
||||
{
|
||||
if (cit != result.begin())
|
||||
printf(", ");
|
||||
printf("%s", (*cit).c_str());
|
||||
}
|
||||
printf("\r\n");
|
||||
result.clear();
|
||||
}
|
||||
|
||||
@ -226,22 +230,24 @@ static void test_zrangebyscore(acl::redis_zset& option, int n)
|
||||
key.c_str());
|
||||
break;
|
||||
}
|
||||
else if (i < 10)
|
||||
else if (i >= 10)
|
||||
{
|
||||
printf("zrangebyscore_with_scores ok, key: %s, ret: %d\r\n",
|
||||
key.c_str(), ret);
|
||||
std::vector<std::pair<acl::string, double> >::const_iterator cit;
|
||||
for (cit = result2.begin(); cit != result2.end(); ++cit)
|
||||
{
|
||||
if (cit != result2.begin())
|
||||
printf(", ");
|
||||
printf("%s: %.2f", cit->first.c_str(),
|
||||
cit->second);
|
||||
}
|
||||
printf("\r\n");
|
||||
result2.clear();
|
||||
continue;
|
||||
}
|
||||
|
||||
result.clear();
|
||||
printf("zrangebyscore_with_scores ok, key: %s, ret: %d\r\n",
|
||||
key.c_str(), ret);
|
||||
std::vector<std::pair<acl::string, double> >::const_iterator cit;
|
||||
for (cit = result2.begin(); cit != result2.end(); ++cit)
|
||||
{
|
||||
if (cit != result2.begin())
|
||||
printf(", ");
|
||||
printf("%s: %.2f", cit->first.c_str(),
|
||||
cit->second);
|
||||
}
|
||||
printf("\r\n");
|
||||
result2.clear();
|
||||
}
|
||||
}
|
||||
|
||||
@ -265,8 +271,9 @@ static void test_zrank(acl::redis_zset& option, int n)
|
||||
break;
|
||||
}
|
||||
else if (j > 0 && j * i < 100)
|
||||
printf("zrank ok, key: %s, member:%s, rank: %d\r\n",
|
||||
key.c_str(), member.c_str(), ret);
|
||||
printf("zrank ok, key: %s, member:%s, "
|
||||
"rank: %d\r\n", key.c_str(),
|
||||
member.c_str(), ret);
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -315,19 +322,183 @@ static void test_zscore(acl::redis_zset& option, int n)
|
||||
for (int j = 0; j < 1000; j++)
|
||||
{
|
||||
member.format("member_%d", j);
|
||||
ret = option.zscore(key.c_str(), member.c_str(), result);
|
||||
ret = option.zscore(key.c_str(), member.c_str(),
|
||||
result);
|
||||
if (ret == false)
|
||||
{
|
||||
printf("zscore error, key: %s\r\n", key.c_str());
|
||||
printf("zscore error, key: %s\r\n",
|
||||
key.c_str());
|
||||
break;
|
||||
}
|
||||
else if (j > 0 && j * i < 100)
|
||||
printf("zscore ok, key: %s, member:%s, score: %.2f\r\n",
|
||||
key.c_str(), member.c_str(), result);
|
||||
printf("zscore ok, key: %s, member:%s, "
|
||||
"score: %.2f\r\n", key.c_str(),
|
||||
member.c_str(), result);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static void test_zunionstore(acl::redis_zset& option, int n)
|
||||
{
|
||||
acl::string dest_key, src1_key, src2_key;
|
||||
std::vector<acl::string> src_keys;
|
||||
|
||||
for (int i = 0; i < n; i++)
|
||||
{
|
||||
dest_key.format("zset_dest_key_%d", i);
|
||||
src1_key.format("zset_src1_key_%d", i);
|
||||
src_keys.push_back(src1_key);
|
||||
src2_key.format("zset_src2_key_%d", i);
|
||||
src_keys.push_back(src2_key);
|
||||
|
||||
int ret = option.zunionstore(dest_key.c_str(), src_keys);
|
||||
if (ret < 0)
|
||||
{
|
||||
printf("zunionstore error, dest: %s\r\n",
|
||||
dest_key.c_str());
|
||||
break;
|
||||
}
|
||||
src_keys.clear();
|
||||
}
|
||||
}
|
||||
|
||||
static void test_zinterstore(acl::redis_zset& option ,int n)
|
||||
{
|
||||
acl::string dest_key, src1_key, src2_key;
|
||||
std::vector<acl::string> src_keys;
|
||||
|
||||
for (int i = 0; i < n; i++)
|
||||
{
|
||||
dest_key.format("zset_dest_key_%d", i);
|
||||
src1_key.format("zset_src1_key_%d", i);
|
||||
src_keys.push_back(src1_key);
|
||||
src2_key.format("zset_src2_key_%d", i);
|
||||
src_keys.push_back(src2_key);
|
||||
|
||||
int ret = option.zinterstore(dest_key.c_str(), src_keys);
|
||||
if (ret < 0)
|
||||
{
|
||||
printf("zinterstore error, dest: %s\r\n",
|
||||
dest_key.c_str());
|
||||
break;
|
||||
}
|
||||
src_keys.clear();
|
||||
}
|
||||
}
|
||||
|
||||
static void test_zscan(acl::redis_zset& option, int n)
|
||||
{
|
||||
acl::string key;
|
||||
int ret = 0, j;
|
||||
std::vector<std::pair<acl::string, double> > result;
|
||||
std::vector<std::pair<acl::string, double> >::const_iterator cit;
|
||||
|
||||
for (int i = 0; i < n; i++)
|
||||
{
|
||||
key.format("%s_%d", __keypre.c_str(), i);
|
||||
while (true)
|
||||
{
|
||||
ret = option.zscan(key.c_str(), ret, result);
|
||||
if (ret < 0)
|
||||
{
|
||||
printf("zscan failed, key: %s\r\n",
|
||||
key.c_str());
|
||||
break;
|
||||
}
|
||||
if (ret == 0)
|
||||
{
|
||||
printf("zscan over, key: %s\r\n",
|
||||
key.c_str());
|
||||
break;
|
||||
}
|
||||
if (i >= 10)
|
||||
continue;
|
||||
|
||||
j = 0;
|
||||
for (cit = result.begin(); cit != result.end(); ++cit)
|
||||
{
|
||||
printf("%s: %.2f\r\n", cit->first.c_str(),
|
||||
cit->second);
|
||||
j++;
|
||||
}
|
||||
printf("\r\n");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static void test_zrangebylex(acl::redis_zset& option, int n)
|
||||
{
|
||||
acl::string key;
|
||||
const char* min = "[aaa", *max = "(g";
|
||||
std::vector<acl::string> result;
|
||||
|
||||
for (int i = 0; i < n; i++)
|
||||
{
|
||||
key.format("%s_%d", __keypre.c_str(), i);
|
||||
int ret = option.zrangebylex(key.c_str(), min, max, result);
|
||||
if (ret < 0)
|
||||
{
|
||||
printf("zrangebylex error, key: %s\r\n", key.c_str());
|
||||
break;
|
||||
}
|
||||
if (i >= 10)
|
||||
{
|
||||
result.clear();
|
||||
continue;
|
||||
}
|
||||
|
||||
std::vector<acl::string>::const_iterator cit;
|
||||
for (cit = result.begin(); cit != result.end(); ++cit)
|
||||
{
|
||||
if (cit != result.begin())
|
||||
printf(", ");
|
||||
printf("%s", (*cit).c_str());
|
||||
}
|
||||
printf("\r\n");
|
||||
}
|
||||
}
|
||||
|
||||
static void test_zlexcount(acl::redis_zset& option, int n)
|
||||
{
|
||||
acl::string key;
|
||||
const char* min = "[aaa", *max = "(g";
|
||||
|
||||
for (int i = 0; i < n; i++)
|
||||
{
|
||||
key.format("%s_%d", __keypre.c_str(), i);
|
||||
int ret = option.zlexcount(key.c_str(), min, max);
|
||||
if (ret < 0)
|
||||
{
|
||||
printf("zlexcount error, key: %s\r\n", key.c_str());
|
||||
break;
|
||||
}
|
||||
if (i >= 10)
|
||||
continue;
|
||||
printf("key: %s, count: %d\r\n", key.c_str(), ret);
|
||||
}
|
||||
}
|
||||
|
||||
static void test_zremrangebylex(acl::redis_zset& option, int n)
|
||||
{
|
||||
acl::string key;
|
||||
const char* min = "[aaa", *max = "(g";
|
||||
|
||||
for (int i = 0; i < n; i++)
|
||||
{
|
||||
key.format("%s_%d", __keypre.c_str(), i);
|
||||
int ret = option.zremrangebylex(key.c_str(), min, max);
|
||||
if (ret < 0)
|
||||
{
|
||||
printf("zremrangebylex error, key: %s\r\n",
|
||||
key.c_str());
|
||||
break;
|
||||
}
|
||||
if (i >= 10)
|
||||
continue;
|
||||
printf("key: %s, count: %d\r\n", key.c_str(), ret);
|
||||
}
|
||||
}
|
||||
|
||||
static void usage(const char* procname)
|
||||
{
|
||||
printf("usage: %s -h[help]\r\n"
|
||||
@ -391,21 +562,8 @@ int main(int argc, char* argv[])
|
||||
test_zrank(option, n);
|
||||
else if (cmd == "zrem")
|
||||
test_zrem(option, n);
|
||||
#if 0
|
||||
else if (cmd == "zremrangebyrank")
|
||||
test_zremrangebyrank(option, n);
|
||||
else if (cmd == "zremrangebyscore")
|
||||
test_zremrangebyscore(option, n);
|
||||
else if (cmd == "zrevrange")
|
||||
test_zrevrange(option, n);
|
||||
else if (cmd == "zrevrangebyscore")
|
||||
test_zrevrangebyscore(option, n);
|
||||
else if (cmd == "zrevrank")
|
||||
test_zrevrank(option, n);
|
||||
#endif
|
||||
else if (cmd == "zscore")
|
||||
test_zscore(option, n);
|
||||
#if 0
|
||||
else if (cmd == "zunionstore")
|
||||
test_zunionstore(option, n);
|
||||
else if (cmd == "zinterstore")
|
||||
@ -417,8 +575,7 @@ int main(int argc, char* argv[])
|
||||
else if (cmd == "zlexcount")
|
||||
test_zlexcount(option, n);
|
||||
else if (cmd == "zremrangebylex")
|
||||
test_zremrangebylex("option, n");
|
||||
#endif
|
||||
test_zremrangebylex(option, n);
|
||||
else if (cmd == "all")
|
||||
{
|
||||
test_zadd(option, n);
|
||||
|
@ -407,6 +407,8 @@ long long int redis_client::get_number64(bool* success /* = NULL */)
|
||||
|
||||
int redis_client::get_number(std::vector<int>& out)
|
||||
{
|
||||
out.clear();
|
||||
|
||||
const redis_result* result = run();
|
||||
if (result == NULL || result->get_type() != REDIS_RESULT_ARRAY)
|
||||
return -1;
|
||||
@ -429,6 +431,8 @@ int redis_client::get_number(std::vector<int>& out)
|
||||
|
||||
int redis_client::get_number64(std::vector<long long int>& out)
|
||||
{
|
||||
out.clear();
|
||||
|
||||
const redis_result* result = run();
|
||||
if (result == NULL || result->get_type() != REDIS_RESULT_ARRAY)
|
||||
return -1;
|
||||
@ -465,6 +469,8 @@ bool redis_client::get_status(const char* success /* = "OK" */)
|
||||
|
||||
int redis_client::get_status(std::vector<bool>& out)
|
||||
{
|
||||
out.clear();
|
||||
|
||||
const redis_result* result = run();
|
||||
if (result == NULL || result->get_type() != REDIS_RESULT_ARRAY)
|
||||
return -1;
|
||||
@ -520,6 +526,8 @@ int redis_client::get_string(char* buf, size_t size)
|
||||
|
||||
int redis_client::get_strings(std::vector<string>& out)
|
||||
{
|
||||
out.clear();
|
||||
|
||||
const redis_result* result = run();
|
||||
if (result == NULL || result->get_type() != REDIS_RESULT_ARRAY)
|
||||
return -1;
|
||||
@ -561,6 +569,8 @@ int redis_client::get_strings(std::vector<string>* out)
|
||||
if (out == NULL)
|
||||
return result->get_size();
|
||||
|
||||
out->clear();
|
||||
|
||||
size_t size;
|
||||
const redis_result** children = result->get_children(&size);
|
||||
if (children == NULL)
|
||||
@ -592,6 +602,8 @@ int redis_client::get_strings(std::vector<string>* out)
|
||||
|
||||
int redis_client::get_strings(std::map<string, string>& out)
|
||||
{
|
||||
out.clear();
|
||||
|
||||
const redis_result* result = run();
|
||||
if (result == NULL || result->get_type() != REDIS_RESULT_ARRAY)
|
||||
return -1;
|
||||
@ -631,6 +643,9 @@ int redis_client::get_strings(std::map<string, string>& out)
|
||||
int redis_client::get_strings(std::vector<string>& names,
|
||||
std::vector<string>& values)
|
||||
{
|
||||
names.clear();
|
||||
values.clear();
|
||||
|
||||
const redis_result* result = run();
|
||||
if (result == NULL || result->get_type() != REDIS_RESULT_ARRAY)
|
||||
return -1;
|
||||
@ -673,6 +688,9 @@ int redis_client::get_strings(std::vector<string>& names,
|
||||
int redis_client::get_strings(std::vector<const char*>& names,
|
||||
std::vector<const char*>& values)
|
||||
{
|
||||
names.clear();
|
||||
values.clear();
|
||||
|
||||
const redis_result* result = run();
|
||||
if (result == NULL || result->get_type() != REDIS_RESULT_ARRAY)
|
||||
return -1;
|
||||
@ -780,7 +798,7 @@ void redis_client::build_request1(size_t argc, const char* argv[], size_t lens[]
|
||||
request_.append(argv[i], lens[i]);
|
||||
request_.append("\r\n");
|
||||
}
|
||||
// printf("%s", request_.c_str());
|
||||
//printf("%s", request_.c_str());
|
||||
}
|
||||
|
||||
void redis_client::build_request2(size_t argc, const char* argv[], size_t lens[])
|
||||
|
@ -91,7 +91,7 @@ const redis_result** redis_command::scan_keys(const char* cmd, const char* key,
|
||||
}
|
||||
|
||||
conn_->build_request(argc, argv, lens);
|
||||
const redis_result* result = conn_->run(2);
|
||||
const redis_result* result = conn_->run();
|
||||
if (result == NULL)
|
||||
{
|
||||
cursor = -1;
|
||||
@ -110,7 +110,13 @@ const redis_result** redis_command::scan_keys(const char* cmd, const char* key,
|
||||
cursor = -1;
|
||||
return NULL;
|
||||
}
|
||||
cursor = rr->get_integer();
|
||||
string tmp(128);
|
||||
if (rr->argv_to_string(tmp) == 0)
|
||||
{
|
||||
cursor = -1;
|
||||
return NULL;
|
||||
}
|
||||
cursor = atoi(tmp.c_str());
|
||||
if (cursor < 0)
|
||||
{
|
||||
cursor = -1;
|
||||
|
@ -377,6 +377,7 @@ int redis_hash::hscan(const char* key, int cursor,
|
||||
|
||||
const redis_result* rr;
|
||||
string name(128), value(128);
|
||||
out.clear();
|
||||
|
||||
for (size_t i = 0; i < size;)
|
||||
{
|
||||
|
@ -512,6 +512,8 @@ int redis_key::scan(int cursor, std::vector<string>& out,
|
||||
|
||||
const redis_result* rr;
|
||||
string key_buf(128);
|
||||
|
||||
out.clear();
|
||||
out.reserve(size);
|
||||
|
||||
for (size_t i = 0; i < size; i++)
|
||||
|
@ -70,7 +70,8 @@ bool redis_list::lset(const char* key, size_t idx, const char* value)
|
||||
return lset(key, idx, value, strlen(value));
|
||||
}
|
||||
|
||||
bool redis_list::lset(const char* key, size_t idx, const char* value, size_t len)
|
||||
bool redis_list::lset(const char* key, size_t idx,
|
||||
const char* value, size_t len)
|
||||
{
|
||||
const char* argv[4];
|
||||
size_t lens[4];
|
||||
@ -448,7 +449,6 @@ bool redis_list::bpop(std::pair<string, string>& out)
|
||||
first->argv_to_string(buf);
|
||||
out.first = buf;
|
||||
|
||||
buf.clear();
|
||||
second->argv_to_string(buf);
|
||||
out.second = buf;
|
||||
return true;
|
||||
|
@ -352,6 +352,7 @@ int redis_pubsub::pubsub_numsub(std::map<string, int>& out)
|
||||
|
||||
string buf(128);
|
||||
const redis_result* rr;
|
||||
out.clear();
|
||||
|
||||
for (size_t i = 0; i < size;)
|
||||
{
|
||||
|
@ -160,6 +160,8 @@ size_t redis_result::get_length() const
|
||||
|
||||
size_t redis_result::argv_to_string(string& buf) const
|
||||
{
|
||||
buf.clear();
|
||||
|
||||
if (idx_ == 0)
|
||||
return 0;
|
||||
|
||||
|
@ -216,6 +216,7 @@ int redis_script::eval_status(const char* cmd, const char* script,
|
||||
|
||||
const redis_result* rr;
|
||||
const char* status;
|
||||
out.clear();
|
||||
|
||||
for (size_t i = 0; i < size; i++)
|
||||
{
|
||||
@ -250,6 +251,8 @@ int redis_script::eval_number(const char* cmd, const char* script,
|
||||
const redis_result* rr;
|
||||
int number;
|
||||
bool success;
|
||||
out.clear();
|
||||
status.clear();
|
||||
|
||||
for (size_t i = 0; i < size; i++)
|
||||
{
|
||||
@ -277,7 +280,9 @@ long long int redis_script::eval_number64(const char* cmd, const char* script,
|
||||
if (children == NULL || size == 0)
|
||||
return 0;
|
||||
|
||||
out.clear();
|
||||
out.reserve(size);
|
||||
status.clear();
|
||||
|
||||
const redis_result* rr;
|
||||
long long int number;
|
||||
@ -308,6 +313,7 @@ int redis_script::eval_strings(const char* cmd, const char* script,
|
||||
if (children == NULL || size == 0)
|
||||
return 0;
|
||||
|
||||
out.clear();
|
||||
out.reserve(size);
|
||||
|
||||
const redis_result* rr;
|
||||
@ -481,6 +487,8 @@ bool redis_script::script_flush()
|
||||
|
||||
bool redis_script::script_load(const string& script, string& out)
|
||||
{
|
||||
out.clear();
|
||||
|
||||
const char* argv[3];
|
||||
size_t lens[3];
|
||||
|
||||
|
@ -421,6 +421,8 @@ int redis_set::sscan(const char* key, int cursor, std::vector<string>& out,
|
||||
|
||||
const redis_result* rr;
|
||||
string key_buf(128);
|
||||
|
||||
out.clear();
|
||||
out.reserve(size);
|
||||
|
||||
for (size_t i = 0; i < size; i++)
|
||||
|
@ -22,7 +22,7 @@ redis_zset::~redis_zset()
|
||||
|
||||
}
|
||||
|
||||
int redis_zset::zadd(const char* key, std::map<string, double>& members)
|
||||
int redis_zset::zadd(const char* key, const std::map<string, double>& members)
|
||||
{
|
||||
dbuf_pool* pool = conn_->get_pool();
|
||||
size_t argc = 2 + members.size() * 2;
|
||||
@ -58,7 +58,7 @@ int redis_zset::zadd(const char* key, std::map<string, double>& members)
|
||||
}
|
||||
|
||||
int redis_zset::zadd(const char* key,
|
||||
std::vector<std::pair<string, double> >&members)
|
||||
const std::vector<std::pair<string, double> >&members)
|
||||
{
|
||||
dbuf_pool* pool = conn_->get_pool();
|
||||
size_t argc = 2 + members.size() * 2;
|
||||
@ -93,7 +93,7 @@ int redis_zset::zadd(const char* key,
|
||||
}
|
||||
|
||||
int redis_zset::zadd(const char* key,
|
||||
std::vector<std::pair<const char*, double> >&members)
|
||||
const std::vector<std::pair<const char*, double> >&members)
|
||||
{
|
||||
dbuf_pool* pool = conn_->get_pool();
|
||||
size_t argc = 2 + members.size() * 2;
|
||||
@ -128,8 +128,8 @@ int redis_zset::zadd(const char* key,
|
||||
return conn_->get_number();
|
||||
}
|
||||
|
||||
int redis_zset::zadd(const char* key, std::vector<string>& members,
|
||||
std::vector<double>& scores)
|
||||
int redis_zset::zadd(const char* key, const std::vector<string>& members,
|
||||
const std::vector<double>& scores)
|
||||
{
|
||||
size_t size = scores.size();
|
||||
if (size != members.size())
|
||||
@ -167,8 +167,8 @@ int redis_zset::zadd(const char* key, std::vector<string>& members,
|
||||
return conn_->get_number();
|
||||
}
|
||||
|
||||
int redis_zset::zadd(const char* key, std::vector<const char*>& members,
|
||||
std::vector<double>& scores)
|
||||
int redis_zset::zadd(const char* key, const std::vector<const char*>& members,
|
||||
const std::vector<double>& scores)
|
||||
{
|
||||
size_t size = scores.size();
|
||||
if (size != members.size())
|
||||
@ -386,6 +386,8 @@ int redis_zset::zrange(const char* key, int start,
|
||||
int redis_zset::zrange_get_with_scores(const char* cmd, const char* key,
|
||||
int start, int stop, std::vector<std::pair<string, double> >& out)
|
||||
{
|
||||
out.clear();
|
||||
|
||||
const char* argv[5];
|
||||
size_t lens[5];
|
||||
|
||||
@ -574,6 +576,7 @@ int redis_zset::zrangebyscore_get_with_scores(const char* cmd,
|
||||
double score;
|
||||
const redis_result* child;
|
||||
string buf(128);
|
||||
out.clear();
|
||||
|
||||
for (size_t i = 0; i < size; i++)
|
||||
{
|
||||
@ -1054,7 +1057,8 @@ int redis_zset::zremrangebylex(const char* key, const char* min, const char* max
|
||||
return conn_->get_number();
|
||||
}
|
||||
|
||||
int redis_zset::zscan(const char* key, int cursor, std::map<string, double>& out,
|
||||
int redis_zset::zscan(const char* key, int cursor,
|
||||
std::vector<std::pair<string, double> >& out,
|
||||
const char* pattern /* = NULL */, const size_t* count /* = NULL */)
|
||||
{
|
||||
if (key == NULL || *key == 0 || cursor < 0)
|
||||
@ -1069,6 +1073,9 @@ int redis_zset::zscan(const char* key, int cursor, std::map<string, double>& out
|
||||
if (size % 2 != 0)
|
||||
return -1;
|
||||
|
||||
out.clear();
|
||||
out.reserve(size);
|
||||
|
||||
const redis_result* rr;
|
||||
string name(128), value(128);
|
||||
|
||||
@ -1082,7 +1089,7 @@ int redis_zset::zscan(const char* key, int cursor, std::map<string, double>& out
|
||||
rr->argv_to_string(value);
|
||||
i++;
|
||||
|
||||
out[name] = atof(value.c_str());
|
||||
out.push_back(std::make_pair(name, atof(value.c_str())));
|
||||
}
|
||||
|
||||
return cursor;
|
||||
|
Loading…
Reference in New Issue
Block a user