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).
9 #include "rocksdb/utilities/table_properties_collectors.h"
10 namespace ROCKSDB_NAMESPACE
{
12 class CompactOnDeletionCollector
: public TablePropertiesCollector
{
14 CompactOnDeletionCollector(size_t sliding_window_size
,
15 size_t deletion_trigger
, double deletion_raatio
);
17 // AddUserKey() will be called when a new key/value pair is inserted into the
19 // @params key the user key that is inserted into the table.
20 // @params value the value that is inserted into the table.
21 // @params file_size file size up to now
22 virtual Status
AddUserKey(const Slice
& key
, const Slice
& value
,
23 EntryType type
, SequenceNumber seq
,
24 uint64_t file_size
) override
;
26 // Finish() will be called when a table has already been built and is ready
27 // for writing the properties block.
28 // @params properties User will add their collected statistics to
30 virtual Status
Finish(UserCollectedProperties
* /*properties*/) override
;
32 // Return the human-readable properties, where the key is property name and
33 // the value is the human-readable form of value.
34 virtual UserCollectedProperties
GetReadableProperties() const override
{
35 return UserCollectedProperties();
38 // The name of the properties collector can be used for debugging purpose.
39 virtual const char* Name() const override
{
40 return "CompactOnDeletionCollector";
43 // EXPERIMENTAL Return whether the output file should be further compacted
44 virtual bool NeedCompact() const override
{
45 return need_compaction_
;
48 static const int kNumBuckets
= 128;
53 // A ring buffer that used to count the number of deletion entries for every
54 // "bucket_size_" keys.
55 size_t num_deletions_in_buckets_
[kNumBuckets
];
56 // the number of keys in a bucket
59 size_t current_bucket_
;
60 size_t num_keys_in_current_bucket_
;
61 size_t num_deletions_in_observation_window_
;
62 size_t deletion_trigger_
;
63 const double deletion_ratio_
;
64 const bool deletion_ratio_enabled_
;
65 size_t total_entries_
= 0;
66 size_t deletion_entries_
= 0;
67 // true if the current SST file needs to be compacted.
68 bool need_compaction_
;
71 } // namespace ROCKSDB_NAMESPACE
72 #endif // !ROCKSDB_LITE