remove BatchWrite Close

leveled and rocksdb use runtime SetFinalizer instead
This commit is contained in:
siddontang 2014-07-29 22:23:50 +08:00
parent 39521c29e9
commit a712a95d0d
9 changed files with 14 additions and 12 deletions

View File

@ -27,7 +27,7 @@ func newTx(l *Ledis) *tx {
} }
func (t *tx) Close() { func (t *tx) Close() {
t.wb.Close() t.wb = nil
} }
func (t *tx) Put(key []byte, value []byte) { func (t *tx) Put(key []byte, value []byte) {

View File

@ -40,8 +40,6 @@ type IIterator interface {
} }
type IWriteBatch interface { type IWriteBatch interface {
Close() error
Put(key []byte, value []byte) Put(key []byte, value []byte)
Delete(key []byte) Delete(key []byte)
Commit() error Commit() error

View File

@ -9,10 +9,6 @@ type WriteBatch struct {
wbatch *leveldb.Batch wbatch *leveldb.Batch
} }
func (w *WriteBatch) Close() error {
return nil
}
func (w *WriteBatch) Put(key, value []byte) { func (w *WriteBatch) Put(key, value []byte) {
w.wbatch.Put(key, value) w.wbatch.Put(key, value)
} }

View File

@ -17,6 +17,8 @@ type WriteBatch struct {
func (w *WriteBatch) Close() error { func (w *WriteBatch) Close() error {
C.leveldb_writebatch_destroy(w.wbatch) C.leveldb_writebatch_destroy(w.wbatch)
w.wbatch = nil
return nil return nil
} }

View File

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

View File

@ -14,10 +14,6 @@ type WriteBatch struct {
wb []Write wb []Write
} }
func (w *WriteBatch) Close() error {
return nil
}
func (w *WriteBatch) Put(key, value []byte) { func (w *WriteBatch) Put(key, value []byte) {
w.wb = append(w.wb, Write{key, value}) w.wb = append(w.wb, Write{key, value})
} }

View File

@ -17,6 +17,7 @@ type WriteBatch struct {
func (w *WriteBatch) Close() error { func (w *WriteBatch) Close() error {
C.rocksdb_writebatch_destroy(w.wbatch) C.rocksdb_writebatch_destroy(w.wbatch)
w.wbatch = nil
return nil return nil
} }

View File

@ -207,6 +207,11 @@ func (db *DB) NewWriteBatch() driver.IWriteBatch {
db: db, db: db,
wbatch: C.rocksdb_writebatch_create(), wbatch: C.rocksdb_writebatch_create(),
} }
runtime.SetFinalizer(wb, func(w *WriteBatch) {
w.Close()
})
return wb return wb
} }

View File

@ -49,7 +49,6 @@ func testBatch(db *DB, t *testing.T) {
db.Put(key2, value) db.Put(key2, value)
wb := db.NewWriteBatch() wb := db.NewWriteBatch()
defer wb.Close()
wb.Delete(key2) wb.Delete(key2)
wb.Put(key1, []byte("hello world2")) wb.Put(key1, []byte("hello world2"))