From 292e32b38fa406b0370adb4ed8377cec37c4c23e Mon Sep 17 00:00:00 2001 From: King_DuckZ Date: Wed, 11 Nov 2015 17:08:17 +0000 Subject: [PATCH] Read db connection parameters from a yml file. --- CMakeLists.txt | 7 +++- cmake/Modules/FindYamlCpp.cmake | 50 +++++++++++++++++++++++++ dindexerrc.yml | 8 ++++ src/dbbackend.cpp | 5 ++- src/dbbackend.hpp | 4 +- src/indexer.cpp | 7 +++- src/indexer.hpp | 4 +- src/main.cpp | 12 +++++- src/settings.cpp | 66 +++++++++++++++++++++++++++++++++ src/settings.hpp | 39 +++++++++++++++++++ 10 files changed, 194 insertions(+), 8 deletions(-) create mode 100644 cmake/Modules/FindYamlCpp.cmake create mode 100644 dindexerrc.yml create mode 100644 src/settings.cpp create mode 100644 src/settings.hpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 7a35d0e..c767601 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,11 +1,13 @@ cmake_minimum_required(VERSION 3.0 FATAL_ERROR) project(dindexer VERSION 0.1 LANGUAGES CXX C) +list (APPEND CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/cmake/Modules) set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -std=c++11") set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -std=c++11") -find_library (Boost REQUIRED 1.53.0) +find_library(Boost REQUIRED 1.53.0) find_package(PostgreSQL REQUIRED) +find_package(YamlCpp REQUIRED 0.5.1) add_executable(${PROJECT_NAME} src/main.cpp @@ -18,11 +20,13 @@ add_executable(${PROJECT_NAME} src/pq/databaseexception.cpp src/pq/resultset.cpp src/dbbackend.cpp + src/settings.cpp ) target_include_directories(${PROJECT_NAME} SYSTEM INTERFACE ${Boost_INCLUDE_DIR} PRIVATE ${PostgreSQL_INCLUDE_DIRS} + PRIVATE ${YAMLCPP_INCLUDE_DIR} ) target_include_directories(${PROJECT_NAME} @@ -32,4 +36,5 @@ target_include_directories(${PROJECT_NAME} target_link_libraries(${PROJECT_NAME} ${PostgreSQL_LIBRARIES} + ${YAMLCPP_LIBRARY} ) diff --git a/cmake/Modules/FindYamlCpp.cmake b/cmake/Modules/FindYamlCpp.cmake new file mode 100644 index 0000000..5ec41c0 --- /dev/null +++ b/cmake/Modules/FindYamlCpp.cmake @@ -0,0 +1,50 @@ +# Locate yaml-cpp +# +# This module defines +# YAMLCPP_FOUND, if false, do not try to link to yaml-cpp +# YAMLCPP_LIBRARY, where to find yaml-cpp +# YAMLCPP_INCLUDE_DIR, where to find yaml.h +# +# By default, the dynamic libraries of yaml-cpp will be found. To find the static ones instead, +# you must set the YAMLCPP_STATIC_LIBRARY variable to TRUE before calling find_package(YamlCpp ...). +# +# If yaml-cpp is not installed in a standard path, you can use the YAMLCPP_DIR CMake variable +# to tell CMake where yaml-cpp is. + +# attempt to find static library first if this is set +if(YAMLCPP_STATIC_LIBRARY) +set(YAMLCPP_STATIC libyaml-cpp.a) +endif() + +# find the yaml-cpp include directory +find_path(YAMLCPP_INCLUDE_DIR yaml-cpp/yaml.h +PATH_SUFFIXES include +PATHS +~/Library/Frameworks/yaml-cpp/include/ +/Library/Frameworks/yaml-cpp/include/ +/usr/local/include/ +/usr/include/ +/sw/yaml-cpp/ # Fink +/opt/local/yaml-cpp/ # DarwinPorts +/opt/csw/yaml-cpp/ # Blastwave +/opt/yaml-cpp/ +${YAMLCPP_DIR}/include/) + +# find the yaml-cpp library +find_library(YAMLCPP_LIBRARY +NAMES ${YAMLCPP_STATIC} yaml-cpp +PATH_SUFFIXES lib64 lib +PATHS ~/Library/Frameworks +/Library/Frameworks +/usr/local +/usr +/sw +/opt/local +/opt/csw +/opt +${YAMLCPP_DIR}/lib) + +# handle the QUIETLY and REQUIRED arguments and set YAMLCPP_FOUND to TRUE if all listed variables are TRUE +include(FindPackageHandleStandardArgs) +FIND_PACKAGE_HANDLE_STANDARD_ARGS(YAMLCPP DEFAULT_MSG YAMLCPP_INCLUDE_DIR YAMLCPP_LIBRARY) +mark_as_advanced(YAMLCPP_INCLUDE_DIR YAMLCPP_LIBRARY) diff --git a/dindexerrc.yml b/dindexerrc.yml new file mode 100644 index 0000000..1d3b7a9 --- /dev/null +++ b/dindexerrc.yml @@ -0,0 +1,8 @@ +%YAML 1.2 +--- +db_settings: + username: your_username + password: your_password + dbname: dindexer + port: 5432 + address: 200.100.200.100 diff --git a/src/dbbackend.cpp b/src/dbbackend.cpp index da35c9d..d0a1cb0 100644 --- a/src/dbbackend.cpp +++ b/src/dbbackend.cpp @@ -17,6 +17,7 @@ #include "dbbackend.hpp" #include "pq/connection.hpp" +#include "settings.hpp" #include #include #include @@ -26,12 +27,12 @@ namespace din { const std::size_t g_batch_size = 100; } //unnamed namespace - void write_to_db (const std::vector& parData) { + void write_to_db (const DinDBSettings& parDB, const std::vector& parData) { if (parData.empty()) { return; } - pq::Connection conn("michele", "password", "dindexer", "100.200.100.200", 5432); + pq::Connection conn(std::string(parDB.username), std::string(parDB.password), std::string(parDB.dbname), std::string(parDB.address), parDB.port); conn.connect(); conn.query_void("BEGIN;"); diff --git a/src/dbbackend.hpp b/src/dbbackend.hpp index 3fe5e58..513eef2 100644 --- a/src/dbbackend.hpp +++ b/src/dbbackend.hpp @@ -23,6 +23,8 @@ #include namespace din { + struct DinDBSettings; + struct FileRecordData { const std::string path; const std::string hash; @@ -32,7 +34,7 @@ namespace din { const bool is_symlink; }; - void write_to_db ( const std::vector& parData ); + void write_to_db ( const DinDBSettings& parDB, const std::vector& parData ); } //namespace din #endif diff --git a/src/indexer.cpp b/src/indexer.cpp index fa93894..7d86c5e 100644 --- a/src/indexer.cpp +++ b/src/indexer.cpp @@ -19,6 +19,7 @@ #include "pathname.hpp" #include "tiger.hpp" #include "dbbackend.hpp" +#include "settings.hpp" #include #include #include @@ -150,6 +151,7 @@ namespace din { struct Indexer::LocalData { typedef std::vector PathList; + DinDBSettings db_settings; PathList paths; std::atomic done_count; std::size_t file_count; @@ -172,7 +174,7 @@ namespace din { ; } - Indexer::Indexer() : + Indexer::Indexer (const DinDBSettings& parDBSettings) : m_local_data(new LocalData) { #if !defined(NDEBUG) @@ -189,6 +191,7 @@ namespace din { #endif m_local_data->done_count = 0; m_local_data->file_count = 0; + m_local_data->db_settings = parDBSettings; } Indexer::~Indexer() { @@ -244,7 +247,7 @@ namespace din { itm.is_symlink }); } - write_to_db(data); + write_to_db(m_local_data->db_settings, data); } } diff --git a/src/indexer.hpp b/src/indexer.hpp index 0772503..a5b5a7e 100644 --- a/src/indexer.hpp +++ b/src/indexer.hpp @@ -21,9 +21,11 @@ #include namespace din { + struct DinDBSettings; + class Indexer { public: - Indexer ( void ); + explicit Indexer ( const DinDBSettings& parDBSettings ); Indexer ( Indexer&& ) = default; Indexer ( const Indexer& ) = delete; ~Indexer ( void ) noexcept; diff --git a/src/main.cpp b/src/main.cpp index c1be112..3db2729 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -19,6 +19,7 @@ #include #include "filesearcher.hpp" #include "indexer.hpp" +#include "settings.hpp" int main (int parArgc, char* parArgv[]) { using std::placeholders::_1; @@ -31,8 +32,17 @@ int main (int parArgc, char* parArgv[]) { } std::cout << std::endl; - din::Indexer indexer; fastf::FileSearcher searcher("/home/michele/dev/code/cpp/dindexer/test"); + din::DinDBSettings settings; + { + const bool loaded = din::load_settings("dindexerrc.yml", settings); + if (not loaded) { + std::cerr << "Can't load settings from dindexerrc.yml, quitting\n"; + return 1; + } + } + + din::Indexer indexer(settings); fastf::FileSearcher::ConstCharVecType ext, ignore; searcher.SetFollowSymlinks(true); searcher.SetCallback(fastf::FileSearcher::CallbackType(std::bind(&din::Indexer::add_path, &indexer, _1, _2, _3, _4))); diff --git a/src/settings.cpp b/src/settings.cpp new file mode 100644 index 0000000..9522bf6 --- /dev/null +++ b/src/settings.cpp @@ -0,0 +1,66 @@ +/* Copyright 2015, 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 . + */ + +#include "settings.hpp" +#include +#include + +namespace YAML { + template<> + struct convert { + static Node encode (const din::DinDBSettings& 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, din::DinDBSettings& parSettings) { + if (not parNode.IsMap() or parNode.size() != 5) { + return false; + } + + parSettings.address = parNode["address"].as(); + parSettings.username = parNode["username"].as(); + parSettings.password = parNode["password"].as(); + parSettings.dbname = parNode["dbname"].as(); + parSettings.port = parNode["port"].as(); + return true; + } + }; +} //namespace YAML + +namespace din { + bool load_settings (const std::string& parPath, DinDBSettings& parOut) { + try { + auto settings = YAML::LoadFile(parPath); + + if (settings["db_settings"]) { + parOut = settings["db_settings"].as(); + return true; + } + } + catch (const std::exception&) { + return false; + } + + return false; + } +} //namespace din diff --git a/src/settings.hpp b/src/settings.hpp new file mode 100644 index 0000000..2670a80 --- /dev/null +++ b/src/settings.hpp @@ -0,0 +1,39 @@ +/* Copyright 2015, 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 . + */ + +#ifndef idDC29E3C667BD4793BA0644AE7DC5BD3F +#define idDC29E3C667BD4793BA0644AE7DC5BD3F + +#include +#include + +namespace din { + struct DinDBSettings { + std::string address; + std::string username; + std::string password; + std::string dbname; + uint16_t port; + }; + + //struct DinSettings { + //}; + + bool load_settings ( const std::string& parPath, DinDBSettings& parOut ); +} //namespace din + +#endif