Autonomy Software C++ 24.5.1
Welcome to the Autonomy Software repository of the Mars Rover Design Team (MRDT) at Missouri University of Science and Technology (Missouri S&T)! API reference contains the source code and other resources for the development of the autonomy software for our Mars rover. The Autonomy Software project aims to compete in the University Rover Challenge (URC) by demonstrating advanced autonomous capabilities and robust navigation algorithms.
Loading...
Searching...
No Matches
duckdb::PartialBlockManager Class Reference
Collaboration diagram for duckdb::PartialBlockManager:

Public Member Functions

 PartialBlockManager (QueryContext context, BlockManager &block_manager, PartialBlockType partial_block_type, optional_idx max_partial_block_size=optional_idx(), uint32_t max_use_count=DEFAULT_MAX_USE_COUNT)
PartialBlockAllocation GetBlockAllocation (uint32_t segment_size)
void RegisterPartialBlock (PartialBlockAllocation allocation)
 Register a partially filled block that is filled with "segment_size" entries.
void ClearBlocks ()
 Clear remaining blocks without writing them to disk.
void Rollback ()
 Rollback all data written by this partial block manager.
void Merge (PartialBlockManager &other)
 Merge this block manager into another one.
void FlushPartialBlocks ()
 Flush any remaining partial blocks to disk.
unique_ptr< PartialBlock > CreatePartialBlock (ColumnData &data, ColumnSegment &segment, PartialBlockState state, BlockManager &block_manager)
unique_lock< mutex > GetLock ()
block_id_t GetFreeBlockId ()
BlockManager & GetBlockManager () const
 Returns a reference to the underlying block manager.
optional_ptr< ClientContext > GetClientContext () const
 Returns the optional client context.

Static Public Attributes

static constexpr const idx_t DEFAULT_MAX_USE_COUNT = 1u << 20
 Max number of shared references to a block. No effective limit by default.
static constexpr const idx_t MAX_BLOCK_MAP_SIZE = 1u << 31

Protected Member Functions

virtual void AllocateBlock (PartialBlockState &state, uint32_t segment_size)
bool GetPartialBlock (idx_t segment_size, unique_ptr< PartialBlock > &state)
bool HasBlockAllocation (uint32_t segment_size)

Protected Attributes

optional_ptr< ClientContext > context
BlockManager & block_manager
PartialBlockType partial_block_type
mutex partial_block_lock
multimap< idx_t, unique_ptr< PartialBlock > > partially_filled_blocks
uint32_t max_partial_block_size
 The maximum size (in bytes) at which a partial block will be considered a partial block.
uint32_t max_use_count

Detailed Description

Enables sharing blocks across some scope. Scope is whatever we want to share blocks across. It may be an entire checkpoint or just a single row group. In any case, they must share a block manager.

Constructor & Destructor Documentation

◆ PartialBlockManager()

duckdb::PartialBlockManager::PartialBlockManager ( QueryContext context,
BlockManager & block_manager,
PartialBlockType partial_block_type,
optional_idx max_partial_block_size = optional_idx(),
uint32_t max_use_count = DEFAULT_MAX_USE_COUNT )
415363 : context(context.GetClientContext()), block_manager(block_manager), partial_block_type(partial_block_type),
415364 max_use_count(max_use_count) {
415365 if (max_partial_block_size_p.IsValid()) {
415366 max_partial_block_size = NumericCast<uint32_t>(max_partial_block_size_p.GetIndex());
415367 return;
415368 }
415369
415370 // Use the default maximum partial block size with a ratio of 20% free and 80% utilization.
415371 max_partial_block_size = NumericCast<uint32_t>(block_manager.GetBlockSize() / 5 * 4);
415372}
uint32_t max_partial_block_size
The maximum size (in bytes) at which a partial block will be considered a partial block.
Definition duckdb.cpp:11462
optional_ptr< ClientContext > context
Definition duckdb.cpp:11452

◆ ~PartialBlockManager()

duckdb::PartialBlockManager::~PartialBlockManager ( )
virtual
415374 {
415375}

Member Function Documentation

◆ GetBlockAllocation()

PartialBlockAllocation duckdb::PartialBlockManager::GetBlockAllocation ( uint32_t segment_size)

there is! increase the reference count of this block

415377 {
415378 PartialBlockAllocation allocation;
415379 allocation.block_manager = &block_manager;
415380 allocation.allocation_size = segment_size;
415381
415382 // if the block is less than 80% full, we consider it a "partial block"
415383 // which means we will try to fit it with other blocks
415384 // check if there is a partial block available we can write to
415385 if (segment_size <= max_partial_block_size && GetPartialBlock(segment_size, allocation.partial_block)) {
415387 allocation.partial_block->state.block_use_count += 1;
415388 allocation.state = allocation.partial_block->state;
415389 if (partial_block_type == PartialBlockType::FULL_CHECKPOINT) {
415390 block_manager.IncreaseBlockReferenceCount(allocation.state.block_id);
415391 }
415392 } else {
415393 // full block: get a free block to write to
415394 AllocateBlock(allocation.state, segment_size);
415395 }
415396 return allocation;
415397}
bool GetPartialBlock(idx_t segment_size, unique_ptr< PartialBlock > &state)
Definition duckdb.cpp:415424
Here is the call graph for this function:
Here is the caller graph for this function:

◆ RegisterPartialBlock()

void duckdb::PartialBlockManager::RegisterPartialBlock ( PartialBlockAllocation allocation)

Register a partially filled block that is filled with "segment_size" entries.

415446 {
415447 auto &state = allocation.partial_block->state;
415448 D_ASSERT(partial_block_type != PartialBlockType::FULL_CHECKPOINT || state.block_id >= 0);
415449 if (state.block_use_count < max_use_count) {
415450 auto unaligned_size = allocation.allocation_size + state.offset;
415451 auto new_size = AlignValue(unaligned_size);
415452 if (new_size != unaligned_size) {
415453 // register the uninitialized region so we can correctly initialize it before writing to disk
415454 allocation.partial_block->AddUninitializedRegion(unaligned_size, new_size);
415455 }
415456 state.offset = new_size;
415457 auto new_space_left = state.block_size - new_size;
415458 // check if the block is STILL partially filled after adding the segment_size
415459 if (new_space_left >= block_manager.GetBlockSize() - max_partial_block_size) {
415460 // the block is still partially filled: add it to the partially_filled_blocks list
415461 D_ASSERT(allocation.partial_block->state.offset > 0);
415462 partially_filled_blocks.insert(make_pair(new_space_left, std::move(allocation.partial_block)));
415463 }
415464 }
415465 idx_t free_space = state.block_size - state.offset;
415466 auto block_to_free = std::move(allocation.partial_block);
415467 if (!block_to_free && partially_filled_blocks.size() > MAX_BLOCK_MAP_SIZE) {
415468 // Free the page with the least space free.
415469 auto itr = partially_filled_blocks.begin();
415470 block_to_free = std::move(itr->second);
415471 free_space = itr->first;
415472 partially_filled_blocks.erase(itr);
415473 }
415474 // Flush any block that we're not going to reuse.
415475 if (block_to_free) {
415476 block_to_free->Flush(context, free_space);
415477 }
415478}
multimap< idx_t, unique_ptr< PartialBlock > > partially_filled_blocks
Definition duckdb.cpp:11459
static constexpr const idx_t MAX_BLOCK_MAP_SIZE
Definition duckdb.cpp:11408
uint64_t idx_t
a saner size_t for loop indices etc
Definition duckdb.hpp:237
static T AlignValue(T n)
Align value (ceiling) (not for pointer types).
Definition duckdb.hpp:2126
Here is the call graph for this function:
Here is the caller graph for this function:

◆ ClearBlocks()

void duckdb::PartialBlockManager::ClearBlocks ( )

Clear remaining blocks without writing them to disk.

415508 {
415509 for (auto &e : partially_filled_blocks) {
415510 e.second->Clear();
415511 }
415512 partially_filled_blocks.clear();
415513}
Here is the caller graph for this function:

◆ Rollback()

void duckdb::PartialBlockManager::Rollback ( )

Rollback all data written by this partial block manager.

415530 {
415531 ClearBlocks();
415532}
void ClearBlocks()
Clear remaining blocks without writing them to disk.
Definition duckdb.cpp:415508
Here is the call graph for this function:

◆ Merge()

void duckdb::PartialBlockManager::Merge ( PartialBlockManager & other)

Merge this block manager into another one.

415480 {
415481 if (&other == this) {
415482 throw InternalException("Cannot merge into itself");
415483 }
415484 // for each partially filled block in the other manager, check if we can merge it into an existing block in this
415485 // manager
415486 for (auto &e : other.partially_filled_blocks) {
415487 if (!e.second) {
415488 throw InternalException("Empty partially filled block found");
415489 }
415490 auto used_space = NumericCast<uint32_t>(block_manager.GetBlockSize() - e.first);
415491 if (HasBlockAllocation(used_space)) {
415492 // we can merge this block into an existing block - merge them
415493 // merge blocks
415494 auto allocation = GetBlockAllocation(used_space);
415495 allocation.partial_block->Merge(*e.second, allocation.state.offset, used_space);
415496
415497 // re-register the partial block
415498 allocation.state.offset += used_space;
415499 RegisterPartialBlock(std::move(allocation));
415500 } else {
415501 // we cannot merge this block - append it directly to the current block manager
415502 partially_filled_blocks.insert(make_pair(e.first, std::move(e.second)));
415503 }
415504 }
415505 other.partially_filled_blocks.clear();
415506}
void RegisterPartialBlock(PartialBlockAllocation allocation)
Register a partially filled block that is filled with "segment_size" entries.
Definition duckdb.cpp:415446
PartialBlockAllocation GetBlockAllocation(uint32_t segment_size)
Definition duckdb.cpp:415377
Here is the call graph for this function:

◆ FlushPartialBlocks()

void duckdb::PartialBlockManager::FlushPartialBlocks ( )

Flush any remaining partial blocks to disk.

415515 {
415516 for (auto &e : partially_filled_blocks) {
415517 e.second->Flush(context, e.first);
415518 }
415519 partially_filled_blocks.clear();
415520}

◆ CreatePartialBlock()

unique_ptr< PartialBlock > duckdb::PartialBlockManager::CreatePartialBlock ( ColumnData & data,
ColumnSegment & segment,
PartialBlockState state,
BlockManager & block_manager )
415439 {
415440 if (partial_block_type == PartialBlockType::IN_MEMORY_CHECKPOINT) {
415441 return make_uniq<InMemoryPartialBlock>(column_data, segment, state, block_manager);
415442 }
415443 return make_uniq<PartialBlockForCheckpoint>(column_data, segment, state, block_manager);
415444}

◆ GetLock()

unique_lock< mutex > duckdb::PartialBlockManager::GetLock ( )
inline
11437 {
11438 return unique_lock<mutex>(partial_block_lock);
11439 }

◆ GetFreeBlockId()

block_id_t duckdb::PartialBlockManager::GetFreeBlockId ( )
415416 {
415417 if (partial_block_type == PartialBlockType::FULL_CHECKPOINT) {
415418 return block_manager.GetFreeBlockIdForCheckpoint();
415419 } else {
415420 return block_manager.GetFreeBlockId();
415421 }
415422}

◆ GetBlockManager()

BlockManager & duckdb::PartialBlockManager::GetBlockManager ( ) const

Returns a reference to the underlying block manager.

415522 {
415523 return block_manager;
415524}

◆ GetClientContext()

optional_ptr< ClientContext > duckdb::PartialBlockManager::GetClientContext ( ) const

Returns the optional client context.

415526 {
415527 return context;
415528}

◆ AllocateBlock()

void duckdb::PartialBlockManager::AllocateBlock ( PartialBlockState & state,
uint32_t segment_size )
protectedvirtual
415404 {
415405 D_ASSERT(segment_size <= block_manager.GetBlockSize());
415406 if (partial_block_type == PartialBlockType::FULL_CHECKPOINT) {
415407 state.block_id = GetFreeBlockId();
415408 } else {
415409 state.block_id = INVALID_BLOCK;
415410 }
415411 state.block_size = NumericCast<uint32_t>(block_manager.GetBlockSize());
415412 state.offset = 0;
415413 state.block_use_count = 1;
415414}

◆ GetPartialBlock()

bool duckdb::PartialBlockManager::GetPartialBlock ( idx_t segment_size,
unique_ptr< PartialBlock > & state )
protected

Try to obtain a partially filled block that can fit "segment_size" bytes If successful, returns true and returns the block_id and offset_in_block to write to Otherwise, returns false

415424 {
415425 auto entry = partially_filled_blocks.lower_bound(segment_size);
415426 if (entry == partially_filled_blocks.end()) {
415427 return false;
415428 }
415429 // found a partially filled block! fill in the info
415430 partial_block = std::move(entry->second);
415431 partially_filled_blocks.erase(entry);
415432
415433 D_ASSERT(partial_block->state.offset > 0);
415434 D_ASSERT(ValueIsAligned(partial_block->state.offset));
415435 return true;
415436}
Here is the caller graph for this function:

◆ HasBlockAllocation()

bool duckdb::PartialBlockManager::HasBlockAllocation ( uint32_t segment_size)
protected
415399 {
415400 return segment_size <= max_partial_block_size &&
415401 partially_filled_blocks.lower_bound(segment_size) != partially_filled_blocks.end();
415402}

Member Data Documentation

◆ MAX_BLOCK_MAP_SIZE

const idx_t duckdb::PartialBlockManager::MAX_BLOCK_MAP_SIZE = 1u << 31
staticconstexpr

No point letting map size grow unbounded. We'll drop blocks with the least free space first.

◆ context

optional_ptr<ClientContext> duckdb::PartialBlockManager::context
protected

The optional client context in which we use the partial block manager. The SingleFileCheckpointWriter contains a partial block manager, and the destructor of AttachedDatabase invokes CreateCheckpoint with that checkpoint writer. Thus, we're using the optional client context instead of a reference.

◆ partially_filled_blocks

multimap<idx_t, unique_ptr<PartialBlock> > duckdb::PartialBlockManager::partially_filled_blocks
protected

A map of (available space -> PartialBlock) for partially filled blocks This is a multimap because there might be outstanding partial blocks with the same amount of left-over space


The documentation for this class was generated from the following file: