forked from mirror/ledisdb
write batch add data function
leveled will support it later
This commit is contained in:
parent
34d485056f
commit
c0f8a436a1
|
@ -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",
|
||||||
|
|
|
@ -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}
|
||||||
}
|
}
|
||||||
|
|
|
@ -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 {
|
||||||
|
|
|
@ -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()
|
||||||
|
}
|
||||||
|
|
|
@ -63,3 +63,8 @@ func (w *WriteBatch) commit(wb *WriteOptions) error {
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (w *WriteBatch) Data() []byte {
|
||||||
|
//todo later
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
|
@ -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))
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue