/root/doris/be/src/storage/utils.h
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 <cstddef> |
27 | | #include <cstdio> |
28 | | #include <cstdlib> |
29 | | #include <iterator> |
30 | | #include <limits> |
31 | | #include <string> |
32 | | #include <vector> |
33 | | |
34 | | #include "common/status.h" |
35 | | #include "storage/olap_common.h" |
36 | | |
37 | | namespace doris { |
38 | | static const std::string DELETE_SIGN = "__DORIS_DELETE_SIGN__"; |
39 | | static const std::string WHERE_SIGN = "__DORIS_WHERE_SIGN__"; |
40 | | static const std::string VERSION_COL = "__DORIS_VERSION_COL__"; |
41 | | static const std::string SKIP_BITMAP_COL = "__DORIS_SKIP_BITMAP_COL__"; |
42 | | static const std::string SEQUENCE_COL = "__DORIS_SEQUENCE_COL__"; |
43 | | |
44 | | // 用来加速运算 |
45 | | const static int32_t g_power_table[] = {1, 10, 100, 1000, 10000, |
46 | | 100000, 1000000, 10000000, 100000000, 1000000000}; |
47 | | |
48 | | // 计时工具,用于确定一段代码执行的时间,用于性能调优 |
49 | | class OlapStopWatch { |
50 | | public: |
51 | 186 | uint64_t get_elapse_time_us() const { |
52 | 186 | struct timeval now; |
53 | 186 | gettimeofday(&now, nullptr); |
54 | 186 | return (uint64_t)((now.tv_sec - _begin_time.tv_sec) * 1e6 + |
55 | 186 | (now.tv_usec - _begin_time.tv_usec)); |
56 | 186 | } |
57 | | |
58 | 99 | double get_elapse_second() const { return get_elapse_time_us() / 1000000.0; } |
59 | | |
60 | 149 | void reset() { gettimeofday(&_begin_time, nullptr); } |
61 | | |
62 | 149 | OlapStopWatch() { reset(); } |
63 | | |
64 | | private: |
65 | | struct timeval _begin_time; // 起始时间戳 |
66 | | }; |
67 | | |
68 | | // @brief 切分字符串 |
69 | | // @param base 原串 |
70 | | // @param separator 分隔符 |
71 | | // @param result 切分结果 |
72 | | template <typename Str, typename T> |
73 | 0 | Status split_string(const Str& base, const T separator, std::vector<std::string>* result) { |
74 | 0 | if (!result) { |
75 | 0 | return Status::Error<ErrorCode::INVALID_ARGUMENT>("split_string meet nullptr result input"); |
76 | 0 | } |
77 | | |
78 | | // 处理base为空的情况 |
79 | | // 在删除功能中,当varchar类型列的过滤条件为空时,会出现这种情况 |
80 | 0 | if (base.size() == 0) { |
81 | 0 | result->push_back(""); |
82 | 0 | return Status::OK(); |
83 | 0 | } |
84 | | |
85 | 0 | size_t offset = 0; |
86 | 0 | while (offset < base.length()) { |
87 | 0 | size_t next = base.find(separator, offset); |
88 | 0 | if (next == std::string::npos) { |
89 | 0 | result->emplace_back(base.substr(offset)); |
90 | 0 | break; |
91 | 0 | } else { |
92 | 0 | result->emplace_back(base.substr(offset, next - offset)); |
93 | 0 | offset = next + 1; |
94 | 0 | } |
95 | 0 | } |
96 | |
|
97 | 0 | return Status::OK(); |
98 | 0 | } Unexecuted instantiation: _ZN5doris12split_stringISt17basic_string_viewIcSt11char_traitsIcEEcEENS_6StatusERKT_T0_PSt6vectorINSt7__cxx1112basic_stringIcS3_SaIcEEESaISE_EE Unexecuted instantiation: _ZN5doris12split_stringINSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEcEENS_6StatusERKT_T0_PSt6vectorIS6_SaIS6_EE |
99 | | |
100 | | uint32_t olap_adler32_init(); |
101 | | uint32_t olap_adler32(uint32_t adler, const char* buf, size_t len); |
102 | | |
103 | | // 获取系统当前时间,并将时间转换为字符串 |
104 | | Status gen_timestamp_string(std::string* out_string); |
105 | | |
106 | | Status check_datapath_rw(const std::string& path); |
107 | | |
108 | | Status read_write_test_file(const std::string& test_file_path); |
109 | | |
110 | | // 打印Errno |
111 | | class Errno { |
112 | | public: |
113 | | // 返回Errno对应的错误信息,线程安全 |
114 | | static const char* str(); |
115 | | static const char* str(int no); |
116 | | static int no(); |
117 | | |
118 | | private: |
119 | | static const int BUF_SIZE = 256; |
120 | | static __thread char _buf[BUF_SIZE]; |
121 | | }; |
122 | | |
123 | | // 检查int8_t, int16_t, int32_t, int64_t的值是否溢出 |
124 | | template <typename T> |
125 | 89 | bool valid_signed_number(const std::string& value_str) { |
126 | 89 | char* endptr = nullptr; |
127 | 89 | errno = 0; |
128 | 89 | int64_t value = strtol(value_str.c_str(), &endptr, 10); |
129 | | |
130 | 89 | if ((errno == ERANGE && (value == LONG_MAX || value == LONG_MIN)) || |
131 | 89 | (errno != 0 && value == 0) || endptr == value_str || *endptr != '\0') { |
132 | 2 | return false; |
133 | 2 | } |
134 | | |
135 | 87 | if (value < std::numeric_limits<T>::min() || value > std::numeric_limits<T>::max()) { |
136 | 6 | return false; |
137 | 6 | } |
138 | | |
139 | 81 | return true; |
140 | 87 | } _ZN5doris19valid_signed_numberIaEEbRKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE Line | Count | Source | 125 | 11 | bool valid_signed_number(const std::string& value_str) { | 126 | 11 | char* endptr = nullptr; | 127 | 11 | errno = 0; | 128 | 11 | int64_t value = strtol(value_str.c_str(), &endptr, 10); | 129 | | | 130 | 11 | if ((errno == ERANGE && (value == LONG_MAX || value == LONG_MIN)) || | 131 | 11 | (errno != 0 && value == 0) || endptr == value_str || *endptr != '\0') { | 132 | 0 | return false; | 133 | 0 | } | 134 | | | 135 | 11 | if (value < std::numeric_limits<T>::min() || value > std::numeric_limits<T>::max()) { | 136 | 2 | return false; | 137 | 2 | } | 138 | | | 139 | 9 | return true; | 140 | 11 | } |
_ZN5doris19valid_signed_numberIsEEbRKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE Line | Count | Source | 125 | 13 | bool valid_signed_number(const std::string& value_str) { | 126 | 13 | char* endptr = nullptr; | 127 | 13 | errno = 0; | 128 | 13 | int64_t value = strtol(value_str.c_str(), &endptr, 10); | 129 | | | 130 | 13 | if ((errno == ERANGE && (value == LONG_MAX || value == LONG_MIN)) || | 131 | 13 | (errno != 0 && value == 0) || endptr == value_str || *endptr != '\0') { | 132 | 0 | return false; | 133 | 0 | } | 134 | | | 135 | 13 | if (value < std::numeric_limits<T>::min() || value > std::numeric_limits<T>::max()) { | 136 | 2 | return false; | 137 | 2 | } | 138 | | | 139 | 11 | return true; | 140 | 13 | } |
_ZN5doris19valid_signed_numberIiEEbRKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE Line | Count | Source | 125 | 54 | bool valid_signed_number(const std::string& value_str) { | 126 | 54 | char* endptr = nullptr; | 127 | 54 | errno = 0; | 128 | 54 | int64_t value = strtol(value_str.c_str(), &endptr, 10); | 129 | | | 130 | 54 | if ((errno == ERANGE && (value == LONG_MAX || value == LONG_MIN)) || | 131 | 54 | (errno != 0 && value == 0) || endptr == value_str || *endptr != '\0') { | 132 | 0 | return false; | 133 | 0 | } | 134 | | | 135 | 54 | if (value < std::numeric_limits<T>::min() || value > std::numeric_limits<T>::max()) { | 136 | 2 | return false; | 137 | 2 | } | 138 | | | 139 | 52 | return true; | 140 | 54 | } |
_ZN5doris19valid_signed_numberIlEEbRKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE Line | Count | Source | 125 | 11 | bool valid_signed_number(const std::string& value_str) { | 126 | 11 | char* endptr = nullptr; | 127 | 11 | errno = 0; | 128 | 11 | int64_t value = strtol(value_str.c_str(), &endptr, 10); | 129 | | | 130 | 11 | if ((errno == ERANGE && (value == LONG_MAX || value == LONG_MIN)) || | 131 | 11 | (errno != 0 && value == 0) || endptr == value_str || *endptr != '\0') { | 132 | 2 | return false; | 133 | 2 | } | 134 | | | 135 | 9 | if (value < std::numeric_limits<T>::min() || value > std::numeric_limits<T>::max()) { | 136 | 0 | return false; | 137 | 0 | } | 138 | | | 139 | 9 | return true; | 140 | 9 | } |
|
141 | | |
142 | | template <> |
143 | | bool valid_signed_number<int128_t>(const std::string& value_str); |
144 | | |
145 | | // 检查uint8_t, uint16_t, uint32_t, uint64_t的值是否溢出 |
146 | | template <typename T> |
147 | 0 | bool valid_unsigned_number(const std::string& value_str) { |
148 | 0 | if (value_str[0] == '-') { |
149 | 0 | return false; |
150 | 0 | } |
151 | | |
152 | 0 | char* endptr = nullptr; |
153 | 0 | errno = 0; |
154 | 0 | uint64_t value = strtoul(value_str.c_str(), &endptr, 10); |
155 | |
|
156 | 0 | if ((errno == ERANGE && (value == ULONG_MAX)) || (errno != 0 && value == 0) || |
157 | 0 | endptr == value_str || *endptr != '\0') { |
158 | 0 | return false; |
159 | 0 | } |
160 | | |
161 | 0 | if (value < std::numeric_limits<T>::min() || value > std::numeric_limits<T>::max()) { |
162 | 0 | return false; |
163 | 0 | } |
164 | | |
165 | 0 | return true; |
166 | 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 |
167 | | |
168 | | bool valid_decimal(const std::string& value_str, const uint32_t precision, const uint32_t frac); |
169 | | |
170 | | // Validate for date/datetime roughly. The format is 'yyyy-MM-dd HH:mm:ss' |
171 | | // TODO: support 'yyyy-MM-dd HH:mm:ss.SSS' |
172 | | bool valid_datetime(const std::string& value_str, const uint32_t scale); |
173 | | |
174 | | bool valid_bool(const std::string& value_str); |
175 | | |
176 | | bool valid_ipv4(const std::string& value_str); |
177 | | |
178 | | bool valid_ipv6(const std::string& value_str); |
179 | | |
180 | 2.70k | constexpr bool is_string_type(const FieldType& field_type) { |
181 | 2.70k | return field_type == FieldType::OLAP_FIELD_TYPE_VARCHAR || |
182 | 2.70k | field_type == FieldType::OLAP_FIELD_TYPE_CHAR || |
183 | 2.70k | field_type == FieldType::OLAP_FIELD_TYPE_STRING; |
184 | 2.70k | } |
185 | | |
186 | | // Util used to get string name of thrift enum item |
187 | | #define EnumToString(enum_type, index, out) \ |
188 | 2.95k | do { \ |
189 | 2.95k | auto it = _##enum_type##_VALUES_TO_NAMES.find(index); \ |
190 | 2.95k | if (it == _##enum_type##_VALUES_TO_NAMES.end()) { \ |
191 | 0 | out = "NULL"; \ |
192 | 2.95k | } else { \ |
193 | 2.95k | out = it->second; \ |
194 | 2.95k | } \ |
195 | 2.95k | } while (0) |
196 | | |
197 | | struct RowLocation { |
198 | 6.21M | RowLocation() : segment_id(0), row_id(0) {} |
199 | 6.70M | RowLocation(uint32_t sid, uint32_t rid) : segment_id(sid), row_id(rid) {} |
200 | | RowLocation(RowsetId rsid, uint32_t sid, uint32_t rid) |
201 | 8.14M | : rowset_id(rsid), segment_id(sid), row_id(rid) {} |
202 | | RowsetId rowset_id; |
203 | | uint32_t segment_id; |
204 | | uint32_t row_id; |
205 | | |
206 | 0 | bool operator==(const RowLocation& rhs) const { |
207 | 0 | return rowset_id == rhs.rowset_id && segment_id == rhs.segment_id && row_id == rhs.row_id; |
208 | 0 | } |
209 | | |
210 | 0 | bool operator<(const RowLocation& rhs) const { |
211 | 0 | if (rowset_id != rhs.rowset_id) { |
212 | 0 | return rowset_id < rhs.rowset_id; |
213 | 0 | } else if (segment_id != rhs.segment_id) { |
214 | 0 | return segment_id < rhs.segment_id; |
215 | 0 | } else { |
216 | 0 | return row_id < rhs.row_id; |
217 | 0 | } |
218 | 0 | } |
219 | | }; |
220 | | using RowLocationSet = std::set<RowLocation>; |
221 | | using RowLocationPairList = std::list<std::pair<RowLocation, RowLocation>>; |
222 | | |
223 | | struct GlobalRowLoacation { |
224 | | GlobalRowLoacation(int64_t tid, RowsetId rsid, uint32_t sid, uint32_t rid) |
225 | 6 | : tablet_id(tid), row_location(rsid, sid, rid) {} |
226 | | int64_t tablet_id; |
227 | | RowLocation row_location; |
228 | | |
229 | 0 | bool operator==(const GlobalRowLoacation& rhs) const { |
230 | 0 | return tablet_id == rhs.tablet_id && row_location == rhs.row_location; |
231 | 0 | } |
232 | | |
233 | 0 | bool operator<(const GlobalRowLoacation& rhs) const { |
234 | 0 | if (tablet_id != rhs.tablet_id) { |
235 | 0 | return tablet_id < rhs.tablet_id; |
236 | 0 | } else { |
237 | 0 | return row_location < rhs.row_location; |
238 | 0 | } |
239 | 0 | } |
240 | | }; |
241 | | |
242 | | // Wire-protocol values: never reorder or reuse an existing value. A new value may use a new |
243 | | // encoded structure and size, provided its decoder keeps supporting all older values. |
244 | | enum class ROW_VERSION : uint8_t { |
245 | | // The row ID is a uint32 ordinal local to the FileMapping identified by file_id. |
246 | | FILE_LOCAL_ROW_ID = 0, |
247 | | // The row ID is an opaque uint64 ID in a fixed Lance dataset snapshot. |
248 | | LANCE_DATASET_ROW_ID = 1, |
249 | | }; |
250 | | |
251 | | /* |
252 | | * A serialized global row location has a fixed size of 24 bytes. The version determines how the |
253 | | * bytes at offsets 4..7 and 16..23 must be interpreted: |
254 | | * |
255 | | * FILE_LOCAL_ROW_ID (version = 0), used by Doris, Parquet, and ORC: |
256 | | * |
257 | | * byte offset 0 1..7 8..15 16..19 20..23 |
258 | | * +--------+----------------+---------------+-----------+-----------+ |
259 | | * | ver=0 | reserved | backend_id | file_id | row_id | |
260 | | * +--------+----------------+---------------+-----------+-----------+ |
261 | | * uint8 7 bytes int64 uint32 uint32 |
262 | | * |
263 | | * row_id is an ordinal local to the FileMapping selected by file_id. |
264 | | * |
265 | | * LANCE_DATASET_ROW_ID (version = 1), used by Lance: |
266 | | * |
267 | | * byte offset 0 1..3 4..7 8..15 16..23 |
268 | | * +--------+----------+-------------+---------------+----------------+ |
269 | | * | ver=1 | reserved | file_id | backend_id | lance_row_id | |
270 | | * +--------+----------+-------------+---------------+----------------+ |
271 | | * uint8 3 bytes uint32 int64 uint64 |
272 | | * |
273 | | * lance_row_id is an opaque row ID in the fixed dataset snapshot recorded by the FileMapping. |
274 | | * |
275 | | * The first union reuses four bytes that are padding in version 0 as Lance's file_id in version 1. |
276 | | * The second union reuses the original {uint32 file_id, uint32 row_id} payload as one uint64 Lance |
277 | | * row ID. Therefore, always check version before reading either union. |
278 | | */ |
279 | | struct GlobalRowLoacationV2 { |
280 | | static constexpr uint8_t VERSION = static_cast<uint8_t>(ROW_VERSION::FILE_LOCAL_ROW_ID); |
281 | | |
282 | | struct FileLocalRowId { |
283 | | uint32_t file_id; |
284 | | uint32_t row_id; |
285 | | }; |
286 | | |
287 | | GlobalRowLoacationV2(uint8_t ver, uint64_t bid, uint32_t fid, uint32_t rid) |
288 | 71 | : version(ver), |
289 | 71 | reserved_for_file_local(0), |
290 | 71 | backend_id(bid), |
291 | 71 | file_local {.file_id = fid, .row_id = rid} {} |
292 | | GlobalRowLoacationV2(ROW_VERSION ver, uint64_t bid, uint32_t fid, uint64_t rid) |
293 | 21 | : version(static_cast<uint8_t>(ver)), |
294 | 21 | lance_file_id(fid), |
295 | 21 | backend_id(bid), |
296 | 21 | lance_row_id(rid) {} |
297 | | |
298 | | uint8_t version; |
299 | | uint8_t reserved_before_file_id[3] {}; |
300 | | union { |
301 | | // version 0: offsets 4..7 remain reserved, preserving the original V2 layout. |
302 | | uint32_t reserved_for_file_local; |
303 | | // version 1: offsets 4..7 identify the FileMapping for lance_row_id. |
304 | | uint32_t lance_file_id; |
305 | | }; |
306 | | int64_t backend_id; |
307 | | union { |
308 | | // version 0: file_id is at offset 16 and its uint32 row ordinal is at offset 20. |
309 | | FileLocalRowId file_local; |
310 | | // version 1: offsets 16..23 are one opaque uint64 Lance row ID. |
311 | | uint64_t lance_row_id; |
312 | | }; |
313 | | }; |
314 | | |
315 | | static_assert(sizeof(GlobalRowLoacationV2) == 24); |
316 | | static_assert(sizeof(GlobalRowLoacationV2::FileLocalRowId) == 8); |
317 | | static_assert(offsetof(GlobalRowLoacationV2, version) == 0); |
318 | | static_assert(offsetof(GlobalRowLoacationV2, reserved_before_file_id) == 1); |
319 | | static_assert(offsetof(GlobalRowLoacationV2, reserved_for_file_local) == 4); |
320 | | static_assert(offsetof(GlobalRowLoacationV2, lance_file_id) == 4); |
321 | | static_assert(offsetof(GlobalRowLoacationV2, backend_id) == 8); |
322 | | static_assert(offsetof(GlobalRowLoacationV2, file_local) == 16); |
323 | | static_assert(offsetof(GlobalRowLoacationV2::FileLocalRowId, file_id) == 0); |
324 | | static_assert(offsetof(GlobalRowLoacationV2::FileLocalRowId, row_id) == 4); |
325 | | static_assert(offsetof(GlobalRowLoacationV2, lance_row_id) == 16); |
326 | | |
327 | | } // namespace doris |