Feat:Add "copy" command

Add "copy" command,also fixes the out of range error of select when the number is negative.

Move execCopy to "keys.go" ,
Add test in "keys_test.go",
Add "copy.go" and "copy_test.go" file.
This commit is contained in:
Eriri
2022-06-23 15:20:07 +08:00
committed by finley
parent 7a9cbb7c11
commit f327000d3a
9 changed files with 395 additions and 3 deletions

View File

@@ -3,6 +3,7 @@ package database
import (
"fmt"
"github.com/hdt3213/godis/lib/utils"
"github.com/hdt3213/godis/redis/connection"
"github.com/hdt3213/godis/redis/protocol"
"github.com/hdt3213/godis/redis/protocol/asserts"
"strconv"
@@ -205,3 +206,48 @@ func TestKeys(t *testing.T) {
result = testDB.Exec(nil, utils.ToCmdLine("keys", "?:*"))
asserts.AssertMultiBulkReplySize(t, result, 2)
}
func TestCopy(t *testing.T) {
testDB.Flush()
testMDB := NewStandaloneServer()
srcKey := utils.RandString(10)
destKey := "from:" + srcKey
value := utils.RandString(10)
conn := new(connection.FakeConn)
testMDB.Exec(conn, utils.ToCmdLine("set", srcKey, value))
// normal copy
result := testMDB.Exec(conn, utils.ToCmdLine("copy", srcKey, destKey))
asserts.AssertIntReply(t, result, 1)
result = testMDB.Exec(conn, utils.ToCmdLine("get", destKey))
asserts.AssertBulkReply(t, result, value)
// copy srcKey(DB 0) to destKey(DB 1)
testMDB.Exec(conn, utils.ToCmdLine("copy", srcKey, destKey, "db", "1"))
testMDB.Exec(conn, utils.ToCmdLine("select", "1"))
result = testMDB.Exec(conn, utils.ToCmdLine("get", destKey))
asserts.AssertBulkReply(t, result, value)
// test destKey already exists
testMDB.Exec(conn, utils.ToCmdLine("select", "0"))
result = testMDB.Exec(conn, utils.ToCmdLine("copy", srcKey, destKey))
asserts.AssertIntReply(t, result, 0)
// copy srcKey(DB 0) to destKey(DB 0) with "Replace"
value = "new:" + value
testMDB.Exec(conn, utils.ToCmdLine("set", srcKey, value)) // reset srcKey
result = testMDB.Exec(conn, utils.ToCmdLine("copy", srcKey, destKey, "replace"))
asserts.AssertIntReply(t, result, 1)
result = testMDB.Exec(conn, utils.ToCmdLine("get", destKey))
asserts.AssertBulkReply(t, result, value)
// test copy expire time
testMDB.Exec(conn, utils.ToCmdLine("set", srcKey, value, "ex", "1000"))
result = testMDB.Exec(conn, utils.ToCmdLine("copy", srcKey, destKey, "replace"))
asserts.AssertIntReply(t, result, 1)
result = testMDB.Exec(conn, utils.ToCmdLine("ttl", srcKey))
asserts.AssertIntReplyGreaterThan(t, result, 0)
result = testMDB.Exec(conn, utils.ToCmdLine("ttl", destKey))
asserts.AssertIntReplyGreaterThan(t, result, 0)
}