-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathresp.go
230 lines (188 loc) · 4.37 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
package main
import (
"bufio"
"fmt"
"io"
"strconv"
)
const (
CHAR_STRING = '+'
CHAR_ERROR = '-'
CHAR_INTEGER = ':'
CHAR_BULK = '$'
CHAR_ARRAY = '*'
)
type COMMAND string
const (
COMMAND_PING COMMAND = "PING"
COMMAND_GET COMMAND = "GET"
COMMAND_SET COMMAND = "SET"
COMMAND_HGET COMMAND = "HGET"
COMMAND_HSET COMMAND = "HSET"
COMMAND_HGETALL COMMAND = "HGETALL"
)
type VALUE_TYPE string
const (
TYPE_ARRAY VALUE_TYPE = "ARRAY"
TYPE_STRING VALUE_TYPE = "STRING"
TYPE_BULK VALUE_TYPE = "BULK"
TYPE_ERROR VALUE_TYPE = "ERROR"
TYPE_NULL VALUE_TYPE = "NULL"
)
// Value is used to represent a redis command
type Value struct {
typ VALUE_TYPE
str string
num int
bulk string
array []Value
}
// Resp is used for reading a stream of RESP encoded redis commands
type Resp struct {
reader *bufio.Reader
}
func NewResp(rd io.Reader) *Resp {
return &Resp{reader: bufio.NewReader(rd)}
}
// reads a full line from the reader. Will stop when we find a \r at the end of the line
func (r *Resp) readLine() (line []byte, n int, err error) {
for {
b, err := r.reader.ReadByte()
if err != nil {
return nil, 0, err
}
n += 1
line = append(line, b)
if len(line) >= 2 && line[len(line)-2] == '\r' {
break
}
}
return line[:len(line)-2], n, nil
}
// readInteger reads the integer from the input stream
func (r *Resp) readInteger() (x int, n int, err error) {
line, n, err := r.readLine()
if err != nil {
return 0, 0, err
}
i64, err := strconv.ParseInt(string(line), 10, 64)
if err != nil {
return 0, n, err
}
return int(i64), n, nil
}
// readArray method reads the input stream and parses into an array
func (r *Resp) readArray() (Value, error) {
val := Value{}
val.typ = TYPE_ARRAY
arrSize, _, err := r.readInteger()
if err != nil {
fmt.Printf("Error in reading array length. err: %v\n", err)
}
val.array = make([]Value, 0)
for range arrSize {
value, err := r.Read()
if err != nil {
fmt.Printf("Error in reading value. err: %v\n", err)
}
val.array = append(val.array, value)
}
return val, nil
}
func (r *Resp) readBulk() (Value, error) {
val := Value{}
val.typ = TYPE_BULK
bulkSize, _, err := r.readInteger()
if err != nil {
fmt.Printf("Error in reading bulk size. err: %v\n", err)
}
bulkBuf := make([]byte, bulkSize)
r.reader.Read(bulkBuf)
val.bulk = string(bulkBuf)
// Read the ending \r\n
r.readLine()
return val, nil
}
func (r *Resp) Read() (Value, error) {
_type, err := r.reader.ReadByte()
if err != nil {
return Value{}, err
}
switch _type {
case CHAR_ARRAY:
return r.readArray()
case CHAR_BULK:
return r.readBulk()
default:
fmt.Printf("Unknown type: %v\n", string(_type))
return Value{}, nil
}
}
// ======================================== Serialization =========================================
type Writer struct {
writer io.Writer
}
func NewWriter(w io.Writer) *Writer {
return &Writer{writer: w}
}
func (w *Writer) Write(v Value) error {
bytes := v.Marshal()
_, err := w.writer.Write(bytes)
if err != nil {
fmt.Printf("Error in writing value: %v. Err: %v", v, err)
return err
}
return nil
}
func (v Value) Marshal() []byte {
switch v.typ {
case "ARRAY":
return v.marshalArray()
case "BULK":
return v.marshalBulkString()
case "STRING":
return v.marshalString()
case "NULL":
return v.marshalNull()
case "ERROR":
return v.marshalError()
default:
return []byte{}
}
}
func (v Value) marshalString() []byte {
var bytes []byte
bytes = append(bytes, CHAR_STRING)
bytes = append(bytes, v.str...)
bytes = append(bytes, '\r', '\n')
return bytes
}
func (v Value) marshalBulkString() []byte {
var bytes []byte
bytes = append(bytes, CHAR_BULK)
bytes = append(bytes, strconv.Itoa(len(v.bulk))...) // append the length of the bulk string
bytes = append(bytes, '\r', '\n')
bytes = append(bytes, v.bulk...)
bytes = append(bytes, '\r', '\n')
return bytes
}
func (v Value) marshalArray() []byte {
var bytes []byte
bytes = append(bytes, CHAR_ARRAY)
bytes = append(bytes, strconv.Itoa(len(v.array))...)
bytes = append(bytes, '\r', '\n')
for _, val := range v.array {
bytes = append(bytes, val.Marshal()...)
}
return bytes
}
func (v Value) marshalError() []byte {
var bytes []byte
bytes = append(bytes, CHAR_ERROR)
bytes = append(bytes, v.str...)
bytes = append(bytes, '\r', '\n')
return bytes
}
func (v Value) marshalNull() []byte {
return []byte("$-1\r\n")
}