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
......
......@@ -2966,27 +2966,28 @@ void crocksdb_get_supported_compression(int* v, size_t l) {
/* Table Properties */
struct crocksdb_user_collected_properties_t {
UserCollectedProperties* rep_ = nullptr;
UserCollectedProperties rep;
};
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_;
UserCollectedProperties::const_iterator cur_;
UserCollectedProperties::const_iterator end_;
};
crocksdb_user_collected_properties_iterator_t*
crocksdb_user_collected_properties_iter_create(
crocksdb_user_collected_properties_t* props) {
const crocksdb_user_collected_properties_t* props) {
auto it = new crocksdb_user_collected_properties_iterator_t;
it->cur_ = props->rep_->begin();
it->end_ = props->rep_->end();
it->cur_ = props->rep.begin();
it->end_ = props->rep.end();
return it;
}
......@@ -2996,7 +2997,7 @@ void crocksdb_user_collected_properties_iter_destroy(
}
unsigned char crocksdb_user_collected_properties_iter_valid(
crocksdb_user_collected_properties_iterator_t* it) {
const crocksdb_user_collected_properties_iterator_t* it) {
return it->cur_ != it->end_;
}
......@@ -3006,85 +3007,100 @@ void crocksdb_user_collected_properties_iter_next(
}
const char* crocksdb_user_collected_properties_iter_key(
crocksdb_user_collected_properties_iterator_t* it, size_t* klen) {
const crocksdb_user_collected_properties_iterator_t* it, size_t* klen) {
*klen = it->cur_->first.size();
return it->cur_->first.data();
}
const char* crocksdb_user_collected_properties_iter_value(
crocksdb_user_collected_properties_iterator_t* it, size_t* vlen) {
const crocksdb_user_collected_properties_iterator_t* it, size_t* vlen) {
*vlen = it->cur_->second.size();
return it->cur_->second.data();
}
struct crocksdb_table_properties_t {
std::shared_ptr<const TableProperties> rep_;
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);
}
const TableProperties rep;
};
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,
crocksdb_table_property_t prop) {
auto rep = props->rep_;
uint64_t crocksdb_table_properties_get_u64(
const crocksdb_table_properties_t* props, crocksdb_table_property_t prop) {
const TableProperties& rep = props->rep;
switch (prop) {
case kDataSize: return rep->data_size;
case kIndexSize: return rep->index_size;
case kFilterSize: return rep->filter_size;
case kRawKeySize: return rep->raw_key_size;
case kRawValueSize: return rep->raw_value_size;
case kNumDataBlocks: return rep->num_data_blocks;
case kNumEntries: return rep->num_entries;
case kFormatVersion: return rep->format_version;
case kFixedKeyLen: return rep->data_size;
case kColumnFamilyID: return rep->column_family_id;
case kDataSize:
return rep.data_size;
case kIndexSize:
return rep.index_size;
case kFilterSize:
return rep.filter_size;
case kRawKeySize:
return rep.raw_key_size;
case kRawValueSize:
return rep.raw_value_size;
case kNumDataBlocks:
return rep.num_data_blocks;
case kNumEntries:
return rep.num_entries;
case kFormatVersion:
return rep.format_version;
case kFixedKeyLen:
return rep.data_size;
case kColumnFamilyID:
return rep.column_family_id;
}
return 0;
}
const char* crocksdb_table_properties_get_str(crocksdb_table_properties_t* props,
crocksdb_table_property_t prop, size_t* slen) {
auto rep = props->rep_;
const char* crocksdb_table_properties_get_str(
const crocksdb_table_properties_t* props, crocksdb_table_property_t prop,
size_t* slen) {
const TableProperties& rep = props->rep;
switch (prop) {
case kColumnFamilyName:
*slen = rep->column_family_name.size();
return rep->column_family_name.data();
*slen = rep.column_family_name.size();
return rep.column_family_name.data();
case kFilterPolicyName:
*slen = rep->filter_policy_name.size();
return rep->filter_policy_name.data();
*slen = rep.filter_policy_name.size();
return rep.filter_policy_name.data();
case kComparatorName:
*slen = rep->comparator_name.size();
return rep->comparator_name.data();
*slen = rep.comparator_name.size();
return rep.comparator_name.data();
case kMergeOperatorName:
*slen = rep->merge_operator_name.size();
return rep->merge_operator_name.data();
*slen = rep.merge_operator_name.size();
return rep.merge_operator_name.data();
case kPrefixExtractorName:
*slen = rep->prefix_extractor_name.size();
return rep->prefix_extractor_name.data();
*slen = rep.prefix_extractor_name.size();
return rep.prefix_extractor_name.data();
case kPropertyCollectorsNames:
*slen = rep->property_collectors_names.size();
return rep->property_collectors_names.data();
*slen = rep.property_collectors_names.size();
return rep.property_collectors_names.data();
case kCompressionName:
*slen = rep->compression_name.size();
return rep->compression_name.data();
*slen = rep.compression_name.size();
return rep.compression_name.data();
}
return nullptr;
}
crocksdb_user_collected_properties_t*
crocksdb_table_properties_get_user_properties(crocksdb_table_properties_t* props) {
return &props->users_;
const crocksdb_user_collected_properties_t*
crocksdb_table_properties_get_user_properties(
const crocksdb_table_properties_t* props) {
return reinterpret_cast<const crocksdb_user_collected_properties_t*>(
&props->rep.user_collected_properties);
}
const char* crocksdb_user_collected_properties_get(
const crocksdb_user_collected_properties_t* props, const char* key,
size_t klen, size_t* vlen) {
auto val = props->rep.find(std::string(key, klen));
if (val == props->rep.end()) {
return nullptr;
}
*vlen = val->second.size();
return val->second.data();
}
size_t crocksdb_user_collected_properties_len(
const crocksdb_user_collected_properties_t* props) {
return props->rep.size();
}
/* Table Properties Collection */
......@@ -3093,24 +3109,24 @@ struct crocksdb_table_properties_collection_t {
TablePropertiesCollection rep_;
};
crocksdb_table_properties_collection_t*
crocksdb_table_properties_collection_create() {
return new crocksdb_table_properties_collection_t;
size_t crocksdb_table_properties_collection_len(
const crocksdb_table_properties_collection_t* props) {
return props->rep_.size();
}
void crocksdb_table_properties_collection_destroy(
crocksdb_table_properties_collection_t* collection) {
delete collection;
crocksdb_table_properties_collection_t* t) {
delete t;
}
struct crocksdb_table_properties_collection_iterator_t {
TablePropertiesCollection::iterator cur_;
TablePropertiesCollection::iterator end_;
TablePropertiesCollection::const_iterator cur_;
TablePropertiesCollection::const_iterator end_;
};
crocksdb_table_properties_collection_iterator_t*
crocksdb_table_properties_collection_iter_create(
crocksdb_table_properties_collection_t* collection) {
const crocksdb_table_properties_collection_t* collection) {
auto it = new crocksdb_table_properties_collection_iterator_t;
it->cur_ = collection->rep_.begin();
it->end_ = collection->rep_.end();
......@@ -3123,7 +3139,7 @@ void crocksdb_table_properties_collection_iter_destroy(
}
unsigned char crocksdb_table_properties_collection_iter_valid(
crocksdb_table_properties_collection_iterator_t* it) {
const crocksdb_table_properties_collection_iterator_t* it) {
return it->cur_ != it->end_;
}
......@@ -3133,14 +3149,16 @@ void crocksdb_table_properties_collection_iter_next(
}
const char* crocksdb_table_properties_collection_iter_key(
crocksdb_table_properties_collection_iterator_t* it, size_t* klen) {
const crocksdb_table_properties_collection_iterator_t* it, size_t* klen) {
*klen = it->cur_->first.size();
return it->cur_->first.data();
}
void crocksdb_table_properties_collection_iter_value(
crocksdb_table_properties_collection_iterator_t* it, crocksdb_table_properties_t* props) {
props->init(it->cur_->second);
const crocksdb_table_properties_t*
crocksdb_table_properties_collection_iter_value(
const crocksdb_table_properties_collection_iterator_t* it) {
return reinterpret_cast<const crocksdb_table_properties_t*>(
it->cur_->second.get());
}
/* Table Properties Collector */
......@@ -3172,9 +3190,8 @@ struct crocksdb_table_properties_collector_t : public TablePropertiesCollector {
}
virtual Status Finish(UserCollectedProperties* rep) override {
crocksdb_user_collected_properties_t props;
props.rep_ = rep;
finish_(state_, &props);
finish_(state_,
reinterpret_cast<crocksdb_user_collected_properties_t*>(rep));
return Status::OK();
}
......@@ -3262,41 +3279,54 @@ void crocksdb_options_add_table_properties_collector_factory(
/* Get Table Properties */
void crocksdb_get_properties_of_all_tables(crocksdb_t* db,
crocksdb_table_properties_collection_t* props, char** errptr) {
crocksdb_table_properties_collection_t* crocksdb_get_properties_of_all_tables(
crocksdb_t* db, char** errptr) {
std::unique_ptr<crocksdb_table_properties_collection_t> props(
new crocksdb_table_properties_collection_t);
auto s = db->rep->GetPropertiesOfAllTables(&props->rep_);
if (!s.ok()) {
SaveError(errptr, s);
return nullptr;
}
return props.release();
}
void crocksdb_get_properties_of_all_tables_cf(
crocksdb_t* db, crocksdb_column_family_handle_t* cf,
crocksdb_table_properties_collection_t* props, char** errptr) {
crocksdb_table_properties_collection_t*
crocksdb_get_properties_of_all_tables_cf(crocksdb_t* db,
crocksdb_column_family_handle_t* cf,
char** errptr) {
std::unique_ptr<crocksdb_table_properties_collection_t> props(
new crocksdb_table_properties_collection_t);
auto s = db->rep->GetPropertiesOfAllTables(cf->rep, &props->rep_);
if (!s.ok()) {
SaveError(errptr, s);
return nullptr;
}
return props.release();
}
void crocksdb_get_properties_of_tables_in_range(
crocksdb_t* db, crocksdb_column_family_handle_t* cf,
int num_ranges,
crocksdb_table_properties_collection_t*
crocksdb_get_properties_of_tables_in_range(
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) {
std::vector<Range> ranges;
for (int i = 0; i < num_ranges; i++) {
ranges.emplace_back(Range(Slice(start_keys[i], start_keys_lens[i]),
Slice(limit_keys[i], limit_keys_lens[i])));
}
std::unique_ptr<crocksdb_table_properties_collection_t> props(
new crocksdb_table_properties_collection_t);
auto s = db->rep->GetPropertiesOfTablesInRange(cf->rep,
ranges.data(),
ranges.size(),
&props->rep_);
if (!s.ok()) {
SaveError(errptr, s);
return nullptr;
}
return props.release();
}
void crocksdb_set_bottommost_compression(crocksdb_options_t* opt, int c) {
......
......@@ -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 iter(&self) -> TablePropertiesCollectionIter {
self.into_iter()
}
pub fn len(&self) -> usize {
unsafe { crocksdb_ffi::crocksdb_table_properties_collection_len(self.inner) }
}
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();
pub fn is_empty(&self) -> bool {
self.len() == 0
}
res
}
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))
}
}
pub fn len(&self) -> usize {
unsafe { crocksdb_ffi::crocksdb_user_collected_properties_len(&self.inner) }
}
pub fn is_empty(&self) -> bool {
self.len() == 0
}
}
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))
}
res
}
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)
}
}
struct UserCollectedPropertiesIter<'a> {
props: PhantomData<&'a TableProperties>,
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) {
fn next(&mut self) -> Option<(&'a [u8], &'a [u8])> {
unsafe {
crocksdb_ffi::crocksdb_user_collected_properties_iter_next(self.inner);
if !crocksdb_ffi::crocksdb_user_collected_properties_iter_valid(self.inner) {
return None;
}
}
fn key(&self) -> &'a [u8] {
unsafe {
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