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.fs.Path; 021import org.apache.hadoop.hbase.io.HeapSize; 022import org.apache.hadoop.hbase.util.ClassSize; 023import org.apache.yetus.audience.InterfaceAudience; 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 BlockType blockType; 034 private final boolean isPrimaryReplicaBlock; 035 private Path filePath; 036 037 /** 038 * Construct a new BlockCacheKey 039 * @param hfileName The name of the HFile this block belongs to. 040 * @param offset Offset of the block into the file 041 */ 042 public BlockCacheKey(String hfileName, long offset) { 043 this(hfileName, offset, true, BlockType.DATA); 044 } 045 046 public BlockCacheKey(String hfileName, long offset, boolean isPrimaryReplica, 047 BlockType blockType) { 048 this.isPrimaryReplicaBlock = isPrimaryReplica; 049 this.hfileName = hfileName; 050 this.offset = offset; 051 this.blockType = blockType; 052 } 053 054 public BlockCacheKey(Path hfilePath, long offset, boolean isPrimaryReplica, BlockType blockType) { 055 this.filePath = hfilePath; 056 this.isPrimaryReplicaBlock = isPrimaryReplica; 057 this.hfileName = hfilePath.getName(); 058 this.offset = offset; 059 this.blockType = blockType; 060 } 061 062 @Override 063 public int hashCode() { 064 return hfileName.hashCode() * 127 + (int) (offset ^ (offset >>> 32)); 065 } 066 067 @Override 068 public boolean equals(Object o) { 069 if (o instanceof BlockCacheKey) { 070 BlockCacheKey k = (BlockCacheKey) o; 071 return offset == k.offset 072 && (hfileName == null ? k.hfileName == null : hfileName.equals(k.hfileName)); 073 } else { 074 return false; 075 } 076 } 077 078 @Override 079 public String toString() { 080 return this.hfileName + '_' + this.offset; 081 } 082 083 public static final long FIXED_OVERHEAD = ClassSize.estimateBase(BlockCacheKey.class, false); 084 085 /** 086 * Strings have two bytes per character due to default Java Unicode encoding (hence length times 087 * 2). 088 */ 089 @Override 090 public long heapSize() { 091 return ClassSize.align(FIXED_OVERHEAD + ClassSize.STRING + 2 * hfileName.length()); 092 } 093 094 // can't avoid this unfortunately 095 /** Returns The hfileName portion of this cache key */ 096 public String getHfileName() { 097 return hfileName; 098 } 099 100 public boolean isPrimary() { 101 return isPrimaryReplicaBlock; 102 } 103 104 public long getOffset() { 105 return offset; 106 } 107 108 public BlockType getBlockType() { 109 return blockType; 110 } 111 112 public void setBlockType(BlockType blockType) { 113 this.blockType = blockType; 114 } 115 116 public Path getFilePath() { 117 return filePath; 118 } 119 120}