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", "ImportPath": "github.com/siddontang/goleveldb/leveldb",
"Rev": "448b15792e63fe835c48d294f9b7859a86f4b76e" "Rev": "41805642b981fb3d9462f6641bcb94b8609ca791"
}, },
{ {
"ImportPath": "github.com/szferi/gomdb", "ImportPath": "github.com/szferi/gomdb",

View File

@ -1,5 +1,9 @@
package driver package driver
import (
"github.com/siddontang/goleveldb/leveldb"
)
type BatchPuter interface { type BatchPuter interface {
BatchPut([]Write) error BatchPut([]Write) error
SyncBatchPut([]Write) error SyncBatchPut([]Write) error
@ -11,34 +15,48 @@ type Write struct {
} }
type WriteBatch struct { type WriteBatch struct {
batch BatchPuter d *leveldb.Batch
wb []Write wb []Write
w BatchPuter
} }
func (w *WriteBatch) Put(key, value []byte) { func (wb *WriteBatch) Put(key, value []byte) {
if value == nil { wb.wb = append(wb.wb, Write{key, value})
value = []byte{}
}
w.wb = append(w.wb, Write{key, value})
} }
func (w *WriteBatch) Delete(key []byte) { func (wb *WriteBatch) Delete(key []byte) {
w.wb = append(w.wb, Write{key, nil}) wb.wb = append(wb.wb, Write{key, nil})
} }
func (w *WriteBatch) Commit() error { func (wb *WriteBatch) Commit() error {
return w.batch.BatchPut(w.wb) return wb.w.BatchPut(wb.wb)
} }
func (w *WriteBatch) SyncCommit() error { func (wb *WriteBatch) SyncCommit() error {
return w.batch.SyncBatchPut(w.wb) return wb.w.SyncBatchPut(wb.wb)
} }
func (w *WriteBatch) Rollback() error { func (wb *WriteBatch) Rollback() error {
w.wb = w.wb[0:0] wb.wb = wb.wb[0:0]
return nil return nil
} }
func NewWriteBatch(puter BatchPuter) IWriteBatch { func (wb *WriteBatch) Data() []byte {
return &WriteBatch{puter, []Write{}} 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 Commit() error
SyncCommit() error SyncCommit() error
Rollback() error Rollback() error
Data() []byte
} }
type Tx interface { type Tx interface {

View File

@ -29,3 +29,7 @@ func (w *WriteBatch) Rollback() error {
w.wbatch.Reset() w.wbatch.Reset()
return nil 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 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 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))
}