Joedb 10.5.0
The Journal-Only Embedded Database
Loading...
Searching...
No Matches
Readonly_Encoded_File.cpp
Go to the documentation of this file.
2
3namespace joedb
4{
5 //////////////////////////////////////////////////////////////////////////
7 //////////////////////////////////////////////////////////////////////////
8 (
9 char * const buffer,
10 const size_t size,
11 const int64_t offset
12 ) const
13 {
14 const int64_t start = offset;
15 const int64_t end = offset + int64_t(size);
16
17 int64_t actual_end = start;
18
19 for (auto b: db.get_buffer_table())
20 {
21 const int64_t b_start = db.get_offset(b);
22 const int64_t b_size = db.get_size(b);
23
24 if (b_size <= 0)
25 continue;
26
27 const int64_t b_end = int64_t(uint64_t(b_start) + uint64_t(b_size));
28
29 const int64_t intersection_start = std::max(start, b_start);
30 const int64_t intersection_end = std::min(end, b_end);
31
32 if (intersection_start < intersection_end && intersection_start <= actual_end)
33 {
34 if (b != decoded_buffer)
35 {
36 if (uint64_t(b_size) > uint64_t(max_buffer_size))
37 continue;
38
39 const size_t read_buffer_size = size_t(b_size);
40
41 if (read_buffer.size() < read_buffer_size)
42 read_buffer.resize(read_buffer_size);
43
44 decoder.decode
45 (
46 blob_reader.read_blob(db.get_data(b), max_buffer_size),
47 read_buffer.data(),
48 read_buffer_size
49 );
50
51 decoded_buffer = b;
52 }
53
54 std::memcpy
55 (
56 buffer + intersection_start - start,
57 read_buffer.data() + intersection_start - b_start,
58 size_t(intersection_end - intersection_start)
59 );
60
61 if (intersection_end > actual_end)
62 actual_end = intersection_end;
63 }
64 }
65
66 return size_t(actual_end - start);
67 }
68
69 //////////////////////////////////////////////////////////////////////////
71 //////////////////////////////////////////////////////////////////////////
72 (
75 const Abstract_File &blob_reader,
76 Open_Mode mode
77 ):
78 Abstract_File(mode),
79 db(db),
80 blob_reader(blob_reader),
81 decoded_buffer{Record_Id::null},
83 {
84 }
85
86 //////////////////////////////////////////////////////////////////////////
88 //////////////////////////////////////////////////////////////////////////
89 (
92 const Abstract_File &blob_reader
93 ):
95 {
96 }
97
98 //////////////////////////////////////////////////////////////////////////
100 //////////////////////////////////////////////////////////////////////////
101 {
102 uint64_t result = 0;
103
104 for (const auto buffer: db.get_buffer_table())
105 {
106 const uint64_t end = uint64_t(db.get_offset(buffer)) + uint64_t(db.get_size(buffer));
107 if (end > result)
108 result = end;
109 }
110
111 return std::max<int64_t>(0, int64_t(result));
112 }
113}
Abstract_File(Open_Mode mode)
Readonly_Encoded_File(Decoder &decoder, const db::encoded_file::Database &db, const Abstract_File &blob_reader, Open_Mode mode)
static constexpr size_t max_buffer_size
size_t pread(char *buffer, size_t size, int64_t offset) const override
Read a range of bytes.
int64_t get_size() const override
Get the size of the file, or -1 if it is unknown.
Store all the tables of the database.
Definition Database.h:71
Open_Mode
Definition Open_Mode.h:8
@ read_existing
fails if does not exist
Definition Open_Mode.h:9