001/**
002 *
003 * Licensed to the Apache Software Foundation (ASF) under one
004 * or more contributor license agreements.  See the NOTICE file
005 * distributed with this work for additional information
006 * regarding copyright ownership.  The ASF licenses this file
007 * to you under the Apache License, Version 2.0 (the
008 * "License"); you may not use this file except in compliance
009 * with the License.  You may obtain a copy of the License at
010 *
011 *     http://www.apache.org/licenses/LICENSE-2.0
012 *
013 * Unless required by applicable law or agreed to in writing, software
014 * distributed under the License is distributed on an "AS IS" BASIS,
015 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
016 * See the License for the specific language governing permissions and
017 * limitations under the License.
018 */
019package org.apache.hadoop.hbase.mob;
020
021import java.io.IOException;
022import java.util.ArrayList;
023import java.util.List;
024
025import org.apache.hadoop.conf.Configuration;
026import org.apache.hadoop.fs.FileSystem;
027import org.apache.hadoop.fs.Path;
028import org.apache.hadoop.hbase.Cell;
029import org.apache.hadoop.hbase.io.hfile.CacheConfig;
030import org.apache.hadoop.hbase.regionserver.BloomType;
031import org.apache.hadoop.hbase.regionserver.HStoreFile;
032import org.apache.hadoop.hbase.regionserver.StoreFileScanner;
033import org.apache.yetus.audience.InterfaceAudience;
034
035/**
036 * The mob file.
037 */
038@InterfaceAudience.Private
039public class MobFile {
040
041  private HStoreFile sf;
042
043  // internal use only for sub classes
044  protected MobFile() {
045  }
046
047  protected MobFile(HStoreFile sf) {
048    this.sf = sf;
049  }
050
051  /**
052   * Internal use only. This is used by the sweeper.
053   *
054   * @return The store file scanner.
055   * @throws IOException
056   */
057  public StoreFileScanner getScanner() throws IOException {
058    List<HStoreFile> sfs = new ArrayList<>();
059    sfs.add(sf);
060    List<StoreFileScanner> sfScanners = StoreFileScanner.getScannersForStoreFiles(sfs, false, true,
061        false, false, sf.getMaxMemStoreTS());
062
063    return sfScanners.get(0);
064  }
065
066  /**
067   * Reads a cell from the mob file.
068   * @param search The cell need to be searched in the mob file.
069   * @param cacheMobBlocks Should this scanner cache blocks.
070   * @return The cell in the mob file.
071   * @throws IOException
072   */
073  public Cell readCell(Cell search, boolean cacheMobBlocks) throws IOException {
074    return readCell(search, cacheMobBlocks, sf.getMaxMemStoreTS());
075  }
076
077  /**
078   * Reads a cell from the mob file.
079   * @param search The cell need to be searched in the mob file.
080   * @param cacheMobBlocks Should this scanner cache blocks.
081   * @param readPt the read point.
082   * @return The cell in the mob file.
083   * @throws IOException
084   */
085  public Cell readCell(Cell search, boolean cacheMobBlocks, long readPt) throws IOException {
086    Cell result = null;
087    StoreFileScanner scanner = null;
088    List<HStoreFile> sfs = new ArrayList<>();
089    sfs.add(sf);
090    try {
091      List<StoreFileScanner> sfScanners = StoreFileScanner.getScannersForStoreFiles(sfs,
092        cacheMobBlocks, true, false, false, readPt);
093      if (!sfScanners.isEmpty()) {
094        scanner = sfScanners.get(0);
095        if (scanner.seek(search)) {
096          result = scanner.peek();
097        }
098      }
099    } 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    sf.initReader();
122  }
123
124  /**
125   * Closes the underlying reader, but do no evict blocks belonging to this file.
126   * It's not thread-safe. Use MobFileCache.closeFile() instead.
127   * @throws IOException
128   */
129  public void close() throws IOException {
130    if (sf != null) {
131      sf.closeStoreFile(false);
132      sf = null;
133    }
134  }
135
136  /**
137   * Creates an instance of the MobFile.
138   * @param fs The file system.
139   * @param path The path of the underlying StoreFile.
140   * @param conf The configuration.
141   * @param cacheConf The CacheConfig.
142   * @return An instance of the MobFile.
143   * @throws IOException
144   */
145  public static MobFile create(FileSystem fs, Path path, Configuration conf, CacheConfig cacheConf)
146      throws IOException {
147    // XXX: primaryReplica is only used for constructing the key of block cache so it is not a
148    // critical problem if we pass the wrong value, so here we always pass true. Need to fix later.
149    HStoreFile sf = new HStoreFile(fs, path, conf, cacheConf, BloomType.NONE, true);
150    return new MobFile(sf);
151  }
152}