001/** 002 * 003 * Licensed to the Apache Software Foundation (ASF) under one 004 * or more contributor license agreements. See the NOTICE file 005 * distributed with this work for additional information 006 * regarding copyright ownership. The ASF licenses this file 007 * to you under the Apache License, Version 2.0 (the 008 * "License"); you may not use this file except in compliance 009 * with the License. You may obtain a copy of the License at 010 * 011 * http://www.apache.org/licenses/LICENSE-2.0 012 * 013 * Unless required by applicable law or agreed to in writing, software 014 * distributed under the License is distributed on an "AS IS" BASIS, 015 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 016 * See the License for the specific language governing permissions and 017 * limitations under the License. 018 */ 019 020package org.apache.hadoop.hbase.io.hfile; 021 022import java.nio.ByteBuffer; 023 024import org.apache.yetus.audience.InterfaceAudience; 025import org.apache.hadoop.hbase.io.HeapSize; 026 027/** 028 * Cacheable is an interface that allows for an object to be cached. If using an 029 * on heap cache, just use heapsize. If using an off heap cache, Cacheable 030 * provides methods for serialization of the object. 031 * 032 * Some objects cannot be moved off heap, those objects will return a 033 * getSerializedLength() of 0. 034 * 035 */ 036@InterfaceAudience.Private 037public interface Cacheable extends HeapSize { 038 /** 039 * Returns the length of the ByteBuffer required to serialized the object. If the 040 * object cannot be serialized, it should return 0. 041 * 042 * @return int length in bytes of the serialized form or 0 if the object cannot be cached. 043 */ 044 int getSerializedLength(); 045 046 /** 047 * Serializes its data into destination. 048 * @param destination Where to serialize to 049 */ 050 void serialize(ByteBuffer destination); 051 052 /** 053 * Returns CacheableDeserializer instance which reconstructs original object from ByteBuffer. 054 * 055 * @return CacheableDeserialzer instance. 056 */ 057 CacheableDeserializer<Cacheable> getDeserializer(); 058 059 /** 060 * @return the block type of this cached HFile block 061 */ 062 BlockType getBlockType(); 063 064 /** 065 * @return the {@code MemoryType} of this Cacheable 066 */ 067 MemoryType getMemoryType(); 068 069 /** 070 * SHARED means when this Cacheable is read back from cache it refers to the same memory area as 071 * used by the cache for caching it. 072 * EXCLUSIVE means when this Cacheable is read back from cache, the data was copied to an 073 * exclusive memory area of this Cacheable. 074 */ 075 public static enum MemoryType { 076 SHARED, EXCLUSIVE; 077 } 078}