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   * @param includeNextBlockMetadata Whether to include nextBlockMetadata in the Cache block.
050   */
051  void serialize(ByteBuffer destination, boolean includeNextBlockMetadata);
052
053  /**
054   * Returns CacheableDeserializer instance which reconstructs original object from ByteBuffer.
055   *
056   * @return CacheableDeserialzer instance.
057   */
058  CacheableDeserializer<Cacheable> getDeserializer();
059
060  /**
061   * @return the block type of this cached HFile block
062   */
063  BlockType getBlockType();
064
065  /**
066   * @return the {@code MemoryType} of this Cacheable
067   */
068  MemoryType getMemoryType();
069
070  /**
071   * SHARED means when this Cacheable is read back from cache it refers to the same memory area as
072   * used by the cache for caching it.
073   * EXCLUSIVE means when this Cacheable is read back from cache, the data was copied to an
074   * exclusive memory area of this Cacheable.
075   */
076  public static enum MemoryType {
077    SHARED, EXCLUSIVE;
078  }
079}