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
|
#include "rocksdb/filter_policy.h"
#include "rocksdb/env.h"
#include <stdexcept>
using std::string;
using rocksdb::FilterPolicy;
using rocksdb::Slice;
using rocksdb::Logger;
namespace py_rocks {
class FilterPolicyWrapper: public FilterPolicy {
public:
typedef void (*create_filter_func)(
void* ctx,
Logger*,
string&,
const Slice* keys,
int n,
string* dst);
typedef bool (*key_may_match_func)(
void* ctx,
Logger*,
string&,
const Slice& key,
const Slice& filter);
FilterPolicyWrapper(
string name,
void* ctx,
create_filter_func create_filter_callback,
key_may_match_func key_may_match_callback):
name(name),
ctx(ctx),
create_filter_callback(create_filter_callback),
key_may_match_callback(key_may_match_callback)
{}
virtual void
CreateFilter(const Slice* keys, int n, std::string* dst) const {
string error_msg;
this->create_filter_callback(
this->ctx,
this->info_log.get(),
error_msg,
keys,
n,
dst);
if (error_msg.size()) {
throw std::runtime_error(error_msg.c_str());
}
}
virtual bool
KeyMayMatch(const Slice& key, const Slice& filter) const {
string error_msg;
bool val;
val = this->key_may_match_callback(
this->ctx,
this->info_log.get(),
error_msg,
key,
filter);
if (error_msg.size()) {
throw std::runtime_error(error_msg.c_str());
}
return val;
}
virtual const char* Name() const {
return this->name.c_str();
}
void set_info_log(std::shared_ptr<Logger> info_log) {
this->info_log = info_log;
}
private:
string name;
void* ctx;
create_filter_func create_filter_callback;
key_may_match_func key_may_match_callback;
std::shared_ptr<Logger> info_log;
};
}
|