acl/lib_acl_cpp/samples/redis/redis_set/redis_set.cpp

466 lines
9.5 KiB
C++
Raw Normal View History

2015-01-27 23:37:33 +08:00
#include "stdafx.h"
static acl::string __keypre("set_key");
2015-01-27 23:37:33 +08:00
static void test_sadd(acl::redis_set& option, int n)
2015-01-27 23:37:33 +08:00
{
acl::string key;
std::vector<acl::string> members;
2015-01-27 23:37:33 +08:00
acl::string member;
members.reserve(1000);
for (int j = 0; j < 1000; j++)
{
member.format("member_%d", j);
members.push_back(member);
}
2015-01-27 23:37:33 +08:00
for (int i = 0; i < n; i++)
{
key.format("%s_%d", __keypre.c_str(), i);
option.reset();
int ret = option.sadd(key, members);
2015-01-27 23:37:33 +08:00
if (ret < 0)
{
printf("sadd key: %s error\r\n", key.c_str());
2015-01-27 23:37:33 +08:00
break;
}
else if (i >= 10)
continue;
printf("sadd ok, key: %s, ret: %d\r\n", key.c_str(), ret);
2015-01-27 23:37:33 +08:00
}
}
static void test_scard(acl::redis_set& option, int n)
2015-01-27 23:37:33 +08:00
{
acl::string key;
for (int i = 0; i < n; i++)
{
key.format("%s_%d", __keypre.c_str(), i);
option.reset();
int ret = option.scard(key.c_str());
2015-01-27 23:37:33 +08:00
if (ret < 0)
{
printf("scard key: %s error\r\n", key.c_str());
2015-01-27 23:37:33 +08:00
break;
}
else if (i >= 10)
continue;
printf("scard ok, key: %s, count: %d\r\n", key.c_str(), ret);
2015-01-27 23:37:33 +08:00
}
}
static void test_sdiff(acl::redis_set& option, int n)
2015-01-27 23:37:33 +08:00
{
acl::string key;
std::vector<acl::string> keys;
std::vector<acl::string> result;
2015-01-27 23:37:33 +08:00
for (int i = 0; i < 10; i++)
2015-01-27 23:37:33 +08:00
{
key.format("%s_%d", __keypre.c_str(), i);
keys.push_back(key);
}
for (int i = 0; i < n; i++)
{
2015-01-27 23:37:33 +08:00
option.reset();
int ret = option.sdiff(keys, &result);
2015-01-27 23:37:33 +08:00
if (ret < 0)
{
printf("sdiff error\r\n");
2015-01-27 23:37:33 +08:00
break;
}
else if (i < 10)
printf("sdiff ok, count: %d\r\n", ret);
2015-01-27 23:37:33 +08:00
}
}
static void test_sdiffstore(acl::redis_set& option, int n)
2015-01-27 23:37:33 +08:00
{
acl::string key, dest_key("set_dest_key");
std::vector<acl::string> keys;
2015-01-27 23:37:33 +08:00
for (int i = 0; i < 10; i++)
2015-01-27 23:37:33 +08:00
{
key.format("%s_%d", __keypre.c_str(), i);
keys.push_back(key);
2015-01-27 23:37:33 +08:00
}
for (int i = 0; i < n; i++)
{
option.reset();
int ret = option.sdiffstore(dest_key.c_str(), keys);
2015-01-27 23:37:33 +08:00
if (ret < 0)
{
printf("sdiffstore error, dest: %s\r\n",
dest_key.c_str());
2015-01-27 23:37:33 +08:00
break;
}
else if (i >= 10)
continue;
printf("sdiffstore ok, dest: %s, count: %d\r\n",
dest_key.c_str(), ret);
2015-01-27 23:37:33 +08:00
}
}
2015-01-27 23:37:33 +08:00
static void test_sismember(acl::redis_set& option, int n)
{
bool ret;
acl::string key, member;
2015-01-27 23:37:33 +08:00
for (int i = 0; i < n; i++)
{
key.format("%s_%d", __keypre.c_str(), i);
for (int j = 0; j < 1000; j++)
2015-01-27 23:37:33 +08:00
{
member.format("member_%d", j);
option.reset();
ret = option.sismember(key.c_str(), member.c_str());
if (option.eof())
{
printf("sismmeber eof, key: %s, member: %s\r\n",
key.c_str(), member.c_str());
return;
}
if (i >= 10)
continue;
2015-01-27 23:37:33 +08:00
printf("sismember: %s, key: %s, member: %s\r\n",
ret ? "true" : "false", key.c_str(),
member.c_str());
2015-01-27 23:37:33 +08:00
}
}
}
static void test_smembers(acl::redis_set& option, int n)
2015-01-27 23:37:33 +08:00
{
acl::string key;
std::vector<acl::string> members;
2015-01-27 23:37:33 +08:00
for (int i = 0; i < n; i++)
{
key.format("%s_%d", __keypre.c_str(), i);
option.reset();
int ret = option.smembers(key.c_str(), &members);
2015-01-27 23:37:33 +08:00
if (ret < 0)
{
printf("smembers error, key: %s\r\n", key.c_str());
2015-01-27 23:37:33 +08:00
break;
}
else if (i >= 10)
continue;
printf("smembers ok, key: %s, count: %d\r\n", key.c_str(), ret);
2015-01-27 23:37:33 +08:00
}
}
static void test_smove(acl::redis_set& option, int n)
2015-01-27 23:37:33 +08:00
{
acl::string src_key, dst_key;
2015-01-27 23:37:33 +08:00
acl::string member;
int ret;
2015-01-27 23:37:33 +08:00
for (int i = 0; i < n; i++)
{
src_key.format("%s_%d", __keypre.c_str(), i);
dst_key.format("dest_%s_%d", __keypre.c_str(), i);
2015-01-27 23:37:33 +08:00
for (int j = 0; j < 1000; j++)
{
member.format("member_%d", j);
option.reset();
ret = option.smove(src_key.c_str(), dst_key.c_str(),
member.c_str());
2015-01-27 23:37:33 +08:00
if (ret < 0)
{
printf("smove error, src: %s, des: %s\r\n",
src_key.c_str(), dst_key.c_str());
2015-01-27 23:37:33 +08:00
break;
}
else if (j * i >= 100)
continue;
printf("smove ok, src: %s, dst: %s, member:%s, ret: %d\r\n",
src_key.c_str(), dst_key.c_str(),
member.c_str(), ret);
2015-01-27 23:37:33 +08:00
}
}
}
static void test_spop(acl::redis_set& option, int n)
2015-01-27 23:37:33 +08:00
{
acl::string key;
acl::string member;
for (int i = 0; i < n; i++)
{
key.format("%s_%d", __keypre.c_str(), i);
option.reset();
bool ret = option.spop(key.c_str(), member);
if (option.eof())
2015-01-27 23:37:33 +08:00
{
printf("spop eof, key: %s\r\n", key.c_str());
2015-01-27 23:37:33 +08:00
break;
}
if (i >= 10)
continue;
printf("spop %s, key: %s, member: %s\r\n",
ret ? "ok" : "empty", key.c_str(),
ret ? member.c_str() : "");
2015-01-27 23:37:33 +08:00
}
}
static void test_srandmember(acl::redis_set& option, int n)
2015-01-27 23:37:33 +08:00
{
acl::string key;
acl::string member;
int ret;
2015-01-27 23:37:33 +08:00
for (int i = 0; i < n; i++)
{
key.format("%s_%d", __keypre.c_str(), i);
option.reset();
ret = option.srandmember(key.c_str(), member);
if (option.eof())
break;
2015-01-27 23:37:33 +08:00
if (i >= 10)
continue;
printf("srandmember %s, key: %s, member: %s\r\n",
ret ? "ok" : "empty", key.c_str(), member.c_str());
2015-01-27 23:37:33 +08:00
}
}
static void test_srem(acl::redis_set& option, int n)
2015-01-27 23:37:33 +08:00
{
acl::string key;
acl::string member;
std::vector<acl::string> members;
2015-01-27 23:37:33 +08:00
members.reserve(1000);
2015-01-27 23:37:33 +08:00
for (int j = 0; j < 1000; j++)
{
member.format("member_%d", j);
members.push_back(member);
2015-01-27 23:37:33 +08:00
}
for (int i = 0; i < n; i++)
{
key.format("%s_%d", __keypre.c_str(), i);
option.reset();
int ret = option.srem(key.c_str(), members);
2015-01-27 23:37:33 +08:00
if (ret < 0)
{
printf("srem error, key: %s\r\n", key.c_str());
2015-01-27 23:37:33 +08:00
break;
}
if (i >= 10)
continue;
printf("srem ok, key: %s, ret: %d\r\n", key.c_str(), ret);
2015-01-27 23:37:33 +08:00
}
}
static void test_sunion(acl::redis_set& option ,int n)
2015-01-27 23:37:33 +08:00
{
acl::string key;
std::vector<acl::string> keys;
std::vector<acl::string> result;
2015-01-27 23:37:33 +08:00
for (int i = 0; i < 10; i++)
2015-01-27 23:37:33 +08:00
{
key.format("%s_%d", __keypre.c_str(), i);
keys.push_back(key);
2015-01-27 23:37:33 +08:00
}
for (int i = 0; i < n; i++)
{
option.reset();
int ret = option.sunion(keys, &result);
2015-01-27 23:37:33 +08:00
if (ret < 0)
{
printf("sunion error\r\n");
2015-01-27 23:37:33 +08:00
break;
}
else if (i < 10)
printf("sunion ok, count: %d\r\n", ret);
2015-01-27 23:37:33 +08:00
}
}
static void test_sunionstore(acl::redis_set& option, int n)
2015-01-27 23:37:33 +08:00
{
acl::string key, dest_key("set_dest_key");
std::vector<acl::string> keys;
2015-01-27 23:37:33 +08:00
for (int i = 0; i < 10; i++)
2015-01-27 23:37:33 +08:00
{
key.format("%s_%d", __keypre.c_str(), i);
keys.push_back(key);
}
for (int i = 0; i < n; i++)
{
option.reset();
int ret = option.sunionstore(dest_key.c_str(), keys);
2015-01-27 23:37:33 +08:00
if (ret < 0)
{
printf("sdiffstore error, dest: %s\r\n",
dest_key.c_str());
2015-01-27 23:37:33 +08:00
break;
}
else if (i < 10)
printf("sdiffstore ok, dest: %s, count: %d\r\n",
dest_key.c_str(), ret);
2015-01-27 23:37:33 +08:00
}
}
static void test_sscan(acl::redis_set& option, int n)
2015-01-27 23:37:33 +08:00
{
acl::string key;
int ret = 0;
std::vector<acl::string> result;
std::vector<acl::string>::const_iterator cit;
2015-01-27 23:37:33 +08:00
for (int i = 0; i < n; i++)
{
key.format("%s_%d", __keypre.c_str(), i);
while (true)
2015-01-27 23:37:33 +08:00
{
option.reset();
ret = option.sscan(key.c_str(), ret, result);
if (ret < 0)
{
printf("sscan failed, key: %s\r\n",
key.c_str());
break;
}
if (i < 10)
{
cit = result.begin();
for (; cit != result.end(); ++cit)
printf("%s: %s\r\n", key.c_str(),
(*cit).c_str());
}
if (ret == 0)
{
printf("sscan over, key: %s\r\n",
key.c_str());
break;
}
2015-01-27 23:37:33 +08:00
}
}
}
static void usage(const char* procname)
{
printf("usage: %s -h[help]\r\n"
"-s redis_addr[127.0.0.1:6379]\r\n"
"-n count\r\n"
"-C connect_timeout[default: 10]\r\n"
"-T rw_timeout[default: 10]\r\n"
"-a cmd[sadd|scard|sdiffstore|sismember|smembers|smove|spop|srandmember|sunion|sunionstore|sscan|srem\r\n",
2015-01-27 23:37:33 +08:00
procname);
}
int main(int argc, char* argv[])
{
int ch, n = 1, conn_timeout = 10, rw_timeout = 10;
acl::string addr("127.0.0.1:6379"), cmd;
while ((ch = getopt(argc, argv, "hs:n:C:T:a:")) > 0)
{
switch (ch)
{
case 'h':
usage(argv[0]);
return 0;
case 's':
addr = optarg;
break;
case 'n':
n = atoi(optarg);
break;
case 'C':
conn_timeout = atoi(optarg);
break;
case 'T':
rw_timeout = atoi(optarg);
break;
case 'a':
cmd = optarg;
break;
default:
break;
}
}
acl::acl_cpp_init();
acl::redis_client client(addr.c_str(), conn_timeout, rw_timeout);
acl::redis_set option(&client);
if (cmd == "sadd")
test_sadd(option, n);
else if (cmd == "scard")
test_scard(option, n);
else if (cmd == "sdiff")
test_sdiff(option, n);
else if (cmd == "sdiffstore")
test_sdiffstore(option, n);
else if (cmd == "sismember")
test_sismember(option, n);
else if (cmd == "smembers")
test_smembers(option, n);
else if (cmd == "smove")
test_smove(option, n);
else if (cmd == "spop")
test_spop(option, n);
else if (cmd == "srandmember")
test_srandmember(option, n);
else if (cmd == "srem")
test_srem(option, n);
else if (cmd == "sunion")
test_sunion(option, n);
else if (cmd == "sunionstore")
test_sunionstore(option, n);
else if (cmd == "sscan")
test_sscan(option, n);
2015-01-27 23:37:33 +08:00
else if (cmd == "all")
{
test_sadd(option, n);
test_scard(option, n);
test_sdiff(option, n);
test_sdiffstore(option, n);
test_sismember(option, n);
test_smembers(option, n);
test_smove(option, n);
test_spop(option, n);
test_srandmember(option, n);
test_sunion(option, n);
test_sunionstore(option, n);
test_sscan(option, n);
test_srem(option, n);
2015-01-27 23:37:33 +08:00
}
else
printf("unknown cmd: %s\r\n", cmd.c_str());
#ifdef WIN32
printf("enter any key to exit\r\n");
getchar();
#endif
return 0;
}