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 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
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
46 }
47
48
49
50
51
52
53
54
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
73
74
75
76
77
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
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 }