Commit d7acfb70 authored by Huachao Huang's avatar Huachao Huang

fetch properties lazily

parent 0fe7d360
...@@ -35,7 +35,7 @@ pub struct CFHandle { ...@@ -35,7 +35,7 @@ pub struct CFHandle {
} }
impl CFHandle { impl CFHandle {
pub fn get_id(&self) -> u32 { pub fn id(&self) -> u32 {
unsafe { crocksdb_ffi::crocksdb_column_family_handle_get_id(self.inner) } unsafe { crocksdb_ffi::crocksdb_column_family_handle_get_id(self.inner) }
} }
} }
...@@ -1047,9 +1047,9 @@ impl DB { ...@@ -1047,9 +1047,9 @@ impl DB {
pub fn get_properties_of_all_tables(&self) -> Result<TablePropertiesCollection, String> { pub fn get_properties_of_all_tables(&self) -> Result<TablePropertiesCollection, String> {
unsafe { unsafe {
let props = TablePropertiesCollectionHandle::new(); let handle = TablePropertiesCollectionHandle::new();
ffi_try!(crocksdb_get_properties_of_all_tables(self.inner, props.inner)); ffi_try!(crocksdb_get_properties_of_all_tables(self.inner, handle.inner));
props.normalize() Ok(TablePropertiesCollection::new(handle))
} }
} }
...@@ -1057,9 +1057,9 @@ impl DB { ...@@ -1057,9 +1057,9 @@ impl DB {
cf: &CFHandle) cf: &CFHandle)
-> Result<TablePropertiesCollection, String> { -> Result<TablePropertiesCollection, String> {
unsafe { unsafe {
let props = TablePropertiesCollectionHandle::new(); let handle = TablePropertiesCollectionHandle::new();
ffi_try!(crocksdb_get_properties_of_all_tables_cf(self.inner, cf.inner, props.inner)); ffi_try!(crocksdb_get_properties_of_all_tables_cf(self.inner, cf.inner, handle.inner));
props.normalize() Ok(TablePropertiesCollection::new(handle))
} }
} }
...@@ -1072,7 +1072,7 @@ impl DB { ...@@ -1072,7 +1072,7 @@ impl DB {
let limit_keys: Vec<*const u8> = ranges.iter().map(|x| x.end_key.as_ptr()).collect(); let limit_keys: Vec<*const u8> = ranges.iter().map(|x| x.end_key.as_ptr()).collect();
let limit_keys_lens: Vec<_> = ranges.iter().map(|x| x.end_key.len()).collect(); let limit_keys_lens: Vec<_> = ranges.iter().map(|x| x.end_key.len()).collect();
unsafe { unsafe {
let props = TablePropertiesCollectionHandle::new(); let handle = TablePropertiesCollectionHandle::new();
ffi_try!(crocksdb_get_properties_of_tables_in_range(self.inner, ffi_try!(crocksdb_get_properties_of_tables_in_range(self.inner,
cf.inner, cf.inner,
ranges.len() as i32, ranges.len() as i32,
...@@ -1080,8 +1080,8 @@ impl DB { ...@@ -1080,8 +1080,8 @@ impl DB {
start_keys_lens.as_ptr(), start_keys_lens.as_ptr(),
limit_keys.as_ptr(), limit_keys.as_ptr(),
limit_keys_lens.as_ptr(), limit_keys_lens.as_ptr(),
props.inner)); handle.inner));
props.normalize() Ok(TablePropertiesCollection::new(handle))
} }
} }
} }
......
This diff is collapsed.
...@@ -16,6 +16,7 @@ use rocksdb::{DB, Range, Options, Writable, DBEntryType, TablePropertiesCollecti ...@@ -16,6 +16,7 @@ use rocksdb::{DB, Range, Options, Writable, DBEntryType, TablePropertiesCollecti
TablePropertiesCollector, TablePropertiesCollectorFactory}; TablePropertiesCollector, TablePropertiesCollectorFactory};
use std::cmp::Ordering; use std::cmp::Ordering;
use std::collections::HashMap; use std::collections::HashMap;
use std::ffi::CString;
use std::fmt; use std::fmt;
use std::io::Cursor; use std::io::Cursor;
use tempdir::TempDir; use tempdir::TempDir;
...@@ -39,6 +40,7 @@ fn decode_u32(x: &[u8]) -> u32 { ...@@ -39,6 +40,7 @@ fn decode_u32(x: &[u8]) -> u32 {
} }
struct ExampleCollector { struct ExampleCollector {
name: CString,
num_keys: u32, num_keys: u32,
num_puts: u32, num_puts: u32,
num_merges: u32, num_merges: u32,
...@@ -49,6 +51,7 @@ struct ExampleCollector { ...@@ -49,6 +51,7 @@ struct ExampleCollector {
impl ExampleCollector { impl ExampleCollector {
fn new() -> ExampleCollector { fn new() -> ExampleCollector {
ExampleCollector { ExampleCollector {
name: CString::new("example-collector").unwrap(),
num_keys: 0, num_keys: 0,
num_puts: 0, num_puts: 0,
num_merges: 0, num_merges: 0,
...@@ -95,11 +98,11 @@ impl fmt::Display for ExampleCollector { ...@@ -95,11 +98,11 @@ impl fmt::Display for ExampleCollector {
} }
impl TablePropertiesCollector for ExampleCollector { impl TablePropertiesCollector for ExampleCollector {
fn name(&self) -> &str { fn name(&self) -> &CString {
"example-collector" &self.name
} }
fn add_userkey(&mut self, key: &[u8], _: &[u8], entry_type: DBEntryType) { fn add_userkey(&mut self, key: &[u8], _: &[u8], entry_type: DBEntryType, _: u64, _: u64) {
if key.cmp(&self.last_key) != Ordering::Equal { if key.cmp(&self.last_key) != Ordering::Equal {
self.num_keys += 1; self.num_keys += 1;
self.last_key.clear(); self.last_key.clear();
...@@ -119,17 +122,19 @@ impl TablePropertiesCollector for ExampleCollector { ...@@ -119,17 +122,19 @@ impl TablePropertiesCollector for ExampleCollector {
} }
} }
struct ExampleFactory {} struct ExampleFactory {
name: CString,
}
impl ExampleFactory { impl ExampleFactory {
fn new() -> ExampleFactory { fn new() -> ExampleFactory {
ExampleFactory {} ExampleFactory { name: CString::new("example-factory").unwrap() }
} }
} }
impl TablePropertiesCollectorFactory for ExampleFactory { impl TablePropertiesCollectorFactory for ExampleFactory {
fn name(&self) -> &str { fn name(&self) -> &CString {
"example-factory" &self.name
} }
fn create_table_properties_collector(&mut self, _: u32) -> Box<TablePropertiesCollector> { fn create_table_properties_collector(&mut self, _: u32) -> Box<TablePropertiesCollector> {
...@@ -143,12 +148,19 @@ fn check_collection(collection: &TablePropertiesCollection, ...@@ -143,12 +148,19 @@ fn check_collection(collection: &TablePropertiesCollection,
num_puts: u32, num_puts: u32,
num_merges: u32, num_merges: u32,
num_deletes: u32) { num_deletes: u32) {
let mut len = 0;
let mut res = ExampleCollector::new(); let mut res = ExampleCollector::new();
for (_, props) in collection { let mut iter = collection.iter();
assert_eq!(props.property_collectors_names, "[example-factory]"); while iter.valid() {
res.add(&ExampleCollector::decode(&props.user_collected_properties)); len += 1;
{
let v = iter.value();
assert_eq!(v.property_collectors_names(), "[example-factory]");
res.add(&ExampleCollector::decode(&v.user_collected_properties()));
}
iter.next();
} }
assert_eq!(collection.len() as u32, num_files); assert_eq!(len, num_files);
assert_eq!(res.num_keys, num_keys); assert_eq!(res.num_keys, num_keys);
assert_eq!(res.num_puts, num_puts); assert_eq!(res.num_puts, num_puts);
assert_eq!(res.num_merges, num_merges); assert_eq!(res.num_merges, num_merges);
......
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