using TCC for MSetNX in cluster

This commit is contained in:
hdt3213
2022-04-08 20:28:13 +08:00
parent e2a1bbbe7d
commit c97f3aae6e
9 changed files with 150 additions and 32 deletions

View File

@@ -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)

View File

@@ -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)