Skip to content

Commit

Permalink
Add ParseReq method and tweak benchmarks.
Browse files Browse the repository at this point in the history
  • Loading branch information
vmihailenco committed Aug 24, 2012
1 parent f56748a commit b6ae953
Show file tree
Hide file tree
Showing 5 changed files with 119 additions and 21 deletions.
78 changes: 62 additions & 16 deletions parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,66 @@ func readLine(rd reader) ([]byte, error) {
return line, nil
}

func readN(rd reader, n int) ([]byte, error) {
buf, err := rd.ReadN(n)
if err == bufio.ErrBufferFull {
newBuf := make([]byte, n)
r := copy(newBuf, buf)
buf = newBuf

for r < n {
n, err := rd.Read(buf[r:])
if err != nil {
return nil, err
}
r += n
}
} else if err != nil {
return nil, err
}
return buf, nil
}

//------------------------------------------------------------------------------

func ParseReq(rd reader) ([]string, error) {
line, err := readLine(rd)
if err != nil {
return nil, err
}

if line[0] != '*' {
return []string{string(line)}, nil
}
numReplies, err := strconv.ParseInt(string(line[1:]), 10, 64)
if err != nil {
return nil, err
}

args := make([]string, 0, numReplies)
for i := int64(0); i < numReplies; i++ {
line, err = readLine(rd)
if err != nil {
return nil, err
}
if line[0] != '$' {
return nil, fmt.Errorf("Expected '$', but got %q", line)
}

argLen, err := strconv.ParseInt(string(line[1:]), 10, 32)
if err != nil {
return nil, err
}

arg, err := readN(rd, int(argLen)+2)
if err != nil {
return nil, err
}
args = append(args, string(arg[:argLen]))
}
return args, nil
}

//------------------------------------------------------------------------------

const (
Expand Down Expand Up @@ -99,24 +159,10 @@ func _parseReply(rd reader, multiBulkType int) (interface{}, error) {
}
replyLen := int(replyLenInt32) + 2

line, err = rd.ReadN(replyLen)
if err == bufio.ErrBufferFull {
buf := make([]byte, replyLen)
r := copy(buf, line)

for r < replyLen {
n, err := rd.Read(buf[r:])
if err != nil {
return "", err
}
r += n
}

line = buf
} else if err != nil {
line, err = readN(rd, replyLen)
if err != nil {
return "", err
}

return string(line[:len(line)-2]), nil
case '*':
if len(line) == 3 && line[1] == '-' && line[2] == '1' {
Expand Down
23 changes: 23 additions & 0 deletions parser_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package redis_test

import (
"bytes"

"github.com/vmihailenco/bufio"
. "launchpad.net/gocheck"

"github.com/vmihailenco/redis"
)

type ParserTest struct{}

var _ = Suite(&ParserTest{})

func (t *ParserTest) TestParseReq(c *C) {
buf := bytes.NewBufferString("*3\r\n$3\r\nSET\r\n$3\r\nkey\r\n$5\r\nhello\r\n")
rd := bufio.NewReaderSize(buf, 1024)

args, err := redis.ParseReq(rd)
c.Check(err, IsNil)
c.Check(args, DeepEquals, []string{"SET", "key", "hello"})
}
2 changes: 1 addition & 1 deletion pubsub.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func (c *PubSubClient) consumeMessages(conn *Conn) {
if err != nil {
msg.Err = err
c.ch <- msg
break
return
}
reply, ok := replyIface.([]interface{})
if !ok {
Expand Down
19 changes: 15 additions & 4 deletions redis.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,9 @@ func (c *BaseClient) conn() (*Conn, error) {
}
err = c.InitConn(client)
if err != nil {
if err := c.ConnPool.Remove(conn); err != nil {
panic(err)
}
return nil, err
}
}
Expand All @@ -102,23 +105,31 @@ func (c *BaseClient) Run(req Req) {

err = c.WriteReq(conn, req)
if err != nil {
c.ConnPool.Remove(conn)
if err := c.ConnPool.Remove(conn); err != nil {
panic(err)
}
req.SetErr(err)
return
}

val, err := req.ParseReply(conn.Rd)
if err != nil {
if err == Nil {
c.ConnPool.Add(conn)
if err := c.ConnPool.Add(conn); err != nil {
panic(err)
}
} else {
c.ConnPool.Remove(conn)
if err := c.ConnPool.Remove(conn); err != nil {
panic(err)
}
}
req.SetErr(err)
return
}

c.ConnPool.Add(conn)
if err := c.ConnPool.Add(conn); err != nil {
panic(err)
}
req.SetVal(val)
}

Expand Down
18 changes: 18 additions & 0 deletions req_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,18 @@ func (t *RequestTest) BenchmarkStatusReq(c *C) {
t.benchmarkReq(c, "+OK\r\n", redis.NewStatusReq(), Equals, "OK")
}

func (t *RequestTest) BenchmarkIntReq(c *C) {
t.benchmarkReq(c, ":1\r\n", redis.NewIntReq(), Equals, int64(1))
}

func (t *RequestTest) BenchmarkStringReq(c *C) {
t.benchmarkReq(c, "$5\r\nhello\r\n", redis.NewStringReq(), Equals, "hello")
}

func (t *RequestTest) BenchmarkFloatReq(c *C) {
t.benchmarkReq(c, "$5\r\n1.111\r\n", redis.NewFloatReq(), Equals, 1.111)
}

func (t *RequestTest) BenchmarkStringSliceReq(c *C) {
t.benchmarkReq(
c,
Expand All @@ -73,3 +81,13 @@ func (t *RequestTest) BenchmarkStringSliceReq(c *C) {
[]string{"hello", "hello"},
)
}

func (t *RequestTest) BenchmarkIfaceSliceReq(c *C) {
t.benchmarkReq(
c,
"*2\r\n$5\r\nhello\r\n$5\r\nhello\r\n",
redis.NewIfaceSliceReq(),
DeepEquals,
[]interface{}{"hello", "hello"},
)
}

0 comments on commit b6ae953

Please sign in to comment.