Commit ea46eb52 authored by Huachao Huang's avatar Huachao Huang

address comment

parent d7acfb70
......@@ -18,6 +18,7 @@ use libc::size_t;
use std::collections::HashMap;
use std::marker::PhantomData;
use std::slice;
use std::str;
pub struct TablePropertiesCollection {
handle: TablePropertiesCollectionHandle,
......@@ -90,13 +91,13 @@ impl<'a> TablePropertiesCollectionIter<'a> {
}
}
pub fn key(&self) -> String {
pub fn key(&self) -> &str {
unsafe {
let mut klen: size_t = 0;
let k = crocksdb_ffi::crocksdb_table_properties_collection_iter_key(self.inner,
&mut klen);
let bytes = slice::from_raw_parts(k, klen);
String::from_utf8(bytes.to_owned()).unwrap()
str::from_utf8(bytes).unwrap()
}
}
......
......@@ -26,7 +26,7 @@ use std::slice;
/// TablePropertiesCollector object per table and then call it sequentially
pub trait TablePropertiesCollector {
/// The name of the properties collector.
fn name(&self) -> &CString;
fn name(&self) -> &str;
/// Will be called when a new key/value pair is inserted into the table.
fn add_userkey(&mut self,
......@@ -41,21 +41,35 @@ pub trait TablePropertiesCollector {
fn finish(&mut self) -> HashMap<Vec<u8>, Vec<u8>>;
}
extern "C" fn name(collector: *mut c_void) -> *const c_char {
struct TablePropertiesCollectorHandle {
name: CString,
rep: Box<TablePropertiesCollector>,
}
impl TablePropertiesCollectorHandle {
fn new(collector: Box<TablePropertiesCollector>) -> TablePropertiesCollectorHandle {
TablePropertiesCollectorHandle {
name: CString::new(collector.name()).unwrap(),
rep: collector,
}
}
}
extern "C" fn name(handle: *mut c_void) -> *const c_char {
unsafe {
let collector = &mut *(collector as *mut Box<TablePropertiesCollector>);
collector.name().as_ptr()
let handle = &mut *(handle as *mut TablePropertiesCollectorHandle);
handle.name.as_ptr()
}
}
extern "C" fn destruct(collector: *mut c_void) {
extern "C" fn destruct(handle: *mut c_void) {
unsafe {
let collector = &mut *(collector as *mut Box<TablePropertiesCollector>);
Box::from_raw(collector);
let handle = &mut *(handle as *mut TablePropertiesCollectorHandle);
Box::from_raw(handle);
}
}
pub extern "C" fn add_userkey(collector: *mut c_void,
pub extern "C" fn add_userkey(handle: *mut c_void,
key: *const uint8_t,
key_len: size_t,
value: *const uint8_t,
......@@ -64,17 +78,17 @@ pub extern "C" fn add_userkey(collector: *mut c_void,
seq: uint64_t,
file_size: uint64_t) {
unsafe {
let collector = &mut *(collector as *mut Box<TablePropertiesCollector>);
let handle = &mut *(handle as *mut TablePropertiesCollectorHandle);
let key = slice::from_raw_parts(key, key_len);
let value = slice::from_raw_parts(value, value_len);
collector.add_userkey(key, value, mem::transmute(entry_type), seq, file_size);
handle.rep.add_userkey(key, value, mem::transmute(entry_type), seq, file_size);
}
}
pub extern "C" fn finish(collector: *mut c_void, props: *mut DBUserCollectedProperties) {
pub extern "C" fn finish(handle: *mut c_void, props: *mut DBUserCollectedProperties) {
unsafe {
let collector = &mut *(collector as *mut Box<TablePropertiesCollector>);
for (key, value) in collector.finish() {
let handle = &mut *(handle as *mut TablePropertiesCollectorHandle);
for (key, value) in handle.rep.finish() {
crocksdb_ffi::crocksdb_user_collected_properties_add(props,
key.as_ptr(),
key.len(),
......@@ -86,8 +100,9 @@ pub extern "C" fn finish(collector: *mut c_void, props: *mut DBUserCollectedProp
pub unsafe fn new_table_properties_collector(collector: Box<TablePropertiesCollector>)
-> *mut DBTablePropertiesCollector {
let handle = TablePropertiesCollectorHandle::new(collector);
crocksdb_ffi::crocksdb_table_properties_collector_create(
Box::into_raw(Box::new(collector)) as *mut c_void,
Box::into_raw(Box::new(handle)) as *mut c_void,
name,
destruct,
add_userkey,
......
......@@ -20,30 +20,44 @@ use table_properties_collector::{TablePropertiesCollector, new_table_properties_
/// Internals create a new `TablePropertiesCollector` for each new table.
pub trait TablePropertiesCollectorFactory {
/// The name of the properties collector factory.
fn name(&self) -> &CString;
fn name(&self) -> &str;
/// Has to be thread-safe.
fn create_table_properties_collector(&mut self, cf: u32) -> Box<TablePropertiesCollector>;
}
extern "C" fn name(factory: *mut c_void) -> *const c_char {
struct TablePropertiesCollectorFactoryHandle {
name: CString,
rep: Box<TablePropertiesCollectorFactory>,
}
impl TablePropertiesCollectorFactoryHandle {
fn new(rep: Box<TablePropertiesCollectorFactory>) -> TablePropertiesCollectorFactoryHandle {
TablePropertiesCollectorFactoryHandle {
name: CString::new(rep.name()).unwrap(),
rep: rep,
}
}
}
extern "C" fn name(handle: *mut c_void) -> *const c_char {
unsafe {
let factory = &mut *(factory as *mut Box<TablePropertiesCollectorFactory>);
factory.name().as_ptr()
let handle = &mut *(handle as *mut TablePropertiesCollectorFactoryHandle);
handle.name.as_ptr()
}
}
extern "C" fn destruct(factory: *mut c_void) {
extern "C" fn destruct(handle: *mut c_void) {
unsafe {
Box::from_raw(factory as *mut Box<TablePropertiesCollectorFactory>);
Box::from_raw(handle as *mut TablePropertiesCollectorFactoryHandle);
}
}
extern "C" fn create_table_properties_collector(factory: *mut c_void,
extern "C" fn create_table_properties_collector(handle: *mut c_void,
cf: uint32_t)
-> *mut DBTablePropertiesCollector {
unsafe {
let factory = &mut *(factory as *mut Box<TablePropertiesCollectorFactory>);
let collector = factory.create_table_properties_collector(cf);
let handle = &mut *(handle as *mut TablePropertiesCollectorFactoryHandle);
let collector = handle.rep.create_table_properties_collector(cf);
new_table_properties_collector(collector)
}
}
......@@ -51,8 +65,9 @@ extern "C" fn create_table_properties_collector(factory: *mut c_void,
pub unsafe fn new_table_properties_collector_factory
(factory: Box<TablePropertiesCollectorFactory>)
-> *mut DBTablePropertiesCollectorFactory {
let handle = TablePropertiesCollectorFactoryHandle::new(factory);
crocksdb_ffi::crocksdb_table_properties_collector_factory_create(
Box::into_raw(Box::new(factory)) as *mut c_void,
Box::into_raw(Box::new(handle)) as *mut c_void,
name,
destruct,
create_table_properties_collector,
......
......@@ -16,7 +16,6 @@ use rocksdb::{DB, Range, Options, Writable, DBEntryType, TablePropertiesCollecti
TablePropertiesCollector, TablePropertiesCollectorFactory};
use std::cmp::Ordering;
use std::collections::HashMap;
use std::ffi::CString;
use std::fmt;
use std::io::Cursor;
use tempdir::TempDir;
......@@ -40,7 +39,6 @@ fn decode_u32(x: &[u8]) -> u32 {
}
struct ExampleCollector {
name: CString,
num_keys: u32,
num_puts: u32,
num_merges: u32,
......@@ -51,7 +49,6 @@ struct ExampleCollector {
impl ExampleCollector {
fn new() -> ExampleCollector {
ExampleCollector {
name: CString::new("example-collector").unwrap(),
num_keys: 0,
num_puts: 0,
num_merges: 0,
......@@ -98,8 +95,8 @@ impl fmt::Display for ExampleCollector {
}
impl TablePropertiesCollector for ExampleCollector {
fn name(&self) -> &CString {
&self.name
fn name(&self) -> &str {
"example-collector"
}
fn add_userkey(&mut self, key: &[u8], _: &[u8], entry_type: DBEntryType, _: u64, _: u64) {
......@@ -122,19 +119,17 @@ impl TablePropertiesCollector for ExampleCollector {
}
}
struct ExampleFactory {
name: CString,
}
struct ExampleFactory {}
impl ExampleFactory {
fn new() -> ExampleFactory {
ExampleFactory { name: CString::new("example-factory").unwrap() }
ExampleFactory {}
}
}
impl TablePropertiesCollectorFactory for ExampleFactory {
fn name(&self) -> &CString {
&self.name
fn name(&self) -> &str {
"example-factory"
}
fn create_table_properties_collector(&mut self, _: u32) -> Box<TablePropertiesCollector> {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment