1 // Copyright (c) 2011-present, Facebook, Inc. All rights reserved.
2 // This source code is licensed under both the GPLv2 (found in the
3 // COPYING file in the root directory) and Apache 2.0 License
4 // (found in the LICENSE.Apache file in the root directory).
11 #include "rocksdb/table_properties.h"
13 namespace ROCKSDB_NAMESPACE
{
15 // A factory of a table property collector that marks a SST
16 // file as need-compaction when it observe at least "D" deletion
17 // entries in any "N" consecutive entries or the ratio of tombstone
18 // entries in the whole file >= the specified deletion ratio.
19 class CompactOnDeletionCollectorFactory
20 : public TablePropertiesCollectorFactory
{
22 // A factory of a table property collector that marks a SST
23 // file as need-compaction when it observe at least "D" deletion
24 // entries in any "N" consecutive entries, or the ratio of tombstone
25 // entries >= deletion_ratio.
27 // @param sliding_window_size "N"
28 // @param deletion_trigger "D"
29 // @param deletion_ratio, if <= 0 or > 1, disable triggering compaction
30 // based on deletion ratio.
31 CompactOnDeletionCollectorFactory(size_t sliding_window_size
,
32 size_t deletion_trigger
,
33 double deletion_ratio
);
35 ~CompactOnDeletionCollectorFactory() {}
37 TablePropertiesCollector
* CreateTablePropertiesCollector(
38 TablePropertiesCollectorFactory::Context context
) override
;
40 // Change the value of sliding_window_size "N"
41 // Setting it to 0 disables the delete triggered compaction
42 void SetWindowSize(size_t sliding_window_size
) {
43 sliding_window_size_
.store(sliding_window_size
);
45 size_t GetWindowSize() const { return sliding_window_size_
.load(); }
47 // Change the value of deletion_trigger "D"
48 void SetDeletionTrigger(size_t deletion_trigger
) {
49 deletion_trigger_
.store(deletion_trigger
);
52 size_t GetDeletionTrigger() const { return deletion_trigger_
.load(); }
53 // Change deletion ratio.
54 // @param deletion_ratio, if <= 0 or > 1, disable triggering compaction
55 // based on deletion ratio.
56 void SetDeletionRatio(double deletion_ratio
) {
57 deletion_ratio_
.store(deletion_ratio
);
60 double GetDeletionRatio() const { return deletion_ratio_
.load(); }
61 static const char* kClassName() { return "CompactOnDeletionCollector"; }
62 const char* Name() const override
{ return kClassName(); }
64 std::string
ToString() const override
;
67 std::atomic
<size_t> sliding_window_size_
;
68 std::atomic
<size_t> deletion_trigger_
;
69 std::atomic
<double> deletion_ratio_
;
72 // Creates a factory of a table property collector that marks a SST
73 // file as need-compaction when it observe at least "D" deletion
74 // entries in any "N" consecutive entries, or the ratio of tombstone
75 // entries >= deletion_ratio.
77 // @param sliding_window_size "N". Note that this number will be
78 // round up to the smallest multiple of 128 that is no less
79 // than the specified size.
80 // @param deletion_trigger "D". Note that even when "N" is changed,
81 // the specified number for "D" will not be changed.
82 // @param deletion_ratio, if <= 0 or > 1, disable triggering compaction
83 // based on deletion ratio. Disabled by default.
84 extern std::shared_ptr
<CompactOnDeletionCollectorFactory
>
85 NewCompactOnDeletionCollectorFactory(size_t sliding_window_size
,
86 size_t deletion_trigger
,
87 double deletion_ratio
= 0);
88 } // namespace ROCKSDB_NAMESPACE
90 #endif // !ROCKSDB_LITE