mirror of
https://github.com/KingDuckZ/dindexer.git
synced 2025-08-18 15:29:48 +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
|
@ -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…
Add table
Add a link
Reference in a new issue