diff --git a/src/observer/util/comparator.cpp b/deps/common/lang/comparator.cpp similarity index 98% rename from src/observer/util/comparator.cpp rename to deps/common/lang/comparator.cpp index 3a5712cf13dc4b4561db17e63991064aeda0b95c..b23d5753fafef07cd7ac8543edc78cc71df843ec 100644 --- a/src/observer/util/comparator.cpp +++ b/deps/common/lang/comparator.cpp @@ -16,6 +16,9 @@ See the Mulan PSL v2 for more details. */ #include #include "common/defs.h" +namespace common { + + int compare_int(void *arg1, void *arg2) { int v1 = *(int *)arg1; @@ -56,3 +59,5 @@ int compare_string(void *arg1, int arg1_max_length, void *arg2, int arg2_max_len } return 0; } + +} \ No newline at end of file diff --git a/src/observer/util/comparator.h b/deps/common/lang/comparator.h similarity index 97% rename from src/observer/util/comparator.h rename to deps/common/lang/comparator.h index 18ab99fddd08874d6e59d9b615501b4be78fcd13..35e0cc9b22f3df2f670d33f08bb97cd68b919ef0 100644 --- a/src/observer/util/comparator.h +++ b/deps/common/lang/comparator.h @@ -14,6 +14,11 @@ See the Mulan PSL v2 for more details. */ #pragma once +namespace common { + + int compare_int(void *arg1, void *arg2); int compare_float(void *arg1, void *arg2); int compare_string(void *arg1, int arg1_max_length, void *arg2, int arg2_max_length); + +} \ No newline at end of file diff --git a/deps/common/lang/string.cpp b/deps/common/lang/string.cpp index 79f044c020be4b4c91816d382450f2cdccf5dab8..ae0e38d26bd7b8aab96b2dfaa06f89cfaeea4254 100644 --- a/deps/common/lang/string.cpp +++ b/deps/common/lang/string.cpp @@ -265,4 +265,23 @@ char *substr(const char *s, int n1, int n2) return sp; } +/** + * double to string + * @param v + * @return + */ +std::string double_to_str(double v) +{ + char buf[256]; + snprintf(buf, sizeof(buf), "%.2f", v); + size_t len = strlen(buf); + while (buf[len - 1] == '0') { + len--; + } + if (buf[len - 1] == '.') { + len--; + } + + return std::string(buf, len); +} } // namespace common diff --git a/deps/common/lang/string.h b/deps/common/lang/string.h index 446d3b515c1650b2f2a37e8a0e6f647737a532d9..bbf8235dbf1e85ba4e1fbe8f7f7ca6c113f25fb3 100644 --- a/deps/common/lang/string.h +++ b/deps/common/lang/string.h @@ -112,6 +112,13 @@ bool str_to_val(const std::string &str, T &val, std::ios_base &(*radix)(std::ios template void val_to_str(const T &val, std::string &str, std::ios_base &(*radix)(std::ios_base &) = std::dec); +/** + * Double to string + * @param v + * @return + */ +std::string double_to_str(double v); + bool is_blank(const char *s); /** diff --git a/src/observer/sql/expr/tuple_cell.cpp b/src/observer/sql/expr/tuple_cell.cpp index bfd5484e8af6fd8beb04646fe19e8af891bf635b..25f5dc4db71b9f25653eeaebabefd4b0435e6aee 100644 --- a/src/observer/sql/expr/tuple_cell.cpp +++ b/src/observer/sql/expr/tuple_cell.cpp @@ -16,8 +16,8 @@ See the Mulan PSL v2 for more details. */ #include "sql/expr/tuple_cell.h" #include "storage/common/field.h" #include "common/log/log.h" -#include "util/comparator.h" -#include "util/util.h" +#include "common/lang/comparator.h" +#include "common/lang/string.h" TupleCellSpec::TupleCellSpec(const char *table_name, const char *field_name, const char *alias) { @@ -140,7 +140,7 @@ void TupleCell::to_string(std::ostream &os) const os << num_value_.int_value_; } break; case FLOATS: { - os << double2string(num_value_.float_value_); + os << common::double_to_str(num_value_.float_value_); } break; case BOOLEANS: { os << num_value_.bool_value_; @@ -159,19 +159,19 @@ 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 common::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 common::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(), + return common::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_); + return common::compare_int((void *)&this->num_value_.bool_value_, (void *)&other.num_value_.bool_value_); } default: { LOG_WARN("unsupported type: %d", this->attr_type_); @@ -179,10 +179,10 @@ int TupleCell::compare(const TupleCell &other) const } } else if (this->attr_type_ == INTS && other.attr_type_ == FLOATS) { float this_data = this->num_value_.int_value_; - return compare_float((void *)&this_data, (void *)&other.num_value_.float_value_); + return common::compare_float((void *)&this_data, (void *)&other.num_value_.float_value_); } else if (this->attr_type_ == FLOATS && other.attr_type_ == INTS) { float other_data = other.num_value_.int_value_; - return compare_float((void *)&this->num_value_.float_value_, (void *)&other_data); + return common::compare_float((void *)&this->num_value_.float_value_, (void *)&other_data); } LOG_WARN("not supported"); return -1; // TODO return rc? diff --git a/src/observer/storage/index/bplus_tree.h b/src/observer/storage/index/bplus_tree.h index 7631b5eda0b09b4edc244cba1c20e9bc94bba43e..68b89d88b79aa03197fb461f3cfa64d8957877d5 100644 --- a/src/observer/storage/index/bplus_tree.h +++ b/src/observer/storage/index/bplus_tree.h @@ -24,7 +24,7 @@ See the Mulan PSL v2 for more details. */ #include "storage/record/record_manager.h" #include "storage/default/disk_buffer_pool.h" #include "sql/parser/parse_defs.h" -#include "util/comparator.h" +#include "common/lang/comparator.h" #define EMPTY_RID_PAGE_NUM -1 #define EMPTY_RID_SLOT_NUM -1 @@ -46,13 +46,13 @@ public: { switch (attr_type_) { case INTS: { - return compare_int((void *)v1, (void *)v2); + return common::compare_int((void *)v1, (void *)v2); } break; case FLOATS: { - return compare_float((void *)v1, (void *)v2); + return common::compare_float((void *)v1, (void *)v2); } case CHARS: { - return compare_string((void *)v1, attr_length_, (void *)v2, attr_length_); + return common::compare_string((void *)v1, attr_length_, (void *)v2, attr_length_); } default: { LOG_ERROR("unknown attr type. %d", attr_type_); diff --git a/src/observer/util/util.cpp b/src/observer/util/util.cpp deleted file mode 100644 index 83465c413818174498e77b3268856553463191a1..0000000000000000000000000000000000000000 --- a/src/observer/util/util.cpp +++ /dev/null @@ -1,31 +0,0 @@ -/* Copyright (c) 2021 Xie Meiyi(xiemeiyi@hust.edu.cn) and OceanBase and/or its affiliates. All rights reserved. -miniob is licensed under Mulan PSL v2. -You can use this software according to the terms and conditions of the Mulan PSL v2. -You may obtain a copy of Mulan PSL v2 at: - http://license.coscl.org.cn/MulanPSL2 -THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, -EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, -MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. -See the Mulan PSL v2 for more details. */ - -// -// Created by wangyunlai on 2022/9/28 -// - -#include -#include "util/util.h" - -std::string double2string(double v) -{ - char buf[256]; - snprintf(buf, sizeof(buf), "%.2f", v); - size_t len = strlen(buf); - while (buf[len - 1] == '0') { - len--; - } - if (buf[len - 1] == '.') { - len--; - } - - return std::string(buf, len); -} diff --git a/src/observer/util/util.h b/src/observer/util/util.h deleted file mode 100644 index c7db5605eae62e9e15902fc30f99b33531257bd1..0000000000000000000000000000000000000000 --- a/src/observer/util/util.h +++ /dev/null @@ -1,19 +0,0 @@ -/* Copyright (c) 2021 Xie Meiyi(xiemeiyi@hust.edu.cn) and OceanBase and/or its affiliates. All rights reserved. -miniob is licensed under Mulan PSL v2. -You can use this software according to the terms and conditions of the Mulan PSL v2. -You may obtain a copy of Mulan PSL v2 at: - http://license.coscl.org.cn/MulanPSL2 -THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, -EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, -MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. -See the Mulan PSL v2 for more details. */ - -// -// Created by wangyunlai on 2022/9/28 -// - -#pragma once - -#include - -std::string double2string(double v);