Line | Count | Source |
1 | | // Licensed to the Apache Software Foundation (ASF) under one |
2 | | // or more contributor license agreements. See the NOTICE file |
3 | | // distributed with this work for additional information |
4 | | // regarding copyright ownership. The ASF licenses this file |
5 | | // to you under the Apache License, Version 2.0 (the |
6 | | // "License"); you may not use this file except in compliance |
7 | | // with the License. You may obtain a copy of the License at |
8 | | // |
9 | | // http://www.apache.org/licenses/LICENSE-2.0 |
10 | | // |
11 | | // Unless required by applicable law or agreed to in writing, |
12 | | // software distributed under the License is distributed on an |
13 | | // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
14 | | // KIND, either express or implied. See the License for the |
15 | | // specific language governing permissions and limitations |
16 | | // under the License. |
17 | | |
18 | | #pragma once |
19 | | |
20 | | // IWYU pragma: no_include <bthread/errno.h> |
21 | | #include <errno.h> // IWYU pragma: keep |
22 | | #include <limits.h> |
23 | | #include <stdint.h> |
24 | | #include <sys/time.h> |
25 | | |
26 | | #include <cstdio> |
27 | | #include <cstdlib> |
28 | | #include <iterator> |
29 | | #include <limits> |
30 | | #include <string> |
31 | | #include <vector> |
32 | | |
33 | | #include "common/status.h" |
34 | | #include "storage/olap_common.h" |
35 | | |
36 | | namespace doris { |
37 | | static const std::string DELETE_SIGN = "__DORIS_DELETE_SIGN__"; |
38 | | static const std::string WHERE_SIGN = "__DORIS_WHERE_SIGN__"; |
39 | | static const std::string VERSION_COL = "__DORIS_VERSION_COL__"; |
40 | | static const std::string SKIP_BITMAP_COL = "__DORIS_SKIP_BITMAP_COL__"; |
41 | | static const std::string SEQUENCE_COL = "__DORIS_SEQUENCE_COL__"; |
42 | | static const std::string COMMIT_TSO_COL = "__DORIS_COMMIT_TSO_COL__"; |
43 | | static const std::string ROW_LSN_COL = "__DORIS_ROW_LSN_COL__"; |
44 | | static const std::string BINLOG_TSO_COL = "__DORIS_BINLOG_TSO__"; |
45 | | static const std::string BINLOG_LSN_COL = "__DORIS_BINLOG_LSN__"; |
46 | | static const std::string BINLOG_OP_COL = "__DORIS_BINLOG_OP__"; |
47 | | |
48 | | // 用来加速运算 |
49 | | const static int32_t g_power_table[] = {1, 10, 100, 1000, 10000, |
50 | | 100000, 1000000, 10000000, 100000000, 1000000000}; |
51 | | |
52 | | // 计时工具,用于确定一段代码执行的时间,用于性能调优 |
53 | | class OlapStopWatch { |
54 | | public: |
55 | 201k | uint64_t get_elapse_time_us() const { |
56 | 201k | struct timeval now; |
57 | 201k | gettimeofday(&now, nullptr); |
58 | 201k | return (uint64_t)((now.tv_sec - _begin_time.tv_sec) * 1e6 + |
59 | 201k | (now.tv_usec - _begin_time.tv_usec)); |
60 | 201k | } |
61 | | |
62 | 394 | double get_elapse_second() const { return get_elapse_time_us() / 1000000.0; } |
63 | | |
64 | 112k | void reset() { gettimeofday(&_begin_time, nullptr); } |
65 | | |
66 | 112k | OlapStopWatch() { reset(); } |
67 | | |
68 | | private: |
69 | | struct timeval _begin_time; // 起始时间戳 |
70 | | }; |
71 | | |
72 | | // @brief 切分字符串 |
73 | | // @param base 原串 |
74 | | // @param separator 分隔符 |
75 | | // @param result 切分结果 |
76 | | template <typename Str, typename T> |
77 | 303k | Status split_string(const Str& base, const T separator, std::vector<std::string>* result) { |
78 | 303k | if (!result) { |
79 | 0 | return Status::Error<ErrorCode::INVALID_ARGUMENT>("split_string meet nullptr result input"); |
80 | 0 | } |
81 | | |
82 | | // 处理base为空的情况 |
83 | | // 在删除功能中,当varchar类型列的过滤条件为空时,会出现这种情况 |
84 | 303k | if (base.size() == 0) { |
85 | 0 | result->push_back(""); |
86 | 0 | return Status::OK(); |
87 | 0 | } |
88 | | |
89 | 303k | size_t offset = 0; |
90 | 902k | while (offset < base.length()) { |
91 | 902k | size_t next = base.find(separator, offset); |
92 | 902k | if (next == std::string::npos) { |
93 | 303k | result->emplace_back(base.substr(offset)); |
94 | 303k | break; |
95 | 598k | } else { |
96 | 598k | result->emplace_back(base.substr(offset, next - offset)); |
97 | 598k | offset = next + 1; |
98 | 598k | } |
99 | 902k | } |
100 | | |
101 | 303k | return Status::OK(); |
102 | 303k | } _ZN5doris12split_stringISt17basic_string_viewIcSt11char_traitsIcEEcEENS_6StatusERKT_T0_PSt6vectorINSt7__cxx1112basic_stringIcS3_SaIcEEESaISE_EE Line | Count | Source | 77 | 295k | Status split_string(const Str& base, const T separator, std::vector<std::string>* result) { | 78 | 295k | if (!result) { | 79 | 0 | return Status::Error<ErrorCode::INVALID_ARGUMENT>("split_string meet nullptr result input"); | 80 | 0 | } | 81 | | | 82 | | // 处理base为空的情况 | 83 | | // 在删除功能中,当varchar类型列的过滤条件为空时,会出现这种情况 | 84 | 295k | if (base.size() == 0) { | 85 | 0 | result->push_back(""); | 86 | 0 | return Status::OK(); | 87 | 0 | } | 88 | | | 89 | 295k | size_t offset = 0; | 90 | 886k | while (offset < base.length()) { | 91 | 886k | size_t next = base.find(separator, offset); | 92 | 886k | if (next == std::string::npos) { | 93 | 295k | result->emplace_back(base.substr(offset)); | 94 | 295k | break; | 95 | 590k | } else { | 96 | 590k | result->emplace_back(base.substr(offset, next - offset)); | 97 | 590k | offset = next + 1; | 98 | 590k | } | 99 | 886k | } | 100 | | | 101 | 295k | return Status::OK(); | 102 | 295k | } |
_ZN5doris12split_stringINSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEcEENS_6StatusERKT_T0_PSt6vectorIS6_SaIS6_EE Line | Count | Source | 77 | 7.94k | Status split_string(const Str& base, const T separator, std::vector<std::string>* result) { | 78 | 7.94k | if (!result) { | 79 | 0 | return Status::Error<ErrorCode::INVALID_ARGUMENT>("split_string meet nullptr result input"); | 80 | 0 | } | 81 | | | 82 | | // 处理base为空的情况 | 83 | | // 在删除功能中,当varchar类型列的过滤条件为空时,会出现这种情况 | 84 | 7.94k | if (base.size() == 0) { | 85 | 0 | result->push_back(""); | 86 | 0 | return Status::OK(); | 87 | 0 | } | 88 | | | 89 | 7.94k | size_t offset = 0; | 90 | 15.8k | while (offset < base.length()) { | 91 | 15.8k | size_t next = base.find(separator, offset); | 92 | 15.8k | if (next == std::string::npos) { | 93 | 7.94k | result->emplace_back(base.substr(offset)); | 94 | 7.94k | break; | 95 | 7.94k | } else { | 96 | 7.94k | result->emplace_back(base.substr(offset, next - offset)); | 97 | 7.94k | offset = next + 1; | 98 | 7.94k | } | 99 | 15.8k | } | 100 | | | 101 | 7.94k | return Status::OK(); | 102 | 7.94k | } |
|
103 | | |
104 | | uint32_t olap_adler32_init(); |
105 | | uint32_t olap_adler32(uint32_t adler, const char* buf, size_t len); |
106 | | |
107 | | // 获取系统当前时间,并将时间转换为字符串 |
108 | | Status gen_timestamp_string(std::string* out_string); |
109 | | |
110 | | Status check_datapath_rw(const std::string& path); |
111 | | |
112 | | Status read_write_test_file(const std::string& test_file_path); |
113 | | |
114 | | // 打印Errno |
115 | | class Errno { |
116 | | public: |
117 | | // 返回Errno对应的错误信息,线程安全 |
118 | | static const char* str(); |
119 | | static const char* str(int no); |
120 | | static int no(); |
121 | | |
122 | | private: |
123 | | static const int BUF_SIZE = 256; |
124 | | static __thread char _buf[BUF_SIZE]; |
125 | | }; |
126 | | |
127 | | // 检查int8_t, int16_t, int32_t, int64_t的值是否溢出 |
128 | | template <typename T> |
129 | 904 | bool valid_signed_number(const std::string& value_str) { |
130 | 904 | char* endptr = nullptr; |
131 | 904 | errno = 0; |
132 | 904 | int64_t value = strtol(value_str.c_str(), &endptr, 10); |
133 | | |
134 | 904 | if ((errno == ERANGE && (value == LONG_MAX || value == LONG_MIN)) || |
135 | 908 | (errno != 0 && value == 0) || endptr == value_str || *endptr != '\0') { |
136 | 2 | return false; |
137 | 2 | } |
138 | | |
139 | 903 | if (value < std::numeric_limits<T>::min() || value > std::numeric_limits<T>::max()) { |
140 | 6 | return false; |
141 | 6 | } |
142 | | |
143 | 896 | return true; |
144 | 902 | } _ZN5doris19valid_signed_numberIlEEbRKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE Line | Count | Source | 129 | 64 | bool valid_signed_number(const std::string& value_str) { | 130 | 64 | char* endptr = nullptr; | 131 | 64 | errno = 0; | 132 | 64 | int64_t value = strtol(value_str.c_str(), &endptr, 10); | 133 | | | 134 | 64 | if ((errno == ERANGE && (value == LONG_MAX || value == LONG_MIN)) || | 135 | 64 | (errno != 0 && value == 0) || endptr == value_str || *endptr != '\0') { | 136 | 2 | return false; | 137 | 2 | } | 138 | | | 139 | 63 | if (value < std::numeric_limits<T>::min() || value > std::numeric_limits<T>::max()) { | 140 | 0 | return false; | 141 | 0 | } | 142 | | | 143 | 62 | return true; | 144 | 62 | } |
_ZN5doris19valid_signed_numberIaEEbRKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE Line | Count | Source | 129 | 25 | bool valid_signed_number(const std::string& value_str) { | 130 | 25 | char* endptr = nullptr; | 131 | 25 | errno = 0; | 132 | 25 | int64_t value = strtol(value_str.c_str(), &endptr, 10); | 133 | | | 134 | 25 | if ((errno == ERANGE && (value == LONG_MAX || value == LONG_MIN)) || | 135 | 25 | (errno != 0 && value == 0) || endptr == value_str || *endptr != '\0') { | 136 | 0 | return false; | 137 | 0 | } | 138 | | | 139 | 25 | if (value < std::numeric_limits<T>::min() || value > std::numeric_limits<T>::max()) { | 140 | 2 | return false; | 141 | 2 | } | 142 | | | 143 | 23 | return true; | 144 | 25 | } |
_ZN5doris19valid_signed_numberIsEEbRKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE Line | Count | Source | 129 | 17 | bool valid_signed_number(const std::string& value_str) { | 130 | 17 | char* endptr = nullptr; | 131 | 17 | errno = 0; | 132 | 17 | int64_t value = strtol(value_str.c_str(), &endptr, 10); | 133 | | | 134 | 17 | if ((errno == ERANGE && (value == LONG_MAX || value == LONG_MIN)) || | 135 | 17 | (errno != 0 && value == 0) || endptr == value_str || *endptr != '\0') { | 136 | 0 | return false; | 137 | 0 | } | 138 | | | 139 | 17 | if (value < std::numeric_limits<T>::min() || value > std::numeric_limits<T>::max()) { | 140 | 2 | return false; | 141 | 2 | } | 142 | | | 143 | 15 | return true; | 144 | 17 | } |
_ZN5doris19valid_signed_numberIiEEbRKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE Line | Count | Source | 129 | 798 | bool valid_signed_number(const std::string& value_str) { | 130 | 798 | char* endptr = nullptr; | 131 | 798 | errno = 0; | 132 | 798 | int64_t value = strtol(value_str.c_str(), &endptr, 10); | 133 | | | 134 | 798 | if ((errno == ERANGE && (value == LONG_MAX || value == LONG_MIN)) || | 135 | 803 | (errno != 0 && value == 0) || endptr == value_str || *endptr != '\0') { | 136 | 0 | return false; | 137 | 0 | } | 138 | | | 139 | 800 | if (value < std::numeric_limits<T>::min() || value > std::numeric_limits<T>::max()) { | 140 | 2 | return false; | 141 | 2 | } | 142 | | | 143 | 796 | return true; | 144 | 798 | } |
|
145 | | |
146 | | template <> |
147 | | bool valid_signed_number<int128_t>(const std::string& value_str); |
148 | | |
149 | | // 检查uint8_t, uint16_t, uint32_t, uint64_t的值是否溢出 |
150 | | template <typename T> |
151 | 0 | bool valid_unsigned_number(const std::string& value_str) { |
152 | 0 | if (value_str[0] == '-') { |
153 | 0 | return false; |
154 | 0 | } |
155 | | |
156 | 0 | char* endptr = nullptr; |
157 | 0 | errno = 0; |
158 | 0 | uint64_t value = strtoul(value_str.c_str(), &endptr, 10); |
159 | |
|
160 | 0 | if ((errno == ERANGE && (value == ULONG_MAX)) || (errno != 0 && value == 0) || |
161 | 0 | endptr == value_str || *endptr != '\0') { |
162 | 0 | return false; |
163 | 0 | } |
164 | | |
165 | 0 | if (value < std::numeric_limits<T>::min() || value > std::numeric_limits<T>::max()) { |
166 | 0 | return false; |
167 | 0 | } |
168 | | |
169 | 0 | return true; |
170 | 0 | } Unexecuted instantiation: _ZN5doris21valid_unsigned_numberIhEEbRKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE Unexecuted instantiation: _ZN5doris21valid_unsigned_numberItEEbRKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE Unexecuted instantiation: _ZN5doris21valid_unsigned_numberIjEEbRKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE Unexecuted instantiation: _ZN5doris21valid_unsigned_numberImEEbRKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE |
171 | | |
172 | | bool valid_decimal(const std::string& value_str, const uint32_t precision, const uint32_t frac); |
173 | | |
174 | | // Validate for date/datetime roughly. The format is 'yyyy-MM-dd HH:mm:ss' |
175 | | // TODO: support 'yyyy-MM-dd HH:mm:ss.SSS' |
176 | | bool valid_datetime(const std::string& value_str, const uint32_t scale); |
177 | | |
178 | | bool valid_bool(const std::string& value_str); |
179 | | |
180 | | bool valid_ipv4(const std::string& value_str); |
181 | | |
182 | | bool valid_ipv6(const std::string& value_str); |
183 | | |
184 | 18.6M | constexpr bool is_string_type(const FieldType& field_type) { |
185 | 18.6M | return field_type == FieldType::OLAP_FIELD_TYPE_VARCHAR || |
186 | 18.6M | field_type == FieldType::OLAP_FIELD_TYPE_CHAR || |
187 | 18.6M | field_type == FieldType::OLAP_FIELD_TYPE_STRING; |
188 | 18.6M | } |
189 | | |
190 | | // Util used to get string name of thrift enum item |
191 | | #define EnumToString(enum_type, index, out) \ |
192 | 35.4M | do { \ |
193 | 35.4M | auto it = _##enum_type##_VALUES_TO_NAMES.find(index); \ |
194 | 35.4M | if (it == _##enum_type##_VALUES_TO_NAMES.end()) { \ |
195 | 0 | out = "NULL"; \ |
196 | 35.4M | } else { \ |
197 | 35.4M | out = it->second; \ |
198 | 35.4M | } \ |
199 | 35.4M | } while (0) |
200 | | |
201 | | struct RowLocation { |
202 | 23.4M | RowLocation() : segment_id(0), row_id(0) {} |
203 | 9.87M | RowLocation(uint32_t sid, uint32_t rid) : segment_id(sid), row_id(rid) {} |
204 | | RowLocation(RowsetId rsid, uint32_t sid, uint32_t rid) |
205 | 11.3M | : rowset_id(rsid), segment_id(sid), row_id(rid) {} |
206 | | RowsetId rowset_id; |
207 | | uint32_t segment_id; |
208 | | uint32_t row_id; |
209 | | |
210 | 0 | bool operator==(const RowLocation& rhs) const { |
211 | 0 | return rowset_id == rhs.rowset_id && segment_id == rhs.segment_id && row_id == rhs.row_id; |
212 | 0 | } |
213 | | |
214 | 1.18k | bool operator<(const RowLocation& rhs) const { |
215 | 1.18k | if (rowset_id != rhs.rowset_id) { |
216 | 152 | return rowset_id < rhs.rowset_id; |
217 | 1.03k | } else if (segment_id != rhs.segment_id) { |
218 | 0 | return segment_id < rhs.segment_id; |
219 | 1.03k | } else { |
220 | 1.03k | return row_id < rhs.row_id; |
221 | 1.03k | } |
222 | 1.18k | } |
223 | | }; |
224 | | using RowLocationSet = std::set<RowLocation>; |
225 | | using RowLocationPairList = std::list<std::pair<RowLocation, RowLocation>>; |
226 | | |
227 | | struct GlobalRowLoacationV2 { |
228 | | GlobalRowLoacationV2(uint8_t ver, uint64_t bid, uint32_t fid, uint32_t rid) |
229 | 23.7M | : version(ver), backend_id(bid), file_id(fid), row_id(rid) {} |
230 | | uint8_t version; |
231 | | int64_t backend_id; |
232 | | uint32_t file_id; |
233 | | uint32_t row_id; |
234 | | |
235 | | auto operator<=>(const GlobalRowLoacationV2&) const = default; |
236 | | }; |
237 | | |
238 | | } // namespace doris |