From 46d2ecda911a832ac12bf76cc6f59087b2ef618a Mon Sep 17 00:00:00 2001 From: siddontang Date: Fri, 27 Jun 2014 16:28:26 +0800 Subject: [PATCH] add hmclear, lmclear and zmclear for multi keys --- doc/commands.md | 23 ++++++++++++++++++++--- ledis/t_hash.go | 18 ++++++++++++++++++ ledis/t_list.go | 19 +++++++++++++++++++ ledis/t_zset.go | 16 ++++++++++++++++ server/cmd_hash.go | 16 ++++++++++++++++ server/cmd_list.go | 16 ++++++++++++++++ server/cmd_zset.go | 16 ++++++++++++++++ 7 files changed, 121 insertions(+), 3 deletions(-) diff --git a/doc/commands.md b/doc/commands.md index 558d7d1..793ec23 100644 --- a/doc/commands.md +++ b/doc/commands.md @@ -557,13 +557,13 @@ ledis> hvals myhash 2) "world" ``` -### hclear key +### hclear key -Deletes the hash key +Deletes the specified hash keys **Return value** -int64: the number of fields in the hash stored at key. +int64: the number of fields in the hash stored at key **Examples** @@ -574,6 +574,23 @@ ledis> hclear myhash (integer) 2 ``` +### hmclear key [key...] + +Deletes the specified hash keys + +**Return value** + +int64: the number of input keys + +**Examples** + +``` +ledis> hmset myhash field1 "hello" field2 "world" +OK +ledis> hclear myhash +(integer) 1 +``` + ### hexpire key seconds Sets a hash key's time to live in seconds, like expire similarly. diff --git a/ledis/t_hash.go b/ledis/t_hash.go index aa1a75f..9fcbcee 100644 --- a/ledis/t_hash.go +++ b/ledis/t_hash.go @@ -434,6 +434,24 @@ func (db *DB) HClear(key []byte) (int64, error) { return num, err } +func (db *DB) HMclear(keys ...[]byte) (int64, error) { + t := db.hashTx + t.Lock() + defer t.Unlock() + + for _, key := range keys { + if err := checkKeySize(key); err != nil { + return 0, err + } + + db.hDelete(t, key) + db.rmExpire(t, hExpType, key) + } + + err := t.Commit() + return int64(len(keys)), err +} + func (db *DB) hFlush() (drop int64, err error) { t := db.kvTx t.Lock() diff --git a/ledis/t_list.go b/ledis/t_list.go index 6306968..d6dca0f 100644 --- a/ledis/t_list.go +++ b/ledis/t_list.go @@ -395,6 +395,25 @@ func (db *DB) LClear(key []byte) (int64, error) { return num, err } +func (db *DB) LMclear(keys ...[]byte) (int64, error) { + t := db.listTx + t.Lock() + defer t.Unlock() + + for _, key := range keys { + if err := checkKeySize(key); err != nil { + return 0, err + } + + db.lDelete(t, key) + db.rmExpire(t, lExpType, key) + + } + + err := t.Commit() + return int64(len(keys)), err +} + func (db *DB) lFlush() (drop int64, err error) { t := db.listTx t.Lock() diff --git a/ledis/t_zset.go b/ledis/t_zset.go index cd0e001..b59cab1 100644 --- a/ledis/t_zset.go +++ b/ledis/t_zset.go @@ -631,6 +631,22 @@ func (db *DB) ZClear(key []byte) (int64, error) { return rmCnt, err } +func (db *DB) ZMclear(keys ...[]byte) (int64, error) { + t := db.zsetTx + t.Lock() + defer t.Unlock() + + for _, key := range keys { + if _, err := db.zRemRange(t, key, MinScore, MaxScore, 0, -1); err != nil { + return 0, err + } + } + + err := t.Commit() + + return int64(len(keys)), err +} + func (db *DB) ZRange(key []byte, start int, stop int) ([]ScorePair, error) { return db.ZRangeGeneric(key, start, stop, false) } diff --git a/server/cmd_hash.go b/server/cmd_hash.go index 3311c8c..fa720db 100644 --- a/server/cmd_hash.go +++ b/server/cmd_hash.go @@ -207,6 +207,21 @@ func hclearCommand(c *client) error { return nil } +func hmclearCommand(c *client) error { + args := c.args + if len(args) < 1 { + return ErrCmdParams + } + + if n, err := c.db.HMclear(args...); err != nil { + return err + } else { + c.writeInteger(n) + } + + return nil +} + func hexpireCommand(c *client) error { args := c.args if len(args) == 0 { @@ -293,6 +308,7 @@ func init() { //ledisdb special command register("hclear", hclearCommand) + register("hmclear", hmclearCommand) register("hexpire", hexpireCommand) register("hexpireat", hexpireAtCommand) register("httl", httlCommand) diff --git a/server/cmd_list.go b/server/cmd_list.go index b511973..c7b1c81 100644 --- a/server/cmd_list.go +++ b/server/cmd_list.go @@ -143,6 +143,21 @@ func lclearCommand(c *client) error { return nil } +func lmclearCommand(c *client) error { + args := c.args + if len(args) < 1 { + return ErrCmdParams + } + + if n, err := c.db.LMclear(args...); err != nil { + return err + } else { + c.writeInteger(n) + } + + return nil +} + func lexpireCommand(c *client) error { args := c.args if len(args) == 0 { @@ -225,6 +240,7 @@ func init() { //ledisdb special command register("lclear", lclearCommand) + register("lmclear", lmclearCommand) register("lexpire", lexpireCommand) register("lexpireat", lexpireAtCommand) register("lttl", lttlCommand) diff --git a/server/cmd_zset.go b/server/cmd_zset.go index 2c6fd7d..bf2fce1 100644 --- a/server/cmd_zset.go +++ b/server/cmd_zset.go @@ -409,6 +409,21 @@ func zclearCommand(c *client) error { return nil } +func zmclearCommand(c *client) error { + args := c.args + if len(args) < 1 { + return ErrCmdParams + } + + if n, err := c.db.ZMclear(args...); err != nil { + return err + } else { + c.writeInteger(n) + } + + return nil +} + func zexpireCommand(c *client) error { args := c.args if len(args) == 0 { @@ -498,6 +513,7 @@ func init() { //ledisdb special command register("zclear", zclearCommand) + register("zmclear", zmclearCommand) register("zexpire", zexpireCommand) register("zexpireat", zexpireAtCommand) register("zttl", zttlCommand)