write batch add Close

This commit is contained in:
siddontang 2014-11-10 15:31:36 +08:00
parent 9b4c23353e
commit f47b916828
8 changed files with 37 additions and 10 deletions

View File

@ -21,6 +21,11 @@ type WriteBatch struct {
w BatchPuter w BatchPuter
} }
func (wb *WriteBatch) Close() {
wb.d.Reset()
wb.wb = wb.wb[0:0]
}
func (wb *WriteBatch) Put(key, value []byte) { func (wb *WriteBatch) Put(key, value []byte) {
if value == nil { if value == nil {
value = []byte{} value = []byte{}

View File

@ -59,6 +59,7 @@ type IWriteBatch interface {
SyncCommit() error SyncCommit() error
Rollback() error Rollback() error
Data() []byte Data() []byte
Close()
} }
type Tx interface { type Tx interface {

View File

@ -30,6 +30,10 @@ func (w *WriteBatch) Rollback() error {
return nil return nil
} }
func (w *WriteBatch) Close() {
w.wbatch.Reset()
}
func (w *WriteBatch) Data() []byte { func (w *WriteBatch) Data() []byte {
return w.wbatch.Dump() return w.wbatch.Dump()
} }

View File

@ -34,13 +34,13 @@ func newWriteBatch(db *DB) *WriteBatch {
return w return w
} }
func (w *WriteBatch) Close() error { func (w *WriteBatch) Close() {
C.leveldb_writebatch_destroy(w.wbatch) if w.wbatch != nil {
w.wbatch = nil C.leveldb_writebatch_destroy(w.wbatch)
w.wbatch = nil
}
w.gbatch = nil w.gbatch = nil
return nil
} }
func (w *WriteBatch) Put(key, value []byte) { func (w *WriteBatch) Put(key, value []byte) {

View File

@ -14,6 +14,7 @@ import (
"github.com/siddontang/ledisdb/config" "github.com/siddontang/ledisdb/config"
"github.com/siddontang/ledisdb/store/driver" "github.com/siddontang/ledisdb/store/driver"
"os" "os"
"runtime"
"unsafe" "unsafe"
) )
@ -182,7 +183,13 @@ func (db *DB) SyncDelete(key []byte) error {
} }
func (db *DB) NewWriteBatch() driver.IWriteBatch { func (db *DB) NewWriteBatch() driver.IWriteBatch {
return newWriteBatch(db) wb := newWriteBatch(db)
runtime.SetFinalizer(wb, func(w *WriteBatch) {
w.Close()
})
return wb
} }
func (db *DB) NewIterator() driver.IIterator { func (db *DB) NewIterator() driver.IIterator {

View File

@ -17,10 +17,11 @@ type WriteBatch struct {
commitOk bool commitOk bool
} }
func (w *WriteBatch) Close() error { func (w *WriteBatch) Close() {
C.rocksdb_writebatch_destroy(w.wbatch) if w.wbatch != nil {
w.wbatch = nil C.rocksdb_writebatch_destroy(w.wbatch)
return nil w.wbatch = nil
}
} }
func (w *WriteBatch) Put(key, value []byte) { func (w *WriteBatch) Put(key, value []byte) {

View File

@ -15,6 +15,7 @@ import (
"github.com/siddontang/ledisdb/config" "github.com/siddontang/ledisdb/config"
"github.com/siddontang/ledisdb/store/driver" "github.com/siddontang/ledisdb/store/driver"
"os" "os"
"runtime"
"unsafe" "unsafe"
) )
@ -215,6 +216,10 @@ func (db *DB) NewWriteBatch() driver.IWriteBatch {
wbatch: C.rocksdb_writebatch_create(), wbatch: C.rocksdb_writebatch_create(),
} }
runtime.SetFinalizer(wb, func(w *WriteBatch) {
w.Close()
})
return wb return wb
} }

View File

@ -16,6 +16,10 @@ type WriteBatch struct {
db *DB db *DB
} }
func (wb *WriteBatch) Close() {
wb.wb.Close()
}
func (wb *WriteBatch) Put(key []byte, value []byte) { func (wb *WriteBatch) Put(key []byte, value []byte) {
wb.putNum++ wb.putNum++
wb.wb.Put(key, value) wb.wb.Put(key, value)