如何从文件系统获取文件属性流?

Hen*_*ory 5 java performance scala akka java-stream

我正在编写一个 Web 服务器,并试图确保尽可能高效,最大限度地减少文件系统调用。问题是返回 Streams 的方法,例如java.nio.file.Files.list返回 Stream of Paths,我想要一个 Stream of BasicFileAttributes,这样我就可以返回每个 Path 的创建时间和更新时间(关于返回LDP Container的结果)。

当然,一个简单的解决方案是使用map一个函数来处理 Stream 的每个元素,该函数接受路径并返回文件属性(p: Path) => Files.getAttributeView... ,但这听起来像是会为每个 Path 调用 FS,这看起来很浪费,因为要获取JDK中的文件信息不可能与属性信息相差太远。

实际上,我在2009 年 OpenJDK 邮件列表中看到了这封邮件,其中指出他们已经讨论了添加一个将返回一对路径和属性的 API...

我在 JDK 上发现了一个非公共类java.nio.file.FileTreeWalker,它有一个 api,允许人们获取属性FileTreeWalker.Event。这实际上利用了sun.nio.fs.BasicFileAttributesHolder允许路径保存属性的缓存。但它不是公开的,也不清楚它在哪里发挥作用。

当然还有整个FileVisitor API,它具有返回 aPath和 的方法BasicFileAttributes,如下所示:

public FileVisitResult visitFile(Path file, BasicFileAttributes attr) {...}
Run Code Online (Sandbox Code Playgroud)

所以我正在寻找是否有一种方法可以将其变成一个 Stream,它尊重Akka推动的Reactive Manifesto的背压原则,而不占用太多资源。我检查了开源Alpakka File项目,但这也正在流式传输返回s 的方法...FilesPath

Dun*_*ncG 2

您可以通过使用Files.find接受 BiPredicate<Path, BasicFileAttributes> 并在测试每个路径时存储值来访问文件属性及其路径。

BiPredicate 内部的副作用操作将启用对两个对象的操作,而无需接触路径中每个项目的文件系统。根据您的谓词条件yourPred,下面的副作用predicate将收集属性供您在流处理中检索:

public static void main(String[] args) throws IOException {
    Path dir = Path.of(args[0]);

    // Use `ConcurrentHashMap` if using `stream.parallel()`
    HashMap <Path,BasicFileAttributes> attrs = new HashMap<>();

    BiPredicate<Path, BasicFileAttributes> yourPred = (p,a) -> true;

    BiPredicate<Path, BasicFileAttributes> predicate = (p,a) -> {
        return yourPred.test(p, a)
                // && p.getNameCount() == dir.getNameCount()+1 // Simulates Files.list
                && attrs.put(p, a) == null;
    };
    try(var stream = Files.find(dir, Integer.MAX_VALUE, predicate)) {
        stream.forEach(p-> System.out.println(p.toString()+" => "+attrs.get(p)));
        // Or: if your put all your handling code in the predicate use stream.count();
    }
}
Run Code Online (Sandbox Code Playgroud)

File.list要模拟使用一级扫描仪的效果find

 BiPredicate<Path, BasicFileAttributes> yourPred = (p,a) -> p.getNameCount() == dir.getNameCount()+1;
Run Code Online (Sandbox Code Playgroud)

attrs.remove(p);对于大型文件夹扫描,您应该通过在消耗路径后插入来清理 attrs 映射。

编辑

上面的答案可以重构为返回流的 3 行调用Map.Entry<Path, BasicFileAttributes>,或者很容易添加一个类/记录来保存 Path/BasicFileAttribute 对并返回Stream<PathInfo>

/**
 * Call Files.find() returning a stream with both Path+BasicFileAttributes
 * as type Map.Entry<Path, BasicFileAttributes>
 * <p>Could declare a specific record to replace Map.Entry as:
 *    record PathInfo(Path path, BasicFileAttributes attr) { };
 */
public static Stream<Map.Entry<Path, BasicFileAttributes>>
find(Path dir, int maxDepth, BiPredicate<Path, BasicFileAttributes> matcher, FileVisitOption... options) throws IOException {

    HashMap <Path,BasicFileAttributes> attrs = new HashMap<>();
    BiPredicate<Path, BasicFileAttributes> predicate = (p,a) -> (matcher == null || matcher.test(p, a)) && attrs.put(p, a) == null;

    return Files.find(dir, maxDepth, predicate, options).map(p -> Map.entry(p, attrs.remove(p)));
}
Run Code Online (Sandbox Code Playgroud)