Merge pull request #401 from tidwall/Fixing-KEYS-command

Fixing KEYS command and tests
This commit is contained in:
Josh Baker 2019-01-14 14:10:27 -07:00 committed by GitHub
commit 4b6c04a198
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 26 additions and 17 deletions

View File

@ -27,6 +27,10 @@ func (c *Server) cmdKeys(msg *Message) (res resp.Value, err error) {
if msg.OutputType == JSON { if msg.OutputType == JSON {
wr.WriteString(`{"ok":true,"keys":[`) wr.WriteString(`{"ok":true,"keys":[`)
} }
var wild bool
if strings.Contains(pattern, "*") {
wild = true
}
var everything bool var everything bool
var greater bool var greater bool
var greaterPivot string var greaterPivot string
@ -58,30 +62,29 @@ func (c *Server) cmdKeys(msg *Message) (res resp.Value, err error) {
case RESP: case RESP:
vals = append(vals, resp.StringValue(key)) vals = append(vals, resp.StringValue(key))
} }
// If no more than one match is expected, stop searching
if !wild {
return false
}
} }
return true return true
} }
// TODO: This can be further optimized by using glob.Parse and limits
if pattern == "*" { if pattern == "*" {
everything = true everything = true
c.cols.Scan(iterator) c.cols.Scan(iterator)
} else { } else if strings.HasSuffix(pattern, "*") {
if strings.HasSuffix(pattern, "*") { greaterPivot = pattern[:len(pattern)-1]
greaterPivot = pattern[:len(pattern)-1] if glob.IsGlob(greaterPivot) {
if glob.IsGlob(greaterPivot) {
greater = false
c.cols.Scan(iterator)
} else {
greater = true
c.cols.Ascend(greaterPivot, iterator)
}
} else if glob.IsGlob(pattern) {
greater = false
c.cols.Scan(iterator) c.cols.Scan(iterator)
} else { } else {
greater = true greater = true
greaterPivot = pattern
c.cols.Ascend(greaterPivot, iterator) c.cols.Ascend(greaterPivot, iterator)
} }
} else {
c.cols.Scan(iterator)
} }
if msg.OutputType == JSON { if msg.OutputType == JSON {
wr.WriteString(`],"elapsed":"` + time.Now().Sub(start).String() + "\"}") wr.WriteString(`],"elapsed":"` + time.Now().Sub(start).String() + "\"}")

View File

@ -149,14 +149,20 @@ func keys_KEYS_test(mc *mockServer) error {
{"SET", "mykey11", "myid3", "OBJECT", `{"type":"Point","coordinates":[-110,25,-8]}`}, {"OK"}, {"SET", "mykey11", "myid3", "OBJECT", `{"type":"Point","coordinates":[-110,25,-8]}`}, {"OK"},
{"SET", "mykey42", "myid2", "HASH", "9my5xp7"}, {"OK"}, {"SET", "mykey42", "myid2", "HASH", "9my5xp7"}, {"OK"},
{"SET", "mykey31", "myid4", "STRING", "value"}, {"OK"}, {"SET", "mykey31", "myid4", "STRING", "value"}, {"OK"},
{"KEYS", "*"}, {"[mykey11 mykey22 mykey31 mykey42]"}, {"SET", "mykey310", "myid5", "STRING", "value"}, {"OK"},
{"KEYS", "*key*"}, {"[mykey11 mykey22 mykey31 mykey42]"}, {"KEYS", "*"}, {"[mykey11 mykey22 mykey31 mykey310 mykey42]"},
{"KEYS", "mykey*"}, {"[mykey11 mykey22 mykey31 mykey42]"}, {"KEYS", "*key*"}, {"[mykey11 mykey22 mykey31 mykey310 mykey42]"},
{"KEYS", "mykey*"}, {"[mykey11 mykey22 mykey31 mykey310 mykey42]"},
{"KEYS", "mykey4*"}, {"[mykey42]"}, {"KEYS", "mykey4*"}, {"[mykey42]"},
{"KEYS", "mykey*1"}, {"[mykey11 mykey31]"}, {"KEYS", "mykey*1"}, {"[mykey11 mykey31]"},
{"KEYS", "mykey*1*"}, {"[mykey11 mykey31 mykey310]"},
{"KEYS", "mykey*10"}, {"[mykey310]"},
{"KEYS", "mykey*2"}, {"[mykey22 mykey42]"}, {"KEYS", "mykey*2"}, {"[mykey22 mykey42]"},
{"KEYS", "*2"}, {"[mykey22 mykey42]"}, {"KEYS", "*2"}, {"[mykey22 mykey42]"},
{"KEYS", "*1*"}, {"[mykey11 mykey31]"}, {"KEYS", "*1*"}, {"[mykey11 mykey31 mykey310]"},
{"KEYS", "mykey"}, {"[]"},
{"KEYS", "mykey31"}, {"[mykey31]"},
{"KEYS", "mykey[^3]*"}, {"[mykey11 mykey22 mykey42]"},
}) })
} }
func keys_PERSIST_test(mc *mockServer) error { func keys_PERSIST_test(mc *mockServer) error {