/Users/deen/code/yugabyte-db/src/yb/rocksdb/utilities/table_properties_collectors/compact_on_deletion_collector.h
Line | Count | Source (jump to first uncovered line) |
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 | | #pragma once |
22 | | |
23 | | #ifndef ROCKSDB_LITE |
24 | | #include "yb/rocksdb/utilities/table_properties_collectors.h" |
25 | | namespace rocksdb { |
26 | | |
27 | | // A factory of a table property collector that marks a SST |
28 | | // file as need-compaction when it observe at least "D" deletion |
29 | | // entries in any "N" consecutive entires. |
30 | | class CompactOnDeletionCollectorFactory |
31 | | : public TablePropertiesCollectorFactory { |
32 | | public: |
33 | | // A factory of a table property collector that marks a SST |
34 | | // file as need-compaction when it observe at least "D" deletion |
35 | | // entries in any "N" consecutive entires. |
36 | | // |
37 | | // @param sliding_window_size "N" |
38 | | // @param deletion_trigger "D" |
39 | | CompactOnDeletionCollectorFactory( |
40 | | size_t sliding_window_size, |
41 | | size_t deletion_trigger) : |
42 | | sliding_window_size_(sliding_window_size), |
43 | 327 | deletion_trigger_(deletion_trigger) {} |
44 | | |
45 | 327 | virtual ~CompactOnDeletionCollectorFactory() {} |
46 | | |
47 | | virtual TablePropertiesCollector* CreateTablePropertiesCollector( |
48 | | TablePropertiesCollectorFactory::Context context) override; |
49 | | |
50 | 0 | virtual const char* Name() const override { |
51 | 0 | return "CompactOnDeletionCollector"; |
52 | 0 | } |
53 | | |
54 | | private: |
55 | | size_t sliding_window_size_; |
56 | | size_t deletion_trigger_; |
57 | | }; |
58 | | |
59 | | class CompactOnDeletionCollector : public TablePropertiesCollector { |
60 | | public: |
61 | | CompactOnDeletionCollector( |
62 | | size_t sliding_window_size, |
63 | | size_t deletion_trigger); |
64 | | |
65 | | // AddUserKey() will be called when a new key/value pair is inserted into the |
66 | | // table. |
67 | | // @params key the user key that is inserted into the table. |
68 | | // @params value the value that is inserted into the table. |
69 | | // @params file_size file size up to now |
70 | | virtual Status AddUserKey(const Slice& key, const Slice& value, |
71 | | EntryType type, SequenceNumber seq, |
72 | | uint64_t file_size) override; |
73 | | |
74 | | // Finish() will be called when a table has already been built and is ready |
75 | | // for writing the properties block. |
76 | | // @params properties User will add their collected statistics to |
77 | | // `properties`. |
78 | 2.50k | virtual Status Finish(UserCollectedProperties* properties) override { |
79 | 2.50k | Reset(); |
80 | 2.50k | return Status::OK(); |
81 | 2.50k | } |
82 | | |
83 | | // Return the human-readable properties, where the key is property name and |
84 | | // the value is the human-readable form of value. |
85 | 0 | virtual UserCollectedProperties GetReadableProperties() const override { |
86 | 0 | return UserCollectedProperties(); |
87 | 0 | } |
88 | | |
89 | | // The name of the properties collector can be used for debugging purpose. |
90 | 0 | virtual const char* Name() const override { |
91 | 0 | return "CompactOnDeletionCollector"; |
92 | 0 | } |
93 | | |
94 | | // EXPERIMENTAL Return whether the output file should be further compacted |
95 | 2.61k | virtual bool NeedCompact() const override { |
96 | 2.61k | return need_compaction_; |
97 | 2.61k | } |
98 | | |
99 | | static const int kNumBuckets = 128; |
100 | | |
101 | | private: |
102 | | void Reset(); |
103 | | |
104 | | // A ring buffer that used to count the number of deletion entries for every |
105 | | // "bucket_size_" keys. |
106 | | size_t num_deletions_in_buckets_[kNumBuckets]; |
107 | | // the number of keys in a bucket |
108 | | size_t bucket_size_; |
109 | | |
110 | | size_t current_bucket_; |
111 | | size_t num_keys_in_current_bucket_; |
112 | | size_t num_deletions_in_observation_window_; |
113 | | size_t deletion_trigger_; |
114 | | // true if the current SST file needs to be compacted. |
115 | | bool need_compaction_; |
116 | | }; |
117 | | } // namespace rocksdb |
118 | | #endif // !ROCKSDB_LITE |