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.io.Writable;
30
31
32
33
34
35
36
37
38 @InterfaceAudience.Private
39 public class TimeRangeTracker implements Writable {
40 static final long INITIAL_MINIMUM_TIMESTAMP = Long.MAX_VALUE;
41 long minimumTimestamp = INITIAL_MINIMUM_TIMESTAMP;
42 long maximumTimestamp = -1;
43
44
45
46
47
48 public TimeRangeTracker() {}
49
50
51
52
53
54 public TimeRangeTracker(final TimeRangeTracker trt) {
55 set(trt.getMinimumTimestamp(), trt.getMaximumTimestamp());
56 }
57
58 public TimeRangeTracker(long minimumTimestamp, long maximumTimestamp) {
59 set(minimumTimestamp, maximumTimestamp);
60 }
61
62 private void set(final long min, final long max) {
63 this.minimumTimestamp = min;
64 this.maximumTimestamp = max;
65 }
66
67
68
69
70
71 private boolean init(final long l) {
72 if (this.minimumTimestamp != INITIAL_MINIMUM_TIMESTAMP) return false;
73 set(l, l);
74 return true;
75 }
76
77
78
79
80
81
82
83 public void includeTimestamp(final Cell cell) {
84 includeTimestamp(cell.getTimestamp());
85 if (CellUtil.isDeleteColumnOrFamily(cell)) {
86 includeTimestamp(0);
87 }
88 }
89
90
91
92
93
94 void includeTimestamp(final long timestamp) {
95
96
97
98 if (timestamp < this.minimumTimestamp) {
99 synchronized (this) {
100 if (!init(timestamp)) {
101 if (timestamp < this.minimumTimestamp) {
102 this.minimumTimestamp = timestamp;
103 }
104 }
105 }
106 } else if (timestamp > this.maximumTimestamp) {
107 synchronized (this) {
108 if (!init(timestamp)) {
109 if (this.maximumTimestamp < timestamp) {
110 this.maximumTimestamp = timestamp;
111 }
112 }
113 }
114 }
115 }
116
117
118
119
120
121
122 public synchronized boolean includesTimeRange(final TimeRange tr) {
123 return (this.minimumTimestamp < tr.getMax() && this.maximumTimestamp >= tr.getMin());
124 }
125
126
127
128
129 public synchronized long getMinimumTimestamp() {
130 return minimumTimestamp;
131 }
132
133
134
135
136 public synchronized long getMaximumTimestamp() {
137 return maximumTimestamp;
138 }
139
140 public synchronized void write(final DataOutput out) throws IOException {
141 out.writeLong(minimumTimestamp);
142 out.writeLong(maximumTimestamp);
143 }
144
145 public synchronized void readFields(final DataInput in) throws IOException {
146 this.minimumTimestamp = in.readLong();
147 this.maximumTimestamp = in.readLong();
148 }
149
150 @Override
151 public synchronized String toString() {
152 return "[" + minimumTimestamp + "," + maximumTimestamp + "]";
153 }
154 }