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.CellUtil;
27  import org.apache.hadoop.hbase.KeyValue;
28  import org.apache.hadoop.hbase.KeyValue.Type;
29  import org.apache.hadoop.hbase.io.TimeRange;
30  import org.apache.hadoop.hbase.util.Bytes;
31  import org.apache.hadoop.io.Writable;
32  
33  /**
34   * Stores the minimum and maximum timestamp values (both are inclusive).
35   * Can be used to find if any given time range overlaps with its time range
36   * MemStores use this class to track its minimum and maximum timestamps.
37   * When writing StoreFiles, this information is stored in meta blocks and used
38   * at read time to match against the required TimeRange.
39   */
40  @InterfaceAudience.Private
41  public class TimeRangeTracker implements Writable {
42  
43    long minimumTimestamp = -1;
44    long maximumTimestamp = -1;
45  
46    /**
47     * Default constructor.
48     * Initializes TimeRange to be null
49     */
50    public TimeRangeTracker() {
51  
52    }
53  
54    /**
55     * Copy Constructor
56     * @param trt source TimeRangeTracker
57     */
58    public TimeRangeTracker(final TimeRangeTracker trt) {
59      this.minimumTimestamp = trt.getMinimumTimestamp();
60      this.maximumTimestamp = trt.getMaximumTimestamp();
61    }
62  
63    public TimeRangeTracker(long minimumTimestamp, long maximumTimestamp) {
64      this.minimumTimestamp = minimumTimestamp;
65      this.maximumTimestamp = maximumTimestamp;
66    }
67  
68    /**
69     * Update the current TimestampRange to include the timestamp from KeyValue
70     * If the Key is of type DeleteColumn or DeleteFamily, it includes the
71     * entire time range from 0 to timestamp of the key.
72     * @param kv the KeyValue to include
73     */
74    public void includeTimestamp(final KeyValue kv) {
75      includeTimestamp(kv.getTimestamp());
76      if (CellUtil.isDeleteColumnOrFamily(kv)) {
77        includeTimestamp(0);
78      }
79    }
80  
81    /**
82     * Update the current TimestampRange to include the timestamp from Key.
83     * If the Key is of type DeleteColumn or DeleteFamily, it includes the
84     * entire time range from 0 to timestamp of the key.
85     * @param key
86     */
87    public void includeTimestamp(final byte[] key) {
88      includeTimestamp(Bytes.toLong(key,key.length-KeyValue.TIMESTAMP_TYPE_SIZE));
89      int type = key[key.length - 1];
90      if (type == Type.DeleteColumn.getCode() ||
91          type == Type.DeleteFamily.getCode()) {
92        includeTimestamp(0);
93      }
94    }
95  
96    /**
97     * If required, update the current TimestampRange to include timestamp
98     * @param timestamp the timestamp value to include
99     */
100   private synchronized void includeTimestamp(final long timestamp) {
101     if (maximumTimestamp == -1) {
102       minimumTimestamp = timestamp;
103       maximumTimestamp = timestamp;
104     }
105     else if (minimumTimestamp > timestamp) {
106       minimumTimestamp = timestamp;
107     }
108     else if (maximumTimestamp < timestamp) {
109       maximumTimestamp = timestamp;
110     }
111     return;
112   }
113 
114   /**
115    * Check if the range has any overlap with TimeRange
116    * @param tr TimeRange
117    * @return True if there is overlap, false otherwise
118    */
119   public synchronized boolean includesTimeRange(final TimeRange tr) {
120     return (this.minimumTimestamp < tr.getMax() &&
121         this.maximumTimestamp >= tr.getMin());
122   }
123 
124   /**
125    * @return the minimumTimestamp
126    */
127   public synchronized long getMinimumTimestamp() {
128     return minimumTimestamp;
129   }
130 
131   /**
132    * @return the maximumTimestamp
133    */
134   public synchronized long getMaximumTimestamp() {
135     return maximumTimestamp;
136   }
137 
138   public synchronized void write(final DataOutput out) throws IOException {
139     out.writeLong(minimumTimestamp);
140     out.writeLong(maximumTimestamp);
141   }
142 
143   public synchronized void readFields(final DataInput in) throws IOException {
144     this.minimumTimestamp = in.readLong();
145     this.maximumTimestamp = in.readLong();
146   }
147 
148   @Override
149   public synchronized String toString() {
150     return "[" + minimumTimestamp + "," + maximumTimestamp + "]";
151   }
152 }