forked from alicebob/miniredis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
redis.go
192 lines (179 loc) · 4.76 KB
/
redis.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
package miniredis
import (
"fmt"
"math"
"strings"
"sync"
"time"
"github.com/alicebob/miniredis/v2/server"
)
const (
msgWrongType = "WRONGTYPE Operation against a key holding the wrong kind of value"
msgInvalidInt = "ERR value is not an integer or out of range"
msgInvalidFloat = "ERR value is not a valid float"
msgInvalidMinMax = "ERR min or max is not a float"
msgInvalidRangeItem = "ERR min or max not valid string range item"
msgInvalidTimeout = "ERR timeout is not an integer or out of range"
msgSyntaxError = "ERR syntax error"
msgKeyNotFound = "ERR no such key"
msgOutOfRange = "ERR index out of range"
msgInvalidCursor = "ERR invalid cursor"
msgXXandNX = "ERR XX and NX options at the same time are not compatible"
msgNegTimeout = "ERR timeout is negative"
msgInvalidSETime = "ERR invalid expire time in set"
msgInvalidSETEXTime = "ERR invalid expire time in setex"
msgInvalidPSETEXTime = "ERR invalid expire time in psetex"
msgInvalidKeysNumber = "ERR Number of keys can't be greater than number of args"
msgNegativeKeysNumber = "ERR Number of keys can't be negative"
msgFScriptUsage = "ERR Unknown subcommand or wrong number of arguments for '%s'. Try SCRIPT HELP."
msgFPubsubUsage = "ERR Unknown subcommand or wrong number of arguments for '%s'. Try PUBSUB HELP."
msgSingleElementPair = "ERR INCR option supports a single increment-element pair"
msgInvalidStreamID = "ERR Invalid stream ID specified as stream command argument"
msgStreamIDTooSmall = "ERR The ID specified in XADD is equal or smaller than the target stream top item"
msgNoScriptFound = "NOSCRIPT No matching script. Please use EVAL."
)
func errWrongNumber(cmd string) string {
return fmt.Sprintf("ERR wrong number of arguments for '%s' command", strings.ToLower(cmd))
}
func errLuaParseError(err error) string {
return fmt.Sprintf("ERR Error compiling script (new function): %s", err.Error())
}
// withTx wraps the non-argument-checking part of command handling code in
// transaction logic.
func withTx(
m *Miniredis,
c *server.Peer,
cb txCmd,
) {
ctx := getCtx(c)
if inTx(ctx) {
addTxCmd(ctx, cb)
c.WriteInline("QUEUED")
return
}
m.Lock()
cb(c, ctx)
// done, wake up anyone who waits on anything.
m.signal.Broadcast()
m.Unlock()
}
// blockCmd is executed returns whether it is done
type blockCmd func(*server.Peer, *connCtx) bool
// blocking keeps trying a command until the callback returns true. Calls
// onTimeout after the timeout (or when we call this in a transaction).
func blocking(
m *Miniredis,
c *server.Peer,
timeout time.Duration,
cb blockCmd,
onTimeout func(*server.Peer),
) {
var (
ctx = getCtx(c)
dl *time.Timer
dlc <-chan time.Time
)
if inTx(ctx) {
addTxCmd(ctx, func(c *server.Peer, ctx *connCtx) {
if !cb(c, ctx) {
onTimeout(c)
}
})
c.WriteInline("QUEUED")
return
}
if timeout != 0 {
dl = time.NewTimer(timeout)
defer dl.Stop()
dlc = dl.C
}
m.Lock()
defer m.Unlock()
for {
done := cb(c, ctx)
if done {
return
}
// there is no cond.WaitTimeout(), so hence the the goroutine to wait
// for a timeout
var (
wg sync.WaitGroup
wakeup = make(chan struct{}, 1)
)
wg.Add(1)
go func() {
m.signal.Wait()
wakeup <- struct{}{}
wg.Done()
}()
select {
case <-wakeup:
case <-dlc:
onTimeout(c)
m.signal.Broadcast() // to kill the wakeup go routine
wg.Wait()
return
}
wg.Wait()
}
}
// formatFloat formats a float the way redis does (sort-of)
func formatFloat(v float64) string {
// Format with %f and strip trailing 0s. This is the most like Redis does
// it :(
// .12 is the magic number where most output is the same as Redis.
if math.IsInf(v, +1) {
return "inf"
}
if math.IsInf(v, -1) {
return "-inf"
}
sv := fmt.Sprintf("%.12f", v)
for strings.Contains(sv, ".") {
if sv[len(sv)-1] != '0' {
break
}
// Remove trailing 0s.
sv = sv[:len(sv)-1]
// Ends with a '.'.
if sv[len(sv)-1] == '.' {
sv = sv[:len(sv)-1]
break
}
}
return sv
}
// redisRange gives Go offsets for something l long with start/end in
// Redis semantics. Both start and end can be negative.
// Used for string range and list range things.
// The results can be used as: v[start:end]
// Note that GETRANGE (on a string key) never returns an empty string when end
// is a large negative number.
func redisRange(l, start, end int, stringSymantics bool) (int, int) {
if start < 0 {
start = l + start
if start < 0 {
start = 0
}
}
if start > l {
start = l
}
if end < 0 {
end = l + end
if end < 0 {
end = -1
if stringSymantics {
end = 0
}
}
}
end++ // end argument is inclusive in Redis.
if end > l {
end = l
}
if end < start {
return 0, 0
}
return start, end
}