ledisdb/ledis/ledis_db.go

99 lines
2.0 KiB
Go
Raw Normal View History

package ledis
2014-07-03 07:38:52 +04:00
import (
2014-08-16 08:38:49 +04:00
"fmt"
2014-07-25 13:58:00 +04:00
"github.com/siddontang/ledisdb/store"
2014-07-03 07:38:52 +04:00
)
func (db *DB) FlushAll() (drop int64, err error) {
all := [...](func() (int64, error)){
db.flush,
db.lFlush,
db.hFlush,
2014-07-03 07:38:52 +04:00
db.zFlush,
db.bFlush,
db.sFlush}
for _, flush := range all {
if n, e := flush(); e != nil {
err = e
return
} else {
drop += n
}
}
return
}
func (db *DB) newEliminator() *elimination {
eliminator := newEliminator(db)
eliminator.regRetireContext(KVType, db.kvTx, db.delete)
eliminator.regRetireContext(ListType, db.listTx, db.lDelete)
eliminator.regRetireContext(HashType, db.hashTx, db.hDelete)
eliminator.regRetireContext(ZSetType, db.zsetTx, db.zDelete)
eliminator.regRetireContext(BitType, db.binTx, db.bDelete)
return eliminator
}
2014-07-03 07:38:52 +04:00
func (db *DB) flushRegion(t *tx, minKey []byte, maxKey []byte) (drop int64, err error) {
2014-07-25 13:58:00 +04:00
it := db.db.RangeIterator(minKey, maxKey, store.RangeROpen)
2014-07-03 07:38:52 +04:00
for ; it.Valid(); it.Next() {
t.Delete(it.RawKey())
2014-07-03 07:38:52 +04:00
drop++
if drop&1023 == 0 {
if err = t.Commit(); err != nil {
return
}
}
}
it.Close()
return
}
2014-08-16 08:38:49 +04:00
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
2014-08-16 11:35:05 +04:00
case BitType:
deleteFunc = db.bDelete
metaDataType = BitMetaType
2014-08-16 12:55:36 +04:00
case SetType:
deleteFunc = db.sDelete
metaDataType = SSizeType
2014-08-16 08:38:49 +04:00
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 {
2014-08-16 11:35:05 +04:00
deleteFunc(t, key)
2014-08-16 08:38:49 +04:00
db.rmExpire(t, dataType, key)
2014-08-16 11:35:05 +04:00
2014-08-16 08:38:49 +04:00
}
if err = t.Commit(); err != nil {
return
2014-08-16 11:35:05 +04:00
} else {
drop += int64(len(keys))
2014-08-16 08:38:49 +04:00
}
keys, err = db.scan(metaDataType, nil, 1024, false)
}
return
}