forked from HDT3213/godis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
multi_utils.go
173 lines (159 loc) · 3.77 KB
/
multi_utils.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
package godis
import (
"github.com/hdt3213/godis/lib/utils"
"strconv"
)
func readFirstKey(args [][]byte) ([]string, []string) {
// assert len(args) > 0
key := string(args[0])
return nil, []string{key}
}
func writeFirstKey(args [][]byte) ([]string, []string) {
key := string(args[0])
return []string{key}, nil
}
func writeAllKeys(args [][]byte) ([]string, []string) {
keys := make([]string, len(args))
for i, v := range args {
keys[i] = string(v)
}
return keys, nil
}
func readAllKeys(args [][]byte) ([]string, []string) {
keys := make([]string, len(args))
for i, v := range args {
keys[i] = string(v)
}
return nil, keys
}
func noPrepare(args [][]byte) ([]string, []string) {
return nil, nil
}
func rollbackFirstKey(db *DB, args [][]byte) []CmdLine {
key := string(args[0])
return rollbackGivenKeys(db, key)
}
func rollbackGivenKeys(db *DB, keys ...string) []CmdLine {
var undoCmdLines [][][]byte
for _, key := range keys {
entity, ok := db.GetEntity(key)
if !ok {
undoCmdLines = append(undoCmdLines,
utils.ToCmdLine("DEL", key),
)
} else {
undoCmdLines = append(undoCmdLines,
utils.ToCmdLine("DEL", key), // clean existed first
EntityToCmd(key, entity).Args,
toTTLCmd(db, key).Args,
)
}
}
return undoCmdLines
}
func rollbackHashFields(db *DB, key string, fields ...string) []CmdLine {
var undoCmdLines [][][]byte
dict, errReply := db.getAsDict(key)
if errReply != nil {
return nil
}
if dict == nil {
undoCmdLines = append(undoCmdLines,
utils.ToCmdLine("DEL", key),
)
return undoCmdLines
}
for _, field := range fields {
entity, ok := dict.Get(field)
if !ok {
undoCmdLines = append(undoCmdLines,
utils.ToCmdLine("HDEL", key, field),
)
} else {
value, _ := entity.([]byte)
undoCmdLines = append(undoCmdLines,
utils.ToCmdLine("HSET", key, field, string(value)),
)
}
}
return undoCmdLines
}
func prepareSetCalculate(args [][]byte) ([]string, []string) {
keys := make([]string, len(args))
for i, arg := range args {
keys[i] = string(arg)
}
return nil, keys
}
func prepareSetCalculateStore(args [][]byte) ([]string, []string) {
dest := string(args[0])
keys := make([]string, len(args)-1)
keyArgs := args[1:]
for i, arg := range keyArgs {
keys[i] = string(arg)
}
return []string{dest}, keys
}
func rollbackSetMembers(db *DB, key string, members ...string) []CmdLine {
var undoCmdLines [][][]byte
set, errReply := db.getAsSet(key)
if errReply != nil {
return nil
}
if set == nil {
undoCmdLines = append(undoCmdLines,
utils.ToCmdLine("DEL", key),
)
return undoCmdLines
}
for _, member := range members {
ok := set.Has(member)
if !ok {
undoCmdLines = append(undoCmdLines,
utils.ToCmdLine("SREM", key, member),
)
} else {
undoCmdLines = append(undoCmdLines,
utils.ToCmdLine("SADD", key, member),
)
}
}
return undoCmdLines
}
// undoSetChange rollbacks SADD and SREM command
func undoSetChange(db *DB, args [][]byte) []CmdLine {
key := string(args[0])
memberArgs := args[1:]
members := make([]string, len(memberArgs))
for i, mem := range memberArgs {
members[i] = string(mem)
}
return rollbackSetMembers(db, key, members...)
}
func rollbackZSetFields(db *DB, key string, fields ...string) []CmdLine {
var undoCmdLines [][][]byte
zset, errReply := db.getAsSortedSet(key)
if errReply != nil {
return nil
}
if zset == nil {
undoCmdLines = append(undoCmdLines,
utils.ToCmdLine("DEL", key),
)
return undoCmdLines
}
for _, field := range fields {
elem, ok := zset.Get(field)
if !ok {
undoCmdLines = append(undoCmdLines,
utils.ToCmdLine("ZREM", key, field),
)
} else {
score := strconv.FormatFloat(elem.Score, 'f', -1, 64)
undoCmdLines = append(undoCmdLines,
utils.ToCmdLine("ZADD", key, score, field),
)
}
}
return undoCmdLines
}