ledisdb/server/cmd_list.go

319 lines
5.1 KiB
Go
Raw Normal View History

package server
2014-05-03 10:55:12 +04:00
import (
2014-10-17 05:55:43 +04:00
"github.com/siddontang/go/hack"
"github.com/siddontang/ledisdb/ledis"
2014-10-17 05:55:43 +04:00
"strconv"
)
2014-05-06 09:32:38 +04:00
2014-08-25 10:18:23 +04:00
func lpushCommand(c *client) error {
args := c.args
2014-05-06 09:32:38 +04:00
if len(args) < 2 {
return ErrCmdParams
}
2014-08-25 10:18:23 +04:00
if n, err := c.db.LPush(args[0], args[1:]...); err != nil {
2014-05-06 09:32:38 +04:00
return err
} else {
2014-08-25 10:18:23 +04:00
c.resp.writeInteger(n)
2014-05-06 09:32:38 +04:00
}
2014-05-03 10:55:12 +04:00
return nil
}
2014-08-25 10:18:23 +04:00
func rpushCommand(c *client) error {
args := c.args
2014-05-06 09:32:38 +04:00
if len(args) < 2 {
return ErrCmdParams
}
2014-08-25 10:18:23 +04:00
if n, err := c.db.RPush(args[0], args[1:]...); err != nil {
2014-05-06 09:32:38 +04:00
return err
} else {
2014-08-25 10:18:23 +04:00
c.resp.writeInteger(n)
2014-05-06 09:32:38 +04:00
}
2014-05-03 10:55:12 +04:00
return nil
}
2014-08-25 10:18:23 +04:00
func lpopCommand(c *client) error {
args := c.args
2014-05-06 09:32:38 +04:00
if len(args) != 1 {
return ErrCmdParams
}
2014-08-25 10:18:23 +04:00
if v, err := c.db.LPop(args[0]); err != nil {
2014-05-06 09:32:38 +04:00
return err
} else {
2014-08-25 10:18:23 +04:00
c.resp.writeBulk(v)
2014-05-06 09:32:38 +04:00
}
2014-05-03 10:55:12 +04:00
return nil
}
2014-08-25 10:18:23 +04:00
func rpopCommand(c *client) error {
args := c.args
2014-05-06 09:32:38 +04:00
if len(args) != 1 {
return ErrCmdParams
}
2014-08-25 10:18:23 +04:00
if v, err := c.db.RPop(args[0]); err != nil {
2014-05-06 09:32:38 +04:00
return err
} else {
2014-08-25 10:18:23 +04:00
c.resp.writeBulk(v)
2014-05-06 09:32:38 +04:00
}
2014-05-03 10:55:12 +04:00
return nil
}
2014-08-25 10:18:23 +04:00
func llenCommand(c *client) error {
args := c.args
2014-05-06 09:32:38 +04:00
if len(args) != 1 {
return ErrCmdParams
}
2014-08-25 10:18:23 +04:00
if n, err := c.db.LLen(args[0]); err != nil {
2014-05-06 09:32:38 +04:00
return err
} else {
2014-08-25 10:18:23 +04:00
c.resp.writeInteger(n)
2014-05-06 09:32:38 +04:00
}
2014-05-03 10:55:12 +04:00
return nil
}
2014-08-25 10:18:23 +04:00
func lindexCommand(c *client) error {
args := c.args
2014-05-06 09:32:38 +04:00
if len(args) != 2 {
return ErrCmdParams
}
index, err := ledis.StrInt64(args[1], nil)
2014-05-06 09:32:38 +04:00
if err != nil {
return ErrValue
2014-05-06 09:32:38 +04:00
}
2014-08-25 10:18:23 +04:00
if v, err := c.db.LIndex(args[0], int32(index)); err != nil {
2014-05-06 09:32:38 +04:00
return err
} else {
2014-08-25 10:18:23 +04:00
c.resp.writeBulk(v)
2014-05-06 09:32:38 +04:00
}
2014-05-03 10:55:12 +04:00
return nil
}
2014-08-25 10:18:23 +04:00
func lrangeCommand(c *client) error {
args := c.args
2014-05-06 09:32:38 +04:00
if len(args) != 3 {
return ErrCmdParams
}
var start int64
var stop int64
var err error
start, err = ledis.StrInt64(args[1], nil)
2014-05-06 09:32:38 +04:00
if err != nil {
return ErrValue
2014-05-06 09:32:38 +04:00
}
stop, err = ledis.StrInt64(args[2], nil)
2014-05-06 09:32:38 +04:00
if err != nil {
return ErrValue
2014-05-06 09:32:38 +04:00
}
2014-08-25 10:18:23 +04:00
if v, err := c.db.LRange(args[0], int32(start), int32(stop)); err != nil {
2014-05-06 09:32:38 +04:00
return err
} else {
2014-08-25 10:18:23 +04:00
c.resp.writeSliceArray(v)
2014-05-06 09:32:38 +04:00
}
2014-05-03 10:55:12 +04:00
return nil
}
2014-08-25 10:18:23 +04:00
func lclearCommand(c *client) error {
args := c.args
2014-05-12 11:08:59 +04:00
if len(args) != 1 {
return ErrCmdParams
}
2014-08-25 10:18:23 +04:00
if n, err := c.db.LClear(args[0]); err != nil {
2014-05-12 11:08:59 +04:00
return err
} else {
2014-08-25 10:18:23 +04:00
c.resp.writeInteger(n)
2014-05-12 11:08:59 +04:00
}
return nil
}
2014-08-25 10:18:23 +04:00
func lmclearCommand(c *client) error {
args := c.args
if len(args) < 1 {
return ErrCmdParams
}
2014-08-25 10:18:23 +04:00
if n, err := c.db.LMclear(args...); err != nil {
return err
} else {
2014-08-25 10:18:23 +04:00
c.resp.writeInteger(n)
}
return nil
}
2014-08-25 10:18:23 +04:00
func lexpireCommand(c *client) error {
args := c.args
2014-06-30 07:23:14 +04:00
if len(args) != 2 {
2014-06-16 15:24:37 +04:00
return ErrCmdParams
}
duration, err := ledis.StrInt64(args[1], nil)
if err != nil {
return ErrValue
2014-06-16 15:24:37 +04:00
}
2014-08-25 10:18:23 +04:00
if v, err := c.db.LExpire(args[0], duration); err != nil {
2014-06-16 15:24:37 +04:00
return err
} else {
2014-08-25 10:18:23 +04:00
c.resp.writeInteger(v)
2014-06-16 15:24:37 +04:00
}
return nil
}
2014-08-25 10:18:23 +04:00
func lexpireAtCommand(c *client) error {
args := c.args
2014-06-30 07:23:14 +04:00
if len(args) != 2 {
2014-06-16 15:24:37 +04:00
return ErrCmdParams
}
when, err := ledis.StrInt64(args[1], nil)
if err != nil {
return ErrValue
2014-06-16 15:24:37 +04:00
}
2014-08-25 10:18:23 +04:00
if v, err := c.db.LExpireAt(args[0], when); err != nil {
2014-06-16 15:24:37 +04:00
return err
} else {
2014-08-25 10:18:23 +04:00
c.resp.writeInteger(v)
2014-06-16 15:24:37 +04:00
}
return nil
}
2014-08-25 10:18:23 +04:00
func lttlCommand(c *client) error {
args := c.args
2014-06-30 07:23:14 +04:00
if len(args) != 1 {
2014-06-16 15:24:37 +04:00
return ErrCmdParams
}
2014-08-25 10:18:23 +04:00
if v, err := c.db.LTTL(args[0]); err != nil {
2014-06-16 15:24:37 +04:00
return err
} else {
2014-08-25 10:18:23 +04:00
c.resp.writeInteger(v)
2014-06-16 15:24:37 +04:00
}
return nil
}
2014-08-25 10:18:23 +04:00
func lpersistCommand(c *client) error {
args := c.args
2014-06-24 08:44:44 +04:00
if len(args) != 1 {
return ErrCmdParams
}
2014-08-25 10:18:23 +04:00
if n, err := c.db.LPersist(args[0]); err != nil {
2014-06-24 08:44:44 +04:00
return err
} else {
2014-08-25 10:18:23 +04:00
c.resp.writeInteger(n)
2014-06-24 08:44:44 +04:00
}
return nil
}
2014-09-09 05:53:26 +04:00
func lxscanCommand(c *client) error {
2014-08-27 06:29:17 +04:00
key, match, count, err := parseScanArgs(c)
2014-08-26 19:21:45 +04:00
if err != nil {
return err
}
2014-08-27 06:29:17 +04:00
if ay, err := c.db.LScan(key, count, false, match); err != nil {
2014-08-26 19:21:45 +04:00
return err
} else {
data := make([]interface{}, 2)
if len(ay) < count {
2014-08-27 06:29:17 +04:00
data[0] = []byte("")
2014-08-26 19:21:45 +04:00
} else {
2014-08-27 06:29:17 +04:00
data[0] = ay[len(ay)-1]
2014-08-26 19:21:45 +04:00
}
data[1] = ay
c.resp.writeArray(data)
}
return nil
}
2014-10-17 05:55:43 +04:00
func blpopCommand(c *client) error {
keys, timeout, err := lParseBPopArgs(c)
if err != nil {
return err
}
if ay, err := c.db.BLPop(keys, timeout); err != nil {
return err
} else {
c.resp.writeArray(ay)
}
return nil
}
func brpopCommand(c *client) error {
keys, timeout, err := lParseBPopArgs(c)
if err != nil {
return err
}
if ay, err := c.db.BRPop(keys, timeout); err != nil {
return err
} else {
c.resp.writeArray(ay)
}
return nil
}
func lParseBPopArgs(c *client) (keys [][]byte, timeout int, err error) {
args := c.args
if len(args) < 2 {
err = ErrCmdParams
return
}
if timeout, err = strconv.Atoi(hack.String(args[len(args)-1])); err != nil {
return
}
keys = args[0 : len(args)-1]
return
}
2014-05-03 10:55:12 +04:00
func init() {
2014-10-17 05:55:43 +04:00
register("blpop", blpopCommand)
register("brpop", brpopCommand)
2014-05-03 10:55:12 +04:00
register("lindex", lindexCommand)
register("llen", llenCommand)
register("lpop", lpopCommand)
register("lrange", lrangeCommand)
register("lpush", lpushCommand)
register("rpop", rpopCommand)
register("rpush", rpushCommand)
2014-05-12 11:08:59 +04:00
//ledisdb special command
register("lclear", lclearCommand)
register("lmclear", lmclearCommand)
2014-06-16 15:24:37 +04:00
register("lexpire", lexpireCommand)
register("lexpireat", lexpireAtCommand)
register("lttl", lttlCommand)
2014-06-24 08:44:44 +04:00
register("lpersist", lpersistCommand)
2014-09-09 05:53:26 +04:00
register("lxscan", lxscanCommand)
2014-05-03 10:55:12 +04:00
}