33#define TYPE_MACRO(cpp_type, return_type, type_id, read_method, W)\
34void joedb::Readonly_Journal::perform_update_##type_id(Writable &writable)\
36 const cpp_type value = read_method();\
37 writable.update_##type_id\
39 table_of_last_operation,\
40 record_of_last_operation,\
41 field_of_last_update,\
63 checkpoint_position(
Header::size),
64 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 soft =
neg(pos[2 * i + j]);
116 if (soft >= checkpoint_position && (file_size < 0 || soft <= file_size))
118 checkpoint_position = soft;
125 if (hard_checkpoint_position > checkpoint_position)
126 checkpoint_position = hard_checkpoint_position;
130void joedb::Readonly_Journal::pull_without_locking()
133 std::array<int64_t, 4> pos;
134 file.full_pread((
char *)&pos,
sizeof(pos), 0);
135 read_checkpoint(pos, -1);
143 const int64_t old_checkpoint = checkpoint_position;
144 pull_without_locking();
145 return checkpoint_position - old_checkpoint;
153 play_until_checkpoint(writable);
164 writable.start_writing(get_position());
165 while(get_position() < checkpoint_position)
168 writable.comment(std::to_string(get_position()));
170 writable.end_writing(get_position());
186 while (get_position() < end)
194 if (get_position() < end)
196 const int64_t writable_position = writable.
get_position();
197 if (get_position() < writable_position)
200 raw_play_until(dummy_writable, writable_position);
203 raw_play_until(writable, end);
217 case operation_t::create_table:
219 const std::string name = safe_read_string();
224 case operation_t::drop_table:
231 case operation_t::rename_table:
234 const std::string name = safe_read_string();
239 case operation_t::add_field:
242 const std::string name = safe_read_string();
243 const Type type = read_type();
244 writable.
add_field(table_id, name, type);
248 case operation_t::drop_field:
256 case operation_t::rename_field:
260 const std::string name = safe_read_string();
265 case operation_t::insert_into:
268 const Record_Id record_id = file_buffer.read_reference();
270 table_of_last_operation = table_id;
271 record_of_last_operation = record_id;
275 case operation_t::delete_from:
278 const Record_Id record_id = file_buffer.read_reference();
283 case operation_t::insert_vector:
286 const Record_Id record_id = file_buffer.read_reference();
287 const size_t size = file_buffer.compact_read<
size_t>();
289 table_of_last_operation = table_id;
290 record_of_last_operation = record_id;
294 case operation_t::delete_vector:
297 const Record_Id record_id = file_buffer.read_reference();
298 const size_t size = file_buffer.compact_read<
size_t>();
303 case operation_t::append:
304 writable.
insert_into(table_of_last_operation, ++record_of_last_operation);
307 #define TYPE_MACRO(cpp_type, return_type, type_id, read_method, W)\
308 case operation_t::update_##type_id:\
309 table_of_last_operation = file_buffer.read_strong_type<Table_Id>();\
310 record_of_last_operation = file_buffer.read_reference();\
311 field_of_last_update = file_buffer.read_strong_type<Field_Id>();\
312 perform_update_##type_id(writable);\
315 case operation_t::update_last_##type_id:\
316 field_of_last_update = file_buffer.read_strong_type<Field_Id>();\
317 perform_update_##type_id(writable);\
320 case operation_t::update_next_##type_id:\
321 ++record_of_last_operation;\
322 perform_update_##type_id(writable);\
326 #define TYPE_MACRO(cpp_type, return_type, type_id, read_method, W)\
327 case operation_t::update_vector_##type_id:\
329 table_of_last_operation = file_buffer.read_strong_type<Table_Id>();\
330 record_of_last_operation = file_buffer.read_reference();\
331 field_of_last_update = file_buffer.read_strong_type<Field_Id>();\
332 const size_t size = file_buffer.compact_read<size_t>();\
335 if (int64_t(size) > checkpoint_position)\
336 throw Exception("update_vector too big");\
338 cpp_type *data = writable.get_own_##type_id##_storage\
340 table_of_last_operation,\
341 record_of_last_operation,\
342 field_of_last_update,\
345 std::vector<cpp_type> buffer;\
348 buffer.resize(size);\
351 else if (to_underlying(record_of_last_operation) < 0 || to_underlying(record_of_last_operation) + size > capacity)\
352 throw Exception("update_vector out of range");\
353 read_vector_of_##type_id(data, size);\
354 writable.update_vector_##type_id\
356 table_of_last_operation,\
357 record_of_last_operation,\
358 field_of_last_update,\
366 case operation_t::custom:
368 const std::string name = safe_read_string();
373 case operation_t::comment:
375 const std::string comment = safe_read_string();
380 case operation_t::timestamp:
382 const int64_t timestamp = file_buffer.read<int64_t>();
387 case operation_t::valid_data:
391 case operation_t::blob:
393 const int64_t size = file_buffer.compact_read<int64_t>();
402 file_buffer.read_data(s.data(), s.size());
407 file_buffer.ignore(size);
413 throw Exception(
"Unexpected operation: get_position() = " + std::to_string(get_position()));
423 if (type_id == Type::Type_Id::reference)
426 return Type(type_id);
433 return file_buffer.safe_read_string(checkpoint_position);
436#define TYPE_MACRO(cpp_type, return_type, type_id, read_method, W)\
437void joedb::Readonly_Journal::read_vector_of_##type_id(cpp_type *data, size_t size)\
439 for (size_t i = 0; i < size; i++)\
440 data[i] = read_method();\
442#define TYPE_MACRO_NO_INT
443#define TYPE_MACRO_NO_FLOAT
446#define TYPE_MACRO(cpp_type, return_type, type_id, read_method, W)\
447void joedb::Readonly_Journal::read_vector_of_##type_id(cpp_type *data, size_t size)\
449 file_buffer.read_data((char *)data, size * sizeof(cpp_type));\
451#define TYPE_MACRO_NO_STRING
452#define TYPE_MACRO_NO_REFERENCE
453#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