mirror of
https://github.com/KingDuckZ/dindexer.git
synced 2024-11-25 00:53:43 +00:00
Read db connection parameters from a yml file.
This commit is contained in:
parent
413bf0444b
commit
292e32b38f
10 changed files with 194 additions and 8 deletions
|
@ -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}
|
||||
)
|
||||
|
|
50
cmake/Modules/FindYamlCpp.cmake
Normal file
50
cmake/Modules/FindYamlCpp.cmake
Normal file
|
@ -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)
|
8
dindexerrc.yml
Normal file
8
dindexerrc.yml
Normal file
|
@ -0,0 +1,8 @@
|
|||
%YAML 1.2
|
||||
---
|
||||
db_settings:
|
||||
username: your_username
|
||||
password: your_password
|
||||
dbname: dindexer
|
||||
port: 5432
|
||||
address: 200.100.200.100
|
|
@ -17,6 +17,7 @@
|
|||
|
||||
#include "dbbackend.hpp"
|
||||
#include "pq/connection.hpp"
|
||||
#include "settings.hpp"
|
||||
#include <string>
|
||||
#include <sstream>
|
||||
#include <utility>
|
||||
|
@ -26,12 +27,12 @@ namespace din {
|
|||
const std::size_t g_batch_size = 100;
|
||||
} //unnamed namespace
|
||||
|
||||
void write_to_db (const std::vector<FileRecordData>& parData) {
|
||||
void write_to_db (const DinDBSettings& parDB, const std::vector<FileRecordData>& 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;");
|
||||
|
|
|
@ -23,6 +23,8 @@
|
|||
#include <cstdint>
|
||||
|
||||
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<FileRecordData>& parData );
|
||||
void write_to_db ( const DinDBSettings& parDB, const std::vector<FileRecordData>& parData );
|
||||
} //namespace din
|
||||
|
||||
#endif
|
||||
|
|
|
@ -19,6 +19,7 @@
|
|||
#include "pathname.hpp"
|
||||
#include "tiger.hpp"
|
||||
#include "dbbackend.hpp"
|
||||
#include "settings.hpp"
|
||||
#include <algorithm>
|
||||
#include <vector>
|
||||
#include <string>
|
||||
|
@ -150,6 +151,7 @@ namespace din {
|
|||
struct Indexer::LocalData {
|
||||
typedef std::vector<FileEntry> PathList;
|
||||
|
||||
DinDBSettings db_settings;
|
||||
PathList paths;
|
||||
std::atomic<std::size_t> 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);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -21,9 +21,11 @@
|
|||
#include <memory>
|
||||
|
||||
namespace din {
|
||||
struct DinDBSettings;
|
||||
|
||||
class Indexer {
|
||||
public:
|
||||
Indexer ( void );
|
||||
explicit Indexer ( const DinDBSettings& parDBSettings );
|
||||
Indexer ( Indexer&& ) = default;
|
||||
Indexer ( const Indexer& ) = delete;
|
||||
~Indexer ( void ) noexcept;
|
||||
|
|
12
src/main.cpp
12
src/main.cpp
|
@ -19,6 +19,7 @@
|
|||
#include <ciso646>
|
||||
#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)));
|
||||
|
|
66
src/settings.cpp
Normal file
66
src/settings.cpp
Normal file
|
@ -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 <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include "settings.hpp"
|
||||
#include <yaml-cpp/yaml.h>
|
||||
#include <ciso646>
|
||||
|
||||
namespace YAML {
|
||||
template<>
|
||||
struct convert<din::DinDBSettings> {
|
||||
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<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 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<DinDBSettings>();
|
||||
return true;
|
||||
}
|
||||
}
|
||||
catch (const std::exception&) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
} //namespace din
|
39
src/settings.hpp
Normal file
39
src/settings.hpp
Normal file
|
@ -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 <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#ifndef idDC29E3C667BD4793BA0644AE7DC5BD3F
|
||||
#define idDC29E3C667BD4793BA0644AE7DC5BD3F
|
||||
|
||||
#include <string>
|
||||
#include <cstdint>
|
||||
|
||||
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
|
Loading…
Reference in a new issue