From 93b79cc6d6bb4d827b71209f37e7f81622833164 Mon Sep 17 00:00:00 2001 From: wangyunlai Date: Wed, 14 Jun 2023 13:41:05 +0800 Subject: [PATCH] Refactor (#195) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ### What problem were solved in this pull request? Issue Number: close #173 close #136 ref #174 ref #165 Problem: 这个PR修改了多个问题,可以参考各个issue。包括observer编译两次、代码目录规划不合理、command executor未全部实现等 ### What is changed and how it works? 做一些重构优化,包括CMakelist、一些代码目录、command executor --- benchmark/bplus_tree_concurrency_test.cpp | 1 - benchmark/record_manager_concurrency_test.cpp | 1 - build.sh | 2 +- deps/common/CMakeLists.txt | 25 +- deps/common/defs.h | 30 +- deps/common/io/io.cpp | 10 +- deps/common/io/io.h | 8 +- deps/common/io/roll_select_dir.h | 2 +- deps/common/log/log.h | 9 +- deps/common/mm/mem.cpp.skip | 424 ------------------ deps/common/mm/mem.h | 6 +- deps/common/os/os.cpp | 2 +- deps/common/os/os.h | 2 +- deps/common/seda/seda_config.cpp | 2 +- deps/common/seda/seda_config.h | 2 +- deps/common/seda/stage_event.cpp | 4 +- deps/common/seda/stage_event.h | 4 +- deps/common/seda/thread_pool.cpp | 2 +- deps/common/seda/timer_stage.cpp | 18 +- deps/common/seda/timer_stage.h | 14 +- deps/common/time/datetime.cpp | 6 +- deps/common/time/datetime.h | 10 +- src/observer/CMakeLists.txt | 42 +- src/observer/{ => common}/global_context.cpp | 2 +- src/observer/{ => common}/global_context.h | 4 + src/observer/{ => common}/ini_setting.h | 4 +- src/observer/{ => common}/init.cpp | 5 +- src/observer/{ => common}/init.h | 5 +- src/observer/{ => common}/rc.cpp | 2 +- src/observer/{ => common}/rc.h | 0 src/observer/{defs.h => common/types.h} | 1 - src/observer/main.cpp | 4 +- src/observer/net/communicator.h | 2 +- src/observer/net/server.cpp | 2 +- src/observer/session/session.cpp | 4 +- .../sql/executor/command_executor.cpp | 49 +- src/observer/sql/executor/command_executor.h | 8 +- .../sql/executor/create_index_executor.cpp | 8 +- .../sql/executor/create_index_executor.h | 7 +- .../sql/executor/create_table_executor.cpp | 40 ++ .../sql/executor/create_table_executor.h | 28 ++ .../sql/executor/desc_table_executor.cpp | 69 +++ .../sql/executor/desc_table_executor.h | 18 +- src/observer/sql/executor/execute_stage.cpp | 230 +--------- src/observer/sql/executor/execute_stage.h | 18 +- src/observer/sql/executor/help_executor.h | 58 +++ .../sql/executor/show_tables_executor.h | 53 +++ src/observer/sql/executor/sql_result.cpp | 2 +- .../sql/executor/trx_begin_executor.h | 42 ++ src/observer/sql/executor/trx_end_executor.h | 47 ++ src/observer/sql/expr/expression.h | 2 +- src/observer/sql/expr/tuple_cell.cpp | 2 +- src/observer/sql/expr/tuple_cell.h | 4 +- .../sql/operator/delete_physical_operator.cpp | 2 +- .../sql/operator/delete_physical_operator.h | 1 - .../sql/operator/insert_physical_operator.cpp | 3 +- .../sql/operator/insert_physical_operator.h | 1 - .../sql/operator/join_physical_operator.h | 1 - src/observer/sql/operator/physical_operator.h | 2 +- .../operator/predicate_physical_operator.cpp | 2 +- .../sql/operator/project_logical_operator.h | 2 +- .../operator/project_physical_operator.cpp | 2 +- .../sql/operator/project_physical_operator.h | 1 - .../sql/operator/table_get_logical_operator.h | 2 +- .../operator/table_scan_physical_operator.cpp | 3 +- .../operator/table_scan_physical_operator.h | 2 +- .../comparison_simplification_rule.h | 2 +- .../conjunction_simplification_rule.h | 1 - .../sql/optimizer/expression_rewriter.h | 2 +- src/observer/sql/optimizer/optimize_stage.h | 41 +- .../sql/optimizer/physical_plan_generator.h | 2 +- src/observer/sql/optimizer/rewrite_rule.h | 8 +- src/observer/sql/parser/parse.cpp | 3 +- src/observer/sql/parser/parse.h | 2 +- src/observer/sql/parser/parse_defs.h | 193 +++++--- src/observer/sql/parser/parse_stage.h | 21 +- src/observer/sql/parser/resolve_stage.h | 7 +- .../sql/plan_cache/plan_cache_stage.h | 8 +- src/observer/sql/stmt/create_index_stmt.cpp | 4 +- src/observer/sql/stmt/create_table_stmt.cpp | 21 + src/observer/sql/stmt/create_table_stmt.h | 47 ++ src/observer/sql/stmt/delete_stmt.cpp | 4 +- src/observer/sql/stmt/delete_stmt.h | 4 +- src/observer/sql/stmt/desc_table_stmt.cpp | 25 ++ src/observer/sql/stmt/desc_table_stmt.h | 44 ++ src/observer/sql/stmt/exit_stmt.h | 39 ++ src/observer/sql/stmt/filter_stmt.cpp | 6 +- src/observer/sql/stmt/filter_stmt.h | 10 +- src/observer/sql/stmt/help_stmt.h | 39 ++ src/observer/sql/stmt/insert_stmt.cpp | 4 +- src/observer/sql/stmt/insert_stmt.h | 2 +- src/observer/sql/stmt/select_stmt.cpp | 4 +- src/observer/sql/stmt/select_stmt.h | 4 +- .../observer/sql/stmt/show_tables_stmt.h | 42 +- src/observer/sql/stmt/stmt.cpp | 39 +- src/observer/sql/stmt/stmt.h | 64 +-- src/observer/sql/stmt/trx_begin_stmt.h | 39 ++ src/observer/sql/stmt/trx_end_stmt.h | 44 ++ src/observer/sql/stmt/update_stmt.h | 2 +- src/observer/storage/buffer/frame.h | 2 +- src/observer/storage/buffer/page.h | 2 +- src/observer/storage/clog/clog.cpp | 4 +- src/observer/storage/clog/clog.h | 1 - .../storage/common/condition_filter.cpp | 2 +- .../storage/common/condition_filter.h | 1 - src/observer/storage/{common => db}/db.cpp | 6 +- src/observer/storage/{common => db}/db.h | 2 +- .../storage/default/default_handler.cpp | 2 +- .../storage/default/default_handler.h | 2 +- .../storage/default/default_storage_stage.cpp | 6 +- .../storage/default/disk_buffer_pool.cpp | 4 +- .../storage/default/disk_buffer_pool.h | 4 +- .../storage/{common => field}/field.cpp | 2 +- .../storage/{common => field}/field.h | 4 +- .../storage/{common => field}/field_meta.cpp | 4 +- .../storage/{common => field}/field_meta.h | 2 +- src/observer/storage/index/bplus_tree.cpp | 1 - src/observer/storage/index/index.h | 9 +- .../storage/{common => index}/index_meta.cpp | 7 +- .../storage/{common => index}/index_meta.h | 2 +- src/observer/storage/persist/persist.h | 2 +- src/observer/storage/record/record.h | 8 +- .../storage/record/record_manager.cpp | 1 - .../storage/{common => table}/table.cpp | 4 +- .../storage/{common => table}/table.h | 2 +- .../storage/{common => table}/table_meta.cpp | 2 +- .../storage/{common => table}/table_meta.h | 6 +- src/observer/storage/trx/latch_memo.h | 2 +- src/observer/storage/trx/mvcc_trx.cpp | 4 +- src/observer/storage/trx/trx.cpp | 6 +- src/observer/storage/trx/trx.h | 6 +- test/perf/client_performance_test.cpp | 3 +- unittest/bplus_tree_test.cpp | 1 - unittest/persist_test.cpp | 1 - 134 files changed, 1208 insertions(+), 1056 deletions(-) delete mode 100644 deps/common/mm/mem.cpp.skip rename src/observer/{ => common}/global_context.cpp (95%) rename src/observer/{ => common}/global_context.h (79%) rename src/observer/{ => common}/ini_setting.h (87%) rename src/observer/{ => common}/init.cpp (99%) rename src/observer/{ => common}/init.h (89%) rename src/observer/{ => common}/rc.cpp (97%) rename src/observer/{ => common}/rc.h (100%) rename src/observer/{defs.h => common/types.h} (99%) create mode 100644 src/observer/sql/executor/create_table_executor.cpp create mode 100644 src/observer/sql/executor/create_table_executor.h create mode 100644 src/observer/sql/executor/desc_table_executor.cpp rename unittest/rc_test.cpp => src/observer/sql/executor/desc_table_executor.h (68%) create mode 100644 src/observer/sql/executor/help_executor.h create mode 100644 src/observer/sql/executor/show_tables_executor.h create mode 100644 src/observer/sql/executor/trx_begin_executor.h create mode 100644 src/observer/sql/executor/trx_end_executor.h create mode 100644 src/observer/sql/stmt/create_table_stmt.cpp create mode 100644 src/observer/sql/stmt/create_table_stmt.h create mode 100644 src/observer/sql/stmt/desc_table_stmt.cpp create mode 100644 src/observer/sql/stmt/desc_table_stmt.h create mode 100644 src/observer/sql/stmt/exit_stmt.h create mode 100644 src/observer/sql/stmt/help_stmt.h rename deps/common/version.h => src/observer/sql/stmt/show_tables_stmt.h (51%) create mode 100644 src/observer/sql/stmt/trx_begin_stmt.h create mode 100644 src/observer/sql/stmt/trx_end_stmt.h rename src/observer/storage/{common => db}/db.cpp (98%) rename src/observer/storage/{common => db}/db.h (98%) rename src/observer/storage/{common => field}/field.cpp (97%) rename src/observer/storage/{common => field}/field.h (95%) rename src/observer/storage/{common => field}/field_meta.cpp (98%) rename src/observer/storage/{common => field}/field_meta.h (98%) rename src/observer/storage/{common => index}/index_meta.cpp (94%) rename src/observer/storage/{common => index}/index_meta.h (98%) rename src/observer/storage/{common => table}/table.cpp (99%) rename src/observer/storage/{common => table}/table.h (98%) rename src/observer/storage/{common => table}/table_meta.cpp (99%) rename src/observer/storage/{common => table}/table_meta.h (95%) diff --git a/benchmark/bplus_tree_concurrency_test.cpp b/benchmark/bplus_tree_concurrency_test.cpp index f8e1656..605c4d2 100644 --- a/benchmark/bplus_tree_concurrency_test.cpp +++ b/benchmark/bplus_tree_concurrency_test.cpp @@ -17,7 +17,6 @@ See the Mulan PSL v2 for more details. */ #include "storage/index/bplus_tree.h" #include "storage/default/disk_buffer_pool.h" -#include "rc.h" #include "common/log/log.h" #include "integer_generator.h" diff --git a/benchmark/record_manager_concurrency_test.cpp b/benchmark/record_manager_concurrency_test.cpp index 4fca60d..b68e766 100644 --- a/benchmark/record_manager_concurrency_test.cpp +++ b/benchmark/record_manager_concurrency_test.cpp @@ -21,7 +21,6 @@ See the Mulan PSL v2 for more details. */ #include "storage/default/disk_buffer_pool.h" #include "storage/common/condition_filter.h" #include "storage/trx/vacuous_trx.h" -#include "rc.h" #include "common/log/log.h" #include "integer_generator.h" diff --git a/build.sh b/build.sh index 8d6d735..bb436b5 100755 --- a/build.sh +++ b/build.sh @@ -78,7 +78,7 @@ function do_init git checkout release-2.1.12-stable && \ mkdir -p build && \ cd build && \ - cmake .. -DEVENT__DISABLE_OPENSSL=ON && \ + cmake .. -DEVENT__DISABLE_OPENSSL=ON -DEVENT__LIBRARY_TYPE=BOTH && \ make -j4 && \ make install diff --git a/deps/common/CMakeLists.txt b/deps/common/CMakeLists.txt index feb5192..61b570e 100644 --- a/deps/common/CMakeLists.txt +++ b/deps/common/CMakeLists.txt @@ -1,15 +1,7 @@ - -PROJECT(common) -MESSAGE("Begin to build " ${PROJECT_NAME}) MESSAGE(STATUS "This is PROJECT_BINARY_DIR dir " ${PROJECT_BINARY_DIR}) MESSAGE(STATUS "This is PROJECT_SOURCE_DIR dir " ${PROJECT_SOURCE_DIR}) -INCLUDE_DIRECTORIES(${PROJECT_SOURCE_DIR}/..) -#find_file() 只会找一个文件 -#AUX_SOURCE_DIRECTORY(. SRC_LIST) -#FOREACH(F ${SRC_LIST}) -# MESSAGE(${F}) -#ENDFOREACH(F) +INCLUDE_DIRECTORIES(${CMAKE_CURRENT_SOURCE_DIR} ${PROJECT_SOURCE_DIR}/deps) FILE(GLOB_RECURSE ALL_SRC *.cpp) FOREACH(F ${ALL_SRC}) @@ -17,7 +9,6 @@ FOREACH(F ${ALL_SRC}) MESSAGE("Use " ${F}) ENDFOREACH(F) - #SHARED,动态库 #STATIC,静态库 ADD_LIBRARY(common STATIC ${ALL_SRC} ) @@ -25,17 +16,7 @@ ADD_LIBRARY(common STATIC ${ALL_SRC} ) # 编译静态库时,自动会把同名的动态库给删除, 因此需要临时设置一下 SET_TARGET_PROPERTIES(common PROPERTIES CLEAN_DIRECT_OUTPUT 1) -# 设置版本号 VERSION指代动态库版本,SOVERSION指代API版本 - -SET(MAIJOR_VER 1) -SET(MINOR_VER 0) -SET(PATCH_VER 0) -SET(OTHER_VER 1) -ADD_DEFINITIONS(-DMAIJOR_VER=${MAIJOR_VER} -DMINOR_VER=${MINOR_VER} -DPATCH_VER=${PATCH_VER} -DOTHER_VER=${OTHER_VER}) - -SET_TARGET_PROPERTIES(common PROPERTIES VERSION ${MAIJOR_VER}.${MINOR_VER}.${PATCH_VER} SOVERSION ${MAIJOR_VER}) - -SET(LIBRARY_OUTPUT_PATH ${PROJECT_BINARY_DIR}/../../lib) +SET(LIBRARY_OUTPUT_PATH ${PROJECT_BINARY_DIR}/lib) # Target 必须在定义 ADD_EXECUTABLE 之后, programs 不受这个限制 # TARGETS和PROGRAMS 的默认权限是OWNER_EXECUTE, GROUP_EXECUTE, 和WORLD_EXECUTE,即755权限, programs 都是处理脚步类 # 类型分为RUNTIME/LIBRARY/ARCHIVE, prog @@ -50,6 +31,6 @@ FILE(GLOB_RECURSE ALL_HEADER *.h) FOREACH(F ${ALL_HEADER}) file(RELATIVE_PATH RELAPATH_HEADER ${PROJECT_SOURCE_DIR} ${F}) # 获取相对路径 get_filename_component(headDir ${RELAPATH_HEADER} DIRECTORY) - MESSAGE("Install " ${RELAPATH_HEADER} " to " ${CMAKE_INSTALL_PREFIX} "/" ${PROJECT_NAME} "/include/" ${headDir}) + MESSAGE("Install " ${RELAPATH_HEADER} " to " ${CMAKE_INSTALL_PREFIX} "/" common "/include/" ${headDir}) INSTALL(FILES ${RELAPATH_HEADER} DESTINATION include/${headDir}) ENDFOREACH(F) diff --git a/deps/common/defs.h b/deps/common/defs.h index 087395a..4ec40df 100644 --- a/deps/common/defs.h +++ b/deps/common/defs.h @@ -23,7 +23,6 @@ See the Mulan PSL v2 for more details. */ #include #include -#include "common/version.h" namespace common { #ifndef gettid @@ -35,14 +34,8 @@ namespace common { #endif -inline const std::string &theSwVersion() +enum { - static const std::string swVersion(VERSION_STR); - - return swVersion; -} - -enum { // General Error Codes STATUS_SUCCESS = 0, //!< Success status should be zero, STATUS_INVALID_PARAM, //!< Invalid parameter @@ -55,32 +48,11 @@ enum { STATUS_UNKNOW_ERROR, STATUS_LAST_ERR //!< last error code - }; -const unsigned int ONE_KILO = 1024; -const unsigned int ONE_MILLION = ONE_KILO * ONE_KILO; -const unsigned int ONE_GIGA = ONE_MILLION * ONE_KILO; -const unsigned int FILENAME_LENGTH_MAX = 256; // the max filename length - static const char FILE_PATH_SPLIT = '/'; static const char FILE_PATH_SPLIT_STR[] = "/"; -/* - * Define types - * - */ -typedef unsigned char u8_t; -typedef unsigned short u16_t; -typedef unsigned int u32_t; -typedef unsigned long long u64_t; - -typedef char s8_t; -typedef short s16_t; -typedef int s32_t; -typedef long long s64_t; - -#define LOCAL_HOST "localhost" #define EPSILON (1E-6) } // namespace common diff --git a/deps/common/io/io.cpp b/deps/common/io/io.cpp index 916671d..0ddc296 100644 --- a/deps/common/io/io.cpp +++ b/deps/common/io/io.cpp @@ -79,7 +79,7 @@ int readFromFile(const std::string &fileName, char *&outputData, size_t &fileSiz return 0; } -int writeToFile(const std::string &fileName, const char *data, u32_t dataSize, const char *openMode) +int writeToFile(const std::string &fileName, const char *data, uint32_t dataSize, const char *openMode) { FILE *file = fopen(fileName.c_str(), openMode); if (file == NULL) { @@ -87,7 +87,7 @@ int writeToFile(const std::string &fileName, const char *data, u32_t dataSize, c return -1; } - u32_t leftSize = dataSize; + uint32_t leftSize = dataSize; const char *buffer = data; while (leftSize > 0) { int writeCount = fwrite(buffer, 1, leftSize, file); @@ -106,7 +106,7 @@ int writeToFile(const std::string &fileName, const char *data, u32_t dataSize, c return 0; } -int getFileLines(const std::string &fileName, u64_t &lineNum) +int getFileLines(const std::string &fileName, uint64_t &lineNum) { lineNum = 0; @@ -130,7 +130,7 @@ int getFileLines(const std::string &fileName, u64_t &lineNum) return 0; } -int getFileNum(u64_t &fileNum, const std::string &path, const std::string &pattern, bool recursive) +int getFileNum(int64_t &fileNum, const std::string &path, const std::string &pattern, bool recursive) { try { DIR *dirp = NULL; @@ -327,7 +327,7 @@ int touch(const std::string &path) return 0; } -int getFileSize(const char *filePath, u64_t &fileLen) +int getFileSize(const char *filePath, int64_t &fileLen) { if (filePath == NULL || *filePath == '\0') { std::cerr << "invalid filepath" << std::endl; diff --git a/deps/common/io/io.h b/deps/common/io/io.h index 92eea6e..9c28da7 100644 --- a/deps/common/io/io.h +++ b/deps/common/io/io.h @@ -28,12 +28,12 @@ namespace common { */ int readFromFile(const std::string &fileName, char *&data, size_t &fileSize); -int writeToFile(const std::string &fileName, const char *data, u32_t dataSize, const char *openMode); +int writeToFile(const std::string &fileName, const char *data, uint32_t dataSize, const char *openMode); /** * return the line number which line.strip() isn't empty */ -int getFileLines(const std::string &fileName, u64_t &lineNum); +int getFileLines(const std::string &fileName, uint64_t &lineNum); /** Get file list from the dir * don't care ".", "..", ".****" hidden files @@ -47,7 +47,7 @@ int getFileLines(const std::string &fileName, u64_t &lineNum); */ int getFileList( std::vector &fileList, const std::string &path, const std::string &pattern, bool recursive); -int getFileNum(u64_t &fileNum, const std::string &path, const std::string &pattern, bool recursive); +int getFileNum(uint64_t &fileNum, const std::string &path, const std::string &pattern, bool recursive); int getDirList(std::vector &dirList, const std::string &path, const std::string &pattern); int touch(const std::string &fileName); @@ -55,7 +55,7 @@ int touch(const std::string &fileName); /** * get file size */ -int getFileSize(const char *filePath, u64_t &fileLen); +int getFileSize(const char *filePath, uint64_t &fileLen); /** * @brief 一次性写入所有指定数据 diff --git a/deps/common/io/roll_select_dir.h b/deps/common/io/roll_select_dir.h index bac1bca..1d36965 100644 --- a/deps/common/io/roll_select_dir.h +++ b/deps/common/io/roll_select_dir.h @@ -46,7 +46,7 @@ public: std::string mBaseDir; std::vector mSubdirs; pthread_mutex_t mMutex; - u32_t mPos; + uint32_t mPos; }; } // namespace common diff --git a/deps/common/log/log.h b/deps/common/log/log.h index 272ce40..173669f 100644 --- a/deps/common/log/log.h +++ b/deps/common/log/log.h @@ -31,6 +31,11 @@ See the Mulan PSL v2 for more details. */ namespace common { +const unsigned int ONE_KILO = 1024; +const unsigned int ONE_MILLION = ONE_KILO * ONE_KILO; +const unsigned int ONE_GIGA = ONE_MILLION * ONE_KILO; +const unsigned int FILENAME_LENGTH_MAX = 256; // the max filename length + const int LOG_STATUS_OK = 0; const int LOG_STATUS_ERR = 1; const int LOG_MAX_LINE = 100000; @@ -182,7 +187,7 @@ extern Log *g_log; p->tm_min, \ p->tm_sec, \ usec, \ - (u32_t)getpid(), \ + (int32_t)getpid(), \ gettid(), \ common::g_log->context_id()); \ common::g_log->rotate(p->tm_year + 1900, p->tm_mon + 1, p->tm_mday); \ @@ -194,7 +199,7 @@ extern Log *g_log; (common::g_log)->prefix_msg(level), \ __FUNCTION__, \ __FILE_NAME__, \ - (u32_t)__LINE__ \ + (int32_t)__LINE__ \ ); \ } diff --git a/deps/common/mm/mem.cpp.skip b/deps/common/mm/mem.cpp.skip deleted file mode 100644 index f50cbf4..0000000 --- a/deps/common/mm/mem.cpp.skip +++ /dev/null @@ -1,424 +0,0 @@ -// __CR__ -// Copyright (c) 2021 LongdaFeng All Rights Reserved -// -// This software contains the intellectual property of LongdaFeng -// or is licensed to LongdaFeng from third parties. Use of this -// software and the intellectual property contained therein is -// expressly limited to the terms and conditions of the License Agreement -// under which it is provided by or on behalf of LongdaFeng. -// __CR__ -// -// Created by Longda on 2010 -// - -#ifdef MEM_DEBUG - -#include -#include -#include - -#include "lang/mutex.h" -#include "mm/lmem.h" - -#define MEM_ID_HASH(p) (((unsigned long)(p) >> 8) % MEM_HASHTABLE_SIZE) - -pthread_mutex_t CLMemTrace::mMutex = PTHREAD_MUTEX_INITIALIZER; -u64_t CLMemTrace::mUsedSize = 0; -MemID *CLMemTrace::mMemIDs[MEM_HASHTABLE_SIZE] = {0}; -bool CLMemTrace::mVerbose = false; -; - -void *CLMemTrace::malloc(size_t size, const char *file, const int line, bool retry) throw(std::bad_alloc) -{ - - size_t allocSize = size + sizeof(MemID); - void *usedPointer = NULL; - - do { - MemID *ptr = (MemID *)::malloc(allocSize); - if (ptr) { - // successfully alloc memory - - // set the MemID - strncpy(ptr->mFile, file, MemID::MEM_FILENAME_LEN - 1); - ptr->mFile[MemID::MEM_FILENAME_LEN - 1] = '\0'; - ptr->mLine = line; - ptr->mSize = size; - - usedPointer = (char *)ptr + sizeof(MemID); - u64_t hashIndex = (u64_t)MEM_ID_HASH(usedPointer); - - MUTEX_LOCK(&mMutex); - - ptr->mNext = mMemIDs[hashIndex]; - mMemIDs[hashIndex] = ptr; - - mUsedSize += size; - - MUTEX_UNLOCK(&mMutex); - - if (mVerbose) { - LOG_INFO("%s:%d alloc %llu memory %p", file, line, size, usedPointer); - } - return usedPointer; - } - - if (retry == false) { - throw std::bad_alloc(); - break; - } - - std::new_handler allocHandler = getNewHandler(); - - if (allocHandler) { - (*allocHandler)(); - } else { - throw std::bad_alloc(); - break; - } - } while (retry); - - return NULL; -} - -void *CLMemTrace::realloc(void *pointer, size_t size, const char *file, const int line) -{ - if (pointer == NULL && size == 0) { - return NULL; - } else if (pointer == NULL && size != 0) { - try { - return malloc(size, file, line); - } catch (std::bad_alloc &e) { - LOG_WARN("NO memory to alloc for %llu", size); - return NULL; - } - } else if (pointer && size == 0) { - free(pointer); - return NULL; - } - - // the left case, ptr && size - MemID *pMemID = NULL; - MemID *pLast = NULL; - - MemID *pFreeMemID = NULL; - MemID *pNewMemID = NULL; - - MemID oldMemID; - - bool foundOld = false; - - // use big lock - MUTEX_LOCK(&mMutex); - u64_t hashIndex = MEM_ID_HASH(pointer); - pMemID = mMemIDs[hashIndex]; - while (pMemID) { - if ((char *)pMemID + sizeof(MemID) != pointer) { - // not found - pLast = pMemID; - pMemID = pMemID->mNext; - continue; - } - - // find - foundOld = true; - - // backup old MemID firstly - memcpy(&oldMemID, pMemID, sizeof(MemID)); - - u64_t allocSize = size + sizeof(MemID); - - pNewMemID = (MemID *)::realloc(pMemID, allocSize); - if (pNewMemID == NULL) { - // case 1:no memory to alloc, free the old one - if (pLast == NULL) { - mMemIDs[hashIndex] = pMemID->mNext; - } else { - pLast->mNext = pMemID->mNext; - } - - pFreeMemID = pMemID; - mUsedSize -= oldMemID.mSize; - break; - } - - // set the new pNewMemID - strncpy(pNewMemID->mFile, file, MemID::MEM_FILENAME_LEN - 1); - pNewMemID->mFile[MemID::MEM_FILENAME_LEN - 1] = '\0'; - pNewMemID->mLine = line; - pNewMemID->mSize = size; - - mUsedSize -= oldMemID.mSize; - mUsedSize += size; - - if (pNewMemID == pMemID) { - // case 2: just extension the old memory - pFreeMemID = NULL; - break; - - } else { - // case 3: the old memory can't meet the requirement, alloc new - /** - * Firstly, remove the old one from table - * don't add new before remove the old one - */ - if (pLast == NULL) { - mMemIDs[hashIndex] = pMemID->mNext; - } else { - pLast->mNext = pMemID->mNext; - } - pFreeMemID = pMemID; - - /** - * Secondly, add the new one to table - */ - u64_t newHashIndex = (u64_t)MEM_ID_HASH((char *)pNewMemID + sizeof(MemID)); - - pNewMemID->mNext = mMemIDs[newHashIndex]; - mMemIDs[newHashIndex] = pNewMemID; - - /** - * Third, do memory copy - * to simplify the old logic, copy memory here - */ - memcpy((char *)pNewMemID + sizeof(MemID), (char *)pFreeMemID + sizeof(MemID), pFreeMemID->mSize); - break; - } - } - - MUTEX_UNLOCK(&mMutex); - - if (foundOld == false) { - LOG_WARN("Something is wrong, the old pointer %p isn't found, so alloc new one", pointer); - try { - return malloc(size, file, line, false); - } catch (std::bad_alloc &e) { - LOG_WARN("NO memory to alloc for %llu", size); - return NULL; - }; - } - - if (mVerbose) { - LOG_INFO("Delete %p, file:%s, line:%u, size:%llu", pointer, oldMemID.mFile, oldMemID.mLine, oldMemID.mSize); - } - - if (pFreeMemID) { - ::free(pFreeMemID); - } - - if (pNewMemID) { - - if (mVerbose) { - LOG_INFO("Alloc %p, file:%s, line:%u, size:%llu", - (char *)pNewMemID + sizeof(MemID), - pNewMemID->mFile, - pNewMemID->mLine, - pNewMemID->mSize); - } - return pNewMemID; - } - - return NULL; -} - -void CLMemTrace::free(void *pointer) -{ - if (pointer == NULL) { - LOG_WARN("Free one empty pointer"); - return; - } - - u64_t hashIndex = MEM_ID_HASH(pointer); - - MemID *pMemID = NULL; - MemID *pLast = NULL; - - // use big lock - MUTEX_LOCK(&mMutex); - - pMemID = mMemIDs[hashIndex]; - while (pMemID) { - if ((char *)pMemID + sizeof(MemID) == pointer) { - // find - if (pLast == NULL) { - mMemIDs[hashIndex] = pMemID->mNext; - } else { - pLast->mNext = pMemID->mNext; - } - - mUsedSize -= pMemID->mSize; - break; - } else { - pLast = pMemID; - pMemID = pMemID->mNext; - } - } - MUTEX_UNLOCK(&mMutex); - - if (pMemID) { - - if (mVerbose) { - LOG_INFO("Delete %p, file:%s, line:%u, size:%llu", pointer, pMemID->mFile, pMemID->mLine, pMemID->mSize); - } - ::free(pMemID); - return; - } else { - // not found - LOG_ERROR("Double free for pointer :%p", pointer); - } - - return; -} - -std::new_handler CLMemTrace::getNewHandler() -{ - std::new_handler newHandler = NULL; - MUTEX_LOCK(&mMutex); - - newHandler = std::set_new_handler(0); - std::set_new_handler(newHandler); - - MUTEX_UNLOCK(&mMutex); - - return newHandler; -} - -void CLMemTrace::output() -{ - for (int i = 0; i < MEM_HASHTABLE_SIZE; ++i) { - // Don't lock outside of the loop - // 1. avoid output too long to alloc/free memory - // 2. if LOG_INFO alloc memory, it will leading to dead loop - MUTEX_LOCK(&mMutex); - MemID *ptr = mMemIDs[i]; - if (ptr == NULL) { - MUTEX_UNLOCK(&mMutex); - continue; - } - while (ptr) { - // if LOG_INFO alloc memory, it will easy leading to dead lock - LOG_INFO( - "Exist %p, file:%s, line:%u, size:%llu", (char *)ptr + sizeof(MemID), ptr->mFile, ptr->mLine, ptr->mSize); - ptr = ptr->mNext; - } - - MUTEX_UNLOCK(&mMutex); - } -} - -void *operator new(std::size_t size, const char *file, int line) -{ - return CLMemTrace::malloc(size, file, line, true); -} - -void *operator new[](std::size_t size, const char *file, int line) -{ - return operator new(size, file, line); -} - -void *operator new(std::size_t size) throw(std::bad_alloc) -{ - return operator new(size, "", 0); -} - -void *operator new[](std::size_t size) throw(std::bad_alloc) -{ - return operator new(size); -} - -void *operator new(std::size_t size, const std::nothrow_t &) throw() -{ - void *pointer = NULL; - try { - pointer = operator new(size); - } catch (std::bad_alloc &e) { - LOG_WARN("Failed to alloc memory"); - return NULL; - } - return pointer; -} - -void *operator new[](std::size_t size, const std::nothrow_t &) throw() -{ - void *pointer = NULL; - try { - pointer = operator[] new(size); - } catch (std::bad_alloc &e) { - LOG_WARN("Failed to alloc memory"); - return NULL; - } - return pointer; -} - -void operator delete(void *pointer) -{ - CLMemTrace::free(pointer); -} - -void operator delete[](void *pointer) -{ - operator delete(pointer); -} - -// Some older compilers like Borland C++ Compiler 5.5.1 and Digital Mars -// Compiler 8.29 do not support placement delete operators. -// NO_PLACEMENT_DELETE needs to be defined when using such compilers. -// Also note that in that case memory leakage will occur if an exception -// is thrown in the initialization (constructor) of a dynamically -// created object. -void operator delete(void *pointer, const char *file, int line) -{ - operator delete(pointer); -} - -void operator delete[](void *pointer, const char *file, int line) -{ - operator delete(pointer, file, line); -} - -void operator delete(void *pointer, const std::nothrow_t &) -{ - operator delete(pointer, "", 0); -} - -void operator delete[](void *pointer, const std::nothrow_t &) -{ - operator delete(pointer, std::nothrow); -} - -void *Lcalloc(size_t nmemb, size_t size, const char *file, const int line) -{ - try { - void *point = CLMemTrace::malloc(size * nmemb, file, line, false); - if (point) { - memset(point, 0, size * nmemb); - } - } catch (std::bad_alloc &e) { - LOG_WARN("Failed to alloc memory"); - return NULL; - } - return pointer; -} - -void *Lmalloc(size_t size, const char *file, const int line) -{ - try { - void *point = CLMemTrace::malloc(size, file, line, false); - } catch (std::bad_alloc &e) { - LOG_WARN("Failed to alloc memory"); - return NULL; - } - return pointer; -} - -void Lfree(void *ptr) -{ - CLMemTrace::free(pointer); -} -void *Lrealloc(void *ptr, size_t size, const char *file, const int line) -{ - // simplify the logic - return CLMemTrace::realloc(ptr, size, file, line); -} - -#endif /* MEM_DEBUG */ diff --git a/deps/common/mm/mem.h b/deps/common/mm/mem.h index 3617873..06ee951 100644 --- a/deps/common/mm/mem.h +++ b/deps/common/mm/mem.h @@ -35,8 +35,8 @@ public: const static int MEM_FILENAME_LEN = 32; struct MemID_t *mNext; char mFile[MEM_FILENAME_LEN]; - u64_t mSize; - u32_t mLine; + uint64_t mSize; + uint32_t mLine; } MemID; class CLMemTrace { @@ -69,7 +69,7 @@ protected: const static int MEM_HASHTABLE_SIZE = 16384; static MemID *mMemIDs[MEM_HASHTABLE_SIZE]; - static u64_t mUsedSize; + static uint64_t mUsedSize; static pthread_mutex_t mMutex; static bool mVerbose; }; diff --git a/deps/common/os/os.cpp b/deps/common/os/os.cpp index f17ef49..2b0690b 100644 --- a/deps/common/os/os.cpp +++ b/deps/common/os/os.cpp @@ -21,7 +21,7 @@ See the Mulan PSL v2 for more details. */ namespace common { // Don't care windows -u32_t getCpuNum() +uint32_t getCpuNum() { return std::thread::hardware_concurrency(); } diff --git a/deps/common/os/os.h b/deps/common/os/os.h index 33c653a..9574f66 100644 --- a/deps/common/os/os.h +++ b/deps/common/os/os.h @@ -16,7 +16,7 @@ See the Mulan PSL v2 for more details. */ namespace common { -u32_t getCpuNum(); +uint32_t getCpuNum(); void print_stacktrace(); diff --git a/deps/common/seda/seda_config.cpp b/deps/common/seda/seda_config.cpp index 4dbffd3..cc1a6d7 100644 --- a/deps/common/seda/seda_config.cpp +++ b/deps/common/seda/seda_config.cpp @@ -198,7 +198,7 @@ void SedaConfig::init_event_history() get_event_history_flag() = ev_hist; // set max event hops - u32_t max_event_hops = 100; + uint32_t max_event_hops = 100; key = MAX_EVENT_HISTORY_NUM; it = base_section.find(key); if (it != base_section.end()) { diff --git a/deps/common/seda/seda_config.h b/deps/common/seda/seda_config.h index ed019a8..8b7ccb3 100644 --- a/deps/common/seda/seda_config.h +++ b/deps/common/seda/seda_config.h @@ -227,7 +227,7 @@ inline Stage *SedaConfig::get_stage(const char *stagename) SedaConfig *&get_seda_config(); bool &get_event_history_flag(); -u32_t &get_max_event_hops(); +uint32_t &get_max_event_hops(); } // namespace common #endif //__COMMON_SEDA_SEDA_CONFIG_H__ diff --git a/deps/common/seda/stage_event.cpp b/deps/common/seda/stage_event.cpp index 15929df..e5e7787 100644 --- a/deps/common/seda/stage_event.cpp +++ b/deps/common/seda/stage_event.cpp @@ -161,9 +161,9 @@ bool StageEvent::has_timed_out() } // Accessor function which wraps value for max hops an event is allowed -u32_t &get_max_event_hops() +uint32_t &get_max_event_hops() { - static u32_t max_event_hops = 0; + static uint32_t max_event_hops = 0; return max_event_hops; } diff --git a/deps/common/seda/stage_event.h b/deps/common/seda/stage_event.h index ffb86de..a207264 100644 --- a/deps/common/seda/stage_event.h +++ b/deps/common/seda/stage_event.h @@ -145,7 +145,7 @@ private: UserData *ud_; // user data associated with event by caller bool cb_flag_; // true if this event is a callback std::list *history_; // List of stages which have handled ev - u32_t stage_hops_; // Number of stages which have handled ev + uint32_t stage_hops_; // Number of stages which have handled ev TimeoutInfo *tm_info_; // the timeout info for this event }; @@ -173,6 +173,6 @@ public: }; bool &get_event_history_flag(); -u32_t &get_max_event_hops(); +uint32_t &get_max_event_hops(); } // namespace common diff --git a/deps/common/seda/thread_pool.cpp b/deps/common/seda/thread_pool.cpp index 4ad6b7a..f6a48a5 100644 --- a/deps/common/seda/thread_pool.cpp +++ b/deps/common/seda/thread_pool.cpp @@ -254,7 +254,7 @@ void *Threadpool::run_thread(void *pool_ptr) set_thread_pool_ptr(pool); // this is not portable, but is easier to map to LWP - s64_t threadid = gettid(); + int64_t threadid = gettid(); LOG_INFO("threadid = %llx, threadname = %s", threadid, pool->get_name().c_str()); #ifdef __APPLE__ pthread_setname_np(pool->get_name().c_str()); diff --git a/deps/common/seda/timer_stage.cpp b/deps/common/seda/timer_stage.cpp index c4f69f3..d7808ea 100644 --- a/deps/common/seda/timer_stage.cpp +++ b/deps/common/seda/timer_stage.cpp @@ -73,13 +73,13 @@ void realtime_to_monotonic(const struct timeval *time_RT, struct timeval *time_M time_Mono->tv_usec = time_temp.tv_usec; } -u64_t TimerToken::next_nonce() +uint64_t TimerToken::next_nonce() { - static u64_t nonce_cntr = 0; + static uint64_t nonce_cntr = 0; static pthread_mutex_t tt_mutex = PTHREAD_MUTEX_INITIALIZER; pthread_mutex_lock(&tt_mutex); - u64_t n = nonce_cntr++; + uint64_t n = nonce_cntr++; pthread_mutex_unlock(&tt_mutex); return n; @@ -89,14 +89,14 @@ TimerToken::TimerToken() { struct timeval t; memset(&t, 0, sizeof(struct timeval)); - u64_t n = next_nonce(); + uint64_t n = next_nonce(); set(t, n); return; } TimerToken::TimerToken(const struct timeval &t) { - u64_t n = next_nonce(); + uint64_t n = next_nonce(); set(t, n); return; } @@ -107,7 +107,7 @@ TimerToken::TimerToken(const TimerToken &tt) return; } -void TimerToken::set(const struct timeval &t, u64_t n) +void TimerToken::set(const struct timeval &t, uint64_t n) { memcpy(&time, &t, sizeof(struct timeval)); nonce = n; @@ -119,7 +119,7 @@ const struct timeval &TimerToken::get_time() const return time; } -u64_t TimerToken::get_nonce() const +uint64_t TimerToken::get_nonce() const { return nonce; } @@ -147,7 +147,7 @@ std::string TimerToken::to_string() const return ss.str(); } -TimerRegisterEvent::TimerRegisterEvent(StageEvent *cb, u64_t time_relative_usec) : TimerEvent(), timer_cb_(cb), token_() +TimerRegisterEvent::TimerRegisterEvent(StageEvent *cb, uint64_t time_relative_usec) : TimerEvent(), timer_cb_(cb), token_() { struct timespec timer_spec; clock_gettime(CLOCK_MONOTONIC, &timer_spec); @@ -305,7 +305,7 @@ bool TimerStage::initialize() return (status == 0); } -u32_t TimerStage::get_num_events() +uint32_t TimerStage::get_num_events() { return num_events_; } diff --git a/deps/common/seda/timer_stage.h b/deps/common/seda/timer_stage.h index e769aa9..5f2d445 100644 --- a/deps/common/seda/timer_stage.h +++ b/deps/common/seda/timer_stage.h @@ -45,7 +45,7 @@ public: TimerToken(const struct timeval &t); TimerToken(const TimerToken &tt); const struct timeval &get_time() const; - u64_t get_nonce() const; + uint64_t get_nonce() const; bool operator<(const TimerToken &other) const; TimerToken &operator=(const TimerToken &src); std::string to_string() const; @@ -53,11 +53,11 @@ public: friend bool timer_token_less_than(const TimerToken &tt1, const TimerToken &tt2); private: - void set(const struct timeval &t, u64_t n); - static u64_t next_nonce(); + void set(const struct timeval &t, uint64_t n); + static uint64_t next_nonce(); struct timeval time; - u64_t nonce; + uint64_t nonce; }; /** @@ -104,7 +104,7 @@ public: * The amount of time (in microseconds) before the timer * triggering the callback should fire. */ - TimerRegisterEvent(StageEvent *cb, u64_t time_relative_usec); + TimerRegisterEvent(StageEvent *cb, uint64_t time_relative_usec); /** * \brief Create an event to request the registration of a timer @@ -286,7 +286,7 @@ public: * \brief Return the number of events that have been registered * but not yet triggered or cancelled. */ - u32_t get_num_events(); + uint32_t get_num_events(); protected: TimerStage(const char *tag); @@ -315,7 +315,7 @@ private: pthread_cond_t timer_condv_; bool shutdown_; // true if stage has received the shutdown signal - u32_t num_events_; // the number of timer events currently outstanding + uint32_t num_events_; // the number of timer events currently outstanding pthread_t timer_thread_id_; // thread id of the timer maintenance thread }; diff --git a/deps/common/time/datetime.cpp b/deps/common/time/datetime.cpp index c24515a..fcff9ab 100644 --- a/deps/common/time/datetime.cpp +++ b/deps/common/time/datetime.cpp @@ -345,15 +345,15 @@ void DateTime::parse_duration(std::string dur_str, struct tm &tm_t) std::string Now::unique() { struct timeval tv; - u64_t temp; - static u64_t last_unique = 0; + uint64_t temp; + static uint64_t last_unique = 0; #if defined(LINUX) static pthread_mutex_t mutex = PTHREAD_ERRORCHECK_MUTEX_INITIALIZER_NP; #elif defined(__MACH__) static pthread_mutex_t mutex = PTHREAD_ERRORCHECK_MUTEX_INITIALIZER; #endif gettimeofday(&tv, NULL); - temp = (((u64_t)tv.tv_sec) << 20) + tv.tv_usec; + temp = (((uint64_t)tv.tv_sec) << 20) + tv.tv_usec; pthread_mutex_lock(&mutex); if (temp > last_unique) { // record last timeStamp diff --git a/deps/common/time/datetime.h b/deps/common/time/datetime.h index c04ef64..7e779be 100644 --- a/deps/common/time/datetime.h +++ b/deps/common/time/datetime.h @@ -457,7 +457,7 @@ public: class Now { public: - static inline s64_t sec() + static inline int64_t sec() { struct timeval tv; gettimeofday(&tv, 0); @@ -468,18 +468,18 @@ public: return sec; } - static inline s64_t usec() + static inline int64_t usec() { struct timeval tv; gettimeofday(&tv, 0); - return (s64_t)tv.tv_sec * 1000000 + tv.tv_usec; + return (int64_t)tv.tv_sec * 1000000 + tv.tv_usec; } - static inline s64_t msec() + static inline int64_t msec() { struct timeval tv; gettimeofday(&tv, 0); - s64_t msec = (s64_t)tv.tv_sec * 1000 + tv.tv_usec / 1000; + int64_t msec = (int64_t)tv.tv_sec * 1000 + tv.tv_usec / 1000; if (tv.tv_usec % 1000 >= 500) msec++; return msec; diff --git a/src/observer/CMakeLists.txt b/src/observer/CMakeLists.txt index 3d1af91..393ba7d 100644 --- a/src/observer/CMakeLists.txt +++ b/src/observer/CMakeLists.txt @@ -1,18 +1,10 @@ -PROJECT(observer) -MESSAGE("Begin to build " ${PROJECT_NAME}) -MESSAGE(STATUS "This is PROJECT_BINARY_DIR dir " ${PROJECT_BINARY_DIR}) -MESSAGE(STATUS "This is PROJECT_SOURCE_DIR dir " ${PROJECT_SOURCE_DIR}) - - -#INCLUDE_DIRECTORIES([AFTER|BEFORE] [SYSTEM] dir1 dir2 ...) -INCLUDE_DIRECTORIES(. ${PROJECT_SOURCE_DIR} ${PROJECT_SOURCE_DIR}/../../deps /usr/local/include SYSTEM) -# 父cmake 设置的include_directories 和link_directories并不传导到子cmake里面 -#INCLUDE_DIRECTORIES(BEFORE ${CMAKE_INSTALL_PREFIX}/include) -LINK_DIRECTORIES(/usr/local/lib ${PROJECT_BINARY_DIR}/../../lib) +MESSAGE(STATUS "This is CMAKE_CURRENT_SOURCE_DIR dir " ${CMAKE_CURRENT_SOURCE_DIR}) +INCLUDE_DIRECTORIES(. ${CMAKE_CURRENT_SOURCE_DIR} ${PROJECT_SOURCE_DIR}/deps /usr/local/include) +LINK_DIRECTORIES(/usr/local/lib) FILE(GLOB_RECURSE ALL_SRC *.cpp *.c) -FILE(GLOB MAIN_SRC main.cpp) +SET(MAIN_SRC main.cpp) MESSAGE("MAIN SRC: " ${MAIN_SRC}) FOREACH (F ${ALL_SRC}) @@ -24,23 +16,29 @@ FOREACH (F ${ALL_SRC}) ENDFOREACH (F) -SET(LIBRARIES common pthread dl libevent_pthreads.a libevent.a libjsoncpp.a) +SET(LIBEVENT_STATIC_LINK TRUE) +FIND_PACKAGE(Libevent CONFIG REQUIRED) + +# JsonCpp cannot work correctly with FIND_PACKAGE + +SET(LIBRARIES common pthread dl libevent::core libevent::pthreads libjsoncpp.a) # 指定目标文件位置 -SET(EXECUTABLE_OUTPUT_PATH ${PROJECT_BINARY_DIR}/../../bin) +SET(EXECUTABLE_OUTPUT_PATH ${PROJECT_BINARY_DIR}/bin) MESSAGE("Binary directory:" ${EXECUTABLE_OUTPUT_PATH}) -ADD_EXECUTABLE(${PROJECT_NAME} ${ALL_SRC}) -TARGET_LINK_LIBRARIES(${PROJECT_NAME} ${LIBRARIES}) - -SET(LIBRARY_OUTPUT_PATH ${PROJECT_BINARY_DIR}/../../lib) +SET(LIBRARY_OUTPUT_PATH ${PROJECT_BINARY_DIR}/lib) MESSAGE("Archive directory:" ${LIBRARY_OUTPUT_PATH}) -ADD_LIBRARY(${PROJECT_NAME}_static STATIC ${LIB_SRC}) -SET_TARGET_PROPERTIES(${PROJECT_NAME}_static PROPERTIES OUTPUT_NAME ${PROJECT_NAME}) -TARGET_LINK_LIBRARIES(${PROJECT_NAME}_static ${LIBRARIES}) + +ADD_EXECUTABLE(observer ${MAIN_SRC}) +TARGET_LINK_LIBRARIES(observer observer_static) + +ADD_LIBRARY(observer_static STATIC ${LIB_SRC}) +SET_TARGET_PROPERTIES(observer_static PROPERTIES OUTPUT_NAME observer) +TARGET_LINK_LIBRARIES(observer_static ${LIBRARIES}) # Target 必须在定义 ADD_EXECUTABLE 之后, programs 不受这个限制 # TARGETS和PROGRAMS 的默认权限是OWNER_EXECUTE, GROUP_EXECUTE, 和WORLD_EXECUTE,即755权限, programs 都是处理脚本类 # 类型分为RUNTIME/LIBRARY/ARCHIVE, prog -INSTALL(TARGETS ${PROJECT_NAME} ${PROJECT_NAME}_static +INSTALL(TARGETS observer observer_static RUNTIME DESTINATION bin ARCHIVE DESTINATION lib) diff --git a/src/observer/global_context.cpp b/src/observer/common/global_context.cpp similarity index 95% rename from src/observer/global_context.cpp rename to src/observer/common/global_context.cpp index 6358967..4931a74 100644 --- a/src/observer/global_context.cpp +++ b/src/observer/common/global_context.cpp @@ -12,7 +12,7 @@ See the Mulan PSL v2 for more details. */ // Created by Wangyunlai on 2023/5/29. // -#include "global_context.h" +#include "common/global_context.h" static GlobalContext global_context; diff --git a/src/observer/global_context.h b/src/observer/common/global_context.h similarity index 79% rename from src/observer/global_context.h rename to src/observer/common/global_context.h index 7362d75..6b5920f 100644 --- a/src/observer/global_context.h +++ b/src/observer/common/global_context.h @@ -18,6 +18,10 @@ class BufferPoolManager; class DefaultHandler; class TrxKit; +/** + * @brief 放一些全局对象 + * @details 为了更好的管理全局对象,这里将其封装到一个类中。初始化的过程可以参考 init_global_objects + */ struct GlobalContext { BufferPoolManager *buffer_pool_manager_ = nullptr; diff --git a/src/observer/ini_setting.h b/src/observer/common/ini_setting.h similarity index 87% rename from src/observer/ini_setting.h rename to src/observer/common/ini_setting.h index ea3d126..d9ada86 100644 --- a/src/observer/ini_setting.h +++ b/src/observer/common/ini_setting.h @@ -12,8 +12,7 @@ See the Mulan PSL v2 for more details. */ // Created by Longda on 2021/4/14. // -#ifndef __SRC_OBSERVER_INI_SETTING_H__ -#define __SRC_OBSERVER_INI_SETTING_H__ +#pragma once //! this document is used for ini setting @@ -26,4 +25,3 @@ See the Mulan PSL v2 for more details. */ #define SOCKET_BUFFER_SIZE 8192 #define SESSION_STAGE_NAME "SessionStage" -#endif //__SRC_OBSERVER_INI_SETTING_H__ diff --git a/src/observer/init.cpp b/src/observer/common/init.cpp similarity index 99% rename from src/observer/init.cpp rename to src/observer/common/init.cpp index ccaa0fa..5f92ff8 100644 --- a/src/observer/init.cpp +++ b/src/observer/common/init.cpp @@ -12,9 +12,9 @@ See the Mulan PSL v2 for more details. */ // Created by Longda on 2021/5/3. // -#include "init.h" +#include "common/init.h" -#include "ini_setting.h" +#include "common/ini_setting.h" #include "common/conf/ini.h" #include "common/lang/string.h" #include "common/log/log.h" @@ -222,7 +222,6 @@ int init(ProcessParam *process_param) // Initialize global variables before enter multi-thread mode // to avoid race condition - theSwVersion(); // Read Configuration files rc = get_properties()->load(process_param->get_conf()); diff --git a/src/observer/init.h b/src/observer/common/init.h similarity index 89% rename from src/observer/init.h rename to src/observer/common/init.h index 8451864..2993b23 100644 --- a/src/observer/init.h +++ b/src/observer/common/init.h @@ -12,13 +12,10 @@ See the Mulan PSL v2 for more details. */ // Created by Longda on 2021/5/3. // -#ifndef __OBSERVER_INIT_H__ -#define __OBSERVER_INIT_H__ +#pragma once #include "common/os/process_param.h" #include "common/conf/ini.h" int init(common::ProcessParam *processParam); void cleanup(); - -#endif //__OBSERVER_INIT_H__ diff --git a/src/observer/rc.cpp b/src/observer/common/rc.cpp similarity index 97% rename from src/observer/rc.cpp rename to src/observer/common/rc.cpp index d1801ac..313a928 100644 --- a/src/observer/rc.cpp +++ b/src/observer/common/rc.cpp @@ -12,7 +12,7 @@ See the Mulan PSL v2 for more details. */ // Created by Wangyunlai on 2021/5/14. // -#include "rc.h" +#include "common/rc.h" const char *strrc(RC rc) { diff --git a/src/observer/rc.h b/src/observer/common/rc.h similarity index 100% rename from src/observer/rc.h rename to src/observer/common/rc.h diff --git a/src/observer/defs.h b/src/observer/common/types.h similarity index 99% rename from src/observer/defs.h rename to src/observer/common/types.h index 3d55e85..2860647 100644 --- a/src/observer/defs.h +++ b/src/observer/common/types.h @@ -25,4 +25,3 @@ using SlotNum = int32_t; /// LSN for log sequence number using LSN = int32_t; - diff --git a/src/observer/main.cpp b/src/observer/main.cpp index a48aad3..601d360 100644 --- a/src/observer/main.cpp +++ b/src/observer/main.cpp @@ -19,8 +19,8 @@ See the Mulan PSL v2 for more details. */ #include #include -#include "init.h" -#include "ini_setting.h" +#include "common/init.h" +#include "common/ini_setting.h" #include "common/os/process.h" #include "common/os/signal.h" #include "net/server.h" diff --git a/src/observer/net/communicator.h b/src/observer/net/communicator.h index 5511de5..774a424 100644 --- a/src/observer/net/communicator.h +++ b/src/observer/net/communicator.h @@ -16,7 +16,7 @@ See the Mulan PSL v2 for more details. */ #include #include -#include "rc.h" +#include "common/rc.h" struct ConnectionContext; class SessionEvent; diff --git a/src/observer/net/server.cpp b/src/observer/net/server.cpp index 0fe3474..aaf990f 100644 --- a/src/observer/net/server.cpp +++ b/src/observer/net/server.cpp @@ -34,7 +34,7 @@ See the Mulan PSL v2 for more details. */ #include "common/seda/seda_config.h" #include "event/session_event.h" #include "session/session.h" -#include "ini_setting.h" +#include "common/ini_setting.h" #include "net/communicator.h" #include diff --git a/src/observer/session/session.cpp b/src/observer/session/session.cpp index e6e25e5..8b95461 100644 --- a/src/observer/session/session.cpp +++ b/src/observer/session/session.cpp @@ -14,9 +14,9 @@ See the Mulan PSL v2 for more details. */ #include "session/session.h" #include "storage/trx/trx.h" -#include "storage/common/db.h" +#include "storage/db/db.h" #include "storage/default/default_handler.h" -#include "global_context.h" +#include "common/global_context.h" Session &Session::default_session() { diff --git a/src/observer/sql/executor/command_executor.cpp b/src/observer/sql/executor/command_executor.cpp index 846da03..541d5f6 100644 --- a/src/observer/sql/executor/command_executor.cpp +++ b/src/observer/sql/executor/command_executor.cpp @@ -13,19 +13,62 @@ See the Mulan PSL v2 for more details. */ // #include "sql/executor/command_executor.h" -#include "session/session.h" +#include "event/sql_event.h" #include "sql/stmt/stmt.h" #include "sql/executor/create_index_executor.h" +#include "sql/executor/create_table_executor.h" +#include "sql/executor/desc_table_executor.h" +#include "sql/executor/help_executor.h" +#include "sql/executor/show_tables_executor.h" +#include "sql/executor/trx_begin_executor.h" +#include "sql/executor/trx_end_executor.h" #include "common/log/log.h" -RC CommandExecutor::execute(Session *session, Stmt *stmt) +RC CommandExecutor::execute(SQLStageEvent *sql_event) { + Stmt *stmt = sql_event->stmt(); + switch (stmt->type()) { case StmtType::CREATE_INDEX: { CreateIndexExecutor executor; - return executor.execute(session, stmt); + return executor.execute(sql_event); + } break; + + case StmtType::CREATE_TABLE: { + CreateTableExecutor executor; + return executor.execute(sql_event); } break; + case StmtType::DESC_TABLE: { + DescTableExecutor executor; + return executor.execute(sql_event); + } + + case StmtType::HELP: { + HelpExecutor executor; + return executor.execute(sql_event); + } + + case StmtType::SHOW_TABLES: { + ShowTablesExecutor executor; + return executor.execute(sql_event); + } + + case StmtType::BEGIN: { + TrxBeginExecutor executor; + return executor.execute(sql_event); + } + + case StmtType::COMMIT: + case StmtType::ROLLBACK: { + TrxEndExecutor executor; + return executor.execute(sql_event); + } + + case StmtType::EXIT: { + return RC::SUCCESS; + } + default: { LOG_ERROR("unknown command: %d", static_cast(stmt->type())); return RC::UNIMPLENMENT; diff --git a/src/observer/sql/executor/command_executor.h b/src/observer/sql/executor/command_executor.h index 4ea7932..413bd03 100644 --- a/src/observer/sql/executor/command_executor.h +++ b/src/observer/sql/executor/command_executor.h @@ -14,10 +14,10 @@ See the Mulan PSL v2 for more details. */ #pragma once -#include "rc.h" +#include "common/rc.h" + +class SQLStageEvent; -class Session; -class Stmt; class CommandExecutor { @@ -25,5 +25,5 @@ public: CommandExecutor() = default; virtual ~CommandExecutor() = default; - RC execute(Session *session, Stmt *stmt); + RC execute(SQLStageEvent *sql_event); }; diff --git a/src/observer/sql/executor/create_index_executor.cpp b/src/observer/sql/executor/create_index_executor.cpp index 98c066e..3579770 100644 --- a/src/observer/sql/executor/create_index_executor.cpp +++ b/src/observer/sql/executor/create_index_executor.cpp @@ -14,12 +14,16 @@ See the Mulan PSL v2 for more details. */ #include "sql/executor/create_index_executor.h" #include "sql/stmt/create_index_stmt.h" +#include "event/sql_event.h" +#include "event/session_event.h" #include "session/session.h" #include "common/log/log.h" -#include "storage/common/table.h" +#include "storage/table/table.h" -RC CreateIndexExecutor::execute(Session *session, Stmt *stmt) +RC CreateIndexExecutor::execute(SQLStageEvent *sql_event) { + Stmt *stmt = sql_event->stmt(); + Session *session = sql_event->session_event()->session(); ASSERT(stmt->type() == StmtType::CREATE_INDEX, "create index executor can not run this command: %d", static_cast(stmt->type())); diff --git a/src/observer/sql/executor/create_index_executor.h b/src/observer/sql/executor/create_index_executor.h index 8684ed4..9ce8657 100644 --- a/src/observer/sql/executor/create_index_executor.h +++ b/src/observer/sql/executor/create_index_executor.h @@ -14,10 +14,9 @@ See the Mulan PSL v2 for more details. */ #pragma once -#include "rc.h" +#include "common/rc.h" -class Session; -class Stmt; +class SQLStageEvent; class CreateIndexExecutor { @@ -25,5 +24,5 @@ public: CreateIndexExecutor() = default; virtual ~CreateIndexExecutor() = default; - RC execute(Session *session, Stmt *stmt); + RC execute(SQLStageEvent *sql_event); }; \ No newline at end of file diff --git a/src/observer/sql/executor/create_table_executor.cpp b/src/observer/sql/executor/create_table_executor.cpp new file mode 100644 index 0000000..8922afe --- /dev/null +++ b/src/observer/sql/executor/create_table_executor.cpp @@ -0,0 +1,40 @@ +/* Copyright (c) 2021 OceanBase and/or its affiliates. All rights reserved. +miniob is licensed under Mulan PSL v2. +You can use this software according to the terms and conditions of the Mulan PSL v2. +You may obtain a copy of Mulan PSL v2 at: + http://license.coscl.org.cn/MulanPSL2 +THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, +EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, +MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. +See the Mulan PSL v2 for more details. */ + +// +// Created by Wangyunlai on 2023/6/13. +// + +#include "sql/executor/create_table_executor.h" + +#include "session/session.h" +#include "common/log/log.h" +#include "storage/table/table.h" +#include "sql/stmt/create_table_stmt.h" +#include "event/sql_event.h" +#include "event/session_event.h" +#include "storage/db/db.h" + +RC CreateTableExecutor::execute(SQLStageEvent *sql_event) +{ + Stmt *stmt = sql_event->stmt(); + Session *session = sql_event->session_event()->session(); + ASSERT(stmt->type() == StmtType::CREATE_TABLE, + "create table executor can not run this command: %d", static_cast(stmt->type())); + + CreateTableStmt *create_table_stmt = static_cast(stmt); + + const int attribute_count = static_cast(create_table_stmt->attr_infos().size()); + + const char *table_name = create_table_stmt->table_name().c_str(); + RC rc = session->get_current_db()->create_table(table_name, attribute_count, create_table_stmt->attr_infos().data()); + + return rc; +} \ No newline at end of file diff --git a/src/observer/sql/executor/create_table_executor.h b/src/observer/sql/executor/create_table_executor.h new file mode 100644 index 0000000..10cf09b --- /dev/null +++ b/src/observer/sql/executor/create_table_executor.h @@ -0,0 +1,28 @@ +/* Copyright (c) 2021 OceanBase and/or its affiliates. All rights reserved. +miniob is licensed under Mulan PSL v2. +You can use this software according to the terms and conditions of the Mulan PSL v2. +You may obtain a copy of Mulan PSL v2 at: + http://license.coscl.org.cn/MulanPSL2 +THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, +EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, +MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. +See the Mulan PSL v2 for more details. */ + +// +// Created by Wangyunlai on 2023/6/13. +// + +#pragma once + +#include "common/rc.h" + +class SQLStageEvent; + +class CreateTableExecutor +{ +public: + CreateTableExecutor() = default; + virtual ~CreateTableExecutor() = default; + + RC execute(SQLStageEvent *sql_event); +}; \ No newline at end of file diff --git a/src/observer/sql/executor/desc_table_executor.cpp b/src/observer/sql/executor/desc_table_executor.cpp new file mode 100644 index 0000000..2feef24 --- /dev/null +++ b/src/observer/sql/executor/desc_table_executor.cpp @@ -0,0 +1,69 @@ +/* Copyright (c) 2021 OceanBase and/or its affiliates. All rights reserved. +miniob is licensed under Mulan PSL v2. +You can use this software according to the terms and conditions of the Mulan PSL v2. +You may obtain a copy of Mulan PSL v2 at: + http://license.coscl.org.cn/MulanPSL2 +THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, +EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, +MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. +See the Mulan PSL v2 for more details. */ + +// +// Created by Wangyunlai on 2023/6/14. +// + +#include + +#include "sql/executor/desc_table_executor.h" + +#include "session/session.h" +#include "event/sql_event.h" +#include "event/session_event.h" +#include "common/log/log.h" +#include "storage/table/table.h" +#include "sql/stmt/desc_table_stmt.h" +#include "storage/db/db.h" +#include "sql/operator/string_list_physical_operator.h" + +using namespace std; + +RC DescTableExecutor::execute(SQLStageEvent *sql_event) +{ + Stmt *stmt = sql_event->stmt(); + SessionEvent *session_event = sql_event->session_event(); + Session *session = session_event->session(); + ASSERT(stmt->type() == StmtType::DESC_TABLE, + "desc table executor can not run this command: %d", static_cast(stmt->type())); + + DescTableStmt *desc_table_stmt = static_cast(stmt); + + SqlResult *sql_result = session_event->sql_result(); + + const char *table_name = desc_table_stmt->table_name().c_str(); + + Db *db = session->get_current_db(); + Table *table = db->find_table(table_name); + if (table != nullptr) { + + TupleSchema tuple_schema; + tuple_schema.append_cell(TupleCellSpec("", "Field", "Field")); + tuple_schema.append_cell(TupleCellSpec("", "Type", "Type")); + tuple_schema.append_cell(TupleCellSpec("", "Length", "Length")); + + sql_result->set_tuple_schema(tuple_schema); + + auto oper = new StringListPhysicalOperator; + const TableMeta &table_meta = table->table_meta(); + for (int i = table_meta.sys_field_num(); i < table_meta.field_num(); i++) { + const FieldMeta *field_meta = table_meta.field(i); + oper->append({field_meta->name(), attr_type_to_string(field_meta->type()), std::to_string(field_meta->len())}); + } + + sql_result->set_operator(unique_ptr(oper)); + } else { + + sql_result->set_return_code(RC::SCHEMA_TABLE_NOT_EXIST); + sql_result->set_state_string("Table not exists"); + } + return RC::SCHEMA_TABLE_NOT_EXIST; +} \ No newline at end of file diff --git a/unittest/rc_test.cpp b/src/observer/sql/executor/desc_table_executor.h similarity index 68% rename from unittest/rc_test.cpp rename to src/observer/sql/executor/desc_table_executor.h index 50e8387..6a547b2 100644 --- a/unittest/rc_test.cpp +++ b/src/observer/sql/executor/desc_table_executor.h @@ -9,12 +9,20 @@ MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. See the Mulan PSL v2 for more details. */ // -// Created by Longda on 2021/5/3. +// Created by Wangyunlai on 2023/6/14. // -#include "rc.h" -#include -int main(int argc, char **argv) +#pragma once + +#include "common/rc.h" + +class SQLStageEvent; + +class DescTableExecutor { +public: + DescTableExecutor() = default; + virtual ~DescTableExecutor() = default; -} \ No newline at end of file + RC execute(SQLStageEvent *sql_event); +}; \ No newline at end of file diff --git a/src/observer/sql/executor/execute_stage.cpp b/src/observer/sql/executor/execute_stage.cpp index ab60abb..5638775 100644 --- a/src/observer/sql/executor/execute_stage.cpp +++ b/src/observer/sql/executor/execute_stage.cpp @@ -14,38 +14,17 @@ See the Mulan PSL v2 for more details. */ #include #include -#include -#include "execute_stage.h" +#include "sql/executor/execute_stage.h" -#include "common/io/io.h" #include "common/log/log.h" -#include "common/lang/defer.h" -#include "common/seda/timer_stage.h" -#include "common/lang/string.h" #include "session/session.h" #include "event/storage_event.h" #include "event/sql_event.h" #include "event/session_event.h" -#include "sql/expr/tuple.h" -#include "sql/operator/table_scan_physical_operator.h" -#include "sql/operator/index_scan_physical_operator.h" -#include "sql/operator/predicate_physical_operator.h" -#include "sql/operator/delete_physical_operator.h" -#include "sql/operator/project_physical_operator.h" -#include "sql/operator/string_list_physical_operator.h" #include "sql/stmt/stmt.h" #include "sql/stmt/select_stmt.h" -#include "sql/stmt/update_stmt.h" -#include "sql/stmt/delete_stmt.h" -#include "sql/stmt/insert_stmt.h" -#include "sql/stmt/filter_stmt.h" -#include "storage/common/table.h" -#include "storage/common/field.h" -#include "storage/index/index.h" #include "storage/default/default_handler.h" -#include "storage/common/condition_filter.h" -#include "storage/trx/trx.h" #include "sql/executor/command_executor.h" using namespace std; @@ -117,7 +96,6 @@ void ExecuteStage::callback_event(StageEvent *event, CallbackContext *context) RC ExecuteStage::handle_request(common::StageEvent *event) { SQLStageEvent *sql_event = static_cast(event); - SqlResult *sql_result = sql_event->session_event()->sql_result(); const unique_ptr &physical_operator = sql_event->physical_operator(); if (physical_operator != nullptr) { @@ -125,74 +103,14 @@ RC ExecuteStage::handle_request(common::StageEvent *event) } SessionEvent *session_event = sql_event->session_event(); - Session *session = session_event->session(); - Command *sql = sql_event->command().get(); Stmt *stmt = sql_event->stmt(); if (stmt != nullptr) { CommandExecutor command_executor; - RC rc = RC::UNIMPLENMENT; - switch (stmt->type()) { - case StmtType::UPDATE: { - // do_update((UpdateStmt *)stmt, session_event); - } break; - case StmtType::CREATE_INDEX: { - rc = command_executor.execute(session, stmt); - } break; - default: { - LOG_WARN("should not happen. please implement this type:%d", stmt->type()); - } break; - } + RC rc = command_executor.execute(sql_event); session_event->sql_result()->set_return_code(rc); } else { - // TODO create command to execute these sql - switch (sql->flag) { - case SCF_HELP: { - do_help(sql_event); - } break; - case SCF_CREATE_TABLE: { - do_create_table(sql_event); - } break; - - case SCF_SHOW_TABLES: { - do_show_tables(sql_event); - } break; - case SCF_DESC_TABLE: { - do_desc_table(sql_event); - } break; - - case SCF_DROP_TABLE: - case SCF_DROP_INDEX: - case SCF_LOAD_DATA: { - default_storage_stage_->handle_event(event); - } break; - case SCF_BEGIN: { - do_begin(sql_event); - } break; - case SCF_COMMIT: { - do_commit(sql_event); - } break; - - case SCF_ROLLBACK: { - Trx *trx = session_event->session()->current_trx(); - RC rc = trx->rollback(); - session->set_trx_multi_operation_mode(false); - - sql_result->set_return_code(rc); - } break; - - case SCF_EXIT: { - // do nothing - sql_result->set_return_code(RC::SUCCESS); - } break; - - default: { - LOG_ERROR("Unsupported command=%d\n", sql->flag); - - sql_result->set_return_code(RC::UNIMPLENMENT); - sql_result->set_state_string("Unsupported command"); - } - } + default_storage_stage_->handle_event(event); } return RC::SUCCESS; } @@ -236,145 +154,3 @@ RC ExecuteStage::handle_request_with_physical_operator(SQLStageEvent *sql_event) return rc; } -void end_trx_if_need(Session *session, Trx *trx, bool all_right) -{ - if (!session->is_trx_multi_operation_mode()) { - if (all_right) { - trx->commit(); - } else { - trx->rollback(); - } - } -} - -RC ExecuteStage::do_help(SQLStageEvent *sql_event) -{ - const char *strings[] = {"show tables;", - "desc `table name`;", - "create table `table name` (`column name` `column type`, ...);", - "create index `index name` on `table` (`column`);", - "insert into `table` values(`value1`,`value2`);", - "update `table` set column=value [where `column`=`value`];", - "delete from `table` [where `column`=`value`];", - "select [ * | `columns` ] from `table`;"}; - - auto oper = new StringListPhysicalOperator(); - for (size_t i = 0; i < sizeof(strings) / sizeof(strings[0]); i++) { - oper->append(strings[i]); - } - - SqlResult *sql_result = sql_event->session_event()->sql_result(); - - TupleSchema schema; - schema.append_cell("Commands"); - - sql_result->set_tuple_schema(schema); - sql_result->set_operator(unique_ptr(oper)); - - return RC::SUCCESS; -} - -RC ExecuteStage::do_create_table(SQLStageEvent *sql_event) -{ - SessionEvent *session_event = sql_event->session_event(); - Db *db = session_event->session()->get_current_db(); - - const CreateTable &create_table = sql_event->command()->create_table; - const int attribute_count = static_cast(create_table.attr_infos.size()); - - RC rc = db->create_table(create_table.relation_name.c_str(), attribute_count, create_table.attr_infos.data()); - - SqlResult *sql_result = session_event->sql_result(); - sql_result->set_return_code(rc); - return rc; -} - -RC ExecuteStage::do_show_tables(SQLStageEvent *sql_event) -{ - SqlResult *sql_result = sql_event->session_event()->sql_result(); - SessionEvent *session_event = sql_event->session_event(); - - Db *db = session_event->session()->get_current_db(); - - std::vector all_tables; - db->all_tables(all_tables); - - TupleSchema tuple_schema; - tuple_schema.append_cell(TupleCellSpec("", "Tables_in_SYS", "Tables_in_SYS")); - sql_result->set_tuple_schema(tuple_schema); - - auto oper = new StringListPhysicalOperator; - for (const std::string &s : all_tables) { - oper->append(s); - } - - sql_result->set_operator(std::unique_ptr(oper)); - return RC::SUCCESS; -} - -RC ExecuteStage::do_desc_table(SQLStageEvent *sql_event) -{ - SqlResult *sql_result = sql_event->session_event()->sql_result(); - - Command *cmd = sql_event->command().get(); - const char *table_name = cmd->desc_table.relation_name.c_str(); - - Db *db = sql_event->session_event()->session()->get_current_db(); - Table *table = db->find_table(table_name); - if (table != nullptr) { - - TupleSchema tuple_schema; - tuple_schema.append_cell(TupleCellSpec("", "Field", "Field")); - tuple_schema.append_cell(TupleCellSpec("", "Type", "Type")); - tuple_schema.append_cell(TupleCellSpec("", "Length", "Length")); - - // TODO add Key - sql_result->set_tuple_schema(tuple_schema); - - auto oper = new StringListPhysicalOperator; - const TableMeta &table_meta = table->table_meta(); - for (int i = table_meta.sys_field_num(); i < table_meta.field_num(); i++) { - const FieldMeta *field_meta = table_meta.field(i); - oper->append({field_meta->name(), attr_type_to_string(field_meta->type()), std::to_string(field_meta->len())}); - } - - sql_result->set_operator(unique_ptr(oper)); - } else { - - sql_result->set_return_code(RC::SCHEMA_TABLE_NOT_EXIST); - sql_result->set_state_string("Table not exists"); - } - return RC::SUCCESS; -} - -RC ExecuteStage::do_begin(SQLStageEvent *sql_event) -{ - SessionEvent *session_event = sql_event->session_event(); - SqlResult *sql_result = session_event->sql_result(); - - Session *session = session_event->session(); - Trx *trx = session->current_trx(); - - session->set_trx_multi_operation_mode(true); - - RC rc = trx->start_if_need(); - sql_result->set_return_code(rc); - - return rc; -} - -RC ExecuteStage::do_commit(SQLStageEvent *sql_event) -{ - SessionEvent *session_event = sql_event->session_event(); - SqlResult *sql_result = session_event->sql_result(); - - Session *session = session_event->session(); - session->set_trx_multi_operation_mode(false); - - Trx *trx = session->current_trx(); - - RC rc = trx->commit(); - sql_result->set_return_code(rc); - - return rc; -} diff --git a/src/observer/sql/executor/execute_stage.h b/src/observer/sql/executor/execute_stage.h index 95f3e6a..b7826e2 100644 --- a/src/observer/sql/executor/execute_stage.h +++ b/src/observer/sql/executor/execute_stage.h @@ -16,13 +16,19 @@ See the Mulan PSL v2 for more details. */ #include "common/seda/stage.h" #include "sql/parser/parse.h" -#include "rc.h" +#include "common/rc.h" class SQLStageEvent; class SessionEvent; class SelectStmt; -class ExecuteStage : public common::Stage { +/** + * @brief 执行SQL语句的Stage,包括DML和DDL + * @details 根据前面阶段生成的结果,有些语句会生成执行计划,有些不会。 + * 整体上分为两类,带执行计划的,或者 @class CommandExecutor 可以直接执行的。 + */ +class ExecuteStage : public common::Stage +{ public: virtual ~ExecuteStage(); static Stage *make_stage(const std::string &tag); @@ -40,14 +46,6 @@ protected: RC handle_request(common::StageEvent *event); RC handle_request_with_physical_operator(SQLStageEvent *sql_event); - RC do_help(SQLStageEvent *session_event); - RC do_create_table(SQLStageEvent *sql_event); - RC do_show_tables(SQLStageEvent *sql_event); - RC do_desc_table(SQLStageEvent *sql_event); - RC do_begin(SQLStageEvent *sql_event); - RC do_commit(SQLStageEvent *sql_event); - -protected: private: Stage *default_storage_stage_ = nullptr; Stage *mem_storage_stage_ = nullptr; diff --git a/src/observer/sql/executor/help_executor.h b/src/observer/sql/executor/help_executor.h new file mode 100644 index 0000000..af8864f --- /dev/null +++ b/src/observer/sql/executor/help_executor.h @@ -0,0 +1,58 @@ +/* Copyright (c) 2021 OceanBase and/or its affiliates. All rights reserved. +miniob is licensed under Mulan PSL v2. +You can use this software according to the terms and conditions of the Mulan PSL v2. +You may obtain a copy of Mulan PSL v2 at: + http://license.coscl.org.cn/MulanPSL2 +THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, +EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, +MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. +See the Mulan PSL v2 for more details. */ + +// +// Created by Wangyunlai on 2023/6/14. +// + +#pragma once + +#include "common/rc.h" +#include "sql/operator/string_list_physical_operator.h" +#include "event/sql_event.h" +#include "event/session_event.h" +#include "sql/executor/sql_result.h" +#include "session/session.h" + +class HelpExecutor +{ +public: + HelpExecutor() = default; + virtual ~HelpExecutor() = default; + + RC execute(SQLStageEvent *sql_event) + { + const char *strings[] = { + "show tables;", + "desc `table name`;", + "create table `table name` (`column name` `column type`, ...);", + "create index `index name` on `table` (`column`);", + "insert into `table` values(`value1`,`value2`);", + "update `table` set column=value [where `column`=`value`];", + "delete from `table` [where `column`=`value`];", + "select [ * | `columns` ] from `table`;" + }; + + auto oper = new StringListPhysicalOperator(); + for (size_t i = 0; i < sizeof(strings) / sizeof(strings[0]); i++) { + oper->append(strings[i]); + } + + SqlResult *sql_result = sql_event->session_event()->sql_result(); + + TupleSchema schema; + schema.append_cell("Commands"); + + sql_result->set_tuple_schema(schema); + sql_result->set_operator(std::unique_ptr(oper)); + + return RC::SUCCESS; + } +}; \ No newline at end of file diff --git a/src/observer/sql/executor/show_tables_executor.h b/src/observer/sql/executor/show_tables_executor.h new file mode 100644 index 0000000..5a7599c --- /dev/null +++ b/src/observer/sql/executor/show_tables_executor.h @@ -0,0 +1,53 @@ +/* Copyright (c) 2021 OceanBase and/or its affiliates. All rights reserved. +miniob is licensed under Mulan PSL v2. +You can use this software according to the terms and conditions of the Mulan PSL v2. +You may obtain a copy of Mulan PSL v2 at: + http://license.coscl.org.cn/MulanPSL2 +THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, +EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, +MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. +See the Mulan PSL v2 for more details. */ + +// +// Created by Wangyunlai on 2023/6/14. +// + +#pragma once + +#include "common/rc.h" +#include "sql/operator/string_list_physical_operator.h" +#include "event/sql_event.h" +#include "event/session_event.h" +#include "sql/executor/sql_result.h" +#include "session/session.h" +#include "storage/db/db.h" + +class ShowTablesExecutor +{ +public: + ShowTablesExecutor() = default; + virtual ~ShowTablesExecutor() = default; + + RC execute(SQLStageEvent *sql_event) + { + SqlResult *sql_result = sql_event->session_event()->sql_result(); + SessionEvent *session_event = sql_event->session_event(); + + Db *db = session_event->session()->get_current_db(); + + std::vector all_tables; + db->all_tables(all_tables); + + TupleSchema tuple_schema; + tuple_schema.append_cell(TupleCellSpec("", "Tables_in_SYS", "Tables_in_SYS")); + sql_result->set_tuple_schema(tuple_schema); + + auto oper = new StringListPhysicalOperator; + for (const std::string &s : all_tables) { + oper->append(s); + } + + sql_result->set_operator(std::unique_ptr(oper)); + return RC::SUCCESS; + } +}; \ No newline at end of file diff --git a/src/observer/sql/executor/sql_result.cpp b/src/observer/sql/executor/sql_result.cpp index ce532fd..5e565d3 100644 --- a/src/observer/sql/executor/sql_result.cpp +++ b/src/observer/sql/executor/sql_result.cpp @@ -12,7 +12,7 @@ See the Mulan PSL v2 for more details. */ // Created by WangYunlai on 2022/11/18. // -#include "rc.h" +#include "common/rc.h" #include "sql/executor/sql_result.h" #include "session/session.h" #include "storage/trx/trx.h" diff --git a/src/observer/sql/executor/trx_begin_executor.h b/src/observer/sql/executor/trx_begin_executor.h new file mode 100644 index 0000000..90cf4a0 --- /dev/null +++ b/src/observer/sql/executor/trx_begin_executor.h @@ -0,0 +1,42 @@ +/* Copyright (c) 2021 OceanBase and/or its affiliates. All rights reserved. +miniob is licensed under Mulan PSL v2. +You can use this software according to the terms and conditions of the Mulan PSL v2. +You may obtain a copy of Mulan PSL v2 at: + http://license.coscl.org.cn/MulanPSL2 +THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, +EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, +MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. +See the Mulan PSL v2 for more details. */ + +// +// Created by Wangyunlai on 2023/6/14. +// + +#pragma once + +#include "common/rc.h" +#include "sql/operator/string_list_physical_operator.h" +#include "event/sql_event.h" +#include "event/session_event.h" +#include "sql/executor/sql_result.h" +#include "session/session.h" +#include "storage/trx/trx.h" + +class TrxBeginExecutor +{ +public: + TrxBeginExecutor() = default; + virtual ~TrxBeginExecutor() = default; + + RC execute(SQLStageEvent *sql_event) + { + SessionEvent *session_event = sql_event->session_event(); + + Session *session = session_event->session(); + Trx *trx = session->current_trx(); + + session->set_trx_multi_operation_mode(true); + + return trx->start_if_need(); + } +}; \ No newline at end of file diff --git a/src/observer/sql/executor/trx_end_executor.h b/src/observer/sql/executor/trx_end_executor.h new file mode 100644 index 0000000..e7bb9c6 --- /dev/null +++ b/src/observer/sql/executor/trx_end_executor.h @@ -0,0 +1,47 @@ +/* Copyright (c) 2021 OceanBase and/or its affiliates. All rights reserved. +miniob is licensed under Mulan PSL v2. +You can use this software according to the terms and conditions of the Mulan PSL v2. +You may obtain a copy of Mulan PSL v2 at: + http://license.coscl.org.cn/MulanPSL2 +THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, +EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, +MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. +See the Mulan PSL v2 for more details. */ + +// +// Created by Wangyunlai on 2023/6/14. +// + +#pragma once + +#include "common/rc.h" +#include "event/sql_event.h" +#include "event/session_event.h" +#include "sql/executor/sql_result.h" +#include "session/session.h" +#include "storage/trx/trx.h" +#include "sql/stmt/stmt.h" + +class TrxEndExecutor +{ +public: + TrxEndExecutor() = default; + virtual ~TrxEndExecutor() = default; + + RC execute(SQLStageEvent *sql_event) + { + Stmt *stmt = sql_event->stmt(); + SessionEvent *session_event = sql_event->session_event(); + + Session *session = session_event->session(); + session->set_trx_multi_operation_mode(false); + Trx *trx = session->current_trx(); + + if (stmt->type() == StmtType::COMMIT) { + return trx->commit(); + } + else { + return trx->rollback(); + } + } +}; \ No newline at end of file diff --git a/src/observer/sql/expr/expression.h b/src/observer/sql/expr/expression.h index 93bc2f9..8be6fb8 100644 --- a/src/observer/sql/expr/expression.h +++ b/src/observer/sql/expr/expression.h @@ -16,7 +16,7 @@ See the Mulan PSL v2 for more details. */ #include #include -#include "storage/common/field.h" +#include "storage/field/field.h" #include "sql/expr/tuple_cell.h" #include "common/log/log.h" diff --git a/src/observer/sql/expr/tuple_cell.cpp b/src/observer/sql/expr/tuple_cell.cpp index f199de2..152e25a 100644 --- a/src/observer/sql/expr/tuple_cell.cpp +++ b/src/observer/sql/expr/tuple_cell.cpp @@ -14,7 +14,7 @@ See the Mulan PSL v2 for more details. */ #include #include "sql/expr/tuple_cell.h" -#include "storage/common/field.h" +#include "storage/field/field.h" #include "common/log/log.h" #include "common/lang/comparator.h" #include "common/lang/string.h" diff --git a/src/observer/sql/expr/tuple_cell.h b/src/observer/sql/expr/tuple_cell.h index 2c101cc..8cc48c3 100644 --- a/src/observer/sql/expr/tuple_cell.h +++ b/src/observer/sql/expr/tuple_cell.h @@ -15,8 +15,8 @@ See the Mulan PSL v2 for more details. */ #pragma once #include -#include "storage/common/table.h" -#include "storage/common/field_meta.h" +#include "storage/table/table.h" +#include "storage/field/field_meta.h" class TupleCellSpec { public: diff --git a/src/observer/sql/operator/delete_physical_operator.cpp b/src/observer/sql/operator/delete_physical_operator.cpp index 22d2dc9..3ebb35d 100644 --- a/src/observer/sql/operator/delete_physical_operator.cpp +++ b/src/observer/sql/operator/delete_physical_operator.cpp @@ -15,7 +15,7 @@ See the Mulan PSL v2 for more details. */ #include "common/log/log.h" #include "sql/operator/delete_physical_operator.h" #include "storage/record/record.h" -#include "storage/common/table.h" +#include "storage/table/table.h" #include "storage/trx/trx.h" #include "sql/stmt/delete_stmt.h" diff --git a/src/observer/sql/operator/delete_physical_operator.h b/src/observer/sql/operator/delete_physical_operator.h index b210034..56d05a1 100644 --- a/src/observer/sql/operator/delete_physical_operator.h +++ b/src/observer/sql/operator/delete_physical_operator.h @@ -15,7 +15,6 @@ See the Mulan PSL v2 for more details. */ #pragma once #include "sql/operator/physical_operator.h" -#include "rc.h" class Trx; class DeleteStmt; diff --git a/src/observer/sql/operator/insert_physical_operator.cpp b/src/observer/sql/operator/insert_physical_operator.cpp index 2bb1508..0abfc5c 100644 --- a/src/observer/sql/operator/insert_physical_operator.cpp +++ b/src/observer/sql/operator/insert_physical_operator.cpp @@ -14,9 +14,8 @@ See the Mulan PSL v2 for more details. */ #include "sql/operator/insert_physical_operator.h" #include "sql/stmt/insert_stmt.h" -#include "storage/common/table.h" +#include "storage/table/table.h" #include "storage/trx/trx.h" -#include "rc.h" using namespace std; diff --git a/src/observer/sql/operator/insert_physical_operator.h b/src/observer/sql/operator/insert_physical_operator.h index bc08444..62151a5 100644 --- a/src/observer/sql/operator/insert_physical_operator.h +++ b/src/observer/sql/operator/insert_physical_operator.h @@ -17,7 +17,6 @@ See the Mulan PSL v2 for more details. */ #include #include "sql/operator/physical_operator.h" #include "sql/parser/parse.h" -#include "rc.h" class InsertStmt; diff --git a/src/observer/sql/operator/join_physical_operator.h b/src/observer/sql/operator/join_physical_operator.h index 55edc80..8f507ad 100644 --- a/src/observer/sql/operator/join_physical_operator.h +++ b/src/observer/sql/operator/join_physical_operator.h @@ -16,7 +16,6 @@ See the Mulan PSL v2 for more details. */ #include "sql/parser/parse.h" #include "sql/operator/physical_operator.h" -#include "rc.h" /** * 最简单的两表(称为左表、右表)join算子 diff --git a/src/observer/sql/operator/physical_operator.h b/src/observer/sql/operator/physical_operator.h index cb871c6..55d19dc 100644 --- a/src/observer/sql/operator/physical_operator.h +++ b/src/observer/sql/operator/physical_operator.h @@ -18,7 +18,7 @@ See the Mulan PSL v2 for more details. */ #include #include -#include "rc.h" +#include "common/rc.h" #include "sql/expr/tuple.h" class Record; diff --git a/src/observer/sql/operator/predicate_physical_operator.cpp b/src/observer/sql/operator/predicate_physical_operator.cpp index 416640b..273ebb0 100644 --- a/src/observer/sql/operator/predicate_physical_operator.cpp +++ b/src/observer/sql/operator/predicate_physical_operator.cpp @@ -16,7 +16,7 @@ See the Mulan PSL v2 for more details. */ #include "sql/operator/predicate_physical_operator.h" #include "storage/record/record.h" #include "sql/stmt/filter_stmt.h" -#include "storage/common/field.h" +#include "storage/field/field.h" PredicatePhysicalOperator::PredicatePhysicalOperator(std::unique_ptr expr) : expression_(std::move(expr)) { diff --git a/src/observer/sql/operator/project_logical_operator.h b/src/observer/sql/operator/project_logical_operator.h index 4a856c0..ecde59c 100644 --- a/src/observer/sql/operator/project_logical_operator.h +++ b/src/observer/sql/operator/project_logical_operator.h @@ -19,7 +19,7 @@ See the Mulan PSL v2 for more details. */ #include "sql/operator/logical_operator.h" #include "sql/expr/expression.h" -#include "storage/common/field.h" +#include "storage/field/field.h" /** * project 表示投影运算 diff --git a/src/observer/sql/operator/project_physical_operator.cpp b/src/observer/sql/operator/project_physical_operator.cpp index f8f8c35..7c700cc 100644 --- a/src/observer/sql/operator/project_physical_operator.cpp +++ b/src/observer/sql/operator/project_physical_operator.cpp @@ -15,7 +15,7 @@ See the Mulan PSL v2 for more details. */ #include "common/log/log.h" #include "sql/operator/project_physical_operator.h" #include "storage/record/record.h" -#include "storage/common/table.h" +#include "storage/table/table.h" RC ProjectPhysicalOperator::open(Trx *trx) { diff --git a/src/observer/sql/operator/project_physical_operator.h b/src/observer/sql/operator/project_physical_operator.h index e69028a..15926b0 100644 --- a/src/observer/sql/operator/project_physical_operator.h +++ b/src/observer/sql/operator/project_physical_operator.h @@ -15,7 +15,6 @@ See the Mulan PSL v2 for more details. */ #pragma once #include "sql/operator/physical_operator.h" -#include "rc.h" class ProjectPhysicalOperator : public PhysicalOperator { diff --git a/src/observer/sql/operator/table_get_logical_operator.h b/src/observer/sql/operator/table_get_logical_operator.h index 448f3bc..0c15360 100644 --- a/src/observer/sql/operator/table_get_logical_operator.h +++ b/src/observer/sql/operator/table_get_logical_operator.h @@ -14,7 +14,7 @@ See the Mulan PSL v2 for more details. */ #pragma once #include "sql/operator/logical_operator.h" -#include "storage/common/field.h" +#include "storage/field/field.h" /** * 表示从表中获取数据的算子 diff --git a/src/observer/sql/operator/table_scan_physical_operator.cpp b/src/observer/sql/operator/table_scan_physical_operator.cpp index db656bf..612ade6 100644 --- a/src/observer/sql/operator/table_scan_physical_operator.cpp +++ b/src/observer/sql/operator/table_scan_physical_operator.cpp @@ -13,8 +13,7 @@ See the Mulan PSL v2 for more details. */ // #include "sql/operator/table_scan_physical_operator.h" -#include "storage/common/table.h" -#include "rc.h" +#include "storage/table/table.h" using namespace std; diff --git a/src/observer/sql/operator/table_scan_physical_operator.h b/src/observer/sql/operator/table_scan_physical_operator.h index fa5a35e..7933fb1 100644 --- a/src/observer/sql/operator/table_scan_physical_operator.h +++ b/src/observer/sql/operator/table_scan_physical_operator.h @@ -16,7 +16,7 @@ See the Mulan PSL v2 for more details. */ #include "sql/operator/physical_operator.h" #include "storage/record/record_manager.h" -#include "rc.h" +#include "common/rc.h" class Table; diff --git a/src/observer/sql/optimizer/comparison_simplification_rule.h b/src/observer/sql/optimizer/comparison_simplification_rule.h index 5a76e24..4be9432 100644 --- a/src/observer/sql/optimizer/comparison_simplification_rule.h +++ b/src/observer/sql/optimizer/comparison_simplification_rule.h @@ -14,7 +14,7 @@ See the Mulan PSL v2 for more details. */ #pragma once -#include "rc.h" +#include "common/rc.h" #include "sql/optimizer/rewrite_rule.h" class LogicalOperator; diff --git a/src/observer/sql/optimizer/conjunction_simplification_rule.h b/src/observer/sql/optimizer/conjunction_simplification_rule.h index 882a009..74cfe80 100644 --- a/src/observer/sql/optimizer/conjunction_simplification_rule.h +++ b/src/observer/sql/optimizer/conjunction_simplification_rule.h @@ -14,7 +14,6 @@ See the Mulan PSL v2 for more details. */ #pragma once -#include "rc.h" #include "sql/optimizer/rewrite_rule.h" class LogicalOperator; diff --git a/src/observer/sql/optimizer/expression_rewriter.h b/src/observer/sql/optimizer/expression_rewriter.h index 4948d60..cb5dfd0 100644 --- a/src/observer/sql/optimizer/expression_rewriter.h +++ b/src/observer/sql/optimizer/expression_rewriter.h @@ -16,7 +16,7 @@ See the Mulan PSL v2 for more details. */ #include -#include "rc.h" +#include "common/rc.h" #include "sql/operator/logical_operator.h" #include "sql/expr/expression.h" #include "sql/optimizer/rewrite_rule.h" diff --git a/src/observer/sql/optimizer/optimize_stage.h b/src/observer/sql/optimizer/optimize_stage.h index 75c2ccb..5a10cb3 100644 --- a/src/observer/sql/optimizer/optimize_stage.h +++ b/src/observer/sql/optimizer/optimize_stage.h @@ -16,7 +16,7 @@ See the Mulan PSL v2 for more details. */ #include -#include "rc.h" +#include "common/rc.h" #include "common/seda/stage.h" #include "sql/operator/logical_operator.h" #include "sql/operator/physical_operator.h" @@ -32,6 +32,13 @@ class DeleteStmt; class FilterStmt; class ExplainStmt; +/** + * @brief 将解析后的Statement转换成执行计划,并进行优化 + * @details 优化分为两种,一个是根据规则重写,一个是根据代价模型优化。 + * 在这里,先将Statement转换成逻辑计划,然后进行重写(rewrite),最后生成物理计划。 + * 不过并不是所有的语句都需要生成计划,有些可以直接执行,比如create table、create index等。 + * 这些语句可以参考 @class CommandExecutor。 + */ class OptimizeStage : public common::Stage { public: @@ -51,6 +58,12 @@ protected: private: RC handle_request(SQLStageEvent *event); + /** + * @brief 根据SQL生成逻辑计划 + * @details 由于SQL语句种类比较多,并且SQL语句可能会有嵌套的情况,比如带有SQL子查询的语句,那就需要递归的创建逻辑计划。 + * @param sql_event 包含SQL信息的事件 + * @param logical_operator 生成的逻辑计划 + */ RC create_logical_plan(SQLStageEvent *sql_event, std::unique_ptr &logical_operator); RC create_logical_plan(Stmt *stmt, std::unique_ptr &logical_operator); RC create_select_logical_plan(SelectStmt *select_stmt, std::unique_ptr &logical_operator); @@ -59,13 +72,33 @@ private: RC create_delete_logical_plan(DeleteStmt *delete_stmt, std::unique_ptr &logical_operator); RC create_explain_logical_plan(ExplainStmt *explain_stmt, std::unique_ptr &logical_operator); + /** + * @brief 重写逻辑计划 + * @details 根据各种规则,对逻辑计划进行重写,比如消除多余的比较(1!=0)等。 + * 规则改写也是一个递归的过程。 + * @param logical_operator 要改写的逻辑计划 + */ RC rewrite(std::unique_ptr &logical_operator); + + /** + * @brief 优化逻辑计划 + * @details 当前什么都没做。可以增加每个逻辑计划的代价模型,然后根据代价模型进行优化。 + * @param logical_operator 需要优化的逻辑计划 + */ RC optimize(std::unique_ptr &logical_operator); + + /** + * @brief 根据逻辑计划生成物理计划 + * @details 生成的物理计划就可以直接让后面的执行器完全按照物理计划执行了。 + * 物理计划与逻辑计划有些不同,逻辑计划描述要干什么,比如从某张表根据什么条件获取什么数据。 + * 而物理计划描述怎么做,比如如何从某张表按照什么条件获取什么数据,是否使用索引,使用哪个索引等。 + * @param physical_operator 生成的物理计划。通常是一个多叉树的形状,这里就拿着根节点就可以了。 + */ RC generate_physical_plan( std::unique_ptr &logical_operator, std::unique_ptr &physical_operator); private: - Stage *execute_stage_ = nullptr; - PhysicalPlanGenerator physical_plan_generator_; - Rewriter rewriter_; + Stage *execute_stage_ = nullptr; /// 生成计划 + PhysicalPlanGenerator physical_plan_generator_; /// 根据逻辑计划生成物理计划 + Rewriter rewriter_; /// 逻辑计划改写 }; diff --git a/src/observer/sql/optimizer/physical_plan_generator.h b/src/observer/sql/optimizer/physical_plan_generator.h index 1d567af..ab77898 100644 --- a/src/observer/sql/optimizer/physical_plan_generator.h +++ b/src/observer/sql/optimizer/physical_plan_generator.h @@ -16,7 +16,7 @@ See the Mulan PSL v2 for more details. */ #include -#include "rc.h" +#include "common/rc.h" #include "sql/operator/physical_operator.h" #include "sql/operator/logical_operator.h" diff --git a/src/observer/sql/optimizer/rewrite_rule.h b/src/observer/sql/optimizer/rewrite_rule.h index 0ddd2c5..22769d8 100644 --- a/src/observer/sql/optimizer/rewrite_rule.h +++ b/src/observer/sql/optimizer/rewrite_rule.h @@ -16,19 +16,21 @@ See the Mulan PSL v2 for more details. */ #include -#include "rc.h" +#include "common/rc.h" class LogicalOperator; class Expression; -class RewriteRule { +class RewriteRule +{ public: virtual ~RewriteRule() = default; virtual RC rewrite(std::unique_ptr &oper, bool &change_made) = 0; }; -class ExpressionRewriteRule { +class ExpressionRewriteRule +{ public: virtual ~ExpressionRewriteRule() = default; diff --git a/src/observer/sql/parser/parse.cpp b/src/observer/sql/parser/parse.cpp index 6615afb..16db0eb 100644 --- a/src/observer/sql/parser/parse.cpp +++ b/src/observer/sql/parser/parse.cpp @@ -14,7 +14,6 @@ See the Mulan PSL v2 for more details. */ #include #include "sql/parser/parse.h" -#include "rc.h" #include "common/log/log.h" RC parse(char *st, Command *sqln); @@ -75,7 +74,7 @@ int Value::length() Command::Command() : flag(SCF_ERROR) {} -Command::Command(enum SqlCommandFlag _flag) : flag(_flag) +Command::Command(SqlCommandFlag _flag) : flag(_flag) {} void ParsedSqlResult::add_command(std::unique_ptr command) diff --git a/src/observer/sql/parser/parse.h b/src/observer/sql/parser/parse.h index 87dd475..a654c7d 100644 --- a/src/observer/sql/parser/parse.h +++ b/src/observer/sql/parser/parse.h @@ -14,7 +14,7 @@ See the Mulan PSL v2 for more details. */ #pragma once -#include "rc.h" +#include "common/rc.h" #include "sql/parser/parse_defs.h" RC parse(const char *st, ParsedSqlResult *sql_result); diff --git a/src/observer/sql/parser/parse_defs.h b/src/observer/sql/parser/parse_defs.h index d6df389..1c6e488 100644 --- a/src/observer/sql/parser/parse_defs.h +++ b/src/observer/sql/parser/parse_defs.h @@ -19,19 +19,21 @@ See the Mulan PSL v2 for more details. */ #include #include -#define MAX_NUM 20 -#define MAX_REL_NAME 20 -#define MAX_ATTR_NAME 20 -#define MAX_ERROR_MESSAGE 20 -#define MAX_DATA 50 - -// 属性结构体 +/** + * @brief 描述一个属性 + * @details 属性,或者说字段(column, field) + * Rel -> Relation + * Attr -> Attribute + */ struct RelAttr { - std::string relation_name; // relation name (may be NULL) 表名 - std::string attribute_name; // attribute name 属性名 + std::string relation_name; /// relation name (may be NULL) 表名 + std::string attribute_name; /// attribute name 属性名 }; +/** + * @brief 描述比较运算符 + */ enum CompOp { EQUAL_TO, //"=" 0 @@ -43,113 +45,169 @@ enum CompOp NO_OP }; -// 属性值类型 +/** + * @brief 属性的类型 + * + */ enum AttrType { UNDEFINED, - CHARS, - INTS, - FLOATS, - BOOLEANS, + CHARS, /// 字符串类型 + INTS, /// 整数类型(4字节) + FLOATS, /// 浮点数类型(4字节) + BOOLEANS, /// boolean类型,当前不是由parser解析出来的,是程序内部使用的 }; -// 属性值 +/** + * @brief 属性的值 + * + */ struct Value { - AttrType type; // type of value - int int_value; - float float_value; - bool bool_value; - std::string string_value; + AttrType type; /// 属性的类型 + int int_value; /// 如果是整数,这个值就有意义 + float float_value; /// 如果是浮点数,这个值就有意义 + bool bool_value; /// 如果是boolean值,这个值就有意义 + std::string string_value; /// 如果是字符串,这个值就有意义 - const char *data() const; - int length(); + const char *data() const; /// 获取值的内存地址 + int length(); /// 获取占用的内存长度 }; +/** + * @brief 表示一个条件比较 + * @details 条件比较就是SQL查询中的 where a>b 这种。 + * 一个条件比较是有两部分组成的,称为左边和右边。 + * 左边和右边理论上都可以是任意的数据,比如是字段(属性,列),也可以是数值常量。 + * 这个结构中记录的仅仅支持字段和值。 + */ struct Condition { - int left_is_attr; // TRUE if left-hand side is an attribute - // 1时,操作符左边是属性名,0时,是属性值 - Value left_value; // left-hand side value if left_is_attr = FALSE - RelAttr left_attr; // left-hand side attribute - CompOp comp; // comparison operator - int right_is_attr; // TRUE if right-hand side is an attribute - // 1时,操作符右边是属性名,0时,是属性值 - RelAttr right_attr; // right-hand side attribute if right_is_attr = TRUE 右边的属性 - Value right_value; // right-hand side value if right_is_attr = FALSE + int left_is_attr; /// TRUE if left-hand side is an attribute + /// 1时,操作符左边是属性名,0时,是属性值 + Value left_value; /// left-hand side value if left_is_attr = FALSE + RelAttr left_attr; /// left-hand side attribute + CompOp comp; /// comparison operator + int right_is_attr; /// TRUE if right-hand side is an attribute + /// 1时,操作符右边是属性名,0时,是属性值 + RelAttr right_attr; /// right-hand side attribute if right_is_attr = TRUE 右边的属性 + Value right_value; /// right-hand side value if right_is_attr = FALSE }; -// struct of select +/** + * @brief 描述一个select语句 + * @details 一个正常的select语句描述起来比这个要复杂很多,这里做了简化。 + * 一个select语句由三部分组成,分别是select, from, where。 + * select部分表示要查询的字段,from部分表示要查询的表,where部分表示查询的条件。 + * 比如 from 中可以是多个表,也可以是另一个查询语句,这里仅仅支持表,也就是 relations。 + * where 条件 conditions,这里表示使用AND串联起来多个条件。正常的SQL语句会有OR,NOT等, + * 甚至可以包含复杂的表达式。 + */ struct Selects { - std::vector attributes; // attributes in select clause - std::vector relations; - std::vector conditions; + std::vector attributes; /// attributes in select clause + std::vector relations; /// 查询的表 + std::vector conditions; /// 查询条件,使用AND串联起来多个条件 }; -// struct of insert +/** + * @brief 描述一个insert语句 + * @details 于Selects类似,也做了很多简化 + */ struct Inserts { - std::string relation_name; // Relation to insert into - std::vector values; + std::string relation_name; /// Relation to insert into + std::vector values; /// 要插入的值 }; -// struct of delete +/** + * @brief 描述一个delete语句 + */ struct Deletes { std::string relation_name; // Relation to delete from std::vector conditions; }; -// struct of update +/** + * @brief 描述一个update语句 + * + */ struct Updates { std::string relation_name; // Relation to update - std::string attribute_name; // Attribute to update - Value value; // update value + std::string attribute_name; /// 更新的字段,仅支持一个字段 + Value value; /// 更新的值,仅支持一个字段 std::vector conditions; }; +/** + * @brief 描述一个属性 + * @details 属性,或者说字段(column, field) + * Rel -> Relation + * Attr -> Attribute + */ struct AttrInfo { - AttrType type; // Type of attribute - std::string name; // Attribute name - size_t length; // Length of attribute + AttrType type; /// Type of attribute + std::string name; /// Attribute name + size_t length; /// Length of attribute }; -// struct of craete_table +/** + * @brief 描述一个create table语句 + * @details 这里也做了很多简化。 + */ struct CreateTable { std::string relation_name; // Relation name std::vector attr_infos; // attributes }; -// struct of drop_table +/** + * @brief 描述一个drop table语句 + * + */ struct DropTable { - std::string relation_name; // Relation name + std::string relation_name; /// 要删除的表名 }; -// struct of create_index +/** + * @brief 描述一个create index语句 + * @details 创建索引时,需要指定索引名,表名,字段名。 + * 正常的SQL语句中,一个索引可能包含了多个字段,这里仅支持一个字段。 + */ struct CreateIndex { - std::string index_name; // Index name - std::string relation_name; // Relation name - std::string attribute_name; // Attribute name + std::string index_name; /// Index name + std::string relation_name; /// Relation name + std::string attribute_name; /// Attribute name }; -// struct of drop_index +/** + * @brief 描述一个drop index语句 + * + */ struct DropIndex { std::string index_name; // Index name std::string relation_name; // Relation name }; +/** + * @brief 描述一个desc table语句 + * @details desc table 是查询表结构信息的语句 + */ struct DescTable { std::string relation_name; }; +/** + * @brief 描述一个load data语句 + * @details 从文件导入数据到表中。文件中的每一行就是一条数据,每行的数据类型、字段个数都与表保持一致 + */ struct LoadData { std::string relation_name; @@ -157,11 +215,22 @@ struct LoadData }; class Command; + +/** + * @brief 描述一个explain语句 + * @details 会创建operator的语句,才能用explain输出执行计划。 + * 一个command就是一个语句,比如select语句,insert语句等。 + * 可能改成SqlCommand更合适。 + */ struct Explain { std::unique_ptr cmd; }; +/** + * @brief 解析SQL语句出现了错误 + * @details 当前解析时并没有处理错误的行号和列号 + */ struct Error { std::string error_msg; @@ -169,7 +238,10 @@ struct Error int column; }; -// 修改yacc中相关数字编码为宏定义 +/** + * @brief 表示一个SQL语句的类型 + * + */ enum SqlCommandFlag { SCF_ERROR = 0, @@ -184,7 +256,7 @@ enum SqlCommandFlag SCF_SYNC, SCF_SHOW_TABLES, SCF_DESC_TABLE, - SCF_BEGIN, + SCF_BEGIN, /// 事务开始语句,可以在这里扩展只读事务 SCF_COMMIT, SCF_CLOG_SYNC, SCF_ROLLBACK, @@ -193,7 +265,10 @@ enum SqlCommandFlag SCF_EXIT, SCF_EXPLAIN, }; -// struct of flag and sql_struct +/** + * @brief 表示一个SQL语句 + * + */ class Command { public: @@ -213,11 +288,11 @@ public: public: Command(); - Command(enum SqlCommandFlag flag); + explicit Command(SqlCommandFlag flag); }; /** - * 表示语法解析后的数据 + * @brief 表示语法解析后的数据 * 叫ParsedSqlNode 可能会更清晰一点 */ class ParsedSqlResult @@ -230,7 +305,7 @@ public: } private: - std::vector> sql_commands_; + std::vector> sql_commands_; /// 这里记录SQL命令。虽然看起来支持多个,但是当前仅处理一个 }; const char *attr_type_to_string(AttrType type); diff --git a/src/observer/sql/parser/parse_stage.h b/src/observer/sql/parser/parse_stage.h index f6905a1..50400aa 100644 --- a/src/observer/sql/parser/parse_stage.h +++ b/src/observer/sql/parser/parse_stage.h @@ -12,15 +12,19 @@ See the Mulan PSL v2 for more details. */ // Created by Longda on 2021/4/13. // -#ifndef __OBSERVER_SQL_PARSE_STAGE_H__ -#define __OBSERVER_SQL_PARSE_STAGE_H__ +#pragma once #include "common/seda/stage.h" -#include "rc.h" - -class ParseStage : public common::Stage { +#include "common/rc.h" + +/** + * @brief 解析SQL语句,解析后的结果可以参考parse_defs.h + * + */ +class ParseStage : public common::Stage +{ public: - ~ParseStage(); + virtual ~ParseStage(); static Stage *make_stage(const std::string &tag); protected: @@ -37,8 +41,5 @@ protected: RC handle_request(common::StageEvent *event); private: - // Stage *optimize_stage_ = nullptr; - Stage *resolve_stage_ = nullptr; + Stage *resolve_stage_ = nullptr; /// 解析后执行的下一个阶段,就是Resolve }; - -#endif //__OBSERVER_SQL_PARSE_STAGE_H__ diff --git a/src/observer/sql/parser/resolve_stage.h b/src/observer/sql/parser/resolve_stage.h index e4bf324..4c13b08 100644 --- a/src/observer/sql/parser/resolve_stage.h +++ b/src/observer/sql/parser/resolve_stage.h @@ -16,7 +16,12 @@ See the Mulan PSL v2 for more details. */ #include "common/seda/stage.h" -class ResolveStage : public common::Stage { +/** + * @brief 执行Resolve,将解析后的SQL语句,转换成各种Stmt(Statement) + * + */ +class ResolveStage : public common::Stage +{ public: ~ResolveStage(); static Stage *make_stage(const std::string &tag); diff --git a/src/observer/sql/plan_cache/plan_cache_stage.h b/src/observer/sql/plan_cache/plan_cache_stage.h index e764777..e48e0ad 100644 --- a/src/observer/sql/plan_cache/plan_cache_stage.h +++ b/src/observer/sql/plan_cache/plan_cache_stage.h @@ -16,7 +16,13 @@ See the Mulan PSL v2 for more details. */ #include "common/seda/stage.h" -class PlanCacheStage : public common::Stage { +/** + * @brief 尝试从Plan的缓存中获取Plan,如果没有命中,则执行Optimizer + * @details 实际上现在什么都没做。不过PlanCache对数据库的优化提升明显,是一个非常有趣的功能, + * 感兴趣的同学可以参考OceanBase的实现 + */ +class PlanCacheStage : public common::Stage +{ public: ~PlanCacheStage(); static Stage *make_stage(const std::string &tag); diff --git a/src/observer/sql/stmt/create_index_stmt.cpp b/src/observer/sql/stmt/create_index_stmt.cpp index 900b0f4..629aece 100644 --- a/src/observer/sql/stmt/create_index_stmt.cpp +++ b/src/observer/sql/stmt/create_index_stmt.cpp @@ -13,8 +13,8 @@ See the Mulan PSL v2 for more details. */ // #include "sql/stmt/create_index_stmt.h" -#include "storage/common/table.h" -#include "storage/common/db.h" +#include "storage/table/table.h" +#include "storage/db/db.h" #include "common/lang/string.h" #include "common/log/log.h" diff --git a/src/observer/sql/stmt/create_table_stmt.cpp b/src/observer/sql/stmt/create_table_stmt.cpp new file mode 100644 index 0000000..82d1073 --- /dev/null +++ b/src/observer/sql/stmt/create_table_stmt.cpp @@ -0,0 +1,21 @@ +/* Copyright (c) 2021 OceanBase and/or its affiliates. All rights reserved. +miniob is licensed under Mulan PSL v2. +You can use this software according to the terms and conditions of the Mulan PSL v2. +You may obtain a copy of Mulan PSL v2 at: + http://license.coscl.org.cn/MulanPSL2 +THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, +EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, +MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. +See the Mulan PSL v2 for more details. */ + +// +// Created by Wangyunlai on 2023/6/13. +// + +#include "sql/stmt/create_table_stmt.h" + +RC CreateTableStmt::create(Db *db, const CreateTable &create_table, Stmt *&stmt) +{ + stmt = new CreateTableStmt(create_table.relation_name, create_table.attr_infos); + return RC::SUCCESS; +} diff --git a/src/observer/sql/stmt/create_table_stmt.h b/src/observer/sql/stmt/create_table_stmt.h new file mode 100644 index 0000000..1db6c19 --- /dev/null +++ b/src/observer/sql/stmt/create_table_stmt.h @@ -0,0 +1,47 @@ +/* Copyright (c) 2021 OceanBase and/or its affiliates. All rights reserved. +miniob is licensed under Mulan PSL v2. +You can use this software according to the terms and conditions of the Mulan PSL v2. +You may obtain a copy of Mulan PSL v2 at: + http://license.coscl.org.cn/MulanPSL2 +THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, +EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, +MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. +See the Mulan PSL v2 for more details. */ + +// +// Created by Wangyunlai on 2023/6/13. +// + +#pragma once + +#include +#include + +#include "sql/stmt/stmt.h" + +class Db; + +/** + * @brief 表示创建表的语句 + * @details 虽然解析成了stmt,但是与原始的SQL解析后的数据也差不多 + */ +class CreateTableStmt : public Stmt +{ +public: + CreateTableStmt(const std::string &table_name, const std::vector &attr_infos) + : table_name_(table_name), + attr_infos_(attr_infos) + {} + virtual ~CreateTableStmt() = default; + + StmtType type() const override { return StmtType::CREATE_TABLE; } + + const std::string &table_name() const { return table_name_; } + const std::vector &attr_infos() const { return attr_infos_; } + + static RC create(Db *db, const CreateTable &create_table, Stmt *&stmt); + +private: + std::string table_name_; + std::vector attr_infos_; +}; \ No newline at end of file diff --git a/src/observer/sql/stmt/delete_stmt.cpp b/src/observer/sql/stmt/delete_stmt.cpp index 8941cd6..e467741 100644 --- a/src/observer/sql/stmt/delete_stmt.cpp +++ b/src/observer/sql/stmt/delete_stmt.cpp @@ -15,8 +15,8 @@ See the Mulan PSL v2 for more details. */ #include "common/log/log.h" #include "sql/stmt/delete_stmt.h" #include "sql/stmt/filter_stmt.h" -#include "storage/common/db.h" -#include "storage/common/table.h" +#include "storage/db/db.h" +#include "storage/table/table.h" DeleteStmt::DeleteStmt(Table *table, FilterStmt *filter_stmt) : table_(table), filter_stmt_(filter_stmt) {} diff --git a/src/observer/sql/stmt/delete_stmt.h b/src/observer/sql/stmt/delete_stmt.h index 5641e99..54914fb 100644 --- a/src/observer/sql/stmt/delete_stmt.h +++ b/src/observer/sql/stmt/delete_stmt.h @@ -14,14 +14,14 @@ See the Mulan PSL v2 for more details. */ #pragma once -#include "rc.h" #include "sql/stmt/stmt.h" #include "sql/parser/parse_defs.h" class Table; class FilterStmt; -class DeleteStmt : public Stmt { +class DeleteStmt : public Stmt +{ public: DeleteStmt(Table *table, FilterStmt *filter_stmt); ~DeleteStmt() override; diff --git a/src/observer/sql/stmt/desc_table_stmt.cpp b/src/observer/sql/stmt/desc_table_stmt.cpp new file mode 100644 index 0000000..1718e6f --- /dev/null +++ b/src/observer/sql/stmt/desc_table_stmt.cpp @@ -0,0 +1,25 @@ +/* Copyright (c) 2021 OceanBase and/or its affiliates. All rights reserved. +miniob is licensed under Mulan PSL v2. +You can use this software according to the terms and conditions of the Mulan PSL v2. +You may obtain a copy of Mulan PSL v2 at: + http://license.coscl.org.cn/MulanPSL2 +THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, +EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, +MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. +See the Mulan PSL v2 for more details. */ + +// +// Created by Wangyunlai on 2023/6/14. +// + +#include "sql/stmt/desc_table_stmt.h" +#include "storage/db/db.h" + +RC DescTableStmt::create(Db *db, const DescTable &desc_table, Stmt *&stmt) +{ + if (db->find_table(desc_table.relation_name.c_str()) == nullptr) { + return RC::SCHEMA_TABLE_NOT_EXIST; + } + stmt = new DescTableStmt(desc_table.relation_name); + return RC::SUCCESS; +} diff --git a/src/observer/sql/stmt/desc_table_stmt.h b/src/observer/sql/stmt/desc_table_stmt.h new file mode 100644 index 0000000..5360568 --- /dev/null +++ b/src/observer/sql/stmt/desc_table_stmt.h @@ -0,0 +1,44 @@ +/* Copyright (c) 2021 OceanBase and/or its affiliates. All rights reserved. +miniob is licensed under Mulan PSL v2. +You can use this software according to the terms and conditions of the Mulan PSL v2. +You may obtain a copy of Mulan PSL v2 at: + http://license.coscl.org.cn/MulanPSL2 +THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, +EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, +MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. +See the Mulan PSL v2 for more details. */ + +// +// Created by Wangyunlai on 2023/6/14. +// + +#pragma once + +#include +#include + +#include "sql/stmt/stmt.h" + +class Db; + +/** + * @brief 描述表的语句 + * @details 虽然解析成了stmt,但是与原始的SQL解析后的数据也差不多 + */ +class DescTableStmt : public Stmt +{ +public: + DescTableStmt(const std::string &table_name) + : table_name_(table_name) + {} + virtual ~DescTableStmt() = default; + + StmtType type() const override { return StmtType::DESC_TABLE; } + + const std::string &table_name() const { return table_name_; } + + static RC create(Db *db, const DescTable &desc_table, Stmt *&stmt); + +private: + std::string table_name_; +}; \ No newline at end of file diff --git a/src/observer/sql/stmt/exit_stmt.h b/src/observer/sql/stmt/exit_stmt.h new file mode 100644 index 0000000..e7eb29c --- /dev/null +++ b/src/observer/sql/stmt/exit_stmt.h @@ -0,0 +1,39 @@ +/* Copyright (c) 2021 OceanBase and/or its affiliates. All rights reserved. +miniob is licensed under Mulan PSL v2. +You can use this software according to the terms and conditions of the Mulan PSL v2. +You may obtain a copy of Mulan PSL v2 at: + http://license.coscl.org.cn/MulanPSL2 +THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, +EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, +MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. +See the Mulan PSL v2 for more details. */ + +// +// Created by Wangyunlai on 2023/6/14. +// + +#pragma once + +#include +#include + +#include "sql/stmt/stmt.h" + +/** + * @brief Exit 语句,表示断开连接,现在什么成员都没有 + */ +class ExitStmt : public Stmt +{ +public: + ExitStmt() + {} + virtual ~ExitStmt() = default; + + StmtType type() const override { return StmtType::EXIT; } + + static RC create(Stmt *&stmt) + { + stmt = new ExitStmt(); + return RC::SUCCESS; + } +}; \ No newline at end of file diff --git a/src/observer/sql/stmt/filter_stmt.cpp b/src/observer/sql/stmt/filter_stmt.cpp index 5183b93..3c04b63 100644 --- a/src/observer/sql/stmt/filter_stmt.cpp +++ b/src/observer/sql/stmt/filter_stmt.cpp @@ -12,12 +12,12 @@ See the Mulan PSL v2 for more details. */ // Created by Wangyunlai on 2022/5/22. // -#include "rc.h" +#include "common/rc.h" #include "common/log/log.h" #include "common/lang/string.h" #include "sql/stmt/filter_stmt.h" -#include "storage/common/db.h" -#include "storage/common/table.h" +#include "storage/db/db.h" +#include "storage/table/table.h" FilterStmt::~FilterStmt() { diff --git a/src/observer/sql/stmt/filter_stmt.h b/src/observer/sql/stmt/filter_stmt.h index 5e0b290..31bef24 100644 --- a/src/observer/sql/stmt/filter_stmt.h +++ b/src/observer/sql/stmt/filter_stmt.h @@ -16,7 +16,6 @@ See the Mulan PSL v2 for more details. */ #include #include -#include "rc.h" #include "sql/parser/parse_defs.h" #include "sql/stmt/stmt.h" #include "sql/expr/expression.h" @@ -25,7 +24,8 @@ class Db; class Table; class FieldMeta; -struct FilterObj { +struct FilterObj +{ bool is_attr; Field field; Value value; @@ -43,7 +43,8 @@ struct FilterObj { } }; -class FilterUnit { +class FilterUnit +{ public: FilterUnit() = default; ~FilterUnit() @@ -83,7 +84,8 @@ private: FilterObj right_; }; -class FilterStmt { +class FilterStmt +{ public: FilterStmt() = default; virtual ~FilterStmt(); diff --git a/src/observer/sql/stmt/help_stmt.h b/src/observer/sql/stmt/help_stmt.h new file mode 100644 index 0000000..8b3b880 --- /dev/null +++ b/src/observer/sql/stmt/help_stmt.h @@ -0,0 +1,39 @@ +/* Copyright (c) 2021 OceanBase and/or its affiliates. All rights reserved. +miniob is licensed under Mulan PSL v2. +You can use this software according to the terms and conditions of the Mulan PSL v2. +You may obtain a copy of Mulan PSL v2 at: + http://license.coscl.org.cn/MulanPSL2 +THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, +EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, +MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. +See the Mulan PSL v2 for more details. */ + +// +// Created by Wangyunlai on 2023/6/14. +// + +#pragma once + +#include +#include + +#include "sql/stmt/stmt.h" + +/** + * @brief Help 语句,现在什么成员都没有 + */ +class HelpStmt : public Stmt +{ +public: + HelpStmt() + {} + virtual ~HelpStmt() = default; + + StmtType type() const override { return StmtType::HELP; } + + static RC create(Stmt *&stmt) + { + stmt = new HelpStmt(); + return RC::SUCCESS; + } +}; \ No newline at end of file diff --git a/src/observer/sql/stmt/insert_stmt.cpp b/src/observer/sql/stmt/insert_stmt.cpp index 9ad1f0e..64731a2 100644 --- a/src/observer/sql/stmt/insert_stmt.cpp +++ b/src/observer/sql/stmt/insert_stmt.cpp @@ -14,8 +14,8 @@ See the Mulan PSL v2 for more details. */ #include "sql/stmt/insert_stmt.h" #include "common/log/log.h" -#include "storage/common/db.h" -#include "storage/common/table.h" +#include "storage/db/db.h" +#include "storage/table/table.h" InsertStmt::InsertStmt(Table *table, const Value *values, int value_amount) : table_(table), values_(values), value_amount_(value_amount) diff --git a/src/observer/sql/stmt/insert_stmt.h b/src/observer/sql/stmt/insert_stmt.h index bf6dcb7..3af4f5e 100644 --- a/src/observer/sql/stmt/insert_stmt.h +++ b/src/observer/sql/stmt/insert_stmt.h @@ -14,7 +14,7 @@ See the Mulan PSL v2 for more details. */ #pragma once -#include "rc.h" +#include "common/rc.h" #include "sql/stmt/stmt.h" class Table; diff --git a/src/observer/sql/stmt/select_stmt.cpp b/src/observer/sql/stmt/select_stmt.cpp index 6e431ec..13571cd 100644 --- a/src/observer/sql/stmt/select_stmt.cpp +++ b/src/observer/sql/stmt/select_stmt.cpp @@ -16,8 +16,8 @@ See the Mulan PSL v2 for more details. */ #include "sql/stmt/filter_stmt.h" #include "common/log/log.h" #include "common/lang/string.h" -#include "storage/common/db.h" -#include "storage/common/table.h" +#include "storage/db/db.h" +#include "storage/table/table.h" SelectStmt::~SelectStmt() { diff --git a/src/observer/sql/stmt/select_stmt.h b/src/observer/sql/stmt/select_stmt.h index bb25177..c6766a8 100644 --- a/src/observer/sql/stmt/select_stmt.h +++ b/src/observer/sql/stmt/select_stmt.h @@ -16,9 +16,9 @@ See the Mulan PSL v2 for more details. */ #include -#include "rc.h" +#include "common/rc.h" #include "sql/stmt/stmt.h" -#include "storage/common/field.h" +#include "storage/field/field.h" class FieldMeta; class FilterStmt; diff --git a/deps/common/version.h b/src/observer/sql/stmt/show_tables_stmt.h similarity index 51% rename from deps/common/version.h rename to src/observer/sql/stmt/show_tables_stmt.h index 0c64228..08b456c 100644 --- a/deps/common/version.h +++ b/src/observer/sql/stmt/show_tables_stmt.h @@ -9,33 +9,33 @@ MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. See the Mulan PSL v2 for more details. */ // -// Created by Longda on 2010 +// Created by Wangyunlai on 2023/6/14. // #pragma once -namespace common { +#include +#include -#ifndef MAIJOR_VER -#define MAIJOR_VER 1 -#endif +#include "sql/stmt/stmt.h" -#ifndef MINOR_VER -#define MINOR_VER 0 -#endif +class Db; -#ifndef PATCH_VER -#define PATCH_VER 0 -#endif +/** + * @brief 描述表的语句 + * @details 虽然解析成了stmt,但是与原始的SQL解析后的数据也差不多 + */ +class ShowTablesStmt : public Stmt +{ +public: + ShowTablesStmt() = default; + virtual ~ShowTablesStmt() = default; -#ifndef OTHER_VER -#define OTHER_VER 1 -#endif + StmtType type() const override { return StmtType::SHOW_TABLES; } -#define STR1(R) #R -#define STR2(R) STR1(R) - -#define VERSION_STR (STR2(MAIJOR_VER) "." STR2(MINOR_VER) "." STR2(PATCH_VER) "." STR2(OTHER_VER)) -#define VERSION_NUM (MAIJOR_VER << 24 | MINOR_VER << 16 | PATCH_VER << 8 | OTHER_VER) - -} // namespace common + static RC create(Db *db, Stmt *&stmt) + { + stmt = new ShowTablesStmt(); + return RC::SUCCESS; + } +}; \ No newline at end of file diff --git a/src/observer/sql/stmt/stmt.cpp b/src/observer/sql/stmt/stmt.cpp index b4702ec..8bb3d66 100644 --- a/src/observer/sql/stmt/stmt.cpp +++ b/src/observer/sql/stmt/stmt.cpp @@ -12,13 +12,20 @@ See the Mulan PSL v2 for more details. */ // Created by Wangyunlai on 2022/5/22. // -#include "rc.h" #include "common/log/log.h" +#include "sql/stmt/stmt.h" #include "sql/stmt/insert_stmt.h" #include "sql/stmt/delete_stmt.h" #include "sql/stmt/select_stmt.h" #include "sql/stmt/explain_stmt.h" #include "sql/stmt/create_index_stmt.h" +#include "sql/stmt/create_table_stmt.h" +#include "sql/stmt/desc_table_stmt.h" +#include "sql/stmt/help_stmt.h" +#include "sql/stmt/show_tables_stmt.h" +#include "sql/stmt/trx_begin_stmt.h" +#include "sql/stmt/trx_end_stmt.h" +#include "sql/stmt/exit_stmt.h" RC Stmt::create_stmt(Db *db, const Command &cmd, Stmt *&stmt) { @@ -42,6 +49,36 @@ RC Stmt::create_stmt(Db *db, const Command &cmd, Stmt *&stmt) case SCF_CREATE_INDEX: { return CreateIndexStmt::create(db, cmd.create_index, stmt); } + + case SCF_CREATE_TABLE: { + return CreateTableStmt::create(db, cmd.create_table, stmt); + } + + case SCF_DESC_TABLE: { + return DescTableStmt::create(db, cmd.desc_table, stmt); + } + + case SCF_HELP: { + return HelpStmt::create(stmt); + } + + case SCF_SHOW_TABLES: { + return ShowTablesStmt::create(db, stmt); + } + + case SCF_BEGIN: { + return TrxBeginStmt::create(stmt); + } + + case SCF_COMMIT: + case SCF_ROLLBACK: { + return TrxEndStmt::create(cmd.flag, stmt); + } + + case SCF_EXIT: { + return ExitStmt::create(stmt); + } + default: { LOG_INFO("Command::type %d doesn't need to create statement.", cmd.flag); } break; diff --git a/src/observer/sql/stmt/stmt.h b/src/observer/sql/stmt/stmt.h index 9f795cf..2b1ccaf 100644 --- a/src/observer/sql/stmt/stmt.h +++ b/src/observer/sql/stmt/stmt.h @@ -14,38 +14,52 @@ See the Mulan PSL v2 for more details. */ #pragma once -#include "rc.h" +#include "common/rc.h" #include "sql/parser/parse_defs.h" class Db; -enum class StmtType -{ - SELECT, - INSERT, - UPDATE, - DELETE, - CREATE_TABLE, - DROP_TABLE, - CREATE_INDEX, - DROP_INDEX, - SYNC, - SHOW_TABLES, - DESC_TABLE, - BEGIN, - COMMIT, - ROLLBACK, - LOAD_DATA, - HELP, - EXIT, - EXPLAIN, - - PREDICATE, +#define DEFINE_ENUM() \ + DEFINE_ENUM_ITEM(SELECT) \ + DEFINE_ENUM_ITEM(INSERT) \ + DEFINE_ENUM_ITEM(UPDATE) \ + DEFINE_ENUM_ITEM(DELETE) \ + DEFINE_ENUM_ITEM(CREATE_TABLE) \ + DEFINE_ENUM_ITEM(DROP_TABLE) \ + DEFINE_ENUM_ITEM(CREATE_INDEX) \ + DEFINE_ENUM_ITEM(DROP_INDEX) \ + DEFINE_ENUM_ITEM(SYNC) \ + DEFINE_ENUM_ITEM(SHOW_TABLES) \ + DEFINE_ENUM_ITEM(DESC_TABLE) \ + DEFINE_ENUM_ITEM(BEGIN) \ + DEFINE_ENUM_ITEM(COMMIT) \ + DEFINE_ENUM_ITEM(ROLLBACK) \ + DEFINE_ENUM_ITEM(LOAD_DATA) \ + DEFINE_ENUM_ITEM(HELP) \ + DEFINE_ENUM_ITEM(EXIT) \ + DEFINE_ENUM_ITEM(EXPLAIN) \ + DEFINE_ENUM_ITEM(PREDICATE) + +enum class StmtType { + #define DEFINE_ENUM_ITEM(name) name, + DEFINE_ENUM() + #undef DEFINE_ENUM_ITEM }; +inline const char *stmt_type_name(StmtType type) +{ + switch (type) { + #define DEFINE_ENUM_ITEM(name) case StmtType::name: return #name; + DEFINE_ENUM() + #undef DEFINE_ENUM_ITEM + default: return "unkown"; + } +} + /** - * Stmt for Statement - * + * @brief Stmt for Statement + * @details SQL解析后的语句,再进一步解析成Stmt,使用内部的数据结构来表示。 + * 比如table_name,解析成具体的 Table对象,attr/field name解析成Field对象。 */ class Stmt { diff --git a/src/observer/sql/stmt/trx_begin_stmt.h b/src/observer/sql/stmt/trx_begin_stmt.h new file mode 100644 index 0000000..f1335d9 --- /dev/null +++ b/src/observer/sql/stmt/trx_begin_stmt.h @@ -0,0 +1,39 @@ +/* Copyright (c) 2021 OceanBase and/or its affiliates. All rights reserved. +miniob is licensed under Mulan PSL v2. +You can use this software according to the terms and conditions of the Mulan PSL v2. +You may obtain a copy of Mulan PSL v2 at: + http://license.coscl.org.cn/MulanPSL2 +THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, +EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, +MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. +See the Mulan PSL v2 for more details. */ + +// +// Created by Wangyunlai on 2023/6/14. +// + +#pragma once + +#include +#include + +#include "sql/stmt/stmt.h" + +/** + * @brief 事务的Begin 语句,现在什么成员都没有 + */ +class TrxBeginStmt : public Stmt +{ +public: + TrxBeginStmt() + {} + virtual ~TrxBeginStmt() = default; + + StmtType type() const override { return StmtType::BEGIN; } + + static RC create(Stmt *&stmt) + { + stmt = new TrxBeginStmt(); + return RC::SUCCESS; + } +}; \ No newline at end of file diff --git a/src/observer/sql/stmt/trx_end_stmt.h b/src/observer/sql/stmt/trx_end_stmt.h new file mode 100644 index 0000000..fc126b7 --- /dev/null +++ b/src/observer/sql/stmt/trx_end_stmt.h @@ -0,0 +1,44 @@ +/* Copyright (c) 2021 OceanBase and/or its affiliates. All rights reserved. +miniob is licensed under Mulan PSL v2. +You can use this software according to the terms and conditions of the Mulan PSL v2. +You may obtain a copy of Mulan PSL v2 at: + http://license.coscl.org.cn/MulanPSL2 +THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, +EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, +MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. +See the Mulan PSL v2 for more details. */ + +// +// Created by Wangyunlai on 2023/6/14. +// + +#pragma once + +#include +#include + +#include "sql/stmt/stmt.h" + +/** + * @brief 事务的 Commit/Rollback 语句,现在什么成员都没有 + */ +class TrxEndStmt : public Stmt +{ +public: + TrxEndStmt(StmtType type) + : type_(type) + {} + virtual ~TrxEndStmt() = default; + + StmtType type() const override { return type_; } + + static RC create(SqlCommandFlag flag, Stmt *&stmt) + { + StmtType type = flag == SqlCommandFlag::SCF_COMMIT ? StmtType::COMMIT : StmtType::ROLLBACK; + stmt = new TrxEndStmt(type); + return RC::SUCCESS; + } + +private: + StmtType type_; +}; \ No newline at end of file diff --git a/src/observer/sql/stmt/update_stmt.h b/src/observer/sql/stmt/update_stmt.h index 4b8f7f7..3148bb2 100644 --- a/src/observer/sql/stmt/update_stmt.h +++ b/src/observer/sql/stmt/update_stmt.h @@ -14,7 +14,7 @@ See the Mulan PSL v2 for more details. */ #pragma once -#include "rc.h" +#include "common/rc.h" #include "sql/stmt/stmt.h" class Table; diff --git a/src/observer/storage/buffer/frame.h b/src/observer/storage/buffer/frame.h index 9f973a5..47ec590 100644 --- a/src/observer/storage/buffer/frame.h +++ b/src/observer/storage/buffer/frame.h @@ -24,7 +24,7 @@ See the Mulan PSL v2 for more details. */ #include "storage/buffer/page.h" #include "common/log/log.h" #include "common/lang/mutex.h" -#include "defs.h" +#include "common/types.h" class FrameId { diff --git a/src/observer/storage/buffer/page.h b/src/observer/storage/buffer/page.h index bda0728..0f08852 100644 --- a/src/observer/storage/buffer/page.h +++ b/src/observer/storage/buffer/page.h @@ -15,7 +15,7 @@ See the Mulan PSL v2 for more details. */ #pragma once #include -#include "defs.h" +#include "common/types.h" using TrxID = int32_t; diff --git a/src/observer/storage/clog/clog.cpp b/src/observer/storage/clog/clog.cpp index 90d7e9f..32899ee 100644 --- a/src/observer/storage/clog/clog.cpp +++ b/src/observer/storage/clog/clog.cpp @@ -17,7 +17,7 @@ See the Mulan PSL v2 for more details. */ #include "common/log/log.h" #include "storage/clog/clog.h" -#include "global_context.h" +#include "common/global_context.h" #include "storage/trx/trx.h" #include "common/io/io.h" @@ -235,7 +235,7 @@ RC CLogBuffer::flush_buffer(CLogFile &log_file) // log buffer 需要支持并发,所以要考虑加锁 // 从队列中取出日志记录然后写入到文件中 - unique_ptr log_record = move(log_records_.front()); + unique_ptr log_record = std::move(log_records_.front()); log_records_.pop_front(); rc = write_log_record(log_file, log_record.get()); diff --git a/src/observer/storage/clog/clog.h b/src/observer/storage/clog/clog.h index c49f171..0d0c100 100644 --- a/src/observer/storage/clog/clog.h +++ b/src/observer/storage/clog/clog.h @@ -26,7 +26,6 @@ See the Mulan PSL v2 for more details. */ #include "storage/record/record.h" #include "storage/persist/persist.h" #include "common/lang/mutex.h" -#include "rc.h" class CLogManager; class CLogBuffer; diff --git a/src/observer/storage/common/condition_filter.cpp b/src/observer/storage/common/condition_filter.cpp index 9e2570d..30ce644 100644 --- a/src/observer/storage/common/condition_filter.cpp +++ b/src/observer/storage/common/condition_filter.cpp @@ -17,7 +17,7 @@ See the Mulan PSL v2 for more details. */ #include "condition_filter.h" #include "storage/record/record_manager.h" #include "common/log/log.h" -#include "storage/common/table.h" +#include "storage/table/table.h" #include "sql/expr/tuple_cell.h" using namespace common; diff --git a/src/observer/storage/common/condition_filter.h b/src/observer/storage/common/condition_filter.h index 265fede..06002d3 100644 --- a/src/observer/storage/common/condition_filter.h +++ b/src/observer/storage/common/condition_filter.h @@ -14,7 +14,6 @@ See the Mulan PSL v2 for more details. */ #pragma once -#include "rc.h" #include "sql/parser/parse.h" class Record; diff --git a/src/observer/storage/common/db.cpp b/src/observer/storage/db/db.cpp similarity index 98% rename from src/observer/storage/common/db.cpp rename to src/observer/storage/db/db.cpp index 5c6d316..b84e526 100644 --- a/src/observer/storage/common/db.cpp +++ b/src/observer/storage/db/db.cpp @@ -12,7 +12,7 @@ See the Mulan PSL v2 for more details. */ // Created by Meiyi & Longda & Wangyunlai on 2021/5/12. // -#include "storage/common/db.h" +#include "storage/db/db.h" #include #include @@ -21,8 +21,8 @@ See the Mulan PSL v2 for more details. */ #include "common/log/log.h" #include "common/os/path.h" #include "common/lang/string.h" -#include "storage/common/table_meta.h" -#include "storage/common/table.h" +#include "storage/table/table_meta.h" +#include "storage/table/table.h" #include "storage/common/meta_util.h" #include "storage/trx/trx.h" #include "storage/clog/clog.h" diff --git a/src/observer/storage/common/db.h b/src/observer/storage/db/db.h similarity index 98% rename from src/observer/storage/common/db.h rename to src/observer/storage/db/db.h index c48dfde..599df9a 100644 --- a/src/observer/storage/common/db.h +++ b/src/observer/storage/db/db.h @@ -19,7 +19,7 @@ See the Mulan PSL v2 for more details. */ #include #include -#include "rc.h" +#include "common/rc.h" #include "sql/parser/parse_defs.h" class Table; diff --git a/src/observer/storage/default/default_handler.cpp b/src/observer/storage/default/default_handler.cpp index b75a930..cc85f6b 100644 --- a/src/observer/storage/default/default_handler.cpp +++ b/src/observer/storage/default/default_handler.cpp @@ -21,7 +21,7 @@ See the Mulan PSL v2 for more details. */ #include "common/lang/string.h" #include "storage/record/record_manager.h" #include "storage/index/bplus_tree.h" -#include "storage/common/table.h" +#include "storage/table/table.h" #include "storage/common/condition_filter.h" #include "storage/clog/clog.h" diff --git a/src/observer/storage/default/default_handler.h b/src/observer/storage/default/default_handler.h index 1ee17e4..f172d5b 100644 --- a/src/observer/storage/default/default_handler.h +++ b/src/observer/storage/default/default_handler.h @@ -16,7 +16,7 @@ See the Mulan PSL v2 for more details. */ #include #include -#include "storage/common/db.h" +#include "storage/db/db.h" class Trx; diff --git a/src/observer/storage/default/default_storage_stage.cpp b/src/observer/storage/default/default_storage_stage.cpp index f1b7bf4..5d806ec 100644 --- a/src/observer/storage/default/default_storage_stage.cpp +++ b/src/observer/storage/default/default_storage_stage.cpp @@ -23,11 +23,11 @@ See the Mulan PSL v2 for more details. */ #include "common/log/log.h" #include "common/seda/timer_stage.h" #include "common/metrics/metrics_registry.h" -#include "rc.h" +#include "common/rc.h" #include "storage/default/default_handler.h" #include "storage/common/condition_filter.h" -#include "storage/common/table.h" -#include "storage/common/table_meta.h" +#include "storage/table/table.h" +#include "storage/table/table_meta.h" #include "storage/trx/trx.h" #include "event/session_event.h" #include "event/sql_event.h" diff --git a/src/observer/storage/default/disk_buffer_pool.cpp b/src/observer/storage/default/disk_buffer_pool.cpp index 697c839..5dea971 100644 --- a/src/observer/storage/default/disk_buffer_pool.cpp +++ b/src/observer/storage/default/disk_buffer_pool.cpp @@ -497,7 +497,7 @@ RC DiskBufferPool::flush_page_internal(Frame &frame) // so it is easier to flush data to file. Page &page = frame.page(); - s64_t offset = ((s64_t)page.page_num) * sizeof(Page); + int64_t offset = ((int64_t)page.page_num) * sizeof(Page); if (lseek(file_desc_, offset, SEEK_SET) == offset - 1) { LOG_ERROR("Failed to flush page %lld of %d due to failed to seek %s.", offset, file_desc_, strerror(errno)); return RC::IOERR_SEEK; @@ -590,7 +590,7 @@ RC DiskBufferPool::check_page_num(PageNum page_num) RC DiskBufferPool::load_page(PageNum page_num, Frame *frame) { - s64_t offset = ((s64_t)page_num) * BP_PAGE_SIZE; + int64_t offset = ((int64_t)page_num) * BP_PAGE_SIZE; if (lseek(file_desc_, offset, SEEK_SET) == -1) { LOG_ERROR("Failed to load page %s:%d, due to failed to lseek:%s.", file_name_.c_str(), page_num, strerror(errno)); diff --git a/src/observer/storage/default/disk_buffer_pool.h b/src/observer/storage/default/disk_buffer_pool.h index 83115d8..10d0e53 100644 --- a/src/observer/storage/default/disk_buffer_pool.h +++ b/src/observer/storage/default/disk_buffer_pool.h @@ -25,8 +25,8 @@ See the Mulan PSL v2 for more details. */ #include #include -#include "rc.h" -#include "defs.h" +#include "common/rc.h" +#include "common/types.h" #include "common/lang/mutex.h" #include "common/mm/mem_pool.h" #include "common/lang/lru_cache.h" diff --git a/src/observer/storage/common/field.cpp b/src/observer/storage/field/field.cpp similarity index 97% rename from src/observer/storage/common/field.cpp rename to src/observer/storage/field/field.cpp index a576de6..f486a29 100644 --- a/src/observer/storage/common/field.cpp +++ b/src/observer/storage/field/field.cpp @@ -12,7 +12,7 @@ See the Mulan PSL v2 for more details. */ // Created by Wangyunlai on 2023/04/24. // -#include "storage/common/field.h" +#include "storage/field/field.h" #include "sql/expr/tuple_cell.h" #include "storage/record/record.h" #include "common/log/log.h" diff --git a/src/observer/storage/common/field.h b/src/observer/storage/field/field.h similarity index 95% rename from src/observer/storage/common/field.h rename to src/observer/storage/field/field.h index daed5de..c2bf15b 100644 --- a/src/observer/storage/common/field.h +++ b/src/observer/storage/field/field.h @@ -14,8 +14,8 @@ See the Mulan PSL v2 for more details. */ #pragma once -#include "storage/common/table.h" -#include "storage/common/field_meta.h" +#include "storage/table/table.h" +#include "storage/field/field_meta.h" class Field { diff --git a/src/observer/storage/common/field_meta.cpp b/src/observer/storage/field/field_meta.cpp similarity index 98% rename from src/observer/storage/common/field_meta.cpp rename to src/observer/storage/field/field_meta.cpp index edb2793..db9eb48 100644 --- a/src/observer/storage/common/field_meta.cpp +++ b/src/observer/storage/field/field_meta.cpp @@ -12,8 +12,8 @@ See the Mulan PSL v2 for more details. */ // Created by Meiyi & Wangyunlai on 2021/5/12. // -#include -#include "storage/common/field_meta.h" +#include "common/lang/string.h" +#include "storage/field/field_meta.h" #include "common/log/log.h" #include "sql/parser/parse_defs.h" diff --git a/src/observer/storage/common/field_meta.h b/src/observer/storage/field/field_meta.h similarity index 98% rename from src/observer/storage/common/field_meta.h rename to src/observer/storage/field/field_meta.h index 533ff89..968d94e 100644 --- a/src/observer/storage/common/field_meta.h +++ b/src/observer/storage/field/field_meta.h @@ -16,7 +16,7 @@ See the Mulan PSL v2 for more details. */ #include -#include "rc.h" +#include "common/rc.h" #include "sql/parser/parse_defs.h" namespace Json { diff --git a/src/observer/storage/index/bplus_tree.cpp b/src/observer/storage/index/bplus_tree.cpp index e239fce..4bc5f6c 100644 --- a/src/observer/storage/index/bplus_tree.cpp +++ b/src/observer/storage/index/bplus_tree.cpp @@ -14,7 +14,6 @@ See the Mulan PSL v2 for more details. */ // #include "storage/index/bplus_tree.h" #include "storage/default/disk_buffer_pool.h" -#include "rc.h" #include "common/log/log.h" #include "sql/parser/parse_defs.h" #include "common/lang/lower_bound.h" diff --git a/src/observer/storage/index/index.h b/src/observer/storage/index/index.h index f61b8ef..8bc72c8 100644 --- a/src/observer/storage/index/index.h +++ b/src/observer/storage/index/index.h @@ -17,12 +17,13 @@ See the Mulan PSL v2 for more details. */ #include #include -#include "rc.h" -#include "storage/common/index_meta.h" -#include "storage/common/field_meta.h" +#include "common/rc.h" +#include "storage/index/index_meta.h" +#include "storage/field/field_meta.h" #include "storage/record/record_manager.h" -class IndexDataOperator { +class IndexDataOperator +{ public: virtual ~IndexDataOperator() = default; virtual int compare(const void *data1, const void *data2) const = 0; diff --git a/src/observer/storage/common/index_meta.cpp b/src/observer/storage/index/index_meta.cpp similarity index 94% rename from src/observer/storage/common/index_meta.cpp rename to src/observer/storage/index/index_meta.cpp index 81e9729..bad6c74 100644 --- a/src/observer/storage/common/index_meta.cpp +++ b/src/observer/storage/index/index_meta.cpp @@ -12,12 +12,11 @@ See the Mulan PSL v2 for more details. */ // Created by Wangyunlai.wyl on 2021/5/18. // -#include "storage/common/index_meta.h" -#include "storage/common/field_meta.h" -#include "storage/common/table_meta.h" +#include "storage/index/index_meta.h" +#include "storage/field/field_meta.h" +#include "storage/table/table_meta.h" #include "common/lang/string.h" #include "common/log/log.h" -#include "rc.h" #include "json/json.h" const static Json::StaticString FIELD_NAME("name"); diff --git a/src/observer/storage/common/index_meta.h b/src/observer/storage/index/index_meta.h similarity index 98% rename from src/observer/storage/common/index_meta.h rename to src/observer/storage/index/index_meta.h index 6b56be5..81af4d6 100644 --- a/src/observer/storage/common/index_meta.h +++ b/src/observer/storage/index/index_meta.h @@ -15,7 +15,7 @@ See the Mulan PSL v2 for more details. */ #pragma once #include -#include "rc.h" +#include "common/rc.h" class TableMeta; class FieldMeta; diff --git a/src/observer/storage/persist/persist.h b/src/observer/storage/persist/persist.h index 916eed1..044cf03 100644 --- a/src/observer/storage/persist/persist.h +++ b/src/observer/storage/persist/persist.h @@ -21,7 +21,7 @@ See the Mulan PSL v2 for more details. */ #include #include -#include "rc.h" +#include "common/rc.h" class PersistHandler { diff --git a/src/observer/storage/record/record.h b/src/observer/storage/record/record.h index 3add96f..c4b95c6 100644 --- a/src/observer/storage/record/record.h +++ b/src/observer/storage/record/record.h @@ -19,11 +19,11 @@ See the Mulan PSL v2 for more details. */ #include #include -#include "rc.h" -#include "defs.h" +#include "common/rc.h" +#include "common/types.h" #include "common/log/log.h" -#include "storage/common/index_meta.h" -#include "storage/common/field_meta.h" +#include "storage/index/index_meta.h" +#include "storage/field/field_meta.h" class Field; diff --git a/src/observer/storage/record/record_manager.cpp b/src/observer/storage/record/record_manager.cpp index 9c8c577..48b673f 100644 --- a/src/observer/storage/record/record_manager.cpp +++ b/src/observer/storage/record/record_manager.cpp @@ -12,7 +12,6 @@ See the Mulan PSL v2 for more details. */ // Created by Meiyi & Longda on 2021/4/13. // #include "storage/record/record_manager.h" -#include "rc.h" #include "common/log/log.h" #include "common/lang/bitmap.h" #include "storage/common/condition_filter.h" diff --git a/src/observer/storage/common/table.cpp b/src/observer/storage/table/table.cpp similarity index 99% rename from src/observer/storage/common/table.cpp rename to src/observer/storage/table/table.cpp index 2aab321..92dc038 100644 --- a/src/observer/storage/common/table.cpp +++ b/src/observer/storage/table/table.cpp @@ -17,8 +17,8 @@ See the Mulan PSL v2 for more details. */ #include #include "common/defs.h" -#include "storage/common/table.h" -#include "storage/common/table_meta.h" +#include "storage/table/table.h" +#include "storage/table/table_meta.h" #include "common/log/log.h" #include "common/lang/string.h" #include "storage/default/disk_buffer_pool.h" diff --git a/src/observer/storage/common/table.h b/src/observer/storage/table/table.h similarity index 98% rename from src/observer/storage/common/table.h rename to src/observer/storage/table/table.h index 8091ccc..caadb94 100644 --- a/src/observer/storage/common/table.h +++ b/src/observer/storage/table/table.h @@ -15,7 +15,7 @@ See the Mulan PSL v2 for more details. */ #pragma once #include -#include "storage/common/table_meta.h" +#include "storage/table/table_meta.h" struct RID; class Record; diff --git a/src/observer/storage/common/table_meta.cpp b/src/observer/storage/table/table_meta.cpp similarity index 99% rename from src/observer/storage/common/table_meta.cpp rename to src/observer/storage/table/table_meta.cpp index 00ecf59..a17f23e 100644 --- a/src/observer/storage/common/table_meta.cpp +++ b/src/observer/storage/table/table_meta.cpp @@ -15,7 +15,7 @@ See the Mulan PSL v2 for more details. */ #include #include -#include "storage/common/table_meta.h" +#include "storage/table/table_meta.h" #include "json/json.h" #include "common/log/log.h" #include "storage/trx/trx.h" diff --git a/src/observer/storage/common/table_meta.h b/src/observer/storage/table/table_meta.h similarity index 95% rename from src/observer/storage/common/table_meta.h rename to src/observer/storage/table/table_meta.h index 1c739c1..73e20b6 100644 --- a/src/observer/storage/common/table_meta.h +++ b/src/observer/storage/table/table_meta.h @@ -17,9 +17,9 @@ See the Mulan PSL v2 for more details. */ #include #include -#include "rc.h" -#include "storage/common/field_meta.h" -#include "storage/common/index_meta.h" +#include "common/rc.h" +#include "storage/field/field_meta.h" +#include "storage/index/index_meta.h" #include "common/lang/serializable.h" class TableMeta : public common::Serializable diff --git a/src/observer/storage/trx/latch_memo.h b/src/observer/storage/trx/latch_memo.h index 7bc469b..e5823ac 100644 --- a/src/observer/storage/trx/latch_memo.h +++ b/src/observer/storage/trx/latch_memo.h @@ -17,7 +17,7 @@ See the Mulan PSL v2 for more details. */ #include #include -#include "rc.h" +#include "common/rc.h" #include "storage/buffer/page.h" class Frame; diff --git a/src/observer/storage/trx/mvcc_trx.cpp b/src/observer/storage/trx/mvcc_trx.cpp index aee10a2..a45d401 100644 --- a/src/observer/storage/trx/mvcc_trx.cpp +++ b/src/observer/storage/trx/mvcc_trx.cpp @@ -14,9 +14,9 @@ See the Mulan PSL v2 for more details. */ #include #include "storage/trx/mvcc_trx.h" -#include "storage/common/field.h" +#include "storage/field/field.h" #include "storage/clog/clog.h" -#include "storage/common/db.h" +#include "storage/db/db.h" #include "storage/clog/clog.h" using namespace std; diff --git a/src/observer/storage/trx/trx.cpp b/src/observer/storage/trx/trx.cpp index a7dc90f..de87f07 100644 --- a/src/observer/storage/trx/trx.cpp +++ b/src/observer/storage/trx/trx.cpp @@ -15,11 +15,11 @@ See the Mulan PSL v2 for more details. */ #include #include "storage/trx/trx.h" -#include "storage/common/table.h" +#include "storage/table/table.h" #include "storage/record/record_manager.h" -#include "storage/common/field_meta.h" +#include "storage/field/field_meta.h" #include "common/log/log.h" -#include "storage/common/field.h" +#include "storage/field/field.h" #include "storage/trx/mvcc_trx.h" #include "storage/trx/vacuous_trx.h" diff --git a/src/observer/storage/trx/trx.h b/src/observer/storage/trx/trx.h index e244a1f..b83d7ea 100644 --- a/src/observer/storage/trx/trx.h +++ b/src/observer/storage/trx/trx.h @@ -21,9 +21,9 @@ See the Mulan PSL v2 for more details. */ #include "sql/parser/parse.h" #include "storage/record/record_manager.h" -#include "storage/common/field_meta.h" -#include "storage/common/table.h" -#include "rc.h" +#include "storage/field/field_meta.h" +#include "storage/table/table.h" +#include "common/rc.h" /** * @defgroup Transaction 事务模块 diff --git a/test/perf/client_performance_test.cpp b/test/perf/client_performance_test.cpp index 1a1d009..647e9f1 100644 --- a/test/perf/client_performance_test.cpp +++ b/test/perf/client_performance_test.cpp @@ -33,7 +33,8 @@ See the Mulan PSL v2 for more details. */ #define PORT_DEFAULT 6789 using namespace common; -char *server_host = (char *)LOCAL_HOST; + +char *server_host = (char *)"localhost"; int server_port = PORT_DEFAULT; void *test_server(void *param) diff --git a/unittest/bplus_tree_test.cpp b/unittest/bplus_tree_test.cpp index 4e8b2d0..2850adb 100644 --- a/unittest/bplus_tree_test.cpp +++ b/unittest/bplus_tree_test.cpp @@ -17,7 +17,6 @@ See the Mulan PSL v2 for more details. */ #include "storage/index/bplus_tree.h" #include "storage/default/disk_buffer_pool.h" -#include "rc.h" #include "common/log/log.h" #include "sql/parser/parse_defs.h" #include "gtest/gtest.h" diff --git a/unittest/persist_test.cpp b/unittest/persist_test.cpp index 3bef4a1..f165885 100644 --- a/unittest/persist_test.cpp +++ b/unittest/persist_test.cpp @@ -15,7 +15,6 @@ See the Mulan PSL v2 for more details. */ #include #include "gtest/gtest.h" #include "storage/persist/persist.h" -#include "rc.h" const int MAX_LEN = 50; -- GitLab