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