Commit 6390f9c0 authored by Matthieu Dorier's avatar Matthieu Dorier
Browse files

modified API to enable named comparison functions

parent 9fa8d431
......@@ -34,6 +34,7 @@ typedef uint64_t sdskv_database_id_t;
#define SDSKV_ERR_ERASE -11 /* Could not erase the given key */
#define SDSKV_ERR_MIGRATION -12 /* Error during data migration */
#define SDSKV_OP_NOT_IMPL -13 /* Operation not implemented for this backend */
#define SDSKV_ERR_COMP_FUNC -14 /* Comparison function does not exist */
#if defined(__cplusplus)
}
......
......@@ -22,15 +22,12 @@ extern "C" {
typedef struct sdskv_server_context_t* sdskv_provider_t;
typedef int (*sdskv_compare_fn)(const void*, size_t, const void*, size_t);
typedef struct sdskv_server_context_t* sdskv_provider_t;
typedef int (*sdskv_compare_fn)(const void*, size_t, const void*, size_t);
typedef struct sdskv_config_t {
const char* db_name; // name of the database
const char* db_path; // path to the database
sdskv_db_type_t db_type; // type of database
sdskv_compare_fn db_comparison_fn; // comparison function (can be NULL)
int db_no_overwrite; // prevents overwritting data if set to 1
const char* db_name; // name of the database
const char* db_path; // path to the database
sdskv_db_type_t db_type; // type of database
const char* db_comp_fn_name; // name of registered comparison function (can be NULL)
int db_no_overwrite; // prevents overwritting data if set to 1
} sdskv_config_t;
#define SDSKV_CONFIG_DEFAULT { "", "", KVDB_MAP, SDSKV_COMPARE_DEFAULT, 0 }
......@@ -51,6 +48,20 @@ int sdskv_provider_register(
ABT_pool pool,
sdskv_provider_t* provider);
/**
* @brief Registers a comparison function for databases to use.
*
* @param provider provider
* @param function_name name of the comparison function
* @param comp_fn pointer to the comparison function
*
* @return SDSKV_SUCCESS or error code defined in sdskv-common.h
*/
int sdskv_provider_add_comparison_function(
sdskv_provider_t provider,
const char* function_name,
sdskv_compare_fn comp_fn);
/**
* Makes the provider start managing a database. The database will
* be created if it does not exist. Otherwise, the provider will start
......
......@@ -184,7 +184,7 @@ int main(int argc, char **argv)
.db_name = opts.db_names[i],
.db_path = "",
.db_type = opts.db_types[i],
.db_comparison_fn = SDSKV_COMPARE_DEFAULT,
.db_comp_fn_name = SDSKV_COMPARE_DEFAULT,
.db_no_overwrite = 0
};
ret = sdskv_provider_attach_database(provider, &db_config, &db_id);
......@@ -220,7 +220,7 @@ int main(int argc, char **argv)
.db_name = opts.db_names[i],
.db_path = "",
.db_type = opts.db_types[i],
.db_comparison_fn = SDSKV_COMPARE_DEFAULT,
.db_comp_fn_name = SDSKV_COMPARE_DEFAULT,
.db_no_overwrite = 0
};
ret = sdskv_provider_attach_database(provider, &db_config, &db_id);
......
......@@ -17,6 +17,7 @@ struct sdskv_server_context_t
std::unordered_map<sdskv_database_id_t, AbstractDataStore*> databases;
std::map<std::string, sdskv_database_id_t> name2id;
std::map<sdskv_database_id_t, std::string> id2name;
std::map<std::string, sdskv_compare_fn> compfunctions;
hg_id_t sdskv_put_id;
hg_id_t sdskv_put_multi_id;
......@@ -199,15 +200,38 @@ extern "C" int sdskv_provider_register(
return SDSKV_SUCCESS;
}
int sdskv_provider_add_comparison_function(
sdskv_provider_t provider,
const char* function_name,
sdskv_compare_fn comp_fn)
{
if(provider->compfunctions.find(std::string(function_name))
!= provider->compfunctions.end())
return SDSKV_ERR_COMP_FUNC;
provider->compfunctions[std::string(function_name)] = comp_fn;
return SDSKV_SUCCESS;
}
extern "C" int sdskv_provider_attach_database(
sdskv_provider_t provider,
const sdskv_config_t* config,
sdskv_database_id_t* db_id)
{
sdskv_compare_fn comp_fn = NULL;
if(config->db_comp_fn_name) {
std::string k(config->db_comp_fn_name);
auto it = provider->compfunctions.find(k);
if(it == provider->compfunctions.end())
return SDSKV_ERR_COMP_FUNC;
comp_fn = it->second;
}
auto db = datastore_factory::open_datastore(config->db_type,
std::string(config->db_name), std::string(config->db_path));
if(db == nullptr) return SDSKV_ERR_DB_CREATE;
db->set_comparison_function(config->db_comparison_fn);
if(comp_fn) {
db->set_comparison_function(comp_fn);
}
sdskv_database_id_t id = (sdskv_database_id_t)(db);
if(config->db_no_overwrite) {
db->set_no_overwrite();
......@@ -222,22 +246,6 @@ extern "C" int sdskv_provider_attach_database(
return SDSKV_SUCCESS;
}
extern "C" int sdskv_provider_add_database(
sdskv_provider_t provider,
const char* db_name,
const char* db_path,
sdskv_db_type_t db_type,
sdskv_compare_fn comp_fn,
sdskv_database_id_t* db_id) {
sdskv_config_t config = SDSKV_CONFIG_DEFAULT;
config.db_name = db_name;
config.db_path = db_path;
config.db_type = db_type;
config.db_comparison_fn = comp_fn;
return sdskv_provider_attach_database(
provider, &config, db_id);
}
extern "C" int sdskv_provider_remove_database(
sdskv_provider_t provider,
sdskv_database_id_t db_id)
......
......@@ -186,10 +186,24 @@ int main(int argc, char **argv)
return(-1);
}
ret = sdskv_provider_add_comparison_function(provider,
"my_custom_comp_function",
custom_key_cmp);
if(ret != SDSKV_SUCCESS) {
fprintf(stderr, "Error: sdskv_provider_add_comparison_function()\n");
margo_finalize(mid);
return(-1);
}
sdskv_database_id_t db_id;
ret = sdskv_provider_add_database(provider,
opts.db_names[i], "", opts.db_types[i], &custom_key_cmp,
&db_id);
sdskv_config_t db_config = {
.db_name = opts.db_names[i],
.db_path = "",
.db_type = opts.db_types[i],
.db_comp_fn_name = "my_custom_comp_function",
.db_no_overwrite = 0
};
ret = sdskv_provider_attach_database(provider, &db_config, &db_id);
if(ret != 0)
{
......@@ -216,12 +230,25 @@ int main(int argc, char **argv)
return(-1);
}
ret = sdskv_provider_add_comparison_function(provider,
"my_custom_comp_function",
custom_key_cmp);
if(ret != SDSKV_SUCCESS) {
fprintf(stderr, "Error: sdskv_provider_add_comparison_function()\n");
margo_finalize(mid);
return(-1);
}
for(i=0; i < opts.num_db; i++) {
sdskv_database_id_t db_id;
ret = sdskv_provider_add_database(provider,
opts.db_names[i], "", opts.db_types[i],
&custom_key_cmp,
&db_id);
sdskv_config_t db_config = {
.db_name = opts.db_names[i],
.db_path = "",
.db_type = opts.db_types[i],
.db_comp_fn_name = "my_custom_comp_function",
.db_no_overwrite = 0
};
ret = sdskv_provider_attach_database(provider, &db_config, &db_id);
if(ret != 0)
{
......
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