Add more set methods

This commit is contained in:
fbraem 2015-11-14 22:02:36 +01:00
parent d4a791d79c
commit 7a72e3d695
4 changed files with 551 additions and 17 deletions

View File

@ -109,17 +109,26 @@ public:
static Command mset(const std::map<std::string, std::string>& keyvalues, bool create = true);
/// Returns a MSET or MSETNX (when create is false) command
static Command sadd(const std::string& key, const std::string& value);
static Command sadd(const std::string& set, const std::string& value);
/// Returns a SADD command
static Command sadd(const std::string& key, const std::vector<std::string>& values);
static Command sadd(const std::string& set, const std::vector<std::string>& values);
/// Returns a SADD command
static Command scard(const std::string& key);
static Command scard(const std::string& set);
/// Returns a SCARD command
static Command smembers(const std::string& key);
/// Returns a SMEMBERS command
static Command sdiff(const std::string& set1, const std::string& set2);
/// Returns a SDIFF command
static Command sdiff(const std::string& set, const std::vector<std::string>& sets);
/// Returns a SDIFF command
static Command sdiffstore(const std::string& set, const std::string& set1, const std::string& set2);
/// Returns a SDIFFSTORE command
static Command sdiffstore(const std::string& set, const std::vector<std::string>& sets);
/// Returns a SDIFFSTORE command
static Command set(const std::string& key, const std::string& value, bool overwrite = true, const Poco::Timespan& expireTime = 0, bool create = true);
/// Returns a SET command to set the key with a value
@ -127,6 +136,33 @@ public:
static Command set(const std::string& key, Int64 value, bool overwrite = true, const Poco::Timespan& expireTime = 0, bool create = true);
/// Returns a SET command to set the key with a value
static Command sinter(const std::string& set1, const std::string& set2);
/// Returns a SINTER command
static Command sinter(const std::string& set, const std::vector<std::string>& sets);
/// Returns a SINTER command
static Command sinterstore(const std::string& set, const std::string& set1, const std::string& set2);
/// Returns a SINTERSTORE command
static Command sinterstore(const std::string& set, const std::vector<std::string>& sets);
/// Returns a SINTERSTORE command
static Command smembers(const std::string& set);
/// Returns a SMEMBERS command
static Command sunion(const std::string& set1, const std::string& set2);
/// Returns a SUNION command
static Command sunion(const std::string& set, const std::vector<std::string>& sets);
/// Returns a SUNION command
static Command sunionstore(const std::string& set, const std::string& set1, const std::string& set2);
/// Returns a SUNIONSTORE command
static Command sunionstore(const std::string& set, const std::vector<std::string>& sets);
/// Returns a SUNIONSTORE command
static Command rename(const std::string& key, const std::string& newName, bool overwrite = true);
/// Returns a RENAME or RENAMENX when overwrite is false

View File

@ -245,20 +245,20 @@ Command Command::mset(const std::map<std::string, std::string>& keyvalues, bool
return cmd;
}
Command Command::sadd(const std::string& key, const std::string& value)
Command Command::sadd(const std::string& set, const std::string& value)
{
Command cmd("SADD");
cmd << key << value;
cmd << set << value;
return cmd;
}
Command Command::sadd(const std::string& key, const std::vector<std::string>& values)
Command Command::sadd(const std::string& set, const std::vector<std::string>& values)
{
Command cmd("SADD");
cmd << key;
cmd << set;
for(std::vector<std::string>::const_iterator it = values.begin(); it != values.end(); ++it)
{
cmd << *it;
@ -267,20 +267,55 @@ Command Command::sadd(const std::string& key, const std::vector<std::string>& va
return cmd;
}
Command Command::scard(const std::string& key)
Command Command::scard(const std::string& set)
{
Command cmd("SCARD");
cmd << key;
cmd << set;
return cmd;
}
Command Command::smembers(const std::string& key)
Command Command::sdiff(const std::string& set1, const std::string& set2)
{
Command cmd("SMEMBERS");
Command cmd("SDIFF");
cmd << key;
cmd << set1 << set2;
return cmd;
}
Command Command::sdiff(const std::string& set, const std::vector<std::string>& sets)
{
Command cmd("SDIFF");
cmd << set;
for(std::vector<std::string>::const_iterator it = sets.begin(); it != sets.end(); ++it)
{
cmd << *it;
}
return cmd;
}
Command Command::sdiffstore(const std::string& set, const std::string& set1, const std::string& set2)
{
Command cmd("SDIFFSTORE");
cmd << set << set1 << set2;
return cmd;
}
Command Command::sdiffstore(const std::string& set, const std::vector<std::string>& sets)
{
Command cmd("SDIFFSTORE");
cmd << set;
for(std::vector<std::string>::const_iterator it = sets.begin(); it != sets.end(); ++it)
{
cmd << *it;
}
return cmd;
}
@ -302,6 +337,103 @@ Command Command::set(const std::string& key, Int64 value, bool overwrite, const
return set(key, NumberFormatter::format(value), overwrite, expireTime, create);
}
Command Command::sinter(const std::string& set1, const std::string& set2)
{
Command cmd("SINTER");
cmd << set1 << set2;
return cmd;
}
Command Command::sinter(const std::string& set, const std::vector<std::string>& sets)
{
Command cmd("SINTER");
cmd << set;
for(std::vector<std::string>::const_iterator it = sets.begin(); it != sets.end(); ++it)
{
cmd << *it;
}
return cmd;
}
Command Command::sinterstore(const std::string& set, const std::string& set1, const std::string& set2)
{
Command cmd("SINTERSTORE");
cmd << set << set1 << set2;
return cmd;
}
Command Command::sinterstore(const std::string& set, const std::vector<std::string>& sets)
{
Command cmd("SINTERSTORE");
cmd << set;
for(std::vector<std::string>::const_iterator it = sets.begin(); it != sets.end(); ++it)
{
cmd << *it;
}
return cmd;
}
Command Command::smembers(const std::string& set)
{
Command cmd("SMEMBERS");
cmd << set;
return cmd;
}
Command Command::sunion(const std::string& set1, const std::string& set2)
{
Command cmd("SUNION");
cmd << set1 << set2;
return cmd;
}
Command Command::sunion(const std::string& set, const std::vector<std::string>& sets)
{
Command cmd("SUNION");
cmd << set;
for(std::vector<std::string>::const_iterator it = sets.begin(); it != sets.end(); ++it)
{
cmd << *it;
}
return cmd;
}
Command Command::sunionstore(const std::string& set, const std::string& set1, const std::string& set2)
{
Command cmd("SUNIONSTORE");
cmd << set << set1 << set2;
return cmd;
}
Command Command::sunionstore(const std::string& set, const std::vector<std::string>& sets)
{
Command cmd("SUNIONSTORE");
cmd << set;
for(std::vector<std::string>::const_iterator it = sets.begin(); it != sets.end(); ++it)
{
cmd << *it;
}
return cmd;
}
Command Command::rename(const std::string& key, const std::string& newName, bool overwrite)
{
Command cmd(overwrite ? "RENAME" : "RENAMENX");

View File

@ -1198,6 +1198,124 @@ void RedisTest::testSCard()
}
}
void RedisTest::testSDiff()
{
if (!_connected)
{
std::cout << "Not connected, test skipped." << std::endl;
return;
}
delKey("key1");
delKey("key2");
std::vector<std::string> values1;
values1.push_back("a");
values1.push_back("b");
values1.push_back("c");
Command sadd = Command::sadd("key1", values1);
try
{
Poco::Int64 result = _redis.execute<Poco::Int64>(sadd);
assert(result == 3);
}
catch(RedisException &e)
{
fail(e.message());
}
std::vector<std::string> values2;
values2.push_back("c");
values2.push_back("d");
values2.push_back("e");
sadd = Command::sadd("key2", values2);
try
{
Poco::Int64 result = _redis.execute<Poco::Int64>(sadd);
assert(result == 3);
}
catch(RedisException &e)
{
fail(e.message());
}
Command sdiff = Command::sdiff("key1", "key2");
try
{
Array result = _redis.execute<Array>(sdiff);
assert(result.size() == 2);
}
catch(RedisException &e)
{
fail(e.message());
}
}
void RedisTest::testSDiffStore()
{
if (!_connected)
{
std::cout << "Not connected, test skipped." << std::endl;
return;
}
delKey("key");
delKey("key1");
delKey("key2");
std::vector<std::string> values1;
values1.push_back("a");
values1.push_back("b");
values1.push_back("c");
Command sadd = Command::sadd("key1", values1);
try
{
Poco::Int64 result = _redis.execute<Poco::Int64>(sadd);
assert(result == 3);
}
catch(RedisException &e)
{
fail(e.message());
}
std::vector<std::string> values2;
values2.push_back("c");
values2.push_back("d");
values2.push_back("e");
sadd = Command::sadd("key2", values2);
try
{
Poco::Int64 result = _redis.execute<Poco::Int64>(sadd);
assert(result == 3);
}
catch(RedisException &e)
{
fail(e.message());
}
Command sdiffstore = Command::sdiffstore("key", "key1", "key2");
try
{
Poco::Int64 result = _redis.execute<Poco::Int64>(sdiffstore);
assert(result == 2);
}
catch(RedisException &e)
{
fail(e.message());
}
Command smembers = Command::smembers("key");
try
{
Array result = _redis.execute<Array>(smembers);
assert(result.size() == 2);
}
catch(RedisException &e)
{
fail(e.message());
}
}
void RedisTest::testSet()
{
if (!_connected)
@ -1234,6 +1352,127 @@ void RedisTest::testSet()
}
}
void RedisTest::testSInter()
{
if (!_connected)
{
std::cout << "Not connected, test skipped." << std::endl;
return;
}
delKey("key1");
delKey("key2");
std::vector<std::string> values1;
values1.push_back("a");
values1.push_back("b");
values1.push_back("c");
Command sadd = Command::sadd("key1", values1);
try
{
Poco::Int64 result = _redis.execute<Poco::Int64>(sadd);
assert(result == 3);
}
catch(RedisException &e)
{
fail(e.message());
}
std::vector<std::string> values2;
values2.push_back("c");
values2.push_back("d");
values2.push_back("e");
sadd = Command::sadd("key2", values2);
try
{
Poco::Int64 result = _redis.execute<Poco::Int64>(sadd);
assert(result == 3);
}
catch(RedisException &e)
{
fail(e.message());
}
Command sinter = Command::sinter("key1", "key2");
try
{
Array result = _redis.execute<Array>(sinter);
assert(result.size() == 1);
assert(result.get<BulkString>(0).value().compare("c") == 0);
}
catch(RedisException &e)
{
fail(e.message());
}
}
void RedisTest::testSInterStore()
{
if (!_connected)
{
std::cout << "Not connected, test skipped." << std::endl;
return;
}
delKey("key");
delKey("key1");
delKey("key2");
std::vector<std::string> values1;
values1.push_back("a");
values1.push_back("b");
values1.push_back("c");
Command sadd = Command::sadd("key1", values1);
try
{
Poco::Int64 result = _redis.execute<Poco::Int64>(sadd);
assert(result == 3);
}
catch(RedisException &e)
{
fail(e.message());
}
std::vector<std::string> values2;
values2.push_back("c");
values2.push_back("d");
values2.push_back("e");
sadd = Command::sadd("key2", values2);
try
{
Poco::Int64 result = _redis.execute<Poco::Int64>(sadd);
assert(result == 3);
}
catch(RedisException &e)
{
fail(e.message());
}
Command sinterstore = Command::sinterstore("key", "key1", "key2");
try
{
Poco::Int64 result = _redis.execute<Poco::Int64>(sinterstore);
assert(result == 1);
}
catch(RedisException &e)
{
fail(e.message());
}
Command smembers = Command::smembers("key");
try
{
Array result = _redis.execute<Array>(smembers);
assert(result.size() == 1);
assert(result.get<BulkString>(0).value().compare("c") == 0);
}
catch(RedisException &e)
{
fail(e.message());
}
}
void RedisTest::testStrlen()
{
if (!_connected)
@ -1308,10 +1547,125 @@ void RedisTest::testSMembers()
try
{
Array result = _redis.execute<Array>(smembers);
assert(result.size() == 2);
assert(result.get<BulkString>(0).value().compare("World") == 0);
assert(result.get<BulkString>(1).value().compare("Hello") == 0);
}
catch(RedisException &e)
{
fail(e.message());
}
}
void RedisTest::testSUnion()
{
if (!_connected)
{
std::cout << "Not connected, test skipped." << std::endl;
return;
}
delKey("key1");
delKey("key2");
std::vector<std::string> values1;
values1.push_back("a");
values1.push_back("b");
values1.push_back("c");
Command sadd = Command::sadd("key1", values1);
try
{
Poco::Int64 result = _redis.execute<Poco::Int64>(sadd);
assert(result == 3);
}
catch(RedisException &e)
{
fail(e.message());
}
std::vector<std::string> values2;
values2.push_back("c");
values2.push_back("d");
values2.push_back("e");
sadd = Command::sadd("key2", values2);
try
{
Poco::Int64 result = _redis.execute<Poco::Int64>(sadd);
assert(result == 3);
}
catch(RedisException &e)
{
fail(e.message());
}
Command sunion = Command::sunion("key1", "key2");
try
{
Array result = _redis.execute<Array>(sunion);
assert(result.size() == 5);
}
catch(RedisException &e)
{
fail(e.message());
}
}
void RedisTest::testSUnionStore()
{
if (!_connected)
{
std::cout << "Not connected, test skipped." << std::endl;
return;
}
delKey("key");
delKey("key1");
delKey("key2");
std::vector<std::string> values1;
values1.push_back("a");
values1.push_back("b");
values1.push_back("c");
Command sadd = Command::sadd("key1", values1);
try
{
Poco::Int64 result = _redis.execute<Poco::Int64>(sadd);
assert(result == 3);
}
catch(RedisException &e)
{
fail(e.message());
}
std::vector<std::string> values2;
values2.push_back("c");
values2.push_back("d");
values2.push_back("e");
sadd = Command::sadd("key2", values2);
try
{
Poco::Int64 result = _redis.execute<Poco::Int64>(sadd);
assert(result == 3);
}
catch(RedisException &e)
{
fail(e.message());
}
Command sunionstore = Command::sunionstore("key", "key1", "key2");
try
{
Poco::Int64 result = _redis.execute<Poco::Int64>(sunionstore);
assert(result == 5);
}
catch(RedisException &e)
{
fail(e.message());
}
Command smembers = Command::smembers("key");
try
{
Array result = _redis.execute<Array>(smembers);
assert(result.size() == 5);
}
catch(RedisException &e)
{
@ -1674,9 +2028,15 @@ CppUnit::Test* RedisTest::suite()
CppUnit_addTest(pSuite, RedisTest, testPubSub);
CppUnit_addTest(pSuite, RedisTest, testSAdd);
CppUnit_addTest(pSuite, RedisTest, testSCard);
CppUnit_addTest(pSuite, RedisTest, testSDiff);
CppUnit_addTest(pSuite, RedisTest, testSDiffStore);
CppUnit_addTest(pSuite, RedisTest, testSet);
CppUnit_addTest(pSuite, RedisTest, testSInter);
CppUnit_addTest(pSuite, RedisTest, testSInterStore);
CppUnit_addTest(pSuite, RedisTest, testSMembers);
CppUnit_addTest(pSuite, RedisTest, testStrlen);
CppUnit_addTest(pSuite, RedisTest, testSUnion);
CppUnit_addTest(pSuite, RedisTest, testSUnionStore);
CppUnit_addTest(pSuite, RedisTest, testRename);
CppUnit_addTest(pSuite, RedisTest, testRenameNx);
CppUnit_addTest(pSuite, RedisTest, testRPop);

View File

@ -53,8 +53,14 @@ public:
void testPubSub();
void testSAdd();
void testSCard();
void testSDiff();
void testSDiffStore();
void testSet();
void testSInter();
void testSInterStore();
void testSMembers();
void testSUnion();
void testSUnionStore();
void testStrlen();
void testRename();
void testRenameNx();