|
7 | 7 | #define CLOUDSQL_TRANSACTION_TRANSACTION_MANAGER_HPP |
8 | 8 |
|
9 | 9 | #include <atomic> |
| 10 | +#include <deque> |
10 | 11 | #include <memory> |
| 12 | +#include <mutex> |
11 | 13 | #include <unordered_map> |
| 14 | +#include <vector> |
12 | 15 |
|
13 | 16 | #include "catalog/catalog.hpp" |
14 | | -#include "recovery/log_manager.hpp" |
15 | 17 | #include "storage/buffer_pool_manager.hpp" |
16 | 18 | #include "transaction/lock_manager.hpp" |
17 | 19 | #include "transaction/transaction.hpp" |
18 | 20 |
|
19 | 21 | namespace cloudsql::transaction { |
20 | 22 |
|
| 23 | +/** |
| 24 | + * @brief Manages the lifecycle of transactions |
| 25 | + */ |
21 | 26 | class TransactionManager { |
22 | | - private: |
23 | | - std::atomic<txn_id_t> next_txn_id_{1}; |
24 | | - std::unordered_map<txn_id_t, std::unique_ptr<Transaction>> active_transactions_; |
25 | | - std::unordered_map<txn_id_t, std::unique_ptr<Transaction>> completed_transactions_; |
26 | | - LockManager& lock_manager_; |
27 | | - Catalog& catalog_; |
28 | | - storage::BufferPoolManager& bpm_; |
29 | | - recovery::LogManager* log_manager_; |
30 | | - std::mutex manager_latch_; |
31 | | - |
32 | | - void undo_transaction(Transaction* txn); |
33 | | - |
34 | 27 | public: |
35 | 28 | explicit TransactionManager(LockManager& lock_manager, Catalog& catalog, |
36 | 29 | storage::BufferPoolManager& bpm, |
37 | | - recovery::LogManager* log_manager = nullptr) |
38 | | - : lock_manager_(lock_manager), catalog_(catalog), bpm_(bpm), log_manager_(log_manager) {} |
| 30 | + recovery::LogManager* log_manager = nullptr); |
| 31 | + |
| 32 | + ~TransactionManager() = default; |
39 | 33 |
|
| 34 | + // Disable copy/move |
| 35 | + TransactionManager(const TransactionManager&) = delete; |
| 36 | + TransactionManager& operator=(const TransactionManager&) = delete; |
| 37 | + TransactionManager(TransactionManager&&) = delete; |
| 38 | + TransactionManager& operator=(TransactionManager&&) = delete; |
| 39 | + |
| 40 | + /** |
| 41 | + * @brief Start a new transaction |
| 42 | + * @param level Isolation level |
| 43 | + * @return Pointer to the new transaction |
| 44 | + */ |
40 | 45 | Transaction* begin(IsolationLevel level = IsolationLevel::REPEATABLE_READ); |
| 46 | + |
| 47 | + /** |
| 48 | + * @brief Commit a transaction |
| 49 | + */ |
41 | 50 | void commit(Transaction* txn); |
| 51 | + |
| 52 | + /** |
| 53 | + * @brief Abort a transaction |
| 54 | + */ |
42 | 55 | void abort(Transaction* txn); |
43 | 56 |
|
| 57 | + /** |
| 58 | + * @brief Get transaction by ID |
| 59 | + */ |
44 | 60 | Transaction* get_transaction(txn_id_t txn_id); |
| 61 | + |
| 62 | + private: |
| 63 | + LockManager& lock_manager_; |
| 64 | + Catalog& catalog_; |
| 65 | + storage::BufferPoolManager& bpm_; |
| 66 | + recovery::LogManager* log_manager_; |
| 67 | + |
| 68 | + std::atomic<txn_id_t> next_txn_id_{1}; |
| 69 | + std::mutex manager_latch_; |
| 70 | + |
| 71 | + // All active transactions |
| 72 | + std::unordered_map<txn_id_t, std::unique_ptr<Transaction>> active_transactions_; |
| 73 | + |
| 74 | + // Transactions that have recently finished (for cleanup/safety) |
| 75 | + std::deque<std::unique_ptr<Transaction>> completed_transactions_; |
| 76 | + |
| 77 | + /** |
| 78 | + * @brief Undo changes made by a transaction |
| 79 | + */ |
| 80 | + void undo_transaction(Transaction* txn); |
45 | 81 | }; |
46 | 82 |
|
47 | 83 | } // namespace cloudsql::transaction |
|
0 commit comments