Coverage Report

Created: 2026-09-02 19:34

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
18.6k
    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
5.65k
    const std::string& resource_id() const { return _rowset_meta_pb.resource_id(); }
82
83
22
    void set_resource_id(const std::string& resource_id) {
84
22
        _rowset_meta_pb.set_resource_id(resource_id);
85
22
    }
86
87
26.0k
    bool is_local() const { return !_rowset_meta_pb.has_resource_id(); }
88
89
    bool has_variant_type_in_schema() const;
90
91
2.56M
    RowsetId rowset_id() const { return _rowset_id; }
92
93
7.90k
    void set_rowset_id(const RowsetId& rowset_id) {
94
        // rowset id is a required field, just set it to 0
95
7.90k
        _rowset_meta_pb.set_rowset_id(0);
96
7.90k
        _rowset_id = rowset_id;
97
7.90k
        _rowset_meta_pb.set_rowset_id_v2(rowset_id.to_string());
98
7.90k
    }
99
100
13.7k
    int64_t tablet_id() const { return _rowset_meta_pb.tablet_id(); }
101
102
6.92k
    void set_tablet_id(int64_t tablet_id) { _rowset_meta_pb.set_tablet_id(tablet_id); }
103
104
44
    int64_t db_id() const { return _rowset_meta_pb.db_id(); }
105
106
1.34k
    void set_db_id(int64_t db_id) { _rowset_meta_pb.set_db_id(db_id); }
107
108
44
    int64_t table_id() const { return _rowset_meta_pb.table_id(); }
109
110
1.34k
    void set_table_id(int64_t table_id) { _rowset_meta_pb.set_table_id(table_id); }
111
112
3
    int64_t index_id() const { return _rowset_meta_pb.index_id(); }
113
114
1.33k
    void set_index_id(int64_t index_id) { _rowset_meta_pb.set_index_id(index_id); }
115
116
7
    bool has_inverted_index_storage_format() const {
117
7
        return _rowset_meta_pb.has_inverted_index_storage_format();
118
7
    }
119
120
2
    InvertedIndexStorageFormatPB inverted_index_storage_format() const {
121
2
        return _rowset_meta_pb.inverted_index_storage_format();
122
2
    }
123
124
    void set_inverted_index_storage_format(InvertedIndexStorageFormatPB format);
125
126
24
    TabletUid tablet_uid() const { return _rowset_meta_pb.tablet_uid(); }
127
128
6.82k
    void set_tablet_uid(TabletUid tablet_uid) {
129
6.82k
        *(_rowset_meta_pb.mutable_tablet_uid()) = tablet_uid.to_proto();
130
6.82k
    }
131
132
27
    int64_t txn_id() const { return _rowset_meta_pb.txn_id(); }
133
134
166
    void set_txn_id(int64_t txn_id) { _rowset_meta_pb.set_txn_id(txn_id); }
135
136
44
    int32_t tablet_schema_hash() const { return _rowset_meta_pb.tablet_schema_hash(); }
137
138
1.41k
    void set_tablet_schema_hash(int32_t tablet_schema_hash) {
139
1.41k
        _rowset_meta_pb.set_tablet_schema_hash(tablet_schema_hash);
140
1.41k
    }
141
142
62
    void mark_row_binlog() { _rowset_meta_pb.set_is_row_binlog(true); }
143
144
1.36k
    bool is_row_binlog() const {
145
1.36k
        return _rowset_meta_pb.has_is_row_binlog() && _rowset_meta_pb.is_row_binlog();
146
1.36k
    }
147
148
14.2k
    RowsetTypePB rowset_type() const { return _rowset_meta_pb.rowset_type(); }
149
150
3.54k
    void set_rowset_type(RowsetTypePB rowset_type) { _rowset_meta_pb.set_rowset_type(rowset_type); }
151
152
11.7k
    RowsetStatePB rowset_state() const { return _rowset_meta_pb.rowset_state(); }
153
154
2.76k
    void set_rowset_state(RowsetStatePB rowset_state) {
155
2.76k
        _rowset_meta_pb.set_rowset_state(rowset_state);
156
2.76k
    }
157
158
2.22M
    Version version() const {
159
2.22M
        return {_rowset_meta_pb.start_version(), _rowset_meta_pb.end_version()};
160
2.22M
    }
161
162
7.69k
    void set_version(Version version) {
163
7.69k
        _rowset_meta_pb.set_start_version(version.first);
164
7.69k
        _rowset_meta_pb.set_end_version(version.second);
165
7.69k
    }
166
167
12.9k
    bool has_version() const {
168
12.9k
        return _rowset_meta_pb.has_start_version() && _rowset_meta_pb.has_end_version();
169
12.9k
    }
170
171
22.5k
    int64_t start_version() const { return _rowset_meta_pb.start_version(); }
172
173
30.9k
    int64_t end_version() const { return _rowset_meta_pb.end_version(); }
174
175
1.97k
    int64_t num_rows() const { return _rowset_meta_pb.num_rows(); }
176
177
1.83k
    void set_num_rows(int64_t num_rows) { _rowset_meta_pb.set_num_rows(num_rows); }
178
179
1.38k
    void set_num_segment_rows(const std::vector<uint32_t>& num_segment_rows) {
180
1.38k
        _rowset_meta_pb.mutable_num_segment_rows()->Assign(num_segment_rows.cbegin(),
181
1.38k
                                                           num_segment_rows.cend());
182
1.38k
    }
183
184
131
    void get_num_segment_rows(std::vector<uint32_t>* num_segment_rows) const {
185
131
        num_segment_rows->assign(_rowset_meta_pb.num_segment_rows().cbegin(),
186
131
                                 _rowset_meta_pb.num_segment_rows().cend());
187
131
    }
188
189
1.66k
    auto& get_num_segment_rows() const { return _rowset_meta_pb.num_segment_rows(); }
190
191
14
    void set_segment_group_sizes(const std::vector<int32_t>& segment_group_sizes) {
192
14
        DORIS_CHECK_GT(segment_group_sizes.size(), 1);
193
14
        int64_t segment_count = 0;
194
45
        for (const auto group_size : segment_group_sizes) {
195
45
            DORIS_CHECK_GT(group_size, 0);
196
45
            segment_count += group_size;
197
45
        }
198
14
        DORIS_CHECK_EQ(segment_count, num_segments());
199
14
        _rowset_meta_pb.mutable_segment_group_sizes()->Assign(segment_group_sizes.cbegin(),
200
14
                                                              segment_group_sizes.cend());
201
14
    }
202
203
1
    void clear_segment_group_sizes() { _rowset_meta_pb.clear_segment_group_sizes(); }
204
205
34
    const auto& segment_group_sizes() const { return _rowset_meta_pb.segment_group_sizes(); }
206
207
7.51k
    int64_t total_disk_size() const { return _rowset_meta_pb.total_disk_size(); }
208
209
9.13k
    void set_total_disk_size(int64_t total_disk_size) {
210
9.13k
        _rowset_meta_pb.set_total_disk_size(total_disk_size);
211
9.13k
    }
212
213
528
    int64_t data_disk_size() const { return _rowset_meta_pb.data_disk_size(); }
214
215
1.57k
    void set_data_disk_size(int64_t data_disk_size) {
216
1.57k
        _rowset_meta_pb.set_data_disk_size(data_disk_size);
217
1.57k
    }
218
219
458
    int64_t index_disk_size() const { return _rowset_meta_pb.index_disk_size(); }
220
221
1.52k
    void set_index_disk_size(int64_t index_disk_size) {
222
1.52k
        _rowset_meta_pb.set_index_disk_size(index_disk_size);
223
1.52k
    }
224
225
0
    void zone_maps(std::vector<::doris::ZoneMap>* zone_maps) {
226
0
        for (const ::doris::ZoneMap& zone_map : _rowset_meta_pb.zone_maps()) {
227
0
            zone_maps->push_back(zone_map);
228
0
        }
229
0
    }
230
231
0
    void set_zone_maps(const std::vector<::doris::ZoneMap>& zone_maps) {
232
0
        for (const ::doris::ZoneMap& zone_map : zone_maps) {
233
0
            ::doris::ZoneMap* new_zone_map = _rowset_meta_pb.add_zone_maps();
234
0
            *new_zone_map = zone_map;
235
0
        }
236
0
    }
237
238
0
    void add_zone_map(const ::doris::ZoneMap& zone_map) {
239
0
        ::doris::ZoneMap* new_zone_map = _rowset_meta_pb.add_zone_maps();
240
0
        *new_zone_map = zone_map;
241
0
    }
242
243
6.38k
    bool has_delete_predicate() const { return _rowset_meta_pb.has_delete_predicate(); }
244
245
178
    const DeletePredicatePB& delete_predicate() const { return _rowset_meta_pb.delete_predicate(); }
246
247
0
    DeletePredicatePB* mutable_delete_predicate() {
248
0
        return _rowset_meta_pb.mutable_delete_predicate();
249
0
    }
250
251
93
    void set_delete_predicate(DeletePredicatePB delete_predicate) {
252
93
        DeletePredicatePB* new_delete_condition = _rowset_meta_pb.mutable_delete_predicate();
253
93
        *new_delete_condition = std::move(delete_predicate);
254
93
    }
255
256
67
    bool empty() const { return _rowset_meta_pb.empty(); }
257
258
1.51k
    void set_empty(bool empty) { _rowset_meta_pb.set_empty(empty); }
259
260
0
    PUniqueId load_id() const { return _rowset_meta_pb.load_id(); }
261
262
72
    void set_load_id(PUniqueId load_id) {
263
72
        PUniqueId* new_load_id = _rowset_meta_pb.mutable_load_id();
264
72
        new_load_id->set_hi(load_id.hi());
265
72
        new_load_id->set_lo(load_id.lo());
266
72
    }
267
268
6
    void set_job_id(const std::string& job_id) { _rowset_meta_pb.set_job_id(job_id); }
269
270
0
    const std::string& job_id() const { return _rowset_meta_pb.job_id(); }
271
272
0
    bool delete_flag() const { return _rowset_meta_pb.delete_flag(); }
273
274
57
    int64_t creation_time() const { return _rowset_meta_pb.creation_time(); }
275
276
1.48k
    void set_creation_time(int64_t creation_time) {
277
1.48k
        return _rowset_meta_pb.set_creation_time(creation_time);
278
1.48k
    }
279
280
779
    int64_t stale_at() const {
281
779
        int64_t stale_time = _stale_at_s.load();
282
779
        return stale_time > 0 ? stale_time : _rowset_meta_pb.creation_time();
283
779
    }
284
285
2
    bool has_stale_at() const { return _stale_at_s.load() > 0; }
286
287
693
    void set_stale_at(int64_t stale_at) { _stale_at_s.store(stale_at); }
288
289
12
    int64_t partition_id() const { return _rowset_meta_pb.partition_id(); }
290
291
1.33k
    void set_partition_id(int64_t partition_id) {
292
1.33k
        return _rowset_meta_pb.set_partition_id(partition_id);
293
1.33k
    }
294
295
59.4k
    int64_t num_segments() const {
296
59.4k
        DCHECK(_rowset_meta_pb.segment_ids_size() == 0 ||
297
59.4k
               _rowset_meta_pb.segment_ids_size() == _rowset_meta_pb.num_segments());
298
59.4k
        return _rowset_meta_pb.num_segments();
299
59.4k
    }
300
301
5.96k
    void set_num_segments(int64_t num_segments) { _rowset_meta_pb.set_num_segments(num_segments); }
302
303
18.8k
    bool has_segment_ids() const { return _rowset_meta_pb.segment_ids_size() > 0; }
304
305
3
    const auto& segment_ids() const { return _rowset_meta_pb.segment_ids(); }
306
307
    void set_segment_ids(const std::vector<int64_t>& segment_ids);
308
309
8.29k
    int64_t segment_id(size_t pos) const {
310
8.29k
        DORIS_CHECK_LT(pos, cast_set<size_t>(num_segments()));
311
8.29k
        return has_segment_ids() ? _rowset_meta_pb.segment_ids(cast_set<int>(pos))
312
8.29k
                                 : cast_set<int64_t>(pos);
313
8.29k
    }
314
315
8.28k
    RowsetSegmentRef segment_ref(size_t pos) const { return {pos, segment_id(pos)}; }
316
317
    RowsetSegmentMetaView segment(size_t pos) const;
318
319
    RowsetSegmentMetaRange segments() const;
320
321
    size_t position_of(int64_t seg_id) const;
322
323
    // Convert to RowsetMetaPB, skip_schema is only used by cloud to separate schema from rowset meta.
324
    void to_rowset_pb(RowsetMetaPB* rs_meta_pb, bool skip_schema = false) const;
325
326
    // Convert to RowsetMetaPB, skip_schema is only used by cloud to separate schema from rowset meta.
327
    RowsetMetaPB get_rowset_pb(bool skip_schema = false) const;
328
329
0
    inline DeletePredicatePB* mutable_delete_pred_pb() {
330
0
        return _rowset_meta_pb.mutable_delete_predicate();
331
0
    }
332
333
243
    bool is_singleton_delta() const {
334
243
        return has_version() && _rowset_meta_pb.start_version() == _rowset_meta_pb.end_version();
335
243
    }
336
337
    // Some time, we may check if this rowset is in rowset meta manager's meta by using RowsetMetaManager::check_rowset_meta.
338
    // But, this check behavior may cost a lot of time when it is frequent.
339
    // If we explicitly remove this rowset from rowset meta manager's meta, we can set _is_removed_from_rowset_meta to true,
340
    // And next time when we want to check if this rowset is in rowset mata manager's meta, we can
341
    // check is_remove_from_rowset_meta() first.
342
0
    void set_remove_from_rowset_meta() { _is_removed_from_rowset_meta = true; }
343
344
0
    bool is_remove_from_rowset_meta() const { return _is_removed_from_rowset_meta; }
345
346
2.06k
    SegmentsOverlapPB segments_overlap() const { return _rowset_meta_pb.segments_overlap_pb(); }
347
348
6.59k
    void set_segments_overlap(SegmentsOverlapPB segments_overlap) {
349
6.59k
        _rowset_meta_pb.set_segments_overlap_pb(segments_overlap);
350
6.59k
    }
351
352
7.80k
    static bool comparator(const RowsetMetaSharedPtr& left, const RowsetMetaSharedPtr& right) {
353
7.80k
        return left->end_version() < right->end_version();
354
7.80k
    }
355
356
    // return true if segments in this rowset has overlapping data.
357
    // this is not same as `segments_overlap()` method.
358
    // `segments_overlap()` only return the value of "segments_overlap" field in rowset meta,
359
    // but "segments_overlap" may be UNKNOWN.
360
    //
361
    // Returns true if all of the following conditions are met:
362
    // 1. The rowset contains more than one segment.
363
    // 2. segments_overlap() is not NONOVERLAPPING (OVERLAP_UNKNOWN, OVERLAPPING, and
364
    //    NONOVERLAPPING_WITHIN_GROUP are considered overlapping).
365
    // 3. The rowset has a singleton version, except row-binlog LMax quick merge rowsets that
366
    //    explicitly set segments_overlap() to OVERLAPPING.
367
20.4k
    bool is_segments_overlapping() const {
368
20.4k
        return num_segments() > 1 && segments_overlap() != NONOVERLAPPING &&
369
20.4k
               (is_singleton_delta() || (is_row_binlog() && segments_overlap() == OVERLAPPING));
370
20.4k
    }
371
372
1
    bool produced_by_compaction() const {
373
1
        return has_version() && (start_version() < end_version() ||
374
1
                                 (start_version() == end_version() &&
375
1
                                  (segments_overlap() == NONOVERLAPPING ||
376
1
                                   segments_overlap() == NONOVERLAPPING_WITHIN_GROUP)));
377
1
    }
378
379
    // get the compaction score of this rowset.
380
    // if segments are overlapping, the score equals to the number of segments,
381
    // otherwise, score is 1.
382
18.2k
    uint32_t get_compaction_score() const {
383
18.2k
        uint32_t score = 0;
384
18.2k
        if (!is_segments_overlapping()) {
385
18.1k
            score = 1;
386
18.1k
        } else {
387
136
            auto num_seg = num_segments();
388
136
            DCHECK_GT(num_seg, 0);
389
136
            score = cast_set<uint32_t>(num_seg);
390
136
            CHECK(score > 0);
391
136
        }
392
18.2k
        return score;
393
18.2k
    }
394
395
56
    uint32_t get_merge_way_num() const {
396
56
        uint32_t way_num = 0;
397
56
        if (!is_segments_overlapping()) {
398
55
            if (num_segments() == 0) {
399
0
                way_num = 0;
400
55
            } else {
401
55
                way_num = 1;
402
55
            }
403
55
        } else {
404
1
            auto num_seg = num_segments();
405
1
            DCHECK_GT(num_seg, 0);
406
407
1
            way_num = cast_set<uint32_t>(num_seg);
408
1
            CHECK(way_num > 0);
409
1
        }
410
56
        return way_num;
411
56
    }
412
413
601
    void get_segments_key_bounds(std::vector<KeyBoundsPB>* segments_key_bounds) const {
414
618
        for (const KeyBoundsPB& key_range : _rowset_meta_pb.segments_key_bounds()) {
415
618
            segments_key_bounds->push_back(key_range);
416
618
        }
417
601
    }
418
419
10
    auto& get_segments_key_bounds() const { return _rowset_meta_pb.segments_key_bounds(); }
420
421
2.28k
    bool is_segments_key_bounds_truncated() const {
422
2.28k
        return _rowset_meta_pb.has_segments_key_bounds_truncated() &&
423
2.28k
               _rowset_meta_pb.segments_key_bounds_truncated();
424
2.28k
    }
425
426
2.03k
    void set_segments_key_bounds_truncated(bool truncated) {
427
2.03k
        _rowset_meta_pb.set_segments_key_bounds_truncated(truncated);
428
2.03k
    }
429
430
    // When true, `segments_key_bounds` holds a single aggregated
431
    // [rowset_min, rowset_max] entry instead of per-segment bounds.
432
480
    bool is_segments_key_bounds_aggregated() const {
433
480
        return _rowset_meta_pb.has_segments_key_bounds_aggregated() &&
434
480
               _rowset_meta_pb.segments_key_bounds_aggregated();
435
480
    }
436
437
1.39k
    void set_segments_key_bounds_aggregated(bool aggregated) {
438
1.39k
        _rowset_meta_pb.set_segments_key_bounds_aggregated(aggregated);
439
1.39k
    }
440
441
266
    bool get_first_segment_key_bound(KeyBoundsPB* key_bounds) {
442
        // for compatibility, old version has not segment key bounds
443
266
        if (_rowset_meta_pb.segments_key_bounds_size() == 0) {
444
0
            return false;
445
0
        }
446
266
        *key_bounds = *_rowset_meta_pb.segments_key_bounds().begin();
447
266
        return true;
448
266
    }
449
450
178
    bool get_last_segment_key_bound(KeyBoundsPB* key_bounds) {
451
178
        if (_rowset_meta_pb.segments_key_bounds_size() == 0) {
452
0
            return false;
453
0
        }
454
178
        *key_bounds = *_rowset_meta_pb.segments_key_bounds().rbegin();
455
178
        return true;
456
178
    }
457
458
    // If `aggregate_into_single` is true, collapse per-segment bounds into a single
459
    // [rowset_min, rowset_max] entry and mark this rowset as aggregated.
460
    void set_segments_key_bounds(const std::vector<KeyBoundsPB>& segments_key_bounds,
461
                                 bool aggregate_into_single = false);
462
463
0
    void add_segment_key_bounds(KeyBoundsPB segments_key_bounds) {
464
0
        *_rowset_meta_pb.add_segments_key_bounds() = std::move(segments_key_bounds);
465
0
        set_segments_overlap(OVERLAPPING);
466
0
    }
467
468
2.19k
    void set_newest_write_timestamp(int64_t timestamp) {
469
2.19k
        _rowset_meta_pb.set_newest_write_timestamp(timestamp);
470
2.19k
    }
471
472
1.42k
    int64_t newest_write_timestamp() const { return _rowset_meta_pb.newest_write_timestamp(); }
473
474
    // for cloud only
475
269
    bool has_visible_ts_ms() const { return _rowset_meta_pb.has_visible_ts_ms(); }
476
268
    int64_t visible_ts_ms() const { return _rowset_meta_pb.visible_ts_ms(); }
477
269
    std::chrono::time_point<std::chrono::system_clock> visible_timestamp() const {
478
269
        using namespace std::chrono;
479
269
        if (has_visible_ts_ms()) {
480
268
            return time_point<system_clock>(milliseconds(visible_ts_ms()));
481
268
        }
482
1
        return system_clock::from_time_t(newest_write_timestamp());
483
269
    }
484
771
    void set_visible_ts_ms(int64_t visible_ts_ms) {
485
771
        _rowset_meta_pb.set_visible_ts_ms(visible_ts_ms);
486
771
    }
487
488
    void set_tablet_schema(const TabletSchemaSPtr& tablet_schema);
489
    void set_tablet_schema(const TabletSchemaPB& tablet_schema);
490
491
31.8k
    const TabletSchemaSPtr& tablet_schema() const { return _schema; }
492
493
6
    void set_txn_expiration(int64_t expiration) { _rowset_meta_pb.set_txn_expiration(expiration); }
494
495
1.33k
    void set_compaction_level(int64_t compaction_level) {
496
1.33k
        _rowset_meta_pb.set_compaction_level(compaction_level);
497
1.33k
    }
498
499
880
    int64_t compaction_level() { return _rowset_meta_pb.compaction_level(); }
500
501
    // `seg_file_size` MUST be ordered by rowset segment position.
502
    void add_segments_file_size(const std::vector<size_t>& seg_file_size);
503
504
    // Return -1 if segment file size is unknown
505
    int64_t segment_file_size_by_pos(size_t pos) const;
506
507
0
    const auto& segments_file_size() const { return _rowset_meta_pb.segments_file_size(); }
508
509
    // Used for partial update, when publish, partial update may add a new rowset and we should update rowset meta
510
    void merge_rowset_meta(const RowsetMeta& other);
511
512
    InvertedIndexFileInfo inverted_index_file_info_by_pos(size_t pos) const;
513
514
68
    const auto& inverted_index_file_info() const {
515
68
        return _rowset_meta_pb.inverted_index_file_info();
516
68
    }
517
518
    void add_inverted_index_files_info(
519
            const std::vector<const InvertedIndexFileInfo*>& idx_file_info);
520
521
    int64_t get_metadata_size() const override;
522
523
    // Because the member field '_handle' is a raw pointer, use member func 'init' to replace copy ctor
524
    RowsetMeta(const RowsetMeta&) = delete;
525
    RowsetMeta operator=(const RowsetMeta&) = delete;
526
527
    void add_packed_slice_location(const std::string& segment_path,
528
                                   const std::string& packed_file_path, int64_t offset,
529
0
                                   int64_t size, int64_t packed_file_size) {
530
0
        auto* index_map = _rowset_meta_pb.mutable_packed_slice_locations();
531
0
        auto& index_pb = (*index_map)[segment_path];
532
0
        index_pb.set_packed_file_path(packed_file_path);
533
0
        index_pb.set_offset(offset);
534
0
        index_pb.set_size(size);
535
0
        index_pb.set_packed_file_size(packed_file_size);
536
0
    }
537
538
148
    int32_t schema_version() const { return _rowset_meta_pb.schema_version(); }
539
540
0
    std::string debug_string() const { return _rowset_meta_pb.ShortDebugString(); }
541
542
    // Pre-set the encryption algorithm to avoid re-entrant get_tablet calls
543
    // that can cause SingleFlight deadlock during tablet loading.
544
0
    void set_encryption_algorithm(EncryptionAlgorithmPB algorithm) {
545
0
        _determine_encryption_once.call(
546
0
                [algorithm]() -> Result<EncryptionAlgorithmPB> { return algorithm; });
547
0
    }
548
549
1.70k
    TsoRange commit_tso() const {
550
1.70k
        const auto& commit_tso_pb = _rowset_meta_pb.commit_tso();
551
1.70k
        return {commit_tso_pb.start_tso(), commit_tso_pb.end_tso()};
552
1.70k
    }
553
554
44
    bool has_commit_tso() const { return _rowset_meta_pb.has_commit_tso(); }
555
556
78
    void set_commit_tso(const TsoRange& commit_tso) {
557
78
        auto* commit_tso_pb = _rowset_meta_pb.mutable_commit_tso();
558
78
        commit_tso_pb->set_start_tso(commit_tso.start_tso());
559
78
        commit_tso_pb->set_end_tso(commit_tso.end_tso());
560
78
    }
561
562
21
    void set_commit_tso(int64_t commit_tso) { set_commit_tso({commit_tso, commit_tso}); }
563
564
0
    void set_cloud_fields_after_visible(int64_t visible_version, int64_t version_update_time_ms) {
565
        // Update rowset meta with correct version and visible_ts
566
        // !!ATTENTION!!: this code should be updated if there are more fields
567
        // in rowset meta which will be modified in meta-service when commit_txn in the future
568
0
        set_version({visible_version, visible_version});
569
0
        if (version_update_time_ms > 0) {
570
0
            set_visible_ts_ms(version_update_time_ms);
571
0
        }
572
0
    }
573
574
private:
575
    bool _deserialize_from_pb(std::string_view value);
576
577
    bool _serialize_to_pb(std::string* value);
578
579
    void _init();
580
581
    void _validate_segment_ids() const;
582
583
    friend bool operator==(const RowsetMeta& a, const RowsetMeta& b);
584
585
0
    friend bool operator!=(const RowsetMeta& a, const RowsetMeta& b) { return !(a == b); }
586
587
private:
588
    RowsetMetaPB _rowset_meta_pb;
589
    TabletSchemaSPtr _schema;
590
    Cache::Handle* _handle = nullptr;
591
    RowsetId _rowset_id;
592
    StorageResource _storage_resource;
593
    bool _is_removed_from_rowset_meta = false;
594
    DorisCallOnce<Result<EncryptionAlgorithmPB>> _determine_encryption_once;
595
    std::atomic<int64_t> _stale_at_s {0};
596
};
597
598
class RowsetSegmentMetaView {
599
public:
600
    RowsetSegmentMetaView(const RowsetMeta* meta, size_t pos)
601
8.22k
            : _meta(meta), _ref(meta->segment_ref(pos)) {}
602
603
1.67k
    size_t pos() const { return _ref.pos; }
604
2.89k
    int64_t id() const { return _ref.id; }
605
5.54k
    RowsetSegmentRef ref() const { return _ref; }
606
607
1
    int64_t file_size() const { return _meta->segment_file_size_by_pos(pos()); }
608
609
1.66k
    InvertedIndexFileInfo inverted_index_file_info() const {
610
1.66k
        return _meta->inverted_index_file_info_by_pos(pos());
611
1.66k
    }
612
613
2
    bool has_num_rows() const {
614
2
        return cast_set<size_t>(_meta->get_num_segment_rows().size()) > pos();
615
2
    }
616
617
1
    int64_t num_rows() const {
618
1
        DORIS_CHECK(has_num_rows());
619
1
        return _meta->get_num_segment_rows().Get(cast_set<int>(pos()));
620
1
    }
621
622
3
    bool has_position_key_bounds() const {
623
3
        return !_meta->is_segments_key_bounds_aggregated() &&
624
3
               cast_set<size_t>(_meta->get_segments_key_bounds().size()) > pos();
625
3
    }
626
627
2
    const KeyBoundsPB& key_bounds() const {
628
2
        DORIS_CHECK(has_position_key_bounds());
629
2
        return _meta->get_segments_key_bounds().Get(cast_set<int>(pos()));
630
2
    }
631
632
private:
633
    const RowsetMeta* _meta;
634
    RowsetSegmentRef _ref;
635
};
636
637
class RowsetSegmentMetaRange {
638
public:
639
    class Iterator {
640
    public:
641
        using iterator_category = std::forward_iterator_tag;
642
        using value_type = RowsetSegmentMetaView;
643
        using difference_type = std::ptrdiff_t;
644
645
22.3k
        Iterator(const RowsetMeta* meta, size_t pos) : _meta(meta), _pos(pos) {}
646
647
362
        RowsetSegmentMetaView operator*() const { return {_meta, _pos}; }
648
649
359
        Iterator& operator++() {
650
359
            ++_pos;
651
359
            return *this;
652
359
        }
653
654
11.5k
        bool operator==(const Iterator& other) const {
655
11.5k
            return _meta == other._meta && _pos == other._pos;
656
11.5k
        }
657
658
11.5k
        bool operator!=(const Iterator& other) const { return !(*this == other); }
659
660
    private:
661
        const RowsetMeta* _meta;
662
        size_t _pos;
663
    };
664
665
11.1k
    explicit RowsetSegmentMetaRange(const RowsetMeta* meta) : _meta(meta) {}
666
667
11.1k
    Iterator begin() const { return {_meta, 0}; }
668
11.1k
    Iterator end() const { return {_meta, cast_set<size_t>(_meta->num_segments())}; }
669
670
private:
671
    const RowsetMeta* _meta;
672
};
673
674
7.86k
inline RowsetSegmentMetaView RowsetMeta::segment(size_t pos) const {
675
7.86k
    return {this, pos};
676
7.86k
}
677
678
11.1k
inline RowsetSegmentMetaRange RowsetMeta::segments() const {
679
11.1k
    return RowsetSegmentMetaRange(this);
680
11.1k
}
681
682
} // namespace doris
683
684
#endif // DORIS_BE_SRC_OLAP_ROWSET_ROWSET_META_H