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