Commit a7c9bd69 authored by Huachao Huang's avatar Huachao Huang

*: rewrite to avoid memory layout dependencies

parent e3bd990f
This diff is collapsed.
...@@ -113,30 +113,37 @@ typedef struct crocksdb_ratelimiter_t crocksdb_ratelimiter_t; ...@@ -113,30 +113,37 @@ typedef struct crocksdb_ratelimiter_t crocksdb_ratelimiter_t;
typedef struct crocksdb_pinnableslice_t crocksdb_pinnableslice_t; typedef struct crocksdb_pinnableslice_t crocksdb_pinnableslice_t;
typedef struct crocksdb_user_collected_properties_t typedef struct crocksdb_user_collected_properties_t
crocksdb_user_collected_properties_t; crocksdb_user_collected_properties_t;
typedef struct crocksdb_user_collected_properties_iterator_t
crocksdb_user_collected_properties_iterator_t;
typedef struct crocksdb_table_properties_t crocksdb_table_properties_t;
typedef struct crocksdb_table_properties_collection_t typedef struct crocksdb_table_properties_collection_t
crocksdb_table_properties_collection_t; crocksdb_table_properties_collection_t;
typedef struct crocksdb_table_properties_collection_iterator_t
crocksdb_table_properties_collection_iterator_t;
typedef struct crocksdb_table_properties_collector_t
crocksdb_table_properties_collector_t;
typedef struct crocksdb_table_properties_collector_factory_t typedef struct crocksdb_table_properties_collector_factory_t
crocksdb_table_properties_collector_factory_t; crocksdb_table_properties_collector_factory_t;
typedef struct crocksdb_table_properties_collector_context_t { typedef enum crocksdb_table_property_t {
void* collector; DATA_SIZE = 1,
const char* (*name)(void*); INDEX_SIZE,
void (*destructor)(void*); FILTER_SIZE,
void (*add_userkey)(void*, RAW_KEY_SIZE,
const char* key, size_t key_len, RAW_VALUE_SIZE,
const char* value, size_t value_len, NUM_DATA_BLOCKS,
int entry_type, uint64_t seq, uint64_t file_size); NUM_ENTRIES,
void (*finish)(void*, crocksdb_user_collected_properties_t* props); FORMAT_VERSION,
void (*readable_properties)(void*, crocksdb_user_collected_properties_t* props); FIXED_KEY_LEN,
} crocksdb_table_properties_collector_context_t; COLUMN_FAMILY_ID,
COLUMN_FAMILY_NAME = 11,
typedef struct crocksdb_table_properties_collector_factory_context_t { FILTER_POLICY_NAME,
void* factory; COMPARATOR_NAME,
const char* (*name)(void*); MERGE_OPERATOR_NAME,
void (*destructor)(void*); PREFIX_EXTRACTOR_NAME,
crocksdb_table_properties_collector_context_t* PROPERTY_COLLECTORS_NAMES,
(*create_table_properties_collector)(void*, uint32_t cf); COMPRESSION_NAME,
} crocksdb_table_properties_collector_factory_context_t; } crocksdb_table_property_t;
/* DB operations */ /* DB operations */
...@@ -224,6 +231,9 @@ crocksdb_create_column_family(crocksdb_t* db, ...@@ -224,6 +231,9 @@ crocksdb_create_column_family(crocksdb_t* db,
extern C_ROCKSDB_LIBRARY_API void crocksdb_drop_column_family( extern C_ROCKSDB_LIBRARY_API void crocksdb_drop_column_family(
crocksdb_t* db, crocksdb_column_family_handle_t* handle, char** errptr); crocksdb_t* db, crocksdb_column_family_handle_t* handle, char** errptr);
extern C_ROCKSDB_LIBRARY_API uint32_t crocksdb_column_family_handle_get_id(
crocksdb_column_family_handle_t*);
extern C_ROCKSDB_LIBRARY_API void crocksdb_column_family_handle_destroy( extern C_ROCKSDB_LIBRARY_API void crocksdb_column_family_handle_destroy(
crocksdb_column_family_handle_t*); crocksdb_column_family_handle_t*);
...@@ -1203,20 +1213,45 @@ extern C_ROCKSDB_LIBRARY_API const char* crocksdb_pinnableslice_value( ...@@ -1203,20 +1213,45 @@ extern C_ROCKSDB_LIBRARY_API const char* crocksdb_pinnableslice_value(
extern C_ROCKSDB_LIBRARY_API void extern C_ROCKSDB_LIBRARY_API void
crocksdb_user_collected_properties_add( crocksdb_user_collected_properties_add(
crocksdb_user_collected_properties_t* props, crocksdb_user_collected_properties_t*,
const char* key, size_t key_len, const char* value, size_t value_len); const char* key, size_t key_len, const char* value, size_t value_len);
extern C_ROCKSDB_LIBRARY_API crocksdb_table_properties_collector_factory_t* extern C_ROCKSDB_LIBRARY_API crocksdb_user_collected_properties_iterator_t*
crocksdb_table_properties_collector_factory_create( crocksdb_user_collected_properties_iter_create(
crocksdb_table_properties_collector_factory_context_t* context); crocksdb_user_collected_properties_t*);
extern C_ROCKSDB_LIBRARY_API void extern C_ROCKSDB_LIBRARY_API void
crocksdb_table_properties_collector_factory_destroy( crocksdb_user_collected_properties_iter_destroy(
crocksdb_table_properties_collector_factory_t* factory); crocksdb_user_collected_properties_iterator_t*);
extern C_ROCKSDB_LIBRARY_API unsigned char
crocksdb_user_collected_properties_iter_valid(
crocksdb_user_collected_properties_iterator_t*);
extern C_ROCKSDB_LIBRARY_API void extern C_ROCKSDB_LIBRARY_API void
crocksdb_options_add_table_properties_collector_factory( crocksdb_user_collected_properties_iter_next(
crocksdb_options_t* opt, crocksdb_table_properties_collector_factory_t* factory); crocksdb_user_collected_properties_iterator_t*);
extern C_ROCKSDB_LIBRARY_API const char*
crocksdb_user_collected_properties_iter_key(
crocksdb_user_collected_properties_iterator_t*, size_t* klen);
extern C_ROCKSDB_LIBRARY_API const char*
crocksdb_user_collected_properties_iter_value(
crocksdb_user_collected_properties_iterator_t*, size_t* vlen);
extern C_ROCKSDB_LIBRARY_API uint64_t
crocksdb_table_properties_get_u64(crocksdb_table_properties_t*,
crocksdb_table_property_t prop);
extern C_ROCKSDB_LIBRARY_API const char*
crocksdb_table_properties_get_str(crocksdb_table_properties_t*,
crocksdb_table_property_t prop, size_t* slen);
extern C_ROCKSDB_LIBRARY_API crocksdb_user_collected_properties_t*
crocksdb_table_properties_get_user_properties(crocksdb_table_properties_t*);
/* Table Properties Collection */
extern C_ROCKSDB_LIBRARY_API crocksdb_table_properties_collection_t* extern C_ROCKSDB_LIBRARY_API crocksdb_table_properties_collection_t*
crocksdb_table_properties_collection_create(); crocksdb_table_properties_collection_create();
...@@ -1224,6 +1259,66 @@ crocksdb_table_properties_collection_create(); ...@@ -1224,6 +1259,66 @@ crocksdb_table_properties_collection_create();
extern C_ROCKSDB_LIBRARY_API void extern C_ROCKSDB_LIBRARY_API void
crocksdb_table_properties_collection_destroy(crocksdb_table_properties_collection_t*); crocksdb_table_properties_collection_destroy(crocksdb_table_properties_collection_t*);
extern C_ROCKSDB_LIBRARY_API crocksdb_table_properties_collection_iterator_t*
crocksdb_table_properties_collection_iter_create(
crocksdb_table_properties_collection_t*);
extern C_ROCKSDB_LIBRARY_API void
crocksdb_table_properties_collection_iter_destroy(
crocksdb_table_properties_collection_iterator_t*);
extern C_ROCKSDB_LIBRARY_API unsigned char
crocksdb_table_properties_collection_iter_valid(
crocksdb_table_properties_collection_iterator_t*);
extern C_ROCKSDB_LIBRARY_API void
crocksdb_table_properties_collection_iter_next(
crocksdb_table_properties_collection_iterator_t*);
extern C_ROCKSDB_LIBRARY_API const char*
crocksdb_table_properties_collection_iter_key(
crocksdb_table_properties_collection_iterator_t*, size_t* klen);
extern C_ROCKSDB_LIBRARY_API crocksdb_table_properties_t*
crocksdb_table_properties_collection_iter_value(
crocksdb_table_properties_collection_iterator_t*);
/* Table Properties Collector */
extern C_ROCKSDB_LIBRARY_API crocksdb_table_properties_collector_t*
crocksdb_table_properties_collector_create(
void* state,
const char* (*name)(void*),
void (*destruct)(void*),
void (*add_userkey)(void*,
const char* key, size_t key_len,
const char* value, size_t value_len,
int entry_type, uint64_t seq, uint64_t file_size),
void (*finish)(void*, crocksdb_user_collected_properties_t* props));
extern C_ROCKSDB_LIBRARY_API void
crocksdb_table_properties_collector_destroy(crocksdb_table_properties_collector_t*);
/* Table Properties Collector Factory */
extern C_ROCKSDB_LIBRARY_API crocksdb_table_properties_collector_factory_t*
crocksdb_table_properties_collector_factory_create(
void* state,
const char* (*name)(void*),
void (*destruct)(void*),
crocksdb_table_properties_collector_t*
(*create_table_properties_collector)(void*, uint32_t cf));
extern C_ROCKSDB_LIBRARY_API void
crocksdb_table_properties_collector_factory_destroy(
crocksdb_table_properties_collector_factory_t*);
extern C_ROCKSDB_LIBRARY_API void
crocksdb_options_add_table_properties_collector_factory(
crocksdb_options_t* opt, crocksdb_table_properties_collector_factory_t* f);
/* Get Table Properties */
extern C_ROCKSDB_LIBRARY_API void extern C_ROCKSDB_LIBRARY_API void
crocksdb_get_propeties_of_all_tables(crocksdb_t* db, crocksdb_get_propeties_of_all_tables(crocksdb_t* db,
crocksdb_table_properties_collection_t* props, char** errptr); crocksdb_table_properties_collection_t* props, char** errptr);
......
This diff is collapsed.
...@@ -34,6 +34,12 @@ pub struct CFHandle { ...@@ -34,6 +34,12 @@ pub struct CFHandle {
inner: *mut DBCFHandle, inner: *mut DBCFHandle,
} }
impl CFHandle {
fn get_id(&self) -> u32 {
unsafe { crocksdb_ffi::crocksdb_column_family_handle_get_id(self.inner) }
}
}
impl Drop for CFHandle { impl Drop for CFHandle {
fn drop(&mut self) { fn drop(&mut self) {
unsafe { unsafe {
......
...@@ -27,7 +27,7 @@ use slice_transform::{SliceTransform, new_slice_transform}; ...@@ -27,7 +27,7 @@ use slice_transform::{SliceTransform, new_slice_transform};
use std::ffi::{CStr, CString}; use std::ffi::{CStr, CString};
use std::mem; use std::mem;
use table_properties_collector_factory::{TablePropertiesCollectorFactory, use table_properties_collector_factory::{TablePropertiesCollectorFactory,
new_table_properties_collector_factory_context}; new_table_properties_collector_factory};
#[derive(Default, Debug)] #[derive(Default, Debug)]
pub struct HistogramData { pub struct HistogramData {
...@@ -384,8 +384,7 @@ impl Options { ...@@ -384,8 +384,7 @@ impl Options {
pub fn add_table_properties_collector_factory(&mut self, pub fn add_table_properties_collector_factory(&mut self,
factory: Box<TablePropertiesCollectorFactory>) { factory: Box<TablePropertiesCollectorFactory>) {
unsafe { unsafe {
let context = Box::into_raw(new_table_properties_collector_factory_context(factory)); let f = new_table_properties_collector_factory(factory);
let f = crocksdb_ffi::crocksdb_table_properties_collector_factory_create(context);
crocksdb_ffi::crocksdb_options_add_table_properties_collector_factory(self.inner, f); crocksdb_ffi::crocksdb_options_add_table_properties_collector_factory(self.inner, f);
} }
} }
......
...@@ -11,7 +11,10 @@ ...@@ -11,7 +11,10 @@
// See the License for the specific language governing permissions and // See the License for the specific language governing permissions and
// limitations under the License. // limitations under the License.
use crocksdb_ffi::{self, DBTablePropertiesCollection, ptr_to_string}; use crocksdb_ffi::{self, DBTableProperties, DBTableProperty, DBUserCollectedProperties,
DBUserCollectedPropertiesIterator, DBTablePropertiesCollection,
DBTablePropertiesCollectionIterator};
use libc::size_t;
use std::collections::HashMap; use std::collections::HashMap;
use std::slice; use std::slice;
...@@ -35,11 +38,39 @@ pub struct TableProperties { ...@@ -35,11 +38,39 @@ pub struct TableProperties {
pub property_collectors_names: String, pub property_collectors_names: String,
pub compression_name: String, pub compression_name: String,
pub user_collected_properties: HashMap<Vec<u8>, Vec<u8>>, pub user_collected_properties: HashMap<Vec<u8>, Vec<u8>>,
pub readable_properties: HashMap<String, String>,
} }
pub type TablePropertiesCollection = HashMap<String, TableProperties>; pub type TablePropertiesCollection = HashMap<String, TableProperties>;
pub struct TablePropertiesHandle {
pub inner: *mut DBTableProperties,
}
impl TablePropertiesHandle {
fn new(inner: *mut DBTableProperties) -> TablePropertiesHandle {
TablePropertiesHandle { inner: inner }
}
fn get_u64(&self, prop: DBTableProperty) -> u64 {
unsafe { crocksdb_ffi::crocksdb_table_properties_get_u64(self.inner, prop) }
}
fn get_str(&self, prop: DBTableProperty) -> Result<String, String> {
unsafe {
let mut slen: size_t = 0;
let s = crocksdb_ffi::crocksdb_table_properties_get_str(self.inner,
prop,
&mut slen as *mut size_t);
let bytes = slice::from_raw_parts(s, slen);
String::from_utf8(bytes.to_owned()).or_else(|e| Err(format!("{}", e)))
}
}
fn get_user_properties(&self) -> *mut DBUserCollectedProperties {
unsafe { crocksdb_ffi::crocksdb_table_properties_get_user_properties(self.inner) }
}
}
pub struct TablePropertiesCollectionHandle { pub struct TablePropertiesCollectionHandle {
pub inner: *mut DBTablePropertiesCollection, pub inner: *mut DBTablePropertiesCollection,
} }
...@@ -63,37 +94,142 @@ impl TablePropertiesCollectionHandle { ...@@ -63,37 +94,142 @@ impl TablePropertiesCollectionHandle {
pub fn normalize(&self) -> Result<TablePropertiesCollection, String> { pub fn normalize(&self) -> Result<TablePropertiesCollection, String> {
let mut collection = TablePropertiesCollection::new(); let mut collection = TablePropertiesCollection::new();
unsafe { let mut it = TablePropertiesCollectionIter::new(self.inner);
let ctx = &*(self.inner as *mut DBTablePropertiesCollection); while it.valid() {
let keys = slice::from_raw_parts(ctx.keys, ctx.size); let k = try!(it.key());
let values = slice::from_raw_parts(ctx.values, ctx.size); let v = TablePropertiesHandle::new(it.value());
for i in 0..ctx.size { let mut props = TableProperties {
let props = &values[i]; data_size: v.get_u64(DBTableProperty::DataSize),
let k = ptr_to_string(keys[i])?; index_size: v.get_u64(DBTableProperty::IndexSize),
let v = TableProperties { filter_size: v.get_u64(DBTableProperty::FilterSize),
data_size: props.data_size, raw_key_size: v.get_u64(DBTableProperty::RawKeySize),
index_size: props.index_size, raw_value_size: v.get_u64(DBTableProperty::RawValueSize),
filter_size: props.filter_size, num_data_blocks: v.get_u64(DBTableProperty::NumDataBlocks),
raw_key_size: props.raw_key_size, num_entries: v.get_u64(DBTableProperty::NumEntries),
raw_value_size: props.raw_value_size, format_version: v.get_u64(DBTableProperty::FormatVersion),
num_data_blocks: props.num_data_blocks, fixed_key_len: v.get_u64(DBTableProperty::FixedKeyLen),
num_entries: props.num_entries, column_family_id: v.get_u64(DBTableProperty::ColumnFamilyId),
format_version: props.format_version, column_family_name: try!(v.get_str(DBTableProperty::ColumnFamilyName)),
fixed_key_len: props.fixed_key_len, filter_policy_name: try!(v.get_str(DBTableProperty::FilterPolicyName)),
column_family_id: props.column_family_id, comparator_name: try!(v.get_str(DBTableProperty::ComparatorName)),
column_family_name: try!(ptr_to_string(props.column_family_name)), merge_operator_name: try!(v.get_str(DBTableProperty::MergeOperatorName)),
filter_policy_name: try!(ptr_to_string(props.filter_policy_name)), prefix_extractor_name: try!(v.get_str(DBTableProperty::PrefixExtractorName)),
comparator_name: try!(ptr_to_string(props.comparator_name)), property_collectors_names:
merge_operator_name: try!(ptr_to_string(props.merge_operator_name)), try!(v.get_str(DBTableProperty::PropertyCollectorsNames)),
prefix_extractor_name: try!(ptr_to_string(props.prefix_extractor_name)), compression_name: try!(v.get_str(DBTableProperty::CompressionName)),
property_collectors_names: try!(ptr_to_string(props.property_collectors_names)), user_collected_properties: HashMap::new(),
compression_name: try!(ptr_to_string(props.compression_name)), };
user_collected_properties: try!(props.user_collected_properties.to_bytes_map()), let mut user_it = UserCollectedPropertiesIter::new(v.get_user_properties());
readable_properties: try!(props.readable_properties.to_strings_map()), while user_it.valid() {
}; {
collection.insert(k, v); let k = user_it.key();
let v = user_it.value();
props.user_collected_properties.insert(k.to_owned(), v.to_owned());
}
user_it.next();
} }
collection.insert(k, props);
it.next();
} }
Ok(collection) Ok(collection)
} }
} }
struct TablePropertiesCollectionIter {
pub inner: *mut DBTablePropertiesCollectionIterator,
}
impl Drop for TablePropertiesCollectionIter {
fn drop(&mut self) {
unsafe {
crocksdb_ffi::crocksdb_table_properties_collection_iter_destroy(self.inner);
}
}
}
impl TablePropertiesCollectionIter {
fn new(inner: *mut DBTablePropertiesCollection) -> TablePropertiesCollectionIter {
unsafe {
TablePropertiesCollectionIter {
inner: crocksdb_ffi::crocksdb_table_properties_collection_iter_create(inner),
}
}
}
fn valid(&self) -> bool {
unsafe { crocksdb_ffi::crocksdb_table_properties_collection_iter_valid(self.inner) }
}
fn next(&mut self) {
unsafe {
crocksdb_ffi::crocksdb_table_properties_collection_iter_next(self.inner);
}
}
fn key(&self) -> Result<String, String> {
unsafe {
let mut klen: size_t = 0;
let k =
crocksdb_ffi::crocksdb_table_properties_collection_iter_key(self.inner,
&mut klen as *mut size_t);
let bytes = slice::from_raw_parts(k, klen);
String::from_utf8(bytes.to_owned()).or_else(|e| Err(format!("{}", e)))
}
}
fn value(&self) -> *mut DBTableProperties {
unsafe { crocksdb_ffi::crocksdb_table_properties_collection_iter_value(self.inner) }
}
}
struct UserCollectedPropertiesIter {
pub inner: *mut DBUserCollectedPropertiesIterator,
}
impl Drop for UserCollectedPropertiesIter {
fn drop(&mut self) {
unsafe {
crocksdb_ffi::crocksdb_user_collected_properties_iter_destroy(self.inner);
}
}
}
impl UserCollectedPropertiesIter {
fn new(inner: *mut DBUserCollectedProperties) -> UserCollectedPropertiesIter {
unsafe {
UserCollectedPropertiesIter {
inner: crocksdb_ffi::crocksdb_user_collected_properties_iter_create(inner),
}
}
}
fn valid(&self) -> bool {
unsafe { crocksdb_ffi::crocksdb_user_collected_properties_iter_valid(self.inner) }
}
fn next(&mut self) {
unsafe {
crocksdb_ffi::crocksdb_user_collected_properties_iter_next(self.inner);
}
}
fn key(&self) -> &[u8] {
unsafe {
let mut klen: size_t = 0;
let k =
crocksdb_ffi::crocksdb_user_collected_properties_iter_key(self.inner,
&mut klen as *mut size_t);
slice::from_raw_parts(k, klen)
}
}
fn value(&self) -> &[u8] {
unsafe {
let mut vlen: size_t = 0;
let v =
crocksdb_ffi::crocksdb_user_collected_properties_iter_value(self.inner,
&mut vlen as *mut size_t);
slice::from_raw_parts(v, vlen)
}
}
}
...@@ -11,7 +11,7 @@ ...@@ -11,7 +11,7 @@
// See the License for the specific language governing permissions and // See the License for the specific language governing permissions and
// limitations under the License. // limitations under the License.
use crocksdb_ffi::{self, DBEntryType, DBTablePropertiesCollectorContext}; use crocksdb_ffi::{self, DBEntryType, DBUserCollectedProperties, DBTablePropertiesCollector};
use libc::{c_void, c_char, c_int, uint8_t, uint64_t, size_t}; use libc::{c_void, c_char, c_int, uint8_t, uint64_t, size_t};
use std::collections::HashMap; use std::collections::HashMap;
use std::mem; use std::mem;
...@@ -33,28 +33,23 @@ pub trait TablePropertiesCollector { ...@@ -33,28 +33,23 @@ pub trait TablePropertiesCollector {
/// Will be called when a table has already been built and is ready for /// Will be called when a table has already been built and is ready for
/// writing the properties block. /// writing the properties block.
fn finish(&mut self) -> HashMap<Vec<u8>, Vec<u8>>; fn finish(&mut self) -> HashMap<Vec<u8>, Vec<u8>>;
/// Return the human-readable properties, where the key is property name and
/// the value is the human-readable form of value.
fn readable_properties(&self) -> HashMap<String, String>;
} }
extern "C" fn name(context: *mut c_void) -> *const c_char { extern "C" fn name(collector: *mut c_void) -> *const c_char {
unsafe { unsafe {
let context = &mut *(context as *mut DBTablePropertiesCollectorContext); let collector = &mut *(collector as *mut Box<TablePropertiesCollector>);
let collector = &mut *(context.collector as *mut Box<TablePropertiesCollector>);
collector.name().as_ptr() as *const c_char collector.name().as_ptr() as *const c_char
} }
} }
extern "C" fn destructor(context: *mut c_void) { extern "C" fn destruct(collector: *mut c_void) {
unsafe { unsafe {
let context = Box::from_raw(context as *mut DBTablePropertiesCollectorContext); let collector = &mut *(collector as *mut Box<TablePropertiesCollector>);
Box::from_raw(context.collector as *mut Box<TablePropertiesCollector>); Box::from_raw(collector);
} }
} }
pub extern "C" fn add_userkey(context: *mut c_void, pub extern "C" fn add_userkey(collector: *mut c_void,
key: *const uint8_t, key: *const uint8_t,
key_len: size_t, key_len: size_t,
value: *const uint8_t, value: *const uint8_t,
...@@ -63,18 +58,16 @@ pub extern "C" fn add_userkey(context: *mut c_void, ...@@ -63,18 +58,16 @@ pub extern "C" fn add_userkey(context: *mut c_void,
_: uint64_t, _: uint64_t,
_: uint64_t) { _: uint64_t) {
unsafe { unsafe {
let context = &mut *(context as *mut DBTablePropertiesCollectorContext); let collector = &mut *(collector as *mut Box<TablePropertiesCollector>);
let collector = &mut *(context.collector as *mut Box<TablePropertiesCollector>);
let key = slice::from_raw_parts(key, key_len); let key = slice::from_raw_parts(key, key_len);
let value = slice::from_raw_parts(value, value_len); let value = slice::from_raw_parts(value, value_len);
collector.add_userkey(key, value, mem::transmute(entry_type)) collector.add_userkey(key, value, mem::transmute(entry_type));
} }
} }
pub extern "C" fn finish(context: *mut c_void, props: *mut c_void) { pub extern "C" fn finish(collector: *mut c_void, props: *mut DBUserCollectedProperties) {
unsafe { unsafe {
let context = &mut *(context as *mut DBTablePropertiesCollectorContext); let collector = &mut *(collector as *mut Box<TablePropertiesCollector>);
let collector = &mut *(context.collector as *mut Box<TablePropertiesCollector>);
for (key, value) in collector.finish() { for (key, value) in collector.finish() {
crocksdb_ffi::crocksdb_user_collected_properties_add(props, crocksdb_ffi::crocksdb_user_collected_properties_add(props,
key.as_ptr(), key.as_ptr(),
...@@ -85,28 +78,13 @@ pub extern "C" fn finish(context: *mut c_void, props: *mut c_void) { ...@@ -85,28 +78,13 @@ pub extern "C" fn finish(context: *mut c_void, props: *mut c_void) {
} }
} }
pub extern "C" fn readable_properties(context: *mut c_void, props: *mut c_void) { pub unsafe fn new_table_properties_collector(collector: Box<TablePropertiesCollector>)
unsafe { -> *mut DBTablePropertiesCollector {
let context = &mut *(context as *mut DBTablePropertiesCollectorContext); crocksdb_ffi::crocksdb_table_properties_collector_create(
let collector = &mut *(context.collector as *mut Box<TablePropertiesCollector>); Box::into_raw(Box::new(collector)) as *mut c_void,
for (key, value) in collector.readable_properties() { name,
crocksdb_ffi::crocksdb_user_collected_properties_add(props, destruct,
key.as_ptr(), add_userkey,
key.len(), finish,
value.as_ptr(), )
value.len());
}
}
}
pub unsafe fn new_table_properties_collector_context(collector: Box<TablePropertiesCollector>)
-> Box<DBTablePropertiesCollectorContext> {
Box::new(DBTablePropertiesCollectorContext {
collector: Box::into_raw(Box::new(collector)) as *mut c_void,
name: name,
destructor: destructor,
add_userkey: add_userkey,
finish: finish,
readable_properties: readable_properties,
})
} }
...@@ -11,9 +11,9 @@ ...@@ -11,9 +11,9 @@
// See the License for the specific language governing permissions and // See the License for the specific language governing permissions and
// limitations under the License. // limitations under the License.
use crocksdb_ffi::DBTablePropertiesCollectorFactoryContext; use crocksdb_ffi::{self, DBTablePropertiesCollector, DBTablePropertiesCollectorFactory};
use libc::{c_void, c_char, uint32_t}; use libc::{c_void, c_char, uint32_t};
use table_properties_collector::{TablePropertiesCollector, new_table_properties_collector_context}; use table_properties_collector::{TablePropertiesCollector, new_table_properties_collector};
/// Constructs `TablePropertiesCollector`. /// Constructs `TablePropertiesCollector`.
/// Internals create a new `TablePropertiesCollector` for each new table. /// Internals create a new `TablePropertiesCollector` for each new table.
...@@ -24,37 +24,36 @@ pub trait TablePropertiesCollectorFactory { ...@@ -24,37 +24,36 @@ pub trait TablePropertiesCollectorFactory {
fn create_table_properties_collector(&mut self, cf: u32) -> Box<TablePropertiesCollector>; fn create_table_properties_collector(&mut self, cf: u32) -> Box<TablePropertiesCollector>;
} }
extern "C" fn name(context: *mut c_void) -> *const c_char { extern "C" fn name(factory: *mut c_void) -> *const c_char {
unsafe { unsafe {
let context = &mut *(context as *mut DBTablePropertiesCollectorFactoryContext); let factory = &mut *(factory as *mut Box<TablePropertiesCollectorFactory>);
let factory = &mut *(context.factory as *mut Box<TablePropertiesCollectorFactory>);
factory.name().as_ptr() as *const c_char factory.name().as_ptr() as *const c_char
} }
} }
extern "C" fn destructor(context: *mut c_void) { extern "C" fn destruct(factory: *mut c_void) {
unsafe { unsafe {
let context = Box::from_raw(context as *mut DBTablePropertiesCollectorFactoryContext); Box::from_raw(factory as *mut Box<TablePropertiesCollectorFactory>);
Box::from_raw(context.factory as *mut Box<TablePropertiesCollectorFactory>);
} }
} }
extern "C" fn create_table_properties_collector(context: *mut c_void, cf: uint32_t) -> *mut c_void { extern "C" fn create_table_properties_collector(factory: *mut c_void,
cf: uint32_t)
-> *mut DBTablePropertiesCollector {
unsafe { unsafe {
let context = &mut *(context as *mut DBTablePropertiesCollectorFactoryContext); let factory = &mut *(factory as *mut Box<TablePropertiesCollectorFactory>);
let factory = &mut *(context.factory as *mut Box<TablePropertiesCollectorFactory>);
let collector = factory.create_table_properties_collector(cf); let collector = factory.create_table_properties_collector(cf);
Box::into_raw(new_table_properties_collector_context(collector)) as *mut c_void new_table_properties_collector(collector)
} }
} }
pub unsafe fn new_table_properties_collector_factory_context pub unsafe fn new_table_properties_collector_factory
(factory: Box<TablePropertiesCollectorFactory>) (factory: Box<TablePropertiesCollectorFactory>)
-> Box<DBTablePropertiesCollectorFactoryContext> { -> *mut DBTablePropertiesCollectorFactory {
Box::new(DBTablePropertiesCollectorFactoryContext { crocksdb_ffi::crocksdb_table_properties_collector_factory_create(
factory: Box::into_raw(Box::new(factory)) as *mut c_void, Box::into_raw(Box::new(factory)) as *mut c_void,
name: name, name,
destructor: destructor, destruct,
create_table_properties_collector: create_table_properties_collector, create_table_properties_collector,
}) )
} }
...@@ -106,26 +106,17 @@ impl TablePropertiesCollector for ExampleCollector { ...@@ -106,26 +106,17 @@ impl TablePropertiesCollector for ExampleCollector {
self.last_key.extend_from_slice(key); self.last_key.extend_from_slice(key);
} }
match entry_type { match entry_type {
DBEntryType::DBPut => self.num_puts += 1, DBEntryType::Put => self.num_puts += 1,
DBEntryType::DBMerge => self.num_merges += 1, DBEntryType::Merge => self.num_merges += 1,
DBEntryType::DBDelete | DBEntryType::Delete |
DBEntryType::DBSingleDelete => self.num_deletes += 1, DBEntryType::SingleDelete => self.num_deletes += 1,
DBEntryType::DBOther => {} DBEntryType::Other => {}
} }
} }
fn finish(&mut self) -> HashMap<Vec<u8>, Vec<u8>> { fn finish(&mut self) -> HashMap<Vec<u8>, Vec<u8>> {
self.encode() self.encode()
} }
fn readable_properties(&self) -> HashMap<String, String> {
let mut props = HashMap::new();
props.insert("num_keys".to_owned(), self.num_keys.to_string());
props.insert("num_puts".to_owned(), self.num_puts.to_string());
props.insert("num_merges".to_owned(), self.num_merges.to_string());
props.insert("num_deletes".to_owned(), self.num_deletes.to_string());
props
}
} }
struct ExampleFactory {} struct ExampleFactory {}
......
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