be/src/storage/simple_rowid_conversion.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 | | #include <map> |
21 | | #include <vector> |
22 | | |
23 | | #include "core/custom_allocator.h" |
24 | | #include "storage/olap_common.h" |
25 | | #include "storage/utils.h" |
26 | | |
27 | | namespace doris { |
28 | | |
29 | | // Simple verion of rowid conversion, for segcompaction |
30 | | // convert rows from several segments to rows in 1 segment |
31 | | class SimpleRowIdConversion { |
32 | | public: |
33 | 3 | SimpleRowIdConversion(const RowsetId& rowset_id) : _rowst_id(rowset_id) {}; |
34 | 3 | ~SimpleRowIdConversion() = default; |
35 | | |
36 | | // resize segment rowid map to its rows num |
37 | 4 | void reset_segment_map(const std::map<uint32_t, uint32_t>& num_rows) { |
38 | 4 | _cur_dst_segment_rowid = 0; |
39 | 30 | for (auto seg_rows : num_rows) { |
40 | 30 | _segments_rowid_map.emplace(seg_rows.first, |
41 | 30 | DorisVector<uint32_t>(seg_rows.second, UINT32_MAX)); |
42 | 30 | } |
43 | 4 | } |
44 | | |
45 | | // add row id to the map |
46 | 30 | void add(const std::vector<RowLocation>& rss_row_ids) { |
47 | 107k | for (auto& item : rss_row_ids) { |
48 | 107k | if (item.row_id == -1) { |
49 | 0 | continue; |
50 | 0 | } |
51 | 107k | DCHECK(_segments_rowid_map.find(item.segment_id) != _segments_rowid_map.end() && |
52 | 107k | _segments_rowid_map[item.segment_id].size() > item.row_id); |
53 | 107k | _segments_rowid_map[item.segment_id][item.row_id] = _cur_dst_segment_rowid++; |
54 | 107k | } |
55 | 30 | } |
56 | | |
57 | | // get destination RowLocation |
58 | | // return non-zero if the src RowLocation does not exist |
59 | 57.3k | int get(const RowLocation& src) const { |
60 | 57.3k | auto it = _segments_rowid_map.find(src.segment_id); |
61 | 57.3k | if (it == _segments_rowid_map.end()) { |
62 | 0 | return -1; |
63 | 0 | } |
64 | 57.3k | const auto& rowid_map = it->second; |
65 | 57.3k | if (src.row_id >= rowid_map.size() || UINT32_MAX == rowid_map[src.row_id]) { |
66 | 5.21k | return -1; |
67 | 5.21k | } |
68 | | |
69 | 52.1k | return rowid_map[src.row_id]; |
70 | 57.3k | } |
71 | | |
72 | | private: |
73 | | // key: index indicates src segment. |
74 | | // value: index indicates row id of source segment, value indicates row id of destination |
75 | | // segment. UINT32_MAX indicates current row not exist. |
76 | | DorisMap<uint32_t, DorisVector<uint32_t>> _segments_rowid_map; |
77 | | |
78 | | // dst rowset id |
79 | | RowsetId _rowst_id; |
80 | | |
81 | | // current rowid of dst segment |
82 | | std::uint32_t _cur_dst_segment_rowid = 0; |
83 | | }; |
84 | | |
85 | | } // namespace doris |