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

Public Member Functions

TableIndexIterationHelper< IndexEntry > IndexEntries () const
TableIndexIterationHelper< Index > Indexes () const
void AddIndex (unique_ptr< Index > index)
 Adds an index entry to the list of index entries.
void RemoveIndex (const string &name)
 Removes an index entry from the list of index entries.
void CommitDrop (const string &name)
 Removes all remaining memory of an index after dropping the catalog entry.
bool NameIsUnique (const string &name)
 Returns true, if the index name does not exist.
optional_ptr< BoundIndex > Find (const string &name)
 Returns an optional pointer to the index matching the name.
void Bind (ClientContext &context, DataTableInfo &table_info, const char *index_type=nullptr)
 Binds unbound indexes possibly present after loading an extension.
bool Empty () const
 Returns true, if there are no index entries.
idx_t Count () const
 Returns the number of index entries.
bool HasUnbound () const
 Returns true, if there are unbound indexes.
unordered_set< string > DistinctIndexTypes () const
 Returns the set of distinct index types across all bound indexes.
void Move (TableIndexList &other)
 Overwrite this list with the other list.
void MergeCheckpointDeltas (transaction_t checkpoint_id)
 Merge any changes added to deltas during a checkpoint back into the main indexes.
optional_ptr< IndexEntry > FindForeignKeyIndex (const vector< PhysicalIndex > &fk_keys, const ForeignKeyType fk_type)
 Returns true, if all indexes Find the foreign key matching the keys.
void VerifyForeignKey (optional_ptr< LocalTableStorage > storage, const vector< PhysicalIndex > &fk_keys, DataChunk &chunk, ConflictManager &conflict_manager)
 Verify a foreign key constraint.
unordered_set< column_t > GetRequiredColumns ()
 Get the combined column ids of the indexes.
IndexSerializationResult SerializeToDisk (QueryContext context, const IndexSerializationInfo &info)
 Serialize all indexes of the table.

Static Public Member Functions

static void InitializeIndexChunk (DataChunk &index_chunk, const vector< LogicalType > &table_types, vector< StorageIndex > &mapped_column_ids, DataTableInfo &data_table_info)
 Initialize an index_chunk from a table.
static void ReferenceIndexChunk (DataChunk &table_chunk, DataChunk &index_chunk, vector< StorageIndex > &mapped_column_ids)
 Reference the indexed columns of a table chunk.

Private Attributes

mutex index_entries_lock
 A lock to prevent any concurrent changes to the index entries.
vector< unique_ptr< IndexEntry > > index_entries
 The index entries of the table.
idx_t unbound_count = 0
 Contains the number of unbound indexes.

Member Function Documentation

◆ IndexEntries()

TableIndexIterationHelper< IndexEntry > duckdb::TableIndexList::IndexEntries ( ) const
442313 {
442314 return TableIndexIterationHelper<IndexEntry>(index_entries_lock, index_entries);
442315}
vector< unique_ptr< IndexEntry > > index_entries
The index entries of the table.
Definition duckdb.hpp:50303
mutex index_entries_lock
A lock to prevent any concurrent changes to the index entries.
Definition duckdb.hpp:50301

◆ Indexes()

TableIndexIterationHelper< Index > duckdb::TableIndexList::Indexes ( ) const
442317 {
442318 return TableIndexIterationHelper<Index>(index_entries_lock, index_entries);
442319}

◆ AddIndex()

void duckdb::TableIndexList::AddIndex ( unique_ptr< Index > index)

Adds an index entry to the list of index entries.

442324 {
442325 D_ASSERT(index);
442326 lock_guard<mutex> lock(index_entries_lock);
442327 auto index_entry = make_uniq<IndexEntry>(std::move(index));
442328 index_entries.push_back(std::move(index_entry));
442329 if (!index_entries.back()->index->IsBound()) {
442330 unbound_count++;
442331 }
442332}
idx_t unbound_count
Contains the number of unbound indexes.
Definition duckdb.hpp:50305

◆ RemoveIndex()

void duckdb::TableIndexList::RemoveIndex ( const string & name)

Removes an index entry from the list of index entries.

442334 {
442335 lock_guard<mutex> lock(index_entries_lock);
442336 for (idx_t i = 0; i < index_entries.size(); i++) {
442337 auto &index = *index_entries[i]->index;
442338 if (index.GetIndexName() == name) {
442339 if (!index.IsBound()) {
442340 unbound_count--;
442341 }
442342 index_entries.erase_at(i);
442343 return;
442344 }
442345 }
442346}
index
uint64_t idx_t
a saner size_t for loop indices etc
Definition duckdb.hpp:237

◆ CommitDrop()

void duckdb::TableIndexList::CommitDrop ( const string & name)

Removes all remaining memory of an index after dropping the catalog entry.

442348 {
442349 lock_guard<mutex> lock(index_entries_lock);
442350 for (auto &entry : index_entries) {
442351 auto &index = *entry->index;
442352 if (index.GetIndexName() == name) {
442353 index.CommitDrop();
442354 return;
442355 }
442356 }
442357}

◆ NameIsUnique()

bool duckdb::TableIndexList::NameIsUnique ( const string & name)

Returns true, if the index name does not exist.

442368 {
442369 // Only covers PK, FK, and UNIQUE indexes.
442370 lock_guard<mutex> lock(index_entries_lock);
442371 for (auto &entry : index_entries) {
442372 auto &index = *entry->index;
442373 if (index.IsPrimary() || index.IsForeign() || index.IsUnique()) {
442374 if (index.GetIndexName() == name) {
442375 return false;
442376 }
442377 }
442378 }
442379 return true;
442380}

◆ Find()

optional_ptr< BoundIndex > duckdb::TableIndexList::Find ( const string & name)

Returns an optional pointer to the index matching the name.

442382 {
442383 for (auto &entry : index_entries) {
442384 auto &index = *entry->index;
442385 if (index.GetIndexName() == name) {
442386 if (!index.IsBound()) {
442387 throw InternalException("cannot return an unbound index in TableIndexList::Find");
442388 }
442389 return index.Cast<BoundIndex>();
442390 }
442391 }
442392 return nullptr;
442393}

◆ Bind()

void duckdb::TableIndexList::Bind ( ClientContext & context,
DataTableInfo & table_info,
const char * index_type = nullptr )

Binds unbound indexes possibly present after loading an extension.

442395 {
442396 {
442397 // Early-out, if we have no unbound indexes.
442398 lock_guard<mutex> lock(index_entries_lock);
442399 if (unbound_count == 0) {
442400 return;
442401 }
442402 }
442403
442404 // Get the table from the catalog, so we can add it to the binder.
442405 auto &catalog = table_info.GetDB().GetCatalog();
442406 auto schema = table_info.GetSchemaName();
442407 auto table_name = table_info.GetTableName();
442408 auto &table_entry = catalog.GetEntry<TableCatalogEntry>(context, schema, table_name);
442409 auto &table = table_entry.Cast<DuckTableEntry>();
442410
442411 vector<LogicalType> column_types;
442412 vector<string> column_names;
442413 for (auto &col : table.GetColumns().Logical()) {
442414 column_types.push_back(col.Type());
442415 column_names.push_back(col.Name());
442416 }
442417
442418 unique_lock<mutex> lock(index_entries_lock);
442419 // Busy-spin trying to bind all indexes.
442420 while (true) {
442421 optional_ptr<IndexEntry> index_entry;
442422 for (auto &entry : index_entries) {
442423 auto &index = *entry->index;
442424 if (!index.IsBound() && (index_type == nullptr || index.GetIndexType() == index_type)) {
442425 index_entry = entry.get();
442426 break;
442427 }
442428 }
442429 if (!index_entry) {
442430 // We bound all indexes. (of this type)
442431 break;
442432 }
442433 if (index_entry->bind_state == IndexBindState::BINDING) {
442434 // Another thread is binding the index.
442435 // Lock and unlock the index entries so that the other thread can commit its changes.
442436 lock.unlock();
442437 lock.lock();
442438 continue;
442439
442440 } else if (index_entry->bind_state == IndexBindState::UNBOUND) {
442441 // We are the thread that'll bind the index.
442442 index_entry->bind_state = IndexBindState::BINDING;
442443 lock.unlock();
442444
442445 } else {
442446 throw InternalException("index entry bind state cannot be BOUND here");
442447 }
442448
442449 // Create a binder to bind this index.
442450 auto binder = Binder::CreateBinder(context);
442451
442452 // Add the table to the binder.
442453 vector<ColumnIndex> dummy_column_ids;
442454 binder->bind_context.AddBaseTable(0, string(), column_names, column_types, dummy_column_ids, table);
442455
442456 // Create an IndexBinder to bind the index
442457 IndexBinder idx_binder(*binder, context);
442458
442459 // Apply any outstanding buffered replays and replace the unbound index with a bound index.
442460 auto &unbound_index = index_entry->index->Cast<UnboundIndex>();
442461 auto bound_idx = idx_binder.BindIndex(unbound_index);
442462 if (unbound_index.HasBufferedReplays()) {
442463 // For replaying buffered index operations, we only want the physical column types (skip over
442464 // generated column types).
442465 vector<LogicalType> physical_column_types;
442466 for (auto &col : table.GetColumns().Physical()) {
442467 physical_column_types.push_back(col.Type());
442468 }
442469 bound_idx->ApplyBufferedReplays(physical_column_types, unbound_index.GetBufferedReplays(),
442470 unbound_index.GetMappedColumnIds());
442471 }
442472
442473 // Commit the bound index to the index entry.
442474 lock.lock();
442475 index_entry->bind_state = IndexBindState::BOUND;
442476 index_entry->index = std::move(bound_idx);
442477 unbound_count--;
442478 }
442479}
float index_type
Here is the call graph for this function:

◆ Empty()

bool duckdb::TableIndexList::Empty ( ) const
inline

Returns true, if there are no index entries.

50258 {
50259 return Count() == 0;
50260 }
idx_t Count() const
Returns the number of index entries.
Definition duckdb.hpp:50262
Here is the call graph for this function:

◆ Count()

idx_t duckdb::TableIndexList::Count ( ) const
inline

Returns the number of index entries.

50262 {
50263 lock_guard<mutex> lock(index_entries_lock);
50264 return index_entries.size();
50265 }
Here is the caller graph for this function:

◆ HasUnbound()

bool duckdb::TableIndexList::HasUnbound ( ) const
inline

Returns true, if there are unbound indexes.

50267 {
50268 lock_guard<mutex> lock(index_entries_lock);
50269 return unbound_count != 0;
50270 }

◆ DistinctIndexTypes()

unordered_set< string > duckdb::TableIndexList::DistinctIndexTypes ( ) const

Returns the set of distinct index types across all bound indexes.

442359 {
442360 lock_guard<mutex> lock(index_entries_lock);
442361 unordered_set<string> result;
442362 for (auto &entry : index_entries) {
442363 result.insert(entry->index->GetIndexType());
442364 }
442365 return result;
442366}

◆ Move()

void duckdb::TableIndexList::Move ( TableIndexList & other)
inline

Overwrite this list with the other list.

50274 {
50275 D_ASSERT(index_entries.empty());
50276 index_entries = std::move(other.index_entries);
50277 }

◆ MergeCheckpointDeltas()

void duckdb::TableIndexList::MergeCheckpointDeltas ( transaction_t checkpoint_id)

Merge any changes added to deltas during a checkpoint back into the main indexes.

442592 {
442593 lock_guard<mutex> lock(index_entries_lock);
442594 for (auto &entry : index_entries) {
442595 // Merge any data appended to the index while the checkpoint was running.
442596 auto &index = *entry->index;
442597 if (!index.IsBound()) {
442598 continue;
442599 }
442600 lock_guard<mutex> guard(entry->lock);
442601 auto &bound_index = index.Cast<BoundIndex>();
442602 if (entry->removed_data_during_checkpoint || entry->added_data_during_checkpoint) {
442603 if (bound_index.GetIndexType() != ART::TYPE_NAME) {
442604 throw InternalException("Concurrent changes made to a non-ART index");
442605 }
442606
442607 auto &art = bound_index.Cast<ART>();
442608
442609 if (entry->removed_data_during_checkpoint) {
442610 art.RemovalMerge(*entry->removed_data_during_checkpoint);
442611 }
442612 if (entry->added_data_during_checkpoint) {
442613 // NOTE: we insert duplicates here (IndexAppendMode::INSERT_DUPLICATES)
442614 // this is necessary due to the way that data is inserted into indexes during transaction commit
442615 // essentially we always FIRST insert data into the index, THEN remove data
442616 // even if the data was logically removed first
442617 // i.e. if we have a transaction like: DELETE FROM tbl WHERE i=42; INSERT INTO tbl VALUES (42);
442618 // we will FIRST insert 42, THEN delete 42 from the index
442619 // We plan to change this in the future - see https://github.com/duckdblabs/duckdb-internal/issues/6886
442620 auto error = art.InsertMerge(*entry->added_data_during_checkpoint, IndexAppendMode::INSERT_DUPLICATES);
442621 if (error.HasError()) {
442622 throw InternalException("Failed to append while merging checkpoint deltas - this "
442623 "signifies a bug or broken index: %s",
442624 error.Message());
442625 }
442626 }
442627 entry->removed_data_during_checkpoint.reset();
442628 entry->added_data_during_checkpoint.reset();
442629 }
442630 entry->last_written_checkpoint = checkpoint_id;
442631 }
442632}
static constexpr const char * TYPE_NAME
Index type name for the ART.
Definition duckdb.cpp:12136

◆ FindForeignKeyIndex()

optional_ptr< IndexEntry > duckdb::TableIndexList::FindForeignKeyIndex ( const vector< PhysicalIndex > & fk_keys,
const ForeignKeyType fk_type )

Returns true, if all indexes Find the foreign key matching the keys.

442506 {
442507 lock_guard<mutex> lock(index_entries_lock);
442508 for (auto &entry : index_entries) {
442509 auto &index = *entry->index;
442510 if (IsForeignKeyIndex(fk_keys, index, fk_type)) {
442511 return entry;
442512 }
442513 }
442514 return nullptr;
442515}
Here is the caller graph for this function:

◆ VerifyForeignKey()

void duckdb::TableIndexList::VerifyForeignKey ( optional_ptr< LocalTableStorage > storage,
const vector< PhysicalIndex > & fk_keys,
DataChunk & chunk,
ConflictManager & conflict_manager )

Verify a foreign key constraint.

442518 {
442519 auto fk_type = conflict_manager.GetVerifyExistenceType() == VerifyExistenceType::APPEND_FK
442520 ? ForeignKeyType::FK_TYPE_PRIMARY_KEY_TABLE
442521 : ForeignKeyType::FK_TYPE_FOREIGN_KEY_TABLE;
442522
442523 // Check whether the chunk can be inserted in or deleted from the referenced table storage.
442524 auto entry = FindForeignKeyIndex(fk_keys, fk_type);
442525 auto &index = *entry->index;
442526 lock_guard<mutex> guard(entry->lock);
442527 D_ASSERT(index.IsBound());
442528 IndexAppendInfo index_append_info;
442529 if (storage) {
442530 auto delete_index = storage->delete_indexes.Find(index.GetIndexName());
442531 if (delete_index) {
442532 index_append_info.delete_indexes.push_back(*delete_index);
442533 }
442534 }
442535 if (entry->removed_data_during_checkpoint) {
442536 index_append_info.delete_indexes.push_back(*entry->removed_data_during_checkpoint);
442537 }
442538
442539 auto &main_index = index.Cast<BoundIndex>();
442540 main_index.VerifyConstraint(chunk, index_append_info, conflict_manager);
442541 if (entry->added_data_during_checkpoint) {
442542 // if we have added any rows during checkpoint - check in that index as well
442543 IndexAppendInfo added_during_checkpoint_info;
442544 entry->added_data_during_checkpoint->VerifyConstraint(chunk, added_during_checkpoint_info, conflict_manager);
442545 }
442546}
optional_ptr< IndexEntry > FindForeignKeyIndex(const vector< PhysicalIndex > &fk_keys, const ForeignKeyType fk_type)
Returns true, if all indexes Find the foreign key matching the keys.
Definition duckdb.cpp:442505
Here is the call graph for this function:

◆ GetRequiredColumns()

unordered_set< column_t > duckdb::TableIndexList::GetRequiredColumns ( )

Get the combined column ids of the indexes.

442548 {
442549 lock_guard<mutex> lock(index_entries_lock);
442550 unordered_set<column_t> column_ids;
442551 for (auto &entry : index_entries) {
442552 auto &index = *entry->index;
442553 for (auto col_id : index.GetColumnIds()) {
442554 column_ids.insert(col_id);
442555 }
442556 }
442557 return column_ids;
442558}
Here is the caller graph for this function:

◆ SerializeToDisk()

IndexSerializationResult duckdb::TableIndexList::SerializeToDisk ( QueryContext context,
const IndexSerializationInfo & info )

Serialize all indexes of the table.

442560 {
442561 lock_guard<mutex> lock(index_entries_lock);
442562
442563 IndexSerializationResult result;
442564
442565 idx_t bound_count = 0;
442566 for (auto &entry : index_entries) {
442567 if (entry->index->IsBound()) {
442568 bound_count++;
442569 }
442570 }
442571 result.bound_infos.reserve(bound_count);
442572 for (auto &entry : index_entries) {
442573 auto &index = *entry->index;
442574 if (!index.IsBound()) {
442575 // Unbound: reference existing storage info
442576 auto &unbound_index = index.Cast<UnboundIndex>();
442577 D_ASSERT(!unbound_index.GetStorageInfo().name.empty());
442578 result.ordered_infos.push_back(unbound_index.GetStorageInfo());
442579 continue;
442580 }
442581 // Bound: move new storage info into bound_infos, then reference it
442582 auto &bound_index = index.Cast<BoundIndex>();
442583 auto storage_info = bound_index.SerializeToDisk(context, info.options);
442584 D_ASSERT(storage_info.IsValid() && !storage_info.name.empty());
442585 result.bound_infos.push_back(std::move(storage_info));
442586 result.ordered_infos.push_back(result.bound_infos.back());
442587 }
442588
442589 return result;
442590}

◆ InitializeIndexChunk()

void duckdb::TableIndexList::InitializeIndexChunk ( DataChunk & index_chunk,
const vector< LogicalType > & table_types,
vector< StorageIndex > & mapped_column_ids,
DataTableInfo & data_table_info )
static

Initialize an index_chunk from a table.

442635 {
442636 // table_chunk contains all table columns.
442637 // We only reference the index columns in the index chunk.
442638 auto &index_list = data_table_info.GetIndexes();
442639 auto indexed_columns = index_list.GetRequiredColumns();
442640
442641 // Store the mapped_column_ids and index_types in sorted canonical form, needed for
442642 // buffering WAL index operations during replay (see notes in unbound_index.hpp).
442643 // First sort mapped_column_ids, then populate index_types according to the sorted order.
442644 for (auto &col : indexed_columns) {
442645 mapped_column_ids.emplace_back(col);
442646 }
442647 std::sort(mapped_column_ids.begin(), mapped_column_ids.end());
442648
442649 vector<LogicalType> index_types;
442650 for (auto &col : mapped_column_ids) {
442651 index_types.push_back(table_types[col.GetPrimaryIndex()]);
442652 }
442653
442654 index_chunk.InitializeEmpty(index_types);
442655}
DUCKDB_API void InitializeEmpty(const vector< LogicalType > &types)
Initializes a DataChunk with the given types and without any vector data allocation.
Definition duckdb.cpp:116783
Here is the call graph for this function:
Here is the caller graph for this function:

◆ ReferenceIndexChunk()

void duckdb::TableIndexList::ReferenceIndexChunk ( DataChunk & table_chunk,
DataChunk & index_chunk,
vector< StorageIndex > & mapped_column_ids )
static

Reference the indexed columns of a table chunk.

442658 {
442659 for (idx_t i = 0; i < mapped_column_ids.size(); i++) {
442660 auto col_id = mapped_column_ids[i].GetPrimaryIndex();
442661 index_chunk.data[i].Reference(table_chunk.data[col_id]);
442662 }
442663 index_chunk.SetCardinality(table_chunk);
442664}
vector< Vector > data
The vectors owned by the DataChunk.
Definition duckdb.hpp:11445
Here is the caller graph for this function:

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