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 <= 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    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 IOException
78     */
79    public TimeRange(long minStamp, long maxStamp)
80    throws IOException {
81      if (minStamp < 0 || maxStamp < 0) {
82        throw new IllegalArgumentException("Timestamp cannot be negative. minStamp:" + minStamp
83          + ", maxStamp" + maxStamp);
84      }
85      if (maxStamp < minStamp) {
86        throw new IOException("maxStamp is smaller than minStamp");
87      }
88      this.minStamp = minStamp;
89      this.maxStamp = maxStamp;
90      this.allTime = this.minStamp == MIN_TIME && this.maxStamp == MAX_TIME;
91    }
92  
93    /**
94     * Represents interval [minStamp, maxStamp)
95     * @param minStamp the minimum timestamp, inclusive
96     * @param maxStamp the maximum timestamp, exclusive
97     * @throws IOException
98     */
99    public TimeRange(byte [] minStamp, byte [] maxStamp)
100   throws IOException {
101     this(Bytes.toLong(minStamp), Bytes.toLong(maxStamp));
102   }
103 
104   /**
105    * @return the smallest timestamp that should be considered
106    */
107   public long getMin() {
108     return minStamp;
109   }
110 
111   /**
112    * @return the biggest timestamp that should be considered
113    */
114   public long getMax() {
115     return maxStamp;
116   }
117 
118   /**
119    * Check if it is for all time
120    * @return true if it is for all time
121    */
122   public boolean isAllTime() {
123     return allTime;
124   }
125 
126   /**
127    * Check if the specified timestamp is within this TimeRange.
128    * <p>
129    * Returns true if within interval [minStamp, maxStamp), false
130    * if not.
131    * @param bytes timestamp to check
132    * @param offset offset into the bytes
133    * @return true if within TimeRange, false if not
134    */
135   public boolean withinTimeRange(byte [] bytes, int offset) {
136   	if (allTime) {
137   	  return true;
138   	}
139   	return withinTimeRange(Bytes.toLong(bytes, offset));
140   }
141 
142   /**
143    * Check if the range has any overlap with TimeRange
144    * @param tr TimeRange
145    * @return True if there is overlap, false otherwise
146    */
147     // This method came from TimeRangeTracker. We used to go there for this function but better
148     // to come here to the immutable, unsynchronized datastructure at read time.
149   public boolean includesTimeRange(final TimeRange tr) {
150     if (this.allTime) {
151       return true;
152     }
153     return getMin() < tr.getMax() && getMax() >= tr.getMin();
154   }
155 
156   /**
157    * Check if the specified timestamp is within this TimeRange.
158    * <p>
159    * Returns true if within interval [minStamp, maxStamp), false
160    * if not.
161    * @param timestamp timestamp to check
162    * @return true if within TimeRange, false if not
163    */
164   public boolean withinTimeRange(long timestamp) {
165   	if(allTime) return true;
166   	// check if >= minStamp
167   	return (minStamp <= timestamp && timestamp < maxStamp);
168   }
169 
170   /**
171    * Check if the specified timestamp is within this TimeRange.
172    * <p>
173    * Returns true if within interval [minStamp, maxStamp), false
174    * if not.
175    * @param timestamp timestamp to check
176    * @return true if within TimeRange, false if not
177    */
178   public boolean withinOrAfterTimeRange(long timestamp) {
179     if(allTime) return true;
180     // check if >= minStamp
181     return (timestamp >= minStamp);
182   }
183 
184   /**
185    * Compare the timestamp to timerange
186    * @param timestamp
187    * @return -1 if timestamp is less than timerange,
188    * 0 if timestamp is within timerange,
189    * 1 if timestamp is greater than timerange
190    */
191   public int compare(long timestamp) {
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 }