View Javadoc

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