/Users/deen/code/yugabyte-db/src/yb/rocksdb/table/block.h
Line | Count | Source (jump to first uncovered line) |
1 | | // Copyright (c) 2011-present, Facebook, Inc. All rights reserved. |
2 | | // This source code is licensed under the BSD-style license found in the |
3 | | // LICENSE file in the root directory of this source tree. An additional grant |
4 | | // of patent rights can be found in the PATENTS file in the same directory. |
5 | | // |
6 | | // The following only applies to changes made to this file as part of YugaByte development. |
7 | | // |
8 | | // Portions Copyright (c) YugaByte, Inc. |
9 | | // |
10 | | // Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except |
11 | | // in compliance with the License. You may obtain a copy of the License at |
12 | | // |
13 | | // http://www.apache.org/licenses/LICENSE-2.0 |
14 | | // |
15 | | // Unless required by applicable law or agreed to in writing, software distributed under the License |
16 | | // is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express |
17 | | // or implied. See the License for the specific language governing permissions and limitations |
18 | | // under the License. |
19 | | // |
20 | | // Copyright (c) 2011 The LevelDB Authors. All rights reserved. |
21 | | // Use of this source code is governed by a BSD-style license that can be |
22 | | // found in the LICENSE file. See the AUTHORS file for names of contributors. |
23 | | #ifndef YB_ROCKSDB_TABLE_BLOCK_H |
24 | | #define YB_ROCKSDB_TABLE_BLOCK_H |
25 | | |
26 | | #pragma once |
27 | | #include <stddef.h> |
28 | | #include <stdint.h> |
29 | | #ifdef ROCKSDB_MALLOC_USABLE_SIZE |
30 | | #include <malloc.h> |
31 | | #endif |
32 | | |
33 | | #include "yb/rocksdb/iterator.h" |
34 | | #include "yb/rocksdb/options.h" |
35 | | #include "yb/rocksdb/db/dbformat.h" |
36 | | #include "yb/rocksdb/table/block_prefix_index.h" |
37 | | #include "yb/rocksdb/table/block_hash_index.h" |
38 | | #include "yb/rocksdb/table/format.h" |
39 | | #include "yb/rocksdb/table/internal_iterator.h" |
40 | | |
41 | | namespace rocksdb { |
42 | | |
43 | | struct BlockContents; |
44 | | class Comparator; |
45 | | class BlockIter; |
46 | | class BlockHashIndex; |
47 | | class BlockPrefixIndex; |
48 | | |
49 | | class Block { |
50 | | public: |
51 | | // Initialize the block with the specified contents. |
52 | | explicit Block(BlockContents&& contents); |
53 | | |
54 | 3.74M | ~Block() = default; |
55 | | |
56 | 8.28k | size_t size() const { return size_; } |
57 | 8.28k | const char* data() const { return data_; } |
58 | 2.13M | bool cachable() const { return contents_.cachable; } |
59 | 2.15M | size_t usable_size() const { |
60 | | #ifdef ROCKSDB_MALLOC_USABLE_SIZE |
61 | | if (contents_.allocation.get() != nullptr) { |
62 | | return malloc_usable_size(contents_.allocation.get()); |
63 | | } |
64 | | #endif // ROCKSDB_MALLOC_USABLE_SIZE |
65 | 2.15M | return size_; |
66 | 2.15M | } |
67 | | uint32_t NumRestarts() const; |
68 | 8.52M | CompressionType compression_type() const { |
69 | 8.52M | return contents_.compression_type; |
70 | 8.52M | } |
71 | | |
72 | | // If hash index lookup is enabled and `use_hash_index` is true. This block |
73 | | // will do hash lookup for the key prefix. |
74 | | // |
75 | | // NOTE: for the hash based lookup, if a key prefix doesn't match any key, |
76 | | // the iterator will simply be set as "invalid", rather than returning |
77 | | // the key that is just pass the target key. |
78 | | // |
79 | | // If iter is null, return new Iterator |
80 | | // If iter is not null, update this one and return it as Iterator* |
81 | | // |
82 | | // If total_order_seek is true, hash_index_ and prefix_index_ are ignored. |
83 | | // This option only applies for index block. For data block, hash_index_ |
84 | | // and prefix_index_ are null, so this option does not matter. |
85 | | // key_value_encoding_format specifies what kind of algorithm to use for decoding entries. |
86 | | InternalIterator* NewIterator(const Comparator* comparator, |
87 | | KeyValueEncodingFormat key_value_encoding_format, |
88 | | BlockIter* iter = nullptr, |
89 | | bool total_order_seek = true); |
90 | | |
91 | | inline InternalIterator* NewIndexIterator( |
92 | 16.2M | const Comparator* comparator, BlockIter* iter = nullptr, bool total_order_seek = true) { |
93 | 16.2M | return NewIterator( |
94 | 16.2M | comparator, kIndexBlockKeyValueEncodingFormat, iter, total_order_seek); |
95 | 16.2M | } |
96 | | |
97 | | void SetBlockHashIndex(BlockHashIndex* hash_index); |
98 | | void SetBlockPrefixIndex(BlockPrefixIndex* prefix_index); |
99 | | |
100 | | // Report an approximation of how much memory has been used. |
101 | | size_t ApproximateMemoryUsage() const; |
102 | | |
103 | | // Returns middle restart key from this data block (see block_builder.cc comments for restart |
104 | | // points description). |
105 | | yb::Result<Slice> GetMiddleKey(KeyValueEncodingFormat key_value_encoding_format) const; |
106 | | |
107 | | private: |
108 | | BlockContents contents_; |
109 | | const char* data_; // contents_.data.data() |
110 | | size_t size_; // contents_.data.size() |
111 | | uint32_t restart_offset_; // Offset in data_ of restart array |
112 | | std::unique_ptr<BlockHashIndex> hash_index_; |
113 | | std::unique_ptr<BlockPrefixIndex> prefix_index_; |
114 | | |
115 | | // No copying allowed |
116 | | Block(const Block&); |
117 | | void operator=(const Block&); |
118 | | }; |
119 | | |
120 | | class BlockIter : public InternalIterator { |
121 | | public: |
122 | | BlockIter() |
123 | | : comparator_(nullptr), |
124 | | data_(nullptr), |
125 | | restarts_(0), |
126 | | num_restarts_(0), |
127 | | current_(0), |
128 | | restart_index_(0), |
129 | | status_(Status::OK()), |
130 | | hash_index_(nullptr), |
131 | 34.8M | prefix_index_(nullptr) {} |
132 | | |
133 | | BlockIter( |
134 | | const Comparator* comparator, const char* data, |
135 | | KeyValueEncodingFormat key_value_encoding_format, uint32_t restarts, uint32_t num_restarts, |
136 | | BlockHashIndex* hash_index, BlockPrefixIndex* prefix_index) |
137 | 15.1M | : BlockIter() { |
138 | 15.1M | Initialize( |
139 | 15.1M | comparator, data, key_value_encoding_format, restarts, num_restarts, hash_index, |
140 | 15.1M | prefix_index); |
141 | 15.1M | } Unexecuted instantiation: _ZN7rocksdb9BlockIterC2EPKNS_10ComparatorEPKcNS_22KeyValueEncodingFormatEjjPNS_14BlockHashIndexEPNS_16BlockPrefixIndexE _ZN7rocksdb9BlockIterC1EPKNS_10ComparatorEPKcNS_22KeyValueEncodingFormatEjjPNS_14BlockHashIndexEPNS_16BlockPrefixIndexE Line | Count | Source | 137 | 15.1M | : BlockIter() { | 138 | 15.1M | Initialize( | 139 | 15.1M | comparator, data, key_value_encoding_format, restarts, num_restarts, hash_index, | 140 | 15.1M | prefix_index); | 141 | 15.1M | } |
|
142 | | |
143 | | void Initialize( |
144 | | const Comparator* comparator, const char* data, |
145 | | KeyValueEncodingFormat key_value_encoding_format, uint32_t restarts, uint32_t num_restarts, |
146 | | BlockHashIndex* hash_index, BlockPrefixIndex* prefix_index); |
147 | | |
148 | 40 | void SetStatus(Status s) { |
149 | 40 | status_ = s; |
150 | 40 | } |
151 | | |
152 | 1.79G | virtual bool Valid() const override { return current_ < restarts_; } |
153 | 60.7M | virtual Status status() const override { return status_; } |
154 | 344M | virtual Slice key() const override { |
155 | 344M | assert(Valid()); |
156 | 344M | return key_.GetKey(); |
157 | 344M | } |
158 | 788M | virtual Slice value() const override { |
159 | 788M | assert(Valid()); |
160 | 788M | return value_; |
161 | 788M | } |
162 | | |
163 | | virtual void Next() override; |
164 | | |
165 | | virtual void Prev() override; |
166 | | |
167 | | virtual void Seek(const Slice& target) override; |
168 | | |
169 | | virtual void SeekToFirst() override; |
170 | | |
171 | | virtual void SeekToLast() override; |
172 | | |
173 | 508k | virtual Status PinData() override { |
174 | | // block data is always pinned. |
175 | 508k | return Status::OK(); |
176 | 508k | } |
177 | | |
178 | 0 | virtual Status ReleasePinnedData() override { |
179 | 0 | // block data is always pinned. |
180 | 0 | return Status::OK(); |
181 | 0 | } |
182 | | |
183 | 442k | virtual bool IsKeyPinned() const override { return key_.IsKeyPinned(); } |
184 | | |
185 | | private: |
186 | | const Comparator* comparator_; |
187 | | const char* data_; // underlying block contents |
188 | | KeyValueEncodingFormat key_value_encoding_format_; |
189 | | uint32_t restarts_; // Offset of restart array (list of fixed32) |
190 | | uint32_t num_restarts_; // Number of uint32_t entries in restart array |
191 | | |
192 | | // current_ is offset in data_ of current entry. >= restarts_ if !Valid |
193 | | uint32_t current_; |
194 | | uint32_t restart_index_; // Index of restart block in which current_ falls |
195 | | IterKey key_; |
196 | | Slice value_; |
197 | | Status status_; |
198 | | BlockHashIndex* hash_index_; |
199 | | BlockPrefixIndex* prefix_index_; |
200 | | |
201 | 527M | inline int Compare(const Slice& a, const Slice& b) const { |
202 | 527M | return comparator_->Compare(a, b); |
203 | 527M | } |
204 | | |
205 | | // Return the offset in data_ just past the end of the current entry. |
206 | 681M | inline uint32_t NextEntryOffset() const { |
207 | | // NOTE: We don't support files bigger than 2GB |
208 | 681M | return static_cast<uint32_t>((value_.cdata() + value_.size()) - data_); |
209 | 681M | } |
210 | | |
211 | 879M | uint32_t GetRestartPoint(uint32_t index) { |
212 | 879M | assert(index < num_restarts_); |
213 | 879M | return DecodeFixed32(data_ + restarts_ + index * sizeof(uint32_t)); |
214 | 879M | } |
215 | | |
216 | 63.5M | void SeekToRestartPoint(uint32_t index) { |
217 | 63.5M | key_.Clear(); |
218 | 63.5M | restart_index_ = index; |
219 | | // current_ will be fixed by ParseNextKey(); |
220 | | |
221 | | // ParseNextKey() starts at the end of value_, so set value_ accordingly |
222 | 63.5M | uint32_t offset = GetRestartPoint(index); |
223 | 63.5M | value_ = Slice(data_ + offset, 0UL); |
224 | 63.5M | } |
225 | | |
226 | | void SetError(const Status& error); |
227 | | void CorruptionError(const std::string& error_details); |
228 | | |
229 | | bool ParseNextKey(); |
230 | | |
231 | | bool BinarySeek(const Slice& target, uint32_t left, uint32_t right, |
232 | | uint32_t* index); |
233 | | |
234 | | int CompareBlockKey(uint32_t block_index, const Slice& target); |
235 | | |
236 | | bool BinaryBlockIndexSeek(const Slice& target, uint32_t* block_ids, |
237 | | uint32_t left, uint32_t right, |
238 | | uint32_t* index); |
239 | | |
240 | | bool HashSeek(const Slice& target, uint32_t* index); |
241 | | |
242 | | bool PrefixSeek(const Slice& target, uint32_t* index); |
243 | | |
244 | | }; |
245 | | |
246 | | } // namespace rocksdb |
247 | | |
248 | | #endif // YB_ROCKSDB_TABLE_BLOCK_H |