| 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 |
|
| // BlockBuilder generates blocks where keys are prefix-compressed: |
| 6 |
|
| // |
| 7 |
|
| // When we store a key, we drop the prefix shared with the previous |
| 8 |
|
| // string. This helps reduce the space requirement significantly. |
| 9 |
|
| // Furthermore, once every K keys, we do not apply the prefix |
| 10 |
|
| // compression and store the entire key. We call this a "restart |
| 11 |
|
| // point". The tail end of the block stores the offsets of all of the |
| 12 |
|
| // restart points, and can be used to do a binary search when looking |
| 13 |
|
| // for a particular key. Values are stored as-is (without compression) |
| 14 |
|
| // immediately following the corresponding key. |
| 15 |
|
| // |
| 16 |
|
| // An entry for a particular key-value pair has the form: |
| 17 |
|
| // shared_bytes: varint32 |
| 18 |
|
| // unshared_bytes: varint32 |
| 19 |
|
| // value_length: varint32 |
| 20 |
|
| // key_delta: char[unshared_bytes] |
| 21 |
|
| // value: char[value_length] |
| 22 |
|
| // shared_bytes == 0 for restart points. |
| 23 |
|
| // |
| 24 |
|
| // The trailer of the block has the form: |
| 25 |
|
| // restarts: uint32[num_restarts] |
| 26 |
|
| // num_restarts: uint32 |
| 27 |
|
| // restarts[i] contains the offset within the block of the ith restart point. |
| 28 |
|
| |
| 29 |
|
| #include "table/block_builder.h" |
| 30 |
|
| |
| 31 |
|
| #include <algorithm> |
| 32 |
|
| #include <assert.h> |
| 33 |
|
| #include "leveldb/comparator.h" |
| 34 |
|
| #include "leveldb/table_builder.h" |
| 35 |
|
| #include "util/coding.h" |
| 36 |
|
| |
| 37 |
|
| namespace leveldb { |
| 38 |
|
| |
| 39 |
3 |
| BlockBuilder::BlockBuilder(const Options* options) |
| 40 |
3 |
| : options_(options), |
| 41 |
3 |
| restarts_(), |
| 42 |
3 |
| counter_(0), |
| 43 |
6 |
| finished_(false) { |
| 44 |
3 |
▶ | assert(options->block_restart_interval >= 1); |
Branch: 1 branch
| Branch | True | False | Total |
| assert(options->block_restart_interval >= 1); |
0 |
3 |
3 |
|
| 45 |
3 |
▶ | restarts_.push_back(0); // First restart point is at offset 0 |
Branch: 1 branch
| Branch | True | False | Total |
| restarts_.push_back(0); // First restart point is at offset 0 |
3 |
0 |
3 |
|
| 46 |
3 |
| } |
| 47 |
|
| |
| 48 |
3 |
| void BlockBuilder::Reset() { |
| 49 |
3 |
| buffer_.clear(); |
| 50 |
3 |
| restarts_.clear(); |
| 51 |
3 |
▶ | restarts_.push_back(0); // First restart point is at offset 0 |
Branch: 1 branch
| Branch | True | False | Total |
| restarts_.push_back(0); // First restart point is at offset 0 |
3 |
0 |
3 |
|
| 52 |
3 |
| counter_ = 0; |
| 53 |
3 |
| finished_ = false; |
| 54 |
3 |
| last_key_.clear(); |
| 55 |
3 |
| } |
| 56 |
|
| |
| 57 |
13 |
| size_t BlockBuilder::CurrentSizeEstimate() const { |
| 58 |
13 |
| return (buffer_.size() + // Raw data buffer |
| 59 |
13 |
| restarts_.size() * sizeof(uint32_t) + // Restart array |
| 60 |
13 |
| sizeof(uint32_t)); // Restart array length |
| 61 |
|
| } |
| 62 |
|
| |
| 63 |
3 |
| Slice BlockBuilder::Finish() { |
| 64 |
|
| // Append restart array |
| 65 |
6 |
▶ | for (size_t i = 0; i < restarts_.size(); i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (size_t i = 0; i < restarts_.size(); i++) { |
3 |
3 |
6 |
|
| 66 |
3 |
| PutFixed32(&buffer_, restarts_[i]); |
| 67 |
|
| } |
| 68 |
3 |
| PutFixed32(&buffer_, restarts_.size()); |
| 69 |
3 |
| finished_ = true; |
| 70 |
3 |
| return Slice(buffer_); |
| 71 |
|
| } |
| 72 |
|
| |
| 73 |
14 |
| void BlockBuilder::Add(const Slice& key, const Slice& value) { |
| 74 |
14 |
| Slice last_key_piece(last_key_); |
| 75 |
14 |
▶ | assert(!finished_); |
Branch: 1 branch
| Branch | True | False | Total |
| assert(!finished_); |
0 |
14 |
14 |
|
| 76 |
14 |
▶ | assert(counter_ <= options_->block_restart_interval); |
Branch: 1 branch
| Branch | True | False | Total |
| assert(counter_ <= options_->block_restart_interval); |
0 |
14 |
14 |
|
| 77 |
14 |
▶ | assert(buffer_.empty() // No values yet? |
Branch: 3 branches
| Branch | True | False | Total |
| assert(buffer_.empty() // No values yet? |
12 |
2 |
14 |
| assert(buffer_.empty() // No values yet? |
12 |
0 |
12 |
| assert(buffer_.empty() // No values yet? |
0 |
12 |
12 |
|
| 78 |
|
| || options_->comparator->Compare(key, last_key_piece) > 0); |
| 79 |
14 |
| size_t shared = 0; |
| 80 |
14 |
▶ | if (counter_ < options_->block_restart_interval) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (counter_ < options_->block_restart_interval) { |
14 |
0 |
14 |
|
| 81 |
|
| // See how much sharing to do with previous string |
| 82 |
14 |
| const size_t min_length = std::min(last_key_piece.size(), key.size()); |
| 83 |
36 |
▶ | while ((shared < min_length) && (last_key_piece[shared] == key[shared])) { |
Branch: 3 branches
| Branch | True | False | Total |
| while ((shared < min_length) && (last_key_piece[shared] == key[shared])) { |
34 |
2 |
36 |
| while ((shared < min_length) && (last_key_piece[shared] == key[shared])) { |
22 |
12 |
34 |
| while ((shared < min_length) && (last_key_piece[shared] == key[shared])) { |
22 |
14 |
36 |
|
| 84 |
22 |
| shared++; |
| 85 |
|
| } |
| 86 |
|
| } else { |
| 87 |
|
| // Restart compression |
| 88 |
0 |
▶ | restarts_.push_back(buffer_.size()); |
Branch: 1 branch
| Branch | True | False | Total |
| restarts_.push_back(buffer_.size()); |
0 |
0 |
0 |
|
| 89 |
0 |
| counter_ = 0; |
| 90 |
|
| } |
| 91 |
14 |
| const size_t non_shared = key.size() - shared; |
| 92 |
|
| |
| 93 |
|
| // Add "<shared><non_shared><value_size>" to buffer_ |
| 94 |
14 |
▶ | PutVarint32(&buffer_, shared); |
Branch: 1 branch
| Branch | True | False | Total |
| PutVarint32(&buffer_, shared); |
14 |
0 |
14 |
|
| 95 |
14 |
▶ | PutVarint32(&buffer_, non_shared); |
Branch: 1 branch
| Branch | True | False | Total |
| PutVarint32(&buffer_, non_shared); |
14 |
0 |
14 |
|
| 96 |
14 |
▶ | PutVarint32(&buffer_, value.size()); |
Branch: 1 branch
| Branch | True | False | Total |
| PutVarint32(&buffer_, value.size()); |
14 |
0 |
14 |
|
| 97 |
|
| |
| 98 |
|
| // Add string delta to buffer_ followed by value |
| 99 |
14 |
▶ | buffer_.append(key.data() + shared, non_shared); |
Branch: 1 branch
| Branch | True | False | Total |
| buffer_.append(key.data() + shared, non_shared); |
14 |
0 |
14 |
|
| 100 |
14 |
▶ | buffer_.append(value.data(), value.size()); |
Branch: 1 branch
| Branch | True | False | Total |
| buffer_.append(value.data(), value.size()); |
14 |
0 |
14 |
|
| 101 |
|
| |
| 102 |
|
| // Update state |
| 103 |
14 |
▶ | last_key_.resize(shared); |
Branch: 1 branch
| Branch | True | False | Total |
| last_key_.resize(shared); |
14 |
0 |
14 |
|
| 104 |
14 |
▶ | last_key_.append(key.data() + shared, non_shared); |
Branch: 1 branch
| Branch | True | False | Total |
| last_key_.append(key.data() + shared, non_shared); |
14 |
0 |
14 |
|
| 105 |
14 |
▶ | assert(Slice(last_key_) == key); |
Branch: 1 branch
| Branch | True | False | Total |
| assert(Slice(last_key_) == key); |
0 |
14 |
14 |
|
| 106 |
14 |
| counter_++; |
| 107 |
14 |
| } |
| 108 |
|
| |
| 109 |
|
| } |