1 // Copyright (c) 2011-present, Facebook, Inc. All rights reserved.
2 // This source code is licensed under both the GPLv2 (found in the
3 // COPYING file in the root directory) and Apache 2.0 License
4 // (found in the LICENSE.Apache file in the root directory).
6 // Copyright (c) 2011 The LevelDB Authors. All rights reserved.
7 // Use of this source code is governed by a BSD-style license that can be
8 // found in the LICENSE file. See the AUTHORS file for names of contributors.
15 #include "rocksdb/slice.h"
16 #include "rocksdb/table.h"
17 #include "table/block_based/data_block_hash_index.h"
19 namespace ROCKSDB_NAMESPACE
{
23 BlockBuilder(const BlockBuilder
&) = delete;
24 void operator=(const BlockBuilder
&) = delete;
26 explicit BlockBuilder(int block_restart_interval
,
27 bool use_delta_encoding
= true,
28 bool use_value_delta_encoding
= false,
29 BlockBasedTableOptions::DataBlockIndexType index_type
=
30 BlockBasedTableOptions::kDataBlockBinarySearch
,
31 double data_block_hash_table_util_ratio
= 0.75);
33 // Reset the contents as if the BlockBuilder was just constructed.
36 // Swap the contents in BlockBuilder with buffer, then reset the BlockBuilder.
37 void SwapAndReset(std::string
& buffer
);
39 // REQUIRES: Finish() has not been called since the last call to Reset().
40 // REQUIRES: key is larger than any previously added key
41 // DO NOT mix with AddWithLastKey() between Resets. For efficiency, use
42 // AddWithLastKey() in contexts where previous added key is already known
43 // and delta encoding might be used.
44 void Add(const Slice
& key
, const Slice
& value
,
45 const Slice
* const delta_value
= nullptr);
47 // A faster version of Add() if the previous key is already known for all
49 // REQUIRES: Finish() has not been called since the last call to Reset().
50 // REQUIRES: key is larger than any previously added key
51 // REQUIRES: if AddWithLastKey has been called since last Reset(), last_key
52 // is the key from most recent AddWithLastKey. (For convenience, last_key
53 // is ignored on first call after creation or Reset().)
54 // DO NOT mix with Add() between Resets.
55 void AddWithLastKey(const Slice
& key
, const Slice
& value
,
56 const Slice
& last_key
,
57 const Slice
* const delta_value
= nullptr);
59 // Finish building the block and return a slice that refers to the
60 // block contents. The returned slice will remain valid for the
61 // lifetime of this builder or until Reset() is called.
64 // Returns an estimate of the current (uncompressed) size of the block
66 inline size_t CurrentSizeEstimate() const {
67 return estimate_
+ (data_block_hash_index_builder_
.Valid()
68 ? data_block_hash_index_builder_
.EstimateSize()
72 // Returns an estimated block size after appending key and value.
73 size_t EstimateSizeAfterKV(const Slice
& key
, const Slice
& value
) const;
75 // Return true iff no entries have been added since the last Reset()
76 bool empty() const { return buffer_
.empty(); }
79 inline void AddWithLastKeyImpl(const Slice
& key
, const Slice
& value
,
80 const Slice
& last_key
,
81 const Slice
* const delta_value
,
84 const int block_restart_interval_
;
85 // TODO(myabandeh): put it into a separate IndexBlockBuilder
86 const bool use_delta_encoding_
;
87 // Refer to BlockIter::DecodeCurrentValue for format of delta encoded values
88 const bool use_value_delta_encoding_
;
90 std::string buffer_
; // Destination buffer
91 std::vector
<uint32_t> restarts_
; // Restart points
93 int counter_
; // Number of entries emitted since restart
94 bool finished_
; // Has Finish() been called?
95 std::string last_key_
;
96 DataBlockHashIndexBuilder data_block_hash_index_builder_
;
98 bool add_with_last_key_called_
= false;
102 } // namespace ROCKSDB_NAMESPACE