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