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.io.RandomAccessFile;
23  import java.nio.ByteBuffer;
24  import java.nio.channels.FileChannel;
25  
26  import org.apache.commons.logging.Log;
27  import org.apache.commons.logging.LogFactory;
28  import org.apache.hadoop.classification.InterfaceAudience;
29  import org.apache.hadoop.util.StringUtils;
30  
31  /**
32   * IO engine that stores data to a file on the local file system.
33   */
34  @InterfaceAudience.Private
35  public class FileIOEngine implements IOEngine {
36    static final Log LOG = LogFactory.getLog(FileIOEngine.class);
37  
38    private FileChannel fileChannel = null;
39  
40    public FileIOEngine(String filePath, long fileSize) throws IOException {
41      RandomAccessFile raf = null;
42      try {
43        raf = new RandomAccessFile(filePath, "rw");
44        raf.setLength(fileSize);
45        fileChannel = raf.getChannel();
46        LOG.info("Allocating " + StringUtils.byteDesc(fileSize)
47            + ", on the path:" + filePath);
48      } catch (java.io.FileNotFoundException fex) {
49        LOG.error("Can't create bucket cache file " + filePath, fex);
50        throw fex;
51      } catch (IOException ioex) {
52        LOG.error("Can't extend bucket cache file; insufficient space for "
53            + StringUtils.byteDesc(fileSize), ioex);
54        if (raf != null) raf.close();
55        throw ioex;
56      }
57    }
58  
59    /**
60     * File IO engine is always able to support persistent storage for the cache
61     * @return true
62     */
63    @Override
64    public boolean isPersistent() {
65      return true;
66    }
67  
68    /**
69     * Transfers data from file to the given byte buffer
70     * @param dstBuffer the given byte buffer into which bytes are to be written
71     * @param offset The offset in the file where the first byte to be read
72     * @return number of bytes read
73     * @throws IOException
74     */
75    @Override
76    public int read(ByteBuffer dstBuffer, long offset) throws IOException {
77      return fileChannel.read(dstBuffer, offset);
78    }
79  
80    /**
81     * Transfers data from the given byte buffer to file
82     * @param srcBuffer the given byte buffer from which bytes are to be read
83     * @param offset The offset in the file where the first byte to be written
84     * @throws IOException
85     */
86    @Override
87    public void write(ByteBuffer srcBuffer, long offset) throws IOException {
88      fileChannel.write(srcBuffer, offset);
89    }
90  
91    /**
92     * Sync the data to file after writing
93     * @throws IOException
94     */
95    @Override
96    public void sync() throws IOException {
97      fileChannel.force(true);
98    }
99  
100   /**
101    * Close the file
102    */
103   @Override
104   public void shutdown() {
105     try {
106       fileChannel.close();
107     } catch (IOException ex) {
108       LOG.error("Can't shutdown cleanly", ex);
109     }
110   }
111 }