2015-03-02 06:10:54 +03:00
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
2017-10-30 11:39:51 +03:00
|
|
|
"bytes"
|
2015-03-02 06:10:54 +03:00
|
|
|
"fmt"
|
2015-05-04 17:42:28 +03:00
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
|
2020-04-24 09:10:03 +03:00
|
|
|
"github.com/ledisdb/ledisdb/ledis"
|
2015-03-02 06:10:54 +03:00
|
|
|
"github.com/siddontang/go/hack"
|
2015-03-02 09:30:04 +03:00
|
|
|
"github.com/siddontang/go/num"
|
2015-03-02 06:10:54 +03:00
|
|
|
)
|
|
|
|
|
2017-10-30 11:39:51 +03:00
|
|
|
func parseXScanArgs(args [][]byte) (cursor []byte, match string, count int, desc bool, err error) {
|
2015-03-02 09:30:04 +03:00
|
|
|
cursor = args[0]
|
|
|
|
|
|
|
|
args = args[1:]
|
|
|
|
|
|
|
|
count = 10
|
|
|
|
|
2015-03-05 10:49:08 +03:00
|
|
|
desc = false
|
|
|
|
|
2015-03-02 09:30:04 +03:00
|
|
|
for i := 0; i < len(args); {
|
|
|
|
switch strings.ToUpper(hack.String(args[i])) {
|
|
|
|
case "MATCH":
|
|
|
|
if i+1 >= len(args) {
|
|
|
|
err = ErrCmdParams
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
match = hack.String(args[i+1])
|
2015-03-05 10:49:08 +03:00
|
|
|
i++
|
2015-03-02 09:30:04 +03:00
|
|
|
case "COUNT":
|
|
|
|
if i+1 >= len(args) {
|
|
|
|
err = ErrCmdParams
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
count, err = strconv.Atoi(hack.String(args[i+1]))
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-03-05 10:49:08 +03:00
|
|
|
i++
|
|
|
|
case "ASC":
|
|
|
|
desc = false
|
|
|
|
case "DESC":
|
|
|
|
desc = true
|
2015-03-02 09:30:04 +03:00
|
|
|
default:
|
|
|
|
err = fmt.Errorf("invalid argument %s", args[i])
|
|
|
|
return
|
|
|
|
}
|
2015-03-05 10:49:08 +03:00
|
|
|
|
|
|
|
i++
|
2015-03-02 09:30:04 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-10-30 11:39:51 +03:00
|
|
|
func parseScanArgs(args [][]byte) (cursor []byte, match string, count int, desc bool, err error) {
|
|
|
|
cursor, match, count, desc, err = parseXScanArgs(args)
|
|
|
|
if bytes.Compare(cursor, nilCursorRedis) == 0 {
|
|
|
|
cursor = nilCursorLedis
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
type scanCommandGroup struct {
|
|
|
|
lastCursor []byte
|
|
|
|
parseArgs func(args [][]byte) (cursor []byte, match string, count int, desc bool, err error)
|
|
|
|
}
|
|
|
|
|
2015-03-05 10:49:08 +03:00
|
|
|
// XSCAN type cursor [MATCH match] [COUNT count] [ASC|DESC]
|
2017-10-30 11:39:51 +03:00
|
|
|
func (scg scanCommandGroup) xscanCommand(c *client) error {
|
2015-03-02 06:10:54 +03:00
|
|
|
args := c.args
|
|
|
|
|
|
|
|
if len(args) < 2 {
|
|
|
|
return ErrCmdParams
|
|
|
|
}
|
|
|
|
|
|
|
|
var dataType ledis.DataType
|
|
|
|
switch strings.ToUpper(hack.String(args[0])) {
|
|
|
|
case "KV":
|
|
|
|
dataType = ledis.KV
|
|
|
|
case "HASH":
|
|
|
|
dataType = ledis.HASH
|
|
|
|
case "LIST":
|
|
|
|
dataType = ledis.LIST
|
|
|
|
case "SET":
|
|
|
|
dataType = ledis.SET
|
|
|
|
case "ZSET":
|
|
|
|
dataType = ledis.ZSET
|
|
|
|
default:
|
|
|
|
return fmt.Errorf("invalid key type %s", args[0])
|
|
|
|
}
|
|
|
|
|
2017-10-30 11:39:51 +03:00
|
|
|
cursor, match, count, desc, err := scg.parseArgs(args[1:])
|
2015-03-02 06:10:54 +03:00
|
|
|
|
2015-03-02 09:30:04 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-03-02 06:10:54 +03:00
|
|
|
|
2015-03-05 10:49:08 +03:00
|
|
|
var ay [][]byte
|
|
|
|
|
|
|
|
if !desc {
|
|
|
|
ay, err = c.db.Scan(dataType, cursor, count, false, match)
|
|
|
|
} else {
|
|
|
|
ay, err = c.db.RevScan(dataType, cursor, count, false, match)
|
|
|
|
}
|
|
|
|
|
2015-03-02 09:30:04 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-03-02 06:10:54 +03:00
|
|
|
|
2015-03-02 09:30:04 +03:00
|
|
|
data := make([]interface{}, 2)
|
|
|
|
if len(ay) < count {
|
2017-10-30 11:39:51 +03:00
|
|
|
data[0] = scg.lastCursor
|
2015-03-02 09:30:04 +03:00
|
|
|
} else {
|
|
|
|
data[0] = ay[len(ay)-1]
|
|
|
|
}
|
|
|
|
data[1] = ay
|
|
|
|
c.resp.writeArray(data)
|
|
|
|
return nil
|
|
|
|
}
|
2015-03-02 06:10:54 +03:00
|
|
|
|
2015-03-05 10:49:08 +03:00
|
|
|
// XHSCAN key cursor [MATCH match] [COUNT count] [ASC|DESC]
|
2017-10-30 11:39:51 +03:00
|
|
|
func (scg scanCommandGroup) xhscanCommand(c *client) error {
|
2015-03-02 09:30:04 +03:00
|
|
|
args := c.args
|
2015-03-02 06:10:54 +03:00
|
|
|
|
2015-03-02 09:30:04 +03:00
|
|
|
if len(args) < 2 {
|
|
|
|
return ErrCmdParams
|
|
|
|
}
|
2015-03-02 06:10:54 +03:00
|
|
|
|
2015-03-02 09:30:04 +03:00
|
|
|
key := args[0]
|
2015-03-02 06:10:54 +03:00
|
|
|
|
2017-10-30 11:39:51 +03:00
|
|
|
cursor, match, count, desc, err := scg.parseArgs(args[1:])
|
2015-03-02 06:10:54 +03:00
|
|
|
|
2015-03-02 09:30:04 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-03-05 10:49:08 +03:00
|
|
|
var ay []ledis.FVPair
|
|
|
|
|
|
|
|
if !desc {
|
|
|
|
ay, err = c.db.HScan(key, cursor, count, false, match)
|
|
|
|
} else {
|
|
|
|
ay, err = c.db.HRevScan(key, cursor, count, false, match)
|
|
|
|
}
|
|
|
|
|
2015-03-02 09:30:04 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
2015-03-02 06:10:54 +03:00
|
|
|
}
|
|
|
|
|
2015-03-02 09:30:04 +03:00
|
|
|
data := make([]interface{}, 2)
|
|
|
|
if len(ay) < count {
|
2017-10-30 11:39:51 +03:00
|
|
|
data[0] = scg.lastCursor
|
2015-03-02 06:10:54 +03:00
|
|
|
} else {
|
2015-03-02 09:30:04 +03:00
|
|
|
data[0] = ay[len(ay)-1].Field
|
|
|
|
}
|
|
|
|
|
|
|
|
vv := make([][]byte, 0, len(ay)*2)
|
|
|
|
|
|
|
|
for _, v := range ay {
|
|
|
|
vv = append(vv, v.Field, v.Value)
|
2015-03-02 06:10:54 +03:00
|
|
|
}
|
|
|
|
|
2015-03-02 09:30:04 +03:00
|
|
|
data[1] = vv
|
|
|
|
|
|
|
|
c.resp.writeArray(data)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-03-05 10:49:08 +03:00
|
|
|
// XSSCAN key cursor [MATCH match] [COUNT count] [ASC|DESC]
|
2017-10-30 11:39:51 +03:00
|
|
|
func (scg scanCommandGroup) xsscanCommand(c *client) error {
|
2015-03-02 09:30:04 +03:00
|
|
|
args := c.args
|
|
|
|
|
|
|
|
if len(args) < 2 {
|
|
|
|
return ErrCmdParams
|
|
|
|
}
|
|
|
|
|
|
|
|
key := args[0]
|
|
|
|
|
2017-10-30 11:39:51 +03:00
|
|
|
cursor, match, count, desc, err := scg.parseArgs(args[1:])
|
2015-03-02 09:30:04 +03:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-03-05 10:49:08 +03:00
|
|
|
var ay [][]byte
|
|
|
|
|
|
|
|
if !desc {
|
|
|
|
ay, err = c.db.SScan(key, cursor, count, false, match)
|
|
|
|
} else {
|
|
|
|
ay, err = c.db.SRevScan(key, cursor, count, false, match)
|
|
|
|
}
|
|
|
|
|
2015-03-02 06:10:54 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
data := make([]interface{}, 2)
|
|
|
|
if len(ay) < count {
|
2017-10-30 11:39:51 +03:00
|
|
|
data[0] = scg.lastCursor
|
2015-03-02 06:10:54 +03:00
|
|
|
} else {
|
|
|
|
data[0] = ay[len(ay)-1]
|
|
|
|
}
|
2015-03-02 09:30:04 +03:00
|
|
|
|
2015-03-02 06:10:54 +03:00
|
|
|
data[1] = ay
|
2015-03-02 09:30:04 +03:00
|
|
|
|
|
|
|
c.resp.writeArray(data)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-03-05 10:49:08 +03:00
|
|
|
// XZSCAN key cursor [MATCH match] [COUNT count] [ASC|DESC]
|
2017-10-30 11:39:51 +03:00
|
|
|
func (scg scanCommandGroup) xzscanCommand(c *client) error {
|
2015-03-02 09:30:04 +03:00
|
|
|
args := c.args
|
|
|
|
|
|
|
|
if len(args) < 2 {
|
|
|
|
return ErrCmdParams
|
|
|
|
}
|
|
|
|
|
|
|
|
key := args[0]
|
|
|
|
|
2017-10-30 11:39:51 +03:00
|
|
|
cursor, match, count, desc, err := scg.parseArgs(args[1:])
|
2015-03-02 09:30:04 +03:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-03-05 10:49:08 +03:00
|
|
|
var ay []ledis.ScorePair
|
|
|
|
|
|
|
|
if !desc {
|
|
|
|
ay, err = c.db.ZScan(key, cursor, count, false, match)
|
|
|
|
} else {
|
|
|
|
ay, err = c.db.ZRevScan(key, cursor, count, false, match)
|
|
|
|
}
|
|
|
|
|
2015-03-02 09:30:04 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
data := make([]interface{}, 2)
|
|
|
|
if len(ay) < count {
|
2017-10-30 11:39:51 +03:00
|
|
|
data[0] = scg.lastCursor
|
2015-03-02 09:30:04 +03:00
|
|
|
} else {
|
|
|
|
data[0] = ay[len(ay)-1].Member
|
|
|
|
}
|
|
|
|
|
|
|
|
vv := make([][]byte, 0, len(ay)*2)
|
|
|
|
|
|
|
|
for _, v := range ay {
|
|
|
|
vv = append(vv, v.Member, num.FormatInt64ToSlice(v.Score))
|
|
|
|
}
|
|
|
|
|
|
|
|
data[1] = vv
|
|
|
|
|
2015-03-02 06:10:54 +03:00
|
|
|
c.resp.writeArray(data)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-10-30 11:39:51 +03:00
|
|
|
var (
|
|
|
|
xScanGroup = scanCommandGroup{nilCursorLedis, parseXScanArgs}
|
|
|
|
scanGroup = scanCommandGroup{nilCursorRedis, parseScanArgs}
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
nilCursorLedis = []byte("")
|
|
|
|
nilCursorRedis = []byte("0")
|
|
|
|
)
|
|
|
|
|
2015-03-02 06:10:54 +03:00
|
|
|
func init() {
|
2017-10-30 11:39:51 +03:00
|
|
|
register("hscan", scanGroup.xhscanCommand)
|
|
|
|
register("sscan", scanGroup.xsscanCommand)
|
|
|
|
register("zscan", scanGroup.xzscanCommand)
|
|
|
|
|
|
|
|
register("xscan", xScanGroup.xscanCommand)
|
|
|
|
register("xhscan", xScanGroup.xhscanCommand)
|
|
|
|
register("xsscan", xScanGroup.xsscanCommand)
|
|
|
|
register("xzscan", xScanGroup.xzscanCommand)
|
2015-03-02 06:10:54 +03:00
|
|
|
}
|