crawlserv++  [under development]
Application for crawling and analyzing textual content of websites.
Database.hpp
Go to the documentation of this file.
1 /*
2  *
3  * ---
4  *
5  * Copyright (C) 2021 Anselm Schmidt (ans[ät]ohai.su)
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation, either version 3 of the License, or
10  * (at your option) any later version in addition to the terms of any
11  * licences already herein identified.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program. If not, see <https://www.gnu.org/licenses/>.
20  *
21  * ---
22  *
23  * Database.hpp
24  *
25  * Database functionality for a single thread.
26  *
27  * Only implements module-independent functionality. For module-specific functionality use the
28  * child classes of the Wrapper::Database interface instead.
29  *
30  * Created on: Oct 22, 2018
31  * Author: ans
32  */
33 
34 #ifndef MODULE_DATABASE_HPP_
35 #define MODULE_DATABASE_HPP_
36 
37 #include "../Helper/DateTime.hpp"
38 #include "../Helper/FileSystem.hpp"
39 #include "../Helper/Memory.hpp"
40 #include "../Helper/Utf8.hpp"
41 #include "../Main/Database.hpp"
42 #include "../Main/Exception.hpp"
43 #include "../Struct/DatabaseSettings.hpp"
44 #include "../Struct/ModuleOptions.hpp"
45 
46 #include <cstddef> // std::size_t
47 #include <cstdint> // std::uint8_t, std::uint64_t
48 #include <fstream> // std::flush, std::ofstream
49 #include <limits> // std::numeric_limits
50 #include <memory> // std::unique_ptr
51 #include <queue> // std::queue
52 #include <string> // std::string, std::to_string
53 #include <string_view> // std::string_view
54 
55 namespace crawlservpp::Wrapper {
56 
57  class Database;
58 
59 } /* namespace crawlservpp::Wrapper */
60 
61 namespace crawlservpp::Module {
62 
63  /*
64  * CONSTANTS
65  */
66 
69 
71  inline constexpr auto sqlArg1{1};
72 
74  inline constexpr auto sqlArg2{2};
75 
77  inline constexpr auto sqlArg3{3};
78 
80 
81  /*
82  * DECLARATION
83  */
84 
86 
91  class Database : public Main::Database {
93  friend class Wrapper::Database;
94 
95  // for convenience
98 
99  using SqlResultSetPtr = std::unique_ptr<sql::ResultSet>;
100 
101  public:
104 
105  Database(const DatabaseSettings& dbSettings, const std::string& dbModule);
106  ~Database() override;
107 
111 
112  void setOptions(const ModuleOptions& moduleOptions);
113  void setThreadId(std::uint64_t id);
114  void setLogging(std::uint8_t level, std::uint8_t min, std::uint8_t verbose);
115 
119 
120  void prepare();
121 
125 
126  void log(std::uint8_t level, const std::string& logEntry);
127  void log(std::uint8_t level, std::queue<std::string>& logEntries);
128  bool isLogLevel(uint8_t level) const;
129 
133 
134  void setThreadStatusMessage(
135  std::uint64_t threadId,
136  bool threadPaused,
137  const std::string& threadStatusMessage
138  );
139  void setThreadProgress(
140  std::uint64_t threadId,
141  float threadProgress,
142  std::uint64_t threadRunTime
143  );
144  void setThreadLast(
145  std::uint64_t threadId,
146  std::uint64_t threadLast,
147  std::uint64_t threadProcessed
148  );
149 
151 
154 
158 
161  Database(Database&) = delete;
162 
164  Database& operator=(Database&) = delete;
165 
167  Database(Database&&) = delete;
168 
170  Database& operator=(Database&&) = delete;
171 
173 
174  private:
175  // general thread options
176  ModuleOptions options;
177  std::string threadIdString;
178  std::string websiteIdString;
179  std::string urlListIdString;
180  std::uint8_t loggingLevel{std::numeric_limits<std::uint8_t>::max() - 1};
181  std::uint8_t loggingMin{1};
182  std::uint8_t loggingVerbose{std::numeric_limits<std::uint8_t>::max()};
183  std::ofstream loggingFile;
184  bool debugLogging{false};
185  const std::string_view debugDir;
186 
187  // private helper function
188  void initDebugLogging();
189 
190  // IDs of prepared SQL statements
191  struct _ps {
192  std::size_t setThreadStatusMessage{};
193  std::size_t setThreadProgress{};
194  std::size_t setThreadLast{};
195  } ps;
196  };
197 
198 } /* namespace crawlservpp::Module */
199 
200 #endif /* MODULE_DATABASE_HPP_ */
constexpr auto sqlArg2
Second argument in a SQL query.
Definition: Database.hpp:97
Class handling database access for threads.
Definition: Database.hpp:91
#define MAIN_EXCEPTION_CLASS()
Macro used to easily define classes for general exceptions.
Definition: Exception.hpp:50
Class handling database access for the command-and-control and its threads.
Definition: Database.hpp:366
Module options containing the thread ID, as well as ID and namespace of website and URL list used by ...
Definition: ModuleOptions.hpp:40
constexpr auto sqlArg3
Third argument in a SQL query.
Definition: Database.hpp:100
Wrapper class providing the database functionality of Module::Database to its child classes...
Definition: Database.hpp:72
Database settings containing its host, port, user, password, schema, and compression.
Definition: DatabaseSettings.hpp:48
Namespace for RAII wrappers and Wrapper::Database.
Definition: Database.hpp:109
constexpr auto sqlArg1
First argument in a SQL query.
Definition: Database.hpp:94
constexpr auto debugDir
The name of the (sub-)directory for debugging.
Definition: Server.hpp:167
Namespace for the different modules run by threads.