tile38/internal/server/scan.go

116 lines
2.5 KiB
Go
Raw Normal View History

package server
2016-03-05 02:08:16 +03:00
import (
"bytes"
"errors"
2016-03-05 02:08:16 +03:00
"time"
"github.com/tidwall/geojson"
"github.com/tidwall/resp"
2019-02-15 18:26:55 +03:00
"github.com/tidwall/tile38/internal/collection"
"github.com/tidwall/tile38/internal/glob"
2016-03-05 02:08:16 +03:00
)
func (c *Server) cmdScanArgs(vs []string) (
2018-08-14 03:05:30 +03:00
s liveFenceSwitches, err error,
) {
var t searchScanBaseTokens
vs, t, err = c.parseSearchScanBaseTokens("scan", 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-29 00:16:21 +03:00
if len(vs) != 0 {
2016-03-05 02:08:16 +03:00
err = errInvalidNumberOfArguments
return
}
return
}
func (c *Server) cmdScan(msg *Message) (res resp.Value, err error) {
2016-03-05 02:08:16 +03:00
start := time.Now()
vs := msg.Args[1:]
2016-03-29 00:16:21 +03:00
s, err := c.cmdScanArgs(vs)
if s.usingLua() {
defer s.Close()
defer func() {
if r := recover(); r != nil {
res = NOMessage
err = errors.New(r.(string))
return
}
}()
}
2016-03-05 02:08:16 +03:00
if err != nil {
return NOMessage, err
2016-03-05 02:08:16 +03:00
}
2017-10-05 18:20:40 +03:00
wr := &bytes.Buffer{}
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 {
return NOMessage, err
2016-03-05 02:08:16 +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
}
sw.writeHead()
if sw.col != nil {
2017-09-04 17:20:03 +03:00
if sw.output == outputCount && len(sw.wheres) == 0 &&
len(sw.whereins) == 0 && sw.globEverything == true {
2016-07-13 07:59:36 +03:00
count := sw.col.Count() - int(s.cursor)
2016-03-05 02:08:16 +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-12 22:18:16 +03:00
if g.Limits[0] == "" && g.Limits[1] == "" {
2018-11-02 16:09:56 +03:00
sw.col.Scan(s.desc, sw,
2019-02-15 18:26:55 +03:00
func(id string, o geojson.Object, fields *collection.Fields) bool {
2017-01-10 19:49:48 +03:00
return sw.writeObject(ScanWriterParams{
id: id,
o: o,
2017-01-10 19:49:48 +03:00
fields: fields,
})
2016-07-12 22:18:16 +03:00
},
)
2016-03-05 02:08:16 +03:00
} else {
2018-11-02 16:09:56 +03:00
sw.col.ScanRange(g.Limits[0], g.Limits[1], s.desc, sw,
2019-02-15 18:26:55 +03:00
func(id string, o geojson.Object, fields *collection.Fields) bool {
2017-01-10 19:49:48 +03:00
return sw.writeObject(ScanWriterParams{
id: id,
o: o,
2017-01-10 19:49:48 +03:00
fields: fields,
})
2016-07-10 23:23:50 +03:00
},
)
2016-03-05 02:08:16 +03:00
}
}
}
sw.writeFoot()
if msg.OutputType == JSON {
2016-03-29 00:16:21 +03:00
wr.WriteString(`,"elapsed":"` + time.Now().Sub(start).String() + "\"}")
2017-10-05 18:20:40 +03:00
return resp.BytesValue(wr.Bytes()), nil
2016-03-29 00:16:21 +03:00
}
2017-10-05 18:20:40 +03:00
return sw.respOut, nil
2016-03-05 02:08:16 +03:00
}
2018-11-02 16:09:56 +03:00
// type tCursor struct {
// offset func() uint64
// iter func(n uint64)
// }
// func (cursor *tCursor) Offset() uint64 {
// return cursor.offset()
// }
// func (cursor *tCursor) Step(n uint64) {
// cursor.iter(n)
// }
// func newCursor(offset func() uint64, iter func(n uint64)) *tCursor {
// return &tCursor{offset, iter}
// }