2016-03-05 02:08:16 +03:00
|
|
|
package controller
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"math"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
2016-03-28 18:57:41 +03:00
|
|
|
|
|
|
|
"github.com/tidwall/resp"
|
2016-03-05 02:08:16 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
const defaultSearchOutput = outputObjects
|
|
|
|
|
|
|
|
var errInvalidNumberOfArguments = errors.New("invalid number of arguments")
|
|
|
|
var errKeyNotFound = errors.New("key not found")
|
|
|
|
var errIDNotFound = errors.New("id not found")
|
2016-10-31 20:17:30 +03:00
|
|
|
var errIDAlreadyExists = errors.New("id already exists")
|
2016-12-12 20:33:28 +03:00
|
|
|
var errPathNotFound = errors.New("path not found")
|
2016-03-05 02:08:16 +03:00
|
|
|
|
|
|
|
func errInvalidArgument(arg string) error {
|
|
|
|
return fmt.Errorf("invalid argument '%s'", arg)
|
|
|
|
}
|
|
|
|
func errDuplicateArgument(arg string) error {
|
|
|
|
return fmt.Errorf("duplicate argument '%s'", arg)
|
|
|
|
}
|
|
|
|
func token(line string) (newLine, token string) {
|
|
|
|
for i := 0; i < len(line); i++ {
|
|
|
|
if line[i] == ' ' {
|
|
|
|
return line[i+1:], line[:i]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return "", line
|
|
|
|
}
|
|
|
|
|
2016-03-28 18:57:41 +03:00
|
|
|
func tokenval(vs []resp.Value) (nvs []resp.Value, token string, ok bool) {
|
|
|
|
if len(vs) > 0 {
|
|
|
|
token = vs[0].String()
|
|
|
|
nvs = vs[1:]
|
|
|
|
ok = true
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-11-09 23:43:26 +03:00
|
|
|
func tokenvalbytes(vs []resp.Value) (nvs []resp.Value, token []byte, ok bool) {
|
|
|
|
if len(vs) > 0 {
|
|
|
|
token = vs[0].Bytes()
|
|
|
|
nvs = vs[1:]
|
|
|
|
ok = true
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-03-05 02:08:16 +03:00
|
|
|
func tokenlc(line string) (newLine, token string) {
|
|
|
|
for i := 0; i < len(line); i++ {
|
|
|
|
ch := line[i]
|
|
|
|
if ch == ' ' {
|
|
|
|
return line[i+1:], line[:i]
|
|
|
|
}
|
|
|
|
if ch >= 'A' && ch <= 'Z' {
|
|
|
|
lc := make([]byte, 0, 16)
|
|
|
|
if i > 0 {
|
|
|
|
lc = append(lc, []byte(line[:i])...)
|
|
|
|
}
|
|
|
|
lc = append(lc, ch+32)
|
|
|
|
i++
|
|
|
|
for ; i < len(line); i++ {
|
|
|
|
ch = line[i]
|
|
|
|
if ch == ' ' {
|
|
|
|
return line[i+1:], string(lc)
|
|
|
|
}
|
|
|
|
if ch >= 'A' && ch <= 'Z' {
|
|
|
|
lc = append(lc, ch+32)
|
|
|
|
} else {
|
|
|
|
lc = append(lc, ch)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return "", string(lc)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return "", line
|
|
|
|
}
|
2016-11-09 23:43:26 +03:00
|
|
|
func lcb(s1 []byte, s2 string) bool {
|
|
|
|
if len(s1) != len(s2) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
for i := 0; i < len(s1); i++ {
|
|
|
|
ch := s1[i]
|
|
|
|
if ch >= 'A' && ch <= 'Z' {
|
|
|
|
if ch+32 != s2[i] {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
} else if ch != s2[i] {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
2016-03-05 02:08:16 +03:00
|
|
|
func lc(s1, s2 string) bool {
|
|
|
|
if len(s1) != len(s2) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
for i := 0; i < len(s1); i++ {
|
|
|
|
ch := s1[i]
|
|
|
|
if ch >= 'A' && ch <= 'Z' {
|
|
|
|
if ch+32 != s2[i] {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
} else if ch != s2[i] {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
type whereT struct {
|
|
|
|
field string
|
|
|
|
minx bool
|
|
|
|
min float64
|
|
|
|
maxx bool
|
|
|
|
max float64
|
|
|
|
}
|
|
|
|
|
|
|
|
func (where whereT) match(value float64) bool {
|
|
|
|
if !where.minx {
|
|
|
|
if value < where.min {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if value <= where.min {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !where.maxx {
|
|
|
|
if value > where.max {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if value >= where.max {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2016-10-03 21:37:16 +03:00
|
|
|
func zMinMaxFromWheres(wheres []whereT) (minZ, maxZ float64) {
|
|
|
|
for _, w := range wheres {
|
|
|
|
if w.field == "z" {
|
|
|
|
minZ = w.min
|
|
|
|
maxZ = w.max
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
minZ = math.Inf(-1)
|
|
|
|
maxZ = math.Inf(+1)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-03-05 02:08:16 +03:00
|
|
|
type searchScanBaseTokens struct {
|
|
|
|
key string
|
|
|
|
cursor uint64
|
|
|
|
output outputT
|
|
|
|
precision uint64
|
|
|
|
lineout string
|
|
|
|
fence bool
|
2017-01-10 19:49:48 +03:00
|
|
|
distance bool
|
2016-04-01 22:46:39 +03:00
|
|
|
detect map[string]bool
|
2016-12-15 20:00:08 +03:00
|
|
|
accept map[string]bool
|
2016-03-05 02:08:16 +03:00
|
|
|
glob string
|
|
|
|
wheres []whereT
|
|
|
|
nofields bool
|
|
|
|
limit uint64
|
|
|
|
sparse uint8
|
2016-07-12 22:18:16 +03:00
|
|
|
desc bool
|
2016-03-05 02:08:16 +03:00
|
|
|
}
|
|
|
|
|
2016-03-29 00:16:21 +03:00
|
|
|
func parseSearchScanBaseTokens(cmd string, vs []resp.Value) (vsout []resp.Value, t searchScanBaseTokens, err error) {
|
|
|
|
var ok bool
|
|
|
|
if vs, t.key, ok = tokenval(vs); !ok || t.key == "" {
|
2016-03-05 02:08:16 +03:00
|
|
|
err = errInvalidNumberOfArguments
|
|
|
|
return
|
|
|
|
}
|
|
|
|
var slimit string
|
|
|
|
var ssparse string
|
|
|
|
var scursor string
|
2016-07-12 22:18:16 +03:00
|
|
|
var asc bool
|
2016-03-05 02:08:16 +03:00
|
|
|
for {
|
2016-03-29 00:16:21 +03:00
|
|
|
nvs, wtok, ok := tokenval(vs)
|
|
|
|
if ok && len(wtok) > 0 {
|
2016-03-05 02:08:16 +03:00
|
|
|
if (wtok[0] == 'C' || wtok[0] == 'c') && strings.ToLower(wtok) == "cursor" {
|
2016-03-29 00:16:21 +03:00
|
|
|
vs = nvs
|
2016-03-05 02:08:16 +03:00
|
|
|
if scursor != "" {
|
|
|
|
err = errDuplicateArgument(strings.ToUpper(wtok))
|
|
|
|
return
|
|
|
|
}
|
2016-03-29 00:16:21 +03:00
|
|
|
if vs, scursor, ok = tokenval(vs); !ok || scursor == "" {
|
2016-03-05 02:08:16 +03:00
|
|
|
err = errInvalidNumberOfArguments
|
|
|
|
return
|
|
|
|
}
|
|
|
|
continue
|
|
|
|
} else if (wtok[0] == 'W' || wtok[0] == 'w') && strings.ToLower(wtok) == "where" {
|
2016-03-29 00:16:21 +03:00
|
|
|
vs = nvs
|
2016-03-05 02:08:16 +03:00
|
|
|
var field, smin, smax string
|
2016-03-29 00:16:21 +03:00
|
|
|
if vs, field, ok = tokenval(vs); !ok || field == "" {
|
2016-03-05 02:08:16 +03:00
|
|
|
err = errInvalidNumberOfArguments
|
|
|
|
return
|
|
|
|
}
|
2016-03-29 00:16:21 +03:00
|
|
|
if vs, smin, ok = tokenval(vs); !ok || smin == "" {
|
2016-03-05 02:08:16 +03:00
|
|
|
err = errInvalidNumberOfArguments
|
|
|
|
return
|
|
|
|
}
|
2016-03-29 00:16:21 +03:00
|
|
|
if vs, smax, ok = tokenval(vs); !ok || smax == "" {
|
2016-03-05 02:08:16 +03:00
|
|
|
err = errInvalidNumberOfArguments
|
|
|
|
return
|
|
|
|
}
|
|
|
|
var minx, maxx bool
|
|
|
|
var min, max float64
|
|
|
|
if strings.ToLower(smin) == "-inf" {
|
|
|
|
min = math.Inf(-1)
|
|
|
|
} else {
|
|
|
|
if strings.HasPrefix(smin, "(") {
|
|
|
|
minx = true
|
|
|
|
smin = smin[1:]
|
|
|
|
}
|
|
|
|
min, err = strconv.ParseFloat(smin, 64)
|
|
|
|
if err != nil {
|
|
|
|
err = errInvalidArgument(smin)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if strings.ToLower(smax) == "+inf" {
|
|
|
|
max = math.Inf(+1)
|
|
|
|
} else {
|
|
|
|
if strings.HasPrefix(smax, "(") {
|
|
|
|
maxx = true
|
|
|
|
smax = smax[1:]
|
|
|
|
}
|
|
|
|
max, err = strconv.ParseFloat(smax, 64)
|
|
|
|
if err != nil {
|
|
|
|
err = errInvalidArgument(smax)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
t.wheres = append(t.wheres, whereT{field, minx, min, maxx, max})
|
|
|
|
continue
|
|
|
|
} else if (wtok[0] == 'N' || wtok[0] == 'n') && strings.ToLower(wtok) == "nofields" {
|
2016-03-29 00:16:21 +03:00
|
|
|
vs = nvs
|
2016-03-05 02:08:16 +03:00
|
|
|
if t.nofields {
|
|
|
|
err = errDuplicateArgument(strings.ToUpper(wtok))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
t.nofields = true
|
|
|
|
continue
|
|
|
|
} else if (wtok[0] == 'L' || wtok[0] == 'l') && strings.ToLower(wtok) == "limit" {
|
2016-03-29 00:16:21 +03:00
|
|
|
vs = nvs
|
2016-03-05 02:08:16 +03:00
|
|
|
if slimit != "" {
|
|
|
|
err = errDuplicateArgument(strings.ToUpper(wtok))
|
|
|
|
return
|
|
|
|
}
|
2016-03-29 00:16:21 +03:00
|
|
|
if vs, slimit, ok = tokenval(vs); !ok || slimit == "" {
|
2016-03-05 02:08:16 +03:00
|
|
|
err = errInvalidNumberOfArguments
|
|
|
|
return
|
|
|
|
}
|
|
|
|
continue
|
|
|
|
} else if (wtok[0] == 'S' || wtok[0] == 's') && strings.ToLower(wtok) == "sparse" {
|
2016-03-29 00:16:21 +03:00
|
|
|
vs = nvs
|
2016-03-05 02:08:16 +03:00
|
|
|
if ssparse != "" {
|
|
|
|
err = errDuplicateArgument(strings.ToUpper(wtok))
|
|
|
|
return
|
|
|
|
}
|
2016-03-29 00:16:21 +03:00
|
|
|
if vs, ssparse, ok = tokenval(vs); !ok || ssparse == "" {
|
2016-03-05 02:08:16 +03:00
|
|
|
err = errInvalidNumberOfArguments
|
|
|
|
return
|
|
|
|
}
|
|
|
|
continue
|
|
|
|
} else if (wtok[0] == 'F' || wtok[0] == 'f') && strings.ToLower(wtok) == "fence" {
|
2016-03-29 00:16:21 +03:00
|
|
|
vs = nvs
|
2016-03-05 02:08:16 +03:00
|
|
|
if t.fence {
|
|
|
|
err = errDuplicateArgument(strings.ToUpper(wtok))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
t.fence = true
|
2016-04-01 22:46:39 +03:00
|
|
|
continue
|
2016-12-15 20:00:08 +03:00
|
|
|
} else if (wtok[0] == 'C' || wtok[0] == 'c') && strings.ToLower(wtok) == "commands" {
|
|
|
|
vs = nvs
|
|
|
|
if t.accept != nil {
|
|
|
|
err = errDuplicateArgument(strings.ToUpper(wtok))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
t.accept = make(map[string]bool)
|
|
|
|
var peek string
|
|
|
|
if vs, peek, ok = tokenval(vs); !ok || peek == "" {
|
|
|
|
err = errInvalidNumberOfArguments
|
|
|
|
return
|
|
|
|
}
|
|
|
|
for _, s := range strings.Split(peek, ",") {
|
|
|
|
part := strings.TrimSpace(strings.ToLower(s))
|
|
|
|
if t.accept[part] {
|
|
|
|
err = errDuplicateArgument(s)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
t.accept[part] = true
|
|
|
|
}
|
|
|
|
if len(t.accept) == 0 {
|
|
|
|
t.accept = nil
|
|
|
|
}
|
|
|
|
continue
|
2017-01-10 19:49:48 +03:00
|
|
|
} else if (wtok[0] == 'D' || wtok[0] == 'd') && strings.ToLower(wtok) == "distance" {
|
|
|
|
vs = nvs
|
|
|
|
if t.distance {
|
|
|
|
err = errDuplicateArgument(strings.ToUpper(wtok))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
t.distance = true
|
|
|
|
continue
|
2016-04-01 22:46:39 +03:00
|
|
|
} else if (wtok[0] == 'D' || wtok[0] == 'd') && strings.ToLower(wtok) == "detect" {
|
|
|
|
vs = nvs
|
|
|
|
if t.detect != nil {
|
|
|
|
err = errDuplicateArgument(strings.ToUpper(wtok))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
t.detect = make(map[string]bool)
|
|
|
|
var peek string
|
|
|
|
if vs, peek, ok = tokenval(vs); !ok || peek == "" {
|
|
|
|
err = errInvalidNumberOfArguments
|
|
|
|
return
|
|
|
|
}
|
|
|
|
for _, s := range strings.Split(peek, ",") {
|
|
|
|
part := strings.TrimSpace(strings.ToLower(s))
|
|
|
|
switch part {
|
|
|
|
default:
|
|
|
|
err = errInvalidArgument(peek)
|
|
|
|
return
|
|
|
|
case "inside", "outside", "enter", "exit", "cross":
|
|
|
|
}
|
|
|
|
if t.detect[part] {
|
|
|
|
err = errDuplicateArgument(s)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
t.detect[part] = true
|
|
|
|
}
|
|
|
|
if len(t.detect) == 0 {
|
|
|
|
t.detect = map[string]bool{
|
|
|
|
"inside": true,
|
|
|
|
"outside": true,
|
|
|
|
"enter": true,
|
|
|
|
"exit": true,
|
|
|
|
"cross": true,
|
|
|
|
}
|
|
|
|
}
|
2016-07-12 22:18:16 +03:00
|
|
|
continue
|
|
|
|
} else if (wtok[0] == 'D' || wtok[0] == 'd') && strings.ToLower(wtok) == "desc" {
|
|
|
|
vs = nvs
|
|
|
|
if t.desc || asc {
|
|
|
|
err = errDuplicateArgument(strings.ToUpper(wtok))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
t.desc = true
|
|
|
|
continue
|
|
|
|
} else if (wtok[0] == 'A' || wtok[0] == 'a') && strings.ToLower(wtok) == "asc" {
|
|
|
|
vs = nvs
|
|
|
|
if t.desc || asc {
|
|
|
|
err = errDuplicateArgument(strings.ToUpper(wtok))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
asc = true
|
2016-03-05 02:08:16 +03:00
|
|
|
continue
|
|
|
|
} else if (wtok[0] == 'M' || wtok[0] == 'm') && strings.ToLower(wtok) == "match" {
|
2016-03-29 00:16:21 +03:00
|
|
|
vs = nvs
|
2016-03-05 02:08:16 +03:00
|
|
|
if t.glob != "" {
|
|
|
|
err = errDuplicateArgument(strings.ToUpper(wtok))
|
|
|
|
return
|
|
|
|
}
|
2016-03-29 00:16:21 +03:00
|
|
|
if vs, t.glob, ok = tokenval(vs); !ok || t.glob == "" {
|
2016-03-05 02:08:16 +03:00
|
|
|
err = errInvalidNumberOfArguments
|
|
|
|
return
|
|
|
|
}
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
// check to make sure that there aren't any conflicts
|
2016-07-13 06:11:02 +03:00
|
|
|
if cmd == "scan" || cmd == "search" {
|
2016-03-05 02:08:16 +03:00
|
|
|
if ssparse != "" {
|
2016-07-13 06:11:02 +03:00
|
|
|
err = errors.New("SPARSE is not allowed for " + strings.ToUpper(cmd))
|
2016-03-05 02:08:16 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
if t.fence {
|
2016-07-13 06:11:02 +03:00
|
|
|
err = errors.New("FENCE is not allowed for " + strings.ToUpper(cmd))
|
2016-03-05 02:08:16 +03:00
|
|
|
return
|
|
|
|
}
|
2016-07-12 22:18:16 +03:00
|
|
|
} else {
|
|
|
|
if t.desc {
|
|
|
|
err = errors.New("DESC is not allowed for " + strings.ToUpper(cmd))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if asc {
|
|
|
|
err = errors.New("ASC is not allowed for " + strings.ToUpper(cmd))
|
|
|
|
return
|
|
|
|
}
|
2016-03-05 02:08:16 +03:00
|
|
|
}
|
|
|
|
if ssparse != "" && slimit != "" {
|
|
|
|
err = errors.New("LIMIT is not allowed when SPARSE is specified")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if scursor != "" && ssparse != "" {
|
|
|
|
err = errors.New("CURSOR is not allowed when SPARSE is specified")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if scursor != "" && t.fence {
|
|
|
|
err = errors.New("CURSOR is not allowed when FENCE is specified")
|
|
|
|
return
|
|
|
|
}
|
2016-04-01 22:46:39 +03:00
|
|
|
if t.detect != nil && !t.fence {
|
|
|
|
err = errors.New("DETECT is not allowed when FENCE is not specified")
|
|
|
|
return
|
|
|
|
}
|
2016-03-05 02:08:16 +03:00
|
|
|
|
|
|
|
t.output = defaultSearchOutput
|
2016-03-29 00:16:21 +03:00
|
|
|
var nvs []resp.Value
|
2016-03-05 02:08:16 +03:00
|
|
|
var sprecision string
|
|
|
|
var which string
|
2016-03-29 00:16:21 +03:00
|
|
|
if nvs, which, ok = tokenval(vs); ok && which != "" {
|
2016-03-05 02:08:16 +03:00
|
|
|
updline := true
|
|
|
|
switch strings.ToLower(which) {
|
|
|
|
default:
|
|
|
|
if cmd == "scan" {
|
|
|
|
err = errInvalidArgument(which)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
updline = false
|
|
|
|
case "count":
|
|
|
|
t.output = outputCount
|
|
|
|
case "objects":
|
|
|
|
t.output = outputObjects
|
|
|
|
case "points":
|
|
|
|
t.output = outputPoints
|
|
|
|
case "hashes":
|
|
|
|
t.output = outputHashes
|
2016-03-29 00:16:21 +03:00
|
|
|
if nvs, sprecision, ok = tokenval(nvs); !ok || sprecision == "" {
|
2016-03-05 02:08:16 +03:00
|
|
|
err = errInvalidNumberOfArguments
|
|
|
|
return
|
|
|
|
}
|
|
|
|
case "bounds":
|
|
|
|
t.output = outputBounds
|
|
|
|
case "ids":
|
|
|
|
t.output = outputIDs
|
|
|
|
}
|
|
|
|
if updline {
|
2016-03-29 00:16:21 +03:00
|
|
|
vs = nvs
|
2016-03-05 02:08:16 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if scursor != "" {
|
|
|
|
if t.cursor, err = strconv.ParseUint(scursor, 10, 64); err != nil {
|
|
|
|
err = errInvalidArgument(scursor)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if sprecision != "" {
|
|
|
|
if t.precision, err = strconv.ParseUint(sprecision, 10, 64); err != nil || t.precision == 0 || t.precision > 64 {
|
|
|
|
err = errInvalidArgument(sprecision)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if slimit != "" {
|
|
|
|
if t.limit, err = strconv.ParseUint(slimit, 10, 64); err != nil || t.limit == 0 {
|
|
|
|
err = errInvalidArgument(slimit)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if ssparse != "" {
|
|
|
|
var sparse uint64
|
|
|
|
if sparse, err = strconv.ParseUint(ssparse, 10, 8); err != nil || sparse == 0 || sparse > 8 {
|
|
|
|
err = errInvalidArgument(ssparse)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
t.sparse = uint8(sparse)
|
|
|
|
t.limit = math.MaxUint64
|
|
|
|
}
|
2016-03-29 00:16:21 +03:00
|
|
|
vsout = vs
|
2016-03-05 02:08:16 +03:00
|
|
|
return
|
|
|
|
}
|