From 9e09e607c8983958dd9a1a51e6dfa168b9bfcbf8 Mon Sep 17 00:00:00 2001 From: siddontang Date: Fri, 20 Jun 2014 10:12:50 +0800 Subject: [PATCH] heel use iterator to multi find and delete --- ledis/t_hash.go | 10 ++++++---- server/cmd_hash.go | 2 +- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/ledis/t_hash.go b/ledis/t_hash.go index 4fa4cdc..5186c04 100644 --- a/ledis/t_hash.go +++ b/ledis/t_hash.go @@ -252,7 +252,7 @@ func (db *DB) HMget(key []byte, args ...[]byte) ([]interface{}, error) { return r, nil } -func (db *DB) HDel(key []byte, args [][]byte) (int64, error) { +func (db *DB) HDel(key []byte, args ...[]byte) (int64, error) { t := db.hashTx var ek []byte @@ -262,6 +262,9 @@ func (db *DB) HDel(key []byte, args [][]byte) (int64, error) { t.Lock() defer t.Unlock() + it := db.db.NewIterator() + defer it.Close() + var num int64 = 0 for i := 0; i < len(args); i++ { if err := checkHashKFSize(key, args[i]); err != nil { @@ -270,9 +273,8 @@ func (db *DB) HDel(key []byte, args [][]byte) (int64, error) { ek = db.hEncodeHashKey(key, args[i]) - if v, err = db.db.Get(ek); err != nil { - return 0, err - } else if v == nil { + v = it.Find(ek) + if v == nil { continue } else { num++ diff --git a/server/cmd_hash.go b/server/cmd_hash.go index a20fef6..23873b1 100644 --- a/server/cmd_hash.go +++ b/server/cmd_hash.go @@ -59,7 +59,7 @@ func hdelCommand(c *client) error { return ErrCmdParams } - if n, err := c.db.HDel(args[0], args[1:]); err != nil { + if n, err := c.db.HDel(args[0], args[1:]...); err != nil { return err } else { c.writeInteger(n)