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

Public Member Functions

 ColumnDataAllocator (Allocator &allocator)
 ColumnDataAllocator (BufferManager &buffer_manager, ColumnDataCollectionLifetime lifetime=ColumnDataCollectionLifetime::REGULAR)
 ColumnDataAllocator (ClientContext &context, ColumnDataAllocatorType allocator_type, ColumnDataCollectionLifetime lifetime=ColumnDataCollectionLifetime::REGULAR)
 ColumnDataAllocator (ColumnDataAllocator &allocator)
Allocator & GetAllocator ()
 Returns an allocator object to allocate with. This returns the allocator in IN_MEMORY_ALLOCATOR, and a buffer allocator in case of BUFFER_MANAGER_ALLOCATOR.
BufferManager & GetBufferManager ()
 Returns the buffer manager, if this is not an in-memory allocation.
ColumnDataAllocatorType GetType ()
 Returns the allocator type.
void MakeShared ()
bool IsShared () const
idx_t BlockCount () const
idx_t SizeInBytes () const
idx_t AllocationSize () const
void SetPartitionIndex (idx_t index)
 Sets the partition index of this tuple data collection.
void AllocateData (idx_t size, uint32_t &block_id, uint32_t &offset, ChunkManagementState *chunk_state)
void Initialize (ColumnDataAllocator &other)
void InitializeChunkState (ChunkManagementState &state, ChunkMetaData &meta_data)
data_ptr_t GetDataPointer (ChunkManagementState &state, uint32_t block_id, uint32_t offset)
void UnswizzlePointers (ChunkManagementState &state, Vector &result, SwizzleMetaData &swizzle_segment, const VectorMetaData &string_heap_segment, const idx_t &v_offset, const bool &copied)
void SetDestroyBufferUponUnpin (uint32_t block_id)
 Prevents the block with the given id from being added to the eviction queue.
shared_ptr< DatabaseInstance > GetDatabase () const
 Gets a shared pointer to the database instance if ColumnDataCollectionLifetime::DATABASE_INSTANCE.

Private Member Functions

void AllocateEmptyBlock (idx_t size)
BufferHandle AllocateBlock (idx_t size)
BufferHandle Pin (uint32_t block_id)
bool HasBlocks () const
void AllocateBuffer (idx_t size, uint32_t &block_id, uint32_t &offset, ChunkManagementState *chunk_state)
void AllocateMemory (idx_t size, uint32_t &block_id, uint32_t &offset, ChunkManagementState *chunk_state)
void AssignPointer (uint32_t &block_id, uint32_t &offset, data_ptr_t pointer)

Private Attributes

ColumnDataAllocatorType type
union { 
   Allocator *   allocator 
 The allocator object (if this is a IN_MEMORY_ALLOCATOR).
   BufferManager *   buffer_manager 
 The buffer manager (if this is a BUFFER_MANAGER_ALLOCATOR).
} alloc
vector< BlockMetaData > blocks
 The set of blocks used by the column data collection.
vector< AllocatedData > allocated_data
 The set of allocated data.
bool shared = false
 Whether this ColumnDataAllocator is shared across ColumnDataCollections that allocate in parallel.
mutex lock
 Lock used in case this ColumnDataAllocator is shared across threads.
idx_t allocated_size = 0
 Total allocated size.
optional_idx partition_index
 Partition index (optional, if partitioned).
ManagedResultSet managed_result_set
 Lifetime management for this allocator.

Constructor & Destructor Documentation

◆ ColumnDataAllocator() [1/4]

duckdb::ColumnDataAllocator::ColumnDataAllocator ( Allocator & allocator)
explicit
114068 alloc.allocator = &allocator;
114069}
Allocator * allocator
The allocator object (if this is a IN_MEMORY_ALLOCATOR).
Definition duckdb.hpp:46935
@ IN_MEMORY_ALLOCATOR
Definition duckdb.hpp:18802

◆ ColumnDataAllocator() [2/4]

duckdb::ColumnDataAllocator::ColumnDataAllocator ( BufferManager & buffer_manager,
ColumnDataCollectionLifetime lifetime = ColumnDataCollectionLifetime::REGULAR )
explicit
114073 alloc.buffer_manager = &buffer_manager;
114075 managed_result_set = ResultSetManager::Get(buffer_manager.GetDatabase()).Add(*this);
114076 }
114077}
ManagedResultSet managed_result_set
Lifetime management for this allocator.
Definition duckdb.hpp:46952
BufferManager * buffer_manager
The buffer manager (if this is a BUFFER_MANAGER_ALLOCATOR).
Definition duckdb.hpp:46937
@ BUFFER_MANAGER_ALLOCATOR
Use a buffer manager to allocate large chunks of memory that vectors then use.
Definition duckdb.hpp:18799
@ THROW_ERROR_AFTER_DATABASE_CLOSES
Definition duckdb.hpp:18822

◆ ColumnDataAllocator() [3/4]

duckdb::ColumnDataAllocator::ColumnDataAllocator ( ClientContext & context,
ColumnDataAllocatorType allocator_type,
ColumnDataCollectionLifetime lifetime = ColumnDataCollectionLifetime::REGULAR )
114081 : type(allocator_type) {
114082 switch (type) {
114085 alloc.buffer_manager = &BufferManager::GetBufferManager(context);
114087 managed_result_set = ResultSetManager::Get(context).Add(*this);
114088 }
114089 break;
114091 alloc.allocator = &Allocator::Get(context);
114092 break;
114093 default:
114094 throw InternalException("Unrecognized column data allocator type");
114095 }
114096}
@ HYBRID
Use a buffer manager to allocate vectors, but use a StringHeap for strings.
Definition duckdb.hpp:18804

◆ ColumnDataAllocator() [4/4]

duckdb::ColumnDataAllocator::ColumnDataAllocator ( ColumnDataAllocator & allocator)
114098 {
114099 type = other.GetType();
114100 switch (type) {
114103 alloc.buffer_manager = other.alloc.buffer_manager;
114104 if (other.managed_result_set.IsValid()) {
114105 ResultSetManager::Get(alloc.buffer_manager->GetDatabase()).Add(*this);
114106 }
114107 break;
114109 alloc.allocator = other.alloc.allocator;
114110 break;
114111 default:
114112 throw InternalException("Unrecognized column data allocator type");
114113 }
114114}

◆ ~ColumnDataAllocator()

duckdb::ColumnDataAllocator::~ColumnDataAllocator ( )
114116 {
114118 return;
114119 }
114120 if (managed_result_set.IsValid()) {
114122 auto db = managed_result_set.GetDatabase();
114123 if (db) {
114124 ResultSetManager::Get(*db).Remove(*this);
114125 }
114126 return;
114127 }
114128 for (auto &block : blocks) {
114129 block.GetHandle()->GetMemory().SetDestroyBufferUpon(DestroyBufferUpon::UNPIN);
114130 }
114131 blocks.clear();
114132}
vector< BlockMetaData > blocks
The set of blocks used by the column data collection.
Definition duckdb.hpp:46940
db
@ UNPIN
Destroy the data buffer upon eviction to storage (destroy instead of evict).
Definition duckdb.hpp:45581

Member Function Documentation

◆ GetAllocator()

Allocator & duckdb::ColumnDataAllocator::GetAllocator ( )

Returns an allocator object to allocate with. This returns the allocator in IN_MEMORY_ALLOCATOR, and a buffer allocator in case of BUFFER_MANAGER_ALLOCATOR.

114327 {
114329 return *alloc.allocator;
114330 }
114331 return alloc.buffer_manager->GetBufferAllocator();
114332}

◆ GetBufferManager()

BufferManager & duckdb::ColumnDataAllocator::GetBufferManager ( )

Returns the buffer manager, if this is not an in-memory allocation.

114334 {
114336 throw InternalException("cannot obtain the buffer manager for in memory allocations");
114337 }
114338 return *alloc.buffer_manager;
114339}

◆ GetType()

ColumnDataAllocatorType duckdb::ColumnDataAllocator::GetType ( )
inline

Returns the allocator type.

46874 {
46875 return type;
46876 }

◆ MakeShared()

void duckdb::ColumnDataAllocator::MakeShared ( )
inline
46877 {
46878 shared = true;
46879 }
bool shared
Whether this ColumnDataAllocator is shared across ColumnDataCollections that allocate in parallel.
Definition duckdb.hpp:46944

◆ IsShared()

bool duckdb::ColumnDataAllocator::IsShared ( ) const
inline
46880 {
46881 return shared;
46882 }

◆ BlockCount()

idx_t duckdb::ColumnDataAllocator::BlockCount ( ) const
inline
46883 {
46884 return blocks.size();
46885 }

◆ SizeInBytes()

idx_t duckdb::ColumnDataAllocator::SizeInBytes ( ) const
inline
46886 {
46887 idx_t total_size = 0;
46888 for (const auto &block : blocks) {
46889 total_size += block.size;
46890 }
46891 return total_size;
46892 }
uint64_t idx_t
a saner size_t for loop indices etc
Definition duckdb.hpp:237

◆ AllocationSize()

idx_t duckdb::ColumnDataAllocator::AllocationSize ( ) const
inline
46893 {
46894 return allocated_size;
46895 }
idx_t allocated_size
Total allocated size.
Definition duckdb.hpp:46948

◆ SetPartitionIndex()

void duckdb::ColumnDataAllocator::SetPartitionIndex ( idx_t index)
inline

Sets the partition index of this tuple data collection.

46897 {
46898 D_ASSERT(!partition_index.IsValid());
46899 D_ASSERT(blocks.empty() && allocated_data.empty());
46901 }
optional_idx partition_index
Partition index (optional, if partitioned).
Definition duckdb.hpp:46950
vector< AllocatedData > allocated_data
The set of allocated data.
Definition duckdb.hpp:46942
index

◆ AllocateData()

void duckdb::ColumnDataAllocator::AllocateData ( idx_t size,
uint32_t & block_id,
uint32_t & offset,
ChunkManagementState * chunk_state )
114231 {
114232 switch (type) {
114235 if (shared) {
114236 lock_guard<mutex> guard(lock);
114237 AllocateBuffer(size, block_id, offset, chunk_state);
114238 } else {
114239 AllocateBuffer(size, block_id, offset, chunk_state);
114240 }
114241 break;
114243 D_ASSERT(!shared);
114244 AllocateMemory(size, block_id, offset, chunk_state);
114245 break;
114246 default:
114247 throw InternalException("Unrecognized allocator type");
114248 }
114249}
mutex lock
Lock used in case this ColumnDataAllocator is shared across threads.
Definition duckdb.hpp:46946

◆ Initialize()

void duckdb::ColumnDataAllocator::Initialize ( ColumnDataAllocator & other)
114251 {
114252 D_ASSERT(other.HasBlocks());
114253 blocks.push_back(other.blocks.back());
114254}

◆ InitializeChunkState()

void duckdb::ColumnDataAllocator::InitializeChunkState ( ChunkManagementState & state,
ChunkMetaData & meta_data )
114341 {
114343 // nothing to pin
114344 return;
114345 }
114346 // release any handles that are no longer required
114347 bool found_handle;
114348 do {
114349 found_handle = false;
114350 for (auto it = state.handles.begin(); it != state.handles.end(); it++) {
114351 if (chunk.block_ids.find(NumericCast<uint32_t>(it->first)) != chunk.block_ids.end()) {
114352 // still required: do not release
114353 continue;
114354 }
114355 state.handles.erase(it);
114356 found_handle = true;
114357 break;
114358 }
114359 } while (found_handle);
114360
114361 // grab any handles that are now required
114362 for (auto &block_id : chunk.block_ids) {
114363 if (state.handles.find(block_id) != state.handles.end()) {
114364 // already pinned: don't need to do anything
114365 continue;
114366 }
114367 state.handles[block_id] = Pin(block_id);
114368 }
114369}

◆ GetDataPointer()

data_ptr_t duckdb::ColumnDataAllocator::GetDataPointer ( ChunkManagementState & state,
uint32_t block_id,
uint32_t offset )
114256 {
114258 // in-memory allocator: construct pointer from block_id and offset
114259 if (sizeof(uintptr_t) == sizeof(uint32_t)) {
114260 uintptr_t pointer_value = uintptr_t(block_id);
114261 return (data_ptr_t)pointer_value; // NOLINT - convert from pointer value back to pointer
114262 } else if (sizeof(uintptr_t) == sizeof(uint64_t)) {
114263 uintptr_t pointer_value = (uintptr_t(offset) << 32) | uintptr_t(block_id);
114264 return (data_ptr_t)pointer_value; // NOLINT - convert from pointer value back to pointer
114265 } else {
114266 throw InternalException("ColumnDataCollection: Architecture not supported!?");
114267 }
114268 }
114269 D_ASSERT(state.handles.find(block_id) != state.handles.end());
114270 return state.handles[block_id].Ptr() + offset;
114271}

◆ UnswizzlePointers()

void duckdb::ColumnDataAllocator::UnswizzlePointers ( ChunkManagementState & state,
Vector & result,
SwizzleMetaData & swizzle_segment,
const VectorMetaData & string_heap_segment,
const idx_t & v_offset,
const bool & copied )
114275 {
114276 D_ASSERT(result.GetType().InternalType() == PhysicalType::VARCHAR);
114277 lock_guard<mutex> guard(lock);
114278 const auto old_base_ptr = char_ptr_cast(swizzle_segment.ptr);
114279 const auto new_base_ptr =
114280 char_ptr_cast(GetDataPointer(state, string_heap_segment.block_id, string_heap_segment.offset));
114281 if (old_base_ptr == new_base_ptr) {
114282 return; // pointers are still valid
114283 }
114284
114285 const auto &validity = FlatVector::Validity(result);
114286 const auto strings = FlatVector::GetData<string_t>(result);
114287
114288 // recompute pointers
114289 const auto start = NumericCast<idx_t>(v_offset + swizzle_segment.offset);
114290 const auto end = start + NumericCast<idx_t>(swizzle_segment.count);
114291 for (idx_t i = start; i < end; i++) {
114292 auto &str = strings[i];
114293 if (!validity.RowIsValid(i) || str.IsInlined()) {
114294 continue;
114295 }
114296 const auto str_offset = str.GetPointer() - old_base_ptr;
114297 D_ASSERT(str_offset >= 0);
114298 str.SetPointer(new_base_ptr + str_offset);
114299#ifdef D_ASSERT_IS_ENABLED
114300 if (result.GetType() == LogicalType::VARCHAR) {
114301 str.Verify();
114302 }
114303#endif
114304 }
114305
114306 if (!copied) {
114307 // if the data was not copied, we modified data on the blocks. store the new base ptr
114308 swizzle_segment.ptr = data_ptr_cast(new_base_ptr);
114309 }
114310}
@ VARCHAR
DuckDB Extensions.
Definition duckdb.hpp:2847

◆ SetDestroyBufferUponUnpin()

void duckdb::ColumnDataAllocator::SetDestroyBufferUponUnpin ( uint32_t block_id)

Prevents the block with the given id from being added to the eviction queue.

114312 {
114313 blocks[block_id].GetHandle()->GetMemory().SetDestroyBufferUpon(DestroyBufferUpon::UNPIN);
114314}

◆ GetDatabase()

shared_ptr< DatabaseInstance > duckdb::ColumnDataAllocator::GetDatabase ( ) const

Gets a shared pointer to the database instance if ColumnDataCollectionLifetime::DATABASE_INSTANCE.

114316 {
114317 if (!managed_result_set.IsValid()) {
114318 return nullptr;
114319 }
114320 auto db = managed_result_set.GetDatabase();
114321 if (!db) {
114322 throw ConnectionException("Trying to access a query result after the database instance has been closed");
114323 }
114324 return db;
114325}

◆ AllocateEmptyBlock()

void duckdb::ColumnDataAllocator::AllocateEmptyBlock ( idx_t size)
private
114165 {
114166 auto allocation_amount = MaxValue<idx_t>(NextPowerOfTwo(size), 4096);
114167 if (!blocks.empty()) {
114168 idx_t last_capacity = blocks.back().capacity;
114169 auto next_capacity = MinValue<idx_t>(last_capacity * 2, last_capacity + Storage::DEFAULT_BLOCK_SIZE);
114170 allocation_amount = MaxValue<idx_t>(next_capacity, allocation_amount);
114171 }
114173 BlockMetaData data;
114174 data.size = 0;
114175 data.capacity = NumericCast<uint32_t>(allocation_amount);
114176 blocks.push_back(std::move(data));
114177 allocated_size += allocation_amount;
114178}
static constexpr idx_t DEFAULT_BLOCK_SIZE
The default block size.
Definition duckdb.hpp:9720

◆ AllocateBlock()

BufferHandle duckdb::ColumnDataAllocator::AllocateBlock ( idx_t size)
private
114148 {
114150 auto max_size = MaxValue<idx_t>(size, GetBufferManager().GetBlockSize());
114151 BlockMetaData data;
114152 data.size = 0;
114153 data.capacity = NumericCast<uint32_t>(max_size);
114154 auto pin = alloc.buffer_manager->Allocate(MemoryTag::COLUMN_DATA, max_size, false);
114155 data.SetHandle(managed_result_set, pin.GetBlockHandle());
114156 blocks.push_back(std::move(data));
114157 if (partition_index.IsValid()) { // Set the eviction queue index logarithmically using RadixBits
114158 blocks.back().GetHandle()->GetMemory().SetEvictionQueueIndex(
114159 RadixPartitioning::RadixBits(partition_index.GetIndex()));
114160 }
114161 allocated_size += max_size;
114162 return pin;
114163}
BufferManager & GetBufferManager()
Returns the buffer manager, if this is not an in-memory allocation.
Definition duckdb.cpp:114334

◆ Pin()

BufferHandle duckdb::ColumnDataAllocator::Pin ( uint32_t block_id)
private
114134 {
114136 shared_ptr<BlockHandle> handle;
114137 if (shared) {
114138 // we only need to grab the lock when accessing the vector, because vector access is not thread-safe:
114139 // the vector can be resized by another thread while we try to access it
114140 lock_guard<mutex> guard(lock);
114141 handle = blocks[block_id].GetHandle();
114142 } else {
114143 handle = blocks[block_id].GetHandle();
114144 }
114145 return alloc.buffer_manager->Pin(handle);
114146}

◆ HasBlocks()

bool duckdb::ColumnDataAllocator::HasBlocks ( ) const
inlineprivate
46922 {
46923 return !blocks.empty();
46924 }

◆ AllocateBuffer()

void duckdb::ColumnDataAllocator::AllocateBuffer ( idx_t size,
uint32_t & block_id,
uint32_t & offset,
ChunkManagementState * chunk_state )
private
114193 {
114194 D_ASSERT(allocated_data.empty());
114195 if (blocks.empty() || blocks.back().Capacity() < size) {
114196 auto pinned_block = AllocateBlock(size);
114197 if (chunk_state) {
114198 D_ASSERT(!blocks.empty());
114199 auto new_block_id = blocks.size() - 1;
114200 chunk_state->handles[new_block_id] = std::move(pinned_block);
114201 }
114202 }
114203 auto &block = blocks.back();
114204 D_ASSERT(size <= block.capacity - block.size);
114205 block_id = NumericCast<uint32_t>(blocks.size() - 1);
114206 if (chunk_state && chunk_state->handles.find(block_id) == chunk_state->handles.end()) {
114207 // not guaranteed to be pinned already by this thread (if shared allocator)
114208 auto handle = blocks[block_id].GetHandle();
114209 chunk_state->handles[block_id] = alloc.buffer_manager->Pin(handle);
114210 }
114211 offset = block.size;
114212 block.size += size;
114213}
GOpaque< Size > size(const GMat &src)

◆ AllocateMemory()

void duckdb::ColumnDataAllocator::AllocateMemory ( idx_t size,
uint32_t & block_id,
uint32_t & offset,
ChunkManagementState * chunk_state )
private
114216 {
114217 D_ASSERT(blocks.size() == allocated_data.size());
114218 if (blocks.empty() || blocks.back().Capacity() < size) {
114219 AllocateEmptyBlock(size);
114220 auto &last_block = blocks.back();
114221 auto allocated = alloc.allocator->Allocate(last_block.capacity);
114222 allocated_data.push_back(std::move(allocated));
114223 }
114224 auto &block = blocks.back();
114225 D_ASSERT(size <= block.capacity - block.size);
114226 AssignPointer(block_id, offset, allocated_data.back().get() + block.size);
114227 block.size += size;
114228}

◆ AssignPointer()

void duckdb::ColumnDataAllocator::AssignPointer ( uint32_t & block_id,
uint32_t & offset,
data_ptr_t pointer )
private
114180 {
114181 auto pointer_value = uintptr_t(pointer);
114182 if (sizeof(uintptr_t) == sizeof(uint32_t)) {
114183 block_id = uint32_t(pointer_value);
114184 } else if (sizeof(uintptr_t) == sizeof(uint64_t)) {
114185 block_id = uint32_t(pointer_value & 0xFFFFFFFF);
114186 offset = uint32_t(pointer_value >> 32);
114187 } else {
114188 throw InternalException("ColumnDataCollection: Architecture not supported!?");
114189 }
114190}
::uint32_t uint32_t

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