Commit a50c0841 authored by Jay Lee's avatar Jay Lee Committed by Jay

user properties: clean up API (#90)

- reduce allocation
- collect lazily
- strict access mode
parent 44b2521a
......@@ -18,7 +18,7 @@ before_script:
- make -f travis-build/Makefile prepare-rustfmt
script:
- cargo fmt && git diff-index --quiet HEAD -- || (echo please make format and run tests before creating a pr!; exit 1)
- cargo fmt -- --write-mode diff || (echo please make format and run tests before creating a pr!; exit 1)
- cargo build --features static-link
- cargo test --all --features static-link
......
This diff is collapsed.
......@@ -1226,22 +1226,23 @@ extern C_ROCKSDB_LIBRARY_API void crocksdb_get_supported_compression(int *, size
/* Table Properties */
extern C_ROCKSDB_LIBRARY_API crocksdb_table_properties_t*
crocksdb_table_properties_create();
extern C_ROCKSDB_LIBRARY_API uint64_t crocksdb_table_properties_get_u64(
const crocksdb_table_properties_t*, crocksdb_table_property_t prop);
extern C_ROCKSDB_LIBRARY_API void
crocksdb_table_properties_destroy(crocksdb_table_properties_t*);
extern C_ROCKSDB_LIBRARY_API const char* crocksdb_table_properties_get_str(
const crocksdb_table_properties_t*, crocksdb_table_property_t prop,
size_t* slen);
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 crocksdb_user_collected_properties_t*
crocksdb_table_properties_get_user_properties(
const crocksdb_table_properties_t*);
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 const char* crocksdb_user_collected_properties_get(
const crocksdb_user_collected_properties_t* props, const char* key,
size_t klen, size_t* vlen);
extern C_ROCKSDB_LIBRARY_API crocksdb_user_collected_properties_t*
crocksdb_table_properties_get_user_properties(crocksdb_table_properties_t*);
extern C_ROCKSDB_LIBRARY_API size_t crocksdb_user_collected_properties_len(
const crocksdb_user_collected_properties_t*);
extern C_ROCKSDB_LIBRARY_API void
crocksdb_user_collected_properties_add(
......@@ -1250,15 +1251,15 @@ crocksdb_user_collected_properties_add(
extern C_ROCKSDB_LIBRARY_API crocksdb_user_collected_properties_iterator_t*
crocksdb_user_collected_properties_iter_create(
crocksdb_user_collected_properties_t*);
const crocksdb_user_collected_properties_t*);
extern C_ROCKSDB_LIBRARY_API void
crocksdb_user_collected_properties_iter_destroy(
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*);
crocksdb_user_collected_properties_iter_valid(
const crocksdb_user_collected_properties_iterator_t*);
extern C_ROCKSDB_LIBRARY_API void
crocksdb_user_collected_properties_iter_next(
......@@ -1266,23 +1267,23 @@ crocksdb_user_collected_properties_iter_next(
extern C_ROCKSDB_LIBRARY_API const char*
crocksdb_user_collected_properties_iter_key(
crocksdb_user_collected_properties_iterator_t*, size_t* klen);
const 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);
const crocksdb_user_collected_properties_iterator_t*, size_t* vlen);
/* Table Properties Collection */
extern C_ROCKSDB_LIBRARY_API crocksdb_table_properties_collection_t*
crocksdb_table_properties_collection_create();
extern C_ROCKSDB_LIBRARY_API size_t crocksdb_table_properties_collection_len(
const crocksdb_table_properties_collection_t*);
extern C_ROCKSDB_LIBRARY_API void
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*);
const crocksdb_table_properties_collection_t*);
extern C_ROCKSDB_LIBRARY_API void
crocksdb_table_properties_collection_iter_destroy(
......@@ -1290,7 +1291,7 @@ crocksdb_table_properties_collection_iter_destroy(
extern C_ROCKSDB_LIBRARY_API unsigned char
crocksdb_table_properties_collection_iter_valid(
crocksdb_table_properties_collection_iterator_t*);
const crocksdb_table_properties_collection_iterator_t*);
extern C_ROCKSDB_LIBRARY_API void
crocksdb_table_properties_collection_iter_next(
......@@ -1298,11 +1299,11 @@ crocksdb_table_properties_collection_iter_next(
extern C_ROCKSDB_LIBRARY_API const char*
crocksdb_table_properties_collection_iter_key(
crocksdb_table_properties_collection_iterator_t*, size_t* klen);
const crocksdb_table_properties_collection_iterator_t*, size_t* klen);
extern C_ROCKSDB_LIBRARY_API void
extern C_ROCKSDB_LIBRARY_API const crocksdb_table_properties_t*
crocksdb_table_properties_collection_iter_value(
crocksdb_table_properties_collection_iterator_t*, crocksdb_table_properties_t* props);
const crocksdb_table_properties_collection_iterator_t*);
/* Table Properties Collector */
......@@ -1340,22 +1341,20 @@ crocksdb_options_add_table_properties_collector_factory(
/* Get Table Properties */
extern C_ROCKSDB_LIBRARY_API void
crocksdb_get_propeties_of_all_tables(crocksdb_t* db,
crocksdb_table_properties_collection_t* props, char** errptr);
extern C_ROCKSDB_LIBRARY_API crocksdb_table_properties_collection_t*
crocksdb_get_propeties_of_all_tables(crocksdb_t* db, char** errptr);
extern C_ROCKSDB_LIBRARY_API void
crocksdb_get_propeties_of_all_tables_cf(
crocksdb_t* db, crocksdb_column_family_handle_t* cf,
crocksdb_table_properties_collection_t* props, char** errptr);
extern C_ROCKSDB_LIBRARY_API crocksdb_table_properties_collection_t*
crocksdb_get_propeties_of_all_tables_cf(crocksdb_t* db,
crocksdb_column_family_handle_t* cf,
char** errptr);
extern C_ROCKSDB_LIBRARY_API void
extern C_ROCKSDB_LIBRARY_API crocksdb_table_properties_collection_t*
crocksdb_get_propeties_of_tables_in_range(
crocksdb_t* db, crocksdb_column_family_handle_t* cf,
int num_ranges,
crocksdb_t* db, crocksdb_column_family_handle_t* cf, int num_ranges,
const char* const* start_keys, const size_t* start_keys_lens,
const char* const* limit_keys, const size_t* limit_keys_lens,
crocksdb_table_properties_collection_t* props, char** errptr);
char** errptr);
#ifdef __cplusplus
} /* end extern "C" */
......
......@@ -866,63 +866,67 @@ extern "C" {
value_len: size_t);
pub fn crocksdb_user_collected_properties_iter_create
(props: *mut DBUserCollectedProperties)
(props: *const DBUserCollectedProperties)
-> *mut DBUserCollectedPropertiesIterator;
pub fn crocksdb_user_collected_properties_iter_destroy
(it: *mut DBUserCollectedPropertiesIterator);
pub fn crocksdb_user_collected_properties_iter_valid
(it: *mut DBUserCollectedPropertiesIterator) -> bool;
(it: *const DBUserCollectedPropertiesIterator) -> bool;
pub fn crocksdb_user_collected_properties_iter_next
(it: *mut DBUserCollectedPropertiesIterator);
pub fn crocksdb_user_collected_properties_iter_key(it: *mut DBUserCollectedPropertiesIterator,
pub fn crocksdb_user_collected_properties_iter_key(it: *const DBUserCollectedPropertiesIterator,
klen: *mut size_t)
-> *const uint8_t;
pub fn crocksdb_user_collected_properties_iter_value
(it: *mut DBUserCollectedPropertiesIterator, vlen: *mut size_t) -> *const uint8_t;
(it: *const DBUserCollectedPropertiesIterator, vlen: *mut size_t) -> *const uint8_t;
pub fn crocksdb_table_properties_create() -> *mut DBTableProperties;
pub fn crocksdb_table_properties_destroy(props: *mut DBTableProperties);
pub fn crocksdb_table_properties_get_u64(props: *mut DBTableProperties,
pub fn crocksdb_table_properties_get_u64(props: *const DBTableProperties,
prop: DBTableProperty)
-> uint64_t;
pub fn crocksdb_table_properties_get_str(props: *mut DBTableProperties,
pub fn crocksdb_table_properties_get_str(props: *const DBTableProperties,
prop: DBTableProperty,
slen: *mut size_t)
-> *const uint8_t;
pub fn crocksdb_table_properties_get_user_properties(props: *mut DBTableProperties)
-> *mut DBUserCollectedProperties;
pub fn crocksdb_table_properties_get_user_properties(props: *const DBTableProperties)
-> *const DBUserCollectedProperties;
pub fn crocksdb_user_collected_properties_get(props: *const DBUserCollectedProperties,
key: *const uint8_t,
klen: size_t,
vlen: *mut size_t)
-> *const uint8_t;
pub fn crocksdb_user_collected_properties_len(props: *const DBUserCollectedProperties) -> size_t;
pub fn crocksdb_table_properties_collection_create() -> *mut DBTablePropertiesCollection;
pub fn crocksdb_table_properties_collection_len(props: *const DBTablePropertiesCollection) -> size_t;
pub fn crocksdb_table_properties_collection_destroy(props: *mut DBTablePropertiesCollection);
pub fn crocksdb_table_properties_collection_iter_create
(props: *mut DBTablePropertiesCollection)
(props: *const DBTablePropertiesCollection)
-> *mut DBTablePropertiesCollectionIterator;
pub fn crocksdb_table_properties_collection_iter_destroy
(it: *mut DBTablePropertiesCollectionIterator);
pub fn crocksdb_table_properties_collection_iter_valid
(it: *mut DBTablePropertiesCollectionIterator) -> bool;
(it: *const DBTablePropertiesCollectionIterator) -> bool;
pub fn crocksdb_table_properties_collection_iter_next
(it: *mut DBTablePropertiesCollectionIterator);
pub fn crocksdb_table_properties_collection_iter_key(
it: *mut DBTablePropertiesCollectionIterator, klen: *mut size_t) -> *const uint8_t;
it: *const DBTablePropertiesCollectionIterator, klen: *mut size_t) -> *const uint8_t;
pub fn crocksdb_table_properties_collection_iter_value
(it: *mut DBTablePropertiesCollectionIterator, value: *mut DBTableProperties);
(it: *const DBTablePropertiesCollectionIterator) -> *const DBTableProperties;
pub fn crocksdb_table_properties_collector_create(state: *mut c_void,
name: extern "C" fn(*mut c_void)
......@@ -958,13 +962,13 @@ extern "C" {
options: *mut DBOptions, f: *mut DBTablePropertiesCollectorFactory);
pub fn crocksdb_get_properties_of_all_tables(db: *mut DBInstance,
props: *mut DBTablePropertiesCollection,
errptr: *mut *mut c_char);
errptr: *mut *mut c_char)
-> *mut DBTablePropertiesCollection;
pub fn crocksdb_get_properties_of_all_tables_cf(db: *mut DBInstance,
cf: *mut DBCFHandle,
props: *mut DBTablePropertiesCollection,
errptr: *mut *mut c_char);
errptr: *mut *mut c_char)
-> *mut DBTablePropertiesCollection;
pub fn crocksdb_get_properties_of_tables_in_range(db: *mut DBInstance,
cf: *mut DBCFHandle,
......@@ -973,8 +977,8 @@ extern "C" {
start_keys_lens: *const size_t,
limit_keys: *const *const uint8_t,
limit_keys_lens: *const size_t,
props: *mut DBTablePropertiesCollection,
errptr: *mut *mut c_char);
errptr: *mut *mut c_char)
-> *mut DBTablePropertiesCollection;
}
......
......@@ -40,6 +40,6 @@ pub use rocksdb::{DB, DBIterator, DBVector, Kv, SeekKey, Writable, WriteBatch, C
pub use rocksdb_options::{BlockBasedOptions, Options, ReadOptions, WriteOptions, RestoreOptions,
IngestExternalFileOptions, EnvOptions, HistogramData, CompactOptions};
pub use slice_transform::SliceTransform;
pub use table_properties::{TableProperties, TablePropertiesCollection};
pub use table_properties::{TableProperties, TablePropertiesCollection, UserCollectedProperties};
pub use table_properties_collector::TablePropertiesCollector;
pub use table_properties_collector_factory::TablePropertiesCollectorFactory;
......@@ -27,7 +27,7 @@ use std::fmt::{self, Debug, Formatter};
use std::ops::Deref;
use std::path::Path;
use std::str::from_utf8;
use table_properties::{TablePropertiesCollection, new_table_properties_collection};
use table_properties::TablePropertiesCollection;
const DEFAULT_COLUMN_FAMILY: &'static str = "default";
......@@ -1048,9 +1048,8 @@ impl DB {
pub fn get_properties_of_all_tables(&self) -> Result<TablePropertiesCollection, String> {
unsafe {
let props = new_table_properties_collection();
ffi_try!(crocksdb_get_properties_of_all_tables(self.inner, props.inner));
Ok(props)
let props = ffi_try!(crocksdb_get_properties_of_all_tables(self.inner));
Ok(TablePropertiesCollection::from_raw(props))
}
}
......@@ -1058,9 +1057,8 @@ impl DB {
cf: &CFHandle)
-> Result<TablePropertiesCollection, String> {
unsafe {
let props = new_table_properties_collection();
ffi_try!(crocksdb_get_properties_of_all_tables_cf(self.inner, cf.inner, props.inner));
Ok(props)
let props = ffi_try!(crocksdb_get_properties_of_all_tables_cf(self.inner, cf.inner));
Ok(TablePropertiesCollection::from_raw(props))
}
}
......@@ -1073,16 +1071,14 @@ impl DB {
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();
unsafe {
let props = new_table_properties_collection();
ffi_try!(crocksdb_get_properties_of_tables_in_range(self.inner,
let props = ffi_try!(crocksdb_get_properties_of_tables_in_range(self.inner,
cf.inner,
ranges.len() as i32,
start_keys.as_ptr(),
start_keys_lens.as_ptr(),
limit_keys.as_ptr(),
limit_keys_lens.as_ptr(),
props.inner));
Ok(props)
limit_keys_lens.as_ptr()));
Ok(TablePropertiesCollection::from_raw(props))
}
}
}
......
......@@ -11,20 +11,18 @@
// See the License for the specific language governing permissions and
// limitations under the License.
use crocksdb_ffi::{self, DBTableProperties, DBTableProperty, DBUserCollectedPropertiesIterator,
DBTablePropertiesCollection, DBTablePropertiesCollectionIterator};
DBTablePropertiesCollection, DBTablePropertiesCollectionIterator,
DBUserCollectedProperties};
use libc::size_t;
use std::collections::HashMap;
use std::{slice, str, mem};
use std::marker::PhantomData;
use std::slice;
use std::str;
use std::ops::Index;
pub fn new_table_properties_collection() -> TablePropertiesCollection {
TablePropertiesCollection::new()
}
pub struct TablePropertiesCollection {
pub inner: *mut DBTablePropertiesCollection,
inner: *mut DBTablePropertiesCollection,
}
impl Drop for TablePropertiesCollection {
......@@ -36,22 +34,29 @@ impl Drop for TablePropertiesCollection {
}
impl TablePropertiesCollection {
fn new() -> TablePropertiesCollection {
unsafe {
TablePropertiesCollection {
inner: crocksdb_ffi::crocksdb_table_properties_collection_create(),
}
}
pub unsafe fn from_raw(ptr: *mut DBTablePropertiesCollection) -> TablePropertiesCollection {
TablePropertiesCollection { inner: ptr }
}
pub fn collect(&self) -> HashMap<&str, TableProperties> {
let mut res = HashMap::new();
let mut iter = TablePropertiesCollectionIter::new(self);
while iter.valid() {
res.insert(iter.key(), iter.value());
iter.next();
}
res
pub fn iter(&self) -> TablePropertiesCollectionIter {
self.into_iter()
}
pub fn len(&self) -> usize {
unsafe { crocksdb_ffi::crocksdb_table_properties_collection_len(self.inner) }
}
pub fn is_empty(&self) -> bool {
self.len() == 0
}
}
impl<'a> IntoIterator for &'a TablePropertiesCollection {
type Item = (&'a str, &'a TableProperties);
type IntoIter = TablePropertiesCollectionIter<'a>;
fn into_iter(self) -> Self::IntoIter {
TablePropertiesCollectionIter::new(self)
}
}
......@@ -77,61 +82,50 @@ impl<'a> TablePropertiesCollectionIter<'a> {
}
}
}
}
pub fn valid(&self) -> bool {
unsafe { crocksdb_ffi::crocksdb_table_properties_collection_iter_valid(self.inner) }
}
impl<'a> Iterator for TablePropertiesCollectionIter<'a> {
type Item = (&'a str, &'a TableProperties);
pub fn next(&mut self) {
fn next(&mut self) -> Option<(&'a str, &'a TableProperties)> {
unsafe {
crocksdb_ffi::crocksdb_table_properties_collection_iter_next(self.inner);
}
}
if !crocksdb_ffi::crocksdb_table_properties_collection_iter_valid(self.inner) {
return None;
}
pub fn key(&self) -> &'a 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);
str::from_utf8(bytes).unwrap()
}
}
pub fn value(&self) -> TableProperties {
unsafe {
let props = TableProperties::new();
crocksdb_ffi::crocksdb_table_properties_collection_iter_value(self.inner, props.inner);
props
let key = str::from_utf8(bytes).unwrap();
let props = crocksdb_ffi::crocksdb_table_properties_collection_iter_value(self.inner);
let val = TableProperties::from_ptr(props);
crocksdb_ffi::crocksdb_table_properties_collection_iter_next(self.inner);
Some((key, val))
}
}
}
pub struct TableProperties {
inner: *mut DBTableProperties,
inner: DBTableProperties,
}
impl Drop for TableProperties {
fn drop(&mut self) {
impl TableProperties {
fn from_ptr<'a>(ptr: *const DBTableProperties) -> &'a TableProperties {
unsafe {
crocksdb_ffi::crocksdb_table_properties_destroy(self.inner);
let res = &*ptr;
mem::transmute(res)
}
}
}
impl TableProperties {
fn new() -> TableProperties {
unsafe { TableProperties { inner: crocksdb_ffi::crocksdb_table_properties_create() } }
}
fn get_u64(&self, prop: DBTableProperty) -> u64 {
unsafe { crocksdb_ffi::crocksdb_table_properties_get_u64(self.inner, prop) }
unsafe { crocksdb_ffi::crocksdb_table_properties_get_u64(&self.inner, prop) }
}
fn get_str(&self, prop: DBTableProperty) -> &str {
unsafe {
let mut slen: size_t = 0;
let s = crocksdb_ffi::crocksdb_table_properties_get_str(self.inner, prop, &mut slen);
let s = crocksdb_ffi::crocksdb_table_properties_get_str(&self.inner, prop, &mut slen);
let bytes = slice::from_raw_parts(s, slen);
str::from_utf8(bytes).unwrap()
}
......@@ -205,19 +199,68 @@ impl TableProperties {
self.get_str(DBTableProperty::CompressionName)
}
pub fn user_collected_properties(&self) -> HashMap<&[u8], &[u8]> {
let mut res = HashMap::new();
let mut iter = UserCollectedPropertiesIter::new(self);
while iter.valid() {
res.insert(iter.key(), iter.value());
iter.next();
pub fn user_collected_properties(&self) -> &UserCollectedProperties {
unsafe {
let ptr = crocksdb_ffi::crocksdb_table_properties_get_user_properties(&self.inner);
UserCollectedProperties::from_ptr(ptr)
}
}
}
pub struct UserCollectedProperties {
inner: DBUserCollectedProperties,
}
impl UserCollectedProperties {
unsafe fn from_ptr<'a>(ptr: *const DBUserCollectedProperties) -> &'a UserCollectedProperties {
let prop = &*ptr;
mem::transmute(prop)
}
pub fn get<Q: AsRef<[u8]>>(&self, index: Q) -> Option<&[u8]> {
let bytes = index.as_ref();
let mut size = 0;
unsafe {
let ptr = crocksdb_ffi::crocksdb_user_collected_properties_get(&self.inner,
bytes.as_ptr(),
bytes.len(),
&mut size);
if ptr.is_null() {
return None;
}
Some(slice::from_raw_parts(ptr, size))
}
res
}
pub fn len(&self) -> usize {
unsafe { crocksdb_ffi::crocksdb_user_collected_properties_len(&self.inner) }
}
pub fn is_empty(&self) -> bool {
self.len() == 0
}
}
struct UserCollectedPropertiesIter<'a> {
props: PhantomData<&'a TableProperties>,
impl<Q: AsRef<[u8]>> Index<Q> for UserCollectedProperties {
type Output = [u8];
fn index(&self, index: Q) -> &[u8] {
let key = index.as_ref();
self.get(key).unwrap_or_else(|| panic!("no entry found for key {:?}", key))
}
}
impl<'a> IntoIterator for &'a UserCollectedProperties {
type Item = (&'a [u8], &'a [u8]);
type IntoIter = UserCollectedPropertiesIter<'a>;
fn into_iter(self) -> Self::IntoIter {
UserCollectedPropertiesIter::new(self)
}
}
pub struct UserCollectedPropertiesIter<'a> {
props: PhantomData<&'a UserCollectedProperties>,
inner: *mut DBUserCollectedPropertiesIterator,
}
......@@ -230,41 +273,37 @@ impl<'a> Drop for UserCollectedPropertiesIter<'a> {
}
impl<'a> UserCollectedPropertiesIter<'a> {
fn new(props: &'a TableProperties) -> UserCollectedPropertiesIter<'a> {
fn new(props: &'a UserCollectedProperties) -> UserCollectedPropertiesIter<'a> {
unsafe {
let inner = crocksdb_ffi::crocksdb_table_properties_get_user_properties(props.inner);
UserCollectedPropertiesIter {
props: PhantomData,
inner: crocksdb_ffi::crocksdb_user_collected_properties_iter_create(inner),
inner: crocksdb_ffi::crocksdb_user_collected_properties_iter_create(&props.inner),
}
}
}
}
fn valid(&self) -> bool {
unsafe { crocksdb_ffi::crocksdb_user_collected_properties_iter_valid(self.inner) }
}
impl<'a> Iterator for UserCollectedPropertiesIter<'a> {
type Item = (&'a [u8], &'a [u8]);
fn next(&mut self) {
unsafe {
crocksdb_ffi::crocksdb_user_collected_properties_iter_next(self.inner);
}
}
fn key(&self) -> &'a [u8] {
fn next(&mut self) -> Option<(&'a [u8], &'a [u8])> {
unsafe {
if !crocksdb_ffi::crocksdb_user_collected_properties_iter_valid(self.inner) {
return None;
}
let mut klen: size_t = 0;
let k = crocksdb_ffi::crocksdb_user_collected_properties_iter_key(self.inner,
&mut klen);
slice::from_raw_parts(k, klen)
}
}
let key = slice::from_raw_parts(k, klen);
fn value(&self) -> &'a [u8] {
unsafe {
let mut vlen: size_t = 0;
let v = crocksdb_ffi::crocksdb_user_collected_properties_iter_value(self.inner,
&mut vlen);
slice::from_raw_parts(v, vlen)
let val = slice::from_raw_parts(v, vlen);
crocksdb_ffi::crocksdb_user_collected_properties_iter_next(self.inner);
Some((key, val))
}
}
}
......@@ -13,7 +13,7 @@
use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt};
use rocksdb::{DB, Range, Options, Writable, DBEntryType, TablePropertiesCollection,
TablePropertiesCollector, TablePropertiesCollectorFactory};
TablePropertiesCollector, TablePropertiesCollectorFactory, UserCollectedProperties};
use std::collections::HashMap;
use std::fmt;
use tempdir::TempDir;
......@@ -70,12 +70,20 @@ impl ExampleCollector {
props
}
fn decode(props: HashMap<&[u8], &[u8]>) -> ExampleCollector {
fn decode(props: &UserCollectedProperties) -> ExampleCollector {
assert!(!props.is_empty());
let mut c = ExampleCollector::new();
c.num_keys = decode_u32(props.get(&[Props::NumKeys as u8].as_ref()).unwrap());
c.num_puts = decode_u32(props.get(&[Props::NumPuts as u8].as_ref()).unwrap());
c.num_merges = decode_u32(props.get(&[Props::NumMerges as u8].as_ref()).unwrap());
c.num_deletes = decode_u32(props.get(&[Props::NumDeletes as u8].as_ref()).unwrap());
c.num_keys = decode_u32(&props[&[Props::NumKeys as u8]]);
c.num_puts = decode_u32(&props[&[Props::NumPuts as u8]]);
c.num_merges = decode_u32(&props[&[Props::NumMerges as u8]]);
c.num_deletes = decode_u32(&props[&[Props::NumDeletes as u8]]);
for (k, v) in props {
assert_eq!(v, props.get(k).unwrap());
}
assert!(props.get(&[Props::NumKeys as u8, Props::NumPuts as u8]).is_none());
assert!(props.len() >= 4);
c
}
}
......@@ -133,7 +141,9 @@ fn check_collection(collection: &TablePropertiesCollection,
num_merges: u32,
num_deletes: u32) {
let mut res = ExampleCollector::new();
let props = collection.collect();
assert!(!collection.is_empty());
let props: HashMap<_, _> = collection.iter().collect();
assert_eq!(props.len(), collection.len());
for (k, v) in &props {
assert!(k.ends_with(".sst"));
assert_eq!(v.property_collectors_names(), "[example-collector]");
......
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