mirror of
https://github.com/KingDuckZ/dindexer.git
synced 2025-02-17 11:45:50 +00:00
Refactor Indexer so that writing to the db is done outside.
This commit is contained in:
parent
8b566c9fdb
commit
487b8efe61
4 changed files with 65 additions and 56 deletions
|
@ -49,10 +49,13 @@ target_include_directories(${bare_name}-inc
|
|||
INTERFACE ${CMAKE_SOURCE_DIR}/include
|
||||
)
|
||||
|
||||
add_subdirectory(src/scan)
|
||||
#Libraries
|
||||
add_subdirectory(src/pq)
|
||||
add_subdirectory(src/main)
|
||||
add_subdirectory(src/common)
|
||||
|
||||
#Actions
|
||||
add_subdirectory(src/main)
|
||||
add_subdirectory(src/scan)
|
||||
add_subdirectory(src/delete)
|
||||
add_subdirectory(src/query)
|
||||
add_subdirectory(src/locate)
|
||||
|
|
|
@ -18,14 +18,12 @@
|
|||
#include "indexer.hpp"
|
||||
#include "pathname.hpp"
|
||||
#include "tiger.hpp"
|
||||
#include "dbbackend.hpp"
|
||||
#include "dindexer-common/settings.hpp"
|
||||
#include "filestats.hpp"
|
||||
#include "mimetype.hpp"
|
||||
#include "recorddata.hpp"
|
||||
#include <algorithm>
|
||||
#include <functional>
|
||||
#include <vector>
|
||||
#include <stdexcept>
|
||||
#if defined(WITH_PROGRESS_FEEDBACK)
|
||||
# include <atomic>
|
||||
|
@ -171,12 +169,29 @@ namespace din {
|
|||
parSt.is_symlink
|
||||
);
|
||||
}
|
||||
|
||||
bool file_record_data_lt (const FileRecordData& parLeft, const FileRecordData& parRight) {
|
||||
const FileRecordData& l = parLeft;
|
||||
const FileRecordData& r = parRight;
|
||||
return
|
||||
(l.level < r.level)
|
||||
or (l.level == r.level and l.is_directory and not r.is_directory)
|
||||
or (l.level == r.level and l.is_directory == r.is_directory and l.path < r.path)
|
||||
|
||||
//sort by directory - parent first, children later
|
||||
//(level == o.level and is_dir and not o.is_dir)
|
||||
//or (level == o.level and is_dir == o.is_dir and path < o.path)
|
||||
//or (level > o.level + 1)
|
||||
//or (level + 1 == o.level and is_dir and not o.is_dir and path < o.path)
|
||||
//or (level + 1 == o.level and is_dir and not o.is_dir and path == PathName(o.path).dirname())
|
||||
//or (level == o.level + 1 and not (o.is_dir and not is_dir and o.path == PathName(path).dirname()))
|
||||
;
|
||||
}
|
||||
} //unnamed namespace
|
||||
|
||||
struct Indexer::LocalData {
|
||||
typedef std::vector<FileRecordData> PathList;
|
||||
|
||||
dinlib::SettingsDB db_settings;
|
||||
PathList paths;
|
||||
#if defined(WITH_PROGRESS_FEEDBACK)
|
||||
std::atomic<std::size_t> done_count;
|
||||
|
@ -187,25 +202,7 @@ namespace din {
|
|||
bool ignore_read_errors;
|
||||
};
|
||||
|
||||
bool file_record_data_lt (const FileRecordData& parLeft, const FileRecordData& parRight) {
|
||||
const FileRecordData& l = parLeft;
|
||||
const FileRecordData& r = parRight;
|
||||
return
|
||||
(l.level < r.level)
|
||||
or (l.level == r.level and l.is_directory and not r.is_directory)
|
||||
or (l.level == r.level and l.is_directory == r.is_directory and l.path < r.path)
|
||||
|
||||
//sort by directory - parent first, children later
|
||||
//(level == o.level and is_dir and not o.is_dir)
|
||||
//or (level == o.level and is_dir == o.is_dir and path < o.path)
|
||||
//or (level > o.level + 1)
|
||||
//or (level + 1 == o.level and is_dir and not o.is_dir and path < o.path)
|
||||
//or (level + 1 == o.level and is_dir and not o.is_dir and path == PathName(o.path).dirname())
|
||||
//or (level == o.level + 1 and not (o.is_dir and not is_dir and o.path == PathName(path).dirname()))
|
||||
;
|
||||
}
|
||||
|
||||
Indexer::Indexer (const dinlib::Settings& parSettings) :
|
||||
Indexer::Indexer() :
|
||||
m_local_data(new LocalData)
|
||||
{
|
||||
#if !defined(NDEBUG)
|
||||
|
@ -225,7 +222,6 @@ namespace din {
|
|||
m_local_data->processing_index = 0;
|
||||
#endif
|
||||
m_local_data->file_count = 0;
|
||||
m_local_data->db_settings = parSettings.db;
|
||||
}
|
||||
|
||||
Indexer::~Indexer() noexcept {
|
||||
|
@ -297,28 +293,6 @@ namespace din {
|
|||
#endif
|
||||
}
|
||||
|
||||
bool Indexer::add_to_db (const std::string& parSetName, char parType, bool parForce) const {
|
||||
#if defined(WITH_PROGRESS_FEEDBACK)
|
||||
assert(m_local_data->done_count == m_local_data->file_count);
|
||||
#endif
|
||||
|
||||
if (not parForce) {
|
||||
const auto& first_hash = m_local_data->paths.front().hash;
|
||||
FileRecordData itm;
|
||||
SetRecordDataFull set;
|
||||
const bool already_in_db = read_from_db(itm, set, m_local_data->db_settings, first_hash);
|
||||
if (already_in_db) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
PathName base_path(m_local_data->paths.front().path);
|
||||
|
||||
SetRecordData set_data {parSetName, parType};
|
||||
write_to_db(m_local_data->db_settings, m_local_data->paths, set_data);
|
||||
return true;
|
||||
}
|
||||
|
||||
bool Indexer::add_path (const char* parPath, const fastf::FileStats& parStats) {
|
||||
m_local_data->paths.push_back(
|
||||
make_file_record_data(parPath, parStats));
|
||||
|
@ -386,4 +360,11 @@ namespace din {
|
|||
void Indexer::ignore_read_errors (bool parIgnore) {
|
||||
m_local_data->ignore_read_errors = parIgnore;
|
||||
}
|
||||
|
||||
const std::vector<FileRecordData>& Indexer::record_data() const {
|
||||
#if defined(WITH_PROGRESS_FEEDBACK)
|
||||
assert(m_local_data->done_count == m_local_data->file_count);
|
||||
#endif
|
||||
return m_local_data->paths;
|
||||
}
|
||||
} //namespace din
|
||||
|
|
|
@ -20,6 +20,7 @@
|
|||
|
||||
#include <memory>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#if !defined(NDEBUG)
|
||||
# define INDEXER_VERBOSE
|
||||
|
@ -40,9 +41,11 @@ namespace dinlib {
|
|||
} //namespace dinlib
|
||||
|
||||
namespace din {
|
||||
struct FileRecordData;
|
||||
|
||||
class Indexer {
|
||||
public:
|
||||
explicit Indexer ( const dinlib::Settings& parSettings );
|
||||
Indexer ( void );
|
||||
Indexer ( Indexer&& ) = default;
|
||||
Indexer ( const Indexer& ) = delete;
|
||||
~Indexer ( void ) noexcept;
|
||||
|
@ -60,9 +63,9 @@ namespace din {
|
|||
std::condition_variable& step_notify ( void );
|
||||
#endif
|
||||
void calculate_hash ( void );
|
||||
bool add_to_db ( const std::string& parSetName, char parType, bool parForce=false ) const;
|
||||
bool empty ( void ) const;
|
||||
void ignore_read_errors ( bool parIgnore );
|
||||
const std::vector<FileRecordData>& record_data ( void ) const;
|
||||
|
||||
private:
|
||||
struct LocalData;
|
||||
|
|
|
@ -19,6 +19,13 @@
|
|||
# undef WITH_PROGRESS_FEEDBACK
|
||||
#endif
|
||||
|
||||
#include "recorddata.hpp"
|
||||
#include "dindexerConfig.h"
|
||||
#include "filesearcher.hpp"
|
||||
#include "indexer.hpp"
|
||||
#include "dindexer-common/settings.hpp"
|
||||
#include "commandline.hpp"
|
||||
#include "dbbackend.hpp"
|
||||
#include <iostream>
|
||||
#include <iomanip>
|
||||
#include <ciso646>
|
||||
|
@ -30,14 +37,10 @@
|
|||
# include <mutex>
|
||||
# include <condition_variable>
|
||||
#endif
|
||||
#include "dindexerConfig.h"
|
||||
#include "filesearcher.hpp"
|
||||
#include "indexer.hpp"
|
||||
#include "dindexer-common/settings.hpp"
|
||||
#include "commandline.hpp"
|
||||
|
||||
namespace {
|
||||
void run_hash_calculation ( din::Indexer& parIndexer, bool parShowProgress );
|
||||
bool add_to_db ( const std::vector<din::FileRecordData>& parData, const std::string& parSetName, char parType, const dinlib::SettingsDB& parDBSettings, bool parForce=false );
|
||||
} //unnamed namespace
|
||||
|
||||
int main (int parArgc, char* parArgv[]) {
|
||||
|
@ -96,7 +99,7 @@ int main (int parArgc, char* parArgv[]) {
|
|||
|
||||
std::cout << "constructing...\n";
|
||||
|
||||
din::Indexer indexer(settings);
|
||||
din::Indexer indexer;
|
||||
indexer.ignore_read_errors(vm.count("ignore-errors") > 0);
|
||||
fastf::FileSearcher searcher(search_path);
|
||||
fastf::FileSearcher::ConstCharVecType ext, ignore;
|
||||
|
@ -116,7 +119,7 @@ int main (int parArgc, char* parArgv[]) {
|
|||
if (verbose) {
|
||||
std::cout << "Writing to database...\n";
|
||||
}
|
||||
if (not indexer.add_to_db(vm["setname"].as<std::string>(), set_type)) {
|
||||
if (not add_to_db(indexer.record_data(), vm["setname"].as<std::string>(), set_type, settings.db)) {
|
||||
std::cerr << "Not written to DB, likely because a set with the same hash already exists\n";
|
||||
}
|
||||
}
|
||||
|
@ -177,4 +180,23 @@ namespace {
|
|||
#endif
|
||||
}
|
||||
|
||||
bool add_to_db (const std::vector<din::FileRecordData>& parData, const std::string& parSetName, char parType, const dinlib::SettingsDB& parDBSettings, bool parForce) {
|
||||
using din::FileRecordData;
|
||||
using din::SetRecordDataFull;
|
||||
using din::SetRecordData;
|
||||
|
||||
if (not parForce) {
|
||||
const auto& first_hash = parData.front().hash;
|
||||
FileRecordData itm;
|
||||
SetRecordDataFull set;
|
||||
const bool already_in_db = read_from_db(itm, set, parDBSettings, first_hash);
|
||||
if (already_in_db) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
SetRecordData set_data {parSetName, parType};
|
||||
write_to_db(parDBSettings, parData, set_data);
|
||||
return true;
|
||||
}
|
||||
} //unnamed namespace
|
||||
|
|
Loading…
Add table
Reference in a new issue