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

Classes

struct  IteratorState

Public Member Functions

 UndoBuffer (DuckTransaction &transaction, ClientContext &context)
UndoBufferReference CreateEntry (UndoFlags type, idx_t len)
 Write a specified entry to the undo buffer.
bool ChangesMade ()
UndoBufferProperties GetProperties ()
void Cleanup (transaction_t lowest_active_transaction)
 Cleanup the undo buffer.
void WriteToWAL (WriteAheadLog &wal, optional_ptr< StorageCommitState > commit_state)
 Commit the changes made in the UndoBuffer: should be called on commit.
void Commit (UndoBuffer::IteratorState &iterator_state, CommitInfo &info)
 Commit the changes made in the UndoBuffer: should be called on commit.
void RevertCommit (UndoBuffer::IteratorState &iterator_state, transaction_t transaction_id)
 Revert committed changes made in the UndoBuffer up until the currently committed state.
void Rollback ()

Private Member Functions

template<class T>
void IterateEntries (UndoBuffer::IteratorState &state, T &&callback)
template<class T>
void IterateEntries (UndoBuffer::IteratorState &state, UndoBuffer::IteratorState &end_state, T &&callback)
template<class T>
void ReverseIterateEntries (T &&callback)

Private Attributes

DuckTransaction & transaction
UndoBufferAllocator allocator
ActiveTransactionState active_transaction_state = ActiveTransactionState::UNSET

Detailed Description

The undo buffer of a transaction is used to hold previous versions of tuples that might be required in the future (because of rollbacks or previous transactions accessing them)

Constructor & Destructor Documentation

◆ UndoBuffer()

duckdb::UndoBuffer::UndoBuffer ( DuckTransaction & transaction,
ClientContext & context )
explicit
447657 : transaction(transaction_p), allocator(DatabaseInstance::GetDatabase(context_p).GetBufferManager()) {
447658}

Member Function Documentation

◆ CreateEntry()

UndoBufferReference duckdb::UndoBuffer::CreateEntry ( UndoFlags type,
idx_t len )

Write a specified entry to the undo buffer.

447660 {
447661 idx_t alloc_len = AlignValue<idx_t>(len + UNDO_ENTRY_HEADER_SIZE);
447662 auto handle = allocator.Allocate(alloc_len);
447663 auto data = handle.Ptr();
447664 // write the undo entry metadata
447665 Store<UndoFlags>(type, data);
447666 data += sizeof(UndoFlags);
447667 Store<uint32_t>(UnsafeNumericCast<uint32_t>(alloc_len - UNDO_ENTRY_HEADER_SIZE), data);
447668 // increment the position of the header past the undo entry metadata
447669 handle.position += UNDO_ENTRY_HEADER_SIZE;
447670 return handle;
447671}
uint64_t idx_t
a saner size_t for loop indices etc
Definition duckdb.hpp:237
static T AlignValue(T n)
Align value (ceiling) (not for pointer types).
Definition duckdb.hpp:2126
Here is the call graph for this function:

◆ ChangesMade()

bool duckdb::UndoBuffer::ChangesMade ( )
447748 {
447749 // we need to search for any index creation entries
447750 return allocator.head.get();
447751}

◆ GetProperties()

UndoBufferProperties duckdb::UndoBuffer::GetProperties ( )
447753 {
447754 UndoBufferProperties properties;
447755 if (!ChangesMade()) {
447756 return properties;
447757 }
447758 auto node = allocator.head.get();
447759 while (node) {
447760 properties.estimated_size += node->position;
447761 node = node->next.get();
447762 }
447763
447764 // we need to search for any index creation entries
447765 IteratorState iterator_state;
447766 IterateEntries(iterator_state, [&](UndoFlags entry_type, data_ptr_t data) {
447767 switch (entry_type) {
447768 case UndoFlags::UPDATE_TUPLE:
447769 properties.has_updates = true;
447770 break;
447771 case UndoFlags::DELETE_TUPLE: {
447772 auto info = reinterpret_cast<DeleteInfo *>(data);
447773 if (info->is_consecutive) {
447774 properties.estimated_size += sizeof(row_t) * info->count;
447775 }
447776 if (info->table->HasIndexes()) {
447777 properties.has_index_deletes = true;
447778 }
447779 properties.has_deletes = true;
447780 break;
447781 }
447782 case UndoFlags::CATALOG_ENTRY: {
447783 properties.has_catalog_changes = true;
447784
447785 auto catalog_entry = Load<CatalogEntry *>(data);
447786 auto &parent = catalog_entry->Parent();
447787 switch (parent.type) {
447788 case CatalogType::DELETED_ENTRY:
447789 properties.has_dropped_entries = true;
447790 break;
447791 case CatalogType::INDEX_ENTRY: {
447792 auto &index = parent.Cast<DuckIndexEntry>();
447793 properties.estimated_size += index.initial_index_size;
447794 break;
447795 }
447796 default:
447797 break;
447798 }
447799 break;
447800 }
447801 default:
447802 break;
447803 }
447804 });
447805 return properties;
447806}
index
int64_t row_t
The type used for row identifiers.
Definition duckdb.hpp:240
Definition duckdb.cpp:9998

◆ Cleanup()

void duckdb::UndoBuffer::Cleanup ( transaction_t lowest_active_transaction)

Cleanup the undo buffer.

447808 {
447809 // garbage collect everything in the Undo Chunk
447810 // this should only happen if
447811 // (1) the transaction this UndoBuffer belongs to has successfully
447812 // committed
447813 // (on Rollback the Rollback() function should be called, that clears
447814 // the chunks)
447815 // (2) there is no active transaction with start_id < commit_id of this
447816 // transaction
447817 CleanupState state(transaction, lowest_active_transaction, active_transaction_state);
447818 UndoBuffer::IteratorState iterator_state;
447819 IterateEntries(iterator_state, [&](UndoFlags type, data_ptr_t data) { state.CleanupEntry(type, data); });
447820}

◆ WriteToWAL()

void duckdb::UndoBuffer::WriteToWAL ( WriteAheadLog & wal,
optional_ptr< StorageCommitState > commit_state )

Commit the changes made in the UndoBuffer: should be called on commit.

447822 {
447823 WALWriteState state(transaction, wal, commit_state);
447824 UndoBuffer::IteratorState iterator_state;
447825 IterateEntries(iterator_state, [&](UndoFlags type, data_ptr_t data) { state.CommitEntry(type, data); });
447826}

◆ Commit()

void duckdb::UndoBuffer::Commit ( UndoBuffer::IteratorState & iterator_state,
CommitInfo & info )

Commit the changes made in the UndoBuffer: should be called on commit.

447828 {
447829 active_transaction_state = info.active_transactions;
447830
447831 CommitState state(transaction, info.commit_id, active_transaction_state, CommitMode::COMMIT);
447832 IterateEntries(iterator_state, [&](UndoFlags type, data_ptr_t data) { state.CommitEntry(type, data); });
447833
447834 state.Verify();
447835}

◆ RevertCommit()

void duckdb::UndoBuffer::RevertCommit ( UndoBuffer::IteratorState & iterator_state,
transaction_t transaction_id )

Revert committed changes made in the UndoBuffer up until the currently committed state.

447837 {
447838 CommitState state(transaction, transaction_id, active_transaction_state, CommitMode::REVERT_COMMIT);
447839 UndoBuffer::IteratorState start_state;
447840 IterateEntries(start_state, end_state, [&](UndoFlags type, data_ptr_t data) { state.RevertCommit(type, data); });
447841
447842 state.Verify();
447843}

◆ Rollback()

void duckdb::UndoBuffer::Rollback ( )

Rollback the changes made in this UndoBuffer: should be called on rollback

447845 {
447846 // rollback needs to be performed in reverse
447847 RollbackState state(transaction);
447848 ReverseIterateEntries([&](UndoFlags type, data_ptr_t data) { state.RollbackEntry(type, data); });
447849}

◆ IterateEntries() [1/2]

template<class T>
void duckdb::UndoBuffer::IterateEntries ( UndoBuffer::IteratorState & state,
T && callback )
private
447674 {
447675 // iterate in insertion order: start with the tail
447676 state.current = allocator.tail.get();
447677 state.started = true;
447678 while (state.current) {
447679 state.handle = allocator.buffer_manager.Pin(state.current->block);
447680 state.start = state.handle.Ptr();
447681 state.end = state.start + state.current->position;
447682 while (state.start < state.end) {
447683 UndoFlags type = Load<UndoFlags>(state.start);
447684 state.start += sizeof(UndoFlags);
447685
447686 uint32_t len = Load<uint32_t>(state.start);
447687 state.start += sizeof(uint32_t);
447688 callback(type, state.start);
447689 state.start += len;
447690 }
447691 state.current = state.current->prev;
447692 }
447693}
::uint32_t uint32_t

◆ IterateEntries() [2/2]

template<class T>
void duckdb::UndoBuffer::IterateEntries ( UndoBuffer::IteratorState & state,
UndoBuffer::IteratorState & end_state,
T && callback )
private
447696 {
447697 if (!end_state.started) {
447698 return;
447699 }
447700 // iterate in insertion order: start with the tail
447701 state.current = allocator.tail.get();
447702 while (state.current) {
447703 state.handle = allocator.buffer_manager.Pin(state.current->block);
447704 state.start = state.handle.Ptr();
447705 state.end = state.current == end_state.current ? end_state.start : state.start + state.current->position;
447706 while (state.start < state.end) {
447707 auto type = Load<UndoFlags>(state.start);
447708 state.start += sizeof(UndoFlags);
447709 auto len = Load<uint32_t>(state.start);
447710 state.start += sizeof(uint32_t);
447711 callback(type, state.start);
447712 state.start += len;
447713 }
447714 if (state.current == end_state.current) {
447715 // finished executing until the current end state
447716 return;
447717 }
447718 state.current = state.current->prev;
447719 }
447720}

◆ ReverseIterateEntries()

template<class T>
void duckdb::UndoBuffer::ReverseIterateEntries ( T && callback)
private
447723 {
447724 // iterate in reverse insertion order: start with the head
447725 auto current = allocator.head.get();
447726 while (current) {
447727 auto handle = allocator.buffer_manager.Pin(current->block);
447728 data_ptr_t start = handle.Ptr();
447729 data_ptr_t end = start + current->position;
447730 // create a vector with all nodes in this chunk
447731 vector<pair<UndoFlags, data_ptr_t>> nodes;
447732 while (start < end) {
447733 auto type = Load<UndoFlags>(start);
447734 start += sizeof(UndoFlags);
447735 auto len = Load<uint32_t>(start);
447736 start += sizeof(uint32_t);
447737 nodes.emplace_back(type, start);
447738 start += len;
447739 }
447740 // iterate over it in reverse order
447741 for (idx_t i = nodes.size(); i > 0; i--) {
447742 callback(nodes[i - 1].first, nodes[i - 1].second);
447743 }
447744 current = current->next.get();
447745 }
447746}

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