diff --git a/benchmark/bplus_tree_concurrency_test.cpp b/benchmark/bplus_tree_concurrency_test.cpp index f8e1656c7b230467a5783929e5bf070ef97ede4e..605c4d2fe07c18abda4723c6c10a333aa76abd80 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 4fca60db8be38d2aa78b5b061ccee1abf2c6047d..b68e766be0085b8d381dffc1f76e652ebbde5d31 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 8d6d735d0d44ed10e136b7f7956c60b4bc00dd95..bb436b531f6678672fa4911577ccc0583b215ee1 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 feb5192f6ab269a864a6005d179b60e06fb8bb0c..61b570e85225dfd985e1efe8ab967a2a95fdc794 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 087395a6d96f1156419ac1a0883f84fbbdae3774..4ec40df5b36fe5b01bf35cb9e3c59fd049d03bde 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 916671dc34f7ef1a36b30c1aa9f5a25b84b53237..0ddc296f6ac78c6717b29206454df2c8bef40a5e 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 92eea6e7dd1bbd1a344c1d14510ea21b7fbd7911..9c28da7ce78f45d158dfaf44c2f7883e9e3c2e78 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 bac1bcac39807b50d3baf8c4c588ac88ba99ca48..1d369652b7ad5041f6ef9c67520f48fb9e630fb4 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 272ce40c63ed05572714fd7f9d886de3a6e3accf..173669f60f34b45ea50624e88cc229c409e8a163 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 f50cbf4379353d07af0bbddc7acab60af7a85a08..0000000000000000000000000000000000000000 --- 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 36178738622f2158bc5d0d801aa1491d8d09210a..06ee951cd120bc537ab6b7b4a630f03d81b8c88f 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 f17ef493ed479fca9ea98c440042c07c972a5ca7..2b0690b5cb7502cdbc779fc2f6dbad45cd9bd8a9 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 33c653a9dfea37f7074e2ebfede49f2d35532ea0..9574f6679495aaa155d6d085d3d2777f4793c53b 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 4dbffd3e1c63c8e281c6c56fc2e582db2b4d5478..cc1a6d7279b2ed5ea15c5cc47cab6e649c1ee576 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 ed019a89771c2644ad0d0246babbac2999f45f03..8b7ccb36d49145e5c94a5437c9f15eac035d7d6b 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 15929df9797f8181b7ca5626cc9e1f024cf77a8b..e5e7787cc668dd6c3ac9debbbb4011e55be45b18 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 ffb86dec031e1047c6fbcfdd6840b8a1ced1a8b9..a207264f5650ef04709e48089111225d9491f24e 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 4ad6b7af2c5d88b8b4a1a7752eaeeed744a6d0f9..f6a48a53b31bbd116954819bfa8c827fbb04e57e 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 c4f69f39dd2d05b44196800c5225344b40b99a83..d7808eaaacdb4a8110e4d45319da3474c02affa4 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 e769aa91f831cd402e983a35d5c318e002360b8a..5f2d4455a4bf1020e3d0c60e4ed4f49ea5ce78b4 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 c24515a7512228d4c5a6b0ffa27b1c3119266e73..fcff9ab779f4107cf271e57b8de22e7bc0d72340 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 c04ef64dd7b59830eee799e32dfdcd5f111c3df5..7e779be1f1dcfbf88814450446148a4284038d72 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 3d1af91c91fcc447b59bb28cc9f4c7dd546a14c0..393ba7d137a577692057b212eec8b1c0882f9dc2 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 6358967a9420b74a070138c40a7495a08ad5ee58..4931a746b00f57cb13585529aa2133c4143ea33a 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 7362d754f081af92a57048c23102cb08f5efe593..6b5920f7441b6cbeb7dc03f74b796387a1315c43 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 ea3d126507a254cb8c0e481da350934338c1f7bc..d9ada86d23aeaa1255842a1d1748664f916b13ca 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 ccaa0fab3b3ac10c0c7b43d88ef3898d38f0a4ef..5f92ff8bba0d141aa71a6514c6663e1f14385d91 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 84518646ebc5c872de93add37f886f9853d5f0dc..2993b238b6a6964c391255c652024ef8cec486db 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 d1801acd4d4cc8983badaedde4e4b14f4623a469..313a928429da5161bcb286ac913497c414004006 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 3d55e858a20b9dece8849ae5ed94b4972d46c4e8..28606471875dd1aba011003dc614db2e7bd1f452 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 a48aad33f46cc06f200eb4201fc33e680aef4618..601d3604467ee339bb038e3b380167235bfde230 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 5511de5a0c6e47244f824e8483119daf8a45380c..774a42443baabd06b3caa2674d9d4017247059a4 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 0fe34741b0a95900df44efbb70ef9e6129311a40..aaf990f7894709261056f0cd6c161c5accf0c814 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 e6e25e59c9e8630e42bd9d556158d3ec5364bc76..8b95461b2ec47726aefd1069b1f092eff1b8357f 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 846da033cc4a190fe1ad56bdd9fb43bacf15293e..541d5f6cbf044fac8867a0edcb78fac3a0a3d5b7 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 4ea7932b94c8109a27ff47a8bf317c5c9a3c6397..413bd0307024f6c3378a8e133eb8aa05a23b9938 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 98c066ec63922acb8340ee0c5368c70472e676c3..35797702a6ef285e2b0373b045946d8344beab04 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 8684ed4ca4c953bca3fe072b397a080ae7971927..9ce86577ba76efc9cd834bf671a3b2c5915c0a3c 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 0000000000000000000000000000000000000000..8922afe563c18bd440c17b90c82771ebf20de669 --- /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 0000000000000000000000000000000000000000..10cf09b6e28ff6c5947eea740b81144be6341d1a --- /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 0000000000000000000000000000000000000000..2feef24bc0fee07b5c59e6959f39340496163e20 --- /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 50e8387ccd800bb951b4042e1b0c50fa6660f345..6a547b2574ed52f0741c0e17249543cc115ef3cf 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 ab60abbc3820dfc3e6c060abd8efc1a084aede7e..5638775111a3476595bba140cd71449cef0b2d7d 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 95f3e6a7bba2ac08279c7057dc1465d0a7781520..b7826e211af7478b8c0507818e3e596e1bd0116c 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 0000000000000000000000000000000000000000..af8864fb8533be815af9fe1d2ebba3f330c567a5 --- /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 0000000000000000000000000000000000000000..5a7599c08d97fcc5da7d2b84f227dce39f27af34 --- /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 ce532fd33417d818ac1e345b13670507151c79a0..5e565d3268edcc0c70ec0cd9bae3a310a5c7f8ff 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 0000000000000000000000000000000000000000..90cf4a072d3bbd25b17d116627b65a10a629c64f --- /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 0000000000000000000000000000000000000000..e7bb9c6d736f1b752072b281c67576d5449a264c --- /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 93bc2f911e343123b93fa529ab855d19792c7a59..8be6fb8ab64747da03023cd5f8cbb4ed7bcfcbdf 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 f199de28a3bd8be85839d29ed57cc3e5e741b15f..152e25a134084d56779fabee84656546f22b6636 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 2c101cccf3b92b8be874e12e53ff3fe0adf083a9..8cc48c35886812258e60af8e867cb6aabf338071 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 22d2dc9b7e9c4c183e1317f2d62c2c16b5989c8c..3ebb35db97eb85e3a798ee545b8dff4397be8fec 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 b210034de61e88210b3a1f12bf7c2966f1c2d752..56d05a145468ff3ed7a96b554d7f81c44a1c4d93 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 2bb15086d3a910efbb580edaf1de626a56dc3dba..0abfc5cbf63d53968ef8c8fd32fb7c41e003a939 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 bc08444a7ac8a4d102c37bb11a4aa12c075d241c..62151a55472ccaef19650de67b3957d4d78197f2 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 55edc808f859f921a9bfe81120e934be1fb40c5d..8f507ad4637003b6f7da17fededf6bccb1e98a69 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 cb871c63f67ef629c56fedcd9b3b51957f470bd9..55d19dc4041e19788d80bdbcb591ac6c71a73911 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 416640b93e871302523b80a86607f81ef31d27c3..273ebb05c5743722f46f13e31f79c656dece9062 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 4a856c0cb3cf7bd4fd5654b878520a899c13ad2a..ecde59cc08fd97e4a5fc75b5510bae9b39563a75 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 f8f8c35584a0051f11cfc51c84a36784977827ce..7c700cc2c1c51c475bc259d79c87cadc3515fa03 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 e69028a3a29a70be8264e5439fc2f7b6a492ce67..15926b02494bf1986b4a0fcf4c0fe06ac9dbb168 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 448f3bc590522a8442f0d01825f02966bdf3037b..0c15360134f20020a242aa8c63b4783cf28ed85e 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 db656bf136624492e9fbf2d24e879a42a4045fe7..612ade66cacb759cd764bd0064b0626b5c39db97 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 fa5a35e2440852c208534a23d9d0fa18405dc6c9..7933fb1b1fa49a710c93a821d431bbd37735cfca 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 5a76e24117ee391bfd70baf8a83364745fe6f64c..4be9432994cc3c6633368a328d5524f0ba655faf 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 882a0099bd4361683e5a9658e88b3a511ea869a5..74cfe80c7357e864208d52708400fe13445b481b 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 4948d60966283ef4416a8e7c125e639557f9d52f..cb5dfd014f16ae0a054400d862e745fab34d4bef 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 75c2ccb5354c809b0b5062218ebd5791d53558c2..5a10cb3b5959ea4983f438c9ae0447b7386c59c9 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 1d567af428f86cd3eac42f7882b4d5f47e082360..ab77898e84a10258e02915c14f8d731cb6404742 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 0ddd2c53198cce22f4c2f018894870d39d06d07a..22769d84958e8012b186505eab089afbae3b52bf 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 6615afb97b61ed2da5c5289d8371cca588b5100d..16db0ebde29f174cfad062c3b1301b59c38b1f2a 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 87dd475ee96d06c0867000bbe9bdfaeeddf9ded5..a654c7d9519ab1306bcc63601a6f819b5d042d54 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 d6df389da7ada8638c4395899999fddcd6a3c5dc..1c6e488ad01a16ef7d81a0de33d50ba4981f808b 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 f6905a13ca9875a3388f962eee9f51e4810028b1..50400aa3a5b780d73fe446a4c727b0bf399d8022 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 e4bf3245031f67045fdab2559f63818bc18bed40..4c13b0881bb4c3b0ce74b3a5eabb08f3716b7039 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 e7647773f9a912348e7bcdb6c77bb0be89ceb676..e48e0ad35281065e38f026eb96eae029377eec78 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 900b0f48bb2d179ffc80dcdac1fee0d0adbefca9..629aece694fe5c03c40fe7351e1dcde99bc3efd6 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 0000000000000000000000000000000000000000..82d1073fbfcaa20db9f7b8f86e8a639f6562c12f --- /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 0000000000000000000000000000000000000000..1db6c1978dc77d030652445f1ab4dad6fc6dfac5 --- /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 8941cd6f6b6192ba064cb6858653c955b31b2067..e467741b592d920f3b11d54d5957a7cf69b6e635 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 5641e99f988c2ae10f7286619c856f4619f0181d..54914fbff53277ceafb827af43f1f3ccc1529266 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 0000000000000000000000000000000000000000..1718e6f3cffe02cc81574f3be5a8405d355c5f94 --- /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 0000000000000000000000000000000000000000..53605685eeb43e1a8b31d4242e6c19c6e10ffec1 --- /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 0000000000000000000000000000000000000000..e7eb29c745b90f56fe8089a7a129912ce6be177f --- /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 5183b93f3d4a9da0e7eaca9a43be71fb4d53bfc6..3c04b639daa257d5c66c15d34ba99e19e3201571 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 5e0b290407e2f8222352c9902e9fdf01dc9ef160..31bef245cbeb47247f0f7bccd65ff297eb6c5a38 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 0000000000000000000000000000000000000000..8b3b880d186829dff754a3dd4b78ff19b7c17088 --- /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 9ad1f0e0a93a24ff5cec83d18ef19c69775bd505..64731a22eaeeedcc76123496cf7fe05babac7914 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 bf6dcb7246e2d4b2d85f877944218aa3ed95e07e..3af4f5e968e8ac79d7d7c4f1ce82a9433f9997a3 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 6e431ec5643b4ac6babdb5be87c03ecb0b3bcfc4..13571cd846790452b5306b82283d48d657f208a6 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 bb251777cf686d35f3ca781a1673cdaa5892c571..c6766a88e95771dc554dd34327e04c5752fc5294 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 0c64228545f9f797d3a0c42acbd292aa1e94633a..08b456cfde5323de86764201a484c8c33b7fcc6b 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 b4702ec048312dc20e10d2d1d510141be1651d84..8bb3d66ef4fa4dbe34c3e01b99fcb77d0b56fd88 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 9f795cfe979e56587a8ed153d288878b3e4d9835..2b1ccafb1fe8960b94b55e2f041eb8f25dc7616b 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 0000000000000000000000000000000000000000..f1335d9d1d71835ca8a744b46bcc65764b3cddad --- /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 0000000000000000000000000000000000000000..fc126b71e6a551d7231b7a38b7135e33af237ef8 --- /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 4b8f7f7d24f009ae155d0ec98ed70894dcdadc0b..3148bb2a78de082fd3515462c14f8222a8513880 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 9f973a57c5568f04e5bb422dd4dc87218807856d..47ec590eda4346454e0ef25dc7733961390196da 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 bda072830d02e575bb36e067ee39c49e4a2778b1..0f08852015e2563cf5302ce9010a7abbcff9aee7 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 90d7e9fa3df5929bfadab368ba1b0f9376265762..32899ee74cc5a9e67e58bbc9eec436f0dbc66422 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 c49f17196c6848b009673133dc2b3f0c9620cc62..0d0c100eea41923a3b2548e199c68b3c295d75a9 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 9e2570dba543018bc3b2574fab53ec11c11a0452..30ce6441bb6fb4cbbbeb2c11230823423810c312 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 265fedea1940c66724f3b1bbfc01c219fb51d44b..06002d37bd0cce34735c4d1fe532c5fc3b86cba5 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 5c6d3164468c1ad6ef1c84fd21e49c02474b9f8d..b84e526dbc345005b2cec753fe4e4bfebe6f8584 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 c48dfde098b7d65556f08ae31ce18d2b75be8d1a..599df9a42964b279560511cd86a658477f607eee 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 b75a930290f14e307d5b7f2db1ecb04d58d2cfc6..cc85f6bd76fa69ca004ba47627b7ecc4527e7339 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 1ee17e427f4a6790bc14ad1746ecc67d5a56e35e..f172d5b1540100aac581daed75f78786c8f18d82 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 f1b7bf40623f3dab043483d73fc9f593ddec2907..5d806ec499047317ea963c5fdd2d70b30b78b0a5 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 697c8392a53c9abecaee1e44f87f9bd4b2bf8573..5dea9712dc2e442f3c4d9134bac8aee621347348 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 83115d8d9d43047fe8000d9b17685a4466b399e3..10d0e53a8ee5cec06170b49bb0cb95f860a43a74 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 a576de611e0698d9a8de777275f901323b1a6e6b..f486a29741a9bf58dd2bceddda78a33734e57c8c 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 daed5de71def84beedddcbddb843051f4e4d2c25..c2bf15b89300fdcd8261cf90bd1490b2bea97b61 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 edb27932024d55ae80c9d935dff05634a705021d..db9eb48a43121a83c52d5fd580e7cf81a29e5f99 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 533ff897ae4f13ca548aec01d28cd941b134d64c..968d94e72cca4451f71c5b9bc3255a4ed9791758 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 e239fce7d8bcffd94c8784e2c3ffd20c39acc405..4bc5f6c0e2772a008fd4d7c36fb02c51b2a48acb 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 f61b8ef45ef4bd8a81c73614f648326764715439..8bc72c86e3dfdd79193e733c78ff16f0dc176407 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 81e97294af0e624e43c760390e8ee524d89ffd7e..bad6c74966cc90d9b44ca582b0af5346cf0392be 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 6b56be55ebd2ef7f2bce95540c17bf82cb7c3e68..81af4d6a2d1c867cd65fc239e011038c03d89844 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 916eed1b2057f22232caa21c1f177d2f307d5bfd..044cf03a6ec8d13ba7c7fe814393b0a064056919 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 3add96fcd7c89e90ed78e19dda01cdf0c99e8fc7..c4b95c60cbc813012ccd21d6d475920d403a5242 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 9c8c57782c45c572a0fcc6bde999b2d8b2c771ec..48b673f173aeb2893137d33f782fd205be7653e6 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 2aab3218eb97192e91c7da400bc4f959b224bd6d..92dc038a336102f4e12d55ec6d801c8a836bee70 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 8091ccca62d1947c165d5e29135ec82e027a2dc8..caadb947816c2154665e4a48692428093094662b 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 00ecf59821ecd0c1986a59dc646d7681e5365059..a17f23e0819b756d8e803f6236bba015eb2bf03c 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 1c739c1751c641fcb1f43e851895f96cde44c206..73e20b6bce7cfed48b465e4f5bb68e098396257d 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 7bc469ba449555c876e5e103cbf01afa692da6a2..e5823ac8a225b9b6aa1f3c7f4316d6a5785c8666 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 aee10a2235501d5f452d0f2f10a5c60877cff95e..a45d40110b8787d545f7f68bde253df95d21b324 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 a7dc90f16c86b90c051d373daf3ed0cee16f1321..de87f0770945075458e3fc5d1319012cd098b399 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 e244a1f85c87109f9b513ceab733870bd142be1f..b83d7ea0c146c0f8289e643c35ad9a1b30994d0d 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 1a1d009f1b03b847f96e2c0e429fadb2d09024b0..647e9f126057ad3d74f6aec54c1cce6b1da999ff 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 4e8b2d01256159b2cd307a44d285b0d837de9e02..2850adbab1c29603376c2ac77f5ae0885a4d7680 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 3bef4a1e780a00e6d707d840dae88b0132645cb7..f165885d824d88e13016fab97174cce0ec035b8a 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;