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     * @throws IOException
63     */
64    @Override
65    public void read(ByteBuffer dstBuffer, long offset) throws IOException {
66      assert dstBuffer.hasArray();
67      bufferArray.getMultiple(offset, dstBuffer.remaining(), dstBuffer.array(),
68          dstBuffer.arrayOffset());
69    }
70  
71    /**
72     * Transfers data from the given byte buffer to the buffer array
73     * @param srcBuffer the given byte buffer from which bytes are to be read
74     * @param offset The offset in the ByteBufferArray of the first byte to be
75     *          written
76     * @throws IOException
77     */
78    @Override
79    public void write(ByteBuffer srcBuffer, long offset) throws IOException {
80      assert srcBuffer.hasArray();
81      bufferArray.putMultiple(offset, srcBuffer.remaining(), srcBuffer.array(),
82          srcBuffer.arrayOffset());
83    }
84  
85    /**
86     * No operation for the sync in the memory IO engine
87     */
88    @Override
89    public void sync() {
90  
91    }
92  
93    /**
94     * No operation for the shutdown in the memory IO engine
95     */
96    @Override
97    public void shutdown() {
98  
99    }
100 }