001/**
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *     http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018package org.apache.hadoop.hbase.io.hfile;
019
020import org.apache.yetus.audience.InterfaceAudience;
021import org.apache.hadoop.hbase.io.HeapSize;
022import org.apache.hadoop.hbase.util.Bytes;
023import org.apache.hadoop.hbase.util.ClassSize;
024
025/**
026 * Cache Key for use with implementations of {@link BlockCache}
027 */
028@InterfaceAudience.Private
029public class BlockCacheKey implements HeapSize, java.io.Serializable {
030  private static final long serialVersionUID = -5199992013113130534L;
031  private final String hfileName;
032  private final long offset;
033  private final BlockType blockType;
034  private final boolean isPrimaryReplicaBlock;
035
036  /**
037   * Construct a new BlockCacheKey
038   * @param hfileName The name of the HFile this block belongs to.
039   * @param offset Offset of the block into the file
040   */
041  public BlockCacheKey(String hfileName, long offset) {
042    this(hfileName, offset, true, BlockType.DATA);
043  }
044
045  public BlockCacheKey(String hfileName, long offset, boolean isPrimaryReplica, BlockType blockType) {
046    this.isPrimaryReplicaBlock = isPrimaryReplica;
047    this.hfileName = hfileName;
048    this.offset = offset;
049    this.blockType = blockType;
050  }
051
052  @Override
053  public int hashCode() {
054    return hfileName.hashCode() * 127 + (int) (offset ^ (offset >>> 32));
055  }
056
057  @Override
058  public boolean equals(Object o) {
059    if (o instanceof BlockCacheKey) {
060      BlockCacheKey k = (BlockCacheKey) o;
061      return offset == k.offset
062          && (hfileName == null ? k.hfileName == null : hfileName
063              .equals(k.hfileName));
064    } else {
065      return false;
066    }
067  }
068
069  @Override
070  public String toString() {
071    return this.hfileName + '_' + this.offset;
072  }
073
074  public static final long FIXED_OVERHEAD = ClassSize.align(
075      ClassSize.OBJECT +
076      Bytes.SIZEOF_BOOLEAN +
077      ClassSize.REFERENCE + // this.hfileName
078      ClassSize.REFERENCE + // this.blockType
079      Bytes.SIZEOF_LONG);    // this.offset
080
081  /**
082   * Strings have two bytes per character due to default Java Unicode encoding
083   * (hence length times 2).
084   */
085  @Override
086  public long heapSize() {
087    return ClassSize.align(FIXED_OVERHEAD + ClassSize.STRING +
088            2 * hfileName.length());
089  }
090
091  // can't avoid this unfortunately
092  /**
093   * @return The hfileName portion of this cache key
094   */
095  public String getHfileName() {
096    return hfileName;
097  }
098
099  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}