1 /*
2 * Copyright 2009 The Apache Software Foundation
3 *
4 * Licensed to the Apache Software Foundation (ASF) under one
5 * or more contributor license agreements. See the NOTICE file
6 * distributed with this work for additional information
7 * regarding copyright ownership. The ASF licenses this file
8 * to you under the Apache License, Version 2.0 (the
9 * "License"); you may not use this file except in compliance
10 * with the License. You may obtain a copy of the License at
11 *
12 * http://www.apache.org/licenses/LICENSE-2.0
13 *
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 */
20
21 package org.apache.hadoop.hbase.io;
22
23 import java.io.DataInput;
24 import java.io.DataOutput;
25 import java.io.IOException;
26
27 import org.apache.hadoop.io.Writable;
28
29 import org.apache.hadoop.hbase.util.Bytes;
30
31 /**
32 * Represents an interval of version timestamps.
33 * <p>
34 * Evaluated according to minStamp <= timestamp < maxStamp
35 * or [minStamp,maxStamp) in interval notation.
36 * <p>
37 * Only used internally; should not be accessed directly by clients.
38 */
39 public class TimeRange implements Writable {
40 private long minStamp = 0L;
41 private long maxStamp = Long.MAX_VALUE;
42 private boolean allTime = false;
43
44 /**
45 * Default constructor.
46 * Represents interval [0, Long.MAX_VALUE) (allTime)
47 */
48 public TimeRange() {
49 allTime = true;
50 }
51
52 /**
53 * Represents interval [minStamp, Long.MAX_VALUE)
54 * @param minStamp the minimum timestamp value, inclusive
55 */
56 public TimeRange(long minStamp) {
57 this.minStamp = minStamp;
58 }
59
60 /**
61 * Represents interval [minStamp, Long.MAX_VALUE)
62 * @param minStamp the minimum timestamp value, inclusive
63 */
64 public TimeRange(byte [] minStamp) {
65 this.minStamp = Bytes.toLong(minStamp);
66 }
67
68 /**
69 * Represents interval [minStamp, maxStamp)
70 * @param minStamp the minimum timestamp, inclusive
71 * @param maxStamp the maximum timestamp, exclusive
72 * @throws IOException
73 */
74 public TimeRange(long minStamp, long maxStamp)
75 throws IOException {
76 if(maxStamp < minStamp) {
77 throw new IOException("maxStamp is smaller than minStamp");
78 }
79 this.minStamp = minStamp;
80 this.maxStamp = maxStamp;
81 }
82
83 /**
84 * Represents interval [minStamp, maxStamp)
85 * @param minStamp the minimum timestamp, inclusive
86 * @param maxStamp the maximum timestamp, exclusive
87 * @throws IOException
88 */
89 public TimeRange(byte [] minStamp, byte [] maxStamp)
90 throws IOException {
91 this(Bytes.toLong(minStamp), Bytes.toLong(maxStamp));
92 }
93
94 /**
95 * @return the smallest timestamp that should be considered
96 */
97 public long getMin() {
98 return minStamp;
99 }
100
101 /**
102 * @return the biggest timestamp that should be considered
103 */
104 public long getMax() {
105 return maxStamp;
106 }
107
108 /**
109 * Check if the specified timestamp is within this TimeRange.
110 * <p>
111 * Returns true if within interval [minStamp, maxStamp), false
112 * if not.
113 * @param bytes timestamp to check
114 * @param offset offset into the bytes
115 * @return true if within TimeRange, false if not
116 */
117 public boolean withinTimeRange(byte [] bytes, int offset) {
118 if(allTime) return true;
119 return withinTimeRange(Bytes.toLong(bytes, offset));
120 }
121
122 /**
123 * Check if the specified timestamp is within this TimeRange.
124 * <p>
125 * Returns true if within interval [minStamp, maxStamp), false
126 * if not.
127 * @param timestamp timestamp to check
128 * @return true if within TimeRange, false if not
129 */
130 public boolean withinTimeRange(long timestamp) {
131 if(allTime) return true;
132 // check if >= minStamp
133 return (minStamp <= timestamp && timestamp < maxStamp);
134 }
135
136 /**
137 * Check if the specified timestamp is within this TimeRange.
138 * <p>
139 * Returns true if within interval [minStamp, maxStamp), false
140 * if not.
141 * @param timestamp timestamp to check
142 * @return true if within TimeRange, false if not
143 */
144 public boolean withinOrAfterTimeRange(long timestamp) {
145 if(allTime) return true;
146 // check if >= minStamp
147 return (timestamp >= minStamp);
148 }
149
150 /**
151 * Compare the timestamp to timerange
152 * @param timestamp
153 * @return -1 if timestamp is less than timerange,
154 * 0 if timestamp is within timerange,
155 * 1 if timestamp is greater than timerange
156 */
157 public int compare(long timestamp) {
158 if (timestamp < minStamp) {
159 return -1;
160 } else if (timestamp >= maxStamp) {
161 return 1;
162 } else {
163 return 0;
164 }
165 }
166
167 @Override
168 public String toString() {
169 StringBuilder sb = new StringBuilder();
170 sb.append("maxStamp=");
171 sb.append(this.maxStamp);
172 sb.append(", minStamp=");
173 sb.append(this.minStamp);
174 return sb.toString();
175 }
176
177 //Writable
178 public void readFields(final DataInput in) throws IOException {
179 this.minStamp = in.readLong();
180 this.maxStamp = in.readLong();
181 this.allTime = in.readBoolean();
182 }
183
184 public void write(final DataOutput out) throws IOException {
185 out.writeLong(minStamp);
186 out.writeLong(maxStamp);
187 out.writeBoolean(this.allTime);
188 }
189 }