From b955826f854d399622035140ff870c9ff6d5ee94 Mon Sep 17 00:00:00 2001 From: Longda Feng Date: Sat, 28 Jan 2023 15:19:34 +0800 Subject: [PATCH] move observer's utility to common directory --- .../util => deps/common/lang}/comparator.cpp | 5 +++ .../util => deps/common/lang}/comparator.h | 5 +++ deps/common/lang/string.cpp | 19 ++++++++++++ deps/common/lang/string.h | 7 +++++ src/observer/sql/expr/tuple_cell.cpp | 18 +++++------ src/observer/storage/index/bplus_tree.h | 8 ++--- src/observer/util/util.cpp | 31 ------------------- src/observer/util/util.h | 19 ------------ 8 files changed, 49 insertions(+), 63 deletions(-) rename {src/observer/util => deps/common/lang}/comparator.cpp (98%) rename {src/observer/util => deps/common/lang}/comparator.h (97%) delete mode 100644 src/observer/util/util.cpp delete mode 100644 src/observer/util/util.h 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 3a5712c..b23d575 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 18ab99f..35e0cc9 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 79f044c..ae0e38d 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 446d3b5..bbf8235 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 bfd5484..25f5dc4 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 7631b5e..68b89d8 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 83465c4..0000000 --- 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 c7db560..0000000 --- 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); -- GitLab