2016-03-05 02:08:16 +03:00
|
|
|
package controller
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2018-02-15 22:08:27 +03:00
|
|
|
"errors"
|
2017-07-26 06:23:21 +03:00
|
|
|
"sort"
|
2016-03-05 02:08:16 +03:00
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
2016-03-29 00:16:21 +03:00
|
|
|
"github.com/tidwall/resp"
|
2018-04-19 18:43:32 +03:00
|
|
|
"github.com/tidwall/tile38/pkg/bing"
|
|
|
|
"github.com/tidwall/tile38/pkg/geojson"
|
|
|
|
"github.com/tidwall/tile38/pkg/geojson/geohash"
|
|
|
|
"github.com/tidwall/tile38/pkg/glob"
|
|
|
|
"github.com/tidwall/tile38/pkg/server"
|
2016-03-05 02:08:16 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
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
|
2017-01-31 02:41:12 +03:00
|
|
|
knn bool
|
2016-12-06 20:30:48 +03:00
|
|
|
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
|
2016-12-15 21:37:38 +03:00
|
|
|
scan string
|
2018-08-14 20:48:28 +03:00
|
|
|
nearbys map[string][]roamMatch
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2018-02-15 22:08:27 +03:00
|
|
|
func (s liveFenceSwitches) Close() {
|
2018-03-05 21:10:40 +03:00
|
|
|
for _, whereeval := range s.whereevals {
|
2018-02-15 22:08:27 +03:00
|
|
|
whereeval.Close()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-05 21:10:40 +03:00
|
|
|
func (s liveFenceSwitches) usingLua() bool {
|
|
|
|
return len(s.whereevals) > 0
|
|
|
|
}
|
|
|
|
|
2018-08-14 03:05:30 +03:00
|
|
|
func (c *Controller) cmdSearchArgs(
|
|
|
|
fromFenceCmd bool, cmd string, vs []resp.Value, types []string,
|
|
|
|
) (s liveFenceSwitches, err error) {
|
|
|
|
var t searchScanBaseTokens
|
|
|
|
if fromFenceCmd {
|
|
|
|
t.fence = true
|
|
|
|
}
|
|
|
|
vs, t, err = c.parseSearchScanBaseTokens(cmd, t, vs)
|
|
|
|
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
|
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
|
|
|
|
}
|
2018-06-08 19:52:26 +03:00
|
|
|
s.meters = -1 // this will become non-negative if search is within a circle
|
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
|
|
|
|
}
|
2017-01-31 02:41:12 +03:00
|
|
|
|
2018-05-08 02:18:18 +03:00
|
|
|
if s.clip {
|
|
|
|
err = errInvalidArgument("cannnot clip with point")
|
|
|
|
}
|
|
|
|
|
2017-01-31 02:41:12 +03:00
|
|
|
umeters := true
|
2016-03-29 00:16:21 +03:00
|
|
|
if vs, smeters, ok = tokenval(vs); !ok || smeters == "" {
|
2017-01-31 02:41:12 +03:00
|
|
|
umeters = false
|
|
|
|
if cmd == "nearby" {
|
|
|
|
// possible that this is KNN search
|
|
|
|
s.knn = s.searchScanBaseTokens.ulimit && // must be true
|
2017-07-24 18:42:12 +03:00
|
|
|
!s.searchScanBaseTokens.usparse // must be false
|
2017-01-31 02:41:12 +03:00
|
|
|
}
|
|
|
|
if !s.knn {
|
|
|
|
err = errInvalidArgument(slat)
|
|
|
|
return
|
|
|
|
}
|
2016-03-05 02:08:16 +03:00
|
|
|
}
|
2017-01-31 02:41:12 +03:00
|
|
|
|
2016-03-05 02:08:16 +03:00
|
|
|
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
|
|
|
|
}
|
2017-01-31 02:41:12 +03:00
|
|
|
|
|
|
|
if umeters {
|
|
|
|
if s.meters, err = strconv.ParseFloat(smeters, 64); err != nil {
|
|
|
|
err = errInvalidArgument(smeters)
|
|
|
|
return
|
|
|
|
}
|
2018-06-08 19:52:26 +03:00
|
|
|
if s.meters < 0 {
|
|
|
|
err = errInvalidArgument(smeters)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
case "circle":
|
2018-05-08 02:18:18 +03:00
|
|
|
if s.clip {
|
|
|
|
err = errInvalidArgument("cannnot clip with circle")
|
|
|
|
}
|
|
|
|
|
2018-06-08 19:52:26 +03:00
|
|
|
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
|
|
|
|
}
|
|
|
|
if vs, smeters, ok = tokenval(vs); !ok || smeters == "" {
|
|
|
|
err = errInvalidArgument(slat)
|
|
|
|
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
|
|
|
|
}
|
|
|
|
if s.meters < 0 {
|
|
|
|
err = errInvalidArgument(smeters)
|
|
|
|
return
|
2016-03-05 02:08:16 +03:00
|
|
|
}
|
|
|
|
case "object":
|
2018-05-08 02:18:18 +03:00
|
|
|
if s.clip {
|
|
|
|
err = errInvalidArgument("cannnot clip with 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":
|
2018-05-08 02:18:18 +03:00
|
|
|
if s.clip {
|
|
|
|
err = errInvalidArgument("cannnot clip with get")
|
|
|
|
}
|
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 := 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
|
|
|
|
}
|
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
|
|
|
|
}
|
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
|
|
|
|
}
|
|
|
|
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"}
|
2018-06-08 19:52:26 +03:00
|
|
|
var withinOrIntersectsTypes = []string{
|
|
|
|
"geo", "bounds", "hash", "tile", "quadkey", "get", "object", "circle"}
|
2016-03-19 17:16:19 +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
|
|
|
func (c *Controller) cmdNearby(msg *server.Message) (res resp.Value, 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{}
|
2018-08-14 03:05:30 +03:00
|
|
|
s, err := c.cmdSearchArgs(false, "nearby", vs, nearbyTypes)
|
2018-03-05 21:10:40 +03:00
|
|
|
if s.usingLua() {
|
|
|
|
defer s.Close()
|
|
|
|
defer func() {
|
|
|
|
if r := recover(); r != nil {
|
|
|
|
res = server.NOMessage
|
|
|
|
err = errors.New(r.(string))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
2016-03-05 02:08:16 +03:00
|
|
|
if err != nil {
|
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 server.NOMessage, err
|
2016-03-05 02:08:16 +03:00
|
|
|
}
|
|
|
|
s.cmd = "nearby"
|
|
|
|
if s.fence {
|
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 server.NOMessage, s
|
2016-03-05 02:08:16 +03:00
|
|
|
}
|
2016-10-03 21:37:16 +03:00
|
|
|
minZ, maxZ := zMinMaxFromWheres(s.wheres)
|
2018-02-15 22:08:27 +03:00
|
|
|
sw, err := c.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 {
|
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 server.NOMessage, err
|
2016-03-29 00:16:21 +03:00
|
|
|
}
|
|
|
|
if msg.OutputType == server.JSON {
|
|
|
|
wr.WriteString(`{"ok":true`)
|
2016-03-05 02:08:16 +03:00
|
|
|
}
|
|
|
|
sw.writeHead()
|
|
|
|
if sw.col != nil {
|
2018-03-10 05:50:34 +03:00
|
|
|
var matched uint32
|
2017-07-26 06:23:21 +03:00
|
|
|
iter := func(id string, o geojson.Object, fields []float64, dist *float64) bool {
|
2017-01-10 19:49:48 +03:00
|
|
|
// Calculate distance if we need to
|
|
|
|
distance := 0.0
|
|
|
|
if s.distance {
|
2017-07-26 06:23:21 +03:00
|
|
|
if dist != nil {
|
|
|
|
distance = *dist
|
|
|
|
} else {
|
|
|
|
distance = o.CalculatedPoint().DistanceTo(geojson.Position{X: s.lon, Y: s.lat, Z: 0})
|
|
|
|
}
|
2017-01-10 19:49:48 +03:00
|
|
|
}
|
|
|
|
return sw.writeObject(ScanWriterParams{
|
2018-03-10 05:50:34 +03:00
|
|
|
id: id,
|
|
|
|
o: o,
|
|
|
|
fields: fields,
|
|
|
|
distance: distance,
|
|
|
|
noLock: true,
|
|
|
|
ignoreGlobMatch: s.knn,
|
2017-01-10 19:49:48 +03:00
|
|
|
})
|
2017-01-31 02:41:12 +03:00
|
|
|
}
|
|
|
|
if s.knn {
|
2018-06-08 19:45:18 +03:00
|
|
|
c.nearestNeighbors(&s, sw, s.lat, s.lon, &matched, iter)
|
2017-01-31 02:41:12 +03:00
|
|
|
} else {
|
2017-07-26 06:23:21 +03:00
|
|
|
sw.col.Nearby(s.sparse, s.lat, s.lon, s.meters, minZ, maxZ,
|
|
|
|
func(id string, o geojson.Object, fields []float64) bool {
|
2018-06-08 19:45:18 +03:00
|
|
|
if c.hasExpired(s.key, id) {
|
|
|
|
return true
|
|
|
|
}
|
2017-07-26 06:23:21 +03:00
|
|
|
return iter(id, o, fields, nil)
|
|
|
|
},
|
|
|
|
)
|
2017-01-31 02:41:12 +03:00
|
|
|
}
|
2016-03-05 02:08:16 +03:00
|
|
|
}
|
2017-07-24 18:26:10 +03:00
|
|
|
sw.writeFoot()
|
2016-03-29 00:16:21 +03:00
|
|
|
if msg.OutputType == server.JSON {
|
|
|
|
wr.WriteString(`,"elapsed":"` + time.Now().Sub(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
|
|
|
}
|
|
|
|
|
2017-07-26 06:23:21 +03:00
|
|
|
type iterItem struct {
|
|
|
|
id string
|
|
|
|
o geojson.Object
|
|
|
|
fields []float64
|
|
|
|
dist float64
|
|
|
|
}
|
|
|
|
|
2018-06-08 19:52:26 +03:00
|
|
|
func (c *Controller) nearestNeighbors(s *liveFenceSwitches, sw *scanWriter, lat, lon float64, matched *uint32,
|
|
|
|
iter func(id string, o geojson.Object, fields []float64, dist *float64) bool) {
|
2017-10-31 19:48:17 +03:00
|
|
|
limit := int(sw.cursor + sw.limit)
|
2017-07-26 06:23:21 +03:00
|
|
|
var items []iterItem
|
2018-06-08 19:52:26 +03:00
|
|
|
sw.col.NearestNeighbors(lat, lon, func(id string, o geojson.Object, fields []float64) bool {
|
|
|
|
if c.hasExpired(s.key, id) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
if _, ok := sw.fieldMatch(fields, o); !ok {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
match, keepGoing := sw.globMatch(id, o)
|
|
|
|
if !match {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
dist := o.CalculatedPoint().DistanceTo(geojson.Position{X: lon, Y: lat, Z: 0})
|
|
|
|
items = append(items, iterItem{id: id, o: o, fields: fields, dist: dist})
|
|
|
|
if !keepGoing {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return len(items) < 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 {
|
|
|
|
if !iter(item.id, item.o, item.fields, &item.dist) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
func (c *Controller) cmdWithin(msg *server.Message) (res resp.Value, err error) {
|
2016-03-29 00:16:21 +03:00
|
|
|
return c.cmdWithinOrIntersects("within", msg)
|
2016-03-05 02:08:16 +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
|
|
|
func (c *Controller) cmdIntersects(msg *server.Message) (res resp.Value, err error) {
|
2016-03-29 00:16:21 +03:00
|
|
|
return c.cmdWithinOrIntersects("intersects", msg)
|
2016-03-05 02:08:16 +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
|
|
|
func (c *Controller) cmdWithinOrIntersects(cmd string, msg *server.Message) (res resp.Value, 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{}
|
2018-08-14 03:05:30 +03:00
|
|
|
s, err := c.cmdSearchArgs(false, cmd, vs, withinOrIntersectsTypes)
|
2018-03-05 21:10:40 +03:00
|
|
|
if s.usingLua() {
|
|
|
|
defer s.Close()
|
|
|
|
defer func() {
|
|
|
|
if r := recover(); r != nil {
|
|
|
|
res = server.NOMessage
|
|
|
|
err = errors.New(r.(string))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
2016-03-05 02:08:16 +03:00
|
|
|
if err != nil {
|
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 server.NOMessage, err
|
2016-03-05 02:08:16 +03:00
|
|
|
}
|
|
|
|
s.cmd = cmd
|
|
|
|
if s.fence {
|
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 server.NOMessage, s
|
2016-03-05 02:08:16 +03:00
|
|
|
}
|
2018-02-15 22:08:27 +03:00
|
|
|
sw, err := c.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 {
|
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 server.NOMessage, err
|
2016-03-05 02:08:16 +03:00
|
|
|
}
|
2016-07-11 07:40:18 +03:00
|
|
|
if msg.OutputType == server.JSON {
|
|
|
|
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 {
|
|
|
|
minZ, maxZ := zMinMaxFromWheres(s.wheres)
|
|
|
|
if cmd == "within" {
|
2018-06-08 19:52:26 +03:00
|
|
|
sw.col.Within(s.sparse,
|
|
|
|
s.o,
|
|
|
|
s.minLat, s.minLon, s.maxLat, s.maxLon,
|
|
|
|
s.lat, s.lon, s.meters,
|
|
|
|
minZ, maxZ,
|
2017-08-03 14:01:07 +03:00
|
|
|
func(id string, o geojson.Object, fields []float64) bool {
|
|
|
|
if c.hasExpired(s.key, id) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return sw.writeObject(ScanWriterParams{
|
|
|
|
id: id,
|
|
|
|
o: o,
|
|
|
|
fields: fields,
|
2017-08-11 03:32:40 +03:00
|
|
|
noLock: true,
|
2017-08-03 14:01:07 +03:00
|
|
|
})
|
|
|
|
},
|
|
|
|
)
|
|
|
|
} else if cmd == "intersects" {
|
2018-06-08 19:52:26 +03:00
|
|
|
sw.col.Intersects(s.sparse,
|
|
|
|
s.o,
|
|
|
|
s.minLat, s.minLon, s.maxLat, s.maxLon,
|
|
|
|
s.lat, s.lon, s.meters,
|
|
|
|
minZ, maxZ,
|
2018-05-08 02:18:18 +03:00
|
|
|
s.clip,
|
|
|
|
func(id string, o geojson.Object, fields []float64, clipbox geojson.BBox) bool {
|
2017-08-03 14:01:07 +03:00
|
|
|
if c.hasExpired(s.key, id) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return sw.writeObject(ScanWriterParams{
|
2018-08-14 03:05:30 +03:00
|
|
|
id: id,
|
|
|
|
o: o,
|
|
|
|
fields: fields,
|
|
|
|
noLock: true,
|
|
|
|
clip: s.clip,
|
2018-05-08 02:18:18 +03:00
|
|
|
clipbox: clipbox,
|
2017-08-03 14:01:07 +03:00
|
|
|
})
|
|
|
|
},
|
|
|
|
)
|
|
|
|
}
|
2016-03-05 02:08:16 +03:00
|
|
|
}
|
2017-07-24 18:26:10 +03:00
|
|
|
sw.writeFoot()
|
2016-07-11 07:40:18 +03:00
|
|
|
if msg.OutputType == server.JSON {
|
|
|
|
wr.WriteString(`,"elapsed":"` + time.Now().Sub(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
|
|
|
}
|
|
|
|
|
2018-08-14 03:05:30 +03:00
|
|
|
func (c *Controller) cmdSeachValuesArgs(vs []resp.Value) (
|
|
|
|
s liveFenceSwitches, err error,
|
|
|
|
) {
|
|
|
|
var t searchScanBaseTokens
|
|
|
|
vs, t, err = c.parseSearchScanBaseTokens("search", t, vs)
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
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
|
|
|
func (c *Controller) cmdSearch(msg *server.Message) (res resp.Value, err error) {
|
2016-07-13 06:11:02 +03:00
|
|
|
start := time.Now()
|
|
|
|
vs := msg.Values[1:]
|
|
|
|
|
2016-07-11 07:40:18 +03:00
|
|
|
wr := &bytes.Buffer{}
|
2018-02-15 22:08:27 +03:00
|
|
|
s, err := c.cmdSeachValuesArgs(vs)
|
2018-03-05 21:10:40 +03:00
|
|
|
if s.usingLua() {
|
|
|
|
defer s.Close()
|
|
|
|
defer func() {
|
|
|
|
if r := recover(); r != nil {
|
|
|
|
res = server.NOMessage
|
|
|
|
err = errors.New(r.(string))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
2016-07-13 06:11:02 +03:00
|
|
|
if err != nil {
|
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 server.NOMessage, err
|
2016-07-13 06:11:02 +03:00
|
|
|
}
|
2018-02-15 22:08:27 +03:00
|
|
|
sw, err := c.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 {
|
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 server.NOMessage, err
|
2016-07-13 06:11:02 +03:00
|
|
|
}
|
2016-07-11 07:40:18 +03:00
|
|
|
if msg.OutputType == server.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 == true {
|
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] == "" {
|
2017-07-24 18:26:10 +03:00
|
|
|
sw.col.SearchValues(s.desc,
|
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{
|
2017-01-13 19:31:35 +03:00
|
|
|
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 {
|
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.
|
|
|
|
sw.globSingle = false
|
2017-07-24 18:26:10 +03:00
|
|
|
sw.col.SearchValuesRange(g.Limits[0], g.Limits[1], s.desc,
|
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{
|
2017-01-13 19:31:35 +03:00
|
|
|
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
|
|
|
}
|
2017-07-24 18:26:10 +03:00
|
|
|
sw.writeFoot()
|
2016-07-11 07:40:18 +03:00
|
|
|
if msg.OutputType == server.JSON {
|
2016-07-13 06:11:02 +03:00
|
|
|
wr.WriteString(`,"elapsed":"` + time.Now().Sub(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
|
|
|
}
|