Skip to content

Commit 2677383

Browse files
committed
refact:添加析构函数来释放ix_scan
1 parent 289363d commit 2677383

4 files changed

Lines changed: 26 additions & 10 deletions

File tree

src/execution/executor_mvcc_index_scan.h

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,15 @@ class MvccIndexScanExecutor : public AbstractExecutor {
112112
fed_conds_ = conds_;
113113
}
114114

115+
/**
116+
* @brief 析构函数,确保正确释放扫描器资源
117+
*/
118+
~MvccIndexScanExecutor() {
119+
if (scan_) {
120+
scan_->unlatch(); // 无论扫描是否结束,都释放持有的锁
121+
}
122+
}
123+
115124
/**
116125
* @brief 初始化索引扫描并定位第一条记录
117126
* @throw InternalError 当索引访问失败时
@@ -202,15 +211,15 @@ class MvccIndexScanExecutor : public AbstractExecutor {
202211
// 移动到第一个满足条件的记录
203212
while (!is_end()) {
204213
rid_ = scan_->rid();
214+
// if(tab_.name == "warehouse") {
215+
// INFO("IndexScanExecutor: rid_ = {},{}", rid_.page_no, rid_.slot_no);
216+
// }
205217
std::unique_ptr<RmRecord> rec = mvcc_get_record(rid_, context_, fh_, txn_mgr_, cols_);
206218
if (rec != nullptr && eval_conds(cols_, fed_conds_, rec.get())) {
207219
return;
208220
}
209221
scan_->next();
210222
}
211-
if (scan_->is_end()) {
212-
scan_->unlatch();
213-
}
214223
}
215224

216225
/**
@@ -233,9 +242,6 @@ class MvccIndexScanExecutor : public AbstractExecutor {
233242
}
234243
scan_->next();
235244
}
236-
if (scan_->is_end()) {
237-
scan_->unlatch();
238-
}
239245
}
240246

241247
/**

src/index/ix_scan.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,7 @@ void IxScan::next_key_position() {
5454
iid_.page_no = node->get_next_leaf();
5555
Page* next_page = bpm_->fetch_page({ih_->fd_, iid_.page_no});
5656
next_page->rlatch();
57-
now->runlatch();
58-
bpm_->unpin_page(node->get_page_id(), false);
57+
unlatch(); // 释放当前页面
5958
now = next_page; // 更新当前页面为下一个叶节点
6059
}
6160
delete node; // 释放内存

src/index/ix_scan.h

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ class IxScan : public RecScan {
5454
* @param bpm 缓冲池管理器
5555
*/
5656
IxScan(const IxIndexHandle *ih, const Iid &lower, const Iid &upper, BufferPoolManager *bpm)
57-
: ih_(ih), iid_(lower), end_(upper), bpm_(bpm), rid_index_(0), has_overflow_cache_(false) {
57+
: ih_(ih), iid_(lower), end_(upper), bpm_(bpm), now(nullptr), rid_index_(0), has_overflow_cache_(false) {
5858
if (!is_end()) {
5959
now = bpm_->fetch_page({ih_->fd_, iid_.page_no});
6060
now->rlatch(); // 获取读锁,确保扫描期间页面不被修改
@@ -63,6 +63,13 @@ class IxScan : public RecScan {
6363
}
6464
}
6565

66+
/**
67+
* @brief 析构函数,确保正确释放页面资源
68+
*/
69+
~IxScan() {
70+
unlatch();
71+
}
72+
6673
/**
6774
* @brief 移动到下一条记录
6875
* @note
@@ -98,6 +105,10 @@ class IxScan : public RecScan {
98105
bpm_->unpin_page(now->get_page_id(), false); // 解除页面固定状态
99106
now = nullptr; // 清空当前页面指针
100107
}
108+
// 重置状态,防止后续误用
109+
has_overflow_cache_ = false;
110+
current_rids_.clear();
111+
rid_index_ = 0;
101112
}
102113

103114
private:

src/system/sm_manager.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -613,7 +613,7 @@ void SmManager::load_csv_data(const std::string& table_name, const std::string&
613613
// INFO("Loading CSV data into table: {}, from file: {}", table_name, file_path);
614614
// std::cerr << "system path: " << get_current_dir_name() << std::endl;
615615
// 1. 检查表是否存在
616-
std::cerr << "system path: " << get_current_dir_name() << std::endl;
616+
// std::cerr << "system path: " << get_current_dir_name() << std::endl;
617617
if (!db_.is_table(table_name)) {
618618
throw TableNotFoundError(table_name);
619619
}

0 commit comments

Comments
 (0)