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    private final String path;
40    private long size;
41  
42    public FileIOEngine(String filePath, long fileSize) throws IOException {
43      this.path = filePath;
44      this.size = fileSize;
45      RandomAccessFile raf = null;
46      try {
47        raf = new RandomAccessFile(filePath, "rw");
48        raf.setLength(fileSize);
49        fileChannel = raf.getChannel();
50        LOG.info("Allocating " + StringUtils.byteDesc(fileSize)
51            + ", on the path:" + filePath);
52      } catch (java.io.FileNotFoundException fex) {
53        LOG.error("Can't create bucket cache file " + filePath, fex);
54        throw fex;
55      } catch (IOException ioex) {
56        LOG.error("Can't extend bucket cache file; insufficient space for "
57            + StringUtils.byteDesc(fileSize), ioex);
58        if (raf != null) raf.close();
59        throw ioex;
60      }
61    }
62  
63    @Override
64    public String toString() {
65      return "ioengine=" + this.getClass().getSimpleName() + ", path=" + this.path +
66        ", size=" + String.format("%,d", this.size);
67    }
68  
69    /**
70     * File IO engine is always able to support persistent storage for the cache
71     * @return true
72     */
73    @Override
74    public boolean isPersistent() {
75      return true;
76    }
77  
78    /**
79     * Transfers data from file to the given byte buffer
80     * @param dstBuffer the given byte buffer into which bytes are to be written
81     * @param offset The offset in the file where the first byte to be read
82     * @return number of bytes read
83     * @throws IOException
84     */
85    @Override
86    public int read(ByteBuffer dstBuffer, long offset) throws IOException {
87      return fileChannel.read(dstBuffer, offset);
88    }
89  
90    /**
91     * Transfers data from the given byte buffer to file
92     * @param srcBuffer the given byte buffer from which bytes are to be read
93     * @param offset The offset in the file where the first byte to be written
94     * @throws IOException
95     */
96    @Override
97    public void write(ByteBuffer srcBuffer, long offset) throws IOException {
98      fileChannel.write(srcBuffer, offset);
99    }
100 
101   /**
102    * Sync the data to file after writing
103    * @throws IOException
104    */
105   @Override
106   public void sync() throws IOException {
107     fileChannel.force(true);
108   }
109 
110   /**
111    * Close the file
112    */
113   @Override
114   public void shutdown() {
115     try {
116       fileChannel.close();
117     } catch (IOException ex) {
118       LOG.error("Can't shutdown cleanly", ex);
119     }
120   }
121 }