From 5863e52dedf80621a5715cac9765a37ce28827a6 Mon Sep 17 00:00:00 2001 From: wenyekui Date: Sun, 1 Feb 2015 17:54:52 +0800 Subject: [PATCH] add xsexists and xzexists commands --- ledis/t_set.go | 12 ++++++++++++ ledis/t_set_test.go | 19 +++++++++++++++++++ ledis/t_zset.go | 12 ++++++++++++ ledis/t_zset_test.go | 18 ++++++++++++++++++ server/cmd_set.go | 15 +++++++++++++++ server/cmd_set_test.go | 12 ++++++++++++ server/cmd_zset.go | 14 ++++++++++++++ server/cmd_zset_test.go | 13 +++++++++++++ 8 files changed, 115 insertions(+) diff --git a/ledis/t_set.go b/ledis/t_set.go index b81496d..126b407 100644 --- a/ledis/t_set.go +++ b/ledis/t_set.go @@ -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) diff --git a/ledis/t_set_test.go b/ledis/t_set_test.go index cee8023..eb24bdb 100644 --- a/ledis/t_set_test.go +++ b/ledis/t_set_test.go @@ -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) + } + +} diff --git a/ledis/t_zset.go b/ledis/t_zset.go index 4719171..36f886b 100644 --- a/ledis/t_zset.go +++ b/ledis/t_zset.go @@ -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 +} diff --git a/ledis/t_zset_test.go b/ledis/t_zset_test.go index 98f0a81..cdb35bc 100644 --- a/ledis/t_zset_test.go +++ b/ledis/t_zset_test.go @@ -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) + } +} diff --git a/server/cmd_set.go b/server/cmd_set.go index ea5a75c..ae22bc5 100644 --- a/server/cmd_set.go +++ b/server/cmd_set.go @@ -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) + } diff --git a/server/cmd_set_test.go b/server/cmd_set_test.go index 0e76520..03c0702 100644 --- a/server/cmd_set_test.go +++ b/server/cmd_set_test.go @@ -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 { diff --git a/server/cmd_zset.go b/server/cmd_zset.go index a58dbbb..ddeefbd 100644 --- a/server/cmd_zset.go +++ b/server/cmd_zset.go @@ -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) } diff --git a/server/cmd_zset_test.go b/server/cmd_zset_test.go index 59411c5..7760e7b 100644 --- a/server/cmd_zset_test.go +++ b/server/cmd_zset_test.go @@ -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 {