Joedb 10.2.3
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 (size < 0 || 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 {
32 if (size < 0)
33 return;
34 else
35 reading_past_end_of_file();
36 }
37
38 destination.pwrite(buffer.data(), received, start + done);
39 done += int64_t(received);
40 }
41 }
42
43 ////////////////////////////////////////////////////////////////////////////
45 ////////////////////////////////////////////////////////////////////////////
46 (
47 const Abstract_File &destination,
48 const int64_t from,
49 const int64_t until
50 ) const
51 {
52 Default_Buffer buffer;
53 Default_Buffer destination_buffer;
54
55 for (int64_t current = from; current < until;)
56 {
57 const size_t n0 = pread
58 (
59 buffer.data(),
60 size_t(std::min(int64_t(buffer.size()), until - current)),
61 current
62 );
63
64 size_t n1 = 0;
65
66 while (n1 < n0)
67 {
68 const size_t n = destination.pread
69 (
70 destination_buffer.data() + n1,
71 n0 - n1,
72 current
73 );
74
75 if (n == 0)
76 break;
77
78 n1 += n;
79 }
80
81 if (n1 != n0)
82 return false;
83
84 if (n0 == 0)
85 reading_past_end_of_file();
86
87 const int diff = std::memcmp
88 (
89 buffer.data(),
90 destination_buffer.data(),
91 n0
92 );
93
94 if (diff)
95 return false;
96
97 current += int64_t(n0);
98 }
99
100 return true;
101 }
102
103 //////////////////////////////////////////////////////////////////////////
105 //////////////////////////////////////////////////////////////////////////
106 {
107 throw Exception("Trying to read past the end of file");
108 }
109
110 ////////////////////////////////////////////////////////////////////////////
111 std::string Abstract_File::read_blob(Blob blob) const
112 ////////////////////////////////////////////////////////////////////////////
113 {
114 Async_Reader reader(*this, blob.get_position(), blob.get_end());
115 std::string result(size_t(blob.get_size()), 0);
116 reader.read(result.data(), result.size());
117 if (reader.is_end_of_file())
118 reading_past_end_of_file();
119 return result;
120 }
121}
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