Coverage Report

Created: 2025-04-11 14:39

/root/doris/be/src/olap/olap_common.h
Line
Count
Source (jump to first uncovered line)
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
#include <gen_cpp/Types_types.h>
21
#include <netinet/in.h>
22
23
#include <atomic>
24
#include <charconv>
25
#include <cstdint>
26
#include <functional>
27
#include <list>
28
#include <map>
29
#include <memory>
30
#include <ostream>
31
#include <sstream>
32
#include <string>
33
#include <typeinfo>
34
#include <unordered_map>
35
#include <unordered_set>
36
#include <utility>
37
38
#include "common/config.h"
39
#include "common/exception.h"
40
#include "io/io_common.h"
41
#include "olap/inverted_index_stats.h"
42
#include "olap/olap_define.h"
43
#include "olap/rowset/rowset_fwd.h"
44
#include "util/hash_util.hpp"
45
#include "util/time.h"
46
#include "util/uid_util.h"
47
48
namespace doris {
49
50
static constexpr int64_t MAX_ROWSET_ID = 1L << 56;
51
static constexpr int64_t LOW_56_BITS = 0x00ffffffffffffff;
52
53
using SchemaHash = int32_t;
54
using int128_t = __int128;
55
using uint128_t = unsigned __int128;
56
57
using TabletUid = UniqueId;
58
59
enum CompactionType { BASE_COMPACTION = 1, CUMULATIVE_COMPACTION = 2, FULL_COMPACTION = 3 };
60
61
enum DataDirType {
62
    SPILL_DISK_DIR,
63
    OLAP_DATA_DIR,
64
    DATA_CACHE_DIR,
65
};
66
67
struct DataDirInfo {
68
    std::string path;
69
    size_t path_hash = 0;
70
    int64_t disk_capacity = 1; // actual disk capacity
71
    int64_t available = 0;     // available space, in bytes unit
72
    int64_t local_used_capacity = 0;
73
    int64_t remote_used_capacity = 0;
74
    int64_t trash_used_capacity = 0;
75
    bool is_used = false;                                      // whether available mark
76
    TStorageMedium::type storage_medium = TStorageMedium::HDD; // Storage medium type: SSD|HDD
77
    DataDirType data_dir_type = DataDirType::OLAP_DATA_DIR;
78
    std::string metric_name;
79
};
80
81
// Sort DataDirInfo by available space.
82
struct DataDirInfoLessAvailability {
83
7
    bool operator()(const DataDirInfo& left, const DataDirInfo& right) const {
84
7
        return left.available < right.available;
85
7
    }
86
};
87
88
struct TabletInfo {
89
    TabletInfo(TTabletId in_tablet_id, UniqueId in_uid)
90
            : tablet_id(in_tablet_id), tablet_uid(in_uid) {}
91
92
    bool operator<(const TabletInfo& right) const {
93
        if (tablet_id != right.tablet_id) {
94
            return tablet_id < right.tablet_id;
95
        } else {
96
            return tablet_uid < right.tablet_uid;
97
        }
98
    }
99
100
    std::string to_string() const {
101
        std::stringstream ss;
102
        ss << tablet_id << "." << tablet_uid.to_string();
103
        return ss.str();
104
    }
105
106
    TTabletId tablet_id;
107
    UniqueId tablet_uid;
108
};
109
110
struct TabletSize {
111
    TabletSize(TTabletId in_tablet_id, size_t in_tablet_size)
112
0
            : tablet_id(in_tablet_id), tablet_size(in_tablet_size) {}
113
114
    TTabletId tablet_id;
115
    size_t tablet_size;
116
};
117
118
// Define all data types supported by Field.
119
// If new filed_type is defined, not only new TypeInfo may need be defined,
120
// but also some functions like get_type_info in types.cpp need to be changed.
121
enum class FieldType {
122
    OLAP_FIELD_TYPE_TINYINT = 1, // MYSQL_TYPE_TINY
123
    OLAP_FIELD_TYPE_UNSIGNED_TINYINT = 2,
124
    OLAP_FIELD_TYPE_SMALLINT = 3, // MYSQL_TYPE_SHORT
125
    OLAP_FIELD_TYPE_UNSIGNED_SMALLINT = 4,
126
    OLAP_FIELD_TYPE_INT = 5, // MYSQL_TYPE_LONG
127
    OLAP_FIELD_TYPE_UNSIGNED_INT = 6,
128
    OLAP_FIELD_TYPE_BIGINT = 7, // MYSQL_TYPE_LONGLONG
129
    OLAP_FIELD_TYPE_UNSIGNED_BIGINT = 8,
130
    OLAP_FIELD_TYPE_LARGEINT = 9,
131
    OLAP_FIELD_TYPE_FLOAT = 10,  // MYSQL_TYPE_FLOAT
132
    OLAP_FIELD_TYPE_DOUBLE = 11, // MYSQL_TYPE_DOUBLE
133
    OLAP_FIELD_TYPE_DISCRETE_DOUBLE = 12,
134
    OLAP_FIELD_TYPE_CHAR = 13,     // MYSQL_TYPE_STRING
135
    OLAP_FIELD_TYPE_DATE = 14,     // MySQL_TYPE_NEWDATE
136
    OLAP_FIELD_TYPE_DATETIME = 15, // MySQL_TYPE_DATETIME
137
    OLAP_FIELD_TYPE_DECIMAL = 16,  // DECIMAL, using different store format against MySQL
138
    OLAP_FIELD_TYPE_VARCHAR = 17,
139
140
    OLAP_FIELD_TYPE_STRUCT = 18,  // Struct
141
    OLAP_FIELD_TYPE_ARRAY = 19,   // ARRAY
142
    OLAP_FIELD_TYPE_MAP = 20,     // Map
143
    OLAP_FIELD_TYPE_UNKNOWN = 21, // UNKNOW OLAP_FIELD_TYPE_STRING
144
    OLAP_FIELD_TYPE_NONE = 22,
145
    OLAP_FIELD_TYPE_HLL = 23,
146
    OLAP_FIELD_TYPE_BOOL = 24,
147
    OLAP_FIELD_TYPE_OBJECT = 25,
148
    OLAP_FIELD_TYPE_STRING = 26,
149
    OLAP_FIELD_TYPE_QUANTILE_STATE = 27,
150
    OLAP_FIELD_TYPE_DATEV2 = 28,
151
    OLAP_FIELD_TYPE_DATETIMEV2 = 29,
152
    OLAP_FIELD_TYPE_TIMEV2 = 30,
153
    OLAP_FIELD_TYPE_DECIMAL32 = 31,
154
    OLAP_FIELD_TYPE_DECIMAL64 = 32,
155
    OLAP_FIELD_TYPE_DECIMAL128I = 33,
156
    OLAP_FIELD_TYPE_JSONB = 34,
157
    OLAP_FIELD_TYPE_VARIANT = 35,
158
    OLAP_FIELD_TYPE_AGG_STATE = 36,
159
    OLAP_FIELD_TYPE_DECIMAL256 = 37,
160
    OLAP_FIELD_TYPE_IPV4 = 38,
161
    OLAP_FIELD_TYPE_IPV6 = 39,
162
};
163
164
// Define all aggregation methods supported by Field
165
// Note that in practice, not all types can use all the following aggregation methods
166
// For example, it is meaningless to use SUM for the string type (but it will not cause the program to crash)
167
// The implementation of the Field class does not perform such checks, and should be constrained when creating the table
168
enum class FieldAggregationMethod {
169
    OLAP_FIELD_AGGREGATION_NONE = 0,
170
    OLAP_FIELD_AGGREGATION_SUM = 1,
171
    OLAP_FIELD_AGGREGATION_MIN = 2,
172
    OLAP_FIELD_AGGREGATION_MAX = 3,
173
    OLAP_FIELD_AGGREGATION_REPLACE = 4,
174
    OLAP_FIELD_AGGREGATION_HLL_UNION = 5,
175
    OLAP_FIELD_AGGREGATION_UNKNOWN = 6,
176
    OLAP_FIELD_AGGREGATION_BITMAP_UNION = 7,
177
    // Replace if and only if added value is not null
178
    OLAP_FIELD_AGGREGATION_REPLACE_IF_NOT_NULL = 8,
179
    OLAP_FIELD_AGGREGATION_QUANTILE_UNION = 9,
180
    OLAP_FIELD_AGGREGATION_GENERIC = 10
181
};
182
183
enum class PushType {
184
    PUSH_NORMAL = 1,          // for broker/hadoop load, not used any more
185
    PUSH_FOR_DELETE = 2,      // for delete
186
    PUSH_FOR_LOAD_DELETE = 3, // not used any more
187
    PUSH_NORMAL_V2 = 4,       // for spark load
188
};
189
190
constexpr bool field_is_slice_type(const FieldType& field_type) {
191
    return field_type == FieldType::OLAP_FIELD_TYPE_VARCHAR ||
192
           field_type == FieldType::OLAP_FIELD_TYPE_CHAR ||
193
           field_type == FieldType::OLAP_FIELD_TYPE_STRING;
194
}
195
196
0
constexpr bool field_is_numeric_type(const FieldType& field_type) {
197
0
    return field_type == FieldType::OLAP_FIELD_TYPE_INT ||
198
0
           field_type == FieldType::OLAP_FIELD_TYPE_UNSIGNED_INT ||
199
0
           field_type == FieldType::OLAP_FIELD_TYPE_BIGINT ||
200
0
           field_type == FieldType::OLAP_FIELD_TYPE_SMALLINT ||
201
0
           field_type == FieldType::OLAP_FIELD_TYPE_UNSIGNED_TINYINT ||
202
0
           field_type == FieldType::OLAP_FIELD_TYPE_UNSIGNED_SMALLINT ||
203
0
           field_type == FieldType::OLAP_FIELD_TYPE_TINYINT ||
204
0
           field_type == FieldType::OLAP_FIELD_TYPE_DOUBLE ||
205
0
           field_type == FieldType::OLAP_FIELD_TYPE_FLOAT ||
206
0
           field_type == FieldType::OLAP_FIELD_TYPE_DATE ||
207
0
           field_type == FieldType::OLAP_FIELD_TYPE_DATEV2 ||
208
0
           field_type == FieldType::OLAP_FIELD_TYPE_DATETIME ||
209
0
           field_type == FieldType::OLAP_FIELD_TYPE_DATETIMEV2 ||
210
0
           field_type == FieldType::OLAP_FIELD_TYPE_LARGEINT ||
211
0
           field_type == FieldType::OLAP_FIELD_TYPE_DECIMAL ||
212
0
           field_type == FieldType::OLAP_FIELD_TYPE_DECIMAL32 ||
213
0
           field_type == FieldType::OLAP_FIELD_TYPE_DECIMAL64 ||
214
0
           field_type == FieldType::OLAP_FIELD_TYPE_DECIMAL128I ||
215
0
           field_type == FieldType::OLAP_FIELD_TYPE_DECIMAL256 ||
216
0
           field_type == FieldType::OLAP_FIELD_TYPE_BOOL ||
217
0
           field_type == FieldType::OLAP_FIELD_TYPE_IPV4 ||
218
0
           field_type == FieldType::OLAP_FIELD_TYPE_IPV6;
219
0
}
220
221
// <start_version_id, end_version_id>, such as <100, 110>
222
//using Version = std::pair<TupleVersion, TupleVersion>;
223
224
struct Version {
225
    int64_t first;
226
    int64_t second;
227
228
1.82M
    Version(int64_t first_, int64_t second_) : first(first_), second(second_) {}
229
7.74k
    Version() : first(0), second(0) {}
230
231
    static Version mock() {
232
        // Every time SchemaChange is used for external rowing, some temporary versions (such as 999, 1000, 1001) will be written, in order to avoid Cache conflicts, temporary
233
        // The version number takes a BIG NUMBER plus the version number of the current SchemaChange
234
        return Version(1 << 28, 1 << 29);
235
    }
236
237
    friend std::ostream& operator<<(std::ostream& os, const Version& version);
238
239
    bool operator!=(const Version& rhs) const { return first != rhs.first || second != rhs.second; }
240
241
358k
    bool operator==(const Version& rhs) const { return first == rhs.first && second == rhs.second; }
242
243
    bool contains(const Version& other) const {
244
        return first <= other.first && second >= other.second;
245
    }
246
247
817
    std::string to_string() const { return fmt::format("[{}-{}]", first, second); }
248
};
249
250
using Versions = std::vector<Version>;
251
252
10
inline std::ostream& operator<<(std::ostream& os, const Version& version) {
253
10
    return os << version.to_string();
254
10
}
255
256
inline std::ostream& operator<<(std::ostream& os, const Versions& versions) {
257
    for (auto& version : versions) {
258
        os << version;
259
    }
260
    return os;
261
}
262
263
// used for hash-struct of hash_map<Version, Rowset*>.
264
struct HashOfVersion {
265
11.6k
    size_t operator()(const Version& version) const {
266
11.6k
        size_t seed = 0;
267
11.6k
        seed = HashUtil::hash64(&version.first, sizeof(version.first), seed);
268
11.6k
        seed = HashUtil::hash64(&version.second, sizeof(version.second), seed);
269
11.6k
        return seed;
270
11.6k
    }
271
};
272
273
// It is used to represent Graph vertex.
274
struct Vertex {
275
    int64_t value = 0;
276
    std::list<int64_t> edges;
277
278
    Vertex(int64_t v) : value(v) {}
279
};
280
281
class Field;
282
class WrapperField;
283
using KeyRange = std::pair<WrapperField*, WrapperField*>;
284
285
// ReaderStatistics used to collect statistics when scan data from storage
286
struct OlapReaderStatistics {
287
    int64_t io_ns = 0;
288
    int64_t compressed_bytes_read = 0;
289
290
    int64_t decompress_ns = 0;
291
    int64_t uncompressed_bytes_read = 0;
292
293
    // total read bytes in memory
294
    int64_t bytes_read = 0;
295
296
    int64_t block_fetch_ns = 0; // time of rowset reader's `next_batch()` call
297
    int64_t block_load_ns = 0;
298
    int64_t blocks_load = 0;
299
    // Not used any more, will be removed after non-vectorized code is removed
300
    int64_t block_seek_num = 0;
301
    // Not used any more, will be removed after non-vectorized code is removed
302
    int64_t block_seek_ns = 0;
303
304
    // block_load_ns
305
    //      block_init_ns
306
    //          block_init_seek_ns
307
    //          generate_row_ranges_ns
308
    //      predicate_column_read_ns
309
    //          predicate_column_read_seek_ns
310
    //      lazy_read_ns
311
    //          block_lazy_read_seek_ns
312
    int64_t block_init_ns = 0;
313
    int64_t block_init_seek_num = 0;
314
    int64_t block_init_seek_ns = 0;
315
    int64_t predicate_column_read_ns = 0;
316
    int64_t non_predicate_read_ns = 0;
317
    int64_t predicate_column_read_seek_num = 0;
318
    int64_t predicate_column_read_seek_ns = 0;
319
    int64_t lazy_read_ns = 0;
320
    int64_t block_lazy_read_seek_num = 0;
321
    int64_t block_lazy_read_seek_ns = 0;
322
323
    int64_t raw_rows_read = 0;
324
325
    int64_t rows_vec_cond_filtered = 0;
326
    int64_t rows_short_circuit_cond_filtered = 0;
327
    int64_t rows_expr_cond_filtered = 0;
328
    int64_t vec_cond_input_rows = 0;
329
    int64_t short_circuit_cond_input_rows = 0;
330
    int64_t expr_cond_input_rows = 0;
331
    int64_t rows_vec_del_cond_filtered = 0;
332
    int64_t vec_cond_ns = 0;
333
    int64_t short_cond_ns = 0;
334
    int64_t expr_filter_ns = 0;
335
    int64_t output_col_ns = 0;
336
    int64_t rows_key_range_filtered = 0;
337
    int64_t rows_stats_filtered = 0;
338
    int64_t rows_stats_rp_filtered = 0;
339
    int64_t rows_bf_filtered = 0;
340
    int64_t rows_dict_filtered = 0;
341
    // Including the number of rows filtered out according to the Delete information in the Tablet,
342
    // and the number of rows filtered for marked deleted rows under the unique key model.
343
    // This metric is mainly used to record the number of rows filtered by the delete condition in Segment V1,
344
    // and it is also used to record the replaced rows in the Unique key model in the "Reader" class.
345
    // In segmentv2, if you want to get all filtered rows, you need the sum of "rows_del_filtered" and "rows_conditions_filtered".
346
    int64_t rows_del_filtered = 0;
347
    int64_t rows_del_by_bitmap = 0;
348
    // the number of rows filtered by various column indexes.
349
    int64_t rows_conditions_filtered = 0;
350
    int64_t generate_row_ranges_by_keys_ns = 0;
351
    int64_t generate_row_ranges_by_column_conditions_ns = 0;
352
    int64_t generate_row_ranges_by_bf_ns = 0;
353
    int64_t generate_row_ranges_by_zonemap_ns = 0;
354
    int64_t generate_row_ranges_by_dict_ns = 0;
355
356
    int64_t index_load_ns = 0;
357
358
    int64_t total_pages_num = 0;
359
    int64_t cached_pages_num = 0;
360
361
    int64_t rows_bitmap_index_filtered = 0;
362
    int64_t bitmap_index_filter_timer = 0;
363
364
    int64_t rows_inverted_index_filtered = 0;
365
    int64_t inverted_index_filter_timer = 0;
366
    int64_t inverted_index_query_timer = 0;
367
    int64_t inverted_index_query_cache_hit = 0;
368
    int64_t inverted_index_query_cache_miss = 0;
369
    int64_t inverted_index_query_null_bitmap_timer = 0;
370
    int64_t inverted_index_query_bitmap_copy_timer = 0;
371
    int64_t inverted_index_searcher_open_timer = 0;
372
    int64_t inverted_index_searcher_search_timer = 0;
373
    int64_t inverted_index_searcher_cache_hit = 0;
374
    int64_t inverted_index_searcher_cache_miss = 0;
375
    int64_t inverted_index_downgrade_count = 0;
376
    InvertedIndexStatistics inverted_index_stats;
377
378
    int64_t output_index_result_column_timer = 0;
379
    // number of segment filtered by column stat when creating seg iterator
380
    int64_t filtered_segment_number = 0;
381
    // total number of segment
382
    int64_t total_segment_number = 0;
383
384
    io::FileCacheStatistics file_cache_stats;
385
    int64_t load_segments_timer = 0;
386
387
    int64_t collect_iterator_merge_next_timer = 0;
388
    int64_t collect_iterator_normal_next_timer = 0;
389
    int64_t delete_bitmap_get_agg_ns = 0;
390
391
    int64_t tablet_reader_init_timer_ns = 0;
392
    int64_t tablet_reader_capture_rs_readers_timer_ns = 0;
393
    int64_t tablet_reader_init_return_columns_timer_ns = 0;
394
    int64_t tablet_reader_init_keys_param_timer_ns = 0;
395
    int64_t tablet_reader_init_orderby_keys_param_timer_ns = 0;
396
    int64_t tablet_reader_init_conditions_param_timer_ns = 0;
397
    int64_t tablet_reader_init_delete_condition_param_timer_ns = 0;
398
    int64_t block_reader_vcollect_iter_init_timer_ns = 0;
399
    int64_t block_reader_rs_readers_init_timer_ns = 0;
400
    int64_t block_reader_build_heap_init_timer_ns = 0;
401
402
    int64_t rowset_reader_get_segment_iterators_timer_ns = 0;
403
    int64_t rowset_reader_create_iterators_timer_ns = 0;
404
    int64_t rowset_reader_init_iterators_timer_ns = 0;
405
    int64_t rowset_reader_load_segments_timer_ns = 0;
406
407
    int64_t segment_iterator_init_timer_ns = 0;
408
    int64_t segment_iterator_init_return_column_iterators_timer_ns = 0;
409
    int64_t segment_iterator_init_bitmap_index_iterators_timer_ns = 0;
410
    int64_t segment_iterator_init_inverted_index_iterators_timer_ns = 0;
411
412
    int64_t segment_create_column_readers_timer_ns = 0;
413
    int64_t segment_load_index_timer_ns = 0;
414
};
415
416
using ColumnId = uint32_t;
417
// Column unique id set
418
using UniqueIdSet = std::set<uint32_t>;
419
// Column unique Id -> column id map
420
using UniqueIdToColumnIdMap = std::map<ColumnId, ColumnId>;
421
422
// 8 bit rowset id version
423
// 56 bit, inc number from 1
424
// 128 bit backend uid, it is a uuid bit, id version
425
struct RowsetId {
426
    int8_t version = 0;
427
    int64_t hi = 0;
428
    int64_t mi = 0;
429
    int64_t lo = 0;
430
431
11.4k
    void init(std::string_view rowset_id_str) {
432
        // for new rowsetid its a 48 hex string
433
        // if the len < 48, then it is an old format rowset id
434
11.4k
        if (rowset_id_str.length() < 48) [[unlikely]] {
435
69
            int64_t high;
436
69
            auto [_, ec] = std::from_chars(rowset_id_str.data(),
437
69
                                           rowset_id_str.data() + rowset_id_str.length(), high);
438
69
            if (ec != std::errc {}) [[unlikely]] {
439
1
                if (config::force_regenerate_rowsetid_on_start_error) {
440
1
                    LOG(WARNING) << "failed to init rowset id: " << rowset_id_str;
441
1
                    high = MAX_ROWSET_ID - 1;
442
1
                } else {
443
0
                    throw Exception(
444
0
                            Status::FatalError("failed to init rowset id: {}", rowset_id_str));
445
0
                }
446
1
            }
447
69
            init(1, high, 0, 0);
448
11.3k
        } else {
449
11.3k
            int64_t high = 0;
450
11.3k
            int64_t middle = 0;
451
11.3k
            int64_t low = 0;
452
11.3k
            from_hex(&high, rowset_id_str.substr(0, 16));
453
11.3k
            from_hex(&middle, rowset_id_str.substr(16, 16));
454
11.3k
            from_hex(&low, rowset_id_str.substr(32, 16));
455
11.3k
            init(high >> 56, high & LOW_56_BITS, middle, low);
456
11.3k
        }
457
11.4k
    }
458
459
    // to compatible with old version
460
1.09k
    void init(int64_t rowset_id) { init(1, rowset_id, 0, 0); }
461
462
28.6k
    void init(int64_t id_version, int64_t high, int64_t middle, int64_t low) {
463
28.6k
        version = id_version;
464
28.6k
        if (UNLIKELY(high >= MAX_ROWSET_ID)) {
465
0
            throw Exception(Status::FatalError("inc rowsetid is too large:{}", high));
466
0
        }
467
28.6k
        hi = (id_version << 56) + (high & LOW_56_BITS);
468
28.6k
        mi = middle;
469
28.6k
        lo = low;
470
28.6k
    }
471
472
45.8k
    std::string to_string() const {
473
45.8k
        if (version < 2) {
474
25.7k
            return std::to_string(hi & LOW_56_BITS);
475
25.7k
        } else {
476
20.1k
            char buf[48];
477
20.1k
            to_hex(hi, buf);
478
20.1k
            to_hex(mi, buf + 16);
479
20.1k
            to_hex(lo, buf + 32);
480
20.1k
            return {buf, 48};
481
20.1k
        }
482
45.8k
    }
483
484
    // std::unordered_map need this api
485
357k
    bool operator==(const RowsetId& rhs) const {
486
357k
        return hi == rhs.hi && mi == rhs.mi && lo == rhs.lo;
487
357k
    }
488
489
    bool operator!=(const RowsetId& rhs) const {
490
        return hi != rhs.hi || mi != rhs.mi || lo != rhs.lo;
491
    }
492
493
44.8M
    bool operator<(const RowsetId& rhs) const {
494
44.8M
        if (hi != rhs.hi) {
495
4.55M
            return hi < rhs.hi;
496
40.2M
        } else if (mi != rhs.mi) {
497
0
            return mi < rhs.mi;
498
40.2M
        } else {
499
40.2M
            return lo < rhs.lo;
500
40.2M
        }
501
44.8M
    }
502
503
949
    friend std::ostream& operator<<(std::ostream& out, const RowsetId& rowset_id) {
504
949
        out << rowset_id.to_string();
505
949
        return out;
506
949
    }
507
};
508
509
using RowsetIdUnorderedSet = std::unordered_set<RowsetId>;
510
511
// Extract rowset id from filename, return uninitialized rowset id if filename is invalid
512
483
inline RowsetId extract_rowset_id(std::string_view filename) {
513
483
    RowsetId rowset_id;
514
483
    if (filename.ends_with(".dat")) {
515
        // filename format: {rowset_id}_{segment_num}.dat
516
243
        auto end = filename.find('_');
517
243
        if (end == std::string::npos) {
518
0
            return rowset_id;
519
0
        }
520
243
        rowset_id.init(filename.substr(0, end));
521
243
        return rowset_id;
522
243
    }
523
240
    if (filename.ends_with(".idx")) {
524
        // filename format: {rowset_id}_{segment_num}_{index_id}.idx
525
240
        auto end = filename.find('_');
526
240
        if (end == std::string::npos) {
527
0
            return rowset_id;
528
0
        }
529
240
        rowset_id.init(filename.substr(0, end));
530
240
        return rowset_id;
531
240
    }
532
0
    return rowset_id;
533
240
}
534
535
class DeleteBitmap;
536
// merge on write context
537
struct MowContext {
538
    MowContext(int64_t version, int64_t txnid, const RowsetIdUnorderedSet& ids,
539
               std::vector<RowsetSharedPtr> rowset_ptrs, std::shared_ptr<DeleteBitmap> db)
540
            : max_version(version),
541
              txn_id(txnid),
542
              rowset_ids(ids),
543
              rowset_ptrs(std::move(rowset_ptrs)),
544
8
              delete_bitmap(std::move(db)) {}
545
    int64_t max_version;
546
    int64_t txn_id;
547
    const RowsetIdUnorderedSet& rowset_ids;
548
    std::vector<RowsetSharedPtr> rowset_ptrs;
549
    std::shared_ptr<DeleteBitmap> delete_bitmap;
550
};
551
552
// used for controll compaction
553
struct VersionWithTime {
554
    std::atomic<int64_t> version;
555
    int64_t update_ts;
556
557
    VersionWithTime() : version(0), update_ts(MonotonicMillis()) {}
558
559
    void update_version_monoto(int64_t new_version) {
560
        int64_t cur_version = version.load(std::memory_order_relaxed);
561
        while (cur_version < new_version) {
562
            if (version.compare_exchange_strong(cur_version, new_version, std::memory_order_relaxed,
563
                                                std::memory_order_relaxed)) {
564
                update_ts = MonotonicMillis();
565
                break;
566
            }
567
        }
568
    }
569
};
570
571
} // namespace doris
572
573
// This intended to be a "good" hash function.  It may change from time to time.
574
template <>
575
struct std::hash<doris::RowsetId> {
576
1.73k
    size_t operator()(const doris::RowsetId& rowset_id) const {
577
1.73k
        size_t seed = 0;
578
1.73k
        seed = doris::HashUtil::xxHash64WithSeed((const char*)&rowset_id.hi, sizeof(rowset_id.hi),
579
1.73k
                                                 seed);
580
1.73k
        seed = doris::HashUtil::xxHash64WithSeed((const char*)&rowset_id.mi, sizeof(rowset_id.mi),
581
1.73k
                                                 seed);
582
1.73k
        seed = doris::HashUtil::xxHash64WithSeed((const char*)&rowset_id.lo, sizeof(rowset_id.lo),
583
1.73k
                                                 seed);
584
1.73k
        return seed;
585
1.73k
    }
586
};