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.
30 * <p>
31 * Evaluated according to minStamp <= timestamp < maxStamp
32 * or [minStamp,maxStamp) in interval notation.
33 * <p>
34 * Only used internally; should not be accessed directly by clients.
35 */
36 @InterfaceAudience.Public
37 @InterfaceStability.Stable
38 public class TimeRange {
39 private long minStamp = 0L;
40 private long maxStamp = Long.MAX_VALUE;
41 private boolean allTime = false;
42
43 /**
44 * Default constructor.
45 * Represents interval [0, Long.MAX_VALUE) (allTime)
46 * @deprecated This is made @InterfaceAudience.Private in the 2.0 line and above
47 */
48 @Deprecated
49 public TimeRange() {
50 allTime = true;
51 }
52
53 /**
54 * Represents interval [minStamp, Long.MAX_VALUE)
55 * @param minStamp the minimum timestamp value, inclusive
56 * @deprecated This is made @InterfaceAudience.Private in the 2.0 line and above
57 */
58 @Deprecated
59 public TimeRange(long minStamp) {
60 this.minStamp = minStamp;
61 }
62
63 /**
64 * Represents interval [minStamp, Long.MAX_VALUE)
65 * @param minStamp the minimum timestamp value, inclusive
66 * @deprecated This is removed in the 2.0 line and above
67 */
68 @Deprecated
69 public TimeRange(byte [] minStamp) {
70 this.minStamp = Bytes.toLong(minStamp);
71 }
72
73 /**
74 * Represents interval [minStamp, maxStamp)
75 * @param minStamp the minimum timestamp, inclusive
76 * @param maxStamp the maximum timestamp, exclusive
77 * @throws IllegalArgumentException if either <0,
78 * @throws IOException if max smaller than min.
79 * @deprecated This is made @InterfaceAudience.Private in the 2.0 line and above
80 */
81 @Deprecated
82 public TimeRange(long minStamp, long maxStamp) throws IOException {
83 if (minStamp < 0 || maxStamp < 0) {
84 throw new IllegalArgumentException("Timestamp cannot be negative. minStamp:" + minStamp
85 + ", maxStamp:" + maxStamp);
86 }
87 if(maxStamp < minStamp) {
88 throw new IOException("maxStamp is smaller than minStamp");
89 }
90 this.minStamp = minStamp;
91 this.maxStamp = maxStamp;
92 }
93
94 /**
95 * Represents interval [minStamp, maxStamp)
96 * @param minStamp the minimum timestamp, inclusive
97 * @param maxStamp the maximum timestamp, exclusive
98 * @throws IOException
99 * @deprecated This is removed in the 2.0 line and above
100 */
101 @Deprecated
102 public TimeRange(byte [] minStamp, byte [] maxStamp)
103 throws IOException {
104 this(Bytes.toLong(minStamp), Bytes.toLong(maxStamp));
105 }
106
107 /**
108 * @return the smallest timestamp that should be considered
109 */
110 public long getMin() {
111 return minStamp;
112 }
113
114 /**
115 * @return the biggest timestamp that should be considered
116 */
117 public long getMax() {
118 return maxStamp;
119 }
120
121 /**
122 * Check if it is for all time
123 * @return true if it is for all time
124 */
125 public boolean isAllTime() {
126 return allTime;
127 }
128
129 /**
130 * Check if the specified timestamp is within this TimeRange.
131 * <p>
132 * Returns true if within interval [minStamp, maxStamp), false
133 * if not.
134 * @param bytes timestamp to check
135 * @param offset offset into the bytes
136 * @return true if within TimeRange, false if not
137 */
138 public boolean withinTimeRange(byte [] bytes, int offset) {
139 if(allTime) return true;
140 return withinTimeRange(Bytes.toLong(bytes, offset));
141 }
142
143 /**
144 * Check if the specified timestamp is within this TimeRange.
145 * <p>
146 * Returns true if within interval [minStamp, maxStamp), false
147 * if not.
148 * @param timestamp timestamp to check
149 * @return true if within TimeRange, false if not
150 */
151 public boolean withinTimeRange(long timestamp) {
152 if(allTime) return true;
153 // check if >= minStamp
154 return (minStamp <= timestamp && timestamp < maxStamp);
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 withinOrAfterTimeRange(long timestamp) {
166 if(allTime) return true;
167 // check if >= minStamp
168 return (timestamp >= minStamp);
169 }
170
171 /**
172 * Compare the timestamp to timerange
173 * @param timestamp
174 * @return -1 if timestamp is less than timerange,
175 * 0 if timestamp is within timerange,
176 * 1 if timestamp is greater than timerange
177 */
178 public int compare(long timestamp) {
179 if (allTime) return 0;
180 if (timestamp < minStamp) {
181 return -1;
182 } else if (timestamp >= maxStamp) {
183 return 1;
184 } else {
185 return 0;
186 }
187 }
188
189 @Override
190 public String toString() {
191 StringBuilder sb = new StringBuilder();
192 sb.append("maxStamp=");
193 sb.append(this.maxStamp);
194 sb.append(", minStamp=");
195 sb.append(this.minStamp);
196 return sb.toString();
197 }
198 }