Skip to content

Commit

Permalink
Get readBulk to handle integer return values.
Browse files Browse the repository at this point in the history
  • Loading branch information
lonnc committed Nov 20, 2010
1 parent df9826b commit 4c4b65a
Showing 1 changed file with 32 additions and 21 deletions.
53 changes: 32 additions & 21 deletions redis.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,21 +47,39 @@ func readBulk(reader *bufio.Reader, head string) ([]byte, os.Error) {
return nil, err
}
}
if head[0] != '$' {
return nil, RedisError("Expecting Prefix '$'")
}

size, err := strconv.Atoi(strings.TrimSpace(head[1:]))
switch head[0] {
case ':':
data = []byte(strings.TrimSpace(head[1:]))

if size == -1 {
return nil, doesNotExist
case '$':
size, err := strconv.Atoi(strings.TrimSpace(head[1:]))
if err != nil {
return nil, err
}
if size == -1 {
return nil, doesNotExist
}
lr := io.LimitReader(reader, int64(size))
data, err = ioutil.ReadAll(lr)
if err == nil {
// read end of line
_, err = reader.ReadString('\n')
}
default:
return nil, RedisError("Expecting Prefix '$' or ':'")
}
lr := io.LimitReader(reader, int64(size))
data, err = ioutil.ReadAll(lr)

return data, err
}

func commandBytes(cmd string, args ...string) []byte {
cmdbuf := bytes.NewBufferString(fmt.Sprintf("*%d\r\n$%d\r\n%s\r\n", len(args)+1, len(cmd), cmd))
for _, s := range args {
cmdbuf.WriteString(fmt.Sprintf("$%d\r\n%s\r\n", len(s), s))
}
return cmdbuf.Bytes()
}

func readResponse(reader *bufio.Reader) (interface{}, os.Error) {

var line string
Expand Down Expand Up @@ -113,15 +131,10 @@ func readResponse(reader *bufio.Reader) (interface{}, os.Error) {
if err != nil {
return nil, err
}
//read the end line
_, err = reader.ReadString('\n')
if err != nil {
return nil, err
}
// dont read end of line as might not have been bulk
}
return res, nil
}

return readBulk(reader, line)
}

Expand Down Expand Up @@ -174,10 +187,6 @@ func (client *Client) openConnection() (c *net.TCPConn, err os.Error) {


func (client *Client) sendCommand(cmd string, args ...string) (data interface{}, err os.Error) {
cmdbuf := bytes.NewBufferString(fmt.Sprintf("*%d\r\n$%d\r\n%s\r\n", len(args)+1, len(cmd), cmd))
for _, s := range args {
cmdbuf.WriteString(fmt.Sprintf("$%d\r\n%s\r\n", len(s), s))
}

if client.pool == nil {
client.pool = make(chan *net.TCPConn, MaxPoolSize)
Expand All @@ -195,14 +204,16 @@ func (client *Client) sendCommand(cmd string, args ...string) (data interface{},
goto End
}
}
data, err = client.rawSend(c, cmdbuf.Bytes())

b := commandBytes(cmd, args...)
data, err = client.rawSend(c, b)
if err == os.EOF {
c, err = client.openConnection()
if err != nil {
goto End
}

data, err = client.rawSend(c, cmdbuf.Bytes())
data, err = client.rawSend(c, b)
}

End:
Expand Down

0 comments on commit 4c4b65a

Please sign in to comment.