add xhexists cmd

This commit is contained in:
wenyekui 2015-02-01 17:19:46 +08:00
parent 31fc7f0375
commit 83c4db020c
4 changed files with 60 additions and 1 deletions

View File

@ -509,3 +509,15 @@ func (db *DB) HPersist(key []byte) (int64, error) {
err = t.Commit() err = t.Commit()
return n, err 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
}

View File

@ -79,6 +79,29 @@ func TestHashPersist(t *testing.T) {
t.Fatal(n) 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) { func TestHFlush(t *testing.T) {
db := getTestDB() db := getTestDB()

View File

@ -300,6 +300,19 @@ func hxrevscanCommand(c *client) error {
return xscanGeneric(c, c.db.HRevScan) 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() { func init() {
register("hdel", hdelCommand) register("hdel", hdelCommand)
register("hexists", hexistsCommand) register("hexists", hexistsCommand)
@ -325,4 +338,5 @@ func init() {
register("hxrevscan", hxrevscanCommand) register("hxrevscan", hxrevscanCommand)
register("xhscan", hxscanCommand) register("xhscan", hxscanCommand)
register("xhrevscan", hxrevscanCommand) register("xhrevscan", hxrevscanCommand)
register("xhexists", hxexistsCommand)
} }

View File

@ -12,11 +12,22 @@ func TestHash(t *testing.T) {
defer c.Close() defer c.Close()
key := []byte("a") 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 { if n, err := ledis.Int(c.Do("hset", key, 1, 0)); err != nil {
t.Fatal(err) t.Fatal(err)
} else if n != 1 { } else if n != 1 {
t.Fatal(n) 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 { if n, err := ledis.Int(c.Do("hexists", key, 1)); err != nil {
t.Fatal(err) t.Fatal(err)
@ -296,5 +307,4 @@ func TestHashErrorParams(t *testing.T) {
if _, err := c.Do("hpersist"); err == nil { if _, err := c.Do("hpersist"); err == nil {
t.Fatal("invalid err of %v", err) t.Fatal("invalid err of %v", err)
} }
} }