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::LocalStorage Class Reference

The LocalStorage class holds appends that have not been committed yet. More...

#include <duckdb.hpp>

Collaboration diagram for duckdb::LocalStorage:

Classes

struct  CommitState

Public Member Functions

 LocalStorage (ClientContext &context, DuckTransaction &transaction)
void InitializeScan (DataTable &table, CollectionScanState &state, optional_ptr< TableFilterSet > table_filters)
 Initialize a scan of the local storage.
void Scan (CollectionScanState &state, const vector< StorageIndex > &column_ids, DataChunk &result)
 Scan.
void InitializeParallelScan (DataTable &table, ParallelCollectionScanState &state)
bool NextParallelScan (ClientContext &context, DataTable &table, ParallelCollectionScanState &state, CollectionScanState &scan_state)
void InitializeAppend (LocalAppendState &state, DataTable &table)
 Begin appending to the local storage.
void InitializeStorage (LocalAppendState &state, DataTable &table)
 Initialize the storage and its indexes, but no row groups.
void LocalMerge (DataTable &table, OptimisticWriteCollection &collection)
 Merge a row group collection into the transaction-local storage.
PhysicalIndex CreateOptimisticCollection (DataTable &table, unique_ptr< OptimisticWriteCollection > collection)
 Create an optimistic row group collection for this table. Returns the index into the optimistic_collections vector for newly created collection.
OptimisticWriteCollection & GetOptimisticCollection (DataTable &table, const PhysicalIndex collection_index)
 Returns the optimistic row group collection corresponding to the index.
void ResetOptimisticCollection (DataTable &table, const PhysicalIndex collection_index)
 Resets the optimistic row group collection corresponding to the index.
OptimisticDataWriter & GetOptimisticWriter (DataTable &table)
 Returns the optimistic writer.
idx_t Delete (DataTable &table, Vector &row_ids, idx_t count)
 Delete a set of rows from the local storage.
void Update (DataTable &table, Vector &row_ids, const vector< PhysicalIndex > &column_ids, DataChunk &data)
 Update a set of rows in the local storage.
void Commit (optional_ptr< StorageCommitState > commit_state)
 Commits the local storage, writing it to the WAL and completing the commit.
void Rollback ()
 Rollback the local storage.
bool ChangesMade () noexcept
idx_t EstimatedSize ()
void DropTable (DataTable &table)
bool Find (DataTable &table)
idx_t AddedRows (DataTable &table)
vector< PartitionStatistics > GetPartitionStats (DataTable &table) const
void AddColumn (DataTable &old_dt, DataTable &new_dt, ColumnDefinition &new_column, ExpressionExecutor &default_executor)
void DropColumn (DataTable &old_dt, DataTable &new_dt, const idx_t drop_column_index)
void ChangeType (DataTable &old_dt, DataTable &new_dt, idx_t changed_idx, const LogicalType &target_type, const vector< StorageIndex > &bound_columns, Expression &cast_expr)
void MoveStorage (DataTable &old_dt, DataTable &new_dt)
void FetchChunk (DataTable &table, const Vector &row_ids, idx_t count, const vector< StorageIndex > &col_ids, DataChunk &chunk, ColumnFetchState &fetch_state)
bool CanFetch (DataTable &table, const row_t row_id)
 Returns true, if the local storage contains the row id.
TableIndexList & GetIndexes (ClientContext &context, DataTable &table)
optional_ptr< LocalTableStorage > GetStorage (DataTable &table)
void VerifyNewConstraint (DataTable &parent, const BoundConstraint &constraint)
ClientContext & GetClientContext () const

Static Public Member Functions

static LocalStorage & Get (DuckTransaction &transaction)
static LocalStorage & Get (ClientContext &context, AttachedDatabase &db)
static LocalStorage & Get (ClientContext &context, Catalog &catalog)
static void Append (LocalAppendState &state, DataChunk &table_chunk, DataTableInfo &data_table_info)
 Append a chunk to the local storage.
static void FinalizeAppend (LocalAppendState &state)
 Finish appending to the local storage.

Private Member Functions

void Flush (DataTable &table, LocalTableStorage &storage, optional_ptr< StorageCommitState > commit_state)

Private Attributes

ClientContext & context
DuckTransaction & transaction
LocalTableManager table_manager

Detailed Description

The LocalStorage class holds appends that have not been committed yet.

Constructor & Destructor Documentation

◆ LocalStorage()

duckdb::LocalStorage::LocalStorage ( ClientContext & context,
DuckTransaction & transaction )
explicit
413831 : context(context), transaction(transaction) {
413832}

Member Function Documentation

◆ Get() [1/3]

LocalStorage & duckdb::LocalStorage::Get ( DuckTransaction & transaction)
static
413840 {
413841 return transaction.GetLocalStorage();
413842}

◆ Get() [2/3]

LocalStorage & duckdb::LocalStorage::Get ( ClientContext & context,
AttachedDatabase & db )
static
413844 {
413845 return DuckTransaction::Get(context, db).GetLocalStorage();
413846}

◆ Get() [3/3]

LocalStorage & duckdb::LocalStorage::Get ( ClientContext & context,
Catalog & catalog )
static
413848 {
413849 return LocalStorage::Get(context, catalog.GetAttached());
413850}

◆ InitializeScan()

void duckdb::LocalStorage::InitializeScan ( DataTable & table,
CollectionScanState & state,
optional_ptr< TableFilterSet > table_filters )

Initialize a scan of the local storage.

413853 {
413854 auto storage = table_manager.GetStorage(table);
413855 if (storage == nullptr || storage->GetCollection().GetTotalRows() == 0) {
413856 return;
413857 }
413858 storage->InitializeScan(state, table_filters);
413859}

◆ Scan()

void duckdb::LocalStorage::Scan ( CollectionScanState & state,
const vector< StorageIndex > & column_ids,
DataChunk & result )

Scan.

413861 {
413862 state.Scan(transaction, result);
413863}

◆ InitializeParallelScan()

void duckdb::LocalStorage::InitializeParallelScan ( DataTable & table,
ParallelCollectionScanState & state )
413865 {
413866 auto storage = table_manager.GetStorage(table);
413867 if (!storage) {
413868 state.max_row = 0;
413869 state.vector_index = 0;
413870 state.current_row_group = nullptr;
413871 } else {
413872 storage->GetCollection().InitializeParallelScan(state);
413873 }
413874}

◆ NextParallelScan()

bool duckdb::LocalStorage::NextParallelScan ( ClientContext & context,
DataTable & table,
ParallelCollectionScanState & state,
CollectionScanState & scan_state )
413885 {
413886 auto storage = table_manager.GetStorage(table);
413887 if (!storage) {
413888 return false;
413889 }
413890 return storage->GetCollection().NextParallelScan(context, state, scan_state);
413891}

◆ InitializeAppend()

void duckdb::LocalStorage::InitializeAppend ( LocalAppendState & state,
DataTable & table )

Begin appending to the local storage.

413893 {
413894 state.storage = &table_manager.GetOrCreateStorage(context, table);
413895 state.storage->GetCollection().InitializeAppend(TransactionData(transaction), state.append_state);
413896}
Here is the call graph for this function:

◆ InitializeStorage()

void duckdb::LocalStorage::InitializeStorage ( LocalAppendState & state,
DataTable & table )

Initialize the storage and its indexes, but no row groups.

413898 {
413899 state.storage = &table_manager.GetOrCreateStorage(context, table);
413900}

◆ Append()

void duckdb::LocalStorage::Append ( LocalAppendState & state,
DataChunk & table_chunk,
DataTableInfo & data_table_info )
static

Append a chunk to the local storage.

413944 {
413945 // Append to any unique indexes.
413946 auto storage = state.storage;
413947 auto offset = NumericCast<idx_t>(MAX_ROW_ID) + storage->GetCollection().GetTotalRows();
413948 idx_t base_id = offset + state.append_state.total_append_count;
413949
413950 if (!storage->append_indexes.Empty()) {
413951 DataChunk index_chunk;
413952 vector<StorageIndex> mapped_column_ids;
413953
413954 // Only initialize the index_chunk, if there are unbound indexes.
413955 if (storage->append_indexes.HasUnbound() || storage->delete_indexes.HasUnbound()) {
413956 TableIndexList::InitializeIndexChunk(index_chunk, table_chunk.GetTypes(), mapped_column_ids,
413957 data_table_info);
413958 TableIndexList::ReferenceIndexChunk(table_chunk, index_chunk, mapped_column_ids);
413959 }
413960
413961 auto error = DataTable::AppendToIndexes(storage->append_indexes, storage->delete_indexes, table_chunk,
413962 index_chunk, mapped_column_ids, NumericCast<row_t>(base_id),
413963 storage->index_append_mode, optional_idx());
413964 if (error.HasError()) {
413965 error.Throw();
413966 }
413967 }
413968
413969 // Append the chunk to the local storage.
413970 auto new_row_group = storage->GetCollection().Append(table_chunk, state.append_state);
413971
413972 // Check if we should pre-emptively flush blocks to disk.
413973 if (new_row_group) {
413974 storage->WriteNewRowGroup();
413975 }
413976}
DUCKDB_API vector< LogicalType > GetTypes() const
Returns a list of types of the vectors of this data chunk.
Definition duckdb.cpp:116995
static ErrorData AppendToIndexes(TableIndexList &indexes, optional_ptr< TableIndexList > delete_indexes, DataChunk &table_chunk, DataChunk &index_chunk, const vector< StorageIndex > &mapped_column_ids, row_t row_start, const IndexAppendMode index_append_mode, optional_idx active_checkpoint)
Appends a chunk with the row ids [row_start, ..., row_start + chunk.size()] to all indexes of the tab...
Definition duckdb.cpp:412695
static void ReferenceIndexChunk(DataChunk &table_chunk, DataChunk &index_chunk, vector< StorageIndex > &mapped_column_ids)
Reference the indexed columns of a table chunk.
Definition duckdb.cpp:442657
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.
Definition duckdb.cpp:442634
const row_t MAX_ROW_ID
The maximum row identifier used in tables.
Definition duckdb.cpp:50301
uint64_t idx_t
a saner size_t for loop indices etc
Definition duckdb.hpp:237
Here is the call graph for this function:
Here is the caller graph for this function:

◆ FinalizeAppend()

void duckdb::LocalStorage::FinalizeAppend ( LocalAppendState & state)
static

Finish appending to the local storage.

413978 {
413979 state.storage->GetCollection().FinalizeAppend(state.append_state.transaction, state.append_state);
413980}
Here is the call graph for this function:
Here is the caller graph for this function:

◆ LocalMerge()

void duckdb::LocalStorage::LocalMerge ( DataTable & table,
OptimisticWriteCollection & collection )

Merge a row group collection into the transaction-local storage.

413982 {
413983 auto &storage = table_manager.GetOrCreateStorage(context, table);
413984 if (!storage.append_indexes.Empty()) {
413985 // append data to indexes if required
413986 row_t base_id = MAX_ROW_ID + NumericCast<row_t>(storage.GetCollection().GetTotalRows());
413987 auto error = storage.AppendToIndexes(transaction, *collection.collection, storage.append_indexes,
413988 table.GetTypes(), base_id);
413989 if (error.HasError()) {
413990 error.Throw();
413991 }
413992 }
413993 auto &target = storage.GetPrimaryCollection();
413994 target.MergeStorage(collection);
413995}
int64_t row_t
The type used for row identifiers.
Definition duckdb.hpp:240
Here is the call graph for this function:

◆ CreateOptimisticCollection()

PhysicalIndex duckdb::LocalStorage::CreateOptimisticCollection ( DataTable & table,
unique_ptr< OptimisticWriteCollection > collection )

Create an optimistic row group collection for this table. Returns the index into the optimistic_collections vector for newly created collection.

413998 {
413999 auto &storage = table_manager.GetOrCreateStorage(context, table);
414000 return storage.CreateOptimisticCollection(std::move(collection));
414001}

◆ GetOptimisticCollection()

OptimisticWriteCollection & duckdb::LocalStorage::GetOptimisticCollection ( DataTable & table,
const PhysicalIndex collection_index )

Returns the optimistic row group collection corresponding to the index.

414004 {
414005 auto &storage = table_manager.GetOrCreateStorage(context, table);
414006 return storage.GetOptimisticCollection(collection_index);
414007}

◆ ResetOptimisticCollection()

void duckdb::LocalStorage::ResetOptimisticCollection ( DataTable & table,
const PhysicalIndex collection_index )

Resets the optimistic row group collection corresponding to the index.

414009 {
414010 auto &storage = table_manager.GetOrCreateStorage(context, table);
414011 storage.ResetOptimisticCollection(collection_index);
414012}

◆ GetOptimisticWriter()

OptimisticDataWriter & duckdb::LocalStorage::GetOptimisticWriter ( DataTable & table)

Returns the optimistic writer.

414014 {
414015 auto &storage = table_manager.GetOrCreateStorage(context, table);
414016 return storage.GetOptimisticWriter();
414017}

◆ Delete()

idx_t duckdb::LocalStorage::Delete ( DataTable & table,
Vector & row_ids,
idx_t count )

Delete a set of rows from the local storage.

414031 {
414032 auto storage = table_manager.GetStorage(table);
414033 D_ASSERT(storage);
414034
414035 // delete from unique indices (if any)
414036 if (!storage->append_indexes.Empty()) {
414037 storage->GetCollection().RemoveFromIndexes(context, storage->append_indexes, row_ids, count,
414039 }
414040
414041 auto ids = FlatVector::GetData<row_t>(row_ids);
414042 idx_t delete_count = storage->GetCollection().Delete(TransactionData(0, 0), table, ids, count);
414043 storage->deleted_rows += delete_count;
414044 return delete_count;
414045}
@ MAIN_INDEX_ONLY
Remove from main index only.
Definition duckdb.hpp:50448

◆ Update()

void duckdb::LocalStorage::Update ( DataTable & table,
Vector & row_ids,
const vector< PhysicalIndex > & column_ids,
DataChunk & data )

Update a set of rows in the local storage.

414048 {
414049 D_ASSERT(updates.size() >= 1);
414050 auto storage = table_manager.GetStorage(table);
414051 D_ASSERT(storage);
414052
414053 auto ids = FlatVector::GetData<row_t>(row_ids);
414054 storage->GetCollection().Update(TransactionData(0, 0), table, ids, column_ids, updates);
414055}

◆ Commit()

void duckdb::LocalStorage::Commit ( optional_ptr< StorageCommitState > commit_state)

Commits the local storage, writing it to the WAL and completing the commit.

414100 {
414101 // commit local storage
414102 // iterate over all entries in the table storage map and commit them
414103 // after this, the local storage is no longer required and can be cleared
414104 auto table_storage = table_manager.MoveEntries();
414105 for (auto &entry : table_storage) {
414106 auto table = entry.first;
414107 auto storage = entry.second.get();
414108 Flush(table, *storage, commit_state);
414109 entry.second.reset();
414110 }
414111}

◆ Rollback()

void duckdb::LocalStorage::Rollback ( )

Rollback the local storage.

414113 {
414114 // rollback local storage
414115 // after this, the local storage is no longer required and can be cleared
414116 auto table_storage = table_manager.MoveEntries();
414117 for (auto &entry : table_storage) {
414118 auto storage = entry.second.get();
414119 if (!storage) {
414120 continue;
414121 }
414122 storage->Rollback();
414123 entry.second.reset();
414124 }
414125}

◆ ChangesMade()

bool duckdb::LocalStorage::ChangesMade ( )
noexcept
414019 {
414020 return !table_manager.IsEmpty();
414021}

◆ EstimatedSize()

idx_t duckdb::LocalStorage::EstimatedSize ( )
414027 {
414028 return table_manager.EstimatedSize();
414029}

◆ DropTable()

void duckdb::LocalStorage::DropTable ( DataTable & table)
414143 {
414144 auto storage = table_manager.GetStorage(table);
414145 if (!storage) {
414146 return;
414147 }
414148 storage->is_dropped = true;
414149}

◆ Find()

bool duckdb::LocalStorage::Find ( DataTable & table)
414023 {
414024 return table_manager.GetStorage(table) != nullptr;
414025}

◆ AddedRows()

idx_t duckdb::LocalStorage::AddedRows ( DataTable & table)
414127 {
414128 auto storage = table_manager.GetStorage(table);
414129 if (!storage) {
414130 return 0;
414131 }
414132 return storage->GetCollection().GetTotalRows() - storage->deleted_rows;
414133}

◆ GetPartitionStats()

vector< PartitionStatistics > duckdb::LocalStorage::GetPartitionStats ( DataTable & table) const
414135 {
414136 auto storage = table_manager.GetStorage(table);
414137 if (!storage) {
414138 return vector<PartitionStatistics>();
414139 }
414140 return storage->GetCollection().GetPartitionStats();
414141}

◆ AddColumn()

void duckdb::LocalStorage::AddColumn ( DataTable & old_dt,
DataTable & new_dt,
ColumnDefinition & new_column,
ExpressionExecutor & default_executor )
414163 {
414164 // check if there are any pending appends for the old version of the table
414165 auto storage = table_manager.MoveEntry(old_dt);
414166 if (!storage) {
414167 return;
414168 }
414169 auto new_storage = make_shared_ptr<LocalTableStorage>(context, new_dt, *storage, new_column, default_executor);
414170 table_manager.InsertEntry(new_dt, std::move(new_storage));
414171}

◆ DropColumn()

void duckdb::LocalStorage::DropColumn ( DataTable & old_dt,
DataTable & new_dt,
const idx_t drop_column_index )
414173 {
414174 // check if there are any pending appends for the old version of the table
414175 auto storage = table_manager.MoveEntry(old_dt);
414176 if (!storage) {
414177 return;
414178 }
414179 auto new_storage = make_shared_ptr<LocalTableStorage>(new_dt, *storage, drop_column_index);
414180 table_manager.InsertEntry(new_dt, std::move(new_storage));
414181}

◆ ChangeType()

void duckdb::LocalStorage::ChangeType ( DataTable & old_dt,
DataTable & new_dt,
idx_t changed_idx,
const LogicalType & target_type,
const vector< StorageIndex > & bound_columns,
Expression & cast_expr )
414184 {
414185 // check if there are any pending appends for the old version of the table
414186 auto storage = table_manager.MoveEntry(old_dt);
414187 if (!storage) {
414188 return;
414189 }
414190 auto new_storage = make_shared_ptr<LocalTableStorage>(context, new_dt, *storage, changed_idx, target_type,
414191 bound_columns, cast_expr);
414192 table_manager.InsertEntry(new_dt, std::move(new_storage));
414193}

◆ MoveStorage()

void duckdb::LocalStorage::MoveStorage ( DataTable & old_dt,
DataTable & new_dt )
414151 {
414152 // check if there are any pending appends for the old version of the table
414153 auto new_storage = table_manager.MoveEntry(old_dt);
414154 if (!new_storage) {
414155 return;
414156 }
414157 // take over the storage from the old entry
414158 new_storage->table_ref = new_dt;
414159 table_manager.InsertEntry(new_dt, std::move(new_storage));
414160}

◆ FetchChunk()

void duckdb::LocalStorage::FetchChunk ( DataTable & table,
const Vector & row_ids,
idx_t count,
const vector< StorageIndex > & col_ids,
DataChunk & chunk,
ColumnFetchState & fetch_state )
414196 {
414197 auto storage = table_manager.GetStorage(table);
414198 if (!storage) {
414199 throw InternalException("LocalStorage::FetchChunk - local storage not found");
414200 }
414201 storage->GetCollection().Fetch(transaction, chunk, col_ids, row_ids, count, fetch_state);
414202}

◆ CanFetch()

bool duckdb::LocalStorage::CanFetch ( DataTable & table,
const row_t row_id )

Returns true, if the local storage contains the row id.

414204 {
414205 auto storage = table_manager.GetStorage(table);
414206 if (!storage) {
414207 throw InternalException("LocalStorage::CanFetch - local storage not found");
414208 }
414209 return storage->GetCollection().CanFetch(transaction, row_id);
414210}
Here is the call graph for this function:
Here is the caller graph for this function:

◆ GetIndexes()

TableIndexList & duckdb::LocalStorage::GetIndexes ( ClientContext & context,
DataTable & table )
414212 {
414213 auto &storage = table_manager.GetOrCreateStorage(context, table);
414214 return storage.append_indexes;
414215}

◆ GetStorage()

optional_ptr< LocalTableStorage > duckdb::LocalStorage::GetStorage ( DataTable & table)
414217 {
414218 return table_manager.GetStorage(table);
414219}

◆ VerifyNewConstraint()

void duckdb::LocalStorage::VerifyNewConstraint ( DataTable & parent,
const BoundConstraint & constraint )
414221 {
414222 auto storage = table_manager.GetStorage(parent);
414223 if (!storage) {
414224 return;
414225 }
414226 storage->GetCollection().VerifyNewConstraint(context, parent, constraint);
414227}

◆ GetClientContext()

ClientContext & duckdb::LocalStorage::GetClientContext ( ) const
inline
50945 {
50946 return context;
50947 }

◆ Flush()

void duckdb::LocalStorage::Flush ( DataTable & table,
LocalTableStorage & storage,
optional_ptr< StorageCommitState > commit_state )
private
414057 {
414058 if (storage.is_dropped) {
414059 return;
414060 }
414061 if (storage.GetCollection().GetTotalRows() <= storage.deleted_rows) {
414062 // all rows that we added were deleted
414063 // rollback any partial blocks that are still outstanding
414064 storage.Rollback();
414065 return;
414066 }
414067
414068 auto append_count = storage.GetCollection().GetTotalRows() - storage.deleted_rows;
414069 const auto row_group_size = storage.GetCollection().GetRowGroupSize();
414070
414071 TableAppendState append_state;
414072 table.AppendLock(transaction, append_state);
414073 if ((append_state.row_start == 0 || storage.GetCollection().GetTotalRows() >= row_group_size) &&
414074 storage.deleted_rows == 0) {
414075 // table is currently empty OR we are bulk appending: move over the storage directly
414076 // first flush any outstanding blocks
414077 storage.FlushBlocks();
414078 // Append to the indexes.
414079 storage.AppendToIndexes(transaction, append_state);
414080 // finally move over the row groups
414081 table.MergeStorage(storage.GetCollection(), commit_state);
414082 } else {
414083 // check if we have written data
414084 // if we have, we cannot merge to disk after all
414085 // so we need to revert the data we have already written
414086 storage.Rollback();
414087 // append to the indexes
414088 storage.AppendToIndexes(transaction, append_state);
414089 // after that is successful - append to the table
414090 storage.AppendToTable(transaction, append_state);
414091 }
414092 transaction.PushAppend(table, NumericCast<idx_t>(append_state.row_start), append_count);
414093
414094#ifdef DEBUG
414095 // Verify that our index memory is stable.
414096 table.VerifyIndexBuffers();
414097#endif
414098}

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