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