Joedb 9.5.0
The Journal-Only Embedded Database
Loading...
Searching...
No Matches
Server_File.cpp
Go to the documentation of this file.
2
3namespace joedb
4{
5 ////////////////////////////////////////////////////////////////////////////
6 void Server_File::write_to_body_error()
7 ////////////////////////////////////////////////////////////////////////////
8 {
9 throw Exception("Cannot write to Server_File body");
10 }
11
12 ////////////////////////////////////////////////////////////////////////////
13 void Server_File::write_checkpoint()
14 ////////////////////////////////////////////////////////////////////////////
15 {
16 head.pwrite((const char *)&connection->server_checkpoint, 8, 0);
17 head.pwrite((const char *)&connection->server_checkpoint, 8, 8);
18 }
19
20 ////////////////////////////////////////////////////////////////////////////
21 Server_File::Server_File(const Connector &connector, std::ostream *log):
22 ////////////////////////////////////////////////////////////////////////////
23 Robust_Connection(connector, log),
25 tail_offset(connection->server_checkpoint)
26 {
27 {
28 Writable_Journal journal(head);
29 }
30 write_checkpoint();
31 }
32
33 ////////////////////////////////////////////////////////////////////////////
35 ////////////////////////////////////////////////////////////////////////////
36 (
37 Lock_Action lock_action,
38 Data_Transfer data_transfer,
39 Writable_Journal &client_journal,
40 std::chrono::milliseconds wait
41 )
42 {
43 if (tail.get_size() > 0)
44 throw Exception("Server_File: pulling with non-empty tail");
45
47 (
48 lock_action,
50 client_journal,
51 wait
52 );
53
54 write_checkpoint();
55 client_journal.pull();
56 tail_offset = connection->server_checkpoint;
57
58 return connection->server_checkpoint;
59 }
60
61 ////////////////////////////////////////////////////////////////////////////
63 ////////////////////////////////////////////////////////////////////////////
64 (
65 const Readonly_Journal &client_journal,
66 Content_Check content_check
67 )
68 {
69 if (&client_journal.get_file() != this)
70 throw Exception("Server_File: wrong file");
71 return connection->server_checkpoint;
72 }
73
74 ////////////////////////////////////////////////////////////////////////////
76 ////////////////////////////////////////////////////////////////////////////
77 (
78 const Readonly_Journal &client_journal,
79 const int64_t server_position,
80 const int64_t until_position,
81 const Unlock_Action unlock_action
82 )
83 {
85 (
86 client_journal,
87 server_position,
88 until_position,
89 unlock_action
90 );
91
92 if (connection->server_checkpoint == get_size())
93 {
94 tail_offset = connection->server_checkpoint;
95 tail.resize(0);
96 }
97 else
98 throw Exception("Server_File could not truncate tail after push");
99
100 return connection->server_checkpoint;
101 }
102
103 ////////////////////////////////////////////////////////////////////////////
104 size_t Server_File::pread(char *data, size_t size, int64_t offset) const
105 ////////////////////////////////////////////////////////////////////////////
106 {
107 if (offset < Header::ssize)
108 return head.pread(data, size, offset);
109
110 if (offset < tail_offset)
111 return Robust_Connection::pread(data, size, offset);
112
113 return tail.pread(data, size, offset - tail_offset);
114 }
115
116 ////////////////////////////////////////////////////////////////////////////
117 void Server_File::pwrite(const char *data, size_t size, int64_t offset)
118 ////////////////////////////////////////////////////////////////////////////
119 {
120 if (offset < Header::ssize)
121 {
122 if (offset + size > Header::ssize)
123 write_to_body_error();
124 else
125 head.pwrite(data, size, offset);
126 }
127 else if (offset >= tail_offset)
128 tail.pwrite(data, size, offset - tail_offset);
129 else
130 write_to_body_error();
131 }
132}
Used by Robust_Connection to reconnect after an error.
Definition Connector.h:14
void pwrite(const char *buffer, size_t size, int64_t offset) override
Write a range of bytes. Extend file size if necessary.
Server_Connection that automatically reconnects on error
int64_t pull(Lock_Action lock_action, Data_Transfer data_transfer, Writable_Journal &client_journal, std::chrono::milliseconds wait) override
Pull from the connection.
std::unique_ptr< Server_Connection > connection
size_t pread(char *data, size_t size, int64_t offset) const
int64_t push(const Readonly_Journal &client_journal, int64_t from_checkpoint, int64_t until_checkpoint, Unlock_Action unlock_action) override
Push new data to the connection.
size_t pread(char *data, size_t size, int64_t offset) const override
Read a range of bytes.
int64_t push(const Readonly_Journal &client_journal, int64_t server_position, int64_t until_position, Unlock_Action unlock_action) override
Push new data to the connection.
int64_t pull(Lock_Action lock_action, Data_Transfer data_transfer, Writable_Journal &client_journal, std::chrono::milliseconds wait) override
Pull from the connection.
void pwrite(const char *data, size_t size, int64_t offset) override
Write a range of bytes. Extend file size if necessary.
Server_File(const Connector &connector, std::ostream *log=nullptr)
int64_t handshake(const Readonly_Journal &client_journal, Content_Check content_check) override
Called during Client construction.
Data_Transfer
Definition Connection.h:27
Lock_Action
Definition Connection.h:34
Content_Check
Definition Connection.h:19
Unlock_Action
Definition Connection.h:41
Open_Mode
Definition Open_Mode.h:8
@ write_existing
fails if does not exist or locked, locks the file for writing
Definition Blob.h:7
static constexpr int64_t ssize
Definition Header.h:18