class Context { public: Context (LockManager *lock_mgr, LogManager *log_mgr, Transaction *txn, char *data_send = nullptr, int *offset = &const_offset) : lock_mgr_(lock_mgr), log_mgr_(log_mgr), txn_(txn), data_send_(data_send), offset_(offset) { ellipsis_ = false; }完善下列代码Rid RmFileHandle::insert_record(char* buf, Context* context) { return Rid{-1, -1}; } 功能是// Todo: // 1. 获取当前未满的page handle // 2. 在page handle中找到空闲slot位置 // 3. 将buf复制到空闲slot位置 // 4. 更新page_handle.page_hdr中的数据结构 // 注意考虑插入一条记录后页面已满的情况,需要更新file_hdr_.first_free_page_no
时间: 2024-04-19 17:23:55 浏览: 203
mgr.rar_MGR_atlasmgr_atlasmgr2_cell_cell phone key
Rid RmFileHandle::insert_record(char* buf, Context* context) {
// 1. 获取当前未满的page handle
PageHandle* page_handle = nullptr;
int page_no = context->txn_->get_page_to_insert();
while (page_no != -1) {
page_handle = get_page_handle(page_no);
if (!page_handle->is_full()) {
break;
}
page_no = page_handle->get_next_page_no();
}
if (page_handle == nullptr) {
// 所有的页面都已满,需要创建新的页面
page_no = create_new_page();
page_handle = get_page_handle(page_no);
}
// 2. 在page handle中找到空闲slot位置
SlotId slot_id = page_handle->get_free_slot();
if (slot_id == -1) {
// 页面已满,需要更新file_hdr_.first_free_page_no
int first_free_page_no = context->txn_->get_first_free_page_no();
if (first_free_page_no == page_no) {
context->txn_->set_first_free_page_no(page_handle->get_next_page_no());
}
// 返回错误Rid表示插入失败
return Rid{-1, -1};
}
// 3. 将buf复制到空闲slot位置
page_handle->copy_to_slot(buf, slot_id);
// 4. 更新page_handle.page_hdr中的数据结构
page_handle->update_page_header();
// 注意考虑插入一条记录后页面已满的情况,需要更新file_hdr_.first_free_page_no
if (page_handle->is_full()) {
int first_free_page_no = context->txn_->get_first_free_page_no();
if (first_free_page_no == page_no) {
context->txn_->set_first_free_page_no(page_handle->get_next_page_no());
}
}
// 返回成功插入的Rid
return Rid{page_no, slot_id};
}
阅读全文