forked from redis/rueidis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
resp.go
254 lines (232 loc) · 5.3 KB
/
resp.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
package rueidis
import (
"bufio"
"errors"
"io"
"strconv"
"strings"
)
var errChunked = errors.New("unbounded redis message")
var errOldNull = errors.New("RESP2 null")
type reader func(i *bufio.Reader) (RedisMessage, error)
var readers = [256]reader{}
func init() {
readers['$'] = readBlobString
readers['+'] = readSimpleString
readers['-'] = readSimpleString
readers[':'] = readInteger
readers['_'] = readNull
readers[','] = readSimpleString
readers['#'] = readBoolean
readers['!'] = readBlobString
readers['='] = readBlobString
readers['('] = readSimpleString
readers['*'] = readArray
readers['%'] = readMap
readers['~'] = readArray
readers['|'] = readMap
readers['>'] = readArray
readers['.'] = readNull
}
func readSimpleString(i *bufio.Reader) (m RedisMessage, err error) {
m.string, err = readS(i)
return
}
func readBlobString(i *bufio.Reader) (m RedisMessage, err error) {
m.string, err = readB(i)
if err == errChunked {
sb := strings.Builder{}
for {
if _, err = i.Discard(1); err != nil { // discard the ';'
return RedisMessage{}, err
}
length, err := readI(i)
if err != nil {
return RedisMessage{}, err
}
if length == 0 {
return RedisMessage{string: sb.String()}, nil
}
sb.Grow(int(length))
if _, err = io.CopyN(&sb, i, length); err != nil {
return RedisMessage{}, err
}
if _, err = i.Discard(2); err != nil {
return RedisMessage{}, err
}
}
}
return
}
func readInteger(i *bufio.Reader) (m RedisMessage, err error) {
m.integer, err = readI(i)
return
}
func readBoolean(i *bufio.Reader) (m RedisMessage, err error) {
b, err := i.ReadByte()
if err != nil {
return RedisMessage{}, err
}
if b == 't' {
m.integer = 1
}
_, err = i.Discard(2)
return
}
func readNull(i *bufio.Reader) (m RedisMessage, err error) {
_, err = i.Discard(2)
return
}
func readArray(i *bufio.Reader) (m RedisMessage, err error) {
length, err := readI(i)
if err == nil {
if length == -1 {
return m, errOldNull
}
m.values, err = readA(i, length)
} else if err == errChunked {
m.values, err = readE(i)
}
return m, err
}
func readMap(i *bufio.Reader) (m RedisMessage, err error) {
length, err := readI(i)
if err == nil {
m.values, err = readA(i, length*2)
} else if err == errChunked {
m.values, err = readE(i)
}
return m, err
}
func readS(i *bufio.Reader) (string, error) {
bs, err := i.ReadBytes('\n')
if err != nil {
return "", err
}
if trim := len(bs) - 2; trim < 0 {
return "", errors.New(unexpectedNoCRLF)
} else {
bs = bs[:trim]
}
return BinaryString(bs), nil
}
func readI(i *bufio.Reader) (int64, error) {
var v int64
var neg bool
for {
c, err := i.ReadByte()
if err != nil {
return 0, err
}
switch {
case c >= '0' && c <= '9':
v = v*10 + int64(c-'0')
case c == '\r':
_, err = i.Discard(1)
if neg {
return v * -1, err
}
return v, err
case c == '-':
neg = true
case c == '?':
if _, err = i.Discard(2); err == nil {
err = errChunked
}
return 0, err
default:
return 0, errors.New(unexpectedNumByte + strconv.Itoa(int(c)))
}
}
}
func readB(i *bufio.Reader) (string, error) {
length, err := readI(i)
if err != nil {
return "", err
}
if length == -1 {
return "", errOldNull
}
bs := make([]byte, length)
if _, err = io.ReadFull(i, bs); err != nil {
return "", err
}
if _, err = i.Discard(2); err != nil {
return "", err
}
return BinaryString(bs), nil
}
func readE(i *bufio.Reader) ([]RedisMessage, error) {
v := make([]RedisMessage, 0)
for {
n, err := readNextMessage(i)
if err != nil {
return nil, err
}
if n.typ == '.' {
return v, err
}
v = append(v, n)
}
}
func readA(i *bufio.Reader, length int64) (v []RedisMessage, err error) {
v = make([]RedisMessage, length)
for n := int64(0); n < length; n++ {
if v[n], err = readNextMessage(i); err != nil {
return nil, err
}
}
return v, nil
}
func writeB(o *bufio.Writer, id byte, str string) (err error) {
_ = writeS(o, id, strconv.Itoa(len(str)))
_, _ = o.WriteString(str)
_, err = o.WriteString("\r\n")
return err
}
func writeS(o *bufio.Writer, id byte, str string) (err error) {
_ = o.WriteByte(id)
_, _ = o.WriteString(str)
_, err = o.WriteString("\r\n")
return err
}
func readNextMessage(i *bufio.Reader) (m RedisMessage, err error) {
var attrs *RedisMessage
var typ byte
for {
if typ, err = i.ReadByte(); err != nil {
return RedisMessage{}, err
}
fn := readers[typ]
if fn == nil {
return RedisMessage{}, errors.New(unknownMessageType + strconv.Itoa(int(typ)))
}
if m, err = fn(i); err != nil {
if err == errOldNull {
return RedisMessage{typ: '_'}, nil
}
return RedisMessage{}, err
}
m.typ = typ
if m.typ == '|' { // handle the attributes
a := m // clone the original m first, and then take address of the clone
attrs = &a // to avoid go compiler allocating the m on heap which causing worse performance.
m = RedisMessage{}
continue
}
m.attrs = attrs
return m, nil
}
}
func writeCmd(o *bufio.Writer, cmd []string) (err error) {
err = writeS(o, '*', strconv.Itoa(len(cmd)))
for _, m := range cmd {
err = writeB(o, '$', m)
}
return err
}
const (
unexpectedNoCRLF = "received unexpected simple string message ending without CRLF"
unexpectedNumByte = "received unexpected number byte: "
unknownMessageType = "received unknown message type: "
)