View Javadoc

1   /**
2    * Copyright The Apache Software Foundation
3    *
4    * Licensed to the Apache Software Foundation (ASF) under one or more
5    * contributor license agreements. See the NOTICE file distributed with this
6    * work for additional information regarding copyright ownership. The ASF
7    * licenses this file to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance with the License.
9    * You may obtain a copy of the License at
10   *
11   * http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing, software
14   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
16   * License for the specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.hadoop.hbase.io.hfile.bucket;
20  
21  import java.io.IOException;
22  import java.nio.ByteBuffer;
23  
24  import org.apache.hadoop.classification.InterfaceAudience;
25  import org.apache.hadoop.hbase.util.ByteBufferArray;
26  
27  /**
28   * IO engine that stores data on the memory using an array of ByteBuffers
29   * {@link ByteBufferArray}
30   */
31  @InterfaceAudience.Private
32  public class ByteBufferIOEngine implements IOEngine {
33  
34    private ByteBufferArray bufferArray;
35  
36    /**
37     * Construct the ByteBufferIOEngine with the given capacity
38     * @param capacity
39     * @param direct true if allocate direct buffer
40     * @throws IOException
41     */
42    public ByteBufferIOEngine(long capacity, boolean direct)
43        throws IOException {
44      bufferArray = new ByteBufferArray(capacity, direct);
45    }
46  
47    /**
48     * Memory IO engine is always unable to support persistent storage for the
49     * cache
50     * @return false
51     */
52    @Override
53    public boolean isPersistent() {
54      return false;
55    }
56  
57    /**
58     * Transfers data from the buffer array to the given byte buffer
59     * @param dstBuffer the given byte buffer into which bytes are to be written
60     * @param offset The offset in the ByteBufferArray of the first byte to be
61     *          read
62     * @return number of bytes read
63     * @throws IOException
64     */
65    @Override
66    public int read(ByteBuffer dstBuffer, long offset) throws IOException {
67      assert dstBuffer.hasArray();
68      return bufferArray.getMultiple(offset, dstBuffer.remaining(), dstBuffer.array(),
69          dstBuffer.arrayOffset());
70    }
71  
72    /**
73     * Transfers data from the given byte buffer to the buffer array
74     * @param srcBuffer the given byte buffer from which bytes are to be read
75     * @param offset The offset in the ByteBufferArray of the first byte to be
76     *          written
77     * @throws IOException
78     */
79    @Override
80    public void write(ByteBuffer srcBuffer, long offset) throws IOException {
81      assert srcBuffer.hasArray();
82      bufferArray.putMultiple(offset, srcBuffer.remaining(), srcBuffer.array(),
83          srcBuffer.arrayOffset());
84    }
85  
86    /**
87     * No operation for the sync in the memory IO engine
88     */
89    @Override
90    public void sync() {
91  
92    }
93  
94    /**
95     * No operation for the shutdown in the memory IO engine
96     */
97    @Override
98    public void shutdown() {
99  
100   }
101 }