store remove tricky code

This commit is contained in:
siddontang 2014-08-30 17:19:10 +08:00
parent ed982db4cc
commit 577d545486
8 changed files with 142 additions and 120 deletions

View File

@ -19,6 +19,16 @@ func (db *DB) NewWriteBatch() WriteBatch {
return db.IDB.NewWriteBatch() return db.IDB.NewWriteBatch()
} }
func (db *DB) NewSnapshot() (*Snapshot, error) {
var err error
s := &Snapshot{}
if s.ISnapshot, err = db.IDB.NewSnapshot(); err != nil {
return nil, err
}
return s, nil
}
func (db *DB) RangeIterator(min []byte, max []byte, rangeType uint8) *RangeLimitIterator { func (db *DB) RangeIterator(min []byte, max []byte, rangeType uint8) *RangeLimitIterator {
return NewRangeLimitIterator(db.NewIterator(), &Range{min, max, rangeType}, &Limit{0, -1}) return NewRangeLimitIterator(db.NewIterator(), &Range{min, max, rangeType}, &Limit{0, -1})
} }

View File

@ -234,10 +234,8 @@ func (db *DB) get(ro *ReadOptions, key []byte) ([]byte, error) {
k = (*C.char)(unsafe.Pointer(&key[0])) k = (*C.char)(unsafe.Pointer(&key[0]))
} }
var value *C.char value := C.leveldb_get(
db.db, ro.Opt, k, C.size_t(len(key)), &vallen, &errStr)
c := C.hyperleveldb_get_ext(
db.db, ro.Opt, k, C.size_t(len(key)), &value, &vallen, &errStr)
if errStr != nil { if errStr != nil {
return nil, saveError(errStr) return nil, saveError(errStr)
@ -247,7 +245,7 @@ func (db *DB) get(ro *ReadOptions, key []byte) ([]byte, error) {
return nil, nil return nil, nil
} }
defer C.hyperleveldb_get_free_ext(unsafe.Pointer(c)) defer C.leveldb_free(unsafe.Pointer(value))
return C.GoBytes(unsafe.Pointer(value), C.int(vallen)), nil return C.GoBytes(unsafe.Pointer(value), C.int(vallen)), nil
} }

View File

@ -3,60 +3,60 @@
#include "hyperleveldb_ext.h" #include "hyperleveldb_ext.h"
#include <stdlib.h> #include <stdlib.h>
#include <string> //#include <string>
#include "hyperleveldb/db.h" //#include "hyperleveldb/db.h"
using namespace leveldb; //using namespace leveldb;
extern "C" { extern "C" {
static bool SaveError(char** errptr, const Status& s) { // static bool SaveError(char** errptr, const Status& s) {
assert(errptr != NULL); // assert(errptr != NULL);
if (s.ok()) { // if (s.ok()) {
return false; // return false;
} else if (*errptr == NULL) { // } else if (*errptr == NULL) {
*errptr = strdup(s.ToString().c_str()); // *errptr = strdup(s.ToString().c_str());
} else { // } else {
free(*errptr); // free(*errptr);
*errptr = strdup(s.ToString().c_str()); // *errptr = strdup(s.ToString().c_str());
} // }
return true; // return true;
} // }
void* hyperleveldb_get_ext( // void* hyperleveldb_get_ext(
leveldb_t* db, // leveldb_t* db,
const leveldb_readoptions_t* options, // const leveldb_readoptions_t* options,
const char* key, size_t keylen, // const char* key, size_t keylen,
char** valptr, // char** valptr,
size_t* vallen, // size_t* vallen,
char** errptr) { // char** errptr) {
std::string *tmp = new(std::string); // std::string *tmp = new(std::string);
//very tricky, maybe changed with c++ leveldb upgrade // //very tricky, maybe changed with c++ leveldb upgrade
Status s = (*(DB**)db)->Get(*(ReadOptions*)options, Slice(key, keylen), tmp); // Status s = (*(DB**)db)->Get(*(ReadOptions*)options, Slice(key, keylen), tmp);
if (s.ok()) { // if (s.ok()) {
*valptr = (char*)tmp->data(); // *valptr = (char*)tmp->data();
*vallen = tmp->size(); // *vallen = tmp->size();
} else { // } else {
delete(tmp); // delete(tmp);
tmp = NULL; // tmp = NULL;
*valptr = NULL; // *valptr = NULL;
*vallen = 0; // *vallen = 0;
if (!s.IsNotFound()) { // if (!s.IsNotFound()) {
SaveError(errptr, s); // SaveError(errptr, s);
} // }
} // }
return tmp; // return tmp;
} // }
void hyperleveldb_get_free_ext(void* context) { // void hyperleveldb_get_free_ext(void* context) {
std::string* s = (std::string*)context; // std::string* s = (std::string*)context;
delete(s); // delete(s);
} // }
unsigned char hyperleveldb_iter_seek_to_first_ext(leveldb_iterator_t* iter) { unsigned char hyperleveldb_iter_seek_to_first_ext(leveldb_iterator_t* iter) {

View File

@ -10,19 +10,19 @@ extern "C" {
#include "hyperleveldb/c.h" #include "hyperleveldb/c.h"
/* Returns NULL if not found. Otherwise stores the value in **valptr. // /* Returns NULL if not found. Otherwise stores the value in **valptr.
Stores the length of the value in *vallen. // Stores the length of the value in *vallen.
Returns a context must be later to free*/ // Returns a context must be later to free*/
extern void* hyperleveldb_get_ext( // extern void* hyperleveldb_get_ext(
leveldb_t* db, // leveldb_t* db,
const leveldb_readoptions_t* options, // const leveldb_readoptions_t* options,
const char* key, size_t keylen, // const char* key, size_t keylen,
char** valptr, // char** valptr,
size_t* vallen, // size_t* vallen,
char** errptr); // char** errptr);
// Free context returns by hyperleveldb_get_ext // // Free context returns by hyperleveldb_get_ext
extern void hyperleveldb_get_free_ext(void* context); // extern void hyperleveldb_get_free_ext(void* context);
// Below iterator functions like leveldb iterator but returns valid status for iterator // Below iterator functions like leveldb iterator but returns valid status for iterator

View File

@ -234,10 +234,8 @@ func (db *DB) get(ro *ReadOptions, key []byte) ([]byte, error) {
k = (*C.char)(unsafe.Pointer(&key[0])) k = (*C.char)(unsafe.Pointer(&key[0]))
} }
var value *C.char value := C.leveldb_get(
db.db, ro.Opt, k, C.size_t(len(key)), &vallen, &errStr)
c := C.leveldb_get_ext(
db.db, ro.Opt, k, C.size_t(len(key)), &value, &vallen, &errStr)
if errStr != nil { if errStr != nil {
return nil, saveError(errStr) return nil, saveError(errStr)
@ -247,7 +245,7 @@ func (db *DB) get(ro *ReadOptions, key []byte) ([]byte, error) {
return nil, nil return nil, nil
} }
defer C.leveldb_get_free_ext(unsafe.Pointer(c)) defer C.leveldb_free(unsafe.Pointer(value))
return C.GoBytes(unsafe.Pointer(value), C.int(vallen)), nil return C.GoBytes(unsafe.Pointer(value), C.int(vallen)), nil
} }

View File

@ -3,60 +3,60 @@
#include "leveldb_ext.h" #include "leveldb_ext.h"
#include <stdlib.h> #include <stdlib.h>
#include <string> //#include <string>
#include "leveldb/db.h" //#include "leveldb/db.h"
using namespace leveldb; //using namespace leveldb;
extern "C" { extern "C" {
static bool SaveError(char** errptr, const Status& s) { // static bool SaveError(char** errptr, const Status& s) {
assert(errptr != NULL); // assert(errptr != NULL);
if (s.ok()) { // if (s.ok()) {
return false; // return false;
} else if (*errptr == NULL) { // } else if (*errptr == NULL) {
*errptr = strdup(s.ToString().c_str()); // *errptr = strdup(s.ToString().c_str());
} else { // } else {
free(*errptr); // free(*errptr);
*errptr = strdup(s.ToString().c_str()); // *errptr = strdup(s.ToString().c_str());
} // }
return true; // return true;
} // }
void* leveldb_get_ext( // void* leveldb_get_ext(
leveldb_t* db, // leveldb_t* db,
const leveldb_readoptions_t* options, // const leveldb_readoptions_t* options,
const char* key, size_t keylen, // const char* key, size_t keylen,
char** valptr, // char** valptr,
size_t* vallen, // size_t* vallen,
char** errptr) { // char** errptr) {
std::string *tmp = new(std::string); // std::string *tmp = new(std::string);
//very tricky, maybe changed with c++ leveldb upgrade // //very tricky, maybe changed with c++ leveldb upgrade
Status s = (*(DB**)db)->Get(*(ReadOptions*)options, Slice(key, keylen), tmp); // Status s = (*(DB**)db)->Get(*(ReadOptions*)options, Slice(key, keylen), tmp);
if (s.ok()) { // if (s.ok()) {
*valptr = (char*)tmp->data(); // *valptr = (char*)tmp->data();
*vallen = tmp->size(); // *vallen = tmp->size();
} else { // } else {
delete(tmp); // delete(tmp);
tmp = NULL; // tmp = NULL;
*valptr = NULL; // *valptr = NULL;
*vallen = 0; // *vallen = 0;
if (!s.IsNotFound()) { // if (!s.IsNotFound()) {
SaveError(errptr, s); // SaveError(errptr, s);
} // }
} // }
return tmp; // return tmp;
} // }
void leveldb_get_free_ext(void* context) { // void leveldb_get_free_ext(void* context) {
std::string* s = (std::string*)context; // std::string* s = (std::string*)context;
delete(s); // delete(s);
} // }
unsigned char leveldb_iter_seek_to_first_ext(leveldb_iterator_t* iter) { unsigned char leveldb_iter_seek_to_first_ext(leveldb_iterator_t* iter) {

View File

@ -10,19 +10,19 @@ extern "C" {
#include "leveldb/c.h" #include "leveldb/c.h"
/* Returns NULL if not found. Otherwise stores the value in **valptr. // /* Returns NULL if not found. Otherwise stores the value in **valptr.
Stores the length of the value in *vallen. // Stores the length of the value in *vallen.
Returns a context must be later to free*/ // Returns a context must be later to free*/
extern void* leveldb_get_ext( // extern void* leveldb_get_ext(
leveldb_t* db, // leveldb_t* db,
const leveldb_readoptions_t* options, // const leveldb_readoptions_t* options,
const char* key, size_t keylen, // const char* key, size_t keylen,
char** valptr, // char** valptr,
size_t* vallen, // size_t* vallen,
char** errptr); // char** errptr);
// Free context returns by leveldb_get_ext // // Free context returns by leveldb_get_ext
extern void leveldb_get_free_ext(void* context); // extern void leveldb_get_free_ext(void* context);
// Below iterator functions like leveldb iterator but returns valid status for iterator // Below iterator functions like leveldb iterator but returns valid status for iterator

16
store/snapshot.go Normal file
View File

@ -0,0 +1,16 @@
package store
import (
"github.com/siddontang/ledisdb/store/driver"
)
type Snapshot struct {
driver.ISnapshot
}
func (s *Snapshot) NewIterator() *Iterator {
it := new(Iterator)
it.it = s.ISnapshot.NewIterator()
return it
}