2016-02-10 00:12:00 +01:00
|
|
|
// Copyright (c) 2011-present, Facebook, Inc. All rights reserved.
|
2017-07-16 01:03:42 +02:00
|
|
|
// 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).
|
2015-08-04 05:42:55 +02:00
|
|
|
|
|
|
|
#ifndef ROCKSDB_LITE
|
2015-10-09 01:57:35 +02:00
|
|
|
#include "utilities/table_properties_collectors/compact_on_deletion_collector.h"
|
2015-08-04 05:42:55 +02:00
|
|
|
|
2015-10-09 01:57:35 +02:00
|
|
|
#include <memory>
|
2015-08-04 05:42:55 +02:00
|
|
|
#include "rocksdb/utilities/table_properties_collectors.h"
|
|
|
|
|
2020-02-20 21:07:53 +01:00
|
|
|
namespace ROCKSDB_NAMESPACE {
|
2015-08-04 05:42:55 +02:00
|
|
|
|
|
|
|
CompactOnDeletionCollector::CompactOnDeletionCollector(
|
2017-09-29 03:07:42 +02:00
|
|
|
size_t sliding_window_size, size_t deletion_trigger)
|
|
|
|
: bucket_size_((sliding_window_size + kNumBuckets - 1) / kNumBuckets),
|
|
|
|
current_bucket_(0),
|
|
|
|
num_keys_in_current_bucket_(0),
|
|
|
|
num_deletions_in_observation_window_(0),
|
|
|
|
deletion_trigger_(deletion_trigger),
|
|
|
|
need_compaction_(false),
|
|
|
|
finished_(false) {
|
|
|
|
memset(num_deletions_in_buckets_, 0, sizeof(size_t) * kNumBuckets);
|
2015-08-04 05:42:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// AddUserKey() will be called when a new key/value pair is inserted into the
|
|
|
|
// table.
|
|
|
|
// @params key the user key that is inserted into the table.
|
|
|
|
// @params value the value that is inserted into the table.
|
|
|
|
// @params file_size file size up to now
|
2018-03-05 22:08:17 +01:00
|
|
|
Status CompactOnDeletionCollector::AddUserKey(const Slice& /*key*/,
|
|
|
|
const Slice& /*value*/,
|
|
|
|
EntryType type,
|
|
|
|
SequenceNumber /*seq*/,
|
|
|
|
uint64_t /*file_size*/) {
|
2017-09-29 03:07:42 +02:00
|
|
|
assert(!finished_);
|
2018-09-21 00:08:20 +02:00
|
|
|
if (bucket_size_ == 0) {
|
|
|
|
// This collector is effectively disabled
|
|
|
|
return Status::OK();
|
|
|
|
}
|
|
|
|
|
2015-08-04 05:42:55 +02:00
|
|
|
if (need_compaction_) {
|
|
|
|
// If the output file already needs to be compacted, skip the check.
|
|
|
|
return Status::OK();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (num_keys_in_current_bucket_ == bucket_size_) {
|
|
|
|
// When the current bucket is full, advance the cursor of the
|
|
|
|
// ring buffer to the next bucket.
|
|
|
|
current_bucket_ = (current_bucket_ + 1) % kNumBuckets;
|
|
|
|
|
|
|
|
// Update the current count of observed deletion keys by excluding
|
|
|
|
// the number of deletion keys in the oldest bucket in the
|
|
|
|
// observation window.
|
|
|
|
assert(num_deletions_in_observation_window_ >=
|
|
|
|
num_deletions_in_buckets_[current_bucket_]);
|
|
|
|
num_deletions_in_observation_window_ -=
|
|
|
|
num_deletions_in_buckets_[current_bucket_];
|
|
|
|
num_deletions_in_buckets_[current_bucket_] = 0;
|
|
|
|
num_keys_in_current_bucket_ = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
num_keys_in_current_bucket_++;
|
|
|
|
if (type == kEntryDelete) {
|
|
|
|
num_deletions_in_observation_window_++;
|
|
|
|
num_deletions_in_buckets_[current_bucket_]++;
|
|
|
|
if (num_deletions_in_observation_window_ >= deletion_trigger_) {
|
|
|
|
need_compaction_ = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return Status::OK();
|
|
|
|
}
|
|
|
|
|
2015-10-09 01:57:35 +02:00
|
|
|
TablePropertiesCollector*
|
|
|
|
CompactOnDeletionCollectorFactory::CreateTablePropertiesCollector(
|
2018-03-05 22:08:17 +01:00
|
|
|
TablePropertiesCollectorFactory::Context /*context*/) {
|
2015-08-04 05:42:55 +02:00
|
|
|
return new CompactOnDeletionCollector(
|
2018-09-21 00:08:20 +02:00
|
|
|
sliding_window_size_.load(), deletion_trigger_.load());
|
2015-08-04 05:42:55 +02:00
|
|
|
}
|
|
|
|
|
2018-09-21 00:08:20 +02:00
|
|
|
std::shared_ptr<CompactOnDeletionCollectorFactory>
|
2015-08-04 05:42:55 +02:00
|
|
|
NewCompactOnDeletionCollectorFactory(
|
|
|
|
size_t sliding_window_size,
|
|
|
|
size_t deletion_trigger) {
|
2018-09-21 00:08:20 +02:00
|
|
|
return std::shared_ptr<CompactOnDeletionCollectorFactory>(
|
2015-08-04 05:42:55 +02:00
|
|
|
new CompactOnDeletionCollectorFactory(
|
|
|
|
sliding_window_size, deletion_trigger));
|
|
|
|
}
|
2020-02-20 21:07:53 +01:00
|
|
|
} // namespace ROCKSDB_NAMESPACE
|
2015-08-04 05:42:55 +02:00
|
|
|
#endif // !ROCKSDB_LITE
|