From cdeb9d704bdd8e07a7cd5c6a5fbec277418ba617 Mon Sep 17 00:00:00 2001 From: Josh Baker Date: Mon, 28 Mar 2016 15:22:30 -0700 Subject: [PATCH] resp keys --- controller/controller.go | 4 +-- controller/keys.go | 55 +++++++++++++++++++++++++++------------- 2 files changed, 39 insertions(+), 20 deletions(-) diff --git a/controller/controller.go b/controller/controller.go index 2f29c572..3f3a96ca 100644 --- a/controller/controller.go +++ b/controller/controller.go @@ -369,8 +369,8 @@ func (c *Controller) command(msg *server.Message, w io.Writer) (res string, d co res, err = c.cmdIntersects(msg) case "get": res, err = c.cmdGet(msg) - // case "keys": - // err = c.cmdKeys(nline, w) + case "keys": + res, err = c.cmdKeys(msg) // case "aof": // err = c.cmdAOF(nline, w) // case "aofmd5": diff --git a/controller/keys.go b/controller/keys.go index 2c3e1421..bbddeb10 100644 --- a/controller/keys.go +++ b/controller/keys.go @@ -2,32 +2,38 @@ package controller import ( "bytes" - "io" "strings" "time" "github.com/google/btree" + "github.com/tidwall/resp" + "github.com/tidwall/tile38/controller/server" ) -func (c *Controller) cmdKeys(line string, w io.Writer) error { - var pattern string - if line, pattern = token(line); pattern == "" { - return errInvalidNumberOfArguments - } - if line != "" { - return errInvalidNumberOfArguments - } +func (c *Controller) cmdKeys(msg *server.Message) (res string, err error) { 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 once bool - wr.WriteString(`{"ok":true,"keys":[`) - + if msg.OutputType == server.JSON { + wr.WriteString(`{"ok":true,"keys":[`) + } var everything bool var greater bool var greaterPivot string + var vals []resp.Value iterator := func(item btree.Item) bool { - key := item.(*collectionT).Key var match bool if everything { @@ -42,12 +48,18 @@ func (c *Controller) cmdKeys(line string, w io.Writer) error { } if match { if once { - wr.WriteByte(',') + if msg.OutputType == server.JSON { + wr.WriteByte(',') + } } else { once = true } - s := jsonString(key) - wr.WriteString(s) + switch msg.OutputType { + case server.JSON: + wr.WriteString(jsonString(key)) + case server.RESP: + vals = append(vals, resp.StringValue(key)) + } } return true } @@ -73,7 +85,14 @@ func (c *Controller) cmdKeys(line string, w io.Writer) error { c.cols.AscendGreaterOrEqual(&collectionT{Key: greaterPivot}, iterator) } } - wr.WriteString(`],"elapsed":"` + time.Now().Sub(start).String() + "\"}") - w.Write(wr.Bytes()) - return nil + if msg.OutputType == server.JSON { + wr.WriteString(`],"elapsed":"` + time.Now().Sub(start).String() + "\"}") + } else { + data, err := resp.ArrayValue(vals).MarshalRESP() + if err != nil { + return "", err + } + wr.Write(data) + } + return wr.String(), nil }