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