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