/Users/deen/code/yugabyte-db/src/yb/rocksdb/utilities/table_properties_collectors/compact_on_deletion_collector.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 | | #ifndef ROCKSDB_LITE |
22 | | #include "yb/rocksdb/utilities/table_properties_collectors/compact_on_deletion_collector.h" |
23 | | |
24 | | #include <memory> |
25 | | #include "yb/rocksdb/utilities/table_properties_collectors.h" |
26 | | |
27 | | namespace rocksdb { |
28 | | |
29 | | CompactOnDeletionCollector::CompactOnDeletionCollector( |
30 | | size_t sliding_window_size, |
31 | 327 | size_t deletion_trigger) { |
32 | 327 | deletion_trigger_ = deletion_trigger; |
33 | | |
34 | | // First, compute the number of keys in each bucket. |
35 | 327 | bucket_size_ = |
36 | 327 | (sliding_window_size + kNumBuckets - 1) / kNumBuckets; |
37 | 327 | assert(bucket_size_ > 0U); |
38 | | |
39 | 327 | Reset(); |
40 | 327 | } |
41 | | |
42 | 2.83k | void CompactOnDeletionCollector::Reset() { |
43 | 365k | for (int i = 0; i < kNumBuckets; ++i) { |
44 | 362k | num_deletions_in_buckets_[i] = 0; |
45 | 362k | } |
46 | 2.83k | current_bucket_ = 0; |
47 | 2.83k | num_keys_in_current_bucket_ = 0; |
48 | 2.83k | num_deletions_in_observation_window_ = 0; |
49 | 2.83k | need_compaction_ = false; |
50 | 2.83k | } |
51 | | |
52 | | // AddUserKey() will be called when a new key/value pair is inserted into the |
53 | | // table. |
54 | | // @params key the user key that is inserted into the table. |
55 | | // @params value the value that is inserted into the table. |
56 | | // @params file_size file size up to now |
57 | | Status CompactOnDeletionCollector::AddUserKey( |
58 | | const Slice& key, const Slice& value, |
59 | | EntryType type, SequenceNumber seq, |
60 | 2.00G | uint64_t file_size) { |
61 | 2.00G | if (need_compaction_) { |
62 | | // If the output file already needs to be compacted, skip the check. |
63 | 231M | return Status::OK(); |
64 | 231M | } |
65 | | |
66 | 1.77G | if (num_keys_in_current_bucket_ == bucket_size_) { |
67 | | // When the current bucket is full, advance the cursor of the |
68 | | // ring buffer to the next bucket. |
69 | 4.78M | current_bucket_ = (current_bucket_ + 1) % kNumBuckets; |
70 | | |
71 | | // Update the current count of observed deletion keys by excluding |
72 | | // the number of deletion keys in the oldest bucket in the |
73 | | // observation window. |
74 | 4.78M | assert(num_deletions_in_observation_window_ >= |
75 | 4.78M | num_deletions_in_buckets_[current_bucket_]); |
76 | 4.78M | num_deletions_in_observation_window_ -= |
77 | 4.78M | num_deletions_in_buckets_[current_bucket_]; |
78 | 4.78M | num_deletions_in_buckets_[current_bucket_] = 0; |
79 | 4.78M | num_keys_in_current_bucket_ = 0; |
80 | 4.78M | } |
81 | | |
82 | 1.77G | num_keys_in_current_bucket_++; |
83 | 1.77G | if (type == kEntryDelete) { |
84 | 551M | num_deletions_in_observation_window_++; |
85 | 551M | num_deletions_in_buckets_[current_bucket_]++; |
86 | 551M | if (num_deletions_in_observation_window_ >= deletion_trigger_) { |
87 | 1.11k | need_compaction_ = true; |
88 | 1.11k | } |
89 | 551M | } |
90 | 1.77G | return Status::OK(); |
91 | 1.77G | } |
92 | | |
93 | | TablePropertiesCollector* |
94 | | CompactOnDeletionCollectorFactory::CreateTablePropertiesCollector( |
95 | 327 | TablePropertiesCollectorFactory::Context context) { |
96 | 327 | return new CompactOnDeletionCollector( |
97 | 327 | sliding_window_size_, deletion_trigger_); |
98 | 327 | } |
99 | | |
100 | | std::shared_ptr<TablePropertiesCollectorFactory> |
101 | | NewCompactOnDeletionCollectorFactory( |
102 | | size_t sliding_window_size, |
103 | 327 | size_t deletion_trigger) { |
104 | 327 | return std::shared_ptr<TablePropertiesCollectorFactory>( |
105 | 327 | new CompactOnDeletionCollectorFactory( |
106 | 327 | sliding_window_size, deletion_trigger)); |
107 | 327 | } |
108 | | } // namespace rocksdb |
109 | | #endif // !ROCKSDB_LITE |