diff --git a/ledis/ledis_test.go b/ledis/ledis_test.go index abd656c..2512632 100644 --- a/ledis/ledis_test.go +++ b/ledis/ledis_test.go @@ -62,3 +62,40 @@ func TestSelect(t *testing.T) { t.Fatal(string(v)) } } + +func TestFlush(t *testing.T) { + db0, _ := testLedis.Select(0) + db1, _ := testLedis.Select(1) + + db0.Set([]byte("a"), []byte("1")) + db0.ZAdd([]byte("zset_0"), ScorePair{int64(1), []byte("ma")}) + db0.ZAdd([]byte("zset_0"), ScorePair{int64(2), []byte("mb")}) + + db1.Set([]byte("b"), []byte("2")) + db1.LPush([]byte("lst"), []byte("a1"), []byte("b2")) + db1.ZAdd([]byte("zset_0"), ScorePair{int64(3), []byte("mc")}) + + db1.Flush() + + // 0 - existing + if exists, _ := db0.Exists([]byte("a")); exists <= 0 { + t.Fatal(false) + } + + if zcnt, _ := db0.ZCard([]byte("zset_0")); zcnt != 2 { + t.Fatal(zcnt) + } + + // 1 - deleted + if exists, _ := db1.Exists([]byte("b")); exists > 0 { + t.Fatal(false) + } + + if llen, _ := db1.LLen([]byte("lst")); llen > 0 { + t.Fatal(llen) + } + + if zcnt, _ := db1.ZCard([]byte("zset_1")); zcnt > 0 { + t.Fatal(zcnt) + } +} diff --git a/ledis/t_hash.go b/ledis/t_hash.go index 5c949c5..27ec359 100644 --- a/ledis/t_hash.go +++ b/ledis/t_hash.go @@ -396,3 +396,26 @@ func (db *DB) HClear(key []byte) (int64, error) { err := t.Commit() return num, err } + +func (db *DB) HFlush() (drop int64, err error) { + t := db.kvTx + t.Lock() + defer t.Unlock() + + minKey := make([]byte, 2) + minKey[0] = db.index + minKey[1] = hashType + + maxKey := make([]byte, 2) + maxKey[0] = db.index + maxKey[1] = hSizeType + 1 + + it := db.db.Iterator(minKey, maxKey, leveldb.RangeROpen, 0, -1) + for ; it.Valid(); it.Next() { + t.Delete(it.Key()) + drop++ + } + + err = t.Commit() + return +} diff --git a/ledis/t_kv.go b/ledis/t_kv.go index 5f08d62..d2acc50 100644 --- a/ledis/t_kv.go +++ b/ledis/t_kv.go @@ -2,6 +2,7 @@ package ledis import ( "errors" + "github.com/siddontang/go-leveldb/leveldb" ) type KVPair struct { @@ -258,3 +259,26 @@ func (db *DB) SetNX(key []byte, value []byte) (int64, error) { return n, err } + +func (db *DB) KvFlush() (drop int64, err error) { + t := db.kvTx + t.Lock() + defer t.Unlock() + + minKey := make([]byte, 2) + minKey[0] = db.index + minKey[1] = kvType + + maxKey := make([]byte, 2) + maxKey[0] = db.index + maxKey[1] = kvType + 1 + + it := db.db.Iterator(minKey, maxKey, leveldb.RangeROpen, 0, -1) + for ; it.Valid(); it.Next() { + t.Delete(it.Key()) + drop++ + } + + err = t.Commit() + return +} diff --git a/ledis/t_list.go b/ledis/t_list.go index bb18585..34cbb66 100644 --- a/ledis/t_list.go +++ b/ledis/t_list.go @@ -380,3 +380,26 @@ func (db *DB) LClear(key []byte) (int64, error) { err = t.Commit() return num, err } + +func (db *DB) LFlush() (drop int64, err error) { + t := db.listTx + t.Lock() + defer t.Unlock() + + minKey := make([]byte, 2) + minKey[0] = db.index + minKey[1] = listType + + maxKey := make([]byte, 2) + maxKey[0] = db.index + maxKey[1] = lMetaType + 1 + + it := db.db.Iterator(minKey, maxKey, leveldb.RangeROpen, 0, -1) + for ; it.Valid(); it.Next() { + t.Delete(it.Key()) + drop++ + } + + err = t.Commit() + return +} diff --git a/ledis/t_zset.go b/ledis/t_zset.go index bd1318c..e1034d9 100644 --- a/ledis/t_zset.go +++ b/ledis/t_zset.go @@ -660,3 +660,27 @@ func (db *DB) ZRangeByScoreGeneric(key []byte, min int64, max int64, return db.zRange(key, min, max, withScores, offset, count, reverse) } + +func (db *DB) ZFlush() (drop int64, err error) { + t := db.zsetTx + t.Lock() + defer t.Unlock() + + minKey := make([]byte, 2) + minKey[0] = db.index + minKey[1] = zsetType + + maxKey := make([]byte, 2) + maxKey[0] = db.index + maxKey[1] = zScoreType + 1 + + it := db.db.Iterator(minKey, maxKey, leveldb.RangeROpen, 0, -1) + for ; it.Valid(); it.Next() { + t.Delete(it.Key()) + drop++ + } + + err = t.Commit() + // to do : binlog + return +}