View Javadoc

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