Skip to content

Commit 6370666

Browse files
FuyukiVilaCopilot
andcommitted
feat: 在创建索引时跳过已删除记录并优化索引文件处理
Co-authored-by: Copilot <copilot@github.com>
1 parent 7665e1f commit 6370666

1 file changed

Lines changed: 6 additions & 2 deletions

File tree

src/system/sm_manager.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -436,7 +436,10 @@ void SmManager::create_index(std::string tab_name, std::vector<std::string> col_
436436
// 6. 扫描表中所有记录,构建B+树索引
437437
for (RmScan rmScan(fh_); !rmScan.is_end(); rmScan.next()) {
438438
// 获取记录数据
439-
auto record = fh_->get_record(rmScan.rid()).second;
439+
auto [tuple_meta, record] = fh_->get_record(rmScan.rid());
440+
if (tuple_meta.is_deleted_) {
441+
continue; // 跳过已删除的记录
442+
}
440443
// 构建组合索引键
441444
int offset = 0;
442445
for (auto& col : cols) {
@@ -451,7 +454,8 @@ void SmManager::create_index(std::string tab_name, std::vector<std::string> col_
451454
auto res = ih_->insert_entry_without_lock(key, rmScan.rid());
452455
// 如果插入失败(可能是违反唯一性约束),回滚索引创建
453456
if (!res) {
454-
drop_index(tab_name, col_names, context);
457+
ix_manager.close_index_without_flush(ih_.get()); // 关闭索引文件
458+
ix_manager.destroy_index_with_index_name(index_name); // 删除索引文件
455459
return;
456460
}
457461
}

0 commit comments

Comments
 (0)