001/**
002 * Copyright The Apache Software Foundation
003 *
004 * Licensed to the Apache Software Foundation (ASF) under one or more
005 * contributor license agreements. See the NOTICE file distributed with this
006 * work for additional information regarding copyright ownership. The ASF
007 * licenses this file to you under the Apache License, Version 2.0 (the
008 * "License"); you may not use this file except in compliance with the License.
009 * 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, WITHOUT
015 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
016 * License for the specific language governing permissions and limitations
017 * under the License.
018 */
019package org.apache.hadoop.hbase.io.hfile.bucket;
020
021import java.io.IOException;
022import java.nio.ByteBuffer;
023
024import org.apache.yetus.audience.InterfaceAudience;
025import org.apache.hadoop.hbase.io.hfile.Cacheable;
026import org.apache.hadoop.hbase.io.hfile.CacheableDeserializer;
027import org.apache.hadoop.hbase.nio.ByteBuff;
028
029/**
030 * A class implementing IOEngine interface supports data services for
031 * {@link BucketCache}.
032 */
033@InterfaceAudience.Private
034public interface IOEngine {
035  /**
036   * @return true if persistent storage is supported for the cache when shutdown
037   */
038  boolean isPersistent();
039
040  /**
041   * IOEngine uses shared memory means, when reading Cacheable from it, those refers to the same
042   * memory area as used by the Engine for caching it.
043   * @return true when IOEngine using shared memory.
044   */
045  default boolean usesSharedMemory() {
046    return false;
047  }
048
049  /**
050   * Transfers data from IOEngine to a Cacheable object.
051   * @param length How many bytes to be read from the offset
052   * @param offset The offset in the IO engine where the first byte to be read
053   * @param deserializer The deserializer to be used to make a Cacheable from the data.
054   * @return Cacheable
055   * @throws IOException
056   * @throws RuntimeException when the length of the ByteBuff read is less than 'len'
057   */
058  Cacheable read(long offset, int length, CacheableDeserializer<Cacheable> deserializer)
059      throws IOException;
060
061  /**
062   * Transfers data from the given byte buffer to IOEngine
063   * @param srcBuffer the given byte buffer from which bytes are to be read
064   * @param offset The offset in the IO engine where the first byte to be
065   *          written
066   * @throws IOException
067   */
068  void write(ByteBuffer srcBuffer, long offset) throws IOException;
069
070  /**
071   * Transfers the data from the given MultiByteBuffer to IOEngine
072   * @param srcBuffer the given MultiBytebufffers from which bytes are to be read
073   * @param offset the offset in the IO engine where the first byte to be written
074   * @throws IOException
075   */
076  void write(ByteBuff srcBuffer, long offset) throws IOException;
077
078  /**
079   * Sync the data to IOEngine after writing
080   * @throws IOException
081   */
082  void sync() throws IOException;
083
084  /**
085   * Shutdown the IOEngine
086   */
087  void shutdown();
088}