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.hbase.classification.InterfaceAudience;
25  import org.apache.hadoop.hbase.util.ByteBufferArray;
26  
27  /**
28   * IO engine that stores data in memory using an array of ByteBuffers
29   * {@link ByteBufferArray}
30   */
31  @InterfaceAudience.Private
32  public class ByteBufferIOEngine implements IOEngine {
33    private ByteBufferArray bufferArray;
34    private final long capacity;
35    private final boolean direct;
36  
37    /**
38     * Construct the ByteBufferIOEngine with the given capacity
39     * @param capacity
40     * @param direct true if allocate direct buffer
41     * @throws IOException
42     */
43    public ByteBufferIOEngine(long capacity, boolean direct)
44        throws IOException {
45      this.capacity = capacity;
46      this.direct = direct;
47      bufferArray = new ByteBufferArray(capacity, direct);
48    }
49  
50    @Override
51    public String toString() {
52      return "ioengine=" + this.getClass().getSimpleName() + ", capacity=" +
53        String.format("%,d", this.capacity) + ", direct=" + this.direct;
54    }
55  
56    /**
57     * Memory IO engine is always unable to support persistent storage for the
58     * cache
59     * @return false
60     */
61    @Override
62    public boolean isPersistent() {
63      return false;
64    }
65  
66    /**
67     * Transfers data from the buffer array to the given byte buffer
68     * @param dstBuffer the given byte buffer into which bytes are to be written
69     * @param offset The offset in the ByteBufferArray of the first byte to be
70     *          read
71     * @return number of bytes read
72     * @throws IOException
73     */
74    @Override
75    public int read(ByteBuffer dstBuffer, long offset) throws IOException {
76      assert dstBuffer.hasArray();
77      return bufferArray.getMultiple(offset, dstBuffer.remaining(), dstBuffer.array(),
78          dstBuffer.arrayOffset());
79    }
80  
81    /**
82     * Transfers data from the given byte buffer to the buffer array
83     * @param srcBuffer the given byte buffer from which bytes are to be read
84     * @param offset The offset in the ByteBufferArray of the first byte to be
85     *          written
86     * @throws IOException
87     */
88    @Override
89    public void write(ByteBuffer srcBuffer, long offset) throws IOException {
90      assert srcBuffer.hasArray();
91      bufferArray.putMultiple(offset, srcBuffer.remaining(), srcBuffer.array(),
92          srcBuffer.arrayOffset());
93    }
94  
95    /**
96     * No operation for the sync in the memory IO engine
97     */
98    @Override
99    public void sync() {
100     // Nothing to do.
101   }
102 
103   /**
104    * No operation for the shutdown in the memory IO engine
105    */
106   @Override
107   public void shutdown() {
108     // Nothing to do.
109   }
110 }