| 1 |
|
| // Copyright (c) 2011 The LevelDB Authors. All rights reserved. |
| 2 |
|
| // Use of this source code is governed by a BSD-style license that can be |
| 3 |
|
| // found in the LICENSE file. See the AUTHORS file for names of contributors. |
| 4 |
|
| // |
| 5 |
|
| // Decodes the blocks generated by block_builder.cc. |
| 6 |
|
| |
| 7 |
|
| #include "table/block.h" |
| 8 |
|
| |
| 9 |
|
| #include <vector> |
| 10 |
|
| #include <algorithm> |
| 11 |
|
| #include "leveldb/comparator.h" |
| 12 |
|
| #include "util/coding.h" |
| 13 |
|
| #include "util/logging.h" |
| 14 |
|
| |
| 15 |
|
| namespace leveldb { |
| 16 |
|
| |
| 17 |
5 |
| inline uint32_t Block::NumRestarts() const { |
| 18 |
5 |
▶ | assert(size_ >= 2*sizeof(uint32_t)); |
Branch: 1 branch
| Branch | True | False | Total |
| assert(size_ >= 2*sizeof(uint32_t)); |
0 |
5 |
5 |
|
| 19 |
5 |
| return DecodeFixed32(data_ + size_ - sizeof(uint32_t)); |
| 20 |
|
| } |
| 21 |
|
| |
| 22 |
2 |
| Block::Block(const char* data, size_t size) |
| 23 |
2 |
| : data_(data), |
| 24 |
2 |
| size_(size) { |
| 25 |
2 |
▶ | if (size_ < sizeof(uint32_t)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (size_ < sizeof(uint32_t)) { |
0 |
2 |
2 |
|
| 26 |
0 |
| size_ = 0; // Error marker |
| 27 |
|
| } else { |
| 28 |
2 |
| restart_offset_ = size_ - (1 + NumRestarts()) * sizeof(uint32_t); |
| 29 |
2 |
▶ | if (restart_offset_ > size_ - sizeof(uint32_t)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (restart_offset_ > size_ - sizeof(uint32_t)) { |
0 |
2 |
2 |
|
| 30 |
|
| // The size is too small for NumRestarts() and therefore |
| 31 |
|
| // restart_offset_ wrapped around. |
| 32 |
0 |
| size_ = 0; |
| 33 |
|
| } |
| 34 |
|
| } |
| 35 |
2 |
| } |
| 36 |
|
| |
| 37 |
2 |
| Block::~Block() { |
| 38 |
2 |
▶ | delete[] data_; |
Branch: 1 branch
| Branch | True | False | Total |
| delete[] data_; |
2 |
0 |
2 |
|
| 39 |
2 |
| } |
| 40 |
|
| |
| 41 |
|
| // Helper routine: decode the next block entry starting at "p", |
| 42 |
|
| // storing the number of shared key bytes, non_shared key bytes, |
| 43 |
|
| // and the length of the value in "*shared", "*non_shared", and |
| 44 |
|
| // "*value_length", respectively. Will not derefence past "limit". |
| 45 |
|
| // |
| 46 |
|
| // If any errors are detected, returns NULL. Otherwise, returns a |
| 47 |
|
| // pointer to the key delta (just past the three decoded values). |
| 48 |
14 |
| static inline const char* DecodeEntry(const char* p, const char* limit, |
| 49 |
|
| uint32_t* shared, |
| 50 |
|
| uint32_t* non_shared, |
| 51 |
|
| uint32_t* value_length) { |
| 52 |
14 |
▶ | if (limit - p < 3) return NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| if (limit - p < 3) return NULL; |
0 |
14 |
14 |
|
| 53 |
14 |
| *shared = reinterpret_cast<const unsigned char*>(p)[0]; |
| 54 |
14 |
| *non_shared = reinterpret_cast<const unsigned char*>(p)[1]; |
| 55 |
14 |
| *value_length = reinterpret_cast<const unsigned char*>(p)[2]; |
| 56 |
14 |
▶ | if ((*shared | *non_shared | *value_length) < 128) { |
Branch: 1 branch
| Branch | True | False | Total |
| if ((*shared | *non_shared | *value_length) < 128) { |
14 |
0 |
14 |
|
| 57 |
|
| // Fast path: all three values are encoded in one byte each |
| 58 |
14 |
| p += 3; |
| 59 |
|
| } else { |
| 60 |
0 |
▶ | if ((p = GetVarint32Ptr(p, limit, shared)) == NULL) return NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| if ((p = GetVarint32Ptr(p, limit, shared)) == NULL) return NULL; |
0 |
0 |
0 |
|
| 61 |
0 |
▶ | if ((p = GetVarint32Ptr(p, limit, non_shared)) == NULL) return NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| if ((p = GetVarint32Ptr(p, limit, non_shared)) == NULL) return NULL; |
0 |
0 |
0 |
|
| 62 |
0 |
▶ | if ((p = GetVarint32Ptr(p, limit, value_length)) == NULL) return NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| if ((p = GetVarint32Ptr(p, limit, value_length)) == NULL) return NULL; |
0 |
0 |
0 |
|
| 63 |
|
| } |
| 64 |
|
| |
| 65 |
14 |
▶ | if (static_cast<uint32_t>(limit - p) < (*non_shared + *value_length)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (static_cast<uint32_t>(limit - p) < (*non_shared + *value_length)) { |
0 |
14 |
14 |
|
| 66 |
0 |
| return NULL; |
| 67 |
|
| } |
| 68 |
14 |
| return p; |
| 69 |
|
| } |
| 70 |
|
| |
| 71 |
|
| class Block::Iter : public Iterator { |
| 72 |
|
| private: |
| 73 |
|
| const Comparator* const comparator_; |
| 74 |
|
| const char* const data_; // underlying block contents |
| 75 |
|
| uint32_t const restarts_; // Offset of restart array (list of fixed32) |
| 76 |
|
| uint32_t const num_restarts_; // Number of uint32_t entries in restart array |
| 77 |
|
| |
| 78 |
|
| // current_ is offset in data_ of current entry. >= restarts_ if !Valid |
| 79 |
|
| uint32_t current_; |
| 80 |
|
| uint32_t restart_index_; // Index of restart block in which current_ falls |
| 81 |
|
| std::string key_; |
| 82 |
|
| Slice value_; |
| 83 |
|
| Status status_; |
| 84 |
|
| |
| 85 |
0 |
| inline int Compare(const Slice& a, const Slice& b) const { |
| 86 |
0 |
| return comparator_->Compare(a, b); |
| 87 |
|
| } |
| 88 |
|
| |
| 89 |
|
| // Return the offset in data_ just past the end of the current entry. |
| 90 |
16 |
| inline uint32_t NextEntryOffset() const { |
| 91 |
16 |
| return (value_.data() + value_.size()) - data_; |
| 92 |
|
| } |
| 93 |
|
| |
| 94 |
2 |
| uint32_t GetRestartPoint(uint32_t index) { |
| 95 |
2 |
▶ | assert(index < num_restarts_); |
Branch: 1 branch
| Branch | True | False | Total |
| assert(index < num_restarts_); |
0 |
2 |
2 |
|
| 96 |
2 |
| return DecodeFixed32(data_ + restarts_ + index * sizeof(uint32_t)); |
| 97 |
|
| } |
| 98 |
|
| |
| 99 |
2 |
| void SeekToRestartPoint(uint32_t index) { |
| 100 |
2 |
| key_.clear(); |
| 101 |
2 |
| restart_index_ = index; |
| 102 |
|
| // current_ will be fixed by ParseNextKey(); |
| 103 |
|
| |
| 104 |
|
| // ParseNextKey() starts at the end of value_, so set value_ accordingly |
| 105 |
2 |
| uint32_t offset = GetRestartPoint(index); |
| 106 |
2 |
| value_ = Slice(data_ + offset, 0); |
| 107 |
2 |
| } |
| 108 |
|
| |
| 109 |
|
| public: |
| 110 |
3 |
| Iter(const Comparator* comparator, |
| 111 |
|
| const char* data, |
| 112 |
|
| uint32_t restarts, |
| 113 |
|
| uint32_t num_restarts) |
| 114 |
6 |
| : comparator_(comparator), |
| 115 |
3 |
| data_(data), |
| 116 |
3 |
| restarts_(restarts), |
| 117 |
3 |
| num_restarts_(num_restarts), |
| 118 |
3 |
| current_(restarts_), |
| 119 |
3 |
| restart_index_(num_restarts_) { |
| 120 |
3 |
▶ | assert(num_restarts_ > 0); |
Branch: 1 branch
| Branch | True | False | Total |
| assert(num_restarts_ > 0); |
0 |
3 |
3 |
|
| 121 |
3 |
| } |
| 122 |
|
| |
| 123 |
48 |
| virtual bool Valid() const { return current_ < restarts_; } |
| 124 |
2 |
| virtual Status status() const { return status_; } |
| 125 |
14 |
| virtual Slice key() const { |
| 126 |
14 |
▶ | assert(Valid()); |
Branch: 1 branch
| Branch | True | False | Total |
| assert(Valid()); |
0 |
14 |
14 |
|
| 127 |
14 |
| return key_; |
| 128 |
|
| } |
| 129 |
1 |
| virtual Slice value() const { |
| 130 |
1 |
▶ | assert(Valid()); |
Branch: 1 branch
| Branch | True | False | Total |
| assert(Valid()); |
0 |
1 |
1 |
|
| 131 |
1 |
| return value_; |
| 132 |
|
| } |
| 133 |
|
| |
| 134 |
14 |
| virtual void Next() { |
| 135 |
14 |
▶ | assert(Valid()); |
Branch: 1 branch
| Branch | True | False | Total |
| assert(Valid()); |
0 |
14 |
14 |
|
| 136 |
14 |
| ParseNextKey(); |
| 137 |
14 |
| } |
| 138 |
|
| |
| 139 |
0 |
| virtual void Prev() { |
| 140 |
0 |
▶ | assert(Valid()); |
Branch: 1 branch
| Branch | True | False | Total |
| assert(Valid()); |
0 |
0 |
0 |
|
| 141 |
|
| |
| 142 |
|
| // Scan backwards to a restart point before current_ |
| 143 |
0 |
| const uint32_t original = current_; |
| 144 |
0 |
▶ | while (GetRestartPoint(restart_index_) >= original) { |
Branch: 1 branch
| Branch | True | False | Total |
| while (GetRestartPoint(restart_index_) >= original) { |
0 |
0 |
0 |
|
| 145 |
0 |
▶ | if (restart_index_ == 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (restart_index_ == 0) { |
0 |
0 |
0 |
|
| 146 |
|
| // No more entries |
| 147 |
0 |
| current_ = restarts_; |
| 148 |
0 |
| restart_index_ = num_restarts_; |
| 149 |
0 |
| return; |
| 150 |
|
| } |
| 151 |
0 |
| restart_index_--; |
| 152 |
|
| } |
| 153 |
|
| |
| 154 |
0 |
| SeekToRestartPoint(restart_index_); |
| 155 |
|
| do { |
| 156 |
|
| // Loop until end of current entry hits the start of original entry |
| 157 |
0 |
▶ | } while (ParseNextKey() && NextEntryOffset() < original); |
Branch: 3 branches
| Branch | True | False | Total |
| } while (ParseNextKey() && NextEntryOffset() < original); |
0 |
0 |
0 |
| } while (ParseNextKey() && NextEntryOffset() < original); |
0 |
0 |
0 |
| } while (ParseNextKey() && NextEntryOffset() < original); |
0 |
0 |
0 |
|
| 158 |
|
| } |
| 159 |
|
| |
| 160 |
0 |
| virtual void Seek(const Slice& target) { |
| 161 |
|
| // Binary search in restart array to find the first restart point |
| 162 |
|
| // with a key >= target |
| 163 |
0 |
| uint32_t left = 0; |
| 164 |
0 |
| uint32_t right = num_restarts_ - 1; |
| 165 |
0 |
▶ | while (left < right) { |
Branch: 1 branch
| Branch | True | False | Total |
| while (left < right) { |
0 |
0 |
0 |
|
| 166 |
0 |
| uint32_t mid = (left + right + 1) / 2; |
| 167 |
0 |
| uint32_t region_offset = GetRestartPoint(mid); |
| 168 |
|
| uint32_t shared, non_shared, value_length; |
| 169 |
0 |
| const char* key_ptr = DecodeEntry(data_ + region_offset, |
| 170 |
0 |
▶ | data_ + restarts_, |
Branch: 1 branch
| Branch | True | False | Total |
| data_ + restarts_, |
0 |
0 |
0 |
|
| 171 |
|
| &shared, &non_shared, &value_length); |
| 172 |
0 |
▶ | if (key_ptr == NULL || (shared != 0)) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (key_ptr == NULL || (shared != 0)) { |
0 |
0 |
0 |
| if (key_ptr == NULL || (shared != 0)) { |
0 |
0 |
0 |
|
| 173 |
0 |
▶ | CorruptionError(); |
Branch: 1 branch
| Branch | True | False | Total |
| CorruptionError(); |
0 |
0 |
0 |
|
| 174 |
0 |
| return; |
| 175 |
|
| } |
| 176 |
0 |
| Slice mid_key(key_ptr, non_shared); |
| 177 |
0 |
▶ | if (Compare(mid_key, target) < 0) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (Compare(mid_key, target) < 0) { |
0 |
0 |
0 |
| if (Compare(mid_key, target) < 0) { |
0 |
0 |
0 |
|
| 178 |
|
| // Key at "mid" is smaller than "target". Therefore all |
| 179 |
|
| // blocks before "mid" are uninteresting. |
| 180 |
0 |
| left = mid; |
| 181 |
|
| } else { |
| 182 |
|
| // Key at "mid" is >= "target". Therefore all blocks at or |
| 183 |
|
| // after "mid" are uninteresting. |
| 184 |
0 |
| right = mid - 1; |
| 185 |
|
| } |
| 186 |
|
| } |
| 187 |
|
| |
| 188 |
|
| // Linear search (within restart block) for first key >= target |
| 189 |
0 |
| SeekToRestartPoint(left); |
| 190 |
|
| while (true) { |
| 191 |
0 |
▶ | if (!ParseNextKey()) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!ParseNextKey()) { |
0 |
0 |
0 |
|
| 192 |
0 |
| return; |
| 193 |
|
| } |
| 194 |
0 |
▶ | if (Compare(key_, target) >= 0) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (Compare(key_, target) >= 0) { |
0 |
0 |
0 |
| if (Compare(key_, target) >= 0) { |
0 |
0 |
0 |
|
| 195 |
0 |
| return; |
| 196 |
|
| } |
| 197 |
|
| } |
| 198 |
|
| } |
| 199 |
|
| |
| 200 |
2 |
| virtual void SeekToFirst() { |
| 201 |
2 |
| SeekToRestartPoint(0); |
| 202 |
2 |
| ParseNextKey(); |
| 203 |
2 |
| } |
| 204 |
|
| |
| 205 |
0 |
| virtual void SeekToLast() { |
| 206 |
0 |
| SeekToRestartPoint(num_restarts_ - 1); |
| 207 |
0 |
▶ | while (ParseNextKey() && NextEntryOffset() < restarts_) { |
Branch: 3 branches
| Branch | True | False | Total |
| while (ParseNextKey() && NextEntryOffset() < restarts_) { |
0 |
0 |
0 |
| while (ParseNextKey() && NextEntryOffset() < restarts_) { |
0 |
0 |
0 |
| while (ParseNextKey() && NextEntryOffset() < restarts_) { |
0 |
0 |
0 |
|
| 208 |
|
| // Keep skipping |
| 209 |
|
| } |
| 210 |
0 |
| } |
| 211 |
|
| |
| 212 |
|
| private: |
| 213 |
0 |
| void CorruptionError() { |
| 214 |
0 |
| current_ = restarts_; |
| 215 |
0 |
| restart_index_ = num_restarts_; |
| 216 |
0 |
▶ | status_ = Status::Corruption("bad entry in block"); |
Branch: 2 branches
| Branch | True | False | Total |
| status_ = Status::Corruption("bad entry in block"); |
0 |
0 |
0 |
| status_ = Status::Corruption("bad entry in block"); |
0 |
0 |
0 |
|
| 217 |
0 |
| key_.clear(); |
| 218 |
0 |
| value_.clear(); |
| 219 |
0 |
| } |
| 220 |
|
| |
| 221 |
16 |
| bool ParseNextKey() { |
| 222 |
16 |
| current_ = NextEntryOffset(); |
| 223 |
16 |
| const char* p = data_ + current_; |
| 224 |
16 |
| const char* limit = data_ + restarts_; // Restarts come right after data |
| 225 |
16 |
▶ | if (p >= limit) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (p >= limit) { |
2 |
14 |
16 |
|
| 226 |
|
| // No more entries to return. Mark as invalid. |
| 227 |
2 |
| current_ = restarts_; |
| 228 |
2 |
| restart_index_ = num_restarts_; |
| 229 |
2 |
| return false; |
| 230 |
|
| } |
| 231 |
|
| |
| 232 |
|
| // Decode next entry |
| 233 |
|
| uint32_t shared, non_shared, value_length; |
| 234 |
14 |
▶ | p = DecodeEntry(p, limit, &shared, &non_shared, &value_length); |
Branch: 1 branch
| Branch | True | False | Total |
| p = DecodeEntry(p, limit, &shared, &non_shared, &value_length); |
14 |
0 |
14 |
|
| 235 |
14 |
▶ | if (p == NULL || key_.size() < shared) { |
Branch: 3 branches
| Branch | True | False | Total |
| if (p == NULL || key_.size() < shared) { |
14 |
0 |
14 |
| if (p == NULL || key_.size() < shared) { |
0 |
14 |
14 |
| if (p == NULL || key_.size() < shared) { |
0 |
14 |
14 |
|
| 236 |
0 |
▶ | CorruptionError(); |
Branch: 1 branch
| Branch | True | False | Total |
| CorruptionError(); |
0 |
0 |
0 |
|
| 237 |
0 |
| return false; |
| 238 |
|
| } else { |
| 239 |
14 |
▶ | key_.resize(shared); |
Branch: 1 branch
| Branch | True | False | Total |
| key_.resize(shared); |
14 |
0 |
14 |
|
| 240 |
14 |
▶ | key_.append(p, non_shared); |
Branch: 1 branch
| Branch | True | False | Total |
| key_.append(p, non_shared); |
14 |
0 |
14 |
|
| 241 |
14 |
| value_ = Slice(p + non_shared, value_length); |
| 242 |
14 |
▶ | while (restart_index_ + 1 < num_restarts_ && |
Branch: 2 branches
| Branch | True | False | Total |
| while (restart_index_ + 1 < num_restarts_ && |
0 |
14 |
14 |
| while (restart_index_ + 1 < num_restarts_ && |
0 |
14 |
14 |
|
| 243 |
0 |
▶ | GetRestartPoint(restart_index_ + 1) < current_) { |
Branch: 1 branch
| Branch | True | False | Total |
| GetRestartPoint(restart_index_ + 1) < current_) { |
0 |
0 |
0 |
|
| 244 |
0 |
| ++restart_index_; |
| 245 |
|
| } |
| 246 |
14 |
| return true; |
| 247 |
|
| } |
| 248 |
|
| } |
| 249 |
|
| }; |
| 250 |
|
| |
| 251 |
3 |
| Iterator* Block::NewIterator(const Comparator* cmp) { |
| 252 |
3 |
▶ | if (size_ < 2*sizeof(uint32_t)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (size_ < 2*sizeof(uint32_t)) { |
0 |
3 |
3 |
|
| 253 |
0 |
▶ | return NewErrorIterator(Status::Corruption("bad block contents")); |
Branch: 2 branches
| Branch | True | False | Total |
| return NewErrorIterator(Status::Corruption("bad block contents")); |
0 |
0 |
0 |
| return NewErrorIterator(Status::Corruption("bad block contents")); |
0 |
0 |
0 |
|
| 254 |
|
| } |
| 255 |
3 |
| const uint32_t num_restarts = NumRestarts(); |
| 256 |
3 |
▶ | if (num_restarts == 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (num_restarts == 0) { |
0 |
3 |
3 |
|
| 257 |
0 |
| return NewEmptyIterator(); |
| 258 |
|
| } else { |
| 259 |
3 |
▶ | return new Iter(cmp, data_, restart_offset_, num_restarts); |
Branch: 2 branches
| Branch | True | False | Total |
| return new Iter(cmp, data_, restart_offset_, num_restarts); |
3 |
0 |
3 |
| return new Iter(cmp, data_, restart_offset_, num_restarts); |
0 |
0 |
0 |
|
| 260 |
|
| } |
| 261 |
|
| } |
| 262 |
|
| |
| 263 |
|
| } |