diff --git a/.clang-format b/.clang-format index 6b231cb5e..9b3a17f68 100644 --- a/.clang-format +++ b/.clang-format @@ -21,7 +21,7 @@ AlwaysBreakTemplateDeclarations: true # false表示函数实参要么都在同一行,要么都各自一行 BinPackArguments: false # 避免格式化 LOG_* 宏 -WhitespaceSensitiveMacros: ['LOG_DEFAULT', 'LOG_PANIC', 'LOG_ERROR', 'LOG_WARN', 'LOG_INFO', 'LOG_DEBUG', 'LOG_TRACE'] +WhitespaceSensitiveMacros: ['LOG_DEFAULT', 'LOG_PANIC', 'LOG_ERROR', 'LOG_WARN', 'LOG_INFO', 'LOG_DEBUG', 'LOG_TRACE', 'ASSERT'] AlwaysBreakBeforeMultilineStrings: false BreakBeforeBinaryOperators: None BreakBeforeTernaryOperators: true diff --git a/.github/workflows/clang-format.yml b/.github/workflows/clang-format.yml new file mode 100644 index 000000000..13fceb302 --- /dev/null +++ b/.github/workflows/clang-format.yml @@ -0,0 +1,16 @@ +name: Clang Format Checker +on: + pull_request: + branches: [ "main" ] +jobs: + clang-format-checking: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v2 + - uses: RafikFarhad/clang-format-github-action@v3 + with: + # sources 和 excludes 最终生成的find查找文件的命令大概是这样的 + # find . -type f ! -wholename "./src/observer/sql/parser/lex_sql.*" \( -wholename "./**/*.h" -o -wholename "./**/*.cpp" \) + sources: "**/*.h,**/*.cpp,src/**/*.h,src/**/*.cpp,deps/common/**/*.h,deps/common/**/*.cpp" + excludes: "src/observer/sql/parser/lex_sql.*,src/observer/sql/parser/yacc_sql.*" + style: "file" \ No newline at end of file diff --git a/.github/workflows/codeql.yml b/.github/workflows/codeql.yml index 8070ca32a..2f09cd8fa 100644 --- a/.github/workflows/codeql.yml +++ b/.github/workflows/codeql.yml @@ -12,11 +12,6 @@ name: "CodeQL" on: - push: - branches: [ "main" ] - pull_request: - # The branches below must be a subset of the branches above - branches: [ "main" ] schedule: - cron: '37 12 * * 5' diff --git a/benchmark/bplus_tree_concurrency_test.cpp b/benchmark/bplus_tree_concurrency_test.cpp index 9f46bac06..fc9b36c53 100644 --- a/benchmark/bplus_tree_concurrency_test.cpp +++ b/benchmark/bplus_tree_concurrency_test.cpp @@ -11,14 +11,14 @@ See the Mulan PSL v2 for more details. */ // // Created by Wangyunlai on 2023/03/14 // +#include #include #include -#include -#include "storage/index/bplus_tree.h" -#include "storage/buffer/disk_buffer_pool.h" #include "common/log/log.h" #include "integer_generator.h" +#include "storage/buffer/disk_buffer_pool.h" +#include "storage/index/bplus_tree.h" using namespace std; using namespace common; diff --git a/benchmark/record_manager_concurrency_test.cpp b/benchmark/record_manager_concurrency_test.cpp index 218ae6fe9..94b4328bf 100644 --- a/benchmark/record_manager_concurrency_test.cpp +++ b/benchmark/record_manager_concurrency_test.cpp @@ -12,17 +12,17 @@ See the Mulan PSL v2 for more details. */ // Created by Wangyunlai on 2023/05/04 // +#include #include #include #include -#include -#include "storage/record/record_manager.h" +#include "common/log/log.h" +#include "integer_generator.h" #include "storage/buffer/disk_buffer_pool.h" #include "storage/common/condition_filter.h" +#include "storage/record/record_manager.h" #include "storage/trx/vacuous_trx.h" -#include "common/log/log.h" -#include "integer_generator.h" using namespace std; using namespace common; diff --git a/benchmark/server_concurrency_test.cpp b/benchmark/server_concurrency_test.cpp index 1e606e4bd..89bf6b74e 100644 --- a/benchmark/server_concurrency_test.cpp +++ b/benchmark/server_concurrency_test.cpp @@ -13,17 +13,17 @@ See the Mulan PSL v2 for more details. */ // #if 0 +#include #include #include #include -#include +#include #include #include -#include -#include "rc.h" #include "common/log/log.h" +#include "rc.h" using namespace std; using namespace common; diff --git a/deps/common/conf/ini.cpp b/deps/common/conf/ini.cpp index 3e8d84ea7..63c55269f 100644 --- a/deps/common/conf/ini.cpp +++ b/deps/common/conf/ini.cpp @@ -24,18 +24,16 @@ See the Mulan PSL v2 for more details. */ namespace common { -const std::string Ini::DEFAULT_SECTION = std::string(""); +const std::string Ini::DEFAULT_SECTION = std::string(""); const std::map Ini::empty_map_; -Ini::Ini() -{} +Ini::Ini() {} -Ini::~Ini() -{} +Ini::~Ini() {} void Ini::insert_session(const std::string &session_name) { - std::map session_map; + std::map session_map; std::pair> entry = std::pair>(session_name, session_map); @@ -103,7 +101,7 @@ int Ini::insert_entry(std::map *session_map, const std return -1; } - std::string key = line.substr(0, equal_pos); + std::string key = line.substr(0, equal_pos); std::string value = line.substr(equal_pos + 1); strip(key); @@ -150,7 +148,7 @@ int Ini::load(const std::string &file_name) if (read_buf[0] == CFG_SESSION_START_TAG && read_buf[strlen(read_buf) - 1] == CFG_SESSION_END_TAG) { read_buf[strlen(read_buf) - 1] = '\0'; - std::string session_name = std::string(read_buf + 1); + std::string session_name = std::string(read_buf + 1); current_session = switch_session(session_name); diff --git a/deps/common/conf/ini.h b/deps/common/conf/ini.h index 384e3c380..ec10eb79d 100644 --- a/deps/common/conf/ini.h +++ b/deps/common/conf/ini.h @@ -25,12 +25,13 @@ See the Mulan PSL v2 for more details. */ namespace common { //******************************************************************** -//#means comments +// #means comments // Ini configuration format //[section] // VARNAME=VALUE -class Ini { +class Ini +{ public: /** * To simplify the logic, no lock's when loading configuration @@ -88,7 +89,7 @@ class Ini { // session name tag static const char CFG_SESSION_START_TAG = '['; - static const char CFG_SESSION_END_TAG = ']'; + static const char CFG_SESSION_END_TAG = ']'; protected: /** @@ -115,7 +116,7 @@ class Ini { static const std::map empty_map_; std::set file_names_; - SessionsMap sections_; + SessionsMap sections_; }; /** diff --git a/deps/common/defs.h b/deps/common/defs.h index 4ec40df5b..0c71a0a1e 100644 --- a/deps/common/defs.h +++ b/deps/common/defs.h @@ -34,7 +34,7 @@ namespace common { #endif -enum +enum { // General Error Codes STATUS_SUCCESS = 0, //!< Success status should be zero, @@ -50,7 +50,7 @@ enum STATUS_LAST_ERR //!< last error code }; -static const char FILE_PATH_SPLIT = '/'; +static const char FILE_PATH_SPLIT = '/'; static const char FILE_PATH_SPLIT_STR[] = "/"; #define EPSILON (1E-6) diff --git a/deps/common/io/io.cpp b/deps/common/io/io.cpp index 0ddc296f6..9fe483961 100644 --- a/deps/common/io/io.cpp +++ b/deps/common/io/io.cpp @@ -39,9 +39,9 @@ int readFromFile(const std::string &fileName, char *&outputData, size_t &fileSiz // size_t fsSize = ftell( file ); // fseek( file, 0, SEEK_SET ); - char buffer[4 * ONE_KILO]; + char buffer[4 * ONE_KILO]; size_t readSize = 0; - size_t oneRead = 0; + size_t oneRead = 0; char *data = NULL; do { @@ -72,10 +72,10 @@ int readFromFile(const std::string &fileName, char *&outputData, size_t &fileSiz fclose(file); - data = (char *)lrealloc(data, readSize + 1); + data = (char *)lrealloc(data, readSize + 1); data[readSize] = '\0'; - outputData = data; - fileSize = readSize; + outputData = data; + fileSize = readSize; return 0; } @@ -87,8 +87,8 @@ int writeToFile(const std::string &fileName, const char *data, uint32_t dataSize return -1; } - uint32_t leftSize = dataSize; - const char *buffer = data; + uint32_t leftSize = dataSize; + const char *buffer = data; while (leftSize > 0) { int writeCount = fwrite(buffer, 1, leftSize, file); if (writeCount <= 0) { @@ -134,15 +134,15 @@ int getFileNum(int64_t &fileNum, const std::string &path, const std::string &pat { try { DIR *dirp = NULL; - dirp = opendir(path.c_str()); + dirp = opendir(path.c_str()); if (dirp == NULL) { std::cerr << "Failed to opendir " << path << SYS_OUTPUT_FILE_POS << SYS_OUTPUT_ERROR << std::endl; return -1; } - std::string fullPath; + std::string fullPath; struct dirent *entry = NULL; - struct stat fs; + struct stat fs; while ((entry = readdir(dirp)) != NULL) { // don't care ".", "..", ".****" hidden files if (!strncmp(entry->d_name, ".", 1)) { @@ -197,15 +197,15 @@ int getFileList(std::vector &fileList, const std::string &path, con { try { DIR *dirp = NULL; - dirp = opendir(path.c_str()); + dirp = opendir(path.c_str()); if (dirp == NULL) { std::cerr << "Failed to opendir " << path << SYS_OUTPUT_FILE_POS << SYS_OUTPUT_ERROR << std::endl; return -1; } - std::string fullPath; + std::string fullPath; struct dirent *entry = NULL; - struct stat fs; + struct stat fs; while ((entry = readdir(dirp)) != NULL) { // don't care ".", "..", ".****" hidden files if (!strncmp(entry->d_name, ".", 1)) { @@ -259,15 +259,15 @@ int getDirList(std::vector &dirList, const std::string &path, const { try { DIR *dirp = NULL; - dirp = opendir(path.c_str()); + dirp = opendir(path.c_str()); if (dirp == NULL) { std::cerr << "Failed to opendir " << path << SYS_OUTPUT_FILE_POS << SYS_OUTPUT_ERROR << std::endl; return -1; } - std::string fullPath; + std::string fullPath; struct dirent *entry = NULL; - struct stat fs; + struct stat fs; while ((entry = readdir(dirp)) != NULL) { // don't care ".", "..", ".****" hidden files if (!strncmp(entry->d_name, ".", 1)) { @@ -354,17 +354,17 @@ int getFileSize(const char *filePath, int64_t &fileLen) int writen(int fd, const void *buf, int size) { const char *tmp = (const char *)buf; - while ( size > 0) { + while (size > 0) { const ssize_t ret = ::write(fd, tmp, size); if (ret >= 0) { - tmp += ret; + tmp += ret; size -= ret; continue; } const int err = errno; if (EAGAIN != err && EINTR != err) return err; - } + } return 0; } @@ -374,12 +374,12 @@ int readn(int fd, void *buf, int size) while (size > 0) { const ssize_t ret = ::read(fd, tmp, size); if (ret > 0) { - tmp += ret; + tmp += ret; size -= ret; continue; } if (0 == ret) - return -1; // end of file + return -1; // end of file const int err = errno; if (EAGAIN != err && EINTR != err) diff --git a/deps/common/io/io.h b/deps/common/io/io.h index 9c28da7ce..ad9a18655 100644 --- a/deps/common/io/io.h +++ b/deps/common/io/io.h @@ -59,7 +59,7 @@ int getFileSize(const char *filePath, uint64_t &fileLen); /** * @brief 一次性写入所有指定数据 - * + * * @param fd 写入的描述符 * @param buf 写入的数据 * @param size 写入多少数据 @@ -69,7 +69,7 @@ int writen(int fd, const void *buf, int size); /** * @brief 一次性读取指定长度的数据 - * + * * @param fd 读取的描述符 * @param buf 读取到这里 * @param size 读取的数据长度 diff --git a/deps/common/io/roll_select_dir.cpp b/deps/common/io/roll_select_dir.cpp index 7b33fef33..1a69d235c 100644 --- a/deps/common/io/roll_select_dir.cpp +++ b/deps/common/io/roll_select_dir.cpp @@ -22,7 +22,7 @@ void RollSelectDir::setBaseDir(std::string baseDir) mBaseDir = baseDir; std::vector dirList; - int rc = getDirList(dirList, mBaseDir, ""); + int rc = getDirList(dirList, mBaseDir, ""); if (rc) { LOG_ERROR("Failed to all subdir entry"); } @@ -40,7 +40,7 @@ void RollSelectDir::setBaseDir(std::string baseDir) MUTEX_LOCK(&mMutex); mSubdirs = dirList; - mPos = 0; + mPos = 0; MUTEX_UNLOCK(&mMutex); return; } diff --git a/deps/common/io/roll_select_dir.h b/deps/common/io/roll_select_dir.h index 1d369652b..2ecf6b0d1 100644 --- a/deps/common/io/roll_select_dir.h +++ b/deps/common/io/roll_select_dir.h @@ -24,29 +24,24 @@ See the Mulan PSL v2 for more details. */ #include "common/lang/mutex.h" namespace common { -class RollSelectDir : public SelectDir { +class RollSelectDir : public SelectDir +{ public: - RollSelectDir() - { - MUTEX_INIT(&mMutex, NULL); - } - ~RollSelectDir() - { - MUTEX_DESTROY(&mMutex); - } + RollSelectDir() { MUTEX_INIT(&mMutex, NULL); } + ~RollSelectDir() { MUTEX_DESTROY(&mMutex); } public: /** * inherit from CSelectDir */ std::string select(); - void setBaseDir(std::string baseDir); + void setBaseDir(std::string baseDir); public: - std::string mBaseDir; + std::string mBaseDir; std::vector mSubdirs; - pthread_mutex_t mMutex; - uint32_t mPos; + pthread_mutex_t mMutex; + uint32_t mPos; }; } // namespace common diff --git a/deps/common/io/select_dir.h b/deps/common/io/select_dir.h index d544d1005..de68d1113 100644 --- a/deps/common/io/select_dir.h +++ b/deps/common/io/select_dir.h @@ -17,13 +17,11 @@ See the Mulan PSL v2 for more details. */ #include namespace common { -class SelectDir { +class SelectDir +{ public: - virtual std::string select() - { - return std::string(""); - }; - virtual void setBaseDir(std::string baseDir){}; + virtual std::string select() { return std::string(""); }; + virtual void setBaseDir(std::string baseDir){}; }; } // namespace common diff --git a/deps/common/lang/bitmap.cpp b/deps/common/lang/bitmap.cpp index 097167eab..e8e8ac6f2 100644 --- a/deps/common/lang/bitmap.cpp +++ b/deps/common/lang/bitmap.cpp @@ -36,20 +36,15 @@ int find_first_setted(char byte, int start) return -1; } -int bytes(int size) -{ - return size % 8 == 0 ? size / 8 : size / 8 + 1; -} +int bytes(int size) { return size % 8 == 0 ? size / 8 : size / 8 + 1; } -Bitmap::Bitmap() : bitmap_(nullptr), size_(0) -{} -Bitmap::Bitmap(char *bitmap, int size) : bitmap_(bitmap), size_(size) -{} +Bitmap::Bitmap() : bitmap_(nullptr), size_(0) {} +Bitmap::Bitmap(char *bitmap, int size) : bitmap_(bitmap), size_(size) {} void Bitmap::init(char *bitmap, int size) { bitmap_ = bitmap; - size_ = size; + size_ = size; } bool Bitmap::get_bit(int index) @@ -72,7 +67,7 @@ void Bitmap::clear_bit(int index) int Bitmap::next_unsetted_bit(int start) { - int ret = -1; + int ret = -1; int start_in_byte = start % 8; for (int iter = start / 8, end = bytes(size_); iter < end; iter++) { char byte = bitmap_[iter]; @@ -95,7 +90,7 @@ int Bitmap::next_unsetted_bit(int start) int Bitmap::next_setted_bit(int start) { - int ret = -1; + int ret = -1; int start_in_byte = start % 8; for (int iter = start / 8, end = bytes(size_); iter < end; iter++) { char byte = bitmap_[iter]; diff --git a/deps/common/lang/bitmap.h b/deps/common/lang/bitmap.h index 012302dc6..8f2575d16 100644 --- a/deps/common/lang/bitmap.h +++ b/deps/common/lang/bitmap.h @@ -16,7 +16,8 @@ See the Mulan PSL v2 for more details. */ namespace common { -class Bitmap { +class Bitmap +{ public: Bitmap(); Bitmap(char *bitmap, int size); @@ -34,7 +35,7 @@ class Bitmap { private: char *bitmap_; - int size_; + int size_; }; } // namespace common diff --git a/deps/common/lang/comparator.cpp b/deps/common/lang/comparator.cpp index 3214f6e17..40edc64f3 100644 --- a/deps/common/lang/comparator.cpp +++ b/deps/common/lang/comparator.cpp @@ -12,13 +12,12 @@ See the Mulan PSL v2 for more details. */ // Created by wangyunlai on 2021/6/11. // -#include -#include #include "common/defs.h" +#include +#include namespace common { - int compare_int(void *arg1, void *arg2) { int v1 = *(int *)arg1; @@ -34,8 +33,8 @@ int compare_int(void *arg1, void *arg2) int compare_float(void *arg1, void *arg2) { - float v1 = *(float *)arg1; - float v2 = *(float *)arg2; + float v1 = *(float *)arg1; + float v2 = *(float *)arg2; float cmp = v1 - v2; if (cmp > EPSILON) { return 1; @@ -48,10 +47,10 @@ int compare_float(void *arg1, void *arg2) int compare_string(void *arg1, int arg1_max_length, void *arg2, int arg2_max_length) { - const char *s1 = (const char *)arg1; - const char *s2 = (const char *)arg2; - int maxlen = std::min(arg1_max_length, arg2_max_length); - int result = strncmp(s1, s2, maxlen); + const char *s1 = (const char *)arg1; + const char *s2 = (const char *)arg2; + int maxlen = std::min(arg1_max_length, arg2_max_length); + int result = strncmp(s1, s2, maxlen); if (0 != result) { return result; } @@ -66,4 +65,4 @@ int compare_string(void *arg1, int arg1_max_length, void *arg2, int arg2_max_len return 0; } -} // namespace common +} // namespace common diff --git a/deps/common/lang/comparator.h b/deps/common/lang/comparator.h index 03711651e..fa5e10f8b 100644 --- a/deps/common/lang/comparator.h +++ b/deps/common/lang/comparator.h @@ -16,9 +16,8 @@ See the Mulan PSL v2 for more details. */ namespace common { - int compare_int(void *arg1, void *arg2); int compare_float(void *arg1, void *arg2); int compare_string(void *arg1, int arg1_max_length, void *arg2, int arg2_max_length); -} \ No newline at end of file +} // namespace common \ No newline at end of file diff --git a/deps/common/lang/defer.h b/deps/common/lang/defer.h index d103dfc8c..72bfc1c56 100644 --- a/deps/common/lang/defer.h +++ b/deps/common/lang/defer.h @@ -20,9 +20,8 @@ namespace common { class DeferHelper { -public: - DeferHelper(const std::function defer) : defer_(std::move(defer)) - {} +public: + DeferHelper(const std::function defer) : defer_(std::move(defer)) {} ~DeferHelper() { @@ -35,10 +34,10 @@ class DeferHelper const std::function defer_; }; -} // namespace common +} // namespace common #define AA(B, C) B##C -#define BB(B, C) AA(B,C) +#define BB(B, C) AA(B, C) #define DEFER(callback) common::DeferHelper BB(defer_helper_, __LINE__)(callback) diff --git a/deps/common/lang/lower_bound.h b/deps/common/lang/lower_bound.h index f204977ab..3aadbf368 100644 --- a/deps/common/lang/lower_bound.h +++ b/deps/common/lang/lower_bound.h @@ -29,15 +29,15 @@ namespace common { * @return ForwardIterator 指向lower bound结果的iterator。如果大于最大的值,那么会指向last */ template -ForwardIterator lower_bound(ForwardIterator first, ForwardIterator last, - const T &val, Compare comp, bool *_found = nullptr) +ForwardIterator lower_bound( + ForwardIterator first, ForwardIterator last, const T &val, Compare comp, bool *_found = nullptr) { - bool found = false; + bool found = false; ForwardIterator iter; - const auto count = std::distance(first, last); - auto last_count = count; + const auto count = std::distance(first, last); + auto last_count = count; while (last_count > 0) { - iter = first; + iter = first; auto step = last_count / 2; std::advance(iter, step); int result = comp(*iter, val); @@ -64,7 +64,7 @@ template class Comparator { public: - int operator() (const T &v1, const T &v2) const + int operator()(const T &v1, const T &v2) const { if (v1 < v2) { return -1; @@ -83,7 +83,8 @@ ForwardIterator lower_bound(ForwardIterator first, ForwardIterator last, const T } // std::iterator is deprecated -// refer to https://www.fluentcpp.com/2018/05/08/std-iterator-deprecated/#:~:text=std%3A%3Aiterator%20is%20deprecated%2C%20so%20we%20should%20stop%20using,the%205%20aliases%20inside%20of%20your%20custom%20iterators. +// refer to +// https://www.fluentcpp.com/2018/05/08/std-iterator-deprecated/#:~:text=std%3A%3Aiterator%20is%20deprecated%2C%20so%20we%20should%20stop%20using,the%205%20aliases%20inside%20of%20your%20custom%20iterators. // a sample code: // https://github.com/google/googletest/commit/25208a60a27c2e634f46327595b281cb67355700 template @@ -91,36 +92,49 @@ class BinaryIterator { public: using iterator_category = std::random_access_iterator_tag; - using value_type = T; - using difference_type = Distance; - using pointer = value_type *; - using reference = value_type &; - -public: + using value_type = T; + using difference_type = Distance; + using pointer = value_type *; + using reference = value_type &; + +public: BinaryIterator() = default; - BinaryIterator(size_t item_num, T *data) : item_num_(item_num), data_(data) - {} + BinaryIterator(size_t item_num, T *data) : item_num_(item_num), data_(data) {} - BinaryIterator &operator+= (int n) { data_ += (item_num_ * n); return *this; } - BinaryIterator &operator-= (int n) { return this->operator+ (-n); } - BinaryIterator &operator++() { return this->operator +=(1); } - BinaryIterator operator++(int) { BinaryIterator tmp(*this); this->operator++(); return tmp; } - BinaryIterator &operator--() { return this->operator += (-1); } - BinaryIterator operator--(int) { BinaryIterator tmp(*this); this->operator += (-1); return tmp; } + BinaryIterator &operator+=(int n) + { + data_ += (item_num_ * n); + return *this; + } + BinaryIterator &operator-=(int n) { return this->operator+(-n); } + BinaryIterator &operator++() { return this->operator+=(1); } + BinaryIterator operator++(int) + { + BinaryIterator tmp(*this); + this-> operator++(); + return tmp; + } + BinaryIterator &operator--() { return this->operator+=(-1); } + BinaryIterator operator--(int) + { + BinaryIterator tmp(*this); + *this += -1; + return tmp; + } - bool operator == (const BinaryIterator &other) const { return data_ == other.data_; } - bool operator != (const BinaryIterator &other) const { return ! (this->operator ==(other)); } + bool operator==(const BinaryIterator &other) const { return data_ == other.data_; } + bool operator!=(const BinaryIterator &other) const { return !(this->operator==(other)); } - T * operator *() { return data_; } - T * operator ->() { return data_; } + T *operator*() { return data_; } + T *operator->() { return data_; } - friend Distance operator - (const BinaryIterator &left, const BinaryIterator &right) + friend Distance operator-(const BinaryIterator &left, const BinaryIterator &right) { return (left.data_ - right.data_) / left.item_num_; } private: size_t item_num_ = 0; - T * data_ = nullptr; + T *data_ = nullptr; }; -} // namespace common +} // namespace common diff --git a/deps/common/lang/lru_cache.h b/deps/common/lang/lru_cache.h index b5c28c0ad..846d731d4 100644 --- a/deps/common/lang/lru_cache.h +++ b/deps/common/lang/lru_cache.h @@ -20,22 +20,24 @@ See the Mulan PSL v2 for more details. */ namespace common { template , typename Pred = std::equal_to> -class LruCache { +class LruCache +{ - class ListNode { + class ListNode + { public: - Key key_; + Key key_; Value value_; ListNode *prev_ = nullptr; ListNode *next_ = nullptr; public: - ListNode(const Key &key, const Value &value) : key_(key), value_(value) - {} + ListNode(const Key &key, const Value &value) : key_(key), value_(value) {} }; - class PListNodeHasher { + class PListNodeHasher + { public: size_t operator()(ListNode *node) const { @@ -49,7 +51,8 @@ class LruCache { Hash hasher_; }; - class PListNodePredicator { + class PListNodePredicator + { public: bool operator()(ListNode *const node1, ListNode *const node2) const { @@ -76,10 +79,7 @@ class LruCache { } } - ~LruCache() - { - destroy(); - } + ~LruCache() { destroy(); } void destroy() { @@ -89,13 +89,10 @@ class LruCache { searcher_.clear(); lru_front_ = nullptr; - lru_tail_ = nullptr; + lru_tail_ = nullptr; } - size_t count() const - { - return searcher_.size(); - } + size_t count() const { return searcher_.size(); } bool get(const Key &key, Value &value) { @@ -114,7 +111,7 @@ class LruCache { auto iter = searcher_.find((ListNode *)&key); if (iter != searcher_.end()) { ListNode *ln = *iter; - ln->value_ = value; + ln->value_ = value; lru_touch(ln); return; } @@ -222,8 +219,8 @@ class LruCache { private: using SearchType = std::unordered_set; SearchType searcher_; - ListNode *lru_front_ = nullptr; - ListNode *lru_tail_ = nullptr; + ListNode *lru_front_ = nullptr; + ListNode *lru_tail_ = nullptr; }; } // namespace common diff --git a/deps/common/lang/mutex.cpp b/deps/common/lang/mutex.cpp index 3636b1150..c8b75d5b7 100644 --- a/deps/common/lang/mutex.cpp +++ b/deps/common/lang/mutex.cpp @@ -16,14 +16,14 @@ See the Mulan PSL v2 for more details. */ #include "common/log/log.h" namespace common { -std::map LockTrace::mLocks; -std::map LockTrace::mWaitTimes; -std::map LockTrace::mWaitLocks; +std::map LockTrace::mLocks; +std::map LockTrace::mWaitTimes; +std::map LockTrace::mWaitLocks; std::map> LockTrace::mOwnLocks; -std::set LockTrace::mEnableRecurisives; +std::set LockTrace::mEnableRecurisives; -pthread_rwlock_t LockTrace::mMapMutex = PTHREAD_RWLOCK_INITIALIZER; -int LockTrace::mMaxBlockTids = 8; +pthread_rwlock_t LockTrace::mMapMutex = PTHREAD_RWLOCK_INITIALIZER; +int LockTrace::mMaxBlockTids = 8; #define CHECK_UNLOCK 0 @@ -132,7 +132,7 @@ bool LockTrace::checkLockTimes(pthread_mutex_t *mutex, const char *file, const i return false; } - int lockTimes = it->second; + int lockTimes = it->second; mWaitTimes[mutex] = lockTimes + 1; if (lockTimes >= mMaxBlockTids) { @@ -271,7 +271,7 @@ void DebugMutex::unlock() #ifdef DEBUG LOG_DEBUG("debug unlock %p, lbt=%s", &lock_, lbt()); lock_.unlock(); -#endif +#endif } //////////////////////////////////////////////////////////////////////////////// @@ -320,7 +320,7 @@ bool SharedMutex::try_lock() } return result; } -void SharedMutex::unlock() // unlock exclusive +void SharedMutex::unlock() // unlock exclusive { LOG_DEBUG("shared lock unlock %p, lbt=%s", &lock_, lbt()); lock_.unlock(); @@ -345,48 +345,32 @@ void SharedMutex::unlock_shared() lock_.unlock_shared(); } -#else // CONCURRENCY undefined +#else // CONCURRENCY undefined -void SharedMutex::lock() -{} -bool SharedMutex::try_lock() -{ - return true; -} -void SharedMutex::unlock() // unlock exclusive +void SharedMutex::lock() {} +bool SharedMutex::try_lock() { return true; } +void SharedMutex::unlock() // unlock exclusive {} -void SharedMutex::lock_shared() -{} -bool SharedMutex::try_lock_shared() -{ - return true; -} -void SharedMutex::unlock_shared() -{} +void SharedMutex::lock_shared() {} +bool SharedMutex::try_lock_shared() { return true; } +void SharedMutex::unlock_shared() {} -#endif // CONCURRENCY end +#endif // CONCURRENCY end //////////////////////////////////////////////////////////////////////////////// #ifndef CONCURRENCY -void RecursiveSharedMutex::lock_shared() -{} +void RecursiveSharedMutex::lock_shared() {} -bool RecursiveSharedMutex::try_lock_shared() -{ - return true; -} +bool RecursiveSharedMutex::try_lock_shared() { return true; } -void RecursiveSharedMutex::unlock_shared() -{} +void RecursiveSharedMutex::unlock_shared() {} -void RecursiveSharedMutex::lock() -{} +void RecursiveSharedMutex::lock() {} -void RecursiveSharedMutex::unlock() -{} +void RecursiveSharedMutex::unlock() {} -#else // ifdef CONCURRENCY +#else // ifdef CONCURRENCY void RecursiveSharedMutex::lock_shared() { @@ -446,6 +430,6 @@ void RecursiveSharedMutex::unlock() } } } -#endif // CONCURRENCY +#endif // CONCURRENCY } // namespace common diff --git a/deps/common/lang/mutex.h b/deps/common/lang/mutex.h index 8d07a6ab7..29ebbe98f 100644 --- a/deps/common/lang/mutex.h +++ b/deps/common/lang/mutex.h @@ -14,18 +14,18 @@ See the Mulan PSL v2 for more details. */ #pragma once -#include -#include -#include -#include -#include #include +#include #include -#include -#include #include +#include #include +#include +#include +#include +#include #include +#include #include "common/log/log.h" @@ -35,7 +35,8 @@ namespace common { #define MUTEX_LOG LOG_DEBUG -class LockTrace { +class LockTrace +{ public: static void check(pthread_mutex_t *mutex, const long long threadId, const char *file, const int line); static void lock(pthread_mutex_t *mutex, const long long threadId, const char *file, const int line); @@ -44,12 +45,12 @@ class LockTrace { static void toString(std::string &result); - class LockID { + class LockID + { public: LockID(const long long threadId, const char *file, const int line) : mFile(file), mThreadId(threadId), mLine(line) {} - LockID() : mFile(), mThreadId(0), mLine(0) - {} + LockID() : mFile(), mThreadId(0), mLine(0) {} std::string toString() { @@ -61,9 +62,9 @@ class LockTrace { } public: - std::string mFile; + std::string mFile; const long long mThreadId; - int mLine; + int mLine; }; static void foundDeadLock(LockID ¤t, LockID &other, pthread_mutex_t *otherWaitMutex); @@ -76,22 +77,19 @@ class LockTrace { static void insertLock(pthread_mutex_t *mutex, const long long threadId, const char *file, const int line); - static void setMaxBlockThreads(int blockNum) - { - mMaxBlockTids = blockNum; - } + static void setMaxBlockThreads(int blockNum) { mMaxBlockTids = blockNum; } public: static std::set mEnableRecurisives; protected: - static std::map mLocks; - static std::map mWaitTimes; - static std::map mWaitLocks; + static std::map mLocks; + static std::map mWaitTimes; + static std::map mWaitLocks; static std::map> mOwnLocks; static pthread_rwlock_t mMapMutex; - static int mMaxBlockTids; + static int mMaxBlockTids; }; // Open this macro in Makefile @@ -247,11 +245,12 @@ class LockTrace { class DebugMutex final { public: - DebugMutex() = default; + DebugMutex() = default; ~DebugMutex() = default; void lock(); void unlock(); + private: #ifdef DEBUG std::mutex lock_; @@ -261,7 +260,7 @@ class DebugMutex final class Mutex final { public: - Mutex() = default; + Mutex() = default; ~Mutex() = default; void lock(); @@ -277,12 +276,12 @@ class Mutex final class SharedMutex final { public: - SharedMutex() = default; + SharedMutex() = default; ~SharedMutex() = default; - void lock(); // lock exclusive + void lock(); // lock exclusive bool try_lock(); - void unlock(); // unlock exclusive + void unlock(); // unlock exclusive void lock_shared(); bool try_lock_shared(); @@ -302,10 +301,10 @@ class SharedMutex final */ class RecursiveSharedMutex { - public: - RecursiveSharedMutex() = default; +public: + RecursiveSharedMutex() = default; ~RecursiveSharedMutex() = default; - + void lock_shared(); bool try_lock_shared(); void unlock_shared(); @@ -315,14 +314,14 @@ class RecursiveSharedMutex private: #ifdef CONCURRENCY - std::mutex mutex_; + std::mutex mutex_; std::condition_variable shared_lock_cv_; std::condition_variable exclusive_lock_cv_; - int shared_lock_count_ = 0; - int exclusive_lock_count_ = 0; - std::thread::id recursive_owner_; - int recursive_count_ = 0; // 表示当前线程加写锁加了多少次 -#endif // CONCURRENCY + int shared_lock_count_ = 0; + int exclusive_lock_count_ = 0; + std::thread::id recursive_owner_; + int recursive_count_ = 0; // 表示当前线程加写锁加了多少次 +#endif // CONCURRENCY }; } // namespace common diff --git a/deps/common/lang/serializable.h b/deps/common/lang/serializable.h index feba9fdc0..387d41124 100644 --- a/deps/common/lang/serializable.h +++ b/deps/common/lang/serializable.h @@ -21,9 +21,15 @@ namespace common { /** * Through this type to determine object type */ -enum { MESSAGE_BASIC = 100, MESSAGE_BASIC_REQUEST = 1000, MESSAGE_BASIC_RESPONSE = -1000 }; +enum +{ + MESSAGE_BASIC = 100, + MESSAGE_BASIC_REQUEST = 1000, + MESSAGE_BASIC_RESPONSE = -1000 +}; -class Deserializable { +class Deserializable +{ public: /* * deserialize buffer to one object @@ -33,7 +39,8 @@ class Deserializable { virtual void *deserialize(const char *buffer, int bufLen) = 0; }; -class Serializable { +class Serializable +{ public: /* * serialize this object to bytes diff --git a/deps/common/lang/string.cpp b/deps/common/lang/string.cpp index 8e0d53fcf..49029c886 100644 --- a/deps/common/lang/string.cpp +++ b/deps/common/lang/string.cpp @@ -84,7 +84,7 @@ std::string &str_to_lower(std::string &s) void split_string(const std::string &str, std::string delim, std::set &results) { - int cut_at; + int cut_at; std::string tmp_str(str); while ((cut_at = tmp_str.find_first_of(delim)) != (signed)tmp_str.npos) { if (cut_at > 0) { @@ -100,7 +100,7 @@ void split_string(const std::string &str, std::string delim, std::set &results) { - int cut_at; + int cut_at; std::string tmp_str(str); while ((cut_at = tmp_str.find_first_of(delim)) != (signed)tmp_str.npos) { if (cut_at > 0) { @@ -191,8 +191,8 @@ void replace(std::string &str, const std::string &old, const std::string &new_st char *bin_to_hex(const char *s, const int len, char *hex_buff) { - int new_len = 0; - unsigned char *end = (unsigned char *)s + len; + int new_len = 0; + unsigned char *end = (unsigned char *)s + len; for (unsigned char *p = (unsigned char *)s; p < end; p++) { new_len += snprintf(hex_buff + new_len, 3, "%02x", *p); } @@ -203,22 +203,22 @@ char *bin_to_hex(const char *s, const int len, char *hex_buff) char *hex_to_bin(const char *s, char *bin_buff, int *dest_len) { - char buff[3]; + char buff[3]; char *src; - int src_len; + int src_len; char *p_dest; char *p_dest_end; src_len = strlen(s); if (src_len == 0) { - *dest_len = 0; + *dest_len = 0; bin_buff[0] = '\0'; return bin_buff; } *dest_len = src_len / 2; - src = (char *)s; - buff[2] = '\0'; + src = (char *)s; + buff[2] = '\0'; p_dest_end = bin_buff + (*dest_len); for (p_dest = bin_buff; p_dest < p_dest_end; p_dest++) { @@ -257,7 +257,7 @@ bool is_blank(const char *s) char *substr(const char *s, int n1, int n2) { char *sp = (char *)malloc(sizeof(char) * (n2 - n1 + 2)); - int i, j = 0; + int i, j = 0; for (i = n1; i <= n2; i++) { sp[j++] = s[i]; } diff --git a/deps/common/lang/string.h b/deps/common/lang/string.h index d19295a54..f46e1b70a 100644 --- a/deps/common/lang/string.h +++ b/deps/common/lang/string.h @@ -16,8 +16,8 @@ See the Mulan PSL v2 for more details. */ // Basic includes #include -#include #include +#include #include #include @@ -33,7 +33,7 @@ namespace common { /** * remove all white space(like ' ', '\t', '\n') from string */ -void strip(std::string &str); +void strip(std::string &str); char *strip(char *str); /** @@ -140,10 +140,10 @@ std::string get_type_name(const T &val); template bool str_to_val(const std::string &str, T &val, std::ios_base &(*radix)(std::ios_base &)/* = std::dec */) { - bool success = true; + bool success = true; std::istringstream is(str); if (!(is >> radix >> val)) { - val = 0; + val = 0; success = false; } return success; @@ -160,8 +160,8 @@ void val_to_str(const T &val, std::string &str, std::ios_base &(*radix)(std::ios template std::string get_type_name(const T &val) { - int status = 0; - char *stmp = abi::__cxa_demangle(typeid(val).name(), 0, 0, &status); + int status = 0; + char *stmp = abi::__cxa_demangle(typeid(val).name(), 0, 0, &status); if (!stmp) return ""; diff --git a/deps/common/log/log.cpp b/deps/common/log/log.cpp index f9c2a8511..869146f20 100644 --- a/deps/common/log/log.cpp +++ b/deps/common/log/log.cpp @@ -14,9 +14,9 @@ See the Mulan PSL v2 for more details. */ #include #include +#include #include #include -#include #include "common/lang/string.h" #include "common/log/log.h" @@ -28,20 +28,20 @@ Log::Log(const std::string &log_file_name, const LOG_LEVEL log_level, const LOG_ : log_name_(log_file_name), log_level_(log_level), console_level_(console_level) { prefix_map_[LOG_LEVEL_PANIC] = "PANIC:"; - prefix_map_[LOG_LEVEL_ERR] = "ERROR:"; - prefix_map_[LOG_LEVEL_WARN] = "WARN:"; - prefix_map_[LOG_LEVEL_INFO] = "INFO:"; + prefix_map_[LOG_LEVEL_ERR] = "ERROR:"; + prefix_map_[LOG_LEVEL_WARN] = "WARN:"; + prefix_map_[LOG_LEVEL_INFO] = "INFO:"; prefix_map_[LOG_LEVEL_DEBUG] = "DEBUG:"; prefix_map_[LOG_LEVEL_TRACE] = "TRACE:"; pthread_mutex_init(&lock_, nullptr); log_date_.year_ = -1; - log_date_.mon_ = -1; - log_date_.day_ = -1; - log_max_line_ = LOG_MAX_LINE; - log_line_ = -1; - rotate_type_ = LOG_ROTATE_BYDAY; + log_date_.mon_ = -1; + log_date_.day_ = -1; + log_max_line_ = LOG_MAX_LINE; + log_line_ = -1; + rotate_type_ = LOG_ROTATE_BYDAY; check_param_valid(); @@ -88,7 +88,7 @@ int Log::output(const LOG_LEVEL level, const char *module, const char *prefix, c bool locked = false; try { va_list args; - char msg[ONE_KILO]; + char msg[ONE_KILO]; va_start(args, f); vsnprintf(msg, sizeof(msg), f, args); @@ -143,10 +143,7 @@ int Log::set_console_level(LOG_LEVEL console_level) return LOG_STATUS_ERR; } -LOG_LEVEL Log::get_console_level() -{ - return console_level_; -} +LOG_LEVEL Log::get_console_level() { return console_level_; } int Log::set_log_level(LOG_LEVEL log_level) { @@ -158,10 +155,7 @@ int Log::set_log_level(LOG_LEVEL log_level) return LOG_STATUS_ERR; } -LOG_LEVEL Log::get_log_level() -{ - return log_level_; -} +LOG_LEVEL Log::get_log_level() { return log_level_; } const char *Log::prefix_msg(LOG_LEVEL level) { @@ -172,10 +166,7 @@ const char *Log::prefix_msg(LOG_LEVEL level) return empty_prefix; } -void Log::set_default_module(const std::string &modules) -{ - split_string(modules, ",", default_set_); -} +void Log::set_default_module(const std::string &modules) { split_string(modules, ",", default_set_); } int Log::set_rotate_type(LOG_ROTATE rotate_type) { @@ -185,10 +176,7 @@ int Log::set_rotate_type(LOG_ROTATE rotate_type) return LOG_STATUS_OK; } -LOG_ROTATE Log::get_rotate_type() -{ - return rotate_type_; -} +LOG_ROTATE Log::get_rotate_type() { return rotate_type_; } int Log::rotate_by_day(const int year, const int month, const int day) { @@ -207,8 +195,8 @@ int Log::rotate_by_day(const int year, const int month, const int day) ofs_.open(log_file_name.c_str(), std::ios_base::out | std::ios_base::app); if (ofs_.good()) { log_date_.year_ = year; - log_date_.mon_ = month; - log_date_.day_ = day; + log_date_.mon_ = month; + log_date_.day_ = day; log_line_ = 0; } @@ -220,12 +208,12 @@ int Log::rotate_by_day(const int year, const int month, const int day) int Log::rename_old_logs() { - int log_index = 1; + int log_index = 1; int max_log_index = 0; while (log_index < MAX_LOG_NUM) { std::string log_name = log_name_ + "." + size_to_pad_str(log_index, 3); - int result = access(log_name.c_str(), R_OK); + int result = access(log_name.c_str(), R_OK); if (result) { break; } @@ -280,7 +268,7 @@ int Log::rotate_by_size() char log_index_str[4] = {0}; snprintf(log_index_str, sizeof(log_index_str), "%03d", 1); std::string log_name_new = log_name_ + "." + log_index_str; - result = rename(log_name_.c_str(), log_name_new.c_str()); + result = rename(log_name_.c_str(), log_name_new.c_str()); if (result) { std::cerr << "Failed to rename " << log_name_ << " to " << log_name_new << std::endl; } @@ -322,10 +310,7 @@ void Log::set_context_getter(std::function context_getter) } } -intptr_t Log::context_id() -{ - return context_getter_(); -} +intptr_t Log::context_id() { return context_getter_(); } LoggerFactory::LoggerFactory() { @@ -366,17 +351,17 @@ int LoggerFactory::init_default( const char *lbt() { constexpr int buffer_size = 100; - void *buffer[buffer_size]; - - constexpr int bt_buffer_size = 4096; + void *buffer[buffer_size]; + + constexpr int bt_buffer_size = 4096; thread_local char backtrace_buffer[bt_buffer_size]; - int size = backtrace(buffer, buffer_size); + int size = backtrace(buffer, buffer_size); int offset = 0; for (int i = 0; i < size && offset < bt_buffer_size - 1; i++) { const char *format = (0 == i) ? "0x%lx" : " 0x%lx"; - offset += snprintf(backtrace_buffer + offset, sizeof(backtrace_buffer) - offset, format, - reinterpret_cast(buffer[i])); + offset += snprintf( + backtrace_buffer + offset, sizeof(backtrace_buffer) - offset, format, reinterpret_cast(buffer[i])); } return backtrace_buffer; } diff --git a/deps/common/log/log.h b/deps/common/log/log.h index 1ee25df9e..71272b9c0 100644 --- a/deps/common/log/log.h +++ b/deps/common/log/log.h @@ -14,43 +14,49 @@ See the Mulan PSL v2 for more details. */ #pragma once -#include #include #include #include #include +#include #include +#include #include #include #include #include -#include #include "common/defs.h" namespace common { -const unsigned int ONE_KILO = 1024; +const unsigned int ONE_KILO = 1024; const unsigned int FILENAME_LENGTH_MAX = 256; // the max filename length -const int LOG_STATUS_OK = 0; +const int LOG_STATUS_OK = 0; const int LOG_STATUS_ERR = 1; -const int LOG_MAX_LINE = 100000; +const int LOG_MAX_LINE = 100000; -typedef enum { +typedef enum +{ LOG_LEVEL_PANIC = 0, - LOG_LEVEL_ERR = 1, - LOG_LEVEL_WARN = 2, - LOG_LEVEL_INFO = 3, + LOG_LEVEL_ERR = 1, + LOG_LEVEL_WARN = 2, + LOG_LEVEL_INFO = 3, LOG_LEVEL_DEBUG = 4, LOG_LEVEL_TRACE = 5, LOG_LEVEL_LAST } LOG_LEVEL; -typedef enum { LOG_ROTATE_BYDAY = 0, LOG_ROTATE_BYSIZE, LOG_ROTATE_LAST } LOG_ROTATE; +typedef enum +{ + LOG_ROTATE_BYDAY = 0, + LOG_ROTATE_BYSIZE, + LOG_ROTATE_LAST +} LOG_ROTATE; -class Log +class Log { public: Log(const std::string &log_name, const LOG_LEVEL log_level = LOG_LEVEL_INFO, @@ -88,13 +94,13 @@ class Log int output(const LOG_LEVEL level, const char *module, const char *prefix, const char *f, ...); - int set_console_level(const LOG_LEVEL console_level); + int set_console_level(const LOG_LEVEL console_level); LOG_LEVEL get_console_level(); - int set_log_level(const LOG_LEVEL log_level); + int set_log_level(const LOG_LEVEL log_level); LOG_LEVEL get_log_level(); - int set_rotate_type(LOG_ROTATE rotate_type); + int set_rotate_type(LOG_ROTATE rotate_type); LOG_ROTATE get_rotate_type(); const char *prefix_msg(const LOG_LEVEL level); @@ -114,9 +120,9 @@ class Log * @details 比如设置一个获取当前session标识的函数,那么每次在打印日志时都会输出session信息。 * 这个回调函数返回了一个intptr_t类型的数据,可能返回字符串更好,但是现在够用了。 */ - void set_context_getter(std::function context_getter); + void set_context_getter(std::function context_getter); intptr_t context_id(); - + private: void check_param_valid(); @@ -129,31 +135,33 @@ class Log private: pthread_mutex_t lock_; - std::ofstream ofs_; - std::string log_name_; - LOG_LEVEL log_level_; - LOG_LEVEL console_level_; + std::ofstream ofs_; + std::string log_name_; + LOG_LEVEL log_level_; + LOG_LEVEL console_level_; - typedef struct _LogDate { + typedef struct _LogDate + { int year_; int mon_; int day_; } LogDate; - LogDate log_date_; - int log_line_; - int log_max_line_; + LogDate log_date_; + int log_line_; + int log_max_line_; LOG_ROTATE rotate_type_; typedef std::map LogPrefixMap; - LogPrefixMap prefix_map_; + LogPrefixMap prefix_map_; typedef std::set DefaultSet; - DefaultSet default_set_; + DefaultSet default_set_; std::function context_getter_; }; -class LoggerFactory { +class LoggerFactory +{ public: LoggerFactory(); virtual ~LoggerFactory(); @@ -177,11 +185,12 @@ extern Log *g_log; if (common::g_log) { \ struct timeval tv; \ gettimeofday(&tv, NULL); \ - struct tm *p = localtime(&tv.tv_sec); \ - char sz_head[LOG_HEAD_SIZE] = {0}; \ + struct tm *p = localtime(&tv.tv_sec); \ + char sz_head[LOG_HEAD_SIZE] = {0}; \ if (p) { \ int usec = (int)tv.tv_usec; \ - snprintf(sz_head, LOG_HEAD_SIZE, \ + snprintf(sz_head, \ + LOG_HEAD_SIZE, \ "%04d-%02d-%02d %02d:%02d:%02u.%06d pid:%u tid:%llx ctx:%lx", \ p->tm_year + 1900, \ p->tm_mon + 1, \ @@ -202,8 +211,7 @@ extern Log *g_log; (common::g_log)->prefix_msg(level), \ __FUNCTION__, \ __FILE_NAME__, \ - (int32_t)__LINE__ \ - ); \ + (int32_t)__LINE__); \ } #define LOG_OUTPUT(level, fmt, ...) \ @@ -306,20 +314,20 @@ int Log::out(const LOG_LEVEL console_level, const LOG_LEVEL log_level, T &msg) #ifndef ASSERT #ifdef DEBUG -#define ASSERT(expression, description, ...) \ - do { \ - if (!(expression)) { \ - LOG_PANIC(description, ##__VA_ARGS__); \ - assert(expression); \ - } \ +#define ASSERT(expression, description, ...) \ + do { \ + if (!(expression)) { \ + LOG_PANIC(description, ##__VA_ARGS__); \ + assert(expression); \ + } \ } while (0) -#else // DEBUG -#define ASSERT(expression, description, ...) \ - do { \ - (void)(expression); \ +#else // DEBUG +#define ASSERT(expression, description, ...) \ + do { \ + (void)(expression); \ } while (0) -#endif // DEBUG +#endif // DEBUG #endif // ASSERT diff --git a/deps/common/math/md5.cpp b/deps/common/math/md5.cpp index b241eb452..c365168a1 100644 --- a/deps/common/math/md5.cpp +++ b/deps/common/math/md5.cpp @@ -206,7 +206,7 @@ void MD5Update(MD5_CTX *context, unsigned char *input, unsigned int inputLen) void MD5Final(unsigned char digest[16], MD5_CTX *context) { unsigned char bits[8]; - unsigned int index, padLen; + unsigned int index, padLen; /* Save number of bits */ Encode(bits, context->count, 8); @@ -214,7 +214,7 @@ void MD5Final(unsigned char digest[16], MD5_CTX *context) /* * Pad out to 56 mod 64. */ - index = (unsigned int)((context->count[0] >> 3) & 0x3f); + index = (unsigned int)((context->count[0] >> 3) & 0x3f); padLen = (index < 56) ? (56 - index) : (120 - index); MD5Update(context, PADDING, padLen); @@ -331,7 +331,7 @@ static void Encode(unsigned char *output, UINT4 *input, unsigned int len) unsigned int i, j; for (i = 0, j = 0; j < len; i++, j += 4) { - output[j] = (unsigned char)(input[i] & 0xff); + output[j] = (unsigned char)(input[i] & 0xff); output[j + 1] = (unsigned char)((input[i] >> 8) & 0xff); output[j + 2] = (unsigned char)((input[i] >> 16) & 0xff); output[j + 3] = (unsigned char)((input[i] >> 24) & 0xff); @@ -379,7 +379,7 @@ static void MD5_memset(POINTER output, int value, unsigned int len) */ int MD5String(char *string, unsigned char digest[16]) { - MD5_CTX context; + MD5_CTX context; unsigned int len = strlen(string); MD5Init(&context); @@ -400,9 +400,9 @@ int MD5Buffer(char *buffer, unsigned int len, unsigned char digest[16]) int MD5File(char *filename, unsigned char digest[16]) { - FILE *file; - MD5_CTX context; - int len; + FILE *file; + MD5_CTX context; + int len; unsigned char buffer[1024]; if ((file = fopen(filename, "rb")) == NULL) diff --git a/deps/common/math/md5.h b/deps/common/math/md5.h index 8cf37aaa4..841afb576 100644 --- a/deps/common/math/md5.h +++ b/deps/common/math/md5.h @@ -17,13 +17,14 @@ See the Mulan PSL v2 for more details. */ #include namespace common { -typedef unsigned char *POINTER; +typedef unsigned char *POINTER; typedef unsigned short int UINT2; -typedef unsigned int UINT4; +typedef unsigned int UINT4; -typedef struct { - UINT4 state[4]; /* state (ABCD) */ - UINT4 count[2]; /* number of bits, modulo 2^64 (lsb first) */ +typedef struct +{ + UINT4 state[4]; /* state (ABCD) */ + UINT4 count[2]; /* number of bits, modulo 2^64 (lsb first) */ unsigned char buffer[64]; /* input buffer */ } MD5_CTX; diff --git a/deps/common/math/random_generator.cpp b/deps/common/math/random_generator.cpp index 78f2fecd0..09dccc498 100644 --- a/deps/common/math/random_generator.cpp +++ b/deps/common/math/random_generator.cpp @@ -18,17 +18,11 @@ See the Mulan PSL v2 for more details. */ namespace common { -RandomGenerator::RandomGenerator() : randomData(std::chrono::system_clock::now().time_since_epoch().count()) -{} +RandomGenerator::RandomGenerator() : randomData(std::chrono::system_clock::now().time_since_epoch().count()) {} -RandomGenerator::~RandomGenerator() -{} +RandomGenerator::~RandomGenerator() {} -unsigned int RandomGenerator::next() -{ - - return randomData(); -} +unsigned int RandomGenerator::next() { return randomData(); } unsigned int RandomGenerator::next(unsigned int range) { diff --git a/deps/common/math/random_generator.h b/deps/common/math/random_generator.h index 8e44a9a71..a3d8a97e2 100644 --- a/deps/common/math/random_generator.h +++ b/deps/common/math/random_generator.h @@ -14,13 +14,14 @@ See the Mulan PSL v2 for more details. */ #ifndef __COMMON_MATH_RANDOM_GENERATOR_H_ #define __COMMON_MATH_RANDOM_GENERATOR_H_ -#include #include +#include namespace common { #define DEFAULT_RANDOM_BUFF_SIZE 512 -class RandomGenerator { +class RandomGenerator +{ public: RandomGenerator(); diff --git a/deps/common/metrics/console_reporter.h b/deps/common/metrics/console_reporter.h index eb1d5bd80..8a03fca9a 100644 --- a/deps/common/metrics/console_reporter.h +++ b/deps/common/metrics/console_reporter.h @@ -19,7 +19,8 @@ See the Mulan PSL v2 for more details. */ namespace common { -class ConsoleReporter : public Reporter { +class ConsoleReporter : public Reporter +{ public: void report(const std::string &tag, Metric *metric); }; diff --git a/deps/common/metrics/histogram_snapshot.cpp b/deps/common/metrics/histogram_snapshot.cpp index 984549585..025e68548 100644 --- a/deps/common/metrics/histogram_snapshot.cpp +++ b/deps/common/metrics/histogram_snapshot.cpp @@ -17,23 +17,18 @@ See the Mulan PSL v2 for more details. */ #include #include -#include #include #include #include +#include namespace common { -HistogramSnapShot::HistogramSnapShot() -{} +HistogramSnapShot::HistogramSnapShot() {} -HistogramSnapShot::HistogramSnapShot(const std::vector &collection) -{ - set_collection(collection); -} +HistogramSnapShot::HistogramSnapShot(const std::vector &collection) { set_collection(collection); } -HistogramSnapShot::~HistogramSnapShot() -{} +HistogramSnapShot::~HistogramSnapShot() {} void HistogramSnapShot::set_collection(const std::vector &collection) { @@ -45,10 +40,7 @@ void HistogramSnapShot::set_collection(const std::vector &collection) std::sort(data_.begin(), data_.end()); } -size_t HistogramSnapShot::size() const -{ - return data_.size(); -} +size_t HistogramSnapShot::size() const { return data_.size(); } double HistogramSnapShot::get_value(double quantile) { @@ -80,33 +72,15 @@ double HistogramSnapShot::get_value(double quantile) return lower + (pos - floor(pos)) * (upper - lower); } -double HistogramSnapShot::get_median() -{ - return get_value(0.5f); -} +double HistogramSnapShot::get_median() { return get_value(0.5f); } -double HistogramSnapShot::get_75th() -{ - return get_value(0.75f); -} +double HistogramSnapShot::get_75th() { return get_value(0.75f); } -double HistogramSnapShot::get_90th() -{ - return get_value(0.90f); -} +double HistogramSnapShot::get_90th() { return get_value(0.90f); } -double HistogramSnapShot::get_95th() -{ - return get_value(0.95f); -} -double HistogramSnapShot::get_99th() -{ - return get_value(0.99f); -} -double HistogramSnapShot::get_999th() -{ - return get_value(0.999f); -} +double HistogramSnapShot::get_95th() { return get_value(0.95f); } +double HistogramSnapShot::get_99th() { return get_value(0.99f); } +double HistogramSnapShot::get_999th() { return get_value(0.999f); } double HistogramSnapShot::get_max() { @@ -135,10 +109,7 @@ double HistogramSnapShot::get_mean() return std::accumulate(data_.begin(), data_.end(), (double)0) * 1.0f / data_.size(); } -const std::vector &HistogramSnapShot::get_values() -{ - return data_; -} +const std::vector &HistogramSnapShot::get_values() { return data_; } std::string HistogramSnapShot::to_string() { diff --git a/deps/common/metrics/histogram_snapshot.h b/deps/common/metrics/histogram_snapshot.h index 747d28a33..0fd78e186 100644 --- a/deps/common/metrics/histogram_snapshot.h +++ b/deps/common/metrics/histogram_snapshot.h @@ -25,7 +25,8 @@ See the Mulan PSL v2 for more details. */ namespace common { -class HistogramSnapShot : public Snapshot { +class HistogramSnapShot : public Snapshot +{ public: HistogramSnapShot(); explicit HistogramSnapShot(const std::vector &collection); diff --git a/deps/common/metrics/log_reporter.cpp b/deps/common/metrics/log_reporter.cpp index 8bc6fe9a4..8370b7b76 100644 --- a/deps/common/metrics/log_reporter.cpp +++ b/deps/common/metrics/log_reporter.cpp @@ -16,8 +16,8 @@ See the Mulan PSL v2 for more details. */ #include -#include "common/metrics/metric.h" #include "common/log/log.h" +#include "common/metrics/metric.h" namespace common { diff --git a/deps/common/metrics/log_reporter.h b/deps/common/metrics/log_reporter.h index f5cd75068..38b26cf55 100644 --- a/deps/common/metrics/log_reporter.h +++ b/deps/common/metrics/log_reporter.h @@ -12,18 +12,17 @@ See the Mulan PSL v2 for more details. */ // Created by Longda on 2021/4/20. // -#ifndef __COMMON_METRICS_LOG_REPORTER_H__ -#define __COMMON_METRICS_LOG_REPORTER_H__ +#pragma once #include "common/metrics/reporter.h" namespace common { -class LogReporter : public Reporter { +class LogReporter : public Reporter +{ public: void report(const std::string &tag, Metric *metric); }; LogReporter *get_log_reporter(); } // namespace common -#endif //__COMMON_METRICS_LOG_REPORTER_H__ diff --git a/deps/common/metrics/metric.h b/deps/common/metrics/metric.h index b9507f037..d27a8173f 100644 --- a/deps/common/metrics/metric.h +++ b/deps/common/metrics/metric.h @@ -19,14 +19,12 @@ See the Mulan PSL v2 for more details. */ namespace common { -class Metric { +class Metric +{ public: virtual void snapshot() = 0; - virtual Snapshot *get_snapshot() - { - return snapshot_value_; - } + virtual Snapshot *get_snapshot() { return snapshot_value_; } protected: Snapshot *snapshot_value_; diff --git a/deps/common/metrics/metrics.cpp b/deps/common/metrics/metrics.cpp index 7dc7dac36..69bde023e 100644 --- a/deps/common/metrics/metrics.cpp +++ b/deps/common/metrics/metrics.cpp @@ -33,15 +33,9 @@ Meter::~Meter() } } -void Meter::inc(long increase) -{ - value_.fetch_add(increase); -} +void Meter::inc(long increase) { value_.fetch_add(increase); } -void Meter::inc() -{ - inc(1l); -} +void Meter::inc() { inc(1l); } void Meter::snapshot() { @@ -53,7 +47,7 @@ void Meter::snapshot() long now_tick = now.tv_sec * 1000000 + now.tv_usec; double temp_value = ((double)value_.exchange(0l)) / ((now_tick - snapshot_tick_) / 1000000); - snapshot_tick_ = now_tick; + snapshot_tick_ = now_tick; if (snapshot_value_ == NULL) { snapshot_value_ = new SnapshotBasic(); @@ -75,10 +69,7 @@ void SimpleTimer::inc(long increase) times_.fetch_add(1); } -void SimpleTimer::update(long one) -{ - inc(one); -} +void SimpleTimer::update(long one) { inc(one); } void SimpleTimer::snapshot() { @@ -92,11 +83,11 @@ void SimpleTimer::snapshot() long value_snapshot = value_.exchange(0l); long times_snapshot = times_.exchange(0l); - double tps = 0; + double tps = 0; double mean = 0; if (times_snapshot > 0) { - tps = ((double)times_snapshot) / ((now_tick - snapshot_tick_) / 1000000); + tps = ((double)times_snapshot) / ((now_tick - snapshot_tick_) / 1000000); mean = ((double)value_snapshot) / times_snapshot; } @@ -108,19 +99,13 @@ void SimpleTimer::snapshot() ((SimplerTimerSnapshot *)snapshot_value_)->setValue(mean, tps); } -Histogram::Histogram(RandomGenerator &random) : UniformReservoir(random) -{} +Histogram::Histogram(RandomGenerator &random) : UniformReservoir(random) {} -Histogram::Histogram(RandomGenerator &random, size_t size) : UniformReservoir(random, size) -{} +Histogram::Histogram(RandomGenerator &random, size_t size) : UniformReservoir(random, size) {} -Histogram::~Histogram() -{} +Histogram::~Histogram() {} -void Histogram::snapshot() -{ - UniformReservoir::snapshot(); -} +void Histogram::snapshot() { UniformReservoir::snapshot(); } Timer::Timer(RandomGenerator &random) : UniformReservoir(random) { @@ -166,7 +151,7 @@ void Timer::snapshot() long now_tick = now.tv_sec * 1000000 + now.tv_usec; - double tps = ((double)value_.exchange(0l)) / ((now_tick - snapshot_tick_) / 1000000); + double tps = ((double)value_.exchange(0l)) / ((now_tick - snapshot_tick_) / 1000000); snapshot_tick_ = now_tick; MUTEX_LOCK(&mutex); @@ -177,11 +162,7 @@ void Timer::snapshot() timer_snapshot->set_tps(tps); } -TimerStat::TimerStat(SimpleTimer &other_st) : st_(other_st), start_tick_(0), end_tick_(0) -{ - - start(); -} +TimerStat::TimerStat(SimpleTimer &other_st) : st_(other_st), start_tick_(0), end_tick_(0) { start(); } TimerStat::~TimerStat() { diff --git a/deps/common/metrics/metrics.h b/deps/common/metrics/metrics.h index fa340bc70..781742708 100644 --- a/deps/common/metrics/metrics.h +++ b/deps/common/metrics/metrics.h @@ -24,23 +24,20 @@ See the Mulan PSL v2 for more details. */ namespace common { -class Gauge : public Metric { +class Gauge : public Metric +{ public: // user implement snapshot function - void set_snapshot(Snapshot *value) - { - snapshot_value_ = value; - } + void set_snapshot(Snapshot *value) { snapshot_value_ = value; } }; -class Counter : public Metric { - void set_snapshot(SnapshotBasic *value) - { - snapshot_value_ = value; - } +class Counter : public Metric +{ + void set_snapshot(SnapshotBasic *value) { snapshot_value_ = value; } }; -class Meter : public Metric { +class Meter : public Metric +{ public: Meter(); virtual ~Meter(); @@ -52,12 +49,13 @@ class Meter : public Metric { protected: std::atomic value_; - long snapshot_tick_; + long snapshot_tick_; }; // SimpleTimer just get tps and meanvalue // time unit is ms -class SimpleTimer : public Meter { +class SimpleTimer : public Meter +{ public: virtual ~SimpleTimer(); @@ -75,7 +73,8 @@ class SimpleTimer : public Meter { // please skip us histogram or Timer as more as possible // try use SimpleTimer to replace them. // if use histogram , please use sampling method. -class Histogram : public UniformReservoir { +class Histogram : public UniformReservoir +{ public: Histogram(RandomGenerator &random); Histogram(RandomGenerator &random, size_t size); @@ -86,7 +85,8 @@ class Histogram : public UniformReservoir { // timeunit is ms // Timer = Histogram + Meter -class Timer : public UniformReservoir { +class Timer : public UniformReservoir +{ public: Timer(RandomGenerator &random); Timer(RandomGenerator &random, size_t size); @@ -97,10 +97,11 @@ class Timer : public UniformReservoir { protected: std::atomic value_; - long snapshot_tick_; + long snapshot_tick_; }; // update ms -class TimerStat { +class TimerStat +{ public: TimerStat(SimpleTimer &st_); @@ -110,8 +111,8 @@ class TimerStat { public: SimpleTimer &st_; - long start_tick_; - long end_tick_; + long start_tick_; + long end_tick_; }; } // namespace common diff --git a/deps/common/metrics/metrics_registry.h b/deps/common/metrics/metrics_registry.h index 45b15c50b..6632a767c 100644 --- a/deps/common/metrics/metrics_registry.h +++ b/deps/common/metrics/metrics_registry.h @@ -15,16 +15,17 @@ See the Mulan PSL v2 for more details. */ #ifndef __COMMON_METRICS_METRICS_REGISTRY_H__ #define __COMMON_METRICS_METRICS_REGISTRY_H__ -#include -#include #include +#include +#include #include "common/metrics/metric.h" #include "common/metrics/reporter.h" namespace common { -class MetricsRegistry { +class MetricsRegistry +{ public: MetricsRegistry(){}; virtual ~MetricsRegistry(){}; @@ -36,14 +37,11 @@ class MetricsRegistry { void report(); - void add_reporter(Reporter *reporter) - { - reporters.push_back(reporter); - } + void add_reporter(Reporter *reporter) { reporters.push_back(reporter); } protected: std::map metrics; - std::list reporters; + std::list reporters; }; MetricsRegistry &get_metrics_registry(); diff --git a/deps/common/metrics/reporter.h b/deps/common/metrics/reporter.h index 4153258f6..23660bf4e 100644 --- a/deps/common/metrics/reporter.h +++ b/deps/common/metrics/reporter.h @@ -15,12 +15,13 @@ See the Mulan PSL v2 for more details. */ #ifndef __COMMON_METRICS_REPORTER_H__ #define __COMMON_METRICS_REPORTER_H__ -#include #include "common/metrics/metric.h" +#include namespace common { -class Reporter { +class Reporter +{ public: virtual void report(const std::string &tag, Metric *metric) = 0; }; diff --git a/deps/common/metrics/reservoir.cpp b/deps/common/metrics/reservoir.cpp index 3624f08ac..68d4ba0a6 100644 --- a/deps/common/metrics/reservoir.cpp +++ b/deps/common/metrics/reservoir.cpp @@ -16,13 +16,8 @@ See the Mulan PSL v2 for more details. */ using namespace common; -Reservoir::Reservoir(RandomGenerator &random) : random(random) -{} +Reservoir::Reservoir(RandomGenerator &random) : random(random) {} -Reservoir::~Reservoir() -{} +Reservoir::~Reservoir() {} -size_t Reservoir::next(size_t range) -{ - return random.next(range); -} +size_t Reservoir::next(size_t range) { return random.next(range); } diff --git a/deps/common/metrics/reservoir.h b/deps/common/metrics/reservoir.h index 3341c3898..08f32e700 100644 --- a/deps/common/metrics/reservoir.h +++ b/deps/common/metrics/reservoir.h @@ -23,13 +23,14 @@ See the Mulan PSL v2 for more details. */ namespace common { -class Reservoir : public Metric { +class Reservoir : public Metric +{ public: Reservoir(RandomGenerator &random); virtual ~Reservoir(); public: - virtual size_t size() = 0; + virtual size_t size() = 0; virtual size_t get_count() = 0; virtual void update(double one) = 0; diff --git a/deps/common/metrics/sampler.cpp b/deps/common/metrics/sampler.cpp index f8a59e785..41b4a0fe6 100644 --- a/deps/common/metrics/sampler.cpp +++ b/deps/common/metrics/sampler.cpp @@ -26,11 +26,9 @@ Sampler *&get_sampler() return g_sampler; } -Sampler::Sampler() : random_() -{} +Sampler::Sampler() : random_() {} -Sampler::~Sampler() -{} +Sampler::~Sampler() {} bool Sampler::sampling() { @@ -42,16 +40,13 @@ bool Sampler::sampling() } } -double Sampler::get_ratio() -{ - return ratio_; -} +double Sampler::get_ratio() { return ratio_; } void Sampler::set_ratio(double ratio) { if (0 <= ratio && ratio <= 1) { this->ratio_ = ratio; - ratio_num_ = ratio * RANGE_SIZE; + ratio_num_ = ratio * RANGE_SIZE; } else { LOG_WARN("Invalid ratio :%lf", ratio); } diff --git a/deps/common/metrics/sampler.h b/deps/common/metrics/sampler.h index 5fb3dde3b..cdb49fb65 100644 --- a/deps/common/metrics/sampler.h +++ b/deps/common/metrics/sampler.h @@ -22,19 +22,20 @@ namespace common { /** * The most simple sample function */ -class Sampler { +class Sampler +{ public: Sampler(); virtual ~Sampler(); bool sampling(); - void set_ratio(double ratio); + void set_ratio(double ratio); double get_ratio(); private: - double ratio_ = 1.0; - int ratio_num_ = 1; + double ratio_ = 1.0; + int ratio_num_ = 1; RandomGenerator random_; }; diff --git a/deps/common/metrics/snapshot.h b/deps/common/metrics/snapshot.h index 07864bec7..3254b8acf 100644 --- a/deps/common/metrics/snapshot.h +++ b/deps/common/metrics/snapshot.h @@ -14,32 +14,30 @@ See the Mulan PSL v2 for more details. */ #pragma once -#include #include "common/lang/string.h" +#include namespace common { -class Snapshot { +class Snapshot +{ public: virtual ~Snapshot(){}; virtual std::string to_string() = 0; }; template -class SnapshotBasic : public Snapshot { +class SnapshotBasic : public Snapshot +{ public: SnapshotBasic() : value(){ }; - virtual ~SnapshotBasic() - {} + virtual ~SnapshotBasic() {} - void setValue(T &input) - { - value = input; - } + void setValue(T &input) { value = input; } std::string to_string() { @@ -52,18 +50,17 @@ class SnapshotBasic : public Snapshot { T value; }; -class SimplerTimerSnapshot : public Snapshot { +class SimplerTimerSnapshot : public Snapshot +{ public: - SimplerTimerSnapshot() - {} + SimplerTimerSnapshot() {} - virtual ~SimplerTimerSnapshot() - {} + virtual ~SimplerTimerSnapshot() {} void setValue(double mean, double tps) { this->mean = mean; - this->tps = tps; + this->tps = tps; } std::string to_string() @@ -76,6 +73,6 @@ class SimplerTimerSnapshot : public Snapshot { private: double mean = 1.0; - double tps = 1.0; + double tps = 1.0; }; } // namespace common diff --git a/deps/common/metrics/timer_snapshot.cpp b/deps/common/metrics/timer_snapshot.cpp index 1b10b3660..5c49775cf 100644 --- a/deps/common/metrics/timer_snapshot.cpp +++ b/deps/common/metrics/timer_snapshot.cpp @@ -17,21 +17,13 @@ See the Mulan PSL v2 for more details. */ namespace common { -TimerSnapshot::TimerSnapshot() -{} +TimerSnapshot::TimerSnapshot() {} -TimerSnapshot::~TimerSnapshot() -{} +TimerSnapshot::~TimerSnapshot() {} -double TimerSnapshot::get_tps() -{ - return tps; -} +double TimerSnapshot::get_tps() { return tps; } -void TimerSnapshot::set_tps(double tps) -{ - this->tps = tps; -} +void TimerSnapshot::set_tps(double tps) { this->tps = tps; } std::string TimerSnapshot::to_string() { diff --git a/deps/common/metrics/timer_snapshot.h b/deps/common/metrics/timer_snapshot.h index 01d668817..06f2cb2dc 100644 --- a/deps/common/metrics/timer_snapshot.h +++ b/deps/common/metrics/timer_snapshot.h @@ -17,13 +17,14 @@ See the Mulan PSL v2 for more details. */ #include "common/metrics/histogram_snapshot.h" namespace common { -class TimerSnapshot : public HistogramSnapShot { +class TimerSnapshot : public HistogramSnapShot +{ public: TimerSnapshot(); virtual ~TimerSnapshot(); double get_tps(); - void set_tps(double tps); + void set_tps(double tps); std::string to_string(); diff --git a/deps/common/metrics/uniform_reservoir.cpp b/deps/common/metrics/uniform_reservoir.cpp index 926d3d3dc..2a494e500 100644 --- a/deps/common/metrics/uniform_reservoir.cpp +++ b/deps/common/metrics/uniform_reservoir.cpp @@ -86,7 +86,7 @@ void UniformReservoir::update(double value) data[count] = (value); } else { size_t rcount = next(data.size()); - data[rcount] = (value); + data[rcount] = (value); } MUTEX_UNLOCK(&mutex); diff --git a/deps/common/metrics/uniform_reservoir.h b/deps/common/metrics/uniform_reservoir.h index 924da90fb..d08790a2a 100644 --- a/deps/common/metrics/uniform_reservoir.h +++ b/deps/common/metrics/uniform_reservoir.h @@ -30,7 +30,8 @@ namespace common { * with a Reservoir */ -class UniformReservoir : public Reservoir { +class UniformReservoir : public Reservoir +{ public: UniformReservoir(RandomGenerator &random); UniformReservoir(RandomGenerator &random, size_t size); @@ -49,10 +50,10 @@ class UniformReservoir : public Reservoir { void init(size_t size); protected: - pthread_mutex_t mutex; - size_t counter; // counter is likely to be bigger than data.size() + pthread_mutex_t mutex; + size_t counter; // counter is likely to be bigger than data.size() std::vector data; - RandomGenerator random; + RandomGenerator random; }; } // namespace common diff --git a/deps/common/mm/debug_new.h b/deps/common/mm/debug_new.h index 364f628e0..52d11737b 100644 --- a/deps/common/mm/debug_new.h +++ b/deps/common/mm/debug_new.h @@ -19,7 +19,7 @@ See the Mulan PSL v2 for more details. */ namespace common { /* Prototypes */ -bool check_leaks(); +bool check_leaks(); void *operator new(size_t size, const char *file, int line); void *operator new[](size_t size, const char *file, int line); #ifndef NO_PLACEMENT_DELETE @@ -39,7 +39,9 @@ void operator delete[](void *); // MSVC 6 requires this declaration #ifdef DEBUG_NEW_EMULATE_MALLOC #define malloc(s) ((void *)(debug_new char[s])) + #define free(p) delete[](char *)(p) + #endif // DEBUG_NEW_EMULATE_MALLOC /* Control flags */ diff --git a/deps/common/mm/mem.h b/deps/common/mm/mem.h index 06ee951cd..c4f96e3c4 100644 --- a/deps/common/mm/mem.h +++ b/deps/common/mm/mem.h @@ -30,16 +30,18 @@ namespace common { #else -typedef struct MemID_t { +typedef struct MemID_t +{ public: const static int MEM_FILENAME_LEN = 32; - struct MemID_t *mNext; - char mFile[MEM_FILENAME_LEN]; - uint64_t mSize; - uint32_t mLine; + struct MemID_t *mNext; + char mFile[MEM_FILENAME_LEN]; + uint64_t mSize; + uint32_t mLine; } MemID; -class CLMemTrace { +class CLMemTrace +{ public: static void *malloc(size_t size, const char *file, const int line, bool retry = false) throw(std::bad_alloc); @@ -53,10 +55,7 @@ class CLMemTrace { /** * set whether show every details */ - static void setVerbose(bool verbose) - { - mVerbose = verbose; - } + static void setVerbose(bool verbose) { mVerbose = verbose; } protected: static std::new_handler getNewHandler(); @@ -68,10 +67,10 @@ class CLMemTrace { // static std::map mMemIds; const static int MEM_HASHTABLE_SIZE = 16384; - static MemID *mMemIDs[MEM_HASHTABLE_SIZE]; - static uint64_t mUsedSize; + static MemID *mMemIDs[MEM_HASHTABLE_SIZE]; + static uint64_t mUsedSize; static pthread_mutex_t mMutex; - static bool mVerbose; + static bool mVerbose; }; #define lcalloc(nmemb, size) Lcalloc(nmemb, size, __FILE__, __LINE__) @@ -84,7 +83,7 @@ class CLMemTrace { void *Lcalloc(size_t nmemb, size_t size, const char *file, const int line); void *Lmalloc(size_t size, const char *file, const int line); -void Lfree(void *ptr); +void Lfree(void *ptr); void *Lrealloc(void *ptr, size_t size, const char *file, const int line); /** diff --git a/deps/common/mm/mem_pool.cpp b/deps/common/mm/mem_pool.cpp index 22aa44703..b7e2dc291 100644 --- a/deps/common/mm/mem_pool.cpp +++ b/deps/common/mm/mem_pool.cpp @@ -27,7 +27,7 @@ int MemPoolItem::init(int item_size, bool dynamic, int pool_num, int item_num_pe return -1; } - this->item_size = item_size; + this->item_size = item_size; this->item_num_per_pool = item_num_per_pool; // in order to init memory pool, enable dynamic here this->dynamic = true; @@ -128,8 +128,8 @@ void *MemPoolItem::alloc() MemPoolItem::unique_ptr MemPoolItem::alloc_unique_ptr() { - void *item = this->alloc(); - auto deleter = [this](void *p) { this->free(p); }; + void *item = this->alloc(); + auto deleter = [this](void *p) { this->free(p); }; return MemPoolItem::unique_ptr(item, deleter); } diff --git a/deps/common/mm/mem_pool.h b/deps/common/mm/mem_pool.h index 2b6b3ad78..39a345878 100644 --- a/deps/common/mm/mem_pool.h +++ b/deps/common/mm/mem_pool.h @@ -14,13 +14,13 @@ See the Mulan PSL v2 for more details. */ #pragma once -#include +#include #include +#include +#include #include -#include #include -#include -#include +#include #include "common/lang/mutex.h" #include "common/log/log.h" @@ -48,10 +48,7 @@ class MemPool MUTEX_INIT(&mutex, &mutexatr); } - virtual ~MemPool() - { - MUTEX_DESTROY(&mutex); - } + virtual ~MemPool() { MUTEX_DESTROY(&mutex); } /** * init memory pool, the major job is to alloc memory for memory pool @@ -90,24 +87,15 @@ class MemPool */ virtual std::string to_string() = 0; - const std::string get_name() const - { - return name; - } - bool is_dynamic() const - { - return dynamic; - } - int get_size() const - { - return size; - } + const std::string get_name() const { return name; } + bool is_dynamic() const { return dynamic; } + int get_size() const { return size; } protected: pthread_mutex_t mutex; - int size; - bool dynamic; - std::string name; + int size; + bool dynamic; + std::string name; }; /** @@ -119,13 +107,9 @@ template class MemPoolSimple : public MemPool { public: - MemPoolSimple(const char *tag) : MemPool(tag) - {} + MemPoolSimple(const char *tag) : MemPool(tag) {} - virtual ~MemPoolSimple() - { - cleanup(); - } + virtual ~MemPoolSimple() { cleanup(); } /** * init memory pool, the major job is to alloc memory for memory pool @@ -163,10 +147,7 @@ class MemPoolSimple : public MemPool */ std::string to_string(); - int get_item_num_per_pool() const - { - return item_num_per_pool; - } + int get_item_num_per_pool() const { return item_num_per_pool; } int get_used_num() { @@ -178,9 +159,9 @@ class MemPoolSimple : public MemPool protected: std::list pools; - std::set used; + std::set used; std::list frees; - int item_num_per_pool; + int item_num_per_pool; }; template @@ -295,7 +276,7 @@ template void MemPoolSimple::free(T *buf) { buf->reset(); - + MUTEX_LOCK(&this->mutex); size_t num = used.erase(buf); @@ -329,7 +310,7 @@ std::string MemPoolSimple::to_string() class MemPoolItem { public: - using unique_ptr = std::unique_ptr>; + using unique_ptr = std::unique_ptr>; public: MemPoolItem(const char *tag) : name(tag) @@ -372,7 +353,7 @@ class MemPoolItem * Alloc one frame from memory Pool * @return */ - void *alloc(); + void *alloc(); unique_ptr alloc_unique_ptr(); /** @@ -408,26 +389,11 @@ class MemPoolItem return ss.str(); } - const std::string get_name() const - { - return name; - } - bool is_dynamic() const - { - return dynamic; - } - int get_size() const - { - return size; - } - int get_item_size() const - { - return item_size; - } - int get_item_num_per_pool() const - { - return item_num_per_pool; - } + const std::string get_name() const { return name; } + bool is_dynamic() const { return dynamic; } + int get_size() const { return size; } + int get_item_size() const { return item_size; } + int get_item_num_per_pool() const { return item_num_per_pool; } int get_used_num() { @@ -439,14 +405,14 @@ class MemPoolItem protected: pthread_mutex_t mutex; - std::string name; - bool dynamic; - int size; - int item_size; - int item_num_per_pool; + std::string name; + bool dynamic; + int size; + int item_size; + int item_num_per_pool; std::list pools; - std::set used; + std::set used; std::list frees; }; diff --git a/deps/common/os/os.cpp b/deps/common/os/os.cpp index 2b0690b5c..88eef3654 100644 --- a/deps/common/os/os.cpp +++ b/deps/common/os/os.cpp @@ -12,27 +12,24 @@ See the Mulan PSL v2 for more details. */ // Created by Longda on 2010. // -#include #include +#include #include "common/defs.h" -#include "common/os/os.h" #include "common/log/log.h" +#include "common/os/os.h" namespace common { // Don't care windows -uint32_t getCpuNum() -{ - return std::thread::hardware_concurrency(); -} +uint32_t getCpuNum() { return std::thread::hardware_concurrency(); } #define MAX_STACK_SIZE 32 void print_stacktrace() { - int size = MAX_STACK_SIZE; - void *array[MAX_STACK_SIZE]; - int stack_num = backtrace(array, size); + int size = MAX_STACK_SIZE; + void *array[MAX_STACK_SIZE]; + int stack_num = backtrace(array, size); char **stacktrace = backtrace_symbols(array, stack_num); for (int i = 0; i < stack_num; ++i) { LOG_INFO("%d ----- %s\n", i, stacktrace[i]); diff --git a/deps/common/os/path.cpp b/deps/common/os/path.cpp index 441780a25..ada5651ef 100644 --- a/deps/common/os/path.cpp +++ b/deps/common/os/path.cpp @@ -11,22 +11,22 @@ See the Mulan PSL v2 for more details. */ // // Created by Longda on 2010 // -#include -#include #include +#include #include +#include #include #include "common/defs.h" -#include "common/os/path.h" #include "common/log/log.h" +#include "common/os/path.h" namespace common { std::string getFileName(const std::string &fullPath) { std::string szRt; - size_t pos; + size_t pos; try { pos = fullPath.rfind(FILE_PATH_SPLIT); if (pos != std::string::npos && pos < fullPath.size() - 1) { @@ -62,7 +62,7 @@ void getFileName(const char *path, std::string &fileName) std::string getDirName(const std::string &fullPath) { std::string szRt; - size_t pos; + size_t pos; try { pos = fullPath.rfind(FILE_PATH_SPLIT); if (pos != std::string::npos && pos > 0) { @@ -98,7 +98,7 @@ void getDirName(const char *path, std::string &parent) std::string getFilePath(const std::string &fullPath) { std::string szRt; - size_t pos; + size_t pos; try { pos = fullPath.rfind("/"); if (pos != std::string::npos) { @@ -118,7 +118,7 @@ std::string getAboslutPath(const char *path) std::string aPath(path); if (path[0] != '/') { const int MAX_SIZE = 256; - char current_absolute_path[MAX_SIZE]; + char current_absolute_path[MAX_SIZE]; if (NULL == getcwd(current_absolute_path, MAX_SIZE)) {} } @@ -157,7 +157,7 @@ bool check_directory(std::string &path) if (0 != mkdir(path.c_str(), 0777) && !is_directory(path.c_str())) return false; - path[i] = '/'; + path[i] = '/'; sep_state = true; } @@ -193,7 +193,7 @@ int list_file(const char *path, const char *filter_pattern, std::vectord_name[0]) // 跳过./..文件和隐藏文件 continue; diff --git a/deps/common/os/path.h b/deps/common/os/path.h index 09a638bbb..5ff6fcb81 100644 --- a/deps/common/os/path.h +++ b/deps/common/os/path.h @@ -27,7 +27,7 @@ namespace common { * input: "" --> return : "" */ std::string getFileName(const std::string &fullPath); -void getFileName(const char *path, std::string &fileName); +void getFileName(const char *path, std::string &fileName); /** * get file path from full path @@ -38,7 +38,7 @@ void getFileName(const char *path, std::string &fileName); * input: "" --> return : "" */ std::string getFilePath(const std::string &fullPath); -void getDirName(const char *path, std::string &parent); +void getDirName(const char *path, std::string &parent); /** * Get absolute path diff --git a/deps/common/os/pidfile.cpp b/deps/common/os/pidfile.cpp index 7cbe0a481..3ec8443de 100644 --- a/deps/common/os/pidfile.cpp +++ b/deps/common/os/pidfile.cpp @@ -48,7 +48,7 @@ int writePidFile(const char *progName) { assert(progName); std::ofstream ostr; - int rv = 1; + int rv = 1; setPidPath(progName); std::string path = getPidPath(); diff --git a/deps/common/os/process.cpp b/deps/common/os/process.cpp index 1c46b5fd6..a18292614 100644 --- a/deps/common/os/process.cpp +++ b/deps/common/os/process.cpp @@ -48,7 +48,7 @@ std::string get_process_name(const char *prog_name) std::cerr << "Failed to alloc memory for program name." << SYS_OUTPUT_FILE_POS << SYS_OUTPUT_ERROR << std::endl; return ""; } - snprintf(buf, buf_len + 1, "%s", prog_name);//第二个参数需为buf_len + 1 + snprintf(buf, buf_len + 1, "%s", prog_name); // 第二个参数需为buf_len + 1 process_name = basename(buf); diff --git a/deps/common/os/process_param.h b/deps/common/os/process_param.h index 9953f4c0e..6e0b2b857 100644 --- a/deps/common/os/process_param.h +++ b/deps/common/os/process_param.h @@ -18,106 +18,51 @@ See the Mulan PSL v2 for more details. */ #include namespace common { -class ProcessParam { +class ProcessParam +{ public: - ProcessParam() - {} + ProcessParam() {} - virtual ~ProcessParam() - {} + virtual ~ProcessParam() {} void init_default(std::string &process_name); - const std::string &get_std_out() const - { - return std_out_; - } + const std::string &get_std_out() const { return std_out_; } - void set_std_out(const std::string &std_out) - { - ProcessParam::std_out_ = std_out; - } + void set_std_out(const std::string &std_out) { ProcessParam::std_out_ = std_out; } - const std::string &get_std_err() const - { - return std_err_; - } + const std::string &get_std_err() const { return std_err_; } - void set_std_err(const std::string &std_err) - { - ProcessParam::std_err_ = std_err; - } + void set_std_err(const std::string &std_err) { ProcessParam::std_err_ = std_err; } - const std::string &get_conf() const - { - return conf; - } + const std::string &get_conf() const { return conf; } - void set_conf(const std::string &conf) - { - ProcessParam::conf = conf; - } + void set_conf(const std::string &conf) { ProcessParam::conf = conf; } - const std::string &get_process_name() const - { - return process_name_; - } + const std::string &get_process_name() const { return process_name_; } - void set_process_name(const std::string &processName) - { - ProcessParam::process_name_ = processName; - } + void set_process_name(const std::string &processName) { ProcessParam::process_name_ = processName; } - bool is_demon() const - { - return demon; - } + bool is_demon() const { return demon; } - void set_demon(bool demon) - { - ProcessParam::demon = demon; - } + void set_demon(bool demon) { ProcessParam::demon = demon; } - const std::vector &get_args() const - { - return args; - } + const std::vector &get_args() const { return args; } - void set_args(const std::vector &args) - { - ProcessParam::args = args; - } + void set_args(const std::vector &args) { ProcessParam::args = args; } - void set_server_port(int port) - { - server_port_ = port; - } + void set_server_port(int port) { server_port_ = port; } - int get_server_port() const - { - return server_port_; - } + int get_server_port() const { return server_port_; } - void set_unix_socket_path(const char *unix_socket_path) - { - unix_socket_path_ = unix_socket_path; - } + void set_unix_socket_path(const char *unix_socket_path) { unix_socket_path_ = unix_socket_path; } - const std::string &get_unix_socket_path() const - { - return unix_socket_path_; - } + const std::string &get_unix_socket_path() const { return unix_socket_path_; } - void set_protocol(const char *protocol) - { - protocol_ = protocol; - } + void set_protocol(const char *protocol) { protocol_ = protocol; } - const std::string &get_protocol() const - { - return protocol_; - } + const std::string &get_protocol() const { return protocol_; } void set_trx_kit_name(const char *kit_name) { @@ -125,33 +70,24 @@ class ProcessParam { trx_kit_name_ = kit_name; } } - const std::string &trx_kit_name() const - { - return trx_kit_name_; - } + const std::string &trx_kit_name() const { return trx_kit_name_; } - void set_buffer_pool_memory_size(int bytes) - { - buffer_pool_memory_size_ = bytes; - } + void set_buffer_pool_memory_size(int bytes) { buffer_pool_memory_size_ = bytes; } - int buffer_pool_memory_size() const - { - return buffer_pool_memory_size_; - } + int buffer_pool_memory_size() const { return buffer_pool_memory_size_; } private: - std::string std_out_; // The output file - std::string std_err_; // The err output file - std::string conf; // The configuration file - std::string process_name_; // The process name - bool demon = false; // whether demon or not - std::vector args; // arguments - int server_port_ = -1; // server port(if valid, will overwrite the port in the config file) - std::string unix_socket_path_; - std::string protocol_; - std::string trx_kit_name_; - int buffer_pool_memory_size_ = -1; + std::string std_out_; // The output file + std::string std_err_; // The err output file + std::string conf; // The configuration file + std::string process_name_; // The process name + bool demon = false; // whether demon or not + std::vector args; // arguments + int server_port_ = -1; // server port(if valid, will overwrite the port in the config file) + std::string unix_socket_path_; + std::string protocol_; + std::string trx_kit_name_; + int buffer_pool_memory_size_ = -1; }; ProcessParam *&the_process_param(); diff --git a/deps/common/os/signal.cpp b/deps/common/os/signal.cpp index 363512ff9..156ac21d9 100644 --- a/deps/common/os/signal.cpp +++ b/deps/common/os/signal.cpp @@ -21,8 +21,9 @@ void setSignalHandler(int sig, sighandler_t func) { struct sigaction newsa, oldsa; sigemptyset(&newsa.sa_mask); - newsa.sa_flags = 0; + newsa.sa_flags = 0; newsa.sa_handler = func; + int rc = sigaction(sig, &newsa, &oldsa); if (rc) { std::cerr << "Failed to set signal " << sig << SYS_OUTPUT_FILE_POS << SYS_OUTPUT_ERROR << std::endl; @@ -68,9 +69,9 @@ void *waitForSignals(void *args) { LOG_INFO("Start thread to wait signals."); sigset_t *signal_set = (sigset_t *)args; - int sig_number = -1; + int sig_number = -1; while (true) { - errno = 0; + errno = 0; int ret = sigwait(signal_set, &sig_number); LOG_INFO("sigwait return value: %d, %d \n", ret, sig_number); if (ret != 0) { @@ -82,7 +83,7 @@ void *waitForSignals(void *args) void startWaitForSignals(sigset_t *signal_set) { - pthread_t pThread; + pthread_t pThread; pthread_attr_t pThreadAttrs; // create all threads as detached. We will not try to join them. diff --git a/deps/common/os/signal.h b/deps/common/os/signal.h index 1346d240e..63a44b1e3 100644 --- a/deps/common/os/signal.h +++ b/deps/common/os/signal.h @@ -30,7 +30,7 @@ void blockDefaultSignals(sigset_t *signal_set, sigset_t *old_set); void unBlockDefaultSignals(sigset_t *signal_set, sigset_t *old_set); void *waitForSignals(sigset_t *signal_set); -void startWaitForSignals(sigset_t *signal_set); +void startWaitForSignals(sigset_t *signal_set); // Set signal handling function /** diff --git a/deps/common/seda/callback.cpp b/deps/common/seda/callback.cpp index c2637b799..1f6b57780 100644 --- a/deps/common/seda/callback.cpp +++ b/deps/common/seda/callback.cpp @@ -75,10 +75,7 @@ void CompletionCallback::event_done(StageEvent *ev) } // Reschedule callback on target stage thread -void CompletionCallback::event_reschedule(StageEvent *ev) -{ - target_stage_->add_event(ev); -} +void CompletionCallback::event_reschedule(StageEvent *ev) { target_stage_->add_event(ev); } void CompletionCallback::event_timeout(StageEvent *ev) { diff --git a/deps/common/seda/callback.h b/deps/common/seda/callback.h index 6117d1cc8..9b605af35 100644 --- a/deps/common/seda/callback.h +++ b/deps/common/seda/callback.h @@ -54,7 +54,8 @@ class CallbackContext; * the current thread. */ -class CompletionCallback { +class CompletionCallback +{ // public interface operations @@ -86,10 +87,10 @@ class CompletionCallback { protected: // implementation state - Stage *target_stage_; // stage which is setting this callback - CallbackContext *context_; // argument to pass when invoking cb - CompletionCallback *next_cb_; // next event in the chain - bool ev_hist_flag_; // true if event histories are enabled + Stage *target_stage_; // stage which is setting this callback + CallbackContext *context_; // argument to pass when invoking cb + CompletionCallback *next_cb_; // next event in the chain + bool ev_hist_flag_; // true if event histories are enabled }; /** @@ -99,22 +100,18 @@ class CompletionCallback { * invoked. To make use of this feature, a stage should derive its own * callback context class from this base. */ -class CallbackContext { +class CallbackContext +{ public: - virtual ~CallbackContext() - {} + virtual ~CallbackContext() {} }; -class CallbackContextEvent : public CallbackContext { +class CallbackContextEvent : public CallbackContext +{ public: - CallbackContextEvent(StageEvent *event = NULL) : ev_(event) - {} - ~CallbackContextEvent() - {} - StageEvent *get_event() - { - return ev_; - } + CallbackContextEvent(StageEvent *event = NULL) : ev_(event) {} + ~CallbackContextEvent() {} + StageEvent *get_event() { return ev_; } private: StageEvent *ev_; diff --git a/deps/common/seda/class_factory.h b/deps/common/seda/class_factory.h index 53e93b1f1..e03d91031 100644 --- a/deps/common/seda/class_factory.h +++ b/deps/common/seda/class_factory.h @@ -41,7 +41,8 @@ namespace common { */ template -class ClassFactory { +class ClassFactory +{ public: typedef T *(*FactoryFunc)(const std::string &); @@ -71,9 +72,9 @@ class ClassFactory { // Accessor function that gets the head of the factory list static ClassFactory *&fact_list_head(); - std::string identifier_; // identifier for this factory - FactoryFunc fact_func_; // factory function for this class - ClassFactory *next_; // next factory in global list + std::string identifier_; // identifier for this factory + FactoryFunc fact_func_; // factory function for this class + ClassFactory *next_; // next factory in global list }; /** @@ -101,7 +102,7 @@ ClassFactory *&ClassFactory::fact_list_head() template ClassFactory::ClassFactory(const std::string &tag, FactoryFunc func) : identifier_(tag), fact_func_(func) { - next_ = fact_list_head(); + next_ = fact_list_head(); fact_list_head() = this; } @@ -119,8 +120,8 @@ ClassFactory::~ClassFactory() template T *ClassFactory::make_instance(const std::string &tag) { - T *instance = NULL; - ClassFactory *current = fact_list_head(); + T *instance = NULL; + ClassFactory *current = fact_list_head(); // search the global factory list for a match while ((current != NULL) && (tag != current->identifier_)) { @@ -130,7 +131,7 @@ T *ClassFactory::make_instance(const std::string &tag) // if we have a match, create and return an instance if (current != NULL) { FactoryFunc fptr = current->fact_func_; - instance = (*fptr)(tag); + instance = (*fptr)(tag); } ASSERT((instance != NULL), "%s%s", tag.c_str(), "instance not created"); diff --git a/deps/common/seda/event_dispatcher.cpp b/deps/common/seda/event_dispatcher.cpp index 1cc61965e..ebe1a5386 100644 --- a/deps/common/seda/event_dispatcher.cpp +++ b/deps/common/seda/event_dispatcher.cpp @@ -47,9 +47,9 @@ void EventDispatcher::handle_event(StageEvent *event) { LOG_TRACE("enter\n"); - std::string hash; + std::string hash; DispatchContext *ctx = NULL; - status_t stat; + status_t stat; pthread_mutex_lock(&event_lock_); stat = dispatch_event(event, ctx, hash); @@ -106,7 +106,7 @@ void EventDispatcher::cleanup() // Wake up a stored event bool EventDispatcher::wakeup_event(std::string hashkey) { - bool sent = false; + bool sent = false; EventHash::iterator i; i = event_store_.find(hashkey); diff --git a/deps/common/seda/event_dispatcher.h b/deps/common/seda/event_dispatcher.h index dab59ab33..f9b089527 100644 --- a/deps/common/seda/event_dispatcher.h +++ b/deps/common/seda/event_dispatcher.h @@ -50,12 +50,18 @@ class DispatchContext; * or process for waking up stored events at the appropriate time. */ -class EventDispatcher : public Stage { +class EventDispatcher : public Stage +{ // public interface operations public: - typedef enum { SEND_EVENT = 0, STORE_EVENT, FAIL_EVENT } status_t; + typedef enum + { + SEND_EVENT = 0, + STORE_EVENT, + FAIL_EVENT + } status_t; /** * Destructor @@ -95,10 +101,7 @@ class EventDispatcher : public Stage { bool initialize(); // set properties for this object - bool set_properties() - { - return true; - } + bool set_properties() { return true; } /** * Cleanup stage after disconnection @@ -134,12 +137,12 @@ class EventDispatcher : public Stage { // implementation state - typedef std::pair StoredEvent; + typedef std::pair StoredEvent; typedef std::map> EventHash; - EventHash event_store_; // events stored here while waiting - pthread_mutex_t event_lock_; // protects access to event_store_ - Stage *next_stage_; // target for dispatched events + EventHash event_store_; // events stored here while waiting + pthread_mutex_t event_lock_; // protects access to event_store_ + Stage *next_stage_; // target for dispatched events protected: }; @@ -148,10 +151,10 @@ class EventDispatcher : public Stage { * Class to store context info with the stored event. Subclasses should * derive from this base class. */ -class DispatchContext { +class DispatchContext +{ public: - virtual ~DispatchContext() - {} + virtual ~DispatchContext() {} }; } // namespace common diff --git a/deps/common/seda/example_stage.cpp b/deps/common/seda/example_stage.cpp index 683aefed1..3bf7469a4 100644 --- a/deps/common/seda/example_stage.cpp +++ b/deps/common/seda/example_stage.cpp @@ -26,12 +26,10 @@ See the Mulan PSL v2 for more details. */ using namespace common; // Constructor -ExampleStage::ExampleStage(const char *tag) : Stage(tag) -{} +ExampleStage::ExampleStage(const char *tag) : Stage(tag) {} // Destructor -ExampleStage::~ExampleStage() -{} +ExampleStage::~ExampleStage() {} // Parse properties, instantiate a stage object Stage *ExampleStage::make_stage(const std::string &tag) diff --git a/deps/common/seda/example_stage.h b/deps/common/seda/example_stage.h index ce0694852..6f0409497 100644 --- a/deps/common/seda/example_stage.h +++ b/deps/common/seda/example_stage.h @@ -18,7 +18,8 @@ See the Mulan PSL v2 for more details. */ namespace common { -class ExampleStage : public Stage { +class ExampleStage : public Stage +{ public: ~ExampleStage(); static Stage *make_stage(const std::string &tag); @@ -34,4 +35,3 @@ class ExampleStage : public Stage { void callback_event(StageEvent *event, CallbackContext *context); }; } // namespace common - diff --git a/deps/common/seda/init.cpp b/deps/common/seda/init.cpp index ac6bfe42d..46ddce6b6 100644 --- a/deps/common/seda/init.cpp +++ b/deps/common/seda/init.cpp @@ -28,13 +28,13 @@ See the Mulan PSL v2 for more details. */ #include "common/io/io.h" #include "common/log/log.h" -#include "common/time/datetime.h" #include "common/seda/kill_thread.h" +#include "common/seda/metrics_stage.h" #include "common/seda/seda_config.h" #include "common/seda/stage_factory.h" -#include "common/seda/metrics_stage.h" #include "common/seda/thread_pool.h" #include "common/seda/timer_stage.h" +#include "common/time/datetime.h" namespace common { int init_seda(ProcessParam *process_cfg) @@ -48,7 +48,7 @@ int init_seda(ProcessParam *process_cfg) static StageFactory seda_stats_factory("MetricsStage", &MetricsStage::make_stage); // try to parse the seda configuration files - SedaConfig *config = SedaConfig::get_instance(); + SedaConfig *config = SedaConfig::get_instance(); SedaConfig::status_t config_stat; config_stat = config->parse(); diff --git a/deps/common/seda/init.h b/deps/common/seda/init.h index f66cb5257..a2ff7bf97 100644 --- a/deps/common/seda/init.h +++ b/deps/common/seda/init.h @@ -17,9 +17,9 @@ See the Mulan PSL v2 for more details. */ // Basic includes #include -#include #include #include +#include #include #include #include diff --git a/deps/common/seda/kill_thread.cpp b/deps/common/seda/kill_thread.cpp index 3d56a3f81..fc727b366 100644 --- a/deps/common/seda/kill_thread.cpp +++ b/deps/common/seda/kill_thread.cpp @@ -39,10 +39,7 @@ void KillThreadStage::handle_event(StageEvent *event) * @post initializing the class members * @return the class object */ -Stage *KillThreadStage::make_stage(const std::string &tag) -{ - return new KillThreadStage(tag.c_str()); -} +Stage *KillThreadStage::make_stage(const std::string &tag) { return new KillThreadStage(tag.c_str()); } bool KillThreadStage::set_properties() { diff --git a/deps/common/seda/kill_thread.h b/deps/common/seda/kill_thread.h index 6fe0b2aef..4d2e34d4c 100644 --- a/deps/common/seda/kill_thread.h +++ b/deps/common/seda/kill_thread.h @@ -34,7 +34,8 @@ class Threadpool; * need to be killed. Each event handled by the stage results in the * death of the thread. */ -class KillThreadStage : public Stage { +class KillThreadStage : public Stage +{ public: /** @@ -54,8 +55,7 @@ class KillThreadStage : public Stage { * @post event queue is empty * @post stage is not connected */ - KillThreadStage(const char *tag) : Stage(tag) - {} + KillThreadStage(const char *tag) : Stage(tag) {} /** * Notify the pool and kill the thread @@ -69,10 +69,7 @@ class KillThreadStage : public Stage { * Handle the callback * Nothing special for callbacks in this stage. */ - void callback_event(StageEvent *event, CallbackContext *context) - { - return; - } + void callback_event(StageEvent *event, CallbackContext *context) { return; } /** * Initialize stage params @@ -81,10 +78,7 @@ class KillThreadStage : public Stage { * @pre Stage not connected * @return true */ - bool initialize() - { - return true; - } + bool initialize() { return true; } /** * set properties for this object diff --git a/deps/common/seda/metrics_report_event.h b/deps/common/seda/metrics_report_event.h index f3991c61f..085d44678 100644 --- a/deps/common/seda/metrics_report_event.h +++ b/deps/common/seda/metrics_report_event.h @@ -18,7 +18,8 @@ See the Mulan PSL v2 for more details. */ #include "common/seda/stage_event.h" namespace common { -class MetricsReportEvent : public StageEvent { +class MetricsReportEvent : public StageEvent +{ public: MetricsReportEvent(){ diff --git a/deps/common/seda/metrics_stage.cpp b/deps/common/seda/metrics_stage.cpp index 8ddc6b027..f8bd8a2f3 100644 --- a/deps/common/seda/metrics_stage.cpp +++ b/deps/common/seda/metrics_stage.cpp @@ -23,9 +23,9 @@ See the Mulan PSL v2 for more details. */ #include "common/log/log.h" #include "common/metrics/metrics_registry.h" #include "common/seda/metrics_report_event.h" +#include "common/seda/seda_defs.h" #include "common/seda/timer_stage.h" #include "common/time/datetime.h" -#include "common/seda/seda_defs.h" using namespace common; @@ -37,12 +37,10 @@ MetricsRegistry &get_metric_registry() } // Constructor -MetricsStage::MetricsStage(const char *tag) : Stage(tag) -{} +MetricsStage::MetricsStage(const char *tag) : Stage(tag) {} // Destructor -MetricsStage::~MetricsStage() -{} +MetricsStage::~MetricsStage() {} // Parse properties, instantiate a stage object Stage *MetricsStage::make_stage(const std::string &tag) @@ -59,13 +57,13 @@ Stage *MetricsStage::make_stage(const std::string &tag) // Set properties for this object set in stage specific properties bool MetricsStage::set_properties() { - std::string stage_name_str(stage_name_); + std::string stage_name_str(stage_name_); std::map section = get_properties()->get(stage_name_str); metric_report_interval_ = DateTime::SECONDS_PER_MIN; - std::string key = METRCS_REPORT_INTERVAL; - std::map::iterator it = section.find(key); + std::string key = METRCS_REPORT_INTERVAL; + std::map::iterator it = section.find(key); if (it != section.end()) { str_to_val(it->second, metric_report_interval_); } @@ -77,7 +75,7 @@ bool MetricsStage::set_properties() bool MetricsStage::initialize() { std::list::iterator stgp = next_stage_list_.begin(); - timer_stage_ = *(stgp++); + timer_stage_ = *(stgp++); MetricsReportEvent *report_event = new MetricsReportEvent(); @@ -86,9 +84,7 @@ bool MetricsStage::initialize() } // Cleanup after disconnection -void MetricsStage::cleanup() -{ -} +void MetricsStage::cleanup() {} void MetricsStage::handle_event(StageEvent *event) { diff --git a/deps/common/seda/metrics_stage.h b/deps/common/seda/metrics_stage.h index b556ec6c8..dd698e4b7 100644 --- a/deps/common/seda/metrics_stage.h +++ b/deps/common/seda/metrics_stage.h @@ -19,7 +19,8 @@ See the Mulan PSL v2 for more details. */ namespace common { -class MetricsStage : public Stage { +class MetricsStage : public Stage +{ public: ~MetricsStage(); static Stage *make_stage(const std::string &tag); diff --git a/deps/common/seda/seda_config.cpp b/deps/common/seda/seda_config.cpp index cc1a6d727..e5463c6e8 100644 --- a/deps/common/seda/seda_config.cpp +++ b/deps/common/seda/seda_config.cpp @@ -15,8 +15,8 @@ See the Mulan PSL v2 for more details. */ #include #include -#include #include +#include #include #include "common/lang/string.h" @@ -38,10 +38,7 @@ SedaConfig *&SedaConfig::get_instance() } // Constructor -SedaConfig::SedaConfig() : cfg_file_(), cfg_str_(), thread_pools_(), stages_() -{ - return; -} +SedaConfig::SedaConfig() : cfg_file_(), cfg_str_(), thread_pools_(), stages_() { return; } // Destructor SedaConfig::~SedaConfig() @@ -114,12 +111,12 @@ SedaConfig::status_t SedaConfig::start() // start the stages_ one by one. connect() calls std::map::iterator iter = stages_.begin(); - std::map::iterator end = stages_.end(); + std::map::iterator end = stages_.end(); while (iter != end) { if (iter->second != NULL) { Stage *stg = iter->second; - bool ret = stg->connect(); + bool ret = stg->connect(); if (!ret) { cleanup(); stat = INITFAIL; @@ -162,7 +159,7 @@ void SedaConfig::cleanup() // first disconnect all stages_ if (stages_.empty() == false) { std::map::iterator iter = stages_.begin(); - std::map::iterator end = stages_.end(); + std::map::iterator end = stages_.end(); while (iter != end) { if (iter->second != NULL) { Stage *stg = iter->second; @@ -181,14 +178,14 @@ void SedaConfig::cleanup() void SedaConfig::init_event_history() { - std::map base_section = get_properties()->get(SEDA_BASE_NAME); + std::map base_section = get_properties()->get(SEDA_BASE_NAME); std::map::iterator it; - std::string key; + std::string key; // check whether event histories are enabled bool ev_hist = false; - key = EVENT_HISTORY; - it = base_section.find(key); + key = EVENT_HISTORY; + it = base_section.find(key); if (it != base_section.end()) { if (it->second.compare("true") == 0) { ev_hist = true; @@ -199,8 +196,8 @@ void SedaConfig::init_event_history() // set max event hops uint32_t max_event_hops = 100; - key = MAX_EVENT_HISTORY_NUM; - it = base_section.find(key); + key = MAX_EVENT_HISTORY_NUM; + it = base_section.find(key); if (it != base_section.end()) { str_to_val(it->second, max_event_hops); } @@ -214,25 +211,25 @@ SedaConfig::status_t SedaConfig::init_thread_pool() { try { - std::map base_section = get_properties()->get(SEDA_BASE_NAME); + std::map base_section = get_properties()->get(SEDA_BASE_NAME); std::map::iterator it; - std::string key; + std::string key; // get thread pool names key = THREAD_POOLS_NAME; - it = base_section.find(key); + it = base_section.find(key); if (it == base_section.end()) { LOG_ERROR("Configuration hasn't set %s", key.c_str()); return INITFAIL; } - std::string pool_names = it->second; + std::string pool_names = it->second; std::vector name_list; - std::string split_tag; + std::string split_tag; split_tag.assign(1, Ini::CFG_DELIMIT_TAG); split_string(pool_names, split_tag, name_list); - int cpu_num = getCpuNum(); + int cpu_num = getCpuNum(); std::string default_cpu_num_str; val_to_str(cpu_num, default_cpu_num_str); @@ -240,7 +237,7 @@ SedaConfig::status_t SedaConfig::init_thread_pool() std::string &thread_name = name_list[pos]; // get count number - key = COUNT; + key = COUNT; std::string count_str = get_properties()->get(key, default_cpu_num_str, thread_name); int thread_count = 1; @@ -288,10 +285,10 @@ std::string SedaConfig::get_thread_pool(std::string &stage_name) { std::string ret = DEFAULT_THREAD_POOL; // Get thread pool - std::map stage_section = get_properties()->get(stage_name); + std::map stage_section = get_properties()->get(stage_name); std::map::iterator itt; - std::string thread_pool_id = THREAD_POOL_ID; - itt = stage_section.find(thread_pool_id); + std::string thread_pool_id = THREAD_POOL_ID; + itt = stage_section.find(thread_pool_id); if (itt == stage_section.end()) { LOG_INFO("Not set thread_pool_id for %s, use default threadpool %s", stage_name.c_str(), DEFAULT_THREAD_POOL); return ret; @@ -320,13 +317,13 @@ std::string SedaConfig::get_thread_pool(std::string &stage_name) SedaConfig::status_t SedaConfig::init_stages() { try { - std::map base_section = get_properties()->get(SEDA_BASE_NAME); + std::map base_section = get_properties()->get(SEDA_BASE_NAME); std::map::iterator it; - std::string key; + std::string key; // get stage names key = STAGES; - it = base_section.find(key); + it = base_section.find(key); if (it == base_section.end()) { LOG_ERROR("Hasn't set stages name in %s", key.c_str()); clear_config(); @@ -341,7 +338,7 @@ SedaConfig::status_t SedaConfig::init_stages() std::string stage_name(*it); std::string thread_name = get_thread_pool(stage_name); - Threadpool *t = thread_pools_[thread_name]; + Threadpool *t = thread_pools_[thread_name]; Stage *stage = StageFactory::make_instance(stage_name); if (stage == NULL) { @@ -376,12 +373,12 @@ SedaConfig::status_t SedaConfig::gen_next_stages() for (std::vector::iterator it_name = stage_names_.begin(); it_name != stage_names_.end(); it_name++) { std::string stage_name(*it_name); - Stage *stage = stages_[stage_name]; + Stage *stage = stages_[stage_name]; - std::map stage_section = get_properties()->get(stage_name); + std::map stage_section = get_properties()->get(stage_name); std::map::iterator it; - std::string next_stage_id = NEXT_STAGES; - it = stage_section.find(next_stage_id); + std::string next_stage_id = NEXT_STAGES; + it = stage_section.find(next_stage_id); if (it == stage_section.end()) { continue; } @@ -389,7 +386,7 @@ SedaConfig::status_t SedaConfig::gen_next_stages() std::string next_stage_names = it->second; std::vector next_stage_name_list; - std::string split_tag; + std::string split_tag; split_tag.assign(1, Ini::CFG_DELIMIT_TAG); split_string(next_stage_names, split_tag, next_stage_name_list); @@ -397,7 +394,7 @@ SedaConfig::status_t SedaConfig::gen_next_stages() next_it != next_stage_name_list.end(); next_it++) { std::string &next_stage_name = *next_it; - Stage *next_stage = stages_[next_stage_name]; + Stage *next_stage = stages_[next_stage_name]; stage->push_stage(next_stage); } @@ -442,7 +439,7 @@ void SedaConfig::clear_config() { // delete stages_ std::map::iterator s_iter = stages_.begin(); - std::map::iterator s_end = stages_.end(); + std::map::iterator s_end = stages_.end(); while (s_iter != s_end) { if (s_iter->second != NULL) { @@ -459,7 +456,7 @@ void SedaConfig::clear_config() // delete thread_pools_ std::map::iterator t_iter = thread_pools_.begin(); - std::map::iterator t_end = thread_pools_.end(); + std::map::iterator t_end = thread_pools_.end(); while (t_iter != t_end) { if (t_iter->second != NULL) { LOG_INFO("ThreadPool %s deleted.", t_iter->first.c_str()); @@ -472,10 +469,7 @@ void SedaConfig::clear_config() LOG_INFO("Seda thread pools released"); } -void SedaConfig::get_stage_names(std::vector &names) const -{ - names = stage_names_; -} +void SedaConfig::get_stage_names(std::vector &names) const { names = stage_names_; } void SedaConfig::get_stage_queue_status(std::vector &stats) const { diff --git a/deps/common/seda/seda_config.h b/deps/common/seda/seda_config.h index 8b7ccb36d..c0bf5e774 100644 --- a/deps/common/seda/seda_config.h +++ b/deps/common/seda/seda_config.h @@ -21,8 +21,8 @@ See the Mulan PSL v2 for more details. */ #include #include -#include "common/seda/thread_pool.h" #include "common/seda/seda_defs.h" +#include "common/seda/thread_pool.h" namespace common { @@ -42,10 +42,16 @@ namespace common { * attributes in case of duplicate attributes */ -class SedaConfig { +class SedaConfig +{ public: - typedef enum { SUCCESS = 0, INITFAIL, PARSEFAIL } status_t; + typedef enum + { + SUCCESS = 0, + INITFAIL, + PARSEFAIL + } status_t; static SedaConfig *&get_instance(); @@ -200,19 +206,13 @@ class SedaConfig { std::string cfg_str_; std::map thread_pools_; - std::map stages_; - std::vector stage_names_; + std::map stages_; + std::vector stage_names_; }; -inline std::map::iterator SedaConfig::begin() -{ - return stages_.begin(); -} +inline std::map::iterator SedaConfig::begin() { return stages_.begin(); } -inline std::map::iterator SedaConfig::end() -{ - return stages_.end(); -} +inline std::map::iterator SedaConfig::end() { return stages_.end(); } inline Stage *SedaConfig::get_stage(const char *stagename) { @@ -226,7 +226,7 @@ inline Stage *SedaConfig::get_stage(const char *stagename) // Global seda config object SedaConfig *&get_seda_config(); -bool &get_event_history_flag(); +bool &get_event_history_flag(); uint32_t &get_max_event_hops(); } // namespace common diff --git a/deps/common/seda/stage.cpp b/deps/common/seda/stage.cpp index e7228a8f2..775a0b1e0 100644 --- a/deps/common/seda/stage.cpp +++ b/deps/common/seda/stage.cpp @@ -87,13 +87,13 @@ bool Stage::connect() assert(!connected_); assert(th_pool_ != NULL); - bool success = false; + bool success = false; unsigned int backlog = 0; success = initialize(); if (success) { MUTEX_LOCK(&list_mutex_); - backlog = event_list_.size(); + backlog = event_list_.size(); event_ref_ = backlog; connected_ = true; MUTEX_UNLOCK(&list_mutex_); diff --git a/deps/common/seda/stage.h b/deps/common/seda/stage.h index e9f7a6f14..e41386c8e 100644 --- a/deps/common/seda/stage.h +++ b/deps/common/seda/stage.h @@ -85,7 +85,8 @@ class CallbackContext; * repeatedly disconnected then re-connected and continue to function * properly. */ -class Stage { +class Stage +{ // public interface operations @@ -109,10 +110,7 @@ class Stage { * Return the Threadpool object * @return reference to the Threadpool for this Stage */ - Threadpool *get_pool() - { - return th_pool_; - } + Threadpool *get_pool() { return th_pool_; } /** * Push stage to the list of the next stages @@ -198,10 +196,7 @@ class Stage { * Query whether stage is connected * @return true if stage is connected */ - bool is_connected() const - { - return connected_; - } + bool is_connected() const { return connected_; } /** * Perform Stage-specific processing for an event @@ -222,8 +217,7 @@ class Stage { * * @param[in] event Pointer to event that initiated the callback. */ - virtual void callback_event(StageEvent *event, CallbackContext *context) - {} + virtual void callback_event(StageEvent *event, CallbackContext *context) {} /** * Perform Stage-specific callback processing for a timed out event @@ -272,10 +266,7 @@ class Stage { * @pre Stage not connected * @return TRUE if and only if outputs are valid and init succeeded. */ - virtual bool initialize() - { - return true; - } + virtual bool initialize() { return true; } /** * set properties for this object @@ -283,10 +274,7 @@ class Stage { * @post initializing the class members * @return Stage instantiated object */ - virtual bool set_properties() - { - return true; - } + virtual bool set_properties() { return true; } /** * Prepare to disconnect the stage. @@ -295,20 +283,14 @@ class Stage { * from the pipeline. Most stages will not need to implement * this function. */ - virtual void disconnect_prepare() - { - return; - } + virtual void disconnect_prepare() { return; } /** * Cleanup stage after disconnection * After disconnection is completed, cleanup any resources held by the * stage and prepare for destruction or re-initialization. */ - virtual void cleanup() - { - return; - } + virtual void cleanup() { return; } // pipeline state std::list next_stage_list_; // next stage(s) in the pipeline @@ -319,12 +301,12 @@ class Stage { friend class Threadpool; private: - std::deque event_list_; // event queue - mutable pthread_mutex_t list_mutex_; // protects the event queue - pthread_cond_t disconnect_cond_; // wait here for disconnect - bool connected_; // is stage connected to pool? - unsigned long event_ref_; // # of outstanding events - Threadpool *th_pool_ = nullptr; // Threadpool for this stage + std::deque event_list_; // event queue + mutable pthread_mutex_t list_mutex_; // protects the event queue + pthread_cond_t disconnect_cond_; // wait here for disconnect + bool connected_; // is stage connected to pool? + unsigned long event_ref_; // # of outstanding events + Threadpool *th_pool_ = nullptr; // Threadpool for this stage }; inline void Stage::set_pool(Threadpool *th) @@ -341,10 +323,7 @@ inline void Stage::push_stage(Stage *st) next_stage_list_.push_back(st); } -inline const char *Stage::get_name() -{ - return stage_name_; -} +inline const char *Stage::get_name() { return stage_name_; } } // namespace common #endif // __COMMON_SEDA_STAGE_H__ diff --git a/deps/common/seda/stage_event.cpp b/deps/common/seda/stage_event.cpp index e5e7787cc..8704b7d5b 100644 --- a/deps/common/seda/stage_event.cpp +++ b/deps/common/seda/stage_event.cpp @@ -26,8 +26,7 @@ See the Mulan PSL v2 for more details. */ namespace common { // Constructor -StageEvent::StageEvent() : comp_cb_(NULL), ud_(NULL), cb_flag_(false), history_(NULL), stage_hops_(0), tm_info_(NULL) -{} +StageEvent::StageEvent() : comp_cb_(NULL), ud_(NULL), cb_flag_(false), history_(NULL), stage_hops_(0), tm_info_(NULL) {} // Destructor StageEvent::~StageEvent() @@ -35,7 +34,7 @@ StageEvent::~StageEvent() // clear all pending callbacks while (comp_cb_) { CompletionCallback *top = comp_cb_; - comp_cb_ = comp_cb_->pop_callback(); + comp_cb_ = comp_cb_->pop_callback(); delete top; } @@ -109,10 +108,7 @@ void StageEvent::set_user_data(UserData *u) return; } -UserData *StageEvent::get_user_data() -{ - return ud_; -} +UserData *StageEvent::get_user_data() { return ud_; } // Add stage to list of stages which have handled this event void StageEvent::save_stage(Stage *stg, HistType type) @@ -133,10 +129,7 @@ void StageEvent::set_timeout_info(time_t deadline) set_timeout_info(tmi); } -void StageEvent::set_timeout_info(const StageEvent &ev) -{ - set_timeout_info(ev.tm_info_); -} +void StageEvent::set_timeout_info(const StageEvent &ev) { set_timeout_info(ev.tm_info_); } void StageEvent::set_timeout_info(TimeoutInfo *tmi) { diff --git a/deps/common/seda/stage_event.h b/deps/common/seda/stage_event.h index a207264f5..8fd1b4de7 100644 --- a/deps/common/seda/stage_event.h +++ b/deps/common/seda/stage_event.h @@ -14,10 +14,10 @@ See the Mulan PSL v2 for more details. */ #pragma once // Include Files -#include #include #include #include +#include #include "common/defs.h" namespace common { @@ -55,11 +55,17 @@ class TimeoutInfo; * */ -class StageEvent { +class StageEvent +{ public: // Interface for collecting debugging information - typedef enum { HANDLE_EV = 0, CALLBACK_EV, TIMEOUT_EV } HistType; + typedef enum + { + HANDLE_EV = 0, + CALLBACK_EV, + TIMEOUT_EV + } HistType; /** * Constructor @@ -105,10 +111,7 @@ class StageEvent { UserData *get_user_data(); // True if event represents a callback - bool is_callback() - { - return cb_flag_; - } + bool is_callback() { return cb_flag_; } // Add stage to list of stages which have handled this event void save_stage(Stage *stg, HistType type); @@ -129,24 +132,18 @@ class StageEvent { typedef std::pair HistEntry; // Interface to allow callbacks to be run on target stage's threads - void mark_callback() - { - cb_flag_ = true; - } - void clear_callback() - { - cb_flag_ = false; - } + void mark_callback() { cb_flag_ = true; } + void clear_callback() { cb_flag_ = false; } // Set a timeout info into the event void set_timeout_info(TimeoutInfo *tmi); - CompletionCallback *comp_cb_; // completion callback stack for this event - 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 - uint32_t stage_hops_; // Number of stages which have handled ev - TimeoutInfo *tm_info_; // the timeout info for this event + CompletionCallback *comp_cb_; // completion callback stack for this event + 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 + uint32_t stage_hops_; // Number of stages which have handled ev + TimeoutInfo *tm_info_; // the timeout info for this event }; /** @@ -161,18 +158,16 @@ class StageEvent { * processing. When the called stage invokes the \c CompletionCallback, the * originating stage can access the \c UserData member to recover its state. */ -class UserData { +class UserData +{ public: /** * \brief A virtual destructor to enable the use of dynamic casts. */ - virtual ~UserData() - { - return; - } + virtual ~UserData() { return; } }; -bool &get_event_history_flag(); +bool &get_event_history_flag(); 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 f6a48a53b..c29d5ce3c 100644 --- a/deps/common/seda/thread_pool.cpp +++ b/deps/common/seda/thread_pool.cpp @@ -90,8 +90,8 @@ unsigned int Threadpool::num_threads() */ unsigned int Threadpool::add_threads(unsigned int threads) { - unsigned int i; - pthread_t pthread; + unsigned int i; + pthread_t pthread; pthread_attr_t pthread_attrs; LOG_TRACE("%s adding threads enter%d", name_.c_str(), threads); // create all threads as detached. We will not try to join them. @@ -236,10 +236,7 @@ void Threadpool::schedule(Stage *stage) } // Get name of thread pool -const std::string &Threadpool::get_name() -{ - return name_; -} +const std::string &Threadpool::get_name() { return name_; } /** * Internal thread control function @@ -256,7 +253,7 @@ void *Threadpool::run_thread(void *pool_ptr) // this is not portable, but is easier to map to LWP int64_t threadid = gettid(); LOG_INFO("threadid = %llx, threadname = %s", threadid, pool->get_name().c_str()); -#ifdef __APPLE__ +#ifdef __APPLE__ pthread_setname_np(pool->get_name().c_str()); #else pthread_setname_np(pthread_self(), pool->get_name().c_str()); @@ -327,19 +324,10 @@ void Threadpool::create_pool_key() pthread_key_create(&pool_ptr_key_, NULL); } -void Threadpool::del_pool_key() -{ - pthread_key_delete(pool_ptr_key_); -} +void Threadpool::del_pool_key() { pthread_key_delete(pool_ptr_key_); } -void Threadpool::set_thread_pool_ptr(const Threadpool *thd_Pool) -{ - pthread_setspecific(pool_ptr_key_, thd_Pool); -} +void Threadpool::set_thread_pool_ptr(const Threadpool *thd_Pool) { pthread_setspecific(pool_ptr_key_, thd_Pool); } -const Threadpool *Threadpool::get_thread_pool_ptr() -{ - return (const Threadpool *)pthread_getspecific(pool_ptr_key_); -} +const Threadpool *Threadpool::get_thread_pool_ptr() { return (const Threadpool *)pthread_getspecific(pool_ptr_key_); } } // namespace common diff --git a/deps/common/seda/thread_pool.h b/deps/common/seda/thread_pool.h index acbb1a3a1..671e05108 100644 --- a/deps/common/seda/thread_pool.h +++ b/deps/common/seda/thread_pool.h @@ -40,7 +40,8 @@ class Stage; * of worker threads, but this number can be adjusted at any time by using * the add_threads(), num_threads(), and kill_threads() interfaces. */ -class Threadpool { +class Threadpool +{ public: // Initialize the static data structures of ThreadPool @@ -144,19 +145,19 @@ class Threadpool { static const Threadpool *get_thread_pool_ptr(); // run queue state - pthread_mutex_t run_mutex_; //< protects the run queue - pthread_cond_t run_cond_; //< wait here for stage to be scheduled + pthread_mutex_t run_mutex_; //< protects the run queue + pthread_cond_t run_cond_; //< wait here for stage to be scheduled std::deque run_queue_; //< list of stages with work to do - bool eventhist_; //< is event history enabled? + bool eventhist_; //< is event history enabled? // thread state - pthread_mutex_t thread_mutex_; //< protects thread state - pthread_cond_t thread_cond_; //< wait here when killing threads - unsigned int nthreads_; //< number of service threads - unsigned int threads_to_kill_; //< number of pending kill events - unsigned int n_idles_; //< number of idle threads - KillThreadStage killer_; //< used to kill threads - std::string name_; //< name of threadpool + pthread_mutex_t thread_mutex_; //< protects thread state + pthread_cond_t thread_cond_; //< wait here when killing threads + unsigned int nthreads_; //< number of service threads + unsigned int threads_to_kill_; //< number of pending kill events + unsigned int n_idles_; //< number of idle threads + KillThreadStage killer_; //< used to kill threads + std::string name_; //< name of threadpool // key of thread specific to store thread pool pointer static pthread_key_t pool_ptr_key_; diff --git a/deps/common/seda/timer_stage.cpp b/deps/common/seda/timer_stage.cpp index d7808eaaa..19f3f587c 100644 --- a/deps/common/seda/timer_stage.cpp +++ b/deps/common/seda/timer_stage.cpp @@ -32,7 +32,7 @@ namespace common { struct timeval sub_timeval(const struct timeval *t1, const struct timeval *t2) { struct timeval result; - result.tv_sec = t1->tv_sec - t2->tv_sec; + result.tv_sec = t1->tv_sec - t2->tv_sec; result.tv_usec = t1->tv_usec - t2->tv_usec; while (result.tv_usec < 0) { --result.tv_sec; @@ -44,7 +44,7 @@ struct timeval sub_timeval(const struct timeval *t1, const struct timeval *t2) struct timeval add_timeval(const struct timeval *t1, const struct timeval *t2) { struct timeval result; - result.tv_sec = t1->tv_sec + t2->tv_sec; + result.tv_sec = t1->tv_sec + t2->tv_sec; result.tv_usec = t1->tv_usec + t2->tv_usec; while (result.tv_usec >= USEC_PER_SEC) { ++result.tv_sec; @@ -65,18 +65,18 @@ void realtime_to_monotonic(const struct timeval *time_RT, struct timeval *time_M clock_gettime(CLOCK_MONOTONIC, &time_ts); struct timeval time_temp; - time_temp.tv_sec = time_ts.tv_sec; + time_temp.tv_sec = time_ts.tv_sec; time_temp.tv_usec = time_ts.tv_nsec / NSEC_PER_USEC; - time_temp = add_timeval(&time_temp, &time_offset); + time_temp = add_timeval(&time_temp, &time_offset); - time_Mono->tv_sec = time_temp.tv_sec; + time_Mono->tv_sec = time_temp.tv_sec; time_Mono->tv_usec = time_temp.tv_usec; } uint64_t TimerToken::next_nonce() { - static uint64_t nonce_cntr = 0; - static pthread_mutex_t tt_mutex = PTHREAD_MUTEX_INITIALIZER; + static uint64_t nonce_cntr = 0; + static pthread_mutex_t tt_mutex = PTHREAD_MUTEX_INITIALIZER; pthread_mutex_lock(&tt_mutex); uint64_t n = nonce_cntr++; @@ -114,15 +114,9 @@ void TimerToken::set(const struct timeval &t, uint64_t n) return; } -const struct timeval &TimerToken::get_time() const -{ - return time; -} +const struct timeval &TimerToken::get_time() const { return time; } -uint64_t TimerToken::get_nonce() const -{ - return nonce; -} +uint64_t TimerToken::get_nonce() const { return nonce; } bool TimerToken::operator<(const TimerToken &other) const { @@ -141,18 +135,19 @@ TimerToken &TimerToken::operator=(const TimerToken &src) std::string TimerToken::to_string() const { - std::string s; + std::string s; std::ostringstream ss(s); ss << time.tv_sec << ":" << time.tv_usec << "-" << nonce; return ss.str(); } -TimerRegisterEvent::TimerRegisterEvent(StageEvent *cb, uint64_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); - timer_when_.tv_sec = timer_spec.tv_sec; + timer_when_.tv_sec = timer_spec.tv_sec; timer_when_.tv_usec = (timer_spec.tv_nsec / NSEC_PER_USEC); timer_when_.tv_usec += time_relative_usec; @@ -171,25 +166,16 @@ TimerRegisterEvent::TimerRegisterEvent(StageEvent *cb, struct timeval &time_abso return; } -TimerRegisterEvent::~TimerRegisterEvent() -{ - return; -} +TimerRegisterEvent::~TimerRegisterEvent() { return; } -const struct timeval &TimerRegisterEvent::get_time() -{ - return timer_when_; -} +const struct timeval &TimerRegisterEvent::get_time() { return timer_when_; } -StageEvent *TimerRegisterEvent::get_callback_event() -{ - return timer_cb_; -} +StageEvent *TimerRegisterEvent::get_callback_event() { return timer_cb_; } StageEvent *TimerRegisterEvent::adopt_callback_event() { StageEvent *e = timer_cb_; - timer_cb_ = NULL; + timer_cb_ = NULL; return e; } @@ -201,7 +187,7 @@ void TimerRegisterEvent::set_cancel_token(const TimerToken &t) std::unique_ptr TimerRegisterEvent::get_cancel_token() { - const TimerToken *token_cp = new TimerToken(token_); + const TimerToken *token_cp = new TimerToken(token_); std::unique_ptr token_ptr(token_cp); return token_ptr; } @@ -212,15 +198,9 @@ TimerCancelEvent::TimerCancelEvent(const TimerToken &cancel_token) return; } -TimerCancelEvent::~TimerCancelEvent() -{ - return; -} +TimerCancelEvent::~TimerCancelEvent() { return; } -const TimerToken &TimerCancelEvent::get_token() -{ - return token_; -} +const TimerToken &TimerCancelEvent::get_token() { return token_; } void TimerCancelEvent::set_success(bool s) { @@ -228,10 +208,7 @@ void TimerCancelEvent::set_success(bool s) return; } -bool TimerCancelEvent::get_success() -{ - return cancelled_; -} +bool TimerCancelEvent::get_success() { return cancelled_; } TimerStage::TimerStage(const char *tag) : Stage(tag), @@ -291,7 +268,7 @@ bool TimerStage::initialize() // Start the thread to maintain the timer const pthread_attr_t *thread_attrs = NULL; - void *thread_args = (void *)this; + void *thread_args = (void *)this; int status = pthread_create(&timer_thread_id_, thread_attrs, &TimerStage::start_timer_thread, thread_args); if (status != 0) LOG_ERROR("failed to create timer thread: status=%d\n", status); @@ -305,10 +282,7 @@ bool TimerStage::initialize() return (status == 0); } -uint32_t TimerStage::get_num_events() -{ - return num_events_; -} +uint32_t TimerStage::get_num_events() { return num_events_; } void TimerStage::disconnect_prepare() { @@ -320,7 +294,7 @@ void TimerStage::disconnect_prepare() LOG_TRACE("waiting for timer maintenance thread to terminate.\n"); void **return_val_ptr = NULL; - int status; + int status; status = pthread_join(timer_thread_id_, return_val_ptr); LOG_TRACE("timer maintenance thread terminated: status=%d\n", status); @@ -350,10 +324,7 @@ void TimerStage::handle_event(StageEvent *event) return; } -void TimerStage::callback_event(StageEvent *e, CallbackContext *ctx) -{ - return; -} +void TimerStage::callback_event(StageEvent *e, CallbackContext *ctx) { return; } void TimerStage::register_timer(TimerRegisterEvent ®_ev) { @@ -365,8 +336,8 @@ void TimerStage::register_timer(TimerRegisterEvent ®_ev) pthread_mutex_lock(&timer_mutex_); // add the event to the timer queue - StageEvent *timer_cb = reg_ev.adopt_callback_event(); - std::pair result = timer_queue_.insert(std::make_pair(tt, timer_cb)); + StageEvent *timer_cb = reg_ev.adopt_callback_event(); + std::pair result = timer_queue_.insert(std::make_pair(tt, timer_cb)); ASSERT(result.second, "Internal error--" "failed to register timer because token is not unique."); @@ -390,8 +361,8 @@ void TimerStage::register_timer(TimerRegisterEvent ®_ev) void TimerStage::cancel_timer(TimerCancelEvent &cancel_ev) { pthread_mutex_lock(&timer_mutex_); - bool success = false; - timer_queue_t::iterator it = timer_queue_.find(cancel_ev.get_token()); + bool success = false; + timer_queue_t::iterator it = timer_queue_.find(cancel_ev.get_token()); if (it != timer_queue_.end()) { success = true; @@ -439,7 +410,7 @@ void TimerStage::check_timer() clock_gettime(CLOCK_MONOTONIC, &ts_now); struct timeval now; - now.tv_sec = ts_now.tv_sec; + now.tv_sec = ts_now.tv_sec; now.tv_usec = ts_now.tv_nsec / NSEC_PER_USEC; // LOG_TRACE("checking timer: sec=%ld, usec=%ld", now.tv_sec, now.tv_usec); @@ -494,7 +465,7 @@ void TimerStage::check_timer() // If timer events are registered, sleep until the first // event should be triggered. struct timespec ts; - ts.tv_sec = first->first.get_time().tv_sec; + ts.tv_sec = first->first.get_time().tv_sec; ts.tv_nsec = first->first.get_time().tv_usec * NSEC_PER_USEC; // LOG_TRACE("sleeping until next deadline: sec=%ld, nsec=%ld\n", ts.tv_sec, ts.tv_nsec); @@ -507,9 +478,6 @@ void TimerStage::check_timer() return; } -bool TimerStage::timer_token_less_than(const TimerToken &tt1, const TimerToken &tt2) -{ - return (tt1 < tt2); -} +bool TimerStage::timer_token_less_than(const TimerToken &tt1, const TimerToken &tt2) { return (tt1 < tt2); } } // namespace common diff --git a/deps/common/seda/timer_stage.h b/deps/common/seda/timer_stage.h index 5f2d4455a..250acd35f 100644 --- a/deps/common/seda/timer_stage.h +++ b/deps/common/seda/timer_stage.h @@ -14,9 +14,9 @@ See the Mulan PSL v2 for more details. */ #pragma once +#include #include #include -#include #include "common/log/log.h" #include "common/seda/callback.h" @@ -39,25 +39,26 @@ namespace common { * It should be considered opaque to all components other than the \c * TimerStage. */ -class TimerToken { +class TimerToken +{ public: TimerToken(); TimerToken(const struct timeval &t); TimerToken(const TimerToken &tt); const struct timeval &get_time() const; - uint64_t get_nonce() const; - bool operator<(const TimerToken &other) const; - TimerToken &operator=(const TimerToken &src); - std::string to_string() const; + uint64_t get_nonce() const; + bool operator<(const TimerToken &other) const; + TimerToken &operator=(const TimerToken &src); + std::string to_string() const; friend bool timer_token_less_than(const TimerToken &tt1, const TimerToken &tt2); private: - void set(const struct timeval &t, uint64_t n); + void set(const struct timeval &t, uint64_t n); static uint64_t next_nonce(); struct timeval time; - uint64_t nonce; + uint64_t nonce; }; /** @@ -66,16 +67,11 @@ class TimerToken { * * \brief An abstract base class for all timer-related events. */ -class TimerEvent : public StageEvent { +class TimerEvent : public StageEvent +{ public: - TimerEvent() : StageEvent() - { - return; - } - virtual ~TimerEvent() - { - return; - } + TimerEvent() : StageEvent() { return; } + virtual ~TimerEvent() { return; } }; /** @@ -92,7 +88,8 @@ class TimerEvent : public StageEvent { * number of available threads, the event may be triggered later than * the requested time. */ -class TimerRegisterEvent : public TimerEvent { +class TimerRegisterEvent : public TimerEvent +{ public: /** * \brief Create an event to request the registration of a timer @@ -183,9 +180,9 @@ class TimerRegisterEvent : public TimerEvent { void set_cancel_token(const TimerToken &t); private: - StageEvent *timer_cb_; + StageEvent *timer_cb_; struct timeval timer_when_; - TimerToken token_; + TimerToken token_; }; /** @@ -200,7 +197,8 @@ class TimerRegisterEvent : public TimerEvent { * reports that the event is cancelled, it will not invoke trigger * the associated callback event. */ -class TimerCancelEvent : public TimerEvent { +class TimerCancelEvent : public TimerEvent +{ public: /** * \brief Create an event to request the cancellation of a timer @@ -244,7 +242,7 @@ class TimerCancelEvent : public TimerEvent { private: TimerToken token_; - bool cancelled_; + bool cancelled_; }; /** @@ -277,7 +275,8 @@ class TimerCancelEvent : public TimerEvent { * Implementation note: The \c TimerStage creates an internal thread * to maintain the timer. */ -class TimerStage : public Stage { +class TimerStage : public Stage +{ public: ~TimerStage(); static Stage *make_stage(const std::string &tag); @@ -309,15 +308,14 @@ class TimerStage : public Stage { static void *start_timer_thread(void *arg); typedef std::map timer_queue_t; - timer_queue_t timer_queue_; + timer_queue_t timer_queue_; pthread_mutex_t timer_mutex_; - pthread_cond_t timer_condv_; + pthread_cond_t timer_condv_; - bool shutdown_; // true if stage has received the shutdown signal - uint32_t num_events_; // the number of timer events currently outstanding + bool shutdown_; // true if stage has received the shutdown signal + uint32_t num_events_; // the number of timer events currently outstanding pthread_t timer_thread_id_; // thread id of the timer maintenance thread }; } // namespace common - diff --git a/deps/common/time/datetime.cpp b/deps/common/time/datetime.cpp index fcff9ab77..05f4af645 100644 --- a/deps/common/time/datetime.cpp +++ b/deps/common/time/datetime.cpp @@ -62,10 +62,10 @@ std::string DateTime::time_t_to_str(int timet) std::string DateTime::time_t_to_xml_str(time_t timet) { - std::string ret_val; + std::string ret_val; std::ostringstream oss; - struct tm tmbuf; - tm *tm_info = gmtime_r(&timet, &tmbuf); + struct tm tmbuf; + tm *tm_info = gmtime_r(&timet, &tmbuf); oss << tm_info->tm_year + 1900 << "-"; if ((tm_info->tm_mon + 1) <= 9) oss << "0"; @@ -88,7 +88,7 @@ std::string DateTime::time_t_to_xml_str(time_t timet) std::string DateTime::str_to_time_t_str(std::string &xml_str) { - tm tmp; + tm tmp; std::ostringstream oss; sscanf(xml_str.c_str(), "%04d-%02d-%02dT%02d:%02d:%02dZ", @@ -98,8 +98,8 @@ std::string DateTime::str_to_time_t_str(std::string &xml_str) &tmp.tm_hour, &tmp.tm_min, &tmp.tm_sec); - m_date = julian_date(tmp.tm_year, tmp.tm_mon, tmp.tm_mday); - m_time = make_hms(tmp.tm_hour, tmp.tm_min, tmp.tm_sec, 0); + m_date = julian_date(tmp.tm_year, tmp.tm_mon, tmp.tm_mday); + m_time = make_hms(tmp.tm_hour, tmp.tm_min, tmp.tm_sec, 0); time_t timestamp = to_time_t(); oss << std::dec << std::setw(10) << timestamp; return oss.str(); @@ -124,8 +124,8 @@ DateTime DateTime::now() std::string DateTime::to_xml_date_time() { - std::string ret_val; - tm tm_info; + std::string ret_val; + tm tm_info; std::ostringstream oss; tm_info = to_tm(); @@ -174,26 +174,26 @@ void DateTime::add_duration_date_time(std::string xml_duration) int e_year, e_month, e_day, e_hour, e_min, e_sec, e_millis = 0; // months - tmp_month = s_month + dur_t.tm_mon; - e_month = ((tmp_month - 1) % 12) + 1; + tmp_month = s_month + dur_t.tm_mon; + e_month = ((tmp_month - 1) % 12) + 1; int carry_month = ((tmp_month - 1) / 12); // years e_year = s_year + dur_t.tm_year + carry_month; // seconds - tmp_sec = s_sec + dur_t.tm_sec; - e_sec = tmp_sec % 60; + tmp_sec = s_sec + dur_t.tm_sec; + e_sec = tmp_sec % 60; int carry_sec = tmp_sec / 60; // minutes - tmp_min = s_min + dur_t.tm_min + carry_sec; - e_min = tmp_min % 60; + tmp_min = s_min + dur_t.tm_min + carry_sec; + e_min = tmp_min % 60; int carry_min = tmp_min / 60; // hours - tmp_hour = s_hour + dur_t.tm_hour + carry_min; - e_hour = tmp_hour % 24; + tmp_hour = s_hour + dur_t.tm_hour + carry_min; + e_hour = tmp_hour % 24; int carry_hr = tmp_hour / 24; // days @@ -206,11 +206,11 @@ void DateTime::add_duration_date_time(std::string xml_duration) tmp_day = s_day; } } - e_day = tmp_day + dur_t.tm_mday + carry_hr; + e_day = tmp_day + dur_t.tm_mday + carry_hr; int carry_day = 0; while (true) { if (e_day < 1) { - e_day = e_day + max_day_in_month_for(e_year, e_month - 1); + e_day = e_day + max_day_in_month_for(e_year, e_month - 1); carry_day = -1; } else { if (e_day > max_day_in_month_for(e_year, e_month)) { @@ -221,8 +221,8 @@ void DateTime::add_duration_date_time(std::string xml_duration) } } tmp_month = e_month + carry_day; - e_month = ((tmp_month - 1) % 12) + 1; - e_year = e_year + (tmp_month - 1) / 12; + e_month = ((tmp_month - 1) % 12) + 1; + e_year = e_year + (tmp_month - 1) / 12; } m_date = julian_date(e_year, e_month, e_day); m_time = make_hms(e_hour, e_min, e_sec, e_millis); @@ -232,7 +232,7 @@ void DateTime::add_duration_date_time(std::string xml_duration) int DateTime::max_day_in_month_for(int yr, int month) { int tmp_month = ((month - 1) % 12) + 1; - int tmp_year = yr + ((tmp_month - 1) / 12); + int tmp_year = yr + ((tmp_month - 1) / 12); if (tmp_month == MON_JAN || tmp_month == MON_MAR || tmp_month == MON_MAY || tmp_month == MON_JUL || tmp_month == MON_AUG || tmp_month == MON_OCT || tmp_month == MON_DEC) { @@ -285,7 +285,7 @@ void DateTime::parse_duration(std::string dur_str, struct tm &tm_t) } int ind_d = dur_str.find('D', index); - int sign = 1; + int sign = 1; if (ind_d != -1) { if (dur_str[index] == '-') { sign = -1; @@ -344,8 +344,8 @@ void DateTime::parse_duration(std::string dur_str, struct tm &tm_t) #define OBJ_ID_TIMESTMP_DIGITS 14 std::string Now::unique() { - struct timeval tv; - uint64_t temp; + struct timeval tv; + uint64_t temp; static uint64_t last_unique = 0; #if defined(LINUX) static pthread_mutex_t mutex = PTHREAD_ERRORCHECK_MUTEX_INITIALIZER_NP; @@ -394,7 +394,7 @@ bool DateTime::is_valid_xml_datetime(const std::string &str) } // check month, date, hour, min, second is valid - tm tmp; + tm tmp; int ret = sscanf(str.c_str(), "%04d-%02d-%02dT%02d:%02d:%02dZ", &tmp.tm_year, diff --git a/deps/common/time/datetime.h b/deps/common/time/datetime.h index 7e779be1f..2be7476ae 100644 --- a/deps/common/time/datetime.h +++ b/deps/common/time/datetime.h @@ -45,25 +45,28 @@ namespace common { * \sa http://scienceworld.wolfram.com/astronomy/Weekday.html */ -struct DateTime { +struct DateTime +{ int m_date; int m_time; - enum { - SECONDS_PER_DAY = 86400, + enum + { + SECONDS_PER_DAY = 86400, SECONDS_PER_HOUR = 3600, - SECONDS_PER_MIN = 60, + SECONDS_PER_MIN = 60, MINUTES_PER_HOUR = 60, - MILLIS_PER_DAY = 86400000, + MILLIS_PER_DAY = 86400000, MILLIS_PER_HOUR = 3600000, - MILLIS_PER_MIN = 60000, - MILLIS_PER_SEC = 1000, + MILLIS_PER_MIN = 60000, + MILLIS_PER_SEC = 1000, // time_t epoch (1970-01-01) as a Julian date JULIAN_19700101 = 2440588 }; - enum { + enum + { MON_JAN = 1, MON_FEB = 2, MON_MAR = 3, @@ -79,12 +82,10 @@ struct DateTime { }; // Default constructor - initializes to zero - DateTime() : m_date(0), m_time(0) - {} + DateTime() : m_date(0), m_time(0) {} // Construct from a Julian day number and time in millis - DateTime(int date, int time) : m_date(date), m_time(time) - {} + DateTime(int date, int time) : m_date(date), m_time(time) {} // Construct from the specified components DateTime(int year, int month, int day, int hour, int minute, int second, int millis) @@ -101,73 +102,55 @@ struct DateTime { // Load the referenced values with the year, month and day // portions of the date in a single operation - inline void get_ymd(int &year, int &month, int &day) const - { - get_ymd(m_date, year, month, day); - } + inline void get_ymd(int &year, int &month, int &day) const { get_ymd(m_date, year, month, day); } // Load the referenced values with the hour, minute, second and // millisecond portions of the time in a single operation inline void get_hms(int &hour, int &minute, int &second, int &millis) const { int ticks = m_time / MILLIS_PER_SEC; - hour = ticks / SECONDS_PER_HOUR; - minute = (ticks / SECONDS_PER_MIN) % MINUTES_PER_HOUR; - second = ticks % SECONDS_PER_MIN; - millis = m_time % MILLIS_PER_SEC; + hour = ticks / SECONDS_PER_HOUR; + minute = (ticks / SECONDS_PER_MIN) % MINUTES_PER_HOUR; + second = ticks % SECONDS_PER_MIN; + millis = m_time % MILLIS_PER_SEC; } // Convert the DateTime to a time_t. Note that this operation // can overflow on 32-bit platforms when we go beyond year 2038. - inline time_t to_time_t() const - { - return (SECONDS_PER_DAY * (m_date - JULIAN_19700101) + m_time / MILLIS_PER_SEC); - } + inline time_t to_time_t() const { return (SECONDS_PER_DAY * (m_date - JULIAN_19700101) + m_time / MILLIS_PER_SEC); } // Convert the DateTime to a struct tm which is in UTC tm to_tm() const { int year, month, day; int hour, minute, second, millis; - tm result = {0}; + tm result = {0}; get_ymd(year, month, day); get_hms(hour, minute, second, millis); - result.tm_year = year - 1900; - result.tm_mon = month - 1; - result.tm_mday = day; - result.tm_hour = hour; - result.tm_min = minute; - result.tm_sec = second; + result.tm_year = year - 1900; + result.tm_mon = month - 1; + result.tm_mday = day; + result.tm_hour = hour; + result.tm_min = minute; + result.tm_sec = second; result.tm_isdst = -1; return result; } // Set the date portion of the DateTime - void set_ymd(int year, int month, int day) - { - m_date = julian_date(year, month, day); - } + void set_ymd(int year, int month, int day) { m_date = julian_date(year, month, day); } // Set the time portion of the DateTime - void set_hms(int hour, int minute, int second, int millis) - { - m_time = make_hms(hour, minute, second, millis); - } + void set_hms(int hour, int minute, int second, int millis) { m_time = make_hms(hour, minute, second, millis); } // Clear the date portion of the DateTime - void clear_date() - { - m_date = 0; - } + void clear_date() { m_date = 0; } // Clear the time portion of the DateTime - void clear_time() - { - m_time = 0; - } + void clear_time() { m_time = 0; } // Set the internal date and time members void set(int date, int time) @@ -233,7 +216,7 @@ struct DateTime { static DateTime from_time_t(time_t t, int millis = 0) { struct tm tmbuf; - tm *tm = gmtime_r(&t, &tmbuf); + tm *tm = gmtime_r(&t, &tmbuf); return from_tm(*tm, millis); } @@ -263,9 +246,9 @@ struct DateTime { int d = (4 * c + 3) / 1461; int e = c - int((1461 * d) / 4); int m = (5 * e + 2) / 153; - day = e - int((153 * m + 2) / 5) + 1; + day = e - int((153 * m + 2) / 5) + 1; month = m + 3 - 12 * int(m / 10); - year = b * 100 + d - 4800 + int(m / 10); + year = b * 100 + d - 4800 + int(m / 10); } // Return a human-friendly string representation of the timestamp, @@ -274,7 +257,7 @@ struct DateTime { { const time_t tt = to_time_t(); // 'man asctime' specifies that buffer must be at least 26 bytes - char buffer[32]; + char buffer[32]; struct tm tm; asctime_r(localtime_r(&tt, &tm), &(buffer[0])); std::string s(buffer); @@ -287,7 +270,7 @@ struct DateTime { { const time_t tt = to_time_t(); // 'man asctime' specifies that buffer must be at least 26 bytes - char buffer[32]; + char buffer[32]; struct tm tm; asctime_r(gmtime_r(&tt, &tm), &(buffer[0])); std::string s(buffer); @@ -312,10 +295,7 @@ inline bool operator==(const DateTime &lhs, const DateTime &rhs) return lhs.m_date == rhs.m_date && lhs.m_time == rhs.m_time; } -inline bool operator!=(const DateTime &lhs, const DateTime &rhs) -{ - return !(lhs == rhs); -} +inline bool operator!=(const DateTime &lhs, const DateTime &rhs) { return !(lhs == rhs); } inline bool operator<(const DateTime &lhs, const DateTime &rhs) { @@ -328,20 +308,11 @@ inline bool operator<(const DateTime &lhs, const DateTime &rhs) return false; } -inline bool operator>(const DateTime &lhs, const DateTime &rhs) -{ - return !(lhs == rhs || lhs < rhs); -} +inline bool operator>(const DateTime &lhs, const DateTime &rhs) { return !(lhs == rhs || lhs < rhs); } -inline bool operator<=(const DateTime &lhs, const DateTime &rhs) -{ - return lhs == rhs || lhs < rhs; -} +inline bool operator<=(const DateTime &lhs, const DateTime &rhs) { return lhs == rhs || lhs < rhs; } -inline bool operator>=(const DateTime &lhs, const DateTime &rhs) -{ - return lhs == rhs || lhs > rhs; -} +inline bool operator>=(const DateTime &lhs, const DateTime &rhs) { return lhs == rhs || lhs > rhs; } // Calculate the difference between two DateTime values and return // the result as a number of seconds @@ -353,11 +324,11 @@ inline int operator-(const DateTime &lhs, const DateTime &rhs) } // Date and Time represented in UTC. -class TimeStamp : public DateTime { +class TimeStamp : public DateTime +{ public: // Defaults to the current date and time - TimeStamp() : DateTime(DateTime::now()) - {} + TimeStamp() : DateTime(DateTime::now()) {} // Defaults to the current date TimeStamp(int hour, int minute, int second, int millisecond = 0) : DateTime(DateTime::now()) @@ -373,89 +344,61 @@ class TimeStamp : public DateTime { : DateTime(year, month, date, hour, minute, second, millisecond) {} - TimeStamp(time_t time, int millisecond = 0) : DateTime(from_time_t(time, millisecond)) - {} + TimeStamp(time_t time, int millisecond = 0) : DateTime(from_time_t(time, millisecond)) {} - TimeStamp(const tm *time, int millisecond = 0) : DateTime(from_tm(*time, millisecond)) - {} + TimeStamp(const tm *time, int millisecond = 0) : DateTime(from_tm(*time, millisecond)) {} - void set_current() - { - set(DateTime::now()); - } + void set_current() { set(DateTime::now()); } }; // Time only represented in UTC. -class Time : public DateTime { +class Time : public DateTime +{ public: // Defaults to the current time - Time() - { - set_current(); - } + Time() { set_current(); } - Time(const DateTime &val) : DateTime(val) - { - clear_date(); - } + Time(const DateTime &val) : DateTime(val) { clear_date(); } - Time(int hour, int minute, int second, int millisecond = 0) - { - set_hms(hour, minute, second, millisecond); - } + Time(int hour, int minute, int second, int millisecond = 0) { set_hms(hour, minute, second, millisecond); } - Time(time_t time, int millisecond = 0) : DateTime(from_time_t(time, millisecond)) - { - clear_date(); - } + Time(time_t time, int millisecond = 0) : DateTime(from_time_t(time, millisecond)) { clear_date(); } - Time(const tm *time, int millisecond = 0) : DateTime(from_tm(*time, millisecond)) - { - clear_date(); - } + Time(const tm *time, int millisecond = 0) : DateTime(from_tm(*time, millisecond)) { clear_date(); } // Set to the current time. void set_current() { DateTime d = now(); - m_time = d.m_time; + m_time = d.m_time; } }; // Date only represented in UTC. -class Date : public DateTime { +class Date : public DateTime +{ public: // Defaults to the current date - Date() - { - set_current(); - } + Date() { set_current(); } - Date(const DateTime &val) : DateTime(val) - { - clear_time(); - } + Date(const DateTime &val) : DateTime(val) { clear_time(); } - Date(int date, int month, int year) : DateTime(year, month, date, 0, 0, 0, 0) - {} + Date(int date, int month, int year) : DateTime(year, month, date, 0, 0, 0, 0) {} - Date(long sec) : DateTime(sec / DateTime::SECONDS_PER_DAY, 0) - {} + Date(long sec) : DateTime(sec / DateTime::SECONDS_PER_DAY, 0) {} - Date(const tm *time) : DateTime(from_tm(*time)) - { - clear_time(); - } + Date(const tm *time) : DateTime(from_tm(*time)) { clear_time(); } // Set to the current time. void set_current() { DateTime d = now(); - m_date = d.m_date; + m_date = d.m_date; } }; -class Now { +class Now +{ public: static inline int64_t sec() { diff --git a/deps/common/time/timeout_info.h b/deps/common/time/timeout_info.h index 5fa1461d6..fdce9b527 100644 --- a/deps/common/time/timeout_info.h +++ b/deps/common/time/timeout_info.h @@ -27,7 +27,8 @@ namespace common { * are used here to simplify the implementation. */ -class TimeoutInfo { +class TimeoutInfo +{ public: /** * Constructor @@ -68,8 +69,8 @@ class TimeoutInfo { bool is_timed_out_; // timeout flag - int ref_cnt_; // reference count of this object - pthread_mutex_t mutex_; // mutex_ to protect ref_cnt_ and flag + int ref_cnt_; // reference count of this object + pthread_mutex_t mutex_; // mutex_ to protect ref_cnt_ and flag }; } // namespace common diff --git a/src/obclient/client.cpp b/src/obclient/client.cpp index 8ecefc216..de1ee9f45 100644 --- a/src/obclient/client.cpp +++ b/src/obclient/client.cpp @@ -23,16 +23,16 @@ See the Mulan PSL v2 for more details. */ #include #include #include -#include #include #include +#include #include "common/defs.h" #include "common/lang/string.h" #ifdef USE_READLINE -#include "readline/readline.h" #include "readline/history.h" +#include "readline/readline.h" #endif #define MAX_MEM_BUFFER_SIZE 8192 @@ -41,10 +41,10 @@ See the Mulan PSL v2 for more details. */ using namespace common; #ifdef USE_READLINE -const std::string HISTORY_FILE = std::string(getenv("HOME")) + "/.miniob.history"; -time_t last_history_write_time = 0; +const std::string HISTORY_FILE = std::string(getenv("HOME")) + "/.miniob.history"; +time_t last_history_write_time = 0; -char *my_readline(const char *prompt) +char *my_readline(const char *prompt) { int size = history_length; if (size == 0) { @@ -67,7 +67,7 @@ char *my_readline(const char *prompt) } return line; } -#else // USE_READLINE +#else // USE_READLINE char *my_readline(const char *prompt) { char *buffer = (char *)malloc(MAX_MEM_BUFFER_SIZE); @@ -84,16 +84,15 @@ char *my_readline(const char *prompt) } return buffer; } -#endif // USE_READLINE +#endif // USE_READLINE /* this function config a exit-cmd list, strncasecmp func truncate the command from terminal according to the number, - 'strncasecmp("exit", cmd, 4)' means that obclient read command string from terminal, truncate it to 4 chars from + 'strncasecmp("exit", cmd, 4)' means that obclient read command string from terminal, truncate it to 4 chars from the beginning, then compare the result with 'exit', if they match, exit the obclient. */ -bool is_exit_command(const char *cmd) { - return 0 == strncasecmp("exit", cmd, 4) || - 0 == strncasecmp("bye", cmd, 3) || - 0 == strncasecmp("\\q", cmd, 2) ; +bool is_exit_command(const char *cmd) +{ + return 0 == strncasecmp("exit", cmd, 4) || 0 == strncasecmp("bye", cmd, 3) || 0 == strncasecmp("\\q", cmd, 2); } int init_unix_sock(const char *unix_sock_path) @@ -119,7 +118,7 @@ int init_unix_sock(const char *unix_sock_path) int init_tcp_sock(const char *server_host, int server_port) { - struct hostent *host; + struct hostent *host; struct sockaddr_in serv_addr; if ((host = gethostbyname(server_host)) == NULL) { @@ -134,8 +133,8 @@ int init_tcp_sock(const char *server_host, int server_port) } serv_addr.sin_family = AF_INET; - serv_addr.sin_port = htons(server_port); - serv_addr.sin_addr = *((struct in_addr *)host->h_addr); + serv_addr.sin_port = htons(server_port); + serv_addr.sin_addr = *((struct in_addr *)host->h_addr); bzero(&(serv_addr.sin_zero), 8); if (connect(sockfd, (struct sockaddr *)&serv_addr, sizeof(struct sockaddr)) == -1) { @@ -148,22 +147,16 @@ int init_tcp_sock(const char *server_host, int server_port) int main(int argc, char *argv[]) { - const char *unix_socket_path = nullptr; - const char *server_host = "127.0.0.1"; - int server_port = PORT_DEFAULT; - int opt; + const char *unix_socket_path = nullptr; + const char *server_host = "127.0.0.1"; + int server_port = PORT_DEFAULT; + int opt; extern char *optarg; while ((opt = getopt(argc, argv, "s:h:p:")) > 0) { switch (opt) { - case 's': - unix_socket_path = optarg; - break; - case 'p': - server_port = atoi(optarg); - break; - case 'h': - server_host = optarg; - break; + case 's': unix_socket_path = optarg; break; + case 'p': server_port = atoi(optarg); break; + case 'h': server_host = optarg; break; } } @@ -194,7 +187,7 @@ int main(int argc, char *argv[]) break; } - if ((send_bytes = write(sockfd, input_command, strlen(input_command) + 1)) == -1) { // TODO writen + if ((send_bytes = write(sockfd, input_command, strlen(input_command) + 1)) == -1) { // TODO writen fprintf(stderr, "send error: %d:%s \n", errno, strerror(errno)); exit(1); } diff --git a/src/observer/session/session.cpp b/src/observer/session/session.cpp index ed75c1d48..cf8613124 100644 --- a/src/observer/session/session.cpp +++ b/src/observer/session/session.cpp @@ -13,10 +13,10 @@ See the Mulan PSL v2 for more details. */ // #include "session/session.h" -#include "storage/trx/trx.h" +#include "common/global_context.h" #include "storage/db/db.h" #include "storage/default/default_handler.h" -#include "common/global_context.h" +#include "storage/trx/trx.h" Session &Session::default_session() { @@ -24,8 +24,7 @@ Session &Session::default_session() return session; } -Session::Session(const Session &other) : db_(other.db_) -{} +Session::Session(const Session &other) : db_(other.db_) {} Session::~Session() { @@ -43,15 +42,12 @@ const char *Session::get_current_db_name() const return ""; } -Db *Session::get_current_db() const -{ - return db_; -} +Db *Session::get_current_db() const { return db_; } void Session::set_current_db(const std::string &dbname) { DefaultHandler &handler = DefaultHandler::get_default(); - Db *db = handler.find_db(dbname.c_str()); + Db *db = handler.find_db(dbname.c_str()); if (db == nullptr) { LOG_WARN("no such database: %s", dbname.c_str()); return; @@ -66,10 +62,7 @@ void Session::set_trx_multi_operation_mode(bool multi_operation_mode) trx_multi_operation_mode_ = multi_operation_mode; } -bool Session::is_trx_multi_operation_mode() const -{ - return trx_multi_operation_mode_; -} +bool Session::is_trx_multi_operation_mode() const { return trx_multi_operation_mode_; } Trx *Session::current_trx() { @@ -81,22 +74,10 @@ Trx *Session::current_trx() thread_local Session *thread_session = nullptr; -void Session::set_current_session(Session *session) -{ - thread_session = session; -} +void Session::set_current_session(Session *session) { thread_session = session; } -Session *Session::current_session() -{ - return thread_session; -} +Session *Session::current_session() { return thread_session; } -void Session::set_current_request(SessionEvent *request) -{ - current_request_ = request; -} +void Session::set_current_request(SessionEvent *request) { current_request_ = request; } -SessionEvent *Session::current_request() const -{ - return current_request_; -} +SessionEvent *Session::current_request() const { return current_request_; } diff --git a/src/observer/session/session.h b/src/observer/session/session.h index 0b5bf9d02..a50050234 100644 --- a/src/observer/session/session.h +++ b/src/observer/session/session.h @@ -24,7 +24,7 @@ class SessionEvent; * @brief 表示会话 * @details 当前一个连接一个会话,没有做特殊的会话管理,这也简化了会话处理 */ -class Session +class Session { public: /** @@ -41,11 +41,11 @@ class Session void operator=(Session &) = delete; const char *get_current_db_name() const; - Db *get_current_db() const; + Db *get_current_db() const; /** * @brief 设置当前会话关联的数据库 - * + * * @param dbname 数据库名字 */ void set_current_db(const std::string &dbname); @@ -62,7 +62,7 @@ class Session /** * @brief 当前会话关联的事务 - * + * */ Trx *current_trx(); @@ -81,7 +81,7 @@ class Session /** * @brief 将指定会话设置到线程变量中 - * + * */ static void set_current_session(Session *session); @@ -90,11 +90,13 @@ class Session * @details 当前某个请求开始时,会将会话设置到线程变量中,在整个请求处理过程中不会改变 */ static Session *current_session(); - + private: - Db *db_ = nullptr; - Trx *trx_ = nullptr; - SessionEvent *current_request_ = nullptr; ///< 当前正在处理的请求 - bool trx_multi_operation_mode_ = false; ///< 当前事务的模式,是否多语句模式. 单语句模式自动提交 - bool sql_debug_ = false; ///< 是否输出SQL调试信息 + Db *db_ = nullptr; + Trx *trx_ = nullptr; + SessionEvent *current_request_ = nullptr; ///< 当前正在处理的请求 + + bool trx_multi_operation_mode_ = false; ///< 当前事务的模式,是否多语句模式. 单语句模式自动提交 + + bool sql_debug_ = false; ///< 是否输出SQL调试信息 }; diff --git a/src/observer/session/session_stage.cpp b/src/observer/session/session_stage.cpp index 49a7ece17..147f7b24d 100644 --- a/src/observer/session/session_stage.cpp +++ b/src/observer/session/session_stage.cpp @@ -18,25 +18,22 @@ See the Mulan PSL v2 for more details. */ #include #include "common/conf/ini.h" -#include "common/log/log.h" #include "common/lang/mutex.h" #include "common/lang/string.h" -#include "common/seda/callback.h" +#include "common/log/log.h" #include "event/session_event.h" #include "event/sql_event.h" -#include "net/server.h" #include "net/communicator.h" +#include "net/server.h" #include "session/session.h" using namespace common; // Constructor -SessionStage::SessionStage(const char *tag) : Stage(tag) -{} +SessionStage::SessionStage(const char *tag) : Stage(tag) {} // Destructor -SessionStage::~SessionStage() -{} +SessionStage::~SessionStage() {} // Parse properties, instantiate a stage object Stage *SessionStage::make_stage(const std::string &tag) @@ -65,16 +62,10 @@ bool SessionStage::set_properties() } // Initialize stage params and validate outputs -bool SessionStage::initialize() -{ - return true; -} +bool SessionStage::initialize() { return true; } // Cleanup after disconnection -void SessionStage::cleanup() -{ - -} +void SessionStage::cleanup() {} void SessionStage::handle_event(StageEvent *event) { @@ -103,9 +94,9 @@ void SessionStage::handle_request(StageEvent *event) SQLStageEvent sql_event(sev, sql); (void)handle_sql(&sql_event); - Communicator *communicator = sev->get_communicator(); - bool need_disconnect = false; - RC rc = communicator->write_result(sev, need_disconnect); + Communicator *communicator = sev->get_communicator(); + bool need_disconnect = false; + RC rc = communicator->write_result(sev, need_disconnect); LOG_INFO("write result return %s", strrc(rc)); if (need_disconnect) { Server::close_connection(communicator); @@ -143,13 +134,13 @@ RC SessionStage::handle_sql(SQLStageEvent *sql_event) LOG_TRACE("failed to do resolve. rc=%s", strrc(rc)); return rc; } - + rc = optimize_stage_.handle_request(sql_event); if (rc != RC::UNIMPLENMENT && rc != RC::SUCCESS) { LOG_TRACE("failed to do optimize. rc=%s", strrc(rc)); return rc; } - + rc = execute_stage_.handle_request(sql_event); if (OB_FAIL(rc)) { LOG_TRACE("failed to do execute. rc=%s", strrc(rc)); diff --git a/src/observer/session/session_stage.h b/src/observer/session/session_stage.h index 7d361ecd7..0c696eac7 100644 --- a/src/observer/session/session_stage.h +++ b/src/observer/session/session_stage.h @@ -15,11 +15,11 @@ See the Mulan PSL v2 for more details. */ #pragma once #include "common/seda/stage.h" -#include "sql/query_cache/query_cache_stage.h" +#include "sql/executor/execute_stage.h" +#include "sql/optimizer/optimize_stage.h" #include "sql/parser/parse_stage.h" #include "sql/parser/resolve_stage.h" -#include "sql/optimizer/optimize_stage.h" -#include "sql/executor/execute_stage.h" +#include "sql/query_cache/query_cache_stage.h" /** * @brief SEDA处理的stage @@ -37,7 +37,7 @@ See the Mulan PSL v2 for more details. */ * @brief SQL处理的session阶段,也是第一个阶段 * @ingroup SQLStage */ -class SessionStage : public common::Stage +class SessionStage : public common::Stage { public: virtual ~SessionStage(); diff --git a/src/observer/session/thread_data.cpp b/src/observer/session/thread_data.cpp index c15231629..96a3d963b 100644 --- a/src/observer/session/thread_data.cpp +++ b/src/observer/session/thread_data.cpp @@ -15,9 +15,6 @@ See the Mulan PSL v2 for more details. */ #include "session/thread_data.h" #include "session/session.h" -thread_local ThreadData * ThreadData::thread_data_; +thread_local ThreadData *ThreadData::thread_data_; -Trx * ThreadData::trx() const -{ - return (session_ == nullptr) ? nullptr : session_->current_trx(); -} \ No newline at end of file +Trx *ThreadData::trx() const { return (session_ == nullptr) ? nullptr : session_->current_trx(); } \ No newline at end of file diff --git a/src/observer/session/thread_data.h b/src/observer/session/thread_data.h index e98d554fd..698c4b1a5 100644 --- a/src/observer/session/thread_data.h +++ b/src/observer/session/thread_data.h @@ -21,19 +21,19 @@ class ThreadData { public: static ThreadData *current() { return thread_data_; } - static void setup(ThreadData *thread) { thread_data_ = thread; } + static void setup(ThreadData *thread) { thread_data_ = thread; } public: - ThreadData() = default; + ThreadData() = default; ~ThreadData() = default; Session *session() const { return session_; } - Trx * trx() const; + Trx *trx() const; void set_session(Session *session) { session_ = session; } private: - static thread_local ThreadData * thread_data_; + static thread_local ThreadData *thread_data_; private: Session *session_ = nullptr; diff --git a/src/observer/sql/executor/command_executor.cpp b/src/observer/sql/executor/command_executor.cpp index 0d4ca7cff..b819777e5 100644 --- a/src/observer/sql/executor/command_executor.cpp +++ b/src/observer/sql/executor/command_executor.cpp @@ -13,18 +13,18 @@ See the Mulan PSL v2 for more details. */ // #include "sql/executor/command_executor.h" +#include "common/log/log.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/load_data_executor.h" +#include "sql/executor/set_variable_executor.h" #include "sql/executor/show_tables_executor.h" #include "sql/executor/trx_begin_executor.h" #include "sql/executor/trx_end_executor.h" -#include "sql/executor/set_variable_executor.h" -#include "sql/executor/load_data_executor.h" -#include "common/log/log.h" +#include "sql/stmt/stmt.h" RC CommandExecutor::execute(SQLStageEvent *sql_event) { diff --git a/src/observer/sql/executor/command_executor.h b/src/observer/sql/executor/command_executor.h index c8b49a7f2..8bb7ef58c 100644 --- a/src/observer/sql/executor/command_executor.h +++ b/src/observer/sql/executor/command_executor.h @@ -30,7 +30,7 @@ class SQLStageEvent; class CommandExecutor { public: - CommandExecutor() = default; + CommandExecutor() = default; virtual ~CommandExecutor() = default; 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 35797702a..6c99dda0b 100644 --- a/src/observer/sql/executor/create_index_executor.cpp +++ b/src/observer/sql/executor/create_index_executor.cpp @@ -13,23 +13,24 @@ 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 "common/log/log.h" #include "event/session_event.h" +#include "event/sql_event.h" #include "session/session.h" -#include "common/log/log.h" +#include "sql/stmt/create_index_stmt.h" #include "storage/table/table.h" RC CreateIndexExecutor::execute(SQLStageEvent *sql_event) { - Stmt *stmt = sql_event->stmt(); + 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())); + ASSERT(stmt->type() == StmtType::CREATE_INDEX, + "create index executor can not run this command: %d", + static_cast(stmt->type())); CreateIndexStmt *create_index_stmt = static_cast(stmt); - - Trx *trx = session->current_trx(); + + Trx *trx = session->current_trx(); Table *table = create_index_stmt->table(); return table->create_index(trx, create_index_stmt->field_meta(), create_index_stmt->index_name().c_str()); } \ No newline at end of file diff --git a/src/observer/sql/executor/create_index_executor.h b/src/observer/sql/executor/create_index_executor.h index 94db59bc6..dcc658f51 100644 --- a/src/observer/sql/executor/create_index_executor.h +++ b/src/observer/sql/executor/create_index_executor.h @@ -26,7 +26,7 @@ class SQLStageEvent; class CreateIndexExecutor { public: - CreateIndexExecutor() = default; + CreateIndexExecutor() = default; virtual ~CreateIndexExecutor() = default; RC execute(SQLStageEvent *sql_event); diff --git a/src/observer/sql/executor/create_table_executor.cpp b/src/observer/sql/executor/create_table_executor.cpp index 8922afe56..200c2274e 100644 --- a/src/observer/sql/executor/create_table_executor.cpp +++ b/src/observer/sql/executor/create_table_executor.cpp @@ -14,20 +14,20 @@ See the Mulan PSL v2 for more details. */ #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 "event/sql_event.h" +#include "session/session.h" +#include "sql/stmt/create_table_stmt.h" #include "storage/db/db.h" RC CreateTableExecutor::execute(SQLStageEvent *sql_event) { - Stmt *stmt = sql_event->stmt(); + 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())); + 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); diff --git a/src/observer/sql/executor/create_table_executor.h b/src/observer/sql/executor/create_table_executor.h index f1c46e952..c4ab9a733 100644 --- a/src/observer/sql/executor/create_table_executor.h +++ b/src/observer/sql/executor/create_table_executor.h @@ -25,7 +25,7 @@ class SQLStageEvent; class CreateTableExecutor { public: - CreateTableExecutor() = default; + CreateTableExecutor() = default; virtual ~CreateTableExecutor() = default; RC execute(SQLStageEvent *sql_event); diff --git a/src/observer/sql/executor/desc_table_executor.cpp b/src/observer/sql/executor/desc_table_executor.cpp index 2382a9cac..3c3ba0ccb 100644 --- a/src/observer/sql/executor/desc_table_executor.cpp +++ b/src/observer/sql/executor/desc_table_executor.cpp @@ -16,36 +16,34 @@ See the Mulan PSL v2 for more details. */ #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 "event/session_event.h" +#include "event/sql_event.h" +#include "session/session.h" +#include "sql/operator/string_list_physical_operator.h" #include "sql/stmt/desc_table_stmt.h" #include "storage/db/db.h" -#include "sql/operator/string_list_physical_operator.h" +#include "storage/table/table.h" using namespace std; RC DescTableExecutor::execute(SQLStageEvent *sql_event) { - RC rc = RC::SUCCESS; - Stmt *stmt = sql_event->stmt(); + RC rc = RC::SUCCESS; + 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())); + 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(); - SqlResult *sql_result = session_event->sql_result(); - - const char *table_name = desc_table_stmt->table_name().c_str(); - - Db *db = session->get_current_db(); + 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")); @@ -53,7 +51,7 @@ RC DescTableExecutor::execute(SQLStageEvent *sql_event) sql_result->set_tuple_schema(tuple_schema); - auto oper = new StringListPhysicalOperator; + 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); diff --git a/src/observer/sql/executor/desc_table_executor.h b/src/observer/sql/executor/desc_table_executor.h index 7e1f02a29..9d4d6544b 100644 --- a/src/observer/sql/executor/desc_table_executor.h +++ b/src/observer/sql/executor/desc_table_executor.h @@ -25,7 +25,7 @@ class SQLStageEvent; class DescTableExecutor { public: - DescTableExecutor() = default; + DescTableExecutor() = default; virtual ~DescTableExecutor() = default; RC execute(SQLStageEvent *sql_event); diff --git a/src/observer/sql/executor/execute_stage.cpp b/src/observer/sql/executor/execute_stage.cpp index 5ee996479..28b417f40 100644 --- a/src/observer/sql/executor/execute_stage.cpp +++ b/src/observer/sql/executor/execute_stage.cpp @@ -12,21 +12,19 @@ See the Mulan PSL v2 for more details. */ // Created by Longda on 2021/4/13. // -#include #include +#include #include "sql/executor/execute_stage.h" #include "common/log/log.h" -#include "session/session.h" -#include "event/storage_event.h" -#include "event/sql_event.h" #include "event/session_event.h" -#include "sql/stmt/stmt.h" -#include "sql/stmt/select_stmt.h" -#include "storage/default/default_handler.h" +#include "event/sql_event.h" #include "sql/executor/command_executor.h" #include "sql/operator/calc_physical_operator.h" +#include "sql/stmt/select_stmt.h" +#include "sql/stmt/stmt.h" +#include "storage/default/default_handler.h" using namespace std; using namespace common; @@ -34,6 +32,7 @@ using namespace common; RC ExecuteStage::handle_request(SQLStageEvent *sql_event) { RC rc = RC::SUCCESS; + const unique_ptr &physical_operator = sql_event->physical_operator(); if (physical_operator != nullptr) { return handle_request_with_physical_operator(sql_event); @@ -66,8 +65,8 @@ RC ExecuteStage::handle_request_with_physical_operator(SQLStageEvent *sql_event) TupleSchema schema; switch (stmt->type()) { case StmtType::SELECT: { - SelectStmt *select_stmt = static_cast(stmt); - bool with_table_name = select_stmt->tables().size() > 1; + SelectStmt *select_stmt = static_cast(stmt); + bool with_table_name = select_stmt->tables().size() > 1; for (const Field &field : select_stmt->query_fields()) { if (with_table_name) { @@ -80,7 +79,7 @@ RC ExecuteStage::handle_request_with_physical_operator(SQLStageEvent *sql_event) case StmtType::CALC: { CalcPhysicalOperator *calc_operator = static_cast(physical_operator.get()); - for (const unique_ptr & expr : calc_operator->expressions()) { + for (const unique_ptr &expr : calc_operator->expressions()) { schema.append_cell(expr->name().c_str()); } } break; diff --git a/src/observer/sql/executor/execute_stage.h b/src/observer/sql/executor/execute_stage.h index 8b7a2b515..356752627 100644 --- a/src/observer/sql/executor/execute_stage.h +++ b/src/observer/sql/executor/execute_stage.h @@ -14,7 +14,6 @@ See the Mulan PSL v2 for more details. */ #pragma once -#include "sql/parser/parse.h" #include "common/rc.h" class SQLStageEvent; @@ -30,7 +29,6 @@ class SelectStmt; class ExecuteStage { public: - RC handle_request(SQLStageEvent *event); RC handle_request_with_physical_operator(SQLStageEvent *sql_event); }; diff --git a/src/observer/sql/executor/help_executor.h b/src/observer/sql/executor/help_executor.h index d29a5225f..57371fd99 100644 --- a/src/observer/sql/executor/help_executor.h +++ b/src/observer/sql/executor/help_executor.h @@ -15,11 +15,10 @@ See the Mulan PSL v2 for more details. */ #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 "event/sql_event.h" #include "sql/executor/sql_result.h" -#include "session/session.h" +#include "sql/operator/string_list_physical_operator.h" /** * @brief Help语句执行器 @@ -28,21 +27,19 @@ See the Mulan PSL v2 for more details. */ class HelpExecutor { public: - HelpExecutor() = default; + HelpExecutor() = default; virtual ~HelpExecutor() = default; RC execute(SQLStageEvent *sql_event) { - const char *strings[] = { - "show tables;", + 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`;" - }; + "select [ * | `columns` ] from `table`;"}; auto oper = new StringListPhysicalOperator(); for (size_t i = 0; i < sizeof(strings) / sizeof(strings[0]); i++) { diff --git a/src/observer/sql/executor/load_data_executor.cpp b/src/observer/sql/executor/load_data_executor.cpp index be97c89af..bdd1bca5a 100644 --- a/src/observer/sql/executor/load_data_executor.cpp +++ b/src/observer/sql/executor/load_data_executor.cpp @@ -13,21 +13,21 @@ See the Mulan PSL v2 for more details. */ // #include "sql/executor/load_data_executor.h" -#include "event/sql_event.h" +#include "common/lang/string.h" #include "event/session_event.h" +#include "event/sql_event.h" #include "sql/executor/sql_result.h" -#include "common/lang/string.h" #include "sql/stmt/load_data_stmt.h" using namespace common; RC LoadDataExecutor::execute(SQLStageEvent *sql_event) { - RC rc = RC::SUCCESS; - SqlResult *sql_result = sql_event->session_event()->sql_result(); - LoadDataStmt *stmt = static_cast(sql_event->stmt()); - Table *table = stmt->table(); - const char *file_name = stmt->filename(); + RC rc = RC::SUCCESS; + SqlResult *sql_result = sql_event->session_event()->sql_result(); + LoadDataStmt *stmt = static_cast(sql_event->stmt()); + Table *table = stmt->table(); + const char *file_name = stmt->filename(); load_data(table, file_name, sql_result); return rc; } @@ -40,13 +40,11 @@ RC LoadDataExecutor::execute(SQLStageEvent *sql_event) * @param errmsg 如果出现错误,通过这个参数返回错误信息 * @return 成功返回RC::SUCCESS */ -RC insert_record_from_file(Table *table, - std::vector &file_values, - std::vector &record_values, - std::stringstream &errmsg) +RC insert_record_from_file( + Table *table, std::vector &file_values, std::vector &record_values, std::stringstream &errmsg) { - const int field_num = record_values.size(); + const int field_num = record_values.size(); const int sys_field_num = table->table_meta().sys_field_num(); if (file_values.size() < record_values.size()) { @@ -132,15 +130,15 @@ void LoadDataExecutor::load_data(Table *table, const char *file_name, SqlResult struct timespec begin_time; clock_gettime(CLOCK_MONOTONIC, &begin_time); const int sys_field_num = table->table_meta().sys_field_num(); - const int field_num = table->table_meta().field_num() - sys_field_num; + const int field_num = table->table_meta().field_num() - sys_field_num; - std::vector record_values(field_num); - std::string line; + std::vector record_values(field_num); + std::string line; std::vector file_values; - const std::string delim("|"); - int line_num = 0; - int insertion_count = 0; - RC rc = RC::SUCCESS; + const std::string delim("|"); + int line_num = 0; + int insertion_count = 0; + RC rc = RC::SUCCESS; while (!fs.eof() && RC::SUCCESS == rc) { std::getline(fs, line); line_num++; diff --git a/src/observer/sql/executor/load_data_executor.h b/src/observer/sql/executor/load_data_executor.h index 98dd11dbb..dc023ba9b 100644 --- a/src/observer/sql/executor/load_data_executor.h +++ b/src/observer/sql/executor/load_data_executor.h @@ -27,11 +27,11 @@ class SqlResult; class LoadDataExecutor { public: - LoadDataExecutor() = default; + LoadDataExecutor() = default; virtual ~LoadDataExecutor() = default; RC execute(SQLStageEvent *sql_event); - + private: void load_data(Table *table, const char *file_name, SqlResult *sql_result); }; diff --git a/src/observer/sql/executor/set_variable_executor.h b/src/observer/sql/executor/set_variable_executor.h index 728bbce5b..b3fe959f9 100644 --- a/src/observer/sql/executor/set_variable_executor.h +++ b/src/observer/sql/executor/set_variable_executor.h @@ -15,10 +15,8 @@ See the Mulan PSL v2 for more details. */ #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 "event/sql_event.h" #include "session/session.h" #include "sql/stmt/set_variable_stmt.h" @@ -29,20 +27,21 @@ See the Mulan PSL v2 for more details. */ class SetVariableExecutor { public: - SetVariableExecutor() = default; + SetVariableExecutor() = default; virtual ~SetVariableExecutor() = default; RC execute(SQLStageEvent *sql_event) { RC rc = RC::SUCCESS; - Session *session = sql_event->session_event()->session(); - SetVariableStmt *stmt = (SetVariableStmt *)sql_event->stmt(); - const char *var_name = stmt->var_name(); + Session *session = sql_event->session_event()->session(); + SetVariableStmt *stmt = (SetVariableStmt *)sql_event->stmt(); + + const char *var_name = stmt->var_name(); const Value &var_value = stmt->var_value(); if (strcasecmp(var_name, "sql_debug") == 0) { bool bool_value = false; - rc = var_value_to_boolean(var_value, bool_value); + rc = var_value_to_boolean(var_value, bool_value); if (rc != RC::SUCCESS) { return rc; } @@ -69,21 +68,9 @@ class SetVariableExecutor bool_value = var_value.get_float() != 0.0; } else if (var_value.attr_type() == AttrType::CHARS) { - std::string true_strings[] = { - "true", - "on", - "yes", - "t", - "1" - }; - - std::string false_strings[] = { - "false", - "off", - "no", - "f", - "0" - }; + std::string true_strings[] = {"true", "on", "yes", "t", "1"}; + + std::string false_strings[] = {"false", "off", "no", "f", "0"}; for (size_t i = 0; i < sizeof(true_strings) / sizeof(true_strings[0]); i++) { if (strcasecmp(var_value.get_string().c_str(), true_strings[i].c_str()) == 0) { diff --git a/src/observer/sql/executor/show_tables_executor.h b/src/observer/sql/executor/show_tables_executor.h index b9a3bed0d..3561faffe 100644 --- a/src/observer/sql/executor/show_tables_executor.h +++ b/src/observer/sql/executor/show_tables_executor.h @@ -15,11 +15,11 @@ See the Mulan PSL v2 for more details. */ #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 "event/sql_event.h" #include "session/session.h" +#include "sql/executor/sql_result.h" +#include "sql/operator/string_list_physical_operator.h" #include "storage/db/db.h" /** @@ -30,12 +30,12 @@ See the Mulan PSL v2 for more details. */ class ShowTablesExecutor { public: - ShowTablesExecutor() = default; + ShowTablesExecutor() = default; virtual ~ShowTablesExecutor() = default; RC execute(SQLStageEvent *sql_event) { - SqlResult *sql_result = sql_event->session_event()->sql_result(); + SqlResult *sql_result = sql_event->session_event()->sql_result(); SessionEvent *session_event = sql_event->session_event(); Db *db = session_event->session()->get_current_db(); diff --git a/src/observer/sql/executor/sql_result.cpp b/src/observer/sql/executor/sql_result.cpp index 5e565d326..2d90ed6de 100644 --- a/src/observer/sql/executor/sql_result.cpp +++ b/src/observer/sql/executor/sql_result.cpp @@ -12,19 +12,15 @@ See the Mulan PSL v2 for more details. */ // Created by WangYunlai on 2022/11/18. // -#include "common/rc.h" #include "sql/executor/sql_result.h" +#include "common/log/log.h" +#include "common/rc.h" #include "session/session.h" #include "storage/trx/trx.h" -#include "common/log/log.h" -SqlResult::SqlResult(Session *session) : session_(session) -{} +SqlResult::SqlResult(Session *session) : session_(session) {} -void SqlResult::set_tuple_schema(const TupleSchema &schema) -{ - tuple_schema_ = schema; -} +void SqlResult::set_tuple_schema(const TupleSchema &schema) { tuple_schema_ = schema; } RC SqlResult::open() { diff --git a/src/observer/sql/executor/sql_result.h b/src/observer/sql/executor/sql_result.h index b21178b2f..68a930e47 100644 --- a/src/observer/sql/executor/sql_result.h +++ b/src/observer/sql/executor/sql_result.h @@ -14,8 +14,8 @@ See the Mulan PSL v2 for more details. */ #pragma once -#include #include +#include #include "sql/expr/tuple.h" #include "sql/operator/physical_operator.h" @@ -24,55 +24,36 @@ class Session; /** * @brief SQL执行结果 - * @details + * @details * 如果当前SQL生成了执行计划,那么在返回客户端时,调用执行计划返回结果。 * 否则返回的结果就是当前SQL的执行结果,比如DDL语句,通过return_code和state_string来描述。 * 如果出现了一些错误,也可以通过return_code和state_string来获取信息。 */ -class SqlResult +class SqlResult { public: SqlResult(Session *session); - ~SqlResult() - {} + ~SqlResult() {} void set_tuple_schema(const TupleSchema &schema); - void set_return_code(RC rc) - { - return_code_ = rc; - } - void set_state_string(const std::string &state_string) - { - state_string_ = state_string; - } + void set_return_code(RC rc) { return_code_ = rc; } + void set_state_string(const std::string &state_string) { state_string_ = state_string; } void set_operator(std::unique_ptr oper); - - bool has_operator() const - { - return operator_ != nullptr; - } - const TupleSchema &tuple_schema() const - { - return tuple_schema_; - } - RC return_code() const - { - return return_code_; - } - const std::string &state_string() const - { - return state_string_; - } + + bool has_operator() const { return operator_ != nullptr; } + const TupleSchema &tuple_schema() const { return tuple_schema_; } + RC return_code() const { return return_code_; } + const std::string &state_string() const { return state_string_; } RC open(); RC close(); RC next_tuple(Tuple *&tuple); private: - Session *session_ = nullptr; ///< 当前所属会话 - std::unique_ptr operator_; ///< 执行计划 - TupleSchema tuple_schema_; ///< 返回的表头信息。可能有也可能没有 - RC return_code_ = RC::SUCCESS; - std::string state_string_; + Session *session_ = nullptr; ///< 当前所属会话 + std::unique_ptr operator_; ///< 执行计划 + TupleSchema tuple_schema_; ///< 返回的表头信息。可能有也可能没有 + RC return_code_ = RC::SUCCESS; + std::string state_string_; }; diff --git a/src/observer/sql/executor/trx_begin_executor.h b/src/observer/sql/executor/trx_begin_executor.h index 540bfa309..a97b8148e 100644 --- a/src/observer/sql/executor/trx_begin_executor.h +++ b/src/observer/sql/executor/trx_begin_executor.h @@ -15,10 +15,8 @@ See the Mulan PSL v2 for more details. */ #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 "event/sql_event.h" #include "session/session.h" #include "storage/trx/trx.h" @@ -29,7 +27,7 @@ See the Mulan PSL v2 for more details. */ class TrxBeginExecutor { public: - TrxBeginExecutor() = default; + TrxBeginExecutor() = default; virtual ~TrxBeginExecutor() = default; RC execute(SQLStageEvent *sql_event) @@ -37,7 +35,7 @@ class TrxBeginExecutor SessionEvent *session_event = sql_event->session_event(); Session *session = session_event->session(); - Trx *trx = session->current_trx(); + Trx *trx = session->current_trx(); session->set_trx_multi_operation_mode(true); diff --git a/src/observer/sql/executor/trx_end_executor.h b/src/observer/sql/executor/trx_end_executor.h index 0a8d1ee85..f1208aea3 100644 --- a/src/observer/sql/executor/trx_end_executor.h +++ b/src/observer/sql/executor/trx_end_executor.h @@ -15,12 +15,11 @@ See the Mulan PSL v2 for more details. */ #pragma once #include "common/rc.h" -#include "event/sql_event.h" #include "event/session_event.h" -#include "sql/executor/sql_result.h" +#include "event/sql_event.h" #include "session/session.h" -#include "storage/trx/trx.h" #include "sql/stmt/stmt.h" +#include "storage/trx/trx.h" /** * @brief 事务结束的执行器,可以是提交或回滚 @@ -29,12 +28,12 @@ See the Mulan PSL v2 for more details. */ class TrxEndExecutor { public: - TrxEndExecutor() = default; + TrxEndExecutor() = default; virtual ~TrxEndExecutor() = default; RC execute(SQLStageEvent *sql_event) { - Stmt *stmt = sql_event->stmt(); + Stmt *stmt = sql_event->stmt(); SessionEvent *session_event = sql_event->session_event(); Session *session = session_event->session(); @@ -43,8 +42,7 @@ class TrxEndExecutor if (stmt->type() == StmtType::COMMIT) { return trx->commit(); - } - else { + } else { return trx->rollback(); } } diff --git a/src/observer/sql/expr/expression.cpp b/src/observer/sql/expr/expression.cpp index 808b3f584..aa13116ff 100644 --- a/src/observer/sql/expr/expression.cpp +++ b/src/observer/sql/expr/expression.cpp @@ -29,12 +29,10 @@ RC ValueExpr::get_value(const Tuple &tuple, Value &value) const } ///////////////////////////////////////////////////////////////////////////////// -CastExpr::CastExpr(unique_ptr child, AttrType cast_type) - : child_(std::move(child)), cast_type_(cast_type) +CastExpr::CastExpr(unique_ptr child, AttrType cast_type) : child_(std::move(child)), cast_type_(cast_type) {} -CastExpr::~CastExpr() -{} +CastExpr::~CastExpr() {} RC CastExpr::cast(const Value &value, Value &cast_value) const { @@ -83,14 +81,13 @@ ComparisonExpr::ComparisonExpr(CompOp comp, unique_ptr left, unique_ : comp_(comp), left_(std::move(left)), right_(std::move(right)) {} -ComparisonExpr::~ComparisonExpr() -{} +ComparisonExpr::~ComparisonExpr() {} RC ComparisonExpr::compare_value(const Value &left, const Value &right, bool &result) const { - RC rc = RC::SUCCESS; + RC rc = RC::SUCCESS; int cmp_result = left.compare(right); - result = false; + result = false; switch (comp_) { case EQUAL_TO: { result = (0 == cmp_result); @@ -122,13 +119,13 @@ RC ComparisonExpr::compare_value(const Value &left, const Value &right, bool &re RC ComparisonExpr::try_get_value(Value &cell) const { if (left_->type() == ExprType::VALUE && right_->type() == ExprType::VALUE) { - ValueExpr *left_value_expr = static_cast(left_.get()); - ValueExpr *right_value_expr = static_cast(right_.get()); - const Value &left_cell = left_value_expr->get_value(); - const Value &right_cell = right_value_expr->get_value(); + ValueExpr *left_value_expr = static_cast(left_.get()); + ValueExpr *right_value_expr = static_cast(right_.get()); + const Value &left_cell = left_value_expr->get_value(); + const Value &right_cell = right_value_expr->get_value(); bool value = false; - RC rc = compare_value(left_cell, right_cell, value); + RC rc = compare_value(left_cell, right_cell, value); if (rc != RC::SUCCESS) { LOG_WARN("failed to compare tuple cells. rc=%s", strrc(rc)); } else { @@ -157,6 +154,7 @@ RC ComparisonExpr::get_value(const Tuple &tuple, Value &value) const } bool bool_value = false; + rc = compare_value(left_value, right_value, bool_value); if (rc == RC::SUCCESS) { value.set_boolean(bool_value); @@ -211,12 +209,11 @@ AttrType ArithmeticExpr::value_type() const return left_->value_type(); } - if (left_->value_type() == AttrType::INTS && - right_->value_type() == AttrType::INTS && + if (left_->value_type() == AttrType::INTS && right_->value_type() == AttrType::INTS && arithmetic_type_ != Type::DIV) { return AttrType::INTS; } - + return AttrType::FLOATS; } @@ -254,14 +251,16 @@ RC ArithmeticExpr::calc_value(const Value &left_value, const Value &right_value, case Type::DIV: { if (target_type == AttrType::INTS) { if (right_value.get_int() == 0) { - // NOTE: 设置为整数最大值是不正确的。通常的做法是设置为NULL,但是当前的miniob没有NULL概念,所以这里设置为整数最大值。 + // NOTE: + // 设置为整数最大值是不正确的。通常的做法是设置为NULL,但是当前的miniob没有NULL概念,所以这里设置为整数最大值。 value.set_int(numeric_limits::max()); } else { value.set_int(left_value.get_int() / right_value.get_int()); } } else { if (right_value.get_float() > -EPSILON && right_value.get_float() < EPSILON) { - // NOTE: 设置为浮点数最大值是不正确的。通常的做法是设置为NULL,但是当前的miniob没有NULL概念,所以这里设置为浮点数最大值。 + // NOTE: + // 设置为浮点数最大值是不正确的。通常的做法是设置为NULL,但是当前的miniob没有NULL概念,所以这里设置为浮点数最大值。 value.set_float(numeric_limits::max()); } else { value.set_float(left_value.get_float() / right_value.get_float()); diff --git a/src/observer/sql/expr/expression.h b/src/observer/sql/expr/expression.h index 4df41beeb..ace5e0a67 100644 --- a/src/observer/sql/expr/expression.h +++ b/src/observer/sql/expr/expression.h @@ -14,13 +14,11 @@ See the Mulan PSL v2 for more details. */ #pragma once -#include #include #include -#include "storage/field/field.h" #include "sql/parser/value.h" -#include "common/log/log.h" +#include "storage/field/field.h" class Tuple; @@ -33,7 +31,7 @@ class Tuple; * @brief 表达式类型 * @ingroup Expression */ -enum class ExprType +enum class ExprType { NONE, STAR, ///< 星号,表示所有字段 @@ -56,10 +54,10 @@ enum class ExprType * 才能计算出来真实的值。但是有些表达式可能就表示某一个固定的 * 值,比如ValueExpr。 */ -class Expression +class Expression { public: - Expression() = default; + Expression() = default; virtual ~Expression() = default; /** @@ -71,10 +69,7 @@ class Expression * @brief 在没有实际运行的情况下,也就是无法获取tuple的情况下,尝试获取表达式的值 * @details 有些表达式的值是固定的,比如ValueExpr,这种情况下可以直接获取值 */ - virtual RC try_get_value(Value &value) const - { - return RC::UNIMPLENMENT; - } + virtual RC try_get_value(Value &value) const { return RC::UNIMPLENMENT; } /** * @brief 表达式的类型 @@ -92,24 +87,22 @@ class Expression * @brief 表达式的名字,比如是字段名称,或者用户在执行SQL语句时输入的内容 */ virtual std::string name() const { return name_; } - virtual void set_name(std::string name) { name_ = name; } + virtual void set_name(std::string name) { name_ = name; } private: - std::string name_; + std::string name_; }; /** * @brief 字段表达式 * @ingroup Expression */ -class FieldExpr : public Expression +class FieldExpr : public Expression { public: FieldExpr() = default; - FieldExpr(const Table *table, const FieldMeta *field) : field_(table, field) - {} - FieldExpr(const Field &field) : field_(field) - {} + FieldExpr(const Table *table, const FieldMeta *field) : field_(table, field) {} + FieldExpr(const Field &field) : field_(field) {} virtual ~FieldExpr() = default; @@ -121,7 +114,6 @@ class FieldExpr : public Expression const Field &field() const { return field_; } const char *table_name() const { return field_.table_name(); } - const char *field_name() const { return field_.field_name(); } RC get_value(const Tuple &tuple, Value &value) const override; @@ -134,24 +126,25 @@ class FieldExpr : public Expression * @brief 常量值表达式 * @ingroup Expression */ -class ValueExpr : public Expression +class ValueExpr : public Expression { public: ValueExpr() = default; - explicit ValueExpr(const Value &value) : value_(value) - {} + explicit ValueExpr(const Value &value) : value_(value) {} virtual ~ValueExpr() = default; RC get_value(const Tuple &tuple, Value &value) const override; - RC try_get_value(Value &value) const override { value = value_; return RC::SUCCESS; } + RC try_get_value(Value &value) const override + { + value = value_; + return RC::SUCCESS; + } ExprType type() const override { return ExprType::VALUE; } - AttrType value_type() const override { return value_.attr_type(); } - void get_value(Value &value) const { value = value_; } - + void get_value(Value &value) const { value = value_; } const Value &get_value() const { return value_; } private: @@ -162,17 +155,14 @@ class ValueExpr : public Expression * @brief 类型转换表达式 * @ingroup Expression */ -class CastExpr : public Expression +class CastExpr : public Expression { public: CastExpr(std::unique_ptr child, AttrType cast_type); virtual ~CastExpr(); - ExprType type() const override - { - return ExprType::CAST; - } - RC get_value(const Tuple &tuple, Value &value) const override; + ExprType type() const override { return ExprType::CAST; } + RC get_value(const Tuple &tuple, Value &value) const override; RC try_get_value(Value &value) const override; @@ -184,29 +174,26 @@ class CastExpr : public Expression RC cast(const Value &value, Value &cast_value) const; private: - std::unique_ptr child_; ///< 从这个表达式转换 - AttrType cast_type_; ///< 想要转换成这个类型 + std::unique_ptr child_; ///< 从这个表达式转换 + AttrType cast_type_; ///< 想要转换成这个类型 }; /** * @brief 比较表达式 * @ingroup Expression */ -class ComparisonExpr : public Expression +class ComparisonExpr : public Expression { public: ComparisonExpr(CompOp comp, std::unique_ptr left, std::unique_ptr right); virtual ~ComparisonExpr(); ExprType type() const override { return ExprType::COMPARISON; } - - RC get_value(const Tuple &tuple, Value &value) const override; - + RC get_value(const Tuple &tuple, Value &value) const override; AttrType value_type() const override { return BOOLEANS; } + CompOp comp() const { return comp_; } - CompOp comp() const { return comp_; } - - std::unique_ptr &left() { return left_; } + std::unique_ptr &left() { return left_; } std::unique_ptr &right() { return right_; } /** @@ -222,7 +209,7 @@ class ComparisonExpr : public Expression RC compare_value(const Value &left, const Value &right, bool &value) const; private: - CompOp comp_; + CompOp comp_; std::unique_ptr left_; std::unique_ptr right_; }; @@ -233,10 +220,11 @@ class ComparisonExpr : public Expression * 多个表达式使用同一种关系(AND或OR)来联结 * 当前miniob仅有AND操作 */ -class ConjunctionExpr : public Expression +class ConjunctionExpr : public Expression { public: - enum class Type { + enum class Type + { AND, OR, }; @@ -246,17 +234,15 @@ class ConjunctionExpr : public Expression virtual ~ConjunctionExpr() = default; ExprType type() const override { return ExprType::CONJUNCTION; } - AttrType value_type() const override { return BOOLEANS; } - - RC get_value(const Tuple &tuple, Value &value) const override; + RC get_value(const Tuple &tuple, Value &value) const override; Type conjunction_type() const { return conjunction_type_; } std::vector> &children() { return children_; } private: - Type conjunction_type_; + Type conjunction_type_; std::vector> children_; }; @@ -264,10 +250,11 @@ class ConjunctionExpr : public Expression * @brief 算术表达式 * @ingroup Expression */ -class ArithmeticExpr : public Expression +class ArithmeticExpr : public Expression { public: - enum class Type { + enum class Type + { ADD, SUB, MUL, @@ -294,9 +281,9 @@ class ArithmeticExpr : public Expression private: RC calc_value(const Value &left_value, const Value &right_value, Value &value) const; - + private: - Type arithmetic_type_; + Type arithmetic_type_; std::unique_ptr left_; std::unique_ptr right_; }; \ No newline at end of file diff --git a/src/observer/sql/expr/tuple.h b/src/observer/sql/expr/tuple.h index 150ba6472..260b80425 100644 --- a/src/observer/sql/expr/tuple.h +++ b/src/observer/sql/expr/tuple.h @@ -15,14 +15,14 @@ See the Mulan PSL v2 for more details. */ #pragma once #include -#include #include +#include #include "common/log/log.h" +#include "sql/expr/expression.h" #include "sql/expr/tuple_cell.h" #include "sql/parser/parse.h" #include "sql/parser/value.h" -#include "sql/expr/expression.h" #include "storage/record/record.h" class Table; @@ -30,7 +30,7 @@ class Table; /** * @defgroup Tuple * @brief Tuple 元组,表示一行数据,当前返回客户端时使用 - * @details + * @details * tuple是一种可以嵌套的数据结构。 * 比如select t1.a+t2.b from t1, t2; * 需要使用下面的结构表示: @@ -41,36 +41,22 @@ class Table; * / \ * Row(t1) Row(t2) * @endcode - * + * */ /** * @brief 元组的结构,包含哪些字段(这里成为Cell),每个字段的说明 * @ingroup Tuple */ -class TupleSchema +class TupleSchema { public: - void append_cell(const TupleCellSpec &cell) - { - cells_.push_back(cell); - } - void append_cell(const char *table, const char *field) - { - append_cell(TupleCellSpec(table, field)); - } - void append_cell(const char *alias) - { - append_cell(TupleCellSpec(alias)); - } - int cell_num() const - { - return static_cast(cells_.size()); - } - const TupleCellSpec &cell_at(int i) const - { - return cells_[i]; - } + void append_cell(const TupleCellSpec &cell) { cells_.push_back(cell); } + void append_cell(const char *table, const char *field) { append_cell(TupleCellSpec(table, field)); } + void append_cell(const char *alias) { append_cell(TupleCellSpec(alias)); } + int cell_num() const { return static_cast(cells_.size()); } + + const TupleCellSpec &cell_at(int i) const { return cells_[i]; } private: std::vector cells_; @@ -80,10 +66,10 @@ class TupleSchema * @brief 元组的抽象描述 * @ingroup Tuple */ -class Tuple +class Tuple { public: - Tuple() = default; + Tuple() = default; virtual ~Tuple() = default; /** @@ -94,7 +80,7 @@ class Tuple /** * @brief 获取指定位置的Cell - * + * * @param index 位置 * @param[out] cell 返回的Cell */ @@ -102,7 +88,7 @@ class Tuple /** * @brief 根据cell的描述,获取cell的值 - * + * * @param spec cell的描述 * @param[out] cell 返回的cell */ @@ -111,7 +97,7 @@ class Tuple virtual std::string to_string() const { std::string str; - const int cell_num = this->cell_num(); + const int cell_num = this->cell_num(); for (int i = 0; i < cell_num - 1; i++) { Value cell; cell_at(i, cell); @@ -133,7 +119,7 @@ class Tuple * @ingroup Tuple * @details 直接就是获取表中的一条记录 */ -class RowTuple : public Tuple +class RowTuple : public Tuple { public: RowTuple() = default; @@ -145,10 +131,7 @@ class RowTuple : public Tuple speces_.clear(); } - void set_record(Record *record) - { - this->record_ = record; - } + void set_record(Record *record) { this->record_ = record; } void set_schema(const Table *table, const std::vector *fields) { @@ -162,10 +145,7 @@ class RowTuple : public Tuple } } - int cell_num() const override - { - return speces_.size(); - } + int cell_num() const override { return speces_.size(); } RC cell_at(int index, Value &cell) const override { @@ -174,7 +154,7 @@ class RowTuple : public Tuple return RC::INVALID_ARGUMENT; } - FieldExpr *field_expr = speces_[index]; + FieldExpr *field_expr = speces_[index]; const FieldMeta *field_meta = field_expr->field().meta(); cell.set_type(field_meta->type()); cell.set_data(this->record_->data() + field_meta->offset(), field_meta->len()); @@ -191,7 +171,7 @@ class RowTuple : public Tuple for (size_t i = 0; i < speces_.size(); ++i) { const FieldExpr *field_expr = speces_[i]; - const Field &field = field_expr->field(); + const Field &field = field_expr->field(); if (0 == strcmp(field_name, field.field_name())) { return cell_at(i, cell); } @@ -211,19 +191,13 @@ class RowTuple : public Tuple } #endif - Record &record() - { - return *record_; - } + Record &record() { return *record_; } - const Record &record() const - { - return *record_; - } + const Record &record() const { return *record_; } private: - Record *record_ = nullptr; - const Table *table_ = nullptr; + Record *record_ = nullptr; + const Table *table_ = nullptr; std::vector speces_; }; @@ -234,7 +208,7 @@ class RowTuple : public Tuple * 投影也可以是很复杂的操作,比如某些字段需要做类型转换、重命名、表达式运算、函数计算等。 * 当前的实现是比较简单的,只是选择部分字段,不做任何其他操作。 */ -class ProjectTuple : public Tuple +class ProjectTuple : public Tuple { public: ProjectTuple() = default; @@ -246,19 +220,10 @@ class ProjectTuple : public Tuple speces_.clear(); } - void set_tuple(Tuple *tuple) - { - this->tuple_ = tuple; - } + void set_tuple(Tuple *tuple) { this->tuple_ = tuple; } - void add_cell_spec(TupleCellSpec *spec) - { - speces_.push_back(spec); - } - int cell_num() const override - { - return speces_.size(); - } + void add_cell_spec(TupleCellSpec *spec) { speces_.push_back(spec); } + int cell_num() const override { return speces_.size(); } RC cell_at(int index, Value &cell) const override { @@ -273,10 +238,7 @@ class ProjectTuple : public Tuple return tuple_->find_cell(*spec, cell); } - RC find_cell(const TupleCellSpec &spec, Value &cell) const override - { - return tuple_->find_cell(spec, cell); - } + RC find_cell(const TupleCellSpec &spec, Value &cell) const override { return tuple_->find_cell(spec, cell); } #if 0 RC cell_spec_at(int index, const TupleCellSpec *&spec) const override @@ -290,25 +252,17 @@ class ProjectTuple : public Tuple #endif private: std::vector speces_; - Tuple *tuple_ = nullptr; + Tuple *tuple_ = nullptr; }; -class ExpressionTuple : public Tuple +class ExpressionTuple : public Tuple { public: - ExpressionTuple(std::vector> &expressions) - : expressions_(expressions) - { - } - - virtual ~ExpressionTuple() - { - } + ExpressionTuple(std::vector> &expressions) : expressions_(expressions) {} - int cell_num() const override - { - return expressions_.size(); - } + virtual ~ExpressionTuple() {} + + int cell_num() const override { return expressions_.size(); } RC cell_at(int index, Value &cell) const override { @@ -330,7 +284,6 @@ class ExpressionTuple : public Tuple return RC::NOTFOUND; } - private: const std::vector> &expressions_; }; @@ -339,21 +292,15 @@ class ExpressionTuple : public Tuple * @brief 一些常量值组成的Tuple * @ingroup Tuple */ -class ValueListTuple : public Tuple +class ValueListTuple : public Tuple { public: - ValueListTuple() = default; + ValueListTuple() = default; virtual ~ValueListTuple() = default; - void set_cells(const std::vector &cells) - { - cells_ = cells; - } + void set_cells(const std::vector &cells) { cells_ = cells; } - virtual int cell_num() const override - { - return static_cast(cells_.size()); - } + virtual int cell_num() const override { return static_cast(cells_.size()); } virtual RC cell_at(int index, Value &cell) const override { @@ -365,10 +312,7 @@ class ValueListTuple : public Tuple return RC::SUCCESS; } - virtual RC find_cell(const TupleCellSpec &spec, Value &cell) const override - { - return RC::INTERNAL; - } + virtual RC find_cell(const TupleCellSpec &spec, Value &cell) const override { return RC::INTERNAL; } private: std::vector cells_; @@ -379,25 +323,16 @@ class ValueListTuple : public Tuple * @ingroup Tuple * @details 在join算子中使用 */ -class JoinedTuple : public Tuple +class JoinedTuple : public Tuple { public: - JoinedTuple() = default; + JoinedTuple() = default; virtual ~JoinedTuple() = default; - void set_left(Tuple *left) - { - left_ = left; - } - void set_right(Tuple *right) - { - right_ = right; - } + void set_left(Tuple *left) { left_ = left; } + void set_right(Tuple *right) { right_ = right; } - int cell_num() const override - { - return left_->cell_num() + right_->cell_num(); - } + int cell_num() const override { return left_->cell_num() + right_->cell_num(); } RC cell_at(int index, Value &value) const override { @@ -424,6 +359,6 @@ class JoinedTuple : public Tuple } private: - Tuple *left_ = nullptr; + Tuple *left_ = nullptr; Tuple *right_ = nullptr; }; diff --git a/src/observer/sql/expr/tuple_cell.cpp b/src/observer/sql/expr/tuple_cell.cpp index 4d7dc1e1f..05a46a9f5 100644 --- a/src/observer/sql/expr/tuple_cell.cpp +++ b/src/observer/sql/expr/tuple_cell.cpp @@ -12,11 +12,7 @@ See the Mulan PSL v2 for more details. */ // Created by WangYunlai on 2022/07/05. // -#include #include "sql/expr/tuple_cell.h" -#include "storage/field/field.h" -#include "common/log/log.h" -#include "common/lang/comparator.h" #include "common/lang/string.h" TupleCellSpec::TupleCellSpec(const char *table_name, const char *field_name, const char *alias) diff --git a/src/observer/sql/expr/tuple_cell.h b/src/observer/sql/expr/tuple_cell.h index 867cd7229..6143b37c2 100644 --- a/src/observer/sql/expr/tuple_cell.h +++ b/src/observer/sql/expr/tuple_cell.h @@ -14,9 +14,8 @@ See the Mulan PSL v2 for more details. */ #pragma once -#include -#include "storage/table/table.h" #include "storage/field/field_meta.h" +#include class TupleCellSpec { @@ -24,18 +23,9 @@ class TupleCellSpec TupleCellSpec(const char *table_name, const char *field_name, const char *alias = nullptr); TupleCellSpec(const char *alias); - const char *table_name() const - { - return table_name_.c_str(); - } - const char *field_name() const - { - return field_name_.c_str(); - } - const char *alias() const - { - return alias_.c_str(); - } + const char *table_name() const { return table_name_.c_str(); } + const char *field_name() const { return field_name_.c_str(); } + const char *alias() const { return alias_.c_str(); } private: std::string table_name_; diff --git a/src/observer/sql/operator/calc_logical_operator.h b/src/observer/sql/operator/calc_logical_operator.h index d2096cc6f..b993f3e96 100644 --- a/src/observer/sql/operator/calc_logical_operator.h +++ b/src/observer/sql/operator/calc_logical_operator.h @@ -14,7 +14,6 @@ See the Mulan PSL v2 for more details. */ #pragma once -#include #include #include "sql/operator/logical_operator.h" @@ -26,13 +25,8 @@ See the Mulan PSL v2 for more details. */ class CalcLogicalOperator : public LogicalOperator { public: - CalcLogicalOperator(std::vector> &&expressions) - - { - expressions_.swap(expressions); - } + CalcLogicalOperator(std::vector> &&expressions) { expressions_.swap(expressions); } virtual ~CalcLogicalOperator() = default; LogicalOperatorType type() const override { return LogicalOperatorType::CALC; } - }; diff --git a/src/observer/sql/operator/calc_physical_operator.h b/src/observer/sql/operator/calc_physical_operator.h index a44f23427..4ac282814 100644 --- a/src/observer/sql/operator/calc_physical_operator.h +++ b/src/observer/sql/operator/calc_physical_operator.h @@ -14,33 +14,24 @@ See the Mulan PSL v2 for more details. */ #pragma once -#include "sql/operator/physical_operator.h" #include "sql/expr/tuple.h" +#include "sql/operator/physical_operator.h" class CalcPhysicalOperator : public PhysicalOperator { public: CalcPhysicalOperator(std::vector> &&expressions) - : expressions_(std::move(expressions)), tuple_(expressions_) + : expressions_(std::move(expressions)), tuple_(expressions_) {} virtual ~CalcPhysicalOperator() = default; - PhysicalOperatorType type() const override - { - return PhysicalOperatorType::CALC; - } + PhysicalOperatorType type() const override { return PhysicalOperatorType::CALC; } - std::string name() const override - { - return "CALC"; - } - std::string param() const override - { - return ""; - } + std::string name() const override { return "CALC"; } + std::string param() const override { return ""; } - RC open(Trx *trx) override { return RC::SUCCESS;} + RC open(Trx *trx) override { return RC::SUCCESS; } RC next() override { RC rc = RC::SUCCESS; @@ -62,23 +53,14 @@ class CalcPhysicalOperator : public PhysicalOperator } RC close() override { return RC::SUCCESS; } - int cell_num() const - { - return tuple_.cell_num(); - } + int cell_num() const { return tuple_.cell_num(); } - Tuple *current_tuple() override - { - return &tuple_; - } + Tuple *current_tuple() override { return &tuple_; } - const std::vector> &expressions() const - { - return expressions_; - } + const std::vector> &expressions() const { return expressions_; } private: std::vector> expressions_; - ExpressionTuple tuple_; - bool emitted_ = false; + ExpressionTuple tuple_; + bool emitted_ = false; }; diff --git a/src/observer/sql/operator/delete_logical_operator.cpp b/src/observer/sql/operator/delete_logical_operator.cpp index 9c6306ab6..5fc03f34e 100644 --- a/src/observer/sql/operator/delete_logical_operator.cpp +++ b/src/observer/sql/operator/delete_logical_operator.cpp @@ -14,5 +14,4 @@ See the Mulan PSL v2 for more details. */ #include "sql/operator/delete_logical_operator.h" -DeleteLogicalOperator::DeleteLogicalOperator(Table *table) : table_(table) -{} +DeleteLogicalOperator::DeleteLogicalOperator(Table *table) : table_(table) {} diff --git a/src/observer/sql/operator/delete_logical_operator.h b/src/observer/sql/operator/delete_logical_operator.h index c9f704bce..c32ce662e 100644 --- a/src/observer/sql/operator/delete_logical_operator.h +++ b/src/observer/sql/operator/delete_logical_operator.h @@ -20,20 +20,14 @@ See the Mulan PSL v2 for more details. */ * @brief 逻辑算子,用于执行delete语句 * @ingroup LogicalOperator */ -class DeleteLogicalOperator : public LogicalOperator +class DeleteLogicalOperator : public LogicalOperator { public: DeleteLogicalOperator(Table *table); virtual ~DeleteLogicalOperator() = default; - LogicalOperatorType type() const override - { - return LogicalOperatorType::DELETE; - } - Table *table() const - { - return table_; - } + LogicalOperatorType type() const override { return LogicalOperatorType::DELETE; } + Table *table() const { return table_; } private: Table *table_ = nullptr; diff --git a/src/observer/sql/operator/delete_physical_operator.cpp b/src/observer/sql/operator/delete_physical_operator.cpp index 3ebb35db9..b8973090d 100644 --- a/src/observer/sql/operator/delete_physical_operator.cpp +++ b/src/observer/sql/operator/delete_physical_operator.cpp @@ -12,12 +12,10 @@ See the Mulan PSL v2 for more details. */ // Created by WangYunlai on 2022/6/27. // -#include "common/log/log.h" #include "sql/operator/delete_physical_operator.h" -#include "storage/record/record.h" +#include "common/log/log.h" #include "storage/table/table.h" #include "storage/trx/trx.h" -#include "sql/stmt/delete_stmt.h" RC DeletePhysicalOperator::open(Trx *trx) { @@ -26,7 +24,7 @@ RC DeletePhysicalOperator::open(Trx *trx) } std::unique_ptr &child = children_[0]; - RC rc = child->open(trx); + RC rc = child->open(trx); if (rc != RC::SUCCESS) { LOG_WARN("failed to open child operator: %s", strrc(rc)); return rc; @@ -53,8 +51,8 @@ RC DeletePhysicalOperator::next() } RowTuple *row_tuple = static_cast(tuple); - Record &record = row_tuple->record(); - rc = trx_->delete_record(table_, record); + Record &record = row_tuple->record(); + rc = trx_->delete_record(table_, record); if (rc != RC::SUCCESS) { LOG_WARN("failed to delete record: %s", strrc(rc)); return rc; diff --git a/src/observer/sql/operator/delete_physical_operator.h b/src/observer/sql/operator/delete_physical_operator.h index 5859c107d..fa120ab7d 100644 --- a/src/observer/sql/operator/delete_physical_operator.h +++ b/src/observer/sql/operator/delete_physical_operator.h @@ -26,26 +26,19 @@ class DeleteStmt; class DeletePhysicalOperator : public PhysicalOperator { public: - DeletePhysicalOperator(Table *table) : table_(table) - {} + DeletePhysicalOperator(Table *table) : table_(table) {} virtual ~DeletePhysicalOperator() = default; - PhysicalOperatorType type() const override - { - return PhysicalOperatorType::DELETE; - } + PhysicalOperatorType type() const override { return PhysicalOperatorType::DELETE; } RC open(Trx *trx) override; RC next() override; RC close() override; - Tuple *current_tuple() override - { - return nullptr; - } + Tuple *current_tuple() override { return nullptr; } private: Table *table_ = nullptr; - Trx *trx_ = nullptr; + Trx *trx_ = nullptr; }; diff --git a/src/observer/sql/operator/explain_logical_operator.h b/src/observer/sql/operator/explain_logical_operator.h index 4920e7988..d10389168 100644 --- a/src/observer/sql/operator/explain_logical_operator.h +++ b/src/observer/sql/operator/explain_logical_operator.h @@ -20,16 +20,11 @@ See the Mulan PSL v2 for more details. */ * @brief Explain逻辑算子 * @ingroup LogicalOperator */ -class ExplainLogicalOperator : public LogicalOperator +class ExplainLogicalOperator : public LogicalOperator { public: - ExplainLogicalOperator() = default; + ExplainLogicalOperator() = default; virtual ~ExplainLogicalOperator() = default; - LogicalOperatorType type() const override - { - return LogicalOperatorType::EXPLAIN; - } - -private: + LogicalOperatorType type() const override { return LogicalOperatorType::EXPLAIN; } }; diff --git a/src/observer/sql/operator/explain_physical_operator.cpp b/src/observer/sql/operator/explain_physical_operator.cpp index b63d46508..01adec7ae 100644 --- a/src/observer/sql/operator/explain_physical_operator.cpp +++ b/src/observer/sql/operator/explain_physical_operator.cpp @@ -12,9 +12,9 @@ See the Mulan PSL v2 for more details. */ // Created by WangYunlai on 2022/12/27. // -#include #include "sql/operator/explain_physical_operator.h" #include "common/log/log.h" +#include using namespace std; @@ -24,10 +24,7 @@ RC ExplainPhysicalOperator::open(Trx *) return RC::SUCCESS; } -RC ExplainPhysicalOperator::close() -{ - return RC::SUCCESS; -} +RC ExplainPhysicalOperator::close() { return RC::SUCCESS; } RC ExplainPhysicalOperator::next() { @@ -38,7 +35,7 @@ RC ExplainPhysicalOperator::next() stringstream ss; ss << "OPERATOR(NAME)\n"; - int level = 0; + int level = 0; std::vector ends; ends.push_back(true); const auto children_size = static_cast(children_.size()); @@ -52,17 +49,14 @@ RC ExplainPhysicalOperator::next() physical_plan_ = ss.str(); vector cells; - Value cell; + Value cell; cell.set_string(physical_plan_.c_str()); cells.emplace_back(cell); tuple_.set_cells(cells); return RC::SUCCESS; } -Tuple *ExplainPhysicalOperator::current_tuple() -{ - return &tuple_; -} +Tuple *ExplainPhysicalOperator::current_tuple() { return &tuple_; } /** * 递归打印某个算子 @@ -104,7 +98,7 @@ void ExplainPhysicalOperator::to_string( ends[level + 1] = false; vector> &children = oper->children(); - const auto size = static_cast(children.size()); + const auto size = static_cast(children.size()); for (auto i = 0; i < size - 1; i++) { to_string(os, children[i].get(), level + 1, false /*last_child*/, ends); } diff --git a/src/observer/sql/operator/explain_physical_operator.h b/src/observer/sql/operator/explain_physical_operator.h index 1832efebd..d0fbff427 100644 --- a/src/observer/sql/operator/explain_physical_operator.h +++ b/src/observer/sql/operator/explain_physical_operator.h @@ -23,23 +23,20 @@ See the Mulan PSL v2 for more details. */ class ExplainPhysicalOperator : public PhysicalOperator { public: - ExplainPhysicalOperator() = default; + ExplainPhysicalOperator() = default; virtual ~ExplainPhysicalOperator() = default; - PhysicalOperatorType type() const override - { - return PhysicalOperatorType::EXPLAIN; - } + PhysicalOperatorType type() const override { return PhysicalOperatorType::EXPLAIN; } - RC open(Trx *trx) override; - RC next() override; - RC close() override; + RC open(Trx *trx) override; + RC next() override; + RC close() override; Tuple *current_tuple() override; private: void to_string(std::ostream &os, PhysicalOperator *oper, int level, bool last_child, std::vector &ends); private: - std::string physical_plan_; + std::string physical_plan_; ValueListTuple tuple_; }; diff --git a/src/observer/sql/operator/index_scan_physical_operator.cpp b/src/observer/sql/operator/index_scan_physical_operator.cpp index 1dad81bb0..21d29343b 100644 --- a/src/observer/sql/operator/index_scan_physical_operator.cpp +++ b/src/observer/sql/operator/index_scan_physical_operator.cpp @@ -16,14 +16,12 @@ See the Mulan PSL v2 for more details. */ #include "storage/index/index.h" #include "storage/trx/trx.h" -IndexScanPhysicalOperator::IndexScanPhysicalOperator( - Table *table, Index *index, bool readonly, - const Value *left_value, bool left_inclusive, - const Value *right_value, bool right_inclusive) - : table_(table), - index_(index), - readonly_(readonly), - left_inclusive_(left_inclusive), +IndexScanPhysicalOperator::IndexScanPhysicalOperator(Table *table, Index *index, bool readonly, const Value *left_value, + bool left_inclusive, const Value *right_value, bool right_inclusive) + : table_(table), + index_(index), + readonly_(readonly), + left_inclusive_(left_inclusive), right_inclusive_(right_inclusive) { if (left_value) { @@ -68,7 +66,7 @@ RC IndexScanPhysicalOperator::open(Trx *trx) RC IndexScanPhysicalOperator::next() { RID rid; - RC rc = RC::SUCCESS; + RC rc = RC::SUCCESS; record_page_handler_.cleanup(); @@ -120,7 +118,7 @@ void IndexScanPhysicalOperator::set_predicates(std::vector &expr : predicates_) { rc = expr->get_value(tuple, value); diff --git a/src/observer/sql/operator/index_scan_physical_operator.h b/src/observer/sql/operator/index_scan_physical_operator.h index b60a38e0c..f647ff971 100644 --- a/src/observer/sql/operator/index_scan_physical_operator.h +++ b/src/observer/sql/operator/index_scan_physical_operator.h @@ -14,8 +14,8 @@ See the Mulan PSL v2 for more details. */ #pragma once -#include "sql/operator/physical_operator.h" #include "sql/expr/tuple.h" +#include "sql/operator/physical_operator.h" #include "storage/record/record_manager.h" /** @@ -25,16 +25,12 @@ See the Mulan PSL v2 for more details. */ class IndexScanPhysicalOperator : public PhysicalOperator { public: - IndexScanPhysicalOperator(Table *table, Index *index, bool readonly, - const Value *left_value, bool left_inclusive, + IndexScanPhysicalOperator(Table *table, Index *index, bool readonly, const Value *left_value, bool left_inclusive, const Value *right_value, bool right_inclusive); virtual ~IndexScanPhysicalOperator() = default; - PhysicalOperatorType type() const override - { - return PhysicalOperatorType::INDEX_SCAN; - } + PhysicalOperatorType type() const override { return PhysicalOperatorType::INDEX_SCAN; } std::string param() const override; @@ -51,21 +47,21 @@ class IndexScanPhysicalOperator : public PhysicalOperator RC filter(RowTuple &tuple, bool &result); private: - Trx * trx_ = nullptr; - Table *table_ = nullptr; - Index *index_ = nullptr; - bool readonly_ = false; - IndexScanner *index_scanner_ = nullptr; + Trx *trx_ = nullptr; + Table *table_ = nullptr; + Index *index_ = nullptr; + bool readonly_ = false; + IndexScanner *index_scanner_ = nullptr; RecordFileHandler *record_handler_ = nullptr; RecordPageHandler record_page_handler_; - Record current_record_; - RowTuple tuple_; + Record current_record_; + RowTuple tuple_; Value left_value_; Value right_value_; - bool left_inclusive_ = false; - bool right_inclusive_ = false; + bool left_inclusive_ = false; + bool right_inclusive_ = false; std::vector> predicates_; }; diff --git a/src/observer/sql/operator/insert_logical_operator.cpp b/src/observer/sql/operator/insert_logical_operator.cpp index 3635c0d06..09f30e44e 100644 --- a/src/observer/sql/operator/insert_logical_operator.cpp +++ b/src/observer/sql/operator/insert_logical_operator.cpp @@ -14,7 +14,5 @@ See the Mulan PSL v2 for more details. */ #include "sql/operator/insert_logical_operator.h" -InsertLogicalOperator::InsertLogicalOperator(Table *table, std::vector values) - : table_(table), values_(values) -{ -} +InsertLogicalOperator::InsertLogicalOperator(Table *table, std::vector values) : table_(table), values_(values) +{} diff --git a/src/observer/sql/operator/insert_logical_operator.h b/src/observer/sql/operator/insert_logical_operator.h index ffaf1772c..18d9178ae 100644 --- a/src/observer/sql/operator/insert_logical_operator.h +++ b/src/observer/sql/operator/insert_logical_operator.h @@ -29,16 +29,13 @@ class InsertLogicalOperator : public LogicalOperator InsertLogicalOperator(Table *table, std::vector values); virtual ~InsertLogicalOperator() = default; - LogicalOperatorType type() const override - { - return LogicalOperatorType::INSERT; - } + LogicalOperatorType type() const override { return LogicalOperatorType::INSERT; } - Table *table() const { return table_; } + Table *table() const { return table_; } const std::vector &values() const { return values_; } - std::vector &values() { return values_; } + std::vector &values() { return values_; } private: - Table *table_ = nullptr; + Table *table_ = nullptr; std::vector values_; }; \ No newline at end of file diff --git a/src/observer/sql/operator/insert_physical_operator.cpp b/src/observer/sql/operator/insert_physical_operator.cpp index 0abfc5cbf..fd1bc9085 100644 --- a/src/observer/sql/operator/insert_physical_operator.cpp +++ b/src/observer/sql/operator/insert_physical_operator.cpp @@ -26,7 +26,7 @@ InsertPhysicalOperator::InsertPhysicalOperator(Table *table, vector &&val RC InsertPhysicalOperator::open(Trx *trx) { Record record; - RC rc = table_->make_record(static_cast(values_.size()), values_.data(), record); + RC rc = table_->make_record(static_cast(values_.size()), values_.data(), record); if (rc != RC::SUCCESS) { LOG_WARN("failed to make record. rc=%s", strrc(rc)); return rc; @@ -39,12 +39,6 @@ RC InsertPhysicalOperator::open(Trx *trx) return rc; } -RC InsertPhysicalOperator::next() -{ - return RC::RECORD_EOF; -} +RC InsertPhysicalOperator::next() { return RC::RECORD_EOF; } -RC InsertPhysicalOperator::close() -{ - return RC::SUCCESS; -} +RC InsertPhysicalOperator::close() { return RC::SUCCESS; } diff --git a/src/observer/sql/operator/insert_physical_operator.h b/src/observer/sql/operator/insert_physical_operator.h index d5ce2c9f8..ac47a5385 100644 --- a/src/observer/sql/operator/insert_physical_operator.h +++ b/src/observer/sql/operator/insert_physical_operator.h @@ -14,9 +14,9 @@ See the Mulan PSL v2 for more details. */ #pragma once -#include #include "sql/operator/physical_operator.h" #include "sql/parser/parse.h" +#include class InsertStmt; @@ -31,10 +31,7 @@ class InsertPhysicalOperator : public PhysicalOperator virtual ~InsertPhysicalOperator() = default; - PhysicalOperatorType type() const override - { - return PhysicalOperatorType::INSERT; - } + PhysicalOperatorType type() const override { return PhysicalOperatorType::INSERT; } RC open(Trx *trx) override; RC next() override; @@ -43,6 +40,6 @@ class InsertPhysicalOperator : public PhysicalOperator Tuple *current_tuple() override { return nullptr; } private: - Table *table_ = nullptr; + Table *table_ = nullptr; std::vector values_; }; diff --git a/src/observer/sql/operator/join_logical_operator.h b/src/observer/sql/operator/join_logical_operator.h index e13909e64..1909fb5bd 100644 --- a/src/observer/sql/operator/join_logical_operator.h +++ b/src/observer/sql/operator/join_logical_operator.h @@ -21,16 +21,13 @@ See the Mulan PSL v2 for more details. */ * @ingroup LogicalOperator * @details 连接算子,用于连接两个表。对应的物理算子或者实现,可能有NestedLoopJoin,HashJoin等等。 */ -class JoinLogicalOperator : public LogicalOperator +class JoinLogicalOperator : public LogicalOperator { public: - JoinLogicalOperator() = default; + JoinLogicalOperator() = default; virtual ~JoinLogicalOperator() = default; - LogicalOperatorType type() const override - { - return LogicalOperatorType::JOIN; - } + LogicalOperatorType type() const override { return LogicalOperatorType::JOIN; } private: }; diff --git a/src/observer/sql/operator/join_physical_operator.cpp b/src/observer/sql/operator/join_physical_operator.cpp index a653b21eb..0de387301 100644 --- a/src/observer/sql/operator/join_physical_operator.cpp +++ b/src/observer/sql/operator/join_physical_operator.cpp @@ -14,8 +14,7 @@ See the Mulan PSL v2 for more details. */ #include "sql/operator/join_physical_operator.h" -NestedLoopJoinPhysicalOperator::NestedLoopJoinPhysicalOperator() -{} +NestedLoopJoinPhysicalOperator::NestedLoopJoinPhysicalOperator() {} RC NestedLoopJoinPhysicalOperator::open(Trx *trx) { @@ -24,13 +23,13 @@ RC NestedLoopJoinPhysicalOperator::open(Trx *trx) return RC::INTERNAL; } - RC rc = RC::SUCCESS; - left_ = children_[0].get(); - right_ = children_[1].get(); + RC rc = RC::SUCCESS; + left_ = children_[0].get(); + right_ = children_[1].get(); right_closed_ = true; - round_done_ = true; + round_done_ = true; - rc = left_->open(trx); + rc = left_->open(trx); trx_ = trx; return rc; } @@ -38,7 +37,7 @@ RC NestedLoopJoinPhysicalOperator::open(Trx *trx) RC NestedLoopJoinPhysicalOperator::next() { bool left_need_step = (left_tuple_ == nullptr); - RC rc = RC::SUCCESS; + RC rc = RC::SUCCESS; if (round_done_) { left_need_step = true; } else { @@ -83,15 +82,12 @@ RC NestedLoopJoinPhysicalOperator::close() return rc; } -Tuple *NestedLoopJoinPhysicalOperator::current_tuple() -{ - return &joined_tuple_; -} +Tuple *NestedLoopJoinPhysicalOperator::current_tuple() { return &joined_tuple_; } RC NestedLoopJoinPhysicalOperator::left_next() { RC rc = RC::SUCCESS; - rc = left_->next(); + rc = left_->next(); if (rc != RC::SUCCESS) { return rc; } @@ -107,6 +103,7 @@ RC NestedLoopJoinPhysicalOperator::right_next() if (round_done_) { if (!right_closed_) { rc = right_->close(); + right_closed_ = true; if (rc != RC::SUCCESS) { return rc; diff --git a/src/observer/sql/operator/join_physical_operator.h b/src/observer/sql/operator/join_physical_operator.h index d5adb17a4..930d5f194 100644 --- a/src/observer/sql/operator/join_physical_operator.h +++ b/src/observer/sql/operator/join_physical_operator.h @@ -14,8 +14,8 @@ See the Mulan PSL v2 for more details. */ #pragma once -#include "sql/parser/parse.h" #include "sql/operator/physical_operator.h" +#include "sql/parser/parse.h" /** * @brief 最简单的两表(称为左表、右表)join算子 @@ -28,14 +28,11 @@ class NestedLoopJoinPhysicalOperator : public PhysicalOperator NestedLoopJoinPhysicalOperator(); virtual ~NestedLoopJoinPhysicalOperator() = default; - PhysicalOperatorType type() const override - { - return PhysicalOperatorType::NESTED_LOOP_JOIN; - } + PhysicalOperatorType type() const override { return PhysicalOperatorType::NESTED_LOOP_JOIN; } - RC open(Trx *trx) override; - RC next() override; - RC close() override; + RC open(Trx *trx) override; + RC next() override; + RC close() override; Tuple *current_tuple() override; private: @@ -46,11 +43,11 @@ class NestedLoopJoinPhysicalOperator : public PhysicalOperator Trx *trx_ = nullptr; //! 左表右表的真实对象是在PhysicalOperator::children_中,这里是为了写的时候更简单 - PhysicalOperator *left_ = nullptr; - PhysicalOperator *right_ = nullptr; - Tuple *left_tuple_ = nullptr; - Tuple *right_tuple_ = nullptr; - JoinedTuple joined_tuple_; //! 当前关联的左右两个tuple - bool round_done_ = true; //! 右表遍历的一轮是否结束 - bool right_closed_ = true; //! 右表算子是否已经关闭 + PhysicalOperator *left_ = nullptr; + PhysicalOperator *right_ = nullptr; + Tuple *left_tuple_ = nullptr; + Tuple *right_tuple_ = nullptr; + JoinedTuple joined_tuple_; //! 当前关联的左右两个tuple + bool round_done_ = true; //! 右表遍历的一轮是否结束 + bool right_closed_ = true; //! 右表算子是否已经关闭 }; diff --git a/src/observer/sql/operator/logical_operator.cpp b/src/observer/sql/operator/logical_operator.cpp index 4672f02fc..b36e117e2 100644 --- a/src/observer/sql/operator/logical_operator.cpp +++ b/src/observer/sql/operator/logical_operator.cpp @@ -14,10 +14,6 @@ See the Mulan PSL v2 for more details. */ #include "sql/operator/logical_operator.h" -LogicalOperator::~LogicalOperator() -{} +LogicalOperator::~LogicalOperator() {} -void LogicalOperator::add_child(std::unique_ptr oper) -{ - children_.emplace_back(std::move(oper)); -} +void LogicalOperator::add_child(std::unique_ptr oper) { children_.emplace_back(std::move(oper)); } diff --git a/src/observer/sql/operator/logical_operator.h b/src/observer/sql/operator/logical_operator.h index f7f779f81..fb89bca02 100644 --- a/src/observer/sql/operator/logical_operator.h +++ b/src/observer/sql/operator/logical_operator.h @@ -28,25 +28,25 @@ See the Mulan PSL v2 for more details. */ /** * @brief 逻辑算子类型 - * + * */ -enum class LogicalOperatorType +enum class LogicalOperatorType { CALC, - TABLE_GET, ///< 从表中获取数据 - PREDICATE, ///< 过滤,就是谓词 - PROJECTION, ///< 投影,就是select - JOIN, ///< 连接 - INSERT, ///< 插入 - DELETE, ///< 删除,删除可能会有子查询 - EXPLAIN, ///< 查看执行计划 + TABLE_GET, ///< 从表中获取数据 + PREDICATE, ///< 过滤,就是谓词 + PROJECTION, ///< 投影,就是select + JOIN, ///< 连接 + INSERT, ///< 插入 + DELETE, ///< 删除,删除可能会有子查询 + EXPLAIN, ///< 查看执行计划 }; /** * @brief 逻辑算子描述当前执行计划要做什么 * @details 可以看OptimizeStage中相关的代码 */ -class LogicalOperator +class LogicalOperator { public: LogicalOperator() = default; @@ -54,20 +54,14 @@ class LogicalOperator virtual LogicalOperatorType type() const = 0; - void add_child(std::unique_ptr oper); - std::vector> &children() - { - return children_; - } - std::vector> &expressions() - { - return expressions_; - } + void add_child(std::unique_ptr oper); + std::vector> &children() { return children_; } + std::vector> &expressions() { return expressions_; } protected: std::vector> children_; ///< 子算子 ///< 表达式,比如select中的列,where中的谓词等等,都可以使用表达式来表示 ///< 表达式能是一个常量,也可以是一个函数,也可以是一个列,也可以是一个子查询等等 - std::vector> expressions_; + std::vector> expressions_; }; diff --git a/src/observer/sql/operator/physical_operator.cpp b/src/observer/sql/operator/physical_operator.cpp index 6f70ca9b9..d1268fcaf 100644 --- a/src/observer/sql/operator/physical_operator.cpp +++ b/src/observer/sql/operator/physical_operator.cpp @@ -17,38 +17,21 @@ See the Mulan PSL v2 for more details. */ std::string physical_operator_type_name(PhysicalOperatorType type) { switch (type) { - case PhysicalOperatorType::TABLE_SCAN: - return "TABLE_SCAN"; - case PhysicalOperatorType::INDEX_SCAN: - return "INDEX_SCAN"; - case PhysicalOperatorType::NESTED_LOOP_JOIN: - return "NESTED_LOOP_JOIN"; - case PhysicalOperatorType::EXPLAIN: - return "EXPLAIN"; - case PhysicalOperatorType::PREDICATE: - return "PREDICATE"; - case PhysicalOperatorType::INSERT: - return "INSERT"; - case PhysicalOperatorType::DELETE: - return "DELETE"; - case PhysicalOperatorType::PROJECT: - return "PROJECT"; - case PhysicalOperatorType::STRING_LIST: - return "STRING_LIST"; - default: - return "UNKNOWN"; + case PhysicalOperatorType::TABLE_SCAN: return "TABLE_SCAN"; + case PhysicalOperatorType::INDEX_SCAN: return "INDEX_SCAN"; + case PhysicalOperatorType::NESTED_LOOP_JOIN: return "NESTED_LOOP_JOIN"; + case PhysicalOperatorType::EXPLAIN: return "EXPLAIN"; + case PhysicalOperatorType::PREDICATE: return "PREDICATE"; + case PhysicalOperatorType::INSERT: return "INSERT"; + case PhysicalOperatorType::DELETE: return "DELETE"; + case PhysicalOperatorType::PROJECT: return "PROJECT"; + case PhysicalOperatorType::STRING_LIST: return "STRING_LIST"; + default: return "UNKNOWN"; } } -PhysicalOperator::~PhysicalOperator() -{} +PhysicalOperator::~PhysicalOperator() {} -std::string PhysicalOperator::name() const -{ - return physical_operator_type_name(type()); -} +std::string PhysicalOperator::name() const { return physical_operator_type_name(type()); } -std::string PhysicalOperator::param() const -{ - return ""; -} +std::string PhysicalOperator::param() const { return ""; } diff --git a/src/observer/sql/operator/physical_operator.h b/src/observer/sql/operator/physical_operator.h index 5d03c1908..39f94ecfd 100644 --- a/src/observer/sql/operator/physical_operator.h +++ b/src/observer/sql/operator/physical_operator.h @@ -14,9 +14,9 @@ See the Mulan PSL v2 for more details. */ #pragma once -#include #include #include +#include #include "common/rc.h" #include "sql/expr/tuple.h" @@ -69,20 +69,14 @@ class PhysicalOperator virtual PhysicalOperatorType type() const = 0; virtual RC open(Trx *trx) = 0; - virtual RC next() = 0; - virtual RC close() = 0; + virtual RC next() = 0; + virtual RC close() = 0; virtual Tuple *current_tuple() = 0; - void add_child(std::unique_ptr oper) - { - children_.emplace_back(std::move(oper)); - } + void add_child(std::unique_ptr oper) { children_.emplace_back(std::move(oper)); } - std::vector> &children() - { - return children_; - } + std::vector> &children() { return children_; } protected: std::vector> children_; diff --git a/src/observer/sql/operator/predicate_logical_operator.h b/src/observer/sql/operator/predicate_logical_operator.h index 1d3480a14..3b2116ce1 100644 --- a/src/observer/sql/operator/predicate_logical_operator.h +++ b/src/observer/sql/operator/predicate_logical_operator.h @@ -14,21 +14,18 @@ See the Mulan PSL v2 for more details. */ #pragma once -#include "sql/operator/logical_operator.h" #include "sql/expr/expression.h" +#include "sql/operator/logical_operator.h" /** * @brief 谓词/过滤逻辑算子 * @ingroup LogicalOperator */ -class PredicateLogicalOperator : public LogicalOperator +class PredicateLogicalOperator : public LogicalOperator { public: PredicateLogicalOperator(std::unique_ptr expression); virtual ~PredicateLogicalOperator() = default; - LogicalOperatorType type() const override - { - return LogicalOperatorType::PREDICATE; - } + LogicalOperatorType type() const override { return LogicalOperatorType::PREDICATE; } }; diff --git a/src/observer/sql/operator/predicate_physical_operator.cpp b/src/observer/sql/operator/predicate_physical_operator.cpp index 2b8f27be4..d2f767ed4 100644 --- a/src/observer/sql/operator/predicate_physical_operator.cpp +++ b/src/observer/sql/operator/predicate_physical_operator.cpp @@ -12,11 +12,11 @@ See the Mulan PSL v2 for more details. */ // Created by WangYunlai on 2022/6/27. // -#include "common/log/log.h" #include "sql/operator/predicate_physical_operator.h" -#include "storage/record/record.h" +#include "common/log/log.h" #include "sql/stmt/filter_stmt.h" #include "storage/field/field.h" +#include "storage/record/record.h" PredicatePhysicalOperator::PredicatePhysicalOperator(std::unique_ptr expr) : expression_(std::move(expr)) { @@ -35,7 +35,7 @@ RC PredicatePhysicalOperator::open(Trx *trx) RC PredicatePhysicalOperator::next() { - RC rc = RC::SUCCESS; + RC rc = RC::SUCCESS; PhysicalOperator *oper = children_.front().get(); while (RC::SUCCESS == (rc = oper->next())) { @@ -65,7 +65,4 @@ RC PredicatePhysicalOperator::close() return RC::SUCCESS; } -Tuple *PredicatePhysicalOperator::current_tuple() -{ - return children_[0]->current_tuple(); -} +Tuple *PredicatePhysicalOperator::current_tuple() { return children_[0]->current_tuple(); } diff --git a/src/observer/sql/operator/predicate_physical_operator.h b/src/observer/sql/operator/predicate_physical_operator.h index 362cbb5e7..3ef325935 100644 --- a/src/observer/sql/operator/predicate_physical_operator.h +++ b/src/observer/sql/operator/predicate_physical_operator.h @@ -14,9 +14,8 @@ See the Mulan PSL v2 for more details. */ #pragma once -#include -#include "sql/operator/physical_operator.h" #include "sql/expr/expression.h" +#include "sql/operator/physical_operator.h" class FilterStmt; @@ -31,10 +30,7 @@ class PredicatePhysicalOperator : public PhysicalOperator virtual ~PredicatePhysicalOperator() = default; - PhysicalOperatorType type() const override - { - return PhysicalOperatorType::PREDICATE; - } + PhysicalOperatorType type() const override { return PhysicalOperatorType::PREDICATE; } RC open(Trx *trx) override; RC next() override; diff --git a/src/observer/sql/operator/project_logical_operator.cpp b/src/observer/sql/operator/project_logical_operator.cpp index 8e253bde8..e6bcbc882 100644 --- a/src/observer/sql/operator/project_logical_operator.cpp +++ b/src/observer/sql/operator/project_logical_operator.cpp @@ -14,5 +14,4 @@ See the Mulan PSL v2 for more details. */ #include "sql/operator/project_logical_operator.h" -ProjectLogicalOperator::ProjectLogicalOperator(const std::vector &fields) : fields_(fields) -{} +ProjectLogicalOperator::ProjectLogicalOperator(const std::vector &fields) : fields_(fields) {} diff --git a/src/observer/sql/operator/project_logical_operator.h b/src/observer/sql/operator/project_logical_operator.h index b517e8495..0a67f6699 100644 --- a/src/observer/sql/operator/project_logical_operator.h +++ b/src/observer/sql/operator/project_logical_operator.h @@ -14,11 +14,11 @@ See the Mulan PSL v2 for more details. */ #pragma once -#include #include +#include -#include "sql/operator/logical_operator.h" #include "sql/expr/expression.h" +#include "sql/operator/logical_operator.h" #include "storage/field/field.h" /** @@ -26,29 +26,17 @@ See the Mulan PSL v2 for more details. */ * @ingroup LogicalOperator * @details 从表中获取数据后,可能需要过滤,投影,连接等等。 */ -class ProjectLogicalOperator : public LogicalOperator +class ProjectLogicalOperator : public LogicalOperator { public: ProjectLogicalOperator(const std::vector &fields); virtual ~ProjectLogicalOperator() = default; - LogicalOperatorType type() const override - { - return LogicalOperatorType::PROJECTION; - } - - std::vector> &expressions() - { - return expressions_; - } - const std::vector> &expressions() const - { - return expressions_; - } - const std::vector &fields() const - { - return fields_; - } + LogicalOperatorType type() const override { return LogicalOperatorType::PROJECTION; } + + std::vector> &expressions() { return expressions_; } + const std::vector> &expressions() const { return expressions_; } + const std::vector &fields() const { return fields_; } private: //! 投影映射的字段名称 diff --git a/src/observer/sql/operator/project_physical_operator.cpp b/src/observer/sql/operator/project_physical_operator.cpp index 7c700cc2c..55014379b 100644 --- a/src/observer/sql/operator/project_physical_operator.cpp +++ b/src/observer/sql/operator/project_physical_operator.cpp @@ -12,8 +12,8 @@ See the Mulan PSL v2 for more details. */ // Created by WangYunlai on 2022/07/01. // -#include "common/log/log.h" #include "sql/operator/project_physical_operator.h" +#include "common/log/log.h" #include "storage/record/record.h" #include "storage/table/table.h" @@ -24,7 +24,7 @@ RC ProjectPhysicalOperator::open(Trx *trx) } PhysicalOperator *child = children_[0].get(); - RC rc = child->open(trx); + RC rc = child->open(trx); if (rc != RC::SUCCESS) { LOG_WARN("failed to open child operator: %s", strrc(rc)); return rc; diff --git a/src/observer/sql/operator/project_physical_operator.h b/src/observer/sql/operator/project_physical_operator.h index 068b6a30f..de7f9cf3a 100644 --- a/src/observer/sql/operator/project_physical_operator.h +++ b/src/observer/sql/operator/project_physical_operator.h @@ -23,30 +23,20 @@ See the Mulan PSL v2 for more details. */ class ProjectPhysicalOperator : public PhysicalOperator { public: - ProjectPhysicalOperator() - {} + ProjectPhysicalOperator() {} virtual ~ProjectPhysicalOperator() = default; - void add_expressions(std::vector> &&expressions) - { - - } + void add_expressions(std::vector> &&expressions) {} void add_projection(const Table *table, const FieldMeta *field); - PhysicalOperatorType type() const override - { - return PhysicalOperatorType::PROJECT; - } + PhysicalOperatorType type() const override { return PhysicalOperatorType::PROJECT; } RC open(Trx *trx) override; RC next() override; RC close() override; - int cell_num() const - { - return tuple_.cell_num(); - } + int cell_num() const { return tuple_.cell_num(); } Tuple *current_tuple() override; diff --git a/src/observer/sql/operator/string_list_physical_operator.h b/src/observer/sql/operator/string_list_physical_operator.h index c42098a7a..b363b3be0 100644 --- a/src/observer/sql/operator/string_list_physical_operator.h +++ b/src/observer/sql/operator/string_list_physical_operator.h @@ -14,8 +14,8 @@ See the Mulan PSL v2 for more details. */ #pragma once -#include #include "sql/operator/physical_operator.h" +#include /** * @brief 字符串列表物理算子 @@ -25,8 +25,7 @@ See the Mulan PSL v2 for more details. */ class StringListPhysicalOperator : public PhysicalOperator { public: - StringListPhysicalOperator() - {} + StringListPhysicalOperator() {} virtual ~StringListPhysicalOperator() = default; @@ -36,10 +35,7 @@ class StringListPhysicalOperator : public PhysicalOperator strings_.emplace_back(begin, end); } - void append(std::initializer_list init) - { - strings_.emplace_back(init); - } + void append(std::initializer_list init) { strings_.emplace_back(init); } template void append(const T &v) @@ -47,20 +43,14 @@ class StringListPhysicalOperator : public PhysicalOperator strings_.emplace_back(1, v); } - PhysicalOperatorType type() const override - { - return PhysicalOperatorType::STRING_LIST; - } + PhysicalOperatorType type() const override { return PhysicalOperatorType::STRING_LIST; } - RC open(Trx *) override - { - return RC::SUCCESS; - } + RC open(Trx *) override { return RC::SUCCESS; } RC next() override { if (!started_) { - started_ = true; + started_ = true; iterator_ = strings_.begin(); } else if (iterator_ != strings_.end()) { ++iterator_; @@ -80,7 +70,7 @@ class StringListPhysicalOperator : public PhysicalOperator return nullptr; } - const StringList &string_list = *iterator_; + const StringList &string_list = *iterator_; std::vector cells; for (const std::string &s : string_list) { @@ -93,10 +83,10 @@ class StringListPhysicalOperator : public PhysicalOperator } private: - using StringList = std::vector; + using StringList = std::vector; using StringListList = std::vector; - StringListList strings_; + StringListList strings_; StringListList::iterator iterator_; - bool started_ = false; - ValueListTuple tuple_; + bool started_ = false; + ValueListTuple tuple_; }; diff --git a/src/observer/sql/operator/table_get_logical_operator.h b/src/observer/sql/operator/table_get_logical_operator.h index 1871f0aea..1985400b6 100644 --- a/src/observer/sql/operator/table_get_logical_operator.h +++ b/src/observer/sql/operator/table_get_logical_operator.h @@ -27,24 +27,18 @@ class TableGetLogicalOperator : public LogicalOperator TableGetLogicalOperator(Table *table, const std::vector &fields, bool readonly); virtual ~TableGetLogicalOperator() = default; - LogicalOperatorType type() const override - { - return LogicalOperatorType::TABLE_GET; - } + LogicalOperatorType type() const override { return LogicalOperatorType::TABLE_GET; } - Table *table() const { return table_; } - bool readonly() const { return readonly_; } + Table *table() const { return table_; } + bool readonly() const { return readonly_; } - void set_predicates(std::vector> &&exprs); - std::vector> &predicates() - { - return predicates_; - } + void set_predicates(std::vector> &&exprs); + std::vector> &predicates() { return predicates_; } private: - Table *table_ = nullptr; + Table *table_ = nullptr; std::vector fields_; - bool readonly_ = false; + bool readonly_ = false; // 与当前表相关的过滤操作,可以尝试在遍历数据时执行 // 这里的表达式都是比较简单的比较运算,并且左右两边都是取字段表达式或值表达式 diff --git a/src/observer/sql/operator/table_scan_physical_operator.cpp b/src/observer/sql/operator/table_scan_physical_operator.cpp index 68dca2f8c..2551416a3 100644 --- a/src/observer/sql/operator/table_scan_physical_operator.cpp +++ b/src/observer/sql/operator/table_scan_physical_operator.cpp @@ -13,8 +13,8 @@ See the Mulan PSL v2 for more details. */ // #include "sql/operator/table_scan_physical_operator.h" -#include "storage/table/table.h" #include "event/sql_debug.h" +#include "storage/table/table.h" using namespace std; @@ -34,7 +34,7 @@ RC TableScanPhysicalOperator::next() return RC::RECORD_EOF; } - RC rc = RC::SUCCESS; + RC rc = RC::SUCCESS; bool filter_result = false; while (record_scanner_.has_next()) { rc = record_scanner_.next(current_record_); @@ -59,10 +59,7 @@ RC TableScanPhysicalOperator::next() return rc; } -RC TableScanPhysicalOperator::close() -{ - return record_scanner_.close_scan(); -} +RC TableScanPhysicalOperator::close() { return record_scanner_.close_scan(); } Tuple *TableScanPhysicalOperator::current_tuple() { @@ -70,10 +67,7 @@ Tuple *TableScanPhysicalOperator::current_tuple() return &tuple_; } -string TableScanPhysicalOperator::param() const -{ - return table_->name(); -} +string TableScanPhysicalOperator::param() const { return table_->name(); } void TableScanPhysicalOperator::set_predicates(vector> &&exprs) { @@ -82,7 +76,7 @@ void TableScanPhysicalOperator::set_predicates(vector> && RC TableScanPhysicalOperator::filter(RowTuple &tuple, bool &result) { - RC rc = RC::SUCCESS; + RC rc = RC::SUCCESS; Value value; for (unique_ptr &expr : predicates_) { rc = expr->get_value(tuple, value); diff --git a/src/observer/sql/operator/table_scan_physical_operator.h b/src/observer/sql/operator/table_scan_physical_operator.h index 5f10fbbdf..683e09114 100644 --- a/src/observer/sql/operator/table_scan_physical_operator.h +++ b/src/observer/sql/operator/table_scan_physical_operator.h @@ -14,9 +14,9 @@ See the Mulan PSL v2 for more details. */ #pragma once +#include "common/rc.h" #include "sql/operator/physical_operator.h" #include "storage/record/record_manager.h" -#include "common/rc.h" class Table; @@ -27,18 +27,13 @@ class Table; class TableScanPhysicalOperator : public PhysicalOperator { public: - TableScanPhysicalOperator(Table *table, bool readonly) - : table_(table), readonly_(readonly) - {} + TableScanPhysicalOperator(Table *table, bool readonly) : table_(table), readonly_(readonly) {} virtual ~TableScanPhysicalOperator() = default; std::string param() const override; - PhysicalOperatorType type() const override - { - return PhysicalOperatorType::TABLE_SCAN; - } + PhysicalOperatorType type() const override { return PhysicalOperatorType::TABLE_SCAN; } RC open(Trx *trx) override; RC next() override; @@ -52,11 +47,11 @@ class TableScanPhysicalOperator : public PhysicalOperator RC filter(RowTuple &tuple, bool &result); private: - Table * table_ = nullptr; - Trx * trx_ = nullptr; + Table *table_ = nullptr; + Trx *trx_ = nullptr; bool readonly_ = false; RecordFileScanner record_scanner_; Record current_record_; RowTuple tuple_; - std::vector> predicates_; // TODO chang predicate to table tuple filter + std::vector> predicates_; // TODO chang predicate to table tuple filter }; diff --git a/src/observer/sql/optimizer/comparison_simplification_rule.cpp b/src/observer/sql/optimizer/comparison_simplification_rule.cpp index 19a343ba8..67e9d64ed 100644 --- a/src/observer/sql/optimizer/comparison_simplification_rule.cpp +++ b/src/observer/sql/optimizer/comparison_simplification_rule.cpp @@ -13,16 +13,19 @@ See the Mulan PSL v2 for more details. */ // #include "sql/optimizer/comparison_simplification_rule.h" -#include "sql/expr/expression.h" #include "common/log/log.h" +#include "sql/expr/expression.h" RC ComparisonSimplificationRule::rewrite(std::unique_ptr &expr, bool &change_made) { RC rc = RC::SUCCESS; + change_made = false; if (expr->type() == ExprType::COMPARISON) { - ComparisonExpr *cmp_expr = static_cast(expr.get()); Value value; + + ComparisonExpr *cmp_expr = static_cast(expr.get()); + RC sub_rc = cmp_expr->try_get_value(value); if (sub_rc == RC::SUCCESS) { std::unique_ptr new_expr(new ValueExpr(value)); diff --git a/src/observer/sql/optimizer/comparison_simplification_rule.h b/src/observer/sql/optimizer/comparison_simplification_rule.h index 0551c9f16..45f1c4e4c 100644 --- a/src/observer/sql/optimizer/comparison_simplification_rule.h +++ b/src/observer/sql/optimizer/comparison_simplification_rule.h @@ -25,10 +25,10 @@ class LogicalOperator; * @details 如果有简单的比较运算,比如比较的两边都是常量,那我们就可以在运行执行计划之前就知道结果, * 进而直接将表达式改成结果,这样就可以减少运行时的计算量。 */ -class ComparisonSimplificationRule : public ExpressionRewriteRule +class ComparisonSimplificationRule : public ExpressionRewriteRule { public: - ComparisonSimplificationRule() = default; + ComparisonSimplificationRule() = default; virtual ~ComparisonSimplificationRule() = default; RC rewrite(std::unique_ptr &expr, bool &change_made) override; diff --git a/src/observer/sql/optimizer/conjunction_simplification_rule.cpp b/src/observer/sql/optimizer/conjunction_simplification_rule.cpp index 231cb8ed7..1495285d6 100644 --- a/src/observer/sql/optimizer/conjunction_simplification_rule.cpp +++ b/src/observer/sql/optimizer/conjunction_simplification_rule.cpp @@ -12,15 +12,15 @@ See the Mulan PSL v2 for more details. */ // Created by Wangyunlai on 2022/12/26. // -#include "common/log/log.h" #include "sql/optimizer/conjunction_simplification_rule.h" +#include "common/log/log.h" #include "sql/expr/expression.h" RC try_to_get_bool_constant(std::unique_ptr &expr, bool &constant_value) { if (expr->type() == ExprType::VALUE && expr->value_type() == BOOLEANS) { auto value_expr = static_cast(expr.get()); - constant_value = value_expr->get_value().get_boolean(); + constant_value = value_expr->get_value().get_boolean(); return RC::SUCCESS; } return RC::INTERNAL; @@ -32,14 +32,17 @@ RC ConjunctionSimplificationRule::rewrite(std::unique_ptr &expr, boo return rc; } - change_made = false; - auto conjunction_expr = static_cast(expr.get()); - std::vector> &child_exprs = conjunction_expr->children(); + change_made = false; + auto conjunction_expr = static_cast(expr.get()); + + std::vector> &child_exprs = conjunction_expr->children(); + // 先看看有没有能够直接去掉的表达式。比如AND时恒为true的表达式可以删除 // 或者是否可以直接计算出当前表达式的值。比如AND时,如果有一个表达式为false,那么整个表达式就是false for (auto iter = child_exprs.begin(); iter != child_exprs.end();) { bool constant_value = false; - rc = try_to_get_bool_constant(*iter, constant_value); + + rc = try_to_get_bool_constant(*iter, constant_value); if (rc != RC::SUCCESS) { rc = RC::SUCCESS; ++iter; diff --git a/src/observer/sql/optimizer/conjunction_simplification_rule.h b/src/observer/sql/optimizer/conjunction_simplification_rule.h index 04c67c3cf..d0ba0fb8c 100644 --- a/src/observer/sql/optimizer/conjunction_simplification_rule.h +++ b/src/observer/sql/optimizer/conjunction_simplification_rule.h @@ -23,10 +23,10 @@ class LogicalOperator; * @ingroup Rewriter * @details 比如只有一个表达式,或者表达式可以直接出来 */ -class ConjunctionSimplificationRule : public ExpressionRewriteRule +class ConjunctionSimplificationRule : public ExpressionRewriteRule { public: - ConjunctionSimplificationRule() = default; + ConjunctionSimplificationRule() = default; virtual ~ConjunctionSimplificationRule() = default; RC rewrite(std::unique_ptr &expr, bool &change_made) override; diff --git a/src/observer/sql/optimizer/expression_rewriter.cpp b/src/observer/sql/optimizer/expression_rewriter.cpp index 3a8eb6f16..7d4f5c83f 100644 --- a/src/observer/sql/optimizer/expression_rewriter.cpp +++ b/src/observer/sql/optimizer/expression_rewriter.cpp @@ -13,9 +13,9 @@ See the Mulan PSL v2 for more details. */ // #include "sql/optimizer/expression_rewriter.h" +#include "common/log/log.h" #include "sql/optimizer/comparison_simplification_rule.h" #include "sql/optimizer/conjunction_simplification_rule.h" -#include "common/log/log.h" ExpressionRewriter::ExpressionRewriter() { @@ -27,8 +27,9 @@ RC ExpressionRewriter::rewrite(std::unique_ptr &oper, bool &cha { RC rc = RC::SUCCESS; - bool sub_change_made = false; - std::vector> &expressions = oper->expressions(); + bool sub_change_made = false; + + std::vector> &expressions = oper->expressions(); for (std::unique_ptr &expr : expressions) { rc = rewrite_expression(expr, sub_change_made); if (rc != RC::SUCCESS) { @@ -47,7 +48,7 @@ RC ExpressionRewriter::rewrite(std::unique_ptr &oper, bool &cha std::vector> &child_opers = oper->children(); for (std::unique_ptr &child_oper : child_opers) { bool sub_change_made = false; - rc = rewrite(child_oper, sub_change_made); + rc = rewrite(child_oper, sub_change_made); if (sub_change_made && !change_made) { change_made = true; } @@ -65,7 +66,8 @@ RC ExpressionRewriter::rewrite_expression(std::unique_ptr &expr, boo change_made = false; for (std::unique_ptr &rule : expr_rewrite_rules_) { bool sub_change_made = false; - rc = rule->rewrite(expr, sub_change_made); + + rc = rule->rewrite(expr, sub_change_made); if (sub_change_made && !change_made) { change_made = true; } @@ -86,22 +88,26 @@ RC ExpressionRewriter::rewrite_expression(std::unique_ptr &expr, boo case ExprType::CAST: { std::unique_ptr &child_expr = (static_cast(expr.get()))->child(); - rc = rewrite_expression(child_expr, change_made); + + rc = rewrite_expression(child_expr, change_made); } break; case ExprType::COMPARISON: { - auto comparison_expr = static_cast(expr.get()); - std::unique_ptr &left_expr = comparison_expr->left(); - std::unique_ptr &right_expr = comparison_expr->right(); + auto comparison_expr = static_cast(expr.get()); + + std::unique_ptr &left_expr = comparison_expr->left(); + std::unique_ptr &right_expr = comparison_expr->right(); bool left_change_made = false; - rc = rewrite_expression(left_expr, left_change_made); + + rc = rewrite_expression(left_expr, left_change_made); if (rc != RC::SUCCESS) { return rc; } bool right_change_made = false; - rc = rewrite_expression(right_expr, right_change_made); + + rc = rewrite_expression(right_expr, right_change_made); if (rc != RC::SUCCESS) { return rc; } @@ -112,11 +118,13 @@ RC ExpressionRewriter::rewrite_expression(std::unique_ptr &expr, boo } break; case ExprType::CONJUNCTION: { - auto conjunction_expr = static_cast(expr.get()); - std::vector> &children = conjunction_expr->children(); + auto conjunction_expr = static_cast(expr.get()); + + std::vector> &children = conjunction_expr->children(); for (std::unique_ptr &child_expr : children) { bool sub_change_made = false; - rc = rewrite_expression(child_expr, sub_change_made); + + rc = rewrite_expression(child_expr, sub_change_made); if (rc != RC::SUCCESS) { LOG_WARN("failed to rewriter conjunction sub expression. rc=%s", strrc(rc)); diff --git a/src/observer/sql/optimizer/expression_rewriter.h b/src/observer/sql/optimizer/expression_rewriter.h index cb5dfd014..dbea1055c 100644 --- a/src/observer/sql/optimizer/expression_rewriter.h +++ b/src/observer/sql/optimizer/expression_rewriter.h @@ -17,11 +17,11 @@ See the Mulan PSL v2 for more details. */ #include #include "common/rc.h" -#include "sql/operator/logical_operator.h" #include "sql/expr/expression.h" +#include "sql/operator/logical_operator.h" #include "sql/optimizer/rewrite_rule.h" -class ExpressionRewriter : public RewriteRule +class ExpressionRewriter : public RewriteRule { public: ExpressionRewriter(); diff --git a/src/observer/sql/optimizer/logical_plan_generator.cpp b/src/observer/sql/optimizer/logical_plan_generator.cpp index 6b72877c6..1f983c6bb 100644 --- a/src/observer/sql/optimizer/logical_plan_generator.cpp +++ b/src/observer/sql/optimizer/logical_plan_generator.cpp @@ -14,24 +14,25 @@ See the Mulan PSL v2 for more details. */ #include "sql/optimizer/logical_plan_generator.h" -#include "sql/operator/logical_operator.h" +#include + #include "sql/operator/calc_logical_operator.h" -#include "sql/operator/project_logical_operator.h" -#include "sql/operator/predicate_logical_operator.h" -#include "sql/operator/table_get_logical_operator.h" -#include "sql/operator/insert_logical_operator.h" #include "sql/operator/delete_logical_operator.h" +#include "sql/operator/explain_logical_operator.h" +#include "sql/operator/insert_logical_operator.h" #include "sql/operator/join_logical_operator.h" +#include "sql/operator/logical_operator.h" +#include "sql/operator/predicate_logical_operator.h" #include "sql/operator/project_logical_operator.h" -#include "sql/operator/explain_logical_operator.h" +#include "sql/operator/table_get_logical_operator.h" -#include "sql/stmt/stmt.h" #include "sql/stmt/calc_stmt.h" -#include "sql/stmt/select_stmt.h" -#include "sql/stmt/filter_stmt.h" -#include "sql/stmt/insert_stmt.h" #include "sql/stmt/delete_stmt.h" #include "sql/stmt/explain_stmt.h" +#include "sql/stmt/filter_stmt.h" +#include "sql/stmt/insert_stmt.h" +#include "sql/stmt/select_stmt.h" +#include "sql/stmt/stmt.h" using namespace std; @@ -41,27 +42,32 @@ RC LogicalPlanGenerator::create(Stmt *stmt, unique_ptr &logical switch (stmt->type()) { case StmtType::CALC: { CalcStmt *calc_stmt = static_cast(stmt); - rc = create_plan(calc_stmt, logical_operator); + + rc = create_plan(calc_stmt, logical_operator); } break; case StmtType::SELECT: { SelectStmt *select_stmt = static_cast(stmt); - rc = create_plan(select_stmt, logical_operator); + + rc = create_plan(select_stmt, logical_operator); } break; case StmtType::INSERT: { InsertStmt *insert_stmt = static_cast(stmt); - rc = create_plan(insert_stmt, logical_operator); + + rc = create_plan(insert_stmt, logical_operator); } break; case StmtType::DELETE: { DeleteStmt *delete_stmt = static_cast(stmt); - rc = create_plan(delete_stmt, logical_operator); + + rc = create_plan(delete_stmt, logical_operator); } break; case StmtType::EXPLAIN: { ExplainStmt *explain_stmt = static_cast(stmt); - rc = create_plan(explain_stmt, logical_operator); + + rc = create_plan(explain_stmt, logical_operator); } break; default: { rc = RC::UNIMPLENMENT; @@ -76,13 +82,12 @@ RC LogicalPlanGenerator::create_plan(CalcStmt *calc_stmt, std::unique_ptr &logical_operator) +RC LogicalPlanGenerator::create_plan(SelectStmt *select_stmt, unique_ptr &logical_operator) { unique_ptr table_oper(nullptr); - const std::vector &tables = select_stmt->tables(); - const std::vector &all_fields = select_stmt->query_fields(); + const std::vector
&tables = select_stmt->tables(); + const std::vector &all_fields = select_stmt->query_fields(); for (Table *table : tables) { std::vector fields; for (const Field &field : all_fields) { @@ -91,7 +96,7 @@ RC LogicalPlanGenerator::create_plan( } } - unique_ptr table_get_oper(new TableGetLogicalOperator(table, fields, true/*readonly*/)); + unique_ptr table_get_oper(new TableGetLogicalOperator(table, fields, true /*readonly*/)); if (table_oper == nullptr) { table_oper = std::move(table_get_oper); } else { @@ -103,7 +108,8 @@ RC LogicalPlanGenerator::create_plan( } unique_ptr predicate_oper; - RC rc = create_plan(select_stmt->filter_stmt(), predicate_oper); + + RC rc = create_plan(select_stmt->filter_stmt(), predicate_oper); if (rc != RC::SUCCESS) { LOG_WARN("failed to create predicate logical plan. rc=%s", strrc(rc)); return rc; @@ -125,22 +131,21 @@ RC LogicalPlanGenerator::create_plan( return RC::SUCCESS; } -RC LogicalPlanGenerator::create_plan( - FilterStmt *filter_stmt, unique_ptr &logical_operator) +RC LogicalPlanGenerator::create_plan(FilterStmt *filter_stmt, unique_ptr &logical_operator) { std::vector> cmp_exprs; - const std::vector &filter_units = filter_stmt->filter_units(); + const std::vector &filter_units = filter_stmt->filter_units(); for (const FilterUnit *filter_unit : filter_units) { - const FilterObj &filter_obj_left = filter_unit->left(); + const FilterObj &filter_obj_left = filter_unit->left(); const FilterObj &filter_obj_right = filter_unit->right(); unique_ptr left(filter_obj_left.is_attr - ? static_cast(new FieldExpr(filter_obj_left.field)) - : static_cast(new ValueExpr(filter_obj_left.value))); + ? static_cast(new FieldExpr(filter_obj_left.field)) + : static_cast(new ValueExpr(filter_obj_left.value))); unique_ptr right(filter_obj_right.is_attr - ? static_cast(new FieldExpr(filter_obj_right.field)) - : static_cast(new ValueExpr(filter_obj_right.value))); + ? static_cast(new FieldExpr(filter_obj_right.field)) + : static_cast(new ValueExpr(filter_obj_right.value))); ComparisonExpr *cmp_expr = new ComparisonExpr(filter_unit->comp(), std::move(left), std::move(right)); cmp_exprs.emplace_back(cmp_expr); @@ -156,10 +161,9 @@ RC LogicalPlanGenerator::create_plan( return RC::SUCCESS; } -RC LogicalPlanGenerator::create_plan( - InsertStmt *insert_stmt, unique_ptr &logical_operator) +RC LogicalPlanGenerator::create_plan(InsertStmt *insert_stmt, unique_ptr &logical_operator) { - Table *table = insert_stmt->table(); + Table *table = insert_stmt->table(); vector values(insert_stmt->values(), insert_stmt->values() + insert_stmt->value_amount()); InsertLogicalOperator *insert_operator = new InsertLogicalOperator(table, values); @@ -167,20 +171,19 @@ RC LogicalPlanGenerator::create_plan( return RC::SUCCESS; } -RC LogicalPlanGenerator::create_plan( - DeleteStmt *delete_stmt, unique_ptr &logical_operator) +RC LogicalPlanGenerator::create_plan(DeleteStmt *delete_stmt, unique_ptr &logical_operator) { - Table *table = delete_stmt->table(); - FilterStmt *filter_stmt = delete_stmt->filter_stmt(); + Table *table = delete_stmt->table(); + FilterStmt *filter_stmt = delete_stmt->filter_stmt(); std::vector fields; for (int i = table->table_meta().sys_field_num(); i < table->table_meta().field_num(); i++) { const FieldMeta *field_meta = table->table_meta().field(i); fields.push_back(Field(table, field_meta)); } - unique_ptr table_get_oper(new TableGetLogicalOperator(table, fields, false/*readonly*/)); + unique_ptr table_get_oper(new TableGetLogicalOperator(table, fields, false /*readonly*/)); unique_ptr predicate_oper; - RC rc = create_plan(filter_stmt, predicate_oper); + RC rc = create_plan(filter_stmt, predicate_oper); if (rc != RC::SUCCESS) { return rc; } @@ -198,12 +201,13 @@ RC LogicalPlanGenerator::create_plan( return rc; } -RC LogicalPlanGenerator::create_plan( - ExplainStmt *explain_stmt, unique_ptr &logical_operator) +RC LogicalPlanGenerator::create_plan(ExplainStmt *explain_stmt, unique_ptr &logical_operator) { - Stmt *child_stmt = explain_stmt->child(); unique_ptr child_oper; - RC rc = create(child_stmt, child_oper); + + Stmt *child_stmt = explain_stmt->child(); + + RC rc = create(child_stmt, child_oper); if (rc != RC::SUCCESS) { LOG_WARN("failed to create explain's child operator. rc=%s", strrc(rc)); return rc; diff --git a/src/observer/sql/optimizer/logical_plan_generator.h b/src/observer/sql/optimizer/logical_plan_generator.h index 37261b8bb..bf56d896f 100644 --- a/src/observer/sql/optimizer/logical_plan_generator.h +++ b/src/observer/sql/optimizer/logical_plan_generator.h @@ -30,7 +30,7 @@ class LogicalOperator; class LogicalPlanGenerator { public: - LogicalPlanGenerator() = default; + LogicalPlanGenerator() = default; virtual ~LogicalPlanGenerator() = default; RC create(Stmt *stmt, std::unique_ptr &logical_operator); diff --git a/src/observer/sql/optimizer/optimize_stage.cpp b/src/observer/sql/optimizer/optimize_stage.cpp index 03558fda7..756af61a4 100644 --- a/src/observer/sql/optimizer/optimize_stage.cpp +++ b/src/observer/sql/optimizer/optimize_stage.cpp @@ -21,12 +21,10 @@ See the Mulan PSL v2 for more details. */ #include "common/io/io.h" #include "common/lang/string.h" #include "common/log/log.h" -#include "sql/expr/expression.h" +#include "event/session_event.h" +#include "event/sql_event.h" #include "sql/operator/logical_operator.h" -#include "sql/executor/sql_result.h" #include "sql/stmt/stmt.h" -#include "event/sql_event.h" -#include "event/session_event.h" using namespace std; using namespace common; @@ -34,7 +32,8 @@ using namespace common; RC OptimizeStage::handle_request(SQLStageEvent *sql_event) { unique_ptr logical_operator; - RC rc = create_logical_plan(sql_event, logical_operator); + + RC rc = create_logical_plan(sql_event, logical_operator); if (rc != RC::SUCCESS) { if (rc != RC::UNIMPLENMENT) { LOG_WARN("failed to create logical plan. rc=%s", strrc(rc)); @@ -76,7 +75,7 @@ RC OptimizeStage::generate_physical_plan( unique_ptr &logical_operator, unique_ptr &physical_operator) { RC rc = RC::SUCCESS; - rc = physical_plan_generator_.create(*logical_operator, physical_operator); + rc = physical_plan_generator_.create(*logical_operator, physical_operator); if (rc != RC::SUCCESS) { LOG_WARN("failed to create physical operator. rc=%s", strrc(rc)); } @@ -86,11 +85,11 @@ RC OptimizeStage::generate_physical_plan( RC OptimizeStage::rewrite(unique_ptr &logical_operator) { RC rc = RC::SUCCESS; - + bool change_made = false; do { change_made = false; - rc = rewriter_.rewrite(logical_operator, change_made); + rc = rewriter_.rewrite(logical_operator, change_made); if (rc != RC::SUCCESS) { LOG_WARN("failed to do expression rewrite on logical plan. rc=%s", strrc(rc)); return rc; diff --git a/src/observer/sql/optimizer/optimize_stage.h b/src/observer/sql/optimizer/optimize_stage.h index 6031a89c6..1c2b9dba2 100644 --- a/src/observer/sql/optimizer/optimize_stage.h +++ b/src/observer/sql/optimizer/optimize_stage.h @@ -14,8 +14,6 @@ See the Mulan PSL v2 for more details. */ #pragma once -#include - #include "common/rc.h" #include "sql/operator/logical_operator.h" #include "sql/operator/physical_operator.h" @@ -43,7 +41,8 @@ class OptimizeStage private: /** * @brief 根据SQL生成逻辑计划 - * @details 由于SQL语句种类比较多,并且SQL语句可能会有嵌套的情况,比如带有SQL子查询的语句,那就需要递归的创建逻辑计划。 + * @details + * 由于SQL语句种类比较多,并且SQL语句可能会有嵌套的情况,比如带有SQL子查询的语句,那就需要递归的创建逻辑计划。 * @param sql_event 包含SQL信息的事件 * @param logical_operator 生成的逻辑计划 */ @@ -75,7 +74,7 @@ class OptimizeStage std::unique_ptr &logical_operator, std::unique_ptr &physical_operator); private: - LogicalPlanGenerator logical_plan_generator_; ///< 根据SQL生成逻辑计划 - PhysicalPlanGenerator physical_plan_generator_; ///< 根据逻辑计划生成物理计划 - Rewriter rewriter_; ///< 逻辑计划改写 + LogicalPlanGenerator logical_plan_generator_; ///< 根据SQL生成逻辑计划 + PhysicalPlanGenerator physical_plan_generator_; ///< 根据逻辑计划生成物理计划 + Rewriter rewriter_; ///< 逻辑计划改写 }; diff --git a/src/observer/sql/optimizer/physical_plan_generator.cpp b/src/observer/sql/optimizer/physical_plan_generator.cpp index 9db98e701..b2cb4608d 100644 --- a/src/observer/sql/optimizer/physical_plan_generator.cpp +++ b/src/observer/sql/optimizer/physical_plan_generator.cpp @@ -14,26 +14,26 @@ See the Mulan PSL v2 for more details. */ #include -#include "sql/optimizer/physical_plan_generator.h" -#include "sql/operator/table_get_logical_operator.h" -#include "sql/operator/table_scan_physical_operator.h" -#include "sql/operator/index_scan_physical_operator.h" -#include "sql/operator/predicate_logical_operator.h" -#include "sql/operator/predicate_physical_operator.h" -#include "sql/operator/project_logical_operator.h" -#include "sql/operator/project_physical_operator.h" -#include "sql/operator/insert_logical_operator.h" -#include "sql/operator/insert_physical_operator.h" +#include "common/log/log.h" +#include "sql/expr/expression.h" +#include "sql/operator/calc_logical_operator.h" +#include "sql/operator/calc_physical_operator.h" #include "sql/operator/delete_logical_operator.h" #include "sql/operator/delete_physical_operator.h" #include "sql/operator/explain_logical_operator.h" #include "sql/operator/explain_physical_operator.h" +#include "sql/operator/index_scan_physical_operator.h" +#include "sql/operator/insert_logical_operator.h" +#include "sql/operator/insert_physical_operator.h" #include "sql/operator/join_logical_operator.h" #include "sql/operator/join_physical_operator.h" -#include "sql/operator/calc_logical_operator.h" -#include "sql/operator/calc_physical_operator.h" -#include "sql/expr/expression.h" -#include "common/log/log.h" +#include "sql/operator/predicate_logical_operator.h" +#include "sql/operator/predicate_physical_operator.h" +#include "sql/operator/project_logical_operator.h" +#include "sql/operator/project_physical_operator.h" +#include "sql/operator/table_get_logical_operator.h" +#include "sql/operator/table_scan_physical_operator.h" +#include "sql/optimizer/physical_plan_generator.h" using namespace std; @@ -87,7 +87,7 @@ RC PhysicalPlanGenerator::create_plan(TableGetLogicalOperator &table_get_oper, u // 看看是否有可以用于索引查找的表达式 Table *table = table_get_oper.table(); - Index *index = nullptr; + Index *index = nullptr; ValueExpr *value_expr = nullptr; for (auto &expr : predicates) { if (expr->type() == ExprType::COMPARISON) { @@ -97,7 +97,7 @@ RC PhysicalPlanGenerator::create_plan(TableGetLogicalOperator &table_get_oper, u continue; } - unique_ptr &left_expr = comparison_expr->left(); + unique_ptr &left_expr = comparison_expr->left(); unique_ptr &right_expr = comparison_expr->right(); // 左右比较的一边最少是一个值 if (left_expr->type() != ExprType::VALUE && right_expr->type() != ExprType::VALUE) { @@ -120,7 +120,7 @@ RC PhysicalPlanGenerator::create_plan(TableGetLogicalOperator &table_get_oper, u } const Field &field = field_expr->field(); - index = table->find_index_by_field(field.field_name()); + index = table->find_index_by_field(field.field_name()); if (nullptr != index) { break; } @@ -130,12 +130,10 @@ RC PhysicalPlanGenerator::create_plan(TableGetLogicalOperator &table_get_oper, u if (index != nullptr) { ASSERT(value_expr != nullptr, "got an index but value expr is null ?"); - const Value &value = value_expr->get_value(); + const Value &value = value_expr->get_value(); IndexScanPhysicalOperator *index_scan_oper = new IndexScanPhysicalOperator( - table, index, table_get_oper.readonly(), - &value, true /*left_inclusive*/, - &value, true /*right_inclusive*/); - + table, index, table_get_oper.readonly(), &value, true /*left_inclusive*/, &value, true /*right_inclusive*/); + index_scan_oper->set_predicates(std::move(predicates)); oper = unique_ptr(index_scan_oper); LOG_TRACE("use index scan"); @@ -157,7 +155,7 @@ RC PhysicalPlanGenerator::create_plan(PredicateLogicalOperator &pred_oper, uniqu LogicalOperator &child_oper = *children_opers.front(); unique_ptr child_phy_oper; - RC rc = create(child_oper, child_phy_oper); + RC rc = create(child_oper, child_phy_oper); if (rc != RC::SUCCESS) { LOG_WARN("failed to create child operator of predicate operator. rc=%s", strrc(rc)); return rc; @@ -181,7 +179,7 @@ RC PhysicalPlanGenerator::create_plan(ProjectLogicalOperator &project_oper, uniq RC rc = RC::SUCCESS; if (!child_opers.empty()) { LogicalOperator *child_oper = child_opers.front().get(); - rc = create(*child_oper, child_phy_oper); + rc = create(*child_oper, child_phy_oper); if (rc != RC::SUCCESS) { LOG_WARN("failed to create project logical operator's child physical operator. rc=%s", strrc(rc)); return rc; @@ -189,7 +187,7 @@ RC PhysicalPlanGenerator::create_plan(ProjectLogicalOperator &project_oper, uniq } ProjectPhysicalOperator *project_operator = new ProjectPhysicalOperator; - const vector &project_fields = project_oper.fields(); + const vector &project_fields = project_oper.fields(); for (const Field &field : project_fields) { project_operator->add_projection(field.table(), field.meta()); } @@ -206,8 +204,8 @@ RC PhysicalPlanGenerator::create_plan(ProjectLogicalOperator &project_oper, uniq RC PhysicalPlanGenerator::create_plan(InsertLogicalOperator &insert_oper, unique_ptr &oper) { - Table *table = insert_oper.table(); - vector &values = insert_oper.values(); + Table *table = insert_oper.table(); + vector &values = insert_oper.values(); InsertPhysicalOperator *insert_phy_oper = new InsertPhysicalOperator(table, std::move(values)); oper.reset(insert_phy_oper); return RC::SUCCESS; @@ -222,7 +220,7 @@ RC PhysicalPlanGenerator::create_plan(DeleteLogicalOperator &delete_oper, unique RC rc = RC::SUCCESS; if (!child_opers.empty()) { LogicalOperator *child_oper = child_opers.front().get(); - rc = create(*child_oper, child_physical_oper); + rc = create(*child_oper, child_physical_oper); if (rc != RC::SUCCESS) { LOG_WARN("failed to create physical operator. rc=%s", strrc(rc)); return rc; @@ -241,7 +239,8 @@ RC PhysicalPlanGenerator::create_plan(ExplainLogicalOperator &explain_oper, uniq { vector> &child_opers = explain_oper.children(); - RC rc = RC::SUCCESS; + RC rc = RC::SUCCESS; + unique_ptr explain_physical_oper(new ExplainPhysicalOperator); for (unique_ptr &child_oper : child_opers) { unique_ptr child_physical_oper; @@ -286,9 +285,9 @@ RC PhysicalPlanGenerator::create_plan(JoinLogicalOperator &join_oper, unique_ptr RC PhysicalPlanGenerator::create_plan(CalcLogicalOperator &logical_oper, std::unique_ptr &oper) { - RC rc = RC::SUCCESS; + RC rc = RC::SUCCESS; + CalcPhysicalOperator *calc_oper = new CalcPhysicalOperator(std::move(logical_oper.expressions())); oper.reset(calc_oper); return rc; } - diff --git a/src/observer/sql/optimizer/physical_plan_generator.h b/src/observer/sql/optimizer/physical_plan_generator.h index e51b15638..99a7b946c 100644 --- a/src/observer/sql/optimizer/physical_plan_generator.h +++ b/src/observer/sql/optimizer/physical_plan_generator.h @@ -14,11 +14,9 @@ See the Mulan PSL v2 for more details. */ #pragma once -#include - #include "common/rc.h" -#include "sql/operator/physical_operator.h" #include "sql/operator/logical_operator.h" +#include "sql/operator/physical_operator.h" class TableGetLogicalOperator; class PredicateLogicalOperator; @@ -35,10 +33,10 @@ class CalcLogicalOperator; * @details 根据逻辑计划生成物理计划。 * 不会做任何优化,完全根据本意生成物理计划。 */ -class PhysicalPlanGenerator +class PhysicalPlanGenerator { public: - PhysicalPlanGenerator() = default; + PhysicalPlanGenerator() = default; virtual ~PhysicalPlanGenerator() = default; RC create(LogicalOperator &logical_operator, std::unique_ptr &oper); diff --git a/src/observer/sql/optimizer/predicate_pushdown_rewriter.cpp b/src/observer/sql/optimizer/predicate_pushdown_rewriter.cpp index 4ea0fb2d1..38df9254a 100644 --- a/src/observer/sql/optimizer/predicate_pushdown_rewriter.cpp +++ b/src/observer/sql/optimizer/predicate_pushdown_rewriter.cpp @@ -13,9 +13,10 @@ See the Mulan PSL v2 for more details. */ // #include "sql/optimizer/predicate_pushdown_rewriter.h" +#include "common/log/log.h" +#include "sql/expr/expression.h" #include "sql/operator/logical_operator.h" #include "sql/operator/table_get_logical_operator.h" -#include "sql/expr/expression.h" RC PredicatePushdownRewriter::rewrite(std::unique_ptr &oper, bool &change_made) { @@ -40,7 +41,7 @@ RC PredicatePushdownRewriter::rewrite(std::unique_ptr &oper, bo return rc; } - std::unique_ptr &predicate_expr = predicate_oper_exprs.front(); + std::unique_ptr &predicate_expr = predicate_oper_exprs.front(); std::vector> pushdown_exprs; rc = get_exprs_can_pushdown(predicate_expr, pushdown_exprs); if (rc != RC::SUCCESS) { @@ -99,15 +100,15 @@ RC PredicatePushdownRewriter::get_exprs_can_pushdown( } } else if (expr->type() == ExprType::COMPARISON) { // 如果是比较操作,并且比较的左边或右边是表某个列值,那么就下推下去 - auto comparison_expr = static_cast(expr.get()); - CompOp comp = comparison_expr->comp(); + auto comparison_expr = static_cast(expr.get()); + CompOp comp = comparison_expr->comp(); if (comp != EQUAL_TO) { // 简单处理,仅取等值比较。当然还可以取一些范围比较,还有 like % 等操作 // 其它的还有 is null 等 return rc; } - std::unique_ptr &left_expr = comparison_expr->left(); + std::unique_ptr &left_expr = comparison_expr->left(); std::unique_ptr &right_expr = comparison_expr->right(); // 比较操作的左右两边只要有一个是取列字段值的并且另一边也是取字段值或常量,就pushdown if (left_expr->type() != ExprType::FIELD && right_expr->type() != ExprType::FIELD) { diff --git a/src/observer/sql/optimizer/predicate_pushdown_rewriter.h b/src/observer/sql/optimizer/predicate_pushdown_rewriter.h index 0ef065fa7..ac7eec3f3 100644 --- a/src/observer/sql/optimizer/predicate_pushdown_rewriter.h +++ b/src/observer/sql/optimizer/predicate_pushdown_rewriter.h @@ -14,18 +14,18 @@ See the Mulan PSL v2 for more details. */ #pragma once -#include #include "sql/optimizer/rewrite_rule.h" +#include /** * @brief 将一些谓词表达式下推到表数据扫描中 * @ingroup Rewriter * @details 这样可以提前过滤一些数据 */ -class PredicatePushdownRewriter : public RewriteRule +class PredicatePushdownRewriter : public RewriteRule { public: - PredicatePushdownRewriter() = default; + PredicatePushdownRewriter() = default; virtual ~PredicatePushdownRewriter() = default; RC rewrite(std::unique_ptr &oper, bool &change_made) override; diff --git a/src/observer/sql/optimizer/predicate_rewrite.h b/src/observer/sql/optimizer/predicate_rewrite.h index 6db2b3dc4..658206c01 100644 --- a/src/observer/sql/optimizer/predicate_rewrite.h +++ b/src/observer/sql/optimizer/predicate_rewrite.h @@ -21,10 +21,10 @@ See the Mulan PSL v2 for more details. */ * @ingroup Rewriter * @details 有些谓词可以在真正运行之前就知道结果,那么就可以提前运算出来,比如1=1,1=0。 */ -class PredicateRewriteRule : public RewriteRule +class PredicateRewriteRule : public RewriteRule { public: - PredicateRewriteRule() = default; + PredicateRewriteRule() = default; virtual ~PredicateRewriteRule() = default; RC rewrite(std::unique_ptr &oper, bool &change_made) override; diff --git a/src/observer/sql/optimizer/rewrite_rule.h b/src/observer/sql/optimizer/rewrite_rule.h index 9a0ce1407..7cb860c0a 100644 --- a/src/observer/sql/optimizer/rewrite_rule.h +++ b/src/observer/sql/optimizer/rewrite_rule.h @@ -25,7 +25,7 @@ class Expression; * @brief 逻辑计划的重写规则 * @ingroup Rewriter */ -class RewriteRule +class RewriteRule { public: virtual ~RewriteRule() = default; @@ -37,7 +37,7 @@ class RewriteRule * @brief 表达式的重写规则 * @ingroup Rewriter */ -class ExpressionRewriteRule +class ExpressionRewriteRule { public: virtual ~ExpressionRewriteRule() = default; diff --git a/src/observer/sql/optimizer/rewriter.cpp b/src/observer/sql/optimizer/rewriter.cpp index dd025cb59..8d1cebcee 100644 --- a/src/observer/sql/optimizer/rewriter.cpp +++ b/src/observer/sql/optimizer/rewriter.cpp @@ -13,10 +13,11 @@ See the Mulan PSL v2 for more details. */ // #include "sql/optimizer/rewriter.h" +#include "common/log/log.h" +#include "sql/operator/logical_operator.h" #include "sql/optimizer/expression_rewriter.h" -#include "sql/optimizer/predicate_rewrite.h" #include "sql/optimizer/predicate_pushdown_rewriter.h" -#include "sql/operator/logical_operator.h" +#include "sql/optimizer/predicate_rewrite.h" Rewriter::Rewriter() { @@ -32,6 +33,7 @@ RC Rewriter::rewrite(std::unique_ptr &oper, bool &change_made) change_made = false; for (std::unique_ptr &rule : rewrite_rules_) { bool sub_change_made = false; + rc = rule->rewrite(oper, sub_change_made); if (rc != RC::SUCCESS) { LOG_WARN("failed to rewrite logical operator. rc=%s", strrc(rc)); @@ -50,7 +52,7 @@ RC Rewriter::rewrite(std::unique_ptr &oper, bool &change_made) std::vector> &child_opers = oper->children(); for (auto &child_oper : child_opers) { bool sub_change_made = false; - rc = this->rewrite(child_oper, sub_change_made); + rc = this->rewrite(child_oper, sub_change_made); if (rc != RC::SUCCESS) { LOG_WARN("failed to rewrite child oper. rc=%s", strrc(rc)); return rc; diff --git a/src/observer/sql/optimizer/rewriter.h b/src/observer/sql/optimizer/rewriter.h index b6d02cd24..315bb5e8f 100644 --- a/src/observer/sql/optimizer/rewriter.h +++ b/src/observer/sql/optimizer/rewriter.h @@ -14,14 +14,14 @@ See the Mulan PSL v2 for more details. */ #pragma once +#include "sql/optimizer/rewrite_rule.h" #include #include -#include "sql/optimizer/rewrite_rule.h" class LogicalOperator; /** - * @defgroup Rewriter + * @defgroup Rewriter * @brief 根据规则对逻辑计划进行重写 */ @@ -31,7 +31,7 @@ class LogicalOperator; * @details 当前仅实现了一两个非常简单的规则。 * 重写包括对逻辑计划和计划中包含的表达式。 */ -class Rewriter +class Rewriter { public: Rewriter(); diff --git a/src/observer/sql/parser/parse.cpp b/src/observer/sql/parser/parse.cpp index 833cc567d..def0ed144 100644 --- a/src/observer/sql/parser/parse.cpp +++ b/src/observer/sql/parser/parse.cpp @@ -12,7 +12,6 @@ See the Mulan PSL v2 for more details. */ // Created by Meiyi // -#include #include "sql/parser/parse.h" #include "common/log/log.h" #include "sql/expr/expression.h" @@ -27,11 +26,9 @@ CalcSqlNode::~CalcSqlNode() expressions.clear(); } -ParsedSqlNode::ParsedSqlNode() : flag(SCF_ERROR) -{} +ParsedSqlNode::ParsedSqlNode() : flag(SCF_ERROR) {} -ParsedSqlNode::ParsedSqlNode(SqlCommandFlag _flag) : flag(_flag) -{} +ParsedSqlNode::ParsedSqlNode(SqlCommandFlag _flag) : flag(_flag) {} void ParsedSqlResult::add_sql_node(std::unique_ptr sql_node) { diff --git a/src/observer/sql/parser/parse_defs.h b/src/observer/sql/parser/parse_defs.h index 9fa7b4036..6037bbcc2 100644 --- a/src/observer/sql/parser/parse_defs.h +++ b/src/observer/sql/parser/parse_defs.h @@ -14,17 +14,16 @@ See the Mulan PSL v2 for more details. */ #pragma once -#include -#include -#include #include +#include +#include #include "sql/parser/value.h" class Expression; /** - * @defgroup SQLParser SQL Parser + * @defgroup SQLParser SQL Parser */ /** @@ -44,7 +43,7 @@ struct RelAttrSqlNode * @brief 描述比较运算符 * @ingroup SQLParser */ -enum CompOp +enum CompOp { EQUAL_TO, ///< "=" LESS_EQUAL, ///< "<=" @@ -65,15 +64,15 @@ enum CompOp */ struct ConditionSqlNode { - 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 - RelAttrSqlNode left_attr; ///< left-hand side attribute - CompOp comp; ///< comparison operator - int right_is_attr; ///< TRUE if right-hand side is an attribute - ///< 1时,操作符右边是属性名,0时,是属性值 - RelAttrSqlNode 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 + RelAttrSqlNode left_attr; ///< left-hand side attribute + CompOp comp; ///< comparison operator + int right_is_attr; ///< TRUE if right-hand side is an attribute + ///< 1时,操作符右边是属性名,0时,是属性值 + RelAttrSqlNode right_attr; ///< right-hand side attribute if right_is_attr = TRUE 右边的属性 + Value right_value; ///< right-hand side value if right_is_attr = FALSE }; /** @@ -89,9 +88,9 @@ struct ConditionSqlNode struct SelectSqlNode { - std::vector attributes; ///< attributes in select clause - std::vector relations; ///< 查询的表 - std::vector conditions; ///< 查询条件,使用AND串联起来多个条件 + std::vector attributes; ///< attributes in select clause + std::vector relations; ///< 查询的表 + std::vector conditions; ///< 查询条件,使用AND串联起来多个条件 }; /** @@ -132,9 +131,9 @@ struct DeleteSqlNode */ struct UpdateSqlNode { - std::string relation_name; ///< Relation to update - std::string attribute_name; ///< 更新的字段,仅支持一个字段 - Value value; ///< 更新的值,仅支持一个字段 + std::string relation_name; ///< Relation to update + std::string attribute_name; ///< 更新的字段,仅支持一个字段 + Value value; ///< 更新的值,仅支持一个字段 std::vector conditions; }; @@ -147,9 +146,9 @@ struct UpdateSqlNode */ struct AttrInfoSqlNode { - 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 }; /** @@ -159,8 +158,8 @@ struct AttrInfoSqlNode */ struct CreateTableSqlNode { - std::string relation_name; ///< Relation name - std::vector attr_infos; ///< attributes + std::string relation_name; ///< Relation name + std::vector attr_infos; ///< attributes }; /** @@ -272,7 +271,7 @@ enum SqlCommandFlag SCF_SYNC, SCF_SHOW_TABLES, SCF_DESC_TABLE, - SCF_BEGIN, ///< 事务开始语句,可以在这里扩展只读事务 + SCF_BEGIN, ///< 事务开始语句,可以在这里扩展只读事务 SCF_COMMIT, SCF_CLOG_SYNC, SCF_ROLLBACK, @@ -280,7 +279,7 @@ enum SqlCommandFlag SCF_HELP, SCF_EXIT, SCF_EXPLAIN, - SCF_SET_VARIABLE, ///< 设置变量 + SCF_SET_VARIABLE, ///< 设置变量 }; /** * @brief 表示一个SQL语句 @@ -289,21 +288,21 @@ enum SqlCommandFlag class ParsedSqlNode { public: - enum SqlCommandFlag flag; - ErrorSqlNode error; - CalcSqlNode calc; - SelectSqlNode selection; - InsertSqlNode insertion; - DeleteSqlNode deletion; - UpdateSqlNode update; - CreateTableSqlNode create_table; - DropTableSqlNode drop_table; - CreateIndexSqlNode create_index; - DropIndexSqlNode drop_index; - DescTableSqlNode desc_table; - LoadDataSqlNode load_data; - ExplainSqlNode explain; - SetVariableSqlNode set_variable; + enum SqlCommandFlag flag; + ErrorSqlNode error; + CalcSqlNode calc; + SelectSqlNode selection; + InsertSqlNode insertion; + DeleteSqlNode deletion; + UpdateSqlNode update; + CreateTableSqlNode create_table; + DropTableSqlNode drop_table; + CreateIndexSqlNode create_index; + DropIndexSqlNode drop_index; + DescTableSqlNode desc_table; + LoadDataSqlNode load_data; + ExplainSqlNode explain; + SetVariableSqlNode set_variable; public: ParsedSqlNode(); @@ -318,10 +317,8 @@ class ParsedSqlResult { public: void add_sql_node(std::unique_ptr sql_node); - std::vector> &sql_nodes() - { - return sql_nodes_; - } + + std::vector> &sql_nodes() { return sql_nodes_; } private: std::vector> sql_nodes_; ///< 这里记录SQL命令。虽然看起来支持多个,但是当前仅处理一个 diff --git a/src/observer/sql/parser/parse_stage.cpp b/src/observer/sql/parser/parse_stage.cpp index 72ce1dbaa..3b32b7882 100644 --- a/src/observer/sql/parser/parse_stage.cpp +++ b/src/observer/sql/parser/parse_stage.cpp @@ -30,9 +30,9 @@ using namespace common; RC ParseStage::handle_request(SQLStageEvent *sql_event) { RC rc = RC::SUCCESS; - - SqlResult *sql_result = sql_event->session_event()->sql_result(); - const std::string &sql = sql_event->sql(); + + SqlResult *sql_result = sql_event->session_event()->sql_result(); + const std::string &sql = sql_event->sql(); ParsedSqlResult parsed_sql_result; diff --git a/src/observer/sql/parser/resolve_stage.cpp b/src/observer/sql/parser/resolve_stage.cpp index f9f3de6ec..e9681fafc 100644 --- a/src/observer/sql/parser/resolve_stage.cpp +++ b/src/observer/sql/parser/resolve_stage.cpp @@ -21,8 +21,8 @@ See the Mulan PSL v2 for more details. */ #include "common/io/io.h" #include "common/lang/string.h" #include "common/log/log.h" -#include "event/sql_event.h" #include "event/session_event.h" +#include "event/sql_event.h" #include "session/session.h" #include "sql/stmt/stmt.h" @@ -30,9 +30,9 @@ using namespace common; RC ResolveStage::handle_request(SQLStageEvent *sql_event) { - RC rc = RC::SUCCESS; + RC rc = RC::SUCCESS; SessionEvent *session_event = sql_event->session_event(); - SqlResult *sql_result = session_event->sql_result(); + SqlResult *sql_result = session_event->sql_result(); Db *db = session_event->session()->get_current_db(); if (nullptr == db) { @@ -44,7 +44,8 @@ RC ResolveStage::handle_request(SQLStageEvent *sql_event) } ParsedSqlNode *sql_node = sql_event->sql_node().get(); - Stmt *stmt = nullptr; + Stmt *stmt = nullptr; + rc = Stmt::create_stmt(db, *sql_node, stmt); if (rc != RC::SUCCESS && rc != RC::UNIMPLENMENT) { LOG_WARN("failed to create stmt. rc=%d:%s", rc, strrc(rc)); diff --git a/src/observer/sql/parser/value.cpp b/src/observer/sql/parser/value.cpp index 619e30c92..e69dace9c 100644 --- a/src/observer/sql/parser/value.cpp +++ b/src/observer/sql/parser/value.cpp @@ -12,12 +12,11 @@ See the Mulan PSL v2 for more details. */ // Created by WangYunlai on 2023/06/28. // -#include #include "sql/parser/value.h" -#include "storage/field/field.h" -#include "common/log/log.h" #include "common/lang/comparator.h" #include "common/lang/string.h" +#include "common/log/log.h" +#include const char *ATTR_TYPE_NAME[] = {"undefined", "chars", "ints", "floats", "booleans"}; @@ -38,25 +37,13 @@ AttrType attr_type_from_string(const char *s) return UNDEFINED; } -Value::Value(int val) -{ - set_int(val); -} +Value::Value(int val) { set_int(val); } -Value::Value(float val) -{ - set_float(val); -} +Value::Value(float val) { set_float(val); } -Value::Value(bool val) -{ - set_boolean(val); -} +Value::Value(bool val) { set_boolean(val); } -Value::Value(const char *s, int len /*= 0*/) -{ - set_string(s, len); -} +Value::Value(const char *s, int len /*= 0*/) { set_string(s, len); } void Value::set_data(char *data, int length) { @@ -66,15 +53,15 @@ void Value::set_data(char *data, int length) } break; case INTS: { num_value_.int_value_ = *(int *)data; - length_ = length; + length_ = length; } break; case FLOATS: { num_value_.float_value_ = *(float *)data; - length_ = length; + length_ = length; } break; case BOOLEANS: { num_value_.bool_value_ = *(int *)data != 0; - length_ = length; + length_ = length; } break; default: { LOG_WARN("unknown data type: %d", attr_type_); @@ -83,22 +70,22 @@ void Value::set_data(char *data, int length) } void Value::set_int(int val) { - attr_type_ = INTS; + attr_type_ = INTS; num_value_.int_value_ = val; - length_ = sizeof(val); + length_ = sizeof(val); } void Value::set_float(float val) { - attr_type_ = FLOATS; + attr_type_ = FLOATS; num_value_.float_value_ = val; - length_ = sizeof(val); + length_ = sizeof(val); } void Value::set_boolean(bool val) { - attr_type_ = BOOLEANS; + attr_type_ = BOOLEANS; num_value_.bool_value_ = val; - length_ = sizeof(val); + length_ = sizeof(val); } void Value::set_string(const char *s, int len /*= 0*/) { @@ -258,10 +245,7 @@ float Value::get_float() const return 0; } -std::string Value::get_string() const -{ - return this->to_string(); -} +std::string Value::get_string() const { return this->to_string(); } bool Value::get_boolean() const { diff --git a/src/observer/sql/parser/value.h b/src/observer/sql/parser/value.h index 32206b49d..e16e6685e 100644 --- a/src/observer/sql/parser/value.h +++ b/src/observer/sql/parser/value.h @@ -18,51 +18,42 @@ See the Mulan PSL v2 for more details. */ /** * @brief 属性的类型 - * + * */ enum AttrType { UNDEFINED, - CHARS, ///< 字符串类型 - INTS, ///< 整数类型(4字节) - FLOATS, ///< 浮点数类型(4字节) - BOOLEANS, ///< boolean类型,当前不是由parser解析出来的,是程序内部使用的 + CHARS, ///< 字符串类型 + INTS, ///< 整数类型(4字节) + FLOATS, ///< 浮点数类型(4字节) + BOOLEANS, ///< boolean类型,当前不是由parser解析出来的,是程序内部使用的 }; const char *attr_type_to_string(AttrType type); -AttrType attr_type_from_string(const char *s); +AttrType attr_type_from_string(const char *s); /** * @brief 属性的值 - * + * */ -class Value +class Value { public: Value() = default; - Value(AttrType attr_type, char *data, int length = 4) : attr_type_(attr_type) - { - this->set_data(data, length); - } + Value(AttrType attr_type, char *data, int length = 4) : attr_type_(attr_type) { this->set_data(data, length); } explicit Value(int val); explicit Value(float val); explicit Value(bool val); explicit Value(const char *s, int len = 0); - Value(const Value &other) = default; + Value(const Value &other) = default; Value &operator=(const Value &other) = default; - void set_type(AttrType type) - { - this->attr_type_ = type; - } + void set_type(AttrType type) { this->attr_type_ = type; } void set_data(char *data, int length); - void set_data(const char *data, int length) - { - this->set_data(const_cast(data), length); - } + void set_data(const char *data, int length) { this->set_data(const_cast(data), length); } void set_int(int val); void set_float(float val); void set_boolean(bool val); @@ -74,34 +65,29 @@ class Value int compare(const Value &other) const; const char *data() const; - int length() const - { - return length_; - } + int length() const { return length_; } - AttrType attr_type() const - { - return attr_type_; - } + AttrType attr_type() const { return attr_type_; } public: /** * 获取对应的值 * 如果当前的类型与期望获取的类型不符,就会执行转换操作 */ - int get_int() const; - float get_float() const; + int get_int() const; + float get_float() const; std::string get_string() const; - bool get_boolean() const; + bool get_boolean() const; private: AttrType attr_type_ = UNDEFINED; - int length_ = 0; + int length_ = 0; - union { - int int_value_; + union + { + int int_value_; float float_value_; - bool bool_value_; + bool bool_value_; } num_value_; std::string str_value_; }; \ No newline at end of file diff --git a/src/observer/sql/plan_cache/plan_cache_stage.h b/src/observer/sql/plan_cache/plan_cache_stage.h index 41dab8bd4..e359bf22b 100644 --- a/src/observer/sql/plan_cache/plan_cache_stage.h +++ b/src/observer/sql/plan_cache/plan_cache_stage.h @@ -23,5 +23,4 @@ See the Mulan PSL v2 for more details. */ * 感兴趣的同学可以参考OceanBase的实现 */ class PlanCacheStage -{ -}; +{}; diff --git a/src/observer/sql/query_cache/query_cache_stage.h b/src/observer/sql/query_cache/query_cache_stage.h index 663c7bf37..629c2d173 100644 --- a/src/observer/sql/query_cache/query_cache_stage.h +++ b/src/observer/sql/query_cache/query_cache_stage.h @@ -26,7 +26,7 @@ class SQLStageEvent; class QueryCacheStage { public: - QueryCacheStage() = default; + QueryCacheStage() = default; virtual ~QueryCacheStage() = default; public: diff --git a/src/observer/sql/stmt/calc_stmt.h b/src/observer/sql/stmt/calc_stmt.h index a6ceb11c0..079880fed 100644 --- a/src/observer/sql/stmt/calc_stmt.h +++ b/src/observer/sql/stmt/calc_stmt.h @@ -15,11 +15,10 @@ See the Mulan PSL v2 for more details. */ #pragma once #include -#include #include "common/rc.h" -#include "sql/stmt/stmt.h" #include "sql/expr/expression.h" +#include "sql/stmt/stmt.h" class Db; class Table; @@ -28,22 +27,19 @@ class Table; * @brief 描述算术运算语句 * @ingroup Statement */ -class CalcStmt : public Stmt +class CalcStmt : public Stmt { public: - CalcStmt() = default; + CalcStmt() = default; virtual ~CalcStmt() override = default; - StmtType type() const override - { - return StmtType::CALC; - } + StmtType type() const override { return StmtType::CALC; } public: static RC create(CalcSqlNode &calc_sql, Stmt *&stmt) { CalcStmt *calc_stmt = new CalcStmt(); - for (Expression * const expr : calc_sql.expressions) { + for (Expression *const expr : calc_sql.expressions) { calc_stmt->expressions_.emplace_back(expr); } calc_sql.expressions.clear(); @@ -52,10 +48,7 @@ class CalcStmt : public Stmt } public: - std::vector> &expressions() - { - return expressions_; - } + std::vector> &expressions() { return expressions_; } private: std::vector> expressions_; diff --git a/src/observer/sql/stmt/create_index_stmt.cpp b/src/observer/sql/stmt/create_index_stmt.cpp index 7b61fbcea..93fae6630 100644 --- a/src/observer/sql/stmt/create_index_stmt.cpp +++ b/src/observer/sql/stmt/create_index_stmt.cpp @@ -13,10 +13,10 @@ See the Mulan PSL v2 for more details. */ // #include "sql/stmt/create_index_stmt.h" -#include "storage/table/table.h" -#include "storage/db/db.h" #include "common/lang/string.h" #include "common/log/log.h" +#include "storage/db/db.h" +#include "storage/table/table.h" using namespace std; using namespace common; @@ -26,7 +26,8 @@ RC CreateIndexStmt::create(Db *db, const CreateIndexSqlNode &create_index, Stmt stmt = nullptr; const char *table_name = create_index.relation_name.c_str(); - if (is_blank(table_name) || is_blank(create_index.index_name.c_str()) || is_blank(create_index.attribute_name.c_str())) { + if (is_blank(table_name) || is_blank(create_index.index_name.c_str()) || + is_blank(create_index.attribute_name.c_str())) { LOG_WARN("invalid argument. db=%p, table_name=%p, index name=%s, attribute name=%s", db, table_name, create_index.index_name.c_str(), create_index.attribute_name.c_str()); return RC::INVALID_ARGUMENT; @@ -43,7 +44,7 @@ RC CreateIndexStmt::create(Db *db, const CreateIndexSqlNode &create_index, Stmt if (nullptr == field_meta) { LOG_WARN("no such field in table. db=%s, table=%s, field name=%s", db->name(), table_name, create_index.attribute_name.c_str()); - return RC::SCHEMA_FIELD_NOT_EXIST; + return RC::SCHEMA_FIELD_NOT_EXIST; } Index *index = table->find_index(create_index.index_name.c_str()); diff --git a/src/observer/sql/stmt/create_index_stmt.h b/src/observer/sql/stmt/create_index_stmt.h index 0c822d72c..6cd9fbbe4 100644 --- a/src/observer/sql/stmt/create_index_stmt.h +++ b/src/observer/sql/stmt/create_index_stmt.h @@ -30,24 +30,22 @@ class CreateIndexStmt : public Stmt { public: CreateIndexStmt(Table *table, const FieldMeta *field_meta, const std::string &index_name) - : table_(table), - field_meta_(field_meta), - index_name_(index_name) + : table_(table), field_meta_(field_meta), index_name_(index_name) {} virtual ~CreateIndexStmt() = default; StmtType type() const override { return StmtType::CREATE_INDEX; } - Table *table() const { return table_; } - const FieldMeta *field_meta() const { return field_meta_; } + Table *table() const { return table_; } + const FieldMeta *field_meta() const { return field_meta_; } const std::string &index_name() const { return index_name_; } public: static RC create(Db *db, const CreateIndexSqlNode &create_index, Stmt *&stmt); private: - Table *table_ = nullptr; + Table *table_ = nullptr; const FieldMeta *field_meta_ = nullptr; - std::string index_name_; + std::string index_name_; }; diff --git a/src/observer/sql/stmt/create_table_stmt.h b/src/observer/sql/stmt/create_table_stmt.h index 695fc49af..710c54733 100644 --- a/src/observer/sql/stmt/create_table_stmt.h +++ b/src/observer/sql/stmt/create_table_stmt.h @@ -30,19 +30,18 @@ class CreateTableStmt : public Stmt { public: CreateTableStmt(const std::string &table_name, const std::vector &attr_infos) - : table_name_(table_name), - attr_infos_(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::string &table_name() const { return table_name_; } const std::vector &attr_infos() const { return attr_infos_; } static RC create(Db *db, const CreateTableSqlNode &create_table, Stmt *&stmt); private: - std::string table_name_; + 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 f0d9515c7..36f8c1f8b 100644 --- a/src/observer/sql/stmt/delete_stmt.cpp +++ b/src/observer/sql/stmt/delete_stmt.cpp @@ -12,14 +12,13 @@ See the Mulan PSL v2 for more details. */ // Created by Wangyunlai on 2022/5/22. // -#include "common/log/log.h" #include "sql/stmt/delete_stmt.h" +#include "common/log/log.h" #include "sql/stmt/filter_stmt.h" #include "storage/db/db.h" #include "storage/table/table.h" -DeleteStmt::DeleteStmt(Table *table, FilterStmt *filter_stmt) : table_(table), filter_stmt_(filter_stmt) -{} +DeleteStmt::DeleteStmt(Table *table, FilterStmt *filter_stmt) : table_(table), filter_stmt_(filter_stmt) {} DeleteStmt::~DeleteStmt() { @@ -48,7 +47,7 @@ RC DeleteStmt::create(Db *db, const DeleteSqlNode &delete_sql, Stmt *&stmt) table_map.insert(std::pair(std::string(table_name), table)); FilterStmt *filter_stmt = nullptr; - RC rc = FilterStmt::create( + RC rc = FilterStmt::create( db, table, &table_map, delete_sql.conditions.data(), static_cast(delete_sql.conditions.size()), filter_stmt); if (rc != RC::SUCCESS) { LOG_WARN("failed to create filter statement. rc=%d:%s", rc, strrc(rc)); diff --git a/src/observer/sql/stmt/delete_stmt.h b/src/observer/sql/stmt/delete_stmt.h index 456c92cad..33cda12a6 100644 --- a/src/observer/sql/stmt/delete_stmt.h +++ b/src/observer/sql/stmt/delete_stmt.h @@ -14,8 +14,8 @@ See the Mulan PSL v2 for more details. */ #pragma once -#include "sql/stmt/stmt.h" #include "sql/parser/parse_defs.h" +#include "sql/stmt/stmt.h" class Table; class FilterStmt; @@ -24,30 +24,21 @@ class FilterStmt; * @brief Delete 语句 * @ingroup Statement */ -class DeleteStmt : public Stmt +class DeleteStmt : public Stmt { public: DeleteStmt(Table *table, FilterStmt *filter_stmt); ~DeleteStmt() override; - Table *table() const - { - return table_; - } - FilterStmt *filter_stmt() const - { - return filter_stmt_; - } + Table *table() const { return table_; } + FilterStmt *filter_stmt() const { return filter_stmt_; } - StmtType type() const override - { - return StmtType::DELETE; - } + StmtType type() const override { return StmtType::DELETE; } public: static RC create(Db *db, const DeleteSqlNode &delete_sql, Stmt *&stmt); private: - Table *table_ = nullptr; + Table *table_ = nullptr; FilterStmt *filter_stmt_ = nullptr; }; diff --git a/src/observer/sql/stmt/desc_table_stmt.h b/src/observer/sql/stmt/desc_table_stmt.h index ca553f16b..c03406e78 100644 --- a/src/observer/sql/stmt/desc_table_stmt.h +++ b/src/observer/sql/stmt/desc_table_stmt.h @@ -15,7 +15,6 @@ See the Mulan PSL v2 for more details. */ #pragma once #include -#include #include "sql/stmt/stmt.h" @@ -29,9 +28,7 @@ class Db; class DescTableStmt : public Stmt { public: - DescTableStmt(const std::string &table_name) - : table_name_(table_name) - {} + DescTableStmt(const std::string &table_name) : table_name_(table_name) {} virtual ~DescTableStmt() = default; StmtType type() const override { return StmtType::DESC_TABLE; } diff --git a/src/observer/sql/stmt/exit_stmt.h b/src/observer/sql/stmt/exit_stmt.h index aa91803be..228223bd6 100644 --- a/src/observer/sql/stmt/exit_stmt.h +++ b/src/observer/sql/stmt/exit_stmt.h @@ -14,9 +14,6 @@ See the Mulan PSL v2 for more details. */ #pragma once -#include -#include - #include "sql/stmt/stmt.h" /** @@ -26,8 +23,7 @@ See the Mulan PSL v2 for more details. */ class ExitStmt : public Stmt { public: - ExitStmt() - {} + ExitStmt() {} virtual ~ExitStmt() = default; StmtType type() const override { return StmtType::EXIT; } diff --git a/src/observer/sql/stmt/explain_stmt.cpp b/src/observer/sql/stmt/explain_stmt.cpp index 97dd4b2a3..e44b213e0 100644 --- a/src/observer/sql/stmt/explain_stmt.cpp +++ b/src/observer/sql/stmt/explain_stmt.cpp @@ -13,22 +13,21 @@ See the Mulan PSL v2 for more details. */ // #include "sql/stmt/explain_stmt.h" -#include "sql/stmt/stmt.h" #include "common/log/log.h" +#include "sql/stmt/stmt.h" -ExplainStmt::ExplainStmt(std::unique_ptr child_stmt) : child_stmt_(std::move(child_stmt)) -{} +ExplainStmt::ExplainStmt(std::unique_ptr child_stmt) : child_stmt_(std::move(child_stmt)) {} RC ExplainStmt::create(Db *db, const ExplainSqlNode &explain, Stmt *&stmt) { Stmt *child_stmt = nullptr; - RC rc = Stmt::create_stmt(db, *explain.sql_node, child_stmt); + RC rc = Stmt::create_stmt(db, *explain.sql_node, child_stmt); if (rc != RC::SUCCESS) { LOG_WARN("failed to create explain's child stmt. rc=%s", strrc(rc)); return rc; } std::unique_ptr child_stmt_ptr = std::unique_ptr(child_stmt); - stmt = new ExplainStmt(std::move(child_stmt_ptr)); + stmt = new ExplainStmt(std::move(child_stmt_ptr)); return rc; } diff --git a/src/observer/sql/stmt/explain_stmt.h b/src/observer/sql/stmt/explain_stmt.h index 0a492a974..ece21f5d3 100644 --- a/src/observer/sql/stmt/explain_stmt.h +++ b/src/observer/sql/stmt/explain_stmt.h @@ -14,28 +14,21 @@ See the Mulan PSL v2 for more details. */ #pragma once -#include #include "sql/stmt/stmt.h" /** * @brief explain语句 * @ingroup Statement */ -class ExplainStmt : public Stmt +class ExplainStmt : public Stmt { public: ExplainStmt(std::unique_ptr child_stmt); virtual ~ExplainStmt() = default; - StmtType type() const override - { - return StmtType::EXPLAIN; - } + StmtType type() const override { return StmtType::EXPLAIN; } - Stmt *child() const - { - return child_stmt_.get(); - } + Stmt *child() const { return child_stmt_.get(); } static RC create(Db *db, const ExplainSqlNode &query, Stmt *&stmt); diff --git a/src/observer/sql/stmt/filter_stmt.cpp b/src/observer/sql/stmt/filter_stmt.cpp index 39126385d..44b1d1a1a 100644 --- a/src/observer/sql/stmt/filter_stmt.cpp +++ b/src/observer/sql/stmt/filter_stmt.cpp @@ -12,10 +12,10 @@ See the Mulan PSL v2 for more details. */ // Created by Wangyunlai on 2022/5/22. // -#include "common/rc.h" -#include "common/log/log.h" -#include "common/lang/string.h" #include "sql/stmt/filter_stmt.h" +#include "common/lang/string.h" +#include "common/log/log.h" +#include "common/rc.h" #include "storage/db/db.h" #include "storage/table/table.h" @@ -31,11 +31,12 @@ RC FilterStmt::create(Db *db, Table *default_table, std::unordered_map -#include +#include "sql/expr/expression.h" #include "sql/parser/parse_defs.h" #include "sql/stmt/stmt.h" -#include "sql/expr/expression.h" +#include +#include class Db; class Table; class FieldMeta; -struct FilterObj +struct FilterObj { - bool is_attr; + bool is_attr; Field field; Value value; void init_attr(const Field &field) { - is_attr = true; + is_attr = true; this->field = field; } void init_value(const Value &value) { - is_attr = false; + is_attr = false; this->value = value; } }; -class FilterUnit +class FilterUnit { public: FilterUnit() = default; - ~FilterUnit() - {} + ~FilterUnit() {} - void set_comp(CompOp comp) - { - comp_ = comp; - } + void set_comp(CompOp comp) { comp_ = comp; } - CompOp comp() const - { - return comp_; - } + CompOp comp() const { return comp_; } - void set_left(const FilterObj &obj) - { - left_ = obj; - } - void set_right(const FilterObj &obj) - { - right_ = obj; - } + void set_left(const FilterObj &obj) { left_ = obj; } + void set_right(const FilterObj &obj) { right_ = obj; } - const FilterObj &left() const - { - return left_; - } - const FilterObj &right() const - { - return right_; - } + const FilterObj &left() const { return left_; } + const FilterObj &right() const { return right_; } private: - CompOp comp_ = NO_OP; + CompOp comp_ = NO_OP; FilterObj left_; FilterObj right_; }; @@ -88,17 +69,14 @@ class FilterUnit * @brief Filter/谓词/过滤语句 * @ingroup Statement */ -class FilterStmt +class FilterStmt { public: FilterStmt() = default; virtual ~FilterStmt(); public: - const std::vector &filter_units() const - { - return filter_units_; - } + const std::vector &filter_units() const { return filter_units_; } public: static RC create(Db *db, Table *default_table, std::unordered_map *tables, diff --git a/src/observer/sql/stmt/help_stmt.h b/src/observer/sql/stmt/help_stmt.h index 518f8b921..1946b56cb 100644 --- a/src/observer/sql/stmt/help_stmt.h +++ b/src/observer/sql/stmt/help_stmt.h @@ -26,8 +26,7 @@ See the Mulan PSL v2 for more details. */ class HelpStmt : public Stmt { public: - HelpStmt() - {} + HelpStmt() {} virtual ~HelpStmt() = default; StmtType type() const override { return StmtType::HELP; } diff --git a/src/observer/sql/stmt/insert_stmt.cpp b/src/observer/sql/stmt/insert_stmt.cpp index 0ceda92e3..3f7afa415 100644 --- a/src/observer/sql/stmt/insert_stmt.cpp +++ b/src/observer/sql/stmt/insert_stmt.cpp @@ -38,10 +38,10 @@ RC InsertStmt::create(Db *db, const InsertSqlNode &inserts, Stmt *&stmt) } // check the fields number - const Value *values = inserts.values.data(); - const int value_num = static_cast(inserts.values.size()); + const Value *values = inserts.values.data(); + const int value_num = static_cast(inserts.values.size()); const TableMeta &table_meta = table->table_meta(); - const int field_num = table_meta.field_num() - table_meta.sys_field_num(); + const int field_num = table_meta.field_num() - table_meta.sys_field_num(); if (field_num != value_num) { LOG_WARN("schema mismatch. value num=%d, field num in schema=%d", value_num, field_num); return RC::SCHEMA_FIELD_MISSING; @@ -51,8 +51,8 @@ RC InsertStmt::create(Db *db, const InsertSqlNode &inserts, Stmt *&stmt) const int sys_field_num = table_meta.sys_field_num(); for (int i = 0; i < value_num; i++) { const FieldMeta *field_meta = table_meta.field(i + sys_field_num); - const AttrType field_type = field_meta->type(); - const AttrType value_type = values[i].attr_type(); + const AttrType field_type = field_meta->type(); + const AttrType value_type = values[i].attr_type(); if (field_type != value_type) { // TODO try to convert the value type to field type LOG_WARN("field type mismatch. table=%s, field=%s, field type=%d, value_type=%d", table_name, field_meta->name(), field_type, value_type); diff --git a/src/observer/sql/stmt/insert_stmt.h b/src/observer/sql/stmt/insert_stmt.h index 28ee6229b..759745cb7 100644 --- a/src/observer/sql/stmt/insert_stmt.h +++ b/src/observer/sql/stmt/insert_stmt.h @@ -24,36 +24,24 @@ class Db; * @brief 插入语句 * @ingroup Statement */ -class InsertStmt : public Stmt +class InsertStmt : public Stmt { public: InsertStmt() = default; InsertStmt(Table *table, const Value *values, int value_amount); - StmtType type() const override - { - return StmtType::INSERT; - } + StmtType type() const override { return StmtType::INSERT; } public: static RC create(Db *db, const InsertSqlNode &insert_sql, Stmt *&stmt); public: - Table *table() const - { - return table_; - } - const Value *values() const - { - return values_; - } - int value_amount() const - { - return value_amount_; - } + Table *table() const { return table_; } + const Value *values() const { return values_; } + int value_amount() const { return value_amount_; } private: - Table *table_ = nullptr; - const Value *values_ = nullptr; - int value_amount_ = 0; + Table *table_ = nullptr; + const Value *values_ = nullptr; + int value_amount_ = 0; }; diff --git a/src/observer/sql/stmt/load_data_stmt.cpp b/src/observer/sql/stmt/load_data_stmt.cpp index d17e0bf22..530faa42a 100644 --- a/src/observer/sql/stmt/load_data_stmt.cpp +++ b/src/observer/sql/stmt/load_data_stmt.cpp @@ -12,18 +12,18 @@ See the Mulan PSL v2 for more details. */ // Created by Wangyunlai on 2023/7/12. // -#include #include "sql/stmt/load_data_stmt.h" -#include "common/log/log.h" #include "common/lang/string.h" +#include "common/log/log.h" #include "storage/db/db.h" -#include "storage/table/table.h" +#include using namespace common; RC LoadDataStmt::create(Db *db, const LoadDataSqlNode &load_data, Stmt *&stmt) { RC rc = RC::SUCCESS; + const char *table_name = load_data.relation_name.c_str(); if (is_blank(table_name) || is_blank(load_data.file_name.c_str())) { LOG_WARN("invalid argument. db=%p, table_name=%p, file name=%s", diff --git a/src/observer/sql/stmt/load_data_stmt.h b/src/observer/sql/stmt/load_data_stmt.h index 496e212fc..9904e82c1 100644 --- a/src/observer/sql/stmt/load_data_stmt.h +++ b/src/observer/sql/stmt/load_data_stmt.h @@ -15,7 +15,6 @@ See the Mulan PSL v2 for more details. */ #pragma once #include -#include #include "sql/stmt/stmt.h" @@ -24,18 +23,17 @@ class Table; class LoadDataStmt : public Stmt { public: - LoadDataStmt(Table *table, const char *filename) : table_(table), filename_(filename) - {} + LoadDataStmt(Table *table, const char *filename) : table_(table), filename_(filename) {} virtual ~LoadDataStmt() = default; StmtType type() const override { return StmtType::LOAD_DATA; } - Table *table() const { return table_; } + Table *table() const { return table_; } const char *filename() const { return filename_.c_str(); } static RC create(Db *db, const LoadDataSqlNode &load_data, Stmt *&stmt); private: - Table *table_ = nullptr; + Table *table_ = nullptr; std::string filename_; }; diff --git a/src/observer/sql/stmt/select_stmt.cpp b/src/observer/sql/stmt/select_stmt.cpp index 6553ef620..97263ec4e 100644 --- a/src/observer/sql/stmt/select_stmt.cpp +++ b/src/observer/sql/stmt/select_stmt.cpp @@ -13,9 +13,9 @@ See the Mulan PSL v2 for more details. */ // #include "sql/stmt/select_stmt.h" -#include "sql/stmt/filter_stmt.h" -#include "common/log/log.h" #include "common/lang/string.h" +#include "common/log/log.h" +#include "sql/stmt/filter_stmt.h" #include "storage/db/db.h" #include "storage/table/table.h" @@ -30,7 +30,7 @@ SelectStmt::~SelectStmt() static void wildcard_fields(Table *table, std::vector &field_metas) { const TableMeta &table_meta = table->table_meta(); - const int field_num = table_meta.field_num(); + const int field_num = table_meta.field_num(); for (int i = table_meta.sys_field_num(); i < field_num; i++) { field_metas.push_back(Field(table, table_meta.field(i))); } @@ -44,7 +44,7 @@ RC SelectStmt::create(Db *db, const SelectSqlNode &select_sql, Stmt *&stmt) } // collect tables in `from` statement - std::vector
tables; + std::vector
tables; std::unordered_map table_map; for (size_t i = 0; i < select_sql.relations.size(); i++) { const char *table_name = select_sql.relations[i].c_str(); @@ -112,7 +112,7 @@ RC SelectStmt::create(Db *db, const SelectSqlNode &select_sql, Stmt *&stmt) return RC::SCHEMA_FIELD_MISSING; } - Table *table = tables[0]; + Table *table = tables[0]; const FieldMeta *field_meta = table->table_meta().field(relation_attr.attribute_name.c_str()); if (nullptr == field_meta) { LOG_WARN("no such field. field=%s.%s.%s", db->name(), table->name(), relation_attr.attribute_name.c_str()); @@ -132,7 +132,7 @@ RC SelectStmt::create(Db *db, const SelectSqlNode &select_sql, Stmt *&stmt) // create filter statement in `where` statement FilterStmt *filter_stmt = nullptr; - RC rc = FilterStmt::create(db, + RC rc = FilterStmt::create(db, default_table, &table_map, select_sql.conditions.data(), @@ -149,6 +149,6 @@ RC SelectStmt::create(Db *db, const SelectSqlNode &select_sql, Stmt *&stmt) select_stmt->tables_.swap(tables); select_stmt->query_fields_.swap(query_fields); select_stmt->filter_stmt_ = filter_stmt; - stmt = select_stmt; + stmt = select_stmt; return RC::SUCCESS; } diff --git a/src/observer/sql/stmt/select_stmt.h b/src/observer/sql/stmt/select_stmt.h index ab84c82c2..5e30ac6ba 100644 --- a/src/observer/sql/stmt/select_stmt.h +++ b/src/observer/sql/stmt/select_stmt.h @@ -14,8 +14,8 @@ See the Mulan PSL v2 for more details. */ #pragma once -#include #include +#include #include "common/rc.h" #include "sql/stmt/stmt.h" @@ -30,36 +30,24 @@ class Table; * @brief 表示select语句 * @ingroup Statement */ -class SelectStmt : public Stmt +class SelectStmt : public Stmt { public: SelectStmt() = default; ~SelectStmt() override; - StmtType type() const override - { - return StmtType::SELECT; - } + StmtType type() const override { return StmtType::SELECT; } public: static RC create(Db *db, const SelectSqlNode &select_sql, Stmt *&stmt); public: - const std::vector
&tables() const - { - return tables_; - } - const std::vector &query_fields() const - { - return query_fields_; - } - FilterStmt *filter_stmt() const - { - return filter_stmt_; - } + const std::vector
&tables() const { return tables_; } + const std::vector &query_fields() const { return query_fields_; } + FilterStmt *filter_stmt() const { return filter_stmt_; } private: - std::vector query_fields_; + std::vector query_fields_; std::vector
tables_; - FilterStmt *filter_stmt_ = nullptr; + FilterStmt *filter_stmt_ = nullptr; }; diff --git a/src/observer/sql/stmt/set_variable_stmt.h b/src/observer/sql/stmt/set_variable_stmt.h index e6d2cde2d..93eb82aaa 100644 --- a/src/observer/sql/stmt/set_variable_stmt.h +++ b/src/observer/sql/stmt/set_variable_stmt.h @@ -26,15 +26,14 @@ See the Mulan PSL v2 for more details. */ class SetVariableStmt : public Stmt { public: - SetVariableStmt(const SetVariableSqlNode &set_variable) : set_variable_(set_variable) - {} + SetVariableStmt(const SetVariableSqlNode &set_variable) : set_variable_(set_variable) {} virtual ~SetVariableStmt() = default; StmtType type() const override { return StmtType::SET_VARIABLE; } - const char *var_name() const { return set_variable_.name.c_str(); } + const char *var_name() const { return set_variable_.name.c_str(); } const Value &var_value() const { return set_variable_.value; } - + static RC create(const SetVariableSqlNode &set_variable, Stmt *&stmt) { /// 可以校验是否存在某个变量,但是这里忽略 diff --git a/src/observer/sql/stmt/show_tables_stmt.h b/src/observer/sql/stmt/show_tables_stmt.h index 62ffdd6c2..601a1d568 100644 --- a/src/observer/sql/stmt/show_tables_stmt.h +++ b/src/observer/sql/stmt/show_tables_stmt.h @@ -29,7 +29,7 @@ class Db; class ShowTablesStmt : public Stmt { public: - ShowTablesStmt() = default; + ShowTablesStmt() = default; virtual ~ShowTablesStmt() = default; StmtType type() const override { return StmtType::SHOW_TABLES; } diff --git a/src/observer/sql/stmt/stmt.cpp b/src/observer/sql/stmt/stmt.cpp index 7aa421d23..9e12e909a 100644 --- a/src/observer/sql/stmt/stmt.cpp +++ b/src/observer/sql/stmt/stmt.cpp @@ -12,23 +12,23 @@ See the Mulan PSL v2 for more details. */ // Created by Wangyunlai on 2022/5/22. // -#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 "common/log/log.h" +#include "sql/stmt/calc_stmt.h" #include "sql/stmt/create_index_stmt.h" #include "sql/stmt/create_table_stmt.h" +#include "sql/stmt/delete_stmt.h" #include "sql/stmt/desc_table_stmt.h" +#include "sql/stmt/exit_stmt.h" +#include "sql/stmt/explain_stmt.h" #include "sql/stmt/help_stmt.h" +#include "sql/stmt/insert_stmt.h" +#include "sql/stmt/load_data_stmt.h" +#include "sql/stmt/select_stmt.h" +#include "sql/stmt/set_variable_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" -#include "sql/stmt/set_variable_stmt.h" -#include "sql/stmt/load_data_stmt.h" -#include "sql/stmt/calc_stmt.h" RC Stmt::create_stmt(Db *db, ParsedSqlNode &sql_node, Stmt *&stmt) { diff --git a/src/observer/sql/stmt/stmt.h b/src/observer/sql/stmt/stmt.h index 52d4bfbec..ed8453049 100644 --- a/src/observer/sql/stmt/stmt.h +++ b/src/observer/sql/stmt/stmt.h @@ -27,43 +27,45 @@ class Db; /** * @brief Statement的类型 - * + * */ -#define DEFINE_ENUM() \ - DEFINE_ENUM_ITEM(CALC) \ - 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) \ +#define DEFINE_ENUM() \ + DEFINE_ENUM_ITEM(CALC) \ + 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) \ DEFINE_ENUM_ITEM(SET_VARIABLE) -enum class StmtType { - #define DEFINE_ENUM_ITEM(name) name, +enum class StmtType +{ +#define DEFINE_ENUM_ITEM(name) name, DEFINE_ENUM() - #undef DEFINE_ENUM_ITEM +#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 DEFINE_ENUM_ITEM(name) \ + case StmtType::name: return #name; DEFINE_ENUM() - #undef DEFINE_ENUM_ITEM +#undef DEFINE_ENUM_ITEM default: return "unkown"; } } @@ -77,7 +79,7 @@ inline const char *stmt_type_name(StmtType type) class Stmt { public: - Stmt() = default; + Stmt() = default; virtual ~Stmt() = default; virtual StmtType type() const = 0; diff --git a/src/observer/sql/stmt/trx_begin_stmt.h b/src/observer/sql/stmt/trx_begin_stmt.h index 555ce0ca7..afdfaf104 100644 --- a/src/observer/sql/stmt/trx_begin_stmt.h +++ b/src/observer/sql/stmt/trx_begin_stmt.h @@ -26,8 +26,7 @@ See the Mulan PSL v2 for more details. */ class TrxBeginStmt : public Stmt { public: - TrxBeginStmt() - {} + TrxBeginStmt() {} virtual ~TrxBeginStmt() = default; StmtType type() const override { return StmtType::BEGIN; } diff --git a/src/observer/sql/stmt/trx_end_stmt.h b/src/observer/sql/stmt/trx_end_stmt.h index 476b60e59..30454ecf6 100644 --- a/src/observer/sql/stmt/trx_end_stmt.h +++ b/src/observer/sql/stmt/trx_end_stmt.h @@ -26,9 +26,7 @@ See the Mulan PSL v2 for more details. */ class TrxEndStmt : public Stmt { public: - TrxEndStmt(StmtType type) - : type_(type) - {} + TrxEndStmt(StmtType type) : type_(type) {} virtual ~TrxEndStmt() = default; StmtType type() const override { return type_; } @@ -36,7 +34,7 @@ class TrxEndStmt : public Stmt static RC create(SqlCommandFlag flag, Stmt *&stmt) { StmtType type = flag == SqlCommandFlag::SCF_COMMIT ? StmtType::COMMIT : StmtType::ROLLBACK; - stmt = new TrxEndStmt(type); + stmt = new TrxEndStmt(type); return RC::SUCCESS; } diff --git a/src/observer/sql/stmt/update_stmt.h b/src/observer/sql/stmt/update_stmt.h index 40da79230..762a80193 100644 --- a/src/observer/sql/stmt/update_stmt.h +++ b/src/observer/sql/stmt/update_stmt.h @@ -23,7 +23,7 @@ class Table; * @brief 更新语句 * @ingroup Statement */ -class UpdateStmt : public Stmt +class UpdateStmt : public Stmt { public: UpdateStmt() = default; @@ -33,21 +33,12 @@ class UpdateStmt : public Stmt static RC create(Db *db, const UpdateSqlNode &update_sql, Stmt *&stmt); public: - Table *table() const - { - return table_; - } - Value *values() const - { - return values_; - } - int value_amount() const - { - return value_amount_; - } + Table *table() const { return table_; } + Value *values() const { return values_; } + int value_amount() const { return value_amount_; } private: - Table *table_ = nullptr; - Value *values_ = nullptr; - int value_amount_ = 0; + Table *table_ = nullptr; + Value *values_ = nullptr; + int value_amount_ = 0; }; diff --git a/src/observer/storage/buffer/disk_buffer_pool.cpp b/src/observer/storage/buffer/disk_buffer_pool.cpp index 5c2e4a72d..62c3fd37a 100644 --- a/src/observer/storage/buffer/disk_buffer_pool.cpp +++ b/src/observer/storage/buffer/disk_buffer_pool.cpp @@ -14,11 +14,10 @@ See the Mulan PSL v2 for more details. */ #include #include -#include "storage/buffer/disk_buffer_pool.h" +#include "common/io/io.h" #include "common/lang/mutex.h" #include "common/log/log.h" -#include "common/os/os.h" -#include "common/io/io.h" +#include "storage/buffer/disk_buffer_pool.h" using namespace common; using namespace std; @@ -30,15 +29,13 @@ static const int MEM_POOL_ITEM_NUM = 20; string BPFileHeader::to_string() const { stringstream ss; - ss << "pageCount:" << page_count - << ", allocatedCount:" << allocated_pages; + ss << "pageCount:" << page_count << ", allocatedCount:" << allocated_pages; return ss.str(); } //////////////////////////////////////////////////////////////////////////////// -BPFrameManager::BPFrameManager(const char *name) : allocator_(name) -{} +BPFrameManager::BPFrameManager(const char *name) : allocator_(name) {} RC BPFrameManager::init(int pool_num) { @@ -103,7 +100,7 @@ int BPFrameManager::purge_frames(int count, std::function purg Frame *BPFrameManager::get(int file_desc, PageNum page_num) { - FrameId frame_id(file_desc, page_num); + FrameId frame_id(file_desc, page_num); std::lock_guard lock_guard(lock_); return get_internal(frame_id); } @@ -123,15 +120,15 @@ Frame *BPFrameManager::alloc(int file_desc, PageNum page_num) FrameId frame_id(file_desc, page_num); std::lock_guard lock_guard(lock_); - Frame *frame = get_internal(frame_id); + Frame *frame = get_internal(frame_id); if (frame != nullptr) { return frame; } frame = allocator_.alloc(); if (frame != nullptr) { - ASSERT(frame->pin_count() == 0, "got an invalid frame that pin count is not 0. frame=%s", - to_string(*frame).c_str()); + ASSERT( + frame->pin_count() == 0, "got an invalid frame that pin count is not 0. frame=%s", to_string(*frame).c_str()); frame->set_page_num(page_num); frame->pin(); frames_.put(frame_id, frame); @@ -149,11 +146,11 @@ RC BPFrameManager::free(int file_desc, PageNum page_num, Frame *frame) RC BPFrameManager::free_internal(const FrameId &frame_id, Frame *frame) { - Frame *frame_source = nullptr; - [[maybe_unused]] bool found = frames_.get(frame_id, frame_source); + Frame *frame_source = nullptr; + [[maybe_unused]] bool found = frames_.get(frame_id, frame_source); ASSERT(found && frame == frame_source && frame->pin_count() == 1, - "failed to free frame. found=%d, frameId=%s, frame_source=%p, frame=%p, pinCount=%d, lbt=%s", - found, to_string(frame_id).c_str(), frame_source, frame, frame->pin_count(), lbt()); + "failed to free frame. found=%d, frameId=%s, frame_source=%p, frame=%p, pinCount=%d, lbt=%s", + found, to_string(frame_id).c_str(), frame_source, frame, frame->pin_count(), lbt()); frame->unpin(); frames_.remove(frame_id); @@ -166,7 +163,7 @@ std::list BPFrameManager::find_list(int file_desc) std::lock_guard lock_guard(lock_); std::list frames; - auto fetcher = [&frames, file_desc](const FrameId &frame_id, Frame *const frame) -> bool { + auto fetcher = [&frames, file_desc](const FrameId &frame_id, Frame *const frame) -> bool { if (file_desc == frame_id.file_desc()) { frame->pin(); frames.push_back(frame); @@ -178,10 +175,8 @@ std::list BPFrameManager::find_list(int file_desc) } //////////////////////////////////////////////////////////////////////////////// -BufferPoolIterator::BufferPoolIterator() -{} -BufferPoolIterator::~BufferPoolIterator() -{} +BufferPoolIterator::BufferPoolIterator() {} +BufferPoolIterator::~BufferPoolIterator() {} RC BufferPoolIterator::init(DiskBufferPool &bp, PageNum start_page /* = 0 */) { bitmap_.init(bp.file_header_->bitmap, bp.file_header_->page_count); @@ -193,10 +188,7 @@ RC BufferPoolIterator::init(DiskBufferPool &bp, PageNum start_page /* = 0 */) return RC::SUCCESS; } -bool BufferPoolIterator::has_next() -{ - return bitmap_.next_setted_bit(current_page_num_ + 1) != -1; -} +bool BufferPoolIterator::has_next() { return bitmap_.next_setted_bit(current_page_num_ + 1) != -1; } PageNum BufferPoolIterator::next() { @@ -237,7 +229,7 @@ RC DiskBufferPool::open_file(const char *file_name) file_desc_ = fd; RC rc = RC::SUCCESS; - rc = allocate_frame(BP_HEADER_PAGE, &hdr_frame_); + rc = allocate_frame(BP_HEADER_PAGE, &hdr_frame_); if (rc != RC::SUCCESS) { LOG_ERROR("failed to allocate frame for header. file name %s", file_name_.c_str()); close(fd); @@ -294,7 +286,7 @@ RC DiskBufferPool::close_file() RC DiskBufferPool::get_this_page(PageNum page_num, Frame **frame) { - RC rc = RC::SUCCESS; + RC rc = RC::SUCCESS; *frame = nullptr; Frame *used_match_frame = frame_manager_.get(file_desc_, page_num); @@ -304,11 +296,11 @@ RC DiskBufferPool::get_this_page(PageNum page_num, Frame **frame) return RC::SUCCESS; } - std::scoped_lock lock_guard(lock_); // 直接加了一把大锁,其实可以根据访问的页面来细化提高并行度 + std::scoped_lock lock_guard(lock_); // 直接加了一把大锁,其实可以根据访问的页面来细化提高并行度 // Allocate one page and load the data into this page Frame *allocated_frame = nullptr; - rc = allocate_frame(page_num, &allocated_frame); + rc = allocate_frame(page_num, &allocated_frame); if (rc != RC::SUCCESS) { LOG_ERROR("Failed to alloc frame %s:%d, due to failed to alloc page.", file_name_.c_str(), page_num); return rc; @@ -333,13 +325,13 @@ RC DiskBufferPool::allocate_page(Frame **frame) RC rc = RC::SUCCESS; lock_.lock(); - + int byte = 0, bit = 0; if ((file_header_->allocated_pages) < (file_header_->page_count)) { // There is one free page for (int i = 0; i < file_header_->page_count; i++) { byte = i / 8; - bit = i % 8; + bit = i % 8; if (((file_header_->bitmap[byte]) & (1 << bit)) == 0) { (file_header_->allocated_pages)++; file_header_->bitmap[byte] |= (1 << bit); @@ -359,8 +351,8 @@ RC DiskBufferPool::allocate_page(Frame **frame) return RC::BUFFERPOOL_NOBUF; } - PageNum page_num = file_header_->page_count; - Frame *allocated_frame = nullptr; + PageNum page_num = file_header_->page_count; + Frame *allocated_frame = nullptr; if ((rc = allocate_frame(page_num, &allocated_frame)) != RC::SUCCESS) { LOG_ERROR("Failed to allocate frame %s, due to no free page.", file_name_.c_str()); lock_.unlock(); @@ -374,7 +366,7 @@ RC DiskBufferPool::allocate_page(Frame **frame) file_header_->page_count++; byte = page_num / 8; - bit = page_num % 8; + bit = page_num % 8; file_header_->bitmap[byte] |= (1 << bit); hdr_frame_->mark_dirty(); @@ -399,7 +391,7 @@ RC DiskBufferPool::allocate_page(Frame **frame) RC DiskBufferPool::dispose_page(PageNum page_num) { std::scoped_lock lock_guard(lock_); - Frame *used_frame = frame_manager_.get(file_desc_, page_num); + Frame *used_frame = frame_manager_.get(file_desc_, page_num); if (used_frame != nullptr) { ASSERT("the page try to dispose is in use. frame:%s", to_string(*used_frame).c_str()); frame_manager_.free(file_desc_, page_num, used_frame); @@ -445,7 +437,7 @@ RC DiskBufferPool::purge_frame(PageNum page_num, Frame *buf) RC DiskBufferPool::purge_page(PageNum page_num) { std::scoped_lock lock_guard(lock_); - Frame *used_frame = frame_manager_.get(file_desc_, page_num); + Frame *used_frame = frame_manager_.get(file_desc_, page_num); if (used_frame != nullptr) { return purge_frame(page_num, used_frame); } @@ -496,7 +488,7 @@ RC DiskBufferPool::flush_page_internal(Frame &frame) // The better way is use mmap the block into memory, // so it is easier to flush data to file. - Page &page = frame.page(); + Page &page = frame.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)); @@ -530,7 +522,7 @@ RC DiskBufferPool::recover_page(PageNum page_num) { int byte = 0, bit = 0; byte = page_num / 8; - bit = page_num % 8; + bit = page_num % 8; std::scoped_lock lock_guard(lock_); if (!(file_header_->bitmap[byte] & (1 << bit))) { @@ -570,7 +562,7 @@ RC DiskBufferPool::allocate_frame(PageNum page_num, Frame **buffer) } LOG_TRACE("frames are all allocated, so we should purge some frames to get one free frame"); - (void)frame_manager_.purge_frames(1/*count*/, purger); + (void)frame_manager_.purge_frames(1 /*count*/, purger); } return RC::BUFFERPOOL_NOBUF; } @@ -598,7 +590,7 @@ RC DiskBufferPool::load_page(PageNum page_num, Frame *frame) } Page &page = frame->page(); - int ret = readn(file_desc_, &page, BP_PAGE_SIZE); + int ret = readn(file_desc_, &page, BP_PAGE_SIZE); if (ret != 0) { LOG_ERROR("Failed to load page %s, file_desc:%d, page num:%d, due to failed to read data:%s, ret=%d, page count=%d", file_name_.c_str(), file_desc_, page_num, strerror(errno), ret, file_header_->allocated_pages); @@ -607,10 +599,7 @@ RC DiskBufferPool::load_page(PageNum page_num, Frame *frame) return RC::SUCCESS; } -int DiskBufferPool::file_desc() const -{ - return file_desc_; -} +int DiskBufferPool::file_desc() const { return file_desc_; } //////////////////////////////////////////////////////////////////////////////// BufferPoolManager::BufferPoolManager(int memory_size /* = 0 */) { @@ -655,9 +644,9 @@ RC BufferPoolManager::create_file(const char *file_name) Page page; memset(&page, 0, BP_PAGE_SIZE); - BPFileHeader *file_header = (BPFileHeader *)page.data; + BPFileHeader *file_header = (BPFileHeader *)page.data; file_header->allocated_pages = 1; - file_header->page_count = 1; + file_header->page_count = 1; char *bitmap = file_header->bitmap; bitmap[0] |= 0x01; @@ -689,7 +678,7 @@ RC BufferPoolManager::open_file(const char *_file_name, DiskBufferPool *&_bp) } DiskBufferPool *bp = new DiskBufferPool(*this, frame_manager_); - RC rc = bp->open_file(_file_name); + RC rc = bp->open_file(_file_name); if (rc != RC::SUCCESS) { LOG_WARN("failed to open file name"); delete bp; @@ -732,7 +721,7 @@ RC BufferPoolManager::close_file(const char *_file_name) DiskBufferPool *bp = iter->second; buffer_pools_.erase(iter); lock_.unlock(); - + delete bp; return RC::SUCCESS; } @@ -742,7 +731,7 @@ RC BufferPoolManager::flush_page(Frame &frame) int fd = frame.file_desc(); std::scoped_lock lock_guard(lock_); - auto iter = fd_buffer_pools_.find(fd); + auto iter = fd_buffer_pools_.find(fd); if (iter == fd_buffer_pools_.end()) { LOG_WARN("unknown buffer pool of fd %d", fd); return RC::INTERNAL; @@ -753,7 +742,7 @@ RC BufferPoolManager::flush_page(Frame &frame) } static BufferPoolManager *default_bpm = nullptr; -void BufferPoolManager::set_instance(BufferPoolManager *bpm) +void BufferPoolManager::set_instance(BufferPoolManager *bpm) { if (default_bpm != nullptr && bpm != nullptr) { LOG_ERROR("default buffer pool manager has been setted"); @@ -761,7 +750,4 @@ void BufferPoolManager::set_instance(BufferPoolManager *bpm) } default_bpm = bpm; } -BufferPoolManager &BufferPoolManager::instance() -{ - return *default_bpm; -} +BufferPoolManager &BufferPoolManager::instance() { return *default_bpm; } diff --git a/src/observer/storage/buffer/disk_buffer_pool.h b/src/observer/storage/buffer/disk_buffer_pool.h index 4b71787bf..60a03e6f3 100644 --- a/src/observer/storage/buffer/disk_buffer_pool.h +++ b/src/observer/storage/buffer/disk_buffer_pool.h @@ -13,26 +13,26 @@ See the Mulan PSL v2 for more details. */ // #pragma once -#include -#include #include +#include +#include #include #include #include -#include #include -#include +#include +#include +#include #include -#include -#include "common/rc.h" -#include "common/types.h" +#include "common/lang/bitmap.h" +#include "common/lang/lru_cache.h" #include "common/lang/mutex.h" #include "common/mm/mem_pool.h" -#include "common/lang/lru_cache.h" -#include "common/lang/bitmap.h" -#include "storage/buffer/page.h" +#include "common/rc.h" +#include "common/types.h" #include "storage/buffer/frame.h" +#include "storage/buffer/page.h" class BufferPoolManager; class DiskBufferPool; @@ -55,11 +55,11 @@ class DiskBufferPool; * 效率非常低,你有办法优化吗? * @endcode */ -struct BPFileHeader +struct BPFileHeader { int32_t page_count; //! 当前文件一共有多少个页面 int32_t allocated_pages; //! 已经分配了多少个页面 - char bitmap[0]; //! 页面分配位图, 第0个页面(就是当前页面),总是1 + char bitmap[0]; //! 页面分配位图, 第0个页面(就是当前页面),总是1 /** * 能够分配的最大的页面个数,即bitmap的字节数 乘以8 @@ -77,7 +77,7 @@ struct BPFileHeader * 这个管理器负责为所有的BufferPool提供页帧管理服务,也就是所有的BufferPool磁盘文件 * 在访问时都使用这个管理器映射到内存。 */ -class BPFrameManager +class BPFrameManager { public: BPFrameManager(const char *tag); @@ -87,7 +87,7 @@ class BPFrameManager /** * @brief 获取指定的页面 - * + * * @param file_desc 文件描述符,也可以当做buffer pool文件的标识 * @param page_num 页面号 * @return Frame* 页帧指针 @@ -96,7 +96,7 @@ class BPFrameManager /** * @brief 列出所有指定文件的页面 - * + * * @param file_desc 文件描述符 * @return std::list 页帧列表 */ @@ -104,7 +104,7 @@ class BPFrameManager /** * @brief 分配一个新的页面 - * + * * @param file_desc 文件描述符 * @param page_num 页面编号 * @return Frame* 页帧指针 @@ -126,36 +126,28 @@ class BPFrameManager */ int purge_frames(int count, std::function purger); - size_t frame_num() const - { - return frames_.count(); - } + size_t frame_num() const { return frames_.count(); } /** * 测试使用。返回已经从内存申请的个数 */ - size_t total_frame_num() const - { - return allocator_.get_size(); - } + size_t total_frame_num() const { return allocator_.get_size(); } private: Frame *get_internal(const FrameId &frame_id); RC free_internal(const FrameId &frame_id, Frame *frame); private: - class BPFrameIdHasher { + class BPFrameIdHasher + { public: - size_t operator()(const FrameId &frame_id) const - { - return frame_id.hash(); - } + size_t operator()(const FrameId &frame_id) const { return frame_id.hash(); } }; - using FrameLruCache = common::LruCache; + using FrameLruCache = common::LruCache; using FrameAllocator = common::MemPoolSimple; - std::mutex lock_; + std::mutex lock_; FrameLruCache frames_; FrameAllocator allocator_; }; @@ -170,21 +162,21 @@ class BufferPoolIterator BufferPoolIterator(); ~BufferPoolIterator(); - RC init(DiskBufferPool &bp, PageNum start_page = 0); - bool has_next(); + RC init(DiskBufferPool &bp, PageNum start_page = 0); + bool has_next(); PageNum next(); - RC reset(); + RC reset(); private: common::Bitmap bitmap_; - PageNum current_page_num_ = -1; + PageNum current_page_num_ = -1; }; /** * @brief BufferPool的实现 * @ingroup BufferPool */ -class DiskBufferPool +class DiskBufferPool { public: DiskBufferPool(BufferPoolManager &bp_manager, BPFrameManager &frame_manager); @@ -219,7 +211,7 @@ class DiskBufferPool /** * @brief 释放某个页面,将此页面设置为未分配状态 - * + * * @param page_num 待释放的页面 */ RC dispose_page(PageNum page_num); @@ -233,7 +225,7 @@ class DiskBufferPool /** * @brief 用于解除pageHandle对应页面的驻留缓冲区限制 - * + * * 在调用GetThisPage或AllocatePage函数将一个页面读入缓冲区后, * 该页面被设置为驻留缓冲区状态,以防止其在处理过程中被置换出去, * 因此在该页面使用完之后应调用此函数解除该限制,使得该页面此后可以正常地被淘汰出缓冲区 @@ -283,16 +275,17 @@ class DiskBufferPool RC flush_page_internal(Frame &frame); private: - BufferPoolManager & bp_manager_; - BPFrameManager & frame_manager_; + BufferPoolManager &bp_manager_; + BPFrameManager &frame_manager_; + + std::string file_name_; + int file_desc_ = -1; + Frame *hdr_frame_ = nullptr; + BPFileHeader *file_header_ = nullptr; + std::set disposed_pages_; - std::string file_name_; - int file_desc_ = -1; - Frame * hdr_frame_ = nullptr; - BPFileHeader * file_header_ = nullptr; - std::set disposed_pages_; + common::Mutex lock_; - common::Mutex lock_; private: friend class BufferPoolIterator; }; @@ -301,7 +294,7 @@ class DiskBufferPool * @brief BufferPool的管理类 * @ingroup BufferPool */ -class BufferPoolManager +class BufferPoolManager { public: BufferPoolManager(int memory_size = 0); @@ -314,13 +307,13 @@ class BufferPoolManager RC flush_page(Frame &frame); public: - static void set_instance(BufferPoolManager *bpm); // TODO 优化全局变量的表示方法 + static void set_instance(BufferPoolManager *bpm); // TODO 优化全局变量的表示方法 static BufferPoolManager &instance(); private: BPFrameManager frame_manager_{"BufPool"}; - common::Mutex lock_; + common::Mutex lock_; std::unordered_map buffer_pools_; - std::unordered_map fd_buffer_pools_; + std::unordered_map fd_buffer_pools_; }; diff --git a/src/observer/storage/buffer/frame.cpp b/src/observer/storage/buffer/frame.cpp index bd42dad3e..157611489 100644 --- a/src/observer/storage/buffer/frame.cpp +++ b/src/observer/storage/buffer/frame.cpp @@ -13,37 +13,24 @@ See the Mulan PSL v2 for more details. */ // #include "storage/buffer/frame.h" -#include "session/thread_data.h" #include "session/session.h" +#include "session/thread_data.h" using namespace std; -FrameId::FrameId(int file_desc, PageNum page_num) : file_desc_(file_desc), page_num_(page_num) -{} +FrameId::FrameId(int file_desc, PageNum page_num) : file_desc_(file_desc), page_num_(page_num) {} bool FrameId::equal_to(const FrameId &other) const { return file_desc_ == other.file_desc_ && page_num_ == other.page_num_; } -bool FrameId::operator==(const FrameId &other) const -{ - return this->equal_to(other); -} +bool FrameId::operator==(const FrameId &other) const { return this->equal_to(other); } -size_t FrameId::hash() const -{ - return (static_cast(file_desc_) << 32L) | page_num_; -} +size_t FrameId::hash() const { return (static_cast(file_desc_) << 32L) | page_num_; } -int FrameId::file_desc() const -{ - return file_desc_; -} -PageNum FrameId::page_num() const -{ - return page_num_; -} +int FrameId::file_desc() const { return file_desc_; } +PageNum FrameId::page_num() const { return page_num_; } string to_string(const FrameId &frame_id) { @@ -55,7 +42,7 @@ string to_string(const FrameId &frame_id) //////////////////////////////////////////////////////////////////////////////// intptr_t get_default_debug_xid() { - #if 0 +#if 0 ThreadData *thd = ThreadData::current(); intptr_t xid = (thd == nullptr) ? // pthread_self的返回值类型是pthread_t,pthread_t在linux和mac上不同 @@ -63,33 +50,30 @@ intptr_t get_default_debug_xid() // 就将pthread_self返回值转换两次 reinterpret_cast(reinterpret_cast(pthread_self())) : reinterpret_cast(thd); - #endif +#endif Session *session = Session::current_session(); if (session == nullptr) { - return reinterpret_cast(reinterpret_cast(pthread_self())); + return reinterpret_cast(reinterpret_cast(pthread_self())); } else { return reinterpret_cast(session); } } -void Frame::write_latch() -{ - write_latch(get_default_debug_xid()); -} +void Frame::write_latch() { write_latch(get_default_debug_xid()); } void Frame::write_latch(intptr_t xid) { { scoped_lock debug_lock(debug_lock_); ASSERT(pin_count_.load() > 0, - "frame lock. write lock failed while pin count is invalid. " - "this=%p, pin=%d, pageNum=%d, fd=%d, xid=%lx, lbt=%s", - this, pin_count_.load(), page_.page_num, file_desc_, xid, lbt()); + "frame lock. write lock failed while pin count is invalid. " + "this=%p, pin=%d, pageNum=%d, fd=%d, xid=%lx, lbt=%s", + this, pin_count_.load(), page_.page_num, file_desc_, xid, lbt()); ASSERT(read_lockers_.find(xid) == read_lockers_.end(), - "frame lock write while holding the read lock." - "this=%p, pin=%d, pageNum=%d, fd=%d, xid=%lx, lbt=%s", - this, pin_count_.load(), page_.page_num, file_desc_, xid, lbt()); + "frame lock write while holding the read lock." + "this=%p, pin=%d, pageNum=%d, fd=%d, xid=%lx, lbt=%s", + this, pin_count_.load(), page_.page_num, file_desc_, xid, lbt()); } lock_.lock(); @@ -101,25 +85,22 @@ void Frame::write_latch(intptr_t xid) this, pin_count_.load(), page_.page_num, write_locker_, write_recursive_count_, file_desc_, xid, lbt()); } -void Frame::write_unlatch() -{ - write_unlatch(get_default_debug_xid()); -} +void Frame::write_unlatch() { write_unlatch(get_default_debug_xid()); } void Frame::write_unlatch(intptr_t xid) { // 因为当前已经加着写锁,而且写锁只有一个,所以不再加debug_lock来做校验 debug_lock_.lock(); - ASSERT(pin_count_.load() > 0, - "frame lock. write unlock failed while pin count is invalid." - "this=%p, pin=%d, pageNum=%d, fd=%d, xid=%lx, lbt=%s", - this, pin_count_.load(), page_.page_num, file_desc_, xid, lbt()); + ASSERT(pin_count_.load() > 0, + "frame lock. write unlock failed while pin count is invalid." + "this=%p, pin=%d, pageNum=%d, fd=%d, xid=%lx, lbt=%s", + this, pin_count_.load(), page_.page_num, file_desc_, xid, lbt()); ASSERT(write_locker_ == xid, - "frame unlock write while not the owner." - "write_locker=%lx, this=%p, pin=%d, pageNum=%d, fd=%d, xid=%lx, lbt=%s", - write_locker_, this, pin_count_.load(), page_.page_num, file_desc_, xid, lbt()); + "frame unlock write while not the owner." + "write_locker=%lx, this=%p, pin=%d, pageNum=%d, fd=%d, xid=%lx, lbt=%s", + write_locker_, this, pin_count_.load(), page_.page_num, file_desc_, xid, lbt()); LOG_DEBUG("frame write unlock success. this=%p, pin=%d, pageNum=%d, fd=%d, xid=%lx, lbt=%s", this, pin_count_.load(), page_.page_num, file_desc_, xid, lbt()); @@ -128,34 +109,32 @@ void Frame::write_unlatch(intptr_t xid) write_locker_ = 0; } debug_lock_.unlock(); - + lock_.unlock(); } -void Frame::read_latch() -{ - read_latch(get_default_debug_xid()); -} +void Frame::read_latch() { read_latch(get_default_debug_xid()); } -void Frame::read_latch(intptr_t xid) +void Frame::read_latch(intptr_t xid) { { std::scoped_lock debug_lock(debug_lock_); - ASSERT(pin_count_ > 0, "frame lock. read lock failed while pin count is invalid." - "this=%p, pin=%d, pageNum=%d, fd=%d, xid=%lx, lbt=%s", - this, pin_count_.load(), page_.page_num, file_desc_, xid, lbt()); + ASSERT(pin_count_ > 0, + "frame lock. read lock failed while pin count is invalid." + "this=%p, pin=%d, pageNum=%d, fd=%d, xid=%lx, lbt=%s", + this, pin_count_.load(), page_.page_num, file_desc_, xid, lbt()); ASSERT(xid != write_locker_, - "frame lock read while holding the write lock." - "this=%p, pin=%d, pageNum=%d, fd=%d, xid=%lx, lbt=%s", - this, pin_count_.load(), page_.page_num, file_desc_, xid, lbt()); + "frame lock read while holding the write lock." + "this=%p, pin=%d, pageNum=%d, fd=%d, xid=%lx, lbt=%s", + this, pin_count_.load(), page_.page_num, file_desc_, xid, lbt()); } lock_.lock_shared(); { scoped_lock debug_lock(debug_lock_); - int recursive_count = ++read_lockers_[xid]; + int recursive_count = ++read_lockers_[xid]; LOG_DEBUG("frame read lock success." "this=%p, pin=%d, pageNum=%d, fd=%d, xid=%lx, recursive=%d, lbt=%s", this, pin_count_.load(), page_.page_num, file_desc_, xid, recursive_count, lbt()); @@ -167,14 +146,15 @@ bool Frame::try_read_latch() intptr_t xid = get_default_debug_xid(); { std::scoped_lock debug_lock(debug_lock_); - ASSERT(pin_count_ > 0, "frame try lock. read lock failed while pin count is invalid." - "this=%p, pin=%d, pageNum=%d, fd=%d, xid=%lx, lbt=%s", - this, pin_count_.load(), page_.page_num, file_desc_, xid, lbt()); + ASSERT(pin_count_ > 0, + "frame try lock. read lock failed while pin count is invalid." + "this=%p, pin=%d, pageNum=%d, fd=%d, xid=%lx, lbt=%s", + this, pin_count_.load(), page_.page_num, file_desc_, xid, lbt()); ASSERT(xid != write_locker_, - "frame try to lock read while holding the write lock." - "this=%p, pin=%d, pageNum=%d, fd=%d, xid=%lx, lbt=%s", - this, pin_count_.load(), page_.page_num, file_desc_, xid, lbt()); + "frame try to lock read while holding the write lock." + "this=%p, pin=%d, pageNum=%d, fd=%d, xid=%lx, lbt=%s", + this, pin_count_.load(), page_.page_num, file_desc_, xid, lbt()); } bool ret = lock_.try_lock_shared(); @@ -190,34 +170,30 @@ bool Frame::try_read_latch() return ret; } -void Frame::read_unlatch() -{ - read_unlatch(get_default_debug_xid()); -} +void Frame::read_unlatch() { read_unlatch(get_default_debug_xid()); } void Frame::read_unlatch(intptr_t xid) { { std::scoped_lock debug_lock(debug_lock_); ASSERT(pin_count_.load() > 0, - "frame lock. read unlock failed while pin count is invalid." - "this=%p, pin=%d, pageNum=%d, fd=%d, xid=%lx, lbt=%s", - this, pin_count_.load(), page_.page_num, file_desc_, xid, lbt()); + "frame lock. read unlock failed while pin count is invalid." + "this=%p, pin=%d, pageNum=%d, fd=%d, xid=%lx, lbt=%s", + this, pin_count_.load(), page_.page_num, file_desc_, xid, lbt()); #if DEBUG - auto read_lock_iter = read_lockers_.find(xid); - int recursive_count = read_lock_iter != read_lockers_.end() ? read_lock_iter->second : 0; + auto read_lock_iter = read_lockers_.find(xid); + int recursive_count = read_lock_iter != read_lockers_.end() ? read_lock_iter->second : 0; ASSERT(recursive_count > 0, - "frame unlock while not holding read lock." - "this=%p, pin=%d, pageNum=%d, fd=%d, xid=%lx, recursive=%d, lbt=%s", - this, pin_count_.load(), page_.page_num, file_desc_, xid, recursive_count, lbt()); + "frame unlock while not holding read lock." + "this=%p, pin=%d, pageNum=%d, fd=%d, xid=%lx, recursive=%d, lbt=%s", + this, pin_count_.load(), page_.page_num, file_desc_, xid, recursive_count, lbt()); if (1 == recursive_count) { read_lockers_.erase(xid); } -#endif // DEBUG - +#endif // DEBUG } LOG_DEBUG("frame read unlock success." @@ -231,8 +207,8 @@ void Frame::pin() { std::scoped_lock debug_lock(debug_lock_); - intptr_t xid = get_default_debug_xid(); - int pin_count = ++pin_count_; + intptr_t xid = get_default_debug_xid(); + int pin_count = ++pin_count_; LOG_DEBUG("after frame pin. " "this=%p, write locker=%lx, read locker has xid %d? pin=%d, fd=%d, pageNum=%d, xid=%lx, lbt=%s", @@ -245,10 +221,10 @@ int Frame::unpin() intptr_t xid = get_default_debug_xid(); ASSERT(pin_count_.load() > 0, - "try to unpin a frame that pin count <= 0." - "this=%p, pin=%d, pageNum=%d, fd=%d, xid=%lx, lbt=%s", - this, pin_count_.load(), page_.page_num, file_desc_, xid, lbt()); - + "try to unpin a frame that pin count <= 0." + "this=%p, pin=%d, pageNum=%d, fd=%d, xid=%lx, lbt=%s", + this, pin_count_.load(), page_.page_num, file_desc_, xid, lbt()); + std::scoped_lock debug_lock(debug_lock_); int pin_count = --pin_count_; @@ -257,19 +233,18 @@ int Frame::unpin() "this=%p, write locker=%lx, read locker has xid? %d, pin=%d, fd=%d, pageNum=%d, xid=%lx, lbt=%s", this, write_locker_, read_lockers_.find(xid) != read_lockers_.end(), pin_count, file_desc_, page_.page_num, xid, lbt()); - + if (0 == pin_count) { ASSERT(write_locker_ == 0, - "frame unpin to 0 failed while someone hold the write lock. write locker=%lx, pageNum=%d, fd=%d, xid=%lx", - write_locker_, page_.page_num, file_desc_, xid); + "frame unpin to 0 failed while someone hold the write lock. write locker=%lx, pageNum=%d, fd=%d, xid=%lx", + write_locker_, page_.page_num, file_desc_, xid); ASSERT(read_lockers_.empty(), - "frame unpin to 0 failed while someone hold the read locks. reader num=%d, pageNum=%d, fd=%d, xid=%lx", - read_lockers_.size(), page_.page_num, file_desc_, xid); + "frame unpin to 0 failed while someone hold the read locks. reader num=%d, pageNum=%d, fd=%d, xid=%lx", + read_lockers_.size(), page_.page_num, file_desc_, xid); } return pin_count; } - unsigned long current_time() { struct timespec tp; @@ -277,19 +252,12 @@ unsigned long current_time() return tp.tv_sec * 1000 * 1000 * 1000UL + tp.tv_nsec; } -void Frame::access() -{ - acc_time_ = current_time(); -} +void Frame::access() { acc_time_ = current_time(); } string to_string(const Frame &frame) { stringstream ss; - ss << "frame id:" << to_string(frame.frame_id()) - << ", dirty=" << frame.dirty() - << ", pin=" << frame.pin_count() - << ", fd=" << frame.file_desc() - << ", page num=" << frame.page_num() - << ", lsn=" << frame.lsn(); + ss << "frame id:" << to_string(frame.frame_id()) << ", dirty=" << frame.dirty() << ", pin=" << frame.pin_count() + << ", fd=" << frame.file_desc() << ", page num=" << frame.page_num() << ", lsn=" << frame.lsn(); return ss.str(); } diff --git a/src/observer/storage/buffer/frame.h b/src/observer/storage/buffer/frame.h index 68bcb2eb8..ba5a4f068 100644 --- a/src/observer/storage/buffer/frame.h +++ b/src/observer/storage/buffer/frame.h @@ -14,23 +14,23 @@ See the Mulan PSL v2 for more details. */ #pragma once +#include +#include #include +#include #include #include -#include -#include -#include -#include "storage/buffer/page.h" -#include "common/log/log.h" #include "common/lang/mutex.h" +#include "common/log/log.h" #include "common/types.h" +#include "storage/buffer/page.h" /** * @brief 页帧标识符 * @ingroup BufferPool */ -class FrameId +class FrameId { public: FrameId(int file_desc, PageNum page_num); @@ -41,6 +41,7 @@ class FrameId PageNum page_num() const; friend std::string to_string(const FrameId &frame_id); + private: int file_desc_; PageNum page_num_; @@ -51,10 +52,10 @@ class FrameId * @ingroup BufferPool * @details 页帧是磁盘文件在内存中的表示。磁盘文件按照页面来操作,操作之前先映射到内存中, * 将磁盘数据读取到内存中,也就是页帧。 - * + * * 当某个页面被淘汰时,如果有些内容曾经变更过,那么就需要将这些内容刷新到磁盘上。这里有 * 一个dirty标识,用来标识页面是否被修改过。 - * + * * 为了防止在使用过程中页面被淘汰,这里使用了pin count,当页面被使用时,pin count会增加, * 当页面不再使用时,pin count会减少。当pin count为0时,页面可以被淘汰。 */ @@ -71,19 +72,14 @@ class Frame * @details 在 MemPoolSimple 分配和释放一个Frame对象时,不会调用构造函数和析构函数, * 而是调用reinit和reset。 */ - void reinit() - {} - void reset() - {} - - void clear_page() - { - memset(&page_, 0, sizeof(page_)); - } + void reinit() {} + void reset() {} + + void clear_page() { memset(&page_, 0, sizeof(page_)); } int file_desc() const { return file_desc_; } void set_file_desc(int fd) { file_desc_ = fd; } - Page & page() { return page_; } + Page &page() { return page_; } PageNum page_num() const { return page_.page_num; } void set_page_num(PageNum page_num) { page_.page_num = page_num; } FrameId frame_id() const { return FrameId(file_desc_, page_.page_num); } @@ -115,15 +111,15 @@ class Frame * @brief 释放一个当前页帧的引用计数 * 与pin对应,但是通常不会加着frame manager的锁来访问 */ - int unpin(); - int pin_count() const { return pin_count_.load(); } + int unpin(); + int pin_count() const { return pin_count_.load(); } void write_latch(); void write_latch(intptr_t xid); void write_unlatch(); void write_unlatch(intptr_t xid); - + void read_latch(); void read_latch(intptr_t xid); bool try_read_latch(); @@ -134,22 +130,21 @@ class Frame friend std::string to_string(const Frame &frame); private: - friend class BufferPool; + friend class BufferPool; - bool dirty_ = false; - std::atomic pin_count_{0}; - unsigned long acc_time_ = 0; - int file_desc_ = -1; - Page page_; + bool dirty_ = false; + std::atomic pin_count_{0}; + unsigned long acc_time_ = 0; + int file_desc_ = -1; + Page page_; /// 在非并发编译时,加锁解锁动作将什么都不做 - common::RecursiveSharedMutex lock_; + common::RecursiveSharedMutex lock_; /// 使用一些手段来做测试,提前检测出头疼的死锁问题 /// 如果编译时没有增加调试选项,这些代码什么都不做 - common::DebugMutex debug_lock_; - intptr_t write_locker_ = 0; - int write_recursive_count_ = 0; - std::unordered_map read_lockers_; + common::DebugMutex debug_lock_; + intptr_t write_locker_ = 0; + int write_recursive_count_ = 0; + std::unordered_map read_lockers_; }; - diff --git a/src/observer/storage/buffer/page.h b/src/observer/storage/buffer/page.h index 2d897e974..1a3ef1f2a 100644 --- a/src/observer/storage/buffer/page.h +++ b/src/observer/storage/buffer/page.h @@ -14,16 +14,16 @@ See the Mulan PSL v2 for more details. */ #pragma once -#include #include "common/types.h" +#include using TrxID = int32_t; static constexpr int BP_INVALID_PAGE_NUM = -1; -static constexpr PageNum BP_HEADER_PAGE = 0; +static constexpr PageNum BP_HEADER_PAGE = 0; -static constexpr const int BP_PAGE_SIZE = (1 << 13); +static constexpr const int BP_PAGE_SIZE = (1 << 13); static constexpr const int BP_PAGE_DATA_SIZE = (BP_PAGE_SIZE - sizeof(PageNum) - sizeof(LSN)); /** @@ -34,5 +34,5 @@ struct Page { PageNum page_num; LSN lsn; - char data[BP_PAGE_DATA_SIZE]; + char data[BP_PAGE_DATA_SIZE]; }; diff --git a/src/observer/storage/clog/clog.cpp b/src/observer/storage/clog/clog.cpp index ed3a0145c..0418fe310 100644 --- a/src/observer/storage/clog/clog.cpp +++ b/src/observer/storage/clog/clog.cpp @@ -15,11 +15,11 @@ See the Mulan PSL v2 for more details. */ #include #include +#include "common/global_context.h" +#include "common/io/io.h" #include "common/log/log.h" #include "storage/clog/clog.h" -#include "common/global_context.h" #include "storage/trx/trx.h" -#include "common/io/io.h" using namespace std; using namespace common; @@ -31,31 +31,25 @@ const char *CLOG_FILE_NAME = "clog"; const char *clog_type_name(CLogType type) { - #define DEFINE_CLOG_TYPE(name) case CLogType::name: return #name; +#define DEFINE_CLOG_TYPE(name) \ + case CLogType::name: return #name; switch (type) { DEFINE_CLOG_TYPE_ENUM; default: return "unknown clog type"; } - #undef DEFINE_CLOG_TYPE +#undef DEFINE_CLOG_TYPE } -int32_t clog_type_to_integer(CLogType type) -{ - return static_cast(type); -} -CLogType clog_type_from_integer(int32_t value) -{ - return static_cast(value); -} +int32_t clog_type_to_integer(CLogType type) { return static_cast(type); } +CLogType clog_type_from_integer(int32_t value) { return static_cast(value); } //////////////////////////////////////////////////////////////////////////////// string CLogRecordHeader::to_string() const { stringstream ss; - ss << "lsn:" << lsn_ - << ", trx_id:" << trx_id_ - << ", type:" << clog_type_name(clog_type_from_integer(type_)) << "(" << type_ << ")" + ss << "lsn:" << lsn_ << ", trx_id:" << trx_id_ << ", type:" << clog_type_name(clog_type_from_integer(type_)) << "(" + << type_ << ")" << ", len:" << logrec_len_; return ss.str(); } @@ -89,52 +83,44 @@ string CLogRecordData::to_string() const //////////////////////////////////////////////////////////////////////////////// -int _align8(int size) -{ - return size / 8 * 8 + ((size % 8 == 0) ? 0 : 8); -} +int _align8(int size) { return size / 8 * 8 + ((size % 8 == 0) ? 0 : 8); } CLogRecord *CLogRecord::build_mtr_record(CLogType type, int32_t trx_id) { - CLogRecord *log_record = new CLogRecord(); - CLogRecordHeader &header = log_record->header_; - header.trx_id_ = trx_id; - header.type_ = clog_type_to_integer(type); + CLogRecord *log_record = new CLogRecord(); + CLogRecordHeader &header = log_record->header_; + header.trx_id_ = trx_id; + header.type_ = clog_type_to_integer(type); return log_record; } CLogRecord *CLogRecord::build_commit_record(int32_t trx_id, int32_t commit_xid) { - CLogRecord *log_record = new CLogRecord(); - CLogRecordHeader &header = log_record->header_; - header.type_ = clog_type_to_integer(CLogType::MTR_COMMIT); - header.trx_id_ = trx_id; - header.logrec_len_ = sizeof(CLogRecordCommitData); - + CLogRecord *log_record = new CLogRecord(); + CLogRecordHeader &header = log_record->header_; + header.type_ = clog_type_to_integer(CLogType::MTR_COMMIT); + header.trx_id_ = trx_id; + header.logrec_len_ = sizeof(CLogRecordCommitData); + CLogRecordCommitData &commit_record = log_record->commit_record(); - commit_record.commit_xid_ = commit_xid; + commit_record.commit_xid_ = commit_xid; return log_record; } -CLogRecord *CLogRecord::build_data_record(CLogType type, - int32_t trx_id, - int32_t table_id, - const RID &rid, - int32_t data_len, - int32_t data_offset, - const char *data) +CLogRecord *CLogRecord::build_data_record(CLogType type, int32_t trx_id, int32_t table_id, const RID &rid, + int32_t data_len, int32_t data_offset, const char *data) { - CLogRecord *log_record = new CLogRecord(); - CLogRecordHeader &header = log_record->header_; - header.trx_id_ = trx_id; - header.type_ = clog_type_to_integer(type); - header.logrec_len_ = CLogRecordData::HEADER_SIZE + data_len; + CLogRecord *log_record = new CLogRecord(); + CLogRecordHeader &header = log_record->header_; + header.trx_id_ = trx_id; + header.type_ = clog_type_to_integer(type); + header.logrec_len_ = CLogRecordData::HEADER_SIZE + data_len; CLogRecordData &data_record = log_record->data_record(); - data_record.table_id_ = table_id; - data_record.rid_ = rid; - data_record.data_len_ = data_len; - data_record.data_offset_ = data_offset; + data_record.table_id_ = table_id; + data_record.rid_ = rid; + data_record.data_len_ = data_len; + data_record.data_offset_ = data_offset; if (data_len > 0) { data_record.data_ = new char[data_len]; @@ -161,7 +147,7 @@ CLogRecord *CLogRecord::build(const CLogRecordHeader &header, char *data) if (header.type_ == clog_type_to_integer(CLogType::MTR_COMMIT)) { ASSERT(header.logrec_len_ == sizeof(CLogRecordCommitData), "invalid length of mtr commit. expect %d, got %d", sizeof(CLogRecordCommitData), header.logrec_len_); - + CLogRecordCommitData &commit_record = log_record->commit_record(); memcpy(reinterpret_cast(&commit_record), data, sizeof(CLogRecordCommitData)); @@ -179,9 +165,7 @@ CLogRecord *CLogRecord::build(const CLogRecordHeader &header, char *data) return log_record; } -CLogRecord::~CLogRecord() -{ -} +CLogRecord::~CLogRecord() {} string CLogRecord::to_string() const { @@ -197,12 +181,9 @@ string CLogRecord::to_string() const //////////////////////////////////////////////////////////////////////////////// static const int CLOG_BUFFER_SIZE = 4 * 1024 * 1024; -CLogBuffer::CLogBuffer() -{ -} +CLogBuffer::CLogBuffer() {} -CLogBuffer::~CLogBuffer() -{} +CLogBuffer::~CLogBuffer() {} RC CLogBuffer::append_log_record(CLogRecord *log_record) { @@ -224,7 +205,7 @@ RC CLogBuffer::append_log_record(CLogRecord *log_record) RC CLogBuffer::flush_buffer(CLogFile &log_file) { - RC rc = RC::SUCCESS; + RC rc = RC::SUCCESS; int count = 0; while (!log_records_.empty()) { lock_.lock(); @@ -256,6 +237,7 @@ RC CLogBuffer::write_log_record(CLogFile &log_file, CLogRecord *log_record) { // TODO 看起来每种类型的日志自己实现 serialize 接口更好一点 const CLogRecordHeader &header = log_record->header(); + RC rc = log_file.write(reinterpret_cast(&header), sizeof(header)); if (rc != RC::SUCCESS) { LOG_WARN("failed to write log record header. size=%d, rc=%s", sizeof(header), strrc(rc)); @@ -269,8 +251,8 @@ RC CLogBuffer::write_log_record(CLogFile &log_file, CLogRecord *log_record) } break; case CLogType::MTR_COMMIT: { - rc = log_file.write(reinterpret_cast(&log_record->commit_record()), - log_record->header().logrec_len_); + rc = log_file.write( + reinterpret_cast(&log_record->commit_record()), log_record->header().logrec_len_); } break; default: { @@ -298,6 +280,7 @@ RC CLogFile::init(const char *path) RC rc = RC::SUCCESS; std::string clog_file_path = std::string(path) + common::FILE_PATH_SPLIT_STR + CLOG_FILE_NAME; + int fd = ::open(clog_file_path.c_str(), O_RDWR | O_APPEND | O_CREAT, S_IRUSR | S_IWUSR); if (fd < 0) { rc = RC::IOERR_OPEN; @@ -374,10 +357,7 @@ RC CLogRecordIterator::init(CLogFile &log_file) return RC::SUCCESS; } -bool CLogRecordIterator::valid() const -{ - return nullptr != log_record_; -} +bool CLogRecordIterator::valid() const { return nullptr != log_record_; } RC CLogRecordIterator::next() { @@ -385,7 +365,7 @@ RC CLogRecordIterator::next() log_record_ = nullptr; CLogRecordHeader header; - RC rc = log_file_->read(reinterpret_cast(&header), sizeof(header)); + RC rc = log_file_->read(reinterpret_cast(&header), sizeof(header)); if (rc != RC::SUCCESS) { if (log_file_->eof()) { return RC::RECORD_EOF; @@ -395,11 +375,11 @@ RC CLogRecordIterator::next() return rc; } - char *data = nullptr; + char *data = nullptr; int32_t record_size = header.logrec_len_; if (record_size > 0) { data = new char[record_size]; - rc = log_file_->read(data, record_size); + rc = log_file_->read(data, record_size); if (OB_FAIL(rc)) { if (log_file_->eof()) { // TODO 遇到了没有写完整数据的log,应该truncate一部分数据, 但是现在不管 @@ -417,10 +397,7 @@ RC CLogRecordIterator::next() return rc; } -const CLogRecord &CLogRecordIterator::log_record() -{ - return *log_record_; -} +const CLogRecord &CLogRecordIterator::log_record() { return *log_record_; } //////////////////////////////////////////////////////////////////////////////// @@ -444,13 +421,8 @@ CLogManager::~CLogManager() } } -RC CLogManager::append_log(CLogType type, - int32_t trx_id, - int32_t table_id, - const RID &rid, - int32_t data_len, - int32_t data_offset, - const char *data) +RC CLogManager::append_log(CLogType type, int32_t trx_id, int32_t table_id, const RID &rid, int32_t data_len, + int32_t data_offset, const char *data) { CLogRecord *log_record = CLogRecord::build_data_record(type, trx_id, table_id, rid, data_len, data_offset, data); if (nullptr == log_record) { @@ -473,7 +445,7 @@ RC CLogManager::commit_trx(int32_t trx_id, int32_t commit_xid) return rc; } - rc = sync(); // 事务提交时需要把当前事务关联的日志,都写入到磁盘中,这样做是保证不丢数据 + rc = sync(); // 事务提交时需要把当前事务关联的日志,都写入到磁盘中,这样做是保证不丢数据 return rc; } @@ -490,15 +462,12 @@ RC CLogManager::append_log(CLogRecord *log_record) return log_buffer_->append_log_record(log_record); } -RC CLogManager::sync() -{ - return log_buffer_->flush_buffer(*log_file_); -} +RC CLogManager::sync() { return log_buffer_->flush_buffer(*log_file_); } RC CLogManager::recover(Db *db) { CLogRecordIterator log_record_iterator; - RC rc = log_record_iterator.init(*log_file_); + RC rc = log_record_iterator.init(*log_file_); if (OB_FAIL(rc)) { LOG_WARN("failed to init log record iterator. rc=%s", strrc(rc)); return rc; @@ -521,7 +490,7 @@ RC CLogManager::recover(Db *db) } } break; - case CLogType::MTR_COMMIT: + case CLogType::MTR_COMMIT: case CLogType::MTR_ROLLBACK: { Trx *trx = trx_manager->find_trx(log_record.trx_id()); if (nullptr == trx) { @@ -541,9 +510,8 @@ RC CLogManager::recover(Db *db) Trx *trx = GCTX.trx_kit_->find_trx(log_record.trx_id()); ASSERT(trx != nullptr, "cannot find such trx. trx id=%d, log_record={%s}", - log_record.trx_id(), - log_record.to_string().c_str()); - + log_record.trx_id(), log_record.to_string().c_str()); + rc = trx->redo(db, log_record); if (rc != RC::SUCCESS) { LOG_WARN("failed to redo log record. log_record={%s}, rc=%s", log_record.to_string().c_str(), strrc(rc)); diff --git a/src/observer/storage/clog/clog.h b/src/observer/storage/clog/clog.h index 7255f6fdd..27428eaae 100644 --- a/src/observer/storage/clog/clog.h +++ b/src/observer/storage/clog/clog.h @@ -14,18 +14,18 @@ See the Mulan PSL v2 for more details. */ #pragma once -#include -#include -#include #include -#include #include +#include #include +#include +#include #include +#include -#include "storage/record/record.h" -#include "storage/persist/persist.h" #include "common/lang/mutex.h" +#include "storage/persist/persist.h" +#include "storage/record/record.h" class CLogManager; class CLogBuffer; @@ -49,16 +49,16 @@ class Db; * 也就是说,像INSERT、DELETE等是事务自己处理的,其实这种类型的日志不需要在这里定义,而是在各个 * 事务模型中定义,由各个事务模型自行处理。 */ -#define DEFINE_CLOG_TYPE_ENUM \ - DEFINE_CLOG_TYPE(ERROR) \ - DEFINE_CLOG_TYPE(MTR_BEGIN) \ - DEFINE_CLOG_TYPE(MTR_COMMIT) \ - DEFINE_CLOG_TYPE(MTR_ROLLBACK) \ - DEFINE_CLOG_TYPE(INSERT) \ +#define DEFINE_CLOG_TYPE_ENUM \ + DEFINE_CLOG_TYPE(ERROR) \ + DEFINE_CLOG_TYPE(MTR_BEGIN) \ + DEFINE_CLOG_TYPE(MTR_COMMIT) \ + DEFINE_CLOG_TYPE(MTR_ROLLBACK) \ + DEFINE_CLOG_TYPE(INSERT) \ DEFINE_CLOG_TYPE(DELETE) -enum class CLogType -{ +enum class CLogType +{ #define DEFINE_CLOG_TYPE(name) name, DEFINE_CLOG_TYPE_ENUM #undef DEFINE_CLOG_TYPE @@ -85,12 +85,12 @@ CLogType clog_type_from_integer(int32_t value); * @brief CLog的记录头。每个日志都带有这个信息 * @ingroup CLog */ -struct CLogRecordHeader +struct CLogRecordHeader { - int32_t lsn_ = -1; ///< log sequence number。当前没有使用 - int32_t trx_id_ = -1; ///< 日志所属事务的编号 - int32_t type_ = clog_type_to_integer(CLogType::ERROR); ///< 日志类型 - int32_t logrec_len_ = 0; ///< record的长度,不包含header长度 + int32_t lsn_ = -1; ///< log sequence number。当前没有使用 + int32_t trx_id_ = -1; ///< 日志所属事务的编号 + int32_t type_ = clog_type_to_integer(CLogType::ERROR); ///< 日志类型 + int32_t logrec_len_ = 0; ///< record的长度,不包含header长度 bool operator==(const CLogRecordHeader &other) const { @@ -107,12 +107,9 @@ struct CLogRecordHeader */ struct CLogRecordCommitData { - int32_t commit_xid_ = -1; ///< 事务提交的事务号 + int32_t commit_xid_ = -1; ///< 事务提交的事务号 - bool operator == (const CLogRecordCommitData &other) const - { - return this->commit_xid_ == other.commit_xid_; - } + bool operator==(const CLogRecordCommitData &other) const { return this->commit_xid_ == other.commit_xid_; } std::string to_string() const; }; @@ -124,21 +121,18 @@ struct CLogRecordCommitData */ struct CLogRecordData { - int32_t table_id_ = -1; ///< 操作的表 - RID rid_; ///< 操作的哪条记录 - int32_t data_len_ = 0; ///< 记录的数据长度(因为header中也包含长度信息,这个长度可以不要) - int32_t data_offset_ = 0; ///< 操作的数据在完整记录中的偏移量 - char * data_ = nullptr; ///< 具体的数据,可能没有任何数据 + int32_t table_id_ = -1; ///< 操作的表 + RID rid_; ///< 操作的哪条记录 + int32_t data_len_ = 0; ///< 记录的数据长度(因为header中也包含长度信息,这个长度可以不要) + int32_t data_offset_ = 0; ///< 操作的数据在完整记录中的偏移量 + char *data_ = nullptr; ///< 具体的数据,可能没有任何数据 ~CLogRecordData(); bool operator==(const CLogRecordData &other) const { - return table_id_ == other.table_id_ && - rid_ == other.rid_ && - data_len_ == other.data_len_ && - data_offset_ == other.data_offset_ && - 0 == memcmp(data_, other.data_, data_len_); + return table_id_ == other.table_id_ && rid_ == other.rid_ && data_len_ == other.data_len_ && + data_offset_ == other.data_offset_ && 0 == memcmp(data_, other.data_, data_len_); } std::string to_string() const; @@ -152,7 +146,7 @@ struct CLogRecordData * @details 一条日志记录由一个日志头和具体的数据构成。 * 具体的数据根据日志类型不同,也是不同的类型。 */ -class CLogRecord +class CLogRecord { public: /** @@ -173,7 +167,7 @@ class CLogRecord /** * @brief 创建一个表示提交事务的日志对象 - * + * * @param trx_id 事务编号 * @param commit_xid 事务提交时使用的编号 */ @@ -181,7 +175,7 @@ class CLogRecord /** * @brief 创建一个表示数据操作的日志对象 - * + * * @param type 类型 * @param trx_id 事务编号 * @param table_id 操作的表 @@ -190,14 +184,9 @@ class CLogRecord * @param data_offset 偏移量,参考 CLogRecordData::data_offset_ * @param data 具体的数据 */ - static CLogRecord *build_data_record(CLogType type, - int32_t trx_id, - int32_t table_id, - const RID &rid, - int32_t data_len, - int32_t data_offset, - const char *data); - + static CLogRecord *build_data_record(CLogType type, int32_t trx_id, int32_t table_id, const RID &rid, + int32_t data_len, int32_t data_offset, const char *data); + /** * @brief 根据二进制数据创建日志对象 * @details 通常是从日志文件中读取数据,然后调用此函数创建日志对象 @@ -206,25 +195,25 @@ class CLogRecord */ static CLogRecord *build(const CLogRecordHeader &header, char *data); - CLogType log_type() const { return clog_type_from_integer(header_.type_); } + CLogType log_type() const { return clog_type_from_integer(header_.type_); } int32_t trx_id() const { return header_.trx_id_; } int32_t logrec_len() const { return header_.logrec_len_; } - CLogRecordHeader &header() { return header_; } + CLogRecordHeader &header() { return header_; } CLogRecordCommitData &commit_record() { return commit_record_; } - CLogRecordData &data_record() { return data_record_; } + CLogRecordData &data_record() { return data_record_; } - const CLogRecordHeader &header() const { return header_; } + const CLogRecordHeader &header() const { return header_; } const CLogRecordCommitData &commit_record() const { return commit_record_; } - const CLogRecordData &data_record() const { return data_record_; } + const CLogRecordData &data_record() const { return data_record_; } std::string to_string() const; protected: - CLogRecordHeader header_; ///< 日志头信息 + CLogRecordHeader header_; ///< 日志头信息 - CLogRecordData data_record_; ///< 如果日志操作的是数据,此结构生效 - CLogRecordCommitData commit_record_; ///< 如果是事务提交日志,此结构生效 + CLogRecordData data_record_; ///< 如果日志操作的是数据,此结构生效 + CLogRecordCommitData commit_record_; ///< 如果是事务提交日志,此结构生效 }; /** @@ -234,7 +223,7 @@ class CLogRecord * 管理二进制buffer的方法。这里仅仅把日志记录下来,放到链表中。如果达到一定量的日志, * 或者日志数量超过某个阈值,就会调用flush_buffer将日志刷新到磁盘中。 */ -class CLogBuffer +class CLogBuffer { public: CLogBuffer(); @@ -256,16 +245,16 @@ class CLogBuffer private: /** * @brief 将日志记录写入到日志文件中 - * + * * @param log_file 日志文件,概念上来讲不一定是某个特定的文件 * @param log_record 要写入的日志记录 */ RC write_log_record(CLogFile &log_file, CLogRecord *log_record); private: - common::Mutex lock_; ///< 加锁支持多线程并发写入 + common::Mutex lock_; ///< 加锁支持多线程并发写入 std::deque> log_records_; ///< 当前等待刷数据的日志记录 - std::atomic_int32_t total_size_; ///< 当前缓存中的日志记录的总大小 + std::atomic_int32_t total_size_; ///< 当前缓存中的日志记录的总大小 }; /** @@ -274,7 +263,7 @@ class CLogBuffer * @details 这里的名字不太贴切,因为这个类希望管理所有日志文件,而不是特定的某个文件。不过当前 * 只有一个文件,并且文件名是固定的。 */ -class CLogFile +class CLogFile { public: CLogFile() = default; @@ -282,7 +271,7 @@ class CLogFile /** * @brief 初始化 - * + * * @param path 日志文件存放的路径。会打开这个目录下叫做 clog 的文件。 */ RC init(const char *path); @@ -320,9 +309,9 @@ class CLogFile bool eof() const { return eof_; } protected: - std::string filename_; ///< 日志文件名。总是init函数参数path路径下的clog文件 - int fd_ = -1; ///< 操作的文件描述符 - bool eof_ = false; ///< 是否已经读取到文件尾 + std::string filename_; ///< 日志文件名。总是init函数参数path路径下的clog文件 + int fd_ = -1; ///< 操作的文件描述符 + bool eof_ = false; ///< 是否已经读取到文件尾 }; /** @@ -333,17 +322,17 @@ class CLogFile class CLogRecordIterator { public: - CLogRecordIterator() = default; + CLogRecordIterator() = default; ~CLogRecordIterator() = default; RC init(CLogFile &log_file); - bool valid() const; - RC next(); + bool valid() const; + RC next(); const CLogRecord &log_record(); private: - CLogFile *log_file_ = nullptr; + CLogFile *log_file_ = nullptr; CLogRecord *log_record_ = nullptr; }; @@ -353,7 +342,7 @@ class CLogRecordIterator * @details 一个日志管理器属于某一个DB(当前仅有一个DB sys)。 * 管理器负责写日志(运行时)、读日志与恢复(启动时) */ -class CLogManager +class CLogManager { public: CLogManager() = default; @@ -361,7 +350,7 @@ class CLogManager /** * @brief 初始化日志管理器 - * + * * @param path 日志都放在这个目录下。当前就是数据库的目录 */ RC init(const char *path); @@ -369,24 +358,19 @@ class CLogManager /** * @brief 新增一条数据更新的日志 */ - RC append_log(CLogType type, - int32_t trx_id, - int32_t table_id, - const RID &rid, - int32_t data_len, - int32_t data_offset, - const char *data); + RC append_log(CLogType type, int32_t trx_id, int32_t table_id, const RID &rid, int32_t data_len, int32_t data_offset, + const char *data); /** * @brief 开启一个事务 - * + * * @param trx_id 事务编号 */ RC begin_trx(int32_t trx_id); /** * @brief 提交一个事务 - * + * * @param trx_id 事务编号 * @param commit_xid 事务提交时使用的编号 */ @@ -394,7 +378,7 @@ class CLogManager /** * @brief 回滚一个事务 - * + * * @param trx_id 事务编号 */ RC rollback_trx(int32_t trx_id); @@ -417,6 +401,6 @@ class CLogManager RC recover(Db *db); private: - CLogBuffer *log_buffer_ = nullptr; ///< 日志缓存。新增日志时先放到内存,也就是这个buffer中 - CLogFile * log_file_ = nullptr; ///< 管理日志,比如读写日志 + CLogBuffer *log_buffer_ = nullptr; ///< 日志缓存。新增日志时先放到内存,也就是这个buffer中 + CLogFile *log_file_ = nullptr; ///< 管理日志,比如读写日志 }; diff --git a/src/observer/storage/common/condition_filter.cpp b/src/observer/storage/common/condition_filter.cpp index 13d9dd794..eb0b3f26b 100644 --- a/src/observer/storage/common/condition_filter.cpp +++ b/src/observer/storage/common/condition_filter.cpp @@ -12,31 +12,29 @@ See the Mulan PSL v2 for more details. */ // Created by Wangyunlai on 2021/5/7. // -#include -#include #include "condition_filter.h" -#include "storage/record/record_manager.h" #include "common/log/log.h" -#include "storage/table/table.h" #include "sql/parser/value.h" +#include "storage/record/record_manager.h" +#include "storage/table/table.h" +#include +#include using namespace common; -ConditionFilter::~ConditionFilter() -{} +ConditionFilter::~ConditionFilter() {} DefaultConditionFilter::DefaultConditionFilter() { - left_.is_attr = false; + left_.is_attr = false; left_.attr_length = 0; left_.attr_offset = 0; - right_.is_attr = false; + right_.is_attr = false; right_.attr_length = 0; right_.attr_offset = 0; } -DefaultConditionFilter::~DefaultConditionFilter() -{} +DefaultConditionFilter::~DefaultConditionFilter() {} RC DefaultConditionFilter::init(const ConDesc &left, const ConDesc &right, AttrType attr_type, CompOp comp_op) { @@ -50,24 +48,24 @@ RC DefaultConditionFilter::init(const ConDesc &left, const ConDesc &right, AttrT return RC::INVALID_ARGUMENT; } - left_ = left; - right_ = right; + left_ = left; + right_ = right; attr_type_ = attr_type; - comp_op_ = comp_op; + comp_op_ = comp_op; return RC::SUCCESS; } RC DefaultConditionFilter::init(Table &table, const ConditionSqlNode &condition) { const TableMeta &table_meta = table.table_meta(); - ConDesc left; - ConDesc right; + ConDesc left; + ConDesc right; - AttrType type_left = UNDEFINED; + AttrType type_left = UNDEFINED; AttrType type_right = UNDEFINED; if (1 == condition.left_is_attr) { - left.is_attr = true; + left.is_attr = true; const FieldMeta *field_left = table_meta.field(condition.left_attr.attribute_name.c_str()); if (nullptr == field_left) { LOG_WARN("No such field in condition. %s.%s", table.name(), condition.left_attr.attribute_name.c_str()); @@ -79,15 +77,15 @@ RC DefaultConditionFilter::init(Table &table, const ConditionSqlNode &condition) type_left = field_left->type(); } else { left.is_attr = false; - left.value = condition.left_value; // 校验type 或者转换类型 - type_left = condition.left_value.attr_type(); + left.value = condition.left_value; // 校验type 或者转换类型 + type_left = condition.left_value.attr_type(); left.attr_length = 0; left.attr_offset = 0; } if (1 == condition.right_is_attr) { - right.is_attr = true; + right.is_attr = true; const FieldMeta *field_right = table_meta.field(condition.right_attr.attribute_name.c_str()); if (nullptr == field_right) { LOG_WARN("No such field in condition. %s.%s", table.name(), condition.right_attr.attribute_name.c_str()); @@ -95,11 +93,11 @@ RC DefaultConditionFilter::init(Table &table, const ConditionSqlNode &condition) } right.attr_length = field_right->len(); right.attr_offset = field_right->offset(); - type_right = field_right->type(); + type_right = field_right->type(); } else { right.is_attr = false; - right.value = condition.right_value; - type_right = condition.right_value.attr_type(); + right.value = condition.right_value; + type_right = condition.right_value.attr_type(); right.attr_length = 0; right.attr_offset = 0; @@ -141,21 +139,14 @@ bool DefaultConditionFilter::filter(const Record &rec) const int cmp_result = left_value.compare(right_value); switch (comp_op_) { - case EQUAL_TO: - return 0 == cmp_result; - case LESS_EQUAL: - return cmp_result <= 0; - case NOT_EQUAL: - return cmp_result != 0; - case LESS_THAN: - return cmp_result < 0; - case GREAT_EQUAL: - return cmp_result >= 0; - case GREAT_THAN: - return cmp_result > 0; - - default: - break; + case EQUAL_TO: return 0 == cmp_result; + case LESS_EQUAL: return cmp_result <= 0; + case NOT_EQUAL: return cmp_result != 0; + case LESS_THAN: return cmp_result < 0; + case GREAT_EQUAL: return cmp_result >= 0; + case GREAT_THAN: return cmp_result > 0; + + default: break; } LOG_PANIC("Never should print this."); @@ -172,8 +163,8 @@ CompositeConditionFilter::~CompositeConditionFilter() RC CompositeConditionFilter::init(const ConditionFilter *filters[], int filter_num, bool own_memory) { - filters_ = filters; - filter_num_ = filter_num; + filters_ = filters; + filter_num_ = filter_num; memory_owner_ = own_memory; return RC::SUCCESS; } @@ -191,11 +182,11 @@ RC CompositeConditionFilter::init(Table &table, const ConditionSqlNode *conditio return RC::INVALID_ARGUMENT; } - RC rc = RC::SUCCESS; + RC rc = RC::SUCCESS; ConditionFilter **condition_filters = new ConditionFilter *[condition_num]; for (int i = 0; i < condition_num; i++) { DefaultConditionFilter *default_condition_filter = new DefaultConditionFilter(); - rc = default_condition_filter->init(table, conditions[i]); + rc = default_condition_filter->init(table, conditions[i]); if (rc != RC::SUCCESS) { delete default_condition_filter; for (int j = i - 1; j >= 0; j--) { diff --git a/src/observer/storage/common/condition_filter.h b/src/observer/storage/common/condition_filter.h index 4aa80a483..ce63ebc0f 100644 --- a/src/observer/storage/common/condition_filter.h +++ b/src/observer/storage/common/condition_filter.h @@ -19,15 +19,15 @@ See the Mulan PSL v2 for more details. */ class Record; class Table; -struct ConDesc +struct ConDesc { - bool is_attr; // 是否属性,false 表示是值 - int attr_length; // 如果是属性,表示属性值长度 - int attr_offset; // 如果是属性,表示在记录中的偏移量 - Value value; // 如果是值类型,这里记录值的数据 + bool is_attr; // 是否属性,false 表示是值 + int attr_length; // 如果是属性,表示属性值长度 + int attr_offset; // 如果是属性,表示在记录中的偏移量 + Value value; // 如果是值类型,这里记录值的数据 }; -class ConditionFilter +class ConditionFilter { public: virtual ~ConditionFilter(); @@ -40,7 +40,7 @@ class ConditionFilter virtual bool filter(const Record &rec) const = 0; }; -class DefaultConditionFilter : public ConditionFilter +class DefaultConditionFilter : public ConditionFilter { public: DefaultConditionFilter(); @@ -52,34 +52,20 @@ class DefaultConditionFilter : public ConditionFilter virtual bool filter(const Record &rec) const; public: - const ConDesc &left() const - { - return left_; - } - - const ConDesc &right() const - { - return right_; - } - - CompOp comp_op() const - { - return comp_op_; - } - - AttrType attr_type() const - { - return attr_type_; - } + const ConDesc &left() const { return left_; } + const ConDesc &right() const { return right_; } + + CompOp comp_op() const { return comp_op_; } + AttrType attr_type() const { return attr_type_; } private: - ConDesc left_; - ConDesc right_; + ConDesc left_; + ConDesc right_; AttrType attr_type_ = UNDEFINED; - CompOp comp_op_ = NO_OP; + CompOp comp_op_ = NO_OP; }; -class CompositeConditionFilter : public ConditionFilter +class CompositeConditionFilter : public ConditionFilter { public: CompositeConditionFilter() = default; @@ -87,23 +73,18 @@ class CompositeConditionFilter : public ConditionFilter RC init(const ConditionFilter *filters[], int filter_num); RC init(Table &table, const ConditionSqlNode *conditions, int condition_num); + virtual bool filter(const Record &rec) const; public: - int filter_num() const - { - return filter_num_; - } - const ConditionFilter &filter(int index) const - { - return *filters_[index]; - } + int filter_num() const { return filter_num_; } + const ConditionFilter &filter(int index) const { return *filters_[index]; } private: RC init(const ConditionFilter *filters[], int filter_num, bool own_memory); private: - const ConditionFilter **filters_ = nullptr; - int filter_num_ = 0; - bool memory_owner_ = false; // filters_的内存是否由自己来控制 + const ConditionFilter **filters_ = nullptr; + int filter_num_ = 0; + bool memory_owner_ = false; // filters_的内存是否由自己来控制 }; diff --git a/src/observer/storage/common/meta_util.cpp b/src/observer/storage/common/meta_util.cpp index 4a82bc96a..8070c4e47 100644 --- a/src/observer/storage/common/meta_util.cpp +++ b/src/observer/storage/common/meta_util.cpp @@ -11,8 +11,8 @@ See the Mulan PSL v2 for more details. */ // Created by wangyunlai.wyl on 2021/5/18. // -#include "common/defs.h" #include "storage/common/meta_util.h" +#include "common/defs.h" std::string table_meta_file(const char *base_dir, const char *table_name) { diff --git a/src/observer/storage/common/meta_util.h b/src/observer/storage/common/meta_util.h index 72067bd51..74abd59c7 100644 --- a/src/observer/storage/common/meta_util.h +++ b/src/observer/storage/common/meta_util.h @@ -15,10 +15,10 @@ See the Mulan PSL v2 for more details. */ #include -static constexpr const char *TABLE_META_SUFFIX = ".table"; +static constexpr const char *TABLE_META_SUFFIX = ".table"; static constexpr const char *TABLE_META_FILE_PATTERN = ".*\\.table$"; -static constexpr const char *TABLE_DATA_SUFFIX = ".data"; -static constexpr const char *TABLE_INDEX_SUFFIX = ".index"; +static constexpr const char *TABLE_DATA_SUFFIX = ".data"; +static constexpr const char *TABLE_INDEX_SUFFIX = ".index"; std::string table_meta_file(const char *base_dir, const char *table_name); std::string table_data_file(const char *base_dir, const char *table_name); diff --git a/src/observer/storage/db/db.cpp b/src/observer/storage/db/db.cpp index c5bfab895..63ea5aa63 100644 --- a/src/observer/storage/db/db.cpp +++ b/src/observer/storage/db/db.cpp @@ -14,18 +14,18 @@ See the Mulan PSL v2 for more details. */ #include "storage/db/db.h" -#include #include +#include #include +#include "common/lang/string.h" #include "common/log/log.h" #include "common/os/path.h" -#include "common/lang/string.h" -#include "storage/table/table_meta.h" -#include "storage/table/table.h" +#include "storage/clog/clog.h" #include "storage/common/meta_util.h" +#include "storage/table/table.h" +#include "storage/table/table_meta.h" #include "storage/trx/trx.h" -#include "storage/clog/clog.h" Db::~Db() { @@ -87,8 +87,8 @@ RC Db::create_table(const char *table_name, int attribute_count, const AttrInfoS // 文件路径可以移到Table模块 std::string table_file_path = table_meta_file(path_.c_str(), table_name); - Table *table = new Table(); - int32_t table_id = next_table_id_++; + Table *table = new Table(); + int32_t table_id = next_table_id_++; rc = table->create(table_id, table_file_path.c_str(), table_name, path_.c_str(), attribute_count, attributes); if (rc != RC::SUCCESS) { LOG_ERROR("Failed to create table %s.", table_name); @@ -123,7 +123,7 @@ Table *Db::find_table(int32_t table_id) const RC Db::open_all_tables() { std::vector table_meta_files; - int ret = common::list_file(path_.c_str(), TABLE_META_FILE_PATTERN, table_meta_files); + int ret = common::list_file(path_.c_str(), TABLE_META_FILE_PATTERN, table_meta_files); if (ret < 0) { LOG_ERROR("Failed to list table meta files under %s.", path_.c_str()); return RC::IOERR_READ; @@ -132,7 +132,7 @@ RC Db::open_all_tables() RC rc = RC::SUCCESS; for (const std::string &filename : table_meta_files) { Table *table = new Table(); - rc = table->open(filename.c_str(), path_.c_str()); + rc = table->open(filename.c_str(), path_.c_str()); if (rc != RC::SUCCESS) { delete table; LOG_ERROR("Failed to open table. filename=%s", filename.c_str()); @@ -157,10 +157,7 @@ RC Db::open_all_tables() return rc; } -const char *Db::name() const -{ - return name_.c_str(); -} +const char *Db::name() const { return name_.c_str(); } void Db::all_tables(std::vector &table_names) const { @@ -174,7 +171,7 @@ RC Db::sync() RC rc = RC::SUCCESS; for (const auto &table_pair : opened_tables_) { Table *table = table_pair.second; - rc = table->sync(); + rc = table->sync(); if (rc != RC::SUCCESS) { LOG_ERROR("Failed to flush table. table=%s.%s, rc=%d:%s", name_.c_str(), table->name(), rc, strrc(rc)); return rc; @@ -185,12 +182,6 @@ RC Db::sync() return rc; } -RC Db::recover() -{ - return clog_manager_->recover(this); -} +RC Db::recover() { return clog_manager_->recover(this); } -CLogManager *Db::clog_manager() -{ - return clog_manager_.get(); -} \ No newline at end of file +CLogManager *Db::clog_manager() { return clog_manager_.get(); } \ No newline at end of file diff --git a/src/observer/storage/db/db.h b/src/observer/storage/db/db.h index aa5b4b6ae..998448c15 100644 --- a/src/observer/storage/db/db.h +++ b/src/observer/storage/db/db.h @@ -64,10 +64,10 @@ class Db RC open_all_tables(); private: - std::string name_; - std::string path_; + std::string name_; + std::string path_; std::unordered_map opened_tables_; - std::unique_ptr clog_manager_; + std::unique_ptr clog_manager_; /// 给每个table都分配一个ID,用来记录日志。这里假设所有的DDL都不会并发操作,所以相关的数据都不上锁 int32_t next_table_id_ = 0; diff --git a/src/observer/storage/default/default_handler.cpp b/src/observer/storage/default/default_handler.cpp index 362d199a0..6bd4c3375 100644 --- a/src/observer/storage/default/default_handler.cpp +++ b/src/observer/storage/default/default_handler.cpp @@ -16,15 +16,15 @@ See the Mulan PSL v2 for more details. */ #include -#include "common/os/path.h" -#include "common/log/log.h" #include "common/lang/string.h" -#include "storage/record/record_manager.h" +#include "common/log/log.h" +#include "common/os/path.h" +#include "session/session.h" +#include "storage/clog/clog.h" +#include "storage/common/condition_filter.h" #include "storage/index/bplus_tree.h" +#include "storage/record/record_manager.h" #include "storage/table/table.h" -#include "storage/common/condition_filter.h" -#include "storage/clog/clog.h" -#include "session/session.h" static DefaultHandler *default_handler = nullptr; @@ -37,18 +37,11 @@ void DefaultHandler::set_default(DefaultHandler *handler) default_handler = handler; } -DefaultHandler &DefaultHandler::get_default() -{ - return *default_handler; -} +DefaultHandler &DefaultHandler::get_default() { return *default_handler; } -DefaultHandler::DefaultHandler() -{} +DefaultHandler::DefaultHandler() {} -DefaultHandler::~DefaultHandler() noexcept -{ - destroy(); -} +DefaultHandler::~DefaultHandler() noexcept { destroy(); } RC DefaultHandler::init(const char *base_dir) { @@ -61,7 +54,7 @@ RC DefaultHandler::init(const char *base_dir) } base_dir_ = base_dir; - db_dir_ = tmp + "/"; + db_dir_ = tmp + "/"; const char *sys_db = "sys"; @@ -115,10 +108,7 @@ RC DefaultHandler::create_db(const char *dbname) return RC::SUCCESS; } -RC DefaultHandler::drop_db(const char *dbname) -{ - return RC::INTERNAL; -} +RC DefaultHandler::drop_db(const char *dbname) { return RC::INTERNAL; } RC DefaultHandler::open_db(const char *dbname) { @@ -137,8 +127,8 @@ RC DefaultHandler::open_db(const char *dbname) } // open db - Db *db = new Db(); - RC ret = RC::SUCCESS; + Db *db = new Db(); + RC ret = RC::SUCCESS; if ((ret = db->init(dbname, dbpath.c_str())) != RC::SUCCESS) { LOG_ERROR("Failed to open db: %s. error=%s", dbname, strrc(ret)); delete db; @@ -148,15 +138,9 @@ RC DefaultHandler::open_db(const char *dbname) return ret; } -RC DefaultHandler::close_db(const char *dbname) -{ - return RC::UNIMPLENMENT; -} +RC DefaultHandler::close_db(const char *dbname) { return RC::UNIMPLENMENT; } -RC DefaultHandler::execute(const char *sql) -{ - return RC::UNIMPLENMENT; -} +RC DefaultHandler::execute(const char *sql) { return RC::UNIMPLENMENT; } RC DefaultHandler::create_table( const char *dbname, const char *relation_name, int attribute_count, const AttrInfoSqlNode *attributes) @@ -168,10 +152,7 @@ RC DefaultHandler::create_table( return db->create_table(relation_name, attribute_count, attributes); } -RC DefaultHandler::drop_table(const char *dbname, const char *relation_name) -{ - return RC::UNIMPLENMENT; -} +RC DefaultHandler::drop_table(const char *dbname, const char *relation_name) { return RC::UNIMPLENMENT; } Db *DefaultHandler::find_db(const char *dbname) const { @@ -201,7 +182,7 @@ RC DefaultHandler::sync() RC rc = RC::SUCCESS; for (const auto &db_pair : opened_dbs_) { Db *db = db_pair.second; - rc = db->sync(); + rc = db->sync(); if (rc != RC::SUCCESS) { LOG_ERROR("Failed to sync db. name=%s, rc=%d:%s", db->name(), rc, strrc(rc)); return rc; diff --git a/src/observer/storage/default/default_handler.h b/src/observer/storage/default/default_handler.h index 901685c00..329edc999 100644 --- a/src/observer/storage/default/default_handler.h +++ b/src/observer/storage/default/default_handler.h @@ -13,20 +13,21 @@ See the Mulan PSL v2 for more details. */ // #pragma once -#include #include +#include #include "storage/db/db.h" class Trx; -class DefaultHandler { +class DefaultHandler +{ public: DefaultHandler(); virtual ~DefaultHandler() noexcept; - RC init(const char *base_dir); + RC init(const char *base_dir); void destroy(); /** @@ -80,7 +81,8 @@ class DefaultHandler { * @param attributes * @return */ - RC create_table(const char *dbname, const char *relation_name, int attribute_count, const AttrInfoSqlNode *attributes); + RC create_table( + const char *dbname, const char *relation_name, int attribute_count, const AttrInfoSqlNode *attributes); /** * 销毁名为relName的表以及在该表上建立的所有索引 @@ -90,17 +92,17 @@ class DefaultHandler { RC drop_table(const char *dbname, const char *relation_name); public: - Db *find_db(const char *dbname) const; + Db *find_db(const char *dbname) const; Table *find_table(const char *dbname, const char *table_name) const; RC sync(); public: - static void set_default(DefaultHandler *handler); + static void set_default(DefaultHandler *handler); static DefaultHandler &get_default(); private: - std::string base_dir_; - std::string db_dir_; + std::string base_dir_; + std::string db_dir_; std::map opened_dbs_; }; // class Handler diff --git a/src/observer/storage/field/field.cpp b/src/observer/storage/field/field.cpp index 87f866454..bfc0777da 100644 --- a/src/observer/storage/field/field.cpp +++ b/src/observer/storage/field/field.cpp @@ -13,15 +13,15 @@ See the Mulan PSL v2 for more details. */ // #include "storage/field/field.h" +#include "common/log/log.h" #include "sql/parser/value.h" #include "storage/record/record.h" -#include "common/log/log.h" void Field::set_int(Record &record, int value) { ASSERT(field_->type() == AttrType::INTS, "could not set int value to a non-int field"); ASSERT(field_->len() == sizeof(value), "invalid field len"); - + char *field_data = record.data() + field_->offset(); memcpy(field_data, &value, sizeof(value)); } @@ -32,7 +32,4 @@ int Field::get_int(const Record &record) return value.get_int(); } -const char *Field::get_data(const Record &record) -{ - return record.data() + field_->offset(); -} \ No newline at end of file +const char *Field::get_data(const Record &record) { return record.data() + field_->offset(); } \ No newline at end of file diff --git a/src/observer/storage/field/field.h b/src/observer/storage/field/field.h index 00a7053b2..7022eb5f1 100644 --- a/src/observer/storage/field/field.h +++ b/src/observer/storage/field/field.h @@ -14,52 +14,30 @@ See the Mulan PSL v2 for more details. */ #pragma once -#include "storage/table/table.h" #include "storage/field/field_meta.h" +#include "storage/table/table.h" /** * @brief 字段 - * + * */ -class Field +class Field { public: Field() = default; - Field(const Table *table, const FieldMeta *field) : table_(table), field_(field) - {} + Field(const Table *table, const FieldMeta *field) : table_(table), field_(field) {} Field(const Field &) = default; - const Table *table() const - { - return table_; - } - const FieldMeta *meta() const - { - return field_; - } + const Table *table() const { return table_; } + const FieldMeta *meta() const { return field_; } - AttrType attr_type() const - { - return field_->type(); - } + AttrType attr_type() const { return field_->type(); } - const char *table_name() const - { - return table_->name(); - } - const char *field_name() const - { - return field_->name(); - } + const char *table_name() const { return table_->name(); } + const char *field_name() const { return field_->name(); } - void set_table(const Table *table) - { - this->table_ = table; - } - void set_field(const FieldMeta *field) - { - this->field_ = field; - } + void set_table(const Table *table) { this->table_ = table; } + void set_field(const FieldMeta *field) { this->field_ = field; } void set_int(Record &record, int value); int get_int(const Record &record); @@ -67,6 +45,6 @@ class Field const char *get_data(const Record &record); private: - const Table *table_ = nullptr; + const Table *table_ = nullptr; const FieldMeta *field_ = nullptr; }; diff --git a/src/observer/storage/field/field_meta.cpp b/src/observer/storage/field/field_meta.cpp index db9eb48a4..4bae4c1d0 100644 --- a/src/observer/storage/field/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 "common/lang/string.h" #include "storage/field/field_meta.h" +#include "common/lang/string.h" #include "common/log/log.h" #include "sql/parser/parse_defs.h" @@ -25,8 +25,7 @@ const static Json::StaticString FIELD_OFFSET("offset"); const static Json::StaticString FIELD_LEN("len"); const static Json::StaticString FIELD_VISIBLE("visible"); -FieldMeta::FieldMeta() : attr_type_(AttrType::UNDEFINED), attr_offset_(-1), attr_len_(0), visible_(false) -{} +FieldMeta::FieldMeta() : attr_type_(AttrType::UNDEFINED), attr_offset_(-1), attr_len_(0), visible_(false) {} FieldMeta::FieldMeta(const char *name, AttrType attr_type, int attr_offset, int attr_len, bool visible) { @@ -42,45 +41,30 @@ RC FieldMeta::init(const char *name, AttrType attr_type, int attr_offset, int at } if (AttrType::UNDEFINED == attr_type || attr_offset < 0 || attr_len <= 0) { - LOG_WARN( - "Invalid argument. name=%s, attr_type=%d, attr_offset=%d, attr_len=%d", name, attr_type, attr_offset, attr_len); + LOG_WARN("Invalid argument. name=%s, attr_type=%d, attr_offset=%d, attr_len=%d", + name, attr_type, attr_offset, attr_len); return RC::INVALID_ARGUMENT; } - name_ = name; - attr_type_ = attr_type; - attr_len_ = attr_len; + name_ = name; + attr_type_ = attr_type; + attr_len_ = attr_len; attr_offset_ = attr_offset; - visible_ = visible; + visible_ = visible; LOG_INFO("Init a field with name=%s", name); return RC::SUCCESS; } -const char *FieldMeta::name() const -{ - return name_.c_str(); -} +const char *FieldMeta::name() const { return name_.c_str(); } -AttrType FieldMeta::type() const -{ - return attr_type_; -} +AttrType FieldMeta::type() const { return attr_type_; } -int FieldMeta::offset() const -{ - return attr_offset_; -} +int FieldMeta::offset() const { return attr_offset_; } -int FieldMeta::len() const -{ - return attr_len_; -} +int FieldMeta::len() const { return attr_len_; } -bool FieldMeta::visible() const -{ - return visible_; -} +bool FieldMeta::visible() const { return visible_; } void FieldMeta::desc(std::ostream &os) const { @@ -90,10 +74,10 @@ void FieldMeta::desc(std::ostream &os) const void FieldMeta::to_json(Json::Value &json_value) const { - json_value[FIELD_NAME] = name_; - json_value[FIELD_TYPE] = attr_type_to_string(attr_type_); - json_value[FIELD_OFFSET] = attr_offset_; - json_value[FIELD_LEN] = attr_len_; + json_value[FIELD_NAME] = name_; + json_value[FIELD_TYPE] = attr_type_to_string(attr_type_); + json_value[FIELD_OFFSET] = attr_offset_; + json_value[FIELD_LEN] = attr_len_; json_value[FIELD_VISIBLE] = visible_; } @@ -104,10 +88,10 @@ RC FieldMeta::from_json(const Json::Value &json_value, FieldMeta &field) return RC::INTERNAL; } - const Json::Value &name_value = json_value[FIELD_NAME]; - const Json::Value &type_value = json_value[FIELD_TYPE]; - const Json::Value &offset_value = json_value[FIELD_OFFSET]; - const Json::Value &len_value = json_value[FIELD_LEN]; + const Json::Value &name_value = json_value[FIELD_NAME]; + const Json::Value &type_value = json_value[FIELD_TYPE]; + const Json::Value &offset_value = json_value[FIELD_OFFSET]; + const Json::Value &len_value = json_value[FIELD_LEN]; const Json::Value &visible_value = json_value[FIELD_VISIBLE]; if (!name_value.isString()) { @@ -138,9 +122,9 @@ RC FieldMeta::from_json(const Json::Value &json_value, FieldMeta &field) return RC::INTERNAL; } - const char *name = name_value.asCString(); - int offset = offset_value.asInt(); - int len = len_value.asInt(); - bool visible = visible_value.asBool(); + const char *name = name_value.asCString(); + int offset = offset_value.asInt(); + int len = len_value.asInt(); + bool visible = visible_value.asBool(); return field.init(name, type, offset, len, visible); } diff --git a/src/observer/storage/field/field_meta.h b/src/observer/storage/field/field_meta.h index e0aec4bf9..799729ea0 100644 --- a/src/observer/storage/field/field_meta.h +++ b/src/observer/storage/field/field_meta.h @@ -25,9 +25,9 @@ class Value; /** * @brief 字段元数据 - * + * */ -class FieldMeta +class FieldMeta { public: FieldMeta(); @@ -38,22 +38,22 @@ class FieldMeta public: const char *name() const; - AttrType type() const; - int offset() const; - int len() const; - bool visible() const; + AttrType type() const; + int offset() const; + int len() const; + bool visible() const; public: void desc(std::ostream &os) const; public: - void to_json(Json::Value &json_value) const; + void to_json(Json::Value &json_value) const; static RC from_json(const Json::Value &json_value, FieldMeta &field); protected: std::string name_; - AttrType attr_type_; - int attr_offset_; - int attr_len_; - bool visible_; + AttrType attr_type_; + int attr_offset_; + int attr_len_; + bool visible_; }; diff --git a/src/observer/storage/index/bplus_tree.cpp b/src/observer/storage/index/bplus_tree.cpp index c3ac5b8a3..e72f5f81b 100644 --- a/src/observer/storage/index/bplus_tree.cpp +++ b/src/observer/storage/index/bplus_tree.cpp @@ -13,10 +13,10 @@ See the Mulan PSL v2 for more details. */ // Rewritten by Longda & Wangyunlai // #include "storage/index/bplus_tree.h" -#include "storage/buffer/disk_buffer_pool.h" +#include "common/lang/lower_bound.h" #include "common/log/log.h" #include "sql/parser/parse_defs.h" -#include "common/lang/lower_bound.h" +#include "storage/buffer/disk_buffer_pool.h" using namespace std; using namespace common; @@ -26,15 +26,14 @@ using namespace common; int calc_internal_page_capacity(int attr_length) { int item_size = attr_length + sizeof(RID) + sizeof(PageNum); - - int capacity = ((int)BP_PAGE_DATA_SIZE - InternalIndexNode::HEADER_SIZE) / item_size; + int capacity = ((int)BP_PAGE_DATA_SIZE - InternalIndexNode::HEADER_SIZE) / item_size; return capacity; } int calc_leaf_page_capacity(int attr_length) { int item_size = attr_length + sizeof(RID) + sizeof(RID); - int capacity = ((int)BP_PAGE_DATA_SIZE - LeafIndexNode::HEADER_SIZE) / item_size; + int capacity = ((int)BP_PAGE_DATA_SIZE - LeafIndexNode::HEADER_SIZE) / item_size; return capacity; } @@ -43,25 +42,16 @@ IndexNodeHandler::IndexNodeHandler(const IndexFileHeader &header, Frame *frame) : header_(header), page_num_(frame->page_num()), node_((IndexNode *)frame->data()) {} -bool IndexNodeHandler::is_leaf() const -{ - return node_->is_leaf; -} +bool IndexNodeHandler::is_leaf() const { return node_->is_leaf; } void IndexNodeHandler::init_empty(bool leaf) { node_->is_leaf = leaf; node_->key_num = 0; - node_->parent = BP_INVALID_PAGE_NUM; -} -PageNum IndexNodeHandler::page_num() const -{ - return page_num_; + node_->parent = BP_INVALID_PAGE_NUM; } +PageNum IndexNodeHandler::page_num() const { return page_num_; } -int IndexNodeHandler::key_size() const -{ - return header_.key_length; -} +int IndexNodeHandler::key_size() const { return header_.key_length; } int IndexNodeHandler::value_size() const { @@ -69,41 +59,23 @@ int IndexNodeHandler::value_size() const return sizeof(RID); } -int IndexNodeHandler::item_size() const -{ - return key_size() + value_size(); -} +int IndexNodeHandler::item_size() const { return key_size() + value_size(); } -int IndexNodeHandler::size() const -{ - return node_->key_num; -} +int IndexNodeHandler::size() const { return node_->key_num; } -int IndexNodeHandler::max_size() const -{ - return is_leaf() ? header_.leaf_max_size : header_.internal_max_size; -} +int IndexNodeHandler::max_size() const { return is_leaf() ? header_.leaf_max_size : header_.internal_max_size; } int IndexNodeHandler::min_size() const { const int max = this->max_size(); - return max - max/2; + return max - max / 2; } -void IndexNodeHandler::increase_size(int n) -{ - node_->key_num += n; -} +void IndexNodeHandler::increase_size(int n) { node_->key_num += n; } -PageNum IndexNodeHandler::parent_page_num() const -{ - return node_->parent; -} +PageNum IndexNodeHandler::parent_page_num() const { return node_->parent; } -void IndexNodeHandler::set_parent_page_num(PageNum page_num) -{ - this->node_->parent = page_num; -} +void IndexNodeHandler::set_parent_page_num(PageNum page_num) { this->node_->parent = page_num; } /** * 检查一个节点经过插入或删除操作后是否需要分裂或合并操作 @@ -122,10 +94,11 @@ bool IndexNodeHandler::is_safe(BplusTreeOperationType op, bool is_root_node) case BplusTreeOperationType::DELETE: { if (is_root_node) { // 参考adjust_root if (node_->is_leaf) { - return size() > 1; // 根节点如果空的话,就需要删除整棵树 + return size() > 1; // 根节点如果空的话,就需要删除整棵树 } // not leaf - return size() > 2; // 根节点还有子节点,但是如果删除一个子节点后,只剩一个子节点,就要把自己删除,把唯一的子节点变更为根节点 + // 根节点还有子节点,但是如果删除一个子节点后,只剩一个子节点,就要把自己删除,把唯一的子节点变更为根节点 + return size() > 2; } return size() > min_size(); } break; @@ -177,15 +150,9 @@ void LeafIndexNodeHandler::init_empty() leaf_node_->next_brother = BP_INVALID_PAGE_NUM; } -void LeafIndexNodeHandler::set_next_page(PageNum page_num) -{ - leaf_node_->next_brother = page_num; -} +void LeafIndexNodeHandler::set_next_page(PageNum page_num) { leaf_node_->next_brother = page_num; } -PageNum LeafIndexNodeHandler::next_page() const -{ - return leaf_node_->next_brother; -} +PageNum LeafIndexNodeHandler::next_page() const { return leaf_node_->next_brother; } char *LeafIndexNodeHandler::key_at(int index) { @@ -201,7 +168,7 @@ char *LeafIndexNodeHandler::value_at(int index) int LeafIndexNodeHandler::lookup(const KeyComparator &comparator, const char *key, bool *found /* = nullptr */) const { - const int size = this->size(); + const int size = this->size(); common::BinaryIterator iter_begin(item_size(), __key_at(0)); common::BinaryIterator iter_end(item_size(), __key_at(size)); common::BinaryIterator iter = lower_bound(iter_begin, iter_end, key, comparator, found); @@ -229,7 +196,7 @@ void LeafIndexNodeHandler::remove(int index) int LeafIndexNodeHandler::remove(const char *key, const KeyComparator &comparator) { bool found = false; - int index = lookup(comparator, key, &found); + int index = lookup(comparator, key, &found); if (found) { this->remove(index); return 1; @@ -239,7 +206,7 @@ int LeafIndexNodeHandler::remove(const char *key, const KeyComparator &comparato RC LeafIndexNodeHandler::move_half_to(LeafIndexNodeHandler &other, DiskBufferPool *bp) { - const int size = this->size(); + const int size = this->size(); const int move_index = size / 2; memcpy(other.__item_at(0), this->__item_at(move_index), static_cast(item_size()) * (size - move_index)); @@ -293,24 +260,14 @@ void LeafIndexNodeHandler::preappend(const char *item) increase_size(1); } -char *LeafIndexNodeHandler::__item_at(int index) const -{ - return leaf_node_->array + (index * item_size()); -} -char *LeafIndexNodeHandler::__key_at(int index) const -{ - return __item_at(index); -} -char *LeafIndexNodeHandler::__value_at(int index) const -{ - return __item_at(index) + key_size(); -} +char *LeafIndexNodeHandler::__item_at(int index) const { return leaf_node_->array + (index * item_size()); } +char *LeafIndexNodeHandler::__key_at(int index) const { return __item_at(index); } +char *LeafIndexNodeHandler::__value_at(int index) const { return __item_at(index) + key_size(); } std::string to_string(const LeafIndexNodeHandler &handler, const KeyPrinter &printer) { std::stringstream ss; - ss << to_string((const IndexNodeHandler &)handler) - << ",next page:" << handler.next_page(); + ss << to_string((const IndexNodeHandler &)handler) << ",next page:" << handler.next_page(); ss << ",values=[" << printer(handler.__key_at(0)); for (int i = 1; i < handler.size(); i++) { ss << "," << printer(handler.__key_at(i)); @@ -341,14 +298,14 @@ bool LeafIndexNodeHandler::validate(const KeyComparator &comparator, DiskBufferP } Frame *parent_frame; - RC rc = bp->get_this_page(parent_page_num, &parent_frame); + RC rc = bp->get_this_page(parent_page_num, &parent_frame); if (rc != RC::SUCCESS) { LOG_WARN("failed to fetch parent page. page num=%d, rc=%d:%s", parent_page_num, rc, strrc(rc)); return false; } InternalIndexNodeHandler parent_node(header_, parent_frame); - int index_in_parent = parent_node.value_index(this->page_num()); + int index_in_parent = parent_node.value_index(this->page_num()); if (index_in_parent < 0) { LOG_WARN("invalid leaf node. cannot find index in parent. this page num=%d, parent page num=%d", this->page_num(), parent_page_num); @@ -401,10 +358,7 @@ std::string to_string(const InternalIndexNodeHandler &node, const KeyPrinter &pr return ss.str(); } -void InternalIndexNodeHandler::init_empty() -{ - IndexNodeHandler::init_empty(false); -} +void InternalIndexNodeHandler::init_empty() { IndexNodeHandler::init_empty(false); } void InternalIndexNodeHandler::create_new_root(PageNum first_page_num, const char *key, PageNum page_num) { memset(__key_at(0), 0, key_size()); @@ -424,7 +378,9 @@ void InternalIndexNodeHandler::insert(const char *key, PageNum page_num, const K int insert_position = -1; lookup(comparator, key, nullptr, &insert_position); if (insert_position < size()) { - memmove(__item_at(insert_position + 1), __item_at(insert_position), (static_cast(size()) - insert_position) * item_size()); + memmove(__item_at(insert_position + 1), + __item_at(insert_position), + (static_cast(size()) - insert_position) * item_size()); } memcpy(__item_at(insert_position), key, key_size()); memcpy(__value_at(insert_position), &page_num, value_size()); @@ -433,9 +389,9 @@ void InternalIndexNodeHandler::insert(const char *key, PageNum page_num, const K RC InternalIndexNodeHandler::move_half_to(InternalIndexNodeHandler &other, DiskBufferPool *bp) { - const int size = this->size(); + const int size = this->size(); const int move_index = size / 2; - RC rc = other.copy_from(this->__item_at(move_index), size - move_index, bp); + RC rc = other.copy_from(this->__item_at(move_index), size - move_index, bp); if (rc != RC::SUCCESS) { LOG_WARN("failed to copy item to new node. rc=%d:%s", rc, strrc(rc)); return rc; @@ -467,7 +423,7 @@ int InternalIndexNodeHandler::lookup(const KeyComparator &comparator, const char common::BinaryIterator iter_begin(item_size(), __key_at(1)); common::BinaryIterator iter_end(item_size(), __key_at(size)); common::BinaryIterator iter = lower_bound(iter_begin, iter_end, key, comparator, found); - int ret = static_cast(iter - iter_begin) + 1; + int ret = static_cast(iter - iter_begin) + 1; if (insert_position) { *insert_position = ret; } @@ -560,12 +516,12 @@ RC InternalIndexNodeHandler::copy_from(const char *items, int num, DiskBufferPoo { memcpy(__item_at(this->size()), items, static_cast(num) * item_size()); - RC rc = RC::SUCCESS; + RC rc = RC::SUCCESS; PageNum this_page_num = this->page_num(); - Frame *frame = nullptr; + Frame *frame = nullptr; for (int i = 0; i < num; i++) { const PageNum page_num = *(const PageNum *)((items + i * item_size()) + key_size()); - rc = disk_buffer_pool->get_this_page(page_num, &frame); + rc = disk_buffer_pool->get_this_page(page_num, &frame); if (rc != RC::SUCCESS) { LOG_WARN("failed to set child's page num. child page num:%d, this page num=%d, rc=%d:%s", page_num, this_page_num, rc, strrc(rc)); @@ -580,15 +536,13 @@ RC InternalIndexNodeHandler::copy_from(const char *items, int num, DiskBufferPoo return rc; } -RC InternalIndexNodeHandler::append(const char *item, DiskBufferPool *bp) -{ - return this->copy_from(item, 1, bp); -} +RC InternalIndexNodeHandler::append(const char *item, DiskBufferPool *bp) { return this->copy_from(item, 1, bp); } RC InternalIndexNodeHandler::preappend(const char *item, DiskBufferPool *bp) { PageNum child_page_num = *(PageNum *)(item + key_size()); - Frame *frame = nullptr; + Frame *frame = nullptr; + RC rc = bp->get_this_page(child_page_num, &frame); if (rc != RC::SUCCESS) { LOG_WARN("failed to fetch child page. rc=%d:%s", rc, strrc(rc)); @@ -610,30 +564,15 @@ RC InternalIndexNodeHandler::preappend(const char *item, DiskBufferPool *bp) return RC::SUCCESS; } -char *InternalIndexNodeHandler::__item_at(int index) const -{ - return internal_node_->array + (index * item_size()); -} +char *InternalIndexNodeHandler::__item_at(int index) const { return internal_node_->array + (index * item_size()); } -char *InternalIndexNodeHandler::__key_at(int index) const -{ - return __item_at(index); -} +char *InternalIndexNodeHandler::__key_at(int index) const { return __item_at(index); } -char *InternalIndexNodeHandler::__value_at(int index) const -{ - return __item_at(index) + key_size(); -} +char *InternalIndexNodeHandler::__value_at(int index) const { return __item_at(index) + key_size(); } -int InternalIndexNodeHandler::value_size() const -{ - return sizeof(PageNum); -} +int InternalIndexNodeHandler::value_size() const { return sizeof(PageNum); } -int InternalIndexNodeHandler::item_size() const -{ - return key_size() + this->value_size(); -} +int InternalIndexNodeHandler::item_size() const { return key_size() + this->value_size(); } bool InternalIndexNodeHandler::validate(const KeyComparator &comparator, DiskBufferPool *bp) const { @@ -657,7 +596,7 @@ bool InternalIndexNodeHandler::validate(const KeyComparator &comparator, DiskBuf LOG_WARN("this page num=%d, got invalid child page. page num=%d", this->page_num(), page_num); } else { Frame *child_frame; - RC rc = bp->get_this_page(page_num, &child_frame); + RC rc = bp->get_this_page(page_num, &child_frame); if (rc != RC::SUCCESS) { LOG_WARN("failed to fetch child page while validate internal page. page num=%d, rc=%d:%s", page_num, rc, strrc(rc)); @@ -683,13 +622,14 @@ bool InternalIndexNodeHandler::validate(const KeyComparator &comparator, DiskBuf } Frame *parent_frame; - RC rc = bp->get_this_page(parent_page_num, &parent_frame); + RC rc = bp->get_this_page(parent_page_num, &parent_frame); if (rc != RC::SUCCESS) { LOG_WARN("failed to fetch parent page. page num=%d, rc=%d:%s", parent_page_num, rc, strrc(rc)); return false; } InternalIndexNodeHandler parent_node(header_, parent_frame); + int index_in_parent = parent_node.value_index(this->page_num()); if (index_in_parent < 0) { LOG_WARN("invalid internal node. cannot find index in parent. this page num=%d, parent page num=%d", @@ -730,6 +670,7 @@ RC BplusTreeHandler::sync() { if (header_dirty_) { Frame *frame = nullptr; + RC rc = disk_buffer_pool_->get_this_page(FIRST_INDEX_PAGE, &frame); if (OB_SUCC(rc) && frame != nullptr) { char *pdata = frame->data(); @@ -749,6 +690,7 @@ RC BplusTreeHandler::create(const char *file_name, AttrType attr_type, int attr_ int leaf_max_size /* = -1 */) { BufferPoolManager &bpm = BufferPoolManager::instance(); + RC rc = bpm.create_file(file_name); if (rc != RC::SUCCESS) { LOG_WARN("Failed to create file. file name=%s, rc=%d:%s", file_name, rc, strrc(rc)); @@ -757,6 +699,7 @@ RC BplusTreeHandler::create(const char *file_name, AttrType attr_type, int attr_ LOG_INFO("Successfully create index file:%s", file_name); DiskBufferPool *bp = nullptr; + rc = bpm.open_file(file_name, bp); if (rc != RC::SUCCESS) { LOG_WARN("Failed to open file. file name=%s, rc=%d:%s", file_name, rc, strrc(rc)); @@ -786,14 +729,14 @@ RC BplusTreeHandler::create(const char *file_name, AttrType attr_type, int attr_ leaf_max_size = calc_leaf_page_capacity(attr_length); } - char *pdata = header_frame->data(); - IndexFileHeader *file_header = (IndexFileHeader *)pdata; - file_header->attr_length = attr_length; - file_header->key_length = attr_length + sizeof(RID); - file_header->attr_type = attr_type; + char *pdata = header_frame->data(); + IndexFileHeader *file_header = (IndexFileHeader *)pdata; + file_header->attr_length = attr_length; + file_header->key_length = attr_length + sizeof(RID); + file_header->attr_type = attr_type; file_header->internal_max_size = internal_max_size; - file_header->leaf_max_size = leaf_max_size; - file_header->root_page = BP_INVALID_PAGE_NUM; + file_header->leaf_max_size = leaf_max_size; + file_header->root_page = BP_INVALID_PAGE_NUM; header_frame->mark_dirty(); @@ -826,16 +769,17 @@ RC BplusTreeHandler::open(const char *file_name) return RC::RECORD_OPENNED; } - BufferPoolManager &bpm = BufferPoolManager::instance(); - DiskBufferPool *disk_buffer_pool; + BufferPoolManager &bpm = BufferPoolManager::instance(); + DiskBufferPool *disk_buffer_pool = nullptr; + RC rc = bpm.open_file(file_name, disk_buffer_pool); if (rc != RC::SUCCESS) { LOG_WARN("Failed to open file name=%s, rc=%d:%s", file_name, rc, strrc(rc)); return rc; } - Frame *frame; - rc = disk_buffer_pool->get_this_page(FIRST_INDEX_PAGE, &frame); + Frame *frame = nullptr; + rc = disk_buffer_pool->get_this_page(FIRST_INDEX_PAGE, &frame); if (rc != RC::SUCCESS) { LOG_WARN("Failed to get first page file name=%s, rc=%d:%s", file_name, rc, strrc(rc)); bpm.close_file(file_name); @@ -844,7 +788,7 @@ RC BplusTreeHandler::open(const char *file_name) char *pdata = frame->data(); memcpy(&file_header_, pdata, sizeof(IndexFileHeader)); - header_dirty_ = false; + header_dirty_ = false; disk_buffer_pool_ = disk_buffer_pool; mem_pool_item_ = make_unique(file_name); @@ -891,7 +835,7 @@ RC BplusTreeHandler::print_internal_node_recursive(Frame *frame) int node_size = internal_node.size(); for (int i = 0; i < node_size; i++) { PageNum page_num = internal_node.value_at(i); - Frame *child_frame; + Frame *child_frame; rc = disk_buffer_pool_->get_this_page(page_num, &child_frame); if (rc != RC::SUCCESS) { LOG_WARN("failed to fetch child page. page id=%d, rc=%d:%s", page_num, rc, strrc(rc)); @@ -927,8 +871,9 @@ RC BplusTreeHandler::print_tree() return RC::SUCCESS; } - Frame *frame; + Frame *frame = nullptr; PageNum page_num = file_header_.root_page; + RC rc = disk_buffer_pool_->get_this_page(page_num, &frame); if (rc != RC::SUCCESS) { LOG_WARN("failed to fetch page. page id=%d, rc=%d:%s", page_num, rc, strrc(rc)); @@ -952,7 +897,7 @@ RC BplusTreeHandler::print_leafs() } LatchMemo latch_memo(disk_buffer_pool_); - Frame *frame = nullptr; + Frame *frame = nullptr; RC rc = left_most_page(latch_memo, frame); if (rc != RC::SUCCESS) { @@ -982,7 +927,7 @@ RC BplusTreeHandler::print_leafs() bool BplusTreeHandler::validate_node_recursive(LatchMemo &latch_memo, Frame *frame) { - bool result = true; + bool result = true; IndexNodeHandler node(file_header_, frame); if (node.is_leaf()) { LeafIndexNodeHandler leaf_node(file_header_, frame); @@ -992,8 +937,8 @@ bool BplusTreeHandler::validate_node_recursive(LatchMemo &latch_memo, Frame *fra result = internal_node.validate(key_comparator_, disk_buffer_pool_); for (int i = 0; result && i < internal_node.size(); i++) { PageNum page_num = internal_node.value_at(i); - Frame *child_frame; - RC rc = latch_memo.get_page(page_num, child_frame); + Frame *child_frame; + RC rc = latch_memo.get_page(page_num, child_frame); if (rc != RC::SUCCESS) { LOG_WARN("failed to fetch child page.page id=%d, rc=%d:%s", page_num, rc, strrc(rc)); result = false; @@ -1014,6 +959,7 @@ bool BplusTreeHandler::validate_leaf_link(LatchMemo &latch_memo) } Frame *frame = nullptr; + RC rc = left_most_page(latch_memo, frame); if (rc != RC::SUCCESS) { LOG_WARN("failed to fetch left most page. rc=%d:%s", rc, strrc(rc)); @@ -1021,7 +967,7 @@ bool BplusTreeHandler::validate_leaf_link(LatchMemo &latch_memo) } LeafIndexNodeHandler leaf_node(file_header_, frame); - PageNum next_page_num = leaf_node.next_page(); + PageNum next_page_num = leaf_node.next_page(); MemPoolItem::unique_ptr prev_key = mem_pool_item_->alloc_unique_ptr(); memcpy(prev_key.get(), leaf_node.key_at(leaf_node.size() - 1), file_header_.key_length); @@ -1055,8 +1001,8 @@ bool BplusTreeHandler::validate_tree() } LatchMemo latch_memo(disk_buffer_pool_); - Frame *frame = nullptr; - RC rc = latch_memo.get_page(file_header_.root_page, frame); // 这里仅仅调试使用,不加root锁 + Frame *frame = nullptr; + RC rc = latch_memo.get_page(file_header_.root_page, frame); // 这里仅仅调试使用,不加root锁 if (rc != RC::SUCCESS) { LOG_WARN("failed to fetch root page. page id=%d, rc=%d:%s", file_header_.root_page, rc, strrc(rc)); return false; @@ -1072,16 +1018,13 @@ bool BplusTreeHandler::validate_tree() return true; } -bool BplusTreeHandler::is_empty() const -{ - return file_header_.root_page == BP_INVALID_PAGE_NUM; -} +bool BplusTreeHandler::is_empty() const { return file_header_.root_page == BP_INVALID_PAGE_NUM; } RC BplusTreeHandler::find_leaf(LatchMemo &latch_memo, BplusTreeOperationType op, const char *key, Frame *&frame) { auto child_page_getter = [this, key](InternalIndexNodeHandler &internal_node) { - return internal_node.value_at(internal_node.lookup(key_comparator_, key)); - }; + return internal_node.value_at(internal_node.lookup(key_comparator_, key)); + }; return find_leaf_internal(latch_memo, op, child_page_getter, frame); } @@ -1091,10 +1034,8 @@ RC BplusTreeHandler::left_most_page(LatchMemo &latch_memo, Frame *&frame) return find_leaf_internal(latch_memo, BplusTreeOperationType::READ, child_page_getter, frame); } -RC BplusTreeHandler::find_leaf_internal( - LatchMemo &latch_memo, BplusTreeOperationType op, - const std::function &child_page_getter, - Frame *&frame) +RC BplusTreeHandler::find_leaf_internal(LatchMemo &latch_memo, BplusTreeOperationType op, + const std::function &child_page_getter, Frame *&frame) { // root locked if (op != BplusTreeOperationType::READ) { @@ -1107,18 +1048,18 @@ RC BplusTreeHandler::find_leaf_internal( return RC::EMPTY; } - RC rc = crabing_protocal_fetch_page(latch_memo, op, file_header_.root_page, true/* is_root_node */, frame); + RC rc = crabing_protocal_fetch_page(latch_memo, op, file_header_.root_page, true /* is_root_node */, frame); if (rc != RC::SUCCESS) { LOG_WARN("failed to fetch root page. page id=%d, rc=%d:%s", file_header_.root_page, rc, strrc(rc)); return rc; } IndexNode *node = (IndexNode *)frame->data(); - PageNum next_page_id; - for (; !node->is_leaf; ) { + PageNum next_page_id; + for (; !node->is_leaf;) { InternalIndexNodeHandler internal_node(file_header_, frame); next_page_id = child_page_getter(internal_node); - rc = crabing_protocal_fetch_page(latch_memo, op, next_page_id, false /* is_root_node */, frame); + rc = crabing_protocal_fetch_page(latch_memo, op, next_page_id, false /* is_root_node */, frame); if (rc != RC::SUCCESS) { LOG_WARN("Failed to load page page_num:%d. rc=%s", next_page_id, strrc(rc)); return rc; @@ -1129,14 +1070,12 @@ RC BplusTreeHandler::find_leaf_internal( return RC::SUCCESS; } -RC BplusTreeHandler::crabing_protocal_fetch_page(LatchMemo &latch_memo, - BplusTreeOperationType op, - PageNum page_num, - bool is_root_node, - Frame *&frame) +RC BplusTreeHandler::crabing_protocal_fetch_page( + LatchMemo &latch_memo, BplusTreeOperationType op, PageNum page_num, bool is_root_node, Frame *&frame) { - bool readonly = (op == BplusTreeOperationType::READ); + bool readonly = (op == BplusTreeOperationType::READ); const int memo_point = latch_memo.memo_point(); + RC rc = latch_memo.get_page(page_num, frame); if (rc != RC::SUCCESS) { LOG_WARN("failed to get frame. pageNum=%d, rc=%s", page_num, strrc(rc)); @@ -1147,7 +1086,7 @@ RC BplusTreeHandler::crabing_protocal_fetch_page(LatchMemo &latch_memo, latch_memo.latch(frame, latch_type); IndexNodeHandler index_node(file_header_, frame); if (index_node.is_safe(op, is_root_node)) { - latch_memo.release_to(memo_point); // 当前节点不会分裂或合并,可以将前面的锁都释放掉 + latch_memo.release_to(memo_point); // 当前节点不会分裂或合并,可以将前面的锁都释放掉 } return rc; } @@ -1155,8 +1094,8 @@ RC BplusTreeHandler::crabing_protocal_fetch_page(LatchMemo &latch_memo, RC BplusTreeHandler::insert_entry_into_leaf_node(LatchMemo &latch_memo, Frame *frame, const char *key, const RID *rid) { LeafIndexNodeHandler leaf_node(file_header_, frame); - bool exists = false; // 该数据是否已经存在指定的叶子节点中了 - int insert_position = leaf_node.lookup(key_comparator_, key, &exists); + bool exists = false; // 该数据是否已经存在指定的叶子节点中了 + int insert_position = leaf_node.lookup(key_comparator_, key, &exists); if (exists) { LOG_TRACE("entry exists"); return RC::RECORD_DUPLICATE_KEY; @@ -1170,7 +1109,7 @@ RC BplusTreeHandler::insert_entry_into_leaf_node(LatchMemo &latch_memo, Frame *f } Frame *new_frame = nullptr; - RC rc = split(latch_memo, frame, new_frame); + RC rc = split(latch_memo, frame, new_frame); if (rc != RC::SUCCESS) { LOG_WARN("failed to split leaf node. rc=%d:%s", rc, strrc(rc)); return rc; @@ -1196,7 +1135,7 @@ RC BplusTreeHandler::insert_entry_into_parent(LatchMemo &latch_memo, Frame *fram IndexNodeHandler node_handler(file_header_, frame); IndexNodeHandler new_node_handler(file_header_, new_frame); - PageNum parent_page_num = node_handler.parent_page_num(); + PageNum parent_page_num = node_handler.parent_page_num(); if (parent_page_num == BP_INVALID_PAGE_NUM) { @@ -1219,7 +1158,7 @@ RC BplusTreeHandler::insert_entry_into_parent(LatchMemo &latch_memo, Frame *fram // disk_buffer_pool_->unpin_page(frame); // disk_buffer_pool_->unpin_page(new_frame); - root_frame->write_latch(); // 在root页面更新之后,别人就可以访问到了,这时候就要加上锁 + root_frame->write_latch(); // 在root页面更新之后,别人就可以访问到了,这时候就要加上锁 update_root_page_num_locked(root_frame->page_num()); root_frame->mark_dirty(); root_frame->write_unlatch(); @@ -1230,6 +1169,7 @@ RC BplusTreeHandler::insert_entry_into_parent(LatchMemo &latch_memo, Frame *fram } else { Frame *parent_frame = nullptr; + rc = latch_memo.get_page(parent_page_num, parent_frame); if (rc != RC::SUCCESS) { LOG_WARN("failed to insert entry into leaf. rc=%d:%s", rc, strrc(rc)); @@ -1256,6 +1196,7 @@ RC BplusTreeHandler::insert_entry_into_parent(LatchMemo &latch_memo, Frame *fram // 当前父节点即将装满了,那只能再将父节点执行分裂操作 Frame *new_parent_frame = nullptr; + rc = split(latch_memo, parent_frame, new_parent_frame); if (rc != RC::SUCCESS) { LOG_WARN("failed to split internal node. rc=%d:%s", rc, strrc(rc)); @@ -1307,7 +1248,7 @@ RC BplusTreeHandler::split(LatchMemo &latch_memo, Frame *frame, Frame *&new_fram new_node.init_empty(); new_node.set_parent_page_num(old_node.parent_page_num()); - old_node.move_half_to(new_node, disk_buffer_pool_); // TODO remove disk buffer pool + old_node.move_half_to(new_node, disk_buffer_pool_); // TODO remove disk buffer pool frame->mark_dirty(); new_frame->mark_dirty(); @@ -1317,7 +1258,7 @@ RC BplusTreeHandler::split(LatchMemo &latch_memo, Frame *frame, Frame *&new_fram void BplusTreeHandler::update_root_page_num_locked(PageNum root_page_num) { file_header_.root_page = root_page_num; - header_dirty_ = true; + header_dirty_ = true; LOG_DEBUG("set root page to %d", root_page_num); } @@ -1331,6 +1272,7 @@ RC BplusTreeHandler::create_new_tree(const char *key, const RID *rid) } Frame *frame = nullptr; + rc = disk_buffer_pool_->allocate_page(&frame); if (rc != RC::SUCCESS) { LOG_WARN("failed to allocate root page. rc=%d:%s", rc, strrc(rc)); @@ -1388,7 +1330,7 @@ RC BplusTreeHandler::insert_entry(const char *user_key, const RID *rid) LatchMemo latch_memo(disk_buffer_pool_); Frame *frame = nullptr; - RC rc = find_leaf(latch_memo, BplusTreeOperationType::INSERT, key, frame); + RC rc = find_leaf(latch_memo, BplusTreeOperationType::INSERT, key, frame); if (rc != RC::SUCCESS) { LOG_WARN("Failed to find leaf %s. rc=%d:%s", rid->to_string().c_str(), rc, strrc(rc)); return rc; @@ -1445,8 +1387,8 @@ RC BplusTreeHandler::adjust_root(LatchMemo &latch_memo, Frame *root_frame) InternalIndexNodeHandler internal_node(file_header_, root_frame); const PageNum child_page_num = internal_node.value_at(0); - Frame *child_frame = nullptr; - RC rc = latch_memo.get_page(child_page_num, child_frame); + Frame *child_frame = nullptr; + RC rc = latch_memo.get_page(child_page_num, child_frame); if (rc != RC::SUCCESS) { LOG_WARN("failed to fetch child page. page num=%d, rc=%d:%s", child_page_num, rc, strrc(rc)); return rc; @@ -1486,6 +1428,7 @@ RC BplusTreeHandler::coalesce_or_redistribute(LatchMemo &latch_memo, Frame *fram } Frame *parent_frame = nullptr; + RC rc = latch_memo.get_page(parent_page_num, parent_frame); if (rc != RC::SUCCESS) { LOG_WARN("failed to fetch parent page. page id=%d, rc=%d:%s", parent_page_num, rc, strrc(rc)); @@ -1493,11 +1436,12 @@ RC BplusTreeHandler::coalesce_or_redistribute(LatchMemo &latch_memo, Frame *fram } InternalIndexNodeHandler parent_index_node(file_header_, parent_frame); + int index = parent_index_node.lookup(key_comparator_, index_node.key_at(index_node.size() - 1)); ASSERT(parent_index_node.value_at(index) == frame->page_num(), "lookup return an invalid value. index=%d, this page num=%d, but got %d", index, frame->page_num(), parent_index_node.value_at(index)); - + PageNum neighbor_page_num; if (index == 0) { neighbor_page_num = parent_index_node.value_at(1); @@ -1506,7 +1450,8 @@ RC BplusTreeHandler::coalesce_or_redistribute(LatchMemo &latch_memo, Frame *fram } Frame *neighbor_frame = nullptr; - rc = latch_memo.get_page(neighbor_page_num, neighbor_frame); // 当前已经拥有了父节点的写锁,所以直接尝试获取此页面然后加锁 + rc = latch_memo.get_page( + neighbor_page_num, neighbor_frame); // 当前已经拥有了父节点的写锁,所以直接尝试获取此页面然后加锁 if (rc != RC::SUCCESS) { LOG_WARN("failed to fetch neighbor page. page id=%d, rc=%d:%s", neighbor_page_num, rc, strrc(rc)); // do something to release resource @@ -1526,19 +1471,20 @@ RC BplusTreeHandler::coalesce_or_redistribute(LatchMemo &latch_memo, Frame *fram } template -RC BplusTreeHandler::coalesce(LatchMemo &latch_memo, Frame *neighbor_frame, Frame *frame, Frame *parent_frame, int index) +RC BplusTreeHandler::coalesce( + LatchMemo &latch_memo, Frame *neighbor_frame, Frame *frame, Frame *parent_frame, int index) { InternalIndexNodeHandler parent_node(file_header_, parent_frame); - Frame *left_frame = nullptr; + Frame *left_frame = nullptr; Frame *right_frame = nullptr; if (index == 0) { // neighbor node is at right - left_frame = frame; + left_frame = frame; right_frame = neighbor_frame; index++; } else { - left_frame = neighbor_frame; + left_frame = neighbor_frame; right_frame = frame; // neighbor is at left } @@ -1569,8 +1515,8 @@ template RC BplusTreeHandler::redistribute(Frame *neighbor_frame, Frame *frame, Frame *parent_frame, int index) { InternalIndexNodeHandler parent_node(file_header_, parent_frame); - IndexNodeHandlerType neighbor_node(file_header_, neighbor_frame); - IndexNodeHandlerType node(file_header_, frame); + IndexNodeHandlerType neighbor_node(file_header_, neighbor_frame); + IndexNodeHandlerType node(file_header_, frame); if (neighbor_node.size() < node.size()) { LOG_ERROR("got invalid nodes. neighbor node size %d, this node size %d", neighbor_node.size(), node.size()); } @@ -1631,15 +1577,16 @@ RC BplusTreeHandler::delete_entry(const char *user_key, const RID *rid) memcpy(key + file_header_.attr_length, rid, sizeof(*rid)); BplusTreeOperationType op = BplusTreeOperationType::DELETE; - LatchMemo latch_memo(disk_buffer_pool_); + LatchMemo latch_memo(disk_buffer_pool_); Frame *leaf_frame = nullptr; + RC rc = find_leaf(latch_memo, op, key, leaf_frame); if (rc == RC::EMPTY) { rc = RC::RECORD_NOT_EXIST; return rc; } - + if (rc != RC::SUCCESS) { LOG_WARN("failed to find leaf page. rc =%s", strrc(rc)); return rc; @@ -1650,18 +1597,14 @@ RC BplusTreeHandler::delete_entry(const char *user_key, const RID *rid) //////////////////////////////////////////////////////////////////////////////// -BplusTreeScanner::BplusTreeScanner(BplusTreeHandler &tree_handler) - : tree_handler_(tree_handler), - latch_memo_(tree_handler.disk_buffer_pool_) +BplusTreeScanner::BplusTreeScanner(BplusTreeHandler &tree_handler) + : tree_handler_(tree_handler), latch_memo_(tree_handler.disk_buffer_pool_) {} -BplusTreeScanner::~BplusTreeScanner() -{ - close(); -} +BplusTreeScanner::~BplusTreeScanner() { close(); } -RC BplusTreeScanner::open(const char *left_user_key, int left_len, bool left_inclusive, - const char *right_user_key, int right_len, bool right_inclusive) +RC BplusTreeScanner::open(const char *left_user_key, int left_len, bool left_inclusive, const char *right_user_key, + int right_len, bool right_inclusive) { RC rc = RC::SUCCESS; if (inited_) { @@ -1669,13 +1612,13 @@ RC BplusTreeScanner::open(const char *left_user_key, int left_len, bool left_inc return RC::INTERNAL; } - inited_ = true; + inited_ = true; first_emitted_ = false; // 校验输入的键值是否是合法范围 if (left_user_key && right_user_key) { const auto &attr_comparator = tree_handler_.key_comparator_.attr_comparator(); - const int result = attr_comparator(left_user_key, right_user_key); + const int result = attr_comparator(left_user_key, right_user_key); if (result > 0 || // left < right // left == right but is (left,right)/[left,right) or (left,right] (result == 0 && (left_inclusive == false || right_inclusive == false))) { @@ -1723,17 +1666,16 @@ RC BplusTreeScanner::open(const char *left_user_key, int left_len, bool left_inc rc = tree_handler_.find_leaf(latch_memo_, BplusTreeOperationType::READ, left_key, current_frame_); if (rc == RC::EMPTY) { - rc = RC::SUCCESS; + rc = RC::SUCCESS; current_frame_ = nullptr; return rc; } else if (rc != RC::SUCCESS) { LOG_WARN("failed to find left page. rc=%s", strrc(rc)); return rc; } - LeafIndexNodeHandler left_node(tree_handler_.file_header_, current_frame_); - int left_index = left_node.lookup(tree_handler_.key_comparator_, left_key); + int left_index = left_node.lookup(tree_handler_.key_comparator_, left_key); // lookup 返回的是适合插入的位置,还需要判断一下是否在合适的边界范围内 if (left_index >= left_node.size()) { // 超出了当前页,就需要向后移动一个位置 const PageNum next_page_num = left_node.next_page(); @@ -1760,8 +1702,8 @@ RC BplusTreeScanner::open(const char *left_user_key, int left_len, bool left_inc right_key_ = nullptr; } else { - char *fixed_right_key = const_cast(right_user_key); - bool should_include_after_fix = false; + char *fixed_right_key = const_cast(right_user_key); + bool should_include_after_fix = false; if (tree_handler_.file_header_.attr_type == CHARS) { rc = fix_user_key(right_user_key, right_len, false /*want_greater*/, &fixed_right_key, &should_include_after_fix); if (rc != RC::SUCCESS) { @@ -1803,10 +1745,11 @@ bool BplusTreeScanner::touch_end() if (right_key_ == nullptr) { return false; } - + LeafIndexNodeHandler node(tree_handler_.file_header_, current_frame_); - const char *this_key = node.key_at(iter_index_); - int compare_result = tree_handler_.key_comparator_(this_key, static_cast(right_key_.get())); + + const char *this_key = node.key_at(iter_index_); + int compare_result = tree_handler_.key_comparator_(this_key, static_cast(right_key_.get())); return compare_result > 0; } @@ -1834,14 +1777,14 @@ RC BplusTreeScanner::next_entry(RID &rid) return RC::SUCCESS; } - RC rc = RC::SUCCESS; + RC rc = RC::SUCCESS; PageNum next_page_num = node.next_page(); if (BP_INVALID_PAGE_NUM == next_page_num) { return RC::RECORD_EOF; } const int memo_point = latch_memo_.memo_point(); - rc = latch_memo_.get_page(next_page_num, current_frame_); + rc = latch_memo_.get_page(next_page_num, current_frame_); if (rc != RC::SUCCESS) { LOG_WARN("failed to get next page. page num=%d, rc=%s", next_page_num, strrc(rc)); return rc; @@ -1858,7 +1801,7 @@ RC BplusTreeScanner::next_entry(RID &rid) } latch_memo_.release_to(memo_point); - iter_index_ = -1; // `next` will add 1 + iter_index_ = -1; // `next` will add 1 return next_entry(rid); } @@ -1883,7 +1826,7 @@ RC BplusTreeScanner::fix_user_key( *should_inclusive = false; int32_t attr_length = tree_handler_.file_header_.attr_length; - char *key_buf = new (std::nothrow) char[attr_length]; + char *key_buf = new (std::nothrow) char[attr_length]; if (nullptr == key_buf) { return RC::NOMEM; } diff --git a/src/observer/storage/index/bplus_tree.h b/src/observer/storage/index/bplus_tree.h index 7594ce725..32249b3f3 100644 --- a/src/observer/storage/index/bplus_tree.h +++ b/src/observer/storage/index/bplus_tree.h @@ -17,17 +17,17 @@ See the Mulan PSL v2 for more details. */ #pragma once -#include -#include #include #include +#include +#include -#include "storage/record/record_manager.h" -#include "storage/buffer/disk_buffer_pool.h" -#include "storage/trx/latch_memo.h" -#include "sql/parser/parse_defs.h" #include "common/lang/comparator.h" #include "common/log/log.h" +#include "sql/parser/parse_defs.h" +#include "storage/buffer/disk_buffer_pool.h" +#include "storage/record/record_manager.h" +#include "storage/trx/latch_memo.h" /** * @brief B+树的实现 @@ -49,19 +49,16 @@ enum class BplusTreeOperationType * @brief 属性比较(BplusTree) * @ingroup BPlusTree */ -class AttrComparator +class AttrComparator { public: void init(AttrType type, int length) { - attr_type_ = type; + attr_type_ = type; attr_length_ = length; } - int attr_length() const - { - return attr_length_; - } + int attr_length() const { return attr_length_; } int operator()(const char *v1, const char *v2) const { @@ -84,7 +81,7 @@ class AttrComparator private: AttrType attr_type_; - int attr_length_; + int attr_length_; }; /** @@ -92,18 +89,12 @@ class AttrComparator * @details BplusTree的键值除了字段属性,还有RID,是为了避免属性值重复而增加的。 * @ingroup BPlusTree */ -class KeyComparator +class KeyComparator { public: - void init(AttrType type, int length) - { - attr_comparator_.init(type, length); - } + void init(AttrType type, int length) { attr_comparator_.init(type, length); } - const AttrComparator &attr_comparator() const - { - return attr_comparator_; - } + const AttrComparator &attr_comparator() const { return attr_comparator_; } int operator()(const char *v1, const char *v2) const { @@ -125,19 +116,16 @@ class KeyComparator * @brief 属性打印,调试使用(BplusTree) * @ingroup BPlusTree */ -class AttrPrinter +class AttrPrinter { public: void init(AttrType type, int length) { - attr_type_ = type; + attr_type_ = type; attr_length_ = length; } - int attr_length() const - { - return attr_length_; - } + int attr_length() const { return attr_length_; } std::string operator()(const char *v) const { @@ -167,25 +155,19 @@ class AttrPrinter private: AttrType attr_type_; - int attr_length_; + int attr_length_; }; /** * @brief 键值打印,调试使用(BplusTree) * @ingroup BPlusTree */ -class KeyPrinter +class KeyPrinter { public: - void init(AttrType type, int length) - { - attr_printer_.init(type, length); - } + void init(AttrType type, int length) { attr_printer_.init(type, length); } - const AttrPrinter &attr_printer() const - { - return attr_printer_; - } + const AttrPrinter &attr_printer() const { return attr_printer_; } std::string operator()(const char *v) const { @@ -207,19 +189,19 @@ class KeyPrinter * @details this is the first page of bplus tree. * only one field can be supported, can you extend it to multi-fields? */ -struct IndexFileHeader +struct IndexFileHeader { IndexFileHeader() { memset(this, 0, sizeof(IndexFileHeader)); root_page = BP_INVALID_PAGE_NUM; } - PageNum root_page; ///< 根节点在磁盘中的页号 - int32_t internal_max_size; ///< 内部节点最大的键值对数 - int32_t leaf_max_size; ///< 叶子节点最大的键值对数 - int32_t attr_length; ///< 键值的长度 - int32_t key_length; ///< attr length + sizeof(RID) - AttrType attr_type; ///< 键值的类型 + PageNum root_page; ///< 根节点在磁盘中的页号 + int32_t internal_max_size; ///< 内部节点最大的键值对数 + int32_t leaf_max_size; ///< 叶子节点最大的键值对数 + int32_t attr_length; ///< 键值的长度 + int32_t key_length; ///< attr length + sizeof(RID) + AttrType attr_type; ///< 键值的类型 const std::string to_string() { @@ -242,9 +224,9 @@ struct IndexFileHeader * @code * storage format: * | page type | item number | parent page id | - * @endcode + * @endcode */ -struct IndexNode +struct IndexNode { static constexpr int HEADER_SIZE = 12; @@ -260,13 +242,13 @@ struct IndexNode * storage format: * | common header | prev page id | next page id | * | key0, rid0 | key1, rid1 | ... | keyn, ridn | - * @endcode + * @endcode * the key is in format: the key value of record and rid. * so the key in leaf page must be unique. * the value is rid. * can you implenment a cluster index ? */ -struct LeafIndexNode : public IndexNode +struct LeafIndexNode : public IndexNode { static constexpr int HEADER_SIZE = IndexNode::HEADER_SIZE + 4; @@ -288,7 +270,7 @@ struct LeafIndexNode : public IndexNode * the first key is ignored(key0). * so it will waste space, can you fix this? */ -struct InternalIndexNode : public IndexNode +struct InternalIndexNode : public IndexNode { static constexpr int HEADER_SIZE = IndexNode::HEADER_SIZE; @@ -304,7 +286,7 @@ struct InternalIndexNode : public IndexNode * IndexNodeHandler 负责对IndexNode做各种操作。 * 作为一个类来说,虚函数会影响“结构体”真实的内存布局,所以将数据存储与操作分开 */ -class IndexNodeHandler +class IndexNodeHandler { public: IndexNodeHandler(const IndexFileHeader &header, Frame *frame); @@ -317,11 +299,11 @@ class IndexNodeHandler int value_size() const; int item_size() const; - void increase_size(int n); - int size() const; - int max_size() const; - int min_size() const; - void set_parent_page_num(PageNum page_num); + void increase_size(int n); + int size() const; + int max_size() const; + int min_size() const; + void set_parent_page_num(PageNum page_num); PageNum parent_page_num() const; PageNum page_num() const; @@ -333,22 +315,22 @@ class IndexNodeHandler protected: const IndexFileHeader &header_; - PageNum page_num_; - IndexNode *node_; + PageNum page_num_; + IndexNode *node_; }; /** * @brief 叶子节点的操作 * @ingroup BPlusTree */ -class LeafIndexNodeHandler : public IndexNodeHandler +class LeafIndexNodeHandler : public IndexNodeHandler { public: LeafIndexNodeHandler(const IndexFileHeader &header, Frame *frame); virtual ~LeafIndexNodeHandler() = default; - void init_empty(); - void set_next_page(PageNum page_num); + void init_empty(); + void set_next_page(PageNum page_num); PageNum next_page() const; char *key_at(int index); @@ -363,9 +345,9 @@ class LeafIndexNodeHandler : public IndexNodeHandler void insert(int index, const char *key, const char *value); void remove(int index); int remove(const char *key, const KeyComparator &comparator); - RC move_half_to(LeafIndexNodeHandler &other, DiskBufferPool *bp); - RC move_first_to_end(LeafIndexNodeHandler &other, DiskBufferPool *disk_buffer_pool); - RC move_last_to_front(LeafIndexNodeHandler &other, DiskBufferPool *bp); + RC move_half_to(LeafIndexNodeHandler &other, DiskBufferPool *bp); + RC move_first_to_end(LeafIndexNodeHandler &other, DiskBufferPool *disk_buffer_pool); + RC move_last_to_front(LeafIndexNodeHandler &other, DiskBufferPool *bp); /** * move all items to left page */ @@ -391,7 +373,7 @@ class LeafIndexNodeHandler : public IndexNodeHandler * @brief 内部节点的操作 * @ingroup BPlusTree */ -class InternalIndexNodeHandler : public IndexNodeHandler +class InternalIndexNodeHandler : public IndexNodeHandler { public: InternalIndexNodeHandler(const IndexFileHeader &header, Frame *frame); @@ -400,15 +382,15 @@ class InternalIndexNodeHandler : public IndexNodeHandler void init_empty(); void create_new_root(PageNum first_page_num, const char *key, PageNum page_num); - void insert(const char *key, PageNum page_num, const KeyComparator &comparator); - RC move_half_to(LeafIndexNodeHandler &other, DiskBufferPool *bp); - char *key_at(int index); + void insert(const char *key, PageNum page_num, const KeyComparator &comparator); + RC move_half_to(LeafIndexNodeHandler &other, DiskBufferPool *bp); + char *key_at(int index); PageNum value_at(int index); /** * 返回指定子节点在当前节点中的索引 */ - int value_index(PageNum page_num); + int value_index(PageNum page_num); void set_key_at(int index, const char *key); void remove(int index); @@ -420,10 +402,8 @@ class InternalIndexNodeHandler : public IndexNodeHandler * @param[out] found 如果是有效指针,将会返回当前是否存在指定的键值 * @param[out] insert_position 如果是有效指针,将会返回可以插入指定键值的位置 */ - int lookup(const KeyComparator &comparator, - const char *key, - bool *found = nullptr, - int *insert_position = nullptr) const; + int lookup( + const KeyComparator &comparator, const char *key, bool *found = nullptr, int *insert_position = nullptr) const; RC move_to(InternalIndexNodeHandler &other, DiskBufferPool *disk_buffer_pool); RC move_first_to_end(InternalIndexNodeHandler &other, DiskBufferPool *disk_buffer_pool); @@ -455,18 +435,15 @@ class InternalIndexNodeHandler : public IndexNodeHandler * @brief B+树的实现 * @ingroup BPlusTree */ -class BplusTreeHandler +class BplusTreeHandler { public: /** * 此函数创建一个名为fileName的索引。 * attrType描述被索引属性的类型,attrLength描述被索引属性的长度 */ - RC create(const char *file_name, - AttrType attr_type, - int attr_length, - int internal_max_size = -1, - int leaf_max_size = -1); + RC create( + const char *file_name, AttrType attr_type, int attr_length, int internal_max_size = -1, int leaf_max_size = -1); /** * 打开名为fileName的索引文件。 @@ -533,14 +510,13 @@ class BplusTreeHandler protected: RC find_leaf(LatchMemo &latch_memo, BplusTreeOperationType op, const char *key, Frame *&frame); RC left_most_page(LatchMemo &latch_memo, Frame *&frame); - RC find_leaf_internal(LatchMemo &latch_memo, BplusTreeOperationType op, - const std::function &child_page_getter, - Frame *&frame); - RC crabing_protocal_fetch_page(LatchMemo &latch_memo, BplusTreeOperationType op, PageNum page_num, bool is_root_page, - Frame *&frame); + RC find_leaf_internal(LatchMemo &latch_memo, BplusTreeOperationType op, + const std::function &child_page_getter, Frame *&frame); + RC crabing_protocal_fetch_page( + LatchMemo &latch_memo, BplusTreeOperationType op, PageNum page_num, bool is_root_page, Frame *&frame); - RC insert_into_parent(LatchMemo &latch_memo, PageNum parent_page, Frame *left_frame, const char *pkey, - Frame &right_frame); + RC insert_into_parent( + LatchMemo &latch_memo, PageNum parent_page, Frame *left_frame, const char *pkey, Frame &right_frame); RC delete_entry_internal(LatchMemo &latch_memo, Frame *leaf_frame, const char *key); @@ -564,19 +540,19 @@ class BplusTreeHandler private: common::MemPoolItem::unique_ptr make_key(const char *user_key, const RID &rid); - void free_key(char *key); + void free_key(char *key); protected: DiskBufferPool *disk_buffer_pool_ = nullptr; - bool header_dirty_ = false; // + bool header_dirty_ = false; // IndexFileHeader file_header_; // 在调整根节点时,需要加上这个锁。 // 这个锁可以使用递归读写锁,但是这里偷懒先不改 - common::SharedMutex root_lock_; + common::SharedMutex root_lock_; - KeyComparator key_comparator_; - KeyPrinter key_printer_; + KeyComparator key_comparator_; + KeyPrinter key_printer_; std::unique_ptr mem_pool_item_; @@ -589,7 +565,7 @@ class BplusTreeHandler * @brief B+树的扫描器 * @ingroup BPlusTree */ -class BplusTreeScanner +class BplusTreeScanner { public: BplusTreeScanner(BplusTreeHandler &tree_handler); @@ -604,8 +580,8 @@ class BplusTreeScanner * @param right_len right_user_key 的内存大小(只有在变长字段中才会关注) * @param right_inclusive 右边界的值是否包含在内 */ - RC open(const char *left_user_key, int left_len, bool left_inclusive, - const char *right_user_key, int right_len, bool right_inclusive); + RC open(const char *left_user_key, int left_len, bool left_inclusive, const char *right_user_key, int right_len, + bool right_inclusive); RC next_entry(RID &rid); @@ -621,7 +597,7 @@ class BplusTreeScanner bool touch_end(); private: - bool inited_ = false; + bool inited_ = false; BplusTreeHandler &tree_handler_; LatchMemo latch_memo_; @@ -631,6 +607,6 @@ class BplusTreeScanner Frame *current_frame_ = nullptr; common::MemPoolItem::unique_ptr right_key_; - int iter_index_ = -1; - bool first_emitted_ = false; + int iter_index_ = -1; + bool first_emitted_ = false; }; diff --git a/src/observer/storage/index/bplus_tree_index.cpp b/src/observer/storage/index/bplus_tree_index.cpp index 4d13e3889..b64fb0c7c 100644 --- a/src/observer/storage/index/bplus_tree_index.cpp +++ b/src/observer/storage/index/bplus_tree_index.cpp @@ -15,18 +15,13 @@ See the Mulan PSL v2 for more details. */ #include "storage/index/bplus_tree_index.h" #include "common/log/log.h" -BplusTreeIndex::~BplusTreeIndex() noexcept -{ - close(); -} +BplusTreeIndex::~BplusTreeIndex() noexcept { close(); } RC BplusTreeIndex::create(const char *file_name, const IndexMeta &index_meta, const FieldMeta &field_meta) { if (inited_) { LOG_WARN("Failed to create index due to the index has been created before. file_name:%s, index:%s, field:%s", - file_name, - index_meta.name(), - index_meta.field()); + file_name, index_meta.name(), index_meta.field()); return RC::RECORD_OPENNED; } @@ -35,16 +30,13 @@ RC BplusTreeIndex::create(const char *file_name, const IndexMeta &index_meta, co RC rc = index_handler_.create(file_name, field_meta.type(), field_meta.len()); if (RC::SUCCESS != rc) { LOG_WARN("Failed to create index_handler, file_name:%s, index:%s, field:%s, rc:%s", - file_name, - index_meta.name(), - index_meta.field(), - strrc(rc)); + file_name, index_meta.name(), index_meta.field(), strrc(rc)); return rc; } inited_ = true; - LOG_INFO( - "Successfully create index, file_name:%s, index:%s, field:%s", file_name, index_meta.name(), index_meta.field()); + LOG_INFO("Successfully create index, file_name:%s, index:%s, field:%s", + file_name, index_meta.name(), index_meta.field()); return RC::SUCCESS; } @@ -52,9 +44,7 @@ RC BplusTreeIndex::open(const char *file_name, const IndexMeta &index_meta, cons { if (inited_) { LOG_WARN("Failed to open index due to the index has been initedd before. file_name:%s, index:%s, field:%s", - file_name, - index_meta.name(), - index_meta.field()); + file_name, index_meta.name(), index_meta.field()); return RC::RECORD_OPENNED; } @@ -63,16 +53,13 @@ RC BplusTreeIndex::open(const char *file_name, const IndexMeta &index_meta, cons RC rc = index_handler_.open(file_name); if (RC::SUCCESS != rc) { LOG_WARN("Failed to open index_handler, file_name:%s, index:%s, field:%s, rc:%s", - file_name, - index_meta.name(), - index_meta.field(), - strrc(rc)); + file_name, index_meta.name(), index_meta.field(), strrc(rc)); return rc; } inited_ = true; - LOG_INFO( - "Successfully open index, file_name:%s, index:%s, field:%s", file_name, index_meta.name(), index_meta.field()); + LOG_INFO("Successfully open index, file_name:%s, index:%s, field:%s", + file_name, index_meta.name(), index_meta.field()); return RC::SUCCESS; } @@ -110,19 +97,12 @@ IndexScanner *BplusTreeIndex::create_scanner( return index_scanner; } -RC BplusTreeIndex::sync() -{ - return index_handler_.sync(); -} +RC BplusTreeIndex::sync() { return index_handler_.sync(); } //////////////////////////////////////////////////////////////////////////////// -BplusTreeIndexScanner::BplusTreeIndexScanner(BplusTreeHandler &tree_handler) : tree_scanner_(tree_handler) -{} +BplusTreeIndexScanner::BplusTreeIndexScanner(BplusTreeHandler &tree_handler) : tree_scanner_(tree_handler) {} -BplusTreeIndexScanner::~BplusTreeIndexScanner() noexcept -{ - tree_scanner_.close(); -} +BplusTreeIndexScanner::~BplusTreeIndexScanner() noexcept { tree_scanner_.close(); } RC BplusTreeIndexScanner::open( const char *left_key, int left_len, bool left_inclusive, const char *right_key, int right_len, bool right_inclusive) @@ -130,10 +110,7 @@ RC BplusTreeIndexScanner::open( return tree_scanner_.open(left_key, left_len, left_inclusive, right_key, right_len, right_inclusive); } -RC BplusTreeIndexScanner::next_entry(RID *rid) -{ - return tree_scanner_.next_entry(*rid); -} +RC BplusTreeIndexScanner::next_entry(RID *rid) { return tree_scanner_.next_entry(*rid); } RC BplusTreeIndexScanner::destroy() { diff --git a/src/observer/storage/index/bplus_tree_index.h b/src/observer/storage/index/bplus_tree_index.h index ba7b2ec25..ad7baf0d9 100644 --- a/src/observer/storage/index/bplus_tree_index.h +++ b/src/observer/storage/index/bplus_tree_index.h @@ -14,14 +14,14 @@ See the Mulan PSL v2 for more details. */ #pragma once -#include "storage/index/index.h" #include "storage/index/bplus_tree.h" +#include "storage/index/index.h" /** * @brief B+树索引 * @ingroup Index */ -class BplusTreeIndex : public Index +class BplusTreeIndex : public Index { public: BplusTreeIndex() = default; @@ -43,7 +43,7 @@ class BplusTreeIndex : public Index RC sync() override; private: - bool inited_ = false; + bool inited_ = false; BplusTreeHandler index_handler_; }; @@ -51,7 +51,7 @@ class BplusTreeIndex : public Index * @brief B+树索引扫描器 * @ingroup Index */ -class BplusTreeIndexScanner : public IndexScanner +class BplusTreeIndexScanner : public IndexScanner { public: BplusTreeIndexScanner(BplusTreeHandler &tree_handle); diff --git a/src/observer/storage/index/index.h b/src/observer/storage/index/index.h index b31519884..f02b426fe 100644 --- a/src/observer/storage/index/index.h +++ b/src/observer/storage/index/index.h @@ -18,8 +18,8 @@ See the Mulan PSL v2 for more details. */ #include #include "common/rc.h" -#include "storage/index/index_meta.h" #include "storage/field/field_meta.h" +#include "storage/index/index_meta.h" #include "storage/record/record_manager.h" class IndexScanner; @@ -34,20 +34,17 @@ class IndexScanner; * @brief 索引基类 * @ingroup Index */ -class Index +class Index { public: - Index() = default; + Index() = default; virtual ~Index() = default; - const IndexMeta &index_meta() const - { - return index_meta_; - } + const IndexMeta &index_meta() const { return index_meta_; } /** * @brief 插入一条数据 - * + * * @param record 插入的记录,当前假设记录是定长的 * @param[out] rid 插入的记录的位置 */ @@ -55,7 +52,7 @@ class Index /** * @brief 删除一条数据 - * + * * @param record 删除的记录,当前假设记录是定长的 * @param[in] rid 删除的记录的位置 */ @@ -63,7 +60,7 @@ class Index /** * @brief 创建一个索引数据的扫描器 - * + * * @param left_key 要扫描的左边界 * @param left_len 左边界的长度 * @param left_inclusive 是否包含左边界 @@ -76,7 +73,7 @@ class Index /** * @brief 同步索引数据到磁盘 - * + * */ virtual RC sync() = 0; @@ -92,10 +89,10 @@ class Index * @brief 索引扫描器 * @ingroup Index */ -class IndexScanner +class IndexScanner { public: - IndexScanner() = default; + IndexScanner() = default; virtual ~IndexScanner() = default; /** @@ -103,5 +100,5 @@ class IndexScanner * 如果没有更多的元素,返回RECORD_EOF */ virtual RC next_entry(RID *rid) = 0; - virtual RC destroy() = 0; + virtual RC destroy() = 0; }; diff --git a/src/observer/storage/index/index_meta.cpp b/src/observer/storage/index/index_meta.cpp index bad6c7496..1ce3ef6d3 100644 --- a/src/observer/storage/index/index_meta.cpp +++ b/src/observer/storage/index/index_meta.cpp @@ -13,10 +13,10 @@ See the Mulan PSL v2 for more details. */ // #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 "storage/field/field_meta.h" +#include "storage/table/table_meta.h" #include "json/json.h" const static Json::StaticString FIELD_NAME("name"); @@ -29,20 +29,20 @@ RC IndexMeta::init(const char *name, const FieldMeta &field) return RC::INVALID_ARGUMENT; } - name_ = name; + name_ = name; field_ = field.name(); return RC::SUCCESS; } void IndexMeta::to_json(Json::Value &json_value) const { - json_value[FIELD_NAME] = name_; + json_value[FIELD_NAME] = name_; json_value[FIELD_FIELD_NAME] = field_; } RC IndexMeta::from_json(const TableMeta &table, const Json::Value &json_value, IndexMeta &index) { - const Json::Value &name_value = json_value[FIELD_NAME]; + const Json::Value &name_value = json_value[FIELD_NAME]; const Json::Value &field_value = json_value[FIELD_FIELD_NAME]; if (!name_value.isString()) { LOG_ERROR("Index name is not a string. json value=%s", name_value.toStyledString().c_str()); @@ -51,8 +51,7 @@ RC IndexMeta::from_json(const TableMeta &table, const Json::Value &json_value, I if (!field_value.isString()) { LOG_ERROR("Field name of index [%s] is not a string. json value=%s", - name_value.asCString(), - field_value.toStyledString().c_str()); + name_value.asCString(), field_value.toStyledString().c_str()); return RC::INTERNAL; } @@ -65,17 +64,8 @@ RC IndexMeta::from_json(const TableMeta &table, const Json::Value &json_value, I return index.init(name_value.asCString(), *field); } -const char *IndexMeta::name() const -{ - return name_.c_str(); -} +const char *IndexMeta::name() const { return name_.c_str(); } -const char *IndexMeta::field() const -{ - return field_.c_str(); -} +const char *IndexMeta::field() const { return field_.c_str(); } -void IndexMeta::desc(std::ostream &os) const -{ - os << "index name=" << name_ << ", field=" << field_; -} \ No newline at end of file +void IndexMeta::desc(std::ostream &os) const { os << "index name=" << name_ << ", field=" << field_; } \ No newline at end of file diff --git a/src/observer/storage/index/index_meta.h b/src/observer/storage/index/index_meta.h index 1c3750e02..4ab410710 100644 --- a/src/observer/storage/index/index_meta.h +++ b/src/observer/storage/index/index_meta.h @@ -14,8 +14,8 @@ See the Mulan PSL v2 for more details. */ #pragma once -#include #include "common/rc.h" +#include class TableMeta; class FieldMeta; @@ -30,7 +30,7 @@ class Value; * @details 一个索引包含了表的哪些字段,索引的名称等。 * 如果以后实现了多种类型的索引,还需要记录索引的类型,对应类型的一些元数据等 */ -class IndexMeta +class IndexMeta { public: IndexMeta() = default; @@ -44,7 +44,7 @@ class IndexMeta void desc(std::ostream &os) const; public: - void to_json(Json::Value &json_value) const; + void to_json(Json::Value &json_value) const; static RC from_json(const TableMeta &table, const Json::Value &json_value, IndexMeta &index); protected: diff --git a/src/observer/storage/persist/persist.cpp b/src/observer/storage/persist/persist.cpp index 2e9a96c71..aa3224bb8 100644 --- a/src/observer/storage/persist/persist.cpp +++ b/src/observer/storage/persist/persist.cpp @@ -15,16 +15,12 @@ See the Mulan PSL v2 for more details. */ #include #include -#include "persist.h" #include "common/log/log.h" +#include "persist.h" -PersistHandler::PersistHandler() -{} +PersistHandler::PersistHandler() {} -PersistHandler::~PersistHandler() -{ - close_file(); -} +PersistHandler::~PersistHandler() { close_file(); } RC PersistHandler::create_file(const char *file_name) { @@ -54,7 +50,7 @@ RC PersistHandler::create_file(const char *file_name) RC PersistHandler::open_file(const char *file_name) { int fd; - RC rc = RC::SUCCESS; + RC rc = RC::SUCCESS; if (file_name == nullptr) { if (file_name_.empty()) { LOG_ERROR("Failed to open file, because no file name."); @@ -141,10 +137,7 @@ RC PersistHandler::write_file(int size, const char *data, int64_t *out_size) int64_t write_size = 0; if ((write_size = write(file_desc_, data, size)) != size) { LOG_ERROR("Failed to write %d:%s due to %s. Write size: %lld", - file_desc_, - file_name_.c_str(), - strerror(errno), - write_size); + file_desc_, file_name_.c_str(), strerror(errno), write_size); rc = RC::FILE_WRITE; } if (out_size != nullptr) { @@ -167,20 +160,13 @@ RC PersistHandler::write_at(uint64_t offset, int size, const char *data, int64_t } else { if (lseek(file_desc_, offset, SEEK_SET) == off_t(-1)) { LOG_ERROR("Failed to write %lld of %d:%s due to failed to seek %s.", - offset, - file_desc_, - file_name_.c_str(), - strerror(errno)); + offset, file_desc_, file_name_.c_str(), strerror(errno)); rc = RC::FILE_SEEK; } else { int64_t write_size = 0; if ((write_size = write(file_desc_, data, size)) != size) { LOG_ERROR("Failed to write %llu of %d:%s due to %s. Write size: %lld", - offset, - file_desc_, - file_name_.c_str(), - strerror(errno), - write_size); + offset, file_desc_, file_name_.c_str(), strerror(errno), write_size); rc = RC::FILE_WRITE; } if (out_size != nullptr) { @@ -203,17 +189,14 @@ RC PersistHandler::append(int size, const char *data, int64_t *out_size) rc = RC::FILE_NOT_OPENED; } else { if (lseek(file_desc_, 0, SEEK_END) == off_t(-1)) { - LOG_ERROR( - "Failed to append file %d:%s due to failed to seek: %s.", file_desc_, file_name_.c_str(), strerror(errno)); + LOG_ERROR("Failed to append file %d:%s due to failed to seek: %s.", + file_desc_, file_name_.c_str(), strerror(errno)); rc = RC::FILE_SEEK; } else { int64_t write_size = 0; if ((write_size = write(file_desc_, data, size)) != size) { LOG_ERROR("Failed to append file %d:%s due to %s. Write size: %lld", - file_desc_, - file_name_.c_str(), - strerror(errno), - write_size); + file_desc_, file_name_.c_str(), strerror(errno), write_size); rc = RC::FILE_WRITE; } if (out_size != nullptr) { @@ -260,18 +243,15 @@ RC PersistHandler::read_at(uint64_t offset, int size, char *data, int64_t *out_s } else { if (lseek(file_desc_, offset, SEEK_SET) == off_t(-1)) { LOG_ERROR("Failed to read %llu of %d:%s due to failed to seek %s.", - offset, - file_desc_, - file_name_.c_str(), - strerror(errno)); + offset, file_desc_, file_name_.c_str(), strerror(errno)); return RC::FILE_SEEK; } else { ssize_t read_size = read(file_desc_, data, size); if (read_size == 0) { LOG_TRACE("read file touch the end. file name=%s", file_name_.c_str()); } else if (read_size < 0) { - LOG_WARN("failed to read file. file name=%s, offset=%lld, size=%d, error=%s", file_name_.c_str(), offset, size, - strerror(errno)); + LOG_WARN("failed to read file. file name=%s, offset=%lld, size=%d, error=%s", + file_name_.c_str(), offset, size, strerror(errno)); rc = RC::FILE_READ; } else if (out_size != nullptr) { *out_size = read_size; diff --git a/src/observer/storage/persist/persist.h b/src/observer/storage/persist/persist.h index c9ae94690..863c1f122 100644 --- a/src/observer/storage/persist/persist.h +++ b/src/observer/storage/persist/persist.h @@ -13,18 +13,18 @@ See the Mulan PSL v2 for more details. */ // #pragma once -#include -#include #include #include #include #include #include #include +#include +#include #include "common/rc.h" -class PersistHandler +class PersistHandler { public: PersistHandler(); @@ -62,5 +62,5 @@ class PersistHandler private: std::string file_name_; - int file_desc_ = -1; + int file_desc_ = -1; }; diff --git a/src/observer/storage/record/record.h b/src/observer/storage/record/record.h index c4b95c60c..a926cdb98 100644 --- a/src/observer/storage/record/record.h +++ b/src/observer/storage/record/record.h @@ -14,16 +14,16 @@ See the Mulan PSL v2 for more details. */ #pragma once -#include -#include #include #include +#include +#include +#include "common/log/log.h" #include "common/rc.h" #include "common/types.h" -#include "common/log/log.h" -#include "storage/index/index_meta.h" #include "storage/field/field_meta.h" +#include "storage/index/index_meta.h" class Field; @@ -158,6 +158,6 @@ class Record RID rid_; char *data_ = nullptr; - int len_ = 0; /// 如果不是record自己来管理内存,这个字段可能是无效的 - bool owner_ = false; /// 表示当前是否由record来管理内存 + int len_ = 0; /// 如果不是record自己来管理内存,这个字段可能是无效的 + bool owner_ = false; /// 表示当前是否由record来管理内存 }; diff --git a/src/observer/storage/record/record_manager.cpp b/src/observer/storage/record/record_manager.cpp index 6991c6a9c..691d5e50f 100644 --- a/src/observer/storage/record/record_manager.cpp +++ b/src/observer/storage/record/record_manager.cpp @@ -12,8 +12,8 @@ See the Mulan PSL v2 for more details. */ // Created by Meiyi & Longda on 2021/4/13. // #include "storage/record/record_manager.h" -#include "common/log/log.h" #include "common/lang/bitmap.h" +#include "common/log/log.h" #include "storage/common/condition_filter.h" #include "storage/trx/trx.h" @@ -24,7 +24,7 @@ static constexpr int PAGE_HEADER_SIZE = (sizeof(PageHeader)); /** * @brief 8字节对齐 * 注: ceiling(a / b) = floor((a + b - 1) / b) - * + * * @param size 待对齐的字节数 */ int align8(int size) { return (size + 7) / 8 * 8; } @@ -107,7 +107,7 @@ RC RecordPageHandler::init(DiskBufferPool &buffer_pool, PageNum page_num, bool r readonly_ = readonly; page_header_ = (PageHeader *)(data); bitmap_ = data + PAGE_HEADER_SIZE; - + LOG_TRACE("Successfully init page_num %d.", page_num); return ret; } diff --git a/src/observer/storage/record/record_manager.h b/src/observer/storage/record/record_manager.h index 93c051bb7..fcd0ef7c4 100644 --- a/src/observer/storage/record/record_manager.h +++ b/src/observer/storage/record/record_manager.h @@ -14,12 +14,12 @@ See the Mulan PSL v2 for more details. */ // #pragma once -#include -#include +#include "common/lang/bitmap.h" #include "storage/buffer/disk_buffer_pool.h" -#include "storage/trx/latch_memo.h" #include "storage/record/record.h" -#include "common/lang/bitmap.h" +#include "storage/trx/latch_memo.h" +#include +#include class ConditionFilter; class RecordPageHandler; @@ -29,7 +29,7 @@ class Table; /** * @brief 这里负责管理在一个文件上表记录(行)的组织/管理 * @defgroup RecordManager - * + * * @details 表记录管理的内容包括如何在文件上存放、读取、检索。也就是记录的增删改查。 * 这里的文件都会被拆分成页面,每个页面都有一样的大小。更详细的信息可以参考BufferPool。 * 按照BufferPool的设计,第一个页面用来存放BufferPool本身的元数据,比如当前文件有多少页面、已经分配了多少页面、 @@ -94,10 +94,10 @@ class RecordPageIterator /** * @brief 读取下一个记录到record中包括RID和数据,并更新下一个记录位置next_slot_num_ - * + * * @param record 返回的下一个记录 */ - RC next(Record &record); + RC next(Record &record); /** * 该迭代器是否有效 @@ -138,7 +138,7 @@ class RecordPageHandler /** * @brief 数据库恢复时,与普通的运行场景有所不同,不做任何并发操作,也不需要加锁 - * + * * @param buffer_pool 关联某个文件时,都通过buffer pool来做读写文件 * @param page_num 操作的页面编号 */ @@ -168,7 +168,7 @@ class RecordPageHandler /** * @brief 数据库恢复时,在指定位置插入数据 - * + * * @param data 要插入的数据行 * @param rid 插入的位置 */ @@ -201,15 +201,16 @@ class RecordPageHandler protected: /** - * @details + * @details * 前面在计算record_capacity时并没有考虑对齐,但第一个record需要8字节对齐 * 因此按此前计算的record_capacity,最后一个记录的部分数据可能会被挤出页面 * 所以需要对record_capacity进行修正,保证记录不会溢出 */ - void fix_record_capacity() { - int32_t last_record_offset = page_header_->first_record_offset + - page_header_->record_capacity * page_header_->record_size; - while(last_record_offset > BP_PAGE_DATA_SIZE) { + void fix_record_capacity() + { + int32_t last_record_offset = + page_header_->first_record_offset + page_header_->record_capacity * page_header_->record_size; + while (last_record_offset > BP_PAGE_DATA_SIZE) { page_header_->record_capacity -= 1; last_record_offset -= page_header_->record_size; } @@ -217,7 +218,7 @@ class RecordPageHandler /** * @brief 获取指定槽位的记录数据 - * + * * @param 指定的记录槽位 */ char *get_record_data(SlotNum slot_num) @@ -227,10 +228,10 @@ class RecordPageHandler protected: DiskBufferPool *disk_buffer_pool_ = nullptr; ///< 当前操作的buffer pool(文件) - Frame *frame_ = nullptr; ///< 当前操作页面关联的frame(frame的更多概念可以参考buffer pool和frame) - bool readonly_ = false; ///< 当前的操作是否都是只读的 - PageHeader *page_header_ = nullptr; ///< 当前页面上页面头 - char *bitmap_ = nullptr; ///< 当前页面上record分配状态信息bitmap内存起始位置 + Frame *frame_ = nullptr; ///< 当前操作页面关联的frame(frame的更多概念可以参考buffer pool和frame) + bool readonly_ = false; ///< 当前的操作是否都是只读的 + PageHeader *page_header_ = nullptr; ///< 当前页面上页面头 + char *bitmap_ = nullptr; ///< 当前页面上record分配状态信息bitmap内存起始位置 private: friend class RecordPageIterator; @@ -261,23 +262,23 @@ class RecordFileHandler /** * @brief 从指定文件中删除指定槽位的记录 - * + * * @param rid 待删除记录的标识符 */ RC delete_record(const RID *rid); /** * @brief 插入一个新的记录到指定文件中,并返回该记录的标识符 - * + * * @param data 纪录内容 * @param record_size 记录大小 * @param rid 返回该记录的标识符 */ RC insert_record(const char *data, int record_size, RID *rid); - /** + /** * @brief 数据库恢复时,在指定文件指定位置插入数据 - * + * * @param data 记录内容 * @param record_size 记录大小 * @param rid 要插入记录的指定标识符 @@ -314,7 +315,7 @@ class RecordFileHandler private: DiskBufferPool *disk_buffer_pool_ = nullptr; std::unordered_set free_pages_; ///< 没有填充满的页面集合 - common::Mutex lock_; ///< 当编译时增加-DCONCURRENCY=ON 选项时,才会真正的支持并发 + common::Mutex lock_; ///< 当编译时增加-DCONCURRENCY=ON 选项时,才会真正的支持并发 }; /** @@ -344,7 +345,7 @@ class RecordFileScanner */ RC close_scan(); - /** + /** * @brief 判断是否还有数据 * @details 判断完成后调用next获取下一条数据 */ @@ -352,12 +353,12 @@ class RecordFileScanner /** * @brief 获取下一条记录 - * + * * @param record 返回的下一条记录 - * + * * @details 获取下一条记录之前先调用has_next()判断是否还有数据 */ - RC next(Record &record); + RC next(Record &record); private: /** @@ -372,10 +373,11 @@ class RecordFileScanner private: // TODO 对于一个纯粹的record遍历器来说,不应该关心表和事务 - Table *table_ = nullptr; ///< 当前遍历的是哪张表。这个字段仅供事务函数使用,如果设计合适,可以去掉 - DiskBufferPool *disk_buffer_pool_ = nullptr; ///< 当前访问的文件 - Trx *trx_ = nullptr; ///< 当前是哪个事务在遍历 - bool readonly_ = false; ///< 遍历出来的数据,是否可能对它做修改 + Table *table_ = nullptr; ///< 当前遍历的是哪张表。这个字段仅供事务函数使用,如果设计合适,可以去掉 + + DiskBufferPool *disk_buffer_pool_ = nullptr; ///< 当前访问的文件 + Trx *trx_ = nullptr; ///< 当前是哪个事务在遍历 + bool readonly_ = false; ///< 遍历出来的数据,是否可能对它做修改 BufferPoolIterator bp_iterator_; ///< 遍历buffer pool的所有页面 ConditionFilter *condition_filter_ = nullptr; ///< 过滤record diff --git a/src/observer/storage/table/table.cpp b/src/observer/storage/table/table.cpp index c091bad56..73217ce6f 100644 --- a/src/observer/storage/table/table.cpp +++ b/src/observer/storage/table/table.cpp @@ -12,21 +12,21 @@ See the Mulan PSL v2 for more details. */ // Created by Meiyi & Wangyunlai on 2021/5/13. // +#include #include #include -#include #include "common/defs.h" -#include "storage/table/table.h" -#include "storage/table/table_meta.h" -#include "common/log/log.h" #include "common/lang/string.h" +#include "common/log/log.h" #include "storage/buffer/disk_buffer_pool.h" -#include "storage/record/record_manager.h" #include "storage/common/condition_filter.h" #include "storage/common/meta_util.h" -#include "storage/index/index.h" #include "storage/index/bplus_tree_index.h" +#include "storage/index/index.h" +#include "storage/record/record_manager.h" +#include "storage/table/table.h" +#include "storage/table/table_meta.h" #include "storage/trx/trx.h" Table::~Table() @@ -50,12 +50,8 @@ Table::~Table() LOG_INFO("Table has been closed: %s", name()); } -RC Table::create(int32_t table_id, - const char *path, - const char *name, - const char *base_dir, - int attribute_count, - const AttrInfoSqlNode attributes[]) +RC Table::create(int32_t table_id, const char *path, const char *name, const char *base_dir, int attribute_count, + const AttrInfoSqlNode attributes[]) { if (table_id < 0) { LOG_WARN("invalid table id. table_id=%d, table_name=%s", table_id, name); @@ -106,9 +102,9 @@ RC Table::create(int32_t table_id, table_meta_.serialize(fs); fs.close(); - std::string data_file = table_data_file(base_dir, name); - BufferPoolManager &bpm = BufferPoolManager::instance(); - rc = bpm.create_file(data_file.c_str()); + std::string data_file = table_data_file(base_dir, name); + BufferPoolManager &bpm = BufferPoolManager::instance(); + rc = bpm.create_file(data_file.c_str()); if (rc != RC::SUCCESS) { LOG_ERROR("Failed to create disk buffer pool of data file. file name=%s", data_file.c_str()); return rc; @@ -130,7 +126,7 @@ RC Table::open(const char *meta_file, const char *base_dir) { // 加载元数据文件 std::fstream fs; - std::string meta_file_path = std::string(base_dir) + common::FILE_PATH_SPLIT_STR + meta_file; + std::string meta_file_path = std::string(base_dir) + common::FILE_PATH_SPLIT_STR + meta_file; fs.open(meta_file_path, std::ios_base::in | std::ios_base::binary); if (!fs.is_open()) { LOG_ERROR("Failed to open meta file for read. file name=%s, errmsg=%s", meta_file_path.c_str(), strerror(errno)); @@ -165,8 +161,9 @@ RC Table::open(const char *meta_file, const char *base_dir) return RC::INTERNAL; } - BplusTreeIndex *index = new BplusTreeIndex(); - std::string index_file = table_index_file(base_dir, name(), index_meta->name()); + BplusTreeIndex *index = new BplusTreeIndex(); + std::string index_file = table_index_file(base_dir, name(), index_meta->name()); + rc = index->open(index_file.c_str(), *index_meta, *field_meta); if (rc != RC::SUCCESS) { delete index; @@ -185,15 +182,15 @@ RC Table::open(const char *meta_file, const char *base_dir) RC Table::insert_record(Record &record) { RC rc = RC::SUCCESS; - rc = record_handler_->insert_record(record.data(), table_meta_.record_size(), &record.rid()); + rc = record_handler_->insert_record(record.data(), table_meta_.record_size(), &record.rid()); if (rc != RC::SUCCESS) { LOG_ERROR("Insert record failed. table name=%s, rc=%s", table_meta_.name(), strrc(rc)); return rc; } rc = insert_entry_of_indexes(record.data(), record.rid()); - if (rc != RC::SUCCESS) { // 可能出现了键值重复 - RC rc2 = delete_entry_of_indexes(record.data(), record.rid(), false/*error_on_not_exists*/); + if (rc != RC::SUCCESS) { // 可能出现了键值重复 + RC rc2 = delete_entry_of_indexes(record.data(), record.rid(), false /*error_on_not_exists*/); if (rc2 != RC::SUCCESS) { LOG_ERROR("Failed to rollback index data when insert index entries failed. table name=%s, rc=%d:%s", name(), rc2, strrc(rc2)); @@ -215,14 +212,14 @@ RC Table::visit_record(const RID &rid, bool readonly, std::functionvisit_record(rid, true/*readonly*/, copier); + RC rc = record_handler_->visit_record(rid, true /*readonly*/, copier); if (rc != RC::SUCCESS) { free(record_data); LOG_WARN("failed to visit record. rid=%s, table=%s, rc=%s", rid.to_string().c_str(), name(), strrc(rc)); @@ -236,15 +233,15 @@ RC Table::get_record(const RID &rid, Record &record) RC Table::recover_insert_record(Record &record) { RC rc = RC::SUCCESS; - rc = record_handler_->recover_insert_record(record.data(), table_meta_.record_size(), record.rid()); + rc = record_handler_->recover_insert_record(record.data(), table_meta_.record_size(), record.rid()); if (rc != RC::SUCCESS) { LOG_ERROR("Insert record failed. table name=%s, rc=%s", table_meta_.name(), strrc(rc)); return rc; } rc = insert_entry_of_indexes(record.data(), record.rid()); - if (rc != RC::SUCCESS) { // 可能出现了键值重复 - RC rc2 = delete_entry_of_indexes(record.data(), record.rid(), false/*error_on_not_exists*/); + if (rc != RC::SUCCESS) { // 可能出现了键值重复 + RC rc2 = delete_entry_of_indexes(record.data(), record.rid(), false /*error_on_not_exists*/); if (rc2 != RC::SUCCESS) { LOG_ERROR("Failed to rollback index data when insert index entries failed. table name=%s, rc=%d:%s", name(), rc2, strrc(rc2)); @@ -258,15 +255,9 @@ RC Table::recover_insert_record(Record &record) return rc; } -const char *Table::name() const -{ - return table_meta_.name(); -} +const char *Table::name() const { return table_meta_.name(); } -const TableMeta &Table::table_meta() const -{ - return table_meta_; -} +const TableMeta &Table::table_meta() const { return table_meta_; } RC Table::make_record(int value_num, const Value *values, Record &record) { @@ -279,7 +270,7 @@ RC Table::make_record(int value_num, const Value *values, Record &record) const int normal_field_start_index = table_meta_.sys_field_num(); for (int i = 0; i < value_num; i++) { const FieldMeta *field = table_meta_.field(i + normal_field_start_index); - const Value &value = values[i]; + const Value &value = values[i]; if (field->type() != value.attr_type()) { LOG_ERROR("Invalid value type. table name =%s, field name=%s, type=%d, but given=%d", table_meta_.name(), field->name(), field->type(), value.attr_type()); @@ -288,13 +279,13 @@ RC Table::make_record(int value_num, const Value *values, Record &record) } // 复制所有字段的值 - int record_size = table_meta_.record_size(); + int record_size = table_meta_.record_size(); char *record_data = (char *)malloc(record_size); for (int i = 0; i < value_num; i++) { - const FieldMeta *field = table_meta_.field(i + normal_field_start_index); - const Value &value = values[i]; - size_t copy_len = field->len(); + const FieldMeta *field = table_meta_.field(i + normal_field_start_index); + const Value &value = values[i]; + size_t copy_len = field->len(); if (field->type() == CHARS) { const size_t data_len = value.length(); if (copy_len > data_len) { @@ -319,6 +310,7 @@ RC Table::init_record_handler(const char *base_dir) } record_handler_ = new RecordFileHandler(); + rc = record_handler_->init(data_buffer_pool_); if (rc != RC::SUCCESS) { LOG_ERROR("Failed to init record handler. rc=%s", strrc(rc)); @@ -349,6 +341,7 @@ RC Table::create_index(Trx *trx, const FieldMeta *field_meta, const char *index_ } IndexMeta new_index_meta; + RC rc = new_index_meta.init(index_name, *field_meta); if (rc != RC::SUCCESS) { LOG_INFO("Failed to init IndexMeta in table:%s, index_name:%s, field_name:%s", @@ -357,8 +350,9 @@ RC Table::create_index(Trx *trx, const FieldMeta *field_meta, const char *index_ } // 创建索引相关数据 - BplusTreeIndex *index = new BplusTreeIndex(); - std::string index_file = table_index_file(base_dir_.c_str(), name(), index_name); + BplusTreeIndex *index = new BplusTreeIndex(); + std::string index_file = table_index_file(base_dir_.c_str(), name(), index_name); + rc = index->create(index_file.c_str(), new_index_meta, *field_meta); if (rc != RC::SUCCESS) { delete index; @@ -368,7 +362,7 @@ RC Table::create_index(Trx *trx, const FieldMeta *field_meta, const char *index_ // 遍历当前的所有数据,插入这个索引 RecordFileScanner scanner; - rc = get_record_scanner(scanner, trx, true/*readonly*/); + rc = get_record_scanner(scanner, trx, true /*readonly*/); if (rc != RC::SUCCESS) { LOG_WARN("failed to create scanner while creating index. table=%s, index=%s, rc=%s", name(), index_name, strrc(rc)); @@ -387,12 +381,12 @@ RC Table::create_index(Trx *trx, const FieldMeta *field_meta, const char *index_ if (rc != RC::SUCCESS) { LOG_WARN("failed to insert record into index while creating index. table=%s, index=%s, rc=%s", name(), index_name, strrc(rc)); - return rc; + return rc; } } scanner.close_scan(); LOG_INFO("inserted all records into new index. table=%s, index=%s", name(), index_name); - + indexes_.push_back(index); /// 接下来将这个索引放到表的元数据中 @@ -406,7 +400,7 @@ RC Table::create_index(Trx *trx, const FieldMeta *field_meta, const char *index_ /// 内存中有一份元数据,磁盘文件也有一份元数据。修改磁盘文件时,先创建一个临时文件,写入完成后再rename为正式文件 /// 这样可以防止文件内容不完整 // 创建元数据临时文件 - std::string tmp_file = table_meta_file(base_dir_.c_str(), name()) + ".tmp"; + std::string tmp_file = table_meta_file(base_dir_.c_str(), name()) + ".tmp"; std::fstream fs; fs.open(tmp_file, std::ios_base::out | std::ios_base::binary | std::ios_base::trunc); if (!fs.is_open()) { @@ -421,6 +415,7 @@ RC Table::create_index(Trx *trx, const FieldMeta *field_meta, const char *index_ // 覆盖原始元数据文件 std::string meta_file = table_meta_file(base_dir_.c_str(), name()); + int ret = rename(tmp_file.c_str(), meta_file.c_str()); if (ret != 0) { LOG_ERROR("Failed to rename tmp meta file (%s) to normal meta file (%s) while creating index (%s) on table (%s). " diff --git a/src/observer/storage/table/table.h b/src/observer/storage/table/table.h index a5bb99e7c..1d999d1c8 100644 --- a/src/observer/storage/table/table.h +++ b/src/observer/storage/table/table.h @@ -14,8 +14,8 @@ See the Mulan PSL v2 for more details. */ #pragma once -#include #include "storage/table/table_meta.h" +#include struct RID; class Record; @@ -31,9 +31,9 @@ class Trx; /** * @brief 表 - * + * */ -class Table +class Table { public: Table() = default; @@ -47,12 +47,8 @@ class Table * @param attribute_count 字段个数 * @param attributes 字段 */ - RC create(int32_t table_id, - const char *path, - const char *name, - const char *base_dir, - int attribute_count, - const AttrInfoSqlNode attributes[]); + RC create(int32_t table_id, const char *path, const char *name, const char *base_dir, int attribute_count, + const AttrInfoSqlNode attributes[]); /** * 打开一个表 @@ -87,13 +83,10 @@ class Table RC get_record_scanner(RecordFileScanner &scanner, Trx *trx, bool readonly); - RecordFileHandler *record_handler() const - { - return record_handler_; - } + RecordFileHandler *record_handler() const { return record_handler_; } public: - int32_t table_id() const { return table_meta_.table_id(); } + int32_t table_id() const { return table_meta_.table_id(); } const char *name() const; const TableMeta &table_meta() const; @@ -112,9 +105,9 @@ class Table Index *find_index_by_field(const char *field_name) const; private: - std::string base_dir_; - TableMeta table_meta_; - DiskBufferPool *data_buffer_pool_ = nullptr; /// 数据文件关联的buffer pool - RecordFileHandler *record_handler_ = nullptr; /// 记录操作 + std::string base_dir_; + TableMeta table_meta_; + DiskBufferPool *data_buffer_pool_ = nullptr; /// 数据文件关联的buffer pool + RecordFileHandler *record_handler_ = nullptr; /// 记录操作 std::vector indexes_; }; diff --git a/src/observer/storage/table/table_meta.cpp b/src/observer/storage/table/table_meta.cpp index 9eab7f260..3b55dbecf 100644 --- a/src/observer/storage/table/table_meta.cpp +++ b/src/observer/storage/table/table_meta.cpp @@ -15,10 +15,10 @@ See the Mulan PSL v2 for more details. */ #include #include -#include "storage/table/table_meta.h" -#include "json/json.h" #include "common/log/log.h" +#include "storage/table/table_meta.h" #include "storage/trx/trx.h" +#include "json/json.h" using namespace std; @@ -29,10 +29,10 @@ static const Json::StaticString FIELD_INDEXES("indexes"); TableMeta::TableMeta(const TableMeta &other) : table_id_(other.table_id_), - name_(other.name_), - fields_(other.fields_), - indexes_(other.indexes_), - record_size_(other.record_size_) + name_(other.name_), + fields_(other.fields_), + indexes_(other.indexes_), + record_size_(other.record_size_) {} void TableMeta::swap(TableMeta &other) noexcept @@ -56,16 +56,17 @@ RC TableMeta::init(int32_t table_id, const char *name, int field_num, const Attr } RC rc = RC::SUCCESS; - - int field_offset = 0; + + int field_offset = 0; int trx_field_num = 0; + const vector *trx_fields = TrxKit::instance()->trx_fields(); if (trx_fields != nullptr) { fields_.resize(field_num + trx_fields->size()); for (size_t i = 0; i < trx_fields->size(); i++) { const FieldMeta &field_meta = (*trx_fields)[i]; - fields_[i] = FieldMeta(field_meta.name(), field_meta.type(), field_offset, field_meta.len(), false/*visible*/); + fields_[i] = FieldMeta(field_meta.name(), field_meta.type(), field_offset, field_meta.len(), false /*visible*/); field_offset += field_meta.len(); } @@ -76,8 +77,8 @@ RC TableMeta::init(int32_t table_id, const char *name, int field_num, const Attr for (int i = 0; i < field_num; i++) { const AttrInfoSqlNode &attr_info = attributes[i]; - rc = fields_[i + trx_field_num].init(attr_info.name.c_str(), - attr_info.type, field_offset, attr_info.length, true/*visible*/); + rc = fields_[i + trx_field_num].init( + attr_info.name.c_str(), attr_info.type, field_offset, attr_info.length, true /*visible*/); if (rc != RC::SUCCESS) { LOG_ERROR("Failed to init field meta. table name=%s, field name: %s", name, attr_info.name.c_str()); return rc; @@ -100,25 +101,16 @@ RC TableMeta::add_index(const IndexMeta &index) return RC::SUCCESS; } -const char *TableMeta::name() const -{ - return name_.c_str(); -} +const char *TableMeta::name() const { return name_.c_str(); } -const FieldMeta *TableMeta::trx_field() const -{ - return &fields_[0]; -} +const FieldMeta *TableMeta::trx_field() const { return &fields_[0]; } const std::pair TableMeta::trx_fields() const { return std::pair{fields_.data(), sys_field_num()}; } -const FieldMeta *TableMeta::field(int index) const -{ - return &fields_[index]; -} +const FieldMeta *TableMeta::field(int index) const { return &fields_[index]; } const FieldMeta *TableMeta::field(const char *name) const { if (nullptr == name) { @@ -141,10 +133,7 @@ const FieldMeta *TableMeta::find_field_by_offset(int offset) const } return nullptr; } -int TableMeta::field_num() const -{ - return fields_.size(); -} +int TableMeta::field_num() const { return fields_.size(); } int TableMeta::sys_field_num() const { @@ -175,20 +164,11 @@ const IndexMeta *TableMeta::find_index_by_field(const char *field) const return nullptr; } -const IndexMeta *TableMeta::index(int i) const -{ - return &indexes_[i]; -} +const IndexMeta *TableMeta::index(int i) const { return &indexes_[i]; } -int TableMeta::index_num() const -{ - return indexes_.size(); -} +int TableMeta::index_num() const { return indexes_.size(); } -int TableMeta::record_size() const -{ - return record_size_; -} +int TableMeta::record_size() const { return record_size_; } int TableMeta::serialize(std::ostream &ss) const { @@ -215,7 +195,7 @@ int TableMeta::serialize(std::ostream &ss) const table_value[FIELD_INDEXES] = std::move(indexes_value); Json::StreamWriterBuilder builder; - Json::StreamWriter *writer = builder.newStreamWriter(); + Json::StreamWriter *writer = builder.newStreamWriter(); std::streampos old_pos = ss.tellp(); writer->write(table_value, &ss); @@ -227,9 +207,9 @@ int TableMeta::serialize(std::ostream &ss) const int TableMeta::deserialize(std::istream &is) { - Json::Value table_value; + Json::Value table_value; Json::CharReaderBuilder builder; - std::string errors; + std::string errors; std::streampos old_pos = is.tellg(); if (!Json::parseFromStream(builder, is, &table_value, &errors)) { @@ -259,14 +239,15 @@ int TableMeta::deserialize(std::istream &is) return -1; } - RC rc = RC::SUCCESS; + RC rc = RC::SUCCESS; int field_num = fields_value.size(); + std::vector fields(field_num); for (int i = 0; i < field_num; i++) { FieldMeta &field = fields[i]; const Json::Value &field_value = fields_value[i]; - rc = FieldMeta::from_json(field_value, field); + rc = FieldMeta::from_json(field_value, field); if (rc != RC::SUCCESS) { LOG_ERROR("Failed to deserialize table meta. table name =%s", table_name.c_str()); return -1; @@ -287,13 +268,13 @@ int TableMeta::deserialize(std::istream &is) LOG_ERROR("Invalid table meta. indexes is not array, json value=%s", fields_value.toStyledString().c_str()); return -1; } - const int index_num = indexes_value.size(); + const int index_num = indexes_value.size(); std::vector indexes(index_num); for (int i = 0; i < index_num; i++) { IndexMeta &index = indexes[i]; const Json::Value &index_value = indexes_value[i]; - rc = IndexMeta::from_json(*this, index_value, index); + rc = IndexMeta::from_json(*this, index_value, index); if (rc != RC::SUCCESS) { LOG_ERROR("Failed to deserialize table meta. table name=%s", table_name.c_str()); return -1; @@ -305,13 +286,9 @@ int TableMeta::deserialize(std::istream &is) return (int)(is.tellg() - old_pos); } -int TableMeta::get_serial_size() const -{ - return -1; -} +int TableMeta::get_serial_size() const { return -1; } -void TableMeta::to_string(std::string &output) const -{} +void TableMeta::to_string(std::string &output) const {} void TableMeta::desc(std::ostream &os) const { diff --git a/src/observer/storage/table/table_meta.h b/src/observer/storage/table/table_meta.h index 74319fc63..31b299a56 100644 --- a/src/observer/storage/table/table_meta.h +++ b/src/observer/storage/table/table_meta.h @@ -17,19 +17,19 @@ See the Mulan PSL v2 for more details. */ #include #include +#include "common/lang/serializable.h" #include "common/rc.h" #include "storage/field/field_meta.h" #include "storage/index/index_meta.h" -#include "common/lang/serializable.h" /** * @brief 表元数据 - * + * */ class TableMeta : public common::Serializable { public: - TableMeta() = default; + TableMeta() = default; ~TableMeta() = default; TableMeta(const TableMeta &other); @@ -41,38 +41,35 @@ class TableMeta : public common::Serializable RC add_index(const IndexMeta &index); public: - int32_t table_id() const { return table_id_; } - const char *name() const; - const FieldMeta *trx_field() const; - const FieldMeta *field(int index) const; - const FieldMeta *field(const char *name) const; - const FieldMeta *find_field_by_offset(int offset) const; - const std::vector *field_metas() const - { - return &fields_; - } - auto trx_fields() const -> const std::pair; - + int32_t table_id() const { return table_id_; } + const char *name() const; + const FieldMeta *trx_field() const; + const FieldMeta *field(int index) const; + const FieldMeta *field(const char *name) const; + const FieldMeta *find_field_by_offset(int offset) const; + const std::vector *field_metas() const { return &fields_; } + auto trx_fields() const -> const std::pair; + int field_num() const; // sys field included int sys_field_num() const; const IndexMeta *index(const char *name) const; const IndexMeta *find_index_by_field(const char *field) const; const IndexMeta *index(int i) const; - int index_num() const; + int index_num() const; int record_size() const; public: - int serialize(std::ostream &os) const override; - int deserialize(std::istream &is) override; - int get_serial_size() const override; + int serialize(std::ostream &os) const override; + int deserialize(std::istream &is) override; + int get_serial_size() const override; void to_string(std::string &output) const override; void desc(std::ostream &os) const; protected: - int32_t table_id_ = -1; - std::string name_; + int32_t table_id_ = -1; + std::string name_; std::vector fields_; // 包含sys_fields std::vector indexes_; diff --git a/src/observer/storage/trx/latch_memo.cpp b/src/observer/storage/trx/latch_memo.cpp index 9ca13ed83..1affee57e 100644 --- a/src/observer/storage/trx/latch_memo.cpp +++ b/src/observer/storage/trx/latch_memo.cpp @@ -13,13 +13,13 @@ See the Mulan PSL v2 for more details. */ // #include "storage/trx/latch_memo.h" -#include "storage/buffer/frame.h" -#include "storage/buffer/disk_buffer_pool.h" #include "common/lang/mutex.h" +#include "storage/buffer/disk_buffer_pool.h" +#include "storage/buffer/frame.h" LatchMemoItem::LatchMemoItem(LatchMemoType type, Frame *frame) { - this->type = type; + this->type = type; this->frame = frame; } @@ -31,13 +31,9 @@ LatchMemoItem::LatchMemoItem(LatchMemoType type, common::SharedMutex *lock) //////////////////////////////////////////////////////////////////////////////// -LatchMemo::LatchMemo(DiskBufferPool *buffer_pool) : buffer_pool_(buffer_pool) -{} +LatchMemo::LatchMemo(DiskBufferPool *buffer_pool) : buffer_pool_(buffer_pool) {} -LatchMemo::~LatchMemo() -{ - this->release(); -} +LatchMemo::~LatchMemo() { this->release(); } RC LatchMemo::get_page(PageNum page_num, Frame *&frame) { @@ -55,20 +51,17 @@ RC LatchMemo::get_page(PageNum page_num, Frame *&frame) RC LatchMemo::allocate_page(Frame *&frame) { frame = nullptr; - + RC rc = buffer_pool_->allocate_page(&frame); if (rc == RC::SUCCESS) { items_.emplace_back(LatchMemoType::PIN, frame); ASSERT(frame->pin_count() == 1, "allocate a new frame. frame=%s", to_string(*frame).c_str()); } - + return rc; } -void LatchMemo::dispose_page(PageNum page_num) -{ - disposed_pages_.emplace_back(page_num); -} +void LatchMemo::dispose_page(PageNum page_num) { disposed_pages_.emplace_back(page_num); } void LatchMemo::latch(Frame *frame, LatchMemoType type) { @@ -87,15 +80,9 @@ void LatchMemo::latch(Frame *frame, LatchMemoType type) items_.emplace_back(type, frame); } -void LatchMemo::xlatch(Frame *frame) -{ - this->latch(frame, LatchMemoType::EXCLUSIVE); -} +void LatchMemo::xlatch(Frame *frame) { this->latch(frame, LatchMemoType::EXCLUSIVE); } -void LatchMemo::slatch(Frame *frame) -{ - this->latch(frame, LatchMemoType::SHARED); -} +void LatchMemo::slatch(Frame *frame) { this->latch(frame, LatchMemoType::SHARED); } bool LatchMemo::try_slatch(Frame *frame) { diff --git a/src/observer/storage/trx/latch_memo.h b/src/observer/storage/trx/latch_memo.h index e5823ac8a..e7ba738bf 100644 --- a/src/observer/storage/trx/latch_memo.h +++ b/src/observer/storage/trx/latch_memo.h @@ -41,9 +41,9 @@ struct LatchMemoItem LatchMemoItem(LatchMemoType type, Frame *frame); LatchMemoItem(LatchMemoType type, common::SharedMutex *lock); - LatchMemoType type = LatchMemoType::NONE; - Frame *frame = nullptr; - common::SharedMutex *lock = nullptr; + LatchMemoType type = LatchMemoType::NONE; + Frame *frame = nullptr; + common::SharedMutex *lock = nullptr; }; class LatchMemo final @@ -70,13 +70,13 @@ class LatchMemo final void release_to(int point); - int memo_point() const { return static_cast(items_.size()); } + int memo_point() const { return static_cast(items_.size()); } private: void release_item(LatchMemoItem &item); - + private: - DiskBufferPool * buffer_pool_ = nullptr; - std::deque items_; - std::vector disposed_pages_; + DiskBufferPool *buffer_pool_ = nullptr; + std::deque items_; + std::vector disposed_pages_; }; \ No newline at end of file diff --git a/src/observer/storage/trx/mvcc_trx.cpp b/src/observer/storage/trx/mvcc_trx.cpp index ecf06a974..5cd682a51 100644 --- a/src/observer/storage/trx/mvcc_trx.cpp +++ b/src/observer/storage/trx/mvcc_trx.cpp @@ -12,12 +12,11 @@ See the Mulan PSL v2 for more details. */ // Created by Wangyunlai on 2023/04/24. // -#include #include "storage/trx/mvcc_trx.h" -#include "storage/field/field.h" #include "storage/clog/clog.h" #include "storage/db/db.h" -#include "storage/clog/clog.h" +#include "storage/field/field.h" +#include using namespace std; @@ -25,7 +24,7 @@ MvccTrxKit::~MvccTrxKit() { vector tmp_trxes; tmp_trxes.swap(trxes_); - + for (Trx *trx : tmp_trxes) { delete trx; } @@ -34,28 +33,18 @@ MvccTrxKit::~MvccTrxKit() RC MvccTrxKit::init() { fields_ = vector{ - FieldMeta("__trx_xid_begin", AttrType::INTS, 0/*attr_offset*/, 4/*attr_len*/, false/*visible*/), - FieldMeta("__trx_xid_end", AttrType::INTS, 0/*attr_offset*/, 4/*attr_len*/, false/*visible*/) - }; + FieldMeta("__trx_xid_begin", AttrType::INTS, 0 /*attr_offset*/, 4 /*attr_len*/, false /*visible*/), + FieldMeta("__trx_xid_end", AttrType::INTS, 0 /*attr_offset*/, 4 /*attr_len*/, false /*visible*/)}; LOG_INFO("init mvcc trx kit done."); return RC::SUCCESS; } -const vector *MvccTrxKit::trx_fields() const -{ - return &fields_; -} +const vector *MvccTrxKit::trx_fields() const { return &fields_; } -int32_t MvccTrxKit::next_trx_id() -{ - return ++current_trx_id_; -} +int32_t MvccTrxKit::next_trx_id() { return ++current_trx_id_; } -int32_t MvccTrxKit::max_trx_id() const -{ - return numeric_limits::max(); -} +int32_t MvccTrxKit::max_trx_id() const { return numeric_limits::max(); } Trx *MvccTrxKit::create_trx(CLogManager *log_manager) { @@ -119,18 +108,15 @@ void MvccTrxKit::all_trxes(std::vector &trxes) //////////////////////////////////////////////////////////////////////////////// -MvccTrx::MvccTrx(MvccTrxKit &kit, CLogManager *log_manager) : trx_kit_(kit), log_manager_(log_manager) -{} +MvccTrx::MvccTrx(MvccTrxKit &kit, CLogManager *log_manager) : trx_kit_(kit), log_manager_(log_manager) {} MvccTrx::MvccTrx(MvccTrxKit &kit, int32_t trx_id) : trx_kit_(kit), trx_id_(trx_id) { - started_ = true; + started_ = true; recovering_ = true; } -MvccTrx::~MvccTrx() -{ -} +MvccTrx::~MvccTrx() {} RC MvccTrx::insert_record(Table *table, Record &record) { @@ -147,12 +133,12 @@ RC MvccTrx::insert_record(Table *table, Record &record) return rc; } - rc = log_manager_->append_log(CLogType::INSERT, trx_id_, table->table_id(), record.rid(), record.len(), 0/*offset*/, record.data()); + rc = log_manager_->append_log( + CLogType::INSERT, trx_id_, table->table_id(), record.rid(), record.len(), 0 /*offset*/, record.data()); ASSERT(rc == RC::SUCCESS, "failed to append insert record log. trx id=%d, table id=%d, rid=%s, record len=%d, rc=%s", trx_id_, table->table_id(), record.rid().to_string().c_str(), record.len(), strrc(rc)); - pair ret = - operations_.insert(Operation(Operation::Type::INSERT, table, record.rid())); + pair ret = operations_.insert(Operation(Operation::Type::INSERT, table, record.rid())); if (!ret.second) { rc = RC::INTERNAL; LOG_WARN("failed to insert operation(insertion) into operation set: duplicate"); @@ -160,14 +146,14 @@ RC MvccTrx::insert_record(Table *table, Record &record) return rc; } -RC MvccTrx::delete_record(Table * table, Record &record) +RC MvccTrx::delete_record(Table *table, Record &record) { Field begin_field; Field end_field; trx_fields(table, begin_field, end_field); - int32_t begin_xid = begin_field.get_int(record); - [[maybe_unused]] int32_t end_xid = end_field.get_int(record); + int32_t begin_xid = begin_field.get_int(record); + [[maybe_unused]] int32_t end_xid = end_field.get_int(record); /// 在删除之前,第一次获取record时,就已经对record做了对应的检查,并且保证不会有其它的事务来访问这条数据 ASSERT(end_xid > 0, "concurrency conflit: other transaction is updating this record. end_xid=%d, current trx id=%d, rid=%s", end_xid, trx_id_, record.rid().to_string().c_str()); @@ -175,7 +161,7 @@ RC MvccTrx::delete_record(Table * table, Record &record) // 当前不是多版本数据中的最新记录,不需要删除 return RC::SUCCESS; } - + end_field.set_int(record, -trx_id_); RC rc = log_manager_->append_log(CLogType::DELETE, trx_id_, table->table_id(), record.rid(), 0, 0, nullptr); ASSERT(rc == RC::SUCCESS, "failed to append delete record log. trx id=%d, table id=%d, rid=%s, record len=%d, rc=%s", @@ -183,9 +169,10 @@ RC MvccTrx::delete_record(Table * table, Record &record) if (begin_xid == -trx_id_) { // fix:此处是为了修复由当前事务插入而又被当前事务删除时无法正确删除的问题: // 在当前事务中创建的记录从来未对外暴露过,未来方便今后添加垃圾回收功能,这里选择直接删除真实记录 - // 就认为记录从来未存在过,此时无论是commit还是rollback都能得到正确的结果,并且需要清空之前的insert operation,避免事务结束时执行 - auto delete_operation = Operation{Operation::Type::INSERT, table, record.rid()}; - std::unordered_set::size_type delete_result = operations_.erase(delete_operation); + // 就认为记录从来未存在过,此时无论是commit还是rollback都能得到正确的结果,并且需要清空之前的insert + // operation,避免事务结束时执行 + auto delete_operation = Operation{Operation::Type::INSERT, table, record.rid()}; + std::unordered_set::size_type delete_result = operations_.erase(delete_operation); ASSERT(delete_result == 1, "failed to delete insert operation,begin_xid=%d, end_xid=%d, tid=%d, rid:%s", begin_xid, end_xid, trx_id_, record.rid().to_string().c_str()); rc = table->delete_record(record); @@ -210,7 +197,7 @@ RC MvccTrx::visit_record(Table *table, Record &record, bool readonly) trx_fields(table, begin_field, end_field); int32_t begin_xid = begin_field.get_int(record); - int32_t end_xid = end_field.get_int(record); + int32_t end_xid = end_field.get_int(record); RC rc = RC::SUCCESS; if (begin_xid > 0 && end_xid > 0) { @@ -239,14 +226,14 @@ RC MvccTrx::visit_record(Table *table, Record &record, bool readonly) /** * @brief 获取指定表上的事务使用的字段 - * + * * @param table 指定的表 * @param begin_xid_field 返回处理begin_xid的字段 * @param end_xid_field 返回处理end_xid的字段 */ void MvccTrx::trx_fields(Table *table, Field &begin_xid_field, Field &end_xid_field) const { - const TableMeta &table_meta = table->table_meta(); + const TableMeta &table_meta = table->table_meta(); const std::pair trx_fields = table_meta.trx_fields(); ASSERT(trx_fields.second >= 2, "invalid trx fields number. %d", trx_fields.second); @@ -278,18 +265,18 @@ RC MvccTrx::commit() RC MvccTrx::commit_with_trx_id(int32_t commit_xid) { // TODO 这里存在一个很大的问题,不能让其他事务一次性看到当前事务更新到的数据或同时看不到 - RC rc = RC::SUCCESS; + RC rc = RC::SUCCESS; started_ = false; - + for (const Operation &operation : operations_) { switch (operation.type()) { case Operation::Type::INSERT: { - RID rid(operation.page_num(), operation.slot_num()); + RID rid(operation.page_num(), operation.slot_num()); Table *table = operation.table(); - Field begin_xid_field, end_xid_field; + Field begin_xid_field, end_xid_field; trx_fields(table, begin_xid_field, end_xid_field); - auto record_updater = [ this, &begin_xid_field, commit_xid](Record &record) { + auto record_updater = [this, &begin_xid_field, commit_xid](Record &record) { LOG_DEBUG("before commit insert record. trx id=%d, begin xid=%d, commit xid=%d, lbt=%s", trx_id_, begin_xid_field.get_int(record), commit_xid, lbt()); ASSERT(begin_xid_field.get_int(record) == -this->trx_id_, @@ -299,15 +286,15 @@ RC MvccTrx::commit_with_trx_id(int32_t commit_xid) begin_xid_field.set_int(record, commit_xid); }; - rc = operation.table()->visit_record(rid, false/*readonly*/, record_updater); + rc = operation.table()->visit_record(rid, false /*readonly*/, record_updater); ASSERT(rc == RC::SUCCESS, "failed to get record while committing. rid=%s, rc=%s", rid.to_string().c_str(), strrc(rc)); } break; case Operation::Type::DELETE: { Table *table = operation.table(); - RID rid(operation.page_num(), operation.slot_num()); - + RID rid(operation.page_num(), operation.slot_num()); + Field begin_xid_field, end_xid_field; trx_fields(table, begin_xid_field, end_xid_field); @@ -316,11 +303,11 @@ RC MvccTrx::commit_with_trx_id(int32_t commit_xid) ASSERT(end_xid_field.get_int(record) == -trx_id_, "got an invalid record while committing. end xid=%d, this trx id=%d", end_xid_field.get_int(record), trx_id_); - + end_xid_field.set_int(record, commit_xid); }; - rc = operation.table()->visit_record(rid, false/*readonly*/, record_updater); + rc = operation.table()->visit_record(rid, false /*readonly*/, record_updater); ASSERT(rc == RC::SUCCESS, "failed to get record while committing. rid=%s, rc=%s", rid.to_string().c_str(), strrc(rc)); } break; @@ -342,20 +329,20 @@ RC MvccTrx::commit_with_trx_id(int32_t commit_xid) RC MvccTrx::rollback() { - RC rc = RC::SUCCESS; + RC rc = RC::SUCCESS; started_ = false; - + for (const Operation &operation : operations_) { switch (operation.type()) { case Operation::Type::INSERT: { - RID rid(operation.page_num(), operation.slot_num()); + RID rid(operation.page_num(), operation.slot_num()); Record record; Table *table = operation.table(); // TODO 这里虽然调用get_record好像多次一举,而且看起来放在table的实现中更好, // 而且实际上trx应该记录下来自己曾经插入过的数据 // 也就是不需要从table中获取这条数据,可以直接从当前内存中获取 // 这里也可以不删除,仅仅给数据加个标识位,等垃圾回收器来收割也行 - rc = table->get_record(rid, record); + rc = table->get_record(rid, record); ASSERT(rc == RC::SUCCESS, "failed to get record while rollback. rid=%s, rc=%s", rid.to_string().c_str(), strrc(rc)); rc = table->delete_record(record); @@ -365,8 +352,8 @@ RC MvccTrx::rollback() case Operation::Type::DELETE: { Table *table = operation.table(); - RID rid(operation.page_num(), operation.slot_num()); - + RID rid(operation.page_num(), operation.slot_num()); + ASSERT(rc == RC::SUCCESS, "failed to get record while rollback. rid=%s, rc=%s", rid.to_string().c_str(), strrc(rc)); Field begin_xid_field, end_xid_field; @@ -379,8 +366,8 @@ RC MvccTrx::rollback() end_xid_field.set_int(record, trx_kit_.max_trx_id()); }; - - rc = table->visit_record(rid, false/*readonly*/, record_updater); + + rc = table->visit_record(rid, false /*readonly*/, record_updater); ASSERT(rc == RC::SUCCESS, "failed to get record while committing. rid=%s, rc=%s", rid.to_string().c_str(), strrc(rc)); } break; @@ -406,14 +393,14 @@ RC find_table(Db *db, const CLogRecord &log_record, Table *&table) case CLogType::INSERT: case CLogType::DELETE: { const CLogRecordData &data_record = log_record.data_record(); - table = db->find_table(data_record.table_id_); + table = db->find_table(data_record.table_id_); if (nullptr == table) { LOG_WARN("no such table to redo. table id=%d, log record=%s", data_record.table_id_, log_record.to_string().c_str()); return RC::SCHEMA_TABLE_NOT_EXIST; } } break; - default:{ + default: { // do nothing } break; } @@ -423,7 +410,7 @@ RC find_table(Db *db, const CLogRecord &log_record, Table *&table) RC MvccTrx::redo(Db *db, const CLogRecord &log_record) { Table *table = nullptr; - RC rc = find_table(db, log_record, table); + RC rc = find_table(db, log_record, table); if (OB_FAIL(rc)) { return rc; } @@ -431,7 +418,7 @@ RC MvccTrx::redo(Db *db, const CLogRecord &log_record) switch (log_record.log_type()) { case CLogType::INSERT: { const CLogRecordData &data_record = log_record.data_record(); - Record record; + Record record; record.set_data(const_cast(data_record.data_), data_record.data_len_); record.set_rid(data_record.rid_); RC rc = table->recover_insert_record(record); @@ -445,8 +432,8 @@ RC MvccTrx::redo(Db *db, const CLogRecord &log_record) case CLogType::DELETE: { const CLogRecordData &data_record = log_record.data_record(); - Field begin_field; - Field end_field; + Field begin_field; + Field end_field; trx_fields(table, begin_field, end_field); auto record_updater = [this, &end_field](Record &record) { @@ -454,14 +441,14 @@ RC MvccTrx::redo(Db *db, const CLogRecord &log_record) ASSERT(end_field.get_int(record) == trx_kit_.max_trx_id(), "got an invalid record while committing. end xid=%d, this trx id=%d", end_field.get_int(record), trx_id_); - + end_field.set_int(record, -trx_id_); }; - RC rc = table->visit_record(data_record.rid_, false/*readonly*/, record_updater); + RC rc = table->visit_record(data_record.rid_, false /*readonly*/, record_updater); ASSERT(rc == RC::SUCCESS, "failed to get record while committing. rid=%s, rc=%s", data_record.rid_.to_string().c_str(), strrc(rc)); - + operations_.insert(Operation(Operation::Type::DELETE, table, data_record.rid_)); } break; @@ -473,7 +460,7 @@ RC MvccTrx::redo(Db *db, const CLogRecord &log_record) case CLogType::MTR_ROLLBACK: { rollback(); } break; - + default: { ASSERT(false, "unsupported redo log. log_record=%s", log_record.to_string().c_str()); return RC::INTERNAL; diff --git a/src/observer/storage/trx/mvcc_trx.h b/src/observer/storage/trx/mvcc_trx.h index 47c40fd81..8de532c14 100644 --- a/src/observer/storage/trx/mvcc_trx.h +++ b/src/observer/storage/trx/mvcc_trx.h @@ -26,11 +26,11 @@ class MvccTrxKit : public TrxKit MvccTrxKit() = default; virtual ~MvccTrxKit(); - RC init() override; + RC init() override; const std::vector *trx_fields() const override; - Trx *create_trx(CLogManager *log_manager) override; - Trx *create_trx(int32_t trx_id) override; - void destroy_trx(Trx *trx) override; + Trx *create_trx(CLogManager *log_manager) override; + Trx *create_trx(int32_t trx_id) override; + void destroy_trx(Trx *trx) override; /** * @brief 找到对应事务号的事务 @@ -46,7 +46,7 @@ class MvccTrxKit : public TrxKit int32_t max_trx_id() const; private: - std::vector fields_; // 存储事务数据需要用到的字段元数据,所有表结构都需要带的 + std::vector fields_; // 存储事务数据需要用到的字段元数据,所有表结构都需要带的 std::atomic current_trx_id_{0}; @@ -63,7 +63,7 @@ class MvccTrx : public Trx { public: MvccTrx(MvccTrxKit &trx_kit, CLogManager *log_manager); - MvccTrx(MvccTrxKit &trx_kit, int32_t trx_id); // used for recover + MvccTrx(MvccTrxKit &trx_kit, int32_t trx_id); // used for recover virtual ~MvccTrx(); RC insert_record(Table *table, Record &record) override; @@ -71,7 +71,7 @@ class MvccTrx : public Trx /** * @brief 当访问到某条数据时,使用此函数来判断是否可见,或者是否有访问冲突 - * + * * @param table 要访问的数据属于哪张表 * @param record 要访问哪条数据 * @param readonly 是否只读访问 @@ -90,7 +90,7 @@ class MvccTrx : public Trx int32_t id() const override { return trx_id_; } private: - RC commit_with_trx_id(int32_t commit_id); + RC commit_with_trx_id(int32_t commit_id); void trx_fields(Table *table, Field &begin_xid_field, Field &end_xid_field) const; private: @@ -98,10 +98,10 @@ class MvccTrx : public Trx private: using OperationSet = std::unordered_set; - MvccTrxKit & trx_kit_; + MvccTrxKit &trx_kit_; CLogManager *log_manager_ = nullptr; - int32_t trx_id_ = -1; - bool started_ = false; - bool recovering_ = false; + int32_t trx_id_ = -1; + bool started_ = false; + bool recovering_ = false; OperationSet operations_; }; diff --git a/src/observer/storage/trx/trx.cpp b/src/observer/storage/trx/trx.cpp index 05f69b67c..61579a07f 100644 --- a/src/observer/storage/trx/trx.cpp +++ b/src/observer/storage/trx/trx.cpp @@ -14,14 +14,14 @@ See the Mulan PSL v2 for more details. */ #include -#include "storage/trx/trx.h" -#include "storage/table/table.h" -#include "storage/record/record_manager.h" -#include "storage/field/field_meta.h" -#include "common/log/log.h" #include "common/lang/string.h" +#include "common/log/log.h" #include "storage/field/field.h" +#include "storage/field/field_meta.h" +#include "storage/record/record_manager.h" +#include "storage/table/table.h" #include "storage/trx/mvcc_trx.h" +#include "storage/trx/trx.h" #include "storage/trx/vacuous_trx.h" static TrxKit *global_trxkit = nullptr; @@ -35,7 +35,7 @@ TrxKit *TrxKit::create(const char *name) if (0 == strcasecmp(name, "mvcc")) { return new MvccTrxKit(); } - + LOG_ERROR("unknown trx kit name. name=%s", name); return nullptr; } @@ -56,12 +56,6 @@ RC TrxKit::init_global(const char *name) return rc; } -TrxKit *TrxKit::instance() -{ - return global_trxkit; -} +TrxKit *TrxKit::instance() { return global_trxkit; } -RC Trx::redo(Db *db, const CLogRecord &) -{ - return RC::UNIMPLENMENT; -} +RC Trx::redo(Db *db, const CLogRecord &) { return RC::UNIMPLENMENT; } diff --git a/src/observer/storage/trx/trx.h b/src/observer/storage/trx/trx.h index e4408eb63..996954a33 100644 --- a/src/observer/storage/trx/trx.h +++ b/src/observer/storage/trx/trx.h @@ -14,16 +14,16 @@ See the Mulan PSL v2 for more details. */ #pragma once +#include #include #include -#include #include +#include "common/rc.h" #include "sql/parser/parse.h" -#include "storage/record/record_manager.h" #include "storage/field/field_meta.h" +#include "storage/record/record_manager.h" #include "storage/table/table.h" -#include "common/rc.h" /** * @defgroup Transaction @@ -40,14 +40,14 @@ class Trx; * @ingroup Transaction * @details 通常包含一个操作的类型,以及操作的对象和具体的数据 */ -class Operation +class Operation { public: /** * @brief 操作的类型 * @ingroup Transaction */ - enum class Type : int + enum class Type : int { INSERT, UPDATE, @@ -56,44 +56,37 @@ class Operation }; public: - Operation(Type type, Table *table, const RID &rid) - : type_(type), - table_(table), - page_num_(rid.page_num), - slot_num_(rid.slot_num) + Operation(Type type, Table *table, const RID &rid) + : type_(type), table_(table), page_num_(rid.page_num), slot_num_(rid.slot_num) {} Type type() const { return type_; } int32_t table_id() const { return table_->table_id(); } - Table * table() const { return table_; } + Table *table() const { return table_; } PageNum page_num() const { return page_num_; } SlotNum slot_num() const { return slot_num_; } private: ///< 操作的哪张表。这里直接使用表其实并不准确,因为表中的索引也可能有日志 Type type_; - - Table * table_ = nullptr; - PageNum page_num_; // TODO use RID instead of page num and slot num + + Table *table_ = nullptr; + PageNum page_num_; // TODO use RID instead of page num and slot num SlotNum slot_num_; }; -class OperationHasher +class OperationHasher { public: - size_t operator()(const Operation &op) const - { - return (((size_t)op.page_num()) << 32) | (op.slot_num()); - } + size_t operator()(const Operation &op) const { return (((size_t)op.page_num()) << 32) | (op.slot_num()); } }; -class OperationEqualer +class OperationEqualer { public: bool operator()(const Operation &op1, const Operation &op2) const { - return op1.table_id() == op2.table_id() && - op1.page_num() == op2.page_num() && op1.slot_num() == op2.slot_num(); + return op1.table_id() == op2.table_id() && op1.page_num() == op2.page_num() && op1.slot_num() == op2.slot_num(); } }; @@ -111,26 +104,26 @@ class TrxKit */ enum Type { - VACUOUS, ///< 空的事务管理器,不做任何事情 - MVCC, ///< 支持MVCC的事务管理器 + VACUOUS, ///< 空的事务管理器,不做任何事情 + MVCC, ///< 支持MVCC的事务管理器 }; public: - TrxKit() = default; + TrxKit() = default; virtual ~TrxKit() = default; - virtual RC init() = 0; - virtual const std::vector *trx_fields() const = 0; - virtual Trx *create_trx(CLogManager *log_manager) = 0; - virtual Trx *create_trx(int32_t trx_id) = 0; - virtual Trx *find_trx(int32_t trx_id) = 0; - virtual void all_trxes(std::vector &trxes) = 0; + virtual RC init() = 0; + virtual const std::vector *trx_fields() const = 0; + virtual Trx *create_trx(CLogManager *log_manager) = 0; + virtual Trx *create_trx(int32_t trx_id) = 0; + virtual Trx *find_trx(int32_t trx_id) = 0; + virtual void all_trxes(std::vector &trxes) = 0; virtual void destroy_trx(Trx *trx) = 0; public: static TrxKit *create(const char *name); - static RC init_global(const char *name); + static RC init_global(const char *name); static TrxKit *instance(); }; @@ -141,16 +134,16 @@ class TrxKit class Trx { public: - Trx() = default; + Trx() = default; virtual ~Trx() = default; - virtual RC insert_record(Table *table, Record &record) = 0; - virtual RC delete_record(Table *table, Record &record) = 0; + virtual RC insert_record(Table *table, Record &record) = 0; + virtual RC delete_record(Table *table, Record &record) = 0; virtual RC visit_record(Table *table, Record &record, bool readonly) = 0; virtual RC start_if_need() = 0; - virtual RC commit() = 0; - virtual RC rollback() = 0; + virtual RC commit() = 0; + virtual RC rollback() = 0; virtual RC redo(Db *db, const CLogRecord &log_record); diff --git a/src/observer/storage/trx/vacuous_trx.cpp b/src/observer/storage/trx/vacuous_trx.cpp index 88eacc430..a0aac4b06 100644 --- a/src/observer/storage/trx/vacuous_trx.cpp +++ b/src/observer/storage/trx/vacuous_trx.cpp @@ -16,67 +16,30 @@ See the Mulan PSL v2 for more details. */ using namespace std; -RC VacuousTrxKit::init() -{ - return RC::SUCCESS; -} - -const vector *VacuousTrxKit::trx_fields() const -{ - return nullptr; -} - -Trx *VacuousTrxKit::create_trx(CLogManager *) -{ - return new VacuousTrx; -} - -Trx *VacuousTrxKit::create_trx(int32_t /*trx_id*/) -{ - return nullptr; -} - -void VacuousTrxKit::destroy_trx(Trx *) -{} - -Trx *VacuousTrxKit::find_trx(int32_t /* trx_id */) -{ - return nullptr; -} - -void VacuousTrxKit::all_trxes(std::vector &trxes) -{ - return; -} +RC VacuousTrxKit::init() { return RC::SUCCESS; } + +const vector *VacuousTrxKit::trx_fields() const { return nullptr; } + +Trx *VacuousTrxKit::create_trx(CLogManager *) { return new VacuousTrx; } + +Trx *VacuousTrxKit::create_trx(int32_t /*trx_id*/) { return nullptr; } + +void VacuousTrxKit::destroy_trx(Trx *) {} + +Trx *VacuousTrxKit::find_trx(int32_t /* trx_id */) { return nullptr; } + +void VacuousTrxKit::all_trxes(std::vector &trxes) { return; } //////////////////////////////////////////////////////////////////////////////// -RC VacuousTrx::insert_record(Table *table, Record &record) -{ - return table->insert_record(record); -} - -RC VacuousTrx::delete_record(Table *table, Record &record) -{ - return table->delete_record(record); -} - -RC VacuousTrx::visit_record(Table *table, Record &record, bool readonly) -{ - return RC::SUCCESS; -} - -RC VacuousTrx::start_if_need() -{ - return RC::SUCCESS; -} - -RC VacuousTrx::commit() -{ - return RC::SUCCESS; -} - -RC VacuousTrx::rollback() -{ - return RC::SUCCESS; -} +RC VacuousTrx::insert_record(Table *table, Record &record) { return table->insert_record(record); } + +RC VacuousTrx::delete_record(Table *table, Record &record) { return table->delete_record(record); } + +RC VacuousTrx::visit_record(Table *table, Record &record, bool readonly) { return RC::SUCCESS; } + +RC VacuousTrx::start_if_need() { return RC::SUCCESS; } + +RC VacuousTrx::commit() { return RC::SUCCESS; } + +RC VacuousTrx::rollback() { return RC::SUCCESS; } diff --git a/src/observer/storage/trx/vacuous_trx.h b/src/observer/storage/trx/vacuous_trx.h index 5dafff81c..34605f9c2 100644 --- a/src/observer/storage/trx/vacuous_trx.h +++ b/src/observer/storage/trx/vacuous_trx.h @@ -22,15 +22,15 @@ See the Mulan PSL v2 for more details. */ class VacuousTrxKit : public TrxKit { public: - VacuousTrxKit() = default; + VacuousTrxKit() = default; virtual ~VacuousTrxKit() = default; - RC init() override; + RC init() override; const std::vector *trx_fields() const override; - Trx *create_trx(CLogManager *log_manager) override; - Trx *create_trx(int32_t trx_id) override; - Trx *find_trx(int32_t trx_id) override; - void all_trxes(std::vector &trxes) override; + Trx *create_trx(CLogManager *log_manager) override; + Trx *create_trx(int32_t trx_id) override; + Trx *find_trx(int32_t trx_id) override; + void all_trxes(std::vector &trxes) override; void destroy_trx(Trx *trx) override; }; @@ -38,7 +38,7 @@ class VacuousTrxKit : public TrxKit class VacuousTrx : public Trx { public: - VacuousTrx() = default; + VacuousTrx() = default; virtual ~VacuousTrx() = default; RC insert_record(Table *table, Record &record) override; diff --git a/test/perf/client_performance_test.cpp b/test/perf/client_performance_test.cpp index 647e9f126..25d2ddc7e 100644 --- a/test/perf/client_performance_test.cpp +++ b/test/perf/client_performance_test.cpp @@ -25,9 +25,9 @@ See the Mulan PSL v2 for more details. */ #include #include "common/defs.h" +#include "common/metrics/console_reporter.h" #include "common/metrics/metrics.h" #include "common/metrics/metrics_registry.h" -#include "common/metrics/console_reporter.h" #define MAX_MEM_BUFFER_SIZE 8192 #define PORT_DEFAULT 6789 @@ -35,7 +35,7 @@ See the Mulan PSL v2 for more details. */ using namespace common; char *server_host = (char *)"localhost"; -int server_port = PORT_DEFAULT; +int server_port = PORT_DEFAULT; void *test_server(void *param) { @@ -49,7 +49,7 @@ void *test_server(void *param) char recv_buf[MAX_MEM_BUFFER_SIZE] = {0}; snprintf(send_buf, sizeof(send_buf), "%s", "select count(*) from test"); // char buf[MAXDATASIZE]; - struct hostent *host; + struct hostent *host; struct sockaddr_in serv_addr; if ((host = gethostbyname(server_host)) == NULL) { @@ -62,8 +62,8 @@ void *test_server(void *param) } serv_addr.sin_family = AF_INET; - serv_addr.sin_port = htons((uint16_t)server_port); - serv_addr.sin_addr = *((struct in_addr *)host->h_addr); + serv_addr.sin_port = htons((uint16_t)server_port); + serv_addr.sin_addr = *((struct in_addr *)host->h_addr); bzero(&(serv_addr.sin_zero), 8); if (connect(sockfd, (struct sockaddr *)&serv_addr, sizeof(struct sockaddr)) == -1) { @@ -97,20 +97,16 @@ void *test_server(void *param) int main(int argc, char *argv[]) { - int opt; + int opt; extern char *optarg; while ((opt = getopt(argc, argv, "h:p:")) > 0) { switch (opt) { - case 'p': - server_port = atoi(optarg); - break; - case 'h': - server_host = optarg; - break; + case 'p': server_port = atoi(optarg); break; + case 'h': server_host = optarg; break; } } - MetricsRegistry &metric_registry = get_metrics_registry(); + MetricsRegistry &metric_registry = get_metrics_registry(); ConsoleReporter *console_reporter = get_console_reporter(); metric_registry.add_reporter(console_reporter); diff --git a/tools/clog_reader_cmd.cpp b/tools/clog_reader_cmd.cpp index 0d21e74ae..5e5cdc046 100644 --- a/tools/clog_reader_cmd.cpp +++ b/tools/clog_reader_cmd.cpp @@ -21,6 +21,7 @@ using namespace std; void dump(const char *filename) { CLogFile file; + RC rc = file.init(filename); if (OB_FAIL(rc)) { printf("failed to open file: '%s'. syserr=%s, rc=%s\n", filename, strerror(errno), strrc(rc)); @@ -33,12 +34,12 @@ void dump(const char *filename) printf("failed to init iterator. rc=%s\n", strrc(rc)); return; } - + int64_t offset = 0; - int index = 0; + int index = 0; for (index++, rc = iterator.next(); OB_SUCC(rc) && iterator.valid(); rc = iterator.next(), ++index) { const CLogRecord &log_record = iterator.log_record(); - + printf("index:%d, file_offset:%" PRId64 ", %s\n", index, offset, log_record.to_string().c_str()); (void)file.offset(offset); } diff --git a/unittest/arithmetic_expression_test.cpp b/unittest/arithmetic_expression_test.cpp index 3d5f08b49..e11a464cc 100644 --- a/unittest/arithmetic_expression_test.cpp +++ b/unittest/arithmetic_expression_test.cpp @@ -29,7 +29,7 @@ TEST(ArithmeticExpr, test_value_type) unique_ptr left_expr(new ValueExpr(int_value1)); unique_ptr right_expr(new ValueExpr(int_value2)); - ArithmeticExpr expr_int(ArithmeticExpr::Type::ADD, std::move(left_expr), std::move(right_expr)); + ArithmeticExpr expr_int(ArithmeticExpr::Type::ADD, std::move(left_expr), std::move(right_expr)); ASSERT_EQ(AttrType::INTS, expr_int.value_type()); left_expr.reset(new ValueExpr(float_value1)); @@ -60,8 +60,8 @@ TEST(ArithmeticExpr, test_try_get_value) unique_ptr left_expr(new ValueExpr(int_value1)); unique_ptr right_expr(new ValueExpr(int_value2)); - ArithmeticExpr int_expr(ArithmeticExpr::Type::ADD, std::move(left_expr), std::move(right_expr)); - + ArithmeticExpr int_expr(ArithmeticExpr::Type::ADD, std::move(left_expr), std::move(right_expr)); + ASSERT_EQ(int_expr.try_get_value(int_result), RC::SUCCESS); ASSERT_EQ(int_result.get_int(), 3); @@ -105,14 +105,14 @@ TEST(ArithmeticExpr, test_try_get_value) float_expr.~ArithmeticExpr(); new (&float_expr)(ArithmeticExpr)(ArithmeticExpr::Type::MUL, std::move(left_expr), std::move(right_expr)); ASSERT_EQ(float_expr.try_get_value(float_result), RC::SUCCESS); - EXPECT_FLOAT_EQ(float_result.get_float(), 1.1*2.2); + EXPECT_FLOAT_EQ(float_result.get_float(), 1.1 * 2.2); left_expr.reset(new ValueExpr(float_value1)); right_expr.reset(new ValueExpr(float_value2)); float_expr.~ArithmeticExpr(); new (&float_expr)(ArithmeticExpr)(ArithmeticExpr::Type::DIV, std::move(left_expr), std::move(right_expr)); ASSERT_EQ(float_expr.try_get_value(float_result), RC::SUCCESS); - EXPECT_FLOAT_EQ(float_result.get_float(), 1.1/2.2); + EXPECT_FLOAT_EQ(float_result.get_float(), 1.1 / 2.2); Value zero_int_value(0); Value zero_float_value((float)0); diff --git a/unittest/bitmap_test.cpp b/unittest/bitmap_test.cpp index 950910a8b..cc4006139 100644 --- a/unittest/bitmap_test.cpp +++ b/unittest/bitmap_test.cpp @@ -14,8 +14,8 @@ See the Mulan PSL v2 for more details. */ #include -#include "gtest/gtest.h" #include "common/lang/bitmap.h" +#include "gtest/gtest.h" #include using namespace common; diff --git a/unittest/bp_manager_test.cpp b/unittest/bp_manager_test.cpp index 36f231ffd..cb5dc5442 100644 --- a/unittest/bp_manager_test.cpp +++ b/unittest/bp_manager_test.cpp @@ -18,8 +18,8 @@ See the Mulan PSL v2 for more details. */ void test_get(BPFrameManager &frame_manager) { const int file_desc = 0; - PageNum page_num = 1; - Frame *frame1 = frame_manager.alloc(file_desc, page_num); + PageNum page_num = 1; + Frame *frame1 = frame_manager.alloc(file_desc, page_num); ASSERT_NE(frame1, nullptr); frame1->set_file_desc(file_desc); @@ -73,8 +73,8 @@ void test_alloc(BPFrameManager &frame_manager) std::list used_list; const int file_desc = 0; - size_t size = 0; - for ( ; true; size++) { + size_t size = 0; + for (; true; size++) { Frame *item = frame_manager.alloc(file_desc, size); if (item != nullptr) { item->set_file_desc(file_desc); @@ -95,7 +95,7 @@ void test_alloc(BPFrameManager &frame_manager) } for (size_t i = size * 2; i < size * 10; i++) { - if (i % 2 == 0) { // from size * 2, that free one frame first + if (i % 2 == 0) { // from size * 2, that free one frame first Frame *item = used_list.front(); used_list.pop_front(); diff --git a/unittest/bplus_tree_test.cpp b/unittest/bplus_tree_test.cpp index 960824b94..3c0dda14d 100644 --- a/unittest/bplus_tree_test.cpp +++ b/unittest/bplus_tree_test.cpp @@ -12,13 +12,13 @@ See the Mulan PSL v2 for more details. */ // Created by longda on 2022 // -#include #include +#include -#include "storage/index/bplus_tree.h" -#include "storage/buffer/disk_buffer_pool.h" #include "common/log/log.h" #include "sql/parser/parse_defs.h" +#include "storage/buffer/disk_buffer_pool.h" +#include "storage/index/bplus_tree.h" #include "gtest/gtest.h" using namespace common; @@ -31,17 +31,14 @@ using namespace common; #define POOL_NUM 2 BufferPoolManager bpm; -BplusTreeHandler *handler = nullptr; -const char *index_name = "test.btree"; -int insert_num = INSERT_NUM; -const int page_size = 1024; -RID rid, check_rid; -int k = 0; - -void init_bpm() -{ - BufferPoolManager::set_instance(&bpm); -} +BplusTreeHandler *handler = nullptr; +const char *index_name = "test.btree"; +int insert_num = INSERT_NUM; +const int page_size = 1024; +RID rid, check_rid; +int k = 0; + +void init_bpm() { BufferPoolManager::set_instance(&bpm); } void test_insert() { RC rc = RC::SUCCESS; @@ -111,7 +108,7 @@ void test_insert() handler->print_leafs(); int start = insert_num / TIMES > page_size ? page_size : insert_num / TIMES; - int end = insert_num / TIMES > page_size ? (2 * page_size) : (2 * insert_num / TIMES); + int end = insert_num / TIMES > page_size ? (2 * page_size) : (2 * insert_num / TIMES); for (int i = start; i < end; i++) { rid.page_num = i / page_size; rid.slot_num = i % page_size; @@ -123,11 +120,11 @@ void test_insert() } else { LOG_INFO("check duplicate Insert %d. rid=%s. i%TIMES=%d", i, rid.to_string().c_str(), i%TIMES); } - rc = handler->insert_entry((const char *)&i, &rid); + rc = handler->insert_entry((const char *)&i, &rid); int t = i % TIMES; if (t == 0 || t == 1 || t == 2) { if (rc != RC::RECORD_DUPLICATE_KEY) { - LOG_WARN("insert duplicate key success"); + LOG_WARN("insert duplicate key success"); } ASSERT_EQ(RC::RECORD_DUPLICATE_KEY, rc); } else { @@ -164,7 +161,7 @@ void test_get() void test_delete() { - RC rc = RC::SUCCESS; + RC rc = RC::SUCCESS; std::list rids; for (int i = 0; i < insert_num / 2; i++) { @@ -183,7 +180,7 @@ void test_delete() rc = handler->delete_entry((const char *)&i, &rid); if (rc != RC::SUCCESS) { - LOG_WARN("failed to delete entry. i=%d, rid=%s", i, rid.to_string().c_str()); + LOG_WARN("failed to delete entry. i=%d, rid=%s", i, rid.to_string().c_str()); } ASSERT_EQ(RC::SUCCESS, rc); @@ -232,12 +229,12 @@ void test_delete() ASSERT_EQ(0, rids.size()); } else { if (rids.size() != 1) { - LOG_WARN("invalid. i=%d, rid=%s, check rid=%s", i, rid.to_string().c_str(), check_rid.to_string().c_str()); + LOG_WARN("invalid. i=%d, rid=%s, check rid=%s", i, rid.to_string().c_str(), check_rid.to_string().c_str()); } ASSERT_EQ(1, rids.size()); check_rid = rids.front(); if (rid != check_rid) { - LOG_WARN("invalid. i=%d, rid=%s, check rid=%s", i, rid.to_string().c_str(), check_rid.to_string().c_str()); + LOG_WARN("invalid. i=%d, rid=%s, check rid=%s", i, rid.to_string().c_str(), check_rid.to_string().c_str()); } ASSERT_EQ(rid.page_num, check_rid.page_num); ASSERT_EQ(rid.slot_num, check_rid.slot_num); @@ -300,7 +297,7 @@ void test_delete() } else { LOG_INFO("Begin to insert entry of index, rid: %s", rid.to_string().c_str()); } - rc = handler->insert_entry((const char *)&i, &rid); + rc = handler->insert_entry((const char *)&i, &rid); int t = i % TIMES; if (t == 0 || t == 1 || t == 2) { ASSERT_EQ(RC::SUCCESS, rc); @@ -315,12 +312,12 @@ void test_delete() TEST(test_bplus_tree, test_leaf_index_node_handle) { IndexFileHeader index_file_header; - index_file_header.root_page = BP_INVALID_PAGE_NUM; + index_file_header.root_page = BP_INVALID_PAGE_NUM; index_file_header.internal_max_size = 5; - index_file_header.leaf_max_size = 5; - index_file_header.attr_length = 4; - index_file_header.key_length = 4 + sizeof(RID); - index_file_header.attr_type = INTS; + index_file_header.leaf_max_size = 5; + index_file_header.attr_length = 4; + index_file_header.key_length = 4 + sizeof(RID); + index_file_header.attr_type = INTS; Frame frame; @@ -332,14 +329,14 @@ TEST(test_bplus_tree, test_leaf_index_node_handle) ASSERT_EQ(0, leaf_node.size()); bool found; - int index; + int index; char key_mem[4 + sizeof(RID)]; - int &key = *(int *)key_mem; - RID &rid = *(RID *)(key_mem + 4); + int &key = *(int *)key_mem; + RID &rid = *(RID *)(key_mem + 4); rid.page_num = 0; rid.slot_num = 0; for (int i = 0; i < 5; i++) { - key = i * 2 + 1; + key = i * 2 + 1; index = leaf_node.lookup(key_comparator, key_mem, &found); ASSERT_EQ(false, found); leaf_node.insert(index, (const char *)&key, (const char *)&rid); @@ -348,19 +345,19 @@ TEST(test_bplus_tree, test_leaf_index_node_handle) ASSERT_EQ(5, leaf_node.size()); for (int i = 0; i < 5; i++) { - key = i * 2; + key = i * 2; index = leaf_node.lookup(key_comparator, key_mem, &found); ASSERT_EQ(false, found); ASSERT_EQ(i, index); } - key = 12; + key = 12; index = leaf_node.lookup(key_comparator, key_mem, &found); ASSERT_EQ(false, found); ASSERT_EQ(5, index); for (int i = 0; i < 5; i++) { - key = i * 2 + 1; + key = i * 2 + 1; index = leaf_node.lookup(key_comparator, key_mem, &found); if (!found || i != index) { printf("found=%d, index=%d, key=%d", found, index, key); @@ -372,12 +369,12 @@ TEST(test_bplus_tree, test_leaf_index_node_handle) TEST(test_bplus_tree, test_internal_index_node_handle) { IndexFileHeader index_file_header; - index_file_header.root_page = BP_INVALID_PAGE_NUM; + index_file_header.root_page = BP_INVALID_PAGE_NUM; index_file_header.internal_max_size = 5; - index_file_header.leaf_max_size = 5; - index_file_header.attr_length = 4; - index_file_header.key_length = 4 + sizeof(RID); - index_file_header.attr_type = INTS; + index_file_header.leaf_max_size = 5; + index_file_header.attr_length = 4; + index_file_header.key_length = 4 + sizeof(RID); + index_file_header.attr_type = INTS; Frame frame; @@ -389,15 +386,15 @@ TEST(test_bplus_tree, test_internal_index_node_handle) ASSERT_EQ(0, internal_node.size()); bool found; - int index; - int insert_position; + int index; + int insert_position; char key_mem[4 + sizeof(RID)]; - int &key = *(int *)key_mem; - RID &rid = *(RID *)(key_mem + 4); + int &key = *(int *)key_mem; + RID &rid = *(RID *)(key_mem + 4); rid.page_num = 0; rid.slot_num = 0; - key = 0; + key = 0; index = internal_node.lookup(key_comparator, key_mem, &found, &insert_position); ASSERT_EQ(false, found); ASSERT_EQ(0, index); @@ -413,49 +410,49 @@ TEST(test_bplus_tree, test_internal_index_node_handle) ASSERT_EQ(5, internal_node.size()); for (int i = 1; i < 5; i++) { - key = i * 2 + 1; - int real_key = *(int*)internal_node.key_at(i); + key = i * 2 + 1; + int real_key = *(int *)internal_node.key_at(i); ASSERT_EQ(key, real_key); } - key = 0; + key = 0; index = internal_node.lookup(key_comparator, key_mem, &found, &insert_position); ASSERT_EQ(false, found); ASSERT_EQ(0, index); ASSERT_EQ(1, insert_position); - key = 2; + key = 2; index = internal_node.lookup(key_comparator, key_mem, &found, &insert_position); ASSERT_EQ(false, found); ASSERT_EQ(0, index); ASSERT_EQ(1, insert_position); - key = 4; + key = 4; index = internal_node.lookup(key_comparator, key_mem, &found, &insert_position); ASSERT_EQ(false, found); ASSERT_EQ(1, index); ASSERT_EQ(2, insert_position); - key = 8; + key = 8; index = internal_node.lookup(key_comparator, key_mem, &found, &insert_position); ASSERT_EQ(false, found); ASSERT_EQ(3, index); ASSERT_EQ(4, insert_position); - key = 10; + key = 10; index = internal_node.lookup(key_comparator, key_mem, &found, &insert_position); ASSERT_EQ(false, found); ASSERT_EQ(4, index); ASSERT_EQ(5, insert_position); - key = 12; + key = 12; index = internal_node.lookup(key_comparator, key_mem, &found); ASSERT_EQ(false, found); ASSERT_EQ(4, index); ASSERT_EQ(5, insert_position); for (int i = 1; i < 5; i++) { - key = i * 2 + 1; + key = i * 2 + 1; index = internal_node.lookup(key_comparator, key_mem, &found); if (!found || i != index) { printf("found=%d, index=%d, key=%d", found, index, key); @@ -474,20 +471,14 @@ TEST(test_bplus_tree, test_chars) handler = new BplusTreeHandler(); handler->create(index_name, CHARS, 8, ORDER, ORDER); - char keys[][9] = { - "abcdefg", - "12345678", - "12345678", - "abcdefg", - "abcdefga" - }; + char keys[][9] = {"abcdefg", "12345678", "12345678", "abcdefg", "abcdefga"}; RID rid; - RC rc = RC::SUCCESS; - for (size_t i = 0; i < sizeof(keys)/sizeof(keys[0]); i++) { + RC rc = RC::SUCCESS; + for (size_t i = 0; i < sizeof(keys) / sizeof(keys[0]); i++) { rid.page_num = 0; rid.slot_num = i; - rc = handler->insert_entry(keys[i], &rid); + rc = handler->insert_entry(keys[i], &rid); ASSERT_EQ(RC::SUCCESS, rc); } @@ -496,8 +487,8 @@ TEST(test_bplus_tree, test_chars) LOG_INFO("end to print bplus tree of chars"); BplusTreeScanner scanner(*handler); - const char *key = "abcdefg"; - rc = scanner.open(key, strlen(key), true, key, strlen(key), true); + const char *key = "abcdefg"; + rc = scanner.open(key, strlen(key), true, key, strlen(key), true); ASSERT_EQ(rc, RC::SUCCESS); int count = 0; @@ -518,14 +509,14 @@ TEST(test_bplus_tree, test_scanner) handler->create(index_name, INTS, sizeof(int), ORDER, ORDER); int count = 0; - RC rc = RC::SUCCESS; + RC rc = RC::SUCCESS; RID rid; // 初始化B树,插入数据[1 - 199] 所有奇数 for (int i = 0; i < 100; i++) { - int key = i * 2 + 1; + int key = i * 2 + 1; rid.page_num = 0; rid.slot_num = key; - rc = handler->insert_entry((const char *)&key, &rid); + rc = handler->insert_entry((const char *)&key, &rid); ASSERT_EQ(RC::SUCCESS, rc); } @@ -534,8 +525,8 @@ TEST(test_bplus_tree, test_scanner) BplusTreeScanner scanner(*handler); int begin = -100; - int end = -20; - rc = scanner.open((const char *)&begin, 4, false, (const char *)&end, 4, false); + int end = -20; + rc = scanner.open((const char *)&begin, 4, false, (const char *)&end, 4, false); ASSERT_EQ(RC::SUCCESS, rc); rc = scanner.next_entry(rid); @@ -544,8 +535,8 @@ TEST(test_bplus_tree, test_scanner) scanner.close(); begin = -100; - end = 1; - rc = scanner.open((const char *)&begin, 4, false, (const char *)&end, 4, false); + end = 1; + rc = scanner.open((const char *)&begin, 4, false, (const char *)&end, 4, false); ASSERT_EQ(RC::SUCCESS, rc); rc = scanner.next_entry(rid); ASSERT_EQ(RC::RECORD_EOF, rc); @@ -553,8 +544,8 @@ TEST(test_bplus_tree, test_scanner) scanner.close(); begin = -100; - end = 1; - rc = scanner.open((const char *)&begin, 4, false, (const char *)&end, 4, true/*inclusive*/); + end = 1; + rc = scanner.open((const char *)&begin, 4, false, (const char *)&end, 4, true /*inclusive*/); ASSERT_EQ(RC::SUCCESS, rc); rc = scanner.next_entry(rid); ASSERT_EQ(RC::SUCCESS, rc); @@ -564,8 +555,8 @@ TEST(test_bplus_tree, test_scanner) scanner.close(); begin = 1; - end = 3; - rc = scanner.open((const char *)&begin, 4, false, (const char *)&end, 4, false/*inclusive*/); + end = 3; + rc = scanner.open((const char *)&begin, 4, false, (const char *)&end, 4, false /*inclusive*/); ASSERT_EQ(RC::SUCCESS, rc); rc = scanner.next_entry(rid); ASSERT_EQ(RC::RECORD_EOF, rc); @@ -573,8 +564,8 @@ TEST(test_bplus_tree, test_scanner) scanner.close(); begin = 1; - end = 3; - rc = scanner.open((const char *)&begin, 4, true, (const char *)&end, 4, true/*inclusive*/); + end = 3; + rc = scanner.open((const char *)&begin, 4, true, (const char *)&end, 4, true /*inclusive*/); ASSERT_EQ(RC::SUCCESS, rc); while ((rc = scanner.next_entry(rid)) == RC::SUCCESS) { count++; @@ -585,8 +576,8 @@ TEST(test_bplus_tree, test_scanner) scanner.close(); begin = 0; - end = 3; - rc = scanner.open((const char *)&begin, 4, true, (const char *)&end, 4, true/*inclusive*/); + end = 3; + rc = scanner.open((const char *)&begin, 4, true, (const char *)&end, 4, true /*inclusive*/); ASSERT_EQ(RC::SUCCESS, rc); count = 0; while ((rc = scanner.next_entry(rid)) == RC::SUCCESS) { @@ -598,8 +589,8 @@ TEST(test_bplus_tree, test_scanner) scanner.close(); begin = 11; - end = 21; - rc = scanner.open((const char *)&begin, 4, true, (const char *)&end, 4, true/*inclusive*/); + end = 21; + rc = scanner.open((const char *)&begin, 4, true, (const char *)&end, 4, true /*inclusive*/); ASSERT_EQ(RC::SUCCESS, rc); count = 0; while ((rc = scanner.next_entry(rid)) == RC::SUCCESS) { @@ -611,8 +602,8 @@ TEST(test_bplus_tree, test_scanner) scanner.close(); begin = 11; - end = 91; - rc = scanner.open((const char *)&begin, 4, true, (const char *)&end, 4, true/*inclusive*/); + end = 91; + rc = scanner.open((const char *)&begin, 4, true, (const char *)&end, 4, true /*inclusive*/); ASSERT_EQ(RC::SUCCESS, rc); count = 0; while ((rc = scanner.next_entry(rid)) == RC::SUCCESS) { @@ -624,8 +615,8 @@ TEST(test_bplus_tree, test_scanner) scanner.close(); begin = 191; - end = 199; - rc = scanner.open((const char *)&begin, 4, true, (const char *)&end, 4, true/*inclusive*/); + end = 199; + rc = scanner.open((const char *)&begin, 4, true, (const char *)&end, 4, true /*inclusive*/); ASSERT_EQ(RC::SUCCESS, rc); count = 0; while ((rc = scanner.next_entry(rid)) == RC::SUCCESS) { @@ -637,8 +628,8 @@ TEST(test_bplus_tree, test_scanner) scanner.close(); begin = 191; - end = 201; - rc = scanner.open((const char *)&begin, 4, true, (const char *)&end, 4, true/*inclusive*/); + end = 201; + rc = scanner.open((const char *)&begin, 4, true, (const char *)&end, 4, true /*inclusive*/); ASSERT_EQ(RC::SUCCESS, rc); count = 0; while ((rc = scanner.next_entry(rid)) == RC::SUCCESS) { @@ -650,8 +641,8 @@ TEST(test_bplus_tree, test_scanner) scanner.close(); begin = 200; - end = 301; - rc = scanner.open((const char *)&begin, 4, true, (const char *)&end, 4, true/*inclusive*/); + end = 301; + rc = scanner.open((const char *)&begin, 4, true, (const char *)&end, 4, true /*inclusive*/); ASSERT_EQ(RC::SUCCESS, rc); rc = scanner.next_entry(rid); ASSERT_EQ(RC::RECORD_EOF, rc); @@ -659,15 +650,15 @@ TEST(test_bplus_tree, test_scanner) scanner.close(); begin = 300; - end = 201; - rc = scanner.open((const char *)&begin, 4, true, (const char *)&end, 4, true/*inclusive*/); + end = 201; + rc = scanner.open((const char *)&begin, 4, true, (const char *)&end, 4, true /*inclusive*/); ASSERT_EQ(RC::INVALID_ARGUMENT, rc); scanner.close(); begin = 300; - end = 201; - rc = scanner.open(nullptr, 4, true, (const char *)&end, 4, true/*inclusive*/); + end = 201; + rc = scanner.open(nullptr, 4, true, (const char *)&end, 4, true /*inclusive*/); ASSERT_EQ(RC::SUCCESS, rc); count = 0; while ((rc = scanner.next_entry(rid)) == RC::SUCCESS) { @@ -679,8 +670,8 @@ TEST(test_bplus_tree, test_scanner) scanner.close(); begin = 300; - end = 10; - rc = scanner.open(nullptr, 4, true, (const char *)&end, 4, true/*inclusive*/); + end = 10; + rc = scanner.open(nullptr, 4, true, (const char *)&end, 4, true /*inclusive*/); ASSERT_EQ(RC::SUCCESS, rc); count = 0; while ((rc = scanner.next_entry(rid)) == RC::SUCCESS) { @@ -692,8 +683,8 @@ TEST(test_bplus_tree, test_scanner) scanner.close(); begin = 190; - end = 10; - rc = scanner.open((const char *)&begin, 4, true, nullptr, 4, true/*inclusive*/); + end = 10; + rc = scanner.open((const char *)&begin, 4, true, nullptr, 4, true /*inclusive*/); ASSERT_EQ(RC::SUCCESS, rc); count = 0; while ((rc = scanner.next_entry(rid)) == RC::SUCCESS) { @@ -705,8 +696,8 @@ TEST(test_bplus_tree, test_scanner) scanner.close(); begin = 190; - end = 10; - rc = scanner.open(nullptr, 0, true, nullptr, 0, true/*inclusive*/); + end = 10; + rc = scanner.open(nullptr, 0, true, nullptr, 0, true /*inclusive*/); ASSERT_EQ(RC::SUCCESS, rc); count = 0; while ((rc = scanner.next_entry(rid)) == RC::SUCCESS) { @@ -746,7 +737,7 @@ int main(int argc, char **argv) // 调用RUN_ALL_TESTS()运行所有测试用例 // main函数返回RUN_ALL_TESTS()的运行结果 - LoggerFactory::init_default("test.log", LOG_LEVEL_TRACE); + LoggerFactory::init_default("test.log", LOG_LEVEL_TRACE); init_bpm(); int rc = RUN_ALL_TESTS(); diff --git a/unittest/clog_test.cpp b/unittest/clog_test.cpp index fe728009c..ebb41ad0e 100644 --- a/unittest/clog_test.cpp +++ b/unittest/clog_test.cpp @@ -22,13 +22,12 @@ using namespace common; TEST(test_clog, test_clog) { - const char *path = "."; + const char *path = "."; const char *clog_file = "./clog"; remove(clog_file); - CLogManager log_mgr; - RC rc = log_mgr.init(path); + RC rc = log_mgr.init(path); ASSERT_EQ(rc, RC::SUCCESS); // TODO test @@ -51,7 +50,7 @@ int main(int argc, char **argv) testing::InitGoogleTest(&argc, argv); LoggerFactory::init_default("clog_test.log", LOG_LEVEL_TRACE); - + // 调用RUN_ALL_TESTS()运行所有测试用例 // main函数返回RUN_ALL_TESTS()的运行结果 return RUN_ALL_TESTS(); diff --git a/unittest/log_test.cpp b/unittest/log_test.cpp index 812b16691..bb7537086 100644 --- a/unittest/log_test.cpp +++ b/unittest/log_test.cpp @@ -43,7 +43,7 @@ int LogTest::init(const std::string &logFile) void *LogTest::log_loop(void *param) { int index = *(int *)param; - int i = 0; + int i = 0; while (i < 100) { i++; LOG_INFO("index:%d --> %d", index, i); @@ -63,10 +63,7 @@ void checkRotate() test.log_loop(&index); } -TEST(checkRotateTest, CheckRoateTest) -{ - checkRotate(); -} +TEST(checkRotateTest, CheckRoateTest) { checkRotate(); } void testEnableTest() { @@ -93,10 +90,7 @@ void testEnableTest() ASSERT_EQ(g_log->check_output(LOG_LEVEL_LAST, __FILE__), true); } -TEST(testEnableTest, CheckEnableTest) -{ - testEnableTest(); -} +TEST(testEnableTest, CheckEnableTest) { testEnableTest(); } int main(int argc, char **argv) { diff --git a/unittest/log_test.h b/unittest/log_test.h index ff6326820..e1928d544 100644 --- a/unittest/log_test.h +++ b/unittest/log_test.h @@ -20,7 +20,8 @@ See the Mulan PSL v2 for more details. */ /* * */ -class LogTest { +class LogTest +{ public: LogTest(); virtual ~LogTest(); diff --git a/unittest/lower_bound_test.cpp b/unittest/lower_bound_test.cpp index f701f48cf..02c6656a7 100644 --- a/unittest/lower_bound_test.cpp +++ b/unittest/lower_bound_test.cpp @@ -12,30 +12,30 @@ See the Mulan PSL v2 for more details. */ // Created by Wangyunlai on 2022/9/5. // -#include -#include "gtest/gtest.h" #include "common/lang/lower_bound.h" +#include "gtest/gtest.h" +#include using namespace common; TEST(lower_bound, test_lower_bound) { - int items[] = {1, 3, 5, 7, 9}; - const int count = sizeof(items) / sizeof(items[0]); + int items[] = {1, 3, 5, 7, 9}; + const int count = sizeof(items) / sizeof(items[0]); std::vector v(items, items + count); for (int i = 0; i <= 10; i++) { - bool found = false; + bool found = false; std::vector::iterator retval = lower_bound(v.begin(), v.end(), i, &found); ASSERT_EQ(retval - v.begin(), i / 2); ASSERT_EQ(found, (i % 2 != 0)); } - int empty_items = {}; - const int empty_count = 0; + int empty_items = {}; + const int empty_count = 0; std::vector empty_v(empty_items, empty_items + empty_count); for (int i = 0; i <= 10; i++) { - bool found = false; + bool found = false; std::vector::iterator retval = lower_bound(empty_v.begin(), empty_v.end(), i, &found); ASSERT_EQ(retval - empty_v.begin(), 0); ASSERT_EQ(found, false); diff --git a/unittest/md5_test.cpp b/unittest/md5_test.cpp index 7f20c5a65..3da795964 100644 --- a/unittest/md5_test.cpp +++ b/unittest/md5_test.cpp @@ -31,7 +31,7 @@ Md5Test::~Md5Test() void Md5Test::string() { - char buf[512] = "/home/fastdfs/longda"; + char buf[512] = "/home/fastdfs/longda"; unsigned char digest[16] = {0}; MD5String(buf, digest); for (int i = 0; i < 16; i++) { diff --git a/unittest/md5_test.h b/unittest/md5_test.h index 77b2ea599..1a684c24d 100644 --- a/unittest/md5_test.h +++ b/unittest/md5_test.h @@ -18,7 +18,8 @@ See the Mulan PSL v2 for more details. */ /* * */ -class Md5Test { +class Md5Test +{ public: Md5Test(); virtual ~Md5Test(); diff --git a/unittest/mem_pool_test.cpp b/unittest/mem_pool_test.cpp index db65ed552..fc8c56f7f 100644 --- a/unittest/mem_pool_test.cpp +++ b/unittest/mem_pool_test.cpp @@ -12,10 +12,10 @@ See the Mulan PSL v2 for more details. */ // Created by longda on 2022 // -#include -#include #include "common/mm/mem_pool.h" #include "gtest/gtest.h" +#include +#include using namespace common; diff --git a/unittest/persist_test.cpp b/unittest/persist_test.cpp index 880eb5285..eac39350d 100644 --- a/unittest/persist_test.cpp +++ b/unittest/persist_test.cpp @@ -12,19 +12,19 @@ See the Mulan PSL v2 for more details. */ // Created by qiling on 2022 // -#include -#include "gtest/gtest.h" #include "storage/persist/persist.h" +#include "gtest/gtest.h" +#include const int MAX_LEN = 50; TEST(test_persist, test_persist_file_io) { - std::string file_name_1 = "test_persist_file_io-file_name_1"; - std::string file_name_2 = "test_persist_file_io-file_name_2"; + std::string file_name_1 = "test_persist_file_io-file_name_1"; + std::string file_name_2 = "test_persist_file_io-file_name_2"; PersistHandler persist_handler; PersistHandler persist_handler_2; - RC rc; + RC rc; // prepare remove(file_name_1.c_str()); @@ -58,10 +58,10 @@ TEST(test_persist, test_persist_file_io) ASSERT_EQ(rc, RC::SUCCESS); // write - std::string str_1 = "this is a string 001. "; - std::string str_2 = "this is a string 002002. "; - std::string str_3 = "THIS IS A STRING 003. "; - int64_t write_size = 0; + std::string str_1 = "this is a string 001. "; + std::string str_2 = "this is a string 002002. "; + std::string str_3 = "THIS IS A STRING 003. "; + int64_t write_size = 0; rc = persist_handler.write_file(str_1.size(), str_1.c_str(), &write_size); ASSERT_EQ(rc, RC::SUCCESS); @@ -88,8 +88,8 @@ TEST(test_persist, test_persist_file_io) ASSERT_EQ(rc, RC::FILE_NOT_EXIST); // read & seek - int64_t read_size = 0; - char buf[MAX_LEN] = {0}; + int64_t read_size = 0; + char buf[MAX_LEN] = {0}; rc = persist_handler.read_at(str_1.size(), str_2.size(), buf, &read_size); ASSERT_EQ(rc, RC::SUCCESS); diff --git a/unittest/pidfile_test.cpp b/unittest/pidfile_test.cpp index 751603bd3..b56918c96 100644 --- a/unittest/pidfile_test.cpp +++ b/unittest/pidfile_test.cpp @@ -15,9 +15,9 @@ See the Mulan PSL v2 for more details. */ #include "gtest/gtest.h" -#include "common/os/pidfile.h" #include "common/io/io.h" #include "common/lang/string.h" +#include "common/os/pidfile.h" using namespace common; @@ -30,12 +30,12 @@ int main() std::string pidFile = getPidPath(); - char *p = NULL; + char *p = NULL; size_t size = 0; readFromFile(pidFile, p, size); std::string temp(p); - long long target = 0; + long long target = 0; str_to_val(temp, target); free(p); diff --git a/unittest/record_manager_test.cpp b/unittest/record_manager_test.cpp index 7fc60db5a..de8734c1a 100644 --- a/unittest/record_manager_test.cpp +++ b/unittest/record_manager_test.cpp @@ -12,13 +12,13 @@ See the Mulan PSL v2 for more details. */ // Created by wangyunlai.wyl on 2022 // -#include #include +#include -#include "gtest/gtest.h" #include "storage/buffer/disk_buffer_pool.h" #include "storage/record/record_manager.h" #include "storage/trx/vacuous_trx.h" +#include "gtest/gtest.h" using namespace common; @@ -28,18 +28,18 @@ TEST(test_record_page_handler, test_record_page_handler) ::remove(record_manager_file); BufferPoolManager *bpm = new BufferPoolManager(); - DiskBufferPool *bp = nullptr; - RC rc = bpm->create_file(record_manager_file); + DiskBufferPool *bp = nullptr; + RC rc = bpm->create_file(record_manager_file); ASSERT_EQ(rc, RC::SUCCESS); - + rc = bpm->open_file(record_manager_file, bp); ASSERT_EQ(rc, RC::SUCCESS); Frame *frame = nullptr; - rc = bp->allocate_page(&frame); + rc = bp->allocate_page(&frame); ASSERT_EQ(rc, RC::SUCCESS); - const int record_size = 8; + const int record_size = 8; RecordPageHandler record_page_handle; rc = record_page_handle.init_empty_page(*bp, frame->page_num(), record_size); ASSERT_EQ(rc, RC::SUCCESS); @@ -47,7 +47,7 @@ TEST(test_record_page_handler, test_record_page_handler) RecordPageIterator iterator; iterator.init(record_page_handle); - int count = 0; + int count = 0; Record record; while (iterator.has_next()) { count++; @@ -57,10 +57,10 @@ TEST(test_record_page_handler, test_record_page_handler) ASSERT_EQ(count, 0); char buf[record_size]; - RID rid; + RID rid; rid.page_num = 100; rid.slot_num = 100; - rc = record_page_handle.insert_record(buf, &rid); + rc = record_page_handle.insert_record(buf, &rid); ASSERT_EQ(rc, RC::SUCCESS); count = 0; @@ -75,7 +75,7 @@ TEST(test_record_page_handler, test_record_page_handler) for (int i = 0; i < 10; i++) { rid.page_num = i; rid.slot_num = i; - rc = record_page_handle.insert_record(buf, &rid); + rc = record_page_handle.insert_record(buf, &rid); ASSERT_EQ(rc, RC::SUCCESS); } @@ -91,7 +91,7 @@ TEST(test_record_page_handler, test_record_page_handler) for (int i = 0; i < 5; i++) { rid.page_num = i * 2; rid.slot_num = i * 2; - rc = record_page_handle.delete_record(&rid); + rc = record_page_handle.delete_record(&rid); ASSERT_EQ(rc, RC::SUCCESS); } @@ -115,10 +115,10 @@ TEST(test_record_page_handler, test_record_file_iterator) ::remove(record_manager_file); BufferPoolManager *bpm = new BufferPoolManager(); - DiskBufferPool *bp = nullptr; - RC rc = bpm->create_file(record_manager_file); + DiskBufferPool *bp = nullptr; + RC rc = bpm->create_file(record_manager_file); ASSERT_EQ(rc, RC::SUCCESS); - + rc = bpm->open_file(record_manager_file, bp); ASSERT_EQ(rc, RC::SUCCESS); @@ -126,12 +126,12 @@ TEST(test_record_page_handler, test_record_file_iterator) rc = file_handler.init(bp); ASSERT_EQ(rc, RC::SUCCESS); - VacuousTrx trx; + VacuousTrx trx; RecordFileScanner file_scanner; - rc = file_scanner.open_scan(nullptr/*table*/, *bp, &trx, true/*readonly*/, nullptr/*condition_filter*/); + rc = file_scanner.open_scan(nullptr /*table*/, *bp, &trx, true /*readonly*/, nullptr /*condition_filter*/); ASSERT_EQ(rc, RC::SUCCESS); - int count = 0; + int count = 0; Record record; while (file_scanner.has_next()) { rc = file_scanner.next(record); @@ -140,9 +140,9 @@ TEST(test_record_page_handler, test_record_file_iterator) } file_scanner.close_scan(); ASSERT_EQ(count, 0); - - const int record_insert_num = 1000; - char record_data[20]; + + const int record_insert_num = 1000; + char record_data[20]; std::vector rids; for (int i = 0; i < record_insert_num; i++) { RID rid; @@ -151,7 +151,7 @@ TEST(test_record_page_handler, test_record_file_iterator) rids.push_back(rid); } - rc = file_scanner.open_scan(nullptr/*table*/, *bp, &trx, true/*readonly*/, nullptr/*condition_filter*/); + rc = file_scanner.open_scan(nullptr /*table*/, *bp, &trx, true /*readonly*/, nullptr /*condition_filter*/); ASSERT_EQ(rc, RC::SUCCESS); count = 0; @@ -162,13 +162,13 @@ TEST(test_record_page_handler, test_record_file_iterator) } file_scanner.close_scan(); ASSERT_EQ(count, rids.size()); - + for (int i = 0; i < record_insert_num; i += 2) { rc = file_handler.delete_record(&rids[i]); ASSERT_EQ(rc, RC::SUCCESS); } - rc = file_scanner.open_scan(nullptr/*table*/, *bp, &trx, true/*readonly*/, nullptr/*condition_filter*/); + rc = file_scanner.open_scan(nullptr /*table*/, *bp, &trx, true /*readonly*/, nullptr /*condition_filter*/); ASSERT_EQ(rc, RC::SUCCESS); count = 0; @@ -179,7 +179,7 @@ TEST(test_record_page_handler, test_record_file_iterator) } file_scanner.close_scan(); ASSERT_EQ(count, rids.size() / 2); - + bpm->close_file(record_manager_file); delete bpm; } diff --git a/unittest/ring_buffer_test.cpp b/unittest/ring_buffer_test.cpp index 332759bb9..aa3ca2233 100644 --- a/unittest/ring_buffer_test.cpp +++ b/unittest/ring_buffer_test.cpp @@ -18,7 +18,7 @@ See the Mulan PSL v2 for more details. */ TEST(ring_buffer, test_init) { - const int buf_size = 10; + const int buf_size = 10; RingBuffer buffer(buf_size); EXPECT_EQ(buffer.capacity(), buf_size); EXPECT_EQ(buffer.remain(), buf_size); @@ -28,12 +28,12 @@ TEST(ring_buffer, test_init) TEST(ring_buffer, test_write) { - const int buf_size = 25; + const int buf_size = 25; RingBuffer buffer(buf_size); - const char *data = "0123456789"; - int32_t size = strlen(data); - int32_t write_size = 0; + const char *data = "0123456789"; + int32_t size = strlen(data); + int32_t write_size = 0; EXPECT_EQ(buffer.write(data, size, write_size), RC::SUCCESS); EXPECT_EQ(write_size, size); EXPECT_EQ(buffer.size(), size); @@ -52,19 +52,19 @@ TEST(ring_buffer, test_write) TEST(ring_buffer, test_read) { - const int buf_size = 100; + const int buf_size = 100; RingBuffer buffer(buf_size); - const char *data = "0123456789"; - int32_t size = strlen(data); - int32_t write_size = 0; + const char *data = "0123456789"; + int32_t size = strlen(data); + int32_t write_size = 0; EXPECT_EQ(buffer.write(data, size, write_size), RC::SUCCESS); EXPECT_EQ(write_size, size); EXPECT_EQ(buffer.size(), size); EXPECT_EQ(buffer.remain(), buf_size - size); - char read_buf[buf_size]; - int32_t read_size = 0; + char read_buf[buf_size]; + int32_t read_size = 0; const int32_t test_read_size = 5; EXPECT_EQ(buffer.read(read_buf, test_read_size, read_size), RC::SUCCESS); EXPECT_EQ(read_size, test_read_size); @@ -84,19 +84,19 @@ TEST(ring_buffer, test_read) TEST(ring_buffer, test_buffer) { - const int buf_size = 15; + const int buf_size = 15; RingBuffer buffer(buf_size); - const char *data = "0123456789"; - int32_t size = strlen(data); - int32_t write_size = 0; + const char *data = "0123456789"; + int32_t size = strlen(data); + int32_t write_size = 0; EXPECT_EQ(buffer.write(data, size, write_size), RC::SUCCESS); EXPECT_EQ(write_size, size); EXPECT_EQ(buffer.size(), size); EXPECT_EQ(buffer.remain(), buf_size - size); - const char *tmp_buffer = nullptr; - int32_t buffer_size = 0; + const char *tmp_buffer = nullptr; + int32_t buffer_size = 0; EXPECT_EQ(buffer.buffer(tmp_buffer, buffer_size), RC::SUCCESS); EXPECT_EQ(buffer_size, size); EXPECT_EQ(buffer.forward(buffer_size), RC::SUCCESS);