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.Collections; 024import java.util.List; 025 026import org.apache.hadoop.conf.Configuration; 027import org.apache.hadoop.fs.FileSystem; 028import org.apache.hadoop.fs.Path; 029import org.apache.hadoop.hbase.Cell; 030import org.apache.hadoop.hbase.io.hfile.CacheConfig; 031import org.apache.hadoop.hbase.regionserver.BloomType; 032import org.apache.hadoop.hbase.regionserver.HStoreFile; 033import org.apache.hadoop.hbase.regionserver.StoreFileScanner; 034import org.apache.yetus.audience.InterfaceAudience; 035 036/** 037 * The mob file. 038 */ 039@InterfaceAudience.Private 040public class MobFile { 041 042 private HStoreFile sf; 043 044 // internal use only for sub classes 045 protected MobFile() { 046 } 047 048 protected MobFile(HStoreFile sf) { 049 this.sf = sf; 050 } 051 052 /** 053 * Internal use only. This is used by the sweeper. 054 * 055 * @return The store file scanner. 056 * @throws IOException 057 */ 058 public StoreFileScanner getScanner() throws IOException { 059 List<HStoreFile> sfs = new ArrayList<>(); 060 sfs.add(sf); 061 List<StoreFileScanner> sfScanners = StoreFileScanner.getScannersForStoreFiles(sfs, false, true, 062 false, false, sf.getMaxMemStoreTS()); 063 064 return sfScanners.get(0); 065 } 066 067 /** 068 * Reads a cell from the mob file. 069 * @param search The cell need to be searched in the mob file. 070 * @param cacheMobBlocks Should this scanner cache blocks. 071 * @return The cell in the mob file. 072 * @throws IOException 073 */ 074 public MobCell readCell(Cell search, boolean cacheMobBlocks) throws IOException { 075 return readCell(search, cacheMobBlocks, sf.getMaxMemStoreTS()); 076 } 077 078 /** 079 * Reads a cell from the mob file. 080 * @param search The cell need to be searched in the mob file. 081 * @param cacheMobBlocks Should this scanner cache blocks. 082 * @param readPt the read point. 083 * @return The cell in the mob file. 084 * @throws IOException 085 */ 086 public MobCell readCell(Cell search, boolean cacheMobBlocks, long readPt) throws IOException { 087 StoreFileScanner scanner = null; 088 boolean succ = false; 089 try { 090 List<StoreFileScanner> sfScanners = StoreFileScanner.getScannersForStoreFiles( 091 Collections.singletonList(sf), cacheMobBlocks, true, false, false, readPt); 092 if (!sfScanners.isEmpty()) { 093 scanner = sfScanners.get(0); 094 if (scanner.seek(search)) { 095 MobCell mobCell = new MobCell(scanner.peek(), scanner); 096 succ = true; 097 return mobCell; 098 } 099 } 100 return null; 101 } finally { 102 if (scanner != null && !succ) { 103 scanner.close(); 104 } 105 } 106 } 107 108 /** 109 * Gets the file name. 110 * @return The file name. 111 */ 112 public String getFileName() { 113 return sf.getPath().getName(); 114 } 115 116 /** 117 * Opens the underlying reader. 118 * It's not thread-safe. Use MobFileCache.openFile() instead. 119 * @throws IOException 120 */ 121 public void open() throws IOException { 122 sf.initReader(); 123 } 124 125 /** 126 * Closes the underlying reader, but do no evict blocks belonging to this file. 127 * It's not thread-safe. Use MobFileCache.closeFile() instead. 128 * @throws IOException 129 */ 130 public void close() throws IOException { 131 if (sf != null) { 132 sf.closeStoreFile(false); 133 sf = null; 134 } 135 } 136 137 /** 138 * Creates an instance of the MobFile. 139 * @param fs The file system. 140 * @param path The path of the underlying StoreFile. 141 * @param conf The configuration. 142 * @param cacheConf The CacheConfig. 143 * @return An instance of the MobFile. 144 * @throws IOException 145 */ 146 public static MobFile create(FileSystem fs, Path path, Configuration conf, CacheConfig cacheConf) 147 throws IOException { 148 // XXX: primaryReplica is only used for constructing the key of block cache so it is not a 149 // critical problem if we pass the wrong value, so here we always pass true. Need to fix later. 150 HStoreFile sf = new HStoreFile(fs, path, conf, cacheConf, BloomType.NONE, true); 151 return new MobFile(sf); 152 } 153}