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