ledisdb/server/cmd_list.go

504 lines
8.2 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
"strconv"
"time"
2015-05-04 17:42:28 +03:00
"bytes"
2020-04-24 09:10:03 +03:00
"github.com/ledisdb/ledisdb/ledis"
2015-05-04 17:42:28 +03:00
"github.com/siddontang/go/hack"
)
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
}
n, err := c.db.LPush(args[0], args[1:]...)
if err != nil {
2014-05-06 09:32:38 +04:00
return err
}
c.resp.writeInteger(n)
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
}
n, err := c.db.RPush(args[0], args[1:]...)
if err != nil {
2014-05-06 09:32:38 +04:00
return err
}
c.resp.writeInteger(n)
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
}
v, err := c.db.LPop(args[0])
if err != nil {
2014-05-06 09:32:38 +04:00
return err
}
c.resp.writeBulk(v)
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
}
v, err := c.db.RPop(args[0])
if err != nil {
2014-05-06 09:32:38 +04:00
return err
}
c.resp.writeBulk(v)
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
}
n, err := c.db.LLen(args[0])
if err != nil {
2014-05-06 09:32:38 +04:00
return err
}
c.resp.writeInteger(n)
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
}
v, err := c.db.LIndex(args[0], int32(index))
if err != nil {
2014-05-06 09:32:38 +04:00
return err
}
c.resp.writeBulk(v)
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
}
v, err := c.db.LRange(args[0], int32(start), int32(stop))
if err != nil {
2014-05-06 09:32:38 +04:00
return err
}
c.resp.writeSliceArray(v)
2014-05-03 10:55:12 +04:00
return nil
}
func lsetCommand(c *client) error {
args := c.args
if len(args) != 3 {
return ErrCmdParams
}
index, err := ledis.StrInt64(args[1], nil)
if err != nil {
return ErrValue
}
if err := c.db.LSet(args[0], int32(index), args[2]); err != nil {
return err
}
c.resp.writeStatus(OK)
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
}
n, err := c.db.LClear(args[0])
if err != nil {
2014-05-12 11:08:59 +04:00
return err
}
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
}
n, err := c.db.LMclear(args...)
if err != nil {
return err
}
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
}
v, err := c.db.LExpire(args[0], duration)
if err != nil {
2014-06-16 15:24:37 +04:00
return err
}
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
}
v, err := c.db.LExpireAt(args[0], when)
if err != nil {
2014-06-16 15:24:37 +04:00
return err
}
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
}
v, err := c.db.LTTL(args[0])
if err != nil {
2014-06-16 15:24:37 +04:00
return err
}
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
}
n, err := c.db.LPersist(args[0])
if err != nil {
2014-06-24 08:44:44 +04:00
return err
}
c.resp.writeInteger(n)
2014-06-24 08:44:44 +04:00
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
}
ay, err := c.db.BLPop(keys, timeout)
if err != nil {
2014-10-17 05:55:43 +04:00
return err
}
c.resp.writeArray(ay)
2014-10-17 05:55:43 +04:00
return nil
}
func brpopCommand(c *client) error {
keys, timeout, err := lParseBPopArgs(c)
if err != nil {
return err
}
ay, err := c.db.BRPop(keys, timeout)
if err != nil {
2014-10-17 05:55:43 +04:00
return err
}
c.resp.writeArray(ay)
2014-10-17 05:55:43 +04:00
return nil
}
func lParseBPopArgs(c *client) (keys [][]byte, timeout time.Duration, err error) {
2014-10-17 05:55:43 +04:00
args := c.args
if len(args) < 2 {
err = ErrCmdParams
return
}
var t float64
if t, err = strconv.ParseFloat(hack.String(args[len(args)-1]), 64); err != nil {
2014-10-17 05:55:43 +04:00
return
}
timeout = time.Duration(t * float64(time.Second))
2014-10-17 05:55:43 +04:00
keys = args[0 : len(args)-1]
return
}
func brpoplpushCommand(c *client) error {
source, dest, timeout, err := lParseBRPoplpushArgs(c)
if err != nil {
return err
}
var ttl int64 = -1
if bytes.Compare(source, dest) == 0 {
var err error
ttl, err = c.db.LTTL(source)
if err != nil {
return err
}
}
ay, err := c.db.BRPop([][]byte{source}, timeout)
if err != nil {
return err
}
if ay == nil {
c.resp.writeBulk(nil)
return nil
}
data, ok := ay[1].([]byte)
if !ok {
//not sure if this even possible
return ErrValue
}
if _, err := c.db.LPush(dest, data); err != nil {
c.db.RPush(source, data) //revert pop
return err
}
//reset ttl
if ttl != -1 {
c.db.LExpire(source, ttl)
}
c.resp.writeBulk(data)
return nil
}
func lParseBRPoplpushArgs(c *client) (source []byte, dest []byte, timeout time.Duration, err error) {
args := c.args
if len(args) != 3 {
err = ErrCmdParams
return
}
source = args[0]
dest = args[1]
var t float64
if t, err = strconv.ParseFloat(hack.String(args[len(args)-1]), 64); err != nil {
return
}
timeout = time.Duration(t * float64(time.Second))
return
}
func rpoplpushCommand(c *client) error {
args := c.args
if len(args) != 2 {
return ErrCmdParams
}
source, dest := args[0], args[1]
var ttl int64 = -1
if bytes.Compare(source, dest) == 0 {
var err error
ttl, err = c.db.LTTL(source)
if err != nil {
return err
}
}
data, err := c.db.RPop(source)
if err != nil {
return err
}
if data == nil {
c.resp.writeBulk(nil)
return nil
}
if _, err := c.db.LPush(dest, data); err != nil {
c.db.RPush(source, data) //revert pop
return err
}
//reset ttl
if ttl != -1 {
c.db.LExpire(source, ttl)
}
c.resp.writeBulk(data)
return nil
}
func lkeyexistsCommand(c *client) error {
2015-02-01 12:41:28 +03:00
args := c.args
if len(args) != 1 {
return ErrCmdParams
}
n, err := c.db.LKeyExists(args[0])
if err != nil {
2015-02-01 12:41:28 +03:00
return err
}
c.resp.writeInteger(n)
2015-02-01 12:41:28 +03:00
return nil
}
2014-10-17 05:55:43 +04:00
2015-11-26 10:04:21 +03:00
func lTrimCommand(c *client) error {
args := c.args
if len(args) != 3 {
return ErrCmdParams
}
var start int64
var stop int64
var err error
start, err = ledis.StrInt64(args[1], nil)
if err != nil {
return ErrValue
}
stop, err = ledis.StrInt64(args[2], nil)
if err != nil {
return ErrValue
}
if err := c.db.LTrim(args[0], start, stop); err != nil {
return err
}
c.resp.writeStatus(OK)
2015-11-26 10:04:21 +03:00
return nil
}
func lTrimFrontCommand(c *client) error {
args := c.args
if len(args) != 2 {
return ErrCmdParams
}
var trimSize int32
var err error
trimSize, err = ledis.StrInt32(args[1], nil)
if err != nil || trimSize < 0 {
return ErrValue
}
n, err := c.db.LTrimFront(args[0], trimSize)
if err != nil {
return err
}
c.resp.writeInteger(int64(n))
return nil
}
func lTrimBackCommand(c *client) error {
args := c.args
if len(args) != 2 {
return ErrCmdParams
}
var trimSize int32
var err error
trimSize, err = ledis.StrInt32(args[1], nil)
if err != nil || trimSize < 0 {
return ErrValue
}
n, err := c.db.LTrimBack(args[0], trimSize)
if err != nil {
return err
}
c.resp.writeInteger(int64(n))
return nil
}
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("lset", lsetCommand)
2014-05-03 10:55:12 +04:00
register("lpush", lpushCommand)
register("rpop", rpopCommand)
register("rpush", rpushCommand)
register("brpoplpush", brpoplpushCommand)
register("rpoplpush", rpoplpushCommand)
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)
register("lkeyexists", lkeyexistsCommand)
register("ltrim_front", lTrimFrontCommand)
register("ltrim_back", lTrimBackCommand)
2015-11-26 10:04:21 +03:00
register("ltrim", lTrimCommand)
2014-05-03 10:55:12 +04:00
}