diff --git a/ledis/t_hash.go b/ledis/t_hash.go index 98ee24f..6235acc 100644 --- a/ledis/t_hash.go +++ b/ledis/t_hash.go @@ -509,3 +509,15 @@ func (db *DB) HPersist(key []byte) (int64, error) { err = t.Commit() return n, err } + +func (db *DB) XHExists(key []byte) (int64, error) { + if err := checkKeySize(key); err != nil { + return 0, err + } + sk := db.hEncodeSizeKey(key) + v, err := db.bucket.Get(sk) + if v != nil && err == nil { + return 1, nil + } + return 0, err +} diff --git a/ledis/t_hash_test.go b/ledis/t_hash_test.go index 294746d..7fac691 100644 --- a/ledis/t_hash_test.go +++ b/ledis/t_hash_test.go @@ -79,6 +79,29 @@ func TestHashPersist(t *testing.T) { t.Fatal(n) } } +func TestXHashExists(t *testing.T) { + db := getTestDB() + key := []byte("xhexists_test") + v, err := db.XHExists(key) + if err != nil { + t.Fatal(err.Error()) + } + if v != 0 { + t.Fatal("invalid value ", v) + } + + if _, err := db.HSet(key, []byte("hello"), []byte("world")); err != nil { + t.Fatal(err.Error()) + } + + v, err = db.XHExists(key) + if err != nil { + t.Fatal(err.Error()) + } + if v != 1 { + t.Fatal("invalid value ", v) + } +} func TestHFlush(t *testing.T) { db := getTestDB() diff --git a/server/cmd_hash.go b/server/cmd_hash.go index f232821..9eb9e82 100644 --- a/server/cmd_hash.go +++ b/server/cmd_hash.go @@ -300,6 +300,19 @@ func hxrevscanCommand(c *client) error { return xscanGeneric(c, c.db.HRevScan) } +func hxexistsCommand(c *client) error { + args := c.args + if len(args) != 1 { + return ErrCmdParams + } + if n, err := c.db.XHExists(args[0]); err != nil { + return err + } else { + c.resp.writeInteger(n) + } + return nil +} + func init() { register("hdel", hdelCommand) register("hexists", hexistsCommand) @@ -325,4 +338,5 @@ func init() { register("hxrevscan", hxrevscanCommand) register("xhscan", hxscanCommand) register("xhrevscan", hxrevscanCommand) + register("xhexists", hxexistsCommand) } diff --git a/server/cmd_hash_test.go b/server/cmd_hash_test.go index 04388cc..7ff5f40 100644 --- a/server/cmd_hash_test.go +++ b/server/cmd_hash_test.go @@ -12,11 +12,22 @@ func TestHash(t *testing.T) { defer c.Close() key := []byte("a") + if n, err := ledis.Int(c.Do("xhexists", key)); err != nil { + t.Fatal(err) + } else if n != 0 { + t.Fatal(n) + } + if n, err := ledis.Int(c.Do("hset", key, 1, 0)); err != nil { t.Fatal(err) } else if n != 1 { t.Fatal(n) } + if n, err := ledis.Int(c.Do("xhexists", key)); err != nil { + t.Fatal(err) + } else if n != 1 { + t.Fatal(n) + } if n, err := ledis.Int(c.Do("hexists", key, 1)); err != nil { t.Fatal(err) @@ -296,5 +307,4 @@ func TestHashErrorParams(t *testing.T) { if _, err := c.Do("hpersist"); err == nil { t.Fatal("invalid err of %v", err) } - }