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.hbase.io.HeapSize;
21  import org.apache.hadoop.hbase.io.encoding.DataBlockEncoding;
22  import org.apache.hadoop.hbase.util.Bytes;
23  import org.apache.hadoop.hbase.util.ClassSize;
24  
25  /**
26   * Cache Key for use with implementations of {@link BlockCache}
27   */
28  public class BlockCacheKey implements HeapSize {
29    private final String hfileName;
30    private final long offset;
31    private final DataBlockEncoding encoding;
32  
33    public BlockCacheKey(String file, long offset, DataBlockEncoding encoding,
34        BlockType blockType) {
35      this.hfileName = file;
36      this.offset = offset;
37      // We add encoding to the cache key only for data blocks. If the block type
38      // is unknown (this should never be the case in production), we just use
39      // the provided encoding, because it might be a data block.
40      this.encoding = (blockType == null || blockType.isData()) ? encoding :
41          DataBlockEncoding.NONE;
42    }
43  
44    /**
45     * Construct a new BlockCacheKey
46     * @param file The name of the HFile this block belongs to.
47     * @param offset Offset of the block into the file
48     */
49    public BlockCacheKey(String file, long offset) {
50      this(file, offset, DataBlockEncoding.NONE, null);
51    }
52  
53    @Override
54    public int hashCode() {
55      return hfileName.hashCode() * 127 + (int) (offset ^ (offset >>> 32)) +
56          encoding.ordinal() * 17;
57    }
58  
59    @Override
60    public boolean equals(Object o) {
61      if (o instanceof BlockCacheKey) {
62        BlockCacheKey k = (BlockCacheKey) o;
63        return offset == k.offset
64            && (hfileName == null ? k.hfileName == null : hfileName
65                .equals(k.hfileName));
66      } else {
67        return false;
68      }
69    }
70  
71    @Override
72    public String toString() {
73      return hfileName + "_" + offset
74          + (encoding == DataBlockEncoding.NONE ? "" : "_" + encoding);
75    }
76  
77    /**
78     * Strings have two bytes per character due to default Java Unicode encoding
79     * (hence length times 2).
80     */
81    @Override
82    public long heapSize() {
83      return ClassSize.align(ClassSize.OBJECT + 2 * hfileName.length() +
84          Bytes.SIZEOF_LONG + 2 * ClassSize.REFERENCE);
85    }
86  
87    // can't avoid this unfortunately
88    /**
89     * @return The hfileName portion of this cache key
90     */
91    public String getHfileName() {
92      return hfileName;
93    }
94  
95    public DataBlockEncoding getDataBlockEncoding() {
96      return encoding;
97    }
98  }