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.nio.ByteBuff;
027
028/**
029 * A class implementing IOEngine interface supports data services for
030 * {@link BucketCache}.
031 */
032@InterfaceAudience.Private
033public interface IOEngine {
034  /**
035   * @return true if persistent storage is supported for the cache when shutdown
036   */
037  boolean isPersistent();
038
039  /**
040   * IOEngine uses shared memory means, when reading Cacheable from it, those refers to the same
041   * memory area as used by the Engine for caching it.
042   * @return true when IOEngine using shared memory.
043   */
044  default boolean usesSharedMemory() {
045    return false;
046  }
047
048  /**
049   * Transfers data from IOEngine to a Cacheable object.
050   * @param be maintains an (offset,len,refCnt) inside.
051   * @return Cacheable which will wrap the NIO ByteBuffers from IOEngine.
052   * @throws IOException when any IO error happen
053   * @throws IllegalArgumentException when the length of the ByteBuff read is less than 'len'
054   */
055  Cacheable read(BucketEntry be) throws IOException;
056
057  /**
058   * Transfers data from the given byte buffer to IOEngine
059   * @param srcBuffer the given byte buffer from which bytes are to be read
060   * @param offset The offset in the IO engine where the first byte to be
061   *          written
062   * @throws IOException
063   */
064  void write(ByteBuffer srcBuffer, long offset) throws IOException;
065
066  /**
067   * Transfers the data from the given MultiByteBuffer to IOEngine
068   * @param srcBuffer the given MultiBytebufffers from which bytes are to be read
069   * @param offset the offset in the IO engine where the first byte to be written
070   * @throws IOException
071   */
072  void write(ByteBuff srcBuffer, long offset) throws IOException;
073
074  /**
075   * Sync the data to IOEngine after writing
076   * @throws IOException
077   */
078  void sync() throws IOException;
079
080  /**
081   * Shutdown the IOEngine
082   */
083  void shutdown();
084}