简体   繁体   English

在 Java 中获取最新(降序上次修改)n 个文件的最佳优化方法是什么 - 无需加载大目录的所有文件

[英]what is best optimized way in Java to get latest (Descending Last-Modified) n files - without loading all files of a large directory

Aim is to get latest 100 files.目的是获取最新的 100 个文件。 Currently it is done by scanning all files - preparing a files list - and then apply sort+limit.目前它是通过扫描所有文件 - 准备文件列表 - 然后应用排序+限制来完成的。

this is very slow - in cases when directory is too large.这非常慢 - 在目录太大的情况下。 So is there any way or API available which does this without loading full file list.那么是否有任何方法或 API 可以在不加载完整文件列表的情况下执行此操作。

Currently following three approaches do not give satisfactory performance when files are in range of few thousands.当前,当文件在数千个范围内时,以下三种方法不能提供令人满意的性能。

  • Files.listFiles - Java 1.2 Files.listFiles - Java 1.2
  • DirectoryStream - Java 1.7 DirectoryStream - Java 1.7
  • Files.Walk - Java 1.8 Files.Walk - Java 1.8

You have to look at the attributes of each file to find its age, and you have to look at them all to find the N newest.你必须查看每个文件的属性才能找到它的年龄,你必须查看所有文件才能找到N 个最新的。

Your only freedom of choice is in how you do the looking.你唯一的选择的自由是你如何看的。 There's no need to read the file contents, for example.例如,无需读取文件内容。

I'd consider using Files.find().我会考虑使用 Files.find()。 This appears from its documentation to do the minimum work required.这从它的文档中可以看出做所需的最少工作。

You don't need to save all files.您不需要保存所有文件。 Track the oldest of the newest 100 seen.跟踪最新的 100 个中最旧的。 If the 'next' file is older than that, you don't need to keep it.如果“下一个”文件早于该文件,则无需保留它。 Otherwise you have to figure out which of the 100 to discard.否则,您必须弄清楚要丢弃 100 个中的哪一个。 This trades off overhead of keeping an entire list for overhead of deciding what to discard.这将保留整个列表的开销与决定丢弃什么的开销进行权衡。 It could work in your favour if the number of files is much larger than 100.如果文件数量远大于 100,它可能对您有利。

To some extent the overhead is file-system dependent.在某种程度上,开销取决于文件系统。 If the last-modified time is stored in the directory entry then there's no need to look at the inode to get it.如果上次修改时间存储在目录条目中,则无需查看 inode 即可获取它。 That's not under your control, of course.当然,这不在你的控制之下。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM