Joedb 10.0.1
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>
20 {
21 private:
22 Writable writable;
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(std::cout)
35 {
36 if (tail)
37 writable.set_start_index(1);
38
40
41 if (tail)
42 writable.set_start_index(0);
43 }
44
45 void read_journal() override
46 {
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 Buffered_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 Open_Mode default_open_mode,
85 DB_Type default_db_type,
86 Arguments &arguments
87 ):
88 default_open_mode(default_open_mode),
89 default_db_type(default_db_type),
90 file_parser
91 (
92 default_open_mode,
93 default_open_mode == Open_Mode::read_existing,
94 true,
95 true
96 )
97 {
98 const bool hard_checkpoint = arguments.has_flag("hard_checkpoint");
99
100 static std::vector<const char *> check_string
101 {
102 "none",
103 "fast",
104 "full"
105 };
106
107 const Content_Check content_check = Content_Check
108 (
109 arguments.get_enum_option
110 (
111 "check",
112 check_string,
114 )
115 );
116
117 static std::vector<const char *> recovery_string
118 {
119 "none",
120 "ignore_header",
121 "overwrite"
122 };
123
124 const Recovery recovery = Recovery
125 (
126 arguments.get_enum_option
127 (
128 "recovery",
129 recovery_string,
130 int(Recovery::none)
131 )
132 );
133
134 static std::vector<const char *> db_string
135 {
136 "none",
137 "interpreted",
138 "dump",
139 "dump_tail",
140 "sql",
141 "sql_tail",
142#ifdef PERSISTENCE_TEST
143 "joedb"
144#endif
145 };
146
147 const DB_Type db_type = DB_Type
148 (
149 arguments.get_enum_option
150 (
151 "db",
152 db_string,
153 int(default_db_type)
154 )
155 );
156
157 arguments.add_parameter("<file>");
158 arguments.add_parameter("<connection>");
159
160 if (arguments.get_remaining_count() == 0)
161 return;
162
163 std::cerr << "hard_checkpoint = " << hard_checkpoint << '\n';
164 std::cerr << "content_check = " << check_string[int(content_check)] << '\n';
165 std::cerr << "recovery = " << recovery_string[int(recovery)] << '\n';
166 std::cerr << "db_type = " << db_string[int(db_type)] << '\n';
167
168 Buffered_File *client_file = file_parser.parse(std::cerr, arguments);
169 Connection *connection = connection_parser.build(arguments, client_file);
170
171 if (!client_file)
172 client_file = dynamic_cast<Buffered_File *>(connection);
173
174 if (!client_file)
175 throw Exception("could not create file");
176
177 if (!connection)
178 throw Exception("could not create connection");
179
180 std::cerr << "Creating client... ";
181
182 if (db_type == DB_Type::none)
183 {
184 if (client_file->is_readonly())
185 {
186 client.reset
187 (
188 new Readonly_Client(*client_file, *connection, content_check, recovery)
189 );
190 }
191 else
192 {
193 client.reset
194 (
196 (
197 *client_file,
198 *connection,
199 content_check,
200 recovery
201 )
202 );
203 }
204 }
205 else if (db_type == DB_Type::interpreted)
206 {
207 if (client_file->is_readonly())
208 {
209 client.reset
210 (
212 (
213 *client_file,
214 *connection,
215 content_check,
216 recovery
217 )
218 );
219 }
220 else
221 {
222 client.reset
223 (
225 (
226 *client_file,
227 *connection,
228 content_check,
230 recovery
231 )
232 );
233 }
234 }
235 else if (db_type == DB_Type::sql || db_type == DB_Type::sql_tail)
236 {
238 (
239 *client_file,
240 *connection,
241 db_type == DB_Type::sql_tail,
242 content_check,
243 recovery
244 ));
245 }
246 else if (db_type == DB_Type::dump || db_type == DB_Type::dump_tail)
247 {
249 (
250 *client_file,
251 *connection,
252 db_type == DB_Type::dump_tail,
253 content_check,
254 recovery
255 ));
256 }
257#ifdef PERSISTENCE_TEST
258 else if (db_type == DB_Type::joedb)
259 {
260 client.reset(new Joedb_Client
261 (
262 *client_file,
263 connection,
264 content_check,
265 recovery
266 ));
267 }
268#endif
269 else
270 throw Exception("unsupported db type");
271
272 {
273 auto *writable_client = dynamic_cast<Writable_Client*>(client.get());
274 if (writable_client)
275 writable_client->set_hard_checkpoint(hard_checkpoint);
276 }
277
278 std::cerr << "OK\n";
279 }
280}
Class for conveniently parsing command-line arguments.
Definition Arguments.h:19
bool is_readonly() const noexcept
Client_Parser(Open_Mode default_open_mode, DB_Type default_db_type, Arguments &arguments)
Connection & connection
Definition Client.h:16
Connection * build(Arguments &arguments, Buffered_File *file) const
Buffered_File * parse(std::ostream &out, Arguments &arguments)
Specialized client for read-only files.
void play_until_checkpoint(Writable &writable)
Readonly_Writable_Client(Buffered_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)
Superclass with all joedb journal event listeners as virtual functions.
Definition Writable.h:17
Content_Check
Definition Connection.h:19
Open_Mode
Definition Open_Mode.h:8
@ read_existing
fails if does not exist
@ none
default: fail if file size > checkpoint
Definition Blob.h:7