tile38/internal/server/search.go

617 lines
15 KiB
Go
Raw Normal View History

package server
2016-03-05 02:08:16 +03:00
import (
"bytes"
"errors"
2017-07-26 06:23:21 +03:00
"sort"
2016-03-05 02:08:16 +03:00
"strconv"
"strings"
"time"
"github.com/mmcloughlin/geohash"
"github.com/tidwall/geojson"
2018-10-31 11:30:10 +03:00
"github.com/tidwall/geojson/geo"
"github.com/tidwall/geojson/geometry"
2016-03-29 00:16:21 +03:00
"github.com/tidwall/resp"
"github.com/tidwall/tile38/internal/bing"
2019-04-24 15:09:41 +03:00
"github.com/tidwall/tile38/internal/deadline"
"github.com/tidwall/tile38/internal/glob"
2016-03-05 02:08:16 +03:00
)
const defaultCircleSteps = 64
2016-03-05 02:08:16 +03:00
type liveFenceSwitches struct {
searchScanBaseTokens
obj geojson.Object
cmd string
roam roamSwitches
groups map[string]string
2016-05-23 23:01:42 +03:00
}
type roamSwitches struct {
on bool
key string
id string
pattern bool
meters float64
scan string
2018-08-14 20:48:28 +03:00
}
type roamMatch struct {
id string
obj geojson.Object
meters float64
2016-03-05 02:08:16 +03:00
}
func (s liveFenceSwitches) Error() string {
2018-08-14 03:05:30 +03:00
return goingLive
2016-03-05 02:08:16 +03:00
}
func (s liveFenceSwitches) Close() {
for _, whereeval := range s.whereevals {
whereeval.Close()
}
}
func (s liveFenceSwitches) usingLua() bool {
return len(s.whereevals) > 0
}
func (server *Server) cmdSearchArgs(
fromFenceCmd bool, cmd string, vs []string, types []string,
2018-08-14 03:05:30 +03:00
) (s liveFenceSwitches, err error) {
var t searchScanBaseTokens
if fromFenceCmd {
t.fence = true
}
vs, t, err = server.parseSearchScanBaseTokens(cmd, t, vs)
2018-08-14 03:05:30 +03:00
if err != nil {
2016-03-05 02:08:16 +03:00
return
}
2018-08-14 03:05:30 +03:00
s.searchScanBaseTokens = t
2016-03-05 02:08:16 +03:00
var typ string
2016-03-29 00:16:21 +03:00
var ok bool
if vs, typ, ok = tokenval(vs); !ok || typ == "" {
2016-03-05 02:08:16 +03:00
err = errInvalidNumberOfArguments
return
}
if s.searchScanBaseTokens.output == outputBounds {
if cmd == "within" || cmd == "intersects" {
if _, err := strconv.ParseFloat(typ, 64); err == nil {
// It's likely that the output was not specified, but rather the search bounds.
s.searchScanBaseTokens.output = defaultSearchOutput
vs = append([]string{typ}, vs...)
2016-03-05 02:08:16 +03:00
typ = "BOUNDS"
}
}
}
2016-05-23 23:01:42 +03:00
ltyp := strings.ToLower(typ)
2016-03-05 02:08:16 +03:00
var found bool
for _, t := range types {
2016-05-23 23:01:42 +03:00
if ltyp == t {
2016-03-05 02:08:16 +03:00
found = true
break
}
}
2016-05-23 23:01:42 +03:00
if !found && s.searchScanBaseTokens.fence && ltyp == "roam" && cmd == "nearby" {
// allow roaming for nearby fence searches.
found = true
}
2016-03-05 02:08:16 +03:00
if !found {
err = errInvalidArgument(typ)
return
}
2016-05-23 23:01:42 +03:00
switch ltyp {
2016-03-05 02:08:16 +03:00
case "point":
fallthrough
case "circle":
if s.clip {
2019-02-09 00:56:07 +03:00
err = errInvalidArgument("cannot clip with " + ltyp)
return
}
2016-03-05 02:08:16 +03:00
var slat, slon, smeters string
2016-03-29 00:16:21 +03:00
if vs, slat, ok = tokenval(vs); !ok || slat == "" {
2016-03-05 02:08:16 +03:00
err = errInvalidNumberOfArguments
return
}
2016-03-29 00:16:21 +03:00
if vs, slon, ok = tokenval(vs); !ok || slon == "" {
2016-03-05 02:08:16 +03:00
err = errInvalidNumberOfArguments
return
}
var lat, lon, meters float64
if lat, err = strconv.ParseFloat(slat, 64); err != nil {
2016-03-05 02:08:16 +03:00
err = errInvalidArgument(slat)
return
}
if lon, err = strconv.ParseFloat(slon, 64); err != nil {
2016-03-05 02:08:16 +03:00
err = errInvalidArgument(slon)
return
}
2018-10-26 02:37:06 +03:00
// radius is optional for nearby, but mandatory for others
if cmd == "nearby" {
if vs, smeters, ok = tokenval(vs); ok && smeters != "" {
if meters, err = strconv.ParseFloat(smeters, 64); err != nil {
err = errInvalidArgument(smeters)
return
}
if meters < 0 {
err = errInvalidArgument(smeters)
return
}
} else {
meters = -1
}
} else {
if vs, smeters, ok = tokenval(vs); !ok || smeters == "" {
err = errInvalidNumberOfArguments
return
}
if meters, err = strconv.ParseFloat(smeters, 64); err != nil {
err = errInvalidArgument(smeters)
return
}
if meters < 0 {
err = errInvalidArgument(smeters)
return
}
}
2018-10-26 02:37:06 +03:00
s.obj = geojson.NewCircle(geometry.Point{X: lon, Y: lat}, meters, defaultCircleSteps)
2016-03-05 02:08:16 +03:00
case "object":
2018-05-08 02:18:18 +03:00
if s.clip {
2019-02-09 00:56:07 +03:00
err = errInvalidArgument("cannot clip with object")
return
2018-05-08 02:18:18 +03:00
}
2016-03-29 00:16:21 +03:00
var obj string
if vs, obj, ok = tokenval(vs); !ok || obj == "" {
2016-03-05 02:08:16 +03:00
err = errInvalidNumberOfArguments
return
}
s.obj, err = geojson.Parse(obj, &server.geomParseOpts)
2016-03-05 02:08:16 +03:00
if err != nil {
return
}
case "bounds":
var sminLat, sminLon, smaxlat, smaxlon string
2016-03-29 00:16:21 +03:00
if vs, sminLat, ok = tokenval(vs); !ok || sminLat == "" {
2016-03-05 02:08:16 +03:00
err = errInvalidNumberOfArguments
return
}
2016-03-29 00:16:21 +03:00
if vs, sminLon, ok = tokenval(vs); !ok || sminLon == "" {
2016-03-05 02:08:16 +03:00
err = errInvalidNumberOfArguments
return
}
2016-03-29 00:16:21 +03:00
if vs, smaxlat, ok = tokenval(vs); !ok || smaxlat == "" {
2016-03-05 02:08:16 +03:00
err = errInvalidNumberOfArguments
return
}
2016-03-29 00:16:21 +03:00
if vs, smaxlon, ok = tokenval(vs); !ok || smaxlon == "" {
2016-03-05 02:08:16 +03:00
err = errInvalidNumberOfArguments
return
}
var minLat, minLon, maxLat, maxLon float64
if minLat, err = strconv.ParseFloat(sminLat, 64); err != nil {
2016-03-05 02:08:16 +03:00
err = errInvalidArgument(sminLat)
return
}
if minLon, err = strconv.ParseFloat(sminLon, 64); err != nil {
2016-03-05 02:08:16 +03:00
err = errInvalidArgument(sminLon)
return
}
if maxLat, err = strconv.ParseFloat(smaxlat, 64); err != nil {
2016-03-05 02:08:16 +03:00
err = errInvalidArgument(smaxlat)
return
}
if maxLon, err = strconv.ParseFloat(smaxlon, 64); err != nil {
2016-03-05 02:08:16 +03:00
err = errInvalidArgument(smaxlon)
return
}
s.obj = geojson.NewRect(geometry.Rect{
Min: geometry.Point{X: minLon, Y: minLat},
Max: geometry.Point{X: maxLon, Y: maxLat},
})
2016-03-05 02:08:16 +03:00
case "hash":
var hash string
2016-03-29 00:16:21 +03:00
if vs, hash, ok = tokenval(vs); !ok || hash == "" {
2016-03-05 02:08:16 +03:00
err = errInvalidNumberOfArguments
return
}
box := geohash.BoundingBox(hash)
s.obj = geojson.NewRect(geometry.Rect{
Min: geometry.Point{X: box.MinLng, Y: box.MinLat},
Max: geometry.Point{X: box.MaxLng, Y: box.MaxLat},
})
2016-03-05 02:08:16 +03:00
case "quadkey":
var key string
2016-03-29 00:16:21 +03:00
if vs, key, ok = tokenval(vs); !ok || key == "" {
2016-03-05 02:08:16 +03:00
err = errInvalidNumberOfArguments
return
}
var minLat, minLon, maxLat, maxLon float64
minLat, minLon, maxLat, maxLon, err = bing.QuadKeyToBounds(key)
if err != nil {
2016-03-05 02:08:16 +03:00
err = errInvalidArgument(key)
return
}
s.obj = geojson.NewRect(geometry.Rect{
Min: geometry.Point{X: minLon, Y: minLat},
Max: geometry.Point{X: maxLon, Y: maxLat},
})
2016-03-05 02:08:16 +03:00
case "tile":
var sx, sy, sz string
2016-03-29 00:16:21 +03:00
if vs, sx, ok = tokenval(vs); !ok || sx == "" {
2016-03-05 02:08:16 +03:00
err = errInvalidNumberOfArguments
return
}
2016-03-29 00:16:21 +03:00
if vs, sy, ok = tokenval(vs); !ok || sy == "" {
2016-03-05 02:08:16 +03:00
err = errInvalidNumberOfArguments
return
}
2016-03-29 00:16:21 +03:00
if vs, sz, ok = tokenval(vs); !ok || sz == "" {
2016-03-05 02:08:16 +03:00
err = errInvalidNumberOfArguments
return
}
var x, y int64
var z uint64
if x, err = strconv.ParseInt(sx, 10, 64); err != nil {
err = errInvalidArgument(sx)
return
}
if y, err = strconv.ParseInt(sy, 10, 64); err != nil {
err = errInvalidArgument(sy)
return
}
if z, err = strconv.ParseUint(sz, 10, 64); err != nil {
err = errInvalidArgument(sz)
return
}
var minLat, minLon, maxLat, maxLon float64
minLat, minLon, maxLat, maxLon = bing.TileXYToBounds(x, y, z)
s.obj = geojson.NewRect(geometry.Rect{
Min: geometry.Point{X: minLon, Y: minLat},
Max: geometry.Point{X: maxLon, Y: maxLat},
})
2016-03-05 02:08:16 +03:00
case "get":
2018-05-08 02:18:18 +03:00
if s.clip {
2019-02-09 00:56:07 +03:00
err = errInvalidArgument("cannot clip with get")
2018-05-08 02:18:18 +03:00
}
2016-03-05 02:08:16 +03:00
var key, id string
2016-03-29 00:16:21 +03:00
if vs, key, ok = tokenval(vs); !ok || key == "" {
2016-03-05 02:08:16 +03:00
err = errInvalidNumberOfArguments
return
}
2016-03-29 00:16:21 +03:00
if vs, id, ok = tokenval(vs); !ok || id == "" {
2016-03-05 02:08:16 +03:00
err = errInvalidNumberOfArguments
return
}
col := server.getCol(key)
2016-03-05 02:08:16 +03:00
if col == nil {
err = errKeyNotFound
return
}
s.obj, _, ok = col.Get(id)
2016-03-05 02:08:16 +03:00
if !ok {
err = errIDNotFound
return
}
2016-05-23 23:01:42 +03:00
case "roam":
s.roam.on = true
if vs, s.roam.key, ok = tokenval(vs); !ok || s.roam.key == "" {
err = errInvalidNumberOfArguments
return
}
if vs, s.roam.id, ok = tokenval(vs); !ok || s.roam.id == "" {
err = errInvalidNumberOfArguments
return
}
2016-07-12 22:18:16 +03:00
s.roam.pattern = glob.IsGlob(s.roam.id)
2016-05-23 23:01:42 +03:00
var smeters string
if vs, smeters, ok = tokenval(vs); !ok || smeters == "" {
err = errInvalidNumberOfArguments
return
}
if s.roam.meters, err = strconv.ParseFloat(smeters, 64); err != nil {
err = errInvalidArgument(smeters)
return
}
var scan string
if vs, scan, ok = tokenval(vs); ok {
if strings.ToLower(scan) != "scan" {
err = errInvalidArgument(scan)
return
}
if vs, scan, ok = tokenval(vs); !ok || scan == "" {
err = errInvalidNumberOfArguments
return
}
s.roam.scan = scan
}
2016-03-05 02:08:16 +03:00
}
2016-03-29 00:16:21 +03:00
if len(vs) != 0 {
2016-03-05 02:08:16 +03:00
err = errInvalidNumberOfArguments
return
}
return
}
2016-03-19 17:16:19 +03:00
var nearbyTypes = []string{"point"}
var withinOrIntersectsTypes = []string{
"geo", "bounds", "hash", "tile", "quadkey", "get", "object", "circle"}
2016-03-19 17:16:19 +03:00
func (server *Server) cmdNearby(msg *Message) (res resp.Value, err error) {
2016-03-05 02:08:16 +03:00
start := time.Now()
vs := msg.Args[1:]
2016-03-05 02:08:16 +03:00
wr := &bytes.Buffer{}
s, err := server.cmdSearchArgs(false, "nearby", vs, nearbyTypes)
if s.usingLua() {
defer s.Close()
defer func() {
if r := recover(); r != nil {
res = NOMessage
err = errors.New(r.(string))
return
}
}()
}
2016-03-05 02:08:16 +03:00
if err != nil {
return NOMessage, err
2016-03-05 02:08:16 +03:00
}
s.cmd = "nearby"
if s.fence {
return NOMessage, s
2016-03-05 02:08:16 +03:00
}
sw, err := server.newScanWriter(
wr, msg, s.key, s.output, s.precision, s.glob, false,
s.cursor, s.limit, s.wheres, s.whereins, s.whereevals, s.nofields)
2016-03-05 02:08:16 +03:00
if err != nil {
return NOMessage, err
2016-03-29 00:16:21 +03:00
}
if msg.OutputType == JSON {
2016-03-29 00:16:21 +03:00
wr.WriteString(`{"ok":true`)
2016-03-05 02:08:16 +03:00
}
sw.writeHead()
if sw.col != nil {
2018-11-02 15:09:51 +03:00
iter := func(id string, o geojson.Object, fields []float64, dist float64) bool {
meters := 0.0
2017-01-10 19:49:48 +03:00
if s.distance {
2018-11-02 15:09:51 +03:00
meters = geo.DistanceFromHaversine(dist)
2017-01-10 19:49:48 +03:00
}
return sw.writeObject(ScanWriterParams{
id: id,
o: o,
fields: fields,
2018-11-02 15:09:51 +03:00
distance: meters,
distOutput: s.distance,
noLock: true,
2018-10-26 02:37:06 +03:00
ignoreGlobMatch: true,
skipTesting: true,
})
}
2019-04-24 15:09:41 +03:00
server.nearestNeighbors(&s, sw, msg.Deadline, s.obj.(*geojson.Circle), iter)
2016-03-05 02:08:16 +03:00
}
sw.writeFoot()
if msg.OutputType == JSON {
wr.WriteString(`,"elapsed":"` + time.Since(start).String() + "\"}")
2017-10-05 18:20:40 +03:00
return resp.BytesValue(wr.Bytes()), nil
2016-03-29 00:16:21 +03:00
}
2017-10-05 18:20:40 +03:00
return sw.respOut, nil
2016-03-05 02:08:16 +03:00
}
2017-07-26 06:23:21 +03:00
type iterItem struct {
id string
o geojson.Object
fields []float64
dist float64
}
func (server *Server) nearestNeighbors(
2019-04-24 15:09:41 +03:00
s *liveFenceSwitches, sw *scanWriter, dl *deadline.Deadline,
target *geojson.Circle,
2018-11-02 15:09:51 +03:00
iter func(id string, o geojson.Object, fields []float64, dist float64,
) bool) {
2018-10-31 11:30:10 +03:00
maxDist := target.Haversine()
2017-07-26 06:23:21 +03:00
var items []iterItem
2019-04-24 15:09:41 +03:00
sw.col.Nearby(target, sw, dl, func(id string, o geojson.Object, fields []float64) bool {
if server.hasExpired(s.key, id) {
return true
}
ok, keepGoing, _ := sw.testObject(id, o, fields, false)
if !ok {
return true
}
2018-10-31 11:30:10 +03:00
dist := target.HaversineTo(o.Center())
2018-10-31 10:45:16 +03:00
if maxDist > 0 && dist > maxDist {
2018-10-30 04:18:04 +03:00
return false
2018-10-26 02:37:06 +03:00
}
items = append(items, iterItem{id: id, o: o, fields: fields, dist: dist})
if !keepGoing {
return false
}
return uint64(len(items)) < sw.limit
})
2017-07-26 06:23:21 +03:00
sort.Slice(items, func(i, j int) bool {
return items[i].dist < items[j].dist
})
for _, item := range items {
2018-11-02 15:09:51 +03:00
if !iter(item.id, item.o, item.fields, item.dist) {
2017-07-26 06:23:21 +03:00
return
}
}
}
func (server *Server) cmdWithin(msg *Message) (res resp.Value, err error) {
return server.cmdWithinOrIntersects("within", msg)
2016-03-05 02:08:16 +03:00
}
func (server *Server) cmdIntersects(msg *Message) (res resp.Value, err error) {
return server.cmdWithinOrIntersects("intersects", msg)
2016-03-05 02:08:16 +03:00
}
func (server *Server) cmdWithinOrIntersects(cmd string, msg *Message) (res resp.Value, err error) {
2016-03-05 02:08:16 +03:00
start := time.Now()
vs := msg.Args[1:]
2016-03-29 00:16:21 +03:00
2016-03-05 02:08:16 +03:00
wr := &bytes.Buffer{}
s, err := server.cmdSearchArgs(false, cmd, vs, withinOrIntersectsTypes)
if s.usingLua() {
defer s.Close()
defer func() {
if r := recover(); r != nil {
res = NOMessage
err = errors.New(r.(string))
return
}
}()
}
2016-03-05 02:08:16 +03:00
if err != nil {
return NOMessage, err
2016-03-05 02:08:16 +03:00
}
s.cmd = cmd
if s.fence {
return NOMessage, s
2016-03-05 02:08:16 +03:00
}
sw, err := server.newScanWriter(
wr, msg, s.key, s.output, s.precision, s.glob, false,
s.cursor, s.limit, s.wheres, s.whereins, s.whereevals, s.nofields)
2016-03-05 02:08:16 +03:00
if err != nil {
return NOMessage, err
2016-03-05 02:08:16 +03:00
}
if msg.OutputType == JSON {
2016-07-11 07:40:18 +03:00
wr.WriteString(`{"ok":true`)
}
2016-03-05 02:08:16 +03:00
sw.writeHead()
2017-08-03 14:01:07 +03:00
if sw.col != nil {
if cmd == "within" {
2019-04-24 15:09:41 +03:00
sw.col.Within(s.obj, s.sparse, sw, msg.Deadline, func(
id string, o geojson.Object, fields []float64,
) bool {
if server.hasExpired(s.key, id) {
return true
}
return sw.writeObject(ScanWriterParams{
id: id,
o: o,
fields: fields,
noLock: true,
})
})
2017-08-03 14:01:07 +03:00
} else if cmd == "intersects" {
2019-04-24 15:09:41 +03:00
sw.col.Intersects(s.obj, s.sparse, sw, msg.Deadline, func(
id string,
o geojson.Object,
fields []float64,
) bool {
if server.hasExpired(s.key, id) {
return true
}
params := ScanWriterParams{
id: id,
o: o,
fields: fields,
noLock: true,
}
if s.clip {
params.clip = s.obj
}
return sw.writeObject(params)
})
2017-08-03 14:01:07 +03:00
}
2016-03-05 02:08:16 +03:00
}
sw.writeFoot()
if msg.OutputType == JSON {
wr.WriteString(`,"elapsed":"` + time.Since(start).String() + "\"}")
2017-10-05 18:20:40 +03:00
return resp.BytesValue(wr.Bytes()), nil
2016-07-11 07:40:18 +03:00
}
2017-10-05 18:20:40 +03:00
return sw.respOut, nil
2016-07-11 07:40:18 +03:00
}
func (server *Server) cmdSeachValuesArgs(vs []string) (
2018-08-14 03:05:30 +03:00
s liveFenceSwitches, err error,
) {
var t searchScanBaseTokens
vs, t, err = server.parseSearchScanBaseTokens("search", t, vs)
2018-08-14 03:05:30 +03:00
if err != nil {
2016-07-11 07:40:18 +03:00
return
}
2018-08-14 03:05:30 +03:00
s.searchScanBaseTokens = t
2016-07-13 06:11:02 +03:00
if len(vs) != 0 {
err = errInvalidNumberOfArguments
2016-07-11 07:40:18 +03:00
return
}
2016-07-13 06:11:02 +03:00
return
}
func (server *Server) cmdSearch(msg *Message) (res resp.Value, err error) {
2016-07-13 06:11:02 +03:00
start := time.Now()
vs := msg.Args[1:]
2016-07-13 06:11:02 +03:00
2016-07-11 07:40:18 +03:00
wr := &bytes.Buffer{}
s, err := server.cmdSeachValuesArgs(vs)
if s.usingLua() {
defer s.Close()
defer func() {
if r := recover(); r != nil {
res = NOMessage
err = errors.New(r.(string))
return
}
}()
}
2016-07-13 06:11:02 +03:00
if err != nil {
return NOMessage, err
2016-07-13 06:11:02 +03:00
}
sw, err := server.newScanWriter(
wr, msg, s.key, s.output, s.precision, s.glob, true,
s.cursor, s.limit, s.wheres, s.whereins, s.whereevals, s.nofields)
2016-07-13 06:11:02 +03:00
if err != nil {
return NOMessage, err
2016-07-13 06:11:02 +03:00
}
if msg.OutputType == JSON {
2016-07-13 06:11:02 +03:00
wr.WriteString(`{"ok":true`)
2016-07-11 07:40:18 +03:00
}
2016-07-13 06:11:02 +03:00
sw.writeHead()
if sw.col != nil {
if sw.output == outputCount && len(sw.wheres) == 0 && sw.globEverything {
2016-07-13 07:59:36 +03:00
count := sw.col.Count() - int(s.cursor)
2016-07-13 06:11:02 +03:00
if count < 0 {
count = 0
}
sw.count = uint64(count)
} else {
2016-07-13 07:51:01 +03:00
g := glob.Parse(sw.globPattern, s.desc)
2016-07-13 06:11:02 +03:00
if g.Limits[0] == "" && g.Limits[1] == "" {
2019-04-24 15:09:41 +03:00
sw.col.SearchValues(s.desc, sw, msg.Deadline,
2016-07-13 06:11:02 +03:00
func(id string, o geojson.Object, fields []float64) bool {
2017-01-10 19:49:48 +03:00
return sw.writeObject(ScanWriterParams{
id: id,
o: o,
2017-01-10 19:49:48 +03:00
fields: fields,
2017-08-11 03:32:40 +03:00
noLock: true,
2017-01-10 19:49:48 +03:00
})
2016-07-13 06:11:02 +03:00
},
)
} else {
// must disable globSingle for string value type matching because
// globSingle is only for ID matches, not values.
sw.globSingle = false
2018-11-02 16:09:56 +03:00
sw.col.SearchValuesRange(g.Limits[0], g.Limits[1], s.desc, sw,
2019-04-24 15:09:41 +03:00
msg.Deadline,
2016-07-13 06:11:02 +03:00
func(id string, o geojson.Object, fields []float64) bool {
2017-01-10 19:49:48 +03:00
return sw.writeObject(ScanWriterParams{
id: id,
o: o,
2017-01-10 19:49:48 +03:00
fields: fields,
2017-08-11 03:32:40 +03:00
noLock: true,
2017-01-10 19:49:48 +03:00
})
2016-07-13 06:11:02 +03:00
},
)
2016-07-11 07:40:18 +03:00
}
}
2016-07-13 06:11:02 +03:00
}
sw.writeFoot()
if msg.OutputType == JSON {
wr.WriteString(`,"elapsed":"` + time.Since(start).String() + "\"}")
2017-10-05 18:20:40 +03:00
return resp.BytesValue(wr.Bytes()), nil
2016-07-11 07:40:18 +03:00
}
2017-10-05 18:20:40 +03:00
return sw.respOut, nil
2016-03-05 02:08:16 +03:00
}