Coverage Report

Created: 2026-08-31 16:24

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
be/src/storage/rowset/rowset_meta.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
#ifndef DORIS_BE_SRC_OLAP_ROWSET_ROWSET_META_H
19
#define DORIS_BE_SRC_OLAP_ROWSET_ROWSET_META_H
20
21
#include <gen_cpp/olap_file.pb.h>
22
#include <glog/logging.h>
23
24
#include <atomic>
25
#include <chrono>
26
#include <cstddef>
27
#include <cstdint>
28
#include <iterator>
29
#include <memory>
30
#include <string>
31
#include <vector>
32
33
#include "common/cast_set.h"
34
#include "common/check.h"
35
#include "common/config.h"
36
#include "common/status.h"
37
#include "io/fs/encrypted_fs_factory.h"
38
#include "io/fs/file_system.h"
39
#include "runtime/memory/lru_cache_policy.h"
40
#include "storage/metadata_adder.h"
41
#include "storage/olap_common.h"
42
#include "storage/rowset/rowset_fwd.h"
43
#include "storage/rowset/rowset_segment_id.h"
44
#include "storage/storage_policy.h"
45
#include "storage/tablet/tablet_fwd.h"
46
#include "util/once.h"
47
48
namespace doris {
49
50
class RowsetSegmentMetaView;
51
class RowsetSegmentMetaRange;
52
53
class RowsetMeta : public MetadataAdder<RowsetMeta> {
54
public:
55
1.11M
    RowsetMeta() = default;
56
    ~RowsetMeta();
57
58
    bool init(std::string_view pb_rowset_meta);
59
60
    bool init(const RowsetMeta* rowset_meta);
61
62
    bool init_from_pb(const RowsetMetaPB& rowset_meta_pb);
63
64
    bool init_from_json(const std::string& json_rowset_meta);
65
66
6
    bool serialize(std::string* value) { return _serialize_to_pb(value); }
67
68
    bool json_rowset_meta(std::string* json_rowset_meta);
69
70
    // If the rowset is a local rowset, return the global local file system.
71
    // Otherwise, return the remote file system corresponding to rowset's resource id.
72
    // Note that if the resource id cannot be found for the corresponding remote file system, nullptr will be returned.
73
    MOCK_FUNCTION io::FileSystemSPtr fs();
74
75
    io::FileSystemSPtr physical_fs();
76
77
    Result<const StorageResource*> remote_storage_resource();
78
79
    void set_remote_storage_resource(StorageResource resource);
80
81
1.93M
    const std::string& resource_id() const { return _rowset_meta_pb.resource_id(); }
82
83
124k
    void set_resource_id(const std::string& resource_id) {
84
124k
        _rowset_meta_pb.set_resource_id(resource_id);
85
124k
    }
86
87
48.4M
    bool is_local() const { return !_rowset_meta_pb.has_resource_id(); }
88
89
    bool has_variant_type_in_schema() const;
90
91
23.5M
    RowsetId rowset_id() const { return _rowset_id; }
92
93
327k
    void set_rowset_id(const RowsetId& rowset_id) {
94
        // rowset id is a required field, just set it to 0
95
327k
        _rowset_meta_pb.set_rowset_id(0);
96
327k
        _rowset_id = rowset_id;
97
327k
        _rowset_meta_pb.set_rowset_id_v2(rowset_id.to_string());
98
327k
    }
99
100
8.63M
    int64_t tablet_id() const { return _rowset_meta_pb.tablet_id(); }
101
102
327k
    void set_tablet_id(int64_t tablet_id) { _rowset_meta_pb.set_tablet_id(tablet_id); }
103
104
1.51k
    int64_t db_id() const { return _rowset_meta_pb.db_id(); }
105
106
197k
    void set_db_id(int64_t db_id) { _rowset_meta_pb.set_db_id(db_id); }
107
108
1.49k
    int64_t table_id() const { return _rowset_meta_pb.table_id(); }
109
110
196k
    void set_table_id(int64_t table_id) { _rowset_meta_pb.set_table_id(table_id); }
111
112
    int64_t index_id() const { return _rowset_meta_pb.index_id(); }
113
114
321k
    void set_index_id(int64_t index_id) { _rowset_meta_pb.set_index_id(index_id); }
115
116
1.45k
    TabletUid tablet_uid() const { return _rowset_meta_pb.tablet_uid(); }
117
118
131k
    void set_tablet_uid(TabletUid tablet_uid) {
119
131k
        *(_rowset_meta_pb.mutable_tablet_uid()) = tablet_uid.to_proto();
120
131k
    }
121
122
341k
    int64_t txn_id() const { return _rowset_meta_pb.txn_id(); }
123
124
320k
    void set_txn_id(int64_t txn_id) { _rowset_meta_pb.set_txn_id(txn_id); }
125
126
124k
    int32_t tablet_schema_hash() const { return _rowset_meta_pb.tablet_schema_hash(); }
127
128
322k
    void set_tablet_schema_hash(int32_t tablet_schema_hash) {
129
322k
        _rowset_meta_pb.set_tablet_schema_hash(tablet_schema_hash);
130
322k
    }
131
132
126
    void mark_row_binlog() { _rowset_meta_pb.set_is_row_binlog(true); }
133
134
141k
    bool is_row_binlog() const {
135
141k
        return _rowset_meta_pb.has_is_row_binlog() && _rowset_meta_pb.is_row_binlog();
136
141k
    }
137
138
2.14M
    RowsetTypePB rowset_type() const { return _rowset_meta_pb.rowset_type(); }
139
140
324k
    void set_rowset_type(RowsetTypePB rowset_type) { _rowset_meta_pb.set_rowset_type(rowset_type); }
141
142
588k
    RowsetStatePB rowset_state() const { return _rowset_meta_pb.rowset_state(); }
143
144
520k
    void set_rowset_state(RowsetStatePB rowset_state) {
145
520k
        _rowset_meta_pb.set_rowset_state(rowset_state);
146
520k
    }
147
148
49.6M
    Version version() const {
149
49.6M
        return {_rowset_meta_pb.start_version(), _rowset_meta_pb.end_version()};
150
49.6M
    }
151
152
258k
    void set_version(Version version) {
153
258k
        _rowset_meta_pb.set_start_version(version.first);
154
258k
        _rowset_meta_pb.set_end_version(version.second);
155
258k
    }
156
157
1.02M
    bool has_version() const {
158
1.02M
        return _rowset_meta_pb.has_start_version() && _rowset_meta_pb.has_end_version();
159
1.02M
    }
160
161
5.11M
    int64_t start_version() const { return _rowset_meta_pb.start_version(); }
162
163
8.15M
    int64_t end_version() const { return _rowset_meta_pb.end_version(); }
164
165
8.63M
    int64_t num_rows() const { return _rowset_meta_pb.num_rows(); }
166
167
344k
    void set_num_rows(int64_t num_rows) { _rowset_meta_pb.set_num_rows(num_rows); }
168
169
217k
    void set_num_segment_rows(const std::vector<uint32_t>& num_segment_rows) {
170
217k
        _rowset_meta_pb.mutable_num_segment_rows()->Assign(num_segment_rows.cbegin(),
171
217k
                                                           num_segment_rows.cend());
172
217k
    }
173
174
135
    void get_num_segment_rows(std::vector<uint32_t>* num_segment_rows) const {
175
135
        num_segment_rows->assign(_rowset_meta_pb.num_segment_rows().cbegin(),
176
135
                                 _rowset_meta_pb.num_segment_rows().cend());
177
135
    }
178
179
143k
    auto& get_num_segment_rows() const { return _rowset_meta_pb.num_segment_rows(); }
180
181
10.6M
    int64_t total_disk_size() const { return _rowset_meta_pb.total_disk_size(); }
182
183
353k
    void set_total_disk_size(int64_t total_disk_size) {
184
353k
        _rowset_meta_pb.set_total_disk_size(total_disk_size);
185
353k
    }
186
187
9.01M
    int64_t data_disk_size() const { return _rowset_meta_pb.data_disk_size(); }
188
189
344k
    void set_data_disk_size(int64_t data_disk_size) {
190
344k
        _rowset_meta_pb.set_data_disk_size(data_disk_size);
191
344k
    }
192
193
2.46M
    int64_t index_disk_size() const { return _rowset_meta_pb.index_disk_size(); }
194
195
344k
    void set_index_disk_size(int64_t index_disk_size) {
196
344k
        _rowset_meta_pb.set_index_disk_size(index_disk_size);
197
344k
    }
198
199
0
    void zone_maps(std::vector<::doris::ZoneMap>* zone_maps) {
200
0
        for (const ::doris::ZoneMap& zone_map : _rowset_meta_pb.zone_maps()) {
201
0
            zone_maps->push_back(zone_map);
202
0
        }
203
0
    }
204
205
0
    void set_zone_maps(const std::vector<::doris::ZoneMap>& zone_maps) {
206
0
        for (const ::doris::ZoneMap& zone_map : zone_maps) {
207
0
            ::doris::ZoneMap* new_zone_map = _rowset_meta_pb.add_zone_maps();
208
0
            *new_zone_map = zone_map;
209
0
        }
210
0
    }
211
212
0
    void add_zone_map(const ::doris::ZoneMap& zone_map) {
213
0
        ::doris::ZoneMap* new_zone_map = _rowset_meta_pb.add_zone_maps();
214
0
        *new_zone_map = zone_map;
215
0
    }
216
217
4.96M
    bool has_delete_predicate() const { return _rowset_meta_pb.has_delete_predicate(); }
218
219
8.11k
    const DeletePredicatePB& delete_predicate() const { return _rowset_meta_pb.delete_predicate(); }
220
221
0
    DeletePredicatePB* mutable_delete_predicate() {
222
0
        return _rowset_meta_pb.mutable_delete_predicate();
223
0
    }
224
225
3.24k
    void set_delete_predicate(DeletePredicatePB delete_predicate) {
226
3.24k
        DeletePredicatePB* new_delete_condition = _rowset_meta_pb.mutable_delete_predicate();
227
3.24k
        *new_delete_condition = std::move(delete_predicate);
228
3.24k
    }
229
230
8.75k
    bool empty() const { return _rowset_meta_pb.empty(); }
231
232
342k
    void set_empty(bool empty) { _rowset_meta_pb.set_empty(empty); }
233
234
178
    PUniqueId load_id() const { return _rowset_meta_pb.load_id(); }
235
236
301k
    void set_load_id(PUniqueId load_id) {
237
301k
        PUniqueId* new_load_id = _rowset_meta_pb.mutable_load_id();
238
301k
        new_load_id->set_hi(load_id.hi());
239
301k
        new_load_id->set_lo(load_id.lo());
240
301k
    }
241
242
196k
    void set_job_id(const std::string& job_id) { _rowset_meta_pb.set_job_id(job_id); }
243
244
0
    const std::string& job_id() const { return _rowset_meta_pb.job_id(); }
245
246
0
    bool delete_flag() const { return _rowset_meta_pb.delete_flag(); }
247
248
130k
    int64_t creation_time() const { return _rowset_meta_pb.creation_time(); }
249
250
342k
    void set_creation_time(int64_t creation_time) {
251
342k
        return _rowset_meta_pb.set_creation_time(creation_time);
252
342k
    }
253
254
524k
    int64_t stale_at() const {
255
524k
        int64_t stale_time = _stale_at_s.load();
256
524k
        return stale_time > 0 ? stale_time : _rowset_meta_pb.creation_time();
257
524k
    }
258
259
13.7k
    bool has_stale_at() const { return _stale_at_s.load() > 0; }
260
261
86.3k
    void set_stale_at(int64_t stale_at) { _stale_at_s.store(stale_at); }
262
263
962
    int64_t partition_id() const { return _rowset_meta_pb.partition_id(); }
264
265
321k
    void set_partition_id(int64_t partition_id) {
266
321k
        return _rowset_meta_pb.set_partition_id(partition_id);
267
321k
    }
268
269
32.6M
    int64_t num_segments() const {
270
32.6M
        DCHECK(_rowset_meta_pb.segment_ids_size() == 0 ||
271
32.6M
               _rowset_meta_pb.segment_ids_size() == _rowset_meta_pb.num_segments());
272
32.6M
        return _rowset_meta_pb.num_segments();
273
32.6M
    }
274
275
377k
    void set_num_segments(int64_t num_segments) { _rowset_meta_pb.set_num_segments(num_segments); }
276
277
4.53M
    bool has_segment_ids() const { return _rowset_meta_pb.segment_ids_size() > 0; }
278
279
230k
    const auto& segment_ids() const { return _rowset_meta_pb.segment_ids(); }
280
281
    void set_segment_ids(const std::vector<int64_t>& segment_ids);
282
283
3.68M
    int64_t segment_id(size_t pos) const {
284
3.68M
        DORIS_CHECK_LT(pos, cast_set<size_t>(num_segments()));
285
3.68M
        return has_segment_ids() ? _rowset_meta_pb.segment_ids(cast_set<int>(pos))
286
3.68M
                                 : cast_set<int64_t>(pos);
287
3.68M
    }
288
289
3.68M
    RowsetSegmentRef segment_ref(size_t pos) const { return {pos, segment_id(pos)}; }
290
291
    RowsetSegmentMetaView segment(size_t pos) const;
292
293
    RowsetSegmentMetaRange segments() const;
294
295
    size_t position_of(int64_t seg_id) const;
296
297
    // Convert to RowsetMetaPB, skip_schema is only used by cloud to separate schema from rowset meta.
298
    void to_rowset_pb(RowsetMetaPB* rs_meta_pb, bool skip_schema = false) const;
299
300
    // Convert to RowsetMetaPB, skip_schema is only used by cloud to separate schema from rowset meta.
301
    RowsetMetaPB get_rowset_pb(bool skip_schema = false) const;
302
303
0
    inline DeletePredicatePB* mutable_delete_pred_pb() {
304
0
        return _rowset_meta_pb.mutable_delete_predicate();
305
0
    }
306
307
11.7k
    bool is_singleton_delta() const {
308
11.7k
        return has_version() && _rowset_meta_pb.start_version() == _rowset_meta_pb.end_version();
309
11.7k
    }
310
311
    // Some time, we may check if this rowset is in rowset meta manager's meta by using RowsetMetaManager::check_rowset_meta.
312
    // But, this check behavior may cost a lot of time when it is frequent.
313
    // If we explicitly remove this rowset from rowset meta manager's meta, we can set _is_removed_from_rowset_meta to true,
314
    // And next time when we want to check if this rowset is in rowset mata manager's meta, we can
315
    // check is_remove_from_rowset_meta() first.
316
114
    void set_remove_from_rowset_meta() { _is_removed_from_rowset_meta = true; }
317
318
114
    bool is_remove_from_rowset_meta() const { return _is_removed_from_rowset_meta; }
319
320
32.2k
    SegmentsOverlapPB segments_overlap() const { return _rowset_meta_pb.segments_overlap_pb(); }
321
322
406k
    void set_segments_overlap(SegmentsOverlapPB segments_overlap) {
323
406k
        _rowset_meta_pb.set_segments_overlap_pb(segments_overlap);
324
406k
    }
325
326
679k
    static bool comparator(const RowsetMetaSharedPtr& left, const RowsetMetaSharedPtr& right) {
327
679k
        return left->end_version() < right->end_version();
328
679k
    }
329
330
    // return true if segments in this rowset has overlapping data.
331
    // this is not same as `segments_overlap()` method.
332
    // `segments_overlap()` only return the value of "segments_overlap" field in rowset meta,
333
    // but "segments_overlap" may be UNKNOWN.
334
    //
335
    // Returns true if all of the following conditions are met
336
    // 1. the rowset contains more than one segment
337
    // 2. the rowset's start version == end version (non-singleton rowset was generated by compaction process
338
    //    which always produces non-overlapped segments)
339
    // 3. segments_overlap() flag is not NONOVERLAPPING (OVERLAP_UNKNOWN and OVERLAPPING are OK)
340
    // 4. relaxing is_singleton_delta() for is_row_binlog as Row-binlog LMax quick merge does produce
341
    // non-singleton overlapping segments, but it writes segments_overlap = OVERLAPPING explicitly
342
5.61M
    bool is_segments_overlapping() const {
343
5.61M
        return num_segments() > 1 && segments_overlap() != NONOVERLAPPING &&
344
5.61M
               (is_singleton_delta() || (is_row_binlog() && segments_overlap() == OVERLAPPING));
345
5.61M
    }
346
347
32
    bool produced_by_compaction() const {
348
32
        return has_version() &&
349
32
               (start_version() < end_version() ||
350
32
                (start_version() == end_version() && segments_overlap() == NONOVERLAPPING));
351
32
    }
352
353
    // get the compaction score of this rowset.
354
    // if segments are overlapping, the score equals to the number of segments,
355
    // otherwise, score is 1.
356
2.54M
    uint32_t get_compaction_score() const {
357
2.54M
        uint32_t score = 0;
358
2.54M
        if (!is_segments_overlapping()) {
359
2.54M
            score = 1;
360
2.54M
        } else {
361
1.92k
            auto num_seg = num_segments();
362
1.92k
            DCHECK_GT(num_seg, 0);
363
1.92k
            score = cast_set<uint32_t>(num_seg);
364
1.92k
            CHECK(score > 0);
365
1.92k
        }
366
2.54M
        return score;
367
2.54M
    }
368
369
72.1k
    uint32_t get_merge_way_num() const {
370
72.1k
        uint32_t way_num = 0;
371
72.5k
        if (!is_segments_overlapping()) {
372
72.5k
            if (num_segments() == 0) {
373
47.8k
                way_num = 0;
374
47.8k
            } else {
375
24.6k
                way_num = 1;
376
24.6k
            }
377
18.4E
        } else {
378
18.4E
            auto num_seg = num_segments();
379
18.4E
            DCHECK_GT(num_seg, 0);
380
381
18.4E
            way_num = cast_set<uint32_t>(num_seg);
382
18.4E
            CHECK(way_num > 0);
383
18.4E
        }
384
72.1k
        return way_num;
385
72.1k
    }
386
387
4.18M
    void get_segments_key_bounds(std::vector<KeyBoundsPB>* segments_key_bounds) const {
388
4.18M
        for (const KeyBoundsPB& key_range : _rowset_meta_pb.segments_key_bounds()) {
389
3.98M
            segments_key_bounds->push_back(key_range);
390
3.98M
        }
391
4.18M
    }
392
393
1.49k
    auto& get_segments_key_bounds() const { return _rowset_meta_pb.segments_key_bounds(); }
394
395
5.14M
    bool is_segments_key_bounds_truncated() const {
396
5.14M
        return _rowset_meta_pb.has_segments_key_bounds_truncated() &&
397
5.14M
               _rowset_meta_pb.segments_key_bounds_truncated();
398
5.14M
    }
399
400
274k
    void set_segments_key_bounds_truncated(bool truncated) {
401
274k
        _rowset_meta_pb.set_segments_key_bounds_truncated(truncated);
402
274k
    }
403
404
    // When true, `segments_key_bounds` holds a single aggregated
405
    // [rowset_min, rowset_max] entry instead of per-segment bounds.
406
4.16M
    bool is_segments_key_bounds_aggregated() const {
407
4.16M
        return _rowset_meta_pb.has_segments_key_bounds_aggregated() &&
408
4.16M
               _rowset_meta_pb.segments_key_bounds_aggregated();
409
4.16M
    }
410
411
217k
    void set_segments_key_bounds_aggregated(bool aggregated) {
412
217k
        _rowset_meta_pb.set_segments_key_bounds_aggregated(aggregated);
413
217k
    }
414
415
978k
    bool get_first_segment_key_bound(KeyBoundsPB* key_bounds) {
416
        // for compatibility, old version has not segment key bounds
417
978k
        if (_rowset_meta_pb.segments_key_bounds_size() == 0) {
418
0
            return false;
419
0
        }
420
978k
        *key_bounds = *_rowset_meta_pb.segments_key_bounds().begin();
421
978k
        return true;
422
978k
    }
423
424
659k
    bool get_last_segment_key_bound(KeyBoundsPB* key_bounds) {
425
659k
        if (_rowset_meta_pb.segments_key_bounds_size() == 0) {
426
0
            return false;
427
0
        }
428
659k
        *key_bounds = *_rowset_meta_pb.segments_key_bounds().rbegin();
429
659k
        return true;
430
659k
    }
431
432
    // If `aggregate_into_single` is true, collapse per-segment bounds into a single
433
    // [rowset_min, rowset_max] entry and mark this rowset as aggregated.
434
    void set_segments_key_bounds(const std::vector<KeyBoundsPB>& segments_key_bounds,
435
                                 bool aggregate_into_single = false);
436
437
19
    void add_segment_key_bounds(KeyBoundsPB segments_key_bounds) {
438
19
        *_rowset_meta_pb.add_segments_key_bounds() = std::move(segments_key_bounds);
439
19
        set_segments_overlap(OVERLAPPING);
440
19
    }
441
442
323k
    void set_newest_write_timestamp(int64_t timestamp) {
443
323k
        _rowset_meta_pb.set_newest_write_timestamp(timestamp);
444
323k
    }
445
446
709k
    int64_t newest_write_timestamp() const { return _rowset_meta_pb.newest_write_timestamp(); }
447
448
    // for cloud only
449
395
    bool has_visible_ts_ms() const { return _rowset_meta_pb.has_visible_ts_ms(); }
450
388
    int64_t visible_ts_ms() const { return _rowset_meta_pb.visible_ts_ms(); }
451
395
    std::chrono::time_point<std::chrono::system_clock> visible_timestamp() const {
452
395
        using namespace std::chrono;
453
395
        if (has_visible_ts_ms()) {
454
388
            return time_point<system_clock>(milliseconds(visible_ts_ms()));
455
388
        }
456
7
        return system_clock::from_time_t(newest_write_timestamp());
457
395
    }
458
87.0k
    void set_visible_ts_ms(int64_t visible_ts_ms) {
459
87.0k
        _rowset_meta_pb.set_visible_ts_ms(visible_ts_ms);
460
87.0k
    }
461
462
    void set_tablet_schema(const TabletSchemaSPtr& tablet_schema);
463
    void set_tablet_schema(const TabletSchemaPB& tablet_schema);
464
465
6.38M
    const TabletSchemaSPtr& tablet_schema() const { return _schema; }
466
467
194k
    void set_txn_expiration(int64_t expiration) { _rowset_meta_pb.set_txn_expiration(expiration); }
468
469
197k
    void set_compaction_level(int64_t compaction_level) {
470
197k
        _rowset_meta_pb.set_compaction_level(compaction_level);
471
197k
    }
472
473
32.8k
    int64_t compaction_level() { return _rowset_meta_pb.compaction_level(); }
474
475
    // `seg_file_size` MUST be ordered by rowset segment position.
476
    void add_segments_file_size(const std::vector<size_t>& seg_file_size);
477
478
    // Return -1 if segment file size is unknown
479
    int64_t segment_file_size_by_pos(size_t pos) const;
480
481
1.42k
    const auto& segments_file_size() const { return _rowset_meta_pb.segments_file_size(); }
482
483
    // Used for partial update, when publish, partial update may add a new rowset and we should update rowset meta
484
    void merge_rowset_meta(const RowsetMeta& other);
485
486
    InvertedIndexFileInfo inverted_index_file_info_by_pos(size_t pos) const;
487
488
249
    const auto& inverted_index_file_info() const {
489
249
        return _rowset_meta_pb.inverted_index_file_info();
490
249
    }
491
492
    void add_inverted_index_files_info(
493
            const std::vector<const InvertedIndexFileInfo*>& idx_file_info);
494
495
    int64_t get_metadata_size() const override;
496
497
    // Because the member field '_handle' is a raw pointer, use member func 'init' to replace copy ctor
498
    RowsetMeta(const RowsetMeta&) = delete;
499
    RowsetMeta operator=(const RowsetMeta&) = delete;
500
501
    void add_packed_slice_location(const std::string& segment_path,
502
                                   const std::string& packed_file_path, int64_t offset,
503
0
                                   int64_t size, int64_t packed_file_size) {
504
0
        auto* index_map = _rowset_meta_pb.mutable_packed_slice_locations();
505
0
        auto& index_pb = (*index_map)[segment_path];
506
0
        index_pb.set_packed_file_path(packed_file_path);
507
0
        index_pb.set_offset(offset);
508
0
        index_pb.set_size(size);
509
0
        index_pb.set_packed_file_size(packed_file_size);
510
0
    }
511
512
372
    int32_t schema_version() const { return _rowset_meta_pb.schema_version(); }
513
514
0
    std::string debug_string() const { return _rowset_meta_pb.ShortDebugString(); }
515
516
    // Pre-set the encryption algorithm to avoid re-entrant get_tablet calls
517
    // that can cause SingleFlight deadlock during tablet loading.
518
170k
    void set_encryption_algorithm(EncryptionAlgorithmPB algorithm) {
519
170k
        _determine_encryption_once.call(
520
170k
                [algorithm]() -> Result<EncryptionAlgorithmPB> { return algorithm; });
521
170k
    }
522
523
3.12M
    TsoRange commit_tso() const {
524
3.12M
        const auto& commit_tso_pb = _rowset_meta_pb.commit_tso();
525
3.12M
        return {commit_tso_pb.start_tso(), commit_tso_pb.end_tso()};
526
3.12M
    }
527
528
46
    bool has_commit_tso() const { return _rowset_meta_pb.has_commit_tso(); }
529
530
9.61k
    void set_commit_tso(const TsoRange& commit_tso) {
531
9.61k
        auto* commit_tso_pb = _rowset_meta_pb.mutable_commit_tso();
532
9.61k
        commit_tso_pb->set_start_tso(commit_tso.start_tso());
533
9.61k
        commit_tso_pb->set_end_tso(commit_tso.end_tso());
534
9.61k
    }
535
536
225
    void set_commit_tso(int64_t commit_tso) { set_commit_tso({commit_tso, commit_tso}); }
537
538
86.2k
    void set_cloud_fields_after_visible(int64_t visible_version, int64_t version_update_time_ms) {
539
        // Update rowset meta with correct version and visible_ts
540
        // !!ATTENTION!!: this code should be updated if there are more fields
541
        // in rowset meta which will be modified in meta-service when commit_txn in the future
542
86.2k
        set_version({visible_version, visible_version});
543
86.2k
        if (version_update_time_ms > 0) {
544
86.2k
            set_visible_ts_ms(version_update_time_ms);
545
86.2k
        }
546
86.2k
    }
547
548
private:
549
    bool _deserialize_from_pb(std::string_view value);
550
551
    bool _serialize_to_pb(std::string* value);
552
553
    void _init();
554
555
    void _validate_segment_ids() const;
556
557
    friend bool operator==(const RowsetMeta& a, const RowsetMeta& b);
558
559
0
    friend bool operator!=(const RowsetMeta& a, const RowsetMeta& b) { return !(a == b); }
560
561
private:
562
    RowsetMetaPB _rowset_meta_pb;
563
    TabletSchemaSPtr _schema;
564
    Cache::Handle* _handle = nullptr;
565
    RowsetId _rowset_id;
566
    StorageResource _storage_resource;
567
    bool _is_removed_from_rowset_meta = false;
568
    DorisCallOnce<Result<EncryptionAlgorithmPB>> _determine_encryption_once;
569
    std::atomic<int64_t> _stale_at_s {0};
570
};
571
572
class RowsetSegmentMetaView {
573
public:
574
    RowsetSegmentMetaView(const RowsetMeta* meta, size_t pos)
575
3.66M
            : _meta(meta), _ref(meta->segment_ref(pos)) {}
576
577
210k
    size_t pos() const { return _ref.pos; }
578
1.87M
    int64_t id() const { return _ref.id; }
579
1.74M
    RowsetSegmentRef ref() const { return _ref; }
580
581
199k
    int64_t file_size() const { return _meta->segment_file_size_by_pos(pos()); }
582
583
10.1k
    InvertedIndexFileInfo inverted_index_file_info() const {
584
10.1k
        return _meta->inverted_index_file_info_by_pos(pos());
585
10.1k
    }
586
587
    bool has_num_rows() const {
588
        return cast_set<size_t>(_meta->get_num_segment_rows().size()) > pos();
589
    }
590
591
    int64_t num_rows() const {
592
        DORIS_CHECK(has_num_rows());
593
        return _meta->get_num_segment_rows().Get(cast_set<int>(pos()));
594
    }
595
596
    bool has_position_key_bounds() const {
597
        return !_meta->is_segments_key_bounds_aggregated() &&
598
               cast_set<size_t>(_meta->get_segments_key_bounds().size()) > pos();
599
    }
600
601
    const KeyBoundsPB& key_bounds() const {
602
        DORIS_CHECK(has_position_key_bounds());
603
        return _meta->get_segments_key_bounds().Get(cast_set<int>(pos()));
604
    }
605
606
private:
607
    const RowsetMeta* _meta;
608
    RowsetSegmentRef _ref;
609
};
610
611
class RowsetSegmentMetaRange {
612
public:
613
    class Iterator {
614
    public:
615
        using iterator_category = std::forward_iterator_tag;
616
        using value_type = RowsetSegmentMetaView;
617
        using difference_type = std::ptrdiff_t;
618
619
555k
        Iterator(const RowsetMeta* meta, size_t pos) : _meta(meta), _pos(pos) {}
620
621
136k
        RowsetSegmentMetaView operator*() const { return {_meta, _pos}; }
622
623
136k
        Iterator& operator++() {
624
136k
            ++_pos;
625
136k
            return *this;
626
136k
        }
627
628
414k
        bool operator==(const Iterator& other) const {
629
415k
            return _meta == other._meta && _pos == other._pos;
630
414k
        }
631
632
414k
        bool operator!=(const Iterator& other) const { return !(*this == other); }
633
634
    private:
635
        const RowsetMeta* _meta;
636
        size_t _pos;
637
    };
638
639
277k
    explicit RowsetSegmentMetaRange(const RowsetMeta* meta) : _meta(meta) {}
640
641
277k
    Iterator begin() const { return {_meta, 0}; }
642
277k
    Iterator end() const { return {_meta, cast_set<size_t>(_meta->num_segments())}; }
643
644
private:
645
    const RowsetMeta* _meta;
646
};
647
648
3.53M
inline RowsetSegmentMetaView RowsetMeta::segment(size_t pos) const {
649
3.53M
    return {this, pos};
650
3.53M
}
651
652
278k
inline RowsetSegmentMetaRange RowsetMeta::segments() const {
653
278k
    return RowsetSegmentMetaRange(this);
654
278k
}
655
656
} // namespace doris
657
658
#endif // DORIS_BE_SRC_OLAP_ROWSET_ROWSET_META_H