use crocksdb_ffi::{self, DBTablePropertiesCollector, DBTablePropertiesCollectorFactory};
use libc::{c_char, c_void, uint32_t};
use std::ffi::CString;
use table_properties_collector::{new_table_properties_collector, TablePropertiesCollector};
pub trait TablePropertiesCollectorFactory {
fn create_table_properties_collector(&mut self, cf: u32) -> Box<TablePropertiesCollector>;
}
struct TablePropertiesCollectorFactoryHandle {
name: CString,
rep: Box<TablePropertiesCollectorFactory>,
}
impl TablePropertiesCollectorFactoryHandle {
fn new(
name: &str,
rep: Box<TablePropertiesCollectorFactory>,
) -> TablePropertiesCollectorFactoryHandle {
TablePropertiesCollectorFactoryHandle {
name: CString::new(name).unwrap(),
rep: rep,
}
}
}
extern "C" fn name(handle: *mut c_void) -> *const c_char {
unsafe {
let handle = &mut *(handle as *mut TablePropertiesCollectorFactoryHandle);
handle.name.as_ptr()
}
}
extern "C" fn destruct(handle: *mut c_void) {
unsafe {
Box::from_raw(handle as *mut TablePropertiesCollectorFactoryHandle);
}
}
extern "C" fn create_table_properties_collector(
handle: *mut c_void,
cf: uint32_t,
) -> *mut DBTablePropertiesCollector {
unsafe {
let handle = &mut *(handle as *mut TablePropertiesCollectorFactoryHandle);
let collector = handle.rep.create_table_properties_collector(cf);
new_table_properties_collector(handle.name.to_str().unwrap(), collector)
}
}
pub unsafe fn new_table_properties_collector_factory(
fname: &str,
factory: Box<TablePropertiesCollectorFactory>,
) -> *mut DBTablePropertiesCollectorFactory {
let handle = TablePropertiesCollectorFactoryHandle::new(fname, factory);
crocksdb_ffi::crocksdb_table_properties_collector_factory_create(
Box::into_raw(Box::new(handle)) as *mut c_void,
name,
destruct,
create_table_properties_collector,
)
}