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