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