tile38/internal/server/keys.go

68 lines
1.3 KiB
Go
Raw Normal View History

package server
2016-03-05 02:08:16 +03:00
import (
2022-09-24 02:12:32 +03:00
"encoding/json"
2016-03-05 02:08:16 +03:00
"time"
2016-03-29 01:22:30 +03:00
"github.com/tidwall/resp"
"github.com/tidwall/tile38/internal/collection"
"github.com/tidwall/tile38/internal/glob"
2016-03-05 02:08:16 +03:00
)
2022-09-24 02:12:32 +03:00
// KEYS pattern
func (s *Server) cmdKEYS(msg *Message) (resp.Value, error) {
2016-03-29 01:22:30 +03:00
var start = time.Now()
2022-09-24 02:12:32 +03:00
// >> Args
2016-03-29 01:22:30 +03:00
2022-09-24 02:12:32 +03:00
args := msg.Args
if len(args) != 2 {
return retrerr(errInvalidNumberOfArguments)
2019-01-14 21:06:12 +03:00
}
2022-09-24 02:12:32 +03:00
pattern := args[1]
2019-01-14 21:06:12 +03:00
2022-09-24 02:12:32 +03:00
// >> Operation
2019-01-14 21:06:12 +03:00
2022-09-24 02:12:32 +03:00
keys := []string{}
g := glob.Parse(pattern, false)
everything := g.Limits[0] == "" && g.Limits[1] == ""
if everything {
s.cols.Scan(
func(key string, _ *collection.Collection) bool {
match, _ := glob.Match(pattern, key)
if match {
keys = append(keys, key)
}
return true
},
)
2019-01-14 21:06:12 +03:00
} else {
2022-09-24 02:12:32 +03:00
s.cols.Ascend(g.Limits[0],
func(key string, _ *collection.Collection) bool {
if key > g.Limits[1] {
return false
}
match, _ := glob.Match(pattern, key)
if match {
keys = append(keys, key)
}
return true
},
)
2016-03-05 02:08:16 +03:00
}
2022-09-24 02:12:32 +03:00
// >> Response
if msg.OutputType == JSON {
2022-09-24 02:12:32 +03:00
data, _ := json.Marshal(keys)
return resp.StringValue(`{"ok":true,"keys":` + string(data) +
`,"elapsed":"` + time.Since(start).String() + `"}`), nil
}
var vals []resp.Value
for _, key := range keys {
vals = append(vals, resp.StringValue(key))
2016-03-29 01:22:30 +03:00
}
2017-10-05 18:20:40 +03:00
return resp.ArrayValue(vals), nil
2016-03-05 02:08:16 +03:00
}