Data_deduplication_service
Service that will use hashes to deduplicate files
dbCommon.h
Go to the documentation of this file.
1 #ifndef DATA_DEDUPLICATION_SERVICE_DBCOMMON_H
2 #define DATA_DEDUPLICATION_SERVICE_DBCOMMON_H
3 
4 #include <fstream>
5 #include <functional>
6 #include <memory>
7 
8 #include <pqxx/pqxx>
9 
10 #include "expected.hpp"
11 #include "myConnString.h"
12 #include "myConcepts.h"
13 #include "HashUtils.h"
14 
15 
17 namespace db_services {
18 
20  using
22 #ifdef IT_test
23  static inline std::string resDirPath = "../../conf/";
24 #else
25  static inline std::string resDirPath = "../../conf/";
26 #endif
27 
29  static inline std::string cfileName = resDirPath.append("config.txt");
30  static constexpr const char *const sqlLimitBreachedState = "23505";
31  static constexpr const char *const sqlFqConstraight = "23503";
32  static const char *const sampleTempDb = "template1";
33 
34  using indexType = int64_t;
35  using trasnactionType = pqxx::transaction<pqxx::isolation_level::read_committed>;
36  using connectionType = pqxx::connection;
37  using conPtr = std::shared_ptr<connectionType>;
38  using resType = pqxx::result;
39  using nonTransType = pqxx::nontransaction;
40 
41 
46  std::string toSpacedPath(std::string_view path);
47 
52  std::string fromSpacedPath(std::string_view path);
53 
58  std::string toTsquerablePath(std::string_view path);
59 
64  bool checkConnection(const conPtr &conn);
65 
70  bool checkConnString(const myConnString &connString);
71 
76  tl::expected <conPtr, returnCodes> connectIfPossible(std::string_view cString);
77 
83  resType terminateAllDbConnections(nonTransType &noTransExec, std::string_view dbName);
84 
90  resType checkDatabaseExistence(nonTransType &noTransExec, std::string_view dbName);
91 
97 
103 
110 
116  resType checkFileExistence(trasnactionType &txn, std::string_view fileName);
117 
123  [[deprecated]]resType checkFilesExistence(trasnactionType &txn, const std::vector<std::filesystem::path> &files);
124 
131  indexType getFileId(trasnactionType &txn, std::string_view fileName);
132 
139  bool doesFileExist(trasnactionType &txn, std::string_view fileName);
140 
146  resType getEntriesForDirectory(trasnactionType &txn, std::string_view dirPath);
147 
153  std::vector<indexType> getFileIdVector(trasnactionType &txn, std::string_view dirPath);
154 
159  tl::expected<indexType, int> getTotalFileSize(trasnactionType &txn);
160 
166 
172 
173  [[deprecated]]resType getFileSizes(trasnactionType &txn);
174 
180  void printRes(resType &rss, std::ostream &out);
181 
186  template<printable T>
187  std::string vecToString(std::vector<T> &vec);
188 
193  void printRowsAffected(const resType &res);
194 
200  resType checkTExistence(db_services::trasnactionType &txn, std::string_view fileName);
201 
206  myConnString loadConfiguration(std::string_view filename);
207 
208  auto static defaultConfiguration = []() {
209  return loadConfiguration(cfileName);
210  };
211 
212  template<typename T, unsigned long size>
213  std::array<T, size> fromString(std::basic_string<T> &string) {
214  std::array<T, size> res;
215  for (int i = 0; i < size; ++i) {
216  res[i] = string[i];
217  }
218  return res;
219  }
220 
221  template<printable T>
222  std::string vecToString(std::vector<T> &vec) {
223  std::stringstream ss;
224  if (vec.empty()) {
225  return ss.str();
226  }
227  int i = 0;
228  for (; i < vec.size() - 1; ++i) {
229  ss << vec[i] << ',';
230  }
231  ss << vec[i];
232  return ss.str();
233  }
234 
235 
244  template<typename ResultType, typename ... Args>
245  tl::expected<ResultType, int>
246  executeInTransaction(conPtr &conn, ResultType (*call)(trasnactionType &, Args ...), Args &&... args) {
247  trasnactionType txn(*conn);//todo if conn is null this one will segfault
248  ResultType res = call(txn, std::forward<Args>(args)...);
249  txn.commit();
250  return res;
251  }
252 
261  template<typename ResultType, typename ... Args>
262  tl::expected<ResultType, int>
263  executeInTransaction(conPtr &conn, const std::function<ResultType(trasnactionType &, Args ...)> &call,
264  Args &&... args) {
265  trasnactionType txn(*conn);
266  ResultType res = call(txn, std::forward<Args>(args)...);
267  txn.commit();
268  return res;
269  }
270 
271 }
272 
273 #endif //DATA_DEDUPLICATION_SERVICE_DBCOMMON_H
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
resType checkFilesExistence(trasnactionType &txn, const std::vector< std::filesystem::path > &files)
Definition: dbCommon.cpp:133
resType getTotalSchemaSizes(trasnactionType &txn)
Definition: dbCommon.cpp:270
resType checkFileExistence(trasnactionType &txn, std::string_view fileName)
Definition: dbCommon.cpp:82
int64_t indexType
Definition: dbCommon.h:34
pqxx::result resType
Definition: dbCommon.h:38
tl::expected< ResultType, int > executeInTransaction(conPtr &conn, ResultType(*call)(trasnactionType &, Args ...), Args &&... args)
Definition: dbCommon.h:246
bool doesFileExist(trasnactionType &txn, std::string_view fileName)
Definition: dbCommon.cpp:94
tl::expected< indexType, int > getTotalFileSize(trasnactionType &txn)
Definition: dbCommon.cpp:285
pqxx::nontransaction nonTransType
Definition: dbCommon.h:39
std::string toSpacedPath(std::string_view path)
Definition: dbCommon.cpp:178
indexType getFileId(trasnactionType &txn, std::string_view fileName)
Definition: dbCommon.cpp:90
tl::expected< conPtr, returnCodes > connectIfPossible(std::string_view cString)
Definition: dbCommon.cpp:10
resType getEntriesForDirectory(trasnactionType &txn, std::string_view dirPath)
Definition: dbCommon.cpp:61
void printRes(resType &rss, std::ostream &out)
Definition: dbCommon.cpp:245
myConnString loadConfiguration(std::string_view filename)
Definition: dbCommon.cpp:106
resType checkDatabaseExistence(nonTransType &noTransExec, std::string_view dbName)
Definition: dbCommon.cpp:49
resType checkSchemas(trasnactionType &txn)
Definition: dbCommon.cpp:54
void printRowsAffected(const resType &res)
Definition: dbCommon.cpp:294
resType getDedupCharacteristics(trasnactionType &txn)
Definition: dbCommon.cpp:205
resType deleteUnusedSegments(trasnactionType &txn)
Definition: dbCommon.cpp:188
bool checkConnection(const conPtr &conn)
Definition: dbCommon.cpp:309
std::string vecToString(std::vector< T > &vec)
Definition: dbCommon.h:222
std::string toTsquerablePath(std::string_view path)
Definition: dbCommon.cpp:148
resType checkTExistence(db_services::trasnactionType &txn, std::string_view fileName)
Definition: dbCommon.cpp:299
resType terminateAllDbConnections(nonTransType &noTransExec, std::string_view dbName)
Definition: dbCommon.cpp:37
std::array< T, size > fromString(std::basic_string< T > &string)
Definition: dbCommon.h:213
resType getFileSizes(trasnactionType &txn)
Definition: dbCommon.cpp:232
std::string fromSpacedPath(std::string_view path)
Definition: dbCommon.cpp:171
std::shared_ptr< connectionType > conPtr
Definition: dbCommon.h:37
indexType checkSegmentCount(trasnactionType &txn)
Definition: dbCommon.cpp:192
pqxx::connection connectionType
Definition: dbCommon.h:36
bool checkConnString(const myConnString &connString)
Definition: dbCommon.cpp:313
std::vector< indexType > getFileIdVector(trasnactionType &txn, std::string_view dirPath)
Definition: dbCommon.cpp:72
std::string getHashStr(std::string_view stringView)
Definition: HashUtils.h:87
concept printable
Definition: myConcepts.h:29
returnCodes
Definition: myConcepts.h:47
std::string vformat(const char *zcFormat,...)
Definition: myConcepts.cpp:11
Structure to store and format connection string.
Definition: myConnString.h:15