Add lset, ltrim and rpop and use operator <<

This commit is contained in:
fbraem 2015-11-13 22:51:32 +01:00
parent 43cdb369b4
commit d1f9d36fb6
2 changed files with 86 additions and 30 deletions

View File

@ -84,6 +84,12 @@ public:
static Command lrem(const std::string& list, Int64 count, const std::string& value); static Command lrem(const std::string& list, Int64 count, const std::string& value);
/// Returns a LREM command /// Returns a LREM command
static Command lset(const std::string& list, Int64 index, const std::string& value);
/// Returns a LSET command
static Command ltrim(const std::string& list, Int64 start = 0, Int64 stop = -1);
/// Returns a LTRIM command
static Command mget(const std::vector<std::string>& keys); static Command mget(const std::vector<std::string>& keys);
/// Returns a MGET command /// Returns a MGET command
@ -96,6 +102,9 @@ public:
static Command set(const std::string& key, Int64 value, bool overwrite = true, const Poco::Timespan& expireTime = 0, bool create = true); 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 /// Returns a SET command to set the key with a value
static Command rpop(const std::string& list);
/// Returns a RPOP command
static Command rpush(const std::string& list, const std::string& value, bool create = true); static Command rpush(const std::string& list, const std::string& value, bool create = true);
/// Returns a RPUSH or RPUSHX (when create is false) command /// Returns a RPUSH or RPUSHX (when create is false) command

View File

@ -37,82 +37,102 @@ Command::~Command()
Command Command::append(const std::string& key, const std::string& value) Command Command::append(const std::string& key, const std::string& value)
{ {
Command cmd("APPEND"); Command cmd("APPEND");
cmd.add(key).add(value);
cmd << key << value;
return cmd; return cmd;
} }
Command Command::decr(const std::string& key, Int64 by) Command Command::decr(const std::string& key, Int64 by)
{ {
Command cmd(by == 0 ? "DECR" : "DECRBY"); Command cmd(by == 0 ? "DECR" : "DECRBY");
cmd.add(key);
if ( by > 0 ) cmd.add(NumberFormatter::format(by)); cmd << key;
if ( by > 0 ) cmd << NumberFormatter::format(by);
return cmd; return cmd;
} }
Command Command::del(const std::string& key) Command Command::del(const std::string& key)
{ {
Command cmd("DEL"); Command cmd("DEL");
cmd.add(key);
cmd << key;
return cmd; return cmd;
} }
Command Command::del(const std::vector<std::string>& keys) Command Command::del(const std::vector<std::string>& keys)
{ {
Command cmd("DEL"); Command cmd("DEL");
for(std::vector<std::string>::const_iterator it = keys.begin(); it != keys.end(); ++it) for(std::vector<std::string>::const_iterator it = keys.begin(); it != keys.end(); ++it)
{ {
cmd.add(*it); cmd << *it;
} }
return cmd; return cmd;
} }
Command Command::get(const std::string& key) Command Command::get(const std::string& key)
{ {
Command cmd("GET"); Command cmd("GET");
cmd.add(key);
cmd << key;
return cmd; return cmd;
} }
Command Command::incr(const std::string& key, Int64 by) Command Command::incr(const std::string& key, Int64 by)
{ {
Command cmd(by == 0 ? "INCR" : "INCRBY"); Command cmd(by == 0 ? "INCR" : "INCRBY");
cmd.add(key);
if ( by > 0 ) cmd.add(NumberFormatter::format(by)); cmd << key;
if ( by > 0 ) cmd << NumberFormatter::format(by);
return cmd; return cmd;
} }
Command Command::lindex(const std::string& list, Int64 index) Command Command::lindex(const std::string& list, Int64 index)
{ {
Command cmd("LINDEX"); Command cmd("LINDEX");
cmd.add(list).add(NumberFormatter::format(index));
cmd << list << NumberFormatter::format(index);
return cmd; return cmd;
} }
Command Command::linsert(const std::string& list, bool before, const std::string& reference, const std::string& value) Command Command::linsert(const std::string& list, bool before, const std::string& reference, const std::string& value)
{ {
Command cmd("LINSERT"); Command cmd("LINSERT");
cmd.add(list).add(before ? "BEFORE" : "AFTER").add(reference).add(value);
cmd << list << (before ? "BEFORE" : "AFTER") << reference << value;
return cmd; return cmd;
} }
Command Command::llen(const std::string& list) Command Command::llen(const std::string& list)
{ {
Command cmd("LLEN"); Command cmd("LLEN");
cmd.add(list);
cmd << list;
return cmd; return cmd;
} }
Command Command::lpop(const std::string& list) Command Command::lpop(const std::string& list)
{ {
Command cmd("LPOP"); Command cmd("LPOP");
cmd.add(list);
cmd << list;
return cmd; return cmd;
} }
Command Command::lpush(const std::string& list, const std::string& value, bool create) Command Command::lpush(const std::string& list, const std::string& value, bool create)
{ {
Command cmd(create ? "LPUSH" : "LPUSHX"); Command cmd(create ? "LPUSH" : "LPUSHX");
cmd.add(list).add(value);
cmd << list << value;
return cmd; return cmd;
} }
@ -120,11 +140,11 @@ Command Command::lpush(const std::string& list, const std::string& value, bool c
Command Command::lpush(const std::string& list, const std::vector<std::string>& values, bool create) Command Command::lpush(const std::string& list, const std::vector<std::string>& values, bool create)
{ {
Command cmd(create ? "LPUSH" : "LPUSHX"); Command cmd(create ? "LPUSH" : "LPUSHX");
cmd.add(list);
cmd << list;
for(std::vector<std::string>::const_iterator it = values.begin(); it != values.end(); ++it) for(std::vector<std::string>::const_iterator it = values.begin(); it != values.end(); ++it)
{ {
cmd.add(*it); cmd << *it;
} }
return cmd; return cmd;
@ -133,7 +153,9 @@ Command Command::lpush(const std::string& list, const std::vector<std::string>&
Command Command::lrange(const std::string& list, Int64 start, Int64 stop) Command Command::lrange(const std::string& list, Int64 start, Int64 stop)
{ {
Command cmd("LRANGE"); Command cmd("LRANGE");
cmd.add(list).add(NumberFormatter::format(start)).add(NumberFormatter::format(stop));
cmd << list << NumberFormatter::format(start) << NumberFormatter::format(stop);
return cmd; return cmd;
} }
@ -141,7 +163,25 @@ Command Command::lrem(const std::string& list, Int64 count, const std::string& v
{ {
Command cmd("LREM"); Command cmd("LREM");
cmd.add(list).add(NumberFormatter::format(count)).add(value); cmd << list << NumberFormatter::format(count) << value;
return cmd;
}
Command Command::lset(const std::string& list, Int64 index, const std::string& value)
{
Command cmd("LSET");
cmd << list << NumberFormatter::format(index) << value;
return cmd;
}
Command Command::ltrim(const std::__cxx11::string& list, Int64 start, Int64 stop)
{
Command cmd("LTRIM");
cmd << list << NumberFormatter::format(start) << NumberFormatter::format(stop);
return cmd; return cmd;
} }
@ -152,7 +192,7 @@ Command Command::mget(const std::vector<std::string>& keys)
for(std::vector<std::string>::const_iterator it = keys.begin(); it != keys.end(); ++it) for(std::vector<std::string>::const_iterator it = keys.begin(); it != keys.end(); ++it)
{ {
cmd.add(*it); cmd << *it;
} }
return cmd; return cmd;
@ -164,8 +204,7 @@ Command Command::mset(const std::map<std::string, std::string>& keyvalues, bool
for(std::map<std::string, std::string>::const_iterator it = keyvalues.begin(); it != keyvalues.end(); ++it) for(std::map<std::string, std::string>::const_iterator it = keyvalues.begin(); it != keyvalues.end(); ++it)
{ {
cmd.add(it->first); cmd << it->first << it->second;
cmd.add(it->second);
} }
return cmd; return cmd;
@ -174,14 +213,11 @@ Command Command::mset(const std::map<std::string, std::string>& keyvalues, bool
Command Command::set(const std::string& key, const std::string& value, bool overwrite, const Poco::Timespan& expireTime, bool create) Command Command::set(const std::string& key, const std::string& value, bool overwrite, const Poco::Timespan& expireTime, bool create)
{ {
Command cmd("SET"); Command cmd("SET");
cmd.add(key).add(value);
if ( ! overwrite ) cmd.add("NX");
if ( ! create ) cmd.add("XX");
if ( expireTime.totalMicroseconds() > 0 ) cmd << key << value;
{ if (! overwrite) cmd << "NX";
cmd.add("PX").add(expireTime.totalMilliseconds()); if (! create) cmd << "XX";
} if (expireTime.totalMicroseconds() > 0) cmd << "PX" << expireTime.totalMilliseconds();
return cmd; return cmd;
} }
@ -191,10 +227,21 @@ Command Command::set(const std::string& key, Int64 value, bool overwrite, const
return set(key, NumberFormatter::format(value), overwrite, expireTime, create); return set(key, NumberFormatter::format(value), overwrite, expireTime, create);
} }
Command Command::rpop(const std::string& list)
{
Command cmd("RPOP");
cmd << list;
return cmd;
}
Command Command::rpush(const std::string& list, const std::string& value, bool create) Command Command::rpush(const std::string& list, const std::string& value, bool create)
{ {
Command cmd(create ? "RPUSH" : "RPUSHX"); Command cmd(create ? "RPUSH" : "RPUSHX");
cmd.add(list).add(value);
cmd << list << value;
return cmd; return cmd;
} }
@ -202,11 +249,11 @@ Command Command::rpush(const std::string& list, const std::string& value, bool c
Command Command::rpush(const std::string& list, const std::vector<std::string>& values, bool create) Command Command::rpush(const std::string& list, const std::vector<std::string>& values, bool create)
{ {
Command cmd(create ? "RPUSH" : "RPUSHX"); Command cmd(create ? "RPUSH" : "RPUSHX");
cmd.add(list);
cmd << list;
for(std::vector<std::string>::const_iterator it = values.begin(); it != values.end(); ++it) for(std::vector<std::string>::const_iterator it = values.begin(); it != values.end(); ++it)
{ {
cmd.add(*it); cmd << *it;
} }
return cmd; return cmd;