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(
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) {
delete handle->rep;
delete handle;
......@@ -2924,25 +2928,202 @@ const char* crocksdb_pinnableslice_value(const crocksdb_pinnableslice_t* v,
return v->rep.data();
}
/* Table Properties */
struct crocksdb_user_collected_properties_t {
UserCollectedProperties* inner;
crocksdb_user_collected_properties_t(UserCollectedProperties* props) : inner(props) {}
UserCollectedProperties* rep_ = nullptr;
};
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,
const char* key, size_t key_len,
const char* value, size_t value_len) {
props->inner->emplace(std::make_pair(std::string(key, key_len), std::string(value, value_len)));
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)));
}
struct crocksdb_table_properties_collector_t : public TablePropertiesCollector {
crocksdb_table_properties_collector_context_t* rep_;
crocksdb_user_collected_properties_iterator_t*
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 */
struct crocksdb_table_properties_collection_t {
TablePropertiesCollection* rep_ = nullptr;
crocksdb_table_properties_collector_t(
crocksdb_table_properties_collector_context_t* context) : rep_(context) {}
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() {
rep_->destructor(rep_);
destruct_(state_);
}
virtual Status AddUserKey(const Slice& key,
......@@ -2950,203 +3131,120 @@ struct crocksdb_table_properties_collector_t : public TablePropertiesCollector {
EntryType entry_type,
SequenceNumber seq,
uint64_t file_size) override {
rep_->add_userkey(rep_,
key.data(), key.size(),
value.data(), value.size(),
entry_type, seq, file_size);
add_userkey_(state_,
key.data(), key.size(),
value.data(), value.size(),
entry_type, seq, file_size);
return Status::OK();
}
virtual Status Finish(UserCollectedProperties* inner) override {
crocksdb_user_collected_properties_t props(inner);
rep_->finish(rep_, &props);
virtual Status Finish(UserCollectedProperties* rep) override {
crocksdb_user_collected_properties_t props;
props.rep_ = rep;
finish_(state_, &props);
return Status::OK();
}
virtual UserCollectedProperties GetReadableProperties() const override {
UserCollectedProperties inner;
crocksdb_user_collected_properties_t props(&inner);
rep_->readable_properties(rep_, &props);
return inner;
// Seems rocksdb will not return the readable properties and we don't need them too.
UserCollectedProperties props;
return props;
}
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_factory_context_t* rep_;
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)) {
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;
}
crocksdb_table_properties_collector_factory_t(
crocksdb_table_properties_collector_factory_context_t* context) : rep_(context) {}
/* Table Properties Collector Factory */
struct crocksdb_table_properties_collector_factory_t : public TablePropertiesCollectorFactory {
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() {
rep_->destructor(rep_);
destruct_(state_);
}
virtual TablePropertiesCollector* CreateTablePropertiesCollector (
TablePropertiesCollectorFactory::Context ctx) override {
auto context = rep_->create_table_properties_collector(rep_, ctx.column_family_id);
return new crocksdb_table_properties_collector_t(context);
return create_table_properties_collector_(state_, ctx.column_family_id);
}
const char* Name() const override {
return rep_->name(rep_);
return name_(state_);
}
};
crocksdb_table_properties_collector_factory_t*
crocksdb_table_properties_collector_factory_create(
crocksdb_table_properties_collector_factory_context_t* context) {
return new crocksdb_table_properties_collector_factory_t(context);
void* state,
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(
crocksdb_table_properties_collector_factory_t* factory) {
delete factory;
crocksdb_table_properties_collector_factory_t* f) {
delete f;
}
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(
std::shared_ptr<TablePropertiesCollectorFactory>(factory));
}
struct crocksdb_shallow_strings_map_t {
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;
std::shared_ptr<TablePropertiesCollectorFactory>(f));
}
void crocksdb_table_properties_collection_destroy(
crocksdb_table_properties_collection_t* collection) {
delete collection;
}
/* Get Table Properties */
void crocksdb_get_properties_of_all_tables(crocksdb_t* db,
crocksdb_table_properties_collection_t* collection, char** errptr) {
auto props = std::unique_ptr<TablePropertiesCollection>(new TablePropertiesCollection);
auto s = db->rep->GetPropertiesOfAllTables(props.get());
crocksdb_table_properties_collection_t* props, char** errptr) {
auto s = db->rep->GetPropertiesOfAllTables(props->rep_);
if (!s.ok()) {
SaveError(errptr, s);
return;
}
collection->FromProperties(props.release());
}
void crocksdb_get_properties_of_all_tables_cf(
crocksdb_t* db, crocksdb_column_family_handle_t* cf,
crocksdb_table_properties_collection_t* collection, char** errptr) {
auto props = std::unique_ptr<TablePropertiesCollection>(new TablePropertiesCollection);
auto s = db->rep->GetPropertiesOfAllTables(cf->rep, props.get());
crocksdb_table_properties_collection_t* props, char** errptr) {
auto s = db->rep->GetPropertiesOfAllTables(cf->rep, props->rep_);
if (!s.ok()) {
SaveError(errptr, s);
return;
}
collection->FromProperties(props.release());
}
void crocksdb_get_properties_of_tables_in_range(
......@@ -3154,19 +3252,16 @@ void crocksdb_get_properties_of_tables_in_range(
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* collection, char** errptr) {
crocksdb_table_properties_collection_t* props, 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])));
}
auto props = std::unique_ptr<TablePropertiesCollection>(new TablePropertiesCollection);
auto s = db->rep->GetPropertiesOfTablesInRange(cf->rep, &ranges[0], ranges.size(), props.get());
auto s = db->rep->GetPropertiesOfTablesInRange(cf->rep, &ranges[0], ranges.size(), props->rep_);
if (!s.ok()) {
SaveError(errptr, s);
return;
}
collection->FromProperties(props.release());
}
} // end extern "C"
......@@ -113,30 +113,37 @@ typedef struct crocksdb_ratelimiter_t crocksdb_ratelimiter_t;
typedef struct crocksdb_pinnableslice_t crocksdb_pinnableslice_t;
typedef struct 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
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
crocksdb_table_properties_collector_factory_t;
typedef struct crocksdb_table_properties_collector_context_t {
void* collector;
const char* (*name)(void*);
void (*destructor)(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);
void (*readable_properties)(void*, crocksdb_user_collected_properties_t* props);
} crocksdb_table_properties_collector_context_t;
typedef struct crocksdb_table_properties_collector_factory_context_t {
void* factory;
const char* (*name)(void*);
void (*destructor)(void*);
crocksdb_table_properties_collector_context_t*
(*create_table_properties_collector)(void*, uint32_t cf);
} crocksdb_table_properties_collector_factory_context_t;
typedef enum crocksdb_table_property_t {
DATA_SIZE = 1,
INDEX_SIZE,
FILTER_SIZE,
RAW_KEY_SIZE,
RAW_VALUE_SIZE,
NUM_DATA_BLOCKS,
NUM_ENTRIES,
FORMAT_VERSION,
FIXED_KEY_LEN,
COLUMN_FAMILY_ID,
COLUMN_FAMILY_NAME = 11,
FILTER_POLICY_NAME,
COMPARATOR_NAME,
MERGE_OPERATOR_NAME,
PREFIX_EXTRACTOR_NAME,
PROPERTY_COLLECTORS_NAMES,
COMPRESSION_NAME,
} crocksdb_table_property_t;
/* DB operations */
......@@ -224,6 +231,9 @@ crocksdb_create_column_family(crocksdb_t* db,
extern C_ROCKSDB_LIBRARY_API void crocksdb_drop_column_family(
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(
crocksdb_column_family_handle_t*);
......@@ -1203,20 +1213,45 @@ extern C_ROCKSDB_LIBRARY_API const char* crocksdb_pinnableslice_value(
extern C_ROCKSDB_LIBRARY_API void
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);
extern C_ROCKSDB_LIBRARY_API crocksdb_table_properties_collector_factory_t*
crocksdb_table_properties_collector_factory_create(
crocksdb_table_properties_collector_factory_context_t* context);
extern C_ROCKSDB_LIBRARY_API crocksdb_user_collected_properties_iterator_t*
crocksdb_user_collected_properties_iter_create(
crocksdb_user_collected_properties_t*);
extern C_ROCKSDB_LIBRARY_API void
crocksdb_table_properties_collector_factory_destroy(
crocksdb_table_properties_collector_factory_t* factory);
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*);
extern C_ROCKSDB_LIBRARY_API void
crocksdb_options_add_table_properties_collector_factory(
crocksdb_options_t* opt, crocksdb_table_properties_collector_factory_t* factory);
crocksdb_user_collected_properties_iter_next(
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*
crocksdb_table_properties_collection_create();
......@@ -1224,6 +1259,66 @@ crocksdb_table_properties_collection_create();
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*);
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
crocksdb_get_propeties_of_all_tables(crocksdb_t* db,
crocksdb_table_properties_collection_t* props, char** errptr);
......
......@@ -18,9 +18,7 @@ extern crate libc;
extern crate tempdir;
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::slice;
pub enum DBOptions {}
pub enum DBInstance {}
......@@ -48,6 +46,11 @@ pub enum DBLogger {}
pub enum DBCompactOptions {}
pub enum DBPinnableSlice {}
pub enum DBUserCollectedProperties {}
pub enum DBUserCollectedPropertiesIterator {}
pub enum DBTableProperties {}
pub enum DBTablePropertiesCollection {}
pub enum DBTablePropertiesCollectionIterator {}
pub enum DBTablePropertiesCollector {}
pub enum DBTablePropertiesCollectorFactory {}
pub fn new_bloom_filter(bits: c_int) -> *mut DBFilterPolicy {
......@@ -61,11 +64,11 @@ pub fn new_cache(capacity: size_t) -> *mut DBCache {
#[repr(C)]
#[derive(PartialEq, Eq, Clone, Copy)]
pub enum DBEntryType {
DBPut = 0,
DBDelete = 1,
DBSingleDelete = 2,
DBMerge = 3,
DBOther = 4,
Put = 0,
Delete = 1,
SingleDelete = 2,
Merge = 3,
Other = 4,
}
#[derive(Copy, Clone)]
......@@ -184,112 +187,26 @@ pub enum DBInfoLogLevel {
DBNumInfoLog = 6,
}
#[derive(Copy, Clone)]
#[repr(C)]
pub struct CShallowStringsMap {
pub size: size_t,
pub keys: *const *const c_char,
pub keys_lens: *const size_t,
pub values: *const *const c_char,
pub values_lens: *const size_t,
}
impl CShallowStringsMap {
pub fn to_bytes_map(&self) -> Result<HashMap<Vec<u8>, Vec<u8>>, String> {
let mut res = HashMap::new();
unsafe {
let keys = slice::from_raw_parts(self.keys, self.size);
let keys_lens = slice::from_raw_parts(self.keys_lens, self.size);
let values = slice::from_raw_parts(self.values, self.size);
let values_lens = slice::from_raw_parts(self.values_lens, self.size);
for ((k, klen), (v, vlen)) in keys.iter()
.zip(keys_lens.iter())
.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 enum DBTableProperty {
DataSize = 1,
IndexSize,
FilterSize,
RawKeySize,
RawValueSize,
NumDataBlocks,
NumEntries,
FormatVersion,
FixedKeyLen,
ColumnFamilyId,
ColumnFamilyName = 11,
FilterPolicyName,
ComparatorName,
MergeOperatorName,
PrefixExtractorName,
PropertyCollectorsNames,
CompressionName,
}
pub fn error_message(ptr: *mut c_char) -> String {
......@@ -719,6 +636,7 @@ extern "C" {
pub fn crocksdb_drop_column_family(db: *mut DBInstance,
column_family_handle: *mut DBCFHandle,
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_list_column_families(db: *const DBOptions,
path: *const c_char,
......@@ -929,25 +847,99 @@ extern "C" {
-> *const u8;
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_len: size_t,
value: *const uint8_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
(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;
pub fn crocksdb_table_properties_collector_factory_destroy(
factory: *mut DBTablePropertiesCollectorFactory);
f: *mut DBTablePropertiesCollectorFactory);
pub fn crocksdb_options_add_table_properties_collector_factory(
options: *mut DBOptions, factory: *mut DBTablePropertiesCollectorFactory);
pub fn crocksdb_table_properties_collection_create() -> *mut DBTablePropertiesCollection;
pub fn crocksdb_table_properties_collection_destroy(c: *mut DBTablePropertiesCollection);
options: *mut DBOptions, f: *mut DBTablePropertiesCollectorFactory);
pub fn crocksdb_get_properties_of_all_tables(db: *mut DBInstance,
props: *mut DBTablePropertiesCollection,
......@@ -967,6 +959,7 @@ extern "C" {
limit_keys_lens: *const size_t,
props: *mut DBTablePropertiesCollection,
errptr: *mut *mut c_char);
}
#[cfg(test)]
......
......@@ -34,6 +34,12 @@ pub struct CFHandle {
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 {
fn drop(&mut self) {
unsafe {
......
......@@ -27,7 +27,7 @@ use slice_transform::{SliceTransform, new_slice_transform};
use std::ffi::{CStr, CString};
use std::mem;
use table_properties_collector_factory::{TablePropertiesCollectorFactory,
new_table_properties_collector_factory_context};
new_table_properties_collector_factory};
#[derive(Default, Debug)]
pub struct HistogramData {
......@@ -384,8 +384,7 @@ impl Options {
pub fn add_table_properties_collector_factory(&mut self,
factory: Box<TablePropertiesCollectorFactory>) {
unsafe {
let context = Box::into_raw(new_table_properties_collector_factory_context(factory));
let f = crocksdb_ffi::crocksdb_table_properties_collector_factory_create(context);
let f = new_table_properties_collector_factory(factory);
crocksdb_ffi::crocksdb_options_add_table_properties_collector_factory(self.inner, f);
}
}
......
......@@ -11,7 +11,10 @@
// See the License for the specific language governing permissions and
// 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::slice;
......@@ -35,11 +38,39 @@ pub struct TableProperties {
pub property_collectors_names: String,
pub compression_name: String,
pub user_collected_properties: HashMap<Vec<u8>, Vec<u8>>,
pub readable_properties: HashMap<String, String>,
}
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 inner: *mut DBTablePropertiesCollection,
}
......@@ -63,37 +94,142 @@ impl TablePropertiesCollectionHandle {
pub fn normalize(&self) -> Result<TablePropertiesCollection, String> {
let mut collection = TablePropertiesCollection::new();
unsafe {
let ctx = &*(self.inner as *mut DBTablePropertiesCollection);
let keys = slice::from_raw_parts(ctx.keys, ctx.size);
let values = slice::from_raw_parts(ctx.values, ctx.size);
for i in 0..ctx.size {
let props = &values[i];
let k = ptr_to_string(keys[i])?;
let v = TableProperties {
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: try!(ptr_to_string(props.column_family_name)),
filter_policy_name: try!(ptr_to_string(props.filter_policy_name)),
comparator_name: try!(ptr_to_string(props.comparator_name)),
merge_operator_name: try!(ptr_to_string(props.merge_operator_name)),
prefix_extractor_name: try!(ptr_to_string(props.prefix_extractor_name)),
property_collectors_names: try!(ptr_to_string(props.property_collectors_names)),
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 it = TablePropertiesCollectionIter::new(self.inner);
while it.valid() {
let k = try!(it.key());
let v = TablePropertiesHandle::new(it.value());
let mut props = TableProperties {
data_size: v.get_u64(DBTableProperty::DataSize),
index_size: v.get_u64(DBTableProperty::IndexSize),
filter_size: v.get_u64(DBTableProperty::FilterSize),
raw_key_size: v.get_u64(DBTableProperty::RawKeySize),
raw_value_size: v.get_u64(DBTableProperty::RawValueSize),
num_data_blocks: v.get_u64(DBTableProperty::NumDataBlocks),
num_entries: v.get_u64(DBTableProperty::NumEntries),
format_version: v.get_u64(DBTableProperty::FormatVersion),
fixed_key_len: v.get_u64(DBTableProperty::FixedKeyLen),
column_family_id: v.get_u64(DBTableProperty::ColumnFamilyId),
column_family_name: try!(v.get_str(DBTableProperty::ColumnFamilyName)),
filter_policy_name: try!(v.get_str(DBTableProperty::FilterPolicyName)),
comparator_name: try!(v.get_str(DBTableProperty::ComparatorName)),
merge_operator_name: try!(v.get_str(DBTableProperty::MergeOperatorName)),
prefix_extractor_name: try!(v.get_str(DBTableProperty::PrefixExtractorName)),
property_collectors_names:
try!(v.get_str(DBTableProperty::PropertyCollectorsNames)),
compression_name: try!(v.get_str(DBTableProperty::CompressionName)),
user_collected_properties: HashMap::new(),
};
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)
}
}
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 @@
// See the License for the specific language governing permissions and
// 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 std::collections::HashMap;
use std::mem;
......@@ -33,28 +33,23 @@ pub trait TablePropertiesCollector {
/// Will be called when a table has already been built and is ready for
/// writing the properties block.
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 {
let context = &mut *(context as *mut DBTablePropertiesCollectorContext);
let collector = &mut *(context.collector as *mut Box<TablePropertiesCollector>);
let collector = &mut *(collector as *mut Box<TablePropertiesCollector>);
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 {
let context = Box::from_raw(context as *mut DBTablePropertiesCollectorContext);
Box::from_raw(context.collector as *mut Box<TablePropertiesCollector>);
let collector = &mut *(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_len: size_t,
value: *const uint8_t,
......@@ -63,18 +58,16 @@ pub extern "C" fn add_userkey(context: *mut c_void,
_: uint64_t,
_: uint64_t) {
unsafe {
let context = &mut *(context as *mut DBTablePropertiesCollectorContext);
let collector = &mut *(context.collector as *mut Box<TablePropertiesCollector>);
let collector = &mut *(collector as *mut Box<TablePropertiesCollector>);
let key = slice::from_raw_parts(key, key_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 {
let context = &mut *(context as *mut DBTablePropertiesCollectorContext);
let collector = &mut *(context.collector as *mut Box<TablePropertiesCollector>);
let collector = &mut *(collector as *mut Box<TablePropertiesCollector>);
for (key, value) in collector.finish() {
crocksdb_ffi::crocksdb_user_collected_properties_add(props,
key.as_ptr(),
......@@ -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) {
unsafe {
let context = &mut *(context as *mut DBTablePropertiesCollectorContext);
let collector = &mut *(context.collector as *mut Box<TablePropertiesCollector>);
for (key, value) in collector.readable_properties() {
crocksdb_ffi::crocksdb_user_collected_properties_add(props,
key.as_ptr(),
key.len(),
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,
})
pub unsafe fn new_table_properties_collector(collector: Box<TablePropertiesCollector>)
-> *mut DBTablePropertiesCollector {
crocksdb_ffi::crocksdb_table_properties_collector_create(
Box::into_raw(Box::new(collector)) as *mut c_void,
name,
destruct,
add_userkey,
finish,
)
}
......@@ -11,9 +11,9 @@
// See the License for the specific language governing permissions and
// limitations under the License.
use crocksdb_ffi::DBTablePropertiesCollectorFactoryContext;
use crocksdb_ffi::{self, DBTablePropertiesCollector, DBTablePropertiesCollectorFactory};
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`.
/// Internals create a new `TablePropertiesCollector` for each new table.
......@@ -24,37 +24,36 @@ pub trait TablePropertiesCollectorFactory {
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 {
let context = &mut *(context as *mut DBTablePropertiesCollectorFactoryContext);
let factory = &mut *(context.factory as *mut Box<TablePropertiesCollectorFactory>);
let factory = &mut *(factory as *mut Box<TablePropertiesCollectorFactory>);
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 {
let context = Box::from_raw(context as *mut DBTablePropertiesCollectorFactoryContext);
Box::from_raw(context.factory as *mut Box<TablePropertiesCollectorFactory>);
Box::from_raw(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 {
let context = &mut *(context as *mut DBTablePropertiesCollectorFactoryContext);
let factory = &mut *(context.factory as *mut Box<TablePropertiesCollectorFactory>);
let factory = &mut *(factory as *mut Box<TablePropertiesCollectorFactory>);
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>)
-> Box<DBTablePropertiesCollectorFactoryContext> {
Box::new(DBTablePropertiesCollectorFactoryContext {
factory: Box::into_raw(Box::new(factory)) as *mut c_void,
name: name,
destructor: destructor,
create_table_properties_collector: create_table_properties_collector,
})
-> *mut DBTablePropertiesCollectorFactory {
crocksdb_ffi::crocksdb_table_properties_collector_factory_create(
Box::into_raw(Box::new(factory)) as *mut c_void,
name,
destruct,
create_table_properties_collector,
)
}
......@@ -106,26 +106,17 @@ impl TablePropertiesCollector for ExampleCollector {
self.last_key.extend_from_slice(key);
}
match entry_type {
DBEntryType::DBPut => self.num_puts += 1,
DBEntryType::DBMerge => self.num_merges += 1,
DBEntryType::DBDelete |
DBEntryType::DBSingleDelete => self.num_deletes += 1,
DBEntryType::DBOther => {}
DBEntryType::Put => self.num_puts += 1,
DBEntryType::Merge => self.num_merges += 1,
DBEntryType::Delete |
DBEntryType::SingleDelete => self.num_deletes += 1,
DBEntryType::Other => {}
}
}
fn finish(&mut self) -> HashMap<Vec<u8>, Vec<u8>> {
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 {}
......
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