add xsexists and xzexists commands

This commit is contained in:
wenyekui 2015-02-01 17:54:52 +08:00
parent 52cad4ab31
commit 5863e52ded
8 changed files with 115 additions and 0 deletions

View File

@ -286,6 +286,18 @@ func (db *DB) SDiffStore(dstKey []byte, keys ...[]byte) (int64, error) {
return n, err
}
func (db *DB) XSExists(key []byte) (int64, error) {
if err := checkKeySize(key); err != nil {
return 0, err
}
sk := db.sEncodeSizeKey(key)
v, err := db.bucket.Get(sk)
if v != nil && err == nil {
return 1, nil
}
return 0, err
}
func (db *DB) sInterGeneric(keys ...[]byte) ([][]byte, error) {
destMap := make(map[string]bool)

View File

@ -371,3 +371,22 @@ func TestSFlush(t *testing.T) {
}
}
func TestXSExists(t *testing.T) {
db := getTestDB()
key := []byte("xsexists_test")
if n, err := db.XSExists(key); err != nil {
t.Fatal(err.Error())
} else if n != 0 {
t.Fatal("invalid value ", n)
}
db.SAdd(key, []byte("hello"), []byte("world"))
if n, err := db.XSExists(key); err != nil {
t.Fatal(err.Error())
} else if n != 1 {
t.Fatal("invalid value ", n)
}
}

View File

@ -1023,3 +1023,15 @@ func (db *DB) ZLexCount(key []byte, min []byte, max []byte, rangeType uint8) (in
return n, nil
}
func (db *DB) XZExists(key []byte) (int64, error) {
if err := checkKeySize(key); err != nil {
return 0, err
}
sk := db.zEncodeSizeKey(key)
v, err := db.bucket.Get(sk)
if v != nil && err == nil {
return 1, nil
}
return 0, err
}

View File

@ -465,3 +465,21 @@ func TestZLex(t *testing.T) {
}
}
func TestXZExists(t *testing.T) {
db := getTestDB()
key := []byte("xzexists_test")
if n, err := db.XZExists(key); err != nil {
t.Fatal(err.Error())
} else if n != 0 {
t.Fatal("invalid value ", n)
}
db.ZAdd(key, ScorePair{0, []byte("a")}, ScorePair{0, []byte("b")})
if n, err := db.XZExists(key); err != nil {
t.Fatal(err.Error())
} else if n != 1 {
t.Fatal("invalid value ", n)
}
}

View File

@ -270,6 +270,19 @@ func sxrevscanCommand(c *client) error {
return xscanGeneric(c, c.db.SRevScan)
}
func xsexistsCommand(c *client) error {
args := c.args
if len(args) != 1 {
return ErrCmdParams
}
if n, err := c.db.XSExists(args[0]); err != nil {
return err
} else {
c.resp.writeInteger(n)
}
return nil
}
func init() {
register("sadd", saddCommand)
register("scard", scardCommand)
@ -292,4 +305,6 @@ func init() {
register("sxrevscan", sxrevscanCommand)
register("xsscan", sxscanCommand)
register("xsrevscan", sxrevscanCommand)
register("xsexists", xsexistsCommand)
}

View File

@ -12,12 +12,24 @@ func TestSet(t *testing.T) {
key1 := "testdb_cmd_set_1"
key2 := "testdb_cmd_set_2"
if n, err := ledis.Int(c.Do("xsexists", key1)); err != nil {
t.Fatal(err)
} else if n != 0 {
t.Fatal(n)
}
if n, err := ledis.Int(c.Do("sadd", key1, 0, 1)); err != nil {
t.Fatal(err)
} else if n != 2 {
t.Fatal(n)
}
if n, err := ledis.Int(c.Do("xsexists", key1)); err != nil {
t.Fatal(err)
} else if n != 1 {
t.Fatal(n)
}
if n, err := ledis.Int(c.Do("sadd", key2, 0, 1, 2, 3)); err != nil {
t.Fatal(err)
} else if n != 4 {

View File

@ -771,6 +771,19 @@ func zlexcountCommand(c *client) error {
return nil
}
func xzexistsCommand(c *client) error {
args := c.args
if len(args) != 1 {
return ErrCmdParams
}
if n, err := c.db.XZExists(args[0]); err != nil {
return err
} else {
c.resp.writeInteger(n)
}
return nil
}
func init() {
register("zadd", zaddCommand)
register("zcard", zcardCommand)
@ -806,4 +819,5 @@ func init() {
register("zxrevscan", zxrevscanCommand)
register("xzscan", zxscanCommand)
register("xzrevscan", zxrevscanCommand)
register("xzexists", xzexistsCommand)
}

View File

@ -13,12 +13,25 @@ func TestZSet(t *testing.T) {
defer c.Close()
key := []byte("myzset")
if n, err := ledis.Int(c.Do("xzexists", key)); err != nil {
t.Fatal(err)
} else if n != 0 {
t.Fatal(n)
}
if n, err := ledis.Int(c.Do("zadd", key, 3, "a", 4, "b")); err != nil {
t.Fatal(err)
} else if n != 2 {
t.Fatal(n)
}
if n, err := ledis.Int(c.Do("xzexists", key)); err != nil {
t.Fatal(err)
} else if n != 1 {
t.Fatal(n)
}
if n, err := ledis.Int(c.Do("zcard", key)); err != nil {
t.Fatal(n)
} else if n != 2 {