Joedb 10.3.0
The Journal-Only Embedded Database
Loading...
Searching...
No Matches
Client_Parser.cpp
Go to the documentation of this file.
8
9#ifdef PERSISTENCE_TEST
10#include "joedb/journal/File.h"
11#endif
12
13#include <iostream>
14#include <cstring>
15
16namespace joedb
17{
18 template<typename Writable_Multiplexer>
20 {
21 private:
22 Writable_Multiplexer writable_multiplexer;
23
24 public:
26 (
29 bool tail,
30 Content_Check content_check,
31 Recovery recovery
32 ):
33 Readonly_Client(file, connection, content_check, recovery),
34 writable_multiplexer(std::cout)
35 {
36 if (tail)
37 writable_multiplexer.set_start_index(1);
38
40
41 if (tail)
42 writable_multiplexer.set_start_index(0);
43 }
44
45 void read_journal() override
46 {
47 Readonly_Journal::play_until_checkpoint(writable_multiplexer);
48 }
49 };
50
51#ifdef PERSISTENCE_TEST
52 class Joedb_Client: public Readonly_Client
53 {
54 private:
55 File data_file;
56 Writable_Journal writable;
57
58 public:
59 Joedb_Client
60 (
61 Abstract_File &file,
62 Connection &connection,
63 Content_Check content_check,
64 Recovery recovery
65 ):
66 Readonly_Client(file, connection, content_check, recovery),
67 data_file("joedb.joedb", Open_Mode::write_existing_or_create_new),
68 writable(data_file)
69 {
70 read_journal();
71 }
72
73 void read_journal() override
74 {
75 this->data_journal.play_until_checkpoint(this->writable);
76 }
77 };
78#endif
79
80 ////////////////////////////////////////////////////////////////////////////
82 ////////////////////////////////////////////////////////////////////////////
83 (
84 Logger &logger,
85 Open_Mode default_open_mode,
86 DB_Type default_db_type,
87 Arguments &arguments
88 ):
89 default_open_mode(default_open_mode),
90 default_db_type(default_db_type),
91 file_parser
92 (
93 default_open_mode,
94 default_open_mode == Open_Mode::read_existing,
95 true,
96 true
97 )
98 {
99 const bool hard_checkpoint = arguments.has_flag("hard_checkpoint");
100
101 static std::vector<const char *> check_string
102 {
103 "none",
104 "fast",
105 "full"
106 };
107
108 const Content_Check content_check = Content_Check
109 (
110 arguments.get_enum_option
111 (
112 "check",
113 check_string,
115 )
116 );
117
118 static std::vector<const char *> recovery_string
119 {
120 "none",
121 "ignore_header",
122 "overwrite"
123 };
124
125 const Recovery recovery = Recovery
126 (
127 arguments.get_enum_option
128 (
129 "recovery",
130 recovery_string,
131 int(Recovery::none)
132 )
133 );
134
135 static std::vector<const char *> db_string
136 {
137 "none",
138 "interpreted",
139 "dump",
140 "dump_tail",
141 "sql",
142 "sql_tail",
143#ifdef PERSISTENCE_TEST
144 "joedb"
145#endif
146 };
147
148 const DB_Type db_type = DB_Type
149 (
150 arguments.get_enum_option
151 (
152 "db",
153 db_string,
154 int(default_db_type)
155 )
156 );
157
158 arguments.add_parameter("<file>");
159 arguments.add_parameter("<connection>");
160
161 if (arguments.get_remaining_count() == 0)
162 return;
163
164 logger.log("hard_checkpoint = " + std::to_string(hard_checkpoint));
165 logger.log("content_check = " + std::string(check_string[int(content_check)]));
166 logger.log("recovery = " + std::string(recovery_string[int(recovery)]));
167 logger.log("db_type = " + std::string(db_string[int(db_type)]));
168
169 Abstract_File *client_file = file_parser.parse(logger, arguments);
170 Connection *connection = connection_parser.build(logger, arguments, client_file);
171
172 if (!client_file)
173 client_file = dynamic_cast<Abstract_File *>(connection);
174
175 if (!client_file)
176 throw Exception("could not create file");
177
178 if (!connection)
179 throw Exception("could not create connection");
180
181 logger.log("creating client");
182
183 if (db_type == DB_Type::none)
184 {
185 if (client_file->is_readonly())
186 {
187 client.reset
188 (
189 new Readonly_Client(*client_file, *connection, content_check, recovery)
190 );
191 }
192 else
193 {
194 client.reset
195 (
197 (
198 *client_file,
199 *connection,
200 content_check,
201 recovery
202 )
203 );
204 }
205 }
206 else if (db_type == DB_Type::interpreted)
207 {
208 if (client_file->is_readonly())
209 {
210 client.reset
211 (
213 (
214 *client_file,
215 *connection,
216 content_check,
217 recovery
218 )
219 );
220 }
221 else
222 {
223 client.reset
224 (
226 (
227 *client_file,
228 *connection,
229 content_check,
231 recovery
232 )
233 );
234 }
235 }
236 else if (db_type == DB_Type::sql || db_type == DB_Type::sql_tail)
237 {
239 (
240 *client_file,
241 *connection,
242 db_type == DB_Type::sql_tail,
243 content_check,
244 recovery
245 ));
246 }
247 else if (db_type == DB_Type::dump || db_type == DB_Type::dump_tail)
248 {
250 (
251 *client_file,
252 *connection,
253 db_type == DB_Type::dump_tail,
254 content_check,
255 recovery
256 ));
257 }
258#ifdef PERSISTENCE_TEST
259 else if (db_type == DB_Type::joedb)
260 {
261 client.reset(new Joedb_Client
262 (
263 *client_file,
264 connection,
265 content_check,
266 recovery
267 ));
268 }
269#endif
270 else
271 throw Exception("unsupported db type");
272
273 {
274 auto *writable_client = dynamic_cast<Writable_Client*>(client.get());
275 if (writable_client)
276 writable_client->set_hard_checkpoint(hard_checkpoint);
277 }
278 }
279}
bool is_readonly() const noexcept
Class for conveniently parsing command-line arguments.
Definition Arguments.h:19
Client_Parser(Logger &logger, Open_Mode default_open_mode, DB_Type default_db_type, Arguments &arguments)
Connection & connection
Definition Client.h:16
Connection * build(Logger &logger, Arguments &arguments, Abstract_File *file) const
Abstract_File * parse(joedb::Logger &log, Arguments &arguments)
Specialized client for read-only files.
void play_until_checkpoint(Writable &writable)
Readonly_Writable_Client(Abstract_File &file, Connection &connection, bool tail, Content_Check content_check, Recovery recovery)
static const Record_Id null
Definition index_types.h:44
Writable specialization of Client.
void set_hard_checkpoint(bool b)
Use hard checkpoints (default = false)
Content_Check
Definition Connection.h:20
Open_Mode
Definition Open_Mode.h:8
@ read_existing
fails if does not exist
@ none
default: fail if file size > checkpoint