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