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