flush_block_policy.cc 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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. #include "rocksdb/flush_block_policy.h"
  6. #include "rocksdb/options.h"
  7. #include "rocksdb/slice.h"
  8. #include "table/block_based/block_builder.h"
  9. #include "table/format.h"
  10. #include <cassert>
  11. namespace ROCKSDB_NAMESPACE {
  12. // Flush block by size
  13. class FlushBlockBySizePolicy : public FlushBlockPolicy {
  14. public:
  15. // @params block_size: Approximate size of user data packed per
  16. // block.
  17. // @params block_size_deviation: This is used to close a block before it
  18. // reaches the configured
  19. FlushBlockBySizePolicy(const uint64_t block_size,
  20. const uint64_t block_size_deviation,
  21. const bool align,
  22. const BlockBuilder& data_block_builder)
  23. : block_size_(block_size),
  24. block_size_deviation_limit_(
  25. ((block_size * (100 - block_size_deviation)) + 99) / 100),
  26. align_(align),
  27. data_block_builder_(data_block_builder) {}
  28. bool Update(const Slice& key, const Slice& value) override {
  29. // it makes no sense to flush when the data block is empty
  30. if (data_block_builder_.empty()) {
  31. return false;
  32. }
  33. auto curr_size = data_block_builder_.CurrentSizeEstimate();
  34. // Do flush if one of the below two conditions is true:
  35. // 1) if the current estimated size already exceeds the block size,
  36. // 2) block_size_deviation is set and the estimated size after appending
  37. // the kv will exceed the block size and the current size is under the
  38. // the deviation.
  39. return curr_size >= block_size_ || BlockAlmostFull(key, value);
  40. }
  41. private:
  42. bool BlockAlmostFull(const Slice& key, const Slice& value) const {
  43. if (block_size_deviation_limit_ == 0) {
  44. return false;
  45. }
  46. const auto curr_size = data_block_builder_.CurrentSizeEstimate();
  47. auto estimated_size_after =
  48. data_block_builder_.EstimateSizeAfterKV(key, value);
  49. if (align_) {
  50. estimated_size_after += kBlockTrailerSize;
  51. return estimated_size_after > block_size_;
  52. }
  53. return estimated_size_after > block_size_ &&
  54. curr_size > block_size_deviation_limit_;
  55. }
  56. const uint64_t block_size_;
  57. const uint64_t block_size_deviation_limit_;
  58. const bool align_;
  59. const BlockBuilder& data_block_builder_;
  60. };
  61. FlushBlockPolicy* FlushBlockBySizePolicyFactory::NewFlushBlockPolicy(
  62. const BlockBasedTableOptions& table_options,
  63. const BlockBuilder& data_block_builder) const {
  64. return new FlushBlockBySizePolicy(
  65. table_options.block_size, table_options.block_size_deviation,
  66. table_options.block_align, data_block_builder);
  67. }
  68. FlushBlockPolicy* FlushBlockBySizePolicyFactory::NewFlushBlockPolicy(
  69. const uint64_t size, const int deviation,
  70. const BlockBuilder& data_block_builder) {
  71. return new FlushBlockBySizePolicy(size, deviation, false, data_block_builder);
  72. }
  73. } // namespace ROCKSDB_NAMESPACE