block_builder.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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. #pragma once
  10. #include <stdint.h>
  11. #include <vector>
  12. #include "rocksdb/slice.h"
  13. #include "rocksdb/table.h"
  14. #include "table/block_based/data_block_hash_index.h"
  15. namespace ROCKSDB_NAMESPACE {
  16. class BlockBuilder {
  17. public:
  18. BlockBuilder(const BlockBuilder&) = delete;
  19. void operator=(const BlockBuilder&) = delete;
  20. explicit BlockBuilder(int block_restart_interval,
  21. bool use_delta_encoding = true,
  22. bool use_value_delta_encoding = false,
  23. BlockBasedTableOptions::DataBlockIndexType index_type =
  24. BlockBasedTableOptions::kDataBlockBinarySearch,
  25. double data_block_hash_table_util_ratio = 0.75,
  26. size_t ts_sz = 0,
  27. bool persist_user_defined_timestamps = true,
  28. bool is_user_key = false);
  29. // Reset the contents as if the BlockBuilder was just constructed.
  30. void Reset();
  31. // Swap the contents in BlockBuilder with buffer, then reset the BlockBuilder.
  32. void SwapAndReset(std::string& buffer);
  33. // REQUIRES: Finish() has not been called since the last call to Reset().
  34. // REQUIRES: Unless a range tombstone block, key is larger than any previously
  35. // added key
  36. // DO NOT mix with AddWithLastKey() between Resets. For efficiency, use
  37. // AddWithLastKey() in contexts where previous added key is already known
  38. // and delta encoding might be used.
  39. void Add(const Slice& key, const Slice& value,
  40. const Slice* const delta_value = nullptr,
  41. bool skip_delta_encoding = false);
  42. // A faster version of Add() if the previous key is already known for all
  43. // Add()s.
  44. // REQUIRES: Finish() has not been called since the last call to Reset().
  45. // REQUIRES: Unless a range tombstone block, key is larger than any previously
  46. // added key
  47. // REQUIRES: if AddWithLastKey has been called since last Reset(), last_key
  48. // is the key from most recent AddWithLastKey. (For convenience, last_key
  49. // is ignored on first call after creation or Reset().)
  50. // DO NOT mix with Add() between Resets.
  51. void AddWithLastKey(const Slice& key, const Slice& value,
  52. const Slice& last_key,
  53. const Slice* const delta_value = nullptr,
  54. bool skip_delta_encoding = false);
  55. // Finish building the block and return a slice that refers to the
  56. // block contents. The returned slice will remain valid for the
  57. // lifetime of this builder or until Reset() is called.
  58. Slice Finish();
  59. // Returns an estimate of the current (uncompressed) size of the block
  60. // we are building.
  61. inline size_t CurrentSizeEstimate() const {
  62. return estimate_ + (data_block_hash_index_builder_.Valid()
  63. ? data_block_hash_index_builder_.EstimateSize()
  64. : 0);
  65. }
  66. // Returns an estimated block size after appending key and value.
  67. size_t EstimateSizeAfterKV(const Slice& key, const Slice& value) const;
  68. // Return true iff no entries have been added since the last Reset()
  69. bool empty() const { return buffer_.empty(); }
  70. std::string& MutableBuffer() { return buffer_; }
  71. private:
  72. inline void AddWithLastKeyImpl(const Slice& key, const Slice& value,
  73. const Slice& last_key,
  74. const Slice* const delta_value,
  75. bool skip_delta_encoding, size_t buffer_size);
  76. inline const Slice MaybeStripTimestampFromKey(std::string* key_buf,
  77. const Slice& key);
  78. const int block_restart_interval_;
  79. // TODO(myabandeh): put it into a separate IndexBlockBuilder
  80. const bool use_delta_encoding_;
  81. // Refer to BlockIter::DecodeCurrentValue for format of delta encoded values
  82. const bool use_value_delta_encoding_;
  83. // Size in bytes for the user-defined timestamp to strip in a user key.
  84. // This is non-zero if there is user-defined timestamp in the user key and it
  85. // should not be persisted.
  86. const size_t strip_ts_sz_;
  87. // Whether the keys provided to build this block are user keys. If not,
  88. // the keys are internal keys. This will affect how timestamp stripping is
  89. // done for the key if `persisted_user_defined_timestamps_` is false and
  90. // `ts_sz_` is non-zero.
  91. // The timestamp stripping only applies to the keys added to the block. If the
  92. // value contains user defined timestamp that needed to be stripped too, such
  93. // as the `first_internal_key` in an `IndexValue` for an index block, the
  94. // value part for a range deletion entry, their timestamp should be stripped
  95. // before calling `BlockBuilder::Add`.
  96. // Timestamp stripping only applies to data block and index blocks including
  97. // index block for data blocks, index block for partitioned filter blocks,
  98. // index block for partitioned index blocks. In summary, this only applies to
  99. // block whose key are real user keys or internal keys created from user keys.
  100. const bool is_user_key_;
  101. std::string buffer_; // Destination buffer
  102. std::vector<uint32_t> restarts_; // Restart points
  103. size_t estimate_;
  104. int counter_; // Number of entries emitted since restart
  105. bool finished_; // Has Finish() been called?
  106. std::string last_key_;
  107. DataBlockHashIndexBuilder data_block_hash_index_builder_;
  108. #ifndef NDEBUG
  109. bool add_with_last_key_called_ = false;
  110. #endif
  111. };
  112. } // namespace ROCKSDB_NAMESPACE