Commit e918d1fc authored by Huachao Huang's avatar Huachao Huang

address comment and use reference

parent ea46eb52
...@@ -662,7 +662,7 @@ void crocksdb_drop_column_family( ...@@ -662,7 +662,7 @@ void crocksdb_drop_column_family(
SaveError(errptr, db->rep->DropColumnFamily(handle->rep)); SaveError(errptr, db->rep->DropColumnFamily(handle->rep));
} }
uint32_t crocksdb_column_family_handle_get_id(crocksdb_column_family_handle_t* handle) { uint32_t crocksdb_column_family_handle_id(crocksdb_column_family_handle_t* handle) {
return handle->rep->GetID(); return handle->rep->GetID();
} }
...@@ -2934,22 +2934,24 @@ struct crocksdb_user_collected_properties_t { ...@@ -2934,22 +2934,24 @@ struct crocksdb_user_collected_properties_t {
UserCollectedProperties* rep_ = nullptr; UserCollectedProperties* rep_ = nullptr;
}; };
struct crocksdb_user_collected_properties_iterator_t { void crocksdb_user_collected_properties_add(
UserCollectedProperties* rep_ = nullptr; crocksdb_user_collected_properties_t* props,
UserCollectedProperties::iterator iter_; const char* k, size_t klen,
}; const char* v, size_t vlen) {
void crocksdb_user_collected_properties_add(crocksdb_user_collected_properties_t* props,
const char* k, size_t klen,
const char* v, size_t vlen) {
props->rep_->emplace(std::make_pair(std::string(k, klen), std::string(v, vlen))); props->rep_->emplace(std::make_pair(std::string(k, klen), std::string(v, vlen)));
} }
struct crocksdb_user_collected_properties_iterator_t {
UserCollectedProperties::iterator cur_;
UserCollectedProperties::iterator end_;
};
crocksdb_user_collected_properties_iterator_t* crocksdb_user_collected_properties_iterator_t*
crocksdb_user_collected_properties_iter_create(crocksdb_user_collected_properties_t* props) { crocksdb_user_collected_properties_iter_create(
crocksdb_user_collected_properties_t* props) {
auto it = new crocksdb_user_collected_properties_iterator_t; auto it = new crocksdb_user_collected_properties_iterator_t;
it->rep_ = props->rep_; it->cur_ = props->rep_->begin();
it->iter_ = props->rep_->begin(); it->end_ = props->rep_->end();
return it; return it;
} }
...@@ -2960,35 +2962,48 @@ void crocksdb_user_collected_properties_iter_destroy( ...@@ -2960,35 +2962,48 @@ void crocksdb_user_collected_properties_iter_destroy(
unsigned char crocksdb_user_collected_properties_iter_valid( unsigned char crocksdb_user_collected_properties_iter_valid(
crocksdb_user_collected_properties_iterator_t* it) { crocksdb_user_collected_properties_iterator_t* it) {
return it->iter_ != it->rep_->end(); return it->cur_ != it->end_;
} }
void crocksdb_user_collected_properties_iter_next( void crocksdb_user_collected_properties_iter_next(
crocksdb_user_collected_properties_iterator_t* it) { crocksdb_user_collected_properties_iterator_t* it) {
++(it->iter_); ++(it->cur_);
} }
const char* crocksdb_user_collected_properties_iter_key( const char* crocksdb_user_collected_properties_iter_key(
crocksdb_user_collected_properties_iterator_t* it, size_t* klen) { crocksdb_user_collected_properties_iterator_t* it, size_t* klen) {
if (klen) { if (klen) {
*klen = it->iter_->first.size(); *klen = it->cur_->first.size();
} }
return it->iter_->first.data(); return it->cur_->first.data();
} }
const char* crocksdb_user_collected_properties_iter_value( const char* crocksdb_user_collected_properties_iter_value(
crocksdb_user_collected_properties_iterator_t* it, size_t* vlen) { crocksdb_user_collected_properties_iterator_t* it, size_t* vlen) {
if (vlen) { if (vlen) {
*vlen = it->iter_->second.size(); *vlen = it->cur_->second.size();
} }
return it->iter_->second.data(); return it->cur_->second.data();
} }
struct crocksdb_table_properties_t { struct crocksdb_table_properties_t {
TableProperties* rep_ = nullptr; std::shared_ptr<const TableProperties> rep_;
crocksdb_user_collected_properties_t user_props_; crocksdb_user_collected_properties_t users_;
void init(std::shared_ptr<const TableProperties> rep) {
rep_ = rep;
users_.rep_ = const_cast<UserCollectedProperties*>(&rep->user_collected_properties);
}
}; };
crocksdb_table_properties_t* crocksdb_table_properties_create() {
return new crocksdb_table_properties_t;
}
void crocksdb_table_properties_destroy(crocksdb_table_properties_t* props) {
delete props;
}
uint64_t crocksdb_table_properties_get_u64(crocksdb_table_properties_t* props, uint64_t crocksdb_table_properties_get_u64(crocksdb_table_properties_t* props,
crocksdb_table_property_t prop) { crocksdb_table_property_t prop) {
auto rep = props->rep_; auto rep = props->rep_;
...@@ -3033,14 +3048,12 @@ const char* crocksdb_table_properties_get_str(crocksdb_table_properties_t* props ...@@ -3033,14 +3048,12 @@ const char* crocksdb_table_properties_get_str(crocksdb_table_properties_t* props
if (slen) *slen = rep->compression_name.size(); if (slen) *slen = rep->compression_name.size();
return rep->compression_name.data(); return rep->compression_name.data();
} }
if (slen) *slen = 0;
return nullptr; return nullptr;
} }
crocksdb_user_collected_properties_t* crocksdb_user_collected_properties_t*
crocksdb_table_properties_get_user_properties(crocksdb_table_properties_t* props) { crocksdb_table_properties_get_user_properties(crocksdb_table_properties_t* props) {
props->user_props_.rep_ = &props->rep_->user_collected_properties; return &props->users_;
return &props->user_props_;
} }
/* Table Properties Collection */ /* Table Properties Collection */
...@@ -3057,12 +3070,6 @@ struct crocksdb_table_properties_collection_t { ...@@ -3057,12 +3070,6 @@ struct crocksdb_table_properties_collection_t {
} }
}; };
struct crocksdb_table_properties_collection_iterator_t {
TablePropertiesCollection* rep_ = nullptr;
TablePropertiesCollection::iterator iter_;
crocksdb_table_properties_t props_;
};
crocksdb_table_properties_collection_t* crocksdb_table_properties_collection_t*
crocksdb_table_properties_collection_create() { crocksdb_table_properties_collection_create() {
return new crocksdb_table_properties_collection_t; return new crocksdb_table_properties_collection_t;
...@@ -3073,11 +3080,17 @@ void crocksdb_table_properties_collection_destroy( ...@@ -3073,11 +3080,17 @@ void crocksdb_table_properties_collection_destroy(
delete collection; delete collection;
} }
struct crocksdb_table_properties_collection_iterator_t {
TablePropertiesCollection::iterator cur_;
TablePropertiesCollection::iterator end_;
};
crocksdb_table_properties_collection_iterator_t* crocksdb_table_properties_collection_iterator_t*
crocksdb_table_properties_collection_iter_create(crocksdb_table_properties_collection_t* c) { crocksdb_table_properties_collection_iter_create(
crocksdb_table_properties_collection_t* collection) {
auto it = new crocksdb_table_properties_collection_iterator_t; auto it = new crocksdb_table_properties_collection_iterator_t;
it->rep_ = c->rep_; it->cur_ = collection->rep_->begin();
it->iter_ = c->rep_->begin(); it->end_ = collection->rep_->end();
return it; return it;
} }
...@@ -3088,26 +3101,25 @@ void crocksdb_table_properties_collection_iter_destroy( ...@@ -3088,26 +3101,25 @@ void crocksdb_table_properties_collection_iter_destroy(
unsigned char crocksdb_table_properties_collection_iter_valid( unsigned char crocksdb_table_properties_collection_iter_valid(
crocksdb_table_properties_collection_iterator_t* it) { crocksdb_table_properties_collection_iterator_t* it) {
return it->iter_ != it->rep_->end(); return it->cur_ != it->end_;
} }
void crocksdb_table_properties_collection_iter_next( void crocksdb_table_properties_collection_iter_next(
crocksdb_table_properties_collection_iterator_t* it) { crocksdb_table_properties_collection_iterator_t* it) {
++(it->iter_); ++(it->cur_);
} }
const char* crocksdb_table_properties_collection_iter_key( const char* crocksdb_table_properties_collection_iter_key(
crocksdb_table_properties_collection_iterator_t* it, size_t* klen) { crocksdb_table_properties_collection_iterator_t* it, size_t* klen) {
if (klen) { if (klen) {
*klen = it->iter_->first.size(); *klen = it->cur_->first.size();
} }
return it->iter_->first.data(); return it->cur_->first.data();
} }
crocksdb_table_properties_t* crocksdb_table_properties_collection_iter_value( void crocksdb_table_properties_collection_iter_value(
crocksdb_table_properties_collection_iterator_t* it) { crocksdb_table_properties_collection_iterator_t* it, crocksdb_table_properties_t* props) {
it->props_.rep_ = const_cast<TableProperties*>(it->iter_->second.get()); props->init(it->cur_->second);
return &it->props_;
} }
/* Table Properties Collector */ /* Table Properties Collector */
...@@ -3116,10 +3128,10 @@ struct crocksdb_table_properties_collector_t : public TablePropertiesCollector { ...@@ -3116,10 +3128,10 @@ struct crocksdb_table_properties_collector_t : public TablePropertiesCollector {
void* state_; void* state_;
const char* (*name_)(void*); const char* (*name_)(void*);
void (*destruct_)(void*); void (*destruct_)(void*);
void (*add_userkey_)(void*, void (*add_)(void*,
const char* key, size_t key_len, const char* key, size_t key_len,
const char* value, size_t value_len, const char* value, size_t value_len,
int entry_type, uint64_t seq, uint64_t file_size); int entry_type, uint64_t seq, uint64_t file_size);
void (*finish_)(void*, crocksdb_user_collected_properties_t* props); void (*finish_)(void*, crocksdb_user_collected_properties_t* props);
virtual ~crocksdb_table_properties_collector_t() { virtual ~crocksdb_table_properties_collector_t() {
...@@ -3131,10 +3143,10 @@ struct crocksdb_table_properties_collector_t : public TablePropertiesCollector { ...@@ -3131,10 +3143,10 @@ struct crocksdb_table_properties_collector_t : public TablePropertiesCollector {
EntryType entry_type, EntryType entry_type,
SequenceNumber seq, SequenceNumber seq,
uint64_t file_size) override { uint64_t file_size) override {
add_userkey_(state_, add_(state_,
key.data(), key.size(), key.data(), key.size(),
value.data(), value.size(), value.data(), value.size(),
entry_type, seq, file_size); entry_type, seq, file_size);
return Status::OK(); return Status::OK();
} }
...@@ -3160,16 +3172,16 @@ crocksdb_table_properties_collector_create( ...@@ -3160,16 +3172,16 @@ crocksdb_table_properties_collector_create(
void* state, void* state,
const char* (*name)(void*), const char* (*name)(void*),
void (*destruct)(void*), void (*destruct)(void*),
void (*add_userkey)(void*, void (*add)(void*,
const char* key, size_t key_len, const char* key, size_t key_len,
const char* value, size_t value_len, const char* value, size_t value_len,
int entry_type, uint64_t seq, uint64_t file_size), int entry_type, uint64_t seq, uint64_t file_size),
void (*finish)(void*, crocksdb_user_collected_properties_t* props)) { void (*finish)(void*, crocksdb_user_collected_properties_t* props)) {
auto c = new crocksdb_table_properties_collector_t; auto c = new crocksdb_table_properties_collector_t;
c->state_ = state; c->state_ = state;
c->name_ = name; c->name_ = name;
c->destruct_ = destruct; c->destruct_ = destruct;
c->add_userkey_ = add_userkey; c->add_ = add;
c->finish_ = finish; c->finish_ = finish;
return c; return c;
} }
......
...@@ -231,7 +231,7 @@ crocksdb_create_column_family(crocksdb_t* db, ...@@ -231,7 +231,7 @@ 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( extern C_ROCKSDB_LIBRARY_API uint32_t crocksdb_column_family_handle_id(
crocksdb_column_family_handle_t*); 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(
...@@ -1211,6 +1211,23 @@ extern C_ROCKSDB_LIBRARY_API const char* crocksdb_pinnableslice_value( ...@@ -1211,6 +1211,23 @@ extern C_ROCKSDB_LIBRARY_API const char* crocksdb_pinnableslice_value(
/* Table Properties */ /* Table Properties */
extern C_ROCKSDB_LIBRARY_API crocksdb_table_properties_t*
crocksdb_table_properties_create();
extern C_ROCKSDB_LIBRARY_API void
crocksdb_table_properties_destroy(crocksdb_table_properties_t*);
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*);
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*, crocksdb_user_collected_properties_t*,
...@@ -1240,17 +1257,6 @@ extern C_ROCKSDB_LIBRARY_API const char* ...@@ -1240,17 +1257,6 @@ extern C_ROCKSDB_LIBRARY_API const char*
crocksdb_user_collected_properties_iter_value( crocksdb_user_collected_properties_iter_value(
crocksdb_user_collected_properties_iterator_t*, size_t* vlen); 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 */ /* Table Properties Collection */
extern C_ROCKSDB_LIBRARY_API crocksdb_table_properties_collection_t* extern C_ROCKSDB_LIBRARY_API crocksdb_table_properties_collection_t*
...@@ -1279,9 +1285,9 @@ extern C_ROCKSDB_LIBRARY_API const char* ...@@ -1279,9 +1285,9 @@ extern C_ROCKSDB_LIBRARY_API const char*
crocksdb_table_properties_collection_iter_key( crocksdb_table_properties_collection_iter_key(
crocksdb_table_properties_collection_iterator_t*, size_t* klen); crocksdb_table_properties_collection_iterator_t*, size_t* klen);
extern C_ROCKSDB_LIBRARY_API crocksdb_table_properties_t* extern C_ROCKSDB_LIBRARY_API void
crocksdb_table_properties_collection_iter_value( crocksdb_table_properties_collection_iter_value(
crocksdb_table_properties_collection_iterator_t*); crocksdb_table_properties_collection_iterator_t*, crocksdb_table_properties_t* props);
/* Table Properties Collector */ /* Table Properties Collector */
...@@ -1290,10 +1296,10 @@ crocksdb_table_properties_collector_create( ...@@ -1290,10 +1296,10 @@ crocksdb_table_properties_collector_create(
void* state, void* state,
const char* (*name)(void*), const char* (*name)(void*),
void (*destruct)(void*), void (*destruct)(void*),
void (*add_userkey)(void*, void (*add)(void*,
const char* key, size_t key_len, const char* key, size_t key_len,
const char* value, size_t value_len, const char* value, size_t value_len,
int entry_type, uint64_t seq, uint64_t file_size), int entry_type, uint64_t seq, uint64_t file_size),
void (*finish)(void*, crocksdb_user_collected_properties_t* props)); void (*finish)(void*, crocksdb_user_collected_properties_t* props));
extern C_ROCKSDB_LIBRARY_API void extern C_ROCKSDB_LIBRARY_API void
......
...@@ -636,7 +636,7 @@ extern "C" { ...@@ -636,7 +636,7 @@ extern "C" {
pub fn crocksdb_drop_column_family(db: *mut DBInstance, pub fn crocksdb_drop_column_family(db: *mut DBInstance,
column_family_handle: *mut DBCFHandle, column_family_handle: *mut DBCFHandle,
err: *mut *mut c_char); err: *mut *mut c_char);
pub fn crocksdb_column_family_handle_get_id(column_family_handle: *mut DBCFHandle) -> u32; pub fn crocksdb_column_family_handle_id(column_family_handle: *mut DBCFHandle) -> u32;
pub fn crocksdb_column_family_handle_destroy(column_family_handle: *mut DBCFHandle); pub fn crocksdb_column_family_handle_destroy(column_family_handle: *mut DBCFHandle);
pub fn crocksdb_list_column_families(db: *const DBOptions, pub fn crocksdb_list_column_families(db: *const DBOptions,
path: *const c_char, path: *const c_char,
...@@ -873,6 +873,10 @@ extern "C" { ...@@ -873,6 +873,10 @@ extern "C" {
pub fn crocksdb_user_collected_properties_iter_value pub fn crocksdb_user_collected_properties_iter_value
(it: *mut DBUserCollectedPropertiesIterator, vlen: *mut size_t) -> *const uint8_t; (it: *mut 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: *mut DBTableProperties,
prop: DBTableProperty) prop: DBTableProperty)
-> uint64_t; -> uint64_t;
...@@ -906,7 +910,7 @@ extern "C" { ...@@ -906,7 +910,7 @@ extern "C" {
it: *mut DBTablePropertiesCollectionIterator, klen: *mut size_t) -> *const uint8_t; it: *mut DBTablePropertiesCollectionIterator, klen: *mut size_t) -> *const uint8_t;
pub fn crocksdb_table_properties_collection_iter_value pub fn crocksdb_table_properties_collection_iter_value
(it: *mut DBTablePropertiesCollectionIterator) -> *mut DBTableProperties; (it: *mut DBTablePropertiesCollectionIterator, value: *mut DBTableProperties);
pub fn crocksdb_table_properties_collector_create(state: *mut c_void, pub fn crocksdb_table_properties_collector_create(state: *mut c_void,
name: extern "C" fn(*mut c_void) name: extern "C" fn(*mut c_void)
......
...@@ -26,7 +26,7 @@ use std::fmt::{self, Debug, Formatter}; ...@@ -26,7 +26,7 @@ use std::fmt::{self, Debug, Formatter};
use std::ops::Deref; use std::ops::Deref;
use std::path::Path; use std::path::Path;
use std::str::from_utf8; use std::str::from_utf8;
use table_properties::{TablePropertiesCollection, TablePropertiesCollectionHandle}; use table_properties::TablePropertiesCollection;
const DEFAULT_COLUMN_FAMILY: &'static str = "default"; const DEFAULT_COLUMN_FAMILY: &'static str = "default";
...@@ -36,7 +36,7 @@ pub struct CFHandle { ...@@ -36,7 +36,7 @@ pub struct CFHandle {
impl CFHandle { impl CFHandle {
pub fn 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_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 handle = TablePropertiesCollectionHandle::new(); let props = TablePropertiesCollection::new();
ffi_try!(crocksdb_get_properties_of_all_tables(self.inner, handle.inner)); ffi_try!(crocksdb_get_properties_of_all_tables(self.inner, props.inner));
Ok(TablePropertiesCollection::new(handle)) Ok(props)
} }
} }
...@@ -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 handle = TablePropertiesCollectionHandle::new(); let props = TablePropertiesCollection::new();
ffi_try!(crocksdb_get_properties_of_all_tables_cf(self.inner, cf.inner, handle.inner)); ffi_try!(crocksdb_get_properties_of_all_tables_cf(self.inner, cf.inner, props.inner));
Ok(TablePropertiesCollection::new(handle)) Ok(props)
} }
} }
...@@ -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 handle = TablePropertiesCollectionHandle::new(); let props = TablePropertiesCollection::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(),
handle.inner)); props.inner));
Ok(TablePropertiesCollection::new(handle)) Ok(props)
} }
} }
} }
......
...@@ -21,24 +21,10 @@ use std::slice; ...@@ -21,24 +21,10 @@ use std::slice;
use std::str; use std::str;
pub struct TablePropertiesCollection { pub struct TablePropertiesCollection {
handle: TablePropertiesCollectionHandle,
}
impl TablePropertiesCollection {
pub fn new(handle: TablePropertiesCollectionHandle) -> TablePropertiesCollection {
TablePropertiesCollection { handle: handle }
}
pub fn iter(&self) -> TablePropertiesCollectionIter {
TablePropertiesCollectionIter::new(self, self.handle.inner)
}
}
pub struct TablePropertiesCollectionHandle {
pub inner: *mut DBTablePropertiesCollection, pub inner: *mut DBTablePropertiesCollection,
} }
impl Drop for TablePropertiesCollectionHandle { impl Drop for TablePropertiesCollection {
fn drop(&mut self) { fn drop(&mut self) {
unsafe { unsafe {
crocksdb_ffi::crocksdb_table_properties_collection_destroy(self.inner); crocksdb_ffi::crocksdb_table_properties_collection_destroy(self.inner);
...@@ -46,18 +32,28 @@ impl Drop for TablePropertiesCollectionHandle { ...@@ -46,18 +32,28 @@ impl Drop for TablePropertiesCollectionHandle {
} }
} }
impl TablePropertiesCollectionHandle { impl TablePropertiesCollection {
pub fn new() -> TablePropertiesCollectionHandle { pub fn new() -> TablePropertiesCollection {
unsafe { unsafe {
TablePropertiesCollectionHandle { TablePropertiesCollection {
inner: crocksdb_ffi::crocksdb_table_properties_collection_create(), inner: crocksdb_ffi::crocksdb_table_properties_collection_create(),
} }
} }
} }
pub fn collect(&self) -> HashMap<&str, TableProperties> {
let mut res = HashMap::new();
let mut iter = TablePropertiesCollectionIter::new(self, self.inner);
while iter.valid() {
res.insert(iter.key(), iter.value());
iter.next();
}
res
}
} }
pub struct TablePropertiesCollectionIter<'a> { pub struct TablePropertiesCollectionIter<'a> {
props: &'a TablePropertiesCollection, props: PhantomData<&'a TablePropertiesCollection>,
inner: *mut DBTablePropertiesCollectionIterator, inner: *mut DBTablePropertiesCollectionIterator,
} }
...@@ -70,12 +66,12 @@ impl<'a> Drop for TablePropertiesCollectionIter<'a> { ...@@ -70,12 +66,12 @@ impl<'a> Drop for TablePropertiesCollectionIter<'a> {
} }
impl<'a> TablePropertiesCollectionIter<'a> { impl<'a> TablePropertiesCollectionIter<'a> {
fn new(props: &'a TablePropertiesCollection, fn new(_: &'a TablePropertiesCollection,
inner: *mut DBTablePropertiesCollection) inner: *mut DBTablePropertiesCollection)
-> TablePropertiesCollectionIter<'a> { -> TablePropertiesCollectionIter<'a> {
unsafe { unsafe {
TablePropertiesCollectionIter { TablePropertiesCollectionIter {
props: props, props: PhantomData,
inner: crocksdb_ffi::crocksdb_table_properties_collection_iter_create(inner), inner: crocksdb_ffi::crocksdb_table_properties_collection_iter_create(inner),
} }
} }
...@@ -91,7 +87,7 @@ impl<'a> TablePropertiesCollectionIter<'a> { ...@@ -91,7 +87,7 @@ impl<'a> TablePropertiesCollectionIter<'a> {
} }
} }
pub fn key(&self) -> &str { pub fn key(&self) -> &'a str {
unsafe { unsafe {
let mut klen: size_t = 0; let mut klen: size_t = 0;
let k = crocksdb_ffi::crocksdb_table_properties_collection_iter_key(self.inner, let k = crocksdb_ffi::crocksdb_table_properties_collection_iter_key(self.inner,
...@@ -103,35 +99,40 @@ impl<'a> TablePropertiesCollectionIter<'a> { ...@@ -103,35 +99,40 @@ impl<'a> TablePropertiesCollectionIter<'a> {
pub fn value(&self) -> TableProperties { pub fn value(&self) -> TableProperties {
unsafe { unsafe {
let inner = crocksdb_ffi::crocksdb_table_properties_collection_iter_value(self.inner); let props = TableProperties::new();
TableProperties::new(self.props, inner) crocksdb_ffi::crocksdb_table_properties_collection_iter_value(self.inner, props.inner);
props
} }
} }
} }
pub struct TableProperties<'a> { pub struct TableProperties {
phantom: PhantomData<&'a TablePropertiesCollection>,
inner: *mut DBTableProperties, inner: *mut DBTableProperties,
} }
impl<'a> TableProperties<'a> { impl Drop for TableProperties {
fn new(_: &'a TablePropertiesCollection, inner: *mut DBTableProperties) -> TableProperties { fn drop(&mut self) {
TableProperties { unsafe {
phantom: PhantomData, crocksdb_ffi::crocksdb_table_properties_destroy(self.inner);
inner: inner,
} }
} }
}
impl TableProperties {
fn new() -> TableProperties {
unsafe { TableProperties { inner: crocksdb_ffi::crocksdb_table_properties_create() } }
}
fn get_u64(&self, prop: DBTableProperty) -> u64 { 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) -> String { fn get_str(&self, prop: DBTableProperty) -> &str {
unsafe { unsafe {
let mut slen: size_t = 0; 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); let bytes = slice::from_raw_parts(s, slen);
String::from_utf8(bytes.to_owned()).unwrap() str::from_utf8(bytes).unwrap()
} }
} }
...@@ -175,49 +176,50 @@ impl<'a> TableProperties<'a> { ...@@ -175,49 +176,50 @@ impl<'a> TableProperties<'a> {
self.get_u64(DBTableProperty::ColumnFamilyId) self.get_u64(DBTableProperty::ColumnFamilyId)
} }
pub fn column_family_name(&self) -> String { pub fn column_family_name(&self) -> &str {
self.get_str(DBTableProperty::ColumnFamilyName) self.get_str(DBTableProperty::ColumnFamilyName)
} }
pub fn filter_policy_name(&self) -> String { pub fn filter_policy_name(&self) -> &str {
self.get_str(DBTableProperty::FilterPolicyName) self.get_str(DBTableProperty::FilterPolicyName)
} }
pub fn comparator_name(&self) -> String { pub fn comparator_name(&self) -> &str {
self.get_str(DBTableProperty::ComparatorName) self.get_str(DBTableProperty::ComparatorName)
} }
pub fn merge_operator_name(&self) -> String { pub fn merge_operator_name(&self) -> &str {
self.get_str(DBTableProperty::MergeOperatorName) self.get_str(DBTableProperty::MergeOperatorName)
} }
pub fn prefix_extractor_name(&self) -> String { pub fn prefix_extractor_name(&self) -> &str {
self.get_str(DBTableProperty::PrefixExtractorName) self.get_str(DBTableProperty::PrefixExtractorName)
} }
pub fn property_collectors_names(&self) -> String { pub fn property_collectors_names(&self) -> &str {
self.get_str(DBTableProperty::PropertyCollectorsNames) self.get_str(DBTableProperty::PropertyCollectorsNames)
} }
pub fn compression_name(&self) -> String { pub fn compression_name(&self) -> &str {
self.get_str(DBTableProperty::CompressionName) self.get_str(DBTableProperty::CompressionName)
} }
pub fn user_collected_properties(&self) -> HashMap<Vec<u8>, Vec<u8>> { pub fn user_collected_properties(&self) -> HashMap<&[u8], &[u8]> {
let inner = let mut res = HashMap::new();
unsafe { crocksdb_ffi::crocksdb_table_properties_get_user_properties(self.inner) }; unsafe {
let mut iter = UserCollectedPropertiesIter::new(self, inner); let inner = crocksdb_ffi::crocksdb_table_properties_get_user_properties(self.inner);
let mut props = HashMap::new(); let mut iter = UserCollectedPropertiesIter::new(self, inner);
while iter.valid() { while iter.valid() {
props.insert(iter.key().to_owned(), iter.value().to_owned()); res.insert(iter.key(), iter.value());
iter.next(); iter.next();
}
} }
props res
} }
} }
struct UserCollectedPropertiesIter<'a> { struct UserCollectedPropertiesIter<'a> {
phantom: PhantomData<&'a TableProperties<'a>>, props: PhantomData<&'a TablePropertiesCollection>,
inner: *mut DBUserCollectedPropertiesIterator, inner: *mut DBUserCollectedPropertiesIterator,
} }
...@@ -235,7 +237,7 @@ impl<'a> UserCollectedPropertiesIter<'a> { ...@@ -235,7 +237,7 @@ impl<'a> UserCollectedPropertiesIter<'a> {
-> UserCollectedPropertiesIter<'a> { -> UserCollectedPropertiesIter<'a> {
unsafe { unsafe {
UserCollectedPropertiesIter { UserCollectedPropertiesIter {
phantom: PhantomData, props: PhantomData,
inner: crocksdb_ffi::crocksdb_user_collected_properties_iter_create(inner), inner: crocksdb_ffi::crocksdb_user_collected_properties_iter_create(inner),
} }
} }
...@@ -251,7 +253,7 @@ impl<'a> UserCollectedPropertiesIter<'a> { ...@@ -251,7 +253,7 @@ impl<'a> UserCollectedPropertiesIter<'a> {
} }
} }
fn key(&self) -> &[u8] { fn key(&self) -> &'a [u8] {
unsafe { unsafe {
let mut klen: size_t = 0; let mut klen: size_t = 0;
let k = crocksdb_ffi::crocksdb_user_collected_properties_iter_key(self.inner, let k = crocksdb_ffi::crocksdb_user_collected_properties_iter_key(self.inner,
...@@ -260,7 +262,7 @@ impl<'a> UserCollectedPropertiesIter<'a> { ...@@ -260,7 +262,7 @@ impl<'a> UserCollectedPropertiesIter<'a> {
} }
} }
fn value(&self) -> &[u8] { fn value(&self) -> &'a [u8] {
unsafe { unsafe {
let mut vlen: size_t = 0; let mut vlen: size_t = 0;
let v = crocksdb_ffi::crocksdb_user_collected_properties_iter_value(self.inner, let v = crocksdb_ffi::crocksdb_user_collected_properties_iter_value(self.inner,
......
...@@ -29,12 +29,12 @@ pub trait TablePropertiesCollector { ...@@ -29,12 +29,12 @@ pub trait TablePropertiesCollector {
fn name(&self) -> &str; fn name(&self) -> &str;
/// Will be called when a new key/value pair is inserted into the table. /// Will be called when a new key/value pair is inserted into the table.
fn add_userkey(&mut self, fn add(&mut self,
key: &[u8], key: &[u8],
value: &[u8], value: &[u8],
entry_type: DBEntryType, entry_type: DBEntryType,
seq: u64, seq: u64,
file_size: u64); file_size: u64);
/// 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.
...@@ -69,19 +69,19 @@ extern "C" fn destruct(handle: *mut c_void) { ...@@ -69,19 +69,19 @@ extern "C" fn destruct(handle: *mut c_void) {
} }
} }
pub extern "C" fn add_userkey(handle: *mut c_void, pub extern "C" fn add(handle: *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,
value_len: size_t, value_len: size_t,
entry_type: c_int, entry_type: c_int,
seq: uint64_t, seq: uint64_t,
file_size: uint64_t) { file_size: uint64_t) {
unsafe { unsafe {
let handle = &mut *(handle as *mut TablePropertiesCollectorHandle); let handle = &mut *(handle as *mut TablePropertiesCollectorHandle);
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);
handle.rep.add_userkey(key, value, mem::transmute(entry_type), seq, file_size); handle.rep.add(key, value, mem::transmute(entry_type), seq, file_size);
} }
} }
...@@ -105,7 +105,7 @@ pub unsafe fn new_table_properties_collector(collector: Box<TablePropertiesColle ...@@ -105,7 +105,7 @@ pub unsafe fn new_table_properties_collector(collector: Box<TablePropertiesColle
Box::into_raw(Box::new(handle)) as *mut c_void, Box::into_raw(Box::new(handle)) as *mut c_void,
name, name,
destruct, destruct,
add_userkey, add,
finish, finish,
) )
} }
...@@ -73,12 +73,12 @@ impl ExampleCollector { ...@@ -73,12 +73,12 @@ impl ExampleCollector {
props props
} }
fn decode(props: &HashMap<Vec<u8>, Vec<u8>>) -> ExampleCollector { fn decode(props: HashMap<&[u8], &[u8]>) -> ExampleCollector {
let mut c = ExampleCollector::new(); let mut c = ExampleCollector::new();
c.num_keys = decode_u32(props.get(&vec![Props::NumKeys as u8]).unwrap()); c.num_keys = decode_u32(props.get(&[Props::NumKeys as u8].as_ref()).unwrap());
c.num_puts = decode_u32(props.get(&vec![Props::NumPuts as u8]).unwrap()); c.num_puts = decode_u32(props.get(&[Props::NumPuts as u8].as_ref()).unwrap());
c.num_merges = decode_u32(props.get(&vec![Props::NumMerges as u8]).unwrap()); c.num_merges = decode_u32(props.get(&[Props::NumMerges as u8].as_ref()).unwrap());
c.num_deletes = decode_u32(props.get(&vec![Props::NumDeletes as u8]).unwrap()); c.num_deletes = decode_u32(props.get(&[Props::NumDeletes as u8].as_ref()).unwrap());
c c
} }
} }
...@@ -99,7 +99,7 @@ impl TablePropertiesCollector for ExampleCollector { ...@@ -99,7 +99,7 @@ impl TablePropertiesCollector for ExampleCollector {
"example-collector" "example-collector"
} }
fn add_userkey(&mut self, key: &[u8], _: &[u8], entry_type: DBEntryType, _: u64, _: u64) { fn add(&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();
...@@ -138,24 +138,19 @@ impl TablePropertiesCollectorFactory for ExampleFactory { ...@@ -138,24 +138,19 @@ impl TablePropertiesCollectorFactory for ExampleFactory {
} }
fn check_collection(collection: &TablePropertiesCollection, fn check_collection(collection: &TablePropertiesCollection,
num_files: u32, num_files: usize,
num_keys: u32, num_keys: u32,
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();
let mut iter = collection.iter(); let props = collection.collect();
while iter.valid() { for (k, v) in &props {
len += 1; assert!(k.ends_with(".sst"));
{ assert_eq!(v.property_collectors_names(), "[example-factory]");
let v = iter.value(); res.add(&ExampleCollector::decode(v.user_collected_properties()));
assert_eq!(v.property_collectors_names(), "[example-factory]");
res.add(&ExampleCollector::decode(&v.user_collected_properties()));
}
iter.next();
} }
assert_eq!(len, num_files); assert_eq!(props.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