1 /*
2 *
3 * Licensed to the Apache Software Foundation (ASF) under one
4 * or more contributor license agreements. See the NOTICE file
5 * distributed with this work for additional information
6 * regarding copyright ownership. The ASF licenses this file
7 * to you under the Apache License, Version 2.0 (the
8 * "License"); you may not use this file except in compliance
9 * with the License. You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 */
19
20 package org.apache.hadoop.hbase.io;
21
22 import java.io.IOException;
23
24 import org.apache.hadoop.hbase.classification.InterfaceAudience;
25 import org.apache.hadoop.hbase.classification.InterfaceStability;
26 import org.apache.hadoop.hbase.util.Bytes;
27
28 /**
29 * Represents an interval of version timestamps. Presumes timestamps between
30 * {@link #INITIAL_MIN_TIMESTAMP} and {@link #INITIAL_MAX_TIMESTAMP} only. Gets freaked out if
31 * passed a timestamp that is < {@link #INITIAL_MIN_TIMESTAMP},
32 * <p>
33 * Evaluated according to minStamp <= timestamp < maxStamp
34 * or [minStamp,maxStamp) in interval notation.
35 * <p>
36 * Only used internally; should not be accessed directly by clients.
37 *<p>Immutable. Thread-safe.
38 */
39 @InterfaceAudience.Public
40 @InterfaceStability.Stable
41 public class TimeRange {
42 public static final long INITIAL_MIN_TIMESTAMP = 0L;
43 public static final long INITIAL_MAX_TIMESTAMP = Long.MAX_VALUE;
44 private final long minStamp;
45 private final long maxStamp;
46 private final boolean allTime;
47
48 /**
49 * Default constructor.
50 * Represents interval [0, Long.MAX_VALUE) (allTime)
51 * @deprecated This is made @InterfaceAudience.Private in the 2.0 line and above
52 */
53 @Deprecated
54 public TimeRange() {
55 this(INITIAL_MIN_TIMESTAMP, INITIAL_MAX_TIMESTAMP);
56 }
57
58 /**
59 * Represents interval [minStamp, Long.MAX_VALUE)
60 * @param minStamp the minimum timestamp value, inclusive
61 * @deprecated This is made @InterfaceAudience.Private in the 2.0 line and above
62 */
63 @Deprecated
64 public TimeRange(long minStamp) {
65 this(minStamp, INITIAL_MAX_TIMESTAMP);
66 }
67
68 /**
69 * Represents interval [minStamp, Long.MAX_VALUE)
70 * @param minStamp the minimum timestamp value, inclusive
71 * @deprecated This is removed in the 2.0 line and above
72 */
73 @Deprecated
74 public TimeRange(byte [] minStamp) {
75 this(Bytes.toLong(minStamp));
76 }
77
78 /**
79 * Represents interval [minStamp, maxStamp)
80 * @param minStamp the minimum timestamp, inclusive
81 * @param maxStamp the maximum timestamp, exclusive
82 * @throws IOException
83 * @deprecated This is removed in the 2.0 line and above
84 */
85 @Deprecated
86 public TimeRange(byte [] minStamp, byte [] maxStamp)
87 throws IOException {
88 this(Bytes.toLong(minStamp), Bytes.toLong(maxStamp));
89 }
90
91 /**
92 * Represents interval [minStamp, maxStamp)
93 * @param minStamp the minimum timestamp, inclusive
94 * @param maxStamp the maximum timestamp, exclusive
95 * @throws IllegalArgumentException if either <0,
96 * @deprecated This is made @InterfaceAudience.Private in the 2.0 line and above
97 */
98 @Deprecated
99 public TimeRange(long minStamp, long maxStamp) {
100 check(minStamp, maxStamp);
101 this.minStamp = minStamp;
102 this.maxStamp = maxStamp;
103 this.allTime = isAllTime(minStamp, maxStamp);
104 }
105
106 private static boolean isAllTime(long minStamp, long maxStamp) {
107 return minStamp == INITIAL_MIN_TIMESTAMP && maxStamp == INITIAL_MAX_TIMESTAMP;
108 }
109
110 private static void check(long minStamp, long maxStamp) {
111 if (minStamp < 0 || maxStamp < 0) {
112 throw new IllegalArgumentException("Timestamp cannot be negative. minStamp:" + minStamp
113 + ", maxStamp:" + maxStamp);
114 }
115 if (maxStamp < minStamp) {
116 throw new IllegalArgumentException("maxStamp is smaller than minStamp");
117 }
118 }
119
120 /**
121 * @return the smallest timestamp that should be considered
122 */
123 public long getMin() {
124 return minStamp;
125 }
126
127 /**
128 * @return the biggest timestamp that should be considered
129 */
130 public long getMax() {
131 return maxStamp;
132 }
133
134 /**
135 * Check if it is for all time
136 * @return true if it is for all time
137 */
138 public boolean isAllTime() {
139 return allTime;
140 }
141
142 /**
143 * Check if the specified timestamp is within this TimeRange.
144 * <p>
145 * Returns true if within interval [minStamp, maxStamp), false if not.
146 * @param bytes timestamp to check
147 * @param offset offset into the bytes
148 * @return true if within TimeRange, false if not
149 */
150 public boolean withinTimeRange(byte [] bytes, int offset) {
151 if (allTime) {
152 return true;
153 }
154 return withinTimeRange(Bytes.toLong(bytes, offset));
155 }
156
157 /**
158 * Check if the specified timestamp is within this TimeRange.
159 * <p>
160 * Returns true if within interval [minStamp, maxStamp), false
161 * if not.
162 * @param timestamp timestamp to check
163 * @return true if within TimeRange, false if not
164 */
165 public boolean withinTimeRange(long timestamp) {
166 assert timestamp >= 0;
167 if (this.allTime) {
168 return true;
169 }
170 // check if >= minStamp
171 return (minStamp <= timestamp && timestamp < maxStamp);
172 }
173
174 /**
175 * Check if the range has any overlap with TimeRange
176 * @param tr TimeRange
177 * @return True if there is overlap, false otherwise
178 */
179 // This method came from TimeRangeTracker. We used to go there for this function but better
180 // to come here to the immutable, unsynchronized datastructure at read time.
181 public boolean includesTimeRange(final TimeRange tr) {
182 if (this.allTime) {
183 return true;
184 }
185 assert tr.getMin() >= 0;
186 return getMin() < tr.getMax() && getMax() >= tr.getMin();
187 }
188
189 /**
190 * Check if the specified timestamp is within or after this TimeRange.
191 * <p>
192 * Returns true if greater than minStamp, false if not.
193 * @param timestamp timestamp to check
194 * @return true if within or after TimeRange, false if not
195 */
196 public boolean withinOrAfterTimeRange(long timestamp) {
197 assert timestamp >= 0;
198 if (allTime) {
199 return true;
200 }
201 // check if >= minStamp
202 return timestamp >= minStamp;
203 }
204
205 /**
206 * Compare the timestamp to timerange.
207 * @return -1 if timestamp is less than timerange,
208 * 0 if timestamp is within timerange,
209 * 1 if timestamp is greater than timerange
210 */
211 public int compare(long timestamp) {
212 assert timestamp >= 0;
213 if (this.allTime) {
214 return 0;
215 }
216 if (timestamp < minStamp) {
217 return -1;
218 }
219 return timestamp >= maxStamp? 1: 0;
220 }
221
222 @Override
223 public String toString() {
224 StringBuilder sb = new StringBuilder();
225 sb.append("maxStamp=");
226 sb.append(this.maxStamp);
227 sb.append(", minStamp=");
228 sb.append(this.minStamp);
229 return sb.toString();
230 }
231 }