tile38/controller/scan.go

82 lines
2.1 KiB
Go
Raw Normal View History

2016-03-05 02:08:16 +03:00
package controller
import (
"bytes"
"strings"
"time"
2016-03-29 00:16:21 +03:00
"github.com/tidwall/resp"
"github.com/tidwall/tile38/controller/server"
2016-03-05 02:08:16 +03:00
"github.com/tidwall/tile38/geojson"
)
2016-03-29 00:16:21 +03:00
func cmdScanArgs(vs []resp.Value) (s liveFenceSwitches, err error) {
if vs, s.searchScanBaseTokens, err = parseSearchScanBaseTokens("scan", vs); err != nil {
2016-03-05 02:08:16 +03:00
return
}
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-29 00:16:21 +03:00
func (c *Controller) cmdScan(msg *server.Message) (res string, err error) {
2016-03-05 02:08:16 +03:00
start := time.Now()
2016-03-29 00:16:21 +03:00
vs := msg.Values[1:]
2016-03-05 02:08:16 +03:00
wr := &bytes.Buffer{}
2016-03-29 00:16:21 +03:00
s, err := cmdScanArgs(vs)
2016-03-05 02:08:16 +03:00
if err != nil {
2016-03-29 00:16:21 +03:00
return "", err
2016-03-05 02:08:16 +03:00
}
2016-03-29 00:16:21 +03:00
sw, err := c.newScanWriter(wr, msg, s.key, s.output, s.precision, s.glob, s.limit, s.wheres, s.nofields)
2016-03-05 02:08:16 +03:00
if err != nil {
2016-03-29 00:16:21 +03:00
return "", err
2016-03-05 02:08:16 +03:00
}
if s.sparse > 0 && sw.col != nil {
2016-03-29 00:16:21 +03:00
msg.Values = append(msg.Values,
resp.StringValue("BOUNDS"),
resp.StringValue("-90"),
resp.StringValue("-180"),
resp.StringValue("180"),
)
return c.cmdWithinOrIntersects("within", msg)
}
if msg.OutputType == server.JSON {
wr.WriteString(`{"ok":true`)
2016-03-05 02:08:16 +03:00
}
sw.writeHead()
if sw.col != nil {
if sw.output == outputCount && len(sw.wheres) == 0 && sw.globEverything == true {
count := sw.col.Count() - int(s.cursor)
if count < 0 {
count = 0
}
sw.count = uint64(count)
} else {
if strings.HasSuffix(sw.glob, "*") {
greaterGlob := sw.glob[:len(sw.glob)-1]
if globIsGlob(greaterGlob) {
s.cursor = sw.col.Scan(s.cursor, func(id string, o geojson.Object, fields []float64) bool {
2016-04-03 00:13:20 +03:00
return sw.writeObject(id, o, fields, false)
2016-03-05 02:08:16 +03:00
})
} else {
s.cursor = sw.col.ScanGreaterOrEqual(sw.glob, s.cursor, func(id string, o geojson.Object, fields []float64) bool {
2016-04-03 00:13:20 +03:00
return sw.writeObject(id, o, fields, false)
2016-03-05 02:08:16 +03:00
})
}
} else {
s.cursor = sw.col.Scan(s.cursor, func(id string, o geojson.Object, fields []float64) bool {
2016-04-03 00:13:20 +03:00
return sw.writeObject(id, o, fields, false)
2016-03-05 02:08:16 +03:00
})
}
}
}
sw.writeFoot(s.cursor)
2016-03-29 00:16:21 +03:00
if msg.OutputType == server.JSON {
wr.WriteString(`,"elapsed":"` + time.Now().Sub(start).String() + "\"}")
}
return string(wr.Bytes()), nil
2016-03-05 02:08:16 +03:00
}