Skip to content

Commit

Permalink
Add MOVE command as Redis (apache#1723)
Browse files Browse the repository at this point in the history
Since kvrocks don't support Redis DBs, so this is a dummy command.
If key does not exist, the command returns 0, otherwise it will always
returns 1.
  • Loading branch information
enjoy-binbin authored Sep 2, 2023
1 parent 357efac commit ba6731f
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
19 changes: 19 additions & 0 deletions src/commands/cmd_key.cc
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,24 @@ class CommandType : public Commander {
}
};

class CommandMove : public Commander {
public:
Status Parse(const std::vector<std::string> &args) override {
GET_OR_RET(ParseInt<int64_t>(args[2], 10));
return Status::OK();
}

Status Execute(Server *svr, Connection *conn, std::string *output) override {
int count = 0;
redis::Database redis(svr->storage, conn->GetNamespace());
rocksdb::Status s = redis.Exists({args_[1]}, &count);
if (!s.ok()) return {Status::RedisExecErr, s.ToString()};

*output = count ? redis::Integer(1) : redis::Integer(0);
return Status::OK();
}
};

class CommandObject : public Commander {
public:
Status Execute(Server *svr, Connection *conn, std::string *output) override {
Expand Down Expand Up @@ -250,6 +268,7 @@ class CommandDel : public Commander {
REDIS_REGISTER_COMMANDS(MakeCmdAttr<CommandTTL>("ttl", 2, "read-only", 1, 1, 1),
MakeCmdAttr<CommandPTTL>("pttl", 2, "read-only", 1, 1, 1),
MakeCmdAttr<CommandType>("type", 2, "read-only", 1, 1, 1),
MakeCmdAttr<CommandMove>("move", 3, "write", 1, 1, 1),
MakeCmdAttr<CommandObject>("object", 3, "read-only", 2, 2, 1),
MakeCmdAttr<CommandExists>("exists", -2, "read-only", 1, -1, 1),
MakeCmdAttr<CommandPersist>("persist", 2, "write", 1, 1, 1),
Expand Down
12 changes: 12 additions & 0 deletions tests/gocase/unit/introspection/introspection_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,18 @@ func TestIntrospection(t *testing.T) {
require.NoError(t, rdb.Set(ctx, "a", "b", 0).Err())
require.GreaterOrEqual(t, time.Since(now).Seconds(), 2.0)
})

t.Run("MOVE dummy coverage", func(t *testing.T) {
require.Error(t, rdb.Do(ctx, "MOVE", "key", "dbid").Err())

// key does not exist, return 0
require.NoError(t, rdb.Do(ctx, "DEL", "key").Err())
require.EqualValues(t, 0, rdb.Do(ctx, "MOVE", "key", "0").Val())

// key exist, always return 1
require.NoError(t, rdb.Do(ctx, "SET", "key", "value").Err())
require.EqualValues(t, 1, rdb.Do(ctx, "MOVE", "key", "0").Val())
})
}

func TestMultiServerIntrospection(t *testing.T) {
Expand Down

0 comments on commit ba6731f

Please sign in to comment.