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).
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"
16 // Flush block by size
17 class FlushBlockBySizePolicy
: public FlushBlockPolicy
{
19 // @params block_size: Approximate size of user data packed per
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
,
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),
31 data_block_builder_(data_block_builder
) {}
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()) {
39 auto curr_size
= data_block_builder_
.CurrentSizeEstimate();
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
46 return curr_size
>= block_size_
|| BlockAlmostFull(key
, value
);
50 bool BlockAlmostFull(const Slice
& key
, const Slice
& value
) const {
51 if (block_size_deviation_limit_
== 0) {
55 const auto curr_size
= data_block_builder_
.CurrentSizeEstimate();
56 auto estimated_size_after
=
57 data_block_builder_
.EstimateSizeAfterKV(key
, value
);
60 estimated_size_after
+= kBlockTrailerSize
;
61 return estimated_size_after
> block_size_
;
64 return estimated_size_after
> block_size_
&&
65 curr_size
> block_size_deviation_limit_
;
68 const uint64_t block_size_
;
69 const uint64_t block_size_deviation_limit_
;
71 const BlockBuilder
& data_block_builder_
;
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
);
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
);
88 } // namespace rocksdb