add 'FlusbDB' api, which used to clear all data on the specific db

This commit is contained in:
silentsai 2014-05-22 19:59:40 +08:00
parent 68b28fe656
commit 247f105ad7
5 changed files with 131 additions and 0 deletions

View File

@ -62,3 +62,40 @@ func TestSelect(t *testing.T) {
t.Fatal(string(v)) 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)
}
}

View File

@ -396,3 +396,26 @@ func (db *DB) HClear(key []byte) (int64, error) {
err := t.Commit() err := t.Commit()
return num, err 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
}

View File

@ -2,6 +2,7 @@ package ledis
import ( import (
"errors" "errors"
"github.com/siddontang/go-leveldb/leveldb"
) )
type KVPair struct { type KVPair struct {
@ -258,3 +259,26 @@ func (db *DB) SetNX(key []byte, value []byte) (int64, error) {
return n, err 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
}

View File

@ -380,3 +380,26 @@ func (db *DB) LClear(key []byte) (int64, error) {
err = t.Commit() err = t.Commit()
return num, err 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
}

View File

@ -660,3 +660,27 @@ func (db *DB) ZRangeByScoreGeneric(key []byte, min int64, max int64,
return db.zRange(key, min, max, withScores, offset, count, reverse) 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
}