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

Public Member Functions

 FixedSizeAllocator (const idx_t segment_size, BlockManager &block_manager, MemoryTag memory_tag=MemoryTag::ART_INDEX)
 Construct a new fixed-size allocator.
IndexPointer New ()
 Get a new IndexPointer to a segment, might cause a new buffer allocation.
void Free (const IndexPointer ptr)
 Free the segment of the IndexPointer.
SegmentHandle GetHandle (const IndexPointer ptr)
 Get a segment handle.
template<class T>
unsafe_optional_ptr< T > Get (const IndexPointer ptr, const bool dirty=true)
data_ptr_t Get (const IndexPointer ptr, const bool dirty=true)
bool LoadedFromStorage (const IndexPointer ptr) const
 Has been loaded into a buffer-managed buffer from a persistent, file-backed block.
template<class T>
unsafe_optional_ptr< T > GetIfLoaded (const IndexPointer ptr)
data_ptr_t GetIfLoaded (const IndexPointer ptr)
void Reset ()
 Resets the allocator, e.g., during 'DELETE FROM table'.
idx_t GetInMemorySize () const
 Returns the in-memory size in bytes.
idx_t GetSegmentSize () const
 Returns the segment size.
idx_t GetSegmentCount () const
 Returns the total segment count.
idx_t GetUpperBoundBufferId () const
 Returns the upper bound of the available buffer IDs, i.e., upper_bound > max_buffer_id.
void Merge (FixedSizeAllocator &other)
 Merge another FixedSizeAllocator into this allocator. Both must have the same segment size.
bool InitializeVacuum ()
 Initialize a vacuum operation, and return true, if the allocator needs a vacuum.
void FinalizeVacuum ()
 Finalize a vacuum operation by freeing all vacuumed buffers.
bool NeedsVacuum (const IndexPointer ptr) const
 Returns true, if an IndexPointer qualifies for a vacuum operation, and false otherwise.
IndexPointer VacuumPointer (const IndexPointer ptr)
 Vacuums an IndexPointer.
FixedSizeAllocatorInfo GetInfo () const
 Returns all FixedSizeAllocator information for serialization.
void SerializeBuffers (PartialBlockManager &partial_block_manager)
 Serializes all in-memory buffers.
vector< IndexBufferInfo > InitSerializationToWAL ()
 Sets the allocation sizes and returns data to serialize each buffer.
void Init (const FixedSizeAllocatorInfo &info)
 Initialize a fixed-size allocator from allocator storage information.
void Deserialize (MetadataManager &metadata_manager, const BlockPointer &block_pointer)
 Deserializes all metadata of older storage files.
bool Empty ()
 Returns true, if the allocator does not contain any segments.
void RemoveEmptyBuffers ()
 Removes empty buffers.
void VerifyBuffers ()
 Verifies that the number of empty buffers does not exceed the empty buffer threshold.

Public Attributes

BlockManager & block_manager
 Block manager of the database instance.
BufferManager & buffer_manager
 Buffer manager of the database instance.

Static Public Attributes

static constexpr uint8_t VACUUM_THRESHOLD = 10
 We can vacuum 10% or more of the total in-memory footprint.

Private Member Functions

idx_t GetAvailableBufferId () const
 Returns an available buffer id.
void NextBufferWithFreeSpace ()
 Caches the next buffer that we're going to fill.

Private Attributes

MemoryTag memory_tag
 Memory tag of memory that is allocated through the allocator.
idx_t segment_size
idx_t bitmask_count
 Number of validity_t values in the bitmask.
idx_t bitmask_offset
 First starting byte of the payload (segments).
idx_t available_segments_per_buffer
 Number of possible segment allocations per buffer.
idx_t total_segment_count
unordered_map< idx_t, unique_ptr< FixedSizeBuffer > > buffers
 Buffers containing the segments.
unordered_set< idx_t > buffers_with_free_space
 Buffers with free space.
optional_idx buffer_with_free_space
unordered_set< idx_t > vacuum_buffers
 Buffers qualifying for a vacuum (helper field to allow for fast NeedsVacuum checks).

Detailed Description

The FixedSizeAllocator provides pointers to fixed-size memory segments of pre-allocated memory buffers. The pointers are IndexPointers, and the leftmost byte (metadata) must always be zero. It is also possible to directly request a C++ pointer to the underlying segment of an index pointer.

Constructor & Destructor Documentation

◆ FixedSizeAllocator()

duckdb::FixedSizeAllocator::FixedSizeAllocator ( const idx_t segment_size,
BlockManager & block_manager,
MemoryTag memory_tag = MemoryTag::ART_INDEX )

Construct a new fixed-size allocator.

156154 if (segment_size > block_manager.GetBlockSize() - sizeof(validity_t)) {
156155 throw InternalException("The maximum segment size of fixed-size allocators is " +
156156 to_string(block_manager.GetBlockSize() - sizeof(validity_t)));
156157 }
156158
156159 // calculate how many segments fit into one buffer (available_segments_per_buffer)
156160
156161 idx_t bits_per_value = sizeof(validity_t) * 8;
156162 idx_t byte_count = 0;
156163
156164 bitmask_count = 0;
156166
156167 while (byte_count < block_manager.GetBlockSize()) {
156168 if (!bitmask_count || (bitmask_count * bits_per_value) % available_segments_per_buffer == 0) {
156169 // we need to add another validity_t value to the bitmask, to allow storing another
156170 // bits_per_value segments on a buffer
156171 bitmask_count++;
156172 byte_count += sizeof(validity_t);
156173 }
156174
156175 auto remaining_bytes = block_manager.GetBlockSize() - byte_count;
156176 auto remaining_segments = MinValue(remaining_bytes / segment_size, bits_per_value);
156177
156178 if (remaining_segments == 0) {
156179 break;
156180 }
156181
156182 available_segments_per_buffer += remaining_segments;
156183 byte_count += remaining_segments * segment_size;
156184 }
156185
156186 bitmask_offset = bitmask_count * sizeof(validity_t);
156187}
MemoryTag memory_tag
Memory tag of memory that is allocated through the allocator.
Definition duckdb.cpp:11817
BlockManager & block_manager
Block manager of the database instance.
Definition duckdb.cpp:11685
idx_t bitmask_count
Number of validity_t values in the bitmask.
Definition duckdb.cpp:11824
idx_t bitmask_offset
First starting byte of the payload (segments).
Definition duckdb.cpp:11826
idx_t segment_size
Definition duckdb.cpp:11821
idx_t total_segment_count
Definition duckdb.cpp:11832
BufferManager & buffer_manager
Buffer manager of the database instance.
Definition duckdb.cpp:11687
idx_t available_segments_per_buffer
Number of possible segment allocations per buffer.
Definition duckdb.cpp:11828
std::string to_string(const T &value)
Definition duckdb.cpp:19425
uint64_t idx_t
a saner size_t for loop indices etc
Definition duckdb.hpp:237
Here is the caller graph for this function:

Member Function Documentation

◆ New()

IndexPointer duckdb::FixedSizeAllocator::New ( )

Get a new IndexPointer to a segment, might cause a new buffer allocation.

156189 {
156190 // No more free segments available.
156191 if (!buffer_with_free_space.IsValid()) {
156192 // Add a new buffer.
156193 auto buffer_id = GetAvailableBufferId();
156194 buffers[buffer_id] = make_uniq<FixedSizeBuffer>(block_manager, memory_tag);
156195 buffers_with_free_space.insert(buffer_id);
156196 buffer_with_free_space = buffer_id;
156197
156198 // Set and initialize the bitmask of the new buffer.
156199 D_ASSERT(buffers.find(buffer_id) != buffers.end());
156200 auto &buffer = buffers.find(buffer_id)->second;
156201
156202 // Get a handle to the buffer's validity mask (offset 0).
156203 SegmentHandle handle(*buffer, 0);
156204 const auto bitmask_ptr = handle.GetPtr<validity_t>();
156205 ValidityMask mask(bitmask_ptr, available_segments_per_buffer);
156206 mask.SetAllValid(available_segments_per_buffer);
156207 }
156208
156209 // Extract an index pointer to a free segment.
156210
156211 D_ASSERT(buffer_with_free_space.IsValid());
156212 auto buffer_id = buffer_with_free_space.GetIndex();
156213
156214 D_ASSERT(buffers.find(buffer_id) != buffers.end());
156215 auto &buffer = buffers.find(buffer_id)->second;
156216 auto offset = buffer->GetOffset(bitmask_count, available_segments_per_buffer);
156217
156219 buffer->segment_count++;
156220
156221 // If the buffer is full, we cache the next buffer that we're going to fill.
156222 if (buffer->segment_count == available_segments_per_buffer) {
156223 buffers_with_free_space.erase(buffer_id);
156225 }
156226
156227 return IndexPointer(uint32_t(buffer_id), offset);
156228}
unordered_map< idx_t, unique_ptr< FixedSizeBuffer > > buffers
Buffers containing the segments.
Definition duckdb.cpp:11835
idx_t GetAvailableBufferId() const
Returns an available buffer id.
Definition duckdb.cpp:156517
optional_idx buffer_with_free_space
Definition duckdb.cpp:11841
void NextBufferWithFreeSpace()
Caches the next buffer that we're going to fill.
Definition duckdb.cpp:156554
unordered_set< idx_t > buffers_with_free_space
Buffers with free space.
Definition duckdb.cpp:11837
GMat mask(const GMat &src, const GMat &mask)
::uint32_t uint32_t
Here is the call graph for this function:
Here is the caller graph for this function:

◆ Free()

void duckdb::FixedSizeAllocator::Free ( const IndexPointer ptr)

Free the segment of the IndexPointer.

156230 {
156231 auto buffer_id = ptr.GetBufferId();
156232 auto offset = ptr.GetOffset();
156233
156234 auto buffer_it = buffers.find(buffer_id);
156235 D_ASSERT(buffer_it != buffers.end());
156236 auto &buffer = buffer_it->second;
156237
156238 {
156239 // Get a handle to the buffer's validity mask (offset 0).
156240 SegmentHandle handle(*buffer, 0);
156241 const auto bitmask_ptr = handle.GetPtr<validity_t>();
156242
156243 ValidityMask mask(bitmask_ptr, offset + 1); // FIXME
156244 D_ASSERT(!mask.RowIsValid(offset));
156245 mask.SetValid(offset);
156246 }
156247
156248 D_ASSERT(total_segment_count > 0);
156249 D_ASSERT(buffer->segment_count > 0);
156250
156251 // Adjust the allocator fields.
156253 buffer->segment_count--;
156254
156255 // Early-out, if the buffer is not empty.
156256 if (buffer->segment_count != 0) {
156257 buffers_with_free_space.insert(buffer_id);
156258 if (!buffer_with_free_space.IsValid()) {
156259 buffer_with_free_space = buffer_id;
156260 }
156261 return;
156262 }
156263
156264 // The segment count went to 0, i.e, the buffer is now empty.
156265 // We keep the buffer alive, if it is the only buffer with free space.
156266 // We do so to prevent too much buffer creation fluctuation.
156267 if (buffers_with_free_space.size() == 1) {
156268 D_ASSERT(*buffers_with_free_space.begin() == buffer_id);
156269 D_ASSERT(buffer_with_free_space.GetIndex() == buffer_id);
156270 return;
156271 }
156272
156273 D_ASSERT(buffer_with_free_space.IsValid());
156274 buffers_with_free_space.erase(buffer_id);
156275 buffers.erase(buffer_it);
156276
156277 // Cache the next buffer that we're going to fill.
156278 if (buffer_with_free_space.GetIndex() == buffer_id) {
156280 }
156281}
Here is the call graph for this function:
Here is the caller graph for this function:

◆ GetHandle()

SegmentHandle duckdb::FixedSizeAllocator::GetHandle ( const IndexPointer ptr)
inline

Get a segment handle.

11696 {
11697 D_ASSERT(ptr.GetOffset() < available_segments_per_buffer);
11698
11699 auto buffer_it = buffers.find(ptr.GetBufferId());
11700 D_ASSERT(buffer_it != buffers.end());
11701
11702 auto offset = ptr.GetOffset() * segment_size + bitmask_offset;
11703 auto &buffer = *buffer_it->second;
11704 return SegmentHandle(buffer, offset);
11705 }
Here is the caller graph for this function:

◆ Get() [1/2]

template<class T>
unsafe_optional_ptr< T > duckdb::FixedSizeAllocator::Get ( const IndexPointer ptr,
const bool dirty = true )
inline

Returns a pointer of type T to a segment. If dirty is false, then T must be a const class. DEPRECATED. Use segment handles.

11710 {
11711 return (T *)Get(ptr, dirty);
11712 }
unsafe_optional_ptr< T > Get(const IndexPointer ptr, const bool dirty=true)
Definition duckdb.cpp:11710
Here is the call graph for this function:
Here is the caller graph for this function:

◆ Get() [2/2]

data_ptr_t duckdb::FixedSizeAllocator::Get ( const IndexPointer ptr,
const bool dirty = true )
inline

Returns the data_ptr_t to a segment, and sets the dirty flag of the buffer containing that segment. DEPRECATED. Use segment handles.

11716 {
11717 D_ASSERT(ptr.GetOffset() < available_segments_per_buffer);
11718
11719 auto buffer_it = buffers.find(ptr.GetBufferId());
11720 D_ASSERT(buffer_it != buffers.end());
11721
11722 auto offset = ptr.GetOffset() * segment_size + bitmask_offset;
11723 auto buffer_ptr = buffer_it->second->GetDeprecated(dirty);
11724 return buffer_ptr + offset;
11725 }
shared_ptr< T > buffer_ptr
FIXME: this should be a single_thread_ptr.
Definition duckdb.hpp:2709

◆ LoadedFromStorage()

bool duckdb::FixedSizeAllocator::LoadedFromStorage ( const IndexPointer ptr) const
inline

Has been loaded into a buffer-managed buffer from a persistent, file-backed block.

11728 {
11729 D_ASSERT(ptr.GetOffset() < available_segments_per_buffer);
11730
11731 auto buffer_it = buffers.find(ptr.GetBufferId());
11732 if (buffer_it == buffers.end()) {
11733 return false;
11734 }
11735
11736 return buffer_it->second->InMemory();
11737 }

◆ GetIfLoaded() [1/2]

template<class T>
unsafe_optional_ptr< T > duckdb::FixedSizeAllocator::GetIfLoaded ( const IndexPointer ptr)
inline

Returns a pointer of type T to a segment, or nullptr, if the buffer is not in memory. DEPRECATED. Use segment handles.

11742 {
11743 return (T *)GetIfLoaded(ptr);
11744 }
unsafe_optional_ptr< T > GetIfLoaded(const IndexPointer ptr)
Definition duckdb.cpp:11742
Here is the call graph for this function:
Here is the caller graph for this function:

◆ GetIfLoaded() [2/2]

data_ptr_t duckdb::FixedSizeAllocator::GetIfLoaded ( const IndexPointer ptr)
inline

Returns the data_ptr_t to a segment, or nullptr, if the buffer is not in memory. DEPRECATED. Use segment handles.

11748 {
11749 D_ASSERT(ptr.GetOffset() < available_segments_per_buffer);
11750 D_ASSERT(buffers.find(ptr.GetBufferId()) != buffers.end());
11751
11752 auto &buffer = buffers.find(ptr.GetBufferId())->second;
11753 if (!buffer->InMemory()) {
11754 return nullptr;
11755 }
11756
11757 auto buffer_ptr = buffer->GetDeprecated();
11758 auto raw_ptr = buffer_ptr + ptr.GetOffset() * segment_size + bitmask_offset;
11759 return raw_ptr;
11760 }

◆ Reset()

void duckdb::FixedSizeAllocator::Reset ( )

Resets the allocator, e.g., during 'DELETE FROM table'.

156283 {
156284 buffers.clear();
156285 buffers_with_free_space.clear();
156286 buffer_with_free_space.SetInvalid();
156288}
Here is the call graph for this function:
Here is the caller graph for this function:

◆ GetInMemorySize()

idx_t duckdb::FixedSizeAllocator::GetInMemorySize ( ) const

Returns the in-memory size in bytes.

156290 {
156291 idx_t memory_usage = 0;
156292 for (auto &buffer : buffers) {
156293 if (buffer.second->InMemory()) {
156294 memory_usage += block_manager.GetBlockSize();
156295 }
156296 }
156297 return memory_usage;
156298}
Here is the call graph for this function:
Here is the caller graph for this function:

◆ GetSegmentSize()

idx_t duckdb::FixedSizeAllocator::GetSegmentSize ( ) const
inline

Returns the segment size.

11768 {
11769 return segment_size;
11770 }

◆ GetSegmentCount()

idx_t duckdb::FixedSizeAllocator::GetSegmentCount ( ) const
inline

Returns the total segment count.

11772 {
11773 return total_segment_count;
11774 }

◆ GetUpperBoundBufferId()

idx_t duckdb::FixedSizeAllocator::GetUpperBoundBufferId ( ) const

Returns the upper bound of the available buffer IDs, i.e., upper_bound > max_buffer_id.

156300 {
156301 idx_t upper_bound_id = 0;
156302 for (auto &buffer : buffers) {
156303 if (buffer.first >= upper_bound_id) {
156304 upper_bound_id = buffer.first + 1;
156305 }
156306 }
156307 return upper_bound_id;
156308}
Here is the call graph for this function:
Here is the caller graph for this function:

◆ Merge()

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

Merge another FixedSizeAllocator into this allocator. Both must have the same segment size.

156310 {
156311 D_ASSERT(segment_size == other.segment_size);
156312
156313 // remember the buffer count and merge the buffers
156314 idx_t upper_bound_id = GetUpperBoundBufferId();
156315 for (auto &buffer : other.buffers) {
156316 buffers.insert(make_pair(buffer.first + upper_bound_id, std::move(buffer.second)));
156317 }
156318 other.buffers.clear();
156319
156320 // merge the buffers with free spaces
156321 for (auto &buffer_id : other.buffers_with_free_space) {
156322 buffers_with_free_space.insert(buffer_id + upper_bound_id);
156323 }
156324 other.buffers_with_free_space.clear();
156326
156327 // add the total allocations
156328 total_segment_count += other.total_segment_count;
156329}
idx_t GetUpperBoundBufferId() const
Returns the upper bound of the available buffer IDs, i.e., upper_bound > max_buffer_id.
Definition duckdb.cpp:156300
Here is the call graph for this function:
Here is the caller graph for this function:

◆ InitializeVacuum()

bool duckdb::FixedSizeAllocator::InitializeVacuum ( )

Initialize a vacuum operation, and return true, if the allocator needs a vacuum.

156331 {
156332 // NOTE: we do not vacuum buffers that are not in memory. We might consider changing this
156333 // in the future, although buffers on disk should almost never be eligible for a vacuum
156334
156335 if (total_segment_count == 0) {
156336 Reset();
156337 return false;
156338 }
156340
156341 // determine if a vacuum is necessary
156342 multimap<idx_t, idx_t> temporary_vacuum_buffers;
156343 D_ASSERT(vacuum_buffers.empty());
156344 idx_t available_segments_in_memory = 0;
156345
156346 for (auto &buffer : buffers) {
156347 buffer.second->vacuum = false;
156348 if (buffer.second->InMemory()) {
156349 auto available_segments_in_buffer = available_segments_per_buffer - buffer.second->segment_count;
156350 available_segments_in_memory += available_segments_in_buffer;
156351 temporary_vacuum_buffers.emplace(available_segments_in_buffer, buffer.first);
156352 }
156353 }
156354
156355 // no buffers in memory
156356 if (temporary_vacuum_buffers.empty()) {
156357 return false;
156358 }
156359
156360 auto excess_buffer_count = available_segments_in_memory / available_segments_per_buffer;
156361
156362 // calculate the vacuum threshold adaptively
156363 D_ASSERT(excess_buffer_count < temporary_vacuum_buffers.size());
156364 idx_t memory_usage = GetInMemorySize();
156365 idx_t excess_memory_usage = excess_buffer_count * block_manager.GetBlockSize();
156366 auto excess_percentage = double(excess_memory_usage) / double(memory_usage);
156367 auto threshold = double(VACUUM_THRESHOLD) / 100.0;
156368 if (excess_percentage < threshold) {
156369 return false;
156370 }
156371
156372 D_ASSERT(excess_buffer_count <= temporary_vacuum_buffers.size());
156373 D_ASSERT(temporary_vacuum_buffers.size() <= buffers.size());
156374
156375 // erasing from a multimap, we vacuum the buffers with the most free spaces (least full)
156376 while (temporary_vacuum_buffers.size() != excess_buffer_count) {
156377 temporary_vacuum_buffers.erase(temporary_vacuum_buffers.begin());
156378 }
156379
156380 // adjust the buffers, and erase all to-be-vacuumed buffers from the available buffer list
156381 for (auto &vacuum_buffer : temporary_vacuum_buffers) {
156382 auto buffer_id = vacuum_buffer.second;
156383 D_ASSERT(buffers.find(buffer_id) != buffers.end());
156384 buffers.find(buffer_id)->second->vacuum = true;
156385 buffers_with_free_space.erase(buffer_id);
156386 }
156387 D_ASSERT(!buffers_with_free_space.empty());
156389
156390 for (auto &vacuum_buffer : temporary_vacuum_buffers) {
156391 vacuum_buffers.insert(vacuum_buffer.second);
156392 }
156393 return true;
156394}
static constexpr uint8_t VACUUM_THRESHOLD
We can vacuum 10% or more of the total in-memory footprint.
Definition duckdb.cpp:11677
void Reset()
Resets the allocator, e.g., during 'DELETE FROM table'.
Definition duckdb.cpp:156283
idx_t GetInMemorySize() const
Returns the in-memory size in bytes.
Definition duckdb.cpp:156290
void RemoveEmptyBuffers()
Removes empty buffers.
Definition duckdb.cpp:156526
unordered_set< idx_t > vacuum_buffers
Buffers qualifying for a vacuum (helper field to allow for fast NeedsVacuum checks).
Definition duckdb.cpp:11844
GMat threshold(const GMat &src, const GScalar &thresh, const GScalar &maxval, int type)
Here is the call graph for this function:
Here is the caller graph for this function:

◆ FinalizeVacuum()

void duckdb::FixedSizeAllocator::FinalizeVacuum ( )

Finalize a vacuum operation by freeing all vacuumed buffers.

156396 {
156397 for (auto &buffer_id : vacuum_buffers) {
156398 D_ASSERT(buffers.find(buffer_id) != buffers.end());
156399 D_ASSERT(buffers.find(buffer_id)->second->InMemory());
156400 buffers.erase(buffer_id);
156401 }
156402 vacuum_buffers.clear();
156403}
Here is the call graph for this function:
Here is the caller graph for this function:

◆ NeedsVacuum()

bool duckdb::FixedSizeAllocator::NeedsVacuum ( const IndexPointer ptr) const
inline

Returns true, if an IndexPointer qualifies for a vacuum operation, and false otherwise.

11786 {
11787 if (vacuum_buffers.find(ptr.GetBufferId()) != vacuum_buffers.end()) {
11788 return true;
11789 }
11790 return false;
11791 }

◆ VacuumPointer()

IndexPointer duckdb::FixedSizeAllocator::VacuumPointer ( const IndexPointer ptr)

Vacuums an IndexPointer.

156405 {
156406 // we do not need to adjust the bitmask of the old buffer, because we will free the entire
156407 // buffer after the vacuum operation
156408
156409 auto new_ptr = New();
156410 // new increases the allocation count, we need to counter that here
156412
156413 auto old_handle = GetHandle(old_ptr);
156414 auto new_handle = GetHandle(new_ptr);
156415
156416 memcpy(new_handle.GetPtr(), old_handle.GetPtr(), segment_size);
156417 return new_ptr;
156418}
SegmentHandle GetHandle(const IndexPointer ptr)
Get a segment handle.
Definition duckdb.cpp:11696
IndexPointer New()
Get a new IndexPointer to a segment, might cause a new buffer allocation.
Definition duckdb.cpp:156189
Here is the call graph for this function:
Here is the caller graph for this function:

◆ GetInfo()

FixedSizeAllocatorInfo duckdb::FixedSizeAllocator::GetInfo ( ) const

Returns all FixedSizeAllocator information for serialization.

156420 {
156421 FixedSizeAllocatorInfo info;
156422 info.segment_size = segment_size;
156423
156424 for (const auto &buffer : buffers) {
156425 info.buffer_ids.push_back(buffer.first);
156426
156427 // Memory safety check.
156428 if (buffer.first > idx_t(MAX_ROW_ID)) {
156429 throw InternalException("Initializing invalid buffer ID in FixedSizeAllocator::GetInfo");
156430 }
156431
156432 info.block_pointers.push_back(buffer.second->block_pointer);
156433 info.segment_counts.push_back(buffer.second->segment_count);
156434 info.allocation_sizes.push_back(buffer.second->allocation_size);
156435 }
156436
156437 for (auto &buffer_id : buffers_with_free_space) {
156438 info.buffers_with_free_space.push_back(buffer_id);
156439 }
156440
156441 return info;
156442}
const row_t MAX_ROW_ID
The maximum row identifier used in tables.
Definition duckdb.cpp:50301
Here is the call graph for this function:
Here is the caller graph for this function:

◆ SerializeBuffers()

void duckdb::FixedSizeAllocator::SerializeBuffers ( PartialBlockManager & partial_block_manager)

Serializes all in-memory buffers.

156444 {
156445 for (auto &buffer : buffers) {
156446 buffer.second->Serialize(partial_block_manager, available_segments_per_buffer, segment_size, bitmask_offset);
156447 }
156448}
Here is the call graph for this function:
Here is the caller graph for this function:

◆ InitSerializationToWAL()

vector< IndexBufferInfo > duckdb::FixedSizeAllocator::InitSerializationToWAL ( )

Sets the allocation sizes and returns data to serialize each buffer.

156450 {
156451 vector<IndexBufferInfo> buffer_infos;
156452 for (auto &buffer : buffers) {
156453 buffer.second->SetAllocationSize(available_segments_per_buffer, segment_size, bitmask_offset);
156454
156455 // Get a handle to the buffer's memory (offset 0).
156456 SegmentHandle handle(*buffer.second, 0);
156457 buffer_infos.emplace_back(handle.GetPtr(), buffer.second->allocation_size);
156458 }
156459 return buffer_infos;
156460}
Here is the call graph for this function:
Here is the caller graph for this function:

◆ Init()

void duckdb::FixedSizeAllocator::Init ( const FixedSizeAllocatorInfo & info)

Initialize a fixed-size allocator from allocator storage information.

156462 {
156463 segment_size = info.segment_size;
156465
156466 for (idx_t i = 0; i < info.buffer_ids.size(); i++) {
156467 // read all FixedSizeBuffer data
156468 auto buffer_id = info.buffer_ids[i];
156469
156470 // Memory safety check.
156471 if (buffer_id > idx_t(MAX_ROW_ID)) {
156472 throw InternalException("Initializing invalid buffer ID in FixedSizeAllocator::Init");
156473 }
156474
156475 auto buffer_block_pointer = info.block_pointers[i];
156476 if (buffer_block_pointer.block_id >= MAXIMUM_BLOCK) {
156477 throw SerializationException("invalid block ID in index storage information");
156478 }
156479
156480 auto segment_count = info.segment_counts[i];
156481 auto allocation_size = info.allocation_sizes[i];
156482
156483 // create the FixedSizeBuffer
156484 buffers[buffer_id] =
156485 make_uniq<FixedSizeBuffer>(block_manager, segment_count, allocation_size, buffer_block_pointer);
156486 total_segment_count += segment_count;
156487 }
156488
156489 for (const auto &buffer_id : info.buffers_with_free_space) {
156490 buffers_with_free_space.insert(buffer_id);
156491 }
156493}
Here is the call graph for this function:
Here is the caller graph for this function:

◆ Deserialize()

void duckdb::FixedSizeAllocator::Deserialize ( MetadataManager & metadata_manager,
const BlockPointer & block_pointer )

Deserializes all metadata of older storage files.

156495 {
156496 MetadataReader reader(metadata_manager, block_pointer);
156497 segment_size = reader.Read<idx_t>();
156498 auto buffer_count = reader.Read<idx_t>();
156499 auto buffers_with_free_space_count = reader.Read<idx_t>();
156500
156502
156503 for (idx_t i = 0; i < buffer_count; i++) {
156504 auto buffer_id = reader.Read<idx_t>();
156505 auto buffer_block_pointer = reader.Read<BlockPointer>();
156506 auto segment_count = reader.Read<idx_t>();
156507 auto allocation_size = reader.Read<idx_t>();
156508 buffers[buffer_id] =
156509 make_uniq<FixedSizeBuffer>(block_manager, segment_count, allocation_size, buffer_block_pointer);
156510 total_segment_count += segment_count;
156511 }
156512 for (idx_t i = 0; i < buffers_with_free_space_count; i++) {
156513 buffers_with_free_space.insert(reader.Read<idx_t>());
156514 }
156515}
Here is the call graph for this function:
Here is the caller graph for this function:

◆ Empty()

bool duckdb::FixedSizeAllocator::Empty ( )
inline

Returns true, if the allocator does not contain any segments.

11807 {
11808 return total_segment_count == 0;
11809 }

◆ RemoveEmptyBuffers()

void duckdb::FixedSizeAllocator::RemoveEmptyBuffers ( )

Removes empty buffers.

156526 {
156527 auto buffer_it = buffers.begin();
156528 while (buffer_it != buffers.end()) {
156529 if (buffer_it->second->segment_count != 0) {
156530 ++buffer_it;
156531 continue;
156532 }
156533 buffers_with_free_space.erase(buffer_it->first);
156534 buffer_it = buffers.erase(buffer_it);
156535 }
156537}
Here is the call graph for this function:
Here is the caller graph for this function:

◆ VerifyBuffers()

void duckdb::FixedSizeAllocator::VerifyBuffers ( )

Verifies that the number of empty buffers does not exceed the empty buffer threshold.

156539 {
156540 idx_t count = 0;
156541 auto buffer_it = buffers.begin();
156542 while (buffer_it != buffers.end()) {
156543 if (buffer_it->second->segment_count == 0) {
156544 count++;
156545 }
156546 buffer_it++;
156547 }
156548
156549 if (count > 1) {
156550 throw InternalException("expected one, but got %d empty buffers in allocator", count);
156551 }
156552}
Here is the call graph for this function:
Here is the caller graph for this function:

◆ GetAvailableBufferId()

idx_t duckdb::FixedSizeAllocator::GetAvailableBufferId ( ) const
private

Returns an available buffer id.

156517 {
156518 idx_t buffer_id = buffers.size();
156519 while (buffers.find(buffer_id) != buffers.end()) {
156520 D_ASSERT(buffer_id > 0);
156521 buffer_id--;
156522 }
156523 return buffer_id;
156524}
Here is the call graph for this function:
Here is the caller graph for this function:

◆ NextBufferWithFreeSpace()

void duckdb::FixedSizeAllocator::NextBufferWithFreeSpace ( )
private

Caches the next buffer that we're going to fill.

156554 {
156555 if (!buffers_with_free_space.empty()) {
156557 return;
156558 }
156559 buffer_with_free_space.SetInvalid();
156560}
Here is the call graph for this function:
Here is the caller graph for this function:

Member Data Documentation

◆ segment_size

idx_t duckdb::FixedSizeAllocator::segment_size
private

Allocation size of one segment in a buffer We only need this value to calculate bitmask_count, bitmask_offset, and available_segments_per_buffer

◆ total_segment_count

idx_t duckdb::FixedSizeAllocator::total_segment_count
private

Total number of allocated segments in all buffers We can recalculate this by iterating over all buffers

◆ buffer_with_free_space

optional_idx duckdb::FixedSizeAllocator::buffer_with_free_space
private

Caches the next buffer to be filled up. Unordered sets make no guarantee that begin() returns the same element. By caching one of the buffers with free space, we get more consistency when filling buffers.


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