1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
30
31
32
33
34
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
49
50
51 public TimeRange() {
52 allTime = true;
53 }
54
55
56
57
58
59 public TimeRange(long minStamp) {
60 this.minStamp = minStamp;
61 this.allTime = this.minStamp == MIN_TIME;
62 }
63
64
65
66
67
68 public TimeRange(byte [] minStamp) {
69 this.minStamp = Bytes.toLong(minStamp);
70 this.allTime = false;
71 }
72
73
74
75
76
77
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
95
96
97
98
99 public TimeRange(byte [] minStamp, byte [] maxStamp)
100 throws IOException {
101 this(Bytes.toLong(minStamp), Bytes.toLong(maxStamp));
102 }
103
104
105
106
107 public long getMin() {
108 return minStamp;
109 }
110
111
112
113
114 public long getMax() {
115 return maxStamp;
116 }
117
118
119
120
121
122 public boolean isAllTime() {
123 return allTime;
124 }
125
126
127
128
129
130
131
132
133
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
144
145
146
147
148
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
158
159
160
161
162
163
164 public boolean withinTimeRange(long timestamp) {
165 if(allTime) return true;
166
167 return (minStamp <= timestamp && timestamp < maxStamp);
168 }
169
170
171
172
173
174
175
176
177
178 public boolean withinOrAfterTimeRange(long timestamp) {
179 if(allTime) return true;
180
181 return (timestamp >= minStamp);
182 }
183
184
185
186
187
188
189
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 }