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

Drop table #466

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/observer/sql/executor/command_executor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ See the Mulan PSL v2 for more details. */
#include "sql/executor/create_index_executor.h"
#include "sql/executor/create_table_executor.h"
#include "sql/executor/desc_table_executor.h"
#include "sql/executor/drop_table_executor.h"
#include "sql/executor/help_executor.h"
#include "sql/executor/load_data_executor.h"
#include "sql/executor/set_variable_executor.h"
Expand All @@ -42,6 +43,11 @@ RC CommandExecutor::execute(SQLStageEvent *sql_event)
rc = executor.execute(sql_event);
} break;

case StmtType::DROP_TABLE: {
DropTableExecutor executor;
rc = executor.execute(sql_event);
} break;

case StmtType::DESC_TABLE: {
DescTableExecutor executor;
rc = executor.execute(sql_event);
Expand Down
25 changes: 25 additions & 0 deletions src/observer/sql/executor/drop_table_executor.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#include "sql/executor/drop_table_executor.h"

#include "common/log/log.h"
#include "event/session_event.h"
#include "event/sql_event.h"
#include "session/session.h"
#include "sql/stmt/drop_table_stmt.h"
#include "storage/db/db.h"

RC DropTableExecutor::execute(SQLStageEvent *sql_event)
{
Stmt *stmt = sql_event->stmt();
Session *session = sql_event->session_event()->session();
ASSERT(stmt->type() == StmtType::DROP_TABLE,
"drop table executor can not run this command: %d",
static_cast<int>(stmt->type()));

DropTableStmt *drop_table_stmt = static_cast<DropTableStmt *>(stmt);

const char *table_name = drop_table_stmt->table_name().c_str();
RC rc = session->get_current_db()->drop_table(table_name);

return rc;
}

19 changes: 19 additions & 0 deletions src/observer/sql/executor/drop_table_executor.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#pragma once

#include "common/rc.h"

class SQLStageEvent;

/**
* @brief 删除表的执行器
* @ingroup Executor
*/
class DropTableExecutor
{
public:
DropTableExecutor() = default;
virtual ~DropTableExecutor() = default;

RC execute(SQLStageEvent *sql_event);
};

11 changes: 11 additions & 0 deletions src/observer/sql/stmt/drop_table_stmt.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#include "common/log/log.h"
#include "common/types.h"
#include "sql/stmt/drop_table_stmt.h"
#include "event/sql_debug.h"

RC DropTableStmt::create(Db *db, const DropTableSqlNode &drop_table, Stmt *&stmt)
{
stmt = new DropTableStmt(drop_table.relation_name);
sql_debug("create table statement: table name %s", drop_table.relation_name.c_str());
return RC::SUCCESS;
}
31 changes: 31 additions & 0 deletions src/observer/sql/stmt/drop_table_stmt.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#pragma once

#include <string>

#include "sql/stmt/stmt.h"

class Db;

/**
* @brief 表示删除表的语句
* @ingroup Statement
* @details 虽然解析成了stmt,但是与原始的SQL解析后的数据也差不多
*/
class DropTableStmt : public Stmt
{
public:
DropTableStmt(const std::string &table_name) : table_name_(table_name) {}
virtual ~DropTableStmt() = default;

StmtType type() const override { return StmtType::DROP_TABLE; }

const std::string &table_name() const { return table_name_; }

//TODO:
static RC create(Db *db, const DropTableSqlNode &create_table, Stmt *&stmt);

private:
std::string table_name_;
};


5 changes: 5 additions & 0 deletions src/observer/sql/stmt/stmt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ See the Mulan PSL v2 for more details. */
#include "sql/stmt/calc_stmt.h"
#include "sql/stmt/create_index_stmt.h"
#include "sql/stmt/create_table_stmt.h"
#include "sql/stmt/drop_table_stmt.h"
#include "sql/stmt/delete_stmt.h"
#include "sql/stmt/desc_table_stmt.h"
#include "sql/stmt/exit_stmt.h"
Expand Down Expand Up @@ -71,6 +72,10 @@ RC Stmt::create_stmt(Db *db, ParsedSqlNode &sql_node, Stmt *&stmt)
return CreateTableStmt::create(db, sql_node.create_table, stmt);
}

case SCF_DROP_TABLE: {
return DropTableStmt::create(db, sql_node.drop_table, stmt);
}

case SCF_DESC_TABLE: {
return DescTableStmt::create(db, sql_node.desc_table, stmt);
}
Expand Down
18 changes: 18 additions & 0 deletions src/observer/storage/db/db.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,24 @@ RC Db::create_table(const char *table_name, span<const AttrInfoSqlNode> attribut
return RC::SUCCESS;
}

RC Db::drop_table(const char *table_name) {
RC rc = RC::SUCCESS;

auto table_it = opened_tables_.find(table_name);
// check table_name
if (table_it == opened_tables_.end()) {
LOG_WARN("%s has not been opened.", table_name);
return RC::SCHEMA_TABLE_NOT_EXIST;
}

string table_file_path = table_meta_file(path_.c_str(), table_name);
Table *table = table_it->second;
opened_tables_.erase(table_it);

Table::drop(table);
return rc;
}

Table *Db::find_table(const char *table_name) const
{
unordered_map<string, Table *>::const_iterator iter = opened_tables_.find(table_name);
Expand Down
6 changes: 6 additions & 0 deletions src/observer/storage/db/db.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,12 @@ class Db
RC create_table(const char *table_name, span<const AttrInfoSqlNode> attributes,
const StorageFormat storage_format = StorageFormat::ROW_FORMAT);

/**
* @brief 删除一个表
* @param table_name 表名
*/
RC drop_table(const char *table_name);

/**
* @brief 根据表名查找表
*/
Expand Down
24 changes: 24 additions & 0 deletions src/observer/storage/table/table.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,23 @@ RC Table::create(Db *db, int32_t table_id, const char *path, const char *name, c
return rc;
}

RC Table::drop(Table *table) {
string base_dir = table->base_dir_;
string name = table->name();
string path = table_meta_file(base_dir.c_str(), name.c_str());

for (vector<Index *>::iterator it = table->indexes_.begin(); it != table->indexes_.end(); ++it) {
Index *index = *it;
drop_index(table, index);
}
delete table;
::remove(path.c_str());
::remove((path + ".tmp").c_str());
string data_file = table_data_file(base_dir.c_str(), name.c_str());
::remove(data_file.c_str());
return RC::SUCCESS;
}

RC Table::open(Db *db, const char *meta_file, const char *base_dir)
{
// 加载元数据文件
Expand Down Expand Up @@ -451,6 +468,13 @@ RC Table::create_index(Trx *trx, const FieldMeta *field_meta, const char *index_
return rc;
}

RC Table::drop_index(Table *table, Index *idx)
{
string index_file = table_index_file(table->base_dir_.c_str(), table->name(), idx->index_meta().name());
::remove(index_file.c_str());
return RC::SUCCESS;
}

RC Table::delete_record(const RID &rid)
{
RC rc = RC::SUCCESS;
Expand Down
10 changes: 10 additions & 0 deletions src/observer/storage/table/table.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,11 @@ class Table
RC create(Db *db, int32_t table_id, const char *path, const char *name, const char *base_dir,
span<const AttrInfoSqlNode> attributes, StorageFormat storage_format);

/*
* 删除一个表,会回收Table内存
*/
static RC drop(Table *table);

/**
* 打开一个表
* @param meta_file 保存表元数据的文件完整路径
Expand Down Expand Up @@ -85,6 +90,11 @@ class Table
// TODO refactor
RC create_index(Trx *trx, const FieldMeta *field_meta, const char *index_name);

/*
* 删除index,不会释放内存
*/
static RC drop_index(Table *table, Index *idx);

RC get_record_scanner(RecordFileScanner &scanner, Trx *trx, ReadWriteMode mode);

RC get_chunk_scanner(ChunkFileScanner &scanner, Trx *trx, ReadWriteMode mode);
Expand Down
Loading