-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
reader.go
142 lines (123 loc) · 2.77 KB
/
reader.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
package greddis
import (
"bufio"
"bytes"
"fmt"
"io"
"strconv"
"unsafe"
)
type ScanFunc func(*bufio.Reader) (int, error)
type reader interface {
Next(ScanFunc) error
Len() int
WriteTo(io.Writer) (int64, error) // copy on write
String() string // peek
Bytes() []byte // peek
Int() int
}
type Reader struct {
r *bufio.Reader
limitR *io.LimitedReader
err error
tokenLen int
intToken int
}
func NewReader(r *bufio.Reader) *Reader {
return &Reader{
r: r,
limitR: &io.LimitedReader{R: r},
}
}
func (r *Reader) Len() int {
return r.tokenLen
}
func (r *Reader) Next(scan ScanFunc) (err error) {
if r.tokenLen > 0 {
r.r.Discard(r.tokenLen + len(sep))
}
r.tokenLen, err = scan(r.r)
return err
}
func (r *Reader) WriteTo(w io.Writer) (int64, error) {
// CopyN internally uses a LimitedReader, but creates a new one every time, this avoids an alloc
r.limitR.N = int64(r.tokenLen)
i, err := io.Copy(w, r.limitR)
r.r.Discard(r.tokenLen - int(i) + len(sep))
r.tokenLen = 0
return i, err
}
func (r *Reader) String() string {
str, _ := r.r.Peek(r.tokenLen)
return *(*string)(unsafe.Pointer(&str))
}
func (r *Reader) Bytes() []byte {
b, _ := r.r.Peek(r.tokenLen)
return b
}
func (r *Reader) Int() int {
r.intToken, _ = strconv.Atoi(r.String())
return r.intToken
}
func ScanSimpleString(r *bufio.Reader) (int, error) {
return scanSimpleString(r, '+')
}
func ScanInteger(r *bufio.Reader) (int, error) {
return scanSimpleString(r, ':')
}
func ScanBulkString(r *bufio.Reader) (int, error) {
return scanBulkString(r, '$')
}
func ScanArray(r *bufio.Reader) (int, error) {
return scanBulkString(r, '*')
}
func scanBulkString(r *bufio.Reader, token byte) (int, error) {
countLen, err := scanSimpleString(r, token)
if err != nil {
return 0, err
}
b, _ := r.Peek(countLen)
arrayLen, err := strconv.Atoi(*(*string)(unsafe.Pointer(&b)))
r.Discard(countLen + len(sep))
if err != nil {
return arrayLen, err
}
return arrayLen, nil
}
func expectType(r *bufio.Reader, checkType byte) error {
b, err := r.ReadByte()
if err != nil {
return err
}
if b == checkType {
return nil
}
if b == '-' {
tokenLen, _ := scanToDelim(r, sep)
e, _ := r.Peek(tokenLen)
defer r.Discard(tokenLen + len(sep))
return fmt.Errorf("Redis error: %s", e)
}
return ErrWrongToken(checkType, b)
}
func scanToDelim(r *bufio.Reader, delim []byte) (int, error) {
for i := 0; ; i++ {
peek, err := r.Peek(i + len(sep))
if err != nil {
return i, err
}
if bytes.Equal(peek[i:], sep) {
return i, nil
}
}
}
func scanSimpleString(r *bufio.Reader, token byte) (int, error) {
if err := expectType(r, token); err != nil {
return 0, err
}
tokenLen, err := scanToDelim(r, sep)
if err != nil {
return 0, err
}
return tokenLen, nil
}