1 /**
2 * Copyright 2010 The Apache Software Foundation
3 *
4 * Licensed to the Apache Software Foundation (ASF) under one
5 * or more contributor license agreements. See the NOTICE file
6 * distributed with this work for additional information
7 * regarding copyright ownership. The ASF licenses this file
8 * to you under the Apache License, Version 2.0 (the
9 * "License"); you may not use this file except in compliance
10 * with the License. You may obtain a copy of the License at
11 *
12 * http://www.apache.org/licenses/LICENSE-2.0
13 *
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 */
20 package org.apache.hadoop.hbase.regionserver;
21
22 import java.io.DataInput;
23 import java.io.DataOutput;
24 import java.io.IOException;
25
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.io.Writable;
31
32 /**
33 * Stores the minimum and maximum timestamp values.
34 * Can be used to find if any given time range overlaps with its time range
35 * MemStores use this class to track its minimum and maximum timestamps.
36 * When writing StoreFiles, this information is stored in meta blocks and used
37 * at read time to match against the required TimeRange
38 */
39 public class TimeRangeTracker implements Writable {
40
41 long minimumTimestamp = -1;
42 long maximumTimestamp = -1;
43
44 /**
45 * Default constructor.
46 * Initializes TimeRange to be null
47 */
48 public TimeRangeTracker() {
49
50 }
51
52 /**
53 * Copy Constructor
54 * @param trt source TimeRangeTracker
55 */
56 public TimeRangeTracker(final TimeRangeTracker trt) {
57 this.minimumTimestamp = trt.getMinimumTimestamp();
58 this.maximumTimestamp = trt.getMaximumTimestamp();
59 }
60
61 public TimeRangeTracker(long minimumTimestamp, long maximumTimestamp) {
62 this.minimumTimestamp = minimumTimestamp;
63 this.maximumTimestamp = maximumTimestamp;
64 }
65
66 /**
67 * Update the current TimestampRange to include the timestamp from KeyValue
68 * If the Key is of type DeleteColumn or DeleteFamily, it includes the
69 * entire time range from 0 to timestamp of the key.
70 * @param kv the KeyValue to include
71 */
72 public void includeTimestamp(final KeyValue kv) {
73 includeTimestamp(kv.getTimestamp());
74 if (kv.isDeleteColumnOrFamily()) {
75 includeTimestamp(0);
76 }
77 }
78
79 /**
80 * Update the current TimestampRange to include the timestamp from Key.
81 * If the Key is of type DeleteColumn or DeleteFamily, it includes the
82 * entire time range from 0 to timestamp of the key.
83 * @param key
84 */
85 public void includeTimestamp(final byte[] key) {
86 includeTimestamp(Bytes.toLong(key,key.length-KeyValue.TIMESTAMP_TYPE_SIZE));
87 int type = key[key.length - 1];
88 if (type == Type.DeleteColumn.getCode() ||
89 type == Type.DeleteFamily.getCode()) {
90 includeTimestamp(0);
91 }
92 }
93
94 /**
95 * If required, update the current TimestampRange to include timestamp
96 * @param timestamp the timestamp value to include
97 */
98 private void includeTimestamp(final long timestamp) {
99 if (maximumTimestamp == -1) {
100 minimumTimestamp = timestamp;
101 maximumTimestamp = timestamp;
102 }
103 else if (minimumTimestamp > timestamp) {
104 minimumTimestamp = timestamp;
105 }
106 else if (maximumTimestamp < timestamp) {
107 maximumTimestamp = timestamp;
108 }
109 return;
110 }
111
112 /**
113 * Check if the range has any overlap with TimeRange
114 * @param tr TimeRange
115 * @return True if there is overlap, false otherwise
116 */
117 public boolean includesTimeRange(final TimeRange tr) {
118 return (this.minimumTimestamp < tr.getMax() &&
119 this.maximumTimestamp >= tr.getMin());
120 }
121
122 /**
123 * @return the minimumTimestamp
124 */
125 public long getMinimumTimestamp() {
126 return minimumTimestamp;
127 }
128
129 /**
130 * @return the maximumTimestamp
131 */
132 public long getMaximumTimestamp() {
133 return maximumTimestamp;
134 }
135
136 public void write(final DataOutput out) throws IOException {
137 out.writeLong(minimumTimestamp);
138 out.writeLong(maximumTimestamp);
139 }
140
141 public void readFields(final DataInput in) throws IOException {
142 this.minimumTimestamp = in.readLong();
143 this.maximumTimestamp = in.readLong();
144 }
145
146 }
147