Joedb 9.1.4
The Journal-Only Embedded Database
Loading...
Searching...
No Matches
Buffered_File.cpp
Go to the documentation of this file.
4
5#include <algorithm>
6
7namespace joedb
8{
9 //////////////////////////////////////////////////////////////////////////
11 //////////////////////////////////////////////////////////////////////////
12 mode(mode),
13 locked_tail
14 (
15 mode != Open_Mode::shared_write &&
17 )
18 {
19 read_buffer_size = 0;
20 buffer.index = 0;
21 }
22
23 ////////////////////////////////////////////////////////////////////////////
25 ////////////////////////////////////////////////////////////////////////////
26 {
27 if (buffer_has_write_data())
28 write_buffer();
29 read_buffer_size = 0;
30 buffer.index = 0;
31 }
32
33 ////////////////////////////////////////////////////////////////////////////
34 void Buffered_File::set_position(int64_t new_position)
35 ////////////////////////////////////////////////////////////////////////////
36 {
37 flush();
39 }
40
41 ////////////////////////////////////////////////////////////////////////////
43 ////////////////////////////////////////////////////////////////////////////
44 (
45 Buffered_File &destination,
46 int64_t start,
47 int64_t size
48 )
49 {
50 set_position(start);
51 destination.set_position(start);
52
53 while (size > 0)
54 {
55 read_buffer();
56 const int64_t copy_size = std::min(size, int64_t(read_buffer_size));
57 destination.sequential_write(buffer.data, size_t(copy_size));
58 size -= copy_size;
59 }
60 }
61
62 ////////////////////////////////////////////////////////////////////////////
63 void Buffered_File::write_string(const std::string &s)
64 ////////////////////////////////////////////////////////////////////////////
65 {
67 write_data(s.data(), s.size());
68 }
69
70 ////////////////////////////////////////////////////////////////////////////
72 ////////////////////////////////////////////////////////////////////////////
73 {
74 const size_t size = compact_read<size_t>();
75 std::string s(size, 0);
76 read_data(s.data(), size);
77 return s;
78 }
79
80 ////////////////////////////////////////////////////////////////////////////
81 std::string Buffered_File::safe_read_string(int64_t max_size)
82 ////////////////////////////////////////////////////////////////////////////
83 {
84 std::string s;
85 const int64_t size = compact_read<int64_t>();
86 if (size > 0 && size < max_size)
87 {
88 s.resize(size_t(size));
89 read_data(s.data(), size_t(size));
90 }
91 return s;
92 }
93
94 ////////////////////////////////////////////////////////////////////////////
96 ////////////////////////////////////////////////////////////////////////////
97 {
98 if (buffer_has_write_data())
99 {
100 Destructor_Logger::write("warning: an unflushed file is being destroyed");
101 try { write_buffer(); } catch (...) {}
102 }
103 }
104
105 ////////////////////////////////////////////////////////////////////////////
106 std::string Buffered_File::read_blob_data(const Blob blob) const
107 ////////////////////////////////////////////////////////////////////////////
108 {
109 Async_Reader reader(*this, blob);
110 std::string result(size_t(reader.get_remaining()), 0);
111 reader.read(result.data(), result.size());
112 if (reader.is_end_of_file())
113 reading_past_end_of_file();
114 return result;
115 }
116}
int64_t get_remaining() const
size_t read(char *buffer, size_t capacity)
bool is_end_of_file() const
size_t index
Definition Buffer.h:21
void set_position(int64_t position)
size_t read_data(char *data, const size_t n)
void write_string(const std::string &s)
virtual std::string read_blob_data(Blob blob) const
std::string read_string()
void destructor_flush() noexcept
void write_data(const char *data, size_t n)
virtual void copy_to(Buffered_File &destination, int64_t start, int64_t size)
std::string safe_read_string(int64_t max_size)
Buffered_File(Open_Mode mode)
static void write(const char *message) noexcept
void sequential_seek(int64_t new_position)
Open_Mode
Definition Open_Mode.h:8
@ shared_write
like write_existing_or_create_new, but does not lock the file, and does not fail if locked
@ read_existing
fails if does not exist
Definition Blob.h:7