]> git.proxmox.com Git - ceph.git/blob - ceph/src/rocksdb/table/block_based/block_builder.h
update ceph source to reef 18.1.2
[ceph.git] / ceph / src / rocksdb / table / block_based / block_builder.h
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).
5 //
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.
9
10 #pragma once
11 #include <stdint.h>
12
13 #include <vector>
14
15 #include "rocksdb/slice.h"
16 #include "rocksdb/table.h"
17 #include "table/block_based/data_block_hash_index.h"
18
19 namespace ROCKSDB_NAMESPACE {
20
21 class BlockBuilder {
22 public:
23 BlockBuilder(const BlockBuilder&) = delete;
24 void operator=(const BlockBuilder&) = delete;
25
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);
32
33 // Reset the contents as if the BlockBuilder was just constructed.
34 void Reset();
35
36 // Swap the contents in BlockBuilder with buffer, then reset the BlockBuilder.
37 void SwapAndReset(std::string& buffer);
38
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);
46
47 // A faster version of Add() if the previous key is already known for all
48 // Add()s.
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);
58
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.
62 Slice Finish();
63
64 // Returns an estimate of the current (uncompressed) size of the block
65 // we are building.
66 inline size_t CurrentSizeEstimate() const {
67 return estimate_ + (data_block_hash_index_builder_.Valid()
68 ? data_block_hash_index_builder_.EstimateSize()
69 : 0);
70 }
71
72 // Returns an estimated block size after appending key and value.
73 size_t EstimateSizeAfterKV(const Slice& key, const Slice& value) const;
74
75 // Return true iff no entries have been added since the last Reset()
76 bool empty() const { return buffer_.empty(); }
77
78 private:
79 inline void AddWithLastKeyImpl(const Slice& key, const Slice& value,
80 const Slice& last_key,
81 const Slice* const delta_value,
82 size_t buffer_size);
83
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_;
89
90 std::string buffer_; // Destination buffer
91 std::vector<uint32_t> restarts_; // Restart points
92 size_t estimate_;
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_;
97 #ifndef NDEBUG
98 bool add_with_last_key_called_ = false;
99 #endif
100 };
101
102 } // namespace ROCKSDB_NAMESPACE