Joedb 10.2.0
The Journal-Only Embedded Database
Loading...
Searching...
No Matches
Abstract_File.cpp
Go to the documentation of this file.
4
5#include <array>
6#include <cstring>
7
8namespace joedb
9{
10 using Default_Buffer = std::array<char, 1 << 12>;
11
12 ////////////////////////////////////////////////////////////////////////////
14 ////////////////////////////////////////////////////////////////////////////
15 (
16 Abstract_File &destination,
17 const int64_t start,
18 const int64_t size
19 ) const
20 {
21 Default_Buffer buffer;
22
23 int64_t done = 0;
24
25 while (done < size)
26 {
27 const size_t asked = size_t(std::min(int64_t(buffer.size()), size - done));
28 const size_t received = pread(buffer.data(), asked, start + done);
29
30 if (received == 0)
31 reading_past_end_of_file();
32
33 destination.pwrite(buffer.data(), received, start + done);
34 done += int64_t(received);
35 }
36 }
37
38 ////////////////////////////////////////////////////////////////////////////
40 ////////////////////////////////////////////////////////////////////////////
41 (
42 const Abstract_File &destination,
43 const int64_t from,
44 const int64_t until
45 ) const
46 {
47 Default_Buffer buffer;
48 Default_Buffer destination_buffer;
49
50 for (int64_t current = from; current < until;)
51 {
52 const size_t n0 = pread
53 (
54 buffer.data(),
55 size_t(std::min(int64_t(buffer.size()), until - current)),
56 current
57 );
58
59 size_t n1 = 0;
60
61 while (n1 < n0)
62 {
63 const size_t n = destination.pread
64 (
65 destination_buffer.data() + n1,
66 n0 - n1,
67 current
68 );
69
70 if (n == 0)
71 break;
72
73 n1 += n;
74 }
75
76 if (n1 != n0)
77 return false;
78
79 if (n0 == 0)
80 reading_past_end_of_file();
81
82 const int diff = std::memcmp
83 (
84 buffer.data(),
85 destination_buffer.data(),
86 n0
87 );
88
89 if (diff)
90 return false;
91
92 current += int64_t(n0);
93 }
94
95 return true;
96 }
97
98 //////////////////////////////////////////////////////////////////////////
100 //////////////////////////////////////////////////////////////////////////
101 {
102 throw Exception("Trying to read past the end of file");
103 }
104
105 ////////////////////////////////////////////////////////////////////////////
106 std::string Abstract_File::read_blob(Blob blob) const
107 ////////////////////////////////////////////////////////////////////////////
108 {
109 Async_Reader reader(*this, blob.get_position(), blob.get_end());
110 std::string result(size_t(blob.get_size()), 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}
virtual void copy_to(Abstract_File &destination, int64_t start, int64_t size) const
static void reading_past_end_of_file()
std::string read_blob(Blob blob) const
virtual bool equal_to(const Abstract_File &destination, int64_t from, int64_t until) const
size_t read(char *buffer, size_t capacity)
bool is_end_of_file() const
std::array< char, 1<< 12 > Default_Buffer