Commit dba88b73 authored by Matthieu Dorier's avatar Matthieu Dorier
Browse files

added path argument to sdskv_provider_add_database

parent 11c4288c
......@@ -39,10 +39,13 @@ int sdskv_provider_register(
sdskv_provider_t* provider);
/**
* Makes the provider start managing a database.
* Makes the provider start managing a database. The database will
* be created if it does not exist. Otherwise, the provider will start
* to manage the existing database.
*
* @param[in] provider provider
* @param[in] db_name name of the database
* @param[in] db_path path where the persistent files of the db should be
* @param[in] db_type type of database
* @param[in] comp_fn comparison function for the database
* @param[out] db_id resulting id identifying the database
......@@ -52,6 +55,7 @@ int sdskv_provider_register(
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* sb_id);
......
......@@ -27,15 +27,11 @@ BerkeleyDBDataStore::~BerkeleyDBDataStore() {
delete _dbenv;
};
void BerkeleyDBDataStore::createDatabase(std::string db_name) {
void BerkeleyDBDataStore::createDatabase(const std::string& db_name, const std::string& db_path) {
int status = 0;
// db_name assumed to include the full path (e.g. /var/data/db.dat)
boost::filesystem::path path(db_name);
std::string basepath = path.parent_path().string();
std::string dbname = path.filename().string();
if (!basepath.empty()) {
boost::filesystem::create_directories(basepath);
if (!db_path.empty()) {
boost::filesystem::create_directories(db_path);
}
// initialize the environment
......@@ -80,7 +76,7 @@ void BerkeleyDBDataStore::createDatabase(std::string db_name) {
}
else {
_dbenv->set_lk_detect(DB_LOCK_MINWRITE);
_dbenv->open(basepath.c_str(), flags, 0644);
_dbenv->open(db_path.c_str(), flags, 0644);
}
}
catch (DbException &e) {
......@@ -114,7 +110,7 @@ void BerkeleyDBDataStore::createDatabase(std::string db_name) {
}
else {
status = _dbm->open(NULL, // txn pointer
dbname.c_str(), // file name
db_name.c_str(), // file name
NULL, // logical DB name
DB_BTREE, // DB type (e.g. BTREE, HASH)
flags,
......
......@@ -27,7 +27,7 @@ class BerkeleyDBDataStore : public AbstractDataStore {
BerkeleyDBDataStore();
BerkeleyDBDataStore(Duplicates duplicates, bool eraseOnGet, bool debug);
virtual ~BerkeleyDBDataStore();
virtual void createDatabase(std::string db_name);
virtual void createDatabase(const std::string& db_name, const std::string& path);
virtual bool put(const ds_bulk_t &key, const ds_bulk_t &data);
virtual bool get(const ds_bulk_t &key, ds_bulk_t &data);
virtual bool get(const ds_bulk_t &key, std::vector<ds_bulk_t> &data);
......
......@@ -25,7 +25,7 @@ BwTreeDataStore::~BwTreeDataStore() {
#endif
};
void BwTreeDataStore::createDatabase(std::string db_name) {
void BwTreeDataStore::createDatabase(const std::string& db_name, const std::string& path) {
_tree = new BwTree<ds_bulk_t, ds_bulk_t,
ds_bulk_less, ds_bulk_equal, ds_bulk_hash,
ds_bulk_equal, ds_bulk_hash>();
......
......@@ -14,7 +14,7 @@ public:
BwTreeDataStore();
BwTreeDataStore(Duplicates duplicates, bool eraseOnGet, bool debug);
virtual ~BwTreeDataStore();
virtual void createDatabase(std::string db_name);
virtual void createDatabase(const std::string& db_name, const std::string& path);
virtual bool put(const ds_bulk_t &key, const ds_bulk_t &data);
virtual bool get(const ds_bulk_t &key, ds_bulk_t &data);
virtual bool get(const ds_bulk_t &key, std::vector<ds_bulk_t> &data);
......
......@@ -19,7 +19,7 @@ public:
AbstractDataStore();
AbstractDataStore(Duplicates duplicates, bool eraseOnGet, bool debug);
virtual ~AbstractDataStore();
virtual void createDatabase(std::string db_name)=0;
virtual void createDatabase(const std::string& db_name, const std::string& path)=0;
virtual bool put(const ds_bulk_t &key, const ds_bulk_t &data)=0;
virtual bool get(const ds_bulk_t &key, ds_bulk_t &data)=0;
virtual bool get(const ds_bulk_t &key, std::vector<ds_bulk_t> &data)=0;
......
......@@ -25,36 +25,40 @@
class datastore_factory {
static AbstractDataStore* create_map_datastore(const std::string& name) {
static AbstractDataStore* create_map_datastore(
const std::string& name, const std::string& path) {
auto db = new MapDataStore();
db->createDatabase(name);
db->createDatabase(name, path);
return db;
}
static AbstractDataStore* create_bwtree_datastore(const std::string& name) {
static AbstractDataStore* create_bwtree_datastore(
const std::string& name, const std::string& path) {
#ifdef USE_BWTREE
auto db = new BwTreeDataStore();
db->createDatabase(name);
db->createDatabase(name, path);
return db;
#else
return nullptr;
#endif
}
static AbstractDataStore* create_berkeleydb_datastore(const std::string& name) {
static AbstractDataStore* create_berkeleydb_datastore(
const std::string& name, const std::string& path) {
#ifdef USE_BDB
auto db = new BerkeleyDBDataStore();
db->createDatabase(name);
db->createDatabase(name, path);
return db;
#else
return nullptr;
#endif
}
static AbstractDataStore* create_leveldb_datastore(const std::string& name) {
static AbstractDataStore* create_leveldb_datastore(
const std::string& name, const std::string& path) {
#ifdef USE_LEVELDB
auto db = new LevelDBDataStore();
db->createDatabase(name);
db->createDatabase(name, path);
return db;
#else
return nullptr;
......@@ -64,20 +68,26 @@ class datastore_factory {
public:
#ifdef SDSKV
static AbstractDataStore* create_datastore(sdskv_db_type_t type, const std::string& name)
static AbstractDataStore* create_datastore(
sdskv_db_type_t type,
const std::string& name,
const std::string& path)
#else
static AbstractDataStore* create_datastore(kv_db_type_t type, const std::string& name="db")
static AbstractDataStore* create_datastore(
kv_db_type_t type,
const std::string& name="db",
const std::string& path="db")
#endif
{
switch(type) {
case KVDB_MAP:
return create_map_datastore(name);
return create_map_datastore(name, path);
case KVDB_BWTREE:
return create_bwtree_datastore(name);
return create_bwtree_datastore(name, path);
case KVDB_LEVELDB:
return create_leveldb_datastore(name);
return create_leveldb_datastore(name, path);
case KVDB_BERKELEYDB:
return create_berkeleydb_datastore(name);
return create_berkeleydb_datastore(name, path);
}
return nullptr;
};
......
......@@ -33,19 +33,19 @@ LevelDBDataStore::~LevelDBDataStore() {
//leveldb::Env::Shutdown(); // Riak version only
};
void LevelDBDataStore::createDatabase(std::string db_name) {
void LevelDBDataStore::createDatabase(const std::string& db_name, const std::string& db_path) {
leveldb::Options options;
leveldb::Status status;
// db_name assumed to include the full path (e.g. /var/data/db.dat)
boost::filesystem::path p(db_name);
std::string basepath = p.parent_path().string();
if (!basepath.empty()) {
boost::filesystem::create_directories(basepath);
if (!db_path.empty()) {
boost::filesystem::create_directories(db_path);
}
options.comparator = &_keycmp;
options.create_if_missing = true;
status = leveldb::DB::Open(options, db_name, &_dbm);
std::string fullname = db_path;
if(!fullname.empty()) fullname += std::string("/");
fullname += db_name;
status = leveldb::DB::Open(options, fullname, &_dbm);
if (!status.ok()) {
// error
......
......@@ -39,7 +39,7 @@ class LevelDBDataStore : public AbstractDataStore {
LevelDBDataStore();
LevelDBDataStore(Duplicates duplicates, bool eraseOnGet, bool debug);
virtual ~LevelDBDataStore();
virtual void createDatabase(std::string db_name);
virtual void createDatabase(const std::string& db_name, const std::string& path);
virtual bool put(const ds_bulk_t &key, const ds_bulk_t &data);
virtual bool get(const ds_bulk_t &key, ds_bulk_t &data);
virtual bool get(const ds_bulk_t &key, std::vector<ds_bulk_t> &data);
......
......@@ -35,7 +35,7 @@ class MapDataStore : public AbstractDataStore {
~MapDataStore() = default;
virtual void createDatabase(std::string db_name) {
virtual void createDatabase(const std::string& db_name, const std::string& path) {
_map.clear();
}
......
......@@ -181,7 +181,9 @@ int main(int argc, char **argv)
sdskv_database_id_t db_id;
ret = sdskv_provider_add_database(provider,
opts.db_names[i], opts.db_types[i], SDSKV_COMPARE_DEFAULT,
opts.db_names[i],
opts.db_names[i], // use the name as path
opts.db_types[i], SDSKV_COMPARE_DEFAULT,
&db_id);
if(ret != 0)
......@@ -211,8 +213,10 @@ int main(int argc, char **argv)
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],
ret = sdskv_provider_add_database(provider,
opts.db_names[i],
opts.db_names[i], // use the name as path
opts.db_types[i],
SDSKV_COMPARE_DEFAULT,
&db_id);
......
......@@ -106,11 +106,13 @@ extern "C" int sdskv_provider_register(
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)
{
auto db = datastore_factory::create_datastore(db_type, std::string(db_name));
auto db = datastore_factory::create_datastore(db_type,
std::string(db_name), std::string(db_path));
if(db == nullptr) return SDSKV_ERR_DB_CREATE;
db->set_comparison_function(comp_fn);
sdskv_database_id_t id = (sdskv_database_id_t)(db);
......
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