1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109
|
#include <string>
#include "rocksdb/slice_transform.h"
#include "rocksdb/env.h"
#include <stdexcept>
using std::string;
using rocksdb::SliceTransform;
using rocksdb::Slice;
using rocksdb::Logger;
namespace py_rocks {
class SliceTransformWrapper: public SliceTransform {
public:
typedef Slice (*transform_func)(
void*,
Logger*,
string&,
const Slice&);
typedef bool (*in_domain_func)(
void*,
Logger*,
string&,
const Slice&);
typedef bool (*in_range_func)(
void*,
Logger*,
string&,
const Slice&);
SliceTransformWrapper(
string name,
void* ctx,
transform_func transform_callback,
in_domain_func in_domain_callback,
in_range_func in_range_callback):
name(name),
ctx(ctx),
transform_callback(transform_callback),
in_domain_callback(in_domain_callback),
in_range_callback(in_range_callback)
{}
virtual const char* Name() const {
return this->name.c_str();
}
virtual Slice Transform(const Slice& src) const {
string error_msg;
Slice val;
val = this->transform_callback(
this->ctx,
this->info_log.get(),
error_msg,
src);
if (error_msg.size()) {
throw std::runtime_error(error_msg.c_str());
}
return val;
}
virtual bool InDomain(const Slice& src) const {
string error_msg;
bool val;
val = this->in_domain_callback(
this->ctx,
this->info_log.get(),
error_msg,
src);
if (error_msg.size()) {
throw std::runtime_error(error_msg.c_str());
}
return val;
}
virtual bool InRange(const Slice& dst) const {
string error_msg;
bool val;
val = this->in_range_callback(
this->ctx,
this->info_log.get(),
error_msg,
dst);
if (error_msg.size()) {
throw std::runtime_error(error_msg.c_str());
}
return val;
}
void set_info_log(std::shared_ptr<Logger> info_log) {
this->info_log = info_log;
}
private:
string name;
void* ctx;
transform_func transform_callback;
in_domain_func in_domain_callback;
in_range_func in_range_callback;
std::shared_ptr<Logger> info_log;
};
}
|