Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add drop table #1

Merged
merged 1 commit into from
Sep 30, 2022
Merged

add drop table #1

merged 1 commit into from
Sep 30, 2022

Conversation

wuxiaobai24
Copy link
Owner

No description provided.

@wuxiaobai24 wuxiaobai24 merged commit d227524 into main Sep 30, 2022
@wuxiaobai24 wuxiaobai24 deleted the drop_table branch September 30, 2022 02:54
@wuxiaobai24
Copy link
Owner Author

背景

题目描述

删除表。清除表相关的资源。
注意:要删除所有与表关联的数据,不仅仅是在create table时创建的资源,还包括索引等数据。

测例

create table t(id int, age int);  
create table t(id int, name char);  
drop table t;  
create table t(id int, name char);

代码阅读

由于 Drop Table 基本可以理解为 Create Table 的逆操作,因此我们可以先阅读一下 Create Table 的代码和基本流程。

具体的调用链为:handle_event => do_create_table => db->create_table => table->create

  • handle_event:执行器的入口函数,会根据解析到的语句,调用不同的执行函数。
  • do_create_table:获取当前的 Database,并调用其create_table成员函数,以及设置 Response 等工作
  • db->create_table
    • 检查表名是否重复
    • 调用table->create对table对象进行初始化
    • 将初始化完成的table对象移入opened_tables_进行管理
  • table->create:核心函数

建表的关键代码在于 table 对象的 create 函数,该函数的主要流程有:

  1. 检查输入信息:表名是否为空,attr 个数是否为0
  2. 判断表文件是否存在
  3. 创建.table文件,并将元数据写入文件中
  4. 调用BufferPoolManager创建数据文件.data
  5. 调用init_record_handler初始化record_handler_
    1. 初始化 BufferPoolManager,并设置disk_buffer_pool_
    2. 初始化record_handler

Drop Table 代码实现

Drop Table 的调用链应该与 Create Table 是类似的:handle_event => do_drop_table => db->dorp_table => table->drop

首先要在handle_event入口函数中,加入对应的代码使得识别到为drop语句时,调用对应的do_drop_table函数。这个没啥难度。

do_drop_table函数与do_create_table函数类似,只是简单获取当前使用的 database,就直接调用 database 的drop_table函数,并设置对应的Response。

db->drop_table的流程:

  1. 检查表名是否存在
  2. opened_tables_获取对应的 Table 对象
  3. 调用table->drop
  4. open_tables中删除对应的对象,并释放 Table 对象的指针

table->drop基本为table->create的逆过程:

  1. 检查输入信息:表名是否为空
  2. 判断表文件是否存在,不存在则退出
  3. 移除record_handler(这里可以封装成remove_record_handler
    1. 调用record_hanlderclose函数,并释放
    2. data_buffer_pool中移除
  4. 删除 BufferPoolManager 创建的.data文件(封装成一个remove_file函数)
  5. 删除.table文件

具体实现见:add drop table by wuxiaobai24 · Pull Request #1 · wuxiaobai24/miniob · GitHub

这里drop_file还没有删除 index 文件

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant