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

Public Types

typedef void(* initialize_update_function_t) (UpdateInfo &base_info, Vector &base_data, UpdateInfo &update_info, UnifiedVectorFormat &update, const SelectionVector &sel)
typedef void(* merge_update_function_t) (UpdateInfo &base_info, Vector &base_data, UpdateInfo &update_info, UnifiedVectorFormat &update, row_t *ids, idx_t count, const SelectionVector &sel, idx_t row_group_start)
typedef void(* fetch_update_function_t) (transaction_t start_time, transaction_t transaction_id, UpdateInfo &info, Vector &result)
typedef void(* fetch_committed_function_t) (UpdateInfo &info, Vector &result)
typedef void(* fetch_committed_range_function_t) (UpdateInfo &info, idx_t start, idx_t end, idx_t result_offset, Vector &result)
typedef void(* fetch_row_function_t) (transaction_t start_time, transaction_t transaction_id, UpdateInfo &info, idx_t row_idx, Vector &result, idx_t result_idx)
typedef void(* rollback_update_function_t) (UpdateInfo &base_info, UpdateInfo &rollback_info)
typedef idx_t(* statistics_update_function_t) (UpdateSegment *segment, SegmentStatistics &stats, UnifiedVectorFormat &update, idx_t count, SelectionVector &sel)
typedef idx_t(* get_effective_updates_t) (UnifiedVectorFormat &update_format, row_t *ids, idx_t count, SelectionVector &sel, Vector &base_data, idx_t id_offset)

Public Member Functions

 UpdateSegment (ColumnData &column_data)
bool HasUpdates () const
bool HasUncommittedUpdates (idx_t vector_index)
bool HasUpdates (idx_t vector_index) const
bool HasUpdates (idx_t start_row_idx, idx_t end_row_idx)
void FetchUpdates (TransactionData transaction, idx_t vector_index, Vector &result)
void FetchCommitted (idx_t vector_index, Vector &result)
void FetchCommittedRange (idx_t start_row, idx_t count, Vector &result)
void Update (TransactionData transaction, DataTable &data_table, idx_t column_index, Vector &update, row_t *ids, idx_t count, Vector &base_data, idx_t row_group_start)
void FetchRow (TransactionData transaction, idx_t row_id, Vector &result, idx_t result_idx)
void RollbackUpdate (UpdateInfo &info)
void CleanupUpdateInternal (const StorageLockKey &lock, UpdateInfo &info)
void CleanupUpdate (UpdateInfo &info)
unique_ptr< BaseStatistics > GetStatistics ()
StringHeap & GetStringHeap ()

Public Attributes

ColumnData & column_data

Private Member Functions

UndoBufferPointer GetUpdateNode (StorageLockKey &lock, idx_t vector_idx) const
void InitializeUpdateInfo (idx_t vector_idx)
void InitializeUpdateInfo (UpdateInfo &info, row_t *ids, const SelectionVector &sel, idx_t count, idx_t vector_index, idx_t vector_offset)

Private Attributes

StorageLock lock
 The lock for the update segment.
unique_ptr< UpdateNode > root
 The root node (if any).
SegmentStatistics stats
 Update statistics.
mutex stats_lock
 Stats lock.
idx_t type_size
 Internal type size.
StringHeap heap
 String heap, only used for strings.
initialize_update_function_t initialize_update_function
merge_update_function_t merge_update_function
fetch_update_function_t fetch_update_function
fetch_committed_function_t fetch_committed_function
fetch_committed_range_function_t fetch_committed_range
fetch_row_function_t fetch_row_function
rollback_update_function_t rollback_update_function
statistics_update_function_t statistics_update_function
get_effective_updates_t get_effective_updates

Constructor & Destructor Documentation

◆ UpdateSegment()

duckdb::UpdateSegment::UpdateSegment ( ColumnData & column_data)
explicit
438866 : column_data(column_data), stats(column_data.type), heap(BufferAllocator::Get(column_data.GetDatabase())) {
438867 auto physical_type = column_data.type.InternalType();
438868
438869 this->type_size = GetTypeIdSize(physical_type);
438870
438871 this->initialize_update_function = GetInitializeUpdateFunction(physical_type);
438872 this->fetch_update_function = GetFetchUpdateFunction(physical_type);
438873 this->fetch_committed_function = GetFetchCommittedFunction(physical_type);
438874 this->fetch_committed_range = GetFetchCommittedRangeFunction(physical_type);
438875 this->fetch_row_function = GetFetchRowFunction(physical_type);
438876 this->merge_update_function = GetMergeUpdateFunction(physical_type);
438877 this->rollback_update_function = GetRollbackUpdateFunction(physical_type);
438878 this->statistics_update_function = GetStatisticsUpdateFunction(physical_type);
438879 this->get_effective_updates = GetEffectiveUpdatesFunction(physical_type);
438880}
SegmentStatistics stats
Update statistics.
Definition duckdb.cpp:428732
idx_t type_size
Internal type size.
Definition duckdb.cpp:428736
StringHeap heap
String heap, only used for strings.
Definition duckdb.cpp:428738

◆ ~UpdateSegment()

duckdb::UpdateSegment::~UpdateSegment ( )
438882 {
438883}

Member Function Documentation

◆ HasUpdates() [1/3]

bool duckdb::UpdateSegment::HasUpdates ( ) const
440258 {
440259 return root.get() != nullptr;
440260}
unique_ptr< UpdateNode > root
The root node (if any).
Definition duckdb.cpp:428730

◆ HasUncommittedUpdates()

bool duckdb::UpdateSegment::HasUncommittedUpdates ( idx_t vector_index)
440267 {
440268 auto read_lock = lock.GetSharedLock();
440269 auto entry = GetUpdateNode(*read_lock, vector_index);
440270 if (!entry.IsSet()) {
440271 return false;
440272 }
440273 auto pin = entry.Pin();
440274 auto &info = UpdateInfo::Get(pin);
440275 if (info.HasNext()) {
440276 return true;
440277 }
440278 return false;
440279}
StorageLock lock
The lock for the update segment.
Definition duckdb.cpp:428728

◆ HasUpdates() [2/3]

bool duckdb::UpdateSegment::HasUpdates ( idx_t vector_index) const
440262 {
440263 auto read_lock = lock.GetSharedLock();
440264 return GetUpdateNode(*read_lock, vector_index).IsSet();
440265}

◆ HasUpdates() [3/3]

bool duckdb::UpdateSegment::HasUpdates ( idx_t start_row_idx,
idx_t end_row_idx )
440281 {
440282 auto read_lock = lock.GetSharedLock();
440283 if (!root) {
440284 return false;
440285 }
440286 idx_t base_vector_index = start_row_index / STANDARD_VECTOR_SIZE;
440287 idx_t end_vector_index = end_row_index / STANDARD_VECTOR_SIZE;
440288 for (idx_t i = base_vector_index; i <= end_vector_index; i++) {
440289 auto entry = GetUpdateNode(*read_lock, i);
440290 if (entry.IsSet()) {
440291 return true;
440292 }
440293 }
440294 return false;
440295}
uint64_t idx_t
a saner size_t for loop indices etc
Definition duckdb.hpp:237

◆ FetchUpdates()

void duckdb::UpdateSegment::FetchUpdates ( TransactionData transaction,
idx_t vector_index,
Vector & result )
439055 {
439056 auto lock_handle = lock.GetSharedLock();
439057 auto node = GetUpdateNode(*lock_handle, vector_index);
439058 if (!node.IsSet()) {
439059 return;
439060 }
439061 // FIXME: normalify if this is not the case... need to pass in count?
439062 D_ASSERT(result.GetVectorType() == VectorType::FLAT_VECTOR);
439063 auto pin = node.Pin();
439064 fetch_update_function(transaction.start_time, transaction.transaction_id, UpdateInfo::Get(pin), result);
439065}

◆ FetchCommitted()

void duckdb::UpdateSegment::FetchCommitted ( idx_t vector_index,
Vector & result )
439125 {
439126 auto lock_handle = lock.GetSharedLock();
439127 auto node = GetUpdateNode(*lock_handle, vector_index);
439128 if (!node.IsSet()) {
439129 return;
439130 }
439131 // FIXME: normalify if this is not the case... need to pass in count?
439132 D_ASSERT(result.GetVectorType() == VectorType::FLAT_VECTOR);
439133 auto pin = node.Pin();
439134 fetch_committed_function(UpdateInfo::Get(pin), result);
439135}

◆ FetchCommittedRange()

void duckdb::UpdateSegment::FetchCommittedRange ( idx_t start_row,
idx_t count,
Vector & result )
439222 {
439223 D_ASSERT(count > 0);
439224 if (!root) {
439225 return;
439226 }
439227 D_ASSERT(start_row <= column_data.count);
439228 D_ASSERT(start_row + count <= column_data.count);
439229 D_ASSERT(result.GetVectorType() == VectorType::FLAT_VECTOR);
439230
439231 idx_t end_row = start_row + count;
439232 idx_t start_vector = start_row / STANDARD_VECTOR_SIZE;
439233 idx_t end_vector = (end_row - 1) / STANDARD_VECTOR_SIZE;
439234 D_ASSERT(start_vector <= end_vector);
439235
439236 auto lock_handle = lock.GetSharedLock();
439237 for (idx_t vector_idx = start_vector; vector_idx <= end_vector; vector_idx++) {
439238 auto entry = GetUpdateNode(*lock_handle, vector_idx);
439239 if (!entry.IsSet()) {
439240 continue;
439241 }
439242 auto pin = entry.Pin();
439243 idx_t start_in_vector = vector_idx == start_vector ? start_row - start_vector * STANDARD_VECTOR_SIZE : 0;
439244 idx_t end_in_vector =
439245 vector_idx == end_vector ? end_row - end_vector * STANDARD_VECTOR_SIZE : STANDARD_VECTOR_SIZE;
439246 D_ASSERT(start_in_vector < end_in_vector);
439247 D_ASSERT(end_in_vector > 0 && end_in_vector <= STANDARD_VECTOR_SIZE);
439248 idx_t result_offset = ((vector_idx * STANDARD_VECTOR_SIZE) + start_in_vector) - start_row;
439249 fetch_committed_range(UpdateInfo::Get(pin), start_in_vector, end_in_vector, result_offset, result);
439250 }
439251}

◆ Update()

void duckdb::UpdateSegment::Update ( TransactionData transaction,
DataTable & data_table,
idx_t column_index,
Vector & update,
row_t * ids,
idx_t count,
Vector & base_data,
idx_t row_group_start )
440122 {
440123 // obtain an exclusive lock
440124 auto write_lock = lock.GetExclusiveLock();
440125
440126 UnifiedVectorFormat update_format;
440127 update_p.ToUnifiedFormat(count, update_format);
440128
440129 // update statistics
440130 SelectionVector sel;
440131 {
440132 lock_guard<mutex> stats_guard(stats_lock);
440133 count = statistics_update_function(this, stats, update_format, count, sel);
440134 }
440135 if (count == 0) {
440136 return;
440137 }
440138
440139 // subsequent algorithms used by the update require row ids to be (1) sorted, and (2) unique
440140 // this is usually the case for "standard" queries (e.g. UPDATE tbl SET x=bla WHERE cond)
440141 // however, for more exotic queries involving e.g. cross products/joins this might not be the case
440142 // hence we explicitly check here if the ids are sorted and, if not, sort + duplicate eliminate them
440143 count = SortSelectionVector(sel, count, ids);
440144 D_ASSERT(count > 0);
440145
440146 // get the vector index based on the first id
440147 // we assert that all updates must be part of the same vector
440148 auto first_id = ids[sel.get_index(0)];
440149 idx_t vector_index = (UnsafeNumericCast<idx_t>(first_id) - row_group_start) / STANDARD_VECTOR_SIZE;
440150 idx_t vector_offset = row_group_start + vector_index * STANDARD_VECTOR_SIZE;
440151
440152 if (!root || vector_index >= root->info.size() || !root->info[vector_index].IsSet()) {
440153 // get a list of effective updates - i.e. updates that actually change rows
440154 // if updates have the same value as the base row we can skip them
440155 // we only do that if we have no updates for this vector
440156 // we could do it otherwise - but that would require merging updates in order to find the current values
440157 count = get_effective_updates(update_format, ids, count, sel, base_data, vector_offset);
440158 if (count == 0) {
440159 return;
440160 }
440161 }
440162
440163 InitializeUpdateInfo(vector_index);
440164
440165 D_ASSERT(idx_t(first_id) >= row_group_start);
440166
440167 if (root->info[vector_index].IsSet()) {
440168 // there is already a version here, check if there are any conflicts and search for the node that belongs to
440169 // this transaction in the version chain
440170 auto root_pointer = root->info[vector_index];
440171 auto root_pin = root_pointer.Pin();
440172 auto &base_info = UpdateInfo::Get(root_pin);
440173
440174 UndoBufferReference node_ref;
440175 CheckForConflicts(base_info.next, transaction, ids, sel, count, UnsafeNumericCast<row_t>(vector_offset),
440176 node_ref);
440177
440178 // there are no conflicts - continue with the update
440179 unsafe_unique_array<char> update_info_data;
440180 optional_ptr<UpdateInfo> node;
440181 if (!node_ref.IsSet()) {
440182 // no updates made yet by this transaction: initially the update info to empty
440183 if (transaction.transaction) {
440184 auto &dtransaction = transaction.transaction->Cast<DuckTransaction>();
440185 node_ref = dtransaction.CreateUpdateInfo(type_size, data_table, count, row_group_start);
440186 node = &UpdateInfo::Get(node_ref);
440187 } else {
440188 node =
440189 CreateEmptyUpdateInfo(transaction, data_table, type_size, count, update_info_data, row_group_start);
440190 }
440191 node->segment = this;
440192 node->vector_index = vector_index;
440193 node->N = 0;
440194 node->column_index = column_index;
440195
440196 // insert the new node into the chain
440197 node->next = base_info.next;
440198 if (node->next.IsSet()) {
440199 auto next_pin = node->next.Pin();
440200 auto &next_info = UpdateInfo::Get(next_pin);
440201 next_info.prev = node_ref.GetBufferPointer();
440202 }
440203 node->prev = root_pointer;
440204 base_info.next = transaction.transaction ? node_ref.GetBufferPointer() : UndoBufferPointer();
440205 } else {
440206 // we already had updates made to this transaction
440207 node = &UpdateInfo::Get(node_ref);
440208 }
440209 base_info.Verify();
440210 node->Verify();
440211
440212 // now we are going to perform the merge
440213 merge_update_function(base_info, base_data, *node, update_format, ids, count, sel, row_group_start);
440214
440215 base_info.Verify();
440216 node->Verify();
440217 } else {
440218 // there is no version info yet: create the top level update info and fill it with the updates
440219 // allocate space for the UpdateInfo in the allocator
440221 auto handle = root->allocator.Allocate(alloc_size);
440222 auto &update_info = UpdateInfo::Get(handle);
440223 UpdateInfo::Initialize(update_info, data_table, TRANSACTION_ID_START - 1, row_group_start);
440224 update_info.column_index = column_index;
440225
440226 InitializeUpdateInfo(update_info, ids, sel, count, vector_index, vector_offset);
440227
440228 // now create the transaction level update info in the undo log
440229 unsafe_unique_array<char> update_info_data;
440230 UndoBufferReference node_ref;
440231 optional_ptr<UpdateInfo> transaction_node;
440232 if (transaction.transaction) {
440233 node_ref = transaction.transaction->CreateUpdateInfo(type_size, data_table, count, row_group_start);
440234 transaction_node = &UpdateInfo::Get(node_ref);
440235 } else {
440236 transaction_node =
440237 CreateEmptyUpdateInfo(transaction, data_table, type_size, count, update_info_data, row_group_start);
440238 }
440239
440240 InitializeUpdateInfo(*transaction_node, ids, sel, count, vector_index, vector_offset);
440241
440242 // we write the updates in the update node data, and write the updates in the info
440243 initialize_update_function(*transaction_node, base_data, update_info, update_format, sel);
440244
440245 update_info.next = transaction.transaction ? node_ref.GetBufferPointer() : UndoBufferPointer();
440246 update_info.prev = UndoBufferPointer();
440247 transaction_node->next = UndoBufferPointer();
440248 transaction_node->prev = handle.GetBufferPointer();
440249 transaction_node->column_index = column_index;
440250
440251 transaction_node->Verify();
440252 update_info.Verify();
440253
440254 root->info[vector_index] = handle.GetBufferPointer();
440255 }
440256}
mutex stats_lock
Stats lock.
Definition duckdb.cpp:428734
static void Initialize(UpdateInfo &info, DataTable &data_table, transaction_t transaction_id, idx_t row_group_start)
Initialize an UpdateInfo struct that has been allocated using GetAllocSize (i.e. has extra space afte...
Definition duckdb.cpp:438946
static idx_t GetAllocSize(idx_t type_size)
Returns the total allocation size for an UpdateInfo entry, together with space for the tuple data.
Definition duckdb.cpp:438942

◆ FetchRow()

void duckdb::UpdateSegment::FetchRow ( TransactionData transaction,
idx_t row_id,
Vector & result,
idx_t result_idx )
439331 {
439332 if (row_id > column_data.count) {
439333 throw InternalException("UpdateSegment::FetchRow out of range");
439334 }
439335 idx_t vector_index = row_id / STANDARD_VECTOR_SIZE;
439336 auto lock_handle = lock.GetSharedLock();
439337 auto entry = GetUpdateNode(*lock_handle, vector_index);
439338 if (!entry.IsSet()) {
439339 return;
439340 }
439341 idx_t row_in_vector = row_id - vector_index * STANDARD_VECTOR_SIZE;
439342 auto pin = entry.Pin();
439343 fetch_row_function(transaction.start_time, transaction.transaction_id, UpdateInfo::Get(pin), row_in_vector, result,
439344 result_idx);
439345}

◆ RollbackUpdate()

void duckdb::UpdateSegment::RollbackUpdate ( UpdateInfo & info)
439405 {
439406 // obtain an exclusive lock
439407 auto lock_handle = lock.GetExclusiveLock();
439408
439409 // move the data from the UpdateInfo back into the base info
439410 auto entry = GetUpdateNode(*lock_handle, info.vector_index);
439411 if (!entry.IsSet()) {
439412 return;
439413 }
439414 auto pin = entry.Pin();
439415 rollback_update_function(UpdateInfo::Get(pin), info);
439416
439417 // clean up the update chain
439418 CleanupUpdateInternal(*lock_handle, info);
439419}

◆ CleanupUpdateInternal()

void duckdb::UpdateSegment::CleanupUpdateInternal ( const StorageLockKey & lock,
UpdateInfo & info )
439424 {
439425 if (info.HasPrev()) {
439426 auto pin = info.prev.Pin();
439427 auto &prev_info = UpdateInfo::Get(pin);
439428 prev_info.next = info.next;
439429 }
439430 if (info.HasNext()) {
439431 auto next = info.next;
439432 auto next_pin = next.Pin();
439433 auto &next_info = UpdateInfo::Get(next_pin);
439434 next_info.prev = info.prev;
439435 }
439436}

◆ CleanupUpdate()

void duckdb::UpdateSegment::CleanupUpdate ( UpdateInfo & info)
439438 {
439439 // obtain an exclusive lock
439440 auto lock_handle = lock.GetExclusiveLock();
439441 CleanupUpdateInternal(*lock_handle, info);
439442}

◆ GetStatistics()

unique_ptr< BaseStatistics > duckdb::UpdateSegment::GetStatistics ( )
439827 {
439828 lock_guard<mutex> stats_guard(stats_lock);
439829 return stats.statistics.ToUnique();
439830}

◆ GetStringHeap()

StringHeap & duckdb::UpdateSegment::GetStringHeap ( )
inline
428722 {
428723 return heap;
428724 }

◆ GetUpdateNode()

UndoBufferPointer duckdb::UpdateSegment::GetUpdateNode ( StorageLockKey & lock,
idx_t vector_idx ) const
private
439045 {
439046 if (!root) {
439047 return UndoBufferPointer();
439048 }
439049 if (vector_idx >= root->info.size()) {
439050 return UndoBufferPointer();
439051 }
439052 return root->info[vector_idx];
439053}

◆ InitializeUpdateInfo() [1/2]

void duckdb::UpdateSegment::InitializeUpdateInfo ( idx_t vector_idx)
private
440107 {
440108 // create the versions for this segment, if there are none yet
440109 if (!root) {
440110 root = make_uniq<UpdateNode>(column_data.block_manager.buffer_manager);
440111 }
440112 if (vector_idx < root->info.size()) {
440113 return;
440114 }
440115 root->info.reserve(vector_idx + 1);
440116 for (idx_t i = root->info.size(); i <= vector_idx; i++) {
440117 root->info.emplace_back();
440118 }
440119}

◆ InitializeUpdateInfo() [2/2]

void duckdb::UpdateSegment::InitializeUpdateInfo ( UpdateInfo & info,
row_t * ids,
const SelectionVector & sel,
idx_t count,
idx_t vector_index,
idx_t vector_offset )
private
439487 {
439488 info.segment = this;
439489 info.vector_index = vector_index;
439490 info.prev = UndoBufferPointer();
439491 info.next = UndoBufferPointer();
439492
439493 // set up the tuple ids
439494 info.N = UnsafeNumericCast<sel_t>(count);
439495 auto tuples = info.GetTuples();
439496 for (idx_t i = 0; i < count; i++) {
439497 auto idx = sel.get_index(i);
439498 auto id = ids[idx];
439499 D_ASSERT(idx_t(id) >= vector_offset && idx_t(id) < vector_offset + STANDARD_VECTOR_SIZE);
439500 tuples[i] = NumericCast<sel_t>(NumericCast<idx_t>(id) - vector_offset);
439501 };
439502}

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