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-04 15:02:55 +04:00
|
|
|
|
2014-07-31 14:28:19 +04:00
|
|
|
func getCommand(req *requestContext) error {
|
|
|
|
args := req.args
|
2014-05-04 15:02:55 +04:00
|
|
|
if len(args) != 1 {
|
|
|
|
return ErrCmdParams
|
|
|
|
}
|
|
|
|
|
2014-07-31 14:28:19 +04:00
|
|
|
if v, err := req.db.Get(args[0]); err != nil {
|
2014-05-04 15:02:55 +04:00
|
|
|
return err
|
|
|
|
} else {
|
2014-07-31 14:28:19 +04:00
|
|
|
req.resp.writeBulk(v)
|
2014-05-04 15:02:55 +04:00
|
|
|
}
|
2014-05-03 10:55:12 +04:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-07-31 14:28:19 +04:00
|
|
|
func setCommand(req *requestContext) error {
|
|
|
|
args := req.args
|
2014-06-11 12:49:17 +04:00
|
|
|
if len(args) != 2 {
|
2014-05-04 15:02:55 +04:00
|
|
|
return ErrCmdParams
|
|
|
|
}
|
|
|
|
|
2014-07-31 14:28:19 +04:00
|
|
|
if err := req.db.Set(args[0], args[1]); err != nil {
|
2014-05-04 15:02:55 +04:00
|
|
|
return err
|
|
|
|
} else {
|
2014-07-31 14:28:19 +04:00
|
|
|
req.resp.writeStatus(OK)
|
2014-05-04 15:02:55 +04:00
|
|
|
}
|
|
|
|
|
2014-05-03 10:55:12 +04:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-07-31 14:28:19 +04:00
|
|
|
func getsetCommand(req *requestContext) error {
|
|
|
|
args := req.args
|
2014-05-04 15:02:55 +04:00
|
|
|
if len(args) != 2 {
|
|
|
|
return ErrCmdParams
|
|
|
|
}
|
|
|
|
|
2014-07-31 14:28:19 +04:00
|
|
|
if v, err := req.db.GetSet(args[0], args[1]); err != nil {
|
2014-05-04 15:02:55 +04:00
|
|
|
return err
|
|
|
|
} else {
|
2014-07-31 14:28:19 +04:00
|
|
|
req.resp.writeBulk(v)
|
2014-05-04 15:02:55 +04:00
|
|
|
}
|
|
|
|
|
2014-05-03 10:55:12 +04:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-07-31 14:28:19 +04:00
|
|
|
func setnxCommand(req *requestContext) error {
|
|
|
|
args := req.args
|
2014-05-04 15:02:55 +04:00
|
|
|
if len(args) != 2 {
|
|
|
|
return ErrCmdParams
|
|
|
|
}
|
|
|
|
|
2014-07-31 14:28:19 +04:00
|
|
|
if n, err := req.db.SetNX(args[0], args[1]); err != nil {
|
2014-05-04 15:02:55 +04:00
|
|
|
return err
|
|
|
|
} else {
|
2014-07-31 14:28:19 +04:00
|
|
|
req.resp.writeInteger(n)
|
2014-05-04 15:02:55 +04:00
|
|
|
}
|
|
|
|
|
2014-05-03 10:55:12 +04:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-07-31 14:28:19 +04:00
|
|
|
func existsCommand(req *requestContext) error {
|
|
|
|
args := req.args
|
2014-05-04 15:02:55 +04:00
|
|
|
if len(args) != 1 {
|
|
|
|
return ErrCmdParams
|
|
|
|
}
|
|
|
|
|
2014-07-31 14:28:19 +04:00
|
|
|
if n, err := req.db.Exists(args[0]); err != nil {
|
2014-05-04 15:02:55 +04:00
|
|
|
return err
|
|
|
|
} else {
|
2014-07-31 14:28:19 +04:00
|
|
|
req.resp.writeInteger(n)
|
2014-05-04 15:02:55 +04:00
|
|
|
}
|
|
|
|
|
2014-05-03 10:55:12 +04:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-07-31 14:28:19 +04:00
|
|
|
func incrCommand(req *requestContext) error {
|
|
|
|
args := req.args
|
2014-05-04 15:02:55 +04:00
|
|
|
if len(args) != 1 {
|
|
|
|
return ErrCmdParams
|
|
|
|
}
|
|
|
|
|
2014-07-31 14:28:19 +04:00
|
|
|
if n, err := req.db.Incr(req.args[0]); err != nil {
|
2014-05-04 15:02:55 +04:00
|
|
|
return err
|
|
|
|
} else {
|
2014-07-31 14:28:19 +04:00
|
|
|
req.resp.writeInteger(n)
|
2014-05-04 15:02:55 +04:00
|
|
|
}
|
|
|
|
|
2014-05-03 10:55:12 +04:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-07-31 14:28:19 +04:00
|
|
|
func decrCommand(req *requestContext) error {
|
|
|
|
args := req.args
|
2014-05-04 15:02:55 +04:00
|
|
|
if len(args) != 1 {
|
|
|
|
return ErrCmdParams
|
|
|
|
}
|
|
|
|
|
2014-07-31 14:28:19 +04:00
|
|
|
if n, err := req.db.Decr(req.args[0]); err != nil {
|
2014-05-04 15:02:55 +04:00
|
|
|
return err
|
|
|
|
} else {
|
2014-07-31 14:28:19 +04:00
|
|
|
req.resp.writeInteger(n)
|
2014-05-04 15:02:55 +04:00
|
|
|
}
|
|
|
|
|
2014-05-03 10:55:12 +04:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-07-31 14:28:19 +04:00
|
|
|
func incrbyCommand(req *requestContext) error {
|
|
|
|
args := req.args
|
2014-05-04 15:02:55 +04:00
|
|
|
if len(args) != 2 {
|
|
|
|
return ErrCmdParams
|
|
|
|
}
|
|
|
|
|
2014-05-16 11:03:23 +04:00
|
|
|
delta, err := ledis.StrInt64(args[1], nil)
|
2014-05-04 15:02:55 +04:00
|
|
|
if err != nil {
|
2014-07-25 13:26:29 +04:00
|
|
|
return ErrValue
|
2014-05-04 15:02:55 +04:00
|
|
|
}
|
|
|
|
|
2014-08-04 07:26:08 +04:00
|
|
|
if n, err := req.db.IncrBy(req.args[0], delta); err != nil {
|
2014-05-04 15:02:55 +04:00
|
|
|
return err
|
|
|
|
} else {
|
2014-07-31 14:28:19 +04:00
|
|
|
req.resp.writeInteger(n)
|
2014-05-04 15:02:55 +04:00
|
|
|
}
|
|
|
|
|
2014-05-03 10:55:12 +04:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-07-31 14:28:19 +04:00
|
|
|
func decrbyCommand(req *requestContext) error {
|
|
|
|
args := req.args
|
2014-05-04 15:02:55 +04:00
|
|
|
if len(args) != 2 {
|
|
|
|
return ErrCmdParams
|
|
|
|
}
|
|
|
|
|
2014-05-16 11:03:23 +04:00
|
|
|
delta, err := ledis.StrInt64(args[1], nil)
|
2014-05-04 15:02:55 +04:00
|
|
|
if err != nil {
|
2014-07-25 13:26:29 +04:00
|
|
|
return ErrValue
|
2014-05-04 15:02:55 +04:00
|
|
|
}
|
|
|
|
|
2014-07-31 14:28:19 +04:00
|
|
|
if n, err := req.db.DecrBy(req.args[0], delta); err != nil {
|
2014-05-04 15:02:55 +04:00
|
|
|
return err
|
|
|
|
} else {
|
2014-07-31 14:28:19 +04:00
|
|
|
req.resp.writeInteger(n)
|
2014-05-04 15:02:55 +04:00
|
|
|
}
|
|
|
|
|
2014-05-03 10:55:12 +04:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-07-31 14:28:19 +04:00
|
|
|
func delCommand(req *requestContext) error {
|
|
|
|
args := req.args
|
2014-05-04 15:02:55 +04:00
|
|
|
if len(args) == 0 {
|
|
|
|
return ErrCmdParams
|
|
|
|
}
|
|
|
|
|
2014-07-31 14:28:19 +04:00
|
|
|
if n, err := req.db.Del(args...); err != nil {
|
2014-05-04 15:02:55 +04:00
|
|
|
return err
|
|
|
|
} else {
|
2014-07-31 14:28:19 +04:00
|
|
|
req.resp.writeInteger(n)
|
2014-05-04 15:02:55 +04:00
|
|
|
}
|
|
|
|
|
2014-05-03 10:55:12 +04:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-07-31 14:28:19 +04:00
|
|
|
func msetCommand(req *requestContext) error {
|
|
|
|
args := req.args
|
2014-05-04 15:02:55 +04:00
|
|
|
if len(args) == 0 || len(args)%2 != 0 {
|
|
|
|
return ErrCmdParams
|
|
|
|
}
|
|
|
|
|
2014-05-16 11:03:23 +04:00
|
|
|
kvs := make([]ledis.KVPair, len(args)/2)
|
2014-05-16 04:56:32 +04:00
|
|
|
for i := 0; i < len(kvs); i++ {
|
|
|
|
kvs[i].Key = args[2*i]
|
|
|
|
kvs[i].Value = args[2*i+1]
|
|
|
|
}
|
|
|
|
|
2014-07-31 14:28:19 +04:00
|
|
|
if err := req.db.MSet(kvs...); err != nil {
|
2014-05-04 15:02:55 +04:00
|
|
|
return err
|
|
|
|
} else {
|
2014-07-31 14:28:19 +04:00
|
|
|
req.resp.writeStatus(OK)
|
2014-05-04 15:02:55 +04:00
|
|
|
}
|
2014-05-03 10:55:12 +04:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-07-31 14:28:19 +04:00
|
|
|
// func setexCommand(req *requestContext) error {
|
2014-05-04 15:02:55 +04:00
|
|
|
// return nil
|
|
|
|
// }
|
|
|
|
|
2014-07-31 14:28:19 +04:00
|
|
|
func mgetCommand(req *requestContext) error {
|
|
|
|
args := req.args
|
2014-05-04 15:02:55 +04:00
|
|
|
if len(args) == 0 {
|
|
|
|
return ErrCmdParams
|
|
|
|
}
|
|
|
|
|
2014-07-31 14:28:19 +04:00
|
|
|
if v, err := req.db.MGet(args...); err != nil {
|
2014-05-04 15:02:55 +04:00
|
|
|
return err
|
|
|
|
} else {
|
2014-07-31 14:28:19 +04:00
|
|
|
req.resp.writeSliceArray(v)
|
2014-05-04 15:02:55 +04:00
|
|
|
}
|
|
|
|
|
2014-05-03 10:55:12 +04:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-07-31 14:28:19 +04:00
|
|
|
func expireCommand(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.Expire(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 expireAtCommand(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.ExpireAt(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 ttlCommand(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.TTL(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 persistCommand(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.Persist(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-06-16 15:24:37 +04:00
|
|
|
// func (db *DB) Expire(key []byte, duration int6
|
|
|
|
// func (db *DB) ExpireAt(key []byte, when int64)
|
|
|
|
// func (db *DB) TTL(key []byte) (int64, error)
|
|
|
|
|
2014-05-03 10:55:12 +04:00
|
|
|
func init() {
|
|
|
|
register("decr", decrCommand)
|
|
|
|
register("decrby", decrbyCommand)
|
|
|
|
register("del", delCommand)
|
|
|
|
register("exists", existsCommand)
|
|
|
|
register("get", getCommand)
|
|
|
|
register("getset", getsetCommand)
|
|
|
|
register("incr", incrCommand)
|
|
|
|
register("incrby", incrbyCommand)
|
|
|
|
register("mget", mgetCommand)
|
|
|
|
register("mset", msetCommand)
|
|
|
|
register("set", setCommand)
|
|
|
|
register("setnx", setnxCommand)
|
2014-06-16 15:24:37 +04:00
|
|
|
register("expire", expireCommand)
|
|
|
|
register("expireat", expireAtCommand)
|
|
|
|
register("ttl", ttlCommand)
|
2014-06-24 08:44:44 +04:00
|
|
|
register("persist", persistCommand)
|
2014-05-03 10:55:12 +04:00
|
|
|
}
|