fork of https://github.com/oxigraph/rocksdb and https://github.com/facebook/rocksdb for nextgraph and oxigraph
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
89 lines
3.1 KiB
89 lines
3.1 KiB
9 years ago
|
// Copyright (c) 2011-present, Facebook, Inc. All rights reserved.
|
||
8 years ago
|
// This source code is licensed under both the GPLv2 (found in the
|
||
|
// COPYING file in the root directory) and Apache 2.0 License
|
||
|
// (found in the LICENSE.Apache file in the root directory).
|
||
11 years ago
|
|
||
|
#include "rocksdb/flush_block_policy.h"
|
||
7 years ago
|
#include "rocksdb/options.h"
|
||
11 years ago
|
#include "rocksdb/slice.h"
|
||
6 years ago
|
#include "table/block_based/block_builder.h"
|
||
7 years ago
|
#include "table/format.h"
|
||
11 years ago
|
|
||
|
#include <cassert>
|
||
|
|
||
|
namespace rocksdb {
|
||
|
|
||
|
// Flush block by size
|
||
|
class FlushBlockBySizePolicy : public FlushBlockPolicy {
|
||
|
public:
|
||
|
// @params block_size: Approximate size of user data packed per
|
||
|
// block.
|
||
|
// @params block_size_deviation: This is used to close a block before it
|
||
|
// reaches the configured
|
||
|
FlushBlockBySizePolicy(const uint64_t block_size,
|
||
|
const uint64_t block_size_deviation,
|
||
7 years ago
|
const bool align,
|
||
9 years ago
|
const BlockBuilder& data_block_builder)
|
||
|
: block_size_(block_size),
|
||
|
block_size_deviation_limit_(
|
||
|
((block_size * (100 - block_size_deviation)) + 99) / 100),
|
||
7 years ago
|
align_(align),
|
||
9 years ago
|
data_block_builder_(data_block_builder) {}
|
||
11 years ago
|
|
||
6 years ago
|
bool Update(const Slice& key, const Slice& value) override {
|
||
11 years ago
|
// it makes no sense to flush when the data block is empty
|
||
|
if (data_block_builder_.empty()) {
|
||
|
return false;
|
||
|
}
|
||
|
|
||
11 years ago
|
auto curr_size = data_block_builder_.CurrentSizeEstimate();
|
||
|
|
||
|
// Do flush if one of the below two conditions is true:
|
||
|
// 1) if the current estimated size already exceeds the block size,
|
||
|
// 2) block_size_deviation is set and the estimated size after appending
|
||
|
// the kv will exceed the block size and the current size is under the
|
||
|
// the deviation.
|
||
|
return curr_size >= block_size_ || BlockAlmostFull(key, value);
|
||
|
}
|
||
|
|
||
|
private:
|
||
|
bool BlockAlmostFull(const Slice& key, const Slice& value) const {
|
||
9 years ago
|
if (block_size_deviation_limit_ == 0) {
|
||
|
return false;
|
||
|
}
|
||
|
|
||
11 years ago
|
const auto curr_size = data_block_builder_.CurrentSizeEstimate();
|
||
7 years ago
|
auto estimated_size_after =
|
||
|
data_block_builder_.EstimateSizeAfterKV(key, value);
|
||
|
|
||
|
if (align_) {
|
||
|
estimated_size_after += kBlockTrailerSize;
|
||
|
return estimated_size_after > block_size_;
|
||
|
}
|
||
11 years ago
|
|
||
9 years ago
|
return estimated_size_after > block_size_ &&
|
||
|
curr_size > block_size_deviation_limit_;
|
||
11 years ago
|
}
|
||
|
|
||
|
const uint64_t block_size_;
|
||
9 years ago
|
const uint64_t block_size_deviation_limit_;
|
||
7 years ago
|
const bool align_;
|
||
11 years ago
|
const BlockBuilder& data_block_builder_;
|
||
|
};
|
||
|
|
||
|
FlushBlockPolicy* FlushBlockBySizePolicyFactory::NewFlushBlockPolicy(
|
||
10 years ago
|
const BlockBasedTableOptions& table_options,
|
||
|
const BlockBuilder& data_block_builder) const {
|
||
11 years ago
|
return new FlushBlockBySizePolicy(
|
||
10 years ago
|
table_options.block_size, table_options.block_size_deviation,
|
||
7 years ago
|
table_options.block_align, data_block_builder);
|
||
11 years ago
|
}
|
||
|
|
||
8 years ago
|
FlushBlockPolicy* FlushBlockBySizePolicyFactory::NewFlushBlockPolicy(
|
||
|
const uint64_t size, const int deviation,
|
||
|
const BlockBuilder& data_block_builder) {
|
||
7 years ago
|
return new FlushBlockBySizePolicy(size, deviation, false, data_block_builder);
|
||
8 years ago
|
}
|
||
|
|
||
11 years ago
|
} // namespace rocksdb
|