Joedb 10.2.1
The Journal-Only Embedded Database
Loading...
Searching...
No Matches
Interpreted_File.cpp
Go to the documentation of this file.
2
3namespace joedb
4{
5 ////////////////////////////////////////////////////////////////////////////
6 void Abstract_Interpreted_File::read_data()
7 ////////////////////////////////////////////////////////////////////////////
8 {
9 interpreter.main_loop(ios, null_stream);
10 ios.clear();
11 ios.seekp(0, std::ios::end);
12
13 if (!interpreter.is_last_line_empty())
14 {
16 (
17 "Interpreted_File: last line of joedbi file must be empty"
18 );
19 }
20
21 journal.soft_checkpoint();
22 }
23
24 ////////////////////////////////////////////////////////////////////////////
26 ////////////////////////////////////////////////////////////////////////////
27 Abstract_File(file.get_mode()),
28 filebuf(file),
29 ios(&filebuf),
30 journal(memory_file),
31 reading_multiplexer{db, journal},
32 interpreter(db, reading_multiplexer, Record_Id::null),
33 interpreter_writable(ios, db),
34 writing_multiplexer{interpreter_writable, db},
35 null_file(Open_Mode::create_new),
36 null_filebuf(null_file),
37 null_stream(&null_filebuf)
38 {
39 interpreter.set_echo(false);
40 interpreter.set_rethrow(true);
41 read_data();
42 }
43
44 ////////////////////////////////////////////////////////////////////////////
46 ////////////////////////////////////////////////////////////////////////////
47 {
48 return memory_file.get_size();
49 }
50
51 ////////////////////////////////////////////////////////////////////////////
53 ////////////////////////////////////////////////////////////////////////////
54 (
55 char *data,
56 size_t size,
57 int64_t offset
58 ) const
59 {
60 return memory_file.pread(data, size, offset);
61 }
62
63 ////////////////////////////////////////////////////////////////////////////
65 ////////////////////////////////////////////////////////////////////////////
66 (
67 const char *buffer,
68 size_t size,
69 int64_t offset
70 )
71 {
72 memory_file.pwrite(buffer, size, offset);
73
74 if (offset < Header::ssize && journal.pull())
75 {
76 journal.play_until_checkpoint(writing_multiplexer);
77 ios.flush();
78 ios.seekg(0, std::ios::end);
79 }
80 }
81
82 ////////////////////////////////////////////////////////////////////////////
84 ////////////////////////////////////////////////////////////////////////////
85 {
87 }
88
89 ////////////////////////////////////////////////////////////////////////////
90 void Abstract_Interpreted_File::shared_lock(int64_t start, int64_t size)
91 ////////////////////////////////////////////////////////////////////////////
92 {
93 filebuf.get_file().shared_lock(start, size);
94 read_data();
95 }
96
97 ////////////////////////////////////////////////////////////////////////////
98 void Abstract_Interpreted_File::exclusive_lock(int64_t start, int64_t size)
99 ////////////////////////////////////////////////////////////////////////////
100 {
101 filebuf.get_file().exclusive_lock(start, size);
102 read_data();
103 }
104
105 ////////////////////////////////////////////////////////////////////////////
106 void Abstract_Interpreted_File::unlock(int64_t start, int64_t size) noexcept
107 ////////////////////////////////////////////////////////////////////////////
108 {
109 filebuf.get_file().unlock(start, size);
110 }
111}
virtual void shared_lock(int64_t start, int64_t size)
Lock a range of bytes for reading (prevents writes, not reads)
virtual void unlock(int64_t start, int64_t size) noexcept
Remove a lock. The range should match the range of a corresponding lock.
virtual void sync()
Write data durably (including file-size change)
virtual void exclusive_lock(int64_t start, int64_t size)
Lock a range of bytes for writing (prevents both writes and reads)
void sync() override
Write data durably (including file-size change)
size_t pread(char *data, size_t size, int64_t offset) const override
Read a range of bytes.
Abstract_Interpreted_File(Abstract_File &file)
void pwrite(const char *buffer, size_t size, int64_t offset) override
Write a range of bytes. Extend file size if necessary.
int64_t get_size() const override
Get the size of the file, or -1 if it is unknown.
void shared_lock(int64_t start, int64_t size) override
Lock a range of bytes for reading (prevents writes, not reads)
void unlock(int64_t start, int64_t size) noexcept override
Remove a lock. The range should match the range of a corresponding lock.
void exclusive_lock(int64_t start, int64_t size) override
Lock a range of bytes for writing (prevents both writes and reads)
void main_loop(std::istream &in, std::ostream &out)
void set_rethrow(bool new_rethrow)
int64_t get_size() const override
Get the size of the file, or -1 if it is unknown.
Definition Memory_File.h:27
https://en.cppreference.com/w/cpp/io/basic_streambuf.html
Definition filebuf.h:17
Abstract_File & get_file()
Definition filebuf.h:50
Open_Mode
Definition Open_Mode.h:8
@ create_new
fails if already exists, locks the file for writing
static constexpr int64_t ssize
Definition Header.h:18