diff --git a/ledis/cmd_hash.go b/ledis/cmd_hash.go index 297d469..91cc192 100644 --- a/ledis/cmd_hash.go +++ b/ledis/cmd_hash.go @@ -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) } diff --git a/ledis/cmd_hash_test.go b/ledis/cmd_hash_test.go index 47a004e..590c022 100644 --- a/ledis/cmd_hash_test.go +++ b/ledis/cmd_hash_test.go @@ -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) + } } diff --git a/ledis/cmd_list.go b/ledis/cmd_list.go index 6b39cc6..09ed7a0 100644 --- a/ledis/cmd_list.go +++ b/ledis/cmd_list.go @@ -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) + } diff --git a/ledis/cmd_list_test.go b/ledis/cmd_list_test.go index 77a9a74..33b7455 100644 --- a/ledis/cmd_list_test.go +++ b/ledis/cmd_list_test.go @@ -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) + } + } diff --git a/ledis/cmd_zset.go b/ledis/cmd_zset.go index e580f51..4035629 100644 --- a/ledis/cmd_zset.go +++ b/ledis/cmd_zset.go @@ -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) + } diff --git a/ledis/cmd_zset_test.go b/ledis/cmd_zset_test.go index c8a05fc..b32d412 100644 --- a/ledis/cmd_zset_test.go +++ b/ledis/cmd_zset_test.go @@ -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) + } + } diff --git a/ledis/t_hash.go b/ledis/t_hash.go index e539389..2c4d488 100644 --- a/ledis/t_hash.go +++ b/ledis/t_hash.go @@ -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 +} diff --git a/ledis/t_list.go b/ledis/t_list.go index 335d2f7..8515b8d 100644 --- a/ledis/t_list.go +++ b/ledis/t_list.go @@ -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 +} diff --git a/ledis/t_zset.go b/ledis/t_zset.go index 4613269..bca7b6d 100644 --- a/ledis/t_zset.go +++ b/ledis/t_zset.go @@ -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) +}