mirror of
https://github.com/HDT3213/godis.git
synced 2025-10-05 08:46:56 +08:00
using TCC for MSetNX in cluster
This commit is contained in:
@@ -36,7 +36,7 @@ func undoDel(db *DB, args [][]byte) []CmdLine {
|
||||
return rollbackGivenKeys(db, keys...)
|
||||
}
|
||||
|
||||
// execExists checks if a is existed in db
|
||||
// execExists checks if given key is existed in db
|
||||
func execExists(db *DB, args [][]byte) redis.Reply {
|
||||
result := int64(0)
|
||||
for _, arg := range args {
|
||||
@@ -49,6 +49,24 @@ func execExists(db *DB, args [][]byte) redis.Reply {
|
||||
return reply.MakeIntReply(result)
|
||||
}
|
||||
|
||||
// execExistIn returns existing key in given keys
|
||||
// example: ExistIn key1 key2 key3..., returns [key1, key2]
|
||||
// custom command for MSetNX tcc transaction
|
||||
func execExistIn(db *DB, args [][]byte) redis.Reply {
|
||||
var result [][]byte
|
||||
for _, arg := range args {
|
||||
key := string(arg)
|
||||
_, exists := db.GetEntity(key)
|
||||
if exists {
|
||||
result = append(result, []byte(key))
|
||||
}
|
||||
}
|
||||
if len(result) == 0 {
|
||||
return reply.MakeEmptyMultiBulkReply()
|
||||
}
|
||||
return reply.MakeMultiBulkReply(result)
|
||||
}
|
||||
|
||||
// execFlushDB removes all data in current db
|
||||
func execFlushDB(db *DB, args [][]byte) redis.Reply {
|
||||
db.Flush()
|
||||
@@ -318,6 +336,7 @@ func init() {
|
||||
RegisterCommand("PTTL", execPTTL, readFirstKey, nil, 2)
|
||||
RegisterCommand("Persist", execPersist, writeFirstKey, undoExpire, 2)
|
||||
RegisterCommand("Exists", execExists, readAllKeys, nil, -2)
|
||||
RegisterCommand("ExistIn", execExistIn, readAllKeys, nil, -1)
|
||||
RegisterCommand("Type", execType, readFirstKey, nil, 2)
|
||||
RegisterCommand("Rename", execRename, prepareRename, undoRename, 3)
|
||||
RegisterCommand("RenameNx", execRenameNx, prepareRename, undoRename, 3)
|
||||
|
@@ -22,6 +22,19 @@ func TestExists(t *testing.T) {
|
||||
asserts.AssertIntReply(t, result, 0)
|
||||
}
|
||||
|
||||
func TestExistIn(t *testing.T) {
|
||||
testDB.Flush()
|
||||
key := utils.RandString(10)
|
||||
value := utils.RandString(10)
|
||||
key2 := utils.RandString(10)
|
||||
testDB.Exec(nil, utils.ToCmdLine("set", key, value))
|
||||
result := testDB.Exec(nil, utils.ToCmdLine("ExistIn", key, key2))
|
||||
asserts.AssertMultiBulkReply(t, result, []string{key})
|
||||
key3 := utils.RandString(10)
|
||||
result = testDB.Exec(nil, utils.ToCmdLine("ExistIn", key2, key3))
|
||||
asserts.AssertMultiBulkReplySize(t, result, 0)
|
||||
}
|
||||
|
||||
func TestType(t *testing.T) {
|
||||
testDB.Flush()
|
||||
key := utils.RandString(10)
|
||||
|
Reference in New Issue
Block a user