提交 b955826f 编写于 作者: L Longda Feng

move observer's utility to common directory

上级 72505bdd
......@@ -16,6 +16,9 @@ See the Mulan PSL v2 for more details. */
#include <algorithm>
#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
......@@ -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
......@@ -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
......@@ -112,6 +112,13 @@ bool str_to_val(const std::string &str, T &val, std::ios_base &(*radix)(std::ios
template <class T>
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);
/**
......
......@@ -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?
......
......@@ -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_);
......
/* 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 <string.h>
#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);
}
/* 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 <string>
std::string double2string(double v);
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册