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