mirror of https://github.com/ledisdb/ledisdb.git
write batch add Close
This commit is contained in:
parent
9b4c23353e
commit
f47b916828
|
@ -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{}
|
||||||
|
|
|
@ -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 {
|
||||||
|
|
|
@ -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()
|
||||||
}
|
}
|
||||||
|
|
|
@ -34,13 +34,13 @@ func newWriteBatch(db *DB) *WriteBatch {
|
||||||
return w
|
return w
|
||||||
}
|
}
|
||||||
|
|
||||||
func (w *WriteBatch) Close() error {
|
func (w *WriteBatch) Close() {
|
||||||
|
if w.wbatch != nil {
|
||||||
C.leveldb_writebatch_destroy(w.wbatch)
|
C.leveldb_writebatch_destroy(w.wbatch)
|
||||||
w.wbatch = nil
|
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) {
|
||||||
|
|
|
@ -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 {
|
||||||
|
|
|
@ -17,10 +17,11 @@ type WriteBatch struct {
|
||||||
commitOk bool
|
commitOk bool
|
||||||
}
|
}
|
||||||
|
|
||||||
func (w *WriteBatch) Close() error {
|
func (w *WriteBatch) Close() {
|
||||||
|
if w.wbatch != nil {
|
||||||
C.rocksdb_writebatch_destroy(w.wbatch)
|
C.rocksdb_writebatch_destroy(w.wbatch)
|
||||||
w.wbatch = nil
|
w.wbatch = nil
|
||||||
return nil
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (w *WriteBatch) Put(key, value []byte) {
|
func (w *WriteBatch) Put(key, value []byte) {
|
||||||
|
|
|
@ -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
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -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)
|
||||||
|
|
Loading…
Reference in New Issue