2016-03-05 02:08:16 +03:00
|
|
|
package controller
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
2016-03-29 00:16:21 +03:00
|
|
|
"github.com/tidwall/resp"
|
2016-03-06 17:55:00 +03:00
|
|
|
"github.com/tidwall/tile38/controller/bing"
|
2016-03-29 00:16:21 +03:00
|
|
|
"github.com/tidwall/tile38/controller/server"
|
2016-03-05 02:08:16 +03:00
|
|
|
"github.com/tidwall/tile38/geojson"
|
|
|
|
"github.com/tidwall/tile38/geojson/geohash"
|
|
|
|
)
|
|
|
|
|
|
|
|
type liveFenceSwitches struct {
|
|
|
|
searchScanBaseTokens
|
|
|
|
lat, lon, meters float64
|
|
|
|
o geojson.Object
|
|
|
|
minLat, minLon float64
|
|
|
|
maxLat, maxLon float64
|
|
|
|
cmd string
|
2016-05-23 23:01:42 +03:00
|
|
|
roam roamSwitches
|
|
|
|
}
|
|
|
|
|
|
|
|
type roamSwitches struct {
|
|
|
|
on bool
|
|
|
|
key string
|
|
|
|
id string
|
|
|
|
pattern bool
|
|
|
|
meters float64
|
2016-03-05 02:08:16 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s liveFenceSwitches) Error() string {
|
|
|
|
return "going live"
|
|
|
|
}
|
|
|
|
|
2016-03-29 00:16:21 +03:00
|
|
|
func (c *Controller) cmdSearchArgs(cmd string, vs []resp.Value, types []string) (s liveFenceSwitches, err error) {
|
|
|
|
if vs, s.searchScanBaseTokens, err = parseSearchScanBaseTokens(cmd, vs); err != nil {
|
2016-03-05 02:08:16 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
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
|
2016-03-29 00:16:21 +03:00
|
|
|
vs = append([]resp.Value{resp.StringValue(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":
|
|
|
|
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
|
|
|
|
}
|
2016-03-29 00:16:21 +03:00
|
|
|
if vs, smeters, ok = tokenval(vs); !ok || smeters == "" {
|
2016-03-05 02:08:16 +03:00
|
|
|
err = errInvalidNumberOfArguments
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if s.lat, err = strconv.ParseFloat(slat, 64); err != nil {
|
|
|
|
err = errInvalidArgument(slat)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if s.lon, err = strconv.ParseFloat(slon, 64); err != nil {
|
|
|
|
err = errInvalidArgument(slon)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if s.meters, err = strconv.ParseFloat(smeters, 64); err != nil {
|
|
|
|
err = errInvalidArgument(smeters)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
case "object":
|
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
|
|
|
|
}
|
2016-03-29 00:16:21 +03:00
|
|
|
s.o, err = geojson.ObjectJSON(obj)
|
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
|
|
|
|
}
|
|
|
|
if s.minLat, err = strconv.ParseFloat(sminLat, 64); err != nil {
|
|
|
|
err = errInvalidArgument(sminLat)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if s.minLon, err = strconv.ParseFloat(sminLon, 64); err != nil {
|
|
|
|
err = errInvalidArgument(sminLon)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if s.maxLat, err = strconv.ParseFloat(smaxlat, 64); err != nil {
|
|
|
|
err = errInvalidArgument(smaxlat)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if s.maxLon, err = strconv.ParseFloat(smaxlon, 64); err != nil {
|
|
|
|
err = errInvalidArgument(smaxlon)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
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
|
|
|
|
}
|
|
|
|
if s.minLat, s.minLon, s.maxLat, s.maxLon, err = geohash.Bounds(hash); err != nil {
|
|
|
|
err = errInvalidArgument(hash)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
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
|
|
|
|
}
|
|
|
|
if s.minLat, s.minLon, s.maxLat, s.maxLon, err = bing.QuadKeyToBounds(key); err != nil {
|
|
|
|
err = errInvalidArgument(key)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
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
|
|
|
|
}
|
|
|
|
s.minLat, s.minLon, s.maxLat, s.maxLon = bing.TileXYToBounds(x, y, z)
|
|
|
|
case "get":
|
|
|
|
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 := c.getCol(key)
|
|
|
|
if col == nil {
|
|
|
|
err = errKeyNotFound
|
|
|
|
return
|
|
|
|
}
|
|
|
|
o, _, ok := col.Get(id)
|
|
|
|
if !ok {
|
|
|
|
err = errIDNotFound
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if o.IsBBoxDefined() {
|
|
|
|
bbox := o.CalculatedBBox()
|
|
|
|
s.minLat = bbox.Min.Y
|
|
|
|
s.minLon = bbox.Min.X
|
|
|
|
s.maxLat = bbox.Max.Y
|
|
|
|
s.maxLon = bbox.Max.X
|
|
|
|
} else {
|
|
|
|
s.o = o
|
|
|
|
}
|
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
|
|
|
|
}
|
|
|
|
s.roam.pattern = globIsGlob(s.roam.id)
|
|
|
|
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
|
|
|
|
}
|
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"}
|
2016-04-19 15:38:02 +03:00
|
|
|
var withinOrIntersectsTypes = []string{"geo", "bounds", "hash", "tile", "quadkey", "get", "object"}
|
2016-03-19 17:16:19 +03:00
|
|
|
|
2016-03-29 00:16:21 +03:00
|
|
|
func (c *Controller) cmdNearby(msg *server.Message) (res string, err error) {
|
2016-03-05 02:08:16 +03:00
|
|
|
start := time.Now()
|
2016-03-29 00:16:21 +03:00
|
|
|
vs := msg.Values[1:]
|
2016-03-05 02:08:16 +03:00
|
|
|
wr := &bytes.Buffer{}
|
2016-03-29 00:16:21 +03:00
|
|
|
s, err := c.cmdSearchArgs("nearby", vs, nearbyTypes)
|
2016-03-05 02:08:16 +03:00
|
|
|
if err != nil {
|
2016-03-29 00:16:21 +03:00
|
|
|
return "", err
|
2016-03-05 02:08:16 +03:00
|
|
|
}
|
|
|
|
s.cmd = "nearby"
|
|
|
|
if s.fence {
|
2016-03-29 00:16:21 +03:00
|
|
|
return "", s
|
2016-03-05 02:08:16 +03:00
|
|
|
}
|
2016-03-29 00:16:21 +03:00
|
|
|
sw, err := c.newScanWriter(wr, msg, s.key, s.output, s.precision, s.glob, s.limit, s.wheres, s.nofields)
|
2016-03-05 02:08:16 +03:00
|
|
|
if err != nil {
|
2016-03-29 00:16:21 +03:00
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
if msg.OutputType == server.JSON {
|
|
|
|
wr.WriteString(`{"ok":true`)
|
2016-03-05 02:08:16 +03:00
|
|
|
}
|
|
|
|
sw.writeHead()
|
|
|
|
if sw.col != nil {
|
|
|
|
s.cursor = sw.col.Nearby(s.cursor, s.sparse, s.lat, s.lon, s.meters, func(id string, o geojson.Object, fields []float64) bool {
|
2016-04-03 00:13:20 +03:00
|
|
|
return sw.writeObject(id, o, fields, false)
|
2016-03-05 02:08:16 +03:00
|
|
|
})
|
|
|
|
}
|
|
|
|
sw.writeFoot(s.cursor)
|
2016-03-29 00:16:21 +03:00
|
|
|
if msg.OutputType == server.JSON {
|
|
|
|
wr.WriteString(`,"elapsed":"` + time.Now().Sub(start).String() + "\"}")
|
|
|
|
}
|
|
|
|
return string(wr.Bytes()), nil
|
2016-03-05 02:08:16 +03:00
|
|
|
}
|
|
|
|
|
2016-03-29 00:16:21 +03:00
|
|
|
func (c *Controller) cmdWithin(msg *server.Message) (res string, err error) {
|
|
|
|
return c.cmdWithinOrIntersects("within", msg)
|
2016-03-05 02:08:16 +03:00
|
|
|
}
|
|
|
|
|
2016-03-29 00:16:21 +03:00
|
|
|
func (c *Controller) cmdIntersects(msg *server.Message) (res string, err error) {
|
|
|
|
return c.cmdWithinOrIntersects("intersects", msg)
|
2016-03-05 02:08:16 +03:00
|
|
|
}
|
|
|
|
|
2016-03-29 00:16:21 +03:00
|
|
|
func (c *Controller) cmdWithinOrIntersects(cmd string, msg *server.Message) (res string, err error) {
|
2016-03-05 02:08:16 +03:00
|
|
|
start := time.Now()
|
2016-03-29 00:16:21 +03:00
|
|
|
vs := msg.Values[1:]
|
|
|
|
|
2016-03-05 02:08:16 +03:00
|
|
|
wr := &bytes.Buffer{}
|
2016-03-29 00:16:21 +03:00
|
|
|
s, err := c.cmdSearchArgs(cmd, vs, withinOrIntersectsTypes)
|
2016-03-05 02:08:16 +03:00
|
|
|
if err != nil {
|
2016-03-29 00:16:21 +03:00
|
|
|
return "", err
|
2016-03-05 02:08:16 +03:00
|
|
|
}
|
|
|
|
s.cmd = cmd
|
|
|
|
if s.fence {
|
2016-03-29 00:16:21 +03:00
|
|
|
return "", s
|
2016-03-05 02:08:16 +03:00
|
|
|
}
|
2016-03-29 00:16:21 +03:00
|
|
|
sw, err := c.newScanWriter(wr, msg, s.key, s.output, s.precision, s.glob, s.limit, s.wheres, s.nofields)
|
2016-03-05 02:08:16 +03:00
|
|
|
if err != nil {
|
2016-03-29 00:16:21 +03:00
|
|
|
return "", err
|
2016-03-05 02:08:16 +03:00
|
|
|
}
|
2016-04-19 15:38:02 +03:00
|
|
|
if sw.col == nil {
|
|
|
|
return "", errKeyNotFound
|
|
|
|
}
|
2016-03-05 02:08:16 +03:00
|
|
|
wr.WriteString(`{"ok":true`)
|
|
|
|
sw.writeHead()
|
|
|
|
if cmd == "within" {
|
|
|
|
s.cursor = sw.col.Within(s.cursor, s.sparse, s.o, s.minLat, s.minLon, s.maxLat, s.maxLon,
|
|
|
|
func(id string, o geojson.Object, fields []float64) bool {
|
2016-04-03 00:13:20 +03:00
|
|
|
return sw.writeObject(id, o, fields, false)
|
2016-03-05 02:08:16 +03:00
|
|
|
},
|
|
|
|
)
|
|
|
|
} else if cmd == "intersects" {
|
|
|
|
s.cursor = sw.col.Intersects(s.cursor, s.sparse, s.o, s.minLat, s.minLon, s.maxLat, s.maxLon,
|
|
|
|
func(id string, o geojson.Object, fields []float64) bool {
|
2016-04-03 00:13:20 +03:00
|
|
|
return sw.writeObject(id, o, fields, false)
|
2016-03-05 02:08:16 +03:00
|
|
|
},
|
|
|
|
)
|
|
|
|
}
|
|
|
|
sw.writeFoot(s.cursor)
|
|
|
|
wr.WriteString(`,"elapsed":"` + time.Now().Sub(start).String() + "\"}")
|
2016-03-29 00:16:21 +03:00
|
|
|
return string(wr.Bytes()), nil
|
2016-03-05 02:08:16 +03:00
|
|
|
}
|