From b47e49d6fd379d358ef0078b23fd26cd02500295 Mon Sep 17 00:00:00 2001 From: siddontang Date: Thu, 30 Oct 2014 11:43:04 +0800 Subject: [PATCH] try reduce rocksdb batch clear --- store/rocksdb/batch.go | 19 +++++++++++++++---- store/rocksdb/rocksdb_ext.cc | 8 ++++++++ store/rocksdb/rocksdb_ext.h | 2 +- 3 files changed, 24 insertions(+), 5 deletions(-) diff --git a/store/rocksdb/batch.go b/store/rocksdb/batch.go index 017fc88..6a94a63 100644 --- a/store/rocksdb/batch.go +++ b/store/rocksdb/batch.go @@ -4,6 +4,7 @@ package rocksdb // #cgo LDFLAGS: -lrocksdb // #include "rocksdb/c.h" +// #include "rocksdb_ext.h" import "C" import ( @@ -11,8 +12,9 @@ import ( ) type WriteBatch struct { - db *DB - wbatch *C.rocksdb_writebatch_t + db *DB + wbatch *C.rocksdb_writebatch_t + commitOk bool } func (w *WriteBatch) Close() error { @@ -22,6 +24,8 @@ func (w *WriteBatch) Close() error { } func (w *WriteBatch) Put(key, value []byte) { + w.commitOk = false + var k, v *C.char if len(key) != 0 { k = (*C.char)(unsafe.Pointer(&key[0])) @@ -37,6 +41,8 @@ func (w *WriteBatch) Put(key, value []byte) { } func (w *WriteBatch) Delete(key []byte) { + w.commitOk = false + C.rocksdb_writebatch_delete(w.wbatch, (*C.char)(unsafe.Pointer(&key[0])), C.size_t(len(key))) } @@ -50,14 +56,19 @@ func (w *WriteBatch) SyncCommit() error { } func (w *WriteBatch) Rollback() error { - C.rocksdb_writebatch_clear(w.wbatch) + if !w.commitOk { + C.rocksdb_writebatch_clear(w.wbatch) + } return nil } func (w *WriteBatch) commit(wb *WriteOptions) error { + w.commitOk = true + var errStr *C.char - C.rocksdb_write(w.db.db, wb.Opt, w.wbatch, &errStr) + C.rocksdb_write_ext(w.db.db, wb.Opt, w.wbatch, &errStr) if errStr != nil { + w.commitOk = false return saveError(errStr) } return nil diff --git a/store/rocksdb/rocksdb_ext.cc b/store/rocksdb/rocksdb_ext.cc index 4a7720f..39036ab 100644 --- a/store/rocksdb/rocksdb_ext.cc +++ b/store/rocksdb/rocksdb_ext.cc @@ -32,5 +32,13 @@ unsigned char rocksdb_iter_prev_ext(rocksdb_iterator_t* iter) { return rocksdb_iter_valid(iter); } +void rocksdb_write_ext(rocksdb_t* db, + const rocksdb_writeoptions_t* options, + rocksdb_writebatch_t* batch, char** errptr) { + rocksdb_write(db, options, batch, errptr); + if(*errptr == NULL) { + rocksdb_writebatch_clear(batch); + } +} } \ No newline at end of file diff --git a/store/rocksdb/rocksdb_ext.h b/store/rocksdb/rocksdb_ext.h index 4938294..11cb653 100644 --- a/store/rocksdb/rocksdb_ext.h +++ b/store/rocksdb/rocksdb_ext.h @@ -15,7 +15,7 @@ extern unsigned char rocksdb_iter_seek_to_last_ext(rocksdb_iterator_t*); extern unsigned char rocksdb_iter_seek_ext(rocksdb_iterator_t*, const char* k, size_t klen); extern unsigned char rocksdb_iter_next_ext(rocksdb_iterator_t*); extern unsigned char rocksdb_iter_prev_ext(rocksdb_iterator_t*); - +extern void rocksdb_write_ext(rocksdb_t* db, const rocksdb_writeoptions_t* options, rocksdb_writebatch_t* batch, char** errptr); #ifdef __cplusplus }