Source:
<code>public Repo<Master> call(long tid, Master master) throws Exception { try { VolumeManager fs = master.getFileSystem(); Map<String,String> fileNameMappings = PopulateMetadataTable.readMappingFile(fs, tableInfo); FileStatus[] exportedFiles = fs.listStatus(new Path(tableInfo.exportDir)); FileStatus[] importedFiles = fs.listStatus(new Path(tableInfo.importDir)); <START> <END> for (String oldFileName : fileNameMappings.keySet()) { if (Arrays.stream(exportedFiles).filter(fstat -> fstat.getPath().getName().equals(oldFileName)).count() != 1) { if (Arrays.stream(importedFiles).filter(fstat -> fstat.getPath().getName().equals(fileNameMappings.get(oldFileName))).count() != 1) { throw new AcceptableThriftTableOperationException(tableInfo.tableId, tableInfo.tableName, TableOperation.IMPORT, TableOperationExceptionType.OTHER, "File referenced by exported table does not exist " + oldFileName); } } } for (FileStatus fileStatus : exportedFiles) { String newName = fileNameMappings.get(fileStatus.getPath().getName()); if (newName != null) fs.rename(fileStatus.getPath(), new Path(tableInfo.importDir, newName)); } return new FinishImportTable(tableInfo); } catch (IOException ioe) { log.warn("{}", ioe.getMessage(), ioe); throw new AcceptableThriftTableOperationException(tableInfo.tableId, tableInfo.tableName, TableOperation.IMPORT, TableOperationExceptionType.OTHER, "Error renaming files " + ioe.getMessage()); } }</code><technical_language>possibly avoid linear search in loop iteration loop. java Set<String> importing = Arrays.stream(exportedFiles).map(fstat -> fstat.getPath().getName()).map(fileNameMapping::get).collect(Collectors.toSet()); Set<String> imported = Arrays.stream(importedFiles).map(fstat -> fstat.getPath().getName()).collect(Collectors.toSet()); //ensure Sets.union(importing, imported) equals new HashSet<>(fileNameMappings.values())</technical_language>

Target:
public Repo<Master> call(long tid, Master master) throws Exception { try { VolumeManager fs = master.getFileSystem(); Map<String,String> fileNameMappings = PopulateMetadataTable.readMappingFile(fs, tableInfo); FileStatus[] exportedFiles = fs.listStatus(new Path(tableInfo.exportDir)); FileStatus[] importedFiles = fs.listStatus(new Path(tableInfo.importDir)); Function<FileStatus,String> fileStatusName = fstat -> fstat.getPath().getName(); Set<String> importing = Arrays.stream(exportedFiles).map(fileStatusName) .map(fileNameMappings::get).collect(Collectors.toSet()); Set<String> imported = Arrays.stream(importedFiles).map(fileStatusName).collect(Collectors.toSet()); if (log.isDebugEnabled()) { log.debug("Files already present in imported (target) directory: {}", imported.stream().collect(Collectors.joining(","))); } Set<String> missingFiles = Sets.difference(new HashSet<String>(fileNameMappings.values()), new HashSet<String>(Sets.union(importing, imported))); if (!missingFiles.isEmpty()) { throw new AcceptableThriftTableOperationException(tableInfo.tableId, tableInfo.tableName, TableOperation.IMPORT, TableOperationExceptionType.OTHER, "Missing source files corresponding to files " + missingFiles.stream().collect(Collectors.joining(","))); } for (FileStatus fileStatus : exportedFiles) { String newName = fileNameMappings.get(fileStatus.getPath().getName()); if (newName != null) { Path newPath = new Path(tableInfo.importDir, newName); log.debug("Renaming file to {}", fileStatus.getPath(), newPath); fs.rename(fileStatus.getPath(), newPath); } } return new FinishImportTable(tableInfo); } catch (IOException ioe) { log.warn("{}", ioe.getMessage(), ioe); throw new AcceptableThriftTableOperationException(tableInfo.tableId, tableInfo.tableName, TableOperation.IMPORT, TableOperationExceptionType.OTHER, "Error renaming files " + ioe.getMessage()); } }