View Javadoc

1   /**
2    *
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *     http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing, software
14   * distributed under the License is distributed on an "AS IS" BASIS,
15   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   * See the License for the specific language governing permissions and
17   * limitations under the License.
18   */
19  package org.apache.hadoop.hbase.util;
20  
21  import java.io.IOException;
22  
23  import org.apache.commons.logging.Log;
24  import org.apache.commons.logging.LogFactory;
25  import org.apache.hadoop.hbase.classification.InterfaceAudience;
26  import org.apache.hadoop.fs.FileStatus;
27  import org.apache.hadoop.fs.FileSystem;
28  import org.apache.hadoop.fs.Path;
29  import org.apache.hadoop.fs.PathFilter;
30  
31  /**
32   * Utility methods for interacting with the hbase.root file system.
33   */
34  @InterfaceAudience.Private
35  public final class FSVisitor {
36    private static final Log LOG = LogFactory.getLog(FSVisitor.class);
37  
38    public interface StoreFileVisitor {
39      void storeFile(final String region, final String family, final String hfileName)
40         throws IOException;
41    }
42  
43    private FSVisitor() {
44      // private constructor for utility class
45    }
46  
47    /**
48     * Iterate over the table store files
49     *
50     * @param fs {@link FileSystem}
51     * @param tableDir {@link Path} to the table directory
52     * @param visitor callback object to get the store files
53     * @throws IOException if an error occurred while scanning the directory
54     */
55    public static void visitTableStoreFiles(final FileSystem fs, final Path tableDir,
56        final StoreFileVisitor visitor) throws IOException {
57      FileStatus[] regions = FSUtils.listStatus(fs, tableDir, new FSUtils.RegionDirFilter(fs));
58      if (regions == null) {
59        if (LOG.isTraceEnabled()) {
60          LOG.trace("No regions under directory:" + tableDir);
61        }
62        return;
63      }
64  
65      for (FileStatus region: regions) {
66        visitRegionStoreFiles(fs, region.getPath(), visitor);
67      }
68    }
69  
70    /**
71     * Iterate over the region store files
72     *
73     * @param fs {@link FileSystem}
74     * @param regionDir {@link Path} to the region directory
75     * @param visitor callback object to get the store files
76     * @throws IOException if an error occurred while scanning the directory
77     */
78    public static void visitRegionStoreFiles(final FileSystem fs, final Path regionDir,
79        final StoreFileVisitor visitor) throws IOException {
80      FileStatus[] families = FSUtils.listStatus(fs, regionDir, new FSUtils.FamilyDirFilter(fs));
81      if (families == null) {
82        if (LOG.isTraceEnabled()) {
83          LOG.trace("No families under region directory:" + regionDir);
84        }
85        return;
86      }
87  
88      PathFilter fileFilter = new FSUtils.FileFilter(fs);
89      for (FileStatus family: families) {
90        Path familyDir = family.getPath();
91        String familyName = familyDir.getName();
92  
93        // get all the storeFiles in the family
94        FileStatus[] storeFiles = FSUtils.listStatus(fs, familyDir, fileFilter);
95        if (storeFiles == null) {
96          if (LOG.isTraceEnabled()) {
97            LOG.trace("No hfiles found for family: " + familyDir + ", skipping.");
98          }
99          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 }