try to optimize rocksdb get

hope can improve performance
This commit is contained in:
siddontang 2014-11-03 23:42:34 +08:00
parent 8cfc5732cf
commit b74487eadb
2 changed files with 77 additions and 0 deletions

View File

@ -0,0 +1,20 @@
#!/bin/bash
ROCKSDB_DIR=/usr/local/rocksdb
if test -z "$TARGET_OS"; then
TARGET_OS=`uname -s`
fi
PLATFORM_SHARED_EXT="so"
PLATFORM_SHARED_LDFLAGS="-shared -Wl,-soname -Wl,"
PLATFORM_SHARED_CFLAGS="-fPIC"
if [ "$TARGET_OS" = "Darwin" ]; then
PLATFORM_SHARED_EXT=dylib
PLATFORM_SHARED_LDFLAGS="-dynamiclib -install_name "
fi
SONAME=librocksdb_ext.$PLATFORM_SHARED_EXT
g++ $PLATFORM_SHARED_LDFLAGS$SONAME $PLATFORM_SHARED_CFLAGS -std=c++0x -L$ROCKSDB_DIR/lib -lrocksdb -I/$ROCKSDB_DIR/include rocksdb_ext.cc -o $SONAME

57
tools/rocksdb_ext.cc Normal file
View File

@ -0,0 +1,57 @@
#include <rocksdb/c.h>
#include <rocksdb/db.h>
#include <string>
using namespace rocksdb;
extern "C" {
static bool SaveError(char** errptr, const Status& s) {
assert(errptr != NULL);
if (s.ok()) {
return false;
} else if (*errptr == NULL) {
*errptr = strdup(s.ToString().c_str());
} else {
free(*errptr);
*errptr = strdup(s.ToString().c_str());
}
return true;
}
void* rocksdb_get_ext(
rocksdb_t* db,
const rocksdb_readoptions_t* options,
const char* key, size_t keylen,
char** valptr,
size_t* vallen,
char** errptr) {
std::string *tmp = new(std::string);
//very tricky, maybe changed with c++ rocksdb upgrade
Status s = (*(DB**)db)->Get(*(ReadOptions*)options, Slice(key, keylen), tmp);
if (s.ok()) {
*valptr = (char*)tmp->data();
*vallen = tmp->size();
} else {
delete(tmp);
tmp = NULL;
*valptr = NULL;
*vallen = 0;
if (!s.IsNotFound()) {
SaveError(errptr, s);
}
}
return tmp;
}
void rocksdb_get_free_ext(void* context) {
std::string* s = (std::string*)context;
delete(s);
}
}