diff --git a/Godeps/Godeps.json b/Godeps/Godeps.json index 362496e..9fa9487 100644 --- a/Godeps/Godeps.json +++ b/Godeps/Godeps.json @@ -56,7 +56,7 @@ }, { "ImportPath": "github.com/siddontang/goleveldb/leveldb", - "Rev": "448b15792e63fe835c48d294f9b7859a86f4b76e" + "Rev": "41805642b981fb3d9462f6641bcb94b8609ca791" }, { "ImportPath": "github.com/szferi/gomdb", diff --git a/store/driver/batch.go b/store/driver/batch.go index 5fc461f..33c07ed 100644 --- a/store/driver/batch.go +++ b/store/driver/batch.go @@ -1,5 +1,9 @@ package driver +import ( + "github.com/siddontang/goleveldb/leveldb" +) + type BatchPuter interface { BatchPut([]Write) error SyncBatchPut([]Write) error @@ -11,34 +15,48 @@ type Write struct { } type WriteBatch struct { - batch BatchPuter - wb []Write + d *leveldb.Batch + + wb []Write + w BatchPuter } -func (w *WriteBatch) Put(key, value []byte) { - if value == nil { - value = []byte{} - } - w.wb = append(w.wb, Write{key, value}) +func (wb *WriteBatch) Put(key, value []byte) { + wb.wb = append(wb.wb, Write{key, value}) } -func (w *WriteBatch) Delete(key []byte) { - w.wb = append(w.wb, Write{key, nil}) +func (wb *WriteBatch) Delete(key []byte) { + wb.wb = append(wb.wb, Write{key, nil}) } -func (w *WriteBatch) Commit() error { - return w.batch.BatchPut(w.wb) +func (wb *WriteBatch) Commit() error { + return wb.w.BatchPut(wb.wb) } -func (w *WriteBatch) SyncCommit() error { - return w.batch.SyncBatchPut(w.wb) +func (wb *WriteBatch) SyncCommit() error { + return wb.w.SyncBatchPut(wb.wb) } -func (w *WriteBatch) Rollback() error { - w.wb = w.wb[0:0] +func (wb *WriteBatch) Rollback() error { + wb.wb = wb.wb[0:0] return nil } -func NewWriteBatch(puter BatchPuter) IWriteBatch { - return &WriteBatch{puter, []Write{}} +func (wb *WriteBatch) Data() []byte { + wb.d.Reset() + for _, w := range wb.wb { + if w.Value == nil { + wb.Delete(w.Key) + } else { + wb.Put(w.Key, w.Value) + } + } + return wb.d.Dump() +} + +func NewWriteBatch(puter BatchPuter) *WriteBatch { + return &WriteBatch{ + &leveldb.Batch{}, + []Write{}, + puter} } diff --git a/store/driver/driver.go b/store/driver/driver.go index 31d15de..04133c7 100644 --- a/store/driver/driver.go +++ b/store/driver/driver.go @@ -58,6 +58,7 @@ type IWriteBatch interface { Commit() error SyncCommit() error Rollback() error + Data() []byte } type Tx interface { diff --git a/store/goleveldb/batch.go b/store/goleveldb/batch.go index 85b78c6..aef0f55 100644 --- a/store/goleveldb/batch.go +++ b/store/goleveldb/batch.go @@ -29,3 +29,7 @@ func (w *WriteBatch) Rollback() error { w.wbatch.Reset() return nil } + +func (w *WriteBatch) Data() []byte { + return w.wbatch.Dump() +} diff --git a/store/leveldb/batch.go b/store/leveldb/batch.go index caadc03..777e11c 100644 --- a/store/leveldb/batch.go +++ b/store/leveldb/batch.go @@ -63,3 +63,8 @@ func (w *WriteBatch) commit(wb *WriteOptions) error { } return nil } + +func (w *WriteBatch) Data() []byte { + //todo later + return nil +} diff --git a/store/rocksdb/batch.go b/store/rocksdb/batch.go index 6a94a63..aa4e407 100644 --- a/store/rocksdb/batch.go +++ b/store/rocksdb/batch.go @@ -73,3 +73,10 @@ func (w *WriteBatch) commit(wb *WriteOptions) error { } return nil } + +func (w *WriteBatch) Data() []byte { + var vallen C.size_t + value := C.rocksdb_writebatch_data(w.wbatch, &vallen) + + return slice(unsafe.Pointer(value), int(vallen)) +}