-
Notifications
You must be signed in to change notification settings - Fork 0
/
command.go
205 lines (170 loc) · 4.79 KB
/
command.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
package proxy
import (
"bufio"
"bytes"
"errors"
"fmt"
"io"
"time"
)
var (
byteCRLF = []byte("\r\n")
byteLF = []byte("\n")
)
type Command struct {
Name string
Params [][]byte
}
func (cmd *Command) ToRESP() []byte {
var buf bytes.Buffer
length := 1 + len(cmd.Params)
buf.WriteByte('*')
buf.Write(NumberToBytes(length))
buf.Write(byteCRLF)
params := make([][]byte, length-1, length)
copy(params, cmd.Params)
params = append([][]byte{[]byte(cmd.Name)}, params...)
for _, param := range params {
buf.WriteByte('$')
buf.Write(NumberToBytes(len(param)))
buf.Write(byteCRLF)
buf.Write(param)
buf.Write(byteCRLF)
}
return buf.Bytes()
}
func (cmd *Command) WriteTo(w io.Writer) (int, error) {
return w.Write(cmd.ToRESP())
}
func (cmd *Command) String() string {
return fmt.Sprintf("%s %v", cmd.Name, cmd.Params)
}
// 2a 31 0d 0a 24 34 0d 0a 50 49 4e 47 0d 0a *1\r\n$4\r\nPING\r\n
func PING() *Command {
return &Command{Name: "PING", Params: nil}
}
func GET(key string) *Command {
return &Command{Name: "GET", Params: [][]byte{[]byte(key)}}
}
func SET(key string, val string) *Command {
return &Command{Name: "SET", Params: [][]byte{[]byte(key), []byte(val)}}
}
func GETSET(key string, val string) *Command {
return &Command{Name: "GETSET", Params: [][]byte{[]byte(key), []byte(val)}}
}
func INCR(key string) *Command {
return INCRBY(key, 1)
}
func INCRBY(key string, increment int) *Command {
return &Command{Name: "INCRBY", Params: [][]byte{NumberToBytes(increment)}}
}
func DECR(key string) *Command {
return DECRBY(key, 1)
}
func DECRBY(key string, decrement int) *Command {
return &Command{Name: "DECRBY", Params: [][]byte{NumberToBytes(decrement)}}
}
func MGET(key string, optionKey ...string) *Command {
params := make([][]byte, 0, len(optionKey))
for _, opKey := range optionKey {
params = append(params, []byte(opKey))
}
return &Command{Name: "MGET", Params: params}
}
func DEL(key string) *Command {
return &Command{Name: "DEL", Params: [][]byte{[]byte(key)}}
}
func EXISTS(key string) *Command {
return &Command{Name: "EXISTS", Params: [][]byte{[]byte(key)}}
}
func EXPIRE(key string, second int) *Command {
return &Command{Name: "EXPIRE", Params: [][]byte{NumberToBytes(second)}}
}
func LRANGE(key string, start int, stop int) *Command {
return &Command{Name: "LRANGE", Params: [][]byte{[]byte(key), NumberToBytes(start), NumberToBytes(stop)}}
}
func INFO(key string, section ...string) *Command {
var params [][]byte
if len(section) > 0 {
params = [][]byte{[]byte(section[0])}
}
return &Command{Name: "INFO", Params: params}
}
func LPUSH(key string, value string, more ...string) *Command {
return listPush("LPUSH", key, value, more...)
}
func RPUSH(key string, value string, more ...string) *Command {
return listPush("RPUSH", key, value, more...)
}
func listPush(name string, key string, value string, more ...string) *Command {
params := [][]byte{
[]byte(value),
}
for _, val := range more {
params = append(params, []byte(val))
}
return &Command{Name: name, Params: params}
}
func LPOP(key string) *Command {
return &Command{Name: "LPOP", Params: [][]byte{[]byte(key)}}
}
func RPOP(key string) *Command {
return &Command{Name: "RPOP", Params: [][]byte{[]byte(key)}}
}
func BLPOP(key string, timeout time.Duration) *Command {
return listBlockPop("BLPOP", key, timeout)
}
func BRPOP(key string, timeout time.Duration) *Command {
return listBlockPop("BRPOP", key, timeout)
}
func listBlockPop(name string, key string, timeout time.Duration) *Command {
if timeout == 0 {
return &Command{Name: name, Params: [][]byte{NumberToBytes(0)}}
}
second := int(timeout.Seconds())
if second < 1 {
return BLPOP(key, 0)
}
return &Command{Name: name, Params: [][]byte{NumberToBytes(second)}}
}
func RawComand(name string, params ...[]byte) *Command {
return &Command{Name: name, Params: params}
}
func UnpackCommand(br *bufio.Reader, req []byte) (*Command, error) {
if bytes.Equal(req, []byte("PING")) { // PING命令存在未按照规范的情况。即直接是PING\r\n
return PING(), nil
}
argLen := BytesToNumber(req[1:])
if argLen < 1 {
return nil, errors.New("command can't be empty")
}
args := make([][]byte, 0)
var arg []byte
var err error
for i := 0; i < argLen; i++ {
arg, err = parseCommandArg(br)
if err != nil {
return nil, err
}
args = append(args, arg)
}
return RawComand(string(args[0]), args[1:]...), nil
}
func parseCommandArg(br *bufio.Reader) ([]byte, error) {
buf, err := br.ReadBytes('\n')
if err != nil {
return nil, err
}
if buf[0] != '$' {
return nil, errors.New("it should be $ as identify")
}
bufLen := len(buf)
if bufLen < 4 {
return nil, errors.New("invalid args length")
}
arg := make([]byte, BytesToNumber(buf[1:bufLen-2])+2)
if _, err = io.ReadFull(br, arg); err != nil {
return nil, err
}
return arg[:len(arg)-2], nil
}