mirror of
https://github.com/KingDuckZ/dindexer.git
synced 2024-11-25 00:53:43 +00:00
Remove hardcoded path to postgresql backend and make the search dynamic
This commit is contained in:
parent
a1490c3096
commit
624bba121f
3 changed files with 49 additions and 15 deletions
|
@ -57,7 +57,7 @@ if ("${DINDEXER_CONFIG_FILE}" STREQUAL "")
|
|||
endif()
|
||||
message(STATUS "Config file set to \"${DINDEXER_CONFIG_FILE}\"")
|
||||
|
||||
find_package(Boost 1.53.0 REQUIRED COMPONENTS program_options filesystem)
|
||||
find_package(Boost 1.53.0 REQUIRED COMPONENTS program_options filesystem system)
|
||||
find_package(PostgreSQL 8.3 REQUIRED)
|
||||
find_package(YamlCpp 0.5.1 REQUIRED)
|
||||
import_libpqtypes_project("${PostgreSQL_INCLUDE_DIRS}" "-O3 ${march_flag}")
|
||||
|
|
|
@ -24,6 +24,7 @@
|
|||
#include <dlfcn.h>
|
||||
#include <cassert>
|
||||
#include <functional>
|
||||
#include <sstream>
|
||||
|
||||
namespace dindb {
|
||||
namespace {
|
||||
|
@ -34,9 +35,22 @@ namespace dindb {
|
|||
assert(parSOHandle);
|
||||
assert(parConfig);
|
||||
|
||||
auto create = reinterpret_cast<CreateBackendFun>(dlsym(parSOHandle, "dindexer_create_backend"));
|
||||
auto destroy = reinterpret_cast<DeleteBackendFun>(dlsym(parSOHandle, "dindexer_destroy_backend"));
|
||||
const char* const fun_name_create = "dindexer_create_backend";
|
||||
const char* const fun_name_destroy = "dindexer_destroy_backend";
|
||||
|
||||
auto create = reinterpret_cast<CreateBackendFun>(dlsym(parSOHandle, fun_name_create));
|
||||
auto destroy = reinterpret_cast<DeleteBackendFun>(dlsym(parSOHandle, fun_name_destroy));
|
||||
|
||||
if (not create) {
|
||||
std::ostringstream oss;
|
||||
oss << "Unable to find function " << fun_name_create;
|
||||
throw std::runtime_error(oss.str());
|
||||
}
|
||||
if (not destroy) {
|
||||
std::ostringstream oss;
|
||||
oss << "Unable to find function " << fun_name_destroy;
|
||||
throw std::runtime_error(oss.str());
|
||||
}
|
||||
return BackendPtr(create(parConfig), destroy);
|
||||
}
|
||||
|
||||
|
@ -65,7 +79,10 @@ namespace dindb {
|
|||
using SoHandle = std::unique_ptr<void, int(*)(void*)>;
|
||||
|
||||
auto handle = SoHandle(dlopen(parSOPath.c_str(), RTLD_LAZY), &dlclose);
|
||||
if (handle)
|
||||
return backend_name(handle.get());
|
||||
else
|
||||
return std::string();
|
||||
}
|
||||
|
||||
BackendPlugin::BackendPlugin() :
|
||||
|
|
|
@ -16,33 +16,40 @@
|
|||
*/
|
||||
|
||||
#include "dindexer-common/settings.hpp"
|
||||
#include "dindexer-common/split_tags.hpp"
|
||||
#include "dindexerConfig.h"
|
||||
#include <yaml-cpp/yaml.h>
|
||||
#include <ciso646>
|
||||
#include <wordexp.h>
|
||||
#include <stdexcept>
|
||||
#include <sstream>
|
||||
#include <boost/filesystem.hpp>
|
||||
#include <boost/range/iterator_range_core.hpp>
|
||||
|
||||
namespace dinlib {
|
||||
namespace {
|
||||
std::string expand ( const char* parString );
|
||||
std::string find_plugin_by_name ( const std::string& parName );
|
||||
std::string find_plugin_by_name ( const std::vector<boost::string_ref>& parSearchPaths, const std::string& parName );
|
||||
void throw_if_plugin_failed ( const dindb::BackendPlugin& parPlugin, const std::string& parPluginPath, const std::string& parIntendedName );
|
||||
} //unnamed namespace
|
||||
|
||||
void load_settings (const std::string& parPath, dinlib::Settings& parOut, bool parExpand) {
|
||||
const std::string path = (parExpand ? expand(parPath.c_str()) : parPath);
|
||||
std::string search_paths(ACTIONS_SEARCH_PATH);
|
||||
|
||||
auto settings = YAML::LoadFile(path);
|
||||
|
||||
if (not settings["backend_name"]) {
|
||||
if (not settings["backend_name"])
|
||||
throw std::runtime_error("No backend_name given in the config file");
|
||||
}
|
||||
if (settings["backend_paths"])
|
||||
search_paths += ":" + settings["backend_paths"].as<std::string>();
|
||||
parOut.backend_name = settings["backend_name"].as<std::string>();
|
||||
const std::string backend_settings_section = parOut.backend_name + "_settings";
|
||||
if (settings[backend_settings_section]) {
|
||||
auto settings_node = settings[backend_settings_section];
|
||||
const std::string plugin_path = find_plugin_by_name(parOut.backend_name);
|
||||
const std::string plugin_path = find_plugin_by_name(split_and_trim(search_paths, ':'), parOut.backend_name);
|
||||
if (plugin_path.empty())
|
||||
throw std::runtime_error(std::string("Unable to find any suitable plugin with the specified name \"") + parOut.backend_name + "\"");
|
||||
parOut.backend_plugin = dindb::BackendPlugin(plugin_path, &settings_node);
|
||||
throw_if_plugin_failed(parOut.backend_plugin, plugin_path, parOut.backend_name);
|
||||
}
|
||||
|
@ -61,14 +68,24 @@ namespace dinlib {
|
|||
return oss.str();
|
||||
}
|
||||
|
||||
std::string find_plugin_by_name (const std::string& parName) {
|
||||
//assert(false); //not implemented
|
||||
//TODO: write a proper implementation
|
||||
std::string path = ACTIONS_SEARCH_PATH;
|
||||
path += "/backends/postgresql/libdindexer-backend-postgresql.so";
|
||||
std::string find_plugin_by_name (const std::vector<boost::string_ref>& parSearchPaths, const std::string& parName) {
|
||||
using boost::filesystem::path;
|
||||
using boost::filesystem::is_directory;
|
||||
using boost::filesystem::directory_iterator;
|
||||
using boost::filesystem::directory_entry;
|
||||
using boost::make_iterator_range;
|
||||
|
||||
assert(dindb::backend_name(path) == parName);
|
||||
return path;
|
||||
for (auto search_path : parSearchPaths) {
|
||||
const std::string search_path_cpy(search_path.begin(), search_path.end());
|
||||
path curr_path(search_path_cpy);
|
||||
auto listing = make_iterator_range(directory_iterator(curr_path), directory_iterator());
|
||||
for (const directory_entry& entry : listing) {
|
||||
auto current_full_path = entry.path().string();
|
||||
if (not is_directory(entry) and dindb::backend_name(current_full_path) == parName)
|
||||
return current_full_path;
|
||||
}
|
||||
}
|
||||
return std::string();
|
||||
}
|
||||
|
||||
void throw_if_plugin_failed (const dindb::BackendPlugin& parPlugin, const std::string& parPluginPath, const std::string& parIntendedName) {
|
||||
|
|
Loading…
Reference in a new issue