Joedb 9.5.0
The Journal-Only Embedded Database
Loading...
Searching...
No Matches
Client_Parser.cpp
Go to the documentation of this file.
10
11#ifdef PERSISTENCE_TEST
12#include "joedb/journal/File.h"
13#endif
14
15#include <iostream>
16#include <cstring>
17
18namespace joedb
19{
20 template<typename Writable>
33
34 template<typename Writable>
36 private Writable_Readonly_Client_Data<Writable>,
37 public Readonly_Client
38 {
39 public:
41 (
42 Buffered_File &file,
44 Content_Check content_check
45 ):
47 Readonly_Client(this->data_journal, connection, content_check)
48 {
50 }
51
52 void read_journal() override
53 {
55 }
56 };
57
58#ifdef PERSISTENCE_TEST
59 class Joedb_Client_Data
60 {
61 protected:
62 File data_file;
63 Writable_Journal writable;
64
65 Readonly_Journal data_journal;
66 Connection data_connection;
67
68 public:
69 Joedb_Client_Data(Buffered_File &file):
70 data_file("joedb.joedb", Open_Mode::write_existing_or_create_new),
71 writable(data_file),
72 data_journal(file)
73 {
74 }
75 };
76
77 class Joedb_Client: public Joedb_Client_Data, public Readonly_Client
78 {
79 public:
80 Joedb_Client
81 (
82 Buffered_File &file,
83 Connection &connection,
84 Content_Check content_check
85 ):
86 Joedb_Client_Data(file),
87 Readonly_Client(data_journal, connection, content_check)
88 {
89 read_journal();
90 }
91
92 void read_journal() override
93 {
94 this->data_journal.play_until_checkpoint(this->writable);
95 }
96 };
97#endif
98
99 ////////////////////////////////////////////////////////////////////////////
101 ////////////////////////////////////////////////////////////////////////////
102 (
103 Open_Mode default_open_mode,
104 DB_Type default_db_type
105 ):
106 file_parser(default_open_mode, default_open_mode == Open_Mode::read_existing, true, true),
107 default_open_mode(default_open_mode),
108 default_db_type(default_db_type)
109 {
110 }
111
112 ////////////////////////////////////////////////////////////////////////////
113 void Client_Parser::print_help(std::ostream &out) const
114 ////////////////////////////////////////////////////////////////////////////
115 {
116 out << " [--check ";
117
118 for (size_t i = 0; i < std::size(check_string); i++)
119 {
120 if (i > 0)
121 out << '|';
122 out << check_string[i];
123 if (size_t(default_content_check) == i)
124 out << '*';
125 }
126
127 out << "] [--db ";
128
129 for (size_t i = 0; i < std::size(db_string); i++)
130 {
131 if (i > 0)
132 out << '|';
133 out << db_string[i];
134 if (size_t(default_db_type) == i)
135 out << '*';
136 }
137
138
139 out << "] <file> <connection>\n\n";
140
141 file_parser.print_help(out);
142 connection_parser.print_help(out);
143 }
144
145 ////////////////////////////////////////////////////////////////////////////
147 ////////////////////////////////////////////////////////////////////////////
148 (
149 const int argc,
150 const char * const * const argv
151 )
152 {
153 int arg_index = 0;
154
155 Content_Check content_check = default_content_check;
156 if (arg_index + 1 < argc && std::strcmp(argv[arg_index], "--check") == 0)
157 {
158 arg_index++;
159 for (size_t i = 0; i < std::size(check_string); i++)
160 if (std::strcmp(argv[arg_index], check_string[i]) == 0)
161 content_check = Content_Check(i);
162 arg_index++;
163 }
164 std::cerr << "content_check = " << check_string[int(content_check)] << '\n';
165
166 DB_Type db_type = default_db_type;
167 if (arg_index + 1 < argc && std::strcmp(argv[arg_index], "--db") == 0)
168 {
169 arg_index++;
170 for (size_t i = 0; i < std::size(db_string); i++)
171 if (std::strcmp(argv[arg_index], db_string[i]) == 0)
172 db_type = DB_Type(i);
173 arg_index++;
174 }
175 std::cerr << "db_type = " << db_string[int(db_type)] << '\n';
176
177 file_parser.parse
178 (
179 std::cerr,
180 argc,
181 argv,
182 arg_index
183 );
184
185 Buffered_File *client_file = file_parser.get_file();
186
187 Connection &connection = connection_parser.build
188 (
189 argc - arg_index,
190 argv + arg_index,
191 client_file
192 );
193
194 if (!client_file)
195 client_file = dynamic_cast<Buffered_File *>(&connection);
196
197 if (!client_file)
198 throw Exception("server file must be used with a network or ssh connection");
199
200 std::cerr << "Creating client... ";
201
202 if (db_type == DB_Type::none)
203 {
204 if (client_file->is_readonly())
205 {
206 client.reset
207 (
208 new Readonly_Journal_Client(*client_file, connection, content_check)
209 );
210 }
211 else
212 {
213 client.reset
214 (
215 new Writable_Journal_Client(*client_file, connection, content_check)
216 );
217 }
218 }
219 else if (db_type == DB_Type::interpreted)
220 {
221 if (client_file->is_readonly())
222 {
223 client.reset
224 (
225 new Readonly_Database_Client(*client_file, connection, content_check)
226 );
227 }
228 else
229 {
230 client.reset
231 (
232 new Writable_Database_Client(*client_file, connection, content_check)
233 );
234 }
235 }
236 else if (db_type == DB_Type::sql)
237 {
239 (
240 *client_file,
241 connection,
242 content_check
243 ));
244 }
245 else if (db_type == DB_Type::dump)
246 {
248 (
249 *client_file,
250 connection,
251 content_check
252 ));
253 }
254#ifdef PERSISTENCE_TEST
255 else if (db_type == DB_Type::joedb)
256 {
257 client.reset(new Joedb_Client
258 (
259 *client_file,
260 connection,
261 content_check
262 ));
263 }
264#endif
265 else
266 throw Exception("unsupported db type");
267
268 std::cerr << "OK\n";
269
270 return *client;
271 }
272}
bool is_readonly() const noexcept
Client_Parser(Open_Mode default_open_mode, DB_Type default_db_type)
Client & parse(int argc, const char *const *argv)
void print_help(std::ostream &out) const
Handle concurrent access to a file with a joedb::Connection.
Definition Client.h:12
Connection & connection
Definition Client.h:16
Specialized client for read-only files.
void play_until_checkpoint(Writable &writable)
Writable_Readonly_Client_Data(Buffered_File &file)
Writable_Readonly_Client(Buffered_File &file, Connection &connection, Content_Check content_check)
Content_Check
Definition Connection.h:19
Open_Mode
Definition Open_Mode.h:8
@ read_existing
fails if does not exist
Definition Blob.h:7