2018-10-29 01:49:45 +03:00
|
|
|
package server
|
2016-03-05 02:08:16 +03:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2018-02-15 22:08:27 +03:00
|
|
|
"errors"
|
2016-03-05 02:08:16 +03:00
|
|
|
"time"
|
|
|
|
|
2018-10-11 00:25:40 +03:00
|
|
|
"github.com/tidwall/geojson"
|
2018-10-29 01:49:45 +03:00
|
|
|
"github.com/tidwall/resp"
|
2018-10-11 00:25:40 +03:00
|
|
|
"github.com/tidwall/tile38/internal/glob"
|
2016-03-05 02:08:16 +03:00
|
|
|
)
|
|
|
|
|
2019-10-30 20:17:59 +03:00
|
|
|
func (s *Server) cmdScanArgs(vs []string) (
|
|
|
|
ls liveFenceSwitches, err error,
|
2018-08-14 03:05:30 +03:00
|
|
|
) {
|
|
|
|
var t searchScanBaseTokens
|
2019-10-30 20:17:59 +03:00
|
|
|
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
|
|
|
|
}
|
2019-10-30 20:17:59 +03:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2019-10-30 20:17:59 +03:00
|
|
|
func (s *Server) cmdScan(msg *Message) (res resp.Value, err error) {
|
2016-03-05 02:08:16 +03:00
|
|
|
start := time.Now()
|
2018-10-29 01:49:45 +03:00
|
|
|
vs := msg.Args[1:]
|
2016-03-29 00:16:21 +03:00
|
|
|
|
2019-10-30 20:17:59 +03:00
|
|
|
args, err := s.cmdScanArgs(vs)
|
|
|
|
if args.usingLua() {
|
|
|
|
defer args.Close()
|
2018-03-05 21:10:40 +03:00
|
|
|
defer func() {
|
|
|
|
if r := recover(); r != nil {
|
2018-10-29 01:49:45 +03:00
|
|
|
res = NOMessage
|
2018-03-05 21:10:40 +03:00
|
|
|
err = errors.New(r.(string))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
2016-03-05 02:08:16 +03:00
|
|
|
if err != nil {
|
2018-10-29 01:49:45 +03:00
|
|
|
return NOMessage, err
|
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
|
|
|
wr := &bytes.Buffer{}
|
2019-10-30 20:17:59 +03:00
|
|
|
sw, err := s.newScanWriter(
|
|
|
|
wr, msg, args.key, args.output, args.precision, args.glob, false,
|
|
|
|
args.cursor, args.limit, args.wheres, args.whereins, args.whereevals,
|
2022-04-22 01:00:17 +03:00
|
|
|
args.nofields, args.mvt)
|
2016-03-05 02:08:16 +03:00
|
|
|
if err != nil {
|
2018-10-29 01:49:45 +03:00
|
|
|
return NOMessage, err
|
2016-03-05 02:08:16 +03:00
|
|
|
}
|
2018-10-29 01:49:45 +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 &&
|
2021-03-31 18:13:44 +03:00
|
|
|
len(sw.whereins) == 0 && sw.globEverything {
|
2019-10-30 20:17:59 +03:00
|
|
|
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 {
|
2019-10-30 20:17:59 +03:00
|
|
|
g := glob.Parse(sw.globPattern, args.desc)
|
2016-07-12 22:18:16 +03:00
|
|
|
if g.Limits[0] == "" && g.Limits[1] == "" {
|
2019-10-30 20:17:59 +03:00
|
|
|
sw.col.Scan(args.desc, sw,
|
2019-04-24 15:09:41 +03:00
|
|
|
msg.Deadline,
|
2016-07-12 22:18:16 +03:00
|
|
|
func(id string, o geojson.Object, fields []float64) bool {
|
2017-01-10 19:49:48 +03:00
|
|
|
return sw.writeObject(ScanWriterParams{
|
2017-07-24 18:26:10 +03:00
|
|
|
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 {
|
2019-10-30 20:17:59 +03:00
|
|
|
sw.col.ScanRange(g.Limits[0], g.Limits[1], args.desc, sw,
|
2019-04-24 15:09:41 +03:00
|
|
|
msg.Deadline,
|
2016-07-10 23:23:50 +03:00
|
|
|
func(id string, o geojson.Object, fields []float64) bool {
|
2017-01-10 19:49:48 +03:00
|
|
|
return sw.writeObject(ScanWriterParams{
|
2017-07-24 18:26:10 +03:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-07-24 18:26:10 +03:00
|
|
|
sw.writeFoot()
|
2018-10-29 01:49:45 +03:00
|
|
|
if msg.OutputType == JSON {
|
2021-03-31 18:13:44 +03:00
|
|
|
wr.WriteString(`,"elapsed":"` + time.Since(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
|
|
|
}
|