2014-07-25 13:58:00 +04:00
|
|
|
// +build leveldb
|
|
|
|
|
2014-06-19 13:19:40 +04:00
|
|
|
package leveldb
|
|
|
|
|
|
|
|
// #cgo LDFLAGS: -lleveldb
|
|
|
|
// #include "leveldb/c.h"
|
|
|
|
import "C"
|
|
|
|
|
|
|
|
import (
|
2014-11-10 06:07:21 +03:00
|
|
|
"github.com/siddontang/goleveldb/leveldb"
|
2014-06-19 13:19:40 +04:00
|
|
|
"unsafe"
|
|
|
|
)
|
|
|
|
|
2014-11-10 06:07:21 +03:00
|
|
|
/*
|
|
|
|
It's not easy to let leveldb support Data() function like rocksdb,
|
|
|
|
so here, we will use goleveldb batch instead
|
|
|
|
|
|
|
|
later optimize
|
|
|
|
*/
|
|
|
|
|
2014-06-19 13:19:40 +04:00
|
|
|
type WriteBatch struct {
|
|
|
|
db *DB
|
|
|
|
wbatch *C.leveldb_writebatch_t
|
2014-11-10 06:07:21 +03:00
|
|
|
|
|
|
|
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
|
2014-06-19 13:19:40 +04:00
|
|
|
}
|
|
|
|
|
2014-11-10 10:31:36 +03:00
|
|
|
func (w *WriteBatch) Close() {
|
|
|
|
if w.wbatch != nil {
|
|
|
|
C.leveldb_writebatch_destroy(w.wbatch)
|
|
|
|
w.wbatch = nil
|
|
|
|
}
|
2014-07-29 18:23:50 +04:00
|
|
|
|
2014-11-10 06:07:21 +03:00
|
|
|
w.gbatch = nil
|
2014-06-19 13:19:40 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (w *WriteBatch) Put(key, value []byte) {
|
2014-11-10 06:07:21 +03:00
|
|
|
w.gbatch.Put(key, value)
|
|
|
|
|
2014-06-19 13:19:40 +04:00
|
|
|
var k, v *C.char
|
|
|
|
if len(key) != 0 {
|
|
|
|
k = (*C.char)(unsafe.Pointer(&key[0]))
|
|
|
|
}
|
|
|
|
if len(value) != 0 {
|
|
|
|
v = (*C.char)(unsafe.Pointer(&value[0]))
|
|
|
|
}
|
|
|
|
|
|
|
|
lenk := len(key)
|
|
|
|
lenv := len(value)
|
|
|
|
|
|
|
|
C.leveldb_writebatch_put(w.wbatch, k, C.size_t(lenk), v, C.size_t(lenv))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *WriteBatch) Delete(key []byte) {
|
2014-11-10 06:07:21 +03:00
|
|
|
w.gbatch.Delete(key)
|
|
|
|
|
2014-06-19 13:19:40 +04:00
|
|
|
C.leveldb_writebatch_delete(w.wbatch,
|
|
|
|
(*C.char)(unsafe.Pointer(&key[0])), C.size_t(len(key)))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *WriteBatch) Commit() error {
|
2014-11-10 06:07:21 +03:00
|
|
|
w.gbatch.Reset()
|
|
|
|
|
2014-06-19 13:19:40 +04:00
|
|
|
return w.commit(w.db.writeOpts)
|
|
|
|
}
|
|
|
|
|
2014-10-09 09:05:55 +04:00
|
|
|
func (w *WriteBatch) SyncCommit() error {
|
2014-11-10 06:07:21 +03:00
|
|
|
w.gbatch.Reset()
|
|
|
|
|
2014-10-09 09:05:55 +04:00
|
|
|
return w.commit(w.db.syncOpts)
|
|
|
|
}
|
|
|
|
|
2014-07-25 13:58:00 +04:00
|
|
|
func (w *WriteBatch) Rollback() error {
|
2014-06-19 13:19:40 +04:00
|
|
|
C.leveldb_writebatch_clear(w.wbatch)
|
2014-11-10 06:07:21 +03:00
|
|
|
|
|
|
|
w.gbatch.Reset()
|
|
|
|
|
2014-07-25 13:58:00 +04:00
|
|
|
return nil
|
2014-06-19 13:19:40 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (w *WriteBatch) commit(wb *WriteOptions) error {
|
|
|
|
var errStr *C.char
|
|
|
|
C.leveldb_write(w.db.db, wb.Opt, w.wbatch, &errStr)
|
|
|
|
if errStr != nil {
|
|
|
|
return saveError(errStr)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2014-11-09 18:10:03 +03:00
|
|
|
|
|
|
|
func (w *WriteBatch) Data() []byte {
|
2014-11-10 06:07:21 +03:00
|
|
|
return w.gbatch.Dump()
|
2014-11-09 18:10:03 +03:00
|
|
|
}
|