提交 3d889e0f 编写于 作者: S songzhao

add hincrbyfloat

上级 200e5606
......@@ -72,6 +72,12 @@ public:
virtual void Do(std::list<std::string> &argvs, std::string &ret);
};
class HIncrbyfloatCmd : public Cmd {
public:
HIncrbyfloatCmd(int a) : Cmd(a) {};
virtual void Do(std::list<std::string> &argvs, std::string &ret);
};
class HKeysCmd : public Cmd {
public:
HKeysCmd(int a) : Cmd(a) {};
......
......@@ -39,6 +39,7 @@ int ll2string(char *s, size_t len, long long value);
int string2ll(const char *s, size_t slen, long long *value);
int string2l(const char *s, size_t slen, long *value);
int d2string(char *buf, size_t len, double value);
int string2d(const char *buf, size_t len, double *value);
sds getAbsolutePath(char *filename);
int pathIsBaseName(char *path);
......
......@@ -141,6 +141,8 @@ int main(int argc, char **argv)
g_pikaCmd.insert(std::pair<std::string, Cmd *>("hgetall", hgetallptr));
HIncrbyCmd *hincrbyptr = new HIncrbyCmd(4);
g_pikaCmd.insert(std::pair<std::string, Cmd *>("hincrby", hincrbyptr));
HIncrbyfloatCmd *hincrbyfloatptr = new HIncrbyfloatCmd(4);
g_pikaCmd.insert(std::pair<std::string, Cmd *>("hincrbyfloat", hincrbyfloatptr));
HKeysCmd *hkeysptr = new HKeysCmd(2);
g_pikaCmd.insert(std::pair<std::string, Cmd *>("hkeys", hkeysptr));
HLenCmd *hlenptr = new HLenCmd(2);
......
......@@ -136,9 +136,8 @@ void HIncrbyCmd::Do(std::list<std::string> &argv, std::string &ret) {
}
nemo::Status s = g_pikaServer->GetHandle()->HIncrby(key, field, by, new_val);
if (s.ok() || s.IsNotFound()) {
char buf[32];
ret = ":";
ret.append(new_val);
ret.append(new_val.data(), new_val.size());
ret.append("\r\n");
} else if (s.IsCorruption() && s.ToString() == "Corruption: value is not integer"){
ret = "-ERR hash value is not an integer\r\n";
......@@ -149,6 +148,42 @@ void HIncrbyCmd::Do(std::list<std::string> &argv, std::string &ret) {
}
}
void HIncrbyfloatCmd::Do(std::list<std::string> &argv, std::string &ret) {
if ((arity > 0 && (int)argv.size() != arity) || (arity < 0 && (int)argv.size() < -arity)) {
ret = "-ERR wrong number of arguments for ";
ret.append(argv.front());
ret.append(" command\r\n");
return;
}
argv.pop_front();
std::string key = argv.front();
argv.pop_front();
std::string field = argv.front();
argv.pop_front();
std::string str_by = argv.front();
argv.pop_front();
double by;
std::string new_val;
if (!string2d(str_by.data(), str_by.size(), &by)) {
ret = "-ERR value is not an float\r\n";
return;
}
nemo::Status s = g_pikaServer->GetHandle()->HIncrbyfloat(key, field, by, new_val);
if (s.ok() || s.IsNotFound()) {
char buf[32];
snprintf(buf, sizeof(buf), "$%lu\r\n", new_val.size());
ret.append(buf);
ret.append(new_val.data(), new_val.size());
ret.append("\r\n");
} else if (s.IsCorruption() && s.ToString() == "Corruption: value is not float"){
ret = "-ERR hash value is not an float\r\n";
} else {
ret.append("-ERR ");
ret.append(s.ToString().c_str());
ret.append("\r\n");
}
}
void HKeysCmd::Do(std::list<std::string> &argv, std::string &ret) {
if ((arity > 0 && (int)argv.size() != arity) || (arity < 0 && (int)argv.size() < -arity)) {
ret = "-ERR wrong number of arguments for ";
......
......@@ -424,6 +424,16 @@ int d2string(char *buf, size_t len, double value) {
return len;
}
int string2d(const char *s, size_t slen, double *dval) {
char *pEnd;
double d = strtod(s, &pEnd);
if (pEnd != s + slen)
return 0;
if (dval != NULL) *dval = d;
return 1;
}
/* Generate the Redis "Run ID", a SHA1-sized random number that identifies a
* given execution of Redis, so that if you are talking with an instance
* having run_id == A, and you reconnect and it has run_id == B, you can be
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册