1
0
Fork 0
mirror of https://github.com/KingDuckZ/dindexer.git synced 2024-11-29 01:33:46 +00:00

Create intermediate backend lib to help loading backend plugins.

This commit is contained in:
King_DuckZ 2016-05-27 20:28:26 +02:00
parent 1fd51f75ba
commit 064fc0cf1a
11 changed files with 429 additions and 4 deletions

View file

@ -129,7 +129,7 @@ add_subdirectory(src/common)
add_subdirectory(src/machinery)
add_subdirectory(lib/pbl)
add_subdirectory(lib/glob2regex)
add_subdirectory(src/backends/postgresql)
add_subdirectory(src/backends)
#Actions
add_subdirectory(src/main)

View file

@ -0,0 +1,59 @@
/* Copyright 2015, 2016, Michele Santullo
* This file is part of "dindexer".
*
* "dindexer" is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* "dindexer" is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with "dindexer". If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef id756A258A98B24B0DB2529BCEEC5137E2
#define id756A258A98B24B0DB2529BCEEC5137E2
#include <string>
#include <memory>
#include <boost/utility/string_ref.hpp>
namespace YAML {
class Node;
} //namespace YAML
namespace dindb {
class Backend;
using BackendPtr = std::unique_ptr<dindb::Backend, void(*)(dindb::Backend*)>;
class BackendPlugin {
public:
BackendPlugin ( void );
BackendPlugin ( BackendPlugin&& ) = default;
BackendPlugin ( const std::string& parSOPath, const YAML::Node* parConfig );
~BackendPlugin ( void ) noexcept;
const boost::string_ref& name ( void ) const;
Backend& backend ( void );
const Backend& backend ( void ) const;
bool is_loaded ( void ) const;
BackendPlugin& operator= ( BackendPlugin&& ) = default;
private:
using SoHandle = std::unique_ptr<void, int(*)(void*)>;
SoHandle m_lib;
BackendPtr m_backend;
boost::string_ref m_name;
};
std::string backend_name ( const std::string& parSOPath );
} //namespace dindb
#endif

View file

@ -0,0 +1,49 @@
/* Copyright 2015, 2016, Michele Santullo
* This file is part of "dindexer".
*
* "dindexer" is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* "dindexer" is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with "dindexer". If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef id7506CA9825454B80856154ACFE8A9DE2
#define id7506CA9825454B80856154ACFE8A9DE2
#include "backends/backend_loader.hpp"
#include <cstdint>
#include <string>
#include <vector>
#include <boost/utility/string_ref.hpp>
namespace dindb {
using GroupIDType = uint32_t;
using FileIDType = uint64_t;
constexpr const GroupIDType InvalidGroupID = 0;
constexpr const FileIDType InvalidFileID = 0;
class Backend {
public:
Backend ( void ) = default;
virtual ~Backend ( void ) noexcept = default;
virtual void tag_files ( const std::vector<FileIDType>& parFiles, const std::vector<boost::string_ref>& parTags, GroupIDType parSet ) const = 0;
virtual void tag_files ( const std::vector<std::string>& parRegexes, const std::vector<boost::string_ref>& parTags, GroupIDType parSet ) const = 0;
virtual void delete_tags ( const std::vector<FileIDType>& parFiles, const std::vector<boost::string_ref>& parTags, GroupIDType parSet ) const = 0;
virtual void delete_tags ( const std::vector<std::string>& parRegexes, const std::vector<boost::string_ref>& parTags, GroupIDType parSet ) const = 0;
virtual void delete_all_tags ( const std::vector<FileIDType>& parFiles, GroupIDType parSet ) const = 0;
virtual void delete_all_tags ( const std::vector<std::string>& parRegexes, GroupIDType parSet ) const = 0;
};
} //namespace dindb
#endif

View file

@ -0,0 +1,33 @@
/* Copyright 2015, 2016, Michele Santullo
* This file is part of "dindexer".
*
* "dindexer" is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* "dindexer" is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with "dindexer". If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef idA9E47E37E2FA49EE84C2E93FB701C368
#define idA9E47E37E2FA49EE84C2E93FB701C368
namespace YAML {
class Node;
} //namespace YAML
namespace dindb {
class Backend;
} //namespace dindb
extern "C" dindb::Backend* dindexer_create_backend ( const YAML::Node* parConfig );
extern "C" void dindexer_destroy_backend ( dindb::Backend* parDele );
extern "C" const char* dindexer_backend_name ( void );
#endif

View file

@ -0,0 +1,28 @@
project(${bare_name}-backend CXX)
add_library(${PROJECT_NAME} STATIC
backend_loader.cpp
)
target_include_directories(${PROJECT_NAME}
PUBLIC ${DINDEXER_PUB_INCLUDE_DIR}
)
target_include_directories(${PROJECT_NAME} SYSTEM
PUBLIC ${YAMLCPP_INCLUDE_DIR}
)
target_link_libraries(${PROJECT_NAME}
PRIVATE ${bare_name}-if
PUBLIC ${YAMLCPP_LIBRARY}
PUBLIC dl
)
#install(TARGETS ${PROJECT_NAME}
# LIBRARY DESTINATION lib
# RUNTIME DESTINATION bin
# ARCHIVE DESTINATION lib/static
#)
add_subdirectory(postgresql)
add_dependencies(${PROJECT_NAME} ${bare_name}-backend-postgresql)

View file

@ -0,0 +1,107 @@
/* Copyright 2015, 2016, Michele Santullo
* This file is part of "dindexer".
*
* "dindexer" is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* "dindexer" is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with "dindexer". If not, see <http://www.gnu.org/licenses/>.
*/
//See:
// http://stackoverflow.com/questions/496664/c-dynamic-shared-library-on-linux
#include "backends/backend_loader.hpp"
#include "backends/exposed_functions.hpp"
#include <dlfcn.h>
#include <cassert>
#include <functional>
namespace dindb {
namespace {
BackendPtr load_backend (void* parSOHandle, const YAML::Node* parConfig) {
typedef decltype(&dindexer_create_backend) CreateBackendFun;
typedef decltype(&dindexer_destroy_backend) DeleteBackendFun;
assert(parSOHandle);
assert(parConfig);
auto create = reinterpret_cast<CreateBackendFun>(dlsym(parSOHandle, "dindexer_create_backend"));
auto destroy = reinterpret_cast<DeleteBackendFun>(dlsym(parSOHandle, "dindexer_destroy_backend"));
return BackendPtr(create(parConfig), destroy);
}
const char* backend_name (void* parSOHandle) {
typedef decltype(&dindexer_backend_name) GetNameFun;
assert(parSOHandle);
auto get_name = reinterpret_cast<GetNameFun>(dlsym(parSOHandle, "dindexer_backend_name"));
return get_name();
}
void nop_destroy (Backend*) {
}
} //unnamed namespace
std::string backend_name (const std::string& parSOPath) {
assert(not parSOPath.empty());
using SoHandle = std::unique_ptr<void, int(*)(void*)>;
auto handle = SoHandle(dlopen(parSOPath.c_str(), RTLD_LAZY), &dlclose);
return backend_name(handle.get());
}
BackendPlugin::BackendPlugin() :
m_lib(nullptr, &dlclose),
m_backend(nullptr, &nop_destroy),
m_name()
{
}
BackendPlugin::BackendPlugin (const std::string& parSOPath, const YAML::Node* parConfig) :
m_lib(dlopen(parSOPath.c_str(), RTLD_LAZY), &dlclose),
m_backend(load_backend(m_lib.get(), parConfig)),
m_name(backend_name(m_lib.get()))
{
}
BackendPlugin::~BackendPlugin() noexcept {
}
const boost::string_ref& BackendPlugin::name() const {
return m_name;
}
Backend& BackendPlugin::backend() {
assert(m_lib);
assert(m_backend);
if (not m_backend) {
throw std::bad_function_call();
}
return *m_backend;
}
const Backend& BackendPlugin::backend() const {
assert(m_lib);
assert(m_backend);
if (not m_backend) {
throw std::bad_function_call();
}
return *m_backend;
}
bool BackendPlugin::is_loaded() const {
return static_cast<bool>(m_backend);
}
} //namespace dindb

View file

@ -6,6 +6,7 @@ add_library(${PROJECT_NAME} STATIC
locate.cpp
scan.cpp
dbsource.cpp
backend_postgresql.cpp
)
target_include_directories(${PROJECT_NAME}

View file

@ -0,0 +1,102 @@
/* Copyright 2015, 2016, Michele Santullo
* This file is part of "dindexer".
*
* "dindexer" is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* "dindexer" is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with "dindexer". If not, see <http://www.gnu.org/licenses/>.
*/
#include "backend_postgresql.hpp"
#include "backends/exposed_functions.hpp"
#include "pq/connection.hpp"
#include <ciso646>
#include <utility>
#include <cassert>
#include <yaml-cpp/yaml.h>
namespace YAML {
template<>
struct convert<dindb::Settings> {
static Node encode (const dindb::Settings& parSettings) {
Node node;
node["address"] = parSettings.address;
node["username"] = parSettings.username;
node["password"] = parSettings.password;
node["port"] = parSettings.port;
node["dbname"] = parSettings.dbname;
return node;
}
static bool decode (const Node& parNode, dindb::Settings& parSettings) {
if (not parNode.IsMap() or parNode.size() != 5) {
return false;
}
parSettings.address = parNode["address"].as<std::string>();
parSettings.username = parNode["username"].as<std::string>();
parSettings.password = parNode["password"].as<std::string>();
parSettings.dbname = parNode["dbname"].as<std::string>();
parSettings.port = parNode["port"].as<uint16_t>();
return true;
}
};
} //namespace YAML
namespace dindb {
BackendPostgreSql::BackendPostgreSql (std::string&& parUser, std::string&& parPass, std::string&& parDB, std::string&& parAddr, uint16_t parPort) :
m_conn(new pq::Connection(std::move(parUser), std::move(parPass), std::move(parDB), std::move(parAddr), parPort))
{
assert(m_conn);
m_conn->connect();
}
BackendPostgreSql::~BackendPostgreSql() noexcept {
m_conn->disconnect();
}
void BackendPostgreSql::tag_files (const std::vector<FileIDType>& parFiles, const std::vector<boost::string_ref>& parTags, GroupIDType parSet) const {
if (InvalidGroupID != parSet) {
const std::string query =
"UPDATE \"files\" SET \"tags\" = ARRAY(SELECT DISTINCT UNNEST(\"tags\" || $1) ORDER BY 1) WHERE \"id\"=ANY($2) AND \"group_id\"=$3;";
m_conn->query(query, parTags, parFiles, parSet);
}
else {
const std::string query =
"UPDATE \"files\" SET \"tags\" = ARRAY(SELECT DISTINCT UNNEST(\"tags\" || $1) ORDER BY 1) WHERE \"id\"=ANY($2);";
m_conn->query(query, parTags, parFiles);
}
}
void BackendPostgreSql::tag_files (const std::vector<std::string>& parRegexes, const std::vector<boost::string_ref>& parTags, GroupIDType parSet) const {
}
void BackendPostgreSql::delete_tags (const std::vector<FileIDType>& parFiles, const std::vector<boost::string_ref>& parTags, GroupIDType parSet) const {
}
void BackendPostgreSql::delete_tags (const std::vector<std::string>& parRegexes, const std::vector<boost::string_ref>& parTags, GroupIDType parSet) const {
}
void BackendPostgreSql::delete_all_tags (const std::vector<FileIDType>& parFiles, GroupIDType parSet) const {
}
void BackendPostgreSql::delete_all_tags (const std::vector<std::string>& parRegexes, GroupIDType parSet) const {
}
} //namespace dindb
extern "C" dindb::Backend* create_backend (const YAML::Node*) {
return new dindb::BackendPostgreSql("A", "B", "C", "D", 1);
}
extern "C" void destroy_backend (dindb::Backend* parDele) {
if (parDele)
delete parDele;
}

View file

@ -0,0 +1,49 @@
/* Copyright 2015, 2016, Michele Santullo
* This file is part of "dindexer".
*
* "dindexer" is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* "dindexer" is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with "dindexer". If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef idBE5BBAFEC7334F39AC9338F193703341
#define idBE5BBAFEC7334F39AC9338F193703341
#include "backends/db_backend.hpp"
#include <string>
#include <cstdint>
#include <memory>
namespace pq {
class Connection;
} //namespace pq
namespace dindb {
class BackendPostgreSql : public Backend {
public:
BackendPostgreSql ( BackendPostgreSql&& ) = default;
BackendPostgreSql ( std::string&& parUser, std::string&& parPass, std::string&& parDB, std::string&& parAddr, uint16_t parPort );
virtual ~BackendPostgreSql ( void ) noexcept;
virtual void tag_files ( const std::vector<FileIDType>& parFiles, const std::vector<boost::string_ref>& parTags, GroupIDType parSet ) const override;
virtual void tag_files ( const std::vector<std::string>& parRegexes, const std::vector<boost::string_ref>& parTags, GroupIDType parSet ) const override;
virtual void delete_tags ( const std::vector<FileIDType>& parFiles, const std::vector<boost::string_ref>& parTags, GroupIDType parSet ) const override;
virtual void delete_tags ( const std::vector<std::string>& parRegexes, const std::vector<boost::string_ref>& parTags, GroupIDType parSet ) const override;
virtual void delete_all_tags ( const std::vector<FileIDType>& parFiles, GroupIDType parSet ) const override;
virtual void delete_all_tags ( const std::vector<std::string>& parRegexes, GroupIDType parSet ) const override;
private:
std::unique_ptr<pq::Connection> m_conn;
};
} //namespace dindb
#endif

View file

@ -19,8 +19,6 @@ target_include_directories(${PROJECT_NAME} SYSTEM
PRIVATE ${Readline_INCLUDE_DIR}
)
string(TOLOWER "${DINDEXER_DB_BACKEND}" DINDEXER_DB_BACKEND_LOWERCASE)
target_link_libraries(${PROJECT_NAME}
PRIVATE ${bare_name}-if
PRIVATE ${YAMLCPP_LIBRARY}

View file

@ -12,7 +12,6 @@ add_executable(${PROJECT_NAME}
make_timestamp(${bare_name} timestamp.h.in)
target_include_directories(${PROJECT_NAME}
PRIVATE ${CMAKE_SOURCE_DIR}/include
PRIVATE ${CMAKE_SOURCE_DIR}/lib/pbl/pbl/src/src
PRIVATE ${CMAKE_CURRENT_BINARY_DIR}
)