block_builder.h 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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 <vector>
  11. #include <stdint.h>
  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. // Reset the contents as if the BlockBuilder was just constructed.
  27. void Reset();
  28. // REQUIRES: Finish() has not been called since the last call to Reset().
  29. // REQUIRES: key is larger than any previously added key
  30. void Add(const Slice& key, const Slice& value,
  31. const Slice* const delta_value = nullptr);
  32. // Finish building the block and return a slice that refers to the
  33. // block contents. The returned slice will remain valid for the
  34. // lifetime of this builder or until Reset() is called.
  35. Slice Finish();
  36. // Returns an estimate of the current (uncompressed) size of the block
  37. // we are building.
  38. inline size_t CurrentSizeEstimate() const {
  39. return estimate_ + (data_block_hash_index_builder_.Valid()
  40. ? data_block_hash_index_builder_.EstimateSize()
  41. : 0);
  42. }
  43. // Returns an estimated block size after appending key and value.
  44. size_t EstimateSizeAfterKV(const Slice& key, const Slice& value) const;
  45. // Return true iff no entries have been added since the last Reset()
  46. bool empty() const { return buffer_.empty(); }
  47. private:
  48. const int block_restart_interval_;
  49. // TODO(myabandeh): put it into a separate IndexBlockBuilder
  50. const bool use_delta_encoding_;
  51. // Refer to BlockIter::DecodeCurrentValue for format of delta encoded values
  52. const bool use_value_delta_encoding_;
  53. std::string buffer_; // Destination buffer
  54. std::vector<uint32_t> restarts_; // Restart points
  55. size_t estimate_;
  56. int counter_; // Number of entries emitted since restart
  57. bool finished_; // Has Finish() been called?
  58. std::string last_key_;
  59. DataBlockHashIndexBuilder data_block_hash_index_builder_;
  60. };
  61. } // namespace ROCKSDB_NAMESPACE