Skip to content

Commit

Permalink
2022 oceanbase miniob competition (#104)
Browse files Browse the repository at this point in the history
2022 oceanbase competition
  • Loading branch information
hnwyllmm authored Oct 17, 2022
1 parent d63a39f commit c7ecaac
Show file tree
Hide file tree
Showing 30 changed files with 1,905 additions and 94 deletions.
63 changes: 24 additions & 39 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,50 +1,35 @@
# Introduction
miniob 是 OceanBase与华中科技大学联合开发的、面向"零"基础数据库内核知识同学的一门数据库实现入门教程实践工具。
miniob设计的目标是让不熟悉数据库设计和实现的同学能够快速的了解与深入学习数据库内核,期望通过相关训练之后,能够对各个数据库内核模块的功能与它们之间的关联有所了解,并能够在
使用数据库时,设计出高效的SQL。面向的对象主要是在校学生,并且诸多模块做了简化,比如不考虑并发操作。
注意:此代码仅供学习使用,不考虑任何安全特性。
# 介绍
miniob 是 OceanBase 与华中科技大学联合开发的、面向"零"基础同学的数据库入门实践工具。
miniob 设计的目标是让同学们快速了解数据库并深入学习数据库内核,期望通过相关训练之后,能够对数据库内核各个模块的功能及其关联有所了解,并能够在
使用数据库时,设计出高效的 SQL 。miniob 面向的对象主要是在校学生,并且诸多模块都做了简化,比如不考虑并发操作。

[GitHub 首页](https://github.com/oceanbase/miniob)

# 如何开发
## 搭建开发环境
有多种方式搭建开发环境,可以直接在本地安装一些三方依赖,或者使用Docker。如果使用的是Windows,我们建议使用Docker来开发。

### 搭建本地开发环境
直接在本地搭建开发环境,可以参考 [how_to_build](docs/how_to_build.md)

### 使用Docker开发

请参考 [如何使用Docker开发MiniOB](docs/how-to-dev-using-docker.md)
(注意:此代码仅供学习使用,不考虑任何安全特性。)

### Windows上开发MiniOB

[如何在Windows上使用Docker开发miniob](docs/how_to_dev_miniob_by_docker_on_windows.md)

## 词法语法解析开发环境

如果已经在处理一些SQL词法语法解析相关的问题,请参考 [MiniOB 词法语法解析开发与测试](docs/miniob-sql-parser.md)
Docker 环境已经预安装了相关的组件。

# 数据库管理系统实现基础讲义
由华中科技大学谢美意和左琼老师联合编撰数据库管理系统实现教材。参考 [数据库管理系统实现基础讲义](docs/lectures/index.md)
[GitHub 首页](https://github.com/oceanbase/miniob)

# miniob 介绍
[miniob代码架构框架设计和说明](docs/miniob-introduction.md)
# 1. 题目说明
[miniob 题目描述](docs/miniob_topics.md)

# miniob 训练
我们为MiniOB设计了配套的训练题目,大家可以在 [MiniOB 训练营](https://open.oceanbase.com/train?questionId=200001) 上进行提交测试。
# 2. 开发指南
## 搭建开发环境
1. [本地配置gcc环境](docs/how_to_build.md)
2. [使用Docker开发](docs/how-to-dev-using-docker.md)
3. [在Windows上使用Docker](docs/how_to_dev_miniob_by_docker_on_windows.md)

[miniob 题目描述](docs/miniob_topics.md)
## 词法、语法解析
请参考 [miniob 词法语法解析开发与测试](docs/miniob-sql-parser.md)

为了满足训练营或比赛测试要求,代码的输出需要满足一定要求,请参考 [MiniOB 输出约定](docs/miniob-output-convention.md)。一般情况下,不需要专门来看这篇文档,但是如果你的测试总是不正确,建议对照一下输出约定。
# 3. 提交测试
题目完成并通过自测后,大家可以在 [miniob 训练营](https://open.oceanbase.com/train?questionId=200001) 上提交代码进行测试。

# miniob 实现解析
客户端输出需要满足一定要求,如果你的测试结果不符合预期,请参考 [miniob 输出约定](docs/miniob-output-convention.md)

[miniob-date 实现解析](https://oceanbase-partner.github.io/lectures-on-dbms-implementation/miniob-date-implementation.html)
# 4. 数据库管理系统实现基础讲义
由华中科技大学谢美意和左琼老师联合编撰的数据库管理系统实现教材:[《数据库管理系统实现基础讲义》](docs/lectures/index.md)

[miniob drop-table 实现解析](https://oceanbase-partner.github.io/lectures-on-dbms-implementation/miniob-drop-table-implementation.html)
# 5. miniob 介绍
[miniob 源码解析视频](https://open.oceanbase.com/activities/4921877)

[miniob select-tables 实现解析](https://oceanbase-partner.github.io/lectures-on-dbms-implementation/miniob-select-tables-implementation.html)
[miniob 源码解析文档](https://www.oceanbase.com/docs/community-developer-quickstart-10000000000627363)

[miniob 调试篇](https://oceanbase-partner.github.io/lectures-on-dbms-implementation/miniob-how-to-debug.html)
(资料持续整理中,请大家自行查阅标题为“MiniOB...”的视频或文档)
7 changes: 5 additions & 2 deletions deps/common/lang/defer.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,5 +37,8 @@ class DeferHelper

} // namespace common

#define DERFER_NAME(suffix) defer_helper_##suffix
#define DEFER(callback) common::DeferHelper DERFER_NAME(__LINE__)(callback)
#define AA(B, C) B##C

#define BB(B, C) AA(B,C)

#define DEFER(callback) common::DeferHelper BB(defer_helper_, __LINE__)(callback)
16 changes: 16 additions & 0 deletions src/observer/rc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ const char *strrc(RC rc)
RC_CASE_STRING(FORMAT);
RC_CASE_STRING(RANGE);
RC_CASE_STRING(NOTADB);
RC_CASE_STRING(LOGBUF);
RC_CASE_STRING(NOTICE);

RC_CASE_STRING(BUFFERPOOL_EXIST);
Expand Down Expand Up @@ -174,8 +175,23 @@ const char *strrc(RC rc)
RC_CASE_STRING(NOTICE_RECOVER_ROLLBACK);
RC_CASE_STRING(NOTICE_AUTOINDEX);

RC_CASE_STRING(FILE_EXIST);
RC_CASE_STRING(FILE_NOT_EXIST);
RC_CASE_STRING(FILE_NAME);
RC_CASE_STRING(FILE_BOUND);
RC_CASE_STRING(FILE_CREATE);
RC_CASE_STRING(FILE_OPEN);
RC_CASE_STRING(FILE_NOT_OPENED);
RC_CASE_STRING(FILE_CLOSE);
RC_CASE_STRING(FILE_REMOVE);
RC_CASE_STRING(FILE_SEEK);
RC_CASE_STRING(FILE_READ);
RC_CASE_STRING(FILE_WRITE);

RC_CASE_STRING(AUTH_USER);

RC_CASE_STRING(LOGBUF_FULL);
RC_CASE_STRING(LOGBUF_EMPTY);
default: {
return "UNKNOWN";
}
Expand Down
40 changes: 40 additions & 0 deletions src/observer/rc.h
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,26 @@ enum RCAuth {
USER = 1,
};

enum RCFILE {
F_EXIST = 1,
F_NOT_EXIST,
F_NAME,
F_BOUND,
F_CREATE,
F_OPEN,
F_NOT_OPENED,
F_CLOSE,
F_REMOVE,
F_SEEK,
F_READ,
F_WRITE,
};

enum RCLOGBUF {
LB_FULL = 1,
LB_EMPTY,
};

enum RC {

SUCCESS = 0, /* Successful result */
Expand Down Expand Up @@ -201,6 +221,8 @@ enum RC {
FORMAT, /* Not used */
RANGE, /* 2nd parameter to bind out of range */
NOTADB, /* File opened that is not a database file */
FILE_ERROR, /* File error */
LOGBUF, /* clog buffer error */
NOTICE = 100, /* Notifications from log() */

/* buffer pool part */
Expand Down Expand Up @@ -337,8 +359,26 @@ enum RC {
NOTICE_RECOVER_ROLLBACK = (NOTICE | (RCNotice::RECOVER_ROLLBACK << 8)),
NOTICE_AUTOINDEX = (NOTICE | (RCNotice::AUTOINDEX << 8)),

/* file part */
FILE_EXIST = ( FILE_ERROR | (RCFILE::F_EXIST << 8)),
FILE_NOT_EXIST = ( FILE_ERROR | (RCFILE::F_NOT_EXIST << 8)),
FILE_NAME = ( FILE_ERROR | (RCFILE::F_NAME << 8)),
FILE_BOUND = ( FILE_ERROR | (RCFILE::F_BOUND << 8)),
FILE_CREATE = ( FILE_ERROR | (RCFILE::F_CREATE << 8)),
FILE_OPEN = ( FILE_ERROR | (RCFILE::F_OPEN << 8)),
FILE_NOT_OPENED = ( FILE_ERROR | (RCFILE::F_NOT_OPENED << 8)),
FILE_CLOSE = ( FILE_ERROR | (RCFILE::F_CLOSE << 8)),
FILE_REMOVE = ( FILE_ERROR | (RCFILE::F_REMOVE << 8)),
FILE_SEEK = ( FILE_ERROR | (RCFILE::F_SEEK << 8)),
FILE_READ = ( FILE_ERROR | (RCFILE::F_READ << 8)),
FILE_WRITE = ( FILE_ERROR | (RCFILE::F_WRITE << 8)),

/* auth part*/
AUTH_USER = (AUTH | (RCAuth::USER << 8)),

/* clog buffer part */
LOGBUF_FULL = (LOGBUF | (RCLOGBUF::LB_FULL << 8)),
LOGBUF_EMPTY = (LOGBUF | (RCLOGBUF::LB_EMPTY << 8)),
};

extern const char *strrc(RC rc);
Expand Down
138 changes: 134 additions & 4 deletions src/observer/sql/executor/execute_stage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ See the Mulan PSL v2 for more details. */
#include "storage/default/default_handler.h"
#include "storage/common/condition_filter.h"
#include "storage/trx/trx.h"
#include "storage/clog/clog.h"

using namespace common;

Expand Down Expand Up @@ -175,18 +176,29 @@ void ExecuteStage::handle_request(common::StageEvent *event)
default_storage_stage_->handle_event(event);
} break;
case SCF_SYNC: {
/*
RC rc = DefaultHandler::get_default().sync();
session_event->set_response(strrc(rc));
*/
} break;
case SCF_BEGIN: {
do_begin(sql_event);
/*
session_event->set_response("SUCCESS\n");
*/
} break;
case SCF_COMMIT: {
do_commit(sql_event);
/*
Trx *trx = session->current_trx();
RC rc = trx->commit();
session->set_trx_multi_operation_mode(false);
session_event->set_response(strrc(rc));
*/
} break;
case SCF_CLOG_SYNC: {
do_clog_sync(sql_event);
}
case SCF_ROLLBACK: {
Trx *trx = session_event->get_client()->session->current_trx();
RC rc = trx->rollback();
Expand Down Expand Up @@ -519,18 +531,40 @@ RC ExecuteStage::do_insert(SQLStageEvent *sql_event)
{
Stmt *stmt = sql_event->stmt();
SessionEvent *session_event = sql_event->session_event();
Session *session = session_event->session();
Db *db = session->get_current_db();
Trx *trx = session->current_trx();
CLogManager *clog_manager = db->get_clog_manager();

if (stmt == nullptr) {
LOG_WARN("cannot find statement");
return RC::GENERIC_ERROR;
}

InsertStmt *insert_stmt = (InsertStmt *)stmt;

Table *table = insert_stmt->table();
RC rc = table->insert_record(nullptr, insert_stmt->value_amount(), insert_stmt->values());

RC rc = table->insert_record(trx, insert_stmt->value_amount(), insert_stmt->values());
if (rc == RC::SUCCESS) {
session_event->set_response("SUCCESS\n");
if (!session->is_trx_multi_operation_mode()) {
CLogRecord *clog_record = nullptr;
rc = clog_manager->clog_gen_record(CLogType::REDO_MTR_COMMIT, trx->get_current_id(), clog_record);
if (rc != RC::SUCCESS || clog_record == nullptr) {
session_event->set_response("FAILURE\n");
return rc;
}

rc = clog_manager->clog_append_record(clog_record);
if (rc != RC::SUCCESS) {
session_event->set_response("FAILURE\n");
return rc;
}

trx->next_current_id();
session_event->set_response("SUCCESS\n");
} else {
session_event->set_response("SUCCESS\n");
}
} else {
session_event->set_response("FAILURE\n");
}
Expand All @@ -541,6 +575,10 @@ RC ExecuteStage::do_delete(SQLStageEvent *sql_event)
{
Stmt *stmt = sql_event->stmt();
SessionEvent *session_event = sql_event->session_event();
Session *session = session_event->session();
Db *db = session->get_current_db();
Trx *trx = session->current_trx();
CLogManager *clog_manager = db->get_clog_manager();

if (stmt == nullptr) {
LOG_WARN("cannot find statement");
Expand All @@ -551,14 +589,106 @@ RC ExecuteStage::do_delete(SQLStageEvent *sql_event)
TableScanOperator scan_oper(delete_stmt->table());
PredicateOperator pred_oper(delete_stmt->filter_stmt());
pred_oper.add_child(&scan_oper);
DeleteOperator delete_oper(delete_stmt);
DeleteOperator delete_oper(delete_stmt, trx);
delete_oper.add_child(&pred_oper);

RC rc = delete_oper.open();
if (rc != RC::SUCCESS) {
session_event->set_response("FAILURE\n");
} else {
session_event->set_response("SUCCESS\n");
if (!session->is_trx_multi_operation_mode()) {
CLogRecord *clog_record = nullptr;
rc = clog_manager->clog_gen_record(CLogType::REDO_MTR_COMMIT, trx->get_current_id(), clog_record);
if (rc != RC::SUCCESS || clog_record == nullptr) {
session_event->set_response("FAILURE\n");
return rc;
}

rc = clog_manager->clog_append_record(clog_record);
if (rc != RC::SUCCESS) {
session_event->set_response("FAILURE\n");
return rc;
}

trx->next_current_id();
session_event->set_response("SUCCESS\n");
}
}
return rc;
}

RC ExecuteStage::do_begin(SQLStageEvent *sql_event)
{
RC rc = RC::SUCCESS;
SessionEvent *session_event = sql_event->session_event();
Session *session = session_event->session();
Db *db = session->get_current_db();
Trx *trx = session->current_trx();
CLogManager *clog_manager = db->get_clog_manager();

session->set_trx_multi_operation_mode(true);

CLogRecord *clog_record = nullptr;
rc = clog_manager->clog_gen_record(CLogType::REDO_MTR_BEGIN, trx->get_current_id(), clog_record);
if (rc != RC::SUCCESS || clog_record == nullptr) {
session_event->set_response("FAILURE\n");
return rc;
}

rc = clog_manager->clog_append_record(clog_record);
if (rc != RC::SUCCESS) {
session_event->set_response("FAILURE\n");
} else {
session_event->set_response("SUCCESS\n");
}

return rc;
}

RC ExecuteStage::do_commit(SQLStageEvent *sql_event)
{
RC rc = RC::SUCCESS;
SessionEvent *session_event = sql_event->session_event();
Session *session = session_event->session();
Db *db = session->get_current_db();
Trx *trx = session->current_trx();
CLogManager *clog_manager = db->get_clog_manager();

session->set_trx_multi_operation_mode(false);

CLogRecord *clog_record = nullptr;
rc = clog_manager->clog_gen_record(CLogType::REDO_MTR_COMMIT, trx->get_current_id(), clog_record);
if (rc != RC::SUCCESS || clog_record == nullptr) {
session_event->set_response("FAILURE\n");
return rc;
}

rc = clog_manager->clog_append_record(clog_record);
if (rc != RC::SUCCESS) {
session_event->set_response("FAILURE\n");
} else {
session_event->set_response("SUCCESS\n");
}

trx->next_current_id();

return rc;
}

RC ExecuteStage::do_clog_sync(SQLStageEvent *sql_event)
{
RC rc = RC::SUCCESS;
SessionEvent *session_event = sql_event->session_event();
Db *db = session_event->session()->get_current_db();
CLogManager *clog_manager = db->get_clog_manager();

rc = clog_manager->clog_sync();
if (rc != RC::SUCCESS) {
session_event->set_response("FAILURE\n");
} else {
session_event->set_response("SUCCESS\n");
}

return rc;
}
3 changes: 3 additions & 0 deletions src/observer/sql/executor/execute_stage.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ class ExecuteStage : public common::Stage {
RC do_select(SQLStageEvent *sql_event);
RC do_insert(SQLStageEvent *sql_event);
RC do_delete(SQLStageEvent *sql_event);
RC do_begin(SQLStageEvent *sql_event);
RC do_commit(SQLStageEvent *sql_event);
RC do_clog_sync(SQLStageEvent *sql_event);

protected:
private:
Expand Down
Loading

0 comments on commit c7ecaac

Please sign in to comment.