Joedb 10.2.1
The Journal-Only Embedded Database
Loading...
Searching...
No Matches
Database_Writable.h
Go to the documentation of this file.
1/////////////////////////////////////////////////////////////////////////////
2//
3// This code was automatically generated by the joedb compiler
4// https://www.joedb.org/
5//
6// Path to compiler: /home/rcoulom/repos/joedb/compcmake/gcc_debug/joedbc
7// Version: 10.2.1
8// joedbc compilation time: Sep 15 2025 20:22:33
9// Generation of this file: 2025-09-15 18:23:01 GMT
10//
11/////////////////////////////////////////////////////////////////////////////
12#ifndef tutorial_rpc_population_Database_Writable_declared
13#define tutorial_rpc_population_Database_Writable_declared
14
18#include "Database.h"
19
20#include <string>
21#include <stdint.h>
22#include <cstring>
23#include <vector>
24
26{
27 namespace detail
28 {
29 extern const char * schema_string;
30 inline constexpr size_t schema_string_size = 89;
31 }
32
33 class container_of_city;
34 class container_of_data;
35
36 /// implement @ref joedb::Writable in a @ref Database
38 {
39 protected:
41 Table_Id current_table_id = Table_Id{0};
42
43 void delete_from(Table_Id table_id, Record_Id record_id) override
44 {
45 if (table_id == Table_Id(1))
46 internal_delete_city(record_id);
47 else if (table_id == Table_Id(2))
48 internal_delete_data(record_id);
49 }
50
51 void insert_into(Table_Id table_id, Record_Id record_id) override
52 {
53 if (to_underlying(record_id) < 0 || (max_record_id && to_underlying(record_id) >= max_record_id))
54 throw_exception("insert_into: too big");
55 if (table_id == Table_Id(1))
56 {
57 if (is_valid_record_id_for_city(record_id))
58 throw_exception("Duplicate insert into table city");
59 if (storage_of_city.size() <= size_t(record_id))
60 storage_of_city.resize(to_underlying(record_id) + 1);
61 internal_insert_city(record_id);
62 }
63 else if (table_id == Table_Id(2))
64 {
65 if (is_valid_record_id_for_data(record_id))
66 throw_exception("Duplicate insert into table data");
67 if (storage_of_data.size() <= size_t(record_id))
68 storage_of_data.resize(to_underlying(record_id) + 1);
69 internal_insert_data(record_id);
70 }
71 }
72
74 (
75 Table_Id table_id,
76 Record_Id record_id,
77 size_t size
78 ) override
79 {
80 joedb::Freedom_Keeper *fk = nullptr;
81
82 if (table_id == Table_Id(1))
83 fk = &storage_of_city.freedom_keeper;
84 else if (table_id == Table_Id(2))
85 fk = &storage_of_data.freedom_keeper;
86
87 if (fk)
88 {
89 JOEDB_RELEASE_ASSERT(fk->is_used_vector(record_id, size));
90 joedb::Writable::delete_vector(table_id, record_id, size);
91 }
92 }
93
95 (
96 Table_Id table_id,
97 Record_Id record_id,
98 size_t size
99 ) override
100 {
101 if
102 (
103 to_underlying(record_id) < 0 ||
104 (max_record_id && (to_underlying(record_id) >= max_record_id || joedb::index_t(size) >= max_record_id))
105 )
106 {
107 throw_exception("insert_vector: null record_id, or too big");
108 }
109 if (table_id == Table_Id(1))
110 {
111 if (storage_of_city.size() < size_t(record_id) + size)
112 storage_of_city.resize(to_underlying(record_id) + size);
113 internal_vector_insert_city(record_id, size);
114 }
115 else if (table_id == Table_Id(2))
116 {
117 if (storage_of_data.size() < size_t(record_id) + size)
118 storage_of_data.resize(to_underlying(record_id) + size);
119 internal_vector_insert_data(record_id, size);
120 }
121 }
122
124 (
125 Table_Id table_id,
126 Record_Id record_id,
127 Field_Id field_id,
128 const std::string& value
129 )
130 override
131 {
132 if (table_id == Table_Id(2))
133 {
134 if (field_id == Field_Id(1))
135 {
136 internal_update_data__city_name(record_id, value);
137 return;
138 }
139 return;
140 }
141 }
142
144 (
145 Table_Id table_id,
146 Record_Id record_id,
147 Field_Id field_id,
148 int64_t value
149 )
150 override
151 {
152 if (table_id == Table_Id(2))
153 {
154 if (field_id == Field_Id(3))
155 {
156 internal_update_data__population(record_id, value);
157 return;
158 }
159 return;
160 }
161 }
162
164 (
165 Table_Id table_id,
166 Record_Id record_id,
167 Field_Id field_id,
168 joedb::Record_Id value
169 )
170 override
171 {
172 if (table_id == Table_Id(2))
173 {
174 if (field_id == Field_Id(2))
175 {
176 internal_update_data__city(record_id, id_of_city(value));
177 return;
178 }
179 return;
180 }
181 }
182
184 (
185 Table_Id table_id,
186 Record_Id record_id,
187 Field_Id field_id,
188 size_t size,
189 const std::string *value
190 )
191 override
192 {
193 if (table_id == Table_Id(2))
194 {
195 if (field_id == Field_Id(1))
196 {
197 internal_update_vector_data__city_name(record_id, size, value);
198 return;
199 }
200 return;
201 }
202 }
203
205 (
206 Table_Id table_id,
207 Record_Id record_id,
208 Field_Id field_id,
209 size_t size,
210 const int64_t *value
211 )
212 override
213 {
214 if (table_id == Table_Id(2))
215 {
216 if (field_id == Field_Id(3))
217 {
218 internal_update_vector_data__population(record_id, size, value);
219 return;
220 }
221 return;
222 }
223 }
224
226 (
227 Table_Id table_id,
228 Record_Id record_id,
229 Field_Id field_id,
230 size_t size,
231 const joedb::Record_Id *value
232 )
233 override
234 {
235 if (table_id == Table_Id(2))
236 {
237 if (field_id == Field_Id(2))
238 {
239 internal_update_vector_data__city(record_id, size, reinterpret_cast<const id_of_city*>(value));
240 return;
241 }
242 return;
243 }
244 }
245
247 (
248 Table_Id table_id,
249 Record_Id record_id,
250 Field_Id field_id,
251 size_t &capacity
252 )
253 override
254 {
255 if (table_id == Table_Id(2))
256 {
257 capacity = size_t(storage_of_data.freedom_keeper.size());
258 if (field_id == Field_Id(1))
259 {
260 return (storage_of_data.field_value_of_city_name.data() + to_underlying(record_id));
261 }
262 return nullptr;
263 }
264 return nullptr;
265 }
266
268 (
269 Table_Id table_id,
270 Record_Id record_id,
271 Field_Id field_id,
272 size_t &capacity
273 )
274 override
275 {
276 if (table_id == Table_Id(2))
277 {
278 capacity = size_t(storage_of_data.freedom_keeper.size());
279 if (field_id == Field_Id(3))
280 {
281 return (storage_of_data.field_value_of_population.data() + to_underlying(record_id));
282 }
283 return nullptr;
284 }
285 return nullptr;
286 }
287
289 (
290 Table_Id table_id,
291 Record_Id record_id,
292 Field_Id field_id,
293 size_t &capacity
294 )
295 override
296 {
297 if (table_id == Table_Id(2))
298 {
299 capacity = size_t(storage_of_data.freedom_keeper.size());
300 if (field_id == Field_Id(2))
301 {
302 return reinterpret_cast<Record_Id *>(storage_of_data.field_value_of_city.data() + to_underlying(record_id));
303 }
304 return nullptr;
305 }
306 return nullptr;
307 }
308
309 void comment(const std::string &comment) override {}
310 void timestamp(int64_t timestamp) override {}
311 void valid_data() override {}
312
313 bool upgrading_schema = false;
316
318 {
319 return schema_file.get_data().size() < detail::schema_string_size;
320 }
321
323 {
324 constexpr size_t pos = joedb::Header::size;
325 const size_t schema_file_size = schema_file.get_data().size();
326
327 if
328 (
329 schema_file_size < pos ||
330 schema_file_size > detail::schema_string_size ||
331 std::memcmp
332 (
333 detail::schema_string + pos,
334 schema_file.get_data().data() + pos,
335 schema_file_size - pos
336 ) != 0
337 )
338 {
339 throw_exception("Trying to open a file with incompatible schema");
340 }
341 }
342
343 void create_table(const std::string &name) override
344 {
348 }
349
350 void drop_table(Table_Id table_id) override
351 {
352 schema_journal.drop_table(table_id);
354 }
355
357 (
358 Table_Id table_id,
359 const std::string &name
360 ) override
361 {
362 schema_journal.rename_table(table_id, name);
364 }
365
367 (
368 Table_Id table_id,
369 const std::string &name,
370 joedb::Type type
371 ) override
372 {
373 schema_journal.add_field(table_id, name, type);
375 }
376
377 void drop_field(Table_Id table_id, Field_Id field_id) override
378 {
379 schema_journal.drop_field(table_id, field_id);
381 }
382
384 (
385 Table_Id table_id,
386 Field_Id field_id,
387 const std::string &name
388 ) override
389 {
390 schema_journal.rename_field(table_id, field_id, name);
392 }
393
394 void custom(const std::string &name) override
395 {
398 }
399
400 public:
405
407 {
408 max_record_id = record_id;
409 }
410
411 int64_t get_schema_checkpoint() const
412 {
414 }
415
417 {
418 max_record_id = size_t(journal.get_checkpoint());
419 journal.replay_log(*this);
420 max_record_id = 0;
421
422 check_schema();
423
425 throw_exception<joedb::Out_Of_Date>("Schema is out of date. Can't upgrade a read-only database.");
426 }
427 };
428}
429
430#endif
bool is_used_vector(Record_Id index, size_t size) const
std::string & get_data()
Definition Memory_File.h:23
void replay_log(Writable &writable)
int64_t get_checkpoint() const
void add_field(Table_Id table_id, const std::string &name, Type type) override
void custom(const std::string &name) override
void create_table(const std::string &name) override
void rename_table(Table_Id table_id, const std::string &name) override
void drop_table(Table_Id table_id) override
void rename_field(Table_Id table_id, Field_Id field_id, const std::string &name) override
void drop_field(Table_Id table_id, Field_Id field_id) override
Superclass with all joedb journal event listeners as virtual functions.
Definition Writable.h:17
virtual void delete_vector(Table_Id table_id, Record_Id record_id, size_t size)=0
Definition Writable.cpp:44
Strongly-typed wrapper around an integer representing a row of the city table.
Definition ids.h:25
implement joedb::Writable in a Database
void drop_field(Table_Id table_id, Field_Id field_id) override
void delete_vector(Table_Id table_id, Record_Id record_id, size_t size) override
void update_reference(Table_Id table_id, Record_Id record_id, Field_Id field_id, joedb::Record_Id value) override
joedb::Record_Id * get_own_reference_storage(Table_Id table_id, Record_Id record_id, Field_Id field_id, size_t &capacity) override
void set_max_record_id(joedb::index_t record_id)
void insert_vector(Table_Id table_id, Record_Id record_id, size_t size) override
void drop_table(Table_Id table_id) override
void timestamp(int64_t timestamp) override
void comment(const std::string &comment) override
void delete_from(Table_Id table_id, Record_Id record_id) override
void rename_table(Table_Id table_id, const std::string &name) override
void initialize_with_readonly_journal(joedb::Readonly_Journal &journal)
void update_int64(Table_Id table_id, Record_Id record_id, Field_Id field_id, int64_t value) override
void create_table(const std::string &name) override
void update_vector_reference(Table_Id table_id, Record_Id record_id, Field_Id field_id, size_t size, const joedb::Record_Id *value) override
void update_vector_string(Table_Id table_id, Record_Id record_id, Field_Id field_id, size_t size, const std::string *value) override
void rename_field(Table_Id table_id, Field_Id field_id, const std::string &name) override
void custom(const std::string &name) override
std::string * get_own_string_storage(Table_Id table_id, Record_Id record_id, Field_Id field_id, size_t &capacity) override
void update_string(Table_Id table_id, Record_Id record_id, Field_Id field_id, const std::string &value) override
void insert_into(Table_Id table_id, Record_Id record_id) override
int64_t * get_own_int64_storage(Table_Id table_id, Record_Id record_id, Field_Id field_id, size_t &capacity) override
void update_vector_int64(Table_Id table_id, Record_Id record_id, Field_Id field_id, size_t size, const int64_t *value) override
void add_field(Table_Id table_id, const std::string &name, joedb::Type type) override
Store all the tables of the database.
Definition Database.h:81
detail::data_of_data storage_of_data
Definition Database.h:99
detail::data_of_city storage_of_city
Definition Database.h:97
void internal_update_vector_data__city_name(Record_Id record_id, size_t size, const std::string *value)
Definition Database.h:148
void internal_update_vector_data__city(Record_Id record_id, size_t size, const id_of_city *value)
Definition Database.h:171
void internal_vector_insert_city(Record_Id record_id, size_t size)
Definition Database.h:121
void internal_update_data__population(Record_Id record_id, int64_t field_value_of_population)
Definition Database.h:184
void internal_delete_city(Record_Id record_id)
Definition Database.h:102
void internal_update_data__city(Record_Id record_id, id_of_city field_value_of_city)
Definition Database.h:161
void internal_delete_data(Record_Id record_id)
Definition Database.h:107
void internal_update_data__city_name(Record_Id record_id, const std::string &field_value_of_city_name)
Definition Database.h:138
bool is_valid_record_id_for_data(Record_Id record_id) const
Definition Database.h:100
bool is_valid_record_id_for_city(Record_Id record_id) const
Definition Database.h:98
void internal_insert_data(Record_Id record_id)
Definition Database.h:126
void internal_update_vector_data__population(Record_Id record_id, size_t size, const int64_t *value)
Definition Database.h:194
static void throw_exception(const std::string &message)
Definition Database.h:88
void internal_vector_insert_data(Record_Id record_id, size_t size)
Definition Database.h:131
void internal_insert_city(Record_Id record_id)
Definition Database.h:116
#define JOEDB_RELEASE_ASSERT(x)
always-tested assertion (release and debug mode)
Definition assert.h:24
ptrdiff_t index_t
Definition index_types.h:18
Automatically generated by joedbc.
Definition Client.h:19
tutorial::id_of_city id_of_city
Definition ids.h:23
static constexpr size_t size
Definition Header.h:19