/Users/deen/code/yugabyte-db/src/yb/docdb/doc_ttl_util.cc
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 | | #include "yb/docdb/doc_ttl_util.h" |
14 | | |
15 | | #include "yb/common/schema.h" |
16 | | #include "yb/docdb/value.h" |
17 | | #include "yb/server/hybrid_clock.h" |
18 | | #include "yb/util/monotime.h" |
19 | | |
20 | | using std::string; |
21 | | |
22 | | using yb::HybridTime; |
23 | | |
24 | | namespace yb { |
25 | | namespace docdb { |
26 | | |
27 | | bool HasExpiredTTL(const HybridTime& key_hybrid_time, const MonoDelta &ttl, |
28 | 27.3M | const HybridTime& read_hybrid_time) { |
29 | 27.3M | if (ttl.Equals(Value::kMaxTtl) || ttl.Equals(Value::kResetTtl)) { |
30 | 14.0M | return false; |
31 | 14.0M | } |
32 | 13.2M | return server::HybridClock::CompareHybridClocksToDelta( |
33 | 13.2M | key_hybrid_time, read_hybrid_time, ttl) > 0; |
34 | 13.2M | } |
35 | | |
36 | 0 | bool HasExpiredTTL(const HybridTime& expiration_time, const HybridTime& read_hybrid_time) { |
37 | 0 | if (expiration_time == kNoExpiration || expiration_time == kUseDefaultTTL) { |
38 | 0 | return false; |
39 | 0 | } |
40 | 0 | return expiration_time < read_hybrid_time; |
41 | 0 | } |
42 | | |
43 | 3.75M | const MonoDelta TableTTL(const Schema& schema) { |
44 | | // In this context, a ttl of kMaxTtl indicates that the table has no default TTL. |
45 | 3.75M | MonoDelta ttl = Value::kMaxTtl; |
46 | 3.75M | if (schema.table_properties().HasDefaultTimeToLive()) { |
47 | 3.60M | uint64_t default_ttl = schema.table_properties().DefaultTimeToLive(); |
48 | 3.60M | return default_ttl == kResetTTL ? Value::kMaxTtl : MonoDelta::FromMilliseconds(default_ttl); |
49 | 3.60M | } |
50 | 147k | return ttl; |
51 | 147k | } |
52 | | |
53 | 20.9k | const MonoDelta ComputeTTL(const MonoDelta& value_ttl, const MonoDelta& default_ttl) { |
54 | 20.9k | MonoDelta ttl; |
55 | | // When encoded with a value, kMaxTtl indicates that table TTL should be used. |
56 | | // If not kMaxTtl, then there is a value-level TTL that we should use instead. |
57 | | // A value of kResetTTL (i.e. 0) indicates the value should not expire, in which |
58 | | // case kMaxTtl (now representing no expiration) is returned. |
59 | 20.9k | if (!value_ttl.Equals(Value::kMaxTtl)) { |
60 | 4 | ttl = value_ttl.ToMilliseconds() == kResetTTL ? Value::kMaxTtl : value_ttl; |
61 | 20.9k | } else { |
62 | 20.9k | ttl = default_ttl; |
63 | 20.9k | } |
64 | 20.9k | return ttl; |
65 | 20.9k | } |
66 | | |
67 | 0 | const MonoDelta ComputeTTL(const MonoDelta& value_ttl, const Schema& schema) { |
68 | 0 | return ComputeTTL(value_ttl, TableTTL(schema)); |
69 | 0 | } |
70 | | |
71 | | const HybridTime FileExpirationFromValueTTL( |
72 | 4.76M | const HybridTime& key_hybrid_time, const MonoDelta& value_ttl) { |
73 | 4.76M | if (value_ttl.Equals(Value::kMaxTtl)) { |
74 | 4.76M | return kUseDefaultTTL; |
75 | 1.56k | } else if (value_ttl.Equals(Value::kResetTtl)) { |
76 | 12 | return kNoExpiration; |
77 | 12 | } |
78 | 1.54k | auto exp_time = server::HybridClock::AddPhysicalTimeToHybridTime(key_hybrid_time, value_ttl); |
79 | | // Sanity check for overflow, return no expiration if detected. |
80 | 1.54k | if (server::HybridClock::CompareHybridClocksToDelta( |
81 | 0 | key_hybrid_time, exp_time, value_ttl) != 0) { |
82 | 0 | return kNoExpiration; |
83 | 0 | } |
84 | 1.54k | return exp_time; |
85 | 1.54k | } |
86 | | |
87 | | const HybridTime MaxExpirationFromValueAndTableTTL(const HybridTime& key_hybrid_time, |
88 | 0 | const MonoDelta& table_ttl, const HybridTime& value_expiry) { |
89 | | // If the max expiration time of values requires the file never expire, |
90 | | // then don't expire the file no matter what. |
91 | 0 | if (value_expiry == kNoExpiration || key_hybrid_time.is_special()) { |
92 | 0 | return kNoExpiration; |
93 | 0 | } |
94 | | // If the table TTL indicates no expiration, then defer to the value expiration. |
95 | | // If the value expiration also indicates to use the table TTL, don't expire the file. |
96 | 0 | if (table_ttl.Equals(Value::kMaxTtl)) { |
97 | 0 | return value_expiry == kUseDefaultTTL ? kNoExpiration : value_expiry; |
98 | 0 | } |
99 | | |
100 | | // Calculate the expiration time based on table TTL only. |
101 | 0 | auto table_expiry = server::HybridClock::AddPhysicalTimeToHybridTime(key_hybrid_time, table_ttl); |
102 | | // Sanity check for overflow, return no expiration if overflow detected. |
103 | 0 | if (server::HybridClock::CompareHybridClocksToDelta( |
104 | 0 | key_hybrid_time, table_expiry, table_ttl) != 0) { |
105 | 0 | return kNoExpiration; |
106 | 0 | } |
107 | | // Return the greater of the table expiration time and the value expiration time. |
108 | 0 | return value_expiry >= table_expiry ? value_expiry : table_expiry; |
109 | 0 | } |
110 | | |
111 | | } // namespace docdb |
112 | | } // namespace yb |