]> git.proxmox.com Git - ceph.git/blob - ceph/src/rocksdb/utilities/table_properties_collectors/compact_on_deletion_collector.cc
add subtree-ish sources for 12.0.3
[ceph.git] / ceph / src / rocksdb / utilities / table_properties_collectors / compact_on_deletion_collector.cc
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 #ifndef ROCKSDB_LITE
7 #include "utilities/table_properties_collectors/compact_on_deletion_collector.h"
8
9 #include <memory>
10 #include "rocksdb/utilities/table_properties_collectors.h"
11
12 namespace rocksdb {
13
14 CompactOnDeletionCollector::CompactOnDeletionCollector(
15 size_t sliding_window_size,
16 size_t deletion_trigger) {
17 deletion_trigger_ = deletion_trigger;
18
19 // First, compute the number of keys in each bucket.
20 bucket_size_ =
21 (sliding_window_size + kNumBuckets - 1) / kNumBuckets;
22 assert(bucket_size_ > 0U);
23
24 Reset();
25 }
26
27 void CompactOnDeletionCollector::Reset() {
28 for (int i = 0; i < kNumBuckets; ++i) {
29 num_deletions_in_buckets_[i] = 0;
30 }
31 current_bucket_ = 0;
32 num_keys_in_current_bucket_ = 0;
33 num_deletions_in_observation_window_ = 0;
34 need_compaction_ = false;
35 }
36
37 // AddUserKey() will be called when a new key/value pair is inserted into the
38 // table.
39 // @params key the user key that is inserted into the table.
40 // @params value the value that is inserted into the table.
41 // @params file_size file size up to now
42 Status CompactOnDeletionCollector::AddUserKey(
43 const Slice& key, const Slice& value,
44 EntryType type, SequenceNumber seq,
45 uint64_t file_size) {
46 if (need_compaction_) {
47 // If the output file already needs to be compacted, skip the check.
48 return Status::OK();
49 }
50
51 if (num_keys_in_current_bucket_ == bucket_size_) {
52 // When the current bucket is full, advance the cursor of the
53 // ring buffer to the next bucket.
54 current_bucket_ = (current_bucket_ + 1) % kNumBuckets;
55
56 // Update the current count of observed deletion keys by excluding
57 // the number of deletion keys in the oldest bucket in the
58 // observation window.
59 assert(num_deletions_in_observation_window_ >=
60 num_deletions_in_buckets_[current_bucket_]);
61 num_deletions_in_observation_window_ -=
62 num_deletions_in_buckets_[current_bucket_];
63 num_deletions_in_buckets_[current_bucket_] = 0;
64 num_keys_in_current_bucket_ = 0;
65 }
66
67 num_keys_in_current_bucket_++;
68 if (type == kEntryDelete) {
69 num_deletions_in_observation_window_++;
70 num_deletions_in_buckets_[current_bucket_]++;
71 if (num_deletions_in_observation_window_ >= deletion_trigger_) {
72 need_compaction_ = true;
73 }
74 }
75 return Status::OK();
76 }
77
78 TablePropertiesCollector*
79 CompactOnDeletionCollectorFactory::CreateTablePropertiesCollector(
80 TablePropertiesCollectorFactory::Context context) {
81 return new CompactOnDeletionCollector(
82 sliding_window_size_, deletion_trigger_);
83 }
84
85 std::shared_ptr<TablePropertiesCollectorFactory>
86 NewCompactOnDeletionCollectorFactory(
87 size_t sliding_window_size,
88 size_t deletion_trigger) {
89 return std::shared_ptr<TablePropertiesCollectorFactory>(
90 new CompactOnDeletionCollectorFactory(
91 sliding_window_size, deletion_trigger));
92 }
93 } // namespace rocksdb
94 #endif // !ROCKSDB_LITE