/Users/deen/code/yugabyte-db/src/yb/docdb/compaction_file_filter.h
Line | Count | Source (jump to first uncovered line) |
1 | | // Copyright (c) YugaByte, Inc. |
2 | | // |
3 | | // Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except |
4 | | // in compliance with the License. You may obtain a copy of the License at |
5 | | // |
6 | | // http://www.apache.org/licenses/LICENSE-2.0 |
7 | | // |
8 | | // Unless required by applicable law or agreed to in writing, software distributed under the License |
9 | | // is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express |
10 | | // or implied. See the License for the specific language governing permissions and limitations |
11 | | // under the License. |
12 | | // |
13 | | |
14 | | #ifndef YB_DOCDB_COMPACTION_FILE_FILTER_H_ |
15 | | #define YB_DOCDB_COMPACTION_FILE_FILTER_H_ |
16 | | |
17 | | #include <memory> |
18 | | #include "yb/docdb/docdb_fwd.h" |
19 | | #include "yb/docdb/doc_ttl_util.h" |
20 | | #include "yb/rocksdb/compaction_filter.h" |
21 | | #include "yb/server/hybrid_clock.h" |
22 | | #include "yb/util/compare_util.h" |
23 | | |
24 | | namespace yb { |
25 | | namespace docdb { |
26 | | |
27 | | typedef enum { |
28 | | EXP_NORMAL, |
29 | | EXP_TABLE_ONLY, |
30 | | EXP_TRUST_VALUE |
31 | | } ExpiryMode; |
32 | | |
33 | | struct ExpirationTime { |
34 | | // Indicates value-level TTL expiration. |
35 | | HybridTime ttl_expiration_ht = kNoExpiration; |
36 | | // Indicates creation hybrid time, used to calculate table-level TTL expiration. |
37 | | HybridTime created_ht = HybridTime::kMax; |
38 | | |
39 | | std::string ToString() const; |
40 | | }; |
41 | | |
42 | | bool operator==(const ExpirationTime& lhs, const ExpirationTime& rhs); |
43 | | |
44 | | ExpirationTime ExtractExpirationTime(const rocksdb::FileMetaData* file); |
45 | | |
46 | | bool TtlIsExpired( |
47 | | const ExpirationTime expiry, |
48 | | const MonoDelta table_ttl, |
49 | | const HybridTime now, |
50 | | const ExpiryMode mode = EXP_NORMAL); |
51 | | |
52 | | bool IsLastKeyCreatedBeforeHistoryCutoff(ExpirationTime expiry, HybridTime history_cutoff); |
53 | | |
54 | | // DocDBCompactionFileFilter will discard any files with a maximum HybridTime below |
55 | | // its max_ht_to_expire_, and keep any files with a maximum HybridTime above that value. |
56 | | // This parameter is determined by the filter factory at the time of the filter's creation. |
57 | | // table_ttl_, history_cutoff_, and filter_ht_ are all recorded at the time of filter |
58 | | // creation, and are used to sanity check the filter and ensure that we don't accidentally |
59 | | // discard a file that hasn't expired. |
60 | | class DocDBCompactionFileFilter : public rocksdb::CompactionFileFilter { |
61 | | public: |
62 | | DocDBCompactionFileFilter( |
63 | | const MonoDelta table_ttl, |
64 | | const HybridTime history_cutoff, |
65 | | const HybridTime max_ht_to_expire, |
66 | | const HybridTime filter_ht, |
67 | | const ExpiryMode mode) |
68 | | : table_ttl_(table_ttl), |
69 | | history_cutoff_(history_cutoff), |
70 | | max_ht_to_expire_(max_ht_to_expire), |
71 | | filter_ht_(filter_ht), |
72 | 0 | mode_(mode) {} |
73 | | |
74 | | rocksdb::FilterDecision Filter(const rocksdb::FileMetaData* file) override; |
75 | | |
76 | | const char* Name() const override; |
77 | | |
78 | | std::string ToString() const; |
79 | | |
80 | | private: |
81 | | const MonoDelta table_ttl_; |
82 | | const HybridTime history_cutoff_; |
83 | | const HybridTime max_ht_to_expire_; |
84 | | const HybridTime filter_ht_; |
85 | | const ExpiryMode mode_; |
86 | | }; |
87 | | |
88 | | // DocDBCompactionFileFilterFactory will create new DocDBCompactionFileFilters, using its |
89 | | // history retention policy and the current HybridTime from its clock to create constant |
90 | | // parameters for the new filter. |
91 | | class DocDBCompactionFileFilterFactory : public rocksdb::CompactionFileFilterFactory { |
92 | | public: |
93 | | DocDBCompactionFileFilterFactory( |
94 | | std::shared_ptr<HistoryRetentionPolicy> retention_policy, |
95 | | scoped_refptr<server::Clock> clock) |
96 | 0 | : retention_policy_(retention_policy), clock_(clock) {} |
97 | | |
98 | | std::unique_ptr<rocksdb::CompactionFileFilter> CreateCompactionFileFilter( |
99 | | const std::vector<rocksdb::FileMetaData*>& inputs) override; |
100 | | |
101 | | const char* Name() const override; |
102 | | |
103 | | private: |
104 | | std::shared_ptr<HistoryRetentionPolicy> retention_policy_; |
105 | | scoped_refptr<server::Clock> clock_; |
106 | | }; |
107 | | |
108 | | } // namespace docdb |
109 | | } // namespace yb |
110 | | |
111 | | #endif // YB_DOCDB_COMPACTION_FILE_FILTER_H_ |