2014-05-16 11:03:23 +04:00
|
|
|
package server
|
2014-05-03 10:55:12 +04:00
|
|
|
|
2014-05-16 11:03:23 +04:00
|
|
|
import (
|
|
|
|
"github.com/siddontang/ledisdb/ledis"
|
|
|
|
)
|
2014-05-06 09:32:38 +04:00
|
|
|
|
2014-07-31 14:28:19 +04:00
|
|
|
func lpushCommand(req *requestContext) error {
|
|
|
|
args := req.args
|
2014-05-06 09:32:38 +04:00
|
|
|
if len(args) < 2 {
|
|
|
|
return ErrCmdParams
|
|
|
|
}
|
|
|
|
|
2014-07-31 14:28:19 +04:00
|
|
|
if n, err := req.db.LPush(args[0], args[1:]...); err != nil {
|
2014-05-06 09:32:38 +04:00
|
|
|
return err
|
|
|
|
} else {
|
2014-07-31 14:28:19 +04:00
|
|
|
req.resp.writeInteger(n)
|
2014-05-06 09:32:38 +04:00
|
|
|
}
|
|
|
|
|
2014-05-03 10:55:12 +04:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-07-31 14:28:19 +04:00
|
|
|
func rpushCommand(req *requestContext) error {
|
|
|
|
args := req.args
|
2014-05-06 09:32:38 +04:00
|
|
|
if len(args) < 2 {
|
|
|
|
return ErrCmdParams
|
|
|
|
}
|
|
|
|
|
2014-07-31 14:28:19 +04:00
|
|
|
if n, err := req.db.RPush(args[0], args[1:]...); err != nil {
|
2014-05-06 09:32:38 +04:00
|
|
|
return err
|
|
|
|
} else {
|
2014-07-31 14:28:19 +04:00
|
|
|
req.resp.writeInteger(n)
|
2014-05-06 09:32:38 +04:00
|
|
|
}
|
|
|
|
|
2014-05-03 10:55:12 +04:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-07-31 14:28:19 +04:00
|
|
|
func lpopCommand(req *requestContext) error {
|
|
|
|
args := req.args
|
2014-05-06 09:32:38 +04:00
|
|
|
if len(args) != 1 {
|
|
|
|
return ErrCmdParams
|
|
|
|
}
|
|
|
|
|
2014-07-31 14:28:19 +04:00
|
|
|
if v, err := req.db.LPop(args[0]); err != nil {
|
2014-05-06 09:32:38 +04:00
|
|
|
return err
|
|
|
|
} else {
|
2014-07-31 14:28:19 +04:00
|
|
|
req.resp.writeBulk(v)
|
2014-05-06 09:32:38 +04:00
|
|
|
}
|
|
|
|
|
2014-05-03 10:55:12 +04:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-07-31 14:28:19 +04:00
|
|
|
func rpopCommand(req *requestContext) error {
|
|
|
|
args := req.args
|
2014-05-06 09:32:38 +04:00
|
|
|
if len(args) != 1 {
|
|
|
|
return ErrCmdParams
|
|
|
|
}
|
|
|
|
|
2014-07-31 14:28:19 +04:00
|
|
|
if v, err := req.db.RPop(args[0]); err != nil {
|
2014-05-06 09:32:38 +04:00
|
|
|
return err
|
|
|
|
} else {
|
2014-07-31 14:28:19 +04:00
|
|
|
req.resp.writeBulk(v)
|
2014-05-06 09:32:38 +04:00
|
|
|
}
|
|
|
|
|
2014-05-03 10:55:12 +04:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-07-31 14:28:19 +04:00
|
|
|
func llenCommand(req *requestContext) error {
|
|
|
|
args := req.args
|
2014-05-06 09:32:38 +04:00
|
|
|
if len(args) != 1 {
|
|
|
|
return ErrCmdParams
|
|
|
|
}
|
|
|
|
|
2014-07-31 14:28:19 +04:00
|
|
|
if n, err := req.db.LLen(args[0]); err != nil {
|
2014-05-06 09:32:38 +04:00
|
|
|
return err
|
|
|
|
} else {
|
2014-07-31 14:28:19 +04:00
|
|
|
req.resp.writeInteger(n)
|
2014-05-06 09:32:38 +04:00
|
|
|
}
|
|
|
|
|
2014-05-03 10:55:12 +04:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-07-31 14:28:19 +04:00
|
|
|
func lindexCommand(req *requestContext) error {
|
|
|
|
args := req.args
|
2014-05-06 09:32:38 +04:00
|
|
|
if len(args) != 2 {
|
|
|
|
return ErrCmdParams
|
|
|
|
}
|
|
|
|
|
2014-05-16 11:03:23 +04:00
|
|
|
index, err := ledis.StrInt64(args[1], nil)
|
2014-05-06 09:32:38 +04:00
|
|
|
if err != nil {
|
2014-07-25 13:26:29 +04:00
|
|
|
return ErrValue
|
2014-05-06 09:32:38 +04:00
|
|
|
}
|
|
|
|
|
2014-07-31 14:28:19 +04:00
|
|
|
if v, err := req.db.LIndex(args[0], int32(index)); err != nil {
|
2014-05-06 09:32:38 +04:00
|
|
|
return err
|
|
|
|
} else {
|
2014-07-31 14:28:19 +04:00
|
|
|
req.resp.writeBulk(v)
|
2014-05-06 09:32:38 +04:00
|
|
|
}
|
|
|
|
|
2014-05-03 10:55:12 +04:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-07-31 14:28:19 +04:00
|
|
|
func lrangeCommand(req *requestContext) error {
|
|
|
|
args := req.args
|
2014-05-06 09:32:38 +04:00
|
|
|
if len(args) != 3 {
|
|
|
|
return ErrCmdParams
|
|
|
|
}
|
|
|
|
|
|
|
|
var start int64
|
|
|
|
var stop int64
|
|
|
|
var err error
|
|
|
|
|
2014-05-16 11:03:23 +04:00
|
|
|
start, err = ledis.StrInt64(args[1], nil)
|
2014-05-06 09:32:38 +04:00
|
|
|
if err != nil {
|
2014-07-25 13:26:29 +04:00
|
|
|
return ErrValue
|
2014-05-06 09:32:38 +04:00
|
|
|
}
|
|
|
|
|
2014-05-16 11:03:23 +04:00
|
|
|
stop, err = ledis.StrInt64(args[2], nil)
|
2014-05-06 09:32:38 +04:00
|
|
|
if err != nil {
|
2014-07-25 13:26:29 +04:00
|
|
|
return ErrValue
|
2014-05-06 09:32:38 +04:00
|
|
|
}
|
|
|
|
|
2014-07-31 14:28:19 +04:00
|
|
|
if v, err := req.db.LRange(args[0], int32(start), int32(stop)); err != nil {
|
2014-05-06 09:32:38 +04:00
|
|
|
return err
|
|
|
|
} else {
|
2014-07-31 14:28:19 +04:00
|
|
|
req.resp.writeSliceArray(v)
|
2014-05-06 09:32:38 +04:00
|
|
|
}
|
|
|
|
|
2014-05-03 10:55:12 +04:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-07-31 14:28:19 +04:00
|
|
|
func lclearCommand(req *requestContext) error {
|
|
|
|
args := req.args
|
2014-05-12 11:08:59 +04:00
|
|
|
if len(args) != 1 {
|
|
|
|
return ErrCmdParams
|
|
|
|
}
|
|
|
|
|
2014-07-31 14:28:19 +04:00
|
|
|
if n, err := req.db.LClear(args[0]); err != nil {
|
2014-05-12 11:08:59 +04:00
|
|
|
return err
|
|
|
|
} else {
|
2014-07-31 14:28:19 +04:00
|
|
|
req.resp.writeInteger(n)
|
2014-05-12 11:08:59 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-07-31 14:28:19 +04:00
|
|
|
func lmclearCommand(req *requestContext) error {
|
|
|
|
args := req.args
|
2014-06-27 12:28:26 +04:00
|
|
|
if len(args) < 1 {
|
|
|
|
return ErrCmdParams
|
|
|
|
}
|
|
|
|
|
2014-07-31 14:28:19 +04:00
|
|
|
if n, err := req.db.LMclear(args...); err != nil {
|
2014-06-27 12:28:26 +04:00
|
|
|
return err
|
|
|
|
} else {
|
2014-07-31 14:28:19 +04:00
|
|
|
req.resp.writeInteger(n)
|
2014-06-27 12:28:26 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-07-31 14:28:19 +04:00
|
|
|
func lexpireCommand(req *requestContext) error {
|
|
|
|
args := req.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 {
|
2014-07-25 13:26:29 +04:00
|
|
|
return ErrValue
|
2014-06-16 15:24:37 +04:00
|
|
|
}
|
|
|
|
|
2014-07-31 14:28:19 +04:00
|
|
|
if v, err := req.db.LExpire(args[0], duration); err != nil {
|
2014-06-16 15:24:37 +04:00
|
|
|
return err
|
|
|
|
} else {
|
2014-07-31 14:28:19 +04:00
|
|
|
req.resp.writeInteger(v)
|
2014-06-16 15:24:37 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-07-31 14:28:19 +04:00
|
|
|
func lexpireAtCommand(req *requestContext) error {
|
|
|
|
args := req.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 {
|
2014-07-25 13:26:29 +04:00
|
|
|
return ErrValue
|
2014-06-16 15:24:37 +04:00
|
|
|
}
|
|
|
|
|
2014-07-31 14:28:19 +04:00
|
|
|
if v, err := req.db.LExpireAt(args[0], when); err != nil {
|
2014-06-16 15:24:37 +04:00
|
|
|
return err
|
|
|
|
} else {
|
2014-07-31 14:28:19 +04:00
|
|
|
req.resp.writeInteger(v)
|
2014-06-16 15:24:37 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-07-31 14:28:19 +04:00
|
|
|
func lttlCommand(req *requestContext) error {
|
|
|
|
args := req.args
|
2014-06-30 07:23:14 +04:00
|
|
|
if len(args) != 1 {
|
2014-06-16 15:24:37 +04:00
|
|
|
return ErrCmdParams
|
|
|
|
}
|
|
|
|
|
2014-07-31 14:28:19 +04:00
|
|
|
if v, err := req.db.LTTL(args[0]); err != nil {
|
2014-06-16 15:24:37 +04:00
|
|
|
return err
|
|
|
|
} else {
|
2014-07-31 14:28:19 +04:00
|
|
|
req.resp.writeInteger(v)
|
2014-06-16 15:24:37 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-07-31 14:28:19 +04:00
|
|
|
func lpersistCommand(req *requestContext) error {
|
|
|
|
args := req.args
|
2014-06-24 08:44:44 +04:00
|
|
|
if len(args) != 1 {
|
|
|
|
return ErrCmdParams
|
|
|
|
}
|
|
|
|
|
2014-07-31 14:28:19 +04:00
|
|
|
if n, err := req.db.LPersist(args[0]); err != nil {
|
2014-06-24 08:44:44 +04:00
|
|
|
return err
|
|
|
|
} else {
|
2014-07-31 14:28:19 +04:00
|
|
|
req.resp.writeInteger(n)
|
2014-06-24 08:44:44 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-05-03 10:55:12 +04:00
|
|
|
func init() {
|
|
|
|
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)
|
2014-06-27 12:28:26 +04:00
|
|
|
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-05-03 10:55:12 +04:00
|
|
|
}
|