View Javadoc

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 &lt;= timestamp &lt; 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    static final long INITIAL_MIN_TIMESTAMP = 0l;
40    private static final long MIN_TIME = INITIAL_MIN_TIMESTAMP;
41    static final long INITIAL_MAX_TIMESTAMP = Long.MAX_VALUE;
42    static final long MAX_TIME = INITIAL_MAX_TIMESTAMP;
43    private long minStamp = MIN_TIME;
44    private long maxStamp = MAX_TIME;
45    private final boolean allTime;
46  
47    /**
48     * Default constructor.
49     * Represents interval [0, Long.MAX_VALUE) (allTime)
50     */
51    public TimeRange() {
52      allTime = true;
53    }
54  
55    /**
56     * Represents interval [minStamp, Long.MAX_VALUE)
57     * @param minStamp the minimum timestamp value, inclusive
58     */
59    public TimeRange(long minStamp) {
60      this.minStamp = minStamp;
61      this.allTime = this.minStamp == MIN_TIME;
62    }
63  
64    /**
65     * Represents interval [minStamp, Long.MAX_VALUE)
66     * @param minStamp the minimum timestamp value, inclusive
67     */
68    public TimeRange(byte [] minStamp) {
69      this.minStamp = Bytes.toLong(minStamp);
70      this.allTime = false;
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
78     */
79    public TimeRange(long minStamp, long maxStamp) {
80      if (minStamp < 0 || maxStamp < 0) {
81        throw new IllegalArgumentException("Timestamp cannot be negative. minStamp:" + minStamp
82          + ", maxStamp:" + maxStamp);
83      }
84      if (maxStamp < minStamp) {
85        throw new IllegalArgumentException("maxStamp is smaller than minStamp");
86      }
87      this.minStamp = minStamp;
88      this.maxStamp = maxStamp;
89      this.allTime = this.minStamp == MIN_TIME && this.maxStamp == MAX_TIME;
90    }
91  
92    /**
93     * Represents interval [minStamp, maxStamp)
94     * @param minStamp the minimum timestamp, inclusive
95     * @param maxStamp the maximum timestamp, exclusive
96     * @throws IOException
97     */
98    public TimeRange(byte [] minStamp, byte [] maxStamp)
99    throws IOException {
100     this(Bytes.toLong(minStamp), Bytes.toLong(maxStamp));
101   }
102 
103   /**
104    * @return the smallest timestamp that should be considered
105    */
106   public long getMin() {
107     return minStamp;
108   }
109 
110   /**
111    * @return the biggest timestamp that should be considered
112    */
113   public long getMax() {
114     return maxStamp;
115   }
116 
117   /**
118    * Check if it is for all time
119    * @return true if it is for all time
120    */
121   public boolean isAllTime() {
122     return allTime;
123   }
124 
125   /**
126    * Check if the specified timestamp is within this TimeRange.
127    * <p>
128    * Returns true if within interval [minStamp, maxStamp), false
129    * if not.
130    * @param bytes timestamp to check
131    * @param offset offset into the bytes
132    * @return true if within TimeRange, false if not
133    */
134   public boolean withinTimeRange(byte [] bytes, int offset) {
135     if(allTime) return true;
136     return withinTimeRange(Bytes.toLong(bytes, offset));
137   }
138 
139   /**
140    * Check if the specified timestamp is within this TimeRange.
141    * <p>
142    * Returns true if within interval [minStamp, maxStamp), false
143    * if not.
144    * @param timestamp timestamp to check
145    * @return true if within TimeRange, false if not
146    */
147   public boolean withinTimeRange(long timestamp) {
148     if (this.allTime) {
149       return true;
150     }
151     // check if >= minStamp
152     return (minStamp <= timestamp && timestamp < maxStamp);
153   }
154 
155   /**
156    * Check if the range has any overlap with TimeRange
157    * @param tr TimeRange
158    * @return True if there is overlap, false otherwise
159    */
160   // This method came from TimeRangeTracker. We used to go there for this function but better
161   // to come here to the immutable, unsynchronized datastructure at read time.
162   public boolean includesTimeRange(final TimeRange tr) {
163     if (this.allTime) {
164       return true;
165     }
166     return getMin() < tr.getMax() && getMax() >= tr.getMin();
167   }
168 
169   /**
170    * Check if the specified timestamp is within this TimeRange.
171    * <p>
172    * Returns true if within interval [minStamp, maxStamp), false
173    * if not.
174    * @param timestamp timestamp to check
175    * @return true if within TimeRange, false if not
176    */
177   public boolean withinOrAfterTimeRange(long timestamp) {
178     if(allTime) return true;
179     // check if >= minStamp
180     return (timestamp >= minStamp);
181   }
182 
183   /**
184    * Compare the timestamp to timerange
185    * @param timestamp
186    * @return -1 if timestamp is less than timerange,
187    * 0 if timestamp is within timerange,
188    * 1 if timestamp is greater than timerange
189    */
190   public int compare(long timestamp) {
191     if (allTime) return 0;
192     if (timestamp < minStamp) {
193       return -1;
194     } else if (timestamp >= maxStamp) {
195       return 1;
196     } else {
197       return 0;
198     }
199   }
200 
201   @Override
202   public String toString() {
203     StringBuilder sb = new StringBuilder();
204     sb.append("maxStamp=");
205     sb.append(this.maxStamp);
206     sb.append(", minStamp=");
207     sb.append(this.minStamp);
208     return sb.toString();
209   }
210 }