resp keys

This commit is contained in:
Josh Baker 2016-03-28 15:22:30 -07:00
parent 3f2977b300
commit cdeb9d704b
2 changed files with 39 additions and 20 deletions

View File

@ -369,8 +369,8 @@ func (c *Controller) command(msg *server.Message, w io.Writer) (res string, d co
res, err = c.cmdIntersects(msg) res, err = c.cmdIntersects(msg)
case "get": case "get":
res, err = c.cmdGet(msg) res, err = c.cmdGet(msg)
// case "keys": case "keys":
// err = c.cmdKeys(nline, w) res, err = c.cmdKeys(msg)
// case "aof": // case "aof":
// err = c.cmdAOF(nline, w) // err = c.cmdAOF(nline, w)
// case "aofmd5": // case "aofmd5":

View File

@ -2,32 +2,38 @@ package controller
import ( import (
"bytes" "bytes"
"io"
"strings" "strings"
"time" "time"
"github.com/google/btree" "github.com/google/btree"
"github.com/tidwall/resp"
"github.com/tidwall/tile38/controller/server"
) )
func (c *Controller) cmdKeys(line string, w io.Writer) error { func (c *Controller) cmdKeys(msg *server.Message) (res string, err error) {
var pattern string
if line, pattern = token(line); pattern == "" {
return errInvalidNumberOfArguments
}
if line != "" {
return errInvalidNumberOfArguments
}
var start = time.Now() var start = time.Now()
vs := msg.Values[1:]
var pattern string
var ok bool
if vs, pattern, ok = tokenval(vs); !ok || pattern == "" {
return "", errInvalidNumberOfArguments
}
if len(vs) != 0 {
return "", errInvalidNumberOfArguments
}
var wr = &bytes.Buffer{} var wr = &bytes.Buffer{}
var once bool var once bool
wr.WriteString(`{"ok":true,"keys":[`) if msg.OutputType == server.JSON {
wr.WriteString(`{"ok":true,"keys":[`)
}
var everything bool var everything bool
var greater bool var greater bool
var greaterPivot string var greaterPivot string
var vals []resp.Value
iterator := func(item btree.Item) bool { iterator := func(item btree.Item) bool {
key := item.(*collectionT).Key key := item.(*collectionT).Key
var match bool var match bool
if everything { if everything {
@ -42,12 +48,18 @@ func (c *Controller) cmdKeys(line string, w io.Writer) error {
} }
if match { if match {
if once { if once {
wr.WriteByte(',') if msg.OutputType == server.JSON {
wr.WriteByte(',')
}
} else { } else {
once = true once = true
} }
s := jsonString(key) switch msg.OutputType {
wr.WriteString(s) case server.JSON:
wr.WriteString(jsonString(key))
case server.RESP:
vals = append(vals, resp.StringValue(key))
}
} }
return true return true
} }
@ -73,7 +85,14 @@ func (c *Controller) cmdKeys(line string, w io.Writer) error {
c.cols.AscendGreaterOrEqual(&collectionT{Key: greaterPivot}, iterator) c.cols.AscendGreaterOrEqual(&collectionT{Key: greaterPivot}, iterator)
} }
} }
wr.WriteString(`],"elapsed":"` + time.Now().Sub(start).String() + "\"}") if msg.OutputType == server.JSON {
w.Write(wr.Bytes()) wr.WriteString(`],"elapsed":"` + time.Now().Sub(start).String() + "\"}")
return nil } else {
data, err := resp.ArrayValue(vals).MarshalRESP()
if err != nil {
return "", err
}
wr.Write(data)
}
return wr.String(), nil
} }