32#define TYPE_MACRO(cpp_type, return_type, type_id, read_method, W)\
33void joedb::Readonly_Journal::perform_update_##type_id(Writable &writable)\
35 const cpp_type value = read_method();\
36 writable.update_##type_id\
38 table_of_last_operation,\
39 record_of_last_operation,\
40 field_of_last_update,\
62 checkpoint_position(
Header::size),
63 hard_checkpoint_position(
Header::size)
80 throw Exception(
"missing joedb signature");
83 throw Exception(
"unsupported file format version");
88 throw Exception(
"Checkpoint is bigger than file size");
98void joedb::Readonly_Journal::read_checkpoint
101 const std::array<int64_t, 4> &pos,
105 for (
int i = 0; i < 2; i++)
107 if (pos[2 * i] == pos[2 * i + 1] && pos[2 * i] >= hard_checkpoint_position)
109 hard_checkpoint_position = pos[2 * i];
113 for (
int j = 0; j < 2; j++)
115 const int64_t p = pos[2 * i + j];
116 const int64_t neg = (p == INT64_MIN) ? INT64_MIN : -p;
117 if (neg >= checkpoint_position && (file_size < 0 || neg <= file_size))
119 checkpoint_position = neg;
126 if (hard_checkpoint_position > checkpoint_position)
127 checkpoint_position = hard_checkpoint_position;
131void joedb::Readonly_Journal::pull_without_locking()
134 std::array<int64_t, 4> pos;
135 file.full_pread((
char *)&pos,
sizeof(pos), 0);
136 read_checkpoint(pos, -1);
144 const int64_t old_checkpoint = checkpoint_position;
145 pull_without_locking();
146 return checkpoint_position - old_checkpoint;
154 play_until_checkpoint(writable);
165 writable.start_writing(get_position());
166 while(get_position() < checkpoint_position)
169 writable.comment(std::to_string(get_position()));
171 writable.end_writing(get_position());
187 while (get_position() < end)
195 if (get_position() < end)
197 const int64_t writable_position = writable.
get_position();
198 if (get_position() < writable_position)
201 raw_play_until(dummy_writable, writable_position);
204 raw_play_until(writable, end);
218 case operation_t::create_table:
220 const std::string name = safe_read_string();
225 case operation_t::drop_table:
232 case operation_t::rename_table:
235 const std::string name = safe_read_string();
240 case operation_t::add_field:
243 const std::string name = safe_read_string();
244 const Type type = read_type();
245 writable.
add_field(table_id, name, type);
249 case operation_t::drop_field:
257 case operation_t::rename_field:
261 const std::string name = safe_read_string();
266 case operation_t::insert_into:
269 const Record_Id record_id = file_buffer.read_reference();
271 table_of_last_operation = table_id;
272 record_of_last_operation = record_id;
276 case operation_t::delete_from:
279 const Record_Id record_id = file_buffer.read_reference();
284 case operation_t::insert_vector:
287 const Record_Id record_id = file_buffer.read_reference();
288 const size_t size = file_buffer.compact_read<
size_t>();
290 table_of_last_operation = table_id;
291 record_of_last_operation = record_id;
295 case operation_t::delete_vector:
298 const Record_Id record_id = file_buffer.read_reference();
299 const size_t size = file_buffer.compact_read<
size_t>();
304 case operation_t::append:
305 writable.
insert_into(table_of_last_operation, ++record_of_last_operation);
308 #define TYPE_MACRO(cpp_type, return_type, type_id, read_method, W)\
309 case operation_t::update_##type_id:\
310 table_of_last_operation = file_buffer.read_strong_type<Table_Id>();\
311 record_of_last_operation = file_buffer.read_reference();\
312 field_of_last_update = file_buffer.read_strong_type<Field_Id>();\
313 perform_update_##type_id(writable);\
316 case operation_t::update_last_##type_id:\
317 field_of_last_update = file_buffer.read_strong_type<Field_Id>();\
318 perform_update_##type_id(writable);\
321 case operation_t::update_next_##type_id:\
322 ++record_of_last_operation;\
323 perform_update_##type_id(writable);\
327 #define TYPE_MACRO(cpp_type, return_type, type_id, read_method, W)\
328 case operation_t::update_vector_##type_id:\
330 table_of_last_operation = file_buffer.read_strong_type<Table_Id>();\
331 record_of_last_operation = file_buffer.read_reference();\
332 field_of_last_update = file_buffer.read_strong_type<Field_Id>();\
333 const size_t size = file_buffer.compact_read<size_t>();\
336 if (int64_t(size) > checkpoint_position)\
337 throw Exception("update_vector too big");\
339 cpp_type *data = writable.get_own_##type_id##_storage\
341 table_of_last_operation,\
342 record_of_last_operation,\
343 field_of_last_update,\
346 std::vector<cpp_type> buffer;\
349 buffer.resize(size);\
352 else if (to_underlying(record_of_last_operation) < 0 || to_underlying(record_of_last_operation) + size > capacity)\
353 throw Exception("update_vector out of range");\
354 read_vector_of_##type_id(data, size);\
355 writable.update_vector_##type_id\
357 table_of_last_operation,\
358 record_of_last_operation,\
359 field_of_last_update,\
367 case operation_t::custom:
369 const std::string name = safe_read_string();
374 case operation_t::comment:
376 const std::string comment = safe_read_string();
381 case operation_t::timestamp:
383 const int64_t timestamp = file_buffer.read<int64_t>();
388 case operation_t::valid_data:
392 case operation_t::blob:
394 const int64_t size = file_buffer.compact_read<int64_t>();
403 file_buffer.read_data(s.data(), s.size());
408 file_buffer.ignore(size);
414 throw Exception(
"Unexpected operation: get_position() = " + std::to_string(get_position()));
424 if (type_id == Type::Type_Id::reference)
427 return Type(type_id);
434 return file_buffer.safe_read_string(checkpoint_position);
437#define TYPE_MACRO(cpp_type, return_type, type_id, read_method, W)\
438void joedb::Readonly_Journal::read_vector_of_##type_id(cpp_type *data, size_t size)\
440 for (size_t i = 0; i < size; i++)\
441 data[i] = read_method();\
443#define TYPE_MACRO_NO_INT
444#define TYPE_MACRO_NO_FLOAT
447#define TYPE_MACRO(cpp_type, return_type, type_id, read_method, W)\
448void joedb::Readonly_Journal::read_vector_of_##type_id(cpp_type *data, size_t size)\
450 file_buffer.read_data((char *)data, size * sizeof(cpp_type));\
452#define TYPE_MACRO_NO_STRING
453#define TYPE_MACRO_NO_REFERENCE
454#define TYPE_MACRO_NO_BLOB
void full_pread(char *data, size_t size, int64_t offset) const
Fill the buffer, or throw if the end of the file is reached.
void unlock_head() noexcept
Writable with empty insert_vector and delete_vector.
void set_position(int64_t position)
Head_Shared_Lock(const Head_Shared_Lock &)=delete
Head_Shared_Lock(Abstract_File &file)
Head_Shared_Lock & operator=(const Head_Shared_Lock &)=delete
bool is_for_writable_journal() const
void play_until(Writable &writable, int64_t end)
void one_step(Writable &writable)
static constexpr uint32_t format_version
std::string safe_read_string()
Record_Id record_of_last_operation
void replay_with_checkpoint_comments(Writable &writable)
void replay_log(Writable &writable)
void raw_play_until(Writable &writable, int64_t end)
Field_Id field_of_last_update
int64_t checkpoint_position
Readonly_Journal(Journal_Construction_Lock &lock)
Table_Id table_of_last_operation
static Type reference(Table_Id table_id)
Superclass with all joedb journal event listeners as virtual functions.
virtual void insert_vector(Table_Id table_id, Record_Id record_id, size_t size)=0
virtual void insert_into(Table_Id table_id, Record_Id record_id)
virtual void drop_table(Table_Id table_id)
virtual void create_table(const std::string &name)
virtual void drop_field(Table_Id table_id, Field_Id field_id)
virtual void rename_table(Table_Id table_id, const std::string &name)
virtual void delete_vector(Table_Id table_id, Record_Id record_id, size_t size)=0
virtual void soft_checkpoint()
virtual bool wants_blob_data() const
virtual void custom(const std::string &name)
virtual void start_writing(int64_t position)
virtual Blob write_blob(std::string_view data)
virtual void add_field(Table_Id table_id, const std::string &name, Type type)
virtual void delete_from(Table_Id table_id, Record_Id record_id)
virtual void end_writing(int64_t position)
virtual int64_t get_position() const
virtual void comment(const std::string &comment)
virtual void on_blob(Blob blob)
virtual void valid_data()
virtual void rename_field(Table_Id table_id, Field_Id field_id, const std::string &name)
virtual void timestamp(int64_t timestamp)
@ ignore_header
use file size as checkpoint