From 928b4a03ae5a1e5472c63afefead92a54bd0e3a6 Mon Sep 17 00:00:00 2001 From: wenyekui Date: Sat, 16 Aug 2014 12:38:49 +0800 Subject: [PATCH] refactor flush --- ledis/ledis_db.go | 38 ++++++++++++++++++++++++++++++++++++++ ledis/t_bit.go | 24 +++++++++++++----------- ledis/t_hash.go | 12 +----------- ledis/t_list.go | 26 +++++++++++++------------- 4 files changed, 65 insertions(+), 35 deletions(-) diff --git a/ledis/ledis_db.go b/ledis/ledis_db.go index 1bd9a54..55f6dd9 100644 --- a/ledis/ledis_db.go +++ b/ledis/ledis_db.go @@ -1,6 +1,7 @@ package ledis import ( + "fmt" "github.com/siddontang/ledisdb/store" ) @@ -50,3 +51,40 @@ func (db *DB) flushRegion(t *tx, minKey []byte, maxKey []byte) (drop int64, err it.Close() return } + +func (db *DB) flushType(t *tx, dataType byte) (drop int64, err error) { + var deleteFunc func(t *tx, key []byte) int64 + var metaDataType byte + switch dataType { + case KVType: + deleteFunc = db.delete + metaDataType = KVType + case ListType: + deleteFunc = db.lDelete + metaDataType = LMetaType + case HashType: + deleteFunc = db.hDelete + metaDataType = HSizeType + case ZSetType: + deleteFunc = db.zDelete + metaDataType = ZSizeType + + default: + return 0, fmt.Errorf("invalid data type: %s", TypeName[dataType]) + } + + var keys [][]byte + keys, err = db.scan(metaDataType, nil, 1024, false) + for len(keys) != 0 || err != nil { + for _, key := range keys { + drop += deleteFunc(t, key) + db.rmExpire(t, dataType, key) + } + + if err = t.Commit(); err != nil { + return + } + keys, err = db.scan(metaDataType, nil, 1024, false) + } + return +} diff --git a/ledis/t_bit.go b/ledis/t_bit.go index eab354d..3fcc4c2 100644 --- a/ledis/t_bit.go +++ b/ledis/t_bit.go @@ -917,17 +917,19 @@ func (db *DB) bFlush() (drop int64, err error) { t.Lock() defer t.Unlock() - minKey := make([]byte, 2) - minKey[0] = db.index - minKey[1] = BitType + var startKey []byte = nil + var keys [][]byte + for keys, err = db.BScan(startKey, 1024, false); len(keys) != 0 || err != nil; { + for _, key := range keys { + drop += db.bDelete(t, key) + db.rmExpire(t, BitType, key) + } - maxKey := make([]byte, 2) - maxKey[0] = db.index - maxKey[1] = BitMetaType + 1 - - drop, err = db.flushRegion(t, minKey, maxKey) - err = db.expFlush(t, BitType) - - err = t.Commit() + if err = t.Commit(); err != nil { + return + } + startKey = keys[len(keys)-1] + keys, err = db.BScan(startKey, 1024, false) + } return } diff --git a/ledis/t_hash.go b/ledis/t_hash.go index eb6bbb1..91cf7a3 100644 --- a/ledis/t_hash.go +++ b/ledis/t_hash.go @@ -453,22 +453,12 @@ func (db *DB) HMclear(keys ...[]byte) (int64, error) { } func (db *DB) hFlush() (drop int64, err error) { - minKey := make([]byte, 2) - minKey[0] = db.index - minKey[1] = HashType - - maxKey := make([]byte, 2) - maxKey[0] = db.index - maxKey[1] = HSizeType + 1 t := db.kvTx t.Lock() defer t.Unlock() - drop, err = db.flushRegion(t, minKey, maxKey) - err = db.expFlush(t, HashType) - - err = t.Commit() + // return db.flushAll(t *tx, dataType byte, expDataType byte) return } diff --git a/ledis/t_list.go b/ledis/t_list.go index f851013..3233548 100644 --- a/ledis/t_list.go +++ b/ledis/t_list.go @@ -424,22 +424,22 @@ func (db *DB) LMclear(keys ...[]byte) (int64, error) { } func (db *DB) lFlush() (drop int64, err error) { - minKey := make([]byte, 2) - minKey[0] = db.index - minKey[1] = ListType - - maxKey := make([]byte, 2) - maxKey[0] = db.index - maxKey[1] = LMetaType + 1 - - t := db.listTx + t := db.binTx t.Lock() defer t.Unlock() - drop, err = db.flushRegion(t, minKey, maxKey) - err = db.expFlush(t, ListType) - - err = t.Commit() + var startKey []byte = nil + var keys [][]byte + for keys, err = db.LScan(startKey, 1024, false); len(keys) != 0 || err != nil; { + var num int64 + if num, err = db.LMclear(keys...); err != nil { + return + } else { + drop += num + } + startKey = keys[len(keys)-1] + keys, err = db.LScan(startKey, 1024, false) + } return }