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.ClassSize;
023
024/**
025 * Cache Key for use with implementations of {@link BlockCache}
026 */
027@InterfaceAudience.Private
028public class BlockCacheKey implements HeapSize, java.io.Serializable {
029  private static final long serialVersionUID = -5199992013113130534L;
030  private final String hfileName;
031  private final long offset;
032  private final BlockType blockType;
033  private final boolean isPrimaryReplicaBlock;
034
035  /**
036   * Construct a new BlockCacheKey
037   * @param hfileName The name of the HFile this block belongs to.
038   * @param offset Offset of the block into the file
039   */
040  public BlockCacheKey(String hfileName, long offset) {
041    this(hfileName, offset, true, BlockType.DATA);
042  }
043
044  public BlockCacheKey(String hfileName, long offset, boolean isPrimaryReplica,
045      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.estimateBase(BlockCacheKey.class, false);
075
076  /**
077   * Strings have two bytes per character due to default Java Unicode encoding
078   * (hence length times 2).
079   */
080  @Override
081  public long heapSize() {
082    return ClassSize.align(FIXED_OVERHEAD + ClassSize.STRING +
083            2 * hfileName.length());
084  }
085
086  // can't avoid this unfortunately
087  /**
088   * @return The hfileName portion of this cache key
089   */
090  public String getHfileName() {
091    return hfileName;
092  }
093
094  public boolean isPrimary() {
095    return isPrimaryReplicaBlock;
096  }
097
098  public long getOffset() {
099    return offset;
100  }
101
102  public BlockType getBlockType() {
103    return blockType;
104  }
105}