ledisdb/server/cmd_hash.go

327 lines
5.1 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-08-25 10:18:23 +04:00
func hsetCommand(c *client) error {
args := c.args
2014-05-06 13:37:58 +04:00
if len(args) != 3 {
return ErrCmdParams
}
2014-08-25 10:18:23 +04:00
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 {
2014-08-25 10:18:23 +04:00
c.resp.writeInteger(n)
2014-05-06 13:37:58 +04:00
}
2014-05-03 10:55:12 +04:00
return nil
}
2014-08-25 10:18:23 +04:00
func hgetCommand(c *client) error {
args := c.args
2014-05-06 13:37:58 +04:00
if len(args) != 2 {
return ErrCmdParams
}
2014-08-25 10:18:23 +04:00
if v, err := c.db.HGet(args[0], args[1]); err != nil {
2014-05-06 13:37:58 +04:00
return err
} else {
2014-08-25 10:18:23 +04:00
c.resp.writeBulk(v)
2014-05-06 13:37:58 +04:00
}
2014-05-03 10:55:12 +04:00
return nil
}
2014-08-25 10:18:23 +04:00
func hexistsCommand(c *client) error {
args := c.args
2014-05-06 13:37:58 +04:00
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
2014-08-25 10:18:23 +04:00
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
}
2014-08-25 10:18:23 +04:00
c.resp.writeInteger(n)
2014-05-06 13:37:58 +04:00
}
2014-05-03 10:55:12 +04:00
return nil
}
2014-08-25 10:18:23 +04:00
func hdelCommand(c *client) error {
args := c.args
2014-05-06 13:37:58 +04:00
if len(args) < 2 {
return ErrCmdParams
}
2014-08-25 10:18:23 +04:00
if n, err := c.db.HDel(args[0], args[1:]...); err != nil {
2014-05-06 13:37:58 +04:00
return err
} else {
2014-08-25 10:18:23 +04:00
c.resp.writeInteger(n)
2014-05-06 13:37:58 +04:00
}
2014-05-03 10:55:12 +04:00
return nil
}
2014-08-25 10:18:23 +04:00
func hlenCommand(c *client) error {
args := c.args
2014-05-06 13:37:58 +04:00
if len(args) != 1 {
return ErrCmdParams
}
2014-08-25 10:18:23 +04:00
if n, err := c.db.HLen(args[0]); err != nil {
2014-05-06 13:37:58 +04:00
return err
} else {
2014-08-25 10:18:23 +04:00
c.resp.writeInteger(n)
2014-05-06 13:37:58 +04:00
}
2014-05-03 10:55:12 +04:00
return nil
}
2014-08-25 10:18:23 +04:00
func hincrbyCommand(c *client) error {
args := c.args
2014-05-06 13:37:58 +04:00
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 ErrValue
2014-05-06 13:37:58 +04:00
}
var n int64
2014-08-25 10:18:23 +04:00
if n, err = c.db.HIncrBy(args[0], args[1], delta); err != nil {
2014-05-06 13:37:58 +04:00
return err
} else {
2014-08-25 10:18:23 +04:00
c.resp.writeInteger(n)
2014-05-06 13:37:58 +04:00
}
return nil
}
2014-08-25 10:18:23 +04:00
func hmsetCommand(c *client) error {
args := c.args
2014-05-06 13:37:58 +04:00
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]
}
2014-08-25 10:18:23 +04:00
if err := c.db.HMset(key, kvs...); err != nil {
2014-05-06 13:37:58 +04:00
return err
} else {
2014-08-25 10:18:23 +04:00
c.resp.writeStatus(OK)
2014-05-06 13:37:58 +04:00
}
2014-05-03 10:55:12 +04:00
return nil
}
2014-08-25 10:18:23 +04:00
func hmgetCommand(c *client) error {
args := c.args
2014-05-06 13:37:58 +04:00
if len(args) < 2 {
return ErrCmdParams
}
2014-08-25 10:18:23 +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 {
2014-08-25 10:18:23 +04:00
c.resp.writeSliceArray(v)
2014-05-06 13:37:58 +04:00
}
2014-05-03 10:55:12 +04:00
return nil
}
2014-08-25 10:18:23 +04:00
func hgetallCommand(c *client) error {
args := c.args
2014-05-06 13:37:58 +04:00
if len(args) != 1 {
return ErrCmdParams
}
2014-08-25 10:18:23 +04:00
if v, err := c.db.HGetAll(args[0]); err != nil {
2014-05-06 13:37:58 +04:00
return err
} else {
2014-08-25 10:18:23 +04:00
c.resp.writeFVPairArray(v)
2014-05-06 13:37:58 +04:00
}
2014-05-03 10:55:12 +04:00
return nil
}
2014-08-25 10:18:23 +04:00
func hkeysCommand(c *client) error {
args := c.args
2014-05-06 13:37:58 +04:00
if len(args) != 1 {
return ErrCmdParams
}
2014-08-25 10:18:23 +04:00
if v, err := c.db.HKeys(args[0]); err != nil {
2014-05-06 13:37:58 +04:00
return err
} else {
2014-08-25 10:18:23 +04:00
c.resp.writeSliceArray(v)
2014-05-06 13:37:58 +04:00
}
2014-05-03 10:55:12 +04:00
return nil
}
2014-08-25 10:18:23 +04:00
func hvalsCommand(c *client) error {
args := c.args
2014-05-06 13:37:58 +04:00
if len(args) != 1 {
return ErrCmdParams
}
2014-08-25 10:18:23 +04:00
if v, err := c.db.HValues(args[0]); err != nil {
2014-05-06 13:37:58 +04:00
return err
} else {
2014-08-25 10:18:23 +04:00
c.resp.writeSliceArray(v)
2014-05-06 13:37:58 +04:00
}
2014-05-03 10:55:12 +04:00
return nil
}
2014-08-25 10:18:23 +04:00
func hclearCommand(c *client) error {
args := c.args
2014-05-12 11:08:59 +04:00
if len(args) != 1 {
return ErrCmdParams
}
2014-08-25 10:18:23 +04:00
if n, err := c.db.HClear(args[0]); err != nil {
2014-05-12 11:08:59 +04:00
return err
} else {
2014-08-25 10:18:23 +04:00
c.resp.writeInteger(n)
2014-05-12 11:08:59 +04:00
}
return nil
}
2014-08-25 10:18:23 +04:00
func hmclearCommand(c *client) error {
args := c.args
if len(args) < 1 {
return ErrCmdParams
}
2014-08-25 10:18:23 +04:00
if n, err := c.db.HMclear(args...); err != nil {
return err
} else {
2014-08-25 10:18:23 +04:00
c.resp.writeInteger(n)
}
return nil
}
2014-08-25 10:18:23 +04:00
func hexpireCommand(c *client) error {
args := c.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 {
return ErrValue
2014-06-16 15:24:37 +04:00
}
2014-08-25 10:18:23 +04:00
if v, err := c.db.HExpire(args[0], duration); err != nil {
2014-06-16 15:24:37 +04:00
return err
} else {
2014-08-25 10:18:23 +04:00
c.resp.writeInteger(v)
2014-06-16 15:24:37 +04:00
}
return nil
}
2014-08-25 10:18:23 +04:00
func hexpireAtCommand(c *client) error {
args := c.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 {
return ErrValue
2014-06-16 15:24:37 +04:00
}
2014-08-25 10:18:23 +04:00
if v, err := c.db.HExpireAt(args[0], when); err != nil {
2014-06-16 15:24:37 +04:00
return err
} else {
2014-08-25 10:18:23 +04:00
c.resp.writeInteger(v)
2014-06-16 15:24:37 +04:00
}
return nil
}
2014-08-25 10:18:23 +04:00
func httlCommand(c *client) error {
args := c.args
2014-06-30 07:23:14 +04:00
if len(args) != 1 {
2014-06-16 15:24:37 +04:00
return ErrCmdParams
}
2014-08-25 10:18:23 +04:00
if v, err := c.db.HTTL(args[0]); err != nil {
2014-06-16 15:24:37 +04:00
return err
} else {
2014-08-25 10:18:23 +04:00
c.resp.writeInteger(v)
2014-06-16 15:24:37 +04:00
}
return nil
}
2014-08-25 10:18:23 +04:00
func hpersistCommand(c *client) error {
args := c.args
2014-06-24 08:44:44 +04:00
if len(args) != 1 {
return ErrCmdParams
}
2014-08-25 10:18:23 +04:00
if n, err := c.db.HPersist(args[0]); err != nil {
2014-06-24 08:44:44 +04:00
return err
} else {
2014-08-25 10:18:23 +04:00
c.resp.writeInteger(n)
2014-06-24 08:44:44 +04:00
}
return nil
}
2014-09-09 05:53:26 +04:00
func hxscanCommand(c *client) error {
2014-10-20 18:36:16 +04:00
return xscanGeneric(c, c.db.HScan)
}
2014-08-26 19:21:45 +04:00
2014-10-20 18:36:16 +04:00
func hxrevscanCommand(c *client) error {
return xscanGeneric(c, c.db.HRevScan)
2014-08-26 19:21:45 +04:00
}
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)
register("hmclear", hmclearCommand)
2014-06-16 15:24:37 +04:00
register("hexpire", hexpireCommand)
register("hexpireat", hexpireAtCommand)
register("httl", httlCommand)
2014-06-24 08:44:44 +04:00
register("hpersist", hpersistCommand)
2014-09-09 05:53:26 +04:00
register("hxscan", hxscanCommand)
2014-10-20 18:36:16 +04:00
register("hxrevscan", hxrevscanCommand)
2014-05-03 10:55:12 +04:00
}