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.classification.InterfaceAudience;
21  import org.apache.hadoop.hbase.io.HeapSize;
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  @InterfaceAudience.Private
29  public class BlockCacheKey implements HeapSize, java.io.Serializable {
30    private static final long serialVersionUID = -5199992013113130534L;
31    private final String hfileName;
32    private final long offset;
33    private final BlockType blockType;
34    private final boolean isPrimaryReplicaBlock;
35  
36    /**
37     * Construct a new BlockCacheKey
38     * @param hfileName The name of the HFile this block belongs to.
39     * @param offset Offset of the block into the file
40     */
41    public BlockCacheKey(String hfileName, long offset) {
42      this(hfileName, offset, true, BlockType.DATA);
43    }
44  
45    public BlockCacheKey(String hfileName, long offset, boolean isPrimaryReplica, BlockType blockType) {
46      this.isPrimaryReplicaBlock = isPrimaryReplica;
47      this.hfileName = hfileName;
48      this.offset = offset;
49      this.blockType = blockType;
50    }
51  
52    @Override
53    public int hashCode() {
54      return hfileName.hashCode() * 127 + (int) (offset ^ (offset >>> 32));
55    }
56  
57    @Override
58    public boolean equals(Object o) {
59      if (o instanceof BlockCacheKey) {
60        BlockCacheKey k = (BlockCacheKey) o;
61        return offset == k.offset
62            && (hfileName == null ? k.hfileName == null : hfileName
63                .equals(k.hfileName));
64      } else {
65        return false;
66      }
67    }
68  
69    @Override
70    public String toString() {
71      return String.format("%s_%d", hfileName, offset);
72    }
73  
74    public static final long FIXED_OVERHEAD = ClassSize.align(
75        ClassSize.OBJECT +
76        Bytes.SIZEOF_BOOLEAN +
77        ClassSize.REFERENCE + // this.hfileName
78        ClassSize.REFERENCE + // this.blockType
79        Bytes.SIZEOF_LONG);    // this.offset
80  
81    /**
82     * Strings have two bytes per character due to default Java Unicode encoding
83     * (hence length times 2).
84     */
85    @Override
86    public long heapSize() {
87      return ClassSize.align(FIXED_OVERHEAD + ClassSize.STRING +
88              2 * hfileName.length());
89    }
90  
91    // can't avoid this unfortunately
92    /**
93     * @return The hfileName portion of this cache key
94     */
95    public String getHfileName() {
96      return hfileName;
97    }
98  
99    public boolean isPrimary() {
100     return isPrimaryReplicaBlock;
101   }
102 
103   public long getOffset() {
104     return offset;
105   }
106 
107   public BlockType getBlockType() {
108     return blockType;
109   }
110 }