-
Notifications
You must be signed in to change notification settings - Fork 556
/
multi_helper.go
47 lines (43 loc) · 1.28 KB
/
multi_helper.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
package cluster
import (
"bytes"
"encoding/base64"
"github.com/hdt3213/godis/redis/parser"
"github.com/hdt3213/godis/redis/protocol"
)
func encodeCmdLine(cmdLines []CmdLine) [][]byte {
var result [][]byte
for _, line := range cmdLines {
raw := protocol.MakeMultiBulkReply(line).ToBytes()
encoded := make([]byte, base64.StdEncoding.EncodedLen(len(raw)))
base64.StdEncoding.Encode(encoded, raw)
result = append(result, encoded)
}
return result
}
func parseEncodedMultiRawReply(args [][]byte) (*protocol.MultiRawReply, error) {
cmdBuf := new(bytes.Buffer)
for _, arg := range args {
dbuf := make([]byte, base64.StdEncoding.DecodedLen(len(arg)))
n, err := base64.StdEncoding.Decode(dbuf, arg)
if err != nil {
continue
}
cmdBuf.Write(dbuf[:n])
}
cmds, err := parser.ParseBytes(cmdBuf.Bytes())
if err != nil {
return nil, protocol.MakeErrReply(err.Error())
}
return protocol.MakeMultiRawReply(cmds), nil
}
func encodeMultiRawReply(src *protocol.MultiRawReply) *protocol.MultiBulkReply {
args := make([][]byte, 0, len(src.Replies))
for _, rep := range src.Replies {
raw := rep.ToBytes()
encoded := make([]byte, base64.StdEncoding.EncodedLen(len(raw)))
base64.StdEncoding.Encode(encoded, raw)
args = append(args, encoded)
}
return protocol.MakeMultiBulkReply(args)
}