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 @edu.umd.cs.findbugs.annotations.SuppressWarnings(value="MT_CORRECTNESS",
95 justification="Intentional")
96 void includeTimestamp(final long timestamp) {
97
98
99
100 if (timestamp < this.minimumTimestamp) {
101 synchronized (this) {
102 if (!init(timestamp)) {
103 if (timestamp < this.minimumTimestamp) {
104 this.minimumTimestamp = timestamp;
105 }
106 }
107 }
108 } else if (timestamp > this.maximumTimestamp) {
109 synchronized (this) {
110 if (!init(timestamp)) {
111 if (this.maximumTimestamp < timestamp) {
112 this.maximumTimestamp = timestamp;
113 }
114 }
115 }
116 }
117 }
118
119
120
121
122
123
124 public synchronized boolean includesTimeRange(final TimeRange tr) {
125 return (this.minimumTimestamp < tr.getMax() && this.maximumTimestamp >= tr.getMin());
126 }
127
128
129
130
131 public synchronized long getMinimumTimestamp() {
132 return minimumTimestamp;
133 }
134
135
136
137
138 public synchronized long getMaximumTimestamp() {
139 return maximumTimestamp;
140 }
141
142 public synchronized void write(final DataOutput out) throws IOException {
143 out.writeLong(minimumTimestamp);
144 out.writeLong(maximumTimestamp);
145 }
146
147 public synchronized void readFields(final DataInput in) throws IOException {
148 this.minimumTimestamp = in.readLong();
149 this.maximumTimestamp = in.readLong();
150 }
151
152 @Override
153 public synchronized String toString() {
154 return "[" + minimumTimestamp + "," + maximumTimestamp + "]";
155 }
156 }