Search code examples
niojava-ionfsfile-attributes

Read file attibutes for all files in the tree using as few IO operation as possible


I have a lot of small files on a NFS drive (Amazon EFS in my case). Files are provided over HTTP protocol, very similar to a classical Web-Server it does. As I need to validate the last modification of the file, it takes at least a single I/O per file request. It is a case even if I already cached the file body in RAM.

Is there a way to read the last modify attribute for all the files in the tree (or at least in a single directory) using only a single I/O operation?

There it a method Files.readAttributes which reads multiple attributes of a single file as a bulk operation. I am looking for bulk operation to read a single attribute of multiple files.

UPDATE: in case of NFS this question is how to utilize NFS command READDIRPLUS. This command does exactly what I need, but it seems to be no way to use it out of Java I/O library.


Solution

  • I don't know of a standard Java class to list all the files and modified time in one operation, but if you are permitted to utilise the host environment and the NFS drive is mount you could adapt the following technique to suit your environment:

    ProcessBuilder listFiles = new ProcessBuilder("bash", "", "ls -l");
    Process p = listFiles.start();
    BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream()));
    String inputLine;
    List<String> filesWithAttributes = new ArrayList<String>();
    
    while ((inputLine = reader.readLine()) != null) {
        filesWithAttributes.add(inputLine);
    }