/Users/deen/code/yugabyte-db/src/yb/rocksdb/table/flush_block_policy.cc
Line | Count | Source |
1 | | // Copyright (c) 2011-present, Facebook, Inc. All rights reserved. |
2 | | // This source code is licensed under the BSD-style license found in the |
3 | | // LICENSE file in the root directory of this source tree. An additional grant |
4 | | // of patent rights can be found in the PATENTS file in the same directory. |
5 | | // |
6 | | // The following only applies to changes made to this file as part of YugaByte development. |
7 | | // |
8 | | // Portions Copyright (c) YugaByte, Inc. |
9 | | // |
10 | | // Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except |
11 | | // in compliance with the License. You may obtain a copy of the License at |
12 | | // |
13 | | // http://www.apache.org/licenses/LICENSE-2.0 |
14 | | // |
15 | | // Unless required by applicable law or agreed to in writing, software distributed under the License |
16 | | // is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express |
17 | | // or implied. See the License for the specific language governing permissions and limitations |
18 | | // under the License. |
19 | | // |
20 | | |
21 | | #include "yb/rocksdb/options.h" |
22 | | #include "yb/rocksdb/flush_block_policy.h" |
23 | | #include "yb/rocksdb/table/block_builder.h" |
24 | | #include "yb/util/slice.h" |
25 | | |
26 | | namespace rocksdb { |
27 | | |
28 | | // Flush block by size |
29 | | class FlushBlockBySizePolicy : public FlushBlockPolicy { |
30 | | public: |
31 | | // @params block_size: Approximate size of user data packed per |
32 | | // block. |
33 | | // @params block_size_deviation: This is used to close a block before it |
34 | | // reaches the configured |
35 | | FlushBlockBySizePolicy(const uint64_t block_size, |
36 | | const uint64_t block_size_deviation, |
37 | | const size_t min_keys_per_block, |
38 | | const BlockBuilder& data_block_builder) : |
39 | | block_size_(block_size), |
40 | | block_size_deviation_(block_size_deviation), |
41 | | min_keys_per_block_(min_keys_per_block), |
42 | 147k | data_block_builder_(data_block_builder) { |
43 | 147k | } |
44 | | |
45 | | virtual bool Update(const Slice& key, |
46 | 182M | const Slice& value) override { |
47 | | // it makes no sense to flush when the data block is empty |
48 | 182M | if (data_block_builder_.empty()) { |
49 | 65.5k | return false; |
50 | 65.5k | } |
51 | | |
52 | 182M | auto curr_size = data_block_builder_.CurrentSizeEstimate(); |
53 | | |
54 | | // Do flush if one of the below two conditions is true and we already have the required minimum |
55 | | // number of keys in block: |
56 | | // 1) if the current estimated size already exceeds the block size, |
57 | | // 2) block_size_deviation is set and the estimated size after appending |
58 | | // the kv will exceed the block size and the current size is under the |
59 | | // the deviation. |
60 | 182M | return (curr_size >= block_size_ || BlockAlmostFull(key, value)181M ) |
61 | 182M | && data_block_builder_.NumKeys() >= min_keys_per_block_2.93M ; |
62 | 182M | } |
63 | | |
64 | | private: |
65 | 181M | bool BlockAlmostFull(const Slice& key, const Slice& value) const { |
66 | 181M | const auto curr_size = data_block_builder_.CurrentSizeEstimate(); |
67 | 181M | const auto estimated_size_after = |
68 | 181M | data_block_builder_.EstimateSizeAfterKV(key, value); |
69 | | |
70 | 181M | return |
71 | 181M | estimated_size_after > block_size_ && |
72 | 181M | block_size_deviation_ > 02.49M && |
73 | 181M | curr_size * 100 > block_size_ * (100 - block_size_deviation_)2.49M ; |
74 | 181M | } |
75 | | |
76 | | const uint64_t block_size_; |
77 | | const uint64_t block_size_deviation_; |
78 | | const size_t min_keys_per_block_; |
79 | | const BlockBuilder& data_block_builder_; |
80 | | }; |
81 | | |
82 | | FlushBlockPolicy* FlushBlockBySizePolicyFactory::NewFlushBlockPolicy( |
83 | | const BlockBasedTableOptions& table_options, |
84 | 65.6k | const BlockBuilder& data_block_builder) const { |
85 | 65.6k | return new FlushBlockBySizePolicy( |
86 | 65.6k | table_options.block_size, table_options.block_size_deviation, 1 /* min_keys_per_block */, |
87 | 65.6k | data_block_builder); |
88 | 65.6k | } |
89 | | |
90 | | std::unique_ptr<FlushBlockPolicy> FlushBlockBySizePolicyFactory::NewFlushBlockPolicy( |
91 | | const uint64_t size, const int deviation, const size_t min_keys_per_block, |
92 | 82.0k | const BlockBuilder& data_block_builder) { |
93 | 82.0k | return std::make_unique<FlushBlockBySizePolicy>( |
94 | 82.0k | size, deviation, min_keys_per_block, data_block_builder); |
95 | 82.0k | } |
96 | | |
97 | | } // namespace rocksdb |