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.
14 #include "rocksdb/slice.h"
15 #include "rocksdb/table.h"
16 #include "table/block_based/data_block_hash_index.h"
18 namespace ROCKSDB_NAMESPACE
{
22 BlockBuilder(const BlockBuilder
&) = delete;
23 void operator=(const BlockBuilder
&) = delete;
25 explicit BlockBuilder(int block_restart_interval
,
26 bool use_delta_encoding
= true,
27 bool use_value_delta_encoding
= false,
28 BlockBasedTableOptions::DataBlockIndexType index_type
=
29 BlockBasedTableOptions::kDataBlockBinarySearch
,
30 double data_block_hash_table_util_ratio
= 0.75);
32 // Reset the contents as if the BlockBuilder was just constructed.
35 // Swap the contents in BlockBuilder with buffer, then reset the BlockBuilder.
36 void SwapAndReset(std::string
& buffer
);
38 // REQUIRES: Finish() has not been called since the last call to Reset().
39 // REQUIRES: key is larger than any previously added key
40 void Add(const Slice
& key
, const Slice
& value
,
41 const Slice
* const delta_value
= nullptr);
43 // Finish building the block and return a slice that refers to the
44 // block contents. The returned slice will remain valid for the
45 // lifetime of this builder or until Reset() is called.
48 // Returns an estimate of the current (uncompressed) size of the block
50 inline size_t CurrentSizeEstimate() const {
51 return estimate_
+ (data_block_hash_index_builder_
.Valid()
52 ? data_block_hash_index_builder_
.EstimateSize()
56 // Returns an estimated block size after appending key and value.
57 size_t EstimateSizeAfterKV(const Slice
& key
, const Slice
& value
) const;
59 // Return true iff no entries have been added since the last Reset()
60 bool empty() const { return buffer_
.empty(); }
63 const int block_restart_interval_
;
64 // TODO(myabandeh): put it into a separate IndexBlockBuilder
65 const bool use_delta_encoding_
;
66 // Refer to BlockIter::DecodeCurrentValue for format of delta encoded values
67 const bool use_value_delta_encoding_
;
69 std::string buffer_
; // Destination buffer
70 std::vector
<uint32_t> restarts_
; // Restart points
72 int counter_
; // Number of entries emitted since restart
73 bool finished_
; // Has Finish() been called?
74 std::string last_key_
;
75 DataBlockHashIndexBuilder data_block_hash_index_builder_
;
78 } // namespace ROCKSDB_NAMESPACE