Commit a7c9bd69 authored by Huachao Huang's avatar Huachao Huang

*: rewrite to avoid memory layout dependencies

parent e3bd990f
...@@ -662,6 +662,10 @@ void crocksdb_drop_column_family( ...@@ -662,6 +662,10 @@ 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) {
return handle->rep->GetID();
}
void crocksdb_column_family_handle_destroy(crocksdb_column_family_handle_t* handle) { void crocksdb_column_family_handle_destroy(crocksdb_column_family_handle_t* handle) {
delete handle->rep; delete handle->rep;
delete handle; delete handle;
...@@ -2924,25 +2928,202 @@ const char* crocksdb_pinnableslice_value(const crocksdb_pinnableslice_t* v, ...@@ -2924,25 +2928,202 @@ const char* crocksdb_pinnableslice_value(const crocksdb_pinnableslice_t* v,
return v->rep.data(); return v->rep.data();
} }
/* Table Properties */
struct crocksdb_user_collected_properties_t { struct crocksdb_user_collected_properties_t {
UserCollectedProperties* inner; UserCollectedProperties* rep_ = nullptr;
crocksdb_user_collected_properties_t(UserCollectedProperties* props) : inner(props) {} };
struct crocksdb_user_collected_properties_iterator_t {
const UserCollectedProperties* rep_ = nullptr;
UserCollectedProperties::const_iterator iter_;
}; };
void crocksdb_user_collected_properties_add(crocksdb_user_collected_properties_t* props, void crocksdb_user_collected_properties_add(crocksdb_user_collected_properties_t* props,
const char* key, size_t key_len, const char* k, size_t klen,
const char* value, size_t value_len) { const char* v, size_t vlen) {
props->inner->emplace(std::make_pair(std::string(key, key_len), std::string(value, value_len))); props->rep_->emplace(std::make_pair(std::string(k, klen), std::string(v, vlen)));
} }
struct crocksdb_table_properties_collector_t : public TablePropertiesCollector { crocksdb_user_collected_properties_iterator_t*
crocksdb_table_properties_collector_context_t* rep_; crocksdb_user_collected_properties_iter_create(crocksdb_user_collected_properties_t* props) {
auto it = new crocksdb_user_collected_properties_iterator_t;
it->rep_ = props->rep_;
it->iter_ = props->rep_->begin();
return it;
}
void crocksdb_user_collected_properties_iter_destroy(
crocksdb_user_collected_properties_iterator_t* it) {
delete it;
}
unsigned char crocksdb_user_collected_properties_iter_valid(
crocksdb_user_collected_properties_iterator_t* it) {
return it->iter_ != it->rep_->end();
}
void crocksdb_user_collected_properties_iter_next(
crocksdb_user_collected_properties_iterator_t* it) {
(it->iter_)++;
}
const char* crocksdb_user_collected_properties_iter_key(
crocksdb_user_collected_properties_iterator_t* it, size_t* klen) {
if (klen) {
*klen = it->iter_->first.size();
}
return it->iter_->first.data();
}
const char* crocksdb_user_collected_properties_iter_value(
crocksdb_user_collected_properties_iterator_t* it, size_t* vlen) {
if (vlen) {
*vlen = it->iter_->second.size();
}
return it->iter_->second.data();
}
struct crocksdb_table_properties_t {
TableProperties* rep_ = nullptr;
crocksdb_user_collected_properties_t user_props_;
};
uint64_t crocksdb_table_properties_get_u64(crocksdb_table_properties_t* props,
crocksdb_table_property_t prop) {
auto rep = props->rep_;
switch (prop) {
case DATA_SIZE: return rep->data_size;
case INDEX_SIZE: return rep->index_size;
case FILTER_SIZE: return rep->filter_size;
case RAW_KEY_SIZE: return rep->raw_key_size;
case RAW_VALUE_SIZE: return rep->raw_value_size;
case NUM_DATA_BLOCKS: return rep->num_data_blocks;
case NUM_ENTRIES: return rep->num_entries;
case FORMAT_VERSION: return rep->format_version;
case FIXED_KEY_LEN: return rep->data_size;
case COLUMN_FAMILY_ID: 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_;
switch (prop) {
case COLUMN_FAMILY_NAME:
if (slen) *slen = rep->column_family_name.size();
return rep->column_family_name.data();
case FILTER_POLICY_NAME:
if (slen) *slen = rep->filter_policy_name.size();
return rep->filter_policy_name.data();
case COMPARATOR_NAME:
if (slen) *slen = rep->comparator_name.size();
return rep->comparator_name.data();
case MERGE_OPERATOR_NAME:
if (slen) *slen = rep->merge_operator_name.size();
return rep->merge_operator_name.data();
case PREFIX_EXTRACTOR_NAME:
if (slen) *slen = rep->prefix_extractor_name.size();
return rep->prefix_extractor_name.data();
case PROPERTY_COLLECTORS_NAMES:
if (slen) *slen = rep->property_collectors_names.size();
return rep->property_collectors_names.data();
case COMPRESSION_NAME:
if (slen) *slen = rep->compression_name.size();
return rep->compression_name.data();
}
return "";
}
crocksdb_user_collected_properties_t*
crocksdb_table_properties_get_user_properties(crocksdb_table_properties_t* props) {
props->user_props_.rep_ =
const_cast<UserCollectedProperties*>(&props->rep_->user_collected_properties);
return &props->user_props_;
}
/* Table Properties Collection */
crocksdb_table_properties_collector_t( struct crocksdb_table_properties_collection_t {
crocksdb_table_properties_collector_context_t* context) : rep_(context) {} TablePropertiesCollection* rep_ = nullptr;
crocksdb_table_properties_collection_t() {
rep_ = new TablePropertiesCollection;
}
~crocksdb_table_properties_collection_t() {
delete rep_;
}
};
struct crocksdb_table_properties_collection_iterator_t {
const TablePropertiesCollection* rep_ = nullptr;
TablePropertiesCollection::const_iterator iter_;
crocksdb_table_properties_t props_;
};
crocksdb_table_properties_collection_t*
crocksdb_table_properties_collection_create() {
return new crocksdb_table_properties_collection_t;
}
void crocksdb_table_properties_collection_destroy(
crocksdb_table_properties_collection_t* collection) {
delete collection;
}
crocksdb_table_properties_collection_iterator_t*
crocksdb_table_properties_collection_iter_create(crocksdb_table_properties_collection_t* c) {
auto it = new crocksdb_table_properties_collection_iterator_t;
it->rep_ = c->rep_;
it->iter_ = c->rep_->begin();
return it;
}
void crocksdb_table_properties_collection_iter_destroy(
crocksdb_table_properties_collection_iterator_t* it) {
delete it;
}
unsigned char crocksdb_table_properties_collection_iter_valid(
crocksdb_table_properties_collection_iterator_t* it) {
return it->iter_ != it->rep_->end();
}
void crocksdb_table_properties_collection_iter_next(
crocksdb_table_properties_collection_iterator_t* it) {
(it->iter_)++;
}
const char* crocksdb_table_properties_collection_iter_key(
crocksdb_table_properties_collection_iterator_t* it, size_t* klen) {
if (klen) {
*klen = it->iter_->first.size();
}
return it->iter_->first.data();
}
crocksdb_table_properties_t* crocksdb_table_properties_collection_iter_value(
crocksdb_table_properties_collection_iterator_t* it) {
it->props_.rep_ = const_cast<TableProperties*>(it->iter_->second.get());
return &it->props_;
}
/* Table Properties Collector */
struct crocksdb_table_properties_collector_t : public TablePropertiesCollector {
void* state_;
const char* (*name_)(void*);
void (*destruct_)(void*);
void (*add_userkey_)(void*,
const char* key, size_t key_len,
const char* value, size_t value_len,
int entry_type, uint64_t seq, uint64_t file_size);
void (*finish_)(void*, crocksdb_user_collected_properties_t* props);
virtual ~crocksdb_table_properties_collector_t() { virtual ~crocksdb_table_properties_collector_t() {
rep_->destructor(rep_); destruct_(state_);
} }
virtual Status AddUserKey(const Slice& key, virtual Status AddUserKey(const Slice& key,
...@@ -2950,203 +3131,120 @@ struct crocksdb_table_properties_collector_t : public TablePropertiesCollector { ...@@ -2950,203 +3131,120 @@ 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 {
rep_->add_userkey(rep_, add_userkey_(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();
} }
virtual Status Finish(UserCollectedProperties* inner) override { virtual Status Finish(UserCollectedProperties* rep) override {
crocksdb_user_collected_properties_t props(inner); crocksdb_user_collected_properties_t props;
rep_->finish(rep_, &props); props.rep_ = rep;
finish_(state_, &props);
return Status::OK(); return Status::OK();
} }
virtual UserCollectedProperties GetReadableProperties() const override { virtual UserCollectedProperties GetReadableProperties() const override {
UserCollectedProperties inner; // Seems rocksdb will not return the readable properties and we don't need them too.
crocksdb_user_collected_properties_t props(&inner); UserCollectedProperties props;
rep_->readable_properties(rep_, &props); return props;
return inner;
} }
const char* Name() const override { const char* Name() const override {
return rep_->name(rep_); return name_(state_);
} }
}; };
struct crocksdb_table_properties_collector_factory_t : public TablePropertiesCollectorFactory { crocksdb_table_properties_collector_t*
crocksdb_table_properties_collector_factory_context_t* rep_; crocksdb_table_properties_collector_create(
void* state,
const char* (*name)(void*),
void (*destruct)(void*),
void (*add_userkey)(void*,
const char* key, size_t key_len,
const char* value, size_t value_len,
int entry_type, uint64_t seq, uint64_t file_size),
void (*finish)(void*, crocksdb_user_collected_properties_t* props)) {
auto c = new crocksdb_table_properties_collector_t;
c->state_ = state;
c->name_ = name;
c->destruct_ = destruct;
c->add_userkey_ = add_userkey;
c->finish_ = finish;
return c;
}
void crocksdb_table_properties_collector_destroy(crocksdb_table_properties_collector_t* c) {
delete c;
}
/* Table Properties Collector Factory */
crocksdb_table_properties_collector_factory_t( struct crocksdb_table_properties_collector_factory_t : public TablePropertiesCollectorFactory {
crocksdb_table_properties_collector_factory_context_t* context) : rep_(context) {} void* state_;
const char* (*name_)(void*);
void (*destruct_)(void*);
crocksdb_table_properties_collector_t*
(*create_table_properties_collector_)(void*, uint32_t cf);
virtual ~crocksdb_table_properties_collector_factory_t() { virtual ~crocksdb_table_properties_collector_factory_t() {
rep_->destructor(rep_); destruct_(state_);
} }
virtual TablePropertiesCollector* CreateTablePropertiesCollector ( virtual TablePropertiesCollector* CreateTablePropertiesCollector (
TablePropertiesCollectorFactory::Context ctx) override { TablePropertiesCollectorFactory::Context ctx) override {
auto context = rep_->create_table_properties_collector(rep_, ctx.column_family_id); return create_table_properties_collector_(state_, ctx.column_family_id);
return new crocksdb_table_properties_collector_t(context);
} }
const char* Name() const override { const char* Name() const override {
return rep_->name(rep_); return name_(state_);
} }
}; };
crocksdb_table_properties_collector_factory_t* crocksdb_table_properties_collector_factory_t*
crocksdb_table_properties_collector_factory_create( crocksdb_table_properties_collector_factory_create(
crocksdb_table_properties_collector_factory_context_t* context) { void* state,
return new crocksdb_table_properties_collector_factory_t(context); const char* (*name)(void*),
void (*destruct)(void*),
crocksdb_table_properties_collector_t*
(*create_table_properties_collector)(void*, uint32_t cf)) {
auto f = new crocksdb_table_properties_collector_factory_t;
f->state_ = state;
f->name_ = name;
f->destruct_ = destruct;
f->create_table_properties_collector_ = create_table_properties_collector;
return f;
} }
void crocksdb_table_properties_collector_factory_destroy( void crocksdb_table_properties_collector_factory_destroy(
crocksdb_table_properties_collector_factory_t* factory) { crocksdb_table_properties_collector_factory_t* f) {
delete factory; delete f;
} }
void crocksdb_options_add_table_properties_collector_factory( void crocksdb_options_add_table_properties_collector_factory(
crocksdb_options_t* opt, crocksdb_table_properties_collector_factory_t* factory) { crocksdb_options_t* opt, crocksdb_table_properties_collector_factory_t* f) {
opt->rep.table_properties_collector_factories.push_back( opt->rep.table_properties_collector_factories.push_back(
std::shared_ptr<TablePropertiesCollectorFactory>(factory)); std::shared_ptr<TablePropertiesCollectorFactory>(f));
} }
struct crocksdb_shallow_strings_map_t { /* Get Table Properties */
size_t size = 0;
const char** keys = nullptr;
size_t* keys_lens = nullptr;
const char** values = nullptr;
size_t* values_lens = nullptr;
~crocksdb_shallow_strings_map_t() {
delete[] keys;
delete[] keys_lens;
delete[] values;
delete[] values_lens;
}
void assign(const std::map<std::string, std::string>& items) {
size = items.size();
keys = new const char* [size];
keys_lens = new size_t [size];
values = new const char* [size];
values_lens = new size_t [size];
size_t i = 0;
for (auto it = items.begin(); it != items.end(); it++, i++) {
keys[i] = it->first.data();
keys_lens[i] = it->first.size();
values[i] = it->second.data();
values_lens[i] = it->second.size();
}
}
};
struct crocksdb_table_properties_t {
uint64_t data_size;
uint64_t index_size;
uint64_t filter_size;
uint64_t raw_key_size;
uint64_t raw_value_size;
uint64_t num_data_blocks;
uint64_t num_entries;
uint64_t format_version;
uint64_t fixed_key_len;
uint64_t column_family_id;
const char* column_family_name;
const char* filter_policy_name;
const char* comparator_name;
const char* merge_operator_name;
const char* prefix_extractor_name;
const char* property_collectors_names;
const char* compression_name;
crocksdb_shallow_strings_map_t user_collected_properties;
crocksdb_shallow_strings_map_t readable_properties;
void FromProperties(const std::shared_ptr<const TableProperties> props) {
data_size = props->data_size;
index_size = props->index_size;
filter_size = props->filter_size;
raw_key_size = props->raw_key_size;
raw_value_size = props->raw_value_size;
num_data_blocks = props->num_data_blocks;
num_entries = props->num_entries;
format_version = props->format_version;
fixed_key_len = props->fixed_key_len;
column_family_id = props->column_family_id;
column_family_name = props->column_family_name.c_str();
filter_policy_name = props->filter_policy_name.c_str();
comparator_name = props->comparator_name.c_str();
merge_operator_name = props->merge_operator_name.c_str();
prefix_extractor_name = props->prefix_extractor_name.c_str();
property_collectors_names = props->property_collectors_names.c_str();
compression_name = props->compression_name.c_str();
user_collected_properties.assign(props->user_collected_properties);
readable_properties.assign(props->readable_properties);
}
};
struct crocksdb_table_properties_collection_t {
TablePropertiesCollection* inner = nullptr;
size_t size = 0;
const char** keys = nullptr;
crocksdb_table_properties_t* values = nullptr;
~crocksdb_table_properties_collection_t() {
delete inner;
delete[] keys;
delete[] values;
}
void FromProperties(TablePropertiesCollection* props) {
inner = props;
size = props->size();
keys = new const char* [size];
values = new crocksdb_table_properties_t [size];
size_t i = 0;
for (auto it = props->begin(); it != props->end(); it++, i++) {
keys[i] = it->first.c_str();
values[i].FromProperties(it->second);
}
}
};
crocksdb_table_properties_collection_t*
crocksdb_table_properties_collection_create() {
return new crocksdb_table_properties_collection_t;
}
void crocksdb_table_properties_collection_destroy(
crocksdb_table_properties_collection_t* collection) {
delete collection;
}
void crocksdb_get_properties_of_all_tables(crocksdb_t* db, void crocksdb_get_properties_of_all_tables(crocksdb_t* db,
crocksdb_table_properties_collection_t* collection, char** errptr) { crocksdb_table_properties_collection_t* props, char** errptr) {
auto props = std::unique_ptr<TablePropertiesCollection>(new TablePropertiesCollection); auto s = db->rep->GetPropertiesOfAllTables(props->rep_);
auto s = db->rep->GetPropertiesOfAllTables(props.get());
if (!s.ok()) { if (!s.ok()) {
SaveError(errptr, s); SaveError(errptr, s);
return;
} }
collection->FromProperties(props.release());
} }
void crocksdb_get_properties_of_all_tables_cf( void crocksdb_get_properties_of_all_tables_cf(
crocksdb_t* db, crocksdb_column_family_handle_t* cf, crocksdb_t* db, crocksdb_column_family_handle_t* cf,
crocksdb_table_properties_collection_t* collection, char** errptr) { crocksdb_table_properties_collection_t* props, char** errptr) {
auto props = std::unique_ptr<TablePropertiesCollection>(new TablePropertiesCollection); auto s = db->rep->GetPropertiesOfAllTables(cf->rep, props->rep_);
auto s = db->rep->GetPropertiesOfAllTables(cf->rep, props.get());
if (!s.ok()) { if (!s.ok()) {
SaveError(errptr, s); SaveError(errptr, s);
return;
} }
collection->FromProperties(props.release());
} }
void crocksdb_get_properties_of_tables_in_range( void crocksdb_get_properties_of_tables_in_range(
...@@ -3154,19 +3252,16 @@ void crocksdb_get_properties_of_tables_in_range( ...@@ -3154,19 +3252,16 @@ void crocksdb_get_properties_of_tables_in_range(
int num_ranges, int num_ranges,
const char* const* start_keys, const size_t* start_keys_lens, const char* const* start_keys, const size_t* start_keys_lens,
const char* const* limit_keys, const size_t* limit_keys_lens, const char* const* limit_keys, const size_t* limit_keys_lens,
crocksdb_table_properties_collection_t* collection, char** errptr) { crocksdb_table_properties_collection_t* props, char** errptr) {
std::vector<Range> ranges; std::vector<Range> ranges;
for (int i = 0; i < num_ranges; i++) { for (int i = 0; i < num_ranges; i++) {
ranges.emplace_back(Range(Slice(start_keys[i], start_keys_lens[i]), ranges.emplace_back(Range(Slice(start_keys[i], start_keys_lens[i]),
Slice(limit_keys[i], limit_keys_lens[i]))); Slice(limit_keys[i], limit_keys_lens[i])));
} }
auto props = std::unique_ptr<TablePropertiesCollection>(new TablePropertiesCollection); auto s = db->rep->GetPropertiesOfTablesInRange(cf->rep, &ranges[0], ranges.size(), props->rep_);
auto s = db->rep->GetPropertiesOfTablesInRange(cf->rep, &ranges[0], ranges.size(), props.get());
if (!s.ok()) { if (!s.ok()) {
SaveError(errptr, s); SaveError(errptr, s);
return;
} }
collection->FromProperties(props.release());
} }
} // end extern "C" } // end extern "C"
...@@ -113,30 +113,37 @@ typedef struct crocksdb_ratelimiter_t crocksdb_ratelimiter_t; ...@@ -113,30 +113,37 @@ typedef struct crocksdb_ratelimiter_t crocksdb_ratelimiter_t;
typedef struct crocksdb_pinnableslice_t crocksdb_pinnableslice_t; typedef struct crocksdb_pinnableslice_t crocksdb_pinnableslice_t;
typedef struct crocksdb_user_collected_properties_t typedef struct crocksdb_user_collected_properties_t
crocksdb_user_collected_properties_t; crocksdb_user_collected_properties_t;
typedef struct crocksdb_user_collected_properties_iterator_t
crocksdb_user_collected_properties_iterator_t;
typedef struct crocksdb_table_properties_t crocksdb_table_properties_t;
typedef struct crocksdb_table_properties_collection_t typedef struct crocksdb_table_properties_collection_t
crocksdb_table_properties_collection_t; crocksdb_table_properties_collection_t;
typedef struct crocksdb_table_properties_collection_iterator_t
crocksdb_table_properties_collection_iterator_t;
typedef struct crocksdb_table_properties_collector_t
crocksdb_table_properties_collector_t;
typedef struct crocksdb_table_properties_collector_factory_t typedef struct crocksdb_table_properties_collector_factory_t
crocksdb_table_properties_collector_factory_t; crocksdb_table_properties_collector_factory_t;
typedef struct crocksdb_table_properties_collector_context_t { typedef enum crocksdb_table_property_t {
void* collector; DATA_SIZE = 1,
const char* (*name)(void*); INDEX_SIZE,
void (*destructor)(void*); FILTER_SIZE,
void (*add_userkey)(void*, RAW_KEY_SIZE,
const char* key, size_t key_len, RAW_VALUE_SIZE,
const char* value, size_t value_len, NUM_DATA_BLOCKS,
int entry_type, uint64_t seq, uint64_t file_size); NUM_ENTRIES,
void (*finish)(void*, crocksdb_user_collected_properties_t* props); FORMAT_VERSION,
void (*readable_properties)(void*, crocksdb_user_collected_properties_t* props); FIXED_KEY_LEN,
} crocksdb_table_properties_collector_context_t; COLUMN_FAMILY_ID,
COLUMN_FAMILY_NAME = 11,
typedef struct crocksdb_table_properties_collector_factory_context_t { FILTER_POLICY_NAME,
void* factory; COMPARATOR_NAME,
const char* (*name)(void*); MERGE_OPERATOR_NAME,
void (*destructor)(void*); PREFIX_EXTRACTOR_NAME,
crocksdb_table_properties_collector_context_t* PROPERTY_COLLECTORS_NAMES,
(*create_table_properties_collector)(void*, uint32_t cf); COMPRESSION_NAME,
} crocksdb_table_properties_collector_factory_context_t; } crocksdb_table_property_t;
/* DB operations */ /* DB operations */
...@@ -224,6 +231,9 @@ crocksdb_create_column_family(crocksdb_t* db, ...@@ -224,6 +231,9 @@ crocksdb_create_column_family(crocksdb_t* db,
extern C_ROCKSDB_LIBRARY_API void crocksdb_drop_column_family( extern C_ROCKSDB_LIBRARY_API void crocksdb_drop_column_family(
crocksdb_t* db, crocksdb_column_family_handle_t* handle, char** errptr); crocksdb_t* db, crocksdb_column_family_handle_t* handle, char** errptr);
extern C_ROCKSDB_LIBRARY_API uint32_t crocksdb_column_family_handle_get_id(
crocksdb_column_family_handle_t*);
extern C_ROCKSDB_LIBRARY_API void crocksdb_column_family_handle_destroy( extern C_ROCKSDB_LIBRARY_API void crocksdb_column_family_handle_destroy(
crocksdb_column_family_handle_t*); crocksdb_column_family_handle_t*);
...@@ -1203,20 +1213,45 @@ extern C_ROCKSDB_LIBRARY_API const char* crocksdb_pinnableslice_value( ...@@ -1203,20 +1213,45 @@ extern C_ROCKSDB_LIBRARY_API const char* crocksdb_pinnableslice_value(
extern C_ROCKSDB_LIBRARY_API void extern C_ROCKSDB_LIBRARY_API void
crocksdb_user_collected_properties_add( crocksdb_user_collected_properties_add(
crocksdb_user_collected_properties_t* props, crocksdb_user_collected_properties_t*,
const char* key, size_t key_len, const char* value, size_t value_len); const char* key, size_t key_len, const char* value, size_t value_len);
extern C_ROCKSDB_LIBRARY_API crocksdb_table_properties_collector_factory_t* extern C_ROCKSDB_LIBRARY_API crocksdb_user_collected_properties_iterator_t*
crocksdb_table_properties_collector_factory_create( crocksdb_user_collected_properties_iter_create(
crocksdb_table_properties_collector_factory_context_t* context); crocksdb_user_collected_properties_t*);
extern C_ROCKSDB_LIBRARY_API void extern C_ROCKSDB_LIBRARY_API void
crocksdb_table_properties_collector_factory_destroy( crocksdb_user_collected_properties_iter_destroy(
crocksdb_table_properties_collector_factory_t* factory); crocksdb_user_collected_properties_iterator_t*);
extern C_ROCKSDB_LIBRARY_API unsigned char
crocksdb_user_collected_properties_iter_valid(
crocksdb_user_collected_properties_iterator_t*);
extern C_ROCKSDB_LIBRARY_API void extern C_ROCKSDB_LIBRARY_API void
crocksdb_options_add_table_properties_collector_factory( crocksdb_user_collected_properties_iter_next(
crocksdb_options_t* opt, crocksdb_table_properties_collector_factory_t* factory); crocksdb_user_collected_properties_iterator_t*);
extern C_ROCKSDB_LIBRARY_API const char*
crocksdb_user_collected_properties_iter_key(
crocksdb_user_collected_properties_iterator_t*, size_t* klen);
extern C_ROCKSDB_LIBRARY_API const char*
crocksdb_user_collected_properties_iter_value(
crocksdb_user_collected_properties_iterator_t*, size_t* vlen);
extern C_ROCKSDB_LIBRARY_API uint64_t
crocksdb_table_properties_get_u64(crocksdb_table_properties_t*,
crocksdb_table_property_t prop);
extern C_ROCKSDB_LIBRARY_API const char*
crocksdb_table_properties_get_str(crocksdb_table_properties_t*,
crocksdb_table_property_t prop, size_t* slen);
extern C_ROCKSDB_LIBRARY_API crocksdb_user_collected_properties_t*
crocksdb_table_properties_get_user_properties(crocksdb_table_properties_t*);
/* Table Properties Collection */
extern C_ROCKSDB_LIBRARY_API crocksdb_table_properties_collection_t* extern C_ROCKSDB_LIBRARY_API crocksdb_table_properties_collection_t*
crocksdb_table_properties_collection_create(); crocksdb_table_properties_collection_create();
...@@ -1224,6 +1259,66 @@ crocksdb_table_properties_collection_create(); ...@@ -1224,6 +1259,66 @@ crocksdb_table_properties_collection_create();
extern C_ROCKSDB_LIBRARY_API void extern C_ROCKSDB_LIBRARY_API void
crocksdb_table_properties_collection_destroy(crocksdb_table_properties_collection_t*); crocksdb_table_properties_collection_destroy(crocksdb_table_properties_collection_t*);
extern C_ROCKSDB_LIBRARY_API crocksdb_table_properties_collection_iterator_t*
crocksdb_table_properties_collection_iter_create(
crocksdb_table_properties_collection_t*);
extern C_ROCKSDB_LIBRARY_API void
crocksdb_table_properties_collection_iter_destroy(
crocksdb_table_properties_collection_iterator_t*);
extern C_ROCKSDB_LIBRARY_API unsigned char
crocksdb_table_properties_collection_iter_valid(
crocksdb_table_properties_collection_iterator_t*);
extern C_ROCKSDB_LIBRARY_API void
crocksdb_table_properties_collection_iter_next(
crocksdb_table_properties_collection_iterator_t*);
extern C_ROCKSDB_LIBRARY_API const char*
crocksdb_table_properties_collection_iter_key(
crocksdb_table_properties_collection_iterator_t*, size_t* klen);
extern C_ROCKSDB_LIBRARY_API crocksdb_table_properties_t*
crocksdb_table_properties_collection_iter_value(
crocksdb_table_properties_collection_iterator_t*);
/* Table Properties Collector */
extern C_ROCKSDB_LIBRARY_API crocksdb_table_properties_collector_t*
crocksdb_table_properties_collector_create(
void* state,
const char* (*name)(void*),
void (*destruct)(void*),
void (*add_userkey)(void*,
const char* key, size_t key_len,
const char* value, size_t value_len,
int entry_type, uint64_t seq, uint64_t file_size),
void (*finish)(void*, crocksdb_user_collected_properties_t* props));
extern C_ROCKSDB_LIBRARY_API void
crocksdb_table_properties_collector_destroy(crocksdb_table_properties_collector_t*);
/* Table Properties Collector Factory */
extern C_ROCKSDB_LIBRARY_API crocksdb_table_properties_collector_factory_t*
crocksdb_table_properties_collector_factory_create(
void* state,
const char* (*name)(void*),
void (*destruct)(void*),
crocksdb_table_properties_collector_t*
(*create_table_properties_collector)(void*, uint32_t cf));
extern C_ROCKSDB_LIBRARY_API void
crocksdb_table_properties_collector_factory_destroy(
crocksdb_table_properties_collector_factory_t*);
extern C_ROCKSDB_LIBRARY_API void
crocksdb_options_add_table_properties_collector_factory(
crocksdb_options_t* opt, crocksdb_table_properties_collector_factory_t* f);
/* Get Table Properties */
extern C_ROCKSDB_LIBRARY_API void extern C_ROCKSDB_LIBRARY_API void
crocksdb_get_propeties_of_all_tables(crocksdb_t* db, crocksdb_get_propeties_of_all_tables(crocksdb_t* db,
crocksdb_table_properties_collection_t* props, char** errptr); crocksdb_table_properties_collection_t* props, char** errptr);
......
...@@ -18,9 +18,7 @@ extern crate libc; ...@@ -18,9 +18,7 @@ extern crate libc;
extern crate tempdir; extern crate tempdir;
use libc::{c_char, c_uchar, c_int, c_void, size_t, uint8_t, uint32_t, uint64_t, c_double}; use libc::{c_char, c_uchar, c_int, c_void, size_t, uint8_t, uint32_t, uint64_t, c_double};
use std::collections::HashMap;
use std::ffi::CStr; use std::ffi::CStr;
use std::slice;
pub enum DBOptions {} pub enum DBOptions {}
pub enum DBInstance {} pub enum DBInstance {}
...@@ -48,6 +46,11 @@ pub enum DBLogger {} ...@@ -48,6 +46,11 @@ pub enum DBLogger {}
pub enum DBCompactOptions {} pub enum DBCompactOptions {}
pub enum DBPinnableSlice {} pub enum DBPinnableSlice {}
pub enum DBUserCollectedProperties {} pub enum DBUserCollectedProperties {}
pub enum DBUserCollectedPropertiesIterator {}
pub enum DBTableProperties {}
pub enum DBTablePropertiesCollection {}
pub enum DBTablePropertiesCollectionIterator {}
pub enum DBTablePropertiesCollector {}
pub enum DBTablePropertiesCollectorFactory {} pub enum DBTablePropertiesCollectorFactory {}
pub fn new_bloom_filter(bits: c_int) -> *mut DBFilterPolicy { pub fn new_bloom_filter(bits: c_int) -> *mut DBFilterPolicy {
...@@ -61,11 +64,11 @@ pub fn new_cache(capacity: size_t) -> *mut DBCache { ...@@ -61,11 +64,11 @@ pub fn new_cache(capacity: size_t) -> *mut DBCache {
#[repr(C)] #[repr(C)]
#[derive(PartialEq, Eq, Clone, Copy)] #[derive(PartialEq, Eq, Clone, Copy)]
pub enum DBEntryType { pub enum DBEntryType {
DBPut = 0, Put = 0,
DBDelete = 1, Delete = 1,
DBSingleDelete = 2, SingleDelete = 2,
DBMerge = 3, Merge = 3,
DBOther = 4, Other = 4,
} }
#[derive(Copy, Clone)] #[derive(Copy, Clone)]
...@@ -184,112 +187,26 @@ pub enum DBInfoLogLevel { ...@@ -184,112 +187,26 @@ pub enum DBInfoLogLevel {
DBNumInfoLog = 6, DBNumInfoLog = 6,
} }
#[derive(Copy, Clone)]
#[repr(C)] #[repr(C)]
pub struct CShallowStringsMap { pub enum DBTableProperty {
pub size: size_t, DataSize = 1,
pub keys: *const *const c_char, IndexSize,
pub keys_lens: *const size_t, FilterSize,
pub values: *const *const c_char, RawKeySize,
pub values_lens: *const size_t, RawValueSize,
} NumDataBlocks,
NumEntries,
impl CShallowStringsMap { FormatVersion,
pub fn to_bytes_map(&self) -> Result<HashMap<Vec<u8>, Vec<u8>>, String> { FixedKeyLen,
let mut res = HashMap::new(); ColumnFamilyId,
unsafe { ColumnFamilyName = 11,
let keys = slice::from_raw_parts(self.keys, self.size); FilterPolicyName,
let keys_lens = slice::from_raw_parts(self.keys_lens, self.size); ComparatorName,
let values = slice::from_raw_parts(self.values, self.size); MergeOperatorName,
let values_lens = slice::from_raw_parts(self.values_lens, self.size); PrefixExtractorName,
for ((k, klen), (v, vlen)) in keys.iter() PropertyCollectorsNames,
.zip(keys_lens.iter()) CompressionName,
.zip(values.iter().zip(values_lens.iter())) {
let k = slice::from_raw_parts(*k as *const u8, *klen);
let v = slice::from_raw_parts(*v as *const u8, *vlen);
res.insert(k.to_owned(), v.to_owned());
}
}
Ok(res)
}
pub fn to_strings_map(&self) -> Result<HashMap<String, String>, String> {
let mut res = HashMap::new();
unsafe {
let keys = slice::from_raw_parts(self.keys, self.size);
let values = slice::from_raw_parts(self.values, self.size);
for i in 0..self.size {
let k = try!(ptr_to_string(keys[i]));
let v = try!(ptr_to_string(values[i]));
res.insert(k, v);
}
}
Ok(res)
}
}
#[repr(C)]
pub struct DBTableProperties {
pub data_size: uint64_t,
pub index_size: uint64_t,
pub filter_size: uint64_t,
pub raw_key_size: uint64_t,
pub raw_value_size: uint64_t,
pub num_data_blocks: uint64_t,
pub num_entries: uint64_t,
pub format_version: uint64_t,
pub fixed_key_len: uint64_t,
pub column_family_id: uint64_t,
pub column_family_name: *const c_char,
pub filter_policy_name: *const c_char,
pub comparator_name: *const c_char,
pub merge_operator_name: *const c_char,
pub prefix_extractor_name: *const c_char,
pub property_collectors_names: *const c_char,
pub compression_name: *const c_char,
pub user_collected_properties: CShallowStringsMap,
pub readable_properties: CShallowStringsMap,
}
#[repr(C)]
pub struct DBTablePropertiesCollection {
pub inner: *mut c_void,
pub size: size_t,
pub keys: *const *const c_char,
pub values: *const DBTableProperties,
}
#[repr(C)]
pub struct DBTablePropertiesCollectorContext {
pub collector: *mut c_void,
pub name: extern "C" fn(*mut c_void) -> *const c_char,
pub destructor: extern "C" fn(*mut c_void),
pub add_userkey: extern "C" fn(*mut c_void,
*const uint8_t,
size_t,
*const uint8_t,
size_t,
c_int,
uint64_t,
uint64_t),
pub finish: extern "C" fn(*mut c_void, *mut c_void),
pub readable_properties: extern "C" fn(*mut c_void, *mut c_void),
}
#[repr(C)]
pub struct DBTablePropertiesCollectorFactoryContext {
pub factory: *mut c_void,
pub name: extern "C" fn(*mut c_void) -> *const c_char,
pub destructor: extern "C" fn(*mut c_void),
pub create_table_properties_collector: extern "C" fn(*mut c_void, uint32_t) -> *mut c_void,
}
pub fn ptr_to_string(ptr: *const c_char) -> Result<String, String> {
unsafe {
match CStr::from_ptr(ptr).to_str() {
Ok(s) => Ok(s.to_owned()),
Err(e) => Err(format!("{}", e)),
}
}
} }
pub fn error_message(ptr: *mut c_char) -> String { pub fn error_message(ptr: *mut c_char) -> String {
...@@ -719,6 +636,7 @@ extern "C" { ...@@ -719,6 +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_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,
...@@ -929,25 +847,99 @@ extern "C" { ...@@ -929,25 +847,99 @@ extern "C" {
-> *const u8; -> *const u8;
pub fn crocksdb_pinnableslice_destroy(v: *mut DBPinnableSlice); pub fn crocksdb_pinnableslice_destroy(v: *mut DBPinnableSlice);
pub fn crocksdb_user_collected_properties_add(props: *mut c_void, pub fn crocksdb_user_collected_properties_add(props: *mut DBUserCollectedProperties,
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);
pub fn crocksdb_user_collected_properties_iter_create
(props: *mut 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;
pub fn crocksdb_user_collected_properties_iter_next
(it: *mut DBUserCollectedPropertiesIterator);
pub fn crocksdb_user_collected_properties_iter_key(it: *mut 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;
pub fn crocksdb_table_properties_get_u64(props: *mut DBTableProperties,
prop: DBTableProperty)
-> uint64_t;
pub fn crocksdb_table_properties_get_str(props: *mut 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_collection_create() -> *mut DBTablePropertiesCollection;
pub fn crocksdb_table_properties_collection_destroy(props: *mut DBTablePropertiesCollection);
pub fn crocksdb_table_properties_collection_iter_create
(props: *mut 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;
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;
pub fn crocksdb_table_properties_collection_iter_value
(it: *mut DBTablePropertiesCollectionIterator) -> *mut DBTableProperties;
pub fn crocksdb_table_properties_collector_create(state: *mut c_void,
name: extern "C" fn(*mut c_void)
-> *const c_char,
destruct: extern "C" fn(*mut c_void),
add_userkey: extern "C" fn(*mut c_void,
*const uint8_t,
size_t,
*const uint8_t,
size_t,
c_int,
uint64_t,
uint64_t),
finish: extern "C" fn(*mut c_void,
*mut DBUserCollectedProperties))
-> *mut DBTablePropertiesCollector;
pub fn crocksdb_table_properties_collector_destroy(c: *mut DBTablePropertiesCollector);
pub fn crocksdb_table_properties_collector_factory_create pub fn crocksdb_table_properties_collector_factory_create
(context: *mut DBTablePropertiesCollectorFactoryContext) (state: *mut c_void,
name: extern "C" fn(*mut c_void) -> *const c_char,
destruct: extern "C" fn(*mut c_void),
create_table_properties_collector: extern "C" fn(*mut c_void, uint32_t)
-> *mut DBTablePropertiesCollector)
-> *mut DBTablePropertiesCollectorFactory; -> *mut DBTablePropertiesCollectorFactory;
pub fn crocksdb_table_properties_collector_factory_destroy( pub fn crocksdb_table_properties_collector_factory_destroy(
factory: *mut DBTablePropertiesCollectorFactory); f: *mut DBTablePropertiesCollectorFactory);
pub fn crocksdb_options_add_table_properties_collector_factory( pub fn crocksdb_options_add_table_properties_collector_factory(
options: *mut DBOptions, factory: *mut DBTablePropertiesCollectorFactory); options: *mut DBOptions, f: *mut DBTablePropertiesCollectorFactory);
pub fn crocksdb_table_properties_collection_create() -> *mut DBTablePropertiesCollection;
pub fn crocksdb_table_properties_collection_destroy(c: *mut DBTablePropertiesCollection);
pub fn crocksdb_get_properties_of_all_tables(db: *mut DBInstance, pub fn crocksdb_get_properties_of_all_tables(db: *mut DBInstance,
props: *mut DBTablePropertiesCollection, props: *mut DBTablePropertiesCollection,
...@@ -967,6 +959,7 @@ extern "C" { ...@@ -967,6 +959,7 @@ extern "C" {
limit_keys_lens: *const size_t, limit_keys_lens: *const size_t,
props: *mut DBTablePropertiesCollection, props: *mut DBTablePropertiesCollection,
errptr: *mut *mut c_char); errptr: *mut *mut c_char);
} }
#[cfg(test)] #[cfg(test)]
......
...@@ -34,6 +34,12 @@ pub struct CFHandle { ...@@ -34,6 +34,12 @@ pub struct CFHandle {
inner: *mut DBCFHandle, inner: *mut DBCFHandle,
} }
impl CFHandle {
fn get_id(&self) -> u32 {
unsafe { crocksdb_ffi::crocksdb_column_family_handle_get_id(self.inner) }
}
}
impl Drop for CFHandle { impl Drop for CFHandle {
fn drop(&mut self) { fn drop(&mut self) {
unsafe { unsafe {
......
...@@ -27,7 +27,7 @@ use slice_transform::{SliceTransform, new_slice_transform}; ...@@ -27,7 +27,7 @@ use slice_transform::{SliceTransform, new_slice_transform};
use std::ffi::{CStr, CString}; use std::ffi::{CStr, CString};
use std::mem; use std::mem;
use table_properties_collector_factory::{TablePropertiesCollectorFactory, use table_properties_collector_factory::{TablePropertiesCollectorFactory,
new_table_properties_collector_factory_context}; new_table_properties_collector_factory};
#[derive(Default, Debug)] #[derive(Default, Debug)]
pub struct HistogramData { pub struct HistogramData {
...@@ -384,8 +384,7 @@ impl Options { ...@@ -384,8 +384,7 @@ impl Options {
pub fn add_table_properties_collector_factory(&mut self, pub fn add_table_properties_collector_factory(&mut self,
factory: Box<TablePropertiesCollectorFactory>) { factory: Box<TablePropertiesCollectorFactory>) {
unsafe { unsafe {
let context = Box::into_raw(new_table_properties_collector_factory_context(factory)); let f = new_table_properties_collector_factory(factory);
let f = crocksdb_ffi::crocksdb_table_properties_collector_factory_create(context);
crocksdb_ffi::crocksdb_options_add_table_properties_collector_factory(self.inner, f); crocksdb_ffi::crocksdb_options_add_table_properties_collector_factory(self.inner, f);
} }
} }
......
...@@ -11,7 +11,10 @@ ...@@ -11,7 +11,10 @@
// See the License for the specific language governing permissions and // See the License for the specific language governing permissions and
// limitations under the License. // limitations under the License.
use crocksdb_ffi::{self, DBTablePropertiesCollection, ptr_to_string}; use crocksdb_ffi::{self, DBTableProperties, DBTableProperty, DBUserCollectedProperties,
DBUserCollectedPropertiesIterator, DBTablePropertiesCollection,
DBTablePropertiesCollectionIterator};
use libc::size_t;
use std::collections::HashMap; use std::collections::HashMap;
use std::slice; use std::slice;
...@@ -35,11 +38,39 @@ pub struct TableProperties { ...@@ -35,11 +38,39 @@ pub struct TableProperties {
pub property_collectors_names: String, pub property_collectors_names: String,
pub compression_name: String, pub compression_name: String,
pub user_collected_properties: HashMap<Vec<u8>, Vec<u8>>, pub user_collected_properties: HashMap<Vec<u8>, Vec<u8>>,
pub readable_properties: HashMap<String, String>,
} }
pub type TablePropertiesCollection = HashMap<String, TableProperties>; pub type TablePropertiesCollection = HashMap<String, TableProperties>;
pub struct TablePropertiesHandle {
pub inner: *mut DBTableProperties,
}
impl TablePropertiesHandle {
fn new(inner: *mut DBTableProperties) -> TablePropertiesHandle {
TablePropertiesHandle { inner: inner }
}
fn get_u64(&self, prop: DBTableProperty) -> u64 {
unsafe { crocksdb_ffi::crocksdb_table_properties_get_u64(self.inner, prop) }
}
fn get_str(&self, prop: DBTableProperty) -> Result<String, String> {
unsafe {
let mut slen: size_t = 0;
let s = crocksdb_ffi::crocksdb_table_properties_get_str(self.inner,
prop,
&mut slen as *mut size_t);
let bytes = slice::from_raw_parts(s, slen);
String::from_utf8(bytes.to_owned()).or_else(|e| Err(format!("{}", e)))
}
}
fn get_user_properties(&self) -> *mut DBUserCollectedProperties {
unsafe { crocksdb_ffi::crocksdb_table_properties_get_user_properties(self.inner) }
}
}
pub struct TablePropertiesCollectionHandle { pub struct TablePropertiesCollectionHandle {
pub inner: *mut DBTablePropertiesCollection, pub inner: *mut DBTablePropertiesCollection,
} }
...@@ -63,37 +94,142 @@ impl TablePropertiesCollectionHandle { ...@@ -63,37 +94,142 @@ impl TablePropertiesCollectionHandle {
pub fn normalize(&self) -> Result<TablePropertiesCollection, String> { pub fn normalize(&self) -> Result<TablePropertiesCollection, String> {
let mut collection = TablePropertiesCollection::new(); let mut collection = TablePropertiesCollection::new();
unsafe { let mut it = TablePropertiesCollectionIter::new(self.inner);
let ctx = &*(self.inner as *mut DBTablePropertiesCollection); while it.valid() {
let keys = slice::from_raw_parts(ctx.keys, ctx.size); let k = try!(it.key());
let values = slice::from_raw_parts(ctx.values, ctx.size); let v = TablePropertiesHandle::new(it.value());
for i in 0..ctx.size { let mut props = TableProperties {
let props = &values[i]; data_size: v.get_u64(DBTableProperty::DataSize),
let k = ptr_to_string(keys[i])?; index_size: v.get_u64(DBTableProperty::IndexSize),
let v = TableProperties { filter_size: v.get_u64(DBTableProperty::FilterSize),
data_size: props.data_size, raw_key_size: v.get_u64(DBTableProperty::RawKeySize),
index_size: props.index_size, raw_value_size: v.get_u64(DBTableProperty::RawValueSize),
filter_size: props.filter_size, num_data_blocks: v.get_u64(DBTableProperty::NumDataBlocks),
raw_key_size: props.raw_key_size, num_entries: v.get_u64(DBTableProperty::NumEntries),
raw_value_size: props.raw_value_size, format_version: v.get_u64(DBTableProperty::FormatVersion),
num_data_blocks: props.num_data_blocks, fixed_key_len: v.get_u64(DBTableProperty::FixedKeyLen),
num_entries: props.num_entries, column_family_id: v.get_u64(DBTableProperty::ColumnFamilyId),
format_version: props.format_version, column_family_name: try!(v.get_str(DBTableProperty::ColumnFamilyName)),
fixed_key_len: props.fixed_key_len, filter_policy_name: try!(v.get_str(DBTableProperty::FilterPolicyName)),
column_family_id: props.column_family_id, comparator_name: try!(v.get_str(DBTableProperty::ComparatorName)),
column_family_name: try!(ptr_to_string(props.column_family_name)), merge_operator_name: try!(v.get_str(DBTableProperty::MergeOperatorName)),
filter_policy_name: try!(ptr_to_string(props.filter_policy_name)), prefix_extractor_name: try!(v.get_str(DBTableProperty::PrefixExtractorName)),
comparator_name: try!(ptr_to_string(props.comparator_name)), property_collectors_names:
merge_operator_name: try!(ptr_to_string(props.merge_operator_name)), try!(v.get_str(DBTableProperty::PropertyCollectorsNames)),
prefix_extractor_name: try!(ptr_to_string(props.prefix_extractor_name)), compression_name: try!(v.get_str(DBTableProperty::CompressionName)),
property_collectors_names: try!(ptr_to_string(props.property_collectors_names)), user_collected_properties: HashMap::new(),
compression_name: try!(ptr_to_string(props.compression_name)),
user_collected_properties: try!(props.user_collected_properties.to_bytes_map()),
readable_properties: try!(props.readable_properties.to_strings_map()),
}; };
collection.insert(k, v); let mut user_it = UserCollectedPropertiesIter::new(v.get_user_properties());
while user_it.valid() {
{
let k = user_it.key();
let v = user_it.value();
props.user_collected_properties.insert(k.to_owned(), v.to_owned());
} }
user_it.next();
}
collection.insert(k, props);
it.next();
} }
Ok(collection) Ok(collection)
} }
} }
struct TablePropertiesCollectionIter {
pub inner: *mut DBTablePropertiesCollectionIterator,
}
impl Drop for TablePropertiesCollectionIter {
fn drop(&mut self) {
unsafe {
crocksdb_ffi::crocksdb_table_properties_collection_iter_destroy(self.inner);
}
}
}
impl TablePropertiesCollectionIter {
fn new(inner: *mut DBTablePropertiesCollection) -> TablePropertiesCollectionIter {
unsafe {
TablePropertiesCollectionIter {
inner: crocksdb_ffi::crocksdb_table_properties_collection_iter_create(inner),
}
}
}
fn valid(&self) -> bool {
unsafe { crocksdb_ffi::crocksdb_table_properties_collection_iter_valid(self.inner) }
}
fn next(&mut self) {
unsafe {
crocksdb_ffi::crocksdb_table_properties_collection_iter_next(self.inner);
}
}
fn key(&self) -> Result<String, String> {
unsafe {
let mut klen: size_t = 0;
let k =
crocksdb_ffi::crocksdb_table_properties_collection_iter_key(self.inner,
&mut klen as *mut size_t);
let bytes = slice::from_raw_parts(k, klen);
String::from_utf8(bytes.to_owned()).or_else(|e| Err(format!("{}", e)))
}
}
fn value(&self) -> *mut DBTableProperties {
unsafe { crocksdb_ffi::crocksdb_table_properties_collection_iter_value(self.inner) }
}
}
struct UserCollectedPropertiesIter {
pub inner: *mut DBUserCollectedPropertiesIterator,
}
impl Drop for UserCollectedPropertiesIter {
fn drop(&mut self) {
unsafe {
crocksdb_ffi::crocksdb_user_collected_properties_iter_destroy(self.inner);
}
}
}
impl UserCollectedPropertiesIter {
fn new(inner: *mut DBUserCollectedProperties) -> UserCollectedPropertiesIter {
unsafe {
UserCollectedPropertiesIter {
inner: crocksdb_ffi::crocksdb_user_collected_properties_iter_create(inner),
}
}
}
fn valid(&self) -> bool {
unsafe { crocksdb_ffi::crocksdb_user_collected_properties_iter_valid(self.inner) }
}
fn next(&mut self) {
unsafe {
crocksdb_ffi::crocksdb_user_collected_properties_iter_next(self.inner);
}
}
fn key(&self) -> &[u8] {
unsafe {
let mut klen: size_t = 0;
let k =
crocksdb_ffi::crocksdb_user_collected_properties_iter_key(self.inner,
&mut klen as *mut size_t);
slice::from_raw_parts(k, klen)
}
}
fn value(&self) -> &[u8] {
unsafe {
let mut vlen: size_t = 0;
let v =
crocksdb_ffi::crocksdb_user_collected_properties_iter_value(self.inner,
&mut vlen as *mut size_t);
slice::from_raw_parts(v, vlen)
}
}
}
...@@ -11,7 +11,7 @@ ...@@ -11,7 +11,7 @@
// See the License for the specific language governing permissions and // See the License for the specific language governing permissions and
// limitations under the License. // limitations under the License.
use crocksdb_ffi::{self, DBEntryType, DBTablePropertiesCollectorContext}; use crocksdb_ffi::{self, DBEntryType, DBUserCollectedProperties, DBTablePropertiesCollector};
use libc::{c_void, c_char, c_int, uint8_t, uint64_t, size_t}; use libc::{c_void, c_char, c_int, uint8_t, uint64_t, size_t};
use std::collections::HashMap; use std::collections::HashMap;
use std::mem; use std::mem;
...@@ -33,28 +33,23 @@ pub trait TablePropertiesCollector { ...@@ -33,28 +33,23 @@ pub trait TablePropertiesCollector {
/// Will be called when a table has already been built and is ready for /// Will be called when a table has already been built and is ready for
/// writing the properties block. /// writing the properties block.
fn finish(&mut self) -> HashMap<Vec<u8>, Vec<u8>>; fn finish(&mut self) -> HashMap<Vec<u8>, Vec<u8>>;
/// Return the human-readable properties, where the key is property name and
/// the value is the human-readable form of value.
fn readable_properties(&self) -> HashMap<String, String>;
} }
extern "C" fn name(context: *mut c_void) -> *const c_char { extern "C" fn name(collector: *mut c_void) -> *const c_char {
unsafe { unsafe {
let context = &mut *(context as *mut DBTablePropertiesCollectorContext); let collector = &mut *(collector as *mut Box<TablePropertiesCollector>);
let collector = &mut *(context.collector as *mut Box<TablePropertiesCollector>);
collector.name().as_ptr() as *const c_char collector.name().as_ptr() as *const c_char
} }
} }
extern "C" fn destructor(context: *mut c_void) { extern "C" fn destruct(collector: *mut c_void) {
unsafe { unsafe {
let context = Box::from_raw(context as *mut DBTablePropertiesCollectorContext); let collector = &mut *(collector as *mut Box<TablePropertiesCollector>);
Box::from_raw(context.collector as *mut Box<TablePropertiesCollector>); Box::from_raw(collector);
} }
} }
pub extern "C" fn add_userkey(context: *mut c_void, pub extern "C" fn add_userkey(collector: *mut c_void,
key: *const uint8_t, key: *const uint8_t,
key_len: size_t, key_len: size_t,
value: *const uint8_t, value: *const uint8_t,
...@@ -63,18 +58,16 @@ pub extern "C" fn add_userkey(context: *mut c_void, ...@@ -63,18 +58,16 @@ pub extern "C" fn add_userkey(context: *mut c_void,
_: uint64_t, _: uint64_t,
_: uint64_t) { _: uint64_t) {
unsafe { unsafe {
let context = &mut *(context as *mut DBTablePropertiesCollectorContext); let collector = &mut *(collector as *mut Box<TablePropertiesCollector>);
let collector = &mut *(context.collector as *mut Box<TablePropertiesCollector>);
let key = slice::from_raw_parts(key, key_len); let key = slice::from_raw_parts(key, key_len);
let value = slice::from_raw_parts(value, value_len); let value = slice::from_raw_parts(value, value_len);
collector.add_userkey(key, value, mem::transmute(entry_type)) collector.add_userkey(key, value, mem::transmute(entry_type));
} }
} }
pub extern "C" fn finish(context: *mut c_void, props: *mut c_void) { pub extern "C" fn finish(collector: *mut c_void, props: *mut DBUserCollectedProperties) {
unsafe { unsafe {
let context = &mut *(context as *mut DBTablePropertiesCollectorContext); let collector = &mut *(collector as *mut Box<TablePropertiesCollector>);
let collector = &mut *(context.collector as *mut Box<TablePropertiesCollector>);
for (key, value) in collector.finish() { for (key, value) in collector.finish() {
crocksdb_ffi::crocksdb_user_collected_properties_add(props, crocksdb_ffi::crocksdb_user_collected_properties_add(props,
key.as_ptr(), key.as_ptr(),
...@@ -85,28 +78,13 @@ pub extern "C" fn finish(context: *mut c_void, props: *mut c_void) { ...@@ -85,28 +78,13 @@ pub extern "C" fn finish(context: *mut c_void, props: *mut c_void) {
} }
} }
pub extern "C" fn readable_properties(context: *mut c_void, props: *mut c_void) { pub unsafe fn new_table_properties_collector(collector: Box<TablePropertiesCollector>)
unsafe { -> *mut DBTablePropertiesCollector {
let context = &mut *(context as *mut DBTablePropertiesCollectorContext); crocksdb_ffi::crocksdb_table_properties_collector_create(
let collector = &mut *(context.collector as *mut Box<TablePropertiesCollector>); Box::into_raw(Box::new(collector)) as *mut c_void,
for (key, value) in collector.readable_properties() { name,
crocksdb_ffi::crocksdb_user_collected_properties_add(props, destruct,
key.as_ptr(), add_userkey,
key.len(), finish,
value.as_ptr(), )
value.len());
}
}
}
pub unsafe fn new_table_properties_collector_context(collector: Box<TablePropertiesCollector>)
-> Box<DBTablePropertiesCollectorContext> {
Box::new(DBTablePropertiesCollectorContext {
collector: Box::into_raw(Box::new(collector)) as *mut c_void,
name: name,
destructor: destructor,
add_userkey: add_userkey,
finish: finish,
readable_properties: readable_properties,
})
} }
...@@ -11,9 +11,9 @@ ...@@ -11,9 +11,9 @@
// See the License for the specific language governing permissions and // See the License for the specific language governing permissions and
// limitations under the License. // limitations under the License.
use crocksdb_ffi::DBTablePropertiesCollectorFactoryContext; use crocksdb_ffi::{self, DBTablePropertiesCollector, DBTablePropertiesCollectorFactory};
use libc::{c_void, c_char, uint32_t}; use libc::{c_void, c_char, uint32_t};
use table_properties_collector::{TablePropertiesCollector, new_table_properties_collector_context}; use table_properties_collector::{TablePropertiesCollector, new_table_properties_collector};
/// Constructs `TablePropertiesCollector`. /// Constructs `TablePropertiesCollector`.
/// Internals create a new `TablePropertiesCollector` for each new table. /// Internals create a new `TablePropertiesCollector` for each new table.
...@@ -24,37 +24,36 @@ pub trait TablePropertiesCollectorFactory { ...@@ -24,37 +24,36 @@ pub trait TablePropertiesCollectorFactory {
fn create_table_properties_collector(&mut self, cf: u32) -> Box<TablePropertiesCollector>; fn create_table_properties_collector(&mut self, cf: u32) -> Box<TablePropertiesCollector>;
} }
extern "C" fn name(context: *mut c_void) -> *const c_char { extern "C" fn name(factory: *mut c_void) -> *const c_char {
unsafe { unsafe {
let context = &mut *(context as *mut DBTablePropertiesCollectorFactoryContext); let factory = &mut *(factory as *mut Box<TablePropertiesCollectorFactory>);
let factory = &mut *(context.factory as *mut Box<TablePropertiesCollectorFactory>);
factory.name().as_ptr() as *const c_char factory.name().as_ptr() as *const c_char
} }
} }
extern "C" fn destructor(context: *mut c_void) { extern "C" fn destruct(factory: *mut c_void) {
unsafe { unsafe {
let context = Box::from_raw(context as *mut DBTablePropertiesCollectorFactoryContext); Box::from_raw(factory as *mut Box<TablePropertiesCollectorFactory>);
Box::from_raw(context.factory as *mut Box<TablePropertiesCollectorFactory>);
} }
} }
extern "C" fn create_table_properties_collector(context: *mut c_void, cf: uint32_t) -> *mut c_void { extern "C" fn create_table_properties_collector(factory: *mut c_void,
cf: uint32_t)
-> *mut DBTablePropertiesCollector {
unsafe { unsafe {
let context = &mut *(context as *mut DBTablePropertiesCollectorFactoryContext); let factory = &mut *(factory as *mut Box<TablePropertiesCollectorFactory>);
let factory = &mut *(context.factory as *mut Box<TablePropertiesCollectorFactory>);
let collector = factory.create_table_properties_collector(cf); let collector = factory.create_table_properties_collector(cf);
Box::into_raw(new_table_properties_collector_context(collector)) as *mut c_void new_table_properties_collector(collector)
} }
} }
pub unsafe fn new_table_properties_collector_factory_context pub unsafe fn new_table_properties_collector_factory
(factory: Box<TablePropertiesCollectorFactory>) (factory: Box<TablePropertiesCollectorFactory>)
-> Box<DBTablePropertiesCollectorFactoryContext> { -> *mut DBTablePropertiesCollectorFactory {
Box::new(DBTablePropertiesCollectorFactoryContext { crocksdb_ffi::crocksdb_table_properties_collector_factory_create(
factory: Box::into_raw(Box::new(factory)) as *mut c_void, Box::into_raw(Box::new(factory)) as *mut c_void,
name: name, name,
destructor: destructor, destruct,
create_table_properties_collector: create_table_properties_collector, create_table_properties_collector,
}) )
} }
...@@ -106,26 +106,17 @@ impl TablePropertiesCollector for ExampleCollector { ...@@ -106,26 +106,17 @@ impl TablePropertiesCollector for ExampleCollector {
self.last_key.extend_from_slice(key); self.last_key.extend_from_slice(key);
} }
match entry_type { match entry_type {
DBEntryType::DBPut => self.num_puts += 1, DBEntryType::Put => self.num_puts += 1,
DBEntryType::DBMerge => self.num_merges += 1, DBEntryType::Merge => self.num_merges += 1,
DBEntryType::DBDelete | DBEntryType::Delete |
DBEntryType::DBSingleDelete => self.num_deletes += 1, DBEntryType::SingleDelete => self.num_deletes += 1,
DBEntryType::DBOther => {} DBEntryType::Other => {}
} }
} }
fn finish(&mut self) -> HashMap<Vec<u8>, Vec<u8>> { fn finish(&mut self) -> HashMap<Vec<u8>, Vec<u8>> {
self.encode() self.encode()
} }
fn readable_properties(&self) -> HashMap<String, String> {
let mut props = HashMap::new();
props.insert("num_keys".to_owned(), self.num_keys.to_string());
props.insert("num_puts".to_owned(), self.num_puts.to_string());
props.insert("num_merges".to_owned(), self.num_merges.to_string());
props.insert("num_deletes".to_owned(), self.num_deletes.to_string());
props
}
} }
struct ExampleFactory {} struct ExampleFactory {}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment