tile38/internal/server/scan.go

110 lines
2.3 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/resp"
2022-09-21 00:20:53 +03:00
"github.com/tidwall/tile38/internal/object"
2016-03-05 02:08:16 +03:00
)
func (s *Server) cmdScanArgs(vs []string) (
ls liveFenceSwitches, err error,
2018-08-14 03:05:30 +03:00
) {
var t searchScanBaseTokens
vs, t, err = s.parseSearchScanBaseTokens("scan", t, vs)
2018-08-14 03:05:30 +03:00
if err != nil {
2016-03-05 02:08:16 +03:00
return
}
ls.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 (s *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
args, err := s.cmdScanArgs(vs)
if args.usingLua() {
defer args.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 := s.newScanWriter(
wr, msg, args.key, args.output, args.precision, args.globs, false,
args.cursor, args.limit, args.wheres, args.whereins, args.whereevals,
args.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
}
2022-09-20 03:47:38 +03:00
var ierr error
2016-03-05 02:08:16 +03:00
if sw.col != nil {
2017-09-04 17:20:03 +03:00
if sw.output == outputCount && len(sw.wheres) == 0 &&
2022-09-20 03:47:38 +03:00
len(sw.whereins) == 0 && len(sw.whereevals) == 0 &&
sw.globEverything {
count := sw.col.Count() - int(args.cursor)
2016-03-05 02:08:16 +03:00
if count < 0 {
count = 0
}
sw.count = uint64(count)
} else {
limits := multiGlobParse(sw.globs, args.desc)
if limits[0] == "" && limits[1] == "" {
sw.col.Scan(args.desc, sw,
2019-04-24 15:09:41 +03:00
msg.Deadline,
2022-09-21 00:20:53 +03:00
func(o *object.Object) bool {
2022-09-20 03:47:38 +03:00
keepGoing, err := sw.pushObject(ScanWriterParams{
2022-09-21 00:20:53 +03:00
obj: o,
2017-01-10 19:49:48 +03:00
})
2022-09-20 03:47:38 +03:00
if err != nil {
ierr = err
return false
}
return keepGoing
2016-07-12 22:18:16 +03:00
},
)
2016-03-05 02:08:16 +03:00
} else {
sw.col.ScanRange(limits[0], limits[1], args.desc, sw,
2019-04-24 15:09:41 +03:00
msg.Deadline,
2022-09-21 00:20:53 +03:00
func(o *object.Object) bool {
2022-09-20 03:47:38 +03:00
keepGoing, err := sw.pushObject(ScanWriterParams{
2022-09-21 00:20:53 +03:00
obj: o,
2017-01-10 19:49:48 +03:00
})
2022-09-20 03:47:38 +03:00
if err != nil {
ierr = err
return false
}
return keepGoing
2016-07-10 23:23:50 +03:00
},
)
2016-03-05 02:08:16 +03:00
}
}
}
2022-09-20 03:47:38 +03:00
if ierr != nil {
return retrerr(ierr)
}
sw.writeFoot()
if msg.OutputType == JSON {
wr.WriteString(`,"elapsed":"` + time.Since(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
}