forked from redis/rueidis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cmds.go
263 lines (230 loc) · 6.97 KB
/
cmds.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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
package cmds
import "strings"
const (
optInTag = uint16(1 << 15)
blockTag = uint16(1 << 14)
readonly = uint16(1 << 13)
noRetTag = uint16(1<<12) | readonly // make noRetTag can also be retried
mtGetTag = uint16(1<<11) | readonly // make mtGetTag can also be retried
// InitSlot indicates that the command be sent to any redis node in cluster
InitSlot = uint16(1 << 14)
// NoSlot indicates that the command has no key slot specified
NoSlot = uint16(1 << 15)
)
var (
// OptInCmd is predefined CLIENT CACHING YES
OptInCmd = Completed{
cs: &CommandSlice{s: []string{"CLIENT", "CACHING", "YES"}},
cf: optInTag,
}
// MultiCmd is predefined MULTI
MultiCmd = Completed{
cs: &CommandSlice{s: []string{"MULTI"}},
}
// ExecCmd is predefined EXEC
ExecCmd = Completed{
cs: &CommandSlice{s: []string{"EXEC"}},
}
// RoleCmd is predefined ROLE
RoleCmd = Completed{
cs: &CommandSlice{s: []string{"ROLE"}},
}
// QuitCmd is predefined QUIT
QuitCmd = Completed{
cs: &CommandSlice{s: []string{"QUIT"}},
}
// UnsubscribeCmd is predefined UNSUBSCRIBE
UnsubscribeCmd = Completed{
cs: &CommandSlice{s: []string{"UNSUBSCRIBE"}},
cf: noRetTag,
}
// PUnsubscribeCmd is predefined PUNSUBSCRIBE
PUnsubscribeCmd = Completed{
cs: &CommandSlice{s: []string{"PUNSUBSCRIBE"}},
cf: noRetTag,
}
// SUnsubscribeCmd is predefined SUNSUBSCRIBE
SUnsubscribeCmd = Completed{
cs: &CommandSlice{s: []string{"SUNSUBSCRIBE"}},
cf: noRetTag,
}
// PingCmd is predefined PING
PingCmd = Completed{
cs: &CommandSlice{s: []string{"PING"}},
}
// SlotCmd is predefined CLUSTER SLOTS
SlotCmd = Completed{
cs: &CommandSlice{s: []string{"CLUSTER", "SLOTS"}},
}
// AskingCmd is predefined CLUSTER ASKING
AskingCmd = Completed{
cs: &CommandSlice{s: []string{"ASKING"}},
}
// SentinelSubscribe is predefined SUBSCRIBE ASKING
SentinelSubscribe = Completed{
cs: &CommandSlice{s: []string{"SUBSCRIBE", "+sentinel", "+switch-master", "+reboot"}},
cf: noRetTag,
}
)
// Completed represents a completed Redis command, should be created by the Build() of command builder.
type Completed struct {
cs *CommandSlice
cf uint16
ks uint16
}
// IsEmpty checks if it is an empty command.
func (c *Completed) IsEmpty() bool {
return c.cs == nil || len(c.cs.s) == 0
}
// IsOptIn checks if it is client side caching opt-int command.
func (c *Completed) IsOptIn() bool {
return c.cf&optInTag == optInTag
}
// IsBlock checks if it is blocking command which needs to be process by dedicated connection.
func (c *Completed) IsBlock() bool {
return c.cf&blockTag == blockTag
}
// ToBlock marks the command with blockTag
func (c *Completed) ToBlock() {
c.cf |= blockTag
}
// NoReply checks if it is one of the SUBSCRIBE, PSUBSCRIBE, UNSUBSCRIBE or PUNSUBSCRIBE commands.
func (c *Completed) NoReply() bool {
return c.cf&noRetTag == noRetTag
}
// IsReadOnly checks if it is readonly command and can be retried when network error.
func (c *Completed) IsReadOnly() bool {
return c.cf&readonly == readonly
}
// IsWrite checks if it is not readonly command.
func (c *Completed) IsWrite() bool {
return !c.IsReadOnly()
}
// Commands returns the commands as []string.
// Note that the returned []string should not be modified
// and should not be read after passing into the Client interface, because it will be recycled.
func (c *Completed) Commands() []string {
return c.cs.s
}
// CommandSlice it the command container which will be recycled by the sync.Pool.
func (c *Completed) CommandSlice() *CommandSlice {
return c.cs
}
// Slot returns the command key slot
func (c *Completed) Slot() uint16 {
return c.ks
}
// Cacheable represents a completed Redis command which supports server-assisted client side caching,
// and it should be created by the Cache() of command builder.
type Cacheable Completed
// Slot returns the command key slot
func (c *Cacheable) Slot() uint16 {
return c.ks
}
// Commands returns the commands as []string.
// Note that the returned []string should not be modified
// and should not be read after passing into the Client interface, because it will be recycled.
func (c *Cacheable) Commands() []string {
return c.cs.s
}
// CommandSlice returns the command container which will be recycled by the sync.Pool.
func (c *Cacheable) CommandSlice() *CommandSlice {
return c.cs
}
// IsMGet returns if the command is MGET
func (c *Cacheable) IsMGet() bool {
return c.cf == mtGetTag
}
// MGetCacheCmd returns the cache command of the MGET singular command
func (c *Cacheable) MGetCacheCmd() string {
if c.cs.s[0][0] == 'J' {
return "JSON.GET" + c.cs.s[len(c.cs.s)-1]
}
return "GET"
}
// MGetCacheKey returns the cache key of the MGET singular command
func (c *Cacheable) MGetCacheKey(i int) string {
return c.cs.s[i+1]
}
// CacheKey returns the cache key used by the server-assisted client side caching
func (c *Cacheable) CacheKey() (key, command string) {
if len(c.cs.s) == 2 {
return c.cs.s[1], c.cs.s[0]
}
length := 0
for i, v := range c.cs.s {
if i == 1 {
continue
}
length += len(v)
}
sb := strings.Builder{}
sb.Grow(length)
for i, v := range c.cs.s {
if i == 1 {
key = v
} else {
sb.WriteString(v)
}
}
return key, sb.String()
}
// NewCompleted creates an arbitrary Completed command.
func NewCompleted(ss []string) Completed {
return Completed{cs: &CommandSlice{s: ss}}
}
// NewBlockingCompleted creates an arbitrary blocking Completed command.
func NewBlockingCompleted(ss []string) Completed {
return Completed{cs: &CommandSlice{s: ss}, cf: blockTag}
}
// NewReadOnlyCompleted creates an arbitrary readonly Completed command.
func NewReadOnlyCompleted(ss []string) Completed {
return Completed{cs: &CommandSlice{s: ss}, cf: readonly}
}
// NewMGetCompleted creates an arbitrary readonly Completed command.
func NewMGetCompleted(ss []string) Completed {
return Completed{cs: &CommandSlice{s: ss}, cf: mtGetTag}
}
// MGets groups keys by their slot and returns multi MGET commands
func MGets(keys []string) map[uint16]Completed {
return slotMGets("MGET", keys)
}
// JsonMGets groups keys by their slot and returns multi JSON.MGET commands
func JsonMGets(keys []string, path string) map[uint16]Completed {
ret := slotMGets("JSON.MGET", keys)
for _, jsonmget := range ret {
jsonmget.cs.s = append(jsonmget.cs.s, path)
}
return ret
}
func slotMGets(cmd string, keys []string) map[uint16]Completed {
ret := make(map[uint16]Completed, 16)
for _, key := range keys {
var cs *CommandSlice
ks := slot(key)
if cp, ok := ret[ks]; ok {
cs = cp.cs
} else {
cs = get()
cs.s = append(cs.s, cmd)
ret[ks] = Completed{cs: cs, cf: mtGetTag, ks: ks}
}
cs.s = append(cs.s, key)
}
return ret
}
// NewMultiCompleted creates multiple arbitrary Completed commands.
func NewMultiCompleted(cs [][]string) []Completed {
ret := make([]Completed, len(cs))
for i, c := range cs {
ret[i] = NewCompleted(c)
}
return ret
}
func check(prev, new uint16) uint16 {
if prev == InitSlot || prev == new {
return new
}
panic(multiKeySlotErr)
}
const multiKeySlotErr = "multi key command with different key slots are not allowed"