diff --git a/deps/common/lang/lru_cache.h b/deps/common/lang/lru_cache.h index a41372d148a69752ee6a287efe77a6d3c00ec4b9..f3b06a9f2df51215d13aade77e8afe42ea66509c 100644 --- a/deps/common/lang/lru_cache.h +++ b/deps/common/lang/lru_cache.h @@ -19,14 +19,12 @@ See the Mulan PSL v2 for more details. */ namespace common { -template , - typename Pred = std::equal_to> +template , typename Pred = std::equal_to> class LruCache { class ListNode { public: - Key key_; + Key key_; Value value_; ListNode *prev_ = nullptr; @@ -39,9 +37,10 @@ class LruCache { class PListNodeHasher { public: - size_t operator() (ListNode *node) const { + size_t operator()(ListNode *node) const + { if (node == nullptr) { - return 0; + return 0; } return hasher_(node->key_); } @@ -52,26 +51,28 @@ class LruCache { class PListNodePredicator { public: - bool operator() (ListNode * const node1, ListNode * const node2) const { + bool operator()(ListNode *const node1, ListNode *const node2) const + { if (node1 == node2) { - return true; + return true; } if (node1 == nullptr || node2 == nullptr) { - return false; + return false; } return pred_(node1->key_, node2->key_); } + private: Pred pred_; }; -public: +public: LruCache(size_t reserve = 0) { if (reserve > 0) { - searcher_.reserve(reserve); + searcher_.reserve(reserve); } } @@ -88,7 +89,7 @@ public: searcher_.clear(); lru_front_ = nullptr; - lru_tail_ = nullptr; + lru_tail_ = nullptr; } size_t count() const @@ -112,10 +113,10 @@ public: { auto iter = searcher_.find((ListNode *)&key); if (iter != searcher_.end()) { - ListNode * ln = *iter; + ListNode *ln = *iter; ln->value_ = value; lru_touch(ln); - return ; + return; } ListNode *ln = new ListNode(key, value); @@ -136,7 +137,7 @@ public: value = nullptr; } - void foreach(std::function func) + void foreach (std::function func) { for (ListNode *node = lru_front_; node != nullptr; node = node->next_) { bool ret = func(node->key_, node->value_); @@ -165,7 +166,7 @@ private: } node->prev_->next_ = node->next_; - + if (node->next_ != nullptr) { node->next_->prev_ = node->prev_; } else { @@ -220,9 +221,9 @@ private: private: using SearchType = std::unordered_set; - SearchType searcher_; - ListNode * lru_front_ = nullptr; - ListNode * lru_tail_ = nullptr; + SearchType searcher_; + ListNode *lru_front_ = nullptr; + ListNode *lru_tail_ = nullptr; }; -} // namespace common +} // namespace common diff --git a/src/observer/event/optimize_event.h b/src/observer/event/optimize_event.h index 2e7d98f6c198af6ec0ed5041adff93bdedae64ba..e2bdff5bc07bd041883a8d9831e1170c387d815c 100644 --- a/src/observer/event/optimize_event.h +++ b/src/observer/event/optimize_event.h @@ -22,16 +22,18 @@ class Stmt; class OptimizeEvent : public common::StageEvent { public: OptimizeEvent(SQLStageEvent *sql_event, common::StageEvent *parent_event) - : sql_event_(sql_event), parent_event_(parent_event) + : sql_event_(sql_event), parent_event_(parent_event) {} virtual ~OptimizeEvent() noexcept = default; - SQLStageEvent *sql_event() const { + SQLStageEvent *sql_event() const + { return sql_event_; } - common::StageEvent *parent_event() const { + common::StageEvent *parent_event() const + { return parent_event_; } @@ -39,4 +41,3 @@ private: SQLStageEvent *sql_event_ = nullptr; common::StageEvent *parent_event_ = nullptr; }; - diff --git a/src/observer/event/session_event.cpp b/src/observer/event/session_event.cpp index e6b6577e87ad2775b778d163272585205b3665a2..361bcaa6ccdb347d8c4853af7221c841de208d29 100644 --- a/src/observer/event/session_event.cpp +++ b/src/observer/event/session_event.cpp @@ -16,8 +16,7 @@ See the Mulan PSL v2 for more details. */ #include "net/communicator.h" SessionEvent::SessionEvent(Communicator *comm) : communicator_(comm) -{ -} +{} SessionEvent::~SessionEvent() { diff --git a/src/observer/event/session_event.h b/src/observer/event/session_event.h index 047099b4555f20a40b77e2388165bd262809fa5e..52810ece54da08bb9e68aca09828df0810781d7f 100644 --- a/src/observer/event/session_event.h +++ b/src/observer/event/session_event.h @@ -32,17 +32,29 @@ public: Communicator *get_communicator() const; Session *session() const; - void set_query(const std::string &query) { query_ = query; } - void set_sql_result(SqlResult *result) { sql_result_ = result; } - const std::string &query() const { return query_; } - SqlResult *sql_result() const { return sql_result_; } + void set_query(const std::string &query) + { + query_ = query; + } + void set_sql_result(SqlResult *result) + { + sql_result_ = result; + } + const std::string &query() const + { + return query_; + } + SqlResult *sql_result() const + { + return sql_result_; + } const char *get_response() const; void set_response(const char *response); void set_response(const char *response, int len); void set_response(std::string &&response); int get_response_len() const; - const char *get_request_buf(); // TODO remove me + const char *get_request_buf(); // TODO remove me private: Communicator *communicator_ = nullptr; diff --git a/src/observer/event/sql_event.h b/src/observer/event/sql_event.h index ecd60fca7266c770f0fd7a35354a81ac18a3ab16..b5875a30b3af6a175429923b606b747847dac8dd 100644 --- a/src/observer/event/sql_event.h +++ b/src/observer/event/sql_event.h @@ -23,8 +23,7 @@ class SessionEvent; class Stmt; class Command; -class SQLStageEvent : public common::StageEvent -{ +class SQLStageEvent : public common::StageEvent { public: SQLStageEvent(SessionEvent *event, const std::string &sql); virtual ~SQLStageEvent() noexcept; @@ -34,16 +33,43 @@ public: return session_event_; } - const std::string &sql() const { return sql_; } - const std::unique_ptr &command() const { return command_; } - Stmt *stmt() const { return stmt_; } - std::unique_ptr &physical_operator() { return operator_; } - const std::unique_ptr &physical_operator() const { return operator_; } + const std::string &sql() const + { + return sql_; + } + const std::unique_ptr &command() const + { + return command_; + } + Stmt *stmt() const + { + return stmt_; + } + std::unique_ptr &physical_operator() + { + return operator_; + } + const std::unique_ptr &physical_operator() const + { + return operator_; + } - void set_sql(const char *sql) { sql_ = sql; } - void set_command(std::unique_ptr cmd) { command_ = std::move(cmd); } - void set_stmt(Stmt *stmt) { stmt_ = stmt; } - void set_operator(std::unique_ptr oper) { operator_ = std::move(oper); } + void set_sql(const char *sql) + { + sql_ = sql; + } + void set_command(std::unique_ptr cmd) + { + command_ = std::move(cmd); + } + void set_stmt(Stmt *stmt) + { + stmt_ = stmt; + } + void set_operator(std::unique_ptr oper) + { + operator_ = std::move(oper); + } private: SessionEvent *session_event_ = nullptr; @@ -52,4 +78,3 @@ private: Stmt *stmt_ = nullptr; std::unique_ptr operator_; }; - diff --git a/src/observer/net/communicator.cpp b/src/observer/net/communicator.cpp index cea06a0790ab87b3543c4c218751e7417e46c1b1..d836f6d94f6538f563abac187bb7baabb6190090 100644 --- a/src/observer/net/communicator.cpp +++ b/src/observer/net/communicator.cpp @@ -20,7 +20,6 @@ See the Mulan PSL v2 for more details. */ #include "common/io/io.h" #include "session/session.h" - RC Communicator::init(int fd, Session *session, const std::string &addr) { fd_ = fd; @@ -88,7 +87,6 @@ RC PlainCommunicator::read_event(SessionEvent *&event) data_len += read_len; } - if (data_len > max_packet_size) { LOG_WARN("The length of sql exceeds the limitation %d", max_packet_size); return RC::IOERR; @@ -107,7 +105,6 @@ RC PlainCommunicator::read_event(SessionEvent *&event) return rc; } - RC PlainCommunicator::write_state(SessionEvent *event, bool &need_disconnect) { SqlResult *sql_result = event->sql_result(); @@ -137,12 +134,12 @@ RC PlainCommunicator::write_state(SessionEvent *event, bool &need_disconnect) RC PlainCommunicator::write_result(SessionEvent *event, bool &need_disconnect) { need_disconnect = true; - + const char message_terminate = '\0'; SqlResult *sql_result = event->sql_result(); if (nullptr == sql_result) { - + const char *response = event->get_response(); int len = event->get_response_len(); @@ -249,7 +246,7 @@ RC PlainCommunicator::write_result(SessionEvent *event, bool &need_disconnect) if (rc == RC::RECORD_EOF) { rc = RC::SUCCESS; } - + if (cell_num == 0) { // 除了select之外,其它的消息通常不会通过operator来返回结果,表头和行数据都是空的 // 这里针对这种情况做特殊处理,当表头和行数据都是空的时候,就返回处理的结果 @@ -271,7 +268,6 @@ RC PlainCommunicator::write_result(SessionEvent *event, bool &need_disconnect) ///////////////////////////////////////////////////////////////////////////////// - Communicator *CommunicatorFactory::create(CommunicateProtocol protocol) { switch (protocol) { diff --git a/src/observer/net/communicator.h b/src/observer/net/communicator.h index 9e58792e822749ae131e66c68f964f90a7cb0b63..3954b152f570ccd22dfc566dfc65d29dcbfff4fd 100644 --- a/src/observer/net/communicator.h +++ b/src/observer/net/communicator.h @@ -28,10 +28,10 @@ class Session; * 在listener接收到一个新的连接(参考 server.cpp::accept), 就创建一个Communicator对象。 * 并调用init进行初始化。 * 在server中监听到某个连接有新的消息,就通过Communicator::read_event接收消息。 - + */ class Communicator { -public: +public: virtual ~Communicator(); /** @@ -57,18 +57,27 @@ public: /** * 关联的会话信息 */ - Session *session() const { return session_; } + Session *session() const + { + return session_; + } /** * libevent使用的数据,参考server.cpp */ - struct event &read_event() { return read_event_; } + struct event &read_event() + { + return read_event_; + } /** * 对端地址 * 如果是unix socket,可能没有意义 */ - const char *addr() const { return addr_.c_str(); } + const char *addr() const + { + return addr_.c_str(); + } protected: Session *session_ = nullptr; @@ -82,26 +91,23 @@ protected: * 使用简单的文本通讯协议,每个消息使用'\0'结尾 */ class PlainCommunicator : public Communicator { -public: +public: RC read_event(SessionEvent *&event) override; RC write_result(SessionEvent *event, bool &need_disconnect) override; private: RC write_state(SessionEvent *event, bool &need_disconnect); - }; /** * 当前支持的通讯协议 */ -enum class CommunicateProtocol -{ +enum class CommunicateProtocol { PLAIN, //! 以'\0'结尾的协议 MYSQL, //! mysql通讯协议。具体实现参考 MysqlCommunicator }; -class CommunicatorFactory -{ -public: +class CommunicatorFactory { +public: Communicator *create(CommunicateProtocol protocol); }; diff --git a/src/observer/net/mysql_communicator.cpp b/src/observer/net/mysql_communicator.cpp index 8c6af1ab1f34b029ea0844c6204efeb564a0eed0..a5402a5cbb2a0f069764641cdd60c75a180cf63d 100644 --- a/src/observer/net/mysql_communicator.cpp +++ b/src/observer/net/mysql_communicator.cpp @@ -23,31 +23,30 @@ See the Mulan PSL v2 for more details. */ // https://dev.mysql.com/doc/dev/mysql-server/latest/group__group__cs__capabilities__flags.html // the flags below are negotiate by handshake packet -const uint32_t CLIENT_PROTOCOL_41 = 512; -//const uint32_t CLIENT_INTERACTIVE = 1024; // This is an interactive client -const uint32_t CLIENT_TRANSACTIONS = 8192; // Client knows about transactions. -const uint32_t CLIENT_SESSION_TRACK = (1UL << 23); // Capable of handling server state change information -const uint32_t CLIENT_DEPRECATE_EOF = (1UL << 24); // Client no longer needs EOF_Packet and will use OK_Packet instead -const uint32_t CLIENT_OPTIONAL_RESULTSET_METADATA = (1UL << 25); // The client can handle optional metadata information in the resultset. +const uint32_t CLIENT_PROTOCOL_41 = 512; +// const uint32_t CLIENT_INTERACTIVE = 1024; // This is an interactive client +const uint32_t CLIENT_TRANSACTIONS = 8192; // Client knows about transactions. +const uint32_t CLIENT_SESSION_TRACK = (1UL << 23); // Capable of handling server state change information +const uint32_t CLIENT_DEPRECATE_EOF = (1UL << 24); // Client no longer needs EOF_Packet and will use OK_Packet instead +const uint32_t CLIENT_OPTIONAL_RESULTSET_METADATA = + (1UL << 25); // The client can handle optional metadata information in the resultset. // Support optional extension for query parameters into the COM_QUERY and COM_STMT_EXECUTE packets. -//const uint32_t CLIENT_QUERY_ATTRIBUTES = (1UL << 27); +// const uint32_t CLIENT_QUERY_ATTRIBUTES = (1UL << 27); // https://dev.mysql.com/doc/dev/mysql-server/latest/group__group__cs__column__definition__flags.html // Column Definition Flags -//const uint32_t NOT_NULL_FLAG = 1; -//const uint32_t PRI_KEY_FLAG = 2; -//const uint32_t UNIQUE_KEY_FLAG = 4; -//const uint32_t MULTIPLE_KEY_FLAG = 8; -//const uint32_t NUM_FLAG = 32768; // Field is num (for clients) -//const uint32_t PART_KEY_FLAG = 16384; // Intern; Part of some key. - -enum ResultSetMetaData -{ +// const uint32_t NOT_NULL_FLAG = 1; +// const uint32_t PRI_KEY_FLAG = 2; +// const uint32_t UNIQUE_KEY_FLAG = 4; +// const uint32_t MULTIPLE_KEY_FLAG = 8; +// const uint32_t NUM_FLAG = 32768; // Field is num (for clients) +// const uint32_t PART_KEY_FLAG = 16384; // Intern; Part of some key. + +enum ResultSetMetaData { RESULTSET_METADATA_NONE = 0, RESULTSET_METADATA_FULL = 1, }; - /** Column types for MySQL */ @@ -185,13 +184,12 @@ int store_lenenc_string(char *buf, const char *s) * https://mariadb.com/kb/en/0-packet/ */ struct PacketHeader { - int32_t payload_length:24; //! 当前packet的除掉头的长度 - int8_t sequence_id = 0; //! 当前packet在当前处理过程中是第几个包 + int32_t payload_length : 24; //! 当前packet的除掉头的长度 + int8_t sequence_id = 0; //! 当前packet在当前处理过程中是第几个包 }; -class BasePacket -{ -public: +class BasePacket { +public: PacketHeader packet_header; BasePacket(int8_t sequence = 0) @@ -209,19 +207,19 @@ public: * 这个包会交互capability与用户名密码 * https://dev.mysql.com/doc/dev/mysql-server/latest/page_protocol_connection_phase_packets_protocol_handshake_v10.html */ -struct HandshakeV10 : public BasePacket -{ - int8_t protocol = 10; - char server_version[7] = "5.7.25"; - int32_t thread_id = 21501807; // conn id - char auth_plugin_data_part_1[9] = "12345678"; // first 8 bytes of the plugin provided data (scramble) // and the filler - int16_t capability_flags_1 = 0xF7DF; // The lower 2 bytes of the Capabilities Flags - int8_t character_set = 83; - int16_t status_flags = 0; - int16_t capability_flags_2 = 0x0000; - int8_t auth_plugin_data_len = 0; - char reserved[10] = {0}; - char auth_plugin_data_part_2[13] = "bbbbbbbbbbbb"; +struct HandshakeV10 : public BasePacket { + int8_t protocol = 10; + char server_version[7] = "5.7.25"; + int32_t thread_id = 21501807; // conn id + char auth_plugin_data_part_1[9] = + "12345678"; // first 8 bytes of the plugin provided data (scramble) // and the filler + int16_t capability_flags_1 = 0xF7DF; // The lower 2 bytes of the Capabilities Flags + int8_t character_set = 83; + int16_t status_flags = 0; + int16_t capability_flags_2 = 0x0000; + int8_t auth_plugin_data_len = 0; + char reserved[10] = {0}; + char auth_plugin_data_part_2[13] = "bbbbbbbbbbbb"; HandshakeV10(int8_t sequence = 0) : BasePacket(sequence) {} @@ -256,19 +254,18 @@ struct HandshakeV10 : public BasePacket store_int3(buf, payload_length); net_packet.resize(pos); LOG_TRACE("encode handshake packet with payload length=%d", payload_length); - + return RC::SUCCESS; } }; -struct OkPacket : public BasePacket -{ - int8_t header = 0; // 0x00 for ok and 0xFE for EOF - int32_t affected_rows = 0; - int32_t last_insert_id = 0; - int16_t status_flags = 0x22; - int16_t warnings = 0; - std::string info; // human readable status information +struct OkPacket : public BasePacket { + int8_t header = 0; // 0x00 for ok and 0xFE for EOF + int32_t affected_rows = 0; + int32_t last_insert_id = 0; + int16_t status_flags = 0x22; + int16_t warnings = 0; + std::string info; // human readable status information OkPacket(int8_t sequence = 0) : BasePacket(sequence) {} @@ -310,11 +307,10 @@ struct OkPacket : public BasePacket } }; -struct EofPacket : public BasePacket -{ - int8_t header = 0xFE; - int16_t warnings = 0; - int16_t status_flags = 0x22; +struct EofPacket : public BasePacket { + int8_t header = 0xFE; + int16_t warnings = 0; + int16_t status_flags = 0x22; EofPacket(int8_t sequence = 0) : BasePacket(sequence) {} @@ -350,13 +346,12 @@ struct EofPacket : public BasePacket } }; -struct ErrPacket : public BasePacket -{ - int8_t header = 0xFF; - int16_t error_code = 0; - char sql_state_marker[1] = {'#'}; - std::string sql_state{"HY000"}; - std::string error_message; +struct ErrPacket : public BasePacket { + int8_t header = 0xFF; + int16_t error_code = 0; + char sql_state_marker[1] = {'#'}; + std::string sql_state{"HY000"}; + std::string error_message; ErrPacket(int8_t sequence = 0) : BasePacket(sequence) {} @@ -395,11 +390,10 @@ struct ErrPacket : public BasePacket // https://dev.mysql.com/doc/dev/mysql-server/latest/page_protocol_command_phase.html // https://mariadb.com/kb/en/2-text-protocol/ -struct QueryPacket -{ +struct QueryPacket { PacketHeader packet_header; - int8_t command; // 0x03: COM_QUERY - std::string query; // the text of the SQL query to execute + int8_t command; // 0x03: COM_QUERY + std::string query; // the text of the SQL query to execute }; /** @@ -474,26 +468,31 @@ RC MysqlCommunicator::read_event(SessionEvent *&event) PacketHeader packet_header; int ret = common::readn(fd_, &packet_header, sizeof(packet_header)); if (ret != 0) { - LOG_WARN("failed to read packet header. length=%d, addr=%s. error=%s", sizeof(packet_header), addr_.c_str(), strerror(errno)); + LOG_WARN("failed to read packet header. length=%d, addr=%s. error=%s", + sizeof(packet_header), + addr_.c_str(), + strerror(errno)); return RC::IOERR; } LOG_TRACE("read packet header. length=%d, sequence_id=%d", sizeof(packet_header), packet_header.sequence_id); sequence_id_ = packet_header.sequence_id + 1; - + std::vector buf(packet_header.payload_length); ret = common::readn(fd_, buf.data(), packet_header.payload_length); if (ret != 0) { LOG_WARN("failed to read packet payload. length=%d, addr=%s, error=%s", - packet_header.payload_length, addr_.c_str(), strerror(errno)); + packet_header.payload_length, + addr_.c_str(), + strerror(errno)); return RC::IOERR; } LOG_TRACE("read packet payload length=%d", packet_header.payload_length); - + event = nullptr; if (!authed_) { - uint32_t client_flag = *(uint32_t*)buf.data(); // TODO should use decode (little endian as default) + uint32_t client_flag = *(uint32_t *)buf.data(); // TODO should use decode (little endian as default) LOG_INFO("client handshake response with capabilities flag=%d", client_flag); client_capabilities_flag_ = client_flag; // send ok packet and return @@ -510,7 +509,7 @@ RC MysqlCommunicator::read_event(SessionEvent *&event) int8_t command_type = buf[0]; LOG_TRACE("recv command from client =%d", command_type); - + if (command_type == 0x03) { // COM_QUERY QueryPacket query_packet; rc = decode_query_packet(buf, query_packet); @@ -541,7 +540,7 @@ RC MysqlCommunicator::read_event(SessionEvent *&event) RC MysqlCommunicator::write_state(SessionEvent *event, bool &need_disconnect) { SqlResult *sql_result = event->sql_result(); - + const int buf_size = 2048; char *buf = new char[buf_size]; const std::string &state_string = sql_result->state_string(); @@ -580,14 +579,14 @@ RC MysqlCommunicator::write_state(SessionEvent *event, bool &need_disconnect) RC MysqlCommunicator::write_result(SessionEvent *event, bool &need_disconnect) { RC rc = RC::SUCCESS; - + need_disconnect = true; SqlResult *sql_result = event->sql_result(); if (nullptr == sql_result) { const char *response = event->get_response(); int len = event->get_response_len(); - OkPacket ok_packet;// TODO if error occurs, we should send an error packet to client + OkPacket ok_packet; // TODO if error occurs, we should send an error packet to client ok_packet.info.assign(response, len); rc = send_packet(ok_packet); if (rc != RC::SUCCESS) { @@ -598,7 +597,7 @@ RC MysqlCommunicator::write_result(SessionEvent *event, bool &need_disconnect) need_disconnect = false; } else { if (RC::SUCCESS != sql_result->return_code() || !sql_result->has_operator()) { - return write_state(event, need_disconnect); + return write_state(event, need_disconnect); } // send result set @@ -624,7 +623,7 @@ RC MysqlCommunicator::write_result(SessionEvent *event, bool &need_disconnect) rc = send_result_rows(sql_result, cell_num == 0, need_disconnect); } - + return rc; } @@ -689,7 +688,7 @@ RC MysqlCommunicator::send_column_definition(SqlResult *sql_result, bool &need_d net_packet.resize(pos); int ret = common::writen(fd_, net_packet.data(), net_packet.size()); - if (ret != 0){ + if (ret != 0) { LOG_WARN("failed to send column count to client. addr=%s, error=%s", addr(), strerror(errno)); need_disconnect = true; return RC::IOERR; @@ -705,12 +704,12 @@ RC MysqlCommunicator::send_column_definition(SqlResult *sql_result, bool &need_d pos += 1; const TupleCellSpec &spec = tuple_schema.cell_at(i); - const char *catalog = "def"; // The catalog used. Currently always "def" - const char *schema = "sys"; // schema name - const char *table = spec.table_name(); + const char *catalog = "def"; // The catalog used. Currently always "def" + const char *schema = "sys"; // schema name + const char *table = spec.table_name(); const char *org_table = spec.table_name(); const char *name = spec.alias(); - //const char *org_name = spec.field_name(); + // const char *org_name = spec.field_name(); const char *org_name = spec.alias(); int fixed_len_fields = 0x0c; int character_set = 33; @@ -736,7 +735,7 @@ RC MysqlCommunicator::send_column_definition(SqlResult *sql_result, bool &need_d pos += 2; store_int1(buf + pos, decimals); pos += 1; - store_int2(buf + pos, 0); // 按照mariadb的文档描述,最后还有一个unused字段int<2>,不过mysql的文档没有给出这样的描述 + store_int2(buf + pos, 0); // 按照mariadb的文档描述,最后还有一个unused字段int<2>,不过mysql的文档没有给出这样的描述 pos += 2; payload_length = pos - 4; @@ -779,7 +778,7 @@ RC MysqlCommunicator::send_result_rows(SqlResult *sql_result, bool no_column_def { RC rc = RC::SUCCESS; std::vector packet; - packet.resize(4 * 1024 * 1024); // TODO warning: length cannot be fix + packet.resize(4 * 1024 * 1024); // TODO warning: length cannot be fix int affected_rows = 0; Tuple *tuple = nullptr; @@ -787,7 +786,7 @@ RC MysqlCommunicator::send_result_rows(SqlResult *sql_result, bool no_column_def assert(tuple != nullptr); affected_rows++; - + const int cell_num = tuple->cell_num(); if (cell_num == 0) { continue; @@ -807,12 +806,12 @@ RC MysqlCommunicator::send_result_rows(SqlResult *sql_result, bool no_column_def rc = tuple->cell_at(i, tuple_cell); if (rc != RC::SUCCESS) { sql_result->set_return_code(rc); - break; // TODO send error packet + break; // TODO send error packet } std::stringstream ss; tuple_cell.to_string(ss); - pos += store_lenenc_string(buf + pos, ss.str().c_str()); + pos += store_lenenc_string(buf + pos, ss.str().c_str()); } int payload_length = pos - 4; diff --git a/src/observer/net/mysql_communicator.h b/src/observer/net/mysql_communicator.h index bb8b238d98a11551916700fd1a1ed98f97b6473f..a450d9839a4a1154cda3c2176bc5356d9102772a 100644 --- a/src/observer/net/mysql_communicator.h +++ b/src/observer/net/mysql_communicator.h @@ -27,7 +27,6 @@ class BasePacket; */ class MysqlCommunicator : public Communicator { public: - /** * 连接刚开始建立时,进行一些初始化 * 参考MySQL或MariaDB的手册,服务端要首先向客户端发送一个握手包,等客户端回复后, diff --git a/src/observer/net/server.cpp b/src/observer/net/server.cpp index 009587fc426c491ef99bd13164605b63af3b3594..c697fa1bcbf4f2c890e56d33113b5e0f76fc3f79 100644 --- a/src/observer/net/server.cpp +++ b/src/observer/net/server.cpp @@ -202,8 +202,8 @@ void Server::accept(int fd, short ev, void *arg) ret = event_base_set(instance->event_base_, &communicator->read_event()); if (ret < 0) { - LOG_ERROR("Failed to do event_base_set for read event of %s into libevent, %s", - communicator->addr(), strerror(errno)); + LOG_ERROR( + "Failed to do event_base_set for read event of %s into libevent, %s", communicator->addr(), strerror(errno)); delete communicator; return; } diff --git a/src/observer/session/session_stage.cpp b/src/observer/session/session_stage.cpp index e4b284a1b4526831ffbfdf2a782818cd134a9aa0..66be3870ff4048447487088b3c472adb921cbd07 100644 --- a/src/observer/session/session_stage.cpp +++ b/src/observer/session/session_stage.cpp @@ -124,7 +124,7 @@ void SessionStage::callback_event(StageEvent *event, CallbackContext *context) RC rc = communicator->write_result(sev, need_disconnect); LOG_INFO("write result return %s", strrc(rc)); if (need_disconnect) { - Server::close_connection(communicator); + Server::close_connection(communicator); } LOG_TRACE("Exit\n"); diff --git a/src/observer/sql/executor/execute_stage.h b/src/observer/sql/executor/execute_stage.h index 4ec32dad6e5417cfd1421eba6eb21b0cb8e0abbc..7732b46353fa3b6464507712d134a12cdfb349d6 100644 --- a/src/observer/sql/executor/execute_stage.h +++ b/src/observer/sql/executor/execute_stage.h @@ -39,7 +39,7 @@ protected: RC handle_request(common::StageEvent *event); RC handle_request_with_physical_operator(SQLStageEvent *sql_event); - + RC do_help(SQLStageEvent *session_event); RC do_create_table(SQLStageEvent *sql_event); RC do_create_index(SQLStageEvent *sql_event); diff --git a/src/observer/sql/executor/sql_result.h b/src/observer/sql/executor/sql_result.h index 08b394ce7207e1f4818d35688f00fcf67c36d88d..1c368799702682920a6777dfb590a284fa142f74 100644 --- a/src/observer/sql/executor/sql_result.h +++ b/src/observer/sql/executor/sql_result.h @@ -24,18 +24,38 @@ class SqlResult { public: SqlResult() = default; ~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_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_operator(std::unique_ptr oper) { operator_ = std::move(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_; } + void set_operator(std::unique_ptr oper) + { + operator_ = std::move(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_; + } RC open(); RC close(); diff --git a/src/observer/sql/expr/expression.cpp b/src/observer/sql/expr/expression.cpp index 510aff3afcda681e4334cdece77fe966256d9f2f..de4fad742e6113ac752a75d84bcf13c2014ade84 100644 --- a/src/observer/sql/expr/expression.cpp +++ b/src/observer/sql/expr/expression.cpp @@ -15,13 +15,12 @@ See the Mulan PSL v2 for more details. */ #include "sql/expr/expression.h" #include "sql/expr/tuple.h" - RC FieldExpr::get_value(const Tuple &tuple, TupleCell &cell) const { return tuple.find_cell(TupleCellSpec(table_name(), field_name()), cell); } -RC ValueExpr::get_value(const Tuple &tuple, TupleCell & cell) const +RC ValueExpr::get_value(const Tuple &tuple, TupleCell &cell) const { cell = tuple_cell_; return RC::SUCCESS; @@ -33,8 +32,7 @@ CastExpr::CastExpr(std::unique_ptr child, AttrType cast_type) {} CastExpr::~CastExpr() -{ -} +{} RC CastExpr::get_value(const Tuple &tuple, TupleCell &cell) const { @@ -67,8 +65,7 @@ ComparisonExpr::ComparisonExpr(CompOp comp, std::unique_ptr left, st {} ComparisonExpr::~ComparisonExpr() -{ -} +{} RC ComparisonExpr::compare_tuple_cell(const TupleCell &left, const TupleCell &right, bool &value) const { @@ -99,7 +96,7 @@ RC ComparisonExpr::compare_tuple_cell(const TupleCell &left, const TupleCell &ri rc = RC::GENERIC_ERROR; } break; } - + return rc; } @@ -128,7 +125,7 @@ RC ComparisonExpr::get_value(const Tuple &tuple, TupleCell &cell) const { TupleCell left_cell; TupleCell right_cell; - + RC rc = left_->get_value(tuple, left_cell); if (rc != RC::SUCCESS) { LOG_WARN("failed to get value of left expression. rc=%s", strrc(rc)); @@ -151,8 +148,7 @@ RC ComparisonExpr::get_value(const Tuple &tuple, TupleCell &cell) const //////////////////////////////////////////////////////////////////////////////// ConjunctionExpr::ConjunctionExpr(Type type, std::vector> &children) : conjunction_type_(type), children_(std::move(children)) -{ -} +{} RC ConjunctionExpr::get_value(const Tuple &tuple, TupleCell &cell) const { @@ -161,7 +157,7 @@ RC ConjunctionExpr::get_value(const Tuple &tuple, TupleCell &cell) const cell.set_boolean(true); return rc; } - + TupleCell tmp_cell; for (const std::unique_ptr &expr : children_) { rc = expr->get_value(tuple, tmp_cell); @@ -170,8 +166,7 @@ RC ConjunctionExpr::get_value(const Tuple &tuple, TupleCell &cell) const return rc; } bool value = tmp_cell.get_boolean(); - if ((conjunction_type_ == Type::AND && !value) - || (conjunction_type_ == Type::OR && value)) { + if ((conjunction_type_ == Type::AND && !value) || (conjunction_type_ == Type::OR && value)) { cell.set_boolean(value); return rc; } diff --git a/src/observer/sql/expr/expression.h b/src/observer/sql/expr/expression.h index 59f8322578dd636806f064479634af4b06ed8820..f4c0248b8dcb3723be76e6543cb26e22b9051112 100644 --- a/src/observer/sql/expr/expression.h +++ b/src/observer/sql/expr/expression.h @@ -41,9 +41,8 @@ enum class ExprType { * 才能计算出来真实的值。但是有些表达式可能就表示某一个固定的 * 值,比如ValueExpr。 */ -class Expression -{ -public: +class Expression { +public: Expression() = default; virtual ~Expression() = default; @@ -61,11 +60,10 @@ public: /** * 表达式值的类型 */ - virtual AttrType value_type() const = 0; + virtual AttrType value_type() const = 0; }; -class FieldExpr : public Expression -{ +class FieldExpr : public Expression { public: FieldExpr() = default; FieldExpr(const Table *table, const FieldMeta *field) : field_(table, field) @@ -105,12 +103,12 @@ public: } RC get_value(const Tuple &tuple, TupleCell &cell) const override; + private: Field field_; }; -class ValueExpr : public Expression -{ +class ValueExpr : public Expression { public: ValueExpr() = default; ValueExpr(const Value &value) @@ -138,7 +136,7 @@ public: virtual ~ValueExpr() = default; - RC get_value(const Tuple &tuple, TupleCell & cell) const override; + RC get_value(const Tuple &tuple, TupleCell &cell) const override; ExprType type() const override { @@ -150,11 +148,13 @@ public: return tuple_cell_.attr_type(); } - void get_tuple_cell(TupleCell &cell) const { + void get_tuple_cell(TupleCell &cell) const + { cell = tuple_cell_; } - const TupleCell &get_tuple_cell() const { + const TupleCell &get_tuple_cell() const + { return tuple_cell_; } @@ -162,38 +162,58 @@ private: TupleCell tuple_cell_; }; -class CastExpr : public Expression -{ -public: +class CastExpr : public Expression { +public: CastExpr(std::unique_ptr child, AttrType cast_type); virtual ~CastExpr(); - ExprType type() const override { return ExprType::CAST; } + ExprType type() const override + { + return ExprType::CAST; + } RC get_value(const Tuple &tuple, TupleCell &cell) const override; AttrType value_type() const override { return cast_type_; } - std::unique_ptr &child() { return child_; } + std::unique_ptr &child() + { + return child_; + } + private: std::unique_ptr child_; AttrType cast_type_; }; -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; } + + ExprType type() const override + { + return ExprType::COMPARISON; + } RC get_value(const Tuple &tuple, TupleCell &cell) const override; - AttrType value_type() const override { return BOOLEANS; } + AttrType value_type() const override + { + return BOOLEANS; + } - CompOp comp() const { return comp_; } - std::unique_ptr &left() { return left_; } - std::unique_ptr &right() { return right_; } + CompOp comp() const + { + return comp_; + } + std::unique_ptr &left() + { + return left_; + } + std::unique_ptr &right() + { + return right_; + } /** * 尝试在没有tuple的情况下获取当前表达式的值 @@ -217,27 +237,38 @@ private: * 多个表达式使用同一种关系(AND或OR)来联结 * 当前miniob仅有AND操作 */ -class ConjunctionExpr : public Expression -{ +class ConjunctionExpr : public Expression { public: - enum class Type - { + enum class Type { AND, OR, }; - + public: ConjunctionExpr(Type type, std::vector> &children); virtual ~ConjunctionExpr() = default; - ExprType type() const override { return ExprType::CONJUNCTION; } - AttrType value_type() const override { return BOOLEANS; } + ExprType type() const override + { + return ExprType::CONJUNCTION; + } + AttrType value_type() const override + { + return BOOLEANS; + } RC get_value(const Tuple &tuple, TupleCell &cell) const override; - Type conjunction_type() const { return conjunction_type_; } + Type conjunction_type() const + { + return conjunction_type_; + } + + std::vector> &children() + { + return children_; + } - std::vector> &children() { return children_; } private: - Type conjunction_type_; + Type conjunction_type_; std::vector> children_; }; diff --git a/src/observer/sql/expr/tuple.h b/src/observer/sql/expr/tuple.h index 9f82546db545e545febb54567b170aa73597b310..6038ce44f0fcec41011bcbeaf8e8ee61d12acff6 100644 --- a/src/observer/sql/expr/tuple.h +++ b/src/observer/sql/expr/tuple.h @@ -25,32 +25,44 @@ See the Mulan PSL v2 for more details. */ class Table; -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]; } +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]; + } private: std::vector cells_; }; -class Tuple -{ +class Tuple { public: Tuple() = default; virtual ~Tuple() = default; - virtual int cell_num() const = 0; - virtual RC cell_at(int index, TupleCell &cell) const = 0; - virtual RC find_cell(const TupleCellSpec &spec, TupleCell &cell) const = 0; + virtual int cell_num() const = 0; + virtual RC cell_at(int index, TupleCell &cell) const = 0; + virtual RC find_cell(const TupleCellSpec &spec, TupleCell &cell) const = 0; }; -class RowTuple : public Tuple -{ +class RowTuple : public Tuple { public: RowTuple() = default; virtual ~RowTuple() @@ -60,7 +72,7 @@ public: } speces_.clear(); } - + void set_record(Record *record) { this->record_ = record; @@ -103,16 +115,16 @@ public: } for (size_t i = 0; i < speces_.size(); ++i) { - const FieldExpr * field_expr = speces_[i]; + const FieldExpr *field_expr = speces_[i]; const Field &field = field_expr->field(); if (0 == strcmp(field_name, field.field_name())) { - return cell_at(i, cell); + return cell_at(i, cell); } } return RC::NOTFOUND; } - #if 0 +#if 0 RC cell_spec_at(int index, const TupleCellSpec *&spec) const override { if (index < 0 || index >= static_cast(speces_.size())) { @@ -122,7 +134,7 @@ public: spec = speces_[index]; return RC::SUCCESS; } - #endif +#endif Record &record() { @@ -133,14 +145,14 @@ public: { return *record_; } + private: Record *record_ = nullptr; const Table *table_ = nullptr; std::vector speces_; }; -class ProjectTuple : public Tuple -{ +class ProjectTuple : public Tuple { public: ProjectTuple() = default; virtual ~ProjectTuple() @@ -183,7 +195,7 @@ public: return tuple_->find_cell(spec, cell); } - #if 0 +#if 0 RC cell_spec_at(int index, const TupleCellSpec *&spec) const override { if (index < 0 || index >= static_cast(speces_.size())) { @@ -192,15 +204,14 @@ public: spec = speces_[index]; return RC::SUCCESS; } - #endif +#endif private: std::vector speces_; Tuple *tuple_ = nullptr; }; -class ValueListTuple : public Tuple -{ -public: +class ValueListTuple : public Tuple { +public: ValueListTuple() = default; virtual ~ValueListTuple() = default; @@ -214,7 +225,7 @@ public: return static_cast(cells_.size()); } - virtual RC cell_at(int index, TupleCell &cell) const override + virtual RC cell_at(int index, TupleCell &cell) const override { if (index < 0 || index >= cell_num()) { return RC::NOTFOUND; @@ -224,7 +235,7 @@ public: return RC::SUCCESS; } - virtual RC find_cell(const TupleCellSpec &spec, TupleCell &cell) const override + virtual RC find_cell(const TupleCellSpec &spec, TupleCell &cell) const override { return RC::INTERNAL; } @@ -237,15 +248,20 @@ private: * 将两个tuple合并为一个tuple * 在join算子中使用 */ -class JoinedTuple : public Tuple -{ +class JoinedTuple : public Tuple { public: 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(); @@ -254,7 +270,7 @@ public: RC cell_at(int index, TupleCell &cell) const override { const int left_cell_num = left_->cell_num(); - if (index >0 && index < left_cell_num) { + if (index > 0 && index < left_cell_num) { return left_->cell_at(index, cell); } @@ -274,8 +290,8 @@ public: return right_->find_cell(spec, cell); } - + private: - Tuple * left_ = nullptr; - Tuple * right_ = 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 3f55ebf0de0b843f3fa3e0e35ca59bd887eb55af..bfd5484e8af6fd8beb04646fe19e8af891bf635b 100644 --- a/src/observer/sql/expr/tuple_cell.cpp +++ b/src/observer/sql/expr/tuple_cell.cpp @@ -136,21 +136,21 @@ const char *TupleCell::data() const void TupleCell::to_string(std::ostream &os) const { switch (attr_type_) { - case INTS: { - os << num_value_.int_value_; - } break; - case FLOATS: { - os << double2string(num_value_.float_value_); - } break; - case BOOLEANS: { - os << num_value_.bool_value_; - } break; - case CHARS: { - os << str_value_; - } break; - default: { - LOG_WARN("unsupported attr type: %d", attr_type_); - } break; + case INTS: { + os << num_value_.int_value_; + } break; + case FLOATS: { + os << double2string(num_value_.float_value_); + } break; + case BOOLEANS: { + os << num_value_.bool_value_; + } break; + case CHARS: { + os << str_value_; + } break; + default: { + LOG_WARN("unsupported attr type: %d", attr_type_); + } break; } } @@ -159,14 +159,16 @@ int TupleCell::compare(const TupleCell &other) const if (this->attr_type_ == other.attr_type_) { switch (this->attr_type_) { case INTS: { - return compare_int((void *)&this->num_value_.int_value_, (void *)&other.num_value_.int_value_); + return compare_int((void *)&this->num_value_.int_value_, (void *)&other.num_value_.int_value_); } break; case FLOATS: { - return compare_float((void *)&this->num_value_.float_value_, (void *)&other.num_value_.float_value_); + return compare_float((void *)&this->num_value_.float_value_, (void *)&other.num_value_.float_value_); } break; case CHARS: { - return compare_string((void *)this->str_value_.c_str(), this->str_value_.length(), - (void *)other.str_value_.c_str(), other.str_value_.length()); + return compare_string((void *)this->str_value_.c_str(), + this->str_value_.length(), + (void *)other.str_value_.c_str(), + other.str_value_.length()); } break; case BOOLEANS: { return compare_int((void *)&this->num_value_.bool_value_, (void *)&other.num_value_.bool_value_); @@ -183,10 +185,9 @@ int TupleCell::compare(const TupleCell &other) const return compare_float((void *)&this->num_value_.float_value_, (void *)&other_data); } LOG_WARN("not supported"); - return -1; // TODO return rc? + return -1; // TODO return rc? } - int TupleCell::get_int() const { switch (attr_type_) { diff --git a/src/observer/sql/expr/tuple_cell.h b/src/observer/sql/expr/tuple_cell.h index b41abcd45194c8ea4efb3752772206a9683b54d5..140a9a11514cab4037ec327101dddc55d7eb1f63 100644 --- a/src/observer/sql/expr/tuple_cell.h +++ b/src/observer/sql/expr/tuple_cell.h @@ -18,15 +18,23 @@ See the Mulan PSL v2 for more details. */ #include "storage/common/table.h" #include "storage/common/field_meta.h" -class TupleCellSpec -{ -public: +class TupleCellSpec { +public: 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_; @@ -38,16 +46,13 @@ private: * 表示tuple中某个元素的值 * @note 可以与value做合并 */ -class TupleCell -{ -public: +class TupleCell { +public: TupleCell() = default; - - TupleCell(FieldMeta *meta, char *data, int length = 4) - : TupleCell(meta->type(), data) + + TupleCell(FieldMeta *meta, char *data, int length = 4) : TupleCell(meta->type(), data) {} - TupleCell(AttrType attr_type, char *data, int length = 4) - : attr_type_(attr_type) + TupleCell(AttrType attr_type, char *data, int length = 4) : attr_type_(attr_type) { this->set_data(data, length); } @@ -55,9 +60,15 @@ public: TupleCell(const TupleCell &other) = default; TupleCell &operator=(const TupleCell &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); @@ -69,7 +80,10 @@ public: int compare(const TupleCell &other) const; const char *data() const; - int length() const { return length_; } + int length() const + { + return length_; + } AttrType attr_type() const { @@ -85,15 +99,15 @@ public: float get_float() const; std::string get_string() const; bool get_boolean() const; - + private: AttrType attr_type_ = UNDEFINED; int length_; union { - int int_value_; + int int_value_; float float_value_; - bool bool_value_; + bool bool_value_; } num_value_; std::string str_value_; }; diff --git a/src/observer/sql/operator/delete_logical_operator.cpp b/src/observer/sql/operator/delete_logical_operator.cpp index ac0ae02b38e47c44c46351ac2d072864558cd375..a0705643c5e58a1958ef8bb707e6824a66d3d5cf 100644 --- a/src/observer/sql/operator/delete_logical_operator.cpp +++ b/src/observer/sql/operator/delete_logical_operator.cpp @@ -14,7 +14,5 @@ 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 26662f7e6a7586b40baebfeaad5db46c69d5bf54..f055b0dced3e37ec7e2bcbdab73c871b4e6c3229 100644 --- a/src/observer/sql/operator/delete_logical_operator.h +++ b/src/observer/sql/operator/delete_logical_operator.h @@ -16,15 +16,20 @@ See the Mulan PSL v2 for more details. */ #include "sql/operator/logical_operator.h" -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 23a7dbd3432ee9b8a0ef7a1e85e4dc14e3636bcb..22fcfdc4d855c363bb1353cf1397642903df9b18 100644 --- a/src/observer/sql/operator/delete_physical_operator.cpp +++ b/src/observer/sql/operator/delete_physical_operator.cpp @@ -41,7 +41,7 @@ RC DeletePhysicalOperator::next() if (children_.empty()) { return RC::RECORD_EOF; } - + PhysicalOperator *child = children_[0].get(); while (RC::SUCCESS == (rc = child->next())) { Tuple *tuple = child->current_tuple(); diff --git a/src/observer/sql/operator/delete_physical_operator.h b/src/observer/sql/operator/delete_physical_operator.h index e7daa4e61b65f27edf6db2ceb5fff3816e939328..0b262bbc48d90c56f5f1b9b1b0bcb60ca59cfa5d 100644 --- a/src/observer/sql/operator/delete_physical_operator.h +++ b/src/observer/sql/operator/delete_physical_operator.h @@ -20,24 +20,27 @@ See the Mulan PSL v2 for more details. */ class Trx; class DeleteStmt; -class DeletePhysicalOperator : public PhysicalOperator -{ +class DeletePhysicalOperator : public PhysicalOperator { public: - DeletePhysicalOperator(Table *table, Trx *trx) - : table_(table), trx_(trx) + DeletePhysicalOperator(Table *table, Trx *trx) : table_(table), trx_(trx) {} virtual ~DeletePhysicalOperator() = default; - PhysicalOperatorType type() const override { return PhysicalOperatorType::DELETE; } - + PhysicalOperatorType type() const override + { + return PhysicalOperatorType::DELETE; + } + RC open() override; RC next() override; RC close() override; - Tuple * current_tuple() override { + Tuple *current_tuple() override + { return nullptr; } + private: Table *table_ = 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 e48593f6434bd8f173fb1bc1789ce64d0938c430..070633e74e2767b6435cceebae793eeb9fa2b428 100644 --- a/src/observer/sql/operator/explain_logical_operator.h +++ b/src/observer/sql/operator/explain_logical_operator.h @@ -16,13 +16,15 @@ See the Mulan PSL v2 for more details. */ #include "sql/operator/logical_operator.h" -class ExplainLogicalOperator : public LogicalOperator -{ +class ExplainLogicalOperator : public LogicalOperator { public: ExplainLogicalOperator() = default; virtual ~ExplainLogicalOperator() = default; - LogicalOperatorType type() const override { return LogicalOperatorType::EXPLAIN; } - + LogicalOperatorType type() const override + { + return LogicalOperatorType::EXPLAIN; + } + private: }; diff --git a/src/observer/sql/operator/explain_physical_operator.cpp b/src/observer/sql/operator/explain_physical_operator.cpp index e6e1a886f747301c420af625c66e3f090a24d24b..67bef1045e6dfe3a563398259cc418a43d9da49d 100644 --- a/src/observer/sql/operator/explain_physical_operator.cpp +++ b/src/observer/sql/operator/explain_physical_operator.cpp @@ -37,19 +37,19 @@ RC ExplainPhysicalOperator::next() if (!physical_plan_.empty()) { return RC::RECORD_EOF; } - + stringstream ss; ss << "OPERATOR(NAME)\n"; - + int level = 0; std::vector ends; ends.push_back(true); const auto children_size = static_cast(children_.size()); for (int i = 0; i < children_size - 1; i++) { - to_string(ss, children_[i].get(), level, false/*last_child*/, ends); + to_string(ss, children_[i].get(), level, false /*last_child*/, ends); } if (children_size > 0) { - to_string(ss, children_[children_size - 1].get(), level, true/*last_child*/, ends); + to_string(ss, children_[children_size - 1].get(), level, true /*last_child*/, ends); } physical_plan_ = ss.str(); @@ -75,9 +75,10 @@ Tuple *ExplainPhysicalOperator::current_tuple() * @param last_child 当前算子是否是当前兄弟节点中最后一个节点 * @param ends 表示当前某个层级上的算子,是否已经没有其它的节点,以判断使用什么打印符号 */ -void ExplainPhysicalOperator::to_string(std::ostream &os, PhysicalOperator *oper, int level, bool last_child, std::vector &ends) +void ExplainPhysicalOperator::to_string( + std::ostream &os, PhysicalOperator *oper, int level, bool last_child, std::vector &ends) { - for (int i = 0; i < level-1; i++) { + for (int i = 0; i < level - 1; i++) { if (ends[i]) { os << " "; } else { @@ -108,9 +109,9 @@ void ExplainPhysicalOperator::to_string(std::ostream &os, PhysicalOperator *oper std::vector> &children = oper->children(); 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); + to_string(os, children[i].get(), level + 1, false /*last_child*/, ends); } if (size > 0) { - to_string(os, children[size - 1].get(), level + 1, true/*last_child*/, ends); + to_string(os, children[size - 1].get(), level + 1, true /*last_child*/, ends); } } diff --git a/src/observer/sql/operator/explain_physical_operator.h b/src/observer/sql/operator/explain_physical_operator.h index d1f65ed307eec16cc3967075f734f96d35fb5ad5..72acc0bab4b2bb34842775c5cbbfa88e8d5c9dd5 100644 --- a/src/observer/sql/operator/explain_physical_operator.h +++ b/src/observer/sql/operator/explain_physical_operator.h @@ -16,14 +16,16 @@ See the Mulan PSL v2 for more details. */ #include "sql/operator/physical_operator.h" -class ExplainPhysicalOperator : public PhysicalOperator -{ +class ExplainPhysicalOperator : public PhysicalOperator { public: ExplainPhysicalOperator() = default; virtual ~ExplainPhysicalOperator() = default; - PhysicalOperatorType type() const override { return PhysicalOperatorType::EXPLAIN; } - + PhysicalOperatorType type() const override + { + return PhysicalOperatorType::EXPLAIN; + } + RC open() override; RC next() override; RC close() override; diff --git a/src/observer/sql/operator/index_scan_physical_operator.cpp b/src/observer/sql/operator/index_scan_physical_operator.cpp index 27c625e73bff33a7296dbedcd8bd59176cba1874..ab54d772cafebcc4c05d87058ef71558bade63a3 100644 --- a/src/observer/sql/operator/index_scan_physical_operator.cpp +++ b/src/observer/sql/operator/index_scan_physical_operator.cpp @@ -15,11 +15,9 @@ See the Mulan PSL v2 for more details. */ #include "sql/operator/index_scan_physical_operator.h" #include "storage/index/index.h" -IndexScanPhysicalOperator::IndexScanPhysicalOperator(const Table *table, Index *index, - const TupleCell *left_cell, bool left_inclusive, - const TupleCell *right_cell, bool right_inclusive) - : table_(table), index_(index), - left_inclusive_(left_inclusive), right_inclusive_(right_inclusive) +IndexScanPhysicalOperator::IndexScanPhysicalOperator(const Table *table, Index *index, const TupleCell *left_cell, + bool left_inclusive, const TupleCell *right_cell, bool right_inclusive) + : table_(table), index_(index), left_inclusive_(left_inclusive), right_inclusive_(right_inclusive) { if (left_cell) { left_cell_ = *left_cell; @@ -35,9 +33,12 @@ RC IndexScanPhysicalOperator::open() return RC::INTERNAL; } - - IndexScanner *index_scanner = index_->create_scanner(left_cell_.data(), left_cell_.length(), left_inclusive_, - right_cell_.data(), right_cell_.length(), right_inclusive_); + IndexScanner *index_scanner = index_->create_scanner(left_cell_.data(), + left_cell_.length(), + left_inclusive_, + right_cell_.data(), + right_cell_.length(), + right_inclusive_); if (nullptr == index_scanner) { LOG_WARN("failed to create index scanner"); return RC::INTERNAL; @@ -52,7 +53,7 @@ RC IndexScanPhysicalOperator::open() index_scanner_ = index_scanner; tuple_.set_schema(table_, table_->table_meta().field_metas()); - + return RC::SUCCESS; } @@ -89,7 +90,7 @@ RC IndexScanPhysicalOperator::close() return RC::SUCCESS; } -Tuple * IndexScanPhysicalOperator::current_tuple() +Tuple *IndexScanPhysicalOperator::current_tuple() { tuple_.set_record(¤t_record_); return &tuple_; diff --git a/src/observer/sql/operator/index_scan_physical_operator.h b/src/observer/sql/operator/index_scan_physical_operator.h index 6ef259aa5e2aea2c52132a69566ea303813e79bc..edcadcfa7af4d1a22971cc5ee99d90df34bc70f0 100644 --- a/src/observer/sql/operator/index_scan_physical_operator.h +++ b/src/observer/sql/operator/index_scan_physical_operator.h @@ -17,31 +17,32 @@ See the Mulan PSL v2 for more details. */ #include "sql/operator/physical_operator.h" #include "sql/expr/tuple.h" -class IndexScanPhysicalOperator : public PhysicalOperator -{ -public: - IndexScanPhysicalOperator(const Table *table, Index *index, - const TupleCell *left_cell, bool left_inclusive, - const TupleCell *right_cell, bool right_inclusive); +class IndexScanPhysicalOperator : public PhysicalOperator { +public: + IndexScanPhysicalOperator(const Table *table, Index *index, const TupleCell *left_cell, bool left_inclusive, + const TupleCell *right_cell, 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; - + RC open() override; RC next() override; RC close() override; - Tuple * current_tuple() override; + Tuple *current_tuple() override; void set_predicates(std::vector> &&exprs); private: // 与TableScanPhysicalOperator代码相同,可以优化 RC filter(RowTuple &tuple, bool &result); - + private: const Table *table_ = nullptr; Index *index_ = nullptr; diff --git a/src/observer/sql/operator/insert_physical_operator.cpp b/src/observer/sql/operator/insert_physical_operator.cpp index 09023846613bd87f297f746774bb2e9c3f350eef..c20d28bd02e1bd8ff4615a401ffcd8c4f7091bf9 100644 --- a/src/observer/sql/operator/insert_physical_operator.cpp +++ b/src/observer/sql/operator/insert_physical_operator.cpp @@ -22,7 +22,7 @@ RC InsertPhysicalOperator::open() Table *table = insert_stmt_->table(); const Value *values = insert_stmt_->values(); int value_amount = insert_stmt_->value_amount(); - return table->insert_record(nullptr, value_amount, values); // TODO trx + return table->insert_record(nullptr, value_amount, values); // TODO trx } RC InsertPhysicalOperator::next() diff --git a/src/observer/sql/operator/insert_physical_operator.h b/src/observer/sql/operator/insert_physical_operator.h index 57a3be85cdb190364aeb2fe6950adb0cff12b392..00ecb64db53e7c864331ac566d9f0d4967d4dba1 100644 --- a/src/observer/sql/operator/insert_physical_operator.h +++ b/src/observer/sql/operator/insert_physical_operator.h @@ -20,17 +20,18 @@ See the Mulan PSL v2 for more details. */ class InsertStmt; -class InsertPhysicalOperator : public PhysicalOperator -{ +class InsertPhysicalOperator : public PhysicalOperator { public: - InsertPhysicalOperator(InsertStmt *insert_stmt) - : insert_stmt_(insert_stmt) + InsertPhysicalOperator(InsertStmt *insert_stmt) : insert_stmt_(insert_stmt) {} virtual ~InsertPhysicalOperator() = default; - PhysicalOperatorType type() const override { return PhysicalOperatorType::INSERT; } - + PhysicalOperatorType type() const override + { + return PhysicalOperatorType::INSERT; + } + RC open() override; RC next() override; RC close() override; diff --git a/src/observer/sql/operator/join_logical_operator.h b/src/observer/sql/operator/join_logical_operator.h index 37812dda139946d4f80116fc146eb11e3669e275..9b68754f10e5f69677ddbb423bf8b624d1394af1 100644 --- a/src/observer/sql/operator/join_logical_operator.h +++ b/src/observer/sql/operator/join_logical_operator.h @@ -16,12 +16,15 @@ See the Mulan PSL v2 for more details. */ #include "sql/operator/logical_operator.h" -class JoinLogicalOperator : public LogicalOperator -{ +class JoinLogicalOperator : public LogicalOperator { public: 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 2718f60b14d0f30deef3f6f277eeebac78c50f1d..261781fd407730f1e0c05f1660917720b7ac4b2d 100644 --- a/src/observer/sql/operator/join_physical_operator.cpp +++ b/src/observer/sql/operator/join_physical_operator.cpp @@ -15,8 +15,7 @@ See the Mulan PSL v2 for more details. */ #include "sql/operator/join_physical_operator.h" NestedLoopJoinPhysicalOperator::NestedLoopJoinPhysicalOperator() -{ -} +{} RC NestedLoopJoinPhysicalOperator::open() { @@ -50,7 +49,7 @@ RC NestedLoopJoinPhysicalOperator::next() return rc; } } else { - return rc; // got one tuple from right + return rc; // got one tuple from right } } @@ -83,7 +82,7 @@ RC NestedLoopJoinPhysicalOperator::close() return rc; } -Tuple * NestedLoopJoinPhysicalOperator::current_tuple() +Tuple *NestedLoopJoinPhysicalOperator::current_tuple() { return &joined_tuple_; } diff --git a/src/observer/sql/operator/join_physical_operator.h b/src/observer/sql/operator/join_physical_operator.h index a59e963e127f529e787bfc16fd740b908f200d49..5ee5681557889a724c98342e66c53349fbd1e1d2 100644 --- a/src/observer/sql/operator/join_physical_operator.h +++ b/src/observer/sql/operator/join_physical_operator.h @@ -22,14 +22,16 @@ See the Mulan PSL v2 for more details. */ * 最简单的两表(称为左表、右表)join算子 * 依次遍历左表的每一行,然后关联右表的每一行 */ -class NestedLoopJoinPhysicalOperator : public PhysicalOperator -{ +class NestedLoopJoinPhysicalOperator : public PhysicalOperator { public: NestedLoopJoinPhysicalOperator(); virtual ~NestedLoopJoinPhysicalOperator() = default; - PhysicalOperatorType type() const override { return PhysicalOperatorType::NESTED_LOOP_JOIN; } - + PhysicalOperatorType type() const override + { + return PhysicalOperatorType::NESTED_LOOP_JOIN; + } + RC open() override; RC next() override; RC close() override; @@ -38,7 +40,7 @@ public: private: RC left_next(); //! 左表遍历下一条数据 RC right_next(); //! 右表遍历下一条数据,如果上一轮结束了就重新开始新的一轮 - + private: //! 左表右表的真实对象是在PhysicalOperator::children_中,这里是为了写的时候更简单 PhysicalOperator *left_ = nullptr; diff --git a/src/observer/sql/operator/logical_operator.cpp b/src/observer/sql/operator/logical_operator.cpp index 95d9d53e0cb8da82f965bb8d0cc0a5f8c14d3462..51d0b5154873d08f650fd3ff338d0605cd306282 100644 --- a/src/observer/sql/operator/logical_operator.cpp +++ b/src/observer/sql/operator/logical_operator.cpp @@ -15,8 +15,7 @@ See the Mulan PSL v2 for more details. */ #include "sql/operator/logical_operator.h" LogicalOperator::~LogicalOperator() -{ -} +{} void LogicalOperator::add_child(std::unique_ptr oper) { diff --git a/src/observer/sql/operator/logical_operator.h b/src/observer/sql/operator/logical_operator.h index 7f8537097fd56197e2e2ff870e4cd4b453653034..8564612ba3f13732df15d6270ce450e9b0e7cab7 100644 --- a/src/observer/sql/operator/logical_operator.h +++ b/src/observer/sql/operator/logical_operator.h @@ -19,8 +19,7 @@ See the Mulan PSL v2 for more details. */ #include "sql/expr/expression.h" -enum class LogicalOperatorType -{ +enum class LogicalOperatorType { TABLE_GET, PREDICATE, PROJECTION, @@ -33,18 +32,23 @@ enum class LogicalOperatorType * 逻辑算子描述当前执行计划要做什么 * 可以看OptimizeStage中相关的代码 */ -class LogicalOperator -{ +class LogicalOperator { public: LogicalOperator() = default; virtual ~LogicalOperator(); virtual LogicalOperatorType type() const = 0; - + void add_child(std::unique_ptr oper); - std::vector> & children() { return children_; } - std::vector> &expressions() { return expressions_; } - + std::vector> &children() + { + return children_; + } + std::vector> &expressions() + { + return expressions_; + } + protected: std::vector> children_; std::vector> expressions_; diff --git a/src/observer/sql/operator/physical_operator.cpp b/src/observer/sql/operator/physical_operator.cpp index 2505379be7890a64492cdfe7c451cf1891fdb477..eea616472104c99388d4328ff04482dab39185a4 100644 --- a/src/observer/sql/operator/physical_operator.cpp +++ b/src/observer/sql/operator/physical_operator.cpp @@ -37,8 +37,7 @@ std::string physical_operator_type_name(PhysicalOperatorType type) } PhysicalOperator::~PhysicalOperator() -{ -} +{} std::string PhysicalOperator::name() const { diff --git a/src/observer/sql/operator/physical_operator.h b/src/observer/sql/operator/physical_operator.h index 3e020deba4b9cb8894cfe224fe8158efb5997176..fbb739b84fae1baddae8439f98385baa00aee98c 100644 --- a/src/observer/sql/operator/physical_operator.h +++ b/src/observer/sql/operator/physical_operator.h @@ -24,8 +24,7 @@ See the Mulan PSL v2 for more details. */ class Record; class TupleCellSpec; -enum class PhysicalOperatorType -{ +enum class PhysicalOperatorType { TABLE_SCAN, INDEX_SCAN, NESTED_LOOP_JOIN, @@ -40,8 +39,7 @@ enum class PhysicalOperatorType /** * 与LogicalOperator对应,物理算子描述执行计划将如何执行 */ -class PhysicalOperator -{ +class PhysicalOperator { public: PhysicalOperator() {} @@ -55,18 +53,22 @@ public: virtual std::string param() const; virtual PhysicalOperatorType type() const = 0; - + virtual RC open() = 0; virtual RC next() = 0; virtual RC close() = 0; - virtual Tuple * current_tuple() = 0; + virtual Tuple *current_tuple() = 0; - void add_child(std::unique_ptr 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 e3641c322d24eddbd204ba9218cfb51fabc9298a..aafd3694f824ddebb61baf281c7ecbeaf6275e8e 100644 --- a/src/observer/sql/operator/predicate_logical_operator.h +++ b/src/observer/sql/operator/predicate_logical_operator.h @@ -17,11 +17,13 @@ See the Mulan PSL v2 for more details. */ #include "sql/operator/logical_operator.h" #include "sql/expr/expression.h" -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 0e984fd1846a5170e7f1384b7f64b5356b84e364..9cdb84825067c138dd3bbeb3d5d3fa6ee446f5a9 100644 --- a/src/observer/sql/operator/predicate_physical_operator.cpp +++ b/src/observer/sql/operator/predicate_physical_operator.cpp @@ -18,8 +18,7 @@ See the Mulan PSL v2 for more details. */ #include "sql/stmt/filter_stmt.h" #include "storage/common/field.h" -PredicatePhysicalOperator::PredicatePhysicalOperator(std::unique_ptr expr) - : expression_(std::move(expr)) +PredicatePhysicalOperator::PredicatePhysicalOperator(std::unique_ptr expr) : expression_(std::move(expr)) { ASSERT(expression_->value_type() == BOOLEANS, "predicate's expression should be BOOLEAN type"); } @@ -38,7 +37,7 @@ RC PredicatePhysicalOperator::next() { RC rc = RC::SUCCESS; PhysicalOperator *oper = children_.front().get(); - + while (RC::SUCCESS == (rc = oper->next())) { Tuple *tuple = oper->current_tuple(); if (nullptr == tuple) { @@ -66,8 +65,7 @@ RC PredicatePhysicalOperator::close() return RC::SUCCESS; } -Tuple * PredicatePhysicalOperator::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 62cc0036a435156e7677d7c6bc48e9fd7144568d..853962876bac407139334005971764d19260365e 100644 --- a/src/observer/sql/operator/predicate_physical_operator.h +++ b/src/observer/sql/operator/predicate_physical_operator.h @@ -20,21 +20,23 @@ See the Mulan PSL v2 for more details. */ class FilterStmt; -class PredicatePhysicalOperator : public PhysicalOperator -{ +class PredicatePhysicalOperator : public PhysicalOperator { public: PredicatePhysicalOperator(std::unique_ptr expr); virtual ~PredicatePhysicalOperator() = default; - PhysicalOperatorType type() const override { return PhysicalOperatorType::PREDICATE; } - + PhysicalOperatorType type() const override + { + return PhysicalOperatorType::PREDICATE; + } + RC open() override; RC next() override; RC close() override; - Tuple * current_tuple() override; - + Tuple *current_tuple() override; + private: std::unique_ptr expression_; }; diff --git a/src/observer/sql/operator/project_logical_operator.cpp b/src/observer/sql/operator/project_logical_operator.cpp index e633327f374b2333fb1c11b86c593f00b389b283..945883e00c7f4e7f63e5afa86c693a5459550df3 100644 --- a/src/observer/sql/operator/project_logical_operator.cpp +++ b/src/observer/sql/operator/project_logical_operator.cpp @@ -14,7 +14,5 @@ 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 6741df79d5a3b6bbacac391b8605c56a3c52a7d3..88dd3b50225017f6b038a03d29f6fe1f66a7558e 100644 --- a/src/observer/sql/operator/project_logical_operator.h +++ b/src/observer/sql/operator/project_logical_operator.h @@ -24,16 +24,21 @@ See the Mulan PSL v2 for more details. */ /** * project 表示投影运算 */ -class ProjectLogicalOperator : public LogicalOperator -{ +class ProjectLogicalOperator : public LogicalOperator { public: ProjectLogicalOperator(const std::vector &fields); virtual ~ProjectLogicalOperator() = default; - LogicalOperatorType type() const override { return LogicalOperatorType::PROJECTION; } + LogicalOperatorType type() const override + { + return LogicalOperatorType::PROJECTION; + } + + const std::vector &fields() const + { + return fields_; + } - const std::vector &fields() const { return fields_; } - private: //! 投影映射的字段名称 //! 并不是所有的select都会查看表字段,也可能是常量数字、字符串, diff --git a/src/observer/sql/operator/project_physical_operator.h b/src/observer/sql/operator/project_physical_operator.h index b567bae3cdfef0005cbf37673e836f02a6813427..301ea3eb2cd5fd750912685a0c15c41a079c5302 100644 --- a/src/observer/sql/operator/project_physical_operator.h +++ b/src/observer/sql/operator/project_physical_operator.h @@ -17,8 +17,7 @@ See the Mulan PSL v2 for more details. */ #include "sql/operator/physical_operator.h" #include "rc.h" -class ProjectPhysicalOperator : public PhysicalOperator -{ +class ProjectPhysicalOperator : public PhysicalOperator { public: ProjectPhysicalOperator() {} @@ -27,8 +26,11 @@ public: 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() override; RC next() override; RC close() override; @@ -38,7 +40,8 @@ public: return tuple_.cell_num(); } - Tuple * current_tuple() override; + Tuple *current_tuple() override; + private: ProjectTuple tuple_; }; diff --git a/src/observer/sql/operator/string_list_physical_operator.h b/src/observer/sql/operator/string_list_physical_operator.h index 90edf05cea8879f3e96f61ff5bb14d8208786fc2..19fba78dba5d97c72688a9ac8dec6483273d2d29 100644 --- a/src/observer/sql/operator/string_list_physical_operator.h +++ b/src/observer/sql/operator/string_list_physical_operator.h @@ -17,9 +17,8 @@ See the Mulan PSL v2 for more details. */ #include #include "sql/operator/physical_operator.h" -class StringListPhysicalOperator : public PhysicalOperator -{ -public: +class StringListPhysicalOperator : public PhysicalOperator { +public: StringListPhysicalOperator() {} @@ -37,18 +36,21 @@ public: } template - void append( const T &v) + void append(const T &v) { strings_.emplace_back(1, v); } - PhysicalOperatorType type() const override { return PhysicalOperatorType::STRING_LIST; } - + PhysicalOperatorType type() const override + { + return PhysicalOperatorType::STRING_LIST; + } + RC open() override { return RC::SUCCESS; } - + RC next() override { if (!started_) { @@ -56,16 +58,17 @@ public: iterator_ = strings_.begin(); } else if (iterator_ != strings_.end()) { ++iterator_; - } + } return iterator_ == strings_.end() ? RC::RECORD_EOF : RC::SUCCESS; } + virtual RC close() override { iterator_ = strings_.end(); return RC::SUCCESS; } - virtual Tuple * current_tuple() override + virtual Tuple *current_tuple() override { if (iterator_ == strings_.end()) { return nullptr; @@ -82,6 +85,7 @@ public: tuple_.set_cells(cells); return &tuple_; } + private: using StringList = std::vector; using StringListList = std::vector; diff --git a/src/observer/sql/operator/table_get_logical_operator.h b/src/observer/sql/operator/table_get_logical_operator.h index 9c51014b492c979e7910a62ea70e3547df7b96f3..cf8011c3051979fe45ffcbd236a44fda59c3700e 100644 --- a/src/observer/sql/operator/table_get_logical_operator.h +++ b/src/observer/sql/operator/table_get_logical_operator.h @@ -20,19 +20,27 @@ See the Mulan PSL v2 for more details. */ * 表示从表中获取数据的算子 * 比如使用全表扫描、通过索引获取数据等 */ -class TableGetLogicalOperator : public LogicalOperator -{ +class TableGetLogicalOperator : public LogicalOperator { public: TableGetLogicalOperator(Table *table, const std::vector &fields); virtual ~TableGetLogicalOperator() = default; - LogicalOperatorType type() const override { return LogicalOperatorType::TABLE_GET; } + LogicalOperatorType type() const override + { + return LogicalOperatorType::TABLE_GET; + } - Table *table() const { return table_; } + Table *table() const + { + return table_; + } void set_predicates(std::vector> &&exprs); - std::vector> &predicates() { return predicates_; } - + std::vector> &predicates() + { + return predicates_; + } + private: Table *table_ = nullptr; std::vector fields_; diff --git a/src/observer/sql/operator/table_scan_physical_operator.cpp b/src/observer/sql/operator/table_scan_physical_operator.cpp index de02dcb36acc75364167aa2951ee23e184a5ab07..e274d497dfc613edb9653fe6cf4e8089d9504f78 100644 --- a/src/observer/sql/operator/table_scan_physical_operator.cpp +++ b/src/observer/sql/operator/table_scan_physical_operator.cpp @@ -38,7 +38,7 @@ RC TableScanPhysicalOperator::next() if (rc != RC::SUCCESS) { return rc; } - + tuple_.set_record(¤t_record_); rc = filter(tuple_, filter_result); if (rc != RC::SUCCESS) { @@ -59,7 +59,7 @@ RC TableScanPhysicalOperator::close() return record_scanner_.close_scan(); } -Tuple * TableScanPhysicalOperator::current_tuple() +Tuple *TableScanPhysicalOperator::current_tuple() { tuple_.set_record(¤t_record_); return &tuple_; @@ -67,7 +67,7 @@ Tuple * TableScanPhysicalOperator::current_tuple() std::string TableScanPhysicalOperator::param() const { - return table_->name(); + return table_->name(); } void TableScanPhysicalOperator::set_predicates(std::vector> &&exprs) diff --git a/src/observer/sql/operator/table_scan_physical_operator.h b/src/observer/sql/operator/table_scan_physical_operator.h index ec66730ebc94a489bfcaa17febb18f8b5f97d657..489534f22946db4849b57603f7e20a1477cf5cf3 100644 --- a/src/observer/sql/operator/table_scan_physical_operator.h +++ b/src/observer/sql/operator/table_scan_physical_operator.h @@ -20,30 +20,31 @@ See the Mulan PSL v2 for more details. */ class Table; -class TableScanPhysicalOperator : public PhysicalOperator -{ +class TableScanPhysicalOperator : public PhysicalOperator { public: - TableScanPhysicalOperator(Table *table) - : table_(table) + TableScanPhysicalOperator(Table *table) : table_(table) {} 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() override; RC next() override; RC close() override; - Tuple * current_tuple() override; + Tuple *current_tuple() override; void set_predicates(std::vector> &&exprs); private: RC filter(RowTuple &tuple, bool &result); - + private: Table *table_ = nullptr; RecordFileScanner record_scanner_; diff --git a/src/observer/sql/optimizer/comparison_simplification_rule.h b/src/observer/sql/optimizer/comparison_simplification_rule.h index 2c9d2812d9a5746a13556fe2a867530849d4082a..06a628aa8f77426385e199a05f333f208807b441 100644 --- a/src/observer/sql/optimizer/comparison_simplification_rule.h +++ b/src/observer/sql/optimizer/comparison_simplification_rule.h @@ -19,12 +19,11 @@ See the Mulan PSL v2 for more details. */ class LogicalOperator; -class ComparisonSimplificationRule : public ExpressionRewriteRule -{ -public: +class ComparisonSimplificationRule : public ExpressionRewriteRule { +public: ComparisonSimplificationRule() = default; virtual ~ComparisonSimplificationRule() = default; - + RC rewrite(std::unique_ptr &expr, bool &change_made) override; private: diff --git a/src/observer/sql/optimizer/conjunction_simplification_rule.cpp b/src/observer/sql/optimizer/conjunction_simplification_rule.cpp index 76381843fe8d091b54da555e37b9867fbb2347a7..213cd8780daffb39c69f5ead8c731bca0edf2a09 100644 --- a/src/observer/sql/optimizer/conjunction_simplification_rule.cpp +++ b/src/observer/sql/optimizer/conjunction_simplification_rule.cpp @@ -37,7 +37,7 @@ RC ConjunctionSimplificationRule::rewrite(std::unique_ptr &expr, boo std::vector> &child_exprs = conjunction_expr->children(); // 先看看有没有能够直接去掉的表达式。比如AND时恒为true的表达式可以删除 // 或者是否可以直接计算出当前表达式的值。比如AND时,如果有一个表达式为false,那么整个表达式就是false - for (auto iter = child_exprs.begin(); iter != child_exprs.end(); ) { + for (auto iter = child_exprs.begin(); iter != child_exprs.end();) { bool constant_value = false; rc = try_to_get_bool_constant(*iter, constant_value); if (rc != RC::SUCCESS) { @@ -68,7 +68,6 @@ RC ConjunctionSimplificationRule::rewrite(std::unique_ptr &expr, boo child_exprs.erase(iter); } } - } if (child_exprs.size() == 1) { LOG_TRACE("conjunction expression has only 1 child"); diff --git a/src/observer/sql/optimizer/conjunction_simplification_rule.h b/src/observer/sql/optimizer/conjunction_simplification_rule.h index f5e89a7be713e47cf18ba0d3bf5a03db580a8760..29789235698931249ababdddd5f6130520f40be2 100644 --- a/src/observer/sql/optimizer/conjunction_simplification_rule.h +++ b/src/observer/sql/optimizer/conjunction_simplification_rule.h @@ -23,12 +23,12 @@ class LogicalOperator; * 简化多个表达式联结的运算 * 比如只有一个表达式,或者表达式可以直接出来 */ -class ConjunctionSimplificationRule : public ExpressionRewriteRule -{ +class ConjunctionSimplificationRule : public ExpressionRewriteRule { public: ConjunctionSimplificationRule() = default; virtual ~ConjunctionSimplificationRule() = default; RC rewrite(std::unique_ptr &expr, bool &change_made) override; + private: }; diff --git a/src/observer/sql/optimizer/expression_rewriter.cpp b/src/observer/sql/optimizer/expression_rewriter.cpp index 64470ece56669ddb8f70cc1775c429fa81e27647..9e481de0a561419590a51b0a9206a4b763333a4f 100644 --- a/src/observer/sql/optimizer/expression_rewriter.cpp +++ b/src/observer/sql/optimizer/expression_rewriter.cpp @@ -61,9 +61,9 @@ RC ExpressionRewriter::rewrite(std::unique_ptr &oper, bool &cha RC ExpressionRewriter::rewrite_expression(std::unique_ptr &expr, bool &change_made) { RC rc = RC::SUCCESS; - + change_made = false; - for (std::unique_ptr & rule: expr_rewrite_rules_) { + for (std::unique_ptr &rule : expr_rewrite_rules_) { bool sub_change_made = false; rc = rule->rewrite(expr, sub_change_made); if (sub_change_made && !change_made) { diff --git a/src/observer/sql/optimizer/expression_rewriter.h b/src/observer/sql/optimizer/expression_rewriter.h index 92b07cffbea9f21c823d9fe833c573b3d62e94ad..c11535e48c7ec2c335ec9ef5285c9e449479096d 100644 --- a/src/observer/sql/optimizer/expression_rewriter.h +++ b/src/observer/sql/optimizer/expression_rewriter.h @@ -21,9 +21,8 @@ See the Mulan PSL v2 for more details. */ #include "sql/expr/expression.h" #include "sql/optimizer/rewrite_rule.h" -class ExpressionRewriter : public RewriteRule -{ -public: +class ExpressionRewriter : public RewriteRule { +public: ExpressionRewriter(); virtual ~ExpressionRewriter() = default; @@ -31,7 +30,6 @@ public: private: RC rewrite_expression(std::unique_ptr &expr, bool &change_made); - private: std::vector> expr_rewrite_rules_; diff --git a/src/observer/sql/optimizer/optimize_stage.cpp b/src/observer/sql/optimizer/optimize_stage.cpp index 240f4ff4fa84ac13cccf2e0928f14eecdaefa36e..5cd0dc6495ebf499f0e4a291f20de0d62d27ff60 100644 --- a/src/observer/sql/optimizer/optimize_stage.cpp +++ b/src/observer/sql/optimizer/optimize_stage.cpp @@ -100,18 +100,17 @@ void OptimizeStage::cleanup() void OptimizeStage::handle_event(StageEvent *event) { LOG_TRACE("Enter"); - SQLStageEvent *sql_event = static_cast(event); + SQLStageEvent *sql_event = static_cast(event); RC rc = handle_request(sql_event); if (rc != RC::UNIMPLENMENT && rc != RC::SUCCESS) { SqlResult *sql_result = new SqlResult; sql_result->set_return_code(rc); - sql_event->session_event()->set_sql_result(sql_result); + sql_event->session_event()->set_sql_result(sql_result); } else { execute_stage_->handle_event(event); } LOG_TRACE("Exit"); - } RC OptimizeStage::handle_request(SQLStageEvent *sql_event) { @@ -123,7 +122,7 @@ RC OptimizeStage::handle_request(SQLStageEvent *sql_event) } return rc; } - + rc = rewrite(logical_operator); if (rc != RC::SUCCESS) { LOG_WARN("failed to rewrite plan. rc=%s", strrc(rc)); @@ -154,8 +153,8 @@ RC OptimizeStage::optimize(std::unique_ptr &oper) return RC::SUCCESS; } -RC OptimizeStage::generate_physical_plan(std::unique_ptr &logical_operator, - std::unique_ptr &physical_operator) +RC OptimizeStage::generate_physical_plan( + std::unique_ptr &logical_operator, std::unique_ptr &physical_operator) { RC rc = RC::SUCCESS; rc = physical_plan_generator_.create(*logical_operator, physical_operator); @@ -212,7 +211,7 @@ RC OptimizeStage::create_logical_plan(Stmt *stmt, std::unique_ptr & logical_operator) +RC OptimizeStage::create_logical_plan(SQLStageEvent *sql_event, std::unique_ptr &logical_operator) { Stmt *stmt = sql_event->stmt(); if (nullptr == stmt) { @@ -222,7 +221,8 @@ RC OptimizeStage::create_logical_plan(SQLStageEvent *sql_event, std::unique_ptr< return create_logical_plan(stmt, logical_operator); } -RC OptimizeStage::create_select_logical_plan(SelectStmt *select_stmt, std::unique_ptr & logical_operator) +RC OptimizeStage::create_select_logical_plan( + SelectStmt *select_stmt, std::unique_ptr &logical_operator) { std::unique_ptr table_oper(nullptr); @@ -235,12 +235,12 @@ RC OptimizeStage::create_select_logical_plan(SelectStmt *select_stmt, std::uniqu fields.push_back(field); } } - + std::unique_ptr table_get_oper(new TableGetLogicalOperator(table, fields)); if (table_oper == nullptr) { table_oper = std::move(table_get_oper); } else { - JoinLogicalOperator * join_oper = new JoinLogicalOperator; + JoinLogicalOperator *join_oper = new JoinLogicalOperator; join_oper->add_child(std::move(table_oper)); join_oper->add_child(std::move(table_get_oper)); table_oper = std::unique_ptr(join_oper); @@ -253,7 +253,7 @@ RC OptimizeStage::create_select_logical_plan(SelectStmt *select_stmt, std::uniqu LOG_WARN("failed to create predicate logical plan. rc=%s", strrc(rc)); return rc; } - + std::unique_ptr project_oper(new ProjectLogicalOperator(all_fields)); if (predicate_oper) { predicate_oper->add_child(move(table_oper)); @@ -266,24 +266,23 @@ RC OptimizeStage::create_select_logical_plan(SelectStmt *select_stmt, std::uniqu return RC::SUCCESS; } -RC OptimizeStage::create_predicate_logical_plan(FilterStmt *filter_stmt, std::unique_ptr &logical_operator) +RC OptimizeStage::create_predicate_logical_plan( + FilterStmt *filter_stmt, std::unique_ptr &logical_operator) { std::vector> cmp_exprs; const std::vector &filter_units = filter_stmt->filter_units(); - for (const FilterUnit * filter_unit : filter_units) { + for (const FilterUnit *filter_unit : filter_units) { const FilterObj &filter_obj_left = filter_unit->left(); const FilterObj &filter_obj_right = filter_unit->right(); - std::unique_ptr left( - filter_obj_left.is_attr ? - static_cast(new FieldExpr(filter_obj_left.field)) : - static_cast(new ValueExpr(filter_obj_left.value))); + std::unique_ptr left(filter_obj_left.is_attr + ? static_cast(new FieldExpr(filter_obj_left.field)) + : static_cast(new ValueExpr(filter_obj_left.value))); + + std::unique_ptr right(filter_obj_right.is_attr + ? static_cast(new FieldExpr(filter_obj_right.field)) + : static_cast(new ValueExpr(filter_obj_right.value))); - std::unique_ptr right( - filter_obj_right.is_attr ? - 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); } @@ -298,7 +297,8 @@ RC OptimizeStage::create_predicate_logical_plan(FilterStmt *filter_stmt, std::un return RC::SUCCESS; } -RC OptimizeStage::create_delete_logical_plan(DeleteStmt *delete_stmt, std::unique_ptr &logical_operator) +RC OptimizeStage::create_delete_logical_plan( + DeleteStmt *delete_stmt, std::unique_ptr &logical_operator) { Table *table = delete_stmt->table(); FilterStmt *filter_stmt = delete_stmt->filter_stmt(); @@ -314,9 +314,9 @@ RC OptimizeStage::create_delete_logical_plan(DeleteStmt *delete_stmt, std::uniqu if (rc != RC::SUCCESS) { return rc; } - + std::unique_ptr delete_oper(new DeleteLogicalOperator(table)); - + if (predicate_oper) { predicate_oper->add_child(move(table_get_oper)); delete_oper->add_child(move(predicate_oper)); @@ -328,7 +328,8 @@ RC OptimizeStage::create_delete_logical_plan(DeleteStmt *delete_stmt, std::uniqu return rc; } -RC OptimizeStage::create_explain_logical_plan(ExplainStmt *explain_stmt, std::unique_ptr &logical_operator) +RC OptimizeStage::create_explain_logical_plan( + ExplainStmt *explain_stmt, std::unique_ptr &logical_operator) { Stmt *child_stmt = explain_stmt->child(); std::unique_ptr child_oper; diff --git a/src/observer/sql/optimizer/optimize_stage.h b/src/observer/sql/optimizer/optimize_stage.h index 26af536a19ccdc981c6959ad98f04b52371b84a3..13809e0e4d1ed6b4d60f0604f902ac29d3cb30c1 100644 --- a/src/observer/sql/optimizer/optimize_stage.h +++ b/src/observer/sql/optimizer/optimize_stage.h @@ -48,20 +48,21 @@ protected: private: RC handle_request(SQLStageEvent *event); - - RC create_logical_plan(SQLStageEvent *sql_event, std::unique_ptr & logical_operator); + + RC create_logical_plan(SQLStageEvent *sql_event, std::unique_ptr &logical_operator); RC create_logical_plan(Stmt *stmt, std::unique_ptr &logical_operator); - RC create_select_logical_plan(SelectStmt *select_stmt, std::unique_ptr & logical_operator); + RC create_select_logical_plan(SelectStmt *select_stmt, std::unique_ptr &logical_operator); RC create_predicate_logical_plan(FilterStmt *filter_stmt, std::unique_ptr &logical_operator); RC create_delete_logical_plan(DeleteStmt *delete_stmt, std::unique_ptr &logical_operator); RC create_explain_logical_plan(ExplainStmt *explain_stmt, std::unique_ptr &logical_operator); - + RC rewrite(std::unique_ptr &logical_operator); RC optimize(std::unique_ptr &logical_operator); - RC generate_physical_plan(std::unique_ptr &logical_operator, std::unique_ptr &physical_operator); - + RC generate_physical_plan( + std::unique_ptr &logical_operator, std::unique_ptr &physical_operator); + private: Stage *execute_stage_ = nullptr; PhysicalPlanGenerator physical_plan_generator_; - Rewriter rewriter_; + Rewriter rewriter_; }; diff --git a/src/observer/sql/optimizer/physical_plan_generator.cpp b/src/observer/sql/optimizer/physical_plan_generator.cpp index f1653dae3d90143730ab79fb96f13a8be695caad..2a82c1e40ecb79c5fb97566e8af7a31cd840bd01 100644 --- a/src/observer/sql/optimizer/physical_plan_generator.cpp +++ b/src/observer/sql/optimizer/physical_plan_generator.cpp @@ -59,7 +59,7 @@ RC PhysicalPlanGenerator::create(LogicalOperator &logical_operator, std::unique_ case LogicalOperatorType::JOIN: { return create_plan(static_cast(logical_operator), oper); } break; - + default: { return RC::INVALID_ARGUMENT; } @@ -117,10 +117,8 @@ RC PhysicalPlanGenerator::create_plan(TableGetLogicalOperator &table_get_oper, s ASSERT(value_expr != nullptr, "got an index but value expr is null ?"); const TupleCell &tuple_cell = value_expr->get_tuple_cell(); - IndexScanPhysicalOperator *index_scan_oper = - new IndexScanPhysicalOperator(table, index, - &tuple_cell, true/*left_inclusive*/, - &tuple_cell, true /*right_inclusive*/); + IndexScanPhysicalOperator *index_scan_oper = new IndexScanPhysicalOperator( + table, index, &tuple_cell, true /*left_inclusive*/, &tuple_cell, true /*right_inclusive*/); index_scan_oper->set_predicates(std::move(predicates)); oper = std::unique_ptr(index_scan_oper); LOG_TRACE("use index scan"); @@ -130,7 +128,7 @@ RC PhysicalPlanGenerator::create_plan(TableGetLogicalOperator &table_get_oper, s oper = std::unique_ptr(table_scan_oper); LOG_TRACE("use table scan"); } - + return RC::SUCCESS; } @@ -147,7 +145,7 @@ RC PhysicalPlanGenerator::create_plan(PredicateLogicalOperator &pred_oper, std:: LOG_WARN("failed to create child operator of predicate operator. rc=%s", strrc(rc)); return rc; } - + std::vector> &expressions = pred_oper.expressions(); ASSERT(expressions.size() == 1, "predicate logical operator's children should be 1"); @@ -160,7 +158,7 @@ RC PhysicalPlanGenerator::create_plan(PredicateLogicalOperator &pred_oper, std:: RC PhysicalPlanGenerator::create_plan(ProjectLogicalOperator &project_oper, std::unique_ptr &oper) { std::vector> &child_opers = project_oper.children(); - + std::unique_ptr child_phy_oper; RC rc = RC::SUCCESS; @@ -175,7 +173,7 @@ RC PhysicalPlanGenerator::create_plan(ProjectLogicalOperator &project_oper, std: ProjectPhysicalOperator *project_operator = new ProjectPhysicalOperator; const std::vector &project_fields = project_oper.fields(); - for (const Field & field : project_fields) { + for (const Field &field : project_fields) { project_operator->add_projection(field.table(), field.meta()); } diff --git a/src/observer/sql/optimizer/physical_plan_generator.h b/src/observer/sql/optimizer/physical_plan_generator.h index 3af52e87e75c7640f73befda44f8ee5995fae317..fca98191df74bc91df6aabf5e1b690ea0eaf4dd6 100644 --- a/src/observer/sql/optimizer/physical_plan_generator.h +++ b/src/observer/sql/optimizer/physical_plan_generator.h @@ -27,16 +27,14 @@ class DeleteLogicalOperator; class ExplainLogicalOperator; class JoinLogicalOperator; -class PhysicalPlanGenerator -{ -public: +class PhysicalPlanGenerator { +public: PhysicalPlanGenerator() = default; virtual ~PhysicalPlanGenerator() = default; RC create(LogicalOperator &logical_operator, std::unique_ptr &oper); - -private: +private: RC create_plan(TableGetLogicalOperator &table_get_oper, std::unique_ptr &oper); RC create_plan(PredicateLogicalOperator &pred_oper, std::unique_ptr &oper); RC create_plan(ProjectLogicalOperator &project_oper, 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 0f139eddbdee6b1275f18f40d66aa5ce8e309b97..23c31635a59f7bb4141b3dff00c8963ee6eac74f 100644 --- a/src/observer/sql/optimizer/predicate_pushdown_rewriter.cpp +++ b/src/observer/sql/optimizer/predicate_pushdown_rewriter.cpp @@ -52,7 +52,7 @@ RC PredicatePushdownRewriter::rewrite(std::unique_ptr &oper, bo // 所有的表达式都下推到了下层算子 // 这个predicate operator其实就可以不要了。但是这里没办法删除,弄一个空的表达式吧 LOG_TRACE("all expressions of predicate operator were pushdown to table get operator, then make a fake one"); - + Value value; value.type = BOOLEANS; value.bool_value = true; @@ -73,8 +73,7 @@ RC PredicatePushdownRewriter::rewrite(std::unique_ptr &oper, bo * pushdown_exprs 只会增加,不要做清理操作 */ RC PredicatePushdownRewriter::get_exprs_can_pushdown( - std::unique_ptr &expr, - std::vector> &pushdown_exprs) + std::unique_ptr &expr, std::vector> &pushdown_exprs) { RC rc = RC::SUCCESS; if (expr->type() == ExprType::CONJUNCTION) { @@ -85,7 +84,7 @@ RC PredicatePushdownRewriter::get_exprs_can_pushdown( } std::vector> &child_exprs = conjunction_expr->children(); - for (auto iter = child_exprs.begin(); iter != child_exprs.end(); ) { + for (auto iter = child_exprs.begin(); iter != child_exprs.end();) { // 对每个子表达式,判断是否可以下放到table get 算子 // 如果可以的话,就从当前孩子节点中删除他 rc = get_exprs_can_pushdown(*iter, pushdown_exprs); diff --git a/src/observer/sql/optimizer/predicate_pushdown_rewriter.h b/src/observer/sql/optimizer/predicate_pushdown_rewriter.h index 5982013397b05889868b18fb1a32143668a33eb6..a3921ff039584e0234f5ed9058a39e96caba7496 100644 --- a/src/observer/sql/optimizer/predicate_pushdown_rewriter.h +++ b/src/observer/sql/optimizer/predicate_pushdown_rewriter.h @@ -21,8 +21,7 @@ See the Mulan PSL v2 for more details. */ * 将一些谓词表达式下推到表数据扫描中 * 这样可以提前过滤一些数据 */ -class PredicatePushdownRewriter : public RewriteRule -{ +class PredicatePushdownRewriter : public RewriteRule { public: PredicatePushdownRewriter() = default; virtual ~PredicatePushdownRewriter() = default; @@ -30,6 +29,6 @@ public: RC rewrite(std::unique_ptr &oper, bool &change_made) override; private: - RC get_exprs_can_pushdown(std::unique_ptr &expr, - std::vector> &pushdown_exprs); + RC get_exprs_can_pushdown( + std::unique_ptr &expr, std::vector> &pushdown_exprs); }; diff --git a/src/observer/sql/optimizer/predicate_rewrite.h b/src/observer/sql/optimizer/predicate_rewrite.h index 44dcc7f312de034b6530ff8fbd5ea1104cb1f746..a8ca79fc844fbf329c1ccfeeae08c78ef601b420 100644 --- a/src/observer/sql/optimizer/predicate_rewrite.h +++ b/src/observer/sql/optimizer/predicate_rewrite.h @@ -16,8 +16,7 @@ See the Mulan PSL v2 for more details. */ #include "sql/optimizer/rewrite_rule.h" -class PredicateRewriteRule : public RewriteRule -{ +class PredicateRewriteRule : public RewriteRule { public: PredicateRewriteRule() = default; virtual ~PredicateRewriteRule() = default; diff --git a/src/observer/sql/optimizer/rewrite_rule.h b/src/observer/sql/optimizer/rewrite_rule.h index 0037e6e1e4b47580e643eb67875b9bb12714dabc..18699288dd2096bcfd8b9f5ebe97ac2112f49b61 100644 --- a/src/observer/sql/optimizer/rewrite_rule.h +++ b/src/observer/sql/optimizer/rewrite_rule.h @@ -21,18 +21,16 @@ See the Mulan PSL v2 for more details. */ class LogicalOperator; class Expression; -class RewriteRule -{ -public: +class RewriteRule { +public: virtual ~RewriteRule() = default; virtual RC rewrite(std::unique_ptr &oper, bool &change_made) = 0; }; -class ExpressionRewriteRule -{ +class ExpressionRewriteRule { public: virtual ~ExpressionRewriteRule() = default; - + virtual RC rewrite(std::unique_ptr &expr, bool &change_made) = 0; }; diff --git a/src/observer/sql/optimizer/rewriter.cpp b/src/observer/sql/optimizer/rewriter.cpp index 49d738b1ffaeb43e4df0753125689863d5f8ab3a..237f3dc8b4b568da8132cd286d1825479d1ca837 100644 --- a/src/observer/sql/optimizer/rewriter.cpp +++ b/src/observer/sql/optimizer/rewriter.cpp @@ -30,7 +30,7 @@ RC Rewriter::rewrite(std::unique_ptr &oper, bool &change_made) RC rc = RC::SUCCESS; change_made = false; - for (std::unique_ptr & rule : rewrite_rules_) { + for (std::unique_ptr &rule : rewrite_rules_) { bool sub_change_made = false; rc = rule->rewrite(oper, sub_change_made); if (rc != RC::SUCCESS) { @@ -46,7 +46,7 @@ RC Rewriter::rewrite(std::unique_ptr &oper, bool &change_made) if (rc != RC::SUCCESS) { return rc; } - + std::vector> &child_opers = oper->children(); for (auto &child_oper : child_opers) { bool sub_change_made = false; diff --git a/src/observer/sql/optimizer/rewriter.h b/src/observer/sql/optimizer/rewriter.h index 71de1c766b74bb518af8e604f1ad635e6824c3aa..54a3dac64700f37505dbeb317f976b5820927333 100644 --- a/src/observer/sql/optimizer/rewriter.h +++ b/src/observer/sql/optimizer/rewriter.h @@ -20,12 +20,11 @@ See the Mulan PSL v2 for more details. */ class LogicalOperator; -class Rewriter -{ -public: +class Rewriter { +public: Rewriter(); virtual ~Rewriter() = default; - + RC rewrite(std::unique_ptr &oper, bool &change_made); private: diff --git a/src/observer/sql/parser/parse.cpp b/src/observer/sql/parser/parse.cpp index a723c8e9a22d3de93f91a30547c497e2280bb614..fea5ad5712732dc4e524f499ec27f4eb1075d9c8 100644 --- a/src/observer/sql/parser/parse.cpp +++ b/src/observer/sql/parser/parse.cpp @@ -9,7 +9,7 @@ MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. See the Mulan PSL v2 for more details. */ // -// Created by Meiyi +// Created by Meiyi // #include @@ -41,11 +41,16 @@ AttrType attr_type_from_string(const char *s) const char *Value::data() const { switch (type) { - case INTS: return (const char *)&int_value; - case FLOATS: return (const char *)&float_value; - case BOOLEANS: return (const char *)&bool_value; - case CHARS: return (const char *)string_value.data(); - case UNDEFINED: return nullptr; + case INTS: + return (const char *)&int_value; + case FLOATS: + return (const char *)&float_value; + case BOOLEANS: + return (const char *)&bool_value; + case CHARS: + return (const char *)string_value.data(); + case UNDEFINED: + return nullptr; } return nullptr; } @@ -53,22 +58,24 @@ const char *Value::data() const int Value::length() { switch (type) { - case INTS: return sizeof(int_value); - case FLOATS: return sizeof(float_value); - case BOOLEANS: return sizeof(bool_value); - case CHARS: return string_value.size(); - case UNDEFINED: return 0; + case INTS: + return sizeof(int_value); + case FLOATS: + return sizeof(float_value); + case BOOLEANS: + return sizeof(bool_value); + case CHARS: + return string_value.size(); + case UNDEFINED: + return 0; } return 0; } -Command::Command() - : flag(SCF_ERROR) -{ -} +Command::Command() : flag(SCF_ERROR) +{} -Command::Command(enum SqlCommandFlag _flag) - : flag(_flag) +Command::Command(enum SqlCommandFlag _flag) : flag(_flag) {} void ParsedSqlResult::add_command(std::unique_ptr command) diff --git a/src/observer/sql/parser/parse_defs.h b/src/observer/sql/parser/parse_defs.h index 0ced0f42f561b50f01b93b74962d748b45f2f035..db9da7ed5ca4a2cada30cf802031ce8f811b2f63 100644 --- a/src/observer/sql/parser/parse_defs.h +++ b/src/observer/sql/parser/parse_defs.h @@ -25,7 +25,7 @@ See the Mulan PSL v2 for more details. */ #define MAX_ERROR_MESSAGE 20 #define MAX_DATA 50 -//属性结构体 +// 属性结构体 struct RelAttr { std::string relation_name; // relation name (may be NULL) 表名 std::string attribute_name; // attribute name 属性名 @@ -41,9 +41,8 @@ enum CompOp { NO_OP }; -//属性值类型 -enum AttrType -{ +// 属性值类型 +enum AttrType { UNDEFINED, CHARS, INTS, @@ -51,12 +50,12 @@ enum AttrType BOOLEANS, }; -//属性值 +// 属性值 struct Value { - AttrType type; // type of value - int int_value; - float float_value; - bool bool_value; + AttrType type; // type of value + int int_value; + float float_value; + bool bool_value; std::string string_value; const char *data() const; @@ -77,42 +76,41 @@ struct Condition { // struct of select struct Selects { - std::vector attributes; // attributes in select clause + std::vector attributes; // attributes in select clause std::vector relations; std::vector conditions; }; // struct of insert struct Inserts { - std::string relation_name; // Relation to insert into + std::string relation_name; // Relation to insert into std::vector values; }; // struct of delete struct Deletes { - std::string relation_name; // Relation to delete from + std::string relation_name; // Relation to delete from std::vector conditions; }; // struct of update struct Updates { - std::string relation_name; // Relation to update - std::string attribute_name; // Attribute to update - Value value; // update value + std::string relation_name; // Relation to update + std::string attribute_name; // Attribute to update + Value value; // update value std::vector conditions; }; -struct AttrInfo -{ - AttrType type; // Type of attribute - std::string name; // Attribute name - size_t length; // Length of attribute +struct AttrInfo { + AttrType type; // Type of attribute + std::string name; // Attribute name + size_t length; // Length of attribute }; // struct of craete_table struct CreateTable { - std::string relation_name; // Relation name - std::vector attr_infos; // attributes + std::string relation_name; // Relation name + std::vector attr_infos; // attributes }; // struct of drop_table @@ -129,8 +127,8 @@ struct CreateIndex { // struct of drop_index struct DropIndex { - std::string index_name; // Index name - std::string relation_name; //Relation name + std::string index_name; // Index name + std::string relation_name; // Relation name }; struct DescTable { @@ -147,8 +145,7 @@ struct Explain { std::unique_ptr cmd; }; -struct Error -{ +struct Error { std::string error_msg; int line; int column; @@ -181,18 +178,18 @@ enum SqlCommandFlag { class Command { public: enum SqlCommandFlag flag; - Error error; - Selects selection; - Inserts insertion; - Deletes deletion; - Updates update; - CreateTable create_table; - DropTable drop_table; - CreateIndex create_index; - DropIndex drop_index; - DescTable desc_table; - LoadData load_data; - Explain explain; + Error error; + Selects selection; + Inserts insertion; + Deletes deletion; + Updates update; + CreateTable create_table; + DropTable drop_table; + CreateIndex create_index; + DropIndex drop_index; + DescTable desc_table; + LoadData load_data; + Explain explain; public: Command(); @@ -203,12 +200,14 @@ public: * 表示语法解析后的数据 * 叫ParsedSqlNode 可能会更清晰一点 */ -class ParsedSqlResult -{ +class ParsedSqlResult { public: void add_command(std::unique_ptr command); - std::vector> &commands() { return sql_commands_; } - + std::vector> &commands() + { + return sql_commands_; + } + private: std::vector> sql_commands_; }; diff --git a/src/observer/sql/parser/parse_stage.cpp b/src/observer/sql/parser/parse_stage.cpp index 583dd294bfc940919340d8070f4c094167429d19..144c564446f10d6b9485801c56e9caea4a1c1bfa 100644 --- a/src/observer/sql/parser/parse_stage.cpp +++ b/src/observer/sql/parser/parse_stage.cpp @@ -137,7 +137,7 @@ RC ParseStage::handle_request(StageEvent *event) if (parsed_sql_result.commands().size() > 1) { LOG_WARN("got multi sql commands but only 1 will be handled"); } - + std::unique_ptr cmd = std::move(parsed_sql_result.commands().front()); if (cmd->flag == SCF_ERROR) { // set error information to event diff --git a/src/observer/sql/parser/resolve_stage.cpp b/src/observer/sql/parser/resolve_stage.cpp index 3658c328a51678b33f225d9024334e0f9e6b20f4..e53c531ded1e9e8f36b1dd97978aaf92567d7130 100644 --- a/src/observer/sql/parser/resolve_stage.cpp +++ b/src/observer/sql/parser/resolve_stage.cpp @@ -98,7 +98,7 @@ void ResolveStage::handle_event(StageEvent *event) Db *db = session_event->session()->get_current_db(); if (nullptr == db) { LOG_ERROR("cannot current db"); - return ; + return; } Command *cmd = sql_event->command().get(); diff --git a/src/observer/sql/stmt/delete_stmt.cpp b/src/observer/sql/stmt/delete_stmt.cpp index 60b1c1b03621df3e4c2b04be6b529278b20a2055..7319acd93e44a1049cc2560ff4369ce3fb7d4970 100644 --- a/src/observer/sql/stmt/delete_stmt.cpp +++ b/src/observer/sql/stmt/delete_stmt.cpp @@ -18,8 +18,7 @@ See the Mulan PSL v2 for more details. */ #include "storage/common/db.h" #include "storage/common/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() @@ -27,15 +26,14 @@ DeleteStmt::~DeleteStmt() if (nullptr != filter_stmt_) { delete filter_stmt_; filter_stmt_ = nullptr; - } + } } RC DeleteStmt::create(Db *db, const Deletes &delete_sql, Stmt *&stmt) { const char *table_name = delete_sql.relation_name.c_str(); if (nullptr == db || nullptr == table_name) { - LOG_WARN("invalid argument. db=%p, table_name=%p", - db, table_name); + LOG_WARN("invalid argument. db=%p, table_name=%p", db, table_name); return RC::INVALID_ARGUMENT; } @@ -50,9 +48,8 @@ RC DeleteStmt::create(Db *db, const Deletes &delete_sql, Stmt *&stmt) table_map.insert(std::pair(std::string(table_name), table)); FilterStmt *filter_stmt = nullptr; - RC rc = FilterStmt::create(db, table, &table_map, - delete_sql.conditions.data(), static_cast(delete_sql.conditions.size()), - filter_stmt); + 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)); return rc; diff --git a/src/observer/sql/stmt/delete_stmt.h b/src/observer/sql/stmt/delete_stmt.h index 295d13a427f498b12519f07480399b9df37317a3..5dfd1d4bee1992f6b9bf7603d8602d11cf258fba 100644 --- a/src/observer/sql/stmt/delete_stmt.h +++ b/src/observer/sql/stmt/delete_stmt.h @@ -21,17 +21,25 @@ See the Mulan PSL v2 for more details. */ class Table; class FilterStmt; -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 Deletes &delete_sql, Stmt *&stmt); @@ -39,4 +47,3 @@ private: Table *table_ = nullptr; FilterStmt *filter_stmt_ = nullptr; }; - diff --git a/src/observer/sql/stmt/explain_stmt.cpp b/src/observer/sql/stmt/explain_stmt.cpp index 338cb3a1cd440a6305937b04c72da9ec81ce79dc..91602b40974a6dd8697e911d787cdf889035a9c6 100644 --- a/src/observer/sql/stmt/explain_stmt.cpp +++ b/src/observer/sql/stmt/explain_stmt.cpp @@ -16,11 +16,10 @@ See the Mulan PSL v2 for more details. */ #include "sql/stmt/stmt.h" #include "common/log/log.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 Explain &explain, Stmt *& stmt) +RC ExplainStmt::create(Db *db, const Explain &explain, Stmt *&stmt) { Stmt *child_stmt = nullptr; RC rc = Stmt::create_stmt(db, *explain.cmd, child_stmt); diff --git a/src/observer/sql/stmt/explain_stmt.h b/src/observer/sql/stmt/explain_stmt.h index a9044d02b7e743bd4e67a1aa65ab2a35edbd2aba..5a92bff0b5c92943ae0345a8d9787aca52a087e0 100644 --- a/src/observer/sql/stmt/explain_stmt.h +++ b/src/observer/sql/stmt/explain_stmt.h @@ -17,18 +17,23 @@ See the Mulan PSL v2 for more details. */ #include #include "sql/stmt/stmt.h" -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 Explain &query, Stmt *&stmt); - static RC create(Db *db, const Explain &query, Stmt *& stmt); - private: std::unique_ptr child_stmt_; }; diff --git a/src/observer/sql/stmt/filter_stmt.cpp b/src/observer/sql/stmt/filter_stmt.cpp index 1b924280d87727bf19a1774a675674f0d6db20d3..afbba0369315ee7f351e08fe3e8fe2ce836ef533 100644 --- a/src/observer/sql/stmt/filter_stmt.cpp +++ b/src/observer/sql/stmt/filter_stmt.cpp @@ -28,8 +28,7 @@ FilterStmt::~FilterStmt() } RC FilterStmt::create(Db *db, Table *default_table, std::unordered_map *tables, - const Condition *conditions, int condition_num, - FilterStmt *&stmt) + const Condition *conditions, int condition_num, FilterStmt *&stmt) { RC rc = RC::SUCCESS; stmt = nullptr; @@ -51,7 +50,7 @@ RC FilterStmt::create(Db *db, Table *default_table, std::unordered_map *tables, - const RelAttr &attr, Table *&table, const FieldMeta *&field) + const RelAttr &attr, Table *&table, const FieldMeta *&field) { if (common::is_blank(attr.relation_name.c_str())) { table = default_table; @@ -79,22 +78,22 @@ RC get_table_and_field(Db *db, Table *default_table, std::unordered_map *tables, - const Condition &condition, FilterUnit *&filter_unit) + const Condition &condition, FilterUnit *&filter_unit) { RC rc = RC::SUCCESS; - + CompOp comp = condition.comp; if (comp < EQUAL_TO || comp >= NO_OP) { LOG_WARN("invalid compare operator : %d", comp); return RC::INVALID_ARGUMENT; } - + filter_unit = new FilterUnit; - + if (condition.left_is_attr) { Table *table = nullptr; const FieldMeta *field = nullptr; - rc = get_table_and_field(db, default_table, tables, condition.left_attr, table, field); + rc = get_table_and_field(db, default_table, tables, condition.left_attr, table, field); if (rc != RC::SUCCESS) { LOG_WARN("cannot find attr"); return rc; @@ -111,7 +110,7 @@ RC FilterStmt::create_filter_unit(Db *db, Table *default_table, std::unordered_m if (condition.right_is_attr) { Table *table = nullptr; const FieldMeta *field = nullptr; - rc = get_table_and_field(db, default_table, tables, condition.right_attr, table, field); + rc = get_table_and_field(db, default_table, tables, condition.right_attr, table, field); if (rc != RC::SUCCESS) { LOG_WARN("cannot find attr"); return rc; @@ -125,7 +124,6 @@ RC FilterStmt::create_filter_unit(Db *db, Table *default_table, std::unordered_m filter_unit->set_right(filter_obj); } - filter_unit->set_comp(comp); // 检查两个类型是否能够比较 diff --git a/src/observer/sql/stmt/filter_stmt.h b/src/observer/sql/stmt/filter_stmt.h index 977d60e34d7a1a6d781d6d5e33e7fd199b98388c..374efb35ad0978e9f391e58eeeef677369c9c375 100644 --- a/src/observer/sql/stmt/filter_stmt.h +++ b/src/observer/sql/stmt/filter_stmt.h @@ -25,36 +25,37 @@ class Db; class Table; class FieldMeta; -struct FilterObj -{ +struct FilterObj { bool is_attr; Field field; Value value; - void init_attr(const Field &field) { + void init_attr(const Field &field) + { is_attr = true; this->field = field; } - void init_value(const Value &value) { + void init_value(const Value &value) + { is_attr = false; this->value = value; } }; -class FilterUnit -{ +class FilterUnit { public: FilterUnit() = default; ~FilterUnit() + {} + + void set_comp(CompOp comp) { - } - - void set_comp(CompOp comp) { comp_ = comp; } - CompOp comp() const { + CompOp comp() const + { return comp_; } @@ -82,10 +83,8 @@ private: FilterObj right_; }; -class FilterStmt -{ +class FilterStmt { public: - FilterStmt() = default; virtual ~FilterStmt(); @@ -97,12 +96,11 @@ public: public: static RC create(Db *db, Table *default_table, std::unordered_map *tables, - const Condition *conditions, int condition_num, - FilterStmt *&stmt); + const Condition *conditions, int condition_num, FilterStmt *&stmt); static RC create_filter_unit(Db *db, Table *default_table, std::unordered_map *tables, - const Condition &condition, FilterUnit *&filter_unit); + const Condition &condition, FilterUnit *&filter_unit); private: - std::vector filter_units_; // 默认当前都是AND关系 + std::vector filter_units_; // 默认当前都是AND关系 }; diff --git a/src/observer/sql/stmt/insert_stmt.cpp b/src/observer/sql/stmt/insert_stmt.cpp index 8649455a1ddc4bc4051c47f26dd1f826c6a0fb84..4c8d21be37d4836e870ca9a0b6817029226912de 100644 --- a/src/observer/sql/stmt/insert_stmt.cpp +++ b/src/observer/sql/stmt/insert_stmt.cpp @@ -18,15 +18,17 @@ See the Mulan PSL v2 for more details. */ #include "storage/common/table.h" InsertStmt::InsertStmt(Table *table, const Value *values, int value_amount) - : table_ (table), values_(values), value_amount_(value_amount) + : table_(table), values_(values), value_amount_(value_amount) {} RC InsertStmt::create(Db *db, const Inserts &inserts, Stmt *&stmt) { const char *table_name = inserts.relation_name.c_str(); if (nullptr == db || nullptr == table_name || inserts.values.empty()) { - LOG_WARN("invalid argument. db=%p, table_name=%p, value_num=%d", - db, table_name, static_cast(inserts.values.size())); + LOG_WARN("invalid argument. db=%p, table_name=%p, value_num=%d", + db, + table_name, + static_cast(inserts.values.size())); return RC::INVALID_ARGUMENT; } @@ -53,9 +55,12 @@ RC InsertStmt::create(Db *db, const Inserts &inserts, Stmt *&stmt) const FieldMeta *field_meta = table_meta.field(i + sys_field_num); const AttrType field_type = field_meta->type(); const AttrType value_type = values[i].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); + 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); return RC::SCHEMA_FIELD_TYPE_MISMATCH; } } diff --git a/src/observer/sql/stmt/insert_stmt.h b/src/observer/sql/stmt/insert_stmt.h index 70fcf786effef2c3016140f552b90eb4dda07a32..cd13b7c5809f396e44d5a90cd68b1ac71644a676 100644 --- a/src/observer/sql/stmt/insert_stmt.h +++ b/src/observer/sql/stmt/insert_stmt.h @@ -20,27 +20,35 @@ See the Mulan PSL v2 for more details. */ class Table; class Db; -class InsertStmt : public Stmt -{ +class InsertStmt : public Stmt { public: - InsertStmt() = default; InsertStmt(Table *table, const Value *values, int value_amount); - StmtType type() const override { + StmtType type() const override + { return StmtType::INSERT; } + public: static RC create(Db *db, const Inserts &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; }; - diff --git a/src/observer/sql/stmt/select_stmt.cpp b/src/observer/sql/stmt/select_stmt.cpp index 46a1c3e10311396c4403c088b60dcd96af3f1162..7824be79510472c688004026826f9c415565ecc1 100644 --- a/src/observer/sql/stmt/select_stmt.cpp +++ b/src/observer/sql/stmt/select_stmt.cpp @@ -60,15 +60,16 @@ RC SelectStmt::create(Db *db, const Selects &select_sql, Stmt *&stmt) } tables.push_back(table); - table_map.insert(std::pair(table_name, table)); + table_map.insert(std::pair(table_name, table)); } - + // collect query fields in `select` statement std::vector query_fields; for (int i = static_cast(select_sql.attributes.size()) - 1; i >= 0; i--) { const RelAttr &relation_attr = select_sql.attributes[i]; - if (common::is_blank(relation_attr.relation_name.c_str()) && 0 == strcmp(relation_attr.attribute_name.c_str(), "*")) { + if (common::is_blank(relation_attr.relation_name.c_str()) && + 0 == strcmp(relation_attr.attribute_name.c_str(), "*")) { for (Table *table : tables) { wildcard_fields(table, query_fields); } @@ -131,8 +132,12 @@ RC SelectStmt::create(Db *db, const Selects &select_sql, Stmt *&stmt) // create filter statement in `where` statement FilterStmt *filter_stmt = nullptr; - RC rc = FilterStmt::create(db, default_table, &table_map, - select_sql.conditions.data(), static_cast(select_sql.conditions.size()), filter_stmt); + RC rc = FilterStmt::create(db, + default_table, + &table_map, + select_sql.conditions.data(), + static_cast(select_sql.conditions.size()), + filter_stmt); if (rc != RC::SUCCESS) { LOG_WARN("cannot construct filter stmt"); return rc; diff --git a/src/observer/sql/stmt/select_stmt.h b/src/observer/sql/stmt/select_stmt.h index 6d18bc74cbb3b3a030655a4b62a419baccd789c9..230525d9938c8e5ec3b81b5b29a9fb3e38788980 100644 --- a/src/observer/sql/stmt/select_stmt.h +++ b/src/observer/sql/stmt/select_stmt.h @@ -25,25 +25,35 @@ class FilterStmt; class Db; class Table; -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 Selects &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
tables_; FilterStmt *filter_stmt_ = nullptr; }; - diff --git a/src/observer/sql/stmt/stmt.cpp b/src/observer/sql/stmt/stmt.cpp index ec140dfe97b5afbbdb06e0eadd87648c57ec9edb..ff001936c7963e72a8983cd4fc1bf2f020d70798 100644 --- a/src/observer/sql/stmt/stmt.cpp +++ b/src/observer/sql/stmt/stmt.cpp @@ -39,10 +39,7 @@ RC Stmt::create_stmt(Db *db, const Command &cmd, Stmt *&stmt) } default: { LOG_INFO("Command::type %d doesn't need to create statement.", cmd.flag); - } - break; + } break; } return RC::UNIMPLENMENT; } - - diff --git a/src/observer/sql/stmt/stmt.h b/src/observer/sql/stmt/stmt.h index 77fe6d3a2bdb585da2768770798cebaa75bb8d72..ace75a7e26f88c4c69e7962034d509a3513c9463 100644 --- a/src/observer/sql/stmt/stmt.h +++ b/src/observer/sql/stmt/stmt.h @@ -19,8 +19,7 @@ See the Mulan PSL v2 for more details. */ class Db; -enum class StmtType -{ +enum class StmtType { SELECT, INSERT, UPDATE, @@ -43,10 +42,8 @@ enum class StmtType PREDICATE, }; -class Stmt -{ +class Stmt { public: - Stmt() = default; virtual ~Stmt() = default; @@ -57,4 +54,3 @@ public: private: }; - diff --git a/src/observer/sql/stmt/update_stmt.cpp b/src/observer/sql/stmt/update_stmt.cpp index 89331f54c740b0a26432cb7a92da4a406365e0d8..742bd23a7324e7c6e1dbf1a89fdf0672002f1ad9 100644 --- a/src/observer/sql/stmt/update_stmt.cpp +++ b/src/observer/sql/stmt/update_stmt.cpp @@ -15,7 +15,7 @@ See the Mulan PSL v2 for more details. */ #include "sql/stmt/update_stmt.h" UpdateStmt::UpdateStmt(Table *table, Value *values, int value_amount) - : table_ (table), values_(values), value_amount_(value_amount) + : table_(table), values_(values), value_amount_(value_amount) {} RC UpdateStmt::create(Db *db, const Updates &update, Stmt *&stmt) diff --git a/src/observer/sql/stmt/update_stmt.h b/src/observer/sql/stmt/update_stmt.h index ca566618d827f10c102f9d3560bbe8b9dabc31db..f5632637055d7c74742d6092300a9f6a4aed1aad 100644 --- a/src/observer/sql/stmt/update_stmt.h +++ b/src/observer/sql/stmt/update_stmt.h @@ -19,10 +19,8 @@ See the Mulan PSL v2 for more details. */ class Table; -class UpdateStmt : public Stmt -{ +class UpdateStmt : public Stmt { public: - UpdateStmt() = default; UpdateStmt(Table *table, Value *values, int value_amount); @@ -30,13 +28,21 @@ public: static RC create(Db *db, const Updates &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; }; - diff --git a/src/observer/storage/clog/clog.cpp b/src/observer/storage/clog/clog.cpp index b21fa26764c819716dd6ca971469933671f9db02..a2e655e50bb47a935be0c75111391bd887c88863 100644 --- a/src/observer/storage/clog/clog.cpp +++ b/src/observer/storage/clog/clog.cpp @@ -194,8 +194,8 @@ RC CLogBuffer::append_log_record(CLogRecord *log_rec, int &start_off) write_offset_ += CLOG_BLOCK_HDR_SIZE; return append_log_record(log_rec, start_off); } else { - if (logrec_left_len <= (CLOG_BLOCK_DATA_SIZE - log_block->log_block_hdr_.log_data_len_)) { //不需要再跨block存放 - if (log_block->log_block_hdr_.log_data_len_ == 0) { //当前为新block + if (logrec_left_len <= (CLOG_BLOCK_DATA_SIZE - log_block->log_block_hdr_.log_data_len_)) { // 不需要再跨block存放 + if (log_block->log_block_hdr_.log_data_len_ == 0) { // 当前为新block if (start_off == 0) { log_block->log_block_hdr_.first_rec_offset_ = CLOG_BLOCK_HDR_SIZE; } else { @@ -206,8 +206,8 @@ RC CLogBuffer::append_log_record(CLogRecord *log_rec, int &start_off) write_offset_ += logrec_left_len; log_block->log_block_hdr_.log_data_len_ += logrec_left_len; start_off += logrec_left_len; - } else { //需要跨block - if (log_block->log_block_hdr_.log_data_len_ == 0) { //当前为新block + } else { // 需要跨block + if (log_block->log_block_hdr_.log_data_len_ == 0) { // 当前为新block log_block->log_block_hdr_.first_rec_offset_ = CLOG_BLOCK_SIZE; } int32_t block_left_len = CLOG_BLOCK_DATA_SIZE - log_block->log_block_hdr_.log_data_len_; @@ -223,7 +223,7 @@ RC CLogBuffer::append_log_record(CLogRecord *log_rec, int &start_off) RC CLogBuffer::flush_buffer(CLogFile *log_file) { - if (write_offset_ == CLOG_BUFFER_SIZE) { //如果是buffer满触发的下刷 + if (write_offset_ == CLOG_BUFFER_SIZE) { // 如果是buffer满触发的下刷 CLogBlock *log_block = (CLogBlock *)buffer_; log_file->write(log_block->log_block_hdr_.log_block_no, CLOG_BUFFER_SIZE, buffer_); write_block_offset_ = 0; @@ -324,7 +324,7 @@ RC CLogFile::recover(CLogMTRManager *mtr_mgr, CLogBuffer *log_buffer) } } - if (log_block->log_block_hdr_.log_data_len_ < CLOG_BLOCK_DATA_SIZE) { //最后一个block + if (log_block->log_block_hdr_.log_data_len_ < CLOG_BLOCK_DATA_SIZE) { // 最后一个block log_buffer->block_copy(0, log_block); log_buffer->set_write_block_offset(0); log_buffer->set_write_offset(log_block->log_block_hdr_.log_data_len_ + CLOG_BLOCK_HDR_SIZE); @@ -347,7 +347,7 @@ done: RC CLogFile::block_recover(CLogBlock *block, int16_t &offset, CLogRecordBuf *logrec_buf, CLogRecord *&log_rec) { if (offset == CLOG_BLOCK_HDR_SIZE && - block->log_block_hdr_.first_rec_offset_ != CLOG_BLOCK_HDR_SIZE) { //跨block中的某部分(非第一部分) + block->log_block_hdr_.first_rec_offset_ != CLOG_BLOCK_HDR_SIZE) { // 跨block中的某部分(非第一部分) // 追加到logrec_buf memcpy(&logrec_buf->buffer_[logrec_buf->write_offset_], (char *)block + (int)offset, @@ -370,7 +370,7 @@ RC CLogFile::block_recover(CLogBlock *block, int16_t &offset, CLogRecordBuf *log if (logrec_hdr->logrec_len_ <= CLOG_BLOCK_SIZE - offset) { log_rec = new CLogRecord((char *)block + (int)offset); offset += logrec_hdr->logrec_len_; - } else { //此时为跨block的第一部分 + } else { // 此时为跨block的第一部分 // 开始写入logrec_buf memcpy( &logrec_buf->buffer_[logrec_buf->write_offset_], (char *)block + (int)offset, CLOG_BLOCK_SIZE - offset); diff --git a/src/observer/storage/clog/clog.h b/src/observer/storage/clog/clog.h index e3ab8679f5affc0ff85abf5a5fdee1cd04416c0b..ef9a4c2667848f8aea9f220e7a5cc86450e7a1a3 100644 --- a/src/observer/storage/clog/clog.h +++ b/src/observer/storage/clog/clog.h @@ -27,7 +27,7 @@ See the Mulan PSL v2 for more details. */ #include "storage/persist/persist.h" #include "rc.h" -//固定文件大小 TODO: 循环文件组 +// 固定文件大小 TODO: 循环文件组 #define CLOG_FILE_SIZE 48 * 1024 * 1024 #define CLOG_BUFFER_SIZE 4 * 1024 * 1024 #define TABLE_NAME_MAX_LEN 20 // TODO: 表名不要超过20字节 @@ -241,7 +241,7 @@ public: RC clog_gen_record(CLogType flag, int32_t trx_id, CLogRecord *&log_rec, const char *table_name = nullptr, int data_len = 0, Record *rec = nullptr); - //追加写到log_buffer + // 追加写到log_buffer RC clog_append_record(CLogRecord *log_rec); // 通常不需要在外部调用 RC clog_sync(); diff --git a/src/observer/storage/common/condition_filter.h b/src/observer/storage/common/condition_filter.h index 9bc0ae48b944b6b683b8f818dce11ef45d6b843c..2fa6e61c5e2d3c6f7a05a76a7a58e843ddc5b40c 100644 --- a/src/observer/storage/common/condition_filter.h +++ b/src/observer/storage/common/condition_filter.h @@ -66,7 +66,10 @@ public: return comp_op_; } - AttrType attr_type() const { return attr_type_; } + AttrType attr_type() const + { + return attr_type_; + } private: ConDesc left_; diff --git a/src/observer/storage/common/db.cpp b/src/observer/storage/common/db.cpp index af1a5c6c7cdd3f07056b4ccad12ae450193ae759..bda0e1835826351d8112ca7c238f84273e9ff928 100644 --- a/src/observer/storage/common/db.cpp +++ b/src/observer/storage/common/db.cpp @@ -72,7 +72,8 @@ RC Db::create_table(const char *table_name, int attribute_count, const AttrInfo // 文件路径可以移到Table模块 std::string table_file_path = table_meta_file(path_.c_str(), table_name); Table *table = new Table(); - rc = table->create(table_file_path.c_str(), table_name, path_.c_str(), attribute_count, attributes, get_clog_manager()); + rc = table->create( + table_file_path.c_str(), table_name, path_.c_str(), attribute_count, attributes, get_clog_manager()); if (rc != RC::SUCCESS) { LOG_ERROR("Failed to create table %s.", table_name); delete table; @@ -164,16 +165,17 @@ RC Db::recover() CLogMTRManager *mtr_manager = clog_manager_->get_mtr_manager(); for (auto it = mtr_manager->log_redo_list.begin(); it != mtr_manager->log_redo_list.end(); it++) { CLogRecord *clog_record = *it; - if (clog_record->get_log_type() != CLogType::REDO_INSERT && clog_record->get_log_type() != CLogType::REDO_DELETE) { + if (clog_record->get_log_type() != CLogType::REDO_INSERT && + clog_record->get_log_type() != CLogType::REDO_DELETE) { delete clog_record; continue; } auto find_iter = mtr_manager->trx_commited.find(clog_record->get_trx_id()); if (find_iter == mtr_manager->trx_commited.end()) { - LOG_ERROR("CLog record without commit message! "); // unexpected error + LOG_ERROR("CLog record without commit message! "); // unexpected error delete clog_record; return RC::GENERIC_ERROR; - } else if (find_iter->second == false ) { + } else if (find_iter->second == false) { delete clog_record; continue; } @@ -184,7 +186,7 @@ RC Db::recover() continue; } - switch(clog_record->get_log_type()) { + switch (clog_record->get_log_type()) { case CLogType::REDO_INSERT: { char *record_data = new char[clog_record->log_record_.ins.data_len_]; memcpy(record_data, clog_record->log_record_.ins.data_, clog_record->log_record_.ins.data_len_); @@ -209,7 +211,7 @@ RC Db::recover() LOG_ERROR("Failed to recover. rc=%d:%s", rc, strrc(rc)); break; } - + if (max_trx_id < clog_record->get_trx_id()) { max_trx_id = clog_record->get_trx_id(); } @@ -224,6 +226,7 @@ RC Db::recover() return rc; } -CLogManager *Db::get_clog_manager() { +CLogManager *Db::get_clog_manager() +{ return clog_manager_; } \ No newline at end of file diff --git a/src/observer/storage/common/field.h b/src/observer/storage/common/field.h index 3725a81a1df3bdfc78052b4144bf6070dc4c20e0..492e66182d42ec6ae000bc7d84b3b5b36db4ee82 100644 --- a/src/observer/storage/common/field.h +++ b/src/observer/storage/common/field.h @@ -17,24 +17,35 @@ See the Mulan PSL v2 for more details. */ #include "storage/common/table.h" #include "storage/common/field_meta.h" -class Field -{ +class Field { public: Field() = default; 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(); } - 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) { @@ -44,6 +55,7 @@ public: { this->field_ = field; } + private: const Table *table_ = nullptr; const FieldMeta *field_ = nullptr; diff --git a/src/observer/storage/common/field_meta.cpp b/src/observer/storage/common/field_meta.cpp index 90f628ea661cfa9c54d0ad5a429cd03bf3532ea0..5146e62b91c2bb85ff103ec80b005f8eb2ea04c6 100644 --- a/src/observer/storage/common/field_meta.cpp +++ b/src/observer/storage/common/field_meta.cpp @@ -25,7 +25,6 @@ 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) {} diff --git a/src/observer/storage/common/table.cpp b/src/observer/storage/common/table.cpp index 0168b63037daf9c552a4bd386fbf17f2500c378c..cf5ed8ba154f8ba051c6c0eea1349defaf8cc070 100644 --- a/src/observer/storage/common/table.cpp +++ b/src/observer/storage/common/table.cpp @@ -51,8 +51,8 @@ Table::~Table() LOG_INFO("Table has been closed: %s", name()); } -RC Table::create( - const char *path, const char *name, const char *base_dir, int attribute_count, const AttrInfo attributes[], CLogManager *clog_manager) +RC Table::create(const char *path, const char *name, const char *base_dir, int attribute_count, + const AttrInfo attributes[], CLogManager *clog_manager) { if (common::is_blank(name)) { @@ -273,7 +273,8 @@ RC Table::insert_record(Trx *trx, Record *record) if (trx != nullptr) { // append clog record CLogRecord *clog_record = nullptr; - rc = clog_manager_->clog_gen_record(CLogType::REDO_INSERT, trx->get_current_id(), clog_record, name(), table_meta_.record_size(), record); + rc = clog_manager_->clog_gen_record( + CLogType::REDO_INSERT, trx->get_current_id(), clog_record, name(), table_meta_.record_size(), record); if (rc != RC::SUCCESS) { LOG_ERROR("Failed to create a clog record. rc=%d:%s", rc, strrc(rc)); return rc; @@ -432,16 +433,15 @@ static RC scan_record_reader_adapter(Record *record, void *context) return RC::SUCCESS; } -RC Table::scan_record(Trx *trx, ConditionFilter *filter, - int limit, void *context, - void (*record_reader)(const char *data, void *context)) +RC Table::scan_record( + Trx *trx, ConditionFilter *filter, int limit, void *context, void (*record_reader)(const char *data, void *context)) { RecordReaderScanAdapter adapter(record_reader, context); return scan_record(trx, filter, limit, (void *)&adapter, scan_record_reader_adapter); } -RC Table::scan_record(Trx *trx, ConditionFilter *filter, int limit, void *context, - RC (*record_reader)(Record *record, void *context)) +RC Table::scan_record( + Trx *trx, ConditionFilter *filter, int limit, void *context, RC (*record_reader)(Record *record, void *context)) { if (nullptr == record_reader) { return RC::INVALID_ARGUMENT; @@ -489,9 +489,8 @@ RC Table::scan_record(Trx *trx, ConditionFilter *filter, int limit, void *contex return rc; } -RC Table::scan_record_by_index(Trx *trx, IndexScanner *scanner, ConditionFilter *filter, - int limit, void *context, - RC (*record_reader)(Record *, void *)) +RC Table::scan_record_by_index(Trx *trx, IndexScanner *scanner, ConditionFilter *filter, int limit, void *context, + RC (*record_reader)(Record *, void *)) { RC rc = RC::SUCCESS; RID rid; @@ -557,7 +556,9 @@ RC Table::create_index(Trx *trx, const char *index_name, const char *attribute_n } if (table_meta_.index(index_name) != nullptr || table_meta_.find_index_by_field((attribute_name))) { LOG_INFO("Invalid input arguments, table name is %s, index %s exist or attribute %s exist index", - name(), index_name, attribute_name); + name(), + index_name, + attribute_name); return RC::SCHEMA_INDEX_EXIST; } @@ -570,8 +571,7 @@ RC Table::create_index(Trx *trx, const char *index_name, const char *attribute_n 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", - name(), index_name, attribute_name); + LOG_INFO("Failed to init IndexMeta in table:%s, index_name:%s, field_name:%s", name(), index_name, attribute_name); return rc; } @@ -689,24 +689,27 @@ RC Table::delete_record(Trx *trx, ConditionFilter *filter, int *deleted_count) RC Table::delete_record(Trx *trx, Record *record) { RC rc = RC::SUCCESS; - + rc = delete_entry_of_indexes(record->data(), record->rid(), false); // 重复代码 refer to commit_delete if (rc != RC::SUCCESS) { LOG_ERROR("Failed to delete indexes of record (rid=%d.%d). rc=%d:%s", - record->rid().page_num, record->rid().slot_num, rc, strrc(rc)); + record->rid().page_num, + record->rid().slot_num, + rc, + strrc(rc)); return rc; - } - + } + rc = record_handler_->delete_record(&record->rid()); if (rc != RC::SUCCESS) { - LOG_ERROR("Failed to delete record (rid=%d.%d). rc=%d:%s", - record->rid().page_num, record->rid().slot_num, rc, strrc(rc)); + LOG_ERROR( + "Failed to delete record (rid=%d.%d). rc=%d:%s", record->rid().page_num, record->rid().slot_num, rc, strrc(rc)); return rc; } if (trx != nullptr) { rc = trx->delete_record(this, record); - + CLogRecord *clog_record = nullptr; rc = clog_manager_->clog_gen_record(CLogType::REDO_DELETE, trx->get_current_id(), clog_record, name(), 0, record); if (rc != RC::SUCCESS) { @@ -726,7 +729,7 @@ RC Table::recover_delete_record(Record *record) { RC rc = RC::SUCCESS; rc = record_handler_->delete_record(&record->rid()); - + return rc; } @@ -741,7 +744,10 @@ RC Table::commit_delete(Trx *trx, const RID &rid) rc = delete_entry_of_indexes(record.data(), record.rid(), false); if (rc != RC::SUCCESS) { LOG_ERROR("Failed to delete indexes of record(rid=%d.%d). rc=%d:%s", - rid.page_num, rid.slot_num, rc, strrc(rc)); // panic? + rid.page_num, + rid.slot_num, + rc, + strrc(rc)); // panic? } rc = record_handler_->delete_record(&rid); @@ -847,36 +853,31 @@ IndexScanner *Table::find_index_for_scan(const DefaultConditionFilter &filter) bool left_inclusive = false; bool right_inclusive = false; switch (filter.comp_op()) { - case EQUAL_TO: { - left_key = (const char *)value_cond_desc->value.data(); - right_key = (const char *)value_cond_desc->value.data(); - left_inclusive = true; - right_inclusive = true; - } - break; - case LESS_EQUAL: { - right_key = (const char *)value_cond_desc->value.data(); - right_inclusive = true; - } - break; - case GREAT_EQUAL: { - left_key = (const char *)value_cond_desc->value.data(); - left_inclusive = true; - } - break; - case LESS_THAN: { - right_key = (const char *)value_cond_desc->value.data(); - right_inclusive = false; - } - break; - case GREAT_THAN: { - left_key = (const char *)value_cond_desc->value.data(); - left_inclusive = false; - } - break; - default: { - return nullptr; - } + case EQUAL_TO: { + left_key = (const char *)value_cond_desc->value.data(); + right_key = (const char *)value_cond_desc->value.data(); + left_inclusive = true; + right_inclusive = true; + } break; + case LESS_EQUAL: { + right_key = (const char *)value_cond_desc->value.data(); + right_inclusive = true; + } break; + case GREAT_EQUAL: { + left_key = (const char *)value_cond_desc->value.data(); + left_inclusive = true; + } break; + case LESS_THAN: { + right_key = (const char *)value_cond_desc->value.data(); + right_inclusive = false; + } break; + case GREAT_THAN: { + left_key = (const char *)value_cond_desc->value.data(); + left_inclusive = false; + } break; + default: { + return nullptr; + } } if (filter.attr_type() == CHARS) { @@ -918,7 +919,10 @@ RC Table::sync() rc = index->sync(); if (rc != RC::SUCCESS) { LOG_ERROR("Failed to flush index's pages. table=%s, index=%s, rc=%d:%s", - name(), index->index_meta().name(), rc, strrc(rc)); + name(), + index->index_meta().name(), + rc, + strrc(rc)); return rc; } } diff --git a/src/observer/storage/common/table_meta.cpp b/src/observer/storage/common/table_meta.cpp index 2407068f1c28d29c53bc64ac59e32a5ced1159f2..53eeef6b1866bbe1ed731625e04de92a2cec7475 100644 --- a/src/observer/storage/common/table_meta.cpp +++ b/src/observer/storage/common/table_meta.cpp @@ -82,7 +82,8 @@ RC TableMeta::init(const char *name, int field_num, const AttrInfo attributes[]) for (int i = 0; i < field_num; i++) { const AttrInfo &attr_info = attributes[i]; - rc = fields_[i + sys_fields_.size()].init(attr_info.name.c_str(), attr_info.type, field_offset, attr_info.length, true); + rc = fields_[i + sys_fields_.size()].init( + attr_info.name.c_str(), attr_info.type, field_offset, attr_info.length, true); 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; diff --git a/src/observer/storage/common/table_meta.h b/src/observer/storage/common/table_meta.h index 354a92df262ced1a83e75a648b782a1d29cf9b2f..46c2da91f3c4d902d9d14387a6da67773652e422 100644 --- a/src/observer/storage/common/table_meta.h +++ b/src/observer/storage/common/table_meta.h @@ -41,8 +41,11 @@ public: 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_; } - int field_num() const; // sys field included + const std::vector *field_metas() const + { + return &fields_; + } + int field_num() const; // sys field included int sys_field_num() const; const IndexMeta *index(const char *name) const; diff --git a/src/observer/storage/default/disk_buffer_pool.cpp b/src/observer/storage/default/disk_buffer_pool.cpp index 62a074dbf33fe0bbebfb88842d2c6b4b7aeab96b..d4027d886f43edecf6e26b05ffce0d61a3e7cb02 100644 --- a/src/observer/storage/default/disk_buffer_pool.cpp +++ b/src/observer/storage/default/disk_buffer_pool.cpp @@ -37,7 +37,7 @@ BPFrameManager::BPFrameManager(const char *name) : allocator_(name) RC BPFrameManager::init(int pool_num) { - int ret = allocator_.init(false, pool_num); + int ret = allocator_.init(false, pool_num); if (ret == 0) { return RC::SUCCESS; } @@ -57,12 +57,12 @@ RC BPFrameManager::cleanup() Frame *BPFrameManager::begin_purge() { Frame *frame_can_purge = nullptr; - auto purge_finder = [&frame_can_purge](const BPFrameId &frame_id, Frame * const frame) { + auto purge_finder = [&frame_can_purge](const BPFrameId &frame_id, Frame *const frame) { if (frame->can_purge()) { frame_can_purge = frame; - return false; // false to break the progress + return false; // false to break the progress } - return true; // true continue to look up + return true; // true continue to look up }; frames_.foreach_reverse(purge_finder); return frame_can_purge; @@ -87,7 +87,7 @@ Frame *BPFrameManager::alloc(int file_desc, PageNum page_num) bool found = frames_.get(frame_id, frame); if (found) { // assert (frame != nullptr); - return nullptr; // should use get + return nullptr; // should use get } frame = allocator_.alloc(); @@ -106,7 +106,10 @@ RC BPFrameManager::free(int file_desc, PageNum page_num, Frame *frame) bool found = frames_.get(frame_id, frame_source); if (!found || frame != frame_source) { LOG_WARN("failed to find frame or got frame not match. file_desc=%d, PageNum=%d, frame_source=%p, frame=%p", - file_desc, page_num, frame_source, frame); + file_desc, + page_num, + frame_source, + frame); return RC::GENERIC_ERROR; } @@ -120,13 +123,13 @@ std::list BPFrameManager::find_list(int file_desc) std::lock_guard lock_guard(lock_); std::list frames; - auto fetcher = [&frames, file_desc](const BPFrameId &frame_id, Frame * const frame) -> bool { + auto fetcher = [&frames, file_desc](const BPFrameId &frame_id, Frame *const frame) -> bool { if (file_desc == frame_id.file_desc()) { frames.push_back(frame); } return true; }; - frames_.foreach(fetcher); + frames_.foreach (fetcher); return frames; } @@ -168,9 +171,8 @@ RC BufferPoolIterator::reset() //////////////////////////////////////////////////////////////////////////////// DiskBufferPool::DiskBufferPool(BufferPoolManager &bp_manager, BPFrameManager &frame_manager) - : bp_manager_(bp_manager), frame_manager_(frame_manager) -{ -} + : bp_manager_(bp_manager), frame_manager_(frame_manager) +{} DiskBufferPool::~DiskBufferPool() { @@ -255,7 +257,6 @@ RC DiskBufferPool::get_this_page(PageNum page_num, Frame **frame) used_match_frame->pin_count_++; used_match_frame->acc_time_ = current_time(); - *frame = used_match_frame; return RC::SUCCESS; } @@ -296,7 +297,7 @@ RC DiskBufferPool::allocate_page(Frame **frame) (file_header_->allocated_pages)++; file_header_->bitmap[byte] |= (1 << bit); // TODO, do we need clean the loaded page's data? - hdr_frame_->mark_dirty(); + hdr_frame_->mark_dirty(); return get_this_page(i, frame); } } @@ -304,7 +305,8 @@ RC DiskBufferPool::allocate_page(Frame **frame) if (file_header_->page_count >= BPFileHeader::MAX_PAGE_NUM) { LOG_WARN("file buffer pool is full. page count %d, max page count %d", - file_header_->page_count, BPFileHeader::MAX_PAGE_NUM); + file_header_->page_count, + BPFileHeader::MAX_PAGE_NUM); return BUFFERPOOL_NOBUF; } @@ -343,7 +345,7 @@ RC DiskBufferPool::allocate_page(Frame **frame) RC DiskBufferPool::unpin_page(Frame *frame) { - assert(frame->pin_count_ >= 1); + ASSERT(frame->pin_count_ >= 1, "Page %d 's pin_count is smaller than 1", frame->page_num()); if (--frame->pin_count_ == 0) { PageNum page_num = frame->page_num(); auto pages_it = disposed_pages.find(page_num); @@ -383,7 +385,9 @@ RC DiskBufferPool::purge_frame(PageNum page_num, Frame *buf) { if (buf->pin_count_ > 0) { LOG_INFO("Begin to free page %d of %d(file id), but it's pinned, pin_count:%d.", - buf->page_num(), buf->file_desc_, buf->pin_count_); + buf->page_num(), + buf->file_desc_, + buf->pin_count_); return RC::LOCKED_UNLOCK; } @@ -423,7 +427,9 @@ RC DiskBufferPool::purge_all_pages() Frame *frame = *it; if (frame->pin_count_ > 0) { LOG_WARN("The page has been pinned, file_desc:%d, pagenum:%d, pin_count=%d", - frame->file_desc_, frame->page_.page_num, frame->pin_count_); + frame->file_desc_, + frame->page_.page_num, + frame->pin_count_); continue; } if (frame->dirty_) { @@ -441,13 +447,17 @@ RC DiskBufferPool::purge_all_pages() RC DiskBufferPool::check_all_pages_unpinned() { std::list frames = frame_manager_.find_list(file_desc_); - for (auto & frame : frames) { + for (auto &frame : frames) { if (frame->page_num() == BP_HEADER_PAGE && frame->pin_count_ > 1) { LOG_WARN("This page has been pinned. file desc=%d, page num:%d, pin count=%d", - file_desc_, frame->page_num(), frame->pin_count_); + file_desc_, + frame->page_num(), + frame->pin_count_); } else if (frame->page_num() != BP_HEADER_PAGE && frame->pin_count_ > 0) { LOG_WARN("This page has been pinned. file desc=%d, page num:%d, pin count=%d", - file_desc_, frame->page_num(), frame->pin_count_); + file_desc_, + frame->page_num(), + frame->pin_count_); } } LOG_INFO("all pages have been checked of file desc %d", file_desc_); @@ -549,8 +559,7 @@ RC DiskBufferPool::load_page(PageNum page_num, Frame *frame) { s64_t offset = ((s64_t)page_num) * sizeof(Page); if (lseek(file_desc_, offset, SEEK_SET) == -1) { - LOG_ERROR("Failed to load page %s:%d, due to failed to lseek:%s.", - file_name_.c_str(), page_num, strerror(errno)); + LOG_ERROR("Failed to load page %s:%d, due to failed to lseek:%s.", file_name_.c_str(), page_num, strerror(errno)); return RC::IOERR_SEEK; } @@ -558,7 +567,11 @@ RC DiskBufferPool::load_page(PageNum page_num, Frame *frame) int ret = readn(file_desc_, &(frame->page_), sizeof(Page)); if (ret != 0) { LOG_ERROR("Failed to load page %s:%d, due to failed to read data:%s, ret=%d, page count=%d", - file_name_.c_str(), page_num, strerror(errno), ret, file_header_->allocated_pages); + file_name_.c_str(), + page_num, + strerror(errno), + ret, + file_header_->allocated_pages); return RC::IOERR_READ; } return RC::SUCCESS; @@ -583,7 +596,7 @@ BufferPoolManager::~BufferPoolManager() { std::unordered_map tmp_bps; tmp_bps.swap(buffer_pools_); - + for (auto &iter : tmp_bps) { delete iter.second; } @@ -634,10 +647,10 @@ RC BufferPoolManager::create_file(const char *file_name) return RC::SUCCESS; } -RC BufferPoolManager::open_file(const char *_file_name, DiskBufferPool *& _bp) +RC BufferPoolManager::open_file(const char *_file_name, DiskBufferPool *&_bp) { std::string file_name(_file_name); - + if (buffer_pools_.find(file_name) != buffer_pools_.end()) { LOG_WARN("file already opened. file name=%s", _file_name); return RC::BUFFERPOOL_OPEN; diff --git a/src/observer/storage/default/disk_buffer_pool.h b/src/observer/storage/default/disk_buffer_pool.h index 33853a1649636bf6cca855fd790985e4c4b4e938..39cb85778e57c79ba2627e4f9e1bb8bb2b1bf0f9 100644 --- a/src/observer/storage/default/disk_buffer_pool.h +++ b/src/observer/storage/default/disk_buffer_pool.h @@ -53,9 +53,9 @@ struct Page { * 效率非常低,你有办法优化吗? */ struct BPFileHeader { - int32_t page_count; //! 当前文件一共有多少个页面 - int32_t allocated_pages; //! 已经分配了多少个页面 - char bitmap[0]; //! 页面分配位图, 第0个页面(就是当前页面),总是1 + int32_t page_count; //! 当前文件一共有多少个页面 + int32_t allocated_pages; //! 已经分配了多少个页面 + char bitmap[0]; //! 页面分配位图, 第0个页面(就是当前页面),总是1 /** * 能够分配的最大的页面个数,即bitmap的字节数 乘以8 @@ -63,8 +63,7 @@ struct BPFileHeader { static const int MAX_PAGE_NUM = (BP_PAGE_DATA_SIZE - sizeof(page_count) - sizeof(allocated_pages)) * 8; }; -class Frame -{ +class Frame { public: void clear_page() { @@ -85,11 +84,13 @@ public: * 标记指定页面为“脏”页。如果修改了页面的内容,则应调用此函数, * 以便该页面被淘汰出缓冲区时系统将新的页面数据写入磁盘文件 */ - void mark_dirty() { + void mark_dirty() + { dirty_ = true; } - char *data() { + char *data() + { return page_.data; } @@ -106,21 +107,20 @@ public: { return pin_count_ <= 0; } + private: friend class DiskBufferPool; - bool dirty_ = false; - unsigned int pin_count_ = 0; - unsigned long acc_time_ = 0; - int file_desc_ = -1; - Page page_; + bool dirty_ = false; + unsigned int pin_count_ = 0; + unsigned long acc_time_ = 0; + int file_desc_ = -1; + Page page_; }; -class BPFrameId -{ -public: - BPFrameId(int file_desc, PageNum page_num) : - file_desc_(file_desc), page_num_(page_num) +class BPFrameId { +public: + BPFrameId(int file_desc, PageNum page_num) : file_desc_(file_desc), page_num_(page_num) {} bool equal_to(const BPFrameId &other) const @@ -128,7 +128,7 @@ public: return file_desc_ == other.file_desc_ && page_num_ == other.page_num_; } - bool operator== (const BPFrameId &other) const + bool operator==(const BPFrameId &other) const { return this->equal_to(other); } @@ -138,16 +138,21 @@ public: return static_cast(file_desc_) << 32L | page_num_; } - int file_desc() const { return file_desc_; } - PageNum page_num() const { return page_num_; } + int file_desc() const + { + return file_desc_; + } + PageNum page_num() const + { + return page_num_; + } private: int file_desc_; PageNum page_num_; }; -class BPFrameManager -{ +class BPFrameManager { public: BPFrameManager(const char *tag); @@ -172,17 +177,24 @@ public: */ Frame *begin_purge(); - 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: class BPFrameIdHasher { public: - size_t operator() (const BPFrameId &frame_id) const { + size_t operator()(const BPFrameId &frame_id) const + { return frame_id.hash(); } }; @@ -194,8 +206,7 @@ private: FrameAllocator allocator_; }; -class BufferPoolIterator -{ +class BufferPoolIterator { public: BufferPoolIterator(); ~BufferPoolIterator(); @@ -204,13 +215,13 @@ public: bool has_next(); PageNum next(); RC reset(); + private: - common::Bitmap bitmap_; - PageNum current_page_num_ = -1; + common::Bitmap bitmap_; + PageNum current_page_num_ = -1; }; -class DiskBufferPool -{ +class DiskBufferPool { public: DiskBufferPool(BufferPoolManager &bp_manager, BPFrameManager &frame_manager); ~DiskBufferPool(); @@ -288,6 +299,7 @@ public: * 回放日志时处理page0中已被认定为不存在的page */ RC recover_page(PageNum page_num); + protected: protected: RC allocate_frame(PageNum page_num, Frame **buf); @@ -305,19 +317,18 @@ protected: private: 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; + BPFrameManager &frame_manager_; + std::string file_name_; + int file_desc_ = -1; + Frame *hdr_frame_ = nullptr; + BPFileHeader *file_header_ = nullptr; + std::set disposed_pages; private: friend class BufferPoolIterator; }; -class BufferPoolManager -{ +class BufferPoolManager { public: BufferPoolManager(); ~BufferPoolManager(); @@ -331,7 +342,7 @@ public: public: static void set_instance(BufferPoolManager *bpm); static BufferPoolManager &instance(); - + private: BPFrameManager frame_manager_{"BufPool"}; std::unordered_map buffer_pools_; diff --git a/src/observer/storage/index/bplus_tree.cpp b/src/observer/storage/index/bplus_tree.cpp index 7201f2d6c94c519e3839ceaa1d0522f1398e07c3..7ea680a75d8c96996a5ad3971077577fdf4c9921 100644 --- a/src/observer/storage/index/bplus_tree.cpp +++ b/src/observer/storage/index/bplus_tree.cpp @@ -25,22 +25,20 @@ 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; } ///////////////////////////////////////////////////////////////////////////////// IndexNodeHandler::IndexNodeHandler(const IndexFileHeader &header, Frame *frame) - : header_(header), page_num_(frame->page_num()), node_((IndexNode *)frame->data()) + : header_(header), page_num_(frame->page_num()), node_((IndexNode *)frame->data()) {} bool IndexNodeHandler::is_leaf() const @@ -97,8 +95,7 @@ std::string to_string(const IndexNodeHandler &handler) { std::stringstream ss; - ss << "PageNum:" << handler.page_num() - << ",is_leaf:" << handler.is_leaf() << "," + ss << "PageNum:" << handler.page_num() << ",is_leaf:" << handler.is_leaf() << "," << "key_num:" << handler.size() << "," << "parent:" << handler.parent_page_num() << ","; @@ -124,7 +121,7 @@ bool IndexNodeHandler::validate() const ///////////////////////////////////////////////////////////////////////////////// LeafIndexNodeHandler::LeafIndexNodeHandler(const IndexFileHeader &header, Frame *frame) - : IndexNodeHandler(header, frame), leaf_node_((LeafIndexNode *)frame->data()) + : IndexNodeHandler(header, frame), leaf_node_((LeafIndexNode *)frame->data()) {} void LeafIndexNodeHandler::init_empty() @@ -219,7 +216,7 @@ RC LeafIndexNodeHandler::move_half_to(LeafIndexNodeHandler &other, DiskBufferPoo memcpy(other.__item_at(0), this->__item_at(move_index), item_size() * (size - move_index)); other.increase_size(size - move_index); - this->increase_size(- ( size - move_index)); + this->increase_size(-(size - move_index)); return RC::SUCCESS; } RC LeafIndexNodeHandler::move_first_to_end(LeafIndexNodeHandler &other, DiskBufferPool *disk_buffer_pool) @@ -227,7 +224,7 @@ RC LeafIndexNodeHandler::move_first_to_end(LeafIndexNodeHandler &other, DiskBuff other.append(__item_at(0)); if (size() >= 1) { - memmove(__item_at(0), __item_at(1), (size() - 1) * item_size() ); + memmove(__item_at(0), __item_at(1), (size() - 1) * item_size()); } increase_size(-1); return RC::SUCCESS; @@ -247,7 +244,7 @@ RC LeafIndexNodeHandler::move_to(LeafIndexNodeHandler &other, DiskBufferPool *bp { memcpy(other.__item_at(other.size()), this->__item_at(0), this->size() * item_size()); other.increase_size(this->size()); - this->increase_size(- this->size()); + this->increase_size(-this->size()); other.set_next_page(this->next_page()); @@ -299,10 +296,9 @@ char *LeafIndexNodeHandler::__value_at(int index) const std::string to_string(const LeafIndexNodeHandler &handler, const KeyPrinter &printer) { std::stringstream ss; - ss << to_string((const IndexNodeHandler &)handler) - << ",prev page:" << handler.prev_page() + ss << to_string((const IndexNodeHandler &)handler) << ",prev page:" << handler.prev_page() << ",next page:" << handler.next_page(); - ss << ",values=[" << printer(handler.__key_at(0)) ; + ss << ",values=[" << printer(handler.__key_at(0)); for (int i = 1; i < handler.size(); i++) { ss << "," << printer(handler.__key_at(i)); } @@ -321,7 +317,10 @@ bool LeafIndexNodeHandler::validate(const KeyComparator &comparator, DiskBufferP for (int i = 1; i < node_size; i++) { if (comparator(__key_at(i - 1), __key_at(i)) >= 0) { LOG_WARN("page number = %d, invalid key order. id1=%d,id2=%d, this=%s", - page_num(), i-1, i, to_string(*this).c_str()); + page_num(), + i - 1, + i, + to_string(*this).c_str()); return false; } } @@ -334,8 +333,7 @@ bool LeafIndexNodeHandler::validate(const KeyComparator &comparator, DiskBufferP Frame *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)); + LOG_WARN("failed to fetch parent page. page num=%d, rc=%d:%s", parent_page_num, rc, strrc(rc)); return false; } @@ -343,7 +341,8 @@ bool LeafIndexNodeHandler::validate(const KeyComparator &comparator, DiskBufferP 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); + this->page_num(), + parent_page_num); bp->unpin_page(parent_frame); return false; } @@ -351,9 +350,11 @@ bool LeafIndexNodeHandler::validate(const KeyComparator &comparator, DiskBufferP if (0 != index_in_parent) { int cmp_result = comparator(__key_at(0), parent_node.key_at(index_in_parent)); if (cmp_result < 0) { - LOG_WARN("invalid leaf node. first item should be greate than or equal to parent item. " \ - "this page num=%d, parent page num=%d, index in parent=%d", - this->page_num(), parent_node.page_num(), index_in_parent); + LOG_WARN("invalid leaf node. first item should be greate than or equal to parent item. " + "this page num=%d, parent page num=%d, index in parent=%d", + this->page_num(), + parent_node.page_num(), + index_in_parent); bp->unpin_page(parent_frame); return false; } @@ -362,9 +363,11 @@ bool LeafIndexNodeHandler::validate(const KeyComparator &comparator, DiskBufferP if (index_in_parent < parent_node.size() - 1) { int cmp_result = comparator(__key_at(size() - 1), parent_node.key_at(index_in_parent + 1)); if (cmp_result >= 0) { - LOG_WARN("invalid leaf node. last item should be less than the item at the first after item in parent." \ - "this page num=%d, parent page num=%d, parent item to compare=%d", - this->page_num(), parent_node.page_num(), index_in_parent + 1); + LOG_WARN("invalid leaf node. last item should be less than the item at the first after item in parent." + "this page num=%d, parent page num=%d, parent item to compare=%d", + this->page_num(), + parent_node.page_num(), + index_in_parent + 1); bp->unpin_page(parent_frame); return false; } @@ -375,7 +378,7 @@ bool LeafIndexNodeHandler::validate(const KeyComparator &comparator, DiskBufferP ///////////////////////////////////////////////////////////////////////////////// InternalIndexNodeHandler::InternalIndexNodeHandler(const IndexFileHeader &header, Frame *frame) - : IndexNodeHandler(header, frame), internal_node_((InternalIndexNode *)frame->data()) + : IndexNodeHandler(header, frame), internal_node_((InternalIndexNode *)frame->data()) {} std::string to_string(const InternalIndexNodeHandler &node, const KeyPrinter &printer) @@ -387,8 +390,7 @@ std::string to_string(const InternalIndexNodeHandler &node, const KeyPrinter &pr << "value:" << *(PageNum *)node.__value_at(0) << "}"; for (int i = 1; i < node.size(); i++) { - ss << ",{key:" << printer(node.__key_at(i)) - << ",value:"<< *(PageNum *)node.__value_at(i) << "}"; + ss << ",{key:" << printer(node.__key_at(i)) << ",value:" << *(PageNum *)node.__value_at(i) << "}"; } ss << "]"; return ss.str(); @@ -434,7 +436,7 @@ RC InternalIndexNodeHandler::move_half_to(InternalIndexNodeHandler &other, DiskB return rc; } - increase_size(- (size - move_index)); + increase_size(-(size - move_index)); return rc; } @@ -451,10 +453,10 @@ int InternalIndexNodeHandler::min_size() const /** * lookup the first item which key <= item * @return unlike the leafNode, the return value is not the insert position, - * but only the index of child to find. + * but only the index of child to find. */ -int InternalIndexNodeHandler::lookup(const KeyComparator &comparator, const char *key, - bool *found /* = nullptr */, int *insert_position /*= nullptr */) const +int InternalIndexNodeHandler::lookup(const KeyComparator &comparator, const char *key, bool *found /* = nullptr */, + int *insert_position /*= nullptr */) const { const int size = this->size(); if (size == 0) { @@ -502,7 +504,7 @@ PageNum InternalIndexNodeHandler::value_at(int index) int InternalIndexNodeHandler::value_index(PageNum page_num) { for (int i = 0; i < size(); i++) { - if (page_num == *(PageNum*)__value_at(i)) { + if (page_num == *(PageNum *)__value_at(i)) { return i; } } @@ -526,7 +528,7 @@ RC InternalIndexNodeHandler::move_to(InternalIndexNodeHandler &other, DiskBuffer return rc; } - increase_size(- this->size()); + increase_size(-this->size()); return RC::SUCCESS; } @@ -539,7 +541,7 @@ RC InternalIndexNodeHandler::move_first_to_end(InternalIndexNodeHandler &other, } if (size() >= 1) { - memmove(__item_at(0), __item_at(1), (size() - 1) * item_size() ); + memmove(__item_at(0), __item_at(1), (size() - 1) * item_size()); } increase_size(-1); return rc; @@ -571,7 +573,10 @@ RC InternalIndexNodeHandler::copy_from(const char *items, int num, DiskBufferPoo 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)); + page_num, + this_page_num, + rc, + strrc(rc)); return rc; } IndexNodeHandler child_node(header_, frame); @@ -649,7 +654,10 @@ bool InternalIndexNodeHandler::validate(const KeyComparator &comparator, DiskBuf for (int i = 2; i < node_size; i++) { if (comparator(__key_at(i - 1), __key_at(i)) >= 0) { LOG_WARN("page number = %d, invalid key order. id1=%d,id2=%d, this=%s", - page_num(), i-1, i, to_string(*this).c_str()); + page_num(), + i - 1, + i, + to_string(*this).c_str()); return false; } } @@ -662,16 +670,18 @@ bool InternalIndexNodeHandler::validate(const KeyComparator &comparator, DiskBuf Frame *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)); + LOG_WARN( + "failed to fetch child page while validate internal page. page num=%d, rc=%d:%s", page_num, rc, strrc(rc)); } else { - IndexNodeHandler child_node(header_, child_frame); - if (child_node.parent_page_num() != this->page_num()) { - LOG_WARN("child's parent page num is invalid. child page num=%d, parent page num=%d, this page num=%d", - child_node.page_num(), child_node.parent_page_num(), this->page_num()); - result = false; - } - bp->unpin_page(child_frame); + IndexNodeHandler child_node(header_, child_frame); + if (child_node.parent_page_num() != this->page_num()) { + LOG_WARN("child's parent page num is invalid. child page num=%d, parent page num=%d, this page num=%d", + child_node.page_num(), + child_node.parent_page_num(), + this->page_num()); + result = false; + } + bp->unpin_page(child_frame); } } } @@ -696,7 +706,8 @@ bool InternalIndexNodeHandler::validate(const KeyComparator &comparator, DiskBuf 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", - this->page_num(), parent_page_num); + this->page_num(), + parent_page_num); bp->unpin_page(parent_frame); return false; } @@ -704,9 +715,11 @@ bool InternalIndexNodeHandler::validate(const KeyComparator &comparator, DiskBuf if (0 != index_in_parent) { int cmp_result = comparator(__key_at(1), parent_node.key_at(index_in_parent)); if (cmp_result < 0) { - LOG_WARN("invalid internal node. the second item should be greate than or equal to parent item. " \ - "this page num=%d, parent page num=%d, index in parent=%d", - this->page_num(), parent_node.page_num(), index_in_parent); + LOG_WARN("invalid internal node. the second item should be greate than or equal to parent item. " + "this page num=%d, parent page num=%d, index in parent=%d", + this->page_num(), + parent_node.page_num(), + index_in_parent); bp->unpin_page(parent_frame); return false; } @@ -715,9 +728,11 @@ bool InternalIndexNodeHandler::validate(const KeyComparator &comparator, DiskBuf if (index_in_parent < parent_node.size() - 1) { int cmp_result = comparator(__key_at(size() - 1), parent_node.key_at(index_in_parent + 1)); if (cmp_result >= 0) { - LOG_WARN("invalid internal node. last item should be less than the item at the first after item in parent." \ - "this page num=%d, parent page num=%d, parent item to compare=%d", - this->page_num(), parent_node.page_num(), index_in_parent + 1); + LOG_WARN("invalid internal node. last item should be less than the item at the first after item in parent." + "this page num=%d, parent page num=%d, parent item to compare=%d", + this->page_num(), + parent_node.page_num(), + index_in_parent + 1); bp->unpin_page(parent_frame); return false; } @@ -734,8 +749,8 @@ RC BplusTreeHandler::sync() return disk_buffer_pool_->flush_all_pages(); } -RC BplusTreeHandler::create(const char *file_name, AttrType attr_type, int attr_length, - int internal_max_size /* = -1*/, int leaf_max_size /* = -1 */) +RC BplusTreeHandler::create(const char *file_name, AttrType attr_type, int attr_length, int internal_max_size /* = -1*/, + int leaf_max_size /* = -1 */) { BufferPoolManager &bpm = BufferPoolManager::instance(); RC rc = bpm.create_file(file_name); @@ -763,7 +778,9 @@ RC BplusTreeHandler::create(const char *file_name, AttrType attr_type, int attr_ if (header_frame->page_num() != FIRST_INDEX_PAGE) { LOG_WARN("header page num should be %d but got %d. is it a new file : %s", - FIRST_INDEX_PAGE, header_frame->page_num(), file_name); + FIRST_INDEX_PAGE, + header_frame->page_num(), + file_name); bpm.close_file(file_name); return RC::INTERNAL; } @@ -853,7 +870,7 @@ RC BplusTreeHandler::close() { if (disk_buffer_pool_ != nullptr) { - disk_buffer_pool_->close_file(); // TODO + disk_buffer_pool_->close_file(); // TODO delete mem_pool_item_; mem_pool_item_ = nullptr; @@ -924,7 +941,7 @@ RC BplusTreeHandler::print_tree() LOG_WARN("failed to fetch page. page id=%d, rc=%d:%s", page_num, rc, strrc(rc)); return rc; } - + IndexNodeHandler node(file_header_, frame); if (node.is_leaf()) { rc = print_leaf(frame); @@ -942,7 +959,7 @@ RC BplusTreeHandler::print_leafs() } Frame *frame; - + RC rc = left_most_page(frame); if (rc != RC::SUCCESS) { LOG_WARN("failed to get left most page. rc=%d:%s", rc, strrc(rc)); @@ -1015,7 +1032,9 @@ bool BplusTreeHandler::validate_leaf_link() LeafIndexNodeHandler leaf_node(file_header_, frame); if (leaf_node.prev_page() != prev_page_num) { LOG_WARN("invalid page. current_page_num=%d, prev page num should be %d but got %d", - frame->page_num(), prev_page_num, leaf_node.prev_page()); + frame->page_num(), + prev_page_num, + leaf_node.prev_page()); return false; } PageNum next_page_num = leaf_node.next_page(); @@ -1037,7 +1056,9 @@ bool BplusTreeHandler::validate_leaf_link() LeafIndexNodeHandler leaf_node(file_header_, frame); if (leaf_node.prev_page() != prev_page_num) { LOG_WARN("invalid page. current_page_num=%d, prev page num should be %d but got %d", - frame->page_num(), prev_page_num, leaf_node.prev_page()); + frame->page_num(), + prev_page_num, + leaf_node.prev_page()); result = false; } if (key_comparator_(prev_key, leaf_node.key_at(0)) >= 0) { @@ -1087,33 +1108,24 @@ bool BplusTreeHandler::is_empty() const RC BplusTreeHandler::find_leaf(const char *key, Frame *&frame) { return find_leaf_internal( - [&](InternalIndexNodeHandler &internal_node) { - return internal_node.value_at(internal_node.lookup(key_comparator_, key)); - }, - frame); + [&](InternalIndexNodeHandler &internal_node) { + return internal_node.value_at(internal_node.lookup(key_comparator_, key)); + }, + frame); } RC BplusTreeHandler::left_most_page(Frame *&frame) { - return find_leaf_internal( - [&](InternalIndexNodeHandler &internal_node) { - return internal_node.value_at(0); - }, - frame - ); + return find_leaf_internal([&](InternalIndexNodeHandler &internal_node) { return internal_node.value_at(0); }, frame); } RC BplusTreeHandler::right_most_page(Frame *&frame) { return find_leaf_internal( - [&](InternalIndexNodeHandler &internal_node) { - return internal_node.value_at(internal_node.size() - 1); - }, - frame - ); + [&](InternalIndexNodeHandler &internal_node) { return internal_node.value_at(internal_node.size() - 1); }, frame); } -RC BplusTreeHandler::find_leaf_internal(const std::function &child_page_getter, - Frame *&frame) +RC BplusTreeHandler::find_leaf_internal( + const std::function &child_page_getter, Frame *&frame) { if (is_empty()) { return RC::EMPTY; @@ -1160,7 +1172,7 @@ RC BplusTreeHandler::insert_entry_into_leaf_node(Frame *frame, const char *key, return RC::SUCCESS; } - Frame * new_frame = nullptr; + Frame *new_frame = nullptr; RC rc = split(frame, new_frame); if (rc != RC::SUCCESS) { LOG_WARN("failed to split leaf node. rc=%d:%s", rc, strrc(rc)); @@ -1175,7 +1187,7 @@ RC BplusTreeHandler::insert_entry_into_leaf_node(Frame *frame, const char *key, PageNum next_page_num = new_index_node.next_page(); if (next_page_num != BP_INVALID_PAGE_NUM) { - Frame * next_frame; + Frame *next_frame; rc = disk_buffer_pool_->get_this_page(next_page_num, &next_frame); if (rc != RC::SUCCESS) { LOG_WARN("failed to fetch next page. page num=%d, rc=%d:%s", next_page_num, rc, strrc(rc)); @@ -1226,7 +1238,7 @@ RC BplusTreeHandler::insert_entry_into_parent(Frame *frame, Frame *new_frame, co disk_buffer_pool_->unpin_page(new_frame); file_header_.root_page = root_frame->page_num(); - update_root_page_num(); // TODO + update_root_page_num(); // TODO root_frame->mark_dirty(); disk_buffer_pool_->unpin_page(root_frame); @@ -1259,28 +1271,28 @@ RC BplusTreeHandler::insert_entry_into_parent(Frame *frame, Frame *new_frame, co } else { // we should split the node and insert the entry and then insert new entry to current node's parent - Frame * new_parent_frame; + Frame *new_parent_frame; rc = split(parent_frame, new_parent_frame); if (rc != RC::SUCCESS) { - LOG_WARN("failed to split internal node. rc=%d:%s", rc, strrc(rc)); - disk_buffer_pool_->unpin_page(frame); - disk_buffer_pool_->unpin_page(new_frame); - disk_buffer_pool_->unpin_page(parent_frame); + LOG_WARN("failed to split internal node. rc=%d:%s", rc, strrc(rc)); + disk_buffer_pool_->unpin_page(frame); + disk_buffer_pool_->unpin_page(new_frame); + disk_buffer_pool_->unpin_page(parent_frame); } else { - // insert into left or right ? decide by key compare result - InternalIndexNodeHandler new_node(file_header_, new_parent_frame); - if (key_comparator_(key, new_node.key_at(0)) > 0) { - new_node.insert(key, new_frame->page_num(), key_comparator_); + // insert into left or right ? decide by key compare result + InternalIndexNodeHandler new_node(file_header_, new_parent_frame); + if (key_comparator_(key, new_node.key_at(0)) > 0) { + new_node.insert(key, new_frame->page_num(), key_comparator_); new_node_handler.set_parent_page_num(new_node.page_num()); - } else { - node.insert(key, new_frame->page_num(), key_comparator_); + } else { + node.insert(key, new_frame->page_num(), key_comparator_); new_node_handler.set_parent_page_num(node.page_num()); - } + } - disk_buffer_pool_->unpin_page(frame); - disk_buffer_pool_->unpin_page(new_frame); - - rc = insert_entry_into_parent(parent_frame, new_parent_frame, new_node.key_at(0)); + disk_buffer_pool_->unpin_page(frame); + disk_buffer_pool_->unpin_page(new_frame); + + rc = insert_entry_into_parent(parent_frame, new_parent_frame, new_node.key_at(0)); } } } @@ -1318,7 +1330,7 @@ RC BplusTreeHandler::split(Frame *frame, Frame *&new_frame) RC BplusTreeHandler::update_root_page_num() { - Frame * header_frame; + Frame *header_frame; RC rc = disk_buffer_pool_->get_this_page(FIRST_INDEX_PAGE, &header_frame); if (rc != RC::SUCCESS) { LOG_WARN("failed to fetch header page. rc=%d:%s", rc, strrc(rc)); @@ -1332,7 +1344,6 @@ RC BplusTreeHandler::update_root_page_num() return rc; } - RC BplusTreeHandler::create_new_tree(const char *key, const RID *rid) { RC rc = RC::SUCCESS; @@ -1423,7 +1434,7 @@ RC BplusTreeHandler::insert_entry(const char *user_key, const RID *rid) RC BplusTreeHandler::get_entry(const char *user_key, int key_len, std::list &rids) { BplusTreeScanner scanner(*this); - RC rc = scanner.open(user_key, key_len, true/*left_inclusive*/, user_key, key_len, true/*right_inclusive*/); + RC rc = scanner.open(user_key, key_len, true /*left_inclusive*/, user_key, key_len, true /*right_inclusive*/); if (rc != RC::SUCCESS) { LOG_WARN("failed to open scanner. rc=%d:%s", rc, strrc(rc)); return rc; @@ -1460,7 +1471,7 @@ RC BplusTreeHandler::adjust_root(Frame *root_frame) InternalIndexNodeHandler internal_node(file_header_, root_frame); const PageNum child_page_num = internal_node.value_at(0); - Frame * child_frame; + Frame *child_frame; RC rc = disk_buffer_pool_->get_this_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)); @@ -1470,7 +1481,7 @@ RC BplusTreeHandler::adjust_root(Frame *root_frame) IndexNodeHandler child_node(file_header_, child_frame); child_node.set_parent_page_num(BP_INVALID_PAGE_NUM); disk_buffer_pool_->unpin_page(child_frame); - + file_header_.root_page = child_page_num; } @@ -1502,7 +1513,7 @@ RC BplusTreeHandler::coalesce_or_redistribute(Frame *frame) return RC::SUCCESS; } - Frame * parent_frame; + Frame *parent_frame; RC rc = disk_buffer_pool_->get_this_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)); @@ -1514,7 +1525,9 @@ RC BplusTreeHandler::coalesce_or_redistribute(Frame *frame) int index = parent_index_node.lookup(key_comparator_, index_node.key_at(index_node.size() - 1)); if (parent_index_node.value_at(index) != frame->page_num()) { LOG_ERROR("lookup return an invalid value. index=%d, this page num=%d, but got %d", - index, frame->page_num(), parent_index_node.value_at(index)); + index, + frame->page_num(), + parent_index_node.value_at(index)); } PageNum neighbor_page_num; if (index == 0) { @@ -1523,7 +1536,7 @@ RC BplusTreeHandler::coalesce_or_redistribute(Frame *frame) neighbor_page_num = parent_index_node.value_at(index - 1); } - Frame * neighbor_frame; + Frame *neighbor_frame; rc = disk_buffer_pool_->get_this_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)); @@ -1554,11 +1567,11 @@ RC BplusTreeHandler::coalesce(Frame *neighbor_frame, Frame *frame, Frame *parent 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 } @@ -1586,9 +1599,9 @@ RC BplusTreeHandler::coalesce(Frame *neighbor_frame, Frame *frame, Frame *parent rc = disk_buffer_pool_->get_this_page(next_right_page_num, &next_right_frame); if (rc != RC::SUCCESS) { LOG_WARN("failed to fetch next right page. page number:%d. rc=%d:%s", next_right_page_num, rc, strrc(rc)); - disk_buffer_pool_->unpin_page(frame); - disk_buffer_pool_->unpin_page(neighbor_frame); - disk_buffer_pool_->unpin_page(parent_frame); + disk_buffer_pool_->unpin_page(frame); + disk_buffer_pool_->unpin_page(neighbor_frame); + disk_buffer_pool_->unpin_page(parent_frame); return rc; } @@ -1596,7 +1609,6 @@ RC BplusTreeHandler::coalesce(Frame *neighbor_frame, Frame *frame, Frame *parent next_right_node.set_prev_page(left_node.page_num()); disk_buffer_pool_->unpin_page(next_right_frame); } - } PageNum right_page_num = right_frame->page_num(); @@ -1613,8 +1625,7 @@ RC BplusTreeHandler::redistribute(Frame *neighbor_frame, Frame *frame, Frame *pa 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()); + LOG_ERROR("got invalid nodes. neighbor node size %d, this node size %d", neighbor_node.size(), node.size()); } if (index == 0) { // the neighbor is at right @@ -1698,8 +1709,8 @@ 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_) { @@ -1708,14 +1719,14 @@ RC BplusTreeScanner::open(const char *left_user_key, int left_len, bool left_inc } inited_ = true; - + // 校验输入的键值是否是合法范围 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); - if (result > 0 || // left < right - // left == right but is (left,right)/[left,right) or (left,right] - (result == 0 && (left_inclusive == false || right_inclusive == false))) { + if (result > 0 || // left < right + // left == right but is (left,right)/[left,right) or (left,right] + (result == 0 && (left_inclusive == false || right_inclusive == false))) { return RC::INVALID_ARGUMENT; } } @@ -1734,14 +1745,14 @@ RC BplusTreeScanner::open(const char *left_user_key, int left_len, bool left_inc char *fixed_left_key = const_cast(left_user_key); if (tree_handler_.file_header_.attr_type == CHARS) { bool should_inclusive_after_fix = false; - rc = fix_user_key(left_user_key, left_len, true/*greater*/, &fixed_left_key, &should_inclusive_after_fix); + rc = fix_user_key(left_user_key, left_len, true /*greater*/, &fixed_left_key, &should_inclusive_after_fix); if (rc != RC::SUCCESS) { LOG_WARN("failed to fix left user key. rc=%s", strrc(rc)); return rc; } if (should_inclusive_after_fix) { - left_inclusive = true; + left_inclusive = true; } } @@ -1766,10 +1777,10 @@ RC BplusTreeScanner::open(const char *left_user_key, int left_len, bool left_inc int left_index = left_node.lookup(tree_handler_.key_comparator_, left_key); tree_handler_.free_key(left_key); // lookup 返回的是适合插入的位置,还需要判断一下是否在合适的边界范围内 - if (left_index >= left_node.size()) { // 超出了当前页,就需要向后移动一个位置 + if (left_index >= left_node.size()) { // 超出了当前页,就需要向后移动一个位置 const PageNum next_page_num = left_node.next_page(); - if (next_page_num == BP_INVALID_PAGE_NUM) { // 这里已经是最后一页,说明当前扫描,没有数据 - return RC::SUCCESS; + if (next_page_num == BP_INVALID_PAGE_NUM) { // 这里已经是最后一页,说明当前扫描,没有数据 + return RC::SUCCESS; } tree_handler_.disk_buffer_pool_->unpin_page(left_frame_); @@ -1800,14 +1811,14 @@ RC BplusTreeScanner::open(const char *left_user_key, int left_len, bool left_inc 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); + rc = fix_user_key(right_user_key, right_len, false /*want_greater*/, &fixed_right_key, &should_include_after_fix); if (rc != RC::SUCCESS) { LOG_WARN("failed to fix right user key. rc=%s", strrc(rc)); return rc; } if (should_include_after_fix) { - right_inclusive = true; + right_inclusive = true; } } if (right_inclusive) { @@ -1860,8 +1871,7 @@ RC BplusTreeScanner::open(const char *left_user_key, int left_len, bool left_inc // 判断是否左边界比右边界要靠后 // 两个边界最多会多一页 // 查找不存在的元素,或者不存在的范围数据时,可能会存在这个问题 - if (left_frame_->page_num() == right_frame_->page_num() && - iter_index_ > end_index_) { + if (left_frame_->page_num() == right_frame_->page_num() && iter_index_ > end_index_) { end_index_ = -1; } else { LeafIndexNodeHandler left_node(tree_handler_.file_header_, left_frame_); @@ -1882,8 +1892,7 @@ RC BplusTreeScanner::next_entry(RID *rid) LeafIndexNodeHandler node(tree_handler_.file_header_, left_frame_); memcpy(rid, node.value_at(iter_index_), sizeof(*rid)); - if (left_frame_->page_num() == right_frame_->page_num() && - iter_index_ == end_index_) { + if (left_frame_->page_num() == right_frame_->page_num() && iter_index_ == end_index_) { end_index_ = -1; return RC::SUCCESS; } @@ -1904,7 +1913,7 @@ RC BplusTreeScanner::next_entry(RID *rid) } else { rc = tree_handler_.disk_buffer_pool_->get_this_page(page_num, &left_frame_); if (rc != RC::SUCCESS) { - left_frame_ = nullptr; + left_frame_ = nullptr; LOG_WARN("failed to fetch next page. page num=%d, rc=%d:%s", page_num, rc, strrc(rc)); return rc; } @@ -1913,7 +1922,8 @@ RC BplusTreeScanner::next_entry(RID *rid) } } else if (end_index_ != -1) { LOG_WARN("should have more pages but not. left page=%d, right page=%d", - left_frame_->page_num(), right_frame_->page_num()); + left_frame_->page_num(), + right_frame_->page_num()); rc = RC::INTERNAL; } return rc; @@ -1935,8 +1945,8 @@ RC BplusTreeScanner::close() return RC::SUCCESS; } -RC BplusTreeScanner::fix_user_key(const char *user_key, int key_len, bool want_greater, - char **fixed_key, bool *should_inclusive) +RC BplusTreeScanner::fix_user_key( + const char *user_key, int key_len, bool want_greater, char **fixed_key, bool *should_inclusive) { if (nullptr == fixed_key || nullptr == should_inclusive) { return RC::INVALID_ARGUMENT; @@ -1945,11 +1955,11 @@ RC BplusTreeScanner::fix_user_key(const char *user_key, int key_len, bool want_g // 这里很粗暴,变长字段才需要做调整,其它默认都不需要做调整 assert(tree_handler_.file_header_.attr_type == CHARS); assert(strlen(user_key) >= static_cast(key_len)); - + *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; } @@ -1980,7 +1990,7 @@ RC BplusTreeScanner::fix_user_key(const char *user_key, int key_len, bool want_g if (want_greater) { key_buf[attr_length - 1]++; } - + *fixed_key = key_buf; return RC::SUCCESS; } diff --git a/src/observer/storage/index/bplus_tree.h b/src/observer/storage/index/bplus_tree.h index cd946462889e330cb032a60b5f806438b0ea2947..7631b5eda0b09b4edc244cba1c20e9bc94bba43e 100644 --- a/src/observer/storage/index/bplus_tree.h +++ b/src/observer/storage/index/bplus_tree.h @@ -29,8 +29,7 @@ See the Mulan PSL v2 for more details. */ #define EMPTY_RID_PAGE_NUM -1 #define EMPTY_RID_SLOT_NUM -1 -class AttrComparator -{ +class AttrComparator { public: void init(AttrType type, int length) { @@ -38,46 +37,49 @@ public: attr_length_ = length; } - int attr_length() const { + int attr_length() const + { return attr_length_; } - int operator()(const char *v1, const char *v2) const { + int operator()(const char *v1, const char *v2) const + { switch (attr_type_) { - case INTS: { - return compare_int((void *)v1, (void *)v2); - } - break; - case FLOATS: { - return compare_float((void *)v1, (void *)v2); - } - case CHARS: { - return compare_string((void *)v1, attr_length_, (void *)v2, attr_length_); - } - default:{ - LOG_ERROR("unknown attr type. %d", attr_type_); - abort(); - } + case INTS: { + return compare_int((void *)v1, (void *)v2); + } break; + case FLOATS: { + return compare_float((void *)v1, (void *)v2); + } + case CHARS: { + return compare_string((void *)v1, attr_length_, (void *)v2, attr_length_); + } + default: { + LOG_ERROR("unknown attr type. %d", attr_type_); + abort(); + } } } + private: AttrType attr_type_; int attr_length_; }; -class KeyComparator -{ +class KeyComparator { public: void init(AttrType type, int length) { attr_comparator_.init(type, length); } - const AttrComparator &attr_comparator() const { + const AttrComparator &attr_comparator() const + { return attr_comparator_; } - int operator() (const char *v1, const char *v2) const { + int operator()(const char *v1, const char *v2) const + { int result = attr_comparator_(v1, v2); if (result != 0) { return result; @@ -92,8 +94,7 @@ private: AttrComparator attr_comparator_; }; -class AttrPrinter -{ +class AttrPrinter { public: void init(AttrType type, int length) { @@ -101,53 +102,56 @@ public: attr_length_ = length; } - int attr_length() const { + int attr_length() const + { return attr_length_; } - std::string operator()(const char *v) const { + std::string operator()(const char *v) const + { switch (attr_type_) { - case INTS: { - return std::to_string(*(int*)v); - } - break; - case FLOATS: { - return std::to_string(*(float*)v); - } - case CHARS: { - std::string str; - for (int i = 0; i < attr_length_; i++) { - if (v[i] == 0) { - break; - } - str.push_back(v[i]); + case INTS: { + return std::to_string(*(int *)v); + } break; + case FLOATS: { + return std::to_string(*(float *)v); + } + case CHARS: { + std::string str; + for (int i = 0; i < attr_length_; i++) { + if (v[i] == 0) { + break; + } + str.push_back(v[i]); + } + return str; + } + default: { + LOG_ERROR("unknown attr type. %d", attr_type_); + abort(); } - return str; - } - default:{ - LOG_ERROR("unknown attr type. %d", attr_type_); - abort(); - } } } + private: AttrType attr_type_; int attr_length_; }; -class KeyPrinter -{ +class KeyPrinter { public: void init(AttrType type, int length) { attr_printer_.init(type, length); } - const AttrPrinter &attr_printer() const { + const AttrPrinter &attr_printer() const + { return attr_printer_; } - std::string operator() (const char *v) const { + std::string operator()(const char *v) const + { std::stringstream ss; ss << "{key:" << attr_printer_(v) << ","; @@ -171,11 +175,11 @@ struct 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) + 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() @@ -220,7 +224,7 @@ struct IndexNode { */ struct LeafIndexNode : public IndexNode { static constexpr int HEADER_SIZE = IndexNode::HEADER_SIZE + 8; - + PageNum prev_brother; PageNum next_brother; /** @@ -232,7 +236,7 @@ struct LeafIndexNode : public IndexNode { /** * internal page of bplus tree * storage format: - * | common header | + * | common header | * | key(0),page_id(0) | key(1), page_id(1) | ... | key(n), page_id(n) | * * the first key is ignored(key0). @@ -254,12 +258,12 @@ public: void init_empty(bool leaf); bool is_leaf() const; - int key_size() const; - int value_size() const; - int item_size() const; + int key_size() const; + int value_size() const; + int item_size() const; void increase_size(int n); - int size() const; + int size() const; void set_parent_page_num(PageNum page_num); PageNum parent_page_num() const; @@ -276,7 +280,7 @@ protected: }; class LeafIndexNodeHandler : public IndexNodeHandler { -public: +public: LeafIndexNodeHandler(const IndexFileHeader &header, Frame *frame); void init_empty(); @@ -297,10 +301,10 @@ public: 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); + 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); /** * move all items to left page */ @@ -312,6 +316,7 @@ public: bool validate(const KeyComparator &comparator, DiskBufferPool *bp) const; friend std::string to_string(const LeafIndexNodeHandler &handler, const KeyPrinter &printer); + private: char *__item_at(int index) const; char *__key_at(int index) const; @@ -332,14 +337,14 @@ public: 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); + 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); @@ -348,9 +353,9 @@ public: * 如果想要返回插入位置,就提供 `insert_position` 参数 * NOTE: 查找效率不高,你可以优化它吗? */ - 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; + int max_size() const; int min_size() const; @@ -362,6 +367,7 @@ public: bool validate(const KeyComparator &comparator, DiskBufferPool *bp) const; friend std::string to_string(const InternalIndexNodeHandler &handler, const KeyPrinter &printer); + private: RC copy_from(const char *items, int num, DiskBufferPool *disk_buffer_pool); RC append(const char *item, DiskBufferPool *bp); @@ -385,8 +391,8 @@ 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的索引文件。 @@ -449,11 +455,9 @@ protected: RC find_leaf(const char *key, Frame *&frame); RC left_most_page(Frame *&frame); RC right_most_page(Frame *&frame); - RC find_leaf_internal(const std::function &child_page_getter, - Frame *&frame); + RC find_leaf_internal(const std::function &child_page_getter, Frame *&frame); - RC insert_into_parent( - PageNum parent_page, Frame *left_frame, const char *pkey, Frame &right_frame); + RC insert_into_parent(PageNum parent_page, Frame *left_frame, const char *pkey, Frame &right_frame); RC delete_entry_internal(Frame *leaf_frame, const char *key); @@ -477,14 +481,15 @@ protected: private: char *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; IndexFileHeader file_header_; KeyComparator key_comparator_; - KeyPrinter key_printer_; + KeyPrinter key_printer_; common::MemPoolItem *mem_pool_item_ = nullptr; @@ -507,8 +512,8 @@ public: * @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); @@ -518,18 +523,18 @@ private: /** * 如果key的类型是CHARS, 扩展或缩减user_key的大小刚好是schema中定义的大小 */ - RC fix_user_key(const char *user_key, int key_len, bool want_greater, - char **fixed_key, bool *should_inclusive); + RC fix_user_key(const char *user_key, int key_len, bool want_greater, char **fixed_key, bool *should_inclusive); + private: bool inited_ = false; BplusTreeHandler &tree_handler_; /// 使用左右叶子节点和位置来表示扫描的起始位置和终止位置 /// 起始位置和终止位置都是有效的数据 - Frame * left_frame_ = nullptr; - Frame * right_frame_ = nullptr; - int iter_index_ = -1; - int end_index_ = -1; // use -1 for end of scan + Frame *left_frame_ = nullptr; + Frame *right_frame_ = nullptr; + int iter_index_ = -1; + int end_index_ = -1; // use -1 for end of scan }; #endif //__OBSERVER_STORAGE_COMMON_INDEX_MANAGER_H_ diff --git a/src/observer/storage/index/bplus_tree_index.cpp b/src/observer/storage/index/bplus_tree_index.cpp index cbaef5cdefc04aaa253c69763f3bd0e03bf62e81..b9eb7ba64668a41356ff39b9eb3a1e0efc2c6671 100644 --- a/src/observer/storage/index/bplus_tree_index.cpp +++ b/src/observer/storage/index/bplus_tree_index.cpp @@ -79,8 +79,7 @@ RC BplusTreeIndex::open(const char *file_name, const IndexMeta &index_meta, cons RC BplusTreeIndex::close() { if (inited_) { - LOG_INFO("Begin to close index, index:%s, field:%s", - index_meta_.name(), index_meta_.field()); + LOG_INFO("Begin to close index, index:%s, field:%s", index_meta_.name(), index_meta_.field()); index_handler_.close(); inited_ = false; } @@ -98,8 +97,8 @@ RC BplusTreeIndex::delete_entry(const char *record, const RID *rid) return index_handler_.delete_entry(record + field_meta_.offset(), rid); } -IndexScanner *BplusTreeIndex::create_scanner(const char *left_key, int left_len, bool left_inclusive, - const char *right_key, int right_len, bool right_inclusive) +IndexScanner *BplusTreeIndex::create_scanner( + const char *left_key, int left_len, bool left_inclusive, const char *right_key, int right_len, bool right_inclusive) { BplusTreeIndexScanner *index_scanner = new BplusTreeIndexScanner(index_handler_); RC rc = index_scanner->open(left_key, left_len, left_inclusive, right_key, right_len, right_inclusive); @@ -125,8 +124,8 @@ 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) +RC BplusTreeIndexScanner::open( + const char *left_key, int left_len, bool left_inclusive, const char *right_key, int right_len, bool right_inclusive) { return tree_scanner_.open(left_key, left_len, left_inclusive, right_key, right_len, right_inclusive); } diff --git a/src/observer/storage/index/bplus_tree_index.h b/src/observer/storage/index/bplus_tree_index.h index c2e66f18227d9f4378287bb2b649a2dd805b7623..4d2e544babab5dc296197aa1833115e3347e614f 100644 --- a/src/observer/storage/index/bplus_tree_index.h +++ b/src/observer/storage/index/bplus_tree_index.h @@ -33,8 +33,8 @@ public: /** * 扫描指定范围的数据 */ - IndexScanner *create_scanner(const char *left_key, int left_len, bool left_inclusive, - const char *right_key, int right_len, bool right_inclusive) override; + IndexScanner *create_scanner(const char *left_key, int left_len, bool left_inclusive, const char *right_key, + int right_len, bool right_inclusive) override; RC sync() override; @@ -51,8 +51,9 @@ public: RC next_entry(RID *rid) override; RC destroy() override; - RC open(const char *left_key, int left_len, bool left_inclusive, - const char *right_key, int right_len, bool right_inclusive); + RC open(const char *left_key, int left_len, bool left_inclusive, const char *right_key, int right_len, + bool right_inclusive); + private: BplusTreeScanner tree_scanner_; }; diff --git a/src/observer/storage/index/index.h b/src/observer/storage/index/index.h index 165b8414887a199476863755a737f550e1a99583..b8feb6ce2c061580b2feb1e81a71a6eeb740147c 100644 --- a/src/observer/storage/index/index.h +++ b/src/observer/storage/index/index.h @@ -46,8 +46,8 @@ public: virtual RC insert_entry(const char *record, const RID *rid) = 0; virtual RC delete_entry(const char *record, const RID *rid) = 0; - virtual IndexScanner *create_scanner(const char *left_key, int left_len, bool left_inclusive, - const char *right_key, int right_len, bool right_inclusive) = 0; + virtual IndexScanner *create_scanner(const char *left_key, int left_len, bool left_inclusive, const char *right_key, + int right_len, bool right_inclusive) = 0; virtual RC sync() = 0; diff --git a/src/observer/storage/persist/persist.cpp b/src/observer/storage/persist/persist.cpp index 643761def820874768ed6a087b180c2596a5c579..30e963829ee72cae3c4adf1d14ce7bbcad46a911 100644 --- a/src/observer/storage/persist/persist.cpp +++ b/src/observer/storage/persist/persist.cpp @@ -35,7 +35,7 @@ RC PersistHandler::create_file(const char *file_name) } else if (!file_name_.empty()) { LOG_ERROR("Failed to create %s, because a file is already bound.", file_name); rc = RC::FILE_BOUND; - } else if (access(file_name, F_OK) != -1){ + } else if (access(file_name, F_OK) != -1) { LOG_WARN("Failed to create %s, because file already exist.", file_name); rc = RC::FILE_EXIST; } else { @@ -43,7 +43,7 @@ RC PersistHandler::create_file(const char *file_name) fd = open(file_name, O_RDWR | O_CREAT | O_EXCL, S_IREAD | S_IWRITE); if (fd < 0) { LOG_ERROR("Failed to create %s, due to %s.", file_name, strerror(errno)); - rc = RC::FILE_CREATE; + rc = RC::FILE_CREATE; } else { file_name_ = file_name; close(fd); @@ -61,7 +61,7 @@ RC PersistHandler::open_file(const char *file_name) if (file_name == nullptr) { if (file_name_.empty()) { LOG_ERROR("Failed to open file, because no file name."); - rc = RC::FILE_NAME; + rc = RC::FILE_NAME; } else { if ((fd = open(file_name_.c_str(), O_RDWR)) < 0) { LOG_ERROR("Failed to open file %s, because %s.", file_name_.c_str(), strerror(errno)); @@ -86,7 +86,7 @@ RC PersistHandler::open_file(const char *file_name) } } } - + return rc; } @@ -143,7 +143,11 @@ RC PersistHandler::write_file(int size, const char *data, int64_t *out_size) } else { 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); + LOG_ERROR("Failed to write %d:%s due to %s. Write size: %lld", + file_desc_, + file_name_.c_str(), + strerror(errno), + write_size); rc = RC::FILE_WRITE; } if (out_size != nullptr) { @@ -154,7 +158,6 @@ RC PersistHandler::write_file(int size, const char *data, int64_t *out_size) return rc; } - RC PersistHandler::write_at(uint64_t offset, int size, const char *data, int64_t *out_size) { RC rc = RC::SUCCESS; @@ -166,12 +169,21 @@ RC PersistHandler::write_at(uint64_t offset, int size, const char *data, int64_t rc = RC::FILE_NOT_OPENED; } 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)); + LOG_ERROR("Failed to write %lld of %d:%s due to failed to seek %s.", + 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); + 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); rc = RC::FILE_WRITE; } if (out_size != nullptr) { @@ -194,12 +206,17 @@ 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); + LOG_ERROR("Failed to append file %d:%s due to %s. Write size: %lld", + file_desc_, + file_name_.c_str(), + strerror(errno), + write_size); rc = RC::FILE_WRITE; } if (out_size != nullptr) { @@ -245,7 +262,11 @@ RC PersistHandler::read_at(uint64_t offset, int size, char *data, int64_t *out_s rc = RC::FILE_NOT_OPENED; } 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)); + LOG_ERROR("Failed to read %llu of %d:%s due to failed to seek %s.", + offset, + file_desc_, + file_name_.c_str(), + strerror(errno)); return RC::FILE_SEEK; } else { int64_t read_size = 0; diff --git a/src/observer/storage/persist/persist.h b/src/observer/storage/persist/persist.h index 306612d3334f19df55145858e697581b874af558..6b65b6c156a36007d87f57e2f08f8dbf95bb6a9c 100644 --- a/src/observer/storage/persist/persist.h +++ b/src/observer/storage/persist/persist.h @@ -24,8 +24,7 @@ See the Mulan PSL v2 for more details. */ #include "rc.h" -class PersistHandler -{ +class PersistHandler { public: PersistHandler(); ~PersistHandler(); @@ -61,8 +60,8 @@ public: RC seek(uint64_t offset); private: - std::string file_name_; - int file_desc_ = -1; + std::string file_name_; + int file_desc_ = -1; }; #endif //__OBSERVER_STORAGE_PERSIST_HANDLER_H_ diff --git a/src/observer/storage/record/record.h b/src/observer/storage/record/record.h index 28c6faf0397ad81956371b5079cbcec16b382f6c..1a0be721eaa2011a5307c25678415607cec1ac00 100644 --- a/src/observer/storage/record/record.h +++ b/src/observer/storage/record/record.h @@ -32,8 +32,7 @@ struct RID { // bool valid; // true means a valid record RID() = default; - RID(const PageNum _page_num, const SlotNum _slot_num) - : page_num(_page_num), slot_num(_slot_num) + RID(const PageNum _page_num, const SlotNum _slot_num) : page_num(_page_num), slot_num(_slot_num) {} const std::string to_string() const @@ -82,25 +81,46 @@ struct RID { } }; -class Record -{ +class Record { public: Record() = default; ~Record() = default; - void set_data(char *data) { this->data_ = data; } - char *data() { return this->data_; } - const char *data() const { return this->data_; } + void set_data(char *data) + { + this->data_ = data; + } + char *data() + { + return this->data_; + } + const char *data() const + { + return this->data_; + } - void set_rid(const RID &rid) { this->rid_ = rid; } - void set_rid(const PageNum page_num, const SlotNum slot_num) { this->rid_.page_num = page_num; this->rid_.slot_num = slot_num; } - RID & rid() { return rid_; } - const RID &rid() const { return rid_; }; + void set_rid(const RID &rid) + { + this->rid_ = rid; + } + void set_rid(const PageNum page_num, const SlotNum slot_num) + { + this->rid_.page_num = page_num; + this->rid_.slot_num = slot_num; + } + RID &rid() + { + return rid_; + } + const RID &rid() const + { + return rid_; + }; private: - RID rid_; + RID rid_; // the data buffer // record will not release the memory - char * data_ = nullptr; + char *data_ = nullptr; }; diff --git a/src/observer/storage/record/record_manager.cpp b/src/observer/storage/record/record_manager.cpp index 4e9cae7409bc89050978eb141022be9501731453..db21d96007abe97fb02c7f4876dd1e05280b3143 100644 --- a/src/observer/storage/record/record_manager.cpp +++ b/src/observer/storage/record/record_manager.cpp @@ -150,7 +150,7 @@ RC RecordPageHandler::init_empty_page(DiskBufferPool &buffer_pool, PageNum page_ bitmap_ = frame_->data() + page_fix_size(); memset(bitmap_, 0, page_bitmap_size(page_header_->record_capacity)); - + if ((ret = buffer_pool.flush_page(*frame_)) != RC::SUCCESS) { LOG_ERROR("Failed to flush page header %d:%d.", page_num); return ret; @@ -225,15 +225,14 @@ RC RecordPageHandler::recover_insert_record(const char *data, RID *rid) RC RecordPageHandler::update_record(const Record *rec) { if (rec->rid().slot_num >= page_header_->record_capacity) { - LOG_ERROR("Invalid slot_num %d, exceed page's record capacity, page_num %d.", - rec->rid().slot_num, frame_->page_num()); + LOG_ERROR( + "Invalid slot_num %d, exceed page's record capacity, page_num %d.", rec->rid().slot_num, frame_->page_num()); return RC::INVALID_ARGUMENT; } Bitmap bitmap(bitmap_, page_header_->record_capacity); if (!bitmap.get_bit(rec->rid().slot_num)) { - LOG_ERROR("Invalid slot_num %d, slot is empty, page_num %d.", - rec->rid().slot_num, frame_->page_num()); + LOG_ERROR("Invalid slot_num %d, slot is empty, page_num %d.", rec->rid().slot_num, frame_->page_num()); return RC::RECORD_RECORD_NOT_EXIST; } else { char *record_data = get_record_data(rec->rid().slot_num); @@ -248,8 +247,7 @@ RC RecordPageHandler::update_record(const Record *rec) RC RecordPageHandler::delete_record(const RID *rid) { if (rid->slot_num >= page_header_->record_capacity) { - LOG_ERROR("Invalid slot_num %d, exceed page's record capacity, page_num %d.", - rid->slot_num, frame_->page_num()); + LOG_ERROR("Invalid slot_num %d, exceed page's record capacity, page_num %d.", rid->slot_num, frame_->page_num()); return RC::INVALID_ARGUMENT; } @@ -267,8 +265,7 @@ RC RecordPageHandler::delete_record(const RID *rid) } return RC::SUCCESS; } else { - LOG_ERROR("Invalid slot_num %d, slot is empty, page_num %d.", - rid->slot_num, frame_->page_num()); + LOG_ERROR("Invalid slot_num %d, slot is empty, page_num %d.", rid->slot_num, frame_->page_num()); return RC::RECORD_RECORD_NOT_EXIST; } } @@ -276,15 +273,13 @@ RC RecordPageHandler::delete_record(const RID *rid) RC RecordPageHandler::get_record(const RID *rid, Record *rec) { if (rid->slot_num >= page_header_->record_capacity) { - LOG_ERROR("Invalid slot_num:%d, exceed page's record capacity, page_num %d.", - rid->slot_num, frame_->page_num()); + LOG_ERROR("Invalid slot_num:%d, exceed page's record capacity, page_num %d.", rid->slot_num, frame_->page_num()); return RC::RECORD_INVALIDRID; } Bitmap bitmap(bitmap_, page_header_->record_capacity); if (!bitmap.get_bit(rid->slot_num)) { - LOG_ERROR("Invalid slot_num:%d, slot is empty, page_num %d.", - rid->slot_num, frame_->page_num()); + LOG_ERROR("Invalid slot_num:%d, slot is empty, page_num %d.", rid->slot_num, frame_->page_num()); return RC::RECORD_RECORD_NOT_EXIST; } @@ -392,12 +387,12 @@ RC RecordFileHandler::insert_record(const char *data, int record_size, RID *rid) ret = record_page_handler.init_empty_page(*disk_buffer_pool_, current_page_num, record_size); if (ret != RC::SUCCESS) { LOG_ERROR("Failed to init empty page. ret:%d", ret); - if (RC::SUCCESS != disk_buffer_pool_->unpin_page(frame)) { - LOG_ERROR("Failed to unpin page. "); - } + // this is for allocate_page + disk_buffer_pool_->unpin_page(frame); return ret; } + // this is for allocate_page disk_buffer_pool_->unpin_page(frame); free_pages_.insert(current_page_num); } @@ -555,7 +550,7 @@ bool RecordFileScanner::has_next() RC RecordFileScanner::next(Record &record) { record = next_record_; - + RC rc = fetch_next_record(); if (rc == RC::RECORD_EOF) { rc = RC::SUCCESS; diff --git a/src/observer/storage/record/record_manager.h b/src/observer/storage/record/record_manager.h index dde9f304bd12b81e55d6531f6e35b7038300fc66..296792dfdc2fd05cdb38cd57f13c3f8f85167a41 100644 --- a/src/observer/storage/record/record_manager.h +++ b/src/observer/storage/record/record_manager.h @@ -30,8 +30,7 @@ struct PageHeader { }; class RecordPageHandler; -class RecordPageIterator -{ +class RecordPageIterator { public: RecordPageIterator(); ~RecordPageIterator(); @@ -39,15 +38,17 @@ public: void init(RecordPageHandler &record_page_handler); bool has_next(); - RC next(Record &record); + RC next(Record &record); - bool is_valid() const { + bool is_valid() const + { return record_page_handler_ != nullptr; } + private: RecordPageHandler *record_page_handler_ = nullptr; PageNum page_num_ = BP_INVALID_PAGE_NUM; - common::Bitmap bitmap_; + common::Bitmap bitmap_; SlotNum next_slot_num_ = 0; }; @@ -144,10 +145,10 @@ public: private: RC init_free_pages(); - + private: DiskBufferPool *disk_buffer_pool_ = nullptr; - std::unordered_set free_pages_; // 没有填充满的页面集合 + std::unordered_set free_pages_; // 没有填充满的页面集合 }; class RecordFileScanner { @@ -166,11 +167,12 @@ public: RC close_scan(); bool has_next(); - RC next(Record &record); + RC next(Record &record); private: RC fetch_next_record(); RC fetch_next_record_in_page(); + private: DiskBufferPool *disk_buffer_pool_ = nullptr; diff --git a/src/observer/storage/trx/trx.cpp b/src/observer/storage/trx/trx.cpp index 635cc36987f5482821d06609ccc942039aa7d8f8..e10efb19e1215a2cc5e95d4fc3cd46c5d571b248 100644 --- a/src/observer/storage/trx/trx.cpp +++ b/src/observer/storage/trx/trx.cpp @@ -87,7 +87,7 @@ RC Trx::insert_record(Table *table, Record *record) } // start_if_not_started(); - + // 记录到operations中 insert_operation(table, Operation::Type::INSERT, record->rid()); return rc; diff --git a/src/observer/util/comparator.cpp b/src/observer/util/comparator.cpp index 5accb8ef2061c97f5d569e0fde37b600f7fe9cfa..3a5712cf13dc4b4561db17e63991064aeda0b95c 100644 --- a/src/observer/util/comparator.cpp +++ b/src/observer/util/comparator.cpp @@ -25,8 +25,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; @@ -42,7 +42,7 @@ int compare_string(void *arg1, int arg1_max_length, void *arg2, int arg2_max_len 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); + int result = strncmp(s1, s2, maxlen); if (0 != result) { return result; } diff --git a/src/observer/util/util.cpp b/src/observer/util/util.cpp index 039dc34c5001a62ddc143ea5a804f6c642852150..83465c413818174498e77b3268856553463191a1 100644 --- a/src/observer/util/util.cpp +++ b/src/observer/util/util.cpp @@ -22,7 +22,6 @@ std::string double2string(double v) size_t len = strlen(buf); while (buf[len - 1] == '0') { len--; - } if (buf[len - 1] == '.') { len--;