mirror of https://github.com/ledisdb/ledisdb.git
parent
c0f8a436a1
commit
38e5d7262c
|
@ -7,22 +7,45 @@ package leveldb
|
||||||
import "C"
|
import "C"
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"github.com/siddontang/goleveldb/leveldb"
|
||||||
"unsafe"
|
"unsafe"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
/*
|
||||||
|
It's not easy to let leveldb support Data() function like rocksdb,
|
||||||
|
so here, we will use goleveldb batch instead
|
||||||
|
|
||||||
|
later optimize
|
||||||
|
*/
|
||||||
|
|
||||||
type WriteBatch struct {
|
type WriteBatch struct {
|
||||||
db *DB
|
db *DB
|
||||||
wbatch *C.leveldb_writebatch_t
|
wbatch *C.leveldb_writebatch_t
|
||||||
|
|
||||||
|
gbatch *leveldb.Batch
|
||||||
|
}
|
||||||
|
|
||||||
|
func newWriteBatch(db *DB) *WriteBatch {
|
||||||
|
w := new(WriteBatch)
|
||||||
|
w.db = db
|
||||||
|
w.wbatch = C.leveldb_writebatch_create()
|
||||||
|
w.gbatch = new(leveldb.Batch)
|
||||||
|
|
||||||
|
return w
|
||||||
}
|
}
|
||||||
|
|
||||||
func (w *WriteBatch) Close() error {
|
func (w *WriteBatch) Close() error {
|
||||||
C.leveldb_writebatch_destroy(w.wbatch)
|
C.leveldb_writebatch_destroy(w.wbatch)
|
||||||
w.wbatch = nil
|
w.wbatch = nil
|
||||||
|
|
||||||
|
w.gbatch = nil
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (w *WriteBatch) Put(key, value []byte) {
|
func (w *WriteBatch) Put(key, value []byte) {
|
||||||
|
w.gbatch.Put(key, value)
|
||||||
|
|
||||||
var k, v *C.char
|
var k, v *C.char
|
||||||
if len(key) != 0 {
|
if len(key) != 0 {
|
||||||
k = (*C.char)(unsafe.Pointer(&key[0]))
|
k = (*C.char)(unsafe.Pointer(&key[0]))
|
||||||
|
@ -38,20 +61,29 @@ func (w *WriteBatch) Put(key, value []byte) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (w *WriteBatch) Delete(key []byte) {
|
func (w *WriteBatch) Delete(key []byte) {
|
||||||
|
w.gbatch.Delete(key)
|
||||||
|
|
||||||
C.leveldb_writebatch_delete(w.wbatch,
|
C.leveldb_writebatch_delete(w.wbatch,
|
||||||
(*C.char)(unsafe.Pointer(&key[0])), C.size_t(len(key)))
|
(*C.char)(unsafe.Pointer(&key[0])), C.size_t(len(key)))
|
||||||
}
|
}
|
||||||
|
|
||||||
func (w *WriteBatch) Commit() error {
|
func (w *WriteBatch) Commit() error {
|
||||||
|
w.gbatch.Reset()
|
||||||
|
|
||||||
return w.commit(w.db.writeOpts)
|
return w.commit(w.db.writeOpts)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (w *WriteBatch) SyncCommit() error {
|
func (w *WriteBatch) SyncCommit() error {
|
||||||
|
w.gbatch.Reset()
|
||||||
|
|
||||||
return w.commit(w.db.syncOpts)
|
return w.commit(w.db.syncOpts)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (w *WriteBatch) Rollback() error {
|
func (w *WriteBatch) Rollback() error {
|
||||||
C.leveldb_writebatch_clear(w.wbatch)
|
C.leveldb_writebatch_clear(w.wbatch)
|
||||||
|
|
||||||
|
w.gbatch.Reset()
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -65,6 +97,5 @@ func (w *WriteBatch) commit(wb *WriteOptions) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (w *WriteBatch) Data() []byte {
|
func (w *WriteBatch) Data() []byte {
|
||||||
//todo later
|
return w.gbatch.Dump()
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -182,12 +182,7 @@ func (db *DB) SyncDelete(key []byte) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (db *DB) NewWriteBatch() driver.IWriteBatch {
|
func (db *DB) NewWriteBatch() driver.IWriteBatch {
|
||||||
wb := &WriteBatch{
|
return newWriteBatch(db)
|
||||||
db: db,
|
|
||||||
wbatch: C.leveldb_writebatch_create(),
|
|
||||||
}
|
|
||||||
|
|
||||||
return wb
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (db *DB) NewIterator() driver.IIterator {
|
func (db *DB) NewIterator() driver.IIterator {
|
||||||
|
|
Loading…
Reference in New Issue