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.util; 020 021import java.io.IOException; 022import java.util.List; 023 024import org.apache.yetus.audience.InterfaceAudience; 025import org.slf4j.Logger; 026import org.slf4j.LoggerFactory; 027import org.apache.hadoop.fs.FileStatus; 028import org.apache.hadoop.fs.FileSystem; 029import org.apache.hadoop.fs.Path; 030import org.apache.hadoop.fs.PathFilter; 031 032/** 033 * Utility methods for interacting with the hbase.root file system. 034 */ 035@InterfaceAudience.Private 036public final class FSVisitor { 037 private static final Logger LOG = LoggerFactory.getLogger(FSVisitor.class); 038 039 public interface StoreFileVisitor { 040 void storeFile(final String region, final String family, final String hfileName) 041 throws IOException; 042 } 043 044 private FSVisitor() { 045 // private constructor for utility class 046 } 047 048 /** 049 * Iterate over the table store files 050 * 051 * @param fs {@link FileSystem} 052 * @param tableDir {@link Path} to the table directory 053 * @param visitor callback object to get the store files 054 * @throws IOException if an error occurred while scanning the directory 055 */ 056 public static void visitTableStoreFiles(final FileSystem fs, final Path tableDir, 057 final StoreFileVisitor visitor) throws IOException { 058 List<FileStatus> regions = FSUtils.listStatusWithStatusFilter(fs, tableDir, new FSUtils.RegionDirFilter(fs)); 059 if (regions == null) { 060 if (LOG.isTraceEnabled()) { 061 LOG.trace("No regions under directory:" + tableDir); 062 } 063 return; 064 } 065 066 for (FileStatus region: regions) { 067 visitRegionStoreFiles(fs, region.getPath(), visitor); 068 } 069 } 070 071 /** 072 * Iterate over the region store files 073 * 074 * @param fs {@link FileSystem} 075 * @param regionDir {@link Path} to the region directory 076 * @param visitor callback object to get the store files 077 * @throws IOException if an error occurred while scanning the directory 078 */ 079 public static void visitRegionStoreFiles(final FileSystem fs, final Path regionDir, 080 final StoreFileVisitor visitor) throws IOException { 081 List<FileStatus> families = FSUtils.listStatusWithStatusFilter(fs, regionDir, new FSUtils.FamilyDirFilter(fs)); 082 if (families == null) { 083 if (LOG.isTraceEnabled()) { 084 LOG.trace("No families under region directory:" + regionDir); 085 } 086 return; 087 } 088 089 PathFilter fileFilter = new FSUtils.FileFilter(fs); 090 for (FileStatus family: families) { 091 Path familyDir = family.getPath(); 092 String familyName = familyDir.getName(); 093 094 // get all the storeFiles in the family 095 FileStatus[] storeFiles = FSUtils.listStatus(fs, familyDir, fileFilter); 096 if (storeFiles == null) { 097 if (LOG.isTraceEnabled()) { 098 LOG.trace("No hfiles found for family: " + familyDir + ", skipping."); 099 } 100 continue; 101 } 102 103 for (FileStatus hfile: storeFiles) { 104 Path hfilePath = hfile.getPath(); 105 visitor.storeFile(regionDir.getName(), familyName, hfilePath.getName()); 106 } 107 } 108 } 109}