1 | #include "duckdb/storage/meta_block_reader.hpp" |
---|---|
2 | |
3 | #include <cstring> |
4 | |
5 | using namespace duckdb; |
6 | using namespace std; |
7 | |
8 | MetaBlockReader::MetaBlockReader(BufferManager &manager, block_id_t block_id) |
9 | : manager(manager), handle(nullptr), offset(0), next_block(-1) { |
10 | ReadNewBlock(block_id); |
11 | } |
12 | |
13 | void MetaBlockReader::ReadData(data_ptr_t buffer, idx_t read_size) { |
14 | while (offset + read_size > handle->node->size) { |
15 | // cannot read entire entry from block |
16 | // first read what we can from this block |
17 | idx_t to_read = handle->node->size - offset; |
18 | if (to_read > 0) { |
19 | memcpy(buffer, handle->node->buffer + offset, to_read); |
20 | read_size -= to_read; |
21 | buffer += to_read; |
22 | } |
23 | // then move to the next block |
24 | ReadNewBlock(next_block); |
25 | } |
26 | // we have enough left in this block to read from the buffer |
27 | memcpy(buffer, handle->node->buffer + offset, read_size); |
28 | offset += read_size; |
29 | } |
30 | |
31 | void MetaBlockReader::ReadNewBlock(block_id_t id) { |
32 | handle = manager.Pin(id); |
33 | |
34 | next_block = *((block_id_t *)handle->node->buffer); |
35 | offset = sizeof(block_id_t); |
36 | } |
37 |