未验证 提交 93b79cc6 编写于 作者: 羽飞's avatar 羽飞 提交者: GitHub

Refactor (#195)

### What problem were solved in this pull request?

Issue Number: close #173 close #136 

ref #174 
ref #165 

Problem:
这个PR修改了多个问题,可以参考各个issue。包括observer编译两次、代码目录规划不合理、command executor未全部实现等

### What is changed and how it works?
做一些重构优化,包括CMakelist、一些代码目录、command executor
上级 aea25b25
......@@ -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"
......
......@@ -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"
......
......@@ -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
......
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)
......@@ -23,7 +23,6 @@ See the Mulan PSL v2 for more details. */
#include <sys/types.h>
#include <unistd.h>
#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
......@@ -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;
......
......@@ -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<std::string> &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<std::string> &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 一次性写入所有指定数据
......
......@@ -46,7 +46,7 @@ public:
std::string mBaseDir;
std::vector<std::string> mSubdirs;
pthread_mutex_t mMutex;
u32_t mPos;
uint32_t mPos;
};
} // namespace common
......
......@@ -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__ \
); \
}
......
// __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 <cstddef>
#include <stdlib.h>
#include <string.h>
#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, "<Unknown>", 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, "<Unknown>", 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 */
......@@ -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;
};
......
......@@ -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();
}
......
......@@ -16,7 +16,7 @@ See the Mulan PSL v2 for more details. */
namespace common {
u32_t getCpuNum();
uint32_t getCpuNum();
void print_stacktrace();
......
......@@ -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()) {
......
......@@ -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__
......@@ -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;
}
......
......@@ -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<HistEntry> *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
......@@ -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());
......
......@@ -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_;
}
......
......@@ -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
};
......
......@@ -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
......
......@@ -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;
......
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)
......@@ -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;
......
......@@ -18,6 +18,10 @@ class BufferPoolManager;
class DefaultHandler;
class TrxKit;
/**
* @brief 放一些全局对象
* @details 为了更好的管理全局对象,这里将其封装到一个类中。初始化的过程可以参考 init_global_objects
*/
struct GlobalContext
{
BufferPoolManager *buffer_pool_manager_ = nullptr;
......
......@@ -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__
......@@ -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());
......
......@@ -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__
......@@ -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)
{
......
......@@ -25,4 +25,3 @@ using SlotNum = int32_t;
/// LSN for log sequence number
using LSN = int32_t;
......@@ -19,8 +19,8 @@ See the Mulan PSL v2 for more details. */
#include <unistd.h>
#include <iostream>
#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"
......
......@@ -16,7 +16,7 @@ See the Mulan PSL v2 for more details. */
#include <string>
#include <event.h>
#include "rc.h"
#include "common/rc.h"
struct ConnectionContext;
class SessionEvent;
......
......@@ -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 <common/metrics/metrics_registry.h>
......
......@@ -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()
{
......
......@@ -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<int>(stmt->type()));
return RC::UNIMPLENMENT;
......
......@@ -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);
};
......@@ -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<int>(stmt->type()));
......
......@@ -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
/* 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<int>(stmt->type()));
CreateTableStmt *create_table_stmt = static_cast<CreateTableStmt *>(stmt);
const int attribute_count = static_cast<int>(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
/* 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
/* 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 <memory>
#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<int>(stmt->type()));
DescTableStmt *desc_table_stmt = static_cast<DescTableStmt *>(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<PhysicalOperator>(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
......@@ -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 <iostream>
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
......@@ -14,38 +14,17 @@ See the Mulan PSL v2 for more details. */
#include <string>
#include <sstream>
#include <memory>
#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<SQLStageEvent *>(event);
SqlResult *sql_result = sql_event->session_event()->sql_result();
const unique_ptr<PhysicalOperator> &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<PhysicalOperator>(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<int>(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<std::string> 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<PhysicalOperator>(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<PhysicalOperator>(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;
}
......@@ -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;
......
/* 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<PhysicalOperator>(oper));
return RC::SUCCESS;
}
};
\ No newline at end of file
/* 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<std::string> 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<PhysicalOperator>(oper));
return RC::SUCCESS;
}
};
\ No newline at end of file
......@@ -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"
......
/* 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
/* 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
......@@ -16,7 +16,7 @@ See the Mulan PSL v2 for more details. */
#include <string.h>
#include <memory>
#include "storage/common/field.h"
#include "storage/field/field.h"
#include "sql/expr/tuple_cell.h"
#include "common/log/log.h"
......
......@@ -14,7 +14,7 @@ See the Mulan PSL v2 for more details. */
#include <sstream>
#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"
......
......@@ -15,8 +15,8 @@ See the Mulan PSL v2 for more details. */
#pragma once
#include <iostream>
#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:
......
......@@ -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"
......
......@@ -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;
......
......@@ -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;
......
......@@ -17,7 +17,6 @@ See the Mulan PSL v2 for more details. */
#include <vector>
#include "sql/operator/physical_operator.h"
#include "sql/parser/parse.h"
#include "rc.h"
class InsertStmt;
......
......@@ -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算子
......
......@@ -18,7 +18,7 @@ See the Mulan PSL v2 for more details. */
#include <memory>
#include <string>
#include "rc.h"
#include "common/rc.h"
#include "sql/expr/tuple.h"
class Record;
......
......@@ -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<Expression> expr) : expression_(std::move(expr))
{
......
......@@ -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 表示投影运算
......
......@@ -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)
{
......
......@@ -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
{
......
......@@ -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"
/**
* 表示从表中获取数据的算子
......
......@@ -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;
......
......@@ -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;
......
......@@ -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;
......
......@@ -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;
......
......@@ -16,7 +16,7 @@ See the Mulan PSL v2 for more details. */
#include <memory>
#include "rc.h"
#include "common/rc.h"
#include "sql/operator/logical_operator.h"
#include "sql/expr/expression.h"
#include "sql/optimizer/rewrite_rule.h"
......
......@@ -16,7 +16,7 @@ See the Mulan PSL v2 for more details. */
#include <memory>
#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<LogicalOperator> &logical_operator);
RC create_logical_plan(Stmt *stmt, std::unique_ptr<LogicalOperator> &logical_operator);
RC create_select_logical_plan(SelectStmt *select_stmt, std::unique_ptr<LogicalOperator> &logical_operator);
......@@ -59,13 +72,33 @@ private:
RC create_delete_logical_plan(DeleteStmt *delete_stmt, std::unique_ptr<LogicalOperator> &logical_operator);
RC create_explain_logical_plan(ExplainStmt *explain_stmt, std::unique_ptr<LogicalOperator> &logical_operator);
/**
* @brief 重写逻辑计划
* @details 根据各种规则,对逻辑计划进行重写,比如消除多余的比较(1!=0)等。
* 规则改写也是一个递归的过程。
* @param logical_operator 要改写的逻辑计划
*/
RC rewrite(std::unique_ptr<LogicalOperator> &logical_operator);
/**
* @brief 优化逻辑计划
* @details 当前什么都没做。可以增加每个逻辑计划的代价模型,然后根据代价模型进行优化。
* @param logical_operator 需要优化的逻辑计划
*/
RC optimize(std::unique_ptr<LogicalOperator> &logical_operator);
/**
* @brief 根据逻辑计划生成物理计划
* @details 生成的物理计划就可以直接让后面的执行器完全按照物理计划执行了。
* 物理计划与逻辑计划有些不同,逻辑计划描述要干什么,比如从某张表根据什么条件获取什么数据。
* 而物理计划描述怎么做,比如如何从某张表按照什么条件获取什么数据,是否使用索引,使用哪个索引等。
* @param physical_operator 生成的物理计划。通常是一个多叉树的形状,这里就拿着根节点就可以了。
*/
RC generate_physical_plan(
std::unique_ptr<LogicalOperator> &logical_operator, std::unique_ptr<PhysicalOperator> &physical_operator);
private:
Stage *execute_stage_ = nullptr;
PhysicalPlanGenerator physical_plan_generator_;
Rewriter rewriter_;
Stage *execute_stage_ = nullptr; /// 生成计划
PhysicalPlanGenerator physical_plan_generator_; /// 根据逻辑计划生成物理计划
Rewriter rewriter_; /// 逻辑计划改写
};
......@@ -16,7 +16,7 @@ See the Mulan PSL v2 for more details. */
#include <memory>
#include "rc.h"
#include "common/rc.h"
#include "sql/operator/physical_operator.h"
#include "sql/operator/logical_operator.h"
......
......@@ -16,19 +16,21 @@ See the Mulan PSL v2 for more details. */
#include <memory>
#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<LogicalOperator> &oper, bool &change_made) = 0;
};
class ExpressionRewriteRule {
class ExpressionRewriteRule
{
public:
virtual ~ExpressionRewriteRule() = default;
......
......@@ -14,7 +14,6 @@ See the Mulan PSL v2 for more details. */
#include <mutex>
#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> command)
......
......@@ -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);
......@@ -19,19 +19,21 @@ See the Mulan PSL v2 for more details. */
#include <vector>
#include <string>
#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<RelAttr> attributes; // attributes in select clause
std::vector<std::string> relations;
std::vector<Condition> conditions;
std::vector<RelAttr> attributes; /// attributes in select clause
std::vector<std::string> relations; /// 查询的表
std::vector<Condition> conditions; /// 查询条件,使用AND串联起来多个条件
};
// struct of insert
/**
* @brief 描述一个insert语句
* @details 于Selects类似,也做了很多简化
*/
struct Inserts
{
std::string relation_name; // Relation to insert into
std::vector<Value> values;
std::string relation_name; /// Relation to insert into
std::vector<Value> values; /// 要插入的值
};
// struct of delete
/**
* @brief 描述一个delete语句
*/
struct Deletes
{
std::string relation_name; // Relation to delete from
std::vector<Condition> 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<Condition> 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<AttrInfo> 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<Command> 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<std::unique_ptr<Command>> sql_commands_;
std::vector<std::unique_ptr<Command>> sql_commands_; /// 这里记录SQL命令。虽然看起来支持多个,但是当前仅处理一个
};
const char *attr_type_to_string(AttrType type);
......
......@@ -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__
......@@ -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);
......
......@@ -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);
......
......@@ -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"
......
/* 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;
}
/* 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 <string>
#include <vector>
#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<AttrInfo> &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<AttrInfo> &attr_infos() const { return attr_infos_; }
static RC create(Db *db, const CreateTable &create_table, Stmt *&stmt);
private:
std::string table_name_;
std::vector<AttrInfo> attr_infos_;
};
\ No newline at end of file
......@@ -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)
{}
......
......@@ -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;
......
/* 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;
}
/* 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 <string>
#include <vector>
#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
/* 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 <string>
#include <vector>
#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
......@@ -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()
{
......
......@@ -16,7 +16,6 @@ See the Mulan PSL v2 for more details. */
#include <vector>
#include <unordered_map>
#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();
......
/* 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 <string>
#include <vector>
#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
......@@ -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)
......
......@@ -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;
......
......@@ -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()
{
......
......@@ -16,9 +16,9 @@ See the Mulan PSL v2 for more details. */
#include <vector>
#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;
......
......@@ -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 <string>
#include <vector>
#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
......@@ -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;
......
......@@ -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
{
......
/* 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 <string>
#include <vector>
#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
/* 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 <string>
#include <vector>
#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
......@@ -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;
......
......@@ -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
{
......
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册