ledisdb/server/cmd_hash.go

285 lines
4.3 KiB
Go
Raw Normal View History

package server
2014-05-03 10:55:12 +04:00
import (
"github.com/siddontang/ledisdb/ledis"
)
2014-05-06 13:37:58 +04:00
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.db.HSet(args[0], args[1], args[2]); err != nil {
2014-05-06 13:37:58 +04:00
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.db.HGet(args[0], args[1]); err != nil {
2014-05-06 13:37:58 +04:00
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.db.HGet(args[0], args[1]); err != nil {
2014-05-06 13:37:58 +04:00
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.db.HDel(args[0], args[1:]...); err != nil {
2014-05-06 13:37:58 +04:00
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.db.HLen(args[0]); err != nil {
2014-05-06 13:37:58 +04:00
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 := ledis.StrInt64(args[2], nil)
2014-05-06 13:37:58 +04:00
if err != nil {
return err
}
var n int64
if n, err = c.db.HIncrBy(args[0], args[1], delta); err != nil {
2014-05-06 13:37:58 +04:00
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
}
2014-05-16 04:56:32 +04:00
key := args[0]
args = args[1:]
kvs := make([]ledis.FVPair, len(args)/2)
2014-05-16 04:56:32 +04:00
for i := 0; i < len(kvs); i++ {
kvs[i].Field = args[2*i]
kvs[i].Value = args[2*i+1]
}
if err := c.db.HMset(key, kvs...); err != nil {
2014-05-06 13:37:58 +04:00
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
}
2014-06-19 13:19:40 +04:00
if v, err := c.db.HMget(args[0], args[1:]...); err != nil {
2014-05-06 13:37:58 +04:00
return err
} else {
c.writeSliceArray(v)
2014-05-06 13:37:58 +04:00
}
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.db.HGetAll(args[0]); err != nil {
2014-05-06 13:37:58 +04:00
return err
} else {
c.writeFVPairArray(v)
2014-05-06 13:37:58 +04:00
}
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.db.HKeys(args[0]); err != nil {
2014-05-06 13:37:58 +04:00
return err
} else {
c.writeSliceArray(v)
2014-05-06 13:37:58 +04:00
}
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.db.HValues(args[0]); err != nil {
2014-05-06 13:37:58 +04:00
return err
} else {
c.writeSliceArray(v)
2014-05-06 13:37:58 +04:00
}
2014-05-03 10:55:12 +04:00
return nil
}
2014-05-12 11:08:59 +04:00
func hclearCommand(c *client) error {
args := c.args
if len(args) != 1 {
return ErrCmdParams
}
if n, err := c.db.HClear(args[0]); err != nil {
2014-05-12 11:08:59 +04:00
return err
} else {
c.writeInteger(n)
}
return nil
}
2014-06-16 15:24:37 +04:00
func hexpireCommand(c *client) error {
args := c.args
if len(args) == 0 {
return ErrCmdParams
}
duration, err := ledis.StrInt64(args[1], nil)
if err != nil {
return err
}
if v, err := c.db.HExpire(args[0], duration); err != nil {
return err
} else {
c.writeInteger(v)
}
return nil
}
func hexpireAtCommand(c *client) error {
args := c.args
if len(args) == 0 {
return ErrCmdParams
}
when, err := ledis.StrInt64(args[1], nil)
if err != nil {
return err
}
if v, err := c.db.HExpireAt(args[0], when); err != nil {
return err
} else {
c.writeInteger(v)
}
return nil
}
func httlCommand(c *client) error {
args := c.args
if len(args) == 0 {
return ErrCmdParams
}
if v, err := c.db.HTTL(args[0]); err != nil {
return err
} else {
c.writeInteger(v)
}
return nil
}
2014-05-03 10:55:12 +04:00
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)
2014-05-12 11:08:59 +04:00
//ledisdb special command
register("hclear", hclearCommand)
2014-06-16 15:24:37 +04:00
register("hexpire", hexpireCommand)
register("hexpireat", hexpireAtCommand)
register("httl", httlCommand)
2014-05-03 10:55:12 +04:00
}