1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.hadoop.hbase.regionserver;
20
21 import java.io.DataInput;
22 import java.io.DataOutput;
23 import java.io.IOException;
24
25 import org.apache.hadoop.hbase.Cell;
26 import org.apache.hadoop.hbase.CellUtil;
27 import org.apache.hadoop.hbase.classification.InterfaceAudience;
28 import org.apache.hadoop.hbase.io.TimeRange;
29 import org.apache.hadoop.hbase.util.Writables;
30 import org.apache.hadoop.io.Writable;
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45 @InterfaceAudience.Private
46 public class TimeRangeTracker implements Writable {
47 static final long INITIAL_MIN_TIMESTAMP = Long.MAX_VALUE;
48 static final long INITIAL_MAX_TIMESTAMP = -1L;
49 long minimumTimestamp = INITIAL_MIN_TIMESTAMP;
50 long maximumTimestamp = INITIAL_MAX_TIMESTAMP;
51
52
53
54
55
56 public TimeRangeTracker() {}
57
58
59
60
61
62 public TimeRangeTracker(final TimeRangeTracker trt) {
63 set(trt.getMin(), trt.getMax());
64 }
65
66 public TimeRangeTracker(long minimumTimestamp, long maximumTimestamp) {
67 set(minimumTimestamp, maximumTimestamp);
68 }
69
70 private void set(final long min, final long max) {
71 this.minimumTimestamp = min;
72 this.maximumTimestamp = max;
73 }
74
75
76
77
78
79 private boolean init(final long l) {
80 if (this.minimumTimestamp != INITIAL_MIN_TIMESTAMP) return false;
81 set(l, l);
82 return true;
83 }
84
85
86
87
88
89
90
91 public void includeTimestamp(final Cell cell) {
92 includeTimestamp(cell.getTimestamp());
93 if (CellUtil.isDeleteColumnOrFamily(cell)) {
94 includeTimestamp(0);
95 }
96 }
97
98
99
100
101
102 @edu.umd.cs.findbugs.annotations.SuppressWarnings(value="MT_CORRECTNESS",
103 justification="Intentional")
104 void includeTimestamp(final long timestamp) {
105
106
107
108 if (timestamp < this.minimumTimestamp) {
109 synchronized (this) {
110 if (!init(timestamp)) {
111 if (timestamp < this.minimumTimestamp) {
112 this.minimumTimestamp = timestamp;
113 }
114 }
115 }
116 } else if (timestamp > this.maximumTimestamp) {
117 synchronized (this) {
118 if (!init(timestamp)) {
119 if (this.maximumTimestamp < timestamp) {
120 this.maximumTimestamp = timestamp;
121 }
122 }
123 }
124 }
125 }
126
127
128
129
130
131
132 public synchronized boolean includesTimeRange(final TimeRange tr) {
133 return (this.minimumTimestamp < tr.getMax() && this.maximumTimestamp >= tr.getMin());
134 }
135
136
137
138
139 public synchronized long getMin() {
140 return minimumTimestamp;
141 }
142
143
144
145
146 public synchronized long getMax() {
147 return maximumTimestamp;
148 }
149
150 public synchronized void write(final DataOutput out) throws IOException {
151 out.writeLong(minimumTimestamp);
152 out.writeLong(maximumTimestamp);
153 }
154
155 public synchronized void readFields(final DataInput in) throws IOException {
156 this.minimumTimestamp = in.readLong();
157 this.maximumTimestamp = in.readLong();
158 }
159
160 @Override
161 public synchronized String toString() {
162 return "[" + minimumTimestamp + "," + maximumTimestamp + "]";
163 }
164
165
166
167
168
169
170 public static TimeRangeTracker getTimeRangeTracker(final byte [] timeRangeTrackerBytes)
171 throws IOException {
172 if (timeRangeTrackerBytes == null) return null;
173 TimeRangeTracker trt = new TimeRangeTracker();
174 Writables.copyWritable(timeRangeTrackerBytes, trt);
175 return trt;
176 }
177
178
179
180
181
182
183 static TimeRange getTimeRange(final byte [] timeRangeTrackerBytes) throws IOException {
184 TimeRangeTracker trt = getTimeRangeTracker(timeRangeTrackerBytes);
185 return trt == null? null: trt.toTimeRange();
186 }
187
188
189
190
191 TimeRange toTimeRange() throws IOException {
192 long min = getMin();
193 long max = getMax();
194
195 if (min == INITIAL_MIN_TIMESTAMP) {
196 min = TimeRange.INITIAL_MIN_TIMESTAMP;
197 }
198 if (max == INITIAL_MAX_TIMESTAMP) {
199 max = TimeRange.INITIAL_MAX_TIMESTAMP;
200 }
201 return new TimeRange(min, max);
202 }
203 }