forked from CodisLabs/codis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
encoder.go
184 lines (159 loc) · 3.65 KB
/
encoder.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
// Copyright 2016 CodisLabs. All Rights Reserved.
// Licensed under the MIT (MIT-LICENSE.txt) license.
package redis
import (
"bytes"
"io"
"strconv"
"github.com/CodisLabs/codis/pkg/utils/bufio2"
"github.com/CodisLabs/codis/pkg/utils/errors"
)
const (
minItoa = -128
maxItoa = 32768
)
var (
itoaOffset [maxItoa - minItoa + 1]uint32
itoaBuffer string
)
func init() {
var b bytes.Buffer
for i := range itoaOffset {
itoaOffset[i] = uint32(b.Len())
b.WriteString(strconv.Itoa(i + minItoa))
}
itoaBuffer = b.String()
}
func itoa(i int64) string {
if i >= minItoa && i <= maxItoa {
beg := itoaOffset[i-minItoa]
if i == maxItoa {
return itoaBuffer[beg:]
}
end := itoaOffset[i-minItoa+1]
return itoaBuffer[beg:end]
}
return strconv.FormatInt(i, 10)
}
type Encoder struct {
bw *bufio2.Writer
Err error
}
var ErrFailedEncoder = errors.New("use of failed encoder")
func NewEncoder(w io.Writer) *Encoder {
return NewEncoderBuffer(bufio2.NewWriterSize(w, 8192))
}
func NewEncoderSize(w io.Writer, size int) *Encoder {
return NewEncoderBuffer(bufio2.NewWriterSize(w, size))
}
func NewEncoderBuffer(bw *bufio2.Writer) *Encoder {
return &Encoder{bw: bw}
}
func (e *Encoder) Encode(r *Resp, flush bool) error {
if e.Err != nil {
return errors.Trace(ErrFailedEncoder)
}
if err := e.encodeResp(r); err != nil {
e.Err = err
} else if flush {
e.Err = errors.Trace(e.bw.Flush())
}
return e.Err
}
func (e *Encoder) EncodeMultiBulk(multi []*Resp, flush bool) error {
if e.Err != nil {
return errors.Trace(ErrFailedEncoder)
}
if err := e.encodeMultiBulk(multi); err != nil {
e.Err = err
} else if flush {
e.Err = errors.Trace(e.bw.Flush())
}
return e.Err
}
func (e *Encoder) Flush() error {
if e.Err != nil {
return errors.Trace(ErrFailedEncoder)
}
if err := e.bw.Flush(); err != nil {
e.Err = errors.Trace(err)
}
return e.Err
}
func Encode(w io.Writer, r *Resp) error {
return NewEncoder(w).Encode(r, true)
}
func EncodeToBytes(r *Resp) ([]byte, error) {
var b = &bytes.Buffer{}
if err := Encode(b, r); err != nil {
return nil, err
}
return b.Bytes(), nil
}
func (e *Encoder) encodeResp(r *Resp) error {
if err := e.bw.WriteByte(byte(r.Type)); err != nil {
return errors.Trace(err)
}
switch r.Type {
default:
return errors.Errorf("bad resp type %s", r.Type)
case TypeString, TypeError, TypeInt:
return e.encodeTextBytes(r.Value)
case TypeBulkBytes:
return e.encodeBulkBytes(r.Value)
case TypeArray:
return e.encodeArray(r.Array)
}
}
func (e *Encoder) encodeMultiBulk(multi []*Resp) error {
if err := e.bw.WriteByte(byte(TypeArray)); err != nil {
return errors.Trace(err)
}
return e.encodeArray(multi)
}
func (e *Encoder) encodeTextBytes(b []byte) error {
if _, err := e.bw.Write(b); err != nil {
return errors.Trace(err)
}
if _, err := e.bw.WriteString("\r\n"); err != nil {
return errors.Trace(err)
}
return nil
}
func (e *Encoder) encodeTextString(s string) error {
if _, err := e.bw.WriteString(s); err != nil {
return errors.Trace(err)
}
if _, err := e.bw.WriteString("\r\n"); err != nil {
return errors.Trace(err)
}
return nil
}
func (e *Encoder) encodeInt(v int64) error {
return e.encodeTextString(itoa(v))
}
func (e *Encoder) encodeBulkBytes(b []byte) error {
if b == nil {
return e.encodeInt(-1)
} else {
if err := e.encodeInt(int64(len(b))); err != nil {
return err
}
return e.encodeTextBytes(b)
}
}
func (e *Encoder) encodeArray(array []*Resp) error {
if array == nil {
return e.encodeInt(-1)
} else {
if err := e.encodeInt(int64(len(array))); err != nil {
return err
}
for _, r := range array {
if err := e.encodeResp(r); err != nil {
return err
}
}
return nil
}
}