ledisdb/ledis/cmd_hash.go

199 lines
3.0 KiB
Go
Raw Normal View History

2014-05-09 10:49:22 +04:00
package ledis
2014-05-03 10:55:12 +04:00
2014-05-06 13:37:58 +04:00
import (
"github.com/siddontang/golib/hack"
"strconv"
)
2014-05-03 10:55:12 +04:00
func hsetCommand(c *client) error {
2014-05-06 13:37:58 +04:00
args := c.args
if len(args) != 3 {
return ErrCmdParams
}
if n, err := c.app.hash_set(args[0], args[1], args[2]); err != nil {
return err
} else {
c.writeInteger(n)
}
2014-05-03 10:55:12 +04:00
return nil
}
func hgetCommand(c *client) error {
2014-05-06 13:37:58 +04:00
args := c.args
if len(args) != 2 {
return ErrCmdParams
}
if v, err := c.app.hash_get(args[0], args[1]); err != nil {
return err
} else {
c.writeBulk(v)
}
2014-05-03 10:55:12 +04:00
return nil
}
func hexistsCommand(c *client) error {
2014-05-06 13:37:58 +04:00
args := c.args
if len(args) != 2 {
return ErrCmdParams
}
2014-05-03 10:55:12 +04:00
2014-05-06 13:37:58 +04:00
var n int64 = 1
if v, err := c.app.hash_get(args[0], args[1]); err != nil {
return err
} else {
if v == nil {
n = 0
}
c.writeInteger(n)
}
2014-05-03 10:55:12 +04:00
return nil
}
func hdelCommand(c *client) error {
2014-05-06 13:37:58 +04:00
args := c.args
if len(args) < 2 {
return ErrCmdParams
}
if n, err := c.app.hash_del(args[0], args[1:]); err != nil {
return err
} else {
c.writeInteger(n)
}
2014-05-03 10:55:12 +04:00
return nil
}
func hlenCommand(c *client) error {
2014-05-06 13:37:58 +04:00
args := c.args
if len(args) != 1 {
return ErrCmdParams
}
if n, err := c.app.hash_len(args[0]); err != nil {
return err
} else {
c.writeInteger(n)
}
2014-05-03 10:55:12 +04:00
return nil
}
func hincrbyCommand(c *client) error {
2014-05-06 13:37:58 +04:00
args := c.args
if len(args) != 3 {
return ErrCmdParams
}
delta, err := strconv.ParseInt(hack.String(args[2]), 10, 64)
if err != nil {
return err
}
var n int64
if n, err = c.app.hash_incrby(args[0], args[1], delta); err != nil {
return err
} else {
c.writeInteger(n)
}
return nil
}
func hmsetCommand(c *client) error {
args := c.args
if len(args) < 3 {
return ErrCmdParams
}
if len(args[1:])%2 != 0 {
return ErrCmdParams
}
if err := c.app.hash_mset(args[0], args[1:]); err != nil {
return err
} else {
c.writeStatus(OK)
}
2014-05-03 10:55:12 +04:00
return nil
}
func hmgetCommand(c *client) error {
2014-05-06 13:37:58 +04:00
args := c.args
if len(args) < 2 {
return ErrCmdParams
}
if v, err := c.app.hash_mget(args[0], args[1:]); err != nil {
return err
} else {
c.writeArray(v)
}
2014-05-03 10:55:12 +04:00
return nil
}
func hgetallCommand(c *client) error {
2014-05-06 13:37:58 +04:00
args := c.args
if len(args) != 1 {
return ErrCmdParams
}
if v, err := c.app.hash_getall(args[0]); err != nil {
return err
} else {
c.writeArray(v)
}
2014-05-03 10:55:12 +04:00
return nil
}
func hkeysCommand(c *client) error {
2014-05-06 13:37:58 +04:00
args := c.args
if len(args) != 1 {
return ErrCmdParams
}
if v, err := c.app.hash_keys(args[0]); err != nil {
return err
} else {
c.writeArray(v)
}
2014-05-03 10:55:12 +04:00
return nil
}
func hvalsCommand(c *client) error {
2014-05-06 13:37:58 +04:00
args := c.args
if len(args) != 1 {
return ErrCmdParams
}
if v, err := c.app.hash_values(args[0]); err != nil {
return err
} else {
c.writeArray(v)
}
2014-05-03 10:55:12 +04:00
return nil
}
func init() {
register("hdel", hdelCommand)
register("hexists", hexistsCommand)
register("hget", hgetCommand)
register("hgetall", hgetallCommand)
register("hincrby", hincrbyCommand)
register("hkeys", hkeysCommand)
register("hlen", hlenCommand)
register("hmget", hmgetCommand)
register("hmset", hmsetCommand)
register("hset", hsetCommand)
register("hvals", hvalsCommand)
}