Joedb 10.5.0
The Journal-Only Embedded Database
Loading...
Searching...
No Matches
Abstract_File.cpp
Go to the documentation of this file.
3
4#include <array>
5#include <cstring>
6
7namespace joedb
8{
9 using Default_Buffer = std::array<char, 1 << 12>;
10
11 ////////////////////////////////////////////////////////////////////////////
13 ////////////////////////////////////////////////////////////////////////////
14 (
15 char *data,
16 const size_t size,
17 const int64_t offset
18 ) const
19 {
20 size_t result = 0;
21
22 while (result < size)
23 {
24 const size_t read_count = pread
25 (
26 data + result,
27 size - result,
28 offset + int64_t(result)
29 );
30
31 if (read_count == 0)
33
34 result += read_count;
35 }
36 }
37
38 ////////////////////////////////////////////////////////////////////////////
40 ////////////////////////////////////////////////////////////////////////////
41 (
42 Abstract_File &destination,
43 const int64_t start,
44 const int64_t size
45 ) const
46 {
47 Default_Buffer buffer;
48
49 int64_t done = 0;
50
51 while (size < 0 || done < size)
52 {
53 const size_t asked = size < 0
54 ? buffer.size()
55 : size_t(std::min(int64_t(buffer.size()), size - done));
56 const size_t received = pread(buffer.data(), asked, start + done);
57
58 if (received == 0)
59 {
60 if (size < 0)
61 return;
62 else
64 }
65
66 destination.pwrite(buffer.data(), received, start + done);
67 done += int64_t(received);
68 }
69 }
70
71 ////////////////////////////////////////////////////////////////////////////
73 ////////////////////////////////////////////////////////////////////////////
74 (
75 const Abstract_File &destination,
76 const int64_t from,
77 const int64_t until
78 ) const
79 {
80 Default_Buffer buffer;
81 Default_Buffer destination_buffer;
82
83 for (int64_t current = from; current < until;)
84 {
85 const size_t n0 = pread
86 (
87 buffer.data(),
88 size_t(std::min(int64_t(buffer.size()), until - current)),
89 current
90 );
91
92 size_t n1 = 0;
93
94 while (n1 < n0)
95 {
96 const size_t n = destination.pread
97 (
98 destination_buffer.data() + n1,
99 n0 - n1,
100 current + int64_t(n1)
101 );
102
103 if (n == 0)
104 break;
105
106 n1 += n;
107 }
108
109 if (n1 != n0)
110 return false;
111
112 if (n0 == 0)
114
115 const int diff = std::memcmp
116 (
117 buffer.data(),
118 destination_buffer.data(),
119 n0
120 );
121
122 if (diff)
123 return false;
124
125 current += int64_t(n0);
126 }
127
128 return true;
129 }
130
131 //////////////////////////////////////////////////////////////////////////
133 //////////////////////////////////////////////////////////////////////////
134 {
135 throw Exception("Trying to read past the end of file");
136 }
137
138 ////////////////////////////////////////////////////////////////////////////
139 std::string Abstract_File::read_blob(Blob blob, int64_t max_size) const
140 ////////////////////////////////////////////////////////////////////////////
141 {
142 if
143 (
144 blob.get_position() < 0 ||
145 blob.get_size() < 0 ||
146 blob.get_size() > max_size
147 )
148 {
150 }
151
152 std::string result(size_t(blob.get_size()), 0);
153 full_pread(result.data(), result.size(), blob.get_position());
154 return result;
155 }
156}
virtual void copy_to(Abstract_File &destination, int64_t start, int64_t size) const
Abstract_File(Open_Mode mode)
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.
static void reading_past_end_of_file()
std::string read_blob(Blob blob, int64_t max_size=std::numeric_limits< int64_t >::max()) const
virtual bool equal_to(const Abstract_File &destination, int64_t from, int64_t until) const
virtual size_t pread(char *data, size_t size, int64_t offset) const
Read a range of bytes.
std::array< char, 1<< 12 > Default_Buffer