|
| 1 | +#include <memory> |
1 | 2 | #include <optional>
|
2 | 3 | #include <shared_mutex>
|
3 | 4 | #include <string>
|
|
15 | 16 | #include "catalog/schema.h"
|
16 | 17 | #include "catalog/table_generator.h"
|
17 | 18 | #include "common/bustub_instance.h"
|
18 |
| -#include "common/enums/statement_type.h" |
19 | 19 | #include "common/exception.h"
|
20 | 20 | #include "common/util/string_util.h"
|
21 | 21 | #include "concurrency/lock_manager.h"
|
|
39 | 39 | namespace bustub {
|
40 | 40 |
|
41 | 41 | auto BustubInstance::MakeExecutorContext(Transaction *txn, bool is_modify) -> std::unique_ptr<ExecutorContext> {
|
42 |
| - return std::make_unique<ExecutorContext>(txn, catalog_, buffer_pool_manager_, txn_manager_, lock_manager_, is_modify); |
| 42 | + return std::make_unique<ExecutorContext>(txn, catalog_.get(), buffer_pool_manager_.get(), txn_manager_.get(), |
| 43 | + lock_manager_.get(), is_modify); |
43 | 44 | }
|
44 | 45 |
|
45 | 46 | BustubInstance::BustubInstance(const std::string &db_file_name) {
|
46 | 47 | enable_logging = false;
|
47 | 48 |
|
48 | 49 | // Storage related.
|
49 |
| - disk_manager_ = new DiskManager(db_file_name); |
| 50 | + disk_manager_ = std::make_unique<DiskManager>(db_file_name); |
50 | 51 |
|
51 | 52 | // Log related.
|
52 |
| - log_manager_ = new LogManager(disk_manager_); |
| 53 | + log_manager_ = std::make_unique<LogManager>(disk_manager_.get()); |
53 | 54 |
|
54 | 55 | // We need more frames for GenerateTestTable to work. Therefore, we use 128 instead of the default
|
55 | 56 | // buffer pool size specified in `config.h`.
|
56 | 57 | try {
|
57 |
| - buffer_pool_manager_ = new BufferPoolManager(128, disk_manager_, LRUK_REPLACER_K, log_manager_); |
| 58 | + buffer_pool_manager_ = |
| 59 | + std::make_unique<BufferPoolManager>(128, disk_manager_.get(), LRUK_REPLACER_K, log_manager_.get()); |
58 | 60 | } catch (NotImplementedException &e) {
|
59 | 61 | std::cerr << "BufferPoolManager is not implemented, only mock tables are supported." << std::endl;
|
60 | 62 | buffer_pool_manager_ = nullptr;
|
61 | 63 | }
|
62 | 64 |
|
63 | 65 | // Transaction (txn) related.
|
| 66 | + lock_manager_ = std::make_unique<LockManager>(); |
64 | 67 |
|
65 |
| - lock_manager_ = new LockManager(); |
| 68 | + txn_manager_ = std::make_unique<TransactionManager>(lock_manager_.get(), log_manager_.get()); |
66 | 69 |
|
67 |
| - txn_manager_ = new TransactionManager(lock_manager_, log_manager_); |
68 |
| - |
69 |
| - lock_manager_->txn_manager_ = txn_manager_; |
| 70 | + lock_manager_->txn_manager_ = txn_manager_.get(); |
70 | 71 |
|
71 | 72 | #ifndef __EMSCRIPTEN__
|
72 | 73 | lock_manager_->StartDeadlockDetection();
|
73 | 74 | #endif
|
74 | 75 |
|
75 | 76 | // Checkpoint related.
|
76 |
| - checkpoint_manager_ = new CheckpointManager(txn_manager_, log_manager_, buffer_pool_manager_); |
| 77 | + checkpoint_manager_ = |
| 78 | + std::make_unique<CheckpointManager>(txn_manager_.get(), log_manager_.get(), buffer_pool_manager_.get()); |
77 | 79 |
|
78 |
| - // Catalog. |
79 |
| - catalog_ = new Catalog(buffer_pool_manager_, lock_manager_, log_manager_); |
| 80 | + // Catalog related. |
| 81 | + catalog_ = std::make_unique<Catalog>(buffer_pool_manager_.get(), lock_manager_.get(), log_manager_.get()); |
80 | 82 |
|
81 |
| - // Execution engine. |
82 |
| - execution_engine_ = new ExecutionEngine(buffer_pool_manager_, txn_manager_, catalog_); |
| 83 | + // Execution engine related. |
| 84 | + execution_engine_ = std::make_unique<ExecutionEngine>(buffer_pool_manager_.get(), txn_manager_.get(), catalog_.get()); |
83 | 85 | }
|
84 | 86 |
|
85 | 87 | BustubInstance::BustubInstance() {
|
86 | 88 | enable_logging = false;
|
87 | 89 |
|
88 | 90 | // Storage related.
|
89 |
| - disk_manager_ = new DiskManagerUnlimitedMemory(); |
| 91 | + disk_manager_ = std::make_unique<DiskManagerUnlimitedMemory>(); |
90 | 92 |
|
91 | 93 | // Log related.
|
92 |
| - log_manager_ = new LogManager(disk_manager_); |
| 94 | + log_manager_ = std::make_unique<LogManager>(disk_manager_.get()); |
93 | 95 |
|
94 | 96 | // We need more frames for GenerateTestTable to work. Therefore, we use 128 instead of the default
|
95 | 97 | // buffer pool size specified in `config.h`.
|
96 | 98 | try {
|
97 |
| - buffer_pool_manager_ = new BufferPoolManager(128, disk_manager_, LRUK_REPLACER_K, log_manager_); |
| 99 | + buffer_pool_manager_ = |
| 100 | + std::make_unique<BufferPoolManager>(128, disk_manager_.get(), LRUK_REPLACER_K, log_manager_.get()); |
98 | 101 | } catch (NotImplementedException &e) {
|
99 | 102 | std::cerr << "BufferPoolManager is not implemented, only mock tables are supported." << std::endl;
|
100 | 103 | buffer_pool_manager_ = nullptr;
|
101 | 104 | }
|
102 | 105 |
|
103 | 106 | // Transaction (txn) related.
|
| 107 | + lock_manager_ = std::make_unique<LockManager>(); |
104 | 108 |
|
105 |
| - lock_manager_ = new LockManager(); |
106 |
| - |
107 |
| - txn_manager_ = new TransactionManager(lock_manager_, log_manager_); |
| 109 | + txn_manager_ = std::make_unique<TransactionManager>(lock_manager_.get(), log_manager_.get()); |
108 | 110 |
|
109 |
| - lock_manager_->txn_manager_ = txn_manager_; |
| 111 | + lock_manager_->txn_manager_ = txn_manager_.get(); |
110 | 112 |
|
111 | 113 | #ifndef __EMSCRIPTEN__
|
112 | 114 | lock_manager_->StartDeadlockDetection();
|
113 | 115 | #endif
|
114 | 116 |
|
115 | 117 | // Checkpoint related.
|
116 |
| - checkpoint_manager_ = new CheckpointManager(txn_manager_, log_manager_, buffer_pool_manager_); |
| 118 | + checkpoint_manager_ = |
| 119 | + std::make_unique<CheckpointManager>(txn_manager_.get(), log_manager_.get(), buffer_pool_manager_.get()); |
117 | 120 |
|
118 |
| - // Catalog. |
119 |
| - catalog_ = new Catalog(buffer_pool_manager_, lock_manager_, log_manager_); |
| 121 | + // Catalog related. |
| 122 | + catalog_ = std::make_unique<Catalog>(buffer_pool_manager_.get(), lock_manager_.get(), log_manager_.get()); |
120 | 123 |
|
121 |
| - // Execution engine. |
122 |
| - execution_engine_ = new ExecutionEngine(buffer_pool_manager_, txn_manager_, catalog_); |
| 124 | + // Execution engine related. |
| 125 | + execution_engine_ = std::make_unique<ExecutionEngine>(buffer_pool_manager_.get(), txn_manager_.get(), catalog_.get()); |
123 | 126 | }
|
124 | 127 |
|
125 | 128 | void BustubInstance::CmdDisplayTables(ResultWriter &writer) {
|
@@ -354,14 +357,6 @@ BustubInstance::~BustubInstance() {
|
354 | 357 | if (enable_logging) {
|
355 | 358 | log_manager_->StopFlushThread();
|
356 | 359 | }
|
357 |
| - delete execution_engine_; |
358 |
| - delete catalog_; |
359 |
| - delete checkpoint_manager_; |
360 |
| - delete log_manager_; |
361 |
| - delete buffer_pool_manager_; |
362 |
| - delete lock_manager_; |
363 |
| - delete txn_manager_; |
364 |
| - delete disk_manager_; |
365 | 360 | }
|
366 | 361 |
|
367 | 362 | } // namespace bustub
|
0 commit comments