add XLExists cmd

This commit is contained in:
wenyekui 2015-02-01 17:41:28 +08:00
parent 83c4db020c
commit 52cad4ab31
5 changed files with 54 additions and 2 deletions

View File

@ -506,6 +506,18 @@ func (db *DB) BRPop(keys [][]byte, timeout time.Duration) ([]interface{}, error)
return db.lblockPop(keys, listTailSeq, timeout)
}
func (db *DB) XLExists(key []byte) (int64, error) {
if err := checkKeySize(key); err != nil {
return 0, err
}
sk := db.lEncodeMetaKey(key)
v, err := db.bucket.Get(sk)
if v != nil && err == nil {
return 1, nil
}
return 0, err
}
func (db *DB) lblockPop(keys [][]byte, whereSeq int32, timeout time.Duration) ([]interface{}, error) {
ch := make(chan []byte)

View File

@ -163,3 +163,19 @@ func TestLFlush(t *testing.T) {
t.Fatal("invalid value length ", len(v))
}
}
func TestXLExists(t *testing.T) {
db := getTestDB()
key := []byte("xlexists_test")
if n, err := db.XLExists(key); err != nil {
t.Fatal(err.Error())
} else if n != 0 {
t.Fatal("invalid value ", n)
}
db.LPush(key, []byte("hello"), []byte("world"))
if n, err := db.XLExists(key); err != nil {
t.Fatal(err.Error())
} else if n != 1 {
t.Fatal("invalid value ", n)
}
}

View File

@ -300,7 +300,7 @@ func hxrevscanCommand(c *client) error {
return xscanGeneric(c, c.db.HRevScan)
}
func hxexistsCommand(c *client) error {
func xhexistsCommand(c *client) error {
args := c.args
if len(args) != 1 {
return ErrCmdParams
@ -338,5 +338,5 @@ func init() {
register("hxrevscan", hxrevscanCommand)
register("xhscan", hxscanCommand)
register("xhrevscan", hxrevscanCommand)
register("xhexists", hxexistsCommand)
register("xhexists", xhexistsCommand)
}

View File

@ -285,6 +285,18 @@ func lParseBPopArgs(c *client) (keys [][]byte, timeout time.Duration, err error)
keys = args[0 : len(args)-1]
return
}
func xlexistsCommand(c *client) error {
args := c.args
if len(args) != 1 {
return ErrCmdParams
}
if n, err := c.db.XLExists(args[0]); err != nil {
return err
} else {
c.resp.writeInteger(n)
}
return nil
}
func init() {
register("blpop", blpopCommand)
@ -309,4 +321,5 @@ func init() {
register("lxrevscan", lxrevscanCommand)
register("xlscan", lxscanCommand)
register("xlrevscan", lxrevscanCommand)
register("xlexists", xlexistsCommand)
}

View File

@ -58,6 +58,11 @@ func TestList(t *testing.T) {
defer c.Close()
key := []byte("a")
if n, err := ledis.Int(c.Do("xlexists", key)); err != nil {
t.Fatal(err)
} else if n != 0 {
t.Fatal(n)
}
if n, err := ledis.Int(c.Do("lpush", key, 1)); err != nil {
t.Fatal(err)
@ -65,6 +70,12 @@ func TestList(t *testing.T) {
t.Fatal(n)
}
if n, err := ledis.Int(c.Do("xlexists", key)); err != nil {
t.Fatal(err)
} else if n != 1 {
t.Fatal(1)
}
if n, err := ledis.Int(c.Do("rpush", key, 2)); err != nil {
t.Fatal(err)
} else if n != 2 {