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.hadoop.hbase.io.HeapSize;
021import org.apache.hadoop.hbase.util.ClassSize;
022import org.apache.yetus.audience.InterfaceAudience;
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.equals(k.hfileName));
063    } else {
064      return false;
065    }
066  }
067
068  @Override
069  public String toString() {
070    return this.hfileName + '_' + this.offset;
071  }
072
073  public static final long FIXED_OVERHEAD = ClassSize.estimateBase(BlockCacheKey.class, false);
074
075  /**
076   * Strings have two bytes per character due to default Java Unicode encoding (hence length times
077   * 2).
078   */
079  @Override
080  public long heapSize() {
081    return ClassSize.align(FIXED_OVERHEAD + ClassSize.STRING + 2 * hfileName.length());
082  }
083
084  // can't avoid this unfortunately
085  /** Returns The hfileName portion of this cache key */
086  public String getHfileName() {
087    return hfileName;
088  }
089
090  public boolean isPrimary() {
091    return isPrimaryReplicaBlock;
092  }
093
094  public long getOffset() {
095    return offset;
096  }
097
098  public BlockType getBlockType() {
099    return blockType;
100  }
101}