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.classification.InterfaceAudience;
25  import org.apache.hadoop.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     */
47    public TimeRange() {
48      allTime = true;
49    }
50  
51    /**
52     * Represents interval [minStamp, Long.MAX_VALUE)
53     * @param minStamp the minimum timestamp value, inclusive
54     */
55    public TimeRange(long minStamp) {
56      this.minStamp = minStamp;
57    }
58  
59    /**
60     * Represents interval [minStamp, Long.MAX_VALUE)
61     * @param minStamp the minimum timestamp value, inclusive
62     */
63    public TimeRange(byte [] minStamp) {
64    	this.minStamp = Bytes.toLong(minStamp);
65    }
66  
67    /**
68     * Represents interval [minStamp, maxStamp)
69     * @param minStamp the minimum timestamp, inclusive
70     * @param maxStamp the maximum timestamp, exclusive
71     * @throws IOException
72     */
73    public TimeRange(long minStamp, long maxStamp)
74    throws IOException {
75      if(maxStamp < minStamp) {
76        throw new IOException("maxStamp is smaller than minStamp");
77      }
78      this.minStamp = minStamp;
79      this.maxStamp = maxStamp;
80    }
81  
82    /**
83     * Represents interval [minStamp, maxStamp)
84     * @param minStamp the minimum timestamp, inclusive
85     * @param maxStamp the maximum timestamp, exclusive
86     * @throws IOException
87     */
88    public TimeRange(byte [] minStamp, byte [] maxStamp)
89    throws IOException {
90      this(Bytes.toLong(minStamp), Bytes.toLong(maxStamp));
91    }
92  
93    /**
94     * @return the smallest timestamp that should be considered
95     */
96    public long getMin() {
97      return minStamp;
98    }
99  
100   /**
101    * @return the biggest timestamp that should be considered
102    */
103   public long getMax() {
104     return maxStamp;
105   }
106 
107   /**
108    * Check if it is for all time
109    * @return true if it is for all time
110    */
111   public boolean isAllTime() {
112     return allTime;
113   }
114 
115   /**
116    * Check if the specified timestamp is within this TimeRange.
117    * <p>
118    * Returns true if within interval [minStamp, maxStamp), false
119    * if not.
120    * @param bytes timestamp to check
121    * @param offset offset into the bytes
122    * @return true if within TimeRange, false if not
123    */
124   public boolean withinTimeRange(byte [] bytes, int offset) {
125   	if(allTime) return true;
126   	return withinTimeRange(Bytes.toLong(bytes, offset));
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 timestamp timestamp to check
135    * @return true if within TimeRange, false if not
136    */
137   public boolean withinTimeRange(long timestamp) {
138   	if(allTime) return true;
139   	// check if >= minStamp
140   	return (minStamp <= timestamp && timestamp < maxStamp);
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 withinOrAfterTimeRange(long timestamp) {
152     if(allTime) return true;
153     // check if >= minStamp
154     return (timestamp >= minStamp);
155   }
156 
157   /**
158    * Compare the timestamp to timerange
159    * @param timestamp
160    * @return -1 if timestamp is less than timerange,
161    * 0 if timestamp is within timerange,
162    * 1 if timestamp is greater than timerange
163    */
164   public int compare(long timestamp) {
165     if (timestamp < minStamp) {
166       return -1;
167     } else if (timestamp >= maxStamp) {
168       return 1;
169     } else {
170       return 0;
171     }
172   }
173 
174   @Override
175   public String toString() {
176     StringBuilder sb = new StringBuilder();
177     sb.append("maxStamp=");
178     sb.append(this.maxStamp);
179     sb.append(", minStamp=");
180     sb.append(this.minStamp);
181     return sb.toString();
182   }
183 }