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.io.hfile.Cacheable;
26  import org.apache.hadoop.hbase.io.hfile.CacheableDeserializer;
27  import org.apache.hadoop.hbase.io.hfile.Cacheable.MemoryType;
28  import org.apache.hadoop.hbase.nio.ByteBuff;
29  import org.apache.hadoop.hbase.util.ByteBufferArray;
30  
31  /**
32   * IO engine that stores data in memory using an array of ByteBuffers
33   * {@link ByteBufferArray}
34   */
35  @InterfaceAudience.Private
36  public class ByteBufferIOEngine implements IOEngine {
37    private ByteBufferArray bufferArray;
38    private final long capacity;
39    private final boolean direct;
40  
41    /**
42     * Construct the ByteBufferIOEngine with the given capacity
43     * @param capacity
44     * @param direct true if allocate direct buffer
45     * @throws IOException
46     */
47    public ByteBufferIOEngine(long capacity, boolean direct)
48        throws IOException {
49      this.capacity = capacity;
50      this.direct = direct;
51      bufferArray = new ByteBufferArray(capacity, direct);
52    }
53  
54    @Override
55    public String toString() {
56      return "ioengine=" + this.getClass().getSimpleName() + ", capacity=" +
57        String.format("%,d", this.capacity) + ", direct=" + this.direct;
58    }
59  
60    /**
61     * Memory IO engine is always unable to support persistent storage for the
62     * cache
63     * @return false
64     */
65    @Override
66    public boolean isPersistent() {
67      return false;
68    }
69  
70    @Override
71    public Cacheable read(long offset, int length, CacheableDeserializer<Cacheable> deserializer)
72        throws IOException {
73      ByteBuff dstBuffer = bufferArray.asSubByteBuff(offset, length);
74      // Here the buffer that is created directly refers to the buffer in the actual buckets.
75      // When any cell is referring to the blocks created out of these buckets then it means that
76      // those cells are referring to a shared memory area which if evicted by the BucketCache would
77      // lead to corruption of results. Hence we set the type of the buffer as SHARED_MEMORY
78      // so that the readers using this block are aware of this fact and do the necessary action
79      // to prevent eviction till the results are either consumed or copied
80      return deserializer.deserialize(dstBuffer, true, MemoryType.SHARED);
81    }
82  
83    /**
84     * Transfers data from the given byte buffer to the buffer array
85     * @param srcBuffer the given byte buffer from which bytes are to be read
86     * @param offset The offset in the ByteBufferArray of the first byte to be
87     *          written
88     * @throws IOException
89     */
90    @Override
91    public void write(ByteBuffer srcBuffer, long offset) throws IOException {
92      assert srcBuffer.hasArray();
93      bufferArray.putMultiple(offset, srcBuffer.remaining(), srcBuffer.array(),
94          srcBuffer.arrayOffset());
95    }
96  
97    @Override
98    public void write(ByteBuff srcBuffer, long offset) throws IOException {
99      // When caching block into BucketCache there will be single buffer backing for this HFileBlock.
100     // This will work for now. But from the DFS itself if we get DBB then this may not hold true.
101     assert srcBuffer.hasArray();
102     bufferArray.putMultiple(offset, srcBuffer.remaining(), srcBuffer.array(),
103         srcBuffer.arrayOffset());
104   }
105   /**
106    * No operation for the sync in the memory IO engine
107    */
108   @Override
109   public void sync() {
110     // Nothing to do.
111   }
112 
113   /**
114    * No operation for the shutdown in the memory IO engine
115    */
116   @Override
117   public void shutdown() {
118     // Nothing to do.
119   }
120 }