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  import java.util.List;
23
24  import org.apache.commons.logging.Log;
25  import org.apache.commons.logging.LogFactory;
26  import org.apache.hadoop.hbase.classification.InterfaceAudience;
27  import org.apache.hadoop.fs.FileStatus;
28  import org.apache.hadoop.fs.FileSystem;
29  import org.apache.hadoop.fs.Path;
30  import org.apache.hadoop.fs.PathFilter;
31
32  /**
33   * Utility methods for interacting with the hbase.root file system.
34   */
35  @InterfaceAudience.Private
36  public final class FSVisitor {
37    private static final Log LOG = LogFactory.getLog(FSVisitor.class);
38
39    public interface StoreFileVisitor {
40      void storeFile(final String region, final String family, final String hfileName)
41         throws IOException;
42    }
43
44    private FSVisitor() {
45      // private constructor for utility class
46    }
47
48    /**
49     * Iterate over the table store files
50     *
51     * @param fs {@link FileSystem}
52     * @param tableDir {@link Path} to the table directory
53     * @param visitor callback object to get the store files
54     * @throws IOException if an error occurred while scanning the directory
55     */
56    public static void visitTableStoreFiles(final FileSystem fs, final Path tableDir,
57        final StoreFileVisitor visitor) throws IOException {
58      List<FileStatus> regions = FSUtils.listStatusWithStatusFilter(fs, tableDir, new FSUtils.RegionDirFilter(fs));
59      if (regions == null) {
60        if (LOG.isTraceEnabled()) {
61          LOG.trace("No regions under directory:" + tableDir);
62        }
63        return;
64      }
65
66      for (FileStatus region: regions) {
67        visitRegionStoreFiles(fs, region.getPath(), visitor);
68      }
69    }
70
71    /**
72     * Iterate over the region store files
73     *
74     * @param fs {@link FileSystem}
75     * @param regionDir {@link Path} to the region directory
76     * @param visitor callback object to get the store files
77     * @throws IOException if an error occurred while scanning the directory
78     */
79    public static void visitRegionStoreFiles(final FileSystem fs, final Path regionDir,
80        final StoreFileVisitor visitor) throws IOException {
81      List<FileStatus> families = FSUtils.listStatusWithStatusFilter(fs, regionDir, new FSUtils.FamilyDirFilter(fs));
82      if (families == null) {
83        if (LOG.isTraceEnabled()) {
84          LOG.trace("No families under region directory:" + regionDir);
85        }
86        return;
87      }
88
89      PathFilter fileFilter = new FSUtils.FileFilter(fs);
90      for (FileStatus family: families) {
91        Path familyDir = family.getPath();
92        String familyName = familyDir.getName();
93
94        // get all the storeFiles in the family
95        FileStatus[] storeFiles = FSUtils.listStatus(fs, familyDir, fileFilter);
96        if (storeFiles == null) {
97          if (LOG.isTraceEnabled()) {
98            LOG.trace("No hfiles found for family: " + familyDir + ", skipping.");
99          }
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 }