View Javadoc

1   /**
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  package org.apache.hadoop.hbase.io.hfile;
19  
20  import org.apache.hadoop.classification.InterfaceAudience;
21  import org.apache.hadoop.hbase.io.HeapSize;
22  import org.apache.hadoop.hbase.io.encoding.DataBlockEncoding;
23  import org.apache.hadoop.hbase.util.Bytes;
24  import org.apache.hadoop.hbase.util.ClassSize;
25  
26  /**
27   * Cache Key for use with implementations of {@link BlockCache}
28   */
29  @InterfaceAudience.Private
30  public class BlockCacheKey implements HeapSize, java.io.Serializable {
31    private final String hfileName;
32    private final long offset;
33    private final DataBlockEncoding encoding;
34  
35    public BlockCacheKey(String file, long offset, DataBlockEncoding encoding,
36        BlockType blockType) {
37      this.hfileName = file;
38      this.offset = offset;
39      // We add encoding to the cache key only for data blocks. If the block type
40      // is unknown (this should never be the case in production), we just use
41      // the provided encoding, because it might be a data block.
42      this.encoding = (blockType == null || blockType.isData()) ? encoding :
43          DataBlockEncoding.NONE;
44    }
45  
46    /**
47     * Construct a new BlockCacheKey
48     * @param file The name of the HFile this block belongs to.
49     * @param offset Offset of the block into the file
50     */
51    public BlockCacheKey(String file, long offset) {
52      this(file, offset, DataBlockEncoding.NONE, null);
53    }
54  
55    @Override
56    public int hashCode() {
57      return hfileName.hashCode() * 127 + (int) (offset ^ (offset >>> 32)) +
58          encoding.ordinal() * 17;
59    }
60  
61    @Override
62    public boolean equals(Object o) {
63      if (o instanceof BlockCacheKey) {
64        BlockCacheKey k = (BlockCacheKey) o;
65        return offset == k.offset
66            && (hfileName == null ? k.hfileName == null : hfileName
67                .equals(k.hfileName));
68      } else {
69        return false;
70      }
71    }
72  
73    @Override
74    public String toString() {
75      return hfileName + "_" + offset
76          + (encoding == DataBlockEncoding.NONE ? "" : "_" + encoding);
77    }
78  
79    /**
80     * Strings have two bytes per character due to default Java Unicode encoding
81     * (hence length times 2).
82     */
83    @Override
84    public long heapSize() {
85      return ClassSize.align(ClassSize.OBJECT + 2 * hfileName.length() +
86          Bytes.SIZEOF_LONG + 2 * ClassSize.REFERENCE);
87    }
88  
89    // can't avoid this unfortunately
90    /**
91     * @return The hfileName portion of this cache key
92     */
93    public String getHfileName() {
94      return hfileName;
95    }
96  
97    public DataBlockEncoding getDataBlockEncoding() {
98      return encoding;
99    }
100 }