Skip to content

Commit

Permalink
Support keys for select by type (OpenAtomFoundation#179)
Browse files Browse the repository at this point in the history
* Support keys for select by type

* limit the number of parameter

* rename parameters

* fix code

* remove redundant code
  • Loading branch information
Leviathan1995 authored and zhangtianshuo committed Oct 13, 2017
1 parent 810d830 commit e77c9ab
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 3 deletions.
6 changes: 5 additions & 1 deletion include/pika_kv.h
Original file line number Diff line number Diff line change
Expand Up @@ -126,11 +126,15 @@ class MgetCmd : public Cmd {

class KeysCmd : public Cmd {
public:
KeysCmd() {}
KeysCmd() : type_("all") {}
virtual void Do();
private:
std::string pattern_;
std::string type_;
virtual void DoInitial(PikaCmdArgsType &argv, const CmdInfo* const ptr_info);
virtual void Clear() {
type_ = "all";
}
};

class SetnxCmd : public Cmd {
Expand Down
2 changes: 2 additions & 0 deletions pikatests.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
#!/bin/bash
rm -rf ./log
rm -rf .db
cp output/bin/pika src/redis-server
cp output/conf/pika.conf tests/assets/default.conf

Expand Down
2 changes: 1 addition & 1 deletion src/pika_command.cc
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ void InitCmdInfoTable() {
CmdInfo* mgetptr = new CmdInfo(kCmdNameMget, -2, kCmdFlagsRead | kCmdFlagsKv);
cmd_infos.insert(std::pair<std::string, CmdInfo*>(kCmdNameMget, mgetptr));
////Keys
CmdInfo* keysptr = new CmdInfo(kCmdNameKeys, 2, kCmdFlagsRead | kCmdFlagsKv);
CmdInfo* keysptr = new CmdInfo(kCmdNameKeys, -2, kCmdFlagsRead | kCmdFlagsKv);
cmd_infos.insert(std::pair<std::string, CmdInfo*>(kCmdNameKeys, keysptr));
////Setnx
CmdInfo* setnxptr = new CmdInfo(kCmdNameSetnx, 3, kCmdFlagsWrite | kCmdFlagsKv);
Expand Down
12 changes: 11 additions & 1 deletion src/pika_kv.cc
Original file line number Diff line number Diff line change
Expand Up @@ -340,12 +340,22 @@ void KeysCmd::DoInitial(PikaCmdArgsType &argv, const CmdInfo* const ptr_info) {
return;
}
pattern_ = argv[1];
if (argv.size() == 3) {
std::string opt = slash::StringToLower(argv[2]);
if (opt == "string" || opt == "zset" || opt == "set" || opt == "list" || opt == "hash") {
type_ = opt;
} else {
res_.SetRes(CmdRes::kSyntaxErr);
}
} else if (argv.size() > 3) {
res_.SetRes(CmdRes::kSyntaxErr);
}
return;
}

void KeysCmd::Do() {
std::vector<std::string> keys;
nemo::Status s = g_pika_server->db()->Keys(pattern_, keys);
nemo::Status s = g_pika_server->db()->Keys(pattern_, keys, type_);
res_.AppendArrayLen(keys.size());
for (std::vector<std::string>::iterator iter = keys.begin(); iter != keys.end(); iter++) {
res_.AppendStringLen(iter->size());
Expand Down
54 changes: 54 additions & 0 deletions tests/unit/keys.tcl
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
start_server {tags {"keys"}} {
test {KEYS with pattern} {
foreach key {key_x key_y key_z foo_a foo_b foo_c} {
r set $key hello
}
assert_equal {foo_a foo_b foo_c} [r keys foo*]
assert_equal {foo_a foo_b foo_c} [r keys f*]
assert_equal {foo_a foo_b foo_c} [r keys f*o*]
}

test {KEYS to get all keys} {
lsort [r keys *]
} {foo_a foo_b foo_c key_x key_y key_z}

test {KEYS select by type} {
foreach key {key_x key_y key_z foo_a foo_b foo_c} {
r del $key
}
r set kv_1 value
r set kv_2 value
r hset hash_1 hash_field 1
r hset hash_2 hash_field 1
r lpush list_1 value
r lpush list_2 value
r zadd zset_1 1 "a"
r zadd zset_2 1 "a"
r sadd set_1 "a"
r sadd set_2 "a"
assert_equal {kv_1 kv_2} [r keys * string]
assert_equal {hash_1 hash_2} [r keys * hash]
assert_equal {list_1 list_2} [r keys * list]
assert_equal {zset_1 zset_2} [r keys * zset]
assert_equal {set_1 set_2} [r keys * set]
assert_equal {kv_1 kv_2 hash_1 hash_2 zset_1 zset_2 set_1 set_2 list_1 list_2} [r keys *]
assert_equal {kv_1 kv_2} [r keys * STRING]
assert_equal {hash_1 hash_2} [r keys * HASH]
assert_equal {list_1 list_2} [r keys * LIST]
assert_equal {zset_1 zset_2} [r keys * ZSET]
assert_equal {set_1 set_2} [r keys * SET]
}

test {KEYS syntax error} {
catch {r keys * a} e1
catch {r keys * strings} e2
catch {r keys * c d} e3
catch {r keys} e4
catch {r keys * set zset} e5
assert_equal {ERR syntax error} [set e1]
assert_equal {ERR syntax error} [set e2]
assert_equal {ERR syntax error} [set e3]
assert_equal {ERR wrong number of arguments for 'keys' command} [set e4]
assert_equal {ERR syntax error} [set e5]
}
}

0 comments on commit e77c9ab

Please sign in to comment.