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