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::MetadataManager Class Reference
Collaboration diagram for duckdb::MetadataManager:

Public Member Functions

 MetadataManager (BlockManager &block_manager, BufferManager &buffer_manager)
BufferManager & GetBufferManager () const
MetadataHandle AllocateHandle ()
MetadataHandle Pin (const MetadataPointer &pointer)
MetadataHandle Pin (const QueryContext &context, const MetadataPointer &pointer)
MetaBlockPointer GetDiskPointer (const MetadataPointer &pointer, uint32_t offset=0)
MetadataPointer FromDiskPointer (MetaBlockPointer pointer)
MetadataPointer RegisterDiskPointer (MetaBlockPointer pointer)
void Flush ()
 Flush all blocks to disk.
bool BlockHasBeenCleared (const MetaBlockPointer &ptr)
void MarkBlocksAsModified ()
void ClearModifiedBlocks (const vector< MetaBlockPointer > &pointers)
vector< MetadataBlockInfo > GetMetadataInfo () const
vector< shared_ptr< BlockHandle > > GetBlocks () const
idx_t BlockCount ()
void Write (WriteStream &sink)
void Read (ReadStream &source)
idx_t GetMetadataBlockSize () const

Static Public Member Functions

static BlockPointer ToBlockPointer (MetaBlockPointer meta_pointer, const idx_t metadata_block_size)
static MetaBlockPointer FromBlockPointer (BlockPointer block_pointer, const idx_t metadata_block_size)

Static Public Attributes

static constexpr const idx_t METADATA_BLOCK_COUNT = 64
 The amount of metadata blocks per storage block.

Protected Member Functions

block_id_t AllocateNewBlock (unique_lock< mutex > &block_lock)
block_id_t PeekNextBlockId () const
block_id_t GetNextBlockId () const
void AddBlock (unique_lock< mutex > &block_lock, MetadataBlock new_block, bool if_exists=false)
void AddAndRegisterBlock (unique_lock< mutex > &block_lock, MetadataBlock block)
void ConvertToTransient (unique_lock< mutex > &block_lock, MetadataBlock &block)
MetadataPointer FromDiskPointerInternal (unique_lock< mutex > &block_lock, MetaBlockPointer pointer)

Protected Attributes

BlockManager & block_manager
BufferManager & buffer_manager
mutex block_lock
unordered_map< block_id_t, MetadataBlock > blocks
unordered_map< block_id_t, idx_t > modified_blocks

Constructor & Destructor Documentation

◆ MetadataManager()

duckdb::MetadataManager::MetadataManager ( BlockManager & block_manager,
BufferManager & buffer_manager )
414310 : block_manager(block_manager), buffer_manager(buffer_manager) {
414311}

◆ ~MetadataManager()

duckdb::MetadataManager::~MetadataManager ( )
414313 {
414314}

Member Function Documentation

◆ GetBufferManager()

BufferManager & duckdb::MetadataManager::GetBufferManager ( ) const
inline
48498 {
48499 return buffer_manager;
48500 }

◆ AllocateHandle()

MetadataHandle duckdb::MetadataManager::AllocateHandle ( )
414316 {
414317 // check if there is any free space left in an existing block
414318 // if not allocate a new block
414319 MetadataPointer pointer;
414320 unique_lock<mutex> guard(block_lock);
414321 block_id_t free_block = INVALID_BLOCK;
414322 for (auto &kv : blocks) {
414323 auto &block = kv.second;
414324 D_ASSERT(kv.first == block.block_id);
414325 if (!block.free_blocks.empty()) {
414326 free_block = kv.first;
414327 break;
414328 }
414329 }
414330 guard.unlock();
414331 if (free_block == INVALID_BLOCK || free_block > PeekNextBlockId()) {
414332 free_block = AllocateNewBlock(guard);
414333 } else {
414334 guard.lock();
414335 }
414336 D_ASSERT(guard.owns_lock());
414337 D_ASSERT(free_block != INVALID_BLOCK);
414338
414339 // select the first free metadata block we can find
414340 pointer.block_index = UnsafeNumericCast<idx_t>(free_block);
414341 auto &block = blocks[free_block];
414342 // the block is now dirty
414343 block.dirty = true;
414344 if (block.block->BlockId() < MAXIMUM_BLOCK) {
414345 // this block is a disk-backed block, yet we are planning to write to it
414346 // we need to convert it into a transient block before we can write to it
414347 ConvertToTransient(guard, block);
414348 D_ASSERT(block.block->BlockId() >= MAXIMUM_BLOCK);
414349 }
414350 D_ASSERT(!block.free_blocks.empty());
414351 pointer.index = block.free_blocks.back();
414352 // mark the block as used
414353 block.free_blocks.pop_back();
414354 D_ASSERT(pointer.index < METADATA_BLOCK_COUNT);
414355 guard.unlock();
414356 // pin the block
414357 return Pin(pointer);
414358}
static constexpr const idx_t METADATA_BLOCK_COUNT
The amount of metadata blocks per storage block.
Definition duckdb.hpp:48492

◆ Pin() [1/2]

MetadataHandle duckdb::MetadataManager::Pin ( const MetadataPointer & pointer)
414360 {
414361 return Pin(QueryContext(), pointer);
414362}

◆ Pin() [2/2]

MetadataHandle duckdb::MetadataManager::Pin ( const QueryContext & context,
const MetadataPointer & pointer )
414364 {
414365 D_ASSERT(pointer.index < METADATA_BLOCK_COUNT);
414366 shared_ptr<BlockHandle> block_handle;
414367 {
414368 lock_guard<mutex> guard(block_lock);
414369 auto entry = blocks.find(UnsafeNumericCast<int64_t>(pointer.block_index));
414370 if (entry == blocks.end()) {
414371 throw InternalException("Trying to pin block %llu - but the block did not exist", pointer.block_index);
414372 }
414373 auto &block = entry->second;
414374#ifdef DEBUG
414375 for (auto &free_block : block.free_blocks) {
414376 if (free_block == pointer.index) {
414377 throw InternalException("Pinning block %d.%d but it is marked as a free block", block.block_id,
414378 free_block);
414379 }
414380 }
414381#endif
414382 block_handle = block.block;
414383 }
414384
414385 MetadataHandle handle;
414386 handle.pointer.block_index = pointer.block_index;
414387 handle.pointer.index = pointer.index;
414388 handle.handle = buffer_manager.Pin(block_handle);
414389 return handle;
414390}

◆ GetDiskPointer()

MetaBlockPointer duckdb::MetadataManager::GetDiskPointer ( const MetadataPointer & pointer,
uint32_t offset = 0 )
414458 {
414459 idx_t block_pointer = idx_t(pointer.block_index);
414460 block_pointer |= idx_t(pointer.index) << 56ULL;
414461 return MetaBlockPointer(block_pointer, offset);
414462}
uint64_t idx_t
a saner size_t for loop indices etc
Definition duckdb.hpp:237

◆ FromDiskPointer()

MetadataPointer duckdb::MetadataManager::FromDiskPointer ( MetaBlockPointer pointer)
414472 {
414473 unique_lock<mutex> guard(block_lock);
414474 return FromDiskPointerInternal(guard, pointer);
414475}

◆ RegisterDiskPointer()

MetadataPointer duckdb::MetadataManager::RegisterDiskPointer ( MetaBlockPointer pointer)
414492 {
414493 unique_lock<mutex> guard(block_lock);
414494
414495 auto block_id = pointer.GetBlockId();
414496 MetadataBlock block;
414497 block.block_id = block_id;
414498 AddAndRegisterBlock(guard, std::move(block));
414499 return FromDiskPointerInternal(guard, pointer);
414500}

◆ ToBlockPointer()

BlockPointer duckdb::MetadataManager::ToBlockPointer ( MetaBlockPointer meta_pointer,
const idx_t metadata_block_size )
static
414502 {
414503 BlockPointer result;
414504 result.block_id = meta_pointer.GetBlockId();
414505 result.offset = meta_pointer.GetBlockIndex() * NumericCast<uint32_t>(metadata_block_size) + meta_pointer.offset;
414506 D_ASSERT(result.offset < metadata_block_size * MetadataManager::METADATA_BLOCK_COUNT);
414507 return result;
414508}

◆ FromBlockPointer()

MetaBlockPointer duckdb::MetadataManager::FromBlockPointer ( BlockPointer block_pointer,
const idx_t metadata_block_size )
static
414510 {
414511 if (!block_pointer.IsValid()) {
414512 return MetaBlockPointer();
414513 }
414514 idx_t index = block_pointer.offset / metadata_block_size;
414515 auto offset = block_pointer.offset % metadata_block_size;
414516 D_ASSERT(index < MetadataManager::METADATA_BLOCK_COUNT);
414517 D_ASSERT(offset < metadata_block_size);
414518 MetaBlockPointer result;
414519 result.block_pointer = idx_t(block_pointer.block_id) | index << 56ULL;
414520 result.offset = UnsafeNumericCast<uint32_t>(offset);
414521 return result;
414522}
index

◆ Flush()

void duckdb::MetadataManager::Flush ( )

Flush all blocks to disk.

414528 {
414529 // Write the blocks of the metadata manager to disk.
414530 const idx_t total_metadata_size = GetMetadataBlockSize() * METADATA_BLOCK_COUNT;
414531
414532 unique_lock<mutex> guard(block_lock, std::defer_lock);
414533 for (auto &kv : blocks) {
414534 auto &block = kv.second;
414535 if (!block.dirty) {
414536 if (block.block->BlockId() >= MAXIMUM_BLOCK) {
414537 throw InternalException("Transient blocks must always be marked as dirty");
414538 }
414539 continue;
414540 }
414541 auto block_handle = block.block;
414542 auto handle = buffer_manager.Pin(block_handle);
414543 // zero-initialize the few leftover bytes
414544 memset(handle.Ptr() + total_metadata_size, 0, block_manager.GetBlockSize() - total_metadata_size);
414545 D_ASSERT(kv.first == block.block_id);
414546 if (block_handle->BlockId() >= MAXIMUM_BLOCK) {
414547 // Convert the temporary block to a persistent block.
414548 // we cannot use ConvertToPersistent as another thread might still be reading the block
414549 // so we use the safe version of ConvertToPersistent
414550 auto new_block = block_manager.ConvertToPersistent(QueryContext(), kv.first, std::move(block_handle),
414551 std::move(handle), ConvertToPersistentMode::THREAD_SAFE);
414552
414553 guard.lock();
414554 block.block = std::move(new_block);
414555 guard.unlock();
414556 } else {
414557 // Already a persistent block, so we only need to write it.
414558 D_ASSERT(block.block->BlockId() == block.block_id);
414559 block_manager.Write(QueryContext(), handle.GetFileBuffer(), block.block_id);
414560 }
414561 // the block is no longer dirty
414562 block.dirty = false;
414563 }
414564}

◆ BlockHasBeenCleared()

bool duckdb::MetadataManager::BlockHasBeenCleared ( const MetaBlockPointer & ptr)
414690 {
414691 unique_lock<mutex> guard(block_lock);
414692 auto block_id = pointer.GetBlockId();
414693 auto block_index = pointer.GetBlockIndex();
414694 auto entry = modified_blocks.find(block_id);
414695 if (entry == modified_blocks.end()) {
414696 throw InternalException("BlockHasBeenCleared - Block id %llu not found in modified_blocks", block_id);
414697 }
414698 auto &modified_list = entry->second;
414699 return (modified_list & (1ULL << block_index)) == 0ULL;
414700}

◆ MarkBlocksAsModified()

void duckdb::MetadataManager::MarkBlocksAsModified ( )
414637 {
414638 unique_lock<mutex> guard(block_lock);
414639 // for any blocks that were modified in the last checkpoint - set them to free blocks currently
414640 for (auto &kv : modified_blocks) {
414641 auto block_id = kv.first;
414642 idx_t modified_list = kv.second;
414643 auto entry = blocks.find(block_id);
414644 D_ASSERT(entry != blocks.end());
414645 auto &block = entry->second;
414646 idx_t current_free_blocks = block.FreeBlocksToInteger();
414647 // merge the current set of free blocks with the modified blocks
414648 idx_t new_free_blocks = current_free_blocks | modified_list;
414649 if (new_free_blocks == NumericLimits<idx_t>::Maximum()) {
414650 // if new free_blocks is all blocks - mark entire block as modified
414651 blocks.erase(entry);
414652
414653 guard.unlock();
414654 block_manager.MarkBlockAsModified(block_id);
414655 guard.lock();
414656 } else {
414657 // set the new set of free blocks
414658 block.FreeBlocksFromInteger(new_free_blocks);
414659 }
414660 }
414661
414662 modified_blocks.clear();
414663
414664 for (auto &kv : blocks) {
414665 auto &block = kv.second;
414666 idx_t free_list = block.FreeBlocksToInteger();
414667 idx_t occupied_list = ~free_list;
414668 modified_blocks[block.block_id] = occupied_list;
414669 }
414670}

◆ ClearModifiedBlocks()

void duckdb::MetadataManager::ClearModifiedBlocks ( const vector< MetaBlockPointer > & pointers)
414672 {
414673 if (pointers.empty()) {
414674 return;
414675 }
414676 unique_lock<mutex> guard(block_lock);
414677 for (auto &pointer : pointers) {
414678 auto block_id = pointer.GetBlockId();
414679 auto block_index = pointer.GetBlockIndex();
414680 auto entry = modified_blocks.find(block_id);
414681 if (entry == modified_blocks.end()) {
414682 throw InternalException("ClearModifiedBlocks - Block id %llu not found in modified_blocks", block_id);
414683 }
414684 auto &modified_list = entry->second;
414685 // unset the bit
414686 modified_list &= ~(1ULL << block_index);
414687 }
414688}

◆ GetMetadataInfo()

vector< MetadataBlockInfo > duckdb::MetadataManager::GetMetadataInfo ( ) const
414702 {
414703 vector<MetadataBlockInfo> result;
414704 unique_lock<mutex> guard(block_lock);
414705 for (auto &block : blocks) {
414706 MetadataBlockInfo block_info;
414707 block_info.block_id = block.second.block_id;
414708 block_info.total_blocks = MetadataManager::METADATA_BLOCK_COUNT;
414709 for (auto free_block : block.second.free_blocks) {
414710 block_info.free_list.push_back(free_block);
414711 }
414712 std::sort(block_info.free_list.begin(), block_info.free_list.end());
414713 result.push_back(std::move(block_info));
414714 }
414715 std::sort(result.begin(), result.end(),
414716 [](const MetadataBlockInfo &a, const MetadataBlockInfo &b) { return a.block_id < b.block_id; });
414717 return result;
414718}

◆ GetBlocks()

vector< shared_ptr< BlockHandle > > duckdb::MetadataManager::GetBlocks ( ) const
414720 {
414721 vector<shared_ptr<BlockHandle>> result;
414722 unique_lock<mutex> guard(block_lock);
414723 for (auto &entry : blocks) {
414724 result.push_back(entry.second.block);
414725 }
414726 return result;
414727}

◆ BlockCount()

idx_t duckdb::MetadataManager::BlockCount ( )
414524 {
414525 return blocks.size();
414526}

◆ Write()

void duckdb::MetadataManager::Write ( WriteStream & sink)
414566 {
414567 sink.Write<uint64_t>(blocks.size());
414568 for (auto &kv : blocks) {
414569 kv.second.Write(sink);
414570 }
414571}
::uint64_t uint64_t

◆ Read()

void duckdb::MetadataManager::Read ( ReadStream & source)
414573 {
414574 auto block_count = source.Read<uint64_t>();
414575 for (idx_t i = 0; i < block_count; i++) {
414576 auto block = MetadataBlock::Read(source);
414577
414578 unique_lock<mutex> guard(block_lock);
414579 auto entry = blocks.find(block.block_id);
414580 if (entry == blocks.end()) {
414581 // block does not exist yet
414582 AddAndRegisterBlock(guard, std::move(block));
414583 } else {
414584 // block was already created - only copy over the free list
414585 entry->second.free_blocks = std::move(block.free_blocks);
414586 }
414587 }
414588}

◆ GetMetadataBlockSize()

idx_t duckdb::MetadataManager::GetMetadataBlockSize ( ) const
414595 {
414596 return AlignValueFloor(block_manager.GetBlockSize() / METADATA_BLOCK_COUNT);
414597}

◆ AllocateNewBlock()

block_id_t duckdb::MetadataManager::AllocateNewBlock ( unique_lock< mutex > & block_lock)
protected
414414 {
414415 D_ASSERT(!block_lock.owns_lock());
414416 auto new_block_id = GetNextBlockId();
414417
414418 MetadataBlock new_block;
414419 auto handle = buffer_manager.Allocate(MemoryTag::METADATA, &block_manager, false);
414420 new_block.block = handle.GetBlockHandle();
414421 new_block.block_id = new_block_id;
414422 for (idx_t i = 0; i < METADATA_BLOCK_COUNT; i++) {
414423 new_block.free_blocks.push_back(NumericCast<uint8_t>(METADATA_BLOCK_COUNT - i - 1));
414424 }
414425 new_block.dirty = true;
414426 // zero-initialize the handle
414427 memset(handle.Ptr(), 0, block_manager.GetBlockSize());
414428
414429 block_lock.lock();
414430 AddBlock(block_lock, std::move(new_block));
414431 return new_block_id;
414432}

◆ PeekNextBlockId()

block_id_t duckdb::MetadataManager::PeekNextBlockId ( ) const
protected
414729 {
414730 return block_manager.PeekFreeBlockId();
414731}

◆ GetNextBlockId()

block_id_t duckdb::MetadataManager::GetNextBlockId ( ) const
protected
414733 {
414734 return block_manager.GetFreeBlockIdForCheckpoint();
414735}

◆ AddBlock()

void duckdb::MetadataManager::AddBlock ( unique_lock< mutex > & block_lock,
MetadataBlock new_block,
bool if_exists = false )
protected
414434 {
414435 D_ASSERT(block_lock.owns_lock());
414436 if (blocks.find(new_block.block_id) != blocks.end()) {
414437 if (if_exists) {
414438 return;
414439 }
414440 throw InternalException("Block id with id %llu already exists", new_block.block_id);
414441 }
414442 blocks[new_block.block_id] = std::move(new_block);
414443}

◆ AddAndRegisterBlock()

void duckdb::MetadataManager::AddAndRegisterBlock ( unique_lock< mutex > & block_lock,
MetadataBlock block )
protected
414445 {
414446 if (block.block) {
414447 throw InternalException("Calling AddAndRegisterBlock on block that already exists");
414448 }
414449 if (block.block_id >= MAXIMUM_BLOCK) {
414450 throw InternalException("AddAndRegisterBlock called with a transient block id");
414451 }
414452 block_lock.unlock();
414453 block.block = block_manager.RegisterBlock(block.block_id);
414454 block_lock.lock();
414455 AddBlock(block_lock, std::move(block), true);
414456}

◆ ConvertToTransient()

void duckdb::MetadataManager::ConvertToTransient ( unique_lock< mutex > & block_lock,
MetadataBlock & block )
protected
414392 {
414393 D_ASSERT(block_lock.owns_lock());
414394 auto old_block = metadata_block.block;
414395 block_lock.unlock();
414396 // pin the old block
414397 auto old_buffer = buffer_manager.Pin(old_block);
414398
414399 // allocate a new transient block to replace it
414400 auto new_buffer = buffer_manager.Allocate(MemoryTag::METADATA, &block_manager, false);
414401 auto new_block = new_buffer.GetBlockHandle();
414402
414403 // copy the data to the transient block
414404 memcpy(new_buffer.Ptr(), old_buffer.Ptr(), block_manager.GetBlockSize());
414405
414406 // unregister the old block
414407 block_manager.UnregisterBlock(metadata_block.block_id);
414408
414409 block_lock.lock();
414410 metadata_block.block = std::move(new_block);
414411 metadata_block.dirty = true;
414412}

◆ FromDiskPointerInternal()

MetadataPointer duckdb::MetadataManager::FromDiskPointerInternal ( unique_lock< mutex > & block_lock,
MetaBlockPointer pointer )
protected
414477 {
414478 auto block_id = pointer.GetBlockId();
414479 auto index = pointer.GetBlockIndex();
414480
414481 auto entry = blocks.find(block_id);
414482 if (entry == blocks.end()) { // LCOV_EXCL_START
414483 throw InternalException("Failed to load metadata pointer (id %llu, idx %llu, ptr %llu)\n", block_id, index,
414484 pointer.block_pointer);
414485 } // LCOV_EXCL_STOP
414486 MetadataPointer result;
414487 result.block_index = UnsafeNumericCast<idx_t>(block_id);
414488 result.index = UnsafeNumericCast<uint8_t>(index);
414489 return result;
414490}

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