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

Public Member Functions

 FixedSizeBuffer (BlockManager &block_manager, MemoryTag memory_tag)
 Constructor for a new in-memory buffer.
 FixedSizeBuffer (BlockManager &block_manager, const idx_t segment_count, const idx_t allocation_size, const BlockPointer &block_pointer)
 Constructor for deserializing buffer metadata from disk.

Static Public Attributes

static constexpr idx_t BASE [] = {0x00000000FFFFFFFF, 0x0000FFFF, 0x00FF, 0x0F, 0x3, 0x1}
 Constants for fast offset calculations in the bitmask.
static constexpr uint8_t SHIFT [] = {32, 16, 8, 4, 2, 1}

Private Member Functions

data_ptr_t GetDeprecated (const bool dirty_p=true)
bool InMemory () const
 Returns true, if the buffer is in-memory.
bool OnDisk () const
 Returns true, if the block is on-disk.
void Serialize (PartialBlockManager &partial_block_manager, const idx_t available_segments, const idx_t segment_size, const idx_t bitmask_offset)
 Serializes a buffer, if dirty or not on disk.
void LoadFromDisk ()
 Load a buffer from disk, if not in memory.
uint32_t GetOffset (const idx_t bitmask_count, const idx_t available_segments)
 Returns the first free offset in a bitmask.
void SetAllocationSize (const idx_t available_segments, const idx_t segment_size, const idx_t bitmask_offset)
 Sets the allocation size, if dirty.

Private Attributes

BlockManager & block_manager
 Block manager of the database instance.
atomic< idx_t > readers
 The number of active segments.
idx_t segment_count
 The number of allocated segments.
idx_t allocation_size
 The size of allocated memory in this buffer (necessary for copying while pinning).
bool dirty
 True: the in-memory buffer is no longer consistent with its optional copy on disk.
bool vacuum
 True: can be vacuumed after the vacuum operation.
bool loaded
 True: has been loaded from disk.
BlockPointer block_pointer
 Partial block id and offset.
BufferHandle buffer_handle
 The buffer handle of the in-memory buffer.
shared_ptr< BlockHandle > block_handle
 The block handle of the on-disk buffer.
mutex lock
 The lock for this fixed size buffer handle.

Friends

class FixedSizeAllocator
class SegmentHandle

Detailed Description

A fixed-size buffer holds fixed-size segments of data. It lazily deserializes a buffer, if on-disk and not in memory, and it only serializes dirty and non-written buffers to disk during serialization.

Constructor & Destructor Documentation

◆ FixedSizeBuffer() [1/2]

duckdb::FixedSizeBuffer::FixedSizeBuffer ( BlockManager & block_manager,
MemoryTag memory_tag )
explicit

Constructor for a new in-memory buffer.

156602 loaded(false), block_pointer(), block_handle(nullptr) {
156603 auto &buffer_manager = block_manager.buffer_manager;
156604 buffer_handle = buffer_manager.Allocate(memory_tag, &block_manager, false);
156605 block_handle = buffer_handle.GetBlockHandle();
156606
156607 // Zero-initialize the buffer as it might get serialized to storage.
156608 auto block_size = block_manager.GetBlockSize();
156609 memset(buffer_handle.Ptr(), 0, block_size);
156610}
BlockManager & block_manager
Block manager of the database instance.
Definition duckdb.cpp:11555
idx_t segment_count
The number of allocated segments.
Definition duckdb.cpp:11561
BlockPointer block_pointer
Partial block id and offset.
Definition duckdb.cpp:11573
BufferHandle buffer_handle
The buffer handle of the in-memory buffer.
Definition duckdb.cpp:11575
shared_ptr< BlockHandle > block_handle
The block handle of the on-disk buffer.
Definition duckdb.cpp:11577
bool dirty
True: the in-memory buffer is no longer consistent with its optional copy on disk.
Definition duckdb.cpp:11566
bool loaded
True: has been loaded from disk.
Definition duckdb.cpp:11570
idx_t allocation_size
The size of allocated memory in this buffer (necessary for copying while pinning).
Definition duckdb.cpp:11563
bool vacuum
True: can be vacuumed after the vacuum operation.
Definition duckdb.cpp:11568
atomic< idx_t > readers
The number of active segments.
Definition duckdb.cpp:11558

◆ FixedSizeBuffer() [2/2]

duckdb::FixedSizeBuffer::FixedSizeBuffer ( BlockManager & block_manager,
const idx_t segment_count,
const idx_t allocation_size,
const BlockPointer & block_pointer )

Constructor for deserializing buffer metadata from disk.

156615 dirty(false), vacuum(false), loaded(false), block_pointer(block_pointer) {
156616 D_ASSERT(block_pointer.IsValid());
156617 block_handle = block_manager.RegisterBlock(block_pointer.block_id);
156618 D_ASSERT(block_handle->BlockId() < MAXIMUM_BLOCK);
156619}

◆ ~FixedSizeBuffer()

duckdb::FixedSizeBuffer::~FixedSizeBuffer ( )
156621 {
156622 lock_guard<mutex> l(lock);
156623 D_ASSERT(readers == 0);
156624
156625 if (InMemory()) {
156626 // we can have multiple readers on a pinned block, and unpinning the buffer handle
156627 // decrements the reader count on the underlying block handle (Destroy() unpins)
156628 buffer_handle.Destroy();
156629 }
156630 if (OnDisk()) {
156631 // marking a block as modified decreases the reference count of multi-use blocks
156632 block_manager.MarkBlockAsModified(block_pointer.block_id);
156633 }
156634}
mutex lock
The lock for this fixed size buffer handle.
Definition duckdb.cpp:11579
bool OnDisk() const
Returns true, if the block is on-disk.
Definition duckdb.cpp:11538
bool InMemory() const
Returns true, if the buffer is in-memory.
Definition duckdb.cpp:11533

Member Function Documentation

◆ GetDeprecated()

data_ptr_t duckdb::FixedSizeBuffer::GetDeprecated ( const bool dirty_p = true)
inlineprivate

Returns a pointer to the buffer in memory, and calls Deserialize, if the buffer is not in memory. DEPRECATED. Use segment handles.

11521 {
11522 lock_guard<mutex> l(lock);
11523 if (!InMemory()) {
11524 LoadFromDisk();
11525 }
11526 if (dirty_p) {
11527 dirty = dirty_p;
11528 }
11529 return buffer_handle.Ptr();
11530 }
void LoadFromDisk()
Load a buffer from disk, if not in memory.
Definition duckdb.cpp:156702
Here is the call graph for this function:

◆ InMemory()

bool duckdb::FixedSizeBuffer::InMemory ( ) const
inlineprivate

Returns true, if the buffer is in-memory.

11533 {
11534 return buffer_handle.IsValid();
11535 }
Here is the caller graph for this function:

◆ OnDisk()

bool duckdb::FixedSizeBuffer::OnDisk ( ) const
inlineprivate

Returns true, if the block is on-disk.

11538 {
11539 return block_pointer.IsValid();
11540 }
Here is the caller graph for this function:

◆ Serialize()

void duckdb::FixedSizeBuffer::Serialize ( PartialBlockManager & partial_block_manager,
const idx_t available_segments,
const idx_t segment_size,
const idx_t bitmask_offset )
private

Serializes a buffer, if dirty or not on disk.

156637 {
156638 D_ASSERT(readers == 0);
156639
156640 // Early-out, if the block is already on disk and not in memory.
156641 if (!InMemory()) {
156642 if (!OnDisk() || dirty) {
156643 throw InternalException("invalid or missing buffer in FixedSizeAllocator");
156644 }
156645 return;
156646 }
156647
156648 // Early-out, if the buffer is already on disk and not dirty.
156649 if (!dirty && OnDisk()) {
156650 return;
156651 }
156652
156653 // Adjust the allocation size.
156654 D_ASSERT(segment_count != 0);
156655 SetAllocationSize(available_segments, segment_size, bitmask_offset);
156656
156657 // The buffer is in memory.
156658 D_ASSERT(InMemory());
156659 if (OnDisk()) {
156660 // We copied it onto a new buffer when loading from disk.
156661 block_manager.MarkBlockAsModified(block_pointer.block_id);
156662 }
156663
156664 // Write the changes.
156665 // First, we get a partial block allocation.
156666 PartialBlockAllocation allocation =
156667 partial_block_manager.GetBlockAllocation(NumericCast<uint32_t>(allocation_size));
156668 block_pointer.block_id = allocation.state.block_id;
156669 block_pointer.offset = allocation.state.offset;
156670
156671 auto &buffer_manager = block_manager.buffer_manager;
156672
156673 if (allocation.partial_block) {
156674 // There is space, so we copy to an existing partial block.
156675 D_ASSERT(block_pointer.offset > 0);
156676 auto &p_block_for_index = allocation.partial_block->Cast<PartialBlockForIndex>();
156677 auto dst_handle = buffer_manager.Pin(p_block_for_index.block_handle);
156678 memcpy(dst_handle.Ptr() + block_pointer.offset, buffer_handle.Ptr(), allocation_size);
156679
156680 } else {
156681 // No partial block available, so we create a new partial block.
156682 D_ASSERT(block_handle);
156683 D_ASSERT(!block_pointer.offset);
156684 auto p_block_for_index = make_uniq<PartialBlockForIndex>(allocation.state, block_manager, block_handle);
156685 allocation.partial_block = std::move(p_block_for_index);
156686 }
156687
156688 // We are done with this buffer.
156689 // To use the fixed-size buffer again, we need to re-load it from disk.
156690 buffer_handle.Destroy();
156691
156692 // Register the partial block and the block handle.
156693 partial_block_manager.RegisterPartialBlock(std::move(allocation));
156694
156695 block_handle = block_manager.RegisterBlock(block_pointer.block_id);
156696 D_ASSERT(block_handle->BlockId() < MAXIMUM_BLOCK);
156697
156698 // We persisted any changes, so the fixed-size buffer is no longer dirty.
156699 dirty = false;
156700}
void SetAllocationSize(const idx_t available_segments, const idx_t segment_size, const idx_t bitmask_offset)
Sets the allocation size, if dirty.
Definition duckdb.cpp:156771
Here is the call graph for this function:

◆ LoadFromDisk()

void duckdb::FixedSizeBuffer::LoadFromDisk ( )
private

Load a buffer from disk, if not in memory.

156702 {
156703 D_ASSERT(block_pointer.IsValid());
156704 D_ASSERT(block_handle && block_handle->BlockId() < MAXIMUM_BLOCK);
156705 D_ASSERT(!dirty);
156706
156707 // Pin the partial block.
156708 auto &buffer_manager = block_manager.buffer_manager;
156709 auto pinned_buffer_handle = buffer_manager.Pin(block_handle);
156710
156711 // Copy the (partial) data into a new (not yet disk-backed) buffer handle.
156712 shared_ptr<BlockHandle> new_block_handle;
156713 auto new_buffer_handle = buffer_manager.Allocate(MemoryTag::ART_INDEX, &block_manager, false);
156714 new_block_handle = new_buffer_handle.GetBlockHandle();
156715 memcpy(new_buffer_handle.Ptr(), pinned_buffer_handle.Ptr() + block_pointer.offset, allocation_size);
156716
156717 buffer_handle = std::move(new_buffer_handle);
156718 block_handle = std::move(new_block_handle);
156719}
Here is the caller graph for this function:

◆ GetOffset()

uint32_t duckdb::FixedSizeBuffer::GetOffset ( const idx_t bitmask_count,
const idx_t available_segments )
private

Returns the first free offset in a bitmask.

156721 {
156722 // Get a handle to the buffer's validity mask (offset 0).
156723 SegmentHandle handle(*this, 0);
156724 const auto bitmask_ptr = handle.GetPtr<validity_t>();
156725
156726 ValidityMask mask(bitmask_ptr, available_segments);
156727 const auto data = mask.GetData();
156728
156729 // fills up a buffer sequentially before searching for free bits
156730 if (mask.RowIsValid(segment_count)) {
156731 mask.SetInvalid(segment_count);
156732 return UnsafeNumericCast<uint32_t>(segment_count);
156733 }
156734
156735 for (idx_t entry_idx = 0; entry_idx < bitmask_count; entry_idx++) {
156736 // get an entry with free bits
156737 if (data[entry_idx] == 0) {
156738 continue;
156739 }
156740
156741 // find the position of the free bit
156742 auto entry = data[entry_idx];
156743 idx_t first_valid_bit = 0;
156744
156745 // this loop finds the position of the rightmost set bit in entry and stores it
156746 // in first_valid_bit
156747 for (idx_t i = 0; i < 6; i++) {
156748 // set the left half of the bits of this level to zero and test if the entry is still not zero
156749 if (entry & BASE[i]) {
156750 // first valid bit is in the rightmost s[i] bits
156751 // permanently set the left half of the bits to zero
156752 entry &= BASE[i];
156753 } else {
156754 // first valid bit is in the leftmost s[i] bits
156755 // shift by s[i] for the next iteration and add s[i] to the position of the rightmost set bit
156756 entry >>= SHIFT[i];
156757 first_valid_bit += SHIFT[i];
156758 }
156759 }
156760 D_ASSERT(entry);
156761
156762 auto prev_bits = entry_idx * sizeof(validity_t) * 8;
156763 D_ASSERT(mask.RowIsValid(prev_bits + first_valid_bit));
156764 mask.SetInvalid(prev_bits + first_valid_bit);
156765 return UnsafeNumericCast<uint32_t>(prev_bits + first_valid_bit);
156766 }
156767
156768 throw InternalException("Invalid bitmask for FixedSizeAllocator");
156769}
static constexpr idx_t BASE[]
Constants for fast offset calculations in the bitmask.
Definition duckdb.cpp:11506
GMat mask(const GMat &src, const GMat &mask)
uint64_t idx_t
a saner size_t for loop indices etc
Definition duckdb.hpp:237

◆ SetAllocationSize()

void duckdb::FixedSizeBuffer::SetAllocationSize ( const idx_t available_segments,
const idx_t segment_size,
const idx_t bitmask_offset )
private

Sets the allocation size, if dirty.

156772 {
156773 if (!dirty) {
156774 return;
156775 }
156776
156777 // We traverse from the back. A binary search would be faster.
156778 // However, buffers are often (almost) full, so the overhead is acceptable.
156779
156780 // Get a handle to the buffer's validity mask (offset 0).
156781 SegmentHandle handle(*this, 0);
156782 const auto bitmask_ptr = handle.GetPtr<validity_t>();
156783 const ValidityMask mask(bitmask_ptr, available_segments);
156784
156785 auto max_offset = available_segments;
156786 for (idx_t i = available_segments; i > 0; i--) {
156787 if (!mask.RowIsValid(i - 1)) {
156788 max_offset = i;
156789 break;
156790 }
156791 }
156792 allocation_size = max_offset * segment_size + bitmask_offset;
156793}
Here is the caller graph for this function:

Member Data Documentation

◆ BASE

idx_t duckdb::FixedSizeBuffer::BASE = {0x00000000FFFFFFFF, 0x0000FFFF, 0x00FF, 0x0F, 0x3, 0x1}
staticconstexpr

Constants for fast offset calculations in the bitmask.

11506{0x00000000FFFFFFFF, 0x0000FFFF, 0x00FF, 0x0F, 0x3, 0x1};

◆ SHIFT

uint8_t duckdb::FixedSizeBuffer::SHIFT = {32, 16, 8, 4, 2, 1}
staticconstexpr
11507{32, 16, 8, 4, 2, 1};

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