2018-10-29 01:49:45 +03:00
|
|
|
package server
|
2016-03-05 02:08:16 +03:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"errors"
|
2017-08-10 23:31:36 +03:00
|
|
|
"math"
|
2016-03-05 02:08:16 +03:00
|
|
|
"strconv"
|
2016-04-02 17:20:30 +03:00
|
|
|
"sync"
|
2016-03-05 02:08:16 +03:00
|
|
|
|
2018-10-11 00:25:40 +03:00
|
|
|
"github.com/mmcloughlin/geohash"
|
|
|
|
"github.com/tidwall/geojson"
|
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/clip"
|
|
|
|
"github.com/tidwall/tile38/internal/collection"
|
|
|
|
"github.com/tidwall/tile38/internal/glob"
|
2016-03-05 02:08:16 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
const limitItems = 100
|
|
|
|
|
|
|
|
type outputT int
|
|
|
|
|
|
|
|
const (
|
|
|
|
outputUnknown outputT = iota
|
|
|
|
outputIDs
|
|
|
|
outputObjects
|
|
|
|
outputCount
|
|
|
|
outputPoints
|
|
|
|
outputHashes
|
|
|
|
outputBounds
|
|
|
|
)
|
|
|
|
|
|
|
|
type scanWriter struct {
|
2016-04-02 17:20:30 +03:00
|
|
|
mu sync.Mutex
|
2018-10-29 01:49:45 +03:00
|
|
|
c *Server
|
2016-03-05 02:08:16 +03:00
|
|
|
wr *bytes.Buffer
|
2018-10-29 01:49:45 +03:00
|
|
|
msg *Message
|
2016-03-05 02:08:16 +03:00
|
|
|
col *collection.Collection
|
|
|
|
fmap map[string]int
|
|
|
|
farr []string
|
|
|
|
fvals []float64
|
|
|
|
output outputT
|
|
|
|
wheres []whereT
|
2017-08-23 23:13:12 +03:00
|
|
|
whereins []whereinT
|
2018-02-15 22:08:27 +03:00
|
|
|
whereevals []whereevalT
|
2016-03-05 02:08:16 +03:00
|
|
|
numberItems uint64
|
|
|
|
nofields bool
|
2017-07-24 18:26:10 +03:00
|
|
|
cursor uint64
|
2016-03-05 02:08:16 +03:00
|
|
|
limit uint64
|
|
|
|
hitLimit bool
|
|
|
|
once bool
|
|
|
|
count uint64
|
|
|
|
precision uint64
|
2016-07-13 07:51:01 +03:00
|
|
|
globPattern string
|
2016-03-05 02:08:16 +03:00
|
|
|
globEverything bool
|
|
|
|
globSingle bool
|
|
|
|
fullFields bool
|
2016-03-29 00:16:21 +03:00
|
|
|
values []resp.Value
|
2016-07-13 06:11:02 +03:00
|
|
|
matchValues bool
|
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
|
|
|
respOut resp.Value
|
2016-03-05 02:08:16 +03:00
|
|
|
}
|
|
|
|
|
2018-10-11 00:25:40 +03:00
|
|
|
// ScanWriterParams ...
|
2017-01-10 19:49:48 +03:00
|
|
|
type ScanWriterParams struct {
|
2018-03-10 05:50:34 +03:00
|
|
|
id string
|
|
|
|
o geojson.Object
|
|
|
|
fields []float64
|
|
|
|
distance float64
|
|
|
|
noLock bool
|
|
|
|
ignoreGlobMatch bool
|
2018-10-11 00:25:40 +03:00
|
|
|
clip geojson.Object
|
2017-01-10 19:49:48 +03:00
|
|
|
}
|
|
|
|
|
2018-10-29 01:49:45 +03:00
|
|
|
func (c *Server) newScanWriter(
|
|
|
|
wr *bytes.Buffer, msg *Message, key string, output outputT,
|
2016-07-13 06:11:02 +03:00
|
|
|
precision uint64, globPattern string, matchValues bool,
|
2018-02-15 22:08:27 +03:00
|
|
|
cursor, limit uint64, wheres []whereT, whereins []whereinT, whereevals []whereevalT, nofields bool,
|
2016-03-05 02:08:16 +03:00
|
|
|
) (
|
|
|
|
*scanWriter, error,
|
|
|
|
) {
|
|
|
|
switch output {
|
|
|
|
default:
|
|
|
|
return nil, errors.New("invalid output type")
|
|
|
|
case outputIDs, outputObjects, outputCount, outputBounds, outputPoints, outputHashes:
|
|
|
|
}
|
2017-08-10 23:31:36 +03:00
|
|
|
if limit == 0 {
|
|
|
|
if output == outputCount {
|
|
|
|
limit = math.MaxUint64
|
|
|
|
} else {
|
|
|
|
limit = limitItems
|
|
|
|
}
|
|
|
|
}
|
2016-03-05 02:08:16 +03:00
|
|
|
sw := &scanWriter{
|
2016-07-13 06:11:02 +03:00
|
|
|
c: c,
|
|
|
|
wr: wr,
|
|
|
|
msg: msg,
|
2017-07-24 18:26:10 +03:00
|
|
|
cursor: cursor,
|
2016-07-13 07:51:01 +03:00
|
|
|
limit: limit,
|
2016-07-13 06:11:02 +03:00
|
|
|
wheres: wheres,
|
2017-08-23 23:13:12 +03:00
|
|
|
whereins: whereins,
|
2018-02-15 22:08:27 +03:00
|
|
|
whereevals: whereevals,
|
2016-07-13 07:51:01 +03:00
|
|
|
output: output,
|
2016-07-13 06:11:02 +03:00
|
|
|
nofields: nofields,
|
2016-07-13 07:51:01 +03:00
|
|
|
precision: precision,
|
|
|
|
globPattern: globPattern,
|
2016-07-13 06:11:02 +03:00
|
|
|
matchValues: matchValues,
|
2016-03-05 02:08:16 +03:00
|
|
|
}
|
2016-07-12 22:18:16 +03:00
|
|
|
if globPattern == "*" || globPattern == "" {
|
2016-03-05 02:08:16 +03:00
|
|
|
sw.globEverything = true
|
|
|
|
} else {
|
2016-07-12 22:18:16 +03:00
|
|
|
if !glob.IsGlob(globPattern) {
|
2016-03-05 02:08:16 +03:00
|
|
|
sw.globSingle = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
sw.col = c.getCol(key)
|
|
|
|
if sw.col != nil {
|
|
|
|
sw.fmap = sw.col.FieldMap()
|
|
|
|
sw.farr = sw.col.FieldArr()
|
|
|
|
}
|
|
|
|
sw.fvals = make([]float64, len(sw.farr))
|
|
|
|
return sw, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (sw *scanWriter) hasFieldsOutput() bool {
|
|
|
|
switch sw.output {
|
|
|
|
default:
|
|
|
|
return false
|
|
|
|
case outputObjects, outputPoints, outputHashes, outputBounds:
|
|
|
|
return !sw.nofields
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (sw *scanWriter) writeHead() {
|
2016-04-02 17:20:30 +03:00
|
|
|
sw.mu.Lock()
|
|
|
|
defer sw.mu.Unlock()
|
2016-03-29 00:16:21 +03:00
|
|
|
switch sw.msg.OutputType {
|
2018-10-29 01:49:45 +03:00
|
|
|
case JSON:
|
2016-03-29 00:16:21 +03:00
|
|
|
if len(sw.farr) > 0 && sw.hasFieldsOutput() {
|
|
|
|
sw.wr.WriteString(`,"fields":[`)
|
|
|
|
for i, field := range sw.farr {
|
|
|
|
if i > 0 {
|
|
|
|
sw.wr.WriteByte(',')
|
|
|
|
}
|
|
|
|
sw.wr.WriteString(jsonString(field))
|
2016-03-05 02:08:16 +03:00
|
|
|
}
|
2016-03-29 00:16:21 +03:00
|
|
|
sw.wr.WriteByte(']')
|
2016-03-05 02:08:16 +03:00
|
|
|
}
|
2016-03-29 00:16:21 +03:00
|
|
|
switch sw.output {
|
|
|
|
case outputIDs:
|
|
|
|
sw.wr.WriteString(`,"ids":[`)
|
|
|
|
case outputObjects:
|
|
|
|
sw.wr.WriteString(`,"objects":[`)
|
|
|
|
case outputPoints:
|
|
|
|
sw.wr.WriteString(`,"points":[`)
|
|
|
|
case outputBounds:
|
|
|
|
sw.wr.WriteString(`,"bounds":[`)
|
|
|
|
case outputHashes:
|
|
|
|
sw.wr.WriteString(`,"hashes":[`)
|
|
|
|
case outputCount:
|
2016-03-05 02:08:16 +03:00
|
|
|
|
2016-03-29 00:16:21 +03:00
|
|
|
}
|
2018-10-29 01:49:45 +03:00
|
|
|
case RESP:
|
2016-03-05 02:08:16 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-24 18:26:10 +03:00
|
|
|
func (sw *scanWriter) writeFoot() {
|
2016-04-02 17:20:30 +03:00
|
|
|
sw.mu.Lock()
|
|
|
|
defer sw.mu.Unlock()
|
2017-07-24 18:42:12 +03:00
|
|
|
cursor := sw.cursor + sw.numberItems
|
2016-03-05 02:08:16 +03:00
|
|
|
if !sw.hitLimit {
|
|
|
|
cursor = 0
|
|
|
|
}
|
2016-03-29 00:16:21 +03:00
|
|
|
switch sw.msg.OutputType {
|
2018-10-29 01:49:45 +03:00
|
|
|
case JSON:
|
2016-03-29 00:16:21 +03:00
|
|
|
switch sw.output {
|
|
|
|
default:
|
|
|
|
sw.wr.WriteByte(']')
|
|
|
|
case outputCount:
|
2016-03-05 02:08:16 +03:00
|
|
|
|
2016-03-29 00:16:21 +03:00
|
|
|
}
|
|
|
|
sw.wr.WriteString(`,"count":` + strconv.FormatUint(sw.count, 10))
|
|
|
|
sw.wr.WriteString(`,"cursor":` + strconv.FormatUint(cursor, 10))
|
2018-10-29 01:49:45 +03:00
|
|
|
case RESP:
|
2016-05-24 00:21:18 +03:00
|
|
|
if sw.output == outputCount {
|
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
|
|
|
sw.respOut = resp.IntegerValue(int(sw.count))
|
2016-05-24 00:21:18 +03:00
|
|
|
} else {
|
|
|
|
values := []resp.Value{
|
|
|
|
resp.IntegerValue(int(cursor)),
|
|
|
|
resp.ArrayValue(sw.values),
|
|
|
|
}
|
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
|
|
|
sw.respOut = resp.ArrayValue(values)
|
2016-03-29 00:16:21 +03:00
|
|
|
}
|
2016-03-05 02:08:16 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-15 22:08:27 +03:00
|
|
|
func (sw *scanWriter) fieldMatch(fields []float64, o geojson.Object) (fvals []float64, match bool) {
|
2016-03-05 02:08:16 +03:00
|
|
|
var z float64
|
|
|
|
var gotz bool
|
2018-02-15 22:08:27 +03:00
|
|
|
fvals = sw.fvals
|
2016-03-05 02:08:16 +03:00
|
|
|
if !sw.hasFieldsOutput() || sw.fullFields {
|
|
|
|
for _, where := range sw.wheres {
|
|
|
|
if where.field == "z" {
|
|
|
|
if !gotz {
|
2018-10-11 00:25:40 +03:00
|
|
|
if point, ok := o.(*geojson.Point); ok {
|
|
|
|
z = point.Z()
|
|
|
|
}
|
2016-03-05 02:08:16 +03:00
|
|
|
}
|
|
|
|
if !where.match(z) {
|
2018-02-15 22:08:27 +03:00
|
|
|
return
|
2016-03-05 02:08:16 +03:00
|
|
|
}
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
var value float64
|
|
|
|
idx, ok := sw.fmap[where.field]
|
|
|
|
if ok {
|
|
|
|
if len(fields) > idx {
|
|
|
|
value = fields[idx]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !where.match(value) {
|
2018-02-15 22:08:27 +03:00
|
|
|
return
|
2016-03-05 02:08:16 +03:00
|
|
|
}
|
|
|
|
}
|
2017-08-23 23:13:12 +03:00
|
|
|
for _, wherein := range sw.whereins {
|
|
|
|
var value float64
|
|
|
|
idx, ok := sw.fmap[wherein.field]
|
|
|
|
if ok {
|
|
|
|
if len(fields) > idx {
|
|
|
|
value = fields[idx]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !wherein.match(value) {
|
2018-02-15 22:08:27 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for _, whereval := range sw.whereevals {
|
|
|
|
fieldsWithNames := make(map[string]float64)
|
|
|
|
for field, idx := range sw.fmap {
|
|
|
|
if idx < len(fields) {
|
|
|
|
fieldsWithNames[field] = fields[idx]
|
|
|
|
} else {
|
|
|
|
fieldsWithNames[field] = 0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !whereval.match(fieldsWithNames) {
|
|
|
|
return
|
2017-08-23 23:13:12 +03:00
|
|
|
}
|
|
|
|
}
|
2016-03-05 02:08:16 +03:00
|
|
|
} else {
|
|
|
|
for idx := range sw.farr {
|
|
|
|
var value float64
|
|
|
|
if len(fields) > idx {
|
|
|
|
value = fields[idx]
|
|
|
|
}
|
|
|
|
sw.fvals[idx] = value
|
|
|
|
}
|
|
|
|
for _, where := range sw.wheres {
|
|
|
|
if where.field == "z" {
|
|
|
|
if !gotz {
|
2018-10-11 00:25:40 +03:00
|
|
|
if point, ok := o.(*geojson.Point); ok {
|
|
|
|
z = point.Z()
|
|
|
|
}
|
2016-03-05 02:08:16 +03:00
|
|
|
}
|
|
|
|
if !where.match(z) {
|
2018-02-15 22:08:27 +03:00
|
|
|
return
|
2016-03-05 02:08:16 +03:00
|
|
|
}
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
var value float64
|
|
|
|
idx, ok := sw.fmap[where.field]
|
|
|
|
if ok {
|
|
|
|
value = sw.fvals[idx]
|
|
|
|
}
|
|
|
|
if !where.match(value) {
|
2018-02-15 22:08:27 +03:00
|
|
|
return
|
2016-03-05 02:08:16 +03:00
|
|
|
}
|
|
|
|
}
|
2017-08-23 23:13:12 +03:00
|
|
|
for _, wherein := range sw.whereins {
|
|
|
|
var value float64
|
|
|
|
idx, ok := sw.fmap[wherein.field]
|
|
|
|
if ok {
|
|
|
|
value = sw.fvals[idx]
|
|
|
|
}
|
|
|
|
if !wherein.match(value) {
|
2018-02-15 22:08:27 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for _, whereval := range sw.whereevals {
|
|
|
|
fieldsWithNames := make(map[string]float64)
|
|
|
|
for field, idx := range sw.fmap {
|
|
|
|
if idx < len(fields) {
|
|
|
|
fieldsWithNames[field] = fields[idx]
|
|
|
|
} else {
|
|
|
|
fieldsWithNames[field] = 0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !whereval.match(fieldsWithNames) {
|
|
|
|
return
|
2017-08-23 23:13:12 +03:00
|
|
|
}
|
|
|
|
}
|
2016-03-05 02:08:16 +03:00
|
|
|
}
|
2018-02-15 22:08:27 +03:00
|
|
|
match = true
|
|
|
|
return
|
2016-03-05 02:08:16 +03:00
|
|
|
}
|
|
|
|
|
2018-03-10 05:50:34 +03:00
|
|
|
func (sw *scanWriter) globMatch(id string, o geojson.Object) (ok, keepGoing bool) {
|
|
|
|
if !sw.globEverything {
|
|
|
|
if sw.globSingle {
|
|
|
|
if sw.globPattern != id {
|
|
|
|
return false, true
|
|
|
|
}
|
|
|
|
return true, false
|
|
|
|
}
|
|
|
|
var val string
|
|
|
|
if sw.matchValues {
|
|
|
|
val = o.String()
|
|
|
|
} else {
|
|
|
|
val = id
|
|
|
|
}
|
|
|
|
ok, _ := glob.Match(sw.globPattern, val)
|
|
|
|
if !ok {
|
|
|
|
return false, true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true, true
|
|
|
|
}
|
|
|
|
|
2017-01-10 19:49:48 +03:00
|
|
|
//id string, o geojson.Object, fields []float64, noLock bool
|
|
|
|
func (sw *scanWriter) writeObject(opts ScanWriterParams) bool {
|
|
|
|
if !opts.noLock {
|
2016-04-03 00:13:20 +03:00
|
|
|
sw.mu.Lock()
|
|
|
|
defer sw.mu.Unlock()
|
|
|
|
}
|
2016-03-05 02:08:16 +03:00
|
|
|
keepGoing := true
|
2018-03-10 05:50:34 +03:00
|
|
|
if !opts.ignoreGlobMatch {
|
|
|
|
var match bool
|
|
|
|
match, keepGoing = sw.globMatch(opts.id, opts.o)
|
|
|
|
if !match {
|
|
|
|
return true
|
2016-03-05 02:08:16 +03:00
|
|
|
}
|
|
|
|
}
|
2017-01-10 19:49:48 +03:00
|
|
|
nfields, ok := sw.fieldMatch(opts.fields, opts.o)
|
2016-03-05 02:08:16 +03:00
|
|
|
if !ok {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
sw.count++
|
2017-07-24 18:42:12 +03:00
|
|
|
if sw.count <= sw.cursor {
|
|
|
|
return true
|
|
|
|
}
|
2016-03-05 02:08:16 +03:00
|
|
|
if sw.output == outputCount {
|
2017-07-26 06:23:21 +03:00
|
|
|
return sw.count < sw.limit
|
2016-03-05 02:08:16 +03:00
|
|
|
}
|
2018-10-11 00:25:40 +03:00
|
|
|
if opts.clip != nil {
|
|
|
|
opts.o = clip.Clip(opts.o, opts.clip)
|
2018-05-08 02:18:18 +03:00
|
|
|
}
|
2016-03-29 00:16:21 +03:00
|
|
|
switch sw.msg.OutputType {
|
2018-10-29 01:49:45 +03:00
|
|
|
case JSON:
|
2016-03-29 00:16:21 +03:00
|
|
|
var wr bytes.Buffer
|
|
|
|
var jsfields string
|
|
|
|
if sw.once {
|
|
|
|
wr.WriteByte(',')
|
|
|
|
} else {
|
|
|
|
sw.once = true
|
|
|
|
}
|
|
|
|
if sw.hasFieldsOutput() {
|
|
|
|
if sw.fullFields {
|
|
|
|
if len(sw.fmap) > 0 {
|
|
|
|
jsfields = `,"fields":{`
|
|
|
|
var i int
|
|
|
|
for field, idx := range sw.fmap {
|
2017-01-10 19:49:48 +03:00
|
|
|
if len(opts.fields) > idx {
|
|
|
|
if opts.fields[idx] != 0 {
|
2016-03-29 00:16:21 +03:00
|
|
|
if i > 0 {
|
|
|
|
jsfields += `,`
|
|
|
|
}
|
2017-01-10 19:49:48 +03:00
|
|
|
jsfields += jsonString(field) + ":" + strconv.FormatFloat(opts.fields[idx], 'f', -1, 64)
|
2016-03-29 00:16:21 +03:00
|
|
|
i++
|
2016-03-05 02:08:16 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-03-29 00:16:21 +03:00
|
|
|
jsfields += `}`
|
2016-03-05 02:08:16 +03:00
|
|
|
}
|
2016-03-29 00:16:21 +03:00
|
|
|
|
|
|
|
} else if len(sw.farr) > 0 {
|
|
|
|
jsfields = `,"fields":[`
|
|
|
|
for i, field := range nfields {
|
|
|
|
if i > 0 {
|
|
|
|
jsfields += ","
|
|
|
|
}
|
|
|
|
jsfields += strconv.FormatFloat(field, 'f', -1, 64)
|
|
|
|
}
|
|
|
|
jsfields += `]`
|
2016-03-05 02:08:16 +03:00
|
|
|
}
|
2016-03-29 00:16:21 +03:00
|
|
|
}
|
|
|
|
if sw.output == outputIDs {
|
2017-01-10 19:49:48 +03:00
|
|
|
wr.WriteString(jsonString(opts.id))
|
2016-03-29 00:16:21 +03:00
|
|
|
} else {
|
2017-01-10 19:49:48 +03:00
|
|
|
wr.WriteString(`{"id":` + jsonString(opts.id))
|
2016-03-29 00:16:21 +03:00
|
|
|
switch sw.output {
|
|
|
|
case outputObjects:
|
2018-10-11 00:25:40 +03:00
|
|
|
wr.WriteString(`,"object":` + string(opts.o.AppendJSON(nil)))
|
2016-03-29 00:16:21 +03:00
|
|
|
case outputPoints:
|
2018-10-11 00:25:40 +03:00
|
|
|
wr.WriteString(`,"point":` + string(appendJSONSimplePoint(nil, opts.o)))
|
2016-03-29 00:16:21 +03:00
|
|
|
case outputHashes:
|
2018-10-11 00:25:40 +03:00
|
|
|
center := opts.o.Center()
|
|
|
|
p := geohash.EncodeWithPrecision(center.Y, center.X, uint(sw.precision))
|
2016-03-29 00:16:21 +03:00
|
|
|
wr.WriteString(`,"hash":"` + p + `"`)
|
|
|
|
case outputBounds:
|
2018-10-11 00:25:40 +03:00
|
|
|
wr.WriteString(`,"bounds":` + string(appendJSONSimpleBounds(nil, opts.o)))
|
2016-03-05 02:08:16 +03:00
|
|
|
}
|
2017-01-10 19:49:48 +03:00
|
|
|
|
2016-03-29 00:16:21 +03:00
|
|
|
wr.WriteString(jsfields)
|
2017-01-10 19:49:48 +03:00
|
|
|
|
|
|
|
if opts.distance > 0 {
|
2018-10-11 00:25:40 +03:00
|
|
|
wr.WriteString(`,"distance":` + strconv.FormatFloat(opts.distance, 'f', -1, 64))
|
2017-01-10 19:49:48 +03:00
|
|
|
}
|
|
|
|
|
2016-03-29 00:16:21 +03:00
|
|
|
wr.WriteString(`}`)
|
2016-03-05 02:08:16 +03:00
|
|
|
}
|
2016-03-29 00:16:21 +03:00
|
|
|
sw.wr.Write(wr.Bytes())
|
2018-10-29 01:49:45 +03:00
|
|
|
case RESP:
|
2016-03-29 00:16:21 +03:00
|
|
|
vals := make([]resp.Value, 1, 3)
|
2017-01-10 19:49:48 +03:00
|
|
|
vals[0] = resp.StringValue(opts.id)
|
2016-03-29 00:16:21 +03:00
|
|
|
if sw.output == outputIDs {
|
|
|
|
sw.values = append(sw.values, vals[0])
|
|
|
|
} else {
|
|
|
|
switch sw.output {
|
|
|
|
case outputObjects:
|
2017-01-10 19:49:48 +03:00
|
|
|
vals = append(vals, resp.StringValue(opts.o.String()))
|
2016-03-29 00:16:21 +03:00
|
|
|
case outputPoints:
|
2018-10-11 00:25:40 +03:00
|
|
|
point := opts.o.Center()
|
|
|
|
var z float64
|
|
|
|
if point, ok := opts.o.(*geojson.Point); ok {
|
|
|
|
z = point.Z()
|
|
|
|
}
|
|
|
|
if z != 0 {
|
2016-03-29 00:16:21 +03:00
|
|
|
vals = append(vals, resp.ArrayValue([]resp.Value{
|
|
|
|
resp.FloatValue(point.Y),
|
|
|
|
resp.FloatValue(point.X),
|
2018-10-11 00:25:40 +03:00
|
|
|
resp.FloatValue(z),
|
2016-03-29 00:16:21 +03:00
|
|
|
}))
|
|
|
|
} else {
|
|
|
|
vals = append(vals, resp.ArrayValue([]resp.Value{
|
|
|
|
resp.FloatValue(point.Y),
|
|
|
|
resp.FloatValue(point.X),
|
|
|
|
}))
|
|
|
|
}
|
|
|
|
case outputHashes:
|
2018-10-11 00:25:40 +03:00
|
|
|
center := opts.o.Center()
|
|
|
|
p := geohash.EncodeWithPrecision(center.Y, center.X, uint(sw.precision))
|
2016-03-29 00:16:21 +03:00
|
|
|
vals = append(vals, resp.StringValue(p))
|
|
|
|
case outputBounds:
|
2018-10-11 00:25:40 +03:00
|
|
|
bbox := opts.o.Rect()
|
2016-03-29 00:16:21 +03:00
|
|
|
vals = append(vals, resp.ArrayValue([]resp.Value{
|
|
|
|
resp.ArrayValue([]resp.Value{
|
|
|
|
resp.FloatValue(bbox.Min.Y),
|
|
|
|
resp.FloatValue(bbox.Min.X),
|
|
|
|
}),
|
|
|
|
resp.ArrayValue([]resp.Value{
|
|
|
|
resp.FloatValue(bbox.Max.Y),
|
|
|
|
resp.FloatValue(bbox.Max.X),
|
|
|
|
}),
|
|
|
|
}))
|
2016-03-05 02:08:16 +03:00
|
|
|
}
|
2016-03-29 00:16:21 +03:00
|
|
|
|
2017-07-22 01:56:29 +03:00
|
|
|
if sw.hasFieldsOutput() {
|
|
|
|
fvs := orderFields(sw.fmap, opts.fields)
|
|
|
|
if len(fvs) > 0 {
|
|
|
|
fvals := make([]resp.Value, 0, len(fvs)*2)
|
|
|
|
for i, fv := range fvs {
|
|
|
|
fvals = append(fvals, resp.StringValue(fv.field), resp.StringValue(strconv.FormatFloat(fv.value, 'f', -1, 64)))
|
|
|
|
i++
|
|
|
|
}
|
|
|
|
vals = append(vals, resp.ArrayValue(fvals))
|
2016-03-29 00:16:21 +03:00
|
|
|
}
|
|
|
|
}
|
2017-01-10 19:49:48 +03:00
|
|
|
if opts.distance > 0 {
|
|
|
|
vals = append(vals, resp.FloatValue(opts.distance))
|
|
|
|
}
|
|
|
|
|
2016-03-29 00:16:21 +03:00
|
|
|
sw.values = append(sw.values, resp.ArrayValue(vals))
|
2016-03-05 02:08:16 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
sw.numberItems++
|
|
|
|
if sw.numberItems == sw.limit {
|
|
|
|
sw.hitLimit = true
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return keepGoing
|
|
|
|
}
|