2014-05-22 16:02:22 +04:00
|
|
|
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-08-25 10:18:23 +04:00
|
|
|
"sync"
|
2014-07-03 07:38:52 +04:00
|
|
|
)
|
|
|
|
|
2014-08-25 10:18:23 +04:00
|
|
|
type ibucket interface {
|
|
|
|
Get(key []byte) ([]byte, error)
|
|
|
|
|
|
|
|
Put(key []byte, value []byte) error
|
|
|
|
Delete(key []byte) error
|
|
|
|
|
|
|
|
NewIterator() *store.Iterator
|
|
|
|
|
|
|
|
NewWriteBatch() store.WriteBatch
|
|
|
|
|
|
|
|
RangeIterator(min []byte, max []byte, rangeType uint8) *store.RangeLimitIterator
|
|
|
|
RevRangeIterator(min []byte, max []byte, rangeType uint8) *store.RangeLimitIterator
|
|
|
|
RangeLimitIterator(min []byte, max []byte, rangeType uint8, offset int, count int) *store.RangeLimitIterator
|
|
|
|
RevRangeLimitIterator(min []byte, max []byte, rangeType uint8, offset int, count int) *store.RangeLimitIterator
|
|
|
|
}
|
|
|
|
|
|
|
|
type DB struct {
|
|
|
|
l *Ledis
|
|
|
|
|
|
|
|
sdb *store.DB
|
|
|
|
|
|
|
|
bucket ibucket
|
|
|
|
|
|
|
|
dbLock *sync.RWMutex
|
|
|
|
|
|
|
|
index uint8
|
|
|
|
|
|
|
|
kvBatch *batch
|
|
|
|
listBatch *batch
|
|
|
|
hashBatch *batch
|
|
|
|
zsetBatch *batch
|
|
|
|
binBatch *batch
|
|
|
|
setBatch *batch
|
|
|
|
|
|
|
|
isTx bool
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l *Ledis) newDB(index uint8) *DB {
|
|
|
|
d := new(DB)
|
|
|
|
|
|
|
|
d.l = l
|
|
|
|
|
|
|
|
d.sdb = l.ldb
|
|
|
|
|
|
|
|
d.bucket = d.sdb
|
|
|
|
|
|
|
|
d.isTx = false
|
|
|
|
d.index = index
|
|
|
|
d.dbLock = &sync.RWMutex{}
|
|
|
|
|
|
|
|
d.kvBatch = d.newBatch()
|
|
|
|
d.listBatch = d.newBatch()
|
|
|
|
d.hashBatch = d.newBatch()
|
|
|
|
d.zsetBatch = d.newBatch()
|
|
|
|
d.binBatch = d.newBatch()
|
|
|
|
d.setBatch = d.newBatch()
|
|
|
|
|
|
|
|
return d
|
|
|
|
}
|
|
|
|
|
|
|
|
func (db *DB) Index() int {
|
|
|
|
return int(db.index)
|
|
|
|
}
|
|
|
|
|
2014-06-03 11:40:10 +04:00
|
|
|
func (db *DB) FlushAll() (drop int64, err error) {
|
2014-05-22 16:02:22 +04:00
|
|
|
all := [...](func() (int64, error)){
|
2014-06-12 06:51:36 +04:00
|
|
|
db.flush,
|
|
|
|
db.lFlush,
|
|
|
|
db.hFlush,
|
2014-07-03 07:38:52 +04:00
|
|
|
db.zFlush,
|
2014-08-14 15:35:41 +04:00
|
|
|
db.bFlush,
|
|
|
|
db.sFlush}
|
2014-05-22 16:02:22 +04:00
|
|
|
|
|
|
|
for _, flush := range all {
|
|
|
|
if n, e := flush(); e != nil {
|
|
|
|
err = e
|
|
|
|
return
|
|
|
|
} else {
|
|
|
|
drop += n
|
|
|
|
}
|
|
|
|
}
|
2014-06-03 11:40:10 +04:00
|
|
|
|
2014-05-22 16:02:22 +04:00
|
|
|
return
|
|
|
|
}
|
2014-06-03 11:40:10 +04:00
|
|
|
|
2014-06-12 06:51:36 +04:00
|
|
|
func (db *DB) newEliminator() *elimination {
|
2014-06-03 11:40:10 +04:00
|
|
|
eliminator := newEliminator(db)
|
2014-08-25 10:18:23 +04:00
|
|
|
|
|
|
|
eliminator.regRetireContext(KVType, db.kvBatch, db.delete)
|
|
|
|
eliminator.regRetireContext(ListType, db.listBatch, db.lDelete)
|
|
|
|
eliminator.regRetireContext(HashType, db.hashBatch, db.hDelete)
|
|
|
|
eliminator.regRetireContext(ZSetType, db.zsetBatch, db.zDelete)
|
|
|
|
eliminator.regRetireContext(BitType, db.binBatch, db.bDelete)
|
|
|
|
eliminator.regRetireContext(SetType, db.setBatch, db.sDelete)
|
2014-06-03 11:40:10 +04:00
|
|
|
|
2014-06-12 06:51:36 +04:00
|
|
|
return eliminator
|
2014-06-03 11:40:10 +04:00
|
|
|
}
|
2014-07-03 07:38:52 +04:00
|
|
|
|
2014-08-25 10:18:23 +04:00
|
|
|
func (db *DB) flushRegion(t *batch, minKey []byte, maxKey []byte) (drop int64, err error) {
|
|
|
|
it := db.bucket.RangeIterator(minKey, maxKey, store.RangeROpen)
|
2014-07-03 07:38:52 +04:00
|
|
|
for ; it.Valid(); it.Next() {
|
2014-07-09 13:01:29 +04:00
|
|
|
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
|
|
|
|
2014-08-25 10:18:23 +04:00
|
|
|
func (db *DB) flushType(t *batch, dataType byte) (drop int64, err error) {
|
|
|
|
var deleteFunc func(t *batch, key []byte) int64
|
2014-08-16 08:38:49 +04:00
|
|
|
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
|
2014-08-25 18:25:10 +04:00
|
|
|
keys, err = db.scan(metaDataType, nil, 1024, false, "")
|
2014-08-16 08:38:49 +04:00
|
|
|
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
|
|
|
}
|
2014-08-25 18:25:10 +04:00
|
|
|
keys, err = db.scan(metaDataType, nil, 1024, false, "")
|
2014-08-16 08:38:49 +04:00
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|