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