add lclear, hclear,zclear to del

This commit is contained in:
siddontang 2014-05-12 15:08:59 +08:00
parent 15a273ae52
commit 3ed4e9fc57
9 changed files with 156 additions and 0 deletions

View File

@ -183,6 +183,21 @@ func hvalsCommand(c *client) error {
return nil
}
func hclearCommand(c *client) error {
args := c.args
if len(args) != 1 {
return ErrCmdParams
}
if n, err := c.app.hash_clear(args[0]); err != nil {
return err
} else {
c.writeInteger(n)
}
return nil
}
func init() {
register("hdel", hdelCommand)
register("hexists", hexistsCommand)
@ -195,4 +210,8 @@ func init() {
register("hmset", hmsetCommand)
register("hset", hsetCommand)
register("hvals", hvalsCommand)
//ledisdb special command
register("hclear", hclearCommand)
}

View File

@ -212,4 +212,15 @@ func TestHashGetAll(t *testing.T) {
}
}
if n, err := redis.Int(c.Do("hclear", key)); err != nil {
t.Fatal(err)
} else if n != 3 {
t.Fatal(n)
}
if n, err := redis.Int(c.Do("hlen", key)); err != nil {
t.Fatal(err)
} else if n != 0 {
t.Fatal(n)
}
}

View File

@ -129,6 +129,21 @@ func lrangeCommand(c *client) error {
return nil
}
func lclearCommand(c *client) error {
args := c.args
if len(args) != 1 {
return ErrCmdParams
}
if n, err := c.app.list_clear(args[0]); err != nil {
return err
} else {
c.writeInteger(n)
}
return nil
}
func init() {
register("lindex", lindexCommand)
register("llen", llenCommand)
@ -137,4 +152,9 @@ func init() {
register("lpush", lpushCommand)
register("rpop", rpopCommand)
register("rpush", rpushCommand)
//ledisdb special command
register("lclear", lclearCommand)
}

View File

@ -282,4 +282,19 @@ func TestPop(t *testing.T) {
} else if n != 0 {
t.Fatal(n)
}
c.Do("rpush", key, 1, 2, 3, 4, 5)
if n, err := redis.Int(c.Do("lclear", key)); err != nil {
t.Fatal(err)
} else if n != 5 {
t.Fatal(n)
}
if n, err := redis.Int(c.Do("llen", key)); err != nil {
t.Fatal(err)
} else if n != 0 {
t.Fatal(n)
}
}

View File

@ -440,6 +440,21 @@ func zrevrangebyscoreCommand(c *client) error {
return zrangebyscoreGeneric(c, true)
}
func zclearCommand(c *client) error {
args := c.args
if len(args) != 1 {
return ErrCmdParams
}
if n, err := c.app.zset_clear(args[0]); err != nil {
return err
} else {
c.writeInteger(n)
}
return nil
}
func init() {
register("zadd", zaddCommand)
register("zcard", zcardCommand)
@ -455,4 +470,8 @@ func init() {
register("zrevrank", zrevrankCommand)
register("zrevrangebyscore", zrevrangebyscoreCommand)
register("zscore", zscoreCommand)
//ledisdb special command
register("zclear", zclearCommand)
}

View File

@ -474,4 +474,17 @@ func TestZSetRange(t *testing.T) {
t.Fatal(err)
}
}
if n, err := redis.Int(c.Do("zclear", key)); err != nil {
t.Fatal(err)
} else if n != 2 {
t.Fatal(n)
}
if n, err := redis.Int(c.Do("zcard", key)); err != nil {
t.Fatal(err)
} else if n != 0 {
t.Fatal(n)
}
}

View File

@ -300,3 +300,28 @@ func (a *App) hash_values(key []byte) ([]interface{}, error) {
return v, nil
}
func (a *App) hash_clear(key []byte) (int64, error) {
sk := encode_hsize_key(key)
t := a.hashTx
t.Lock()
defer t.Unlock()
start := encode_hash_start_key(key)
stop := encode_hash_stop_key(key)
var num int64 = 0
it := a.db.Iterator(start, stop, leveldb.RangeROpen, 0, -1)
for ; it.Valid(); it.Next() {
t.Delete(it.Key())
num++
}
it.Close()
t.Delete(sk)
err := t.Commit()
return num, err
}

View File

@ -289,3 +289,33 @@ func (a *App) list_index(key []byte, index int32) ([]byte, error) {
return a.db.Get(encode_list_key(key, seq))
}
func (a *App) list_clear(key []byte) (int64, error) {
mk := encode_lmeta_key(key)
t := a.listTx
t.Lock()
defer t.Unlock()
metaKey := encode_lmeta_key(key)
headSeq, tailSeq, _, err := a.list_getMeta(metaKey)
if err != nil {
return 0, err
}
var num int64 = 0
it := a.db.Iterator(encode_list_key(key, headSeq),
encode_list_key(key, tailSeq), leveldb.RangeClose, 0, -1)
for ; it.Valid(); it.Next() {
t.Delete(it.Key())
num++
}
it.Close()
t.Delete(mk)
err = t.Commit()
return num, err
}

View File

@ -445,3 +445,7 @@ func (a *App) zset_range(key []byte, min int64, max int64, withScores bool, offs
return v, nil
}
func (a *App) zset_clear(key []byte) (int64, error) {
return a.zset_remRange(key, MinScore, MaxScore, 0, -1)
}