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.
7 #include "utilities/table_properties_collectors/compact_on_deletion_collector.h"
10 #include "rocksdb/utilities/table_properties_collectors.h"
14 CompactOnDeletionCollector::CompactOnDeletionCollector(
15 size_t sliding_window_size
,
16 size_t deletion_trigger
) {
17 deletion_trigger_
= deletion_trigger
;
19 // First, compute the number of keys in each bucket.
21 (sliding_window_size
+ kNumBuckets
- 1) / kNumBuckets
;
22 assert(bucket_size_
> 0U);
27 void CompactOnDeletionCollector::Reset() {
28 for (int i
= 0; i
< kNumBuckets
; ++i
) {
29 num_deletions_in_buckets_
[i
] = 0;
32 num_keys_in_current_bucket_
= 0;
33 num_deletions_in_observation_window_
= 0;
34 need_compaction_
= false;
37 // AddUserKey() will be called when a new key/value pair is inserted into the
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
,
46 if (need_compaction_
) {
47 // If the output file already needs to be compacted, skip the check.
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
;
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;
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;
78 TablePropertiesCollector
*
79 CompactOnDeletionCollectorFactory::CreateTablePropertiesCollector(
80 TablePropertiesCollectorFactory::Context context
) {
81 return new CompactOnDeletionCollector(
82 sliding_window_size_
, deletion_trigger_
);
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
));
93 } // namespace rocksdb
94 #endif // !ROCKSDB_LITE