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.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  
34    /**
35     * Construct a new BlockCacheKey
36     * @param hfileName The name of the HFile this block belongs to.
37     * @param offset Offset of the block into the file
38     */
39    public BlockCacheKey(String hfileName, long offset) {
40      this.hfileName = hfileName;
41      this.offset = offset;
42    }
43  
44    @Override
45    public int hashCode() {
46      return hfileName.hashCode() * 127 + (int) (offset ^ (offset >>> 32));
47    }
48  
49    @Override
50    public boolean equals(Object o) {
51      if (o instanceof BlockCacheKey) {
52        BlockCacheKey k = (BlockCacheKey) o;
53        return offset == k.offset
54            && (hfileName == null ? k.hfileName == null : hfileName
55                .equals(k.hfileName));
56      } else {
57        return false;
58      }
59    }
60  
61    @Override
62    public String toString() {
63      return String.format("%s_%d", hfileName, offset);
64    }
65  
66    public static final long FIXED_OVERHEAD = ClassSize.align(ClassSize.OBJECT +
67            ClassSize.REFERENCE + // this.hfileName
68            Bytes.SIZEOF_LONG);    // this.offset
69  
70    /**
71     * Strings have two bytes per character due to default Java Unicode encoding
72     * (hence length times 2).
73     */
74    @Override
75    public long heapSize() {
76      return ClassSize.align(FIXED_OVERHEAD + ClassSize.STRING +
77              2 * hfileName.length());
78    }
79  
80    // can't avoid this unfortunately
81    /**
82     * @return The hfileName portion of this cache key
83     */
84    public String getHfileName() {
85      return hfileName;
86    }
87  
88    public long getOffset() {
89      return offset;
90    }
91  }