be/src/storage/index/index_page.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 <butil/macros.h> |
21 | | #include <gen_cpp/segment_v2.pb.h> |
22 | | #include <glog/logging.h> |
23 | | #include <stdint.h> |
24 | | |
25 | | #include <cstddef> |
26 | | #include <vector> |
27 | | |
28 | | #include "common/status.h" |
29 | | #include "storage/metadata_adder.h" |
30 | | #include "storage/segment/page_pointer.h" |
31 | | #include "util/faststring.h" |
32 | | #include "util/slice.h" |
33 | | |
34 | | namespace doris { |
35 | | namespace segment_v2 { |
36 | | #include "common/compile_check_begin.h" |
37 | | |
38 | | // IndexPage is the building block for IndexedColumn's ordinal index and value index. |
39 | | // It is used to guide searching for a particular key to the data page containing it. |
40 | | // We use the same general format for all index pages, regardless of the data type and node type (leaf or internal) |
41 | | // IndexPageBody := IndexEntry^NumEntry |
42 | | // IndexEntry := KeyLength(vint), Byte^KeyLength, PageOffset(vlong), PageSize(vint) |
43 | | // |
44 | | // IndexPageFooterPB records NumEntry and type (leaf/internal) of the index page. |
45 | | // For leaf, IndexKey records the first/smallest key of the data page PagePointer points to. |
46 | | // For internal, IndexKey records the first/smallest key of the next-level index page PagePointer points to. |
47 | | // |
48 | | // All keys are treated as binary string and compared with memcpy. Keys of other data type are encoded first by |
49 | | // KeyCoder, e.g., ordinal index's original key type is uint64_t but is encoded to binary string. |
50 | | class IndexPageBuilder { |
51 | | public: |
52 | | explicit IndexPageBuilder(size_t index_page_size, bool is_leaf) |
53 | 34.3k | : _index_page_size(index_page_size), _is_leaf(is_leaf) {} |
54 | | |
55 | | void add(const Slice& key, const PagePointer& ptr); |
56 | | |
57 | | bool is_full() const; |
58 | | |
59 | 35.8k | size_t count() const { return _count; } |
60 | | |
61 | | void finish(OwnedSlice* body, PageFooterPB* footer); |
62 | | |
63 | 8.94k | uint64_t size() { return _buffer.size(); } |
64 | | |
65 | | // Return the key of the first entry in this index block. |
66 | | // The pointed-to data is only valid until the next call to this builder. |
67 | | Status get_first_key(Slice* key) const; |
68 | | |
69 | 0 | void reset() { |
70 | 0 | _finished = false; |
71 | 0 | _buffer.clear(); |
72 | 0 | _count = 0; |
73 | 0 | } |
74 | | |
75 | | private: |
76 | | DISALLOW_COPY_AND_ASSIGN(IndexPageBuilder); |
77 | | const size_t _index_page_size; |
78 | | const bool _is_leaf; |
79 | | bool _finished = false; |
80 | | faststring _buffer; |
81 | | uint32_t _count = 0; |
82 | | }; |
83 | | |
84 | | class IndexPageReader : public MetadataAdder<IndexPageReader> { |
85 | | public: |
86 | 2.06k | IndexPageReader() : _parsed(false) {} |
87 | | |
88 | | Status parse(const Slice& body, const IndexPageFooterPB& footer); |
89 | | |
90 | 1.87k | size_t count() const { |
91 | 1.87k | DCHECK(_parsed); |
92 | 1.87k | return _footer.num_entries(); |
93 | 1.87k | } |
94 | | |
95 | 0 | bool is_leaf() const { |
96 | 0 | DCHECK(_parsed); |
97 | 0 | return _footer.type() == IndexPageFooterPB::LEAF; |
98 | 0 | } |
99 | | |
100 | 20.6k | const Slice& get_key(int idx) const { |
101 | 20.6k | DCHECK(_parsed); |
102 | 20.6k | DCHECK(idx >= 0 && idx < _footer.num_entries()); |
103 | 20.6k | return _keys[idx]; |
104 | 20.6k | } |
105 | | |
106 | 20.5k | const PagePointer& get_value(int idx) const { |
107 | 20.5k | DCHECK(_parsed); |
108 | 20.5k | DCHECK(idx >= 0 && idx < _footer.num_entries()); |
109 | 20.5k | return _values[idx]; |
110 | 20.5k | } |
111 | | |
112 | | void reset(); |
113 | | |
114 | | private: |
115 | | int64_t get_metadata_size() const override; |
116 | | |
117 | | bool _parsed; |
118 | | |
119 | | IndexPageFooterPB _footer; |
120 | | std::vector<Slice> _keys; |
121 | | std::vector<PagePointer> _values; |
122 | | }; |
123 | | |
124 | | class IndexPageIterator { |
125 | | public: |
126 | 374 | explicit IndexPageIterator(const IndexPageReader* reader) : _reader(reader), _pos(0) {} |
127 | | |
128 | | // Find the largest index entry whose key is <= search_key. |
129 | | // Return OK status when such entry exists. |
130 | | // Return ENTRY_NOT_FOUND when no such entry is found (all keys > search_key). |
131 | | // Return other error status otherwise. |
132 | | Status seek_at_or_before(const Slice& search_key); |
133 | | |
134 | 0 | void seek_to_first() { _pos = 0; } |
135 | | |
136 | | // Move to the next index entry. |
137 | | // Return true on success, false when no more entries can be read. |
138 | 30 | bool move_next() { |
139 | 30 | _pos++; |
140 | 30 | if (_pos >= _reader->count()) { |
141 | 0 | return false; |
142 | 0 | } |
143 | 30 | return true; |
144 | 30 | } |
145 | | |
146 | | // Return true when has next page. |
147 | 4 | bool has_next() { return (_pos + 1) < _reader->count(); } |
148 | | |
149 | 0 | const Slice& current_key() const { return _reader->get_key(cast_set<int>(_pos)); } |
150 | | |
151 | 170 | const PagePointer& current_page_pointer() const { |
152 | 170 | return _reader->get_value(cast_set<int>(_pos)); |
153 | 170 | } |
154 | | |
155 | | private: |
156 | | const IndexPageReader* _reader = nullptr; |
157 | | |
158 | | size_t _pos; |
159 | | }; |
160 | | |
161 | | #include "common/compile_check_end.h" |
162 | | } // namespace segment_v2 |
163 | | } // namespace doris |