View Javadoc

1   /**
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  package org.apache.hadoop.hbase.backup;
19  
20  import java.io.IOException;
21  import java.util.ArrayList;
22  import java.util.Arrays;
23  import java.util.Collection;
24  import java.util.Collections;
25  import java.util.List;
26  
27  import org.apache.commons.logging.Log;
28  import org.apache.commons.logging.LogFactory;
29  import org.apache.hadoop.conf.Configuration;
30  import org.apache.hadoop.fs.FileStatus;
31  import org.apache.hadoop.fs.FileSystem;
32  import org.apache.hadoop.fs.Path;
33  import org.apache.hadoop.fs.PathFilter;
34  import org.apache.hadoop.hbase.TableName;
35  import org.apache.hadoop.hbase.HRegionInfo;
36  import org.apache.hadoop.hbase.regionserver.HRegion;
37  import org.apache.hadoop.hbase.regionserver.StoreFile;
38  import org.apache.hadoop.hbase.util.Bytes;
39  import org.apache.hadoop.hbase.util.EnvironmentEdgeManager;
40  import org.apache.hadoop.hbase.util.FSUtils;
41  import org.apache.hadoop.hbase.util.HFileArchiveUtil;
42  import org.apache.hadoop.io.MultipleIOException;
43  
44  import com.google.common.base.Function;
45  import com.google.common.base.Preconditions;
46  import com.google.common.collect.Collections2;
47  import com.google.common.collect.Lists;
48  
49  /**
50   * Utility class to handle the removal of HFiles (or the respective {@link StoreFile StoreFiles})
51   * for a HRegion from the {@link FileSystem}. The hfiles will be archived or deleted, depending on
52   * the state of the system.
53   */
54  public class HFileArchiver {
55    private static final Log LOG = LogFactory.getLog(HFileArchiver.class);
56    private static final String SEPARATOR = ".";
57  
58    /** Number of retries in case of fs operation failure */
59    private static final int DEFAULT_RETRIES_NUMBER = 3;
60  
61    private HFileArchiver() {
62      // hidden ctor since this is just a util
63    }
64  
65    /**
66     * Cleans up all the files for a HRegion by archiving the HFiles to the
67     * archive directory
68     * @param conf the configuration to use
69     * @param fs the file system object
70     * @param info HRegionInfo for region to be deleted
71     * @throws IOException
72     */
73    public static void archiveRegion(Configuration conf, FileSystem fs, HRegionInfo info)
74        throws IOException {
75      Path rootDir = FSUtils.getRootDir(conf);
76      archiveRegion(fs, rootDir, FSUtils.getTableDir(rootDir, info.getTableName()),
77        HRegion.getRegionDir(rootDir, info));
78    }
79  
80    /**
81     * Remove an entire region from the table directory via archiving the region's hfiles.
82     * @param fs {@link FileSystem} from which to remove the region
83     * @param rootdir {@link Path} to the root directory where hbase files are stored (for building
84     *          the archive path)
85     * @param tableDir {@link Path} to where the table is being stored (for building the archive path)
86     * @param regionDir {@link Path} to where a region is being stored (for building the archive path)
87     * @return <tt>true</tt> if the region was sucessfully deleted. <tt>false</tt> if the filesystem
88     *         operations could not complete.
89     * @throws IOException if the request cannot be completed
90     */
91    public static boolean archiveRegion(FileSystem fs, Path rootdir, Path tableDir, Path regionDir)
92        throws IOException {
93      if (LOG.isDebugEnabled()) {
94        LOG.debug("ARCHIVING region " + regionDir.toString());
95      }
96  
97      // otherwise, we archive the files
98      // make sure we can archive
99      if (tableDir == null || regionDir == null) {
100       LOG.error("No archive directory could be found because tabledir (" + tableDir
101           + ") or regiondir (" + regionDir + "was null. Deleting files instead.");
102       deleteRegionWithoutArchiving(fs, regionDir);
103       // we should have archived, but failed to. Doesn't matter if we deleted
104       // the archived files correctly or not.
105       return false;
106     }
107 
108     // make sure the regiondir lives under the tabledir
109     Preconditions.checkArgument(regionDir.toString().startsWith(tableDir.toString()));
110     Path regionArchiveDir = HFileArchiveUtil.getRegionArchiveDir(rootdir,
111         FSUtils.getTableName(tableDir),
112         regionDir.getName());
113 
114     FileStatusConverter getAsFile = new FileStatusConverter(fs);
115     // otherwise, we attempt to archive the store files
116 
117     // build collection of just the store directories to archive
118     Collection<File> toArchive = new ArrayList<File>();
119     final PathFilter dirFilter = new FSUtils.DirFilter(fs);
120     PathFilter nonHidden = new PathFilter() {
121       @Override
122       public boolean accept(Path file) {
123         return dirFilter.accept(file) && !file.getName().toString().startsWith(".");
124       }
125     };
126     FileStatus[] storeDirs = FSUtils.listStatus(fs, regionDir, nonHidden);
127     // if there no files, we can just delete the directory and return;
128     if (storeDirs == null) {
129       LOG.debug("Region directory (" + regionDir + ") was empty, just deleting and returning!");
130       return deleteRegionWithoutArchiving(fs, regionDir);
131     }
132 
133     // convert the files in the region to a File
134     toArchive.addAll(Lists.transform(Arrays.asList(storeDirs), getAsFile));
135     LOG.debug("Archiving:" + toArchive);
136     boolean success = false;
137     try {
138       success = resolveAndArchive(fs, regionArchiveDir, toArchive);
139     } catch (IOException e) {
140       LOG.error("Failed to archive: " + toArchive, e);
141       success = false;
142     }
143 
144     // if that was successful, then we delete the region
145     if (success) {
146       return deleteRegionWithoutArchiving(fs, regionDir);
147     }
148 
149     throw new IOException("Received error when attempting to archive files (" + toArchive
150         + "), cannot delete region directory. ");
151   }
152 
153   /**
154    * Remove from the specified region the store files of the specified column family,
155    * either by archiving them or outright deletion
156    * @param fs the filesystem where the store files live
157    * @param conf {@link Configuration} to examine to determine the archive directory
158    * @param parent Parent region hosting the store files
159    * @param tableDir {@link Path} to where the table is being stored (for building the archive path)
160    * @param family the family hosting the store files
161    * @throws IOException if the files could not be correctly disposed.
162    */
163   public static void archiveFamily(FileSystem fs, Configuration conf,
164       HRegionInfo parent, Path tableDir, byte[] family) throws IOException {
165     Path familyDir = new Path(tableDir, new Path(parent.getEncodedName(), Bytes.toString(family)));
166     FileStatus[] storeFiles = FSUtils.listStatus(fs, familyDir);
167     if (storeFiles == null) {
168       LOG.debug("No store files to dispose for region=" + parent.getRegionNameAsString() +
169           ", family=" + Bytes.toString(family));
170       return;
171     }
172 
173     FileStatusConverter getAsFile = new FileStatusConverter(fs);
174     Collection<File> toArchive = Lists.transform(Arrays.asList(storeFiles), getAsFile);
175     Path storeArchiveDir = HFileArchiveUtil.getStoreArchivePath(conf, parent, tableDir, family);
176 
177     // do the actual archive
178     if (!resolveAndArchive(fs, storeArchiveDir, toArchive)) {
179       throw new IOException("Failed to archive/delete all the files for region:"
180           + Bytes.toString(parent.getRegionName()) + ", family:" + Bytes.toString(family)
181           + " into " + storeArchiveDir + ". Something is probably awry on the filesystem.");
182     }
183   }
184 
185   /**
186    * Remove the store files, either by archiving them or outright deletion
187    * @param conf {@link Configuration} to examine to determine the archive directory
188    * @param fs the filesystem where the store files live
189    * @param regionInfo {@link HRegionInfo} of the region hosting the store files
190    * @param family the family hosting the store files
191    * @param compactedFiles files to be disposed of. No further reading of these files should be
192    *          attempted; otherwise likely to cause an {@link IOException}
193    * @throws IOException if the files could not be correctly disposed.
194    */
195   public static void archiveStoreFiles(Configuration conf, FileSystem fs, HRegionInfo regionInfo,
196       Path tableDir, byte[] family, Collection<StoreFile> compactedFiles) throws IOException {
197 
198     // sometimes in testing, we don't have rss, so we need to check for that
199     if (fs == null) {
200       LOG.warn("Passed filesystem is null, so just deleting the files without archiving for region:"
201           + Bytes.toString(regionInfo.getRegionName()) + ", family:" + Bytes.toString(family));
202       deleteStoreFilesWithoutArchiving(compactedFiles);
203       return;
204     }
205 
206     // short circuit if we don't have any files to delete
207     if (compactedFiles.size() == 0) {
208       LOG.debug("No store files to dispose, done!");
209       return;
210     }
211 
212     // build the archive path
213     if (regionInfo == null || family == null) throw new IOException(
214         "Need to have a region and a family to archive from.");
215 
216     Path storeArchiveDir = HFileArchiveUtil.getStoreArchivePath(conf, regionInfo, tableDir, family);
217 
218     // make sure we don't archive if we can't and that the archive dir exists
219     if (!fs.mkdirs(storeArchiveDir)) {
220       throw new IOException("Could not make archive directory (" + storeArchiveDir + ") for store:"
221           + Bytes.toString(family) + ", deleting compacted files instead.");
222     }
223 
224     // otherwise we attempt to archive the store files
225     if (LOG.isTraceEnabled()) LOG.trace("Archiving compacted store files.");
226 
227     // wrap the storefile into a File
228     StoreToFile getStorePath = new StoreToFile(fs);
229     Collection<File> storeFiles = Collections2.transform(compactedFiles, getStorePath);
230 
231     // do the actual archive
232     if (!resolveAndArchive(fs, storeArchiveDir, storeFiles)) {
233       throw new IOException("Failed to archive/delete all the files for region:"
234           + Bytes.toString(regionInfo.getRegionName()) + ", family:" + Bytes.toString(family)
235           + " into " + storeArchiveDir + ". Something is probably awry on the filesystem.");
236     }
237   }
238 
239   /**
240    * Archive the store file
241    * @param fs the filesystem where the store files live
242    * @param regionInfo region hosting the store files
243    * @param conf {@link Configuration} to examine to determine the archive directory
244    * @param tableDir {@link Path} to where the table is being stored (for building the archive path)
245    * @param family the family hosting the store files
246    * @param storeFile file to be archived
247    * @throws IOException if the files could not be correctly disposed.
248    */
249   public static void archiveStoreFile(Configuration conf, FileSystem fs, HRegionInfo regionInfo,
250       Path tableDir, byte[] family, Path storeFile) throws IOException {
251     Path storeArchiveDir = HFileArchiveUtil.getStoreArchivePath(conf, regionInfo, tableDir, family);
252     // make sure we don't archive if we can't and that the archive dir exists
253     if (!fs.mkdirs(storeArchiveDir)) {
254       throw new IOException("Could not make archive directory (" + storeArchiveDir + ") for store:"
255           + Bytes.toString(family) + ", deleting compacted files instead.");
256     }
257 
258     // do the actual archive
259     long start = EnvironmentEdgeManager.currentTimeMillis();
260     File file = new FileablePath(fs, storeFile);
261     if (!resolveAndArchiveFile(storeArchiveDir, file, Long.toString(start))) {
262       throw new IOException("Failed to archive/delete the file for region:"
263           + regionInfo.getRegionNameAsString() + ", family:" + Bytes.toString(family)
264           + " into " + storeArchiveDir + ". Something is probably awry on the filesystem.");
265     }
266   }
267 
268   /**
269    * Archive the given files and resolve any conflicts with existing files via appending the time
270    * archiving started (so all conflicts in the same group have the same timestamp appended).
271    * <p>
272    * If any of the passed files to archive are directories, archives all the files under that
273    * directory. Archive directory structure for children is the base archive directory name + the
274    * parent directory and is built recursively is passed files are directories themselves.
275    * @param fs {@link FileSystem} on which to archive the files
276    * @param baseArchiveDir base archive directory to archive the given files
277    * @param toArchive files to be archived
278    * @return <tt>true</tt> on success, <tt>false</tt> otherwise
279    * @throws IOException on unexpected failure
280    */
281   private static boolean resolveAndArchive(FileSystem fs, Path baseArchiveDir,
282       Collection<File> toArchive) throws IOException {
283     if (LOG.isTraceEnabled()) LOG.trace("Starting to archive files:" + toArchive);
284     long start = EnvironmentEdgeManager.currentTimeMillis();
285     List<File> failures = resolveAndArchive(fs, baseArchiveDir, toArchive, start);
286 
287     // notify that some files were not archived.
288     // We can't delete the files otherwise snapshots or other backup system
289     // that relies on the archiver end up with data loss.
290     if (failures.size() > 0) {
291       LOG.warn("Failed to complete archive of: " + failures +
292         ". Those files are still in the original location, and they may slow down reads.");
293       return false;
294     }
295     return true;
296   }
297 
298   /**
299    * Resolve any conflict with an existing archive file via timestamp-append
300    * renaming of the existing file and then archive the passed in files.
301    * @param fs {@link FileSystem} on which to archive the files
302    * @param baseArchiveDir base archive directory to store the files. If any of
303    *          the files to archive are directories, will append the name of the
304    *          directory to the base archive directory name, creating a parallel
305    *          structure.
306    * @param toArchive files/directories that need to be archvied
307    * @param start time the archiving started - used for resolving archive
308    *          conflicts.
309    * @return the list of failed to archive files.
310    * @throws IOException if an unexpected file operation exception occured
311    */
312   private static List<File> resolveAndArchive(FileSystem fs, Path baseArchiveDir,
313       Collection<File> toArchive, long start) throws IOException {
314     // short circuit if no files to move
315     if (toArchive.size() == 0) return Collections.emptyList();
316 
317     if (LOG.isTraceEnabled()) LOG.trace("moving files to the archive directory: " + baseArchiveDir);
318 
319     // make sure the archive directory exists
320     if (!fs.exists(baseArchiveDir)) {
321       if (!fs.mkdirs(baseArchiveDir)) {
322         throw new IOException("Failed to create the archive directory:" + baseArchiveDir
323             + ", quitting archive attempt.");
324       }
325       if (LOG.isTraceEnabled()) LOG.trace("Created archive directory:" + baseArchiveDir);
326     }
327 
328     List<File> failures = new ArrayList<File>();
329     String startTime = Long.toString(start);
330     for (File file : toArchive) {
331       // if its a file archive it
332       try {
333         if (LOG.isTraceEnabled()) LOG.trace("Archiving: " + file);
334         if (file.isFile()) {
335           // attempt to archive the file
336           if (!resolveAndArchiveFile(baseArchiveDir, file, startTime)) {
337             LOG.warn("Couldn't archive " + file + " into backup directory: " + baseArchiveDir);
338             failures.add(file);
339           }
340         } else {
341           // otherwise its a directory and we need to archive all files
342           if (LOG.isTraceEnabled()) LOG.trace(file + " is a directory, archiving children files");
343           // so we add the directory name to the one base archive
344           Path parentArchiveDir = new Path(baseArchiveDir, file.getName());
345           // and then get all the files from that directory and attempt to
346           // archive those too
347           Collection<File> children = file.getChildren();
348           failures.addAll(resolveAndArchive(fs, parentArchiveDir, children, start));
349         }
350       } catch (IOException e) {
351         LOG.warn("Failed to archive file: " + file, e);
352         failures.add(file);
353       }
354     }
355     return failures;
356   }
357 
358   /**
359    * Attempt to archive the passed in file to the archive directory.
360    * <p>
361    * If the same file already exists in the archive, it is moved to a timestamped directory under
362    * the archive directory and the new file is put in its place.
363    * @param archiveDir {@link Path} to the directory that stores the archives of the hfiles
364    * @param currentFile {@link Path} to the original HFile that will be archived
365    * @param archiveStartTime time the archiving started, to resolve naming conflicts
366    * @return <tt>true</tt> if the file is successfully archived. <tt>false</tt> if there was a
367    *         problem, but the operation still completed.
368    * @throws IOException on failure to complete {@link FileSystem} operations.
369    */
370   private static boolean resolveAndArchiveFile(Path archiveDir, File currentFile,
371       String archiveStartTime) throws IOException {
372     // build path as it should be in the archive
373     String filename = currentFile.getName();
374     Path archiveFile = new Path(archiveDir, filename);
375     FileSystem fs = currentFile.getFileSystem();
376 
377     // if the file already exists in the archive, move that one to a timestamped backup. This is a
378     // really, really unlikely situtation, where we get the same name for the existing file, but
379     // is included just for that 1 in trillion chance.
380     if (fs.exists(archiveFile)) {
381       if (LOG.isDebugEnabled()) {
382         LOG.debug("File:" + archiveFile + " already exists in archive, moving to "
383             + "timestamped backup and overwriting current.");
384       }
385 
386       // move the archive file to the stamped backup
387       Path backedupArchiveFile = new Path(archiveDir, filename + SEPARATOR + archiveStartTime);
388       if (!fs.rename(archiveFile, backedupArchiveFile)) {
389         LOG.error("Could not rename archive file to backup: " + backedupArchiveFile
390             + ", deleting existing file in favor of newer.");
391         // try to delete the exisiting file, if we can't rename it
392         if (!fs.delete(archiveFile, false)) {
393           throw new IOException("Couldn't delete existing archive file (" + archiveFile
394               + ") or rename it to the backup file (" + backedupArchiveFile
395               + ") to make room for similarly named file.");
396         }
397       }
398       LOG.debug("Backed up archive file from: " + archiveFile);
399     }
400 
401     if (LOG.isTraceEnabled()) {
402       LOG.trace("No existing file in archive for: " + archiveFile +
403         ", free to archive original file.");
404     }
405 
406     // at this point, we should have a free spot for the archive file
407     boolean success = false;
408     for (int i = 0; !success && i < DEFAULT_RETRIES_NUMBER; ++i) {
409       if (i > 0) {
410         // Ensure that the archive directory exists.
411         // The previous "move to archive" operation has failed probably because
412         // the cleaner has removed our archive directory (HBASE-7643).
413         // (we're in a retry loop, so don't worry too much about the exception)
414         try {
415           if (!fs.exists(archiveDir)) {
416             if (fs.mkdirs(archiveDir)) {
417               LOG.debug("Created archive directory:" + archiveDir);
418             }
419           }
420         } catch (IOException e) {
421           LOG.warn("Failed to create the archive directory: " + archiveDir, e);
422         }
423       }
424 
425       try {
426         success = currentFile.moveAndClose(archiveFile);
427       } catch (IOException e) {
428         LOG.warn("Failed to archive file: " + currentFile + " on try #" + i, e);
429         success = false;
430       }
431     }
432 
433     if (!success) {
434       LOG.error("Failed to archive file:" + currentFile);
435       return false;
436     }
437 
438     if (LOG.isDebugEnabled()) {
439       LOG.debug("Finished archiving file from: " + currentFile + ", to: " + archiveFile);
440     }
441     return true;
442   }
443 
444   /**
445    * Without regard for backup, delete a region. Should be used with caution.
446    * @param regionDir {@link Path} to the region to be deleted.
447    * @param fs FileSystem from which to delete the region
448    * @return <tt>true</tt> on successful deletion, <tt>false</tt> otherwise
449    * @throws IOException on filesystem operation failure
450    */
451   private static boolean deleteRegionWithoutArchiving(FileSystem fs, Path regionDir)
452       throws IOException {
453     if (fs.delete(regionDir, true)) {
454       LOG.debug("Deleted all region files in: " + regionDir);
455       return true;
456     }
457     LOG.debug("Failed to delete region directory:" + regionDir);
458     return false;
459   }
460 
461   /**
462    * Just do a simple delete of the given store files
463    * <p>
464    * A best effort is made to delete each of the files, rather than bailing on the first failure.
465    * <p>
466    * This method is preferable to {@link #deleteFilesWithoutArchiving(Collection)} since it consumes
467    * less resources, but is limited in terms of usefulness
468    * @param compactedFiles store files to delete from the file system.
469    * @throws IOException if a file cannot be deleted. All files will be attempted to deleted before
470    *           throwing the exception, rather than failing at the first file.
471    */
472   private static void deleteStoreFilesWithoutArchiving(Collection<StoreFile> compactedFiles)
473       throws IOException {
474     LOG.debug("Deleting store files without archiving.");
475     List<IOException> errors = new ArrayList<IOException>(0);
476     for (StoreFile hsf : compactedFiles) {
477       try {
478         hsf.deleteReader();
479       } catch (IOException e) {
480         LOG.error("Failed to delete store file:" + hsf.getPath());
481         errors.add(e);
482       }
483     }
484     if (errors.size() > 0) {
485       throw MultipleIOException.createIOException(errors);
486     }
487   }
488 
489   /**
490    * Adapt a type to match the {@link File} interface, which is used internally for handling
491    * archival/removal of files
492    * @param <T> type to adapt to the {@link File} interface
493    */
494   private static abstract class FileConverter<T> implements Function<T, File> {
495     protected final FileSystem fs;
496 
497     public FileConverter(FileSystem fs) {
498       this.fs = fs;
499     }
500   }
501 
502   /**
503    * Convert a FileStatus to something we can manage in the archiving
504    */
505   private static class FileStatusConverter extends FileConverter<FileStatus> {
506     public FileStatusConverter(FileSystem fs) {
507       super(fs);
508     }
509 
510     @Override
511     public File apply(FileStatus input) {
512       return new FileablePath(fs, input.getPath());
513     }
514   }
515 
516   /**
517    * Convert the {@link StoreFile} into something we can manage in the archive
518    * methods
519    */
520   private static class StoreToFile extends FileConverter<StoreFile> {
521     public StoreToFile(FileSystem fs) {
522       super(fs);
523     }
524 
525     @Override
526     public File apply(StoreFile input) {
527       return new FileableStoreFile(fs, input);
528     }
529   }
530 
531   /**
532    * Wrapper to handle file operations uniformly
533    */
534   private static abstract class File {
535     protected final FileSystem fs;
536 
537     public File(FileSystem fs) {
538       this.fs = fs;
539     }
540 
541     /**
542      * Delete the file
543      * @throws IOException on failure
544      */
545     abstract void delete() throws IOException;
546 
547     /**
548      * Check to see if this is a file or a directory
549      * @return <tt>true</tt> if it is a file, <tt>false</tt> otherwise
550      * @throws IOException on {@link FileSystem} connection error
551      */
552     abstract boolean isFile() throws IOException;
553 
554     /**
555      * @return if this is a directory, returns all the children in the
556      *         directory, otherwise returns an empty list
557      * @throws IOException
558      */
559     abstract Collection<File> getChildren() throws IOException;
560 
561     /**
562      * close any outside readers of the file
563      * @throws IOException
564      */
565     abstract void close() throws IOException;
566 
567     /**
568      * @return the name of the file (not the full fs path, just the individual
569      *         file name)
570      */
571     abstract String getName();
572 
573     /**
574      * @return the path to this file
575      */
576     abstract Path getPath();
577 
578     /**
579      * Move the file to the given destination
580      * @param dest
581      * @return <tt>true</tt> on success
582      * @throws IOException
583      */
584     public boolean moveAndClose(Path dest) throws IOException {
585       this.close();
586       Path p = this.getPath();
587       return FSUtils.renameAndSetModifyTime(fs, p, dest);
588     }
589 
590     /**
591      * @return the {@link FileSystem} on which this file resides
592      */
593     public FileSystem getFileSystem() {
594       return this.fs;
595     }
596 
597     @Override
598     public String toString() {
599       return this.getClass() + ", file:" + getPath().toString();
600     }
601   }
602 
603   /**
604    * A {@link File} that wraps a simple {@link Path} on a {@link FileSystem}.
605    */
606   private static class FileablePath extends File {
607     private final Path file;
608     private final FileStatusConverter getAsFile;
609 
610     public FileablePath(FileSystem fs, Path file) {
611       super(fs);
612       this.file = file;
613       this.getAsFile = new FileStatusConverter(fs);
614     }
615 
616     @Override
617     public void delete() throws IOException {
618       if (!fs.delete(file, true)) throw new IOException("Failed to delete:" + this.file);
619     }
620 
621     @Override
622     public String getName() {
623       return file.getName();
624     }
625 
626     @Override
627     public Collection<File> getChildren() throws IOException {
628       if (fs.isFile(file)) return Collections.emptyList();
629       return Collections2.transform(Arrays.asList(fs.listStatus(file)), getAsFile);
630     }
631 
632     @Override
633     public boolean isFile() throws IOException {
634       return fs.isFile(file);
635     }
636 
637     @Override
638     public void close() throws IOException {
639       // NOOP - files are implicitly closed on removal
640     }
641 
642     @Override
643     Path getPath() {
644       return file;
645     }
646   }
647 
648   /**
649    * {@link File} adapter for a {@link StoreFile} living on a {@link FileSystem}
650    * .
651    */
652   private static class FileableStoreFile extends File {
653     StoreFile file;
654 
655     public FileableStoreFile(FileSystem fs, StoreFile store) {
656       super(fs);
657       this.file = store;
658     }
659 
660     @Override
661     public void delete() throws IOException {
662       file.deleteReader();
663     }
664 
665     @Override
666     public String getName() {
667       return file.getPath().getName();
668     }
669 
670     @Override
671     public boolean isFile() {
672       return true;
673     }
674 
675     @Override
676     public Collection<File> getChildren() throws IOException {
677       // storefiles don't have children
678       return Collections.emptyList();
679     }
680 
681     @Override
682     public void close() throws IOException {
683       file.closeReader(true);
684     }
685 
686     @Override
687     Path getPath() {
688       return file.getPath();
689     }
690   }
691 }