diff --git a/ledis/t_list.go b/ledis/t_list.go index 7f66bed..93c6ba2 100644 --- a/ledis/t_list.go +++ b/ledis/t_list.go @@ -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) diff --git a/ledis/t_list_test.go b/ledis/t_list_test.go index 4fd2633..b80af1d 100644 --- a/ledis/t_list_test.go +++ b/ledis/t_list_test.go @@ -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) + } +} diff --git a/server/cmd_hash.go b/server/cmd_hash.go index 9eb9e82..8c277ee 100644 --- a/server/cmd_hash.go +++ b/server/cmd_hash.go @@ -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) } diff --git a/server/cmd_list.go b/server/cmd_list.go index 63e6f59..7000a74 100644 --- a/server/cmd_list.go +++ b/server/cmd_list.go @@ -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) } diff --git a/server/cmd_list_test.go b/server/cmd_list_test.go index 5a7cf51..526862b 100644 --- a/server/cmd_list_test.go +++ b/server/cmd_list_test.go @@ -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 {