1 #ifndef DATA_DEDUPLICATION_SERVICE_FILESERVICE_H
2 #define DATA_DEDUPLICATION_SERVICE_FILESERVICE_H
40 template<dbUsageStrategy dbUsageStrategy = use>
41 int dbLoad(std::string_view dbName, std::string_view configurationFile = db_services::cfileName);
50 template<dbUsageStrategy dbUsageStrategy = use>
53 int dbDrop(std::string_view dbName) {
54 auto res = manager_.dropDatabase(dbName);
65 template<dataInsetionStrategy data_insertion_str = PreserveOld>
66 int processDirectory(std::string_view dirPath,
size_t segmentSize,
const hash_function& hash=
SHA_256);
78 template<dataInsetionStrategy data_insertion_str = PreserveOld,
bool existence_checks = true>
79 int processFile(std::string_view filePath,
size_t segmentSize,
const hash_function& hash=
SHA_256);
85 int insertDirEntry(std::string_view dirPath);
90 tl::expected<double,int> getCoefficient();
94 tl::expected<std::array<int, 5>,
int> getDataD();
105 int loadDirectory(std::string_view fromDir, std::string_view toDir);
116 int loadFile(std::string_view fromFile, std::string_view toFile,
123 int deleteFile(std::string_view filePath);
129 int deleteDirectory(std::string_view dirPath);
132 return manager_.checkConnection();
141 template<
typename ResType1,
typename ... Args>
145 return manager_.executeInTransaction(call, std::forward<Args>(args)...);
153 template<
typename ResType1,
typename ... Args>
154 tl::expected<ResType1, int>
157 return manager_.executeInTransaction(call, std::forward<Args>(args)...);
161 manager_.disconnect();
169 template<rootDirectoryHandlingStrategy dir_s, dataRetrievalStrategy rr,
bool from_load_dir>
173 fs::path fromFilePath;
174 fs::path parentDirPath;
177 parentDirPath = toFilePath.parent_path();
180 if (!fs::exists(parentDirPath)) {
182 fs::create_directories(parentDirPath);
184 <<
vformat(
"Root directory \"%s\" was created successfully",
185 parentDirPath.c_str());
188 <<
vformat(
"\"%s\" no such file or directory\n", parentDirPath.c_str());
194 if (!fs::is_directory(parentDirPath)) {
195 VLOG(1) <<
vformat(
"\"%s\" is not a directory use procesFile for files\n",
196 parentDirPath.c_str());
202 catch (
const fs::filesystem_error &e) {
203 VLOG(1) <<
vformat(
"Filesystem error : %s , error code %d\n", e.what(), e.code());
206 if (fs::is_directory(toFilePath)) {
207 VLOG(1) <<
vformat(
"Entry %s is not a file use processDirectory for directories\n", toFilePath.c_str());
211 std::basic_ofstream<symbolType> out(toFilePath.c_str());
213 auto streamRes = manager_.getFileStreamed(fromFilePath.string(), out, fileId);
218 VLOG(1) <<
vformat(
"Error occurred during "
219 "file \"%s\" streaming",
220 fromFilePath.c_str());
226 auto delRes = manager_.deleteFile(fromFilePath.string());
229 VLOG(1) <<
vformat(
"Error occurred during "
230 "file \"%s\" deletion",
231 fromFilePath.c_str());
239 template<rootDirectoryHandlingStrategy dir_s, dataRetrievalStrategy rr>
246 if (!fs::exists(toDir)) {
248 fs::create_directories(toDir);
250 <<
vformat(
"Root directory \"%s\" was created successfully", toDir.data());
252 VLOG(1) <<
vformat(
"\"%s\" no such file or directory\n", toDir.data());
258 if (!fs::is_directory(toDir)) {
259 VLOG(1) <<
vformat(
"\"%s\" is not a directory change to_dir path\n",
265 }
catch (
const fs::filesystem_error &e) {
266 VLOG(1) <<
vformat(
"Filesystem error : %s , error code %d\n", e.what(), e.code());
270 if (!fs::is_directory(newDirPath)) {
271 VLOG(1) <<
vformat(
"Entry %s is not a directory use processFile for files\n", newDirPath.c_str());
274 auto files = manager_.getAllFiles(fromDirPath.string());
276 VLOG(1) <<
vformat(
"No files found for directory %s", fromDir.data());
280 for (
const std::pair<db_services::indexType, std::string> &pair:
files) {
281 std::string filePath = pair.second;
283 auto newDirRealPath = newDirPath / fs::path(pair.second).lexically_relative(fromDirPath);
285 auto result = this->
template loadFile<rootDirectoryHandlingStrategy::CreateMain, rr, true>
286 (pair.second, newDirRealPath.string(), pair.first);
288 VLOG(1) <<
vformat(
"Error occurred during "
289 "file \"%s\" retrieval",
290 fromDirPath.c_str());
296 manager_.deleteDirectory(fromDirPath.string());
302 template<dbUsageStrategy str>
305 cString.setDbname(dbName);
306 return dbLoad<str>(cString);
310 template<dbUsageStrategy str>
314 if constexpr (str ==
create) {
316 auto reusult = manager_.createDatabase(cStr.
getDbname());
319 VLOG(1) <<
vformat(
"Error occurred during database \"%s\" creation\n", cStr.
getDbname().data());
323 reusult = manager_.fillSchemas();
327 <<
vformat(
"Error occurred during database's \"%s\" schema's creation\n",
333 auto res = manager_.connectToDb();
336 <<
vformat(
"Error occurred during database's \"%s\" schema's creation\n",
341 VLOG(2) << ((manager_.checkConnection()) ?
"connection established\n" :
"cannot connect\n");
346 template<dataInsetionStrategy strategy,
bool existence_checks>
349 if constexpr (existence_checks) {
351 if (!result.has_value()) {
354 file = result.value();
359 auto size = fs::file_size(file);
362 auto fileId = manager_.createFile(file, size, segmentSize, hash);
370 auto res = manager_.deleteFile(file, fileId);
374 <<
vformat(
"Error occurred during insert/replace.\n File path \"%s\"!",
378 fileId = manager_.createFile(file, size, segmentSize, hash);
383 <<
vformat(
"Error occurred during file creation.\n File path \"%s\"!", file.c_str());
386 std::basic_ifstream<symbolType> in(file);
389 auto res1 = manager_.insertFileFromStream(file, in, segmentSize, size,hash);
394 <<
vformat(
"Error occurred during file contents streaming.\n File path \"%s\"!",
399 res1 = manager_.finishFileProcessing(file, fileId);
404 <<
vformat(
"Error occurred during file contents processing.\n File path \"%s\"!",
412 template<dataInsetionStrategy strategy>
417 if (!result.has_value()) {
422 auto dd = fs::canonical(pp).string();
427 auto res1=insertDirEntry(dd);
433 for (
const auto &entry: fs::recursive_directory_iterator(pp)) {
434 if (!fs::is_directory(entry)) {
435 auto file = fs::canonical(entry.path()).string();
437 auto results = this->
template processFile<strategy, false>(file, segmentSize, hash);
448 auto dd = fs::canonical(entry.path()).string();
449 auto results=insertDirEntry(dd);
Database manager that handles database management.
Definition: dbManager.h:39
this class handles file/directory management and uses dbManager to perform calls
Definition: FileService.h:24
void disconnect()
Definition: FileService.h:160
int processFile(std::string_view filePath, size_t segmentSize, const hash_function &hash=SHA_256)
Definition: FileService.h:347
tl::expected< ResType1, int > executeInTransaction(const std::function< ResType1(db_services::trasnactionType &, Args ...)> &call, Args &&... args)
Definition: FileService.h:155
db_services::indexType indexType
Definition: FileService.h:26
int loadDirectory(std::string_view fromDir, std::string_view toDir)
Definition: FileService.h:240
int loadFile(std::string_view fromFile, std::string_view toFile, indexType fileId=paramType::EmptyParameterValue)
Definition: FileService.h:170
int processDirectory(std::string_view dirPath, size_t segmentSize, const hash_function &hash=SHA_256)
Definition: FileService.h:413
int dbDrop(std::string_view dbName)
Definition: FileService.h:53
bool checkConnection()
Definition: FileService.h:131
int dbLoad(std::string_view dbName, std::string_view configurationFile=db_services::cfileName)
Definition: FileService.h:303
tl::expected< ResType1, int > executeInTransaction(ResType1(*call)(db_services::trasnactionType &, Args ...), Args &&... args)
Definition: FileService.h:142
void tak(const std::source_location &location=std::source_location::current())
Definition: clockArray.h:231
void tik(const std::source_location &location=std::source_location::current())
Definition: clockArray.h:256
std::unordered_map< int, fileLoad > files
Definition: common.cpp:16
db_services namespace
Definition: dbCommon.h:17
pqxx::transaction< pqxx::isolation_level::read_committed > trasnactionType
Definition: dbCommon.h:35
int64_t indexType
Definition: dbCommon.h:34
myConnString loadConfiguration(std::string_view filename)
Definition: dbCommon.cpp:106
file services namespace
Definition: FileService.h:14
rootDirectoryHandlingStrategy
Definition: fileUtils.h:55
@ NoCreateMain
will return an error code
Definition: fileUtils.h:57
@ CreateMain
will create this directory using create_directories
Definition: fileUtils.h:59
dataRetrievalStrategy
Definition: fileUtils.h:45
@ Remove
will delete requested data from database
Definition: fileUtils.h:49
@ Persist
will leave data as is
Definition: fileUtils.h:47
tl::expected< std::string, int > checkFileExistence(std::string_view filePath)
Definition: fileUtils.cpp:101
@ PreserveOld
will ignore files that already exist
Definition: fileUtils.h:37
tl::expected< std::string, int > checkDirectoryExistence(std::string_view dirPath)
Definition: fileUtils.cpp:123
@ create
create new database if it doesn't exist
Definition: fileUtils.h:29
fs::path getNormalAbs(const fs::path &path)
hash_function
Definition: HashUtils.h:28
@ SHA_256
Definition: HashUtils.h:30
@ ErrorOccured
Definition: myConcepts.h:50
@ AlreadyExists
Definition: myConcepts.h:49
@ ReturnSucess
Definition: myConcepts.h:51
std::string vformat(const char *zcFormat,...)
Definition: myConcepts.cpp:11
@ EmptyParameterValue
Definition: myConcepts.h:58
clockType gClk
Definition: myConcepts.cpp:9
Structure to store and format connection string.
Definition: myConnString.h:15
const std::string & getDbname() const
Definition: myConnString.h:78