Skip to content

Commit

Permalink
performance enhancement on listing files in DirScan
Browse files Browse the repository at this point in the history
  • Loading branch information
optyfr committed Feb 17, 2024
1 parent f2b62b6 commit bec4a6a
Showing 1 changed file with 34 additions and 32 deletions.
66 changes: 34 additions & 32 deletions jrmcore/src/main/java/jrm/profile/scan/DirScan.java
Original file line number Diff line number Diff line change
Expand Up @@ -373,13 +373,11 @@ private DirScan(final Session session, final File dir, final ProgressHandler han
if(!Files.isDirectory(path))
return;


/*
* Initialize progression
*/
handler.clearInfos();
handler.setInfos(options.nThreads,null);


listFiles(dir, handler, path, options);

Expand Down Expand Up @@ -484,28 +482,42 @@ private void listFiles(final File dir, final ProgressHandler handler, final Path
* We go up to 100 subdirs for src dir but 1 level for dest dir type, and we follow links
*/

try(Stream<Path> stream = Files.walk(path, options.isDest ? 1 : 100, FileVisitOption.FOLLOW_LINKS))
{
handler.setProgress(String.format(Messages.getString("DirScan.ListingFiles"), getRelativePath(dir.toPath())));

try {
final var i = new AtomicInteger();
// handler.setProgress(String.format(Messages.getString("DirScan.ListingFiles"), getRelativePath(dir.toPath())), 0); //$NON-NLS-1$
// handler.setProgress2("", null); //$NON-NLS-1$
handler.setProgress(null, -1);
handler.setProgress2(String.format(Messages.getString("DirScan.ListingFiles"), getRelativePath(dir.toPath())), 0, 100); //$NON-NLS-1$

try (final var mt = new MultiThreading<Path>(options.nThreads, p -> {

Files.walkFileTree(path, Collections.singleton(FileVisitOption.FOLLOW_LINKS), options.isDest ? 1 : 100, listFilesVisitor(dir, handler, path, options, i));
/*
* Remove files from cache that are not in up2date state, because that mean that
* those files were removed from FS since the previous scan
*/
containersByName.entrySet().removeIf(entry -> !entry.getValue().isUp2date());
} catch (IOException e) {
Log.err("IOException when listing", e); //$NON-NLS-1$
} catch (final Exception e) {
Log.err("Other Exception when listing", e); //$NON-NLS-1$
}

}

private SimpleFileVisitor<Path> listFilesVisitor(final File dir, final ProgressHandler handler, final Path path,
final ScanOptions options, final AtomicInteger i) {
return new SimpleFileVisitor<Path>() {
@Override
public FileVisitResult visitFile(Path p, BasicFileAttributes attrs) throws IOException {
if (handler.isCancel())
return;
return FileVisitResult.TERMINATE;
if (path.equals(p))
return;
return FileVisitResult.CONTINUE;
final var file = p.toFile();
try {
final BasicFileAttributes attr = Files.readAttributes(p, BasicFileAttributes.class);
if (options.isDest) {
if (exclusions.stream().anyMatch(pm -> pm.matches(p)))
return;
listFilesDest(file, attr);
return FileVisitResult.CONTINUE;
listFilesDest(file, attrs);
} else
listFilesSrc(path, p, file, attr, options);
listFilesSrc(path, p, file, attrs, options);
handler.setProgress(path.relativize(p).toString(), -1); // $NON-NLS-1$
handler.setProgress2(String.format(Messages.getString("DirScan.ListingFiles2"), //$NON-NLS-1$
getRelativePath(dir.toPath()), i.incrementAndGet()), 0);
Expand All @@ -514,23 +526,13 @@ private void listFiles(final File dir, final ProgressHandler handler, final Path
} catch (final BreakException e) {
handler.doCancel();
}
return;
})) {
mt.start(stream);

handler.setProgress(path.relativize(p).toString(), -1); // $NON-NLS-1$
handler.setProgress2(String.format(Messages.getString("DirScan.ListingFiles2"), //$NON-NLS-1$
getRelativePath(dir.toPath()), i.incrementAndGet()), 0);
return FileVisitResult.CONTINUE;
}
/*
* Remove files from cache that are not in up2date state, because that mean that those files were removed from FS since the previous scan
*/
containersByName.entrySet().removeIf(entry -> !entry.getValue().isUp2date());
}
catch(final IOException e)
{
Log.err("IOException when listing", e); //$NON-NLS-1$
}
catch(final Exception e)
{
Log.err("Other Exception when listing", e); //$NON-NLS-1$
}
};
}

/**
Expand Down

0 comments on commit bec4a6a

Please sign in to comment.