1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
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
45 }
46
47
48
49
50
51
52
53
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
72
73
74
75
76
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
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 }