be/src/storage/index/index_page.cpp
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 | | #include "storage/index/index_page.h" |
19 | | |
20 | | #include <gen_cpp/segment_v2.pb.h> |
21 | | |
22 | | #include <algorithm> |
23 | | #include <ostream> |
24 | | |
25 | | #include "util/coding.h" |
26 | | |
27 | | namespace doris { |
28 | | namespace segment_v2 { |
29 | | |
30 | 1.81M | void IndexPageBuilder::add(const Slice& key, const PagePointer& ptr) { |
31 | 18.4E | DCHECK(!_finished) << "must reset() after finish() to add new entry"; |
32 | 1.81M | put_length_prefixed_slice(&_buffer, key); |
33 | 1.81M | ptr.encode_to(&_buffer); |
34 | 1.81M | _count++; |
35 | 1.81M | } |
36 | | |
37 | 0 | bool IndexPageBuilder::is_full() const { |
38 | | // estimate size of IndexPageFooterPB to be 16 |
39 | 0 | return _buffer.size() + 16 > _index_page_size; |
40 | 0 | } |
41 | | |
42 | 15.1k | void IndexPageBuilder::finish(OwnedSlice* body, PageFooterPB* footer) { |
43 | 15.1k | DCHECK(!_finished) << "already called finish()"; |
44 | 15.1k | *body = _buffer.build(); |
45 | | |
46 | 15.1k | footer->set_type(INDEX_PAGE); |
47 | 15.1k | footer->set_uncompressed_size(cast_set<uint32_t>(body->slice().get_size())); |
48 | 15.1k | footer->mutable_index_page_footer()->set_num_entries(_count); |
49 | 15.1k | footer->mutable_index_page_footer()->set_type(_is_leaf ? IndexPageFooterPB::LEAF |
50 | 15.1k | : IndexPageFooterPB::INTERNAL); |
51 | 15.1k | } |
52 | | |
53 | 0 | Status IndexPageBuilder::get_first_key(Slice* key) const { |
54 | 0 | if (_count == 0) { |
55 | 0 | return Status::Error<ErrorCode::ENTRY_NOT_FOUND>("index page is empty"); |
56 | 0 | } |
57 | 0 | Slice input(_buffer); |
58 | 0 | if (get_length_prefixed_slice(&input, key)) { |
59 | 0 | return Status::OK(); |
60 | 0 | } else { |
61 | 0 | return Status::Corruption("can't decode first key"); |
62 | 0 | } |
63 | 0 | } |
64 | | |
65 | | /////////////////////////////////////////////////////////////////////////////// |
66 | | |
67 | 485k | int64_t IndexPageReader::get_metadata_size() const { |
68 | 485k | return sizeof(IndexPageReader) + _footer.ByteSizeLong(); |
69 | 485k | } |
70 | | |
71 | 485k | Status IndexPageReader::parse(const Slice& body, const IndexPageFooterPB& footer) { |
72 | 485k | _footer = footer; |
73 | 485k | size_t num_entries = _footer.num_entries(); |
74 | | |
75 | 485k | Slice input(body); |
76 | 3.83M | for (int i = 0; i < num_entries; ++i) { |
77 | 3.35M | Slice key; |
78 | 3.35M | PagePointer value; |
79 | 3.35M | if (!get_length_prefixed_slice(&input, &key)) { |
80 | 0 | return Status::InternalError("Data corruption"); |
81 | 0 | } |
82 | 3.35M | if (!value.decode_from(&input)) { |
83 | 0 | return Status::InternalError("Data corruption"); |
84 | 0 | } |
85 | 3.35M | _keys.push_back(key); |
86 | 3.35M | _values.push_back(value); |
87 | 3.35M | } |
88 | | |
89 | 485k | update_metadata_size(); |
90 | 485k | _parsed = true; |
91 | 485k | return Status::OK(); |
92 | 485k | } |
93 | | /////////////////////////////////////////////////////////////////////////////// |
94 | | |
95 | 3.87M | Status IndexPageIterator::seek_at_or_before(const Slice& search_key) { |
96 | 3.87M | int32_t left = 0; |
97 | 3.87M | auto right = cast_set<int32_t>(_reader->count() - 1); |
98 | 22.1M | while (left <= right) { |
99 | 18.1M | int32_t mid = left + (right - left) / 2; |
100 | 18.1M | int cmp = search_key.compare(_reader->get_key(mid)); |
101 | 18.1M | if (cmp < 0) { |
102 | 6.91M | right = mid - 1; |
103 | 11.3M | } else if (cmp > 0) { |
104 | 11.3M | left = mid + 1; |
105 | 18.4E | } else { |
106 | 18.4E | _pos = mid; |
107 | 18.4E | return Status::OK(); |
108 | 18.4E | } |
109 | 18.1M | } |
110 | | // no exact match, the insertion point is `left` |
111 | 3.94M | if (left == 0) { |
112 | | // search key is smaller than all keys |
113 | 254 | return Status::Error<ErrorCode::ENTRY_NOT_FOUND>( |
114 | 254 | "given key is smaller than all keys in page"); |
115 | 254 | } |
116 | | // index entry records the first key of the indexed page, |
117 | | // therefore the first page with keys >= searched key is the one before the insertion point |
118 | 3.94M | _pos = left - 1; |
119 | 3.94M | return Status::OK(); |
120 | 3.94M | } |
121 | | |
122 | | } // namespace segment_v2 |
123 | | } // namespace doris |