| 1 | //===----------------------------------------------------------------------===// |
| 2 | // DuckDB |
| 3 | // |
| 4 | // duckdb/transaction/duck_transaction.hpp |
| 5 | // |
| 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
| 9 | #pragma once |
| 10 | |
| 11 | #include "duckdb/transaction/transaction.hpp" |
| 12 | |
| 13 | namespace duckdb { |
| 14 | |
| 15 | class DuckTransaction : public Transaction { |
| 16 | public: |
| 17 | DuckTransaction(TransactionManager &manager, ClientContext &context, transaction_t start_time, |
| 18 | transaction_t transaction_id); |
| 19 | ~DuckTransaction(); |
| 20 | |
| 21 | //! The start timestamp of this transaction |
| 22 | transaction_t start_time; |
| 23 | //! The transaction id of this transaction |
| 24 | transaction_t transaction_id; |
| 25 | //! The commit id of this transaction, if it has successfully been committed |
| 26 | transaction_t commit_id; |
| 27 | //! Map of all sequences that were used during the transaction and the value they had in this transaction |
| 28 | unordered_map<SequenceCatalogEntry *, SequenceValue> sequence_usage; |
| 29 | //! Highest active query when the transaction finished, used for cleaning up |
| 30 | transaction_t highest_active_query; |
| 31 | |
| 32 | public: |
| 33 | static DuckTransaction &Get(ClientContext &context, AttachedDatabase &db); |
| 34 | static DuckTransaction &Get(ClientContext &context, Catalog &catalog); |
| 35 | LocalStorage &GetLocalStorage(); |
| 36 | |
| 37 | void PushCatalogEntry(CatalogEntry &entry, data_ptr_t = nullptr, idx_t = 0); |
| 38 | |
| 39 | //! Commit the current transaction with the given commit identifier. Returns an error message if the transaction |
| 40 | //! commit failed, or an empty string if the commit was sucessful |
| 41 | string Commit(AttachedDatabase &db, transaction_t commit_id, bool checkpoint) noexcept; |
| 42 | //! Returns whether or not a commit of this transaction should trigger an automatic checkpoint |
| 43 | bool AutomaticCheckpoint(AttachedDatabase &db); |
| 44 | |
| 45 | //! Rollback |
| 46 | void Rollback() noexcept; |
| 47 | //! Cleanup the undo buffer |
| 48 | void Cleanup(); |
| 49 | |
| 50 | bool ChangesMade(); |
| 51 | |
| 52 | void PushDelete(DataTable &table, ChunkVectorInfo *vinfo, row_t rows[], idx_t count, idx_t base_row); |
| 53 | void PushAppend(DataTable &table, idx_t row_start, idx_t row_count); |
| 54 | UpdateInfo *CreateUpdateInfo(idx_t type_size, idx_t entries); |
| 55 | |
| 56 | bool IsDuckTransaction() const override { |
| 57 | return true; |
| 58 | } |
| 59 | |
| 60 | private: |
| 61 | //! The undo buffer is used to store old versions of rows that are updated |
| 62 | //! or deleted |
| 63 | UndoBuffer undo_buffer; |
| 64 | //! The set of uncommitted appends for the transaction |
| 65 | unique_ptr<LocalStorage> storage; |
| 66 | }; |
| 67 | |
| 68 | } // namespace duckdb |
| 69 | |