write batch add data function

leveled will support it later
This commit is contained in:
siddontang 2014-11-09 23:10:03 +08:00
parent 34d485056f
commit c0f8a436a1
6 changed files with 53 additions and 18 deletions

2
Godeps/Godeps.json generated
View File

@ -56,7 +56,7 @@
},
{
"ImportPath": "github.com/siddontang/goleveldb/leveldb",
"Rev": "448b15792e63fe835c48d294f9b7859a86f4b76e"
"Rev": "41805642b981fb3d9462f6641bcb94b8609ca791"
},
{
"ImportPath": "github.com/szferi/gomdb",

View File

@ -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}
}

View File

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

View File

@ -29,3 +29,7 @@ func (w *WriteBatch) Rollback() error {
w.wbatch.Reset()
return nil
}
func (w *WriteBatch) Data() []byte {
return w.wbatch.Dump()
}

View File

@ -63,3 +63,8 @@ func (w *WriteBatch) commit(wb *WriteOptions) error {
}
return nil
}
func (w *WriteBatch) Data() []byte {
//todo later
return nil
}

View File

@ -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))
}