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 (
|
2023-02-15 10:41:45 +03:00
|
|
|
"git.internal/re/ledisdb/ledis"
|
2014-05-16 11:03:23 +04:00
|
|
|
)
|
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
|
|
|
|
}
|
|
|
|
|
2020-05-10 13:17:39 +03:00
|
|
|
n, err := c.db.HSet(args[0], args[1], args[2])
|
|
|
|
if err != nil {
|
2014-05-06 13:37:58 +04:00
|
|
|
return err
|
|
|
|
}
|
2020-05-10 13:17:39 +03:00
|
|
|
c.resp.writeInteger(n)
|
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
|
|
|
|
}
|
|
|
|
|
2020-05-10 13:17:39 +03:00
|
|
|
v, err := c.db.HGet(args[0], args[1])
|
|
|
|
if err != nil {
|
2014-05-06 13:37:58 +04:00
|
|
|
return err
|
|
|
|
}
|
2020-05-10 13:17:39 +03:00
|
|
|
c.resp.writeBulk(v)
|
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
|
2020-05-10 13:17:39 +03:00
|
|
|
v, err := c.db.HGet(args[0], args[1])
|
|
|
|
if err != nil {
|
2014-05-06 13:37:58 +04:00
|
|
|
return err
|
|
|
|
}
|
2020-05-10 13:17:39 +03:00
|
|
|
if v == nil {
|
|
|
|
n = 0
|
|
|
|
}
|
|
|
|
|
|
|
|
c.resp.writeInteger(n)
|
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
|
|
|
|
}
|
|
|
|
|
2020-05-10 13:17:39 +03:00
|
|
|
n, err := c.db.HDel(args[0], args[1:]...)
|
|
|
|
if err != nil {
|
2014-05-06 13:37:58 +04:00
|
|
|
return err
|
|
|
|
}
|
2020-05-10 13:17:39 +03:00
|
|
|
c.resp.writeInteger(n)
|
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
|
|
|
|
}
|
|
|
|
|
2020-05-10 13:17:39 +03:00
|
|
|
n, err := c.db.HLen(args[0])
|
|
|
|
if err != nil {
|
2014-05-06 13:37:58 +04:00
|
|
|
return err
|
|
|
|
}
|
2020-05-10 13:17:39 +03:00
|
|
|
c.resp.writeInteger(n)
|
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
|
|
|
|
}
|
|
|
|
|
2014-05-16 11:03:23 +04:00
|
|
|
delta, err := ledis.StrInt64(args[2], nil)
|
2014-05-06 13:37:58 +04:00
|
|
|
if err != nil {
|
2014-07-25 13:26:29 +04:00
|
|
|
return ErrValue
|
2014-05-06 13:37:58 +04:00
|
|
|
}
|
|
|
|
|
2020-05-10 13:17:39 +03:00
|
|
|
n, err := c.db.HIncrBy(args[0], args[1], delta)
|
|
|
|
if err != nil {
|
2014-05-06 13:37:58 +04:00
|
|
|
return err
|
|
|
|
}
|
2020-05-10 13:17:39 +03: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:]
|
|
|
|
|
2014-05-16 11:03:23 +04:00
|
|
|
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
|
|
|
|
}
|
2020-05-10 13:17:39 +03:00
|
|
|
c.resp.writeStatus(OK)
|
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
|
|
|
|
}
|
|
|
|
|
2020-05-10 13:17:39 +03:00
|
|
|
v, err := c.db.HMget(args[0], args[1:]...)
|
|
|
|
if err != nil {
|
2014-05-06 13:37:58 +04:00
|
|
|
return err
|
|
|
|
}
|
2020-05-10 13:17:39 +03:00
|
|
|
c.resp.writeSliceArray(v)
|
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
|
|
|
|
}
|
|
|
|
|
2020-05-10 13:17:39 +03:00
|
|
|
v, err := c.db.HGetAll(args[0])
|
|
|
|
if err != nil {
|
2014-05-06 13:37:58 +04:00
|
|
|
return err
|
|
|
|
}
|
2020-05-10 13:17:39 +03:00
|
|
|
c.resp.writeFVPairArray(v)
|
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
|
|
|
|
}
|
|
|
|
|
2020-05-10 13:17:39 +03:00
|
|
|
v, err := c.db.HKeys(args[0])
|
|
|
|
if err != nil {
|
2014-05-06 13:37:58 +04:00
|
|
|
return err
|
|
|
|
}
|
2020-05-10 13:17:39 +03:00
|
|
|
c.resp.writeSliceArray(v)
|
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
|
|
|
|
}
|
|
|
|
|
2020-05-10 13:17:39 +03:00
|
|
|
v, err := c.db.HValues(args[0])
|
|
|
|
if err != nil {
|
2014-05-06 13:37:58 +04:00
|
|
|
return err
|
|
|
|
}
|
2020-05-10 13:17:39 +03:00
|
|
|
c.resp.writeSliceArray(v)
|
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
|
|
|
|
}
|
|
|
|
|
2020-05-10 13:17:39 +03:00
|
|
|
n, err := c.db.HClear(args[0])
|
|
|
|
if err != nil {
|
2014-05-12 11:08:59 +04:00
|
|
|
return err
|
|
|
|
}
|
2020-05-10 13:17:39 +03: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
|
2014-06-27 12:28:26 +04:00
|
|
|
if len(args) < 1 {
|
|
|
|
return ErrCmdParams
|
|
|
|
}
|
|
|
|
|
2020-05-10 13:17:39 +03:00
|
|
|
n, err := c.db.HMclear(args...)
|
|
|
|
if err != nil {
|
2014-06-27 12:28:26 +04:00
|
|
|
return err
|
|
|
|
}
|
2020-05-10 13:17:39 +03:00
|
|
|
c.resp.writeInteger(n)
|
2014-06-27 12:28:26 +04:00
|
|
|
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 {
|
2014-07-25 13:26:29 +04:00
|
|
|
return ErrValue
|
2014-06-16 15:24:37 +04:00
|
|
|
}
|
|
|
|
|
2020-05-10 13:17:39 +03:00
|
|
|
v, err := c.db.HExpire(args[0], duration)
|
|
|
|
if err != nil {
|
2014-06-16 15:24:37 +04:00
|
|
|
return err
|
|
|
|
}
|
2020-05-10 13:17:39 +03: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 {
|
2014-07-25 13:26:29 +04:00
|
|
|
return ErrValue
|
2014-06-16 15:24:37 +04:00
|
|
|
}
|
|
|
|
|
2020-05-10 13:17:39 +03:00
|
|
|
v, err := c.db.HExpireAt(args[0], when)
|
|
|
|
if err != nil {
|
2014-06-16 15:24:37 +04:00
|
|
|
return err
|
|
|
|
}
|
2020-05-10 13:17:39 +03: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
|
|
|
|
}
|
|
|
|
|
2020-05-10 13:17:39 +03:00
|
|
|
v, err := c.db.HTTL(args[0])
|
|
|
|
if err != nil {
|
2014-06-16 15:24:37 +04:00
|
|
|
return err
|
|
|
|
}
|
2020-05-10 13:17:39 +03: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
|
|
|
|
}
|
|
|
|
|
2020-05-10 13:17:39 +03:00
|
|
|
n, err := c.db.HPersist(args[0])
|
|
|
|
if err != nil {
|
2014-06-24 08:44:44 +04:00
|
|
|
return err
|
|
|
|
}
|
2020-05-10 13:17:39 +03:00
|
|
|
c.resp.writeInteger(n)
|
2014-06-24 08:44:44 +04:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-03-04 04:33:25 +03:00
|
|
|
func hkeyexistsCommand(c *client) error {
|
2015-02-01 12:19:46 +03:00
|
|
|
args := c.args
|
|
|
|
if len(args) != 1 {
|
|
|
|
return ErrCmdParams
|
|
|
|
}
|
2020-05-10 13:17:39 +03:00
|
|
|
n, err := c.db.HKeyExists(args[0])
|
|
|
|
if err != nil {
|
2015-02-01 12:19:46 +03:00
|
|
|
return err
|
|
|
|
}
|
2020-05-10 13:17:39 +03:00
|
|
|
c.resp.writeInteger(n)
|
2015-02-01 12:19:46 +03:00
|
|
|
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-27 12:28:26 +04:00
|
|
|
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)
|
2015-03-04 04:33:25 +03:00
|
|
|
register("hkeyexists", hkeyexistsCommand)
|
2014-05-03 10:55:12 +04:00
|
|
|
}
|