View Javadoc

1   /**
2    *
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  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,
15   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   * See the License for the specific language governing permissions and
17   * limitations under the License.
18   */
19  package org.apache.hadoop.hbase.mob;
20  
21  import java.io.IOException;
22  import java.util.ArrayList;
23  import java.util.List;
24  
25  import org.apache.hadoop.hbase.classification.InterfaceAudience;
26  import org.apache.hadoop.conf.Configuration;
27  import org.apache.hadoop.fs.FileSystem;
28  import org.apache.hadoop.fs.Path;
29  import org.apache.hadoop.hbase.Cell;
30  import org.apache.hadoop.hbase.io.hfile.CacheConfig;
31  import org.apache.hadoop.hbase.regionserver.BloomType;
32  import org.apache.hadoop.hbase.regionserver.StoreFile;
33  import org.apache.hadoop.hbase.regionserver.StoreFileScanner;
34  
35  /**
36   * The mob file.
37   */
38  @InterfaceAudience.Private
39  public class MobFile {
40  
41    private StoreFile sf;
42  
43    // internal use only for sub classes
44    protected MobFile() {
45    }
46  
47    protected MobFile(StoreFile sf) {
48      this.sf = sf;
49    }
50  
51    /**
52     * Internal use only. This is used by the sweeper.
53     *
54     * @return The store file scanner.
55     * @throws IOException
56     */
57    public StoreFileScanner getScanner() throws IOException {
58      List<StoreFile> sfs = new ArrayList<StoreFile>();
59      sfs.add(sf);
60      List<StoreFileScanner> sfScanners = StoreFileScanner.getScannersForStoreFiles(sfs, false, true,
61          false, null, sf.getMaxMemstoreTS());
62  
63      return sfScanners.get(0);
64    }
65  
66    /**
67     * Reads a cell from the mob file.
68     * @param search The cell need to be searched in the mob file.
69     * @param cacheMobBlocks Should this scanner cache blocks.
70     * @return The cell in the mob file.
71     * @throws IOException
72     */
73    public Cell readCell(Cell search, boolean cacheMobBlocks) throws IOException {
74      return readCell(search, cacheMobBlocks, sf.getMaxMemstoreTS());
75    }
76  
77    /**
78     * Reads a cell from the mob file.
79     * @param search The cell need to be searched in the mob file.
80     * @param cacheMobBlocks Should this scanner cache blocks.
81     * @param readPt the read point.
82     * @return The cell in the mob file.
83     * @throws IOException
84     */
85    public Cell readCell(Cell search, boolean cacheMobBlocks, long readPt) throws IOException {
86      Cell result = null;
87      StoreFileScanner scanner = null;
88      List<StoreFile> sfs = new ArrayList<StoreFile>();
89      sfs.add(sf);
90      try {
91        List<StoreFileScanner> sfScanners = StoreFileScanner.getScannersForStoreFiles(sfs,
92          cacheMobBlocks, true, false, null, readPt);
93        if (!sfScanners.isEmpty()) {
94          scanner = sfScanners.get(0);
95          if (scanner.seek(search)) {
96            result = scanner.peek();
97          }
98        }
99      } finally {
100       if (scanner != null) {
101         scanner.close();
102       }
103     }
104     return result;
105   }
106 
107   /**
108    * Gets the file name.
109    * @return The file name.
110    */
111   public String getFileName() {
112     return sf.getPath().getName();
113   }
114 
115   /**
116    * Opens the underlying reader.
117    * It's not thread-safe. Use MobFileCache.openFile() instead.
118    * @throws IOException
119    */
120   public void open() throws IOException {
121     if (sf.getReader() == null) {
122       sf.createReader();
123     }
124   }
125 
126   /**
127    * Closes the underlying reader, but do no evict blocks belonging to this file.
128    * It's not thread-safe. Use MobFileCache.closeFile() instead.
129    * @throws IOException
130    */
131   public void close() throws IOException {
132     if (sf != null) {
133       sf.closeReader(false);
134       sf = null;
135     }
136   }
137 
138   /**
139    * Creates an instance of the MobFile.
140    * @param fs The file system.
141    * @param path The path of the underlying StoreFile.
142    * @param conf The configuration.
143    * @param cacheConf The CacheConfig.
144    * @return An instance of the MobFile.
145    * @throws IOException
146    */
147   public static MobFile create(FileSystem fs, Path path, Configuration conf, CacheConfig cacheConf)
148       throws IOException {
149     StoreFile sf = new StoreFile(fs, path, conf, cacheConf, BloomType.NONE);
150     return new MobFile(sf);
151   }
152 }