2018-10-29 01:49:45 +03:00
|
|
|
package server
|
2016-03-05 02:08:16 +03:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2018-02-15 22:08:27 +03:00
|
|
|
"errors"
|
2021-09-05 12:48:34 +03:00
|
|
|
"fmt"
|
2016-03-05 02:08:16 +03:00
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
2021-09-05 12:48:34 +03:00
|
|
|
"github.com/iwpnd/sectr"
|
2018-10-11 00:25:40 +03:00
|
|
|
"github.com/mmcloughlin/geohash"
|
|
|
|
"github.com/tidwall/geojson"
|
|
|
|
"github.com/tidwall/geojson/geometry"
|
2016-03-29 00:16:21 +03:00
|
|
|
"github.com/tidwall/resp"
|
2018-10-11 00:25:40 +03:00
|
|
|
"github.com/tidwall/tile38/internal/bing"
|
2021-12-10 04:14:50 +03:00
|
|
|
"github.com/tidwall/tile38/internal/buffer"
|
2021-07-11 20:09:51 +03:00
|
|
|
"github.com/tidwall/tile38/internal/clip"
|
2022-09-20 03:47:38 +03:00
|
|
|
"github.com/tidwall/tile38/internal/field"
|
2018-10-11 00:25:40 +03:00
|
|
|
"github.com/tidwall/tile38/internal/glob"
|
2016-03-05 02:08:16 +03:00
|
|
|
)
|
|
|
|
|
2018-10-11 00:25:40 +03:00
|
|
|
const defaultCircleSteps = 64
|
|
|
|
|
2016-03-05 02:08:16 +03:00
|
|
|
type liveFenceSwitches struct {
|
|
|
|
searchScanBaseTokens
|
2021-08-20 15:00:14 +03:00
|
|
|
obj geojson.Object
|
|
|
|
cmd string
|
|
|
|
roam roamSwitches
|
2016-05-23 23:01:42 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
type roamSwitches struct {
|
|
|
|
on bool
|
|
|
|
key string
|
|
|
|
id string
|
|
|
|
pattern bool
|
|
|
|
meters float64
|
2016-12-15 21:37:38 +03:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2021-12-09 19:24:26 +03:00
|
|
|
func (lfs liveFenceSwitches) Error() string {
|
2018-08-14 03:05:30 +03:00
|
|
|
return goingLive
|
2016-03-05 02:08:16 +03:00
|
|
|
}
|
|
|
|
|
2021-12-09 19:24:26 +03:00
|
|
|
func (lfs liveFenceSwitches) Close() {
|
|
|
|
for _, whereeval := range lfs.whereevals {
|
2018-02-15 22:08:27 +03:00
|
|
|
whereeval.Close()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-09 19:24:26 +03:00
|
|
|
func (lfs liveFenceSwitches) usingLua() bool {
|
|
|
|
return len(lfs.whereevals) > 0
|
2018-03-05 21:10:40 +03:00
|
|
|
}
|
|
|
|
|
2020-03-20 08:09:24 +03:00
|
|
|
func parseRectArea(ltyp string, vs []string) (nvs []string, rect *geojson.Rect, err error) {
|
|
|
|
|
|
|
|
var ok bool
|
|
|
|
|
|
|
|
switch ltyp {
|
|
|
|
default:
|
|
|
|
err = errNotRectangle
|
|
|
|
return
|
|
|
|
case "bounds":
|
|
|
|
var sminLat, sminLon, smaxlat, smaxlon string
|
|
|
|
if vs, sminLat, ok = tokenval(vs); !ok || sminLat == "" {
|
|
|
|
err = errInvalidNumberOfArguments
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if vs, sminLon, ok = tokenval(vs); !ok || sminLon == "" {
|
|
|
|
err = errInvalidNumberOfArguments
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if vs, smaxlat, ok = tokenval(vs); !ok || smaxlat == "" {
|
|
|
|
err = errInvalidNumberOfArguments
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if vs, smaxlon, ok = tokenval(vs); !ok || smaxlon == "" {
|
|
|
|
err = errInvalidNumberOfArguments
|
|
|
|
return
|
|
|
|
}
|
|
|
|
var minLat, minLon, maxLat, maxLon float64
|
|
|
|
if minLat, err = strconv.ParseFloat(sminLat, 64); err != nil {
|
|
|
|
err = errInvalidArgument(sminLat)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if minLon, err = strconv.ParseFloat(sminLon, 64); err != nil {
|
|
|
|
err = errInvalidArgument(sminLon)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if maxLat, err = strconv.ParseFloat(smaxlat, 64); err != nil {
|
|
|
|
err = errInvalidArgument(smaxlat)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if maxLon, err = strconv.ParseFloat(smaxlon, 64); err != nil {
|
|
|
|
err = errInvalidArgument(smaxlon)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
rect = geojson.NewRect(geometry.Rect{
|
|
|
|
Min: geometry.Point{X: minLon, Y: minLat},
|
|
|
|
Max: geometry.Point{X: maxLon, Y: maxLat},
|
|
|
|
})
|
|
|
|
case "hash":
|
|
|
|
var hash string
|
|
|
|
if vs, hash, ok = tokenval(vs); !ok || hash == "" {
|
|
|
|
err = errInvalidNumberOfArguments
|
|
|
|
return
|
|
|
|
}
|
|
|
|
box := geohash.BoundingBox(hash)
|
|
|
|
rect = geojson.NewRect(geometry.Rect{
|
|
|
|
Min: geometry.Point{X: box.MinLng, Y: box.MinLat},
|
|
|
|
Max: geometry.Point{X: box.MaxLng, Y: box.MaxLat},
|
|
|
|
})
|
|
|
|
case "quadkey":
|
|
|
|
var key string
|
|
|
|
if vs, key, ok = tokenval(vs); !ok || key == "" {
|
|
|
|
err = errInvalidNumberOfArguments
|
|
|
|
return
|
|
|
|
}
|
|
|
|
var minLat, minLon, maxLat, maxLon float64
|
|
|
|
minLat, minLon, maxLat, maxLon, err = bing.QuadKeyToBounds(key)
|
|
|
|
if err != nil {
|
|
|
|
err = errInvalidArgument(key)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
rect = geojson.NewRect(geometry.Rect{
|
|
|
|
Min: geometry.Point{X: minLon, Y: minLat},
|
|
|
|
Max: geometry.Point{X: maxLon, Y: maxLat},
|
|
|
|
})
|
|
|
|
case "tile":
|
|
|
|
var sx, sy, sz string
|
|
|
|
if vs, sx, ok = tokenval(vs); !ok || sx == "" {
|
|
|
|
err = errInvalidNumberOfArguments
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if vs, sy, ok = tokenval(vs); !ok || sy == "" {
|
|
|
|
err = errInvalidNumberOfArguments
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if vs, sz, ok = tokenval(vs); !ok || sz == "" {
|
|
|
|
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)
|
|
|
|
rect = geojson.NewRect(geometry.Rect{
|
|
|
|
Min: geometry.Point{X: minLon, Y: minLat},
|
|
|
|
Max: geometry.Point{X: maxLon, Y: maxLat},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
nvs = vs
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-12-09 19:24:26 +03:00
|
|
|
func (s *Server) cmdSearchArgs(
|
2021-12-10 04:14:50 +03:00
|
|
|
fromFenceCmd bool, cmd string, vs []string, types map[string]bool,
|
2021-12-09 19:24:26 +03:00
|
|
|
) (lfs liveFenceSwitches, err error) {
|
2018-08-14 03:05:30 +03:00
|
|
|
var t searchScanBaseTokens
|
|
|
|
if fromFenceCmd {
|
|
|
|
t.fence = true
|
|
|
|
}
|
2021-12-09 19:24:26 +03:00
|
|
|
vs, t, err = s.parseSearchScanBaseTokens(cmd, t, vs)
|
2018-08-14 03:05:30 +03:00
|
|
|
if err != nil {
|
2016-03-05 02:08:16 +03:00
|
|
|
return
|
|
|
|
}
|
2021-12-09 19:24:26 +03:00
|
|
|
lfs.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
|
|
|
|
}
|
2021-12-09 19:24:26 +03:00
|
|
|
if lfs.searchScanBaseTokens.output == outputBounds {
|
2016-03-05 02:08:16 +03:00
|
|
|
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.
|
2021-12-09 19:24:26 +03:00
|
|
|
lfs.searchScanBaseTokens.output = defaultSearchOutput
|
2018-10-29 01:49:45 +03:00
|
|
|
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)
|
2021-12-10 04:14:50 +03:00
|
|
|
found := types[ltyp]
|
2021-12-09 19:24:26 +03:00
|
|
|
if !found && lfs.searchScanBaseTokens.fence && ltyp == "roam" && cmd == "nearby" {
|
2016-05-23 23:01:42 +03:00
|
|
|
// 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
|
|
|
|
}
|
2018-10-11 00:25:40 +03:00
|
|
|
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
|
|
|
|
}
|
2018-10-11 00:25:40 +03:00
|
|
|
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 != "" {
|
2021-12-10 04:14:50 +03:00
|
|
|
meters, err = strconv.ParseFloat(smeters, 64)
|
|
|
|
if err != nil || meters < 0 {
|
2018-10-26 02:37:06 +03:00
|
|
|
err = errInvalidArgument(smeters)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
meters = -1
|
|
|
|
}
|
2021-12-10 04:14:50 +03:00
|
|
|
// Nearby used the Circle type
|
|
|
|
lfs.obj = geojson.NewCircle(geometry.Point{X: lon, Y: lat}, meters, defaultCircleSteps)
|
2018-10-26 02:37:06 +03:00
|
|
|
} else {
|
2021-12-10 04:14:50 +03:00
|
|
|
// Intersects and Within use the Point type
|
|
|
|
lfs.obj = geojson.NewPoint(geometry.Point{X: lon, Y: lat})
|
|
|
|
}
|
|
|
|
case "circle":
|
|
|
|
if lfs.clip {
|
|
|
|
err = errInvalidArgument("cannot clip with " + ltyp)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
var slat, slon, smeters string
|
|
|
|
if vs, slat, ok = tokenval(vs); !ok || slat == "" {
|
|
|
|
err = errInvalidNumberOfArguments
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if vs, slon, ok = tokenval(vs); !ok || slon == "" {
|
|
|
|
err = errInvalidNumberOfArguments
|
|
|
|
return
|
|
|
|
}
|
|
|
|
var lat, lon, meters float64
|
|
|
|
if lat, err = strconv.ParseFloat(slat, 64); err != nil {
|
|
|
|
err = errInvalidArgument(slat)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if lon, err = strconv.ParseFloat(slon, 64); err != nil {
|
|
|
|
err = errInvalidArgument(slon)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if vs, smeters, ok = tokenval(vs); !ok || smeters == "" {
|
|
|
|
err = errInvalidNumberOfArguments
|
|
|
|
return
|
|
|
|
}
|
|
|
|
meters, err = strconv.ParseFloat(smeters, 64)
|
|
|
|
if err != nil || meters < 0 {
|
|
|
|
err = errInvalidArgument(smeters)
|
|
|
|
return
|
2018-06-08 19:52:26 +03:00
|
|
|
}
|
2021-12-09 19:24:26 +03:00
|
|
|
lfs.obj = geojson.NewCircle(geometry.Point{X: lon, Y: lat}, meters, defaultCircleSteps)
|
2016-03-05 02:08:16 +03:00
|
|
|
case "object":
|
2021-12-09 19:24:26 +03:00
|
|
|
if lfs.clip {
|
2019-02-09 00:56:07 +03:00
|
|
|
err = errInvalidArgument("cannot clip with object")
|
2018-10-11 00:25:40 +03:00
|
|
|
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
|
|
|
|
}
|
2021-12-09 19:24:26 +03:00
|
|
|
lfs.obj, err = geojson.Parse(obj, &s.geomParseOpts)
|
2016-03-05 02:08:16 +03:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2021-09-05 13:05:15 +03:00
|
|
|
case "sector":
|
2021-12-09 19:24:26 +03:00
|
|
|
if lfs.clip {
|
2021-09-05 13:05:15 +03:00
|
|
|
err = errInvalidArgument("cannot clip with " + ltyp)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
var slat, slon, smeters, sb1, sb2 string
|
|
|
|
if vs, slat, ok = tokenval(vs); !ok || slat == "" {
|
|
|
|
err = errInvalidNumberOfArguments
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if vs, slon, ok = tokenval(vs); !ok || slon == "" {
|
|
|
|
err = errInvalidNumberOfArguments
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if vs, smeters, ok = tokenval(vs); !ok || smeters == "" {
|
|
|
|
err = errInvalidNumberOfArguments
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if vs, sb1, ok = tokenval(vs); !ok || sb1 == "" {
|
|
|
|
err = errInvalidNumberOfArguments
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if vs, sb2, ok = tokenval(vs); !ok || sb2 == "" {
|
|
|
|
err = errInvalidNumberOfArguments
|
|
|
|
return
|
|
|
|
}
|
|
|
|
var lat, lon, meters, b1, b2 float64
|
|
|
|
if lat, err = strconv.ParseFloat(slat, 64); err != nil {
|
|
|
|
err = errInvalidArgument(slat)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if lon, err = strconv.ParseFloat(slon, 64); err != nil {
|
|
|
|
err = errInvalidArgument(slon)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if meters, err = strconv.ParseFloat(smeters, 64); err != nil {
|
|
|
|
err = errInvalidArgument(smeters)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if b1, err = strconv.ParseFloat(sb1, 64); err != nil {
|
|
|
|
err = errInvalidArgument(sb1)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if b2, err = strconv.ParseFloat(sb2, 64); err != nil {
|
|
|
|
err = errInvalidArgument(sb2)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if b1 == b2 {
|
|
|
|
err = fmt.Errorf("equal bearings (%s == %s), use CIRCLE instead", sb1, sb2)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
origin := sectr.Point{Lng: lon, Lat: lat}
|
|
|
|
sector := sectr.NewSector(origin, meters, b1, b2)
|
|
|
|
|
2021-12-09 19:24:26 +03:00
|
|
|
lfs.obj, err = geojson.Parse(string(sector.JSON()), &s.geomParseOpts)
|
2021-09-05 13:05:15 +03:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2020-03-20 08:09:24 +03:00
|
|
|
case "bounds", "hash", "tile", "quadkey":
|
2021-12-09 19:24:26 +03:00
|
|
|
vs, lfs.obj, err = parseRectArea(ltyp, vs)
|
2018-10-11 00:25:40 +03:00
|
|
|
if err != nil {
|
2016-03-05 02:08:16 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
case "get":
|
2021-12-09 19:24:26 +03:00
|
|
|
if lfs.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
|
|
|
|
}
|
2022-09-13 18:16:41 +03:00
|
|
|
col, _ := s.cols.Get(key)
|
2016-03-05 02:08:16 +03:00
|
|
|
if col == nil {
|
|
|
|
err = errKeyNotFound
|
|
|
|
return
|
|
|
|
}
|
2021-12-09 19:24:26 +03:00
|
|
|
lfs.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":
|
2021-12-09 19:24:26 +03:00
|
|
|
lfs.roam.on = true
|
|
|
|
if vs, lfs.roam.key, ok = tokenval(vs); !ok || lfs.roam.key == "" {
|
2016-05-23 23:01:42 +03:00
|
|
|
err = errInvalidNumberOfArguments
|
|
|
|
return
|
|
|
|
}
|
2021-12-09 19:24:26 +03:00
|
|
|
if vs, lfs.roam.id, ok = tokenval(vs); !ok || lfs.roam.id == "" {
|
2016-05-23 23:01:42 +03:00
|
|
|
err = errInvalidNumberOfArguments
|
|
|
|
return
|
|
|
|
}
|
2021-12-09 19:24:26 +03:00
|
|
|
lfs.roam.pattern = glob.IsGlob(lfs.roam.id)
|
2016-05-23 23:01:42 +03:00
|
|
|
var smeters string
|
|
|
|
if vs, smeters, ok = tokenval(vs); !ok || smeters == "" {
|
|
|
|
err = errInvalidNumberOfArguments
|
|
|
|
return
|
|
|
|
}
|
2021-12-09 19:24:26 +03:00
|
|
|
if lfs.roam.meters, err = strconv.ParseFloat(smeters, 64); err != nil {
|
2016-05-23 23:01:42 +03:00
|
|
|
err = errInvalidArgument(smeters)
|
|
|
|
return
|
|
|
|
}
|
2016-12-15 21:37:38 +03:00
|
|
|
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
|
|
|
|
}
|
2021-12-09 19:24:26 +03:00
|
|
|
lfs.roam.scan = scan
|
2016-12-15 21:37:38 +03:00
|
|
|
}
|
2016-03-05 02:08:16 +03:00
|
|
|
}
|
2020-03-20 08:09:24 +03:00
|
|
|
|
|
|
|
var clip_rect *geojson.Rect
|
|
|
|
var tok, ltok string
|
|
|
|
for len(vs) > 0 {
|
|
|
|
if vs, tok, ok = tokenval(vs); !ok || tok == "" {
|
|
|
|
err = errInvalidNumberOfArguments
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if strings.ToLower(tok) != "clipby" {
|
|
|
|
err = errInvalidNumberOfArguments
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if vs, tok, ok = tokenval(vs); !ok || tok == "" {
|
|
|
|
err = errInvalidNumberOfArguments
|
|
|
|
return
|
|
|
|
}
|
|
|
|
ltok = strings.ToLower(tok)
|
|
|
|
switch ltok {
|
|
|
|
case "bounds", "hash", "tile", "quadkey":
|
|
|
|
vs, clip_rect, err = parseRectArea(ltok, vs)
|
|
|
|
if err == errNotRectangle {
|
|
|
|
err = errInvalidArgument("cannot clipby " + ltok)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2021-12-09 19:24:26 +03:00
|
|
|
lfs.obj = clip.Clip(lfs.obj, clip_rect, &s.geomIndexOpts)
|
2020-03-20 08:09:24 +03:00
|
|
|
default:
|
|
|
|
err = errInvalidArgument("cannot clipby " + ltok)
|
|
|
|
return
|
|
|
|
}
|
2016-03-05 02:08:16 +03:00
|
|
|
}
|
2021-12-10 04:14:50 +03:00
|
|
|
|
|
|
|
if lfs.hasbuffer {
|
|
|
|
lfs.obj, err = buffer.Simple(lfs.obj, lfs.buffer)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2016-03-05 02:08:16 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-12-10 04:14:50 +03:00
|
|
|
var nearbyTypes = map[string]bool{
|
|
|
|
"point": true,
|
|
|
|
}
|
|
|
|
var withinOrIntersectsTypes = map[string]bool{
|
|
|
|
"geo": true, "bounds": true, "hash": true, "tile": true, "quadkey": true,
|
|
|
|
"get": true, "object": true, "circle": true, "point": true, "sector": true,
|
|
|
|
}
|
2016-03-19 17:16:19 +03:00
|
|
|
|
2021-12-09 19:24:26 +03:00
|
|
|
func (s *Server) cmdNearby(msg *Message) (res resp.Value, err error) {
|
2016-03-05 02:08:16 +03:00
|
|
|
start := time.Now()
|
2018-10-29 01:49:45 +03:00
|
|
|
vs := msg.Args[1:]
|
2016-03-05 02:08:16 +03:00
|
|
|
wr := &bytes.Buffer{}
|
2021-12-09 19:24:26 +03:00
|
|
|
sargs, err := s.cmdSearchArgs(false, "nearby", vs, nearbyTypes)
|
|
|
|
if sargs.usingLua() {
|
|
|
|
defer sargs.Close()
|
2018-03-05 21:10:40 +03:00
|
|
|
defer func() {
|
|
|
|
if r := recover(); r != nil {
|
2018-10-29 01:49:45 +03:00
|
|
|
res = NOMessage
|
2018-03-05 21:10:40 +03:00
|
|
|
err = errors.New(r.(string))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
2016-03-05 02:08:16 +03:00
|
|
|
if err != nil {
|
2018-10-29 01:49:45 +03:00
|
|
|
return NOMessage, err
|
2016-03-05 02:08:16 +03:00
|
|
|
}
|
2021-12-09 19:24:26 +03:00
|
|
|
sargs.cmd = "nearby"
|
|
|
|
if sargs.fence {
|
|
|
|
return NOMessage, sargs
|
2016-03-05 02:08:16 +03:00
|
|
|
}
|
2021-12-09 19:24:26 +03:00
|
|
|
sw, err := s.newScanWriter(
|
2022-09-02 05:43:30 +03:00
|
|
|
wr, msg, sargs.key, sargs.output, sargs.precision, sargs.globs, false,
|
2021-12-09 19:24:26 +03:00
|
|
|
sargs.cursor, sargs.limit, sargs.wheres, sargs.whereins, sargs.whereevals, sargs.nofields)
|
2016-03-05 02:08:16 +03:00
|
|
|
if err != nil {
|
2018-10-29 01:49:45 +03:00
|
|
|
return NOMessage, err
|
2016-03-29 00:16:21 +03:00
|
|
|
}
|
2018-10-29 01:49:45 +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
|
|
|
}
|
2022-09-20 03:47:38 +03:00
|
|
|
var ierr error
|
2016-03-05 02:08:16 +03:00
|
|
|
if sw.col != nil {
|
2022-09-20 03:47:38 +03:00
|
|
|
iterStep := func(id string, o geojson.Object, fields field.List, meters float64) bool {
|
|
|
|
keepGoing, err := sw.pushObject(ScanWriterParams{
|
2021-07-11 20:09:51 +03:00
|
|
|
id: id,
|
|
|
|
o: o,
|
|
|
|
fields: fields,
|
|
|
|
distance: meters,
|
2021-12-09 19:24:26 +03:00
|
|
|
distOutput: sargs.distance,
|
2021-07-11 20:09:51 +03:00
|
|
|
ignoreGlobMatch: true,
|
|
|
|
skipTesting: true,
|
2018-10-11 00:25:40 +03:00
|
|
|
})
|
2022-09-20 03:47:38 +03:00
|
|
|
if err != nil {
|
|
|
|
ierr = err
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return keepGoing
|
2017-01-31 02:41:12 +03:00
|
|
|
}
|
2021-12-09 19:24:26 +03:00
|
|
|
maxDist := sargs.obj.(*geojson.Circle).Meters()
|
|
|
|
if sargs.sparse > 0 {
|
2021-07-22 18:39:57 +03:00
|
|
|
if maxDist < 0 {
|
|
|
|
// error cannot use SPARSE and KNN together
|
|
|
|
return NOMessage,
|
|
|
|
errors.New("cannot use SPARSE without a point distance")
|
|
|
|
}
|
|
|
|
// An intersects operation is required for SPARSE
|
2022-09-20 03:47:38 +03:00
|
|
|
iter := func(id string, o geojson.Object, fields field.List) bool {
|
2021-07-22 18:39:57 +03:00
|
|
|
var meters float64
|
2021-12-09 19:24:26 +03:00
|
|
|
if sargs.distance {
|
|
|
|
meters = o.Distance(sargs.obj)
|
2021-07-22 18:39:57 +03:00
|
|
|
}
|
|
|
|
return iterStep(id, o, fields, meters)
|
|
|
|
}
|
2021-12-09 19:24:26 +03:00
|
|
|
sw.col.Intersects(sargs.obj, sargs.sparse, sw, msg.Deadline, iter)
|
2021-07-22 18:39:57 +03:00
|
|
|
} else {
|
2022-09-20 03:47:38 +03:00
|
|
|
iter := func(id string, o geojson.Object, fields field.List, dist float64) bool {
|
2021-07-22 18:39:57 +03:00
|
|
|
if maxDist > 0 && dist > maxDist {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
var meters float64
|
2021-12-09 19:24:26 +03:00
|
|
|
if sargs.distance {
|
2021-07-22 18:39:57 +03:00
|
|
|
meters = dist
|
|
|
|
}
|
|
|
|
return iterStep(id, o, fields, meters)
|
|
|
|
}
|
2021-12-09 19:24:26 +03:00
|
|
|
sw.col.Nearby(sargs.obj, sw, msg.Deadline, iter)
|
2021-07-22 18:39:57 +03:00
|
|
|
}
|
2016-03-05 02:08:16 +03:00
|
|
|
}
|
2022-09-20 03:47:38 +03:00
|
|
|
if ierr != nil {
|
|
|
|
return retrerr(ierr)
|
|
|
|
}
|
2017-07-24 18:26:10 +03:00
|
|
|
sw.writeFoot()
|
2018-10-29 01:49:45 +03:00
|
|
|
if msg.OutputType == JSON {
|
2021-03-31 18:13:44 +03:00
|
|
|
wr.WriteString(`,"elapsed":"` + time.Since(start).String() + "\"}")
|
Lua scripting feature. (#224)
* Start on lua scripting
* Implement evalsha, script load, script exists, and script flush
* Type conversions from lua to resp/json.
Refactor to make luastate and luascripts persistent in the controller.
* Change controller.command and all underlying commands to return resp.Value.
Serialize only during the ouput.
* First stab at tile38 call from lua
* Change tile38 into tile38.call in Lua
* Property return errors from scripts
* Minor refactoring. No locking on script run
* Cleanup/refactoring
* Create a pool of 5 lua states, allow for more as needed. Refactor.
* Use safe map for scripts. Add a limit for max number of lua states. Refactor.
* Refactor
* Refactor script commands into atomic, read-only, and non-atomic classes.
Proper locking for all three classes.
Add tests for scripts
* More tests for scripts
* Properly escape newlines in lua-produced errors
* Better test for readonly failure
* Correctly convert ok/err messages between lua and resp.
Add pcall, sha1hex, error_reply, status_reply functions to tile38 namespace in lua.
* Add pcall test. Change writeErr to work with string argument
* Make sure eval/evalsha never attempt to write AOF
* Add eval-set and eval-get to benchmarks
* Fix eval benchmark tests, add more
* Improve benchmarks
* Optimizations and refactoring.
* Add lua memtest
* Typo
* Add dependency
* golint fixes
* gofmt fixes
* Add scripting commands to the core/commands.json
* Use ARGV for args inside lua
2017-10-05 18:20:40 +03:00
|
|
|
return resp.BytesValue(wr.Bytes()), nil
|
2016-03-29 00:16:21 +03:00
|
|
|
}
|
Lua scripting feature. (#224)
* Start on lua scripting
* Implement evalsha, script load, script exists, and script flush
* Type conversions from lua to resp/json.
Refactor to make luastate and luascripts persistent in the controller.
* Change controller.command and all underlying commands to return resp.Value.
Serialize only during the ouput.
* First stab at tile38 call from lua
* Change tile38 into tile38.call in Lua
* Property return errors from scripts
* Minor refactoring. No locking on script run
* Cleanup/refactoring
* Create a pool of 5 lua states, allow for more as needed. Refactor.
* Use safe map for scripts. Add a limit for max number of lua states. Refactor.
* Refactor
* Refactor script commands into atomic, read-only, and non-atomic classes.
Proper locking for all three classes.
Add tests for scripts
* More tests for scripts
* Properly escape newlines in lua-produced errors
* Better test for readonly failure
* Correctly convert ok/err messages between lua and resp.
Add pcall, sha1hex, error_reply, status_reply functions to tile38 namespace in lua.
* Add pcall test. Change writeErr to work with string argument
* Make sure eval/evalsha never attempt to write AOF
* Add eval-set and eval-get to benchmarks
* Fix eval benchmark tests, add more
* Improve benchmarks
* Optimizations and refactoring.
* Add lua memtest
* Typo
* Add dependency
* golint fixes
* gofmt fixes
* Add scripting commands to the core/commands.json
* Use ARGV for args inside lua
2017-10-05 18:20:40 +03:00
|
|
|
return sw.respOut, nil
|
2016-03-05 02:08:16 +03:00
|
|
|
}
|
|
|
|
|
2022-09-20 03:47:38 +03:00
|
|
|
func (s *Server) cmdWITHIN(msg *Message) (res resp.Value, err error) {
|
|
|
|
return s.cmdWITHINorINTERSECTS("within", msg)
|
2016-03-05 02:08:16 +03:00
|
|
|
}
|
|
|
|
|
2022-09-20 03:47:38 +03:00
|
|
|
func (s *Server) cmdINTERSECTS(msg *Message) (res resp.Value, err error) {
|
|
|
|
return s.cmdWITHINorINTERSECTS("intersects", msg)
|
2016-03-05 02:08:16 +03:00
|
|
|
}
|
|
|
|
|
2022-09-20 03:47:38 +03:00
|
|
|
func (s *Server) cmdWITHINorINTERSECTS(cmd string, msg *Message) (res resp.Value, err error) {
|
2016-03-05 02:08:16 +03:00
|
|
|
start := time.Now()
|
2018-10-29 01:49:45 +03:00
|
|
|
vs := msg.Args[1:]
|
2016-03-29 00:16:21 +03:00
|
|
|
|
2016-03-05 02:08:16 +03:00
|
|
|
wr := &bytes.Buffer{}
|
2021-12-09 19:24:26 +03:00
|
|
|
sargs, err := s.cmdSearchArgs(false, cmd, vs, withinOrIntersectsTypes)
|
|
|
|
if sargs.usingLua() {
|
|
|
|
defer sargs.Close()
|
2018-03-05 21:10:40 +03:00
|
|
|
defer func() {
|
|
|
|
if r := recover(); r != nil {
|
2018-10-29 01:49:45 +03:00
|
|
|
res = NOMessage
|
2018-03-05 21:10:40 +03:00
|
|
|
err = errors.New(r.(string))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
2016-03-05 02:08:16 +03:00
|
|
|
if err != nil {
|
2018-10-29 01:49:45 +03:00
|
|
|
return NOMessage, err
|
2016-03-05 02:08:16 +03:00
|
|
|
}
|
2021-12-09 19:24:26 +03:00
|
|
|
sargs.cmd = cmd
|
|
|
|
if sargs.fence {
|
|
|
|
return NOMessage, sargs
|
2016-03-05 02:08:16 +03:00
|
|
|
}
|
2021-12-09 19:24:26 +03:00
|
|
|
sw, err := s.newScanWriter(
|
2022-09-02 05:43:30 +03:00
|
|
|
wr, msg, sargs.key, sargs.output, sargs.precision, sargs.globs, false,
|
2021-12-09 19:24:26 +03:00
|
|
|
sargs.cursor, sargs.limit, sargs.wheres, sargs.whereins, sargs.whereevals, sargs.nofields)
|
2016-03-05 02:08:16 +03:00
|
|
|
if err != nil {
|
2018-10-29 01:49:45 +03:00
|
|
|
return NOMessage, err
|
2016-03-05 02:08:16 +03:00
|
|
|
}
|
2018-10-29 01:49:45 +03:00
|
|
|
if msg.OutputType == JSON {
|
2016-07-11 07:40:18 +03:00
|
|
|
wr.WriteString(`{"ok":true`)
|
|
|
|
}
|
2022-09-20 03:47:38 +03:00
|
|
|
var ierr error
|
2017-08-03 14:01:07 +03:00
|
|
|
if sw.col != nil {
|
|
|
|
if cmd == "within" {
|
2021-12-09 19:24:26 +03:00
|
|
|
sw.col.Within(sargs.obj, sargs.sparse, sw, msg.Deadline, func(
|
2022-09-20 03:47:38 +03:00
|
|
|
id string, o geojson.Object, fields field.List,
|
2018-10-11 00:25:40 +03:00
|
|
|
) bool {
|
2022-09-20 03:47:38 +03:00
|
|
|
keepGoing, err := sw.pushObject(ScanWriterParams{
|
2018-10-11 00:25:40 +03:00
|
|
|
id: id,
|
|
|
|
o: o,
|
|
|
|
fields: fields,
|
|
|
|
})
|
2022-09-20 03:47:38 +03:00
|
|
|
if err != nil {
|
|
|
|
ierr = err
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return keepGoing
|
2018-10-11 00:25:40 +03:00
|
|
|
})
|
2017-08-03 14:01:07 +03:00
|
|
|
} else if cmd == "intersects" {
|
2021-12-09 19:24:26 +03:00
|
|
|
sw.col.Intersects(sargs.obj, sargs.sparse, sw, msg.Deadline, func(
|
2018-10-11 00:25:40 +03:00
|
|
|
id string,
|
|
|
|
o geojson.Object,
|
2022-09-20 03:47:38 +03:00
|
|
|
fields field.List,
|
2018-10-11 00:25:40 +03:00
|
|
|
) bool {
|
|
|
|
params := ScanWriterParams{
|
|
|
|
id: id,
|
|
|
|
o: o,
|
|
|
|
fields: fields,
|
|
|
|
}
|
2021-12-09 19:24:26 +03:00
|
|
|
if sargs.clip {
|
|
|
|
params.clip = sargs.obj
|
2018-10-11 00:25:40 +03:00
|
|
|
}
|
2022-09-20 03:47:38 +03:00
|
|
|
keepGoing, err := sw.pushObject(params)
|
|
|
|
if err != nil {
|
|
|
|
ierr = err
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return keepGoing
|
2018-10-11 00:25:40 +03:00
|
|
|
})
|
2017-08-03 14:01:07 +03:00
|
|
|
}
|
2016-03-05 02:08:16 +03:00
|
|
|
}
|
2022-09-20 03:47:38 +03:00
|
|
|
if ierr != nil {
|
|
|
|
return retrerr(ierr)
|
|
|
|
}
|
2017-07-24 18:26:10 +03:00
|
|
|
sw.writeFoot()
|
2018-10-29 01:49:45 +03:00
|
|
|
if msg.OutputType == JSON {
|
2021-03-31 18:13:44 +03:00
|
|
|
wr.WriteString(`,"elapsed":"` + time.Since(start).String() + "\"}")
|
Lua scripting feature. (#224)
* Start on lua scripting
* Implement evalsha, script load, script exists, and script flush
* Type conversions from lua to resp/json.
Refactor to make luastate and luascripts persistent in the controller.
* Change controller.command and all underlying commands to return resp.Value.
Serialize only during the ouput.
* First stab at tile38 call from lua
* Change tile38 into tile38.call in Lua
* Property return errors from scripts
* Minor refactoring. No locking on script run
* Cleanup/refactoring
* Create a pool of 5 lua states, allow for more as needed. Refactor.
* Use safe map for scripts. Add a limit for max number of lua states. Refactor.
* Refactor
* Refactor script commands into atomic, read-only, and non-atomic classes.
Proper locking for all three classes.
Add tests for scripts
* More tests for scripts
* Properly escape newlines in lua-produced errors
* Better test for readonly failure
* Correctly convert ok/err messages between lua and resp.
Add pcall, sha1hex, error_reply, status_reply functions to tile38 namespace in lua.
* Add pcall test. Change writeErr to work with string argument
* Make sure eval/evalsha never attempt to write AOF
* Add eval-set and eval-get to benchmarks
* Fix eval benchmark tests, add more
* Improve benchmarks
* Optimizations and refactoring.
* Add lua memtest
* Typo
* Add dependency
* golint fixes
* gofmt fixes
* Add scripting commands to the core/commands.json
* Use ARGV for args inside lua
2017-10-05 18:20:40 +03:00
|
|
|
return resp.BytesValue(wr.Bytes()), nil
|
2016-07-11 07:40:18 +03:00
|
|
|
}
|
Lua scripting feature. (#224)
* Start on lua scripting
* Implement evalsha, script load, script exists, and script flush
* Type conversions from lua to resp/json.
Refactor to make luastate and luascripts persistent in the controller.
* Change controller.command and all underlying commands to return resp.Value.
Serialize only during the ouput.
* First stab at tile38 call from lua
* Change tile38 into tile38.call in Lua
* Property return errors from scripts
* Minor refactoring. No locking on script run
* Cleanup/refactoring
* Create a pool of 5 lua states, allow for more as needed. Refactor.
* Use safe map for scripts. Add a limit for max number of lua states. Refactor.
* Refactor
* Refactor script commands into atomic, read-only, and non-atomic classes.
Proper locking for all three classes.
Add tests for scripts
* More tests for scripts
* Properly escape newlines in lua-produced errors
* Better test for readonly failure
* Correctly convert ok/err messages between lua and resp.
Add pcall, sha1hex, error_reply, status_reply functions to tile38 namespace in lua.
* Add pcall test. Change writeErr to work with string argument
* Make sure eval/evalsha never attempt to write AOF
* Add eval-set and eval-get to benchmarks
* Fix eval benchmark tests, add more
* Improve benchmarks
* Optimizations and refactoring.
* Add lua memtest
* Typo
* Add dependency
* golint fixes
* gofmt fixes
* Add scripting commands to the core/commands.json
* Use ARGV for args inside lua
2017-10-05 18:20:40 +03:00
|
|
|
return sw.respOut, nil
|
2016-07-11 07:40:18 +03:00
|
|
|
}
|
|
|
|
|
2021-12-09 19:24:26 +03:00
|
|
|
func (s *Server) cmdSeachValuesArgs(vs []string) (
|
|
|
|
lfs liveFenceSwitches, err error,
|
2018-08-14 03:05:30 +03:00
|
|
|
) {
|
|
|
|
var t searchScanBaseTokens
|
2021-12-09 19:24:26 +03:00
|
|
|
vs, t, err = s.parseSearchScanBaseTokens("search", t, vs)
|
2018-08-14 03:05:30 +03:00
|
|
|
if err != nil {
|
2016-07-11 07:40:18 +03:00
|
|
|
return
|
|
|
|
}
|
2021-12-09 19:24:26 +03:00
|
|
|
lfs.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
|
|
|
|
}
|
|
|
|
|
2022-09-02 05:43:30 +03:00
|
|
|
func multiGlobParse(globs []string, desc bool) [2]string {
|
|
|
|
var limits [2]string
|
|
|
|
for i, pattern := range globs {
|
|
|
|
g := glob.Parse(pattern, desc)
|
|
|
|
if g.Limits[0] == "" && g.Limits[1] == "" {
|
|
|
|
limits[0], limits[1] = "", ""
|
|
|
|
break
|
|
|
|
}
|
|
|
|
if i == 0 {
|
|
|
|
limits[0], limits[1] = g.Limits[0], g.Limits[1]
|
|
|
|
} else if desc {
|
|
|
|
if g.Limits[0] > limits[0] {
|
|
|
|
limits[0] = g.Limits[0]
|
|
|
|
}
|
|
|
|
if g.Limits[1] < limits[1] {
|
|
|
|
limits[1] = g.Limits[1]
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if g.Limits[0] < limits[0] {
|
|
|
|
limits[0] = g.Limits[0]
|
|
|
|
}
|
|
|
|
if g.Limits[1] > limits[1] {
|
|
|
|
limits[1] = g.Limits[1]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return limits
|
|
|
|
}
|
|
|
|
|
2021-12-09 19:24:26 +03:00
|
|
|
func (s *Server) cmdSearch(msg *Message) (res resp.Value, err error) {
|
2016-07-13 06:11:02 +03:00
|
|
|
start := time.Now()
|
2018-10-29 01:49:45 +03:00
|
|
|
vs := msg.Args[1:]
|
2016-07-13 06:11:02 +03:00
|
|
|
|
2016-07-11 07:40:18 +03:00
|
|
|
wr := &bytes.Buffer{}
|
2021-12-09 19:24:26 +03:00
|
|
|
sargs, err := s.cmdSeachValuesArgs(vs)
|
|
|
|
if sargs.usingLua() {
|
|
|
|
defer sargs.Close()
|
2018-03-05 21:10:40 +03:00
|
|
|
defer func() {
|
|
|
|
if r := recover(); r != nil {
|
2018-10-29 01:49:45 +03:00
|
|
|
res = NOMessage
|
2018-03-05 21:10:40 +03:00
|
|
|
err = errors.New(r.(string))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
2016-07-13 06:11:02 +03:00
|
|
|
if err != nil {
|
2018-10-29 01:49:45 +03:00
|
|
|
return NOMessage, err
|
2016-07-13 06:11:02 +03:00
|
|
|
}
|
2021-12-09 19:24:26 +03:00
|
|
|
sw, err := s.newScanWriter(
|
2022-09-02 05:43:30 +03:00
|
|
|
wr, msg, sargs.key, sargs.output, sargs.precision, sargs.globs, true,
|
2021-12-09 19:24:26 +03:00
|
|
|
sargs.cursor, sargs.limit, sargs.wheres, sargs.whereins, sargs.whereevals, sargs.nofields)
|
2016-07-13 06:11:02 +03:00
|
|
|
if err != nil {
|
2018-10-29 01:49:45 +03:00
|
|
|
return NOMessage, err
|
2016-07-13 06:11:02 +03:00
|
|
|
}
|
2018-10-29 01:49:45 +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
|
|
|
}
|
2022-09-20 03:47:38 +03:00
|
|
|
var ierr error
|
2016-07-13 06:11:02 +03:00
|
|
|
if sw.col != nil {
|
2021-03-31 18:13:44 +03:00
|
|
|
if sw.output == outputCount && len(sw.wheres) == 0 && sw.globEverything {
|
2021-12-09 19:24:26 +03:00
|
|
|
count := sw.col.Count() - int(sargs.cursor)
|
2016-07-13 06:11:02 +03:00
|
|
|
if count < 0 {
|
|
|
|
count = 0
|
|
|
|
}
|
|
|
|
sw.count = uint64(count)
|
|
|
|
} else {
|
2022-09-02 05:43:30 +03:00
|
|
|
limits := multiGlobParse(sw.globs, sargs.desc)
|
|
|
|
if limits[0] == "" && limits[1] == "" {
|
2021-12-09 19:24:26 +03:00
|
|
|
sw.col.SearchValues(sargs.desc, sw, msg.Deadline,
|
2022-09-20 03:47:38 +03:00
|
|
|
func(id string, o geojson.Object, fields field.List) bool {
|
|
|
|
keepGoing, err := sw.pushObject(ScanWriterParams{
|
2017-01-13 19:31:35 +03:00
|
|
|
id: id,
|
|
|
|
o: o,
|
2017-01-10 19:49:48 +03:00
|
|
|
fields: fields,
|
|
|
|
})
|
2022-09-20 03:47:38 +03:00
|
|
|
if err != nil {
|
|
|
|
ierr = err
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return keepGoing
|
2016-07-13 06:11:02 +03:00
|
|
|
},
|
|
|
|
)
|
|
|
|
} else {
|
2016-11-14 21:03:54 +03:00
|
|
|
// must disable globSingle for string value type matching because
|
|
|
|
// globSingle is only for ID matches, not values.
|
2022-09-02 05:43:30 +03:00
|
|
|
sw.col.SearchValuesRange(limits[0], limits[1], sargs.desc, sw,
|
2019-04-24 15:09:41 +03:00
|
|
|
msg.Deadline,
|
2022-09-20 03:47:38 +03:00
|
|
|
func(id string, o geojson.Object, fields field.List) bool {
|
|
|
|
keepGoing, err := sw.pushObject(ScanWriterParams{
|
2017-01-13 19:31:35 +03:00
|
|
|
id: id,
|
|
|
|
o: o,
|
2017-01-10 19:49:48 +03:00
|
|
|
fields: fields,
|
|
|
|
})
|
2022-09-20 03:47:38 +03:00
|
|
|
if err != nil {
|
|
|
|
ierr = err
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return keepGoing
|
2016-07-13 06:11:02 +03:00
|
|
|
},
|
|
|
|
)
|
2016-07-11 07:40:18 +03:00
|
|
|
}
|
|
|
|
}
|
2016-07-13 06:11:02 +03:00
|
|
|
}
|
2022-09-20 03:47:38 +03:00
|
|
|
if ierr != nil {
|
|
|
|
return retrerr(ierr)
|
|
|
|
}
|
2017-07-24 18:26:10 +03:00
|
|
|
sw.writeFoot()
|
2018-10-29 01:49:45 +03:00
|
|
|
if msg.OutputType == JSON {
|
2021-03-31 18:13:44 +03:00
|
|
|
wr.WriteString(`,"elapsed":"` + time.Since(start).String() + "\"}")
|
Lua scripting feature. (#224)
* Start on lua scripting
* Implement evalsha, script load, script exists, and script flush
* Type conversions from lua to resp/json.
Refactor to make luastate and luascripts persistent in the controller.
* Change controller.command and all underlying commands to return resp.Value.
Serialize only during the ouput.
* First stab at tile38 call from lua
* Change tile38 into tile38.call in Lua
* Property return errors from scripts
* Minor refactoring. No locking on script run
* Cleanup/refactoring
* Create a pool of 5 lua states, allow for more as needed. Refactor.
* Use safe map for scripts. Add a limit for max number of lua states. Refactor.
* Refactor
* Refactor script commands into atomic, read-only, and non-atomic classes.
Proper locking for all three classes.
Add tests for scripts
* More tests for scripts
* Properly escape newlines in lua-produced errors
* Better test for readonly failure
* Correctly convert ok/err messages between lua and resp.
Add pcall, sha1hex, error_reply, status_reply functions to tile38 namespace in lua.
* Add pcall test. Change writeErr to work with string argument
* Make sure eval/evalsha never attempt to write AOF
* Add eval-set and eval-get to benchmarks
* Fix eval benchmark tests, add more
* Improve benchmarks
* Optimizations and refactoring.
* Add lua memtest
* Typo
* Add dependency
* golint fixes
* gofmt fixes
* Add scripting commands to the core/commands.json
* Use ARGV for args inside lua
2017-10-05 18:20:40 +03:00
|
|
|
return resp.BytesValue(wr.Bytes()), nil
|
2016-07-11 07:40:18 +03:00
|
|
|
}
|
Lua scripting feature. (#224)
* Start on lua scripting
* Implement evalsha, script load, script exists, and script flush
* Type conversions from lua to resp/json.
Refactor to make luastate and luascripts persistent in the controller.
* Change controller.command and all underlying commands to return resp.Value.
Serialize only during the ouput.
* First stab at tile38 call from lua
* Change tile38 into tile38.call in Lua
* Property return errors from scripts
* Minor refactoring. No locking on script run
* Cleanup/refactoring
* Create a pool of 5 lua states, allow for more as needed. Refactor.
* Use safe map for scripts. Add a limit for max number of lua states. Refactor.
* Refactor
* Refactor script commands into atomic, read-only, and non-atomic classes.
Proper locking for all three classes.
Add tests for scripts
* More tests for scripts
* Properly escape newlines in lua-produced errors
* Better test for readonly failure
* Correctly convert ok/err messages between lua and resp.
Add pcall, sha1hex, error_reply, status_reply functions to tile38 namespace in lua.
* Add pcall test. Change writeErr to work with string argument
* Make sure eval/evalsha never attempt to write AOF
* Add eval-set and eval-get to benchmarks
* Fix eval benchmark tests, add more
* Improve benchmarks
* Optimizations and refactoring.
* Add lua memtest
* Typo
* Add dependency
* golint fixes
* gofmt fixes
* Add scripting commands to the core/commands.json
* Use ARGV for args inside lua
2017-10-05 18:20:40 +03:00
|
|
|
return sw.respOut, nil
|
2016-03-05 02:08:16 +03:00
|
|
|
}
|