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.hadoop.hbase.io.HeapSize;
025import org.apache.hadoop.hbase.nio.HBaseReferenceCounted;
026import org.apache.yetus.audience.InterfaceAudience;
027
028/**
029 * Cacheable is an interface that allows for an object to be cached. If using an
030 * on heap cache, just use heapsize. If using an off heap cache, Cacheable
031 * provides methods for serialization of the object.
032 *
033 * Some objects cannot be moved off heap, those objects will return a
034 * getSerializedLength() of 0.
035 *
036 */
037@InterfaceAudience.Private
038public interface Cacheable extends HeapSize, HBaseReferenceCounted {
039  /**
040   * Returns the length of the ByteBuffer required to serialized the object. If the
041   * object cannot be serialized, it should return 0.
042   *
043   * @return int length in bytes of the serialized form or 0 if the object cannot be cached.
044   */
045  int getSerializedLength();
046
047  /**
048   * Serializes its data into destination.
049   * @param destination Where to serialize to
050   * @param includeNextBlockMetadata Whether to include nextBlockMetadata in the Cache block.
051   */
052  void serialize(ByteBuffer destination, boolean includeNextBlockMetadata);
053
054  /**
055   * Returns CacheableDeserializer instance which reconstructs original object from ByteBuffer.
056   *
057   * @return CacheableDeserialzer instance.
058   */
059  CacheableDeserializer<Cacheable> getDeserializer();
060
061  /**
062   * @return the block type of this cached HFile block
063   */
064  BlockType getBlockType();
065
066  /******************************* ReferenceCounted Interfaces ***********************************/
067
068  /**
069   * Increase its reference count, and only when no reference we can free the object's memory.
070   */
071  default Cacheable retain() {
072    return this;
073  }
074
075  /**
076   * Reference count of this Cacheable.
077   */
078  default int refCnt() {
079    return 0;
080  }
081
082  /**
083   * Decrease its reference count, and if no reference then free the memory of this object, its
084   * backend is usually a {@link org.apache.hadoop.hbase.nio.ByteBuff}, and we will put its NIO
085   * ByteBuffers back to {@link org.apache.hadoop.hbase.io.ByteBuffAllocator}
086   */
087  default boolean release() {
088    return false;
089  }
090}