remove a little bit of waffling in arrays

master
Jordan Orelli 11 years ago
parent 03de2c0c0e
commit 37895517bd

@ -80,7 +80,7 @@ func readValue(r io.Reader) (value, error) {
case *bufio.Reader: case *bufio.Reader:
br = t br = t
default: default:
br = bufio.NewReader(r) br = bufio.NewReaderSize(r, 65536)
} }
line, err := br.ReadBytes('\n') line, err := br.ReadBytes('\n')
@ -112,8 +112,7 @@ func readValue(r io.Reader) (value, error) {
case start_bulkstring: case start_bulkstring:
return readBulkString(line, br) return readBulkString(line, br)
case start_array: case start_array:
line = line[:len(line)-2] return readArray(line, br)
return readArray(line[1:], br)
default: default:
return nil, fmt.Errorf("unable to read redis protocol value: illegal start character: %c", line[0]) return nil, fmt.Errorf("unable to read redis protocol value: illegal start character: %c", line[0])
} }
@ -226,7 +225,7 @@ func (s BulkStringVal) Write(w io.Writer) (int, error) {
type Array []byte type Array []byte
func readArray(prefix []byte, r *bufio.Reader) (value, error) { func readArray(prefix []byte, r *bufio.Reader) (value, error) {
n, err := strconv.Atoi(string(prefix)) n, err := strconv.Atoi(string(prefix[1 : len(prefix)-2]))
if err != nil { if err != nil {
return nil, fmt.Errorf("unable to read array in redis protocol: bad prefix: %v", err) return nil, fmt.Errorf("unable to read array in redis protocol: bad prefix: %v", err)
} }
@ -239,9 +238,7 @@ func readArray(prefix []byte, r *bufio.Reader) (value, error) {
} }
buf := bytes.NewBuffer(make([]byte, 0, n*128)) buf := bytes.NewBuffer(make([]byte, 0, n*128))
buf.Write([]byte{'*'}) buf.Write(prefix)
buf.Write([]byte(strconv.Itoa(n)))
buf.Write([]byte{'\r', '\n'})
for i := 0; i < n; i++ { for i := 0; i < n; i++ {
v, err := readValue(r) v, err := readValue(r)
if err != nil { if err != nil {
@ -253,6 +250,5 @@ func readArray(prefix []byte, r *bufio.Reader) (value, error) {
} }
func (a Array) Write(w io.Writer) (int, error) { func (a Array) Write(w io.Writer) (int, error) {
w.Write(a) return w.Write(a)
return 0, nil
} }

Loading…
Cancel
Save