Java Code Examples for org.apache.hadoop.fs.FileStatus#compareTo()

The following examples show how to use org.apache.hadoop.fs.FileStatus#compareTo() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: FSTableDescriptors.java    From hbase with Apache License 2.0 4 votes vote down vote up
@Override
public int compare(FileStatus left, FileStatus right) {
  return right.compareTo(left);
}
 
Example 2
Source File: GoogleHadoopFileSystemBase.java    From hadoop-connectors with Apache License 2.0 4 votes vote down vote up
private FileStatus[] flatGlobInternal(Path fixedPath, PathFilter filter) throws IOException {
  String pathString = fixedPath.toString();
  String prefixString = trimToPrefixWithoutGlob(pathString);
  Path prefixPath = new Path(prefixString);
  URI prefixUri = getGcsPath(prefixPath);

  if (prefixString.endsWith("/") && !prefixPath.toString().endsWith("/")) {
    // Path strips a trailing slash unless it's the 'root' path. We want to keep the trailing
    // slash so that we don't wastefully list sibling files which may match the directory-name
    // as a strict prefix but would've been omitted due to not containing the '/' at the end.
    prefixUri = UriPaths.toDirectory(prefixUri);
  }

  // Get everything matching the non-glob prefix.
  logger.atFinest().log("Listing everything with '%s' prefix", prefixUri);
  List<FileStatus> matchedStatuses = null;
  String pageToken = null;
  do {
    ListPage<FileInfo> infoPage = getGcsFs().listAllFileInfoForPrefixPage(prefixUri, pageToken);

    // TODO: Are implicit directories really always needed for globbing?
    //  Probably they should be inferred only when fs.gs.implicit.dir.infer.enable is true.
    Collection<FileStatus> statusPage =
        toFileStatusesWithImplicitDirectories(infoPage.getItems());

    // TODO: refactor to use GlobPattern and PathFilter directly without helper FS
    FileSystem helperFileSystem =
        InMemoryGlobberFileSystem.createInstance(getConf(), getWorkingDirectory(), statusPage);
    FileStatus[] matchedStatusPage = helperFileSystem.globStatus(fixedPath, filter);
    if (matchedStatusPage != null) {
      Collections.addAll(
          (matchedStatuses == null ? matchedStatuses = new ArrayList<>() : matchedStatuses),
          matchedStatusPage);
    }

    pageToken = infoPage.getNextPageToken();
  } while (pageToken != null);

  if (matchedStatuses == null || matchedStatuses.isEmpty()) {
    return matchedStatuses == null ? null : new FileStatus[0];
  }

  matchedStatuses.sort(
      ((Comparator<FileStatus>) Comparator.<FileStatus>naturalOrder())
          // Place duplicate implicit directories after real directory
          .thenComparingInt((FileStatus f) -> isImplicitDirectory(f) ? 1 : 0));

  // Remove duplicate file statuses that could be in the matchedStatuses
  // because of pagination and implicit directories
  List<FileStatus> filteredStatuses = new ArrayList<>(matchedStatuses.size());
  FileStatus lastAdded = null;
  for (FileStatus fileStatus : matchedStatuses) {
    if (lastAdded == null || lastAdded.compareTo(fileStatus) != 0) {
      filteredStatuses.add(fileStatus);
      lastAdded = fileStatus;
    }
  }

  return filteredStatuses.toArray(new FileStatus[0]);
}