/** * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file * distributed with this work for additional information * regarding copyright ownership. The ASF licenses this file * to you under the Apache License, Version 2.0 (the * "License"); you may not use this file except in compliance * with the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package org.apache.hadoop.hbase.io.hfile; import org.apache.hadoop.hbase.io.HeapSize; import org.apache.hadoop.hbase.util.Bytes; /** * Cache Key for use with implementations of {@link BlockCache} */ public class BlockCacheKey implements HeapSize { private String hfileName; private long offset; /** * Construct a new BlockCacheKey * @param file The name of the HFile this block belongs to. * @param offset Offset of the block into the file */ public BlockCacheKey(String file, long offset) { this.hfileName = file; this.offset = offset; } @Override public int hashCode() { return hfileName.hashCode() * 127 + (int) (offset ^ (offset >>> 32)); } @Override public boolean equals(Object o) { if (o instanceof BlockCacheKey) { BlockCacheKey k = (BlockCacheKey) o; return offset == k.offset && (hfileName == null ? k.hfileName == null : hfileName .equals(k.hfileName)); } else { return false; } } @Override public String toString() { return hfileName + "_" + offset; } // Strings have two bytes per character due to default // Java unicode encoding (hence the times 2). @Override public long heapSize() { return 2 * hfileName.length() + Bytes.SIZEOF_LONG; } // can't avoid this unfortunately /** * @return The hfileName portion of this cache key */ public String getHfileName() { return hfileName; } }