2016-02-10 00:12:00 +01:00
|
|
|
// Copyright (c) 2011-present, Facebook, Inc. All rights reserved.
|
2013-10-16 23:59:46 +02:00
|
|
|
// This source code is licensed under the BSD-style license found in the
|
|
|
|
// LICENSE file in the root directory of this source tree. An additional grant
|
|
|
|
// of patent rights can be found in the PATENTS file in the same directory.
|
2017-04-28 02:50:56 +02:00
|
|
|
// This source code is also licensed under the GPLv2 license found in the
|
|
|
|
// COPYING file in the root directory of this source tree.
|
2013-10-16 23:59:46 +02:00
|
|
|
//
|
2011-03-18 23:37:00 +01:00
|
|
|
// Copyright (c) 2011 The LevelDB Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style license that can be
|
|
|
|
// found in the LICENSE file. See the AUTHORS file for names of contributors.
|
|
|
|
//
|
|
|
|
// BlockBuilder generates blocks where keys are prefix-compressed:
|
|
|
|
//
|
|
|
|
// When we store a key, we drop the prefix shared with the previous
|
|
|
|
// string. This helps reduce the space requirement significantly.
|
|
|
|
// Furthermore, once every K keys, we do not apply the prefix
|
|
|
|
// compression and store the entire key. We call this a "restart
|
|
|
|
// point". The tail end of the block stores the offsets of all of the
|
|
|
|
// restart points, and can be used to do a binary search when looking
|
|
|
|
// for a particular key. Values are stored as-is (without compression)
|
|
|
|
// immediately following the corresponding key.
|
|
|
|
//
|
|
|
|
// An entry for a particular key-value pair has the form:
|
|
|
|
// shared_bytes: varint32
|
|
|
|
// unshared_bytes: varint32
|
|
|
|
// value_length: varint32
|
|
|
|
// key_delta: char[unshared_bytes]
|
|
|
|
// value: char[value_length]
|
|
|
|
// shared_bytes == 0 for restart points.
|
|
|
|
//
|
|
|
|
// The trailer of the block has the form:
|
|
|
|
// restarts: uint32[num_restarts]
|
|
|
|
// num_restarts: uint32
|
|
|
|
// restarts[i] contains the offset within the block of the ith restart point.
|
|
|
|
|
|
|
|
#include "table/block_builder.h"
|
|
|
|
|
|
|
|
#include <algorithm>
|
|
|
|
#include <assert.h>
|
2013-08-23 17:38:13 +02:00
|
|
|
#include "rocksdb/comparator.h"
|
2014-01-27 22:53:22 +01:00
|
|
|
#include "db/dbformat.h"
|
2011-03-18 23:37:00 +01:00
|
|
|
#include "util/coding.h"
|
|
|
|
|
2013-10-04 06:49:15 +02:00
|
|
|
namespace rocksdb {
|
2011-03-18 23:37:00 +01:00
|
|
|
|
2015-12-16 21:08:30 +01:00
|
|
|
BlockBuilder::BlockBuilder(int block_restart_interval, bool use_delta_encoding)
|
2013-10-08 03:33:49 +02:00
|
|
|
: block_restart_interval_(block_restart_interval),
|
2015-12-16 21:08:30 +01:00
|
|
|
use_delta_encoding_(use_delta_encoding),
|
2011-03-18 23:37:00 +01:00
|
|
|
restarts_(),
|
|
|
|
counter_(0),
|
|
|
|
finished_(false) {
|
2013-10-08 03:33:49 +02:00
|
|
|
assert(block_restart_interval_ >= 1);
|
2011-03-18 23:37:00 +01:00
|
|
|
restarts_.push_back(0); // First restart point is at offset 0
|
2016-06-13 18:57:43 +02:00
|
|
|
estimate_ = sizeof(uint32_t) + sizeof(uint32_t);
|
2011-03-18 23:37:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void BlockBuilder::Reset() {
|
|
|
|
buffer_.clear();
|
|
|
|
restarts_.clear();
|
|
|
|
restarts_.push_back(0); // First restart point is at offset 0
|
2016-06-13 18:57:43 +02:00
|
|
|
estimate_ = sizeof(uint32_t) + sizeof(uint32_t);
|
2011-03-18 23:37:00 +01:00
|
|
|
counter_ = 0;
|
|
|
|
finished_ = false;
|
|
|
|
last_key_.clear();
|
|
|
|
}
|
|
|
|
|
2013-05-15 19:34:02 +02:00
|
|
|
size_t BlockBuilder::EstimateSizeAfterKV(const Slice& key, const Slice& value)
|
|
|
|
const {
|
|
|
|
size_t estimate = CurrentSizeEstimate();
|
|
|
|
estimate += key.size() + value.size();
|
2013-10-08 03:33:49 +02:00
|
|
|
if (counter_ >= block_restart_interval_) {
|
2013-05-15 19:34:02 +02:00
|
|
|
estimate += sizeof(uint32_t); // a new restart entry.
|
|
|
|
}
|
|
|
|
|
|
|
|
estimate += sizeof(int32_t); // varint for shared prefix length.
|
|
|
|
estimate += VarintLength(key.size()); // varint for key length.
|
|
|
|
estimate += VarintLength(value.size()); // varint for value length.
|
|
|
|
|
|
|
|
return estimate;
|
|
|
|
}
|
|
|
|
|
2011-03-18 23:37:00 +01:00
|
|
|
Slice BlockBuilder::Finish() {
|
|
|
|
// Append restart array
|
2011-04-21 00:48:11 +02:00
|
|
|
for (size_t i = 0; i < restarts_.size(); i++) {
|
2011-03-18 23:37:00 +01:00
|
|
|
PutFixed32(&buffer_, restarts_[i]);
|
|
|
|
}
|
2014-11-11 22:47:22 +01:00
|
|
|
PutFixed32(&buffer_, static_cast<uint32_t>(restarts_.size()));
|
2011-03-18 23:37:00 +01:00
|
|
|
finished_ = true;
|
|
|
|
return Slice(buffer_);
|
|
|
|
}
|
|
|
|
|
|
|
|
void BlockBuilder::Add(const Slice& key, const Slice& value) {
|
|
|
|
assert(!finished_);
|
2013-10-08 03:33:49 +02:00
|
|
|
assert(counter_ <= block_restart_interval_);
|
2015-12-16 21:08:30 +01:00
|
|
|
size_t shared = 0; // number of bytes shared with prev key
|
|
|
|
if (counter_ >= block_restart_interval_) {
|
|
|
|
// Restart compression
|
|
|
|
restarts_.push_back(static_cast<uint32_t>(buffer_.size()));
|
2016-06-13 18:57:43 +02:00
|
|
|
estimate_ += sizeof(uint32_t);
|
2015-12-16 21:08:30 +01:00
|
|
|
counter_ = 0;
|
2016-06-13 18:57:43 +02:00
|
|
|
|
|
|
|
if (use_delta_encoding_) {
|
|
|
|
// Update state
|
|
|
|
last_key_.assign(key.data(), key.size());
|
|
|
|
}
|
2015-12-16 21:08:30 +01:00
|
|
|
} else if (use_delta_encoding_) {
|
2016-06-13 18:57:43 +02:00
|
|
|
Slice last_key_piece(last_key_);
|
2011-03-18 23:37:00 +01:00
|
|
|
// See how much sharing to do with previous string
|
2016-06-13 18:57:43 +02:00
|
|
|
shared = key.difference_offset(last_key_piece);
|
|
|
|
|
|
|
|
// Update state
|
|
|
|
// We used to just copy the changed data here, but it appears to be
|
|
|
|
// faster to just copy the whole thing.
|
|
|
|
last_key_.assign(key.data(), key.size());
|
2011-03-18 23:37:00 +01:00
|
|
|
}
|
2016-06-13 18:57:43 +02:00
|
|
|
|
2011-03-18 23:37:00 +01:00
|
|
|
const size_t non_shared = key.size() - shared;
|
2016-06-13 18:57:43 +02:00
|
|
|
const size_t curr_size = buffer_.size();
|
2011-03-18 23:37:00 +01:00
|
|
|
|
|
|
|
// Add "<shared><non_shared><value_size>" to buffer_
|
2016-06-13 18:57:43 +02:00
|
|
|
PutVarint32Varint32Varint32(&buffer_, static_cast<uint32_t>(shared),
|
|
|
|
static_cast<uint32_t>(non_shared),
|
|
|
|
static_cast<uint32_t>(value.size()));
|
2011-03-18 23:37:00 +01:00
|
|
|
|
|
|
|
// Add string delta to buffer_ followed by value
|
|
|
|
buffer_.append(key.data() + shared, non_shared);
|
|
|
|
buffer_.append(value.data(), value.size());
|
|
|
|
|
|
|
|
counter_++;
|
2016-06-13 18:57:43 +02:00
|
|
|
estimate_ += buffer_.size() - curr_size;
|
2011-03-18 23:37:00 +01:00
|
|
|
}
|
|
|
|
|
2013-10-04 06:49:15 +02:00
|
|
|
} // namespace rocksdb
|