ledisdb/store/writebatch.go

146 lines
2.4 KiB
Go
Raw Normal View History

2014-07-25 13:58:00 +04:00
package store
import (
2014-11-10 06:07:33 +03:00
"encoding/binary"
"github.com/siddontang/goleveldb/leveldb"
2014-07-25 13:58:00 +04:00
"github.com/siddontang/ledisdb/store/driver"
2014-10-30 11:06:44 +03:00
"time"
2014-07-25 13:58:00 +04:00
)
2014-10-15 06:18:20 +04:00
type WriteBatch struct {
2014-10-30 11:06:44 +03:00
wb driver.IWriteBatch
st *Stat
2014-10-16 05:35:35 +04:00
2014-10-31 15:33:13 +03:00
putNum int64
deleteNum int64
db *DB
2014-10-15 06:18:20 +04:00
}
func (wb *WriteBatch) Put(key []byte, value []byte) {
2014-10-31 15:33:13 +03:00
wb.putNum++
2014-10-16 05:35:35 +04:00
wb.wb.Put(key, value)
2014-10-15 06:18:20 +04:00
}
func (wb *WriteBatch) Delete(key []byte) {
2014-10-31 15:33:13 +03:00
wb.deleteNum++
2014-10-16 05:35:35 +04:00
wb.wb.Delete(key)
2014-10-15 06:18:20 +04:00
}
func (wb *WriteBatch) Commit() error {
wb.st.BatchCommitNum.Add(1)
2014-10-31 15:33:13 +03:00
wb.st.PutNum.Add(wb.putNum)
wb.st.DeleteNum.Add(wb.deleteNum)
wb.putNum = 0
wb.deleteNum = 0
2014-10-30 11:06:44 +03:00
var err error
t := time.Now()
2014-10-16 05:35:35 +04:00
if wb.db == nil || !wb.db.needSyncCommit() {
2014-10-30 11:06:44 +03:00
err = wb.wb.Commit()
2014-10-16 05:35:35 +04:00
} else {
2014-10-30 11:06:44 +03:00
err = wb.wb.SyncCommit()
2014-10-16 05:35:35 +04:00
}
2014-10-30 11:06:44 +03:00
wb.st.BatchCommitTotalTime.Add(time.Now().Sub(t))
return err
2014-10-15 06:18:20 +04:00
}
func (wb *WriteBatch) Rollback() error {
2014-10-31 15:33:13 +03:00
wb.putNum = 0
wb.deleteNum = 0
2014-10-16 05:35:35 +04:00
return wb.wb.Rollback()
2014-07-25 13:58:00 +04:00
}
2014-11-10 06:07:33 +03:00
// the data will be undefined after commit or rollback
func (wb *WriteBatch) BatchData() *BatchData {
2014-11-10 06:07:33 +03:00
data := wb.wb.Data()
d, err := NewBatchData(data)
if err != nil {
//can not enter this
panic(err)
}
return d
}
func (wb *WriteBatch) Data() []byte {
b := wb.BatchData()
return b.Data()
2014-11-10 06:07:33 +03:00
}
const BatchDataHeadLen = 12
/*
see leveldb batch data format for more information
*/
type BatchData struct {
leveldb.Batch
}
func NewBatchData(data []byte) (*BatchData, error) {
b := new(BatchData)
if err := b.Load(data); err != nil {
return nil, err
}
return b, nil
}
func (d *BatchData) Append(do *BatchData) error {
d1 := d.Dump()
d2 := do.Dump()
n := d.Len() + do.Len()
d1 = append(d1, d2[BatchDataHeadLen:]...)
binary.LittleEndian.PutUint32(d1[8:], uint32(n))
2014-11-10 06:07:33 +03:00
return d.Load(d1)
}
func (d *BatchData) Data() []byte {
return d.Dump()
}
func (d *BatchData) Reset() {
d.Batch.Reset()
}
2014-11-10 06:07:33 +03:00
type BatchDataReplay interface {
Put(key, value []byte)
Delete(key []byte)
}
type BatchItem struct {
Key []byte
Value []byte
}
type batchItems []BatchItem
func (bs *batchItems) Put(key, value []byte) {
*bs = append(*bs, BatchItem{key, value})
}
func (bs *batchItems) Delete(key []byte) {
*bs = append(*bs, BatchItem{key, nil})
}
func (d *BatchData) Replay(r BatchDataReplay) error {
return d.Batch.Replay(r)
}
func (d *BatchData) Items() ([]BatchItem, error) {
is := make(batchItems, 0, d.Len())
if err := d.Replay(&is); err != nil {
return nil, err
}
return []BatchItem(is), nil
}