-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtransaction_manager.hpp
More file actions
90 lines (71 loc) · 2.36 KB
/
transaction_manager.hpp
File metadata and controls
90 lines (71 loc) · 2.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
/**
* @file transaction_manager.hpp
* @brief Transaction Manager for lifecycle management
*/
#ifndef CLOUDSQL_TRANSACTION_TRANSACTION_MANAGER_HPP
#define CLOUDSQL_TRANSACTION_TRANSACTION_MANAGER_HPP
#include <atomic>
#include <deque>
#include <memory>
#include <mutex>
#include <unordered_map>
#include <vector>
#include "catalog/catalog.hpp"
#include "storage/buffer_pool_manager.hpp"
#include "transaction/lock_manager.hpp"
#include "transaction/transaction.hpp"
namespace cloudsql::transaction {
/**
* @brief Manages the lifecycle of transactions
*/
class TransactionManager {
public:
explicit TransactionManager(LockManager& lock_manager, Catalog& catalog,
storage::BufferPoolManager& bpm,
recovery::LogManager* log_manager = nullptr);
~TransactionManager() = default;
// Disable copy/move
TransactionManager(const TransactionManager&) = delete;
TransactionManager& operator=(const TransactionManager&) = delete;
TransactionManager(TransactionManager&&) = delete;
TransactionManager& operator=(TransactionManager&&) = delete;
/**
* @brief Start a new transaction
* @param level Isolation level
* @return Pointer to the new transaction
*/
Transaction* begin(IsolationLevel level = IsolationLevel::REPEATABLE_READ);
/**
* @brief Commit a transaction
*/
void commit(Transaction* txn);
/**
* @brief Prepare a transaction (2PC Phase 1)
*/
void prepare(Transaction* txn);
/**
* @brief Abort a transaction
*/
void abort(Transaction* txn);
/**
* @brief Get transaction by ID
*/
Transaction* get_transaction(txn_id_t txn_id);
private:
LockManager& lock_manager_;
Catalog& catalog_;
storage::BufferPoolManager& bpm_;
recovery::LogManager* log_manager_;
std::atomic<txn_id_t> next_txn_id_{1};
std::mutex manager_latch_;
// All active transactions
std::unordered_map<txn_id_t, std::unique_ptr<Transaction>> active_transactions_;
// Transactions that have recently finished (for cleanup/safety)
std::deque<std::unique_ptr<Transaction>> completed_transactions_;
/**
* @brief Undo changes made by a transaction
*/
bool undo_transaction(Transaction* txn);
};
} // namespace cloudsql::transaction
#endif // CLOUDSQL_TRANSACTION_TRANSACTION_MANAGER_HPP