2014-05-09 10:49:22 +04:00
|
|
|
package ledis
|
2014-05-03 10:55:12 +04:00
|
|
|
|
2014-05-04 15:02:55 +04:00
|
|
|
import (
|
|
|
|
"github.com/siddontang/golib/hack"
|
|
|
|
"strconv"
|
|
|
|
)
|
|
|
|
|
2014-05-03 10:55:12 +04:00
|
|
|
func getCommand(c *client) error {
|
2014-05-04 15:02:55 +04:00
|
|
|
args := c.args
|
|
|
|
if len(args) != 1 {
|
|
|
|
return ErrCmdParams
|
|
|
|
}
|
|
|
|
|
|
|
|
if v, err := c.app.kv_get(args[0]); err != nil {
|
|
|
|
return err
|
|
|
|
} else {
|
|
|
|
c.writeBulk(v)
|
|
|
|
}
|
2014-05-03 10:55:12 +04:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func setCommand(c *client) error {
|
2014-05-04 15:02:55 +04:00
|
|
|
args := c.args
|
|
|
|
if len(args) < 2 {
|
|
|
|
return ErrCmdParams
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := c.app.kv_set(args[0], args[1]); err != nil {
|
|
|
|
return err
|
|
|
|
} else {
|
|
|
|
c.writeStatus(OK)
|
|
|
|
}
|
|
|
|
|
2014-05-03 10:55:12 +04:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func getsetCommand(c *client) error {
|
2014-05-04 15:02:55 +04:00
|
|
|
args := c.args
|
|
|
|
if len(args) != 2 {
|
|
|
|
return ErrCmdParams
|
|
|
|
}
|
|
|
|
|
|
|
|
if v, err := c.app.kv_getset(args[0], args[1]); err != nil {
|
|
|
|
return err
|
|
|
|
} else {
|
|
|
|
c.writeBulk(v)
|
|
|
|
}
|
|
|
|
|
2014-05-03 10:55:12 +04:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func setnxCommand(c *client) error {
|
2014-05-04 15:02:55 +04:00
|
|
|
args := c.args
|
|
|
|
if len(args) != 2 {
|
|
|
|
return ErrCmdParams
|
|
|
|
}
|
|
|
|
|
|
|
|
if n, err := c.app.kv_setnx(args[0], args[1]); err != nil {
|
|
|
|
return err
|
|
|
|
} else {
|
|
|
|
c.writeInteger(n)
|
|
|
|
}
|
|
|
|
|
2014-05-03 10:55:12 +04:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func existsCommand(c *client) error {
|
2014-05-04 15:02:55 +04:00
|
|
|
args := c.args
|
|
|
|
if len(args) != 1 {
|
|
|
|
return ErrCmdParams
|
|
|
|
}
|
|
|
|
|
|
|
|
if n, err := c.app.kv_exists(args[0]); err != nil {
|
|
|
|
return err
|
|
|
|
} else {
|
|
|
|
c.writeInteger(n)
|
|
|
|
}
|
|
|
|
|
2014-05-03 10:55:12 +04:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func incrCommand(c *client) error {
|
2014-05-04 15:02:55 +04:00
|
|
|
args := c.args
|
|
|
|
if len(args) != 1 {
|
|
|
|
return ErrCmdParams
|
|
|
|
}
|
|
|
|
|
|
|
|
if n, err := c.app.kv_incr(c.args[0], 1); err != nil {
|
|
|
|
return err
|
|
|
|
} else {
|
|
|
|
c.writeInteger(n)
|
|
|
|
}
|
|
|
|
|
2014-05-03 10:55:12 +04:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func decrCommand(c *client) error {
|
2014-05-04 15:02:55 +04:00
|
|
|
args := c.args
|
|
|
|
if len(args) != 1 {
|
|
|
|
return ErrCmdParams
|
|
|
|
}
|
|
|
|
|
|
|
|
if n, err := c.app.kv_incr(c.args[0], -1); err != nil {
|
|
|
|
return err
|
|
|
|
} else {
|
|
|
|
c.writeInteger(n)
|
|
|
|
}
|
|
|
|
|
2014-05-03 10:55:12 +04:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func incrbyCommand(c *client) error {
|
2014-05-04 15:02:55 +04:00
|
|
|
args := c.args
|
|
|
|
if len(args) != 2 {
|
|
|
|
return ErrCmdParams
|
|
|
|
}
|
|
|
|
|
|
|
|
delta, err := strconv.ParseInt(hack.String(args[1]), 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if n, err := c.app.kv_incr(c.args[0], delta); err != nil {
|
|
|
|
return err
|
|
|
|
} else {
|
|
|
|
c.writeInteger(n)
|
|
|
|
}
|
|
|
|
|
2014-05-03 10:55:12 +04:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func decrbyCommand(c *client) error {
|
2014-05-04 15:02:55 +04:00
|
|
|
args := c.args
|
|
|
|
if len(args) != 2 {
|
|
|
|
return ErrCmdParams
|
|
|
|
}
|
|
|
|
|
|
|
|
delta, err := strconv.ParseInt(hack.String(args[1]), 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if n, err := c.app.kv_incr(c.args[0], -delta); err != nil {
|
|
|
|
return err
|
|
|
|
} else {
|
|
|
|
c.writeInteger(n)
|
|
|
|
}
|
|
|
|
|
2014-05-03 10:55:12 +04:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func delCommand(c *client) error {
|
2014-05-04 15:02:55 +04:00
|
|
|
args := c.args
|
|
|
|
if len(args) == 0 {
|
|
|
|
return ErrCmdParams
|
|
|
|
}
|
|
|
|
|
|
|
|
if n, err := c.app.tx_del(args); err != nil {
|
|
|
|
return err
|
|
|
|
} else {
|
|
|
|
c.writeInteger(n)
|
|
|
|
}
|
|
|
|
|
2014-05-03 10:55:12 +04:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func msetCommand(c *client) error {
|
2014-05-04 15:02:55 +04:00
|
|
|
args := c.args
|
|
|
|
if len(args) == 0 || len(args)%2 != 0 {
|
|
|
|
return ErrCmdParams
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := c.app.tx_mset(args); err != nil {
|
|
|
|
return err
|
|
|
|
} else {
|
|
|
|
c.writeStatus(OK)
|
|
|
|
}
|
2014-05-03 10:55:12 +04:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-05-04 15:02:55 +04:00
|
|
|
// func setexCommand(c *client) error {
|
|
|
|
// return nil
|
|
|
|
// }
|
|
|
|
|
2014-05-03 10:55:12 +04:00
|
|
|
func mgetCommand(c *client) error {
|
2014-05-04 15:02:55 +04:00
|
|
|
args := c.args
|
|
|
|
if len(args) == 0 {
|
|
|
|
return ErrCmdParams
|
|
|
|
}
|
|
|
|
|
|
|
|
if v, err := c.app.kv_mget(args); err != nil {
|
|
|
|
return err
|
|
|
|
} else {
|
|
|
|
c.writeArray(v)
|
|
|
|
}
|
|
|
|
|
2014-05-03 10:55:12 +04:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
2014-05-04 15:02:55 +04:00
|
|
|
// register("setex", setexCommand)
|
2014-05-03 10:55:12 +04:00
|
|
|
register("setnx", setnxCommand)
|
|
|
|
}
|