VfsCache extends ForwardingFileSystem { @Override public DirectoryStream list(Inode inode, byte[] verifier, long cookie) throws IOException { InodeCacheEntry cacheKey; if (cookie == 0L && Arrays.equals(verifier, DirectoryStream.ZERO_VERIFIER)) { cacheKey = new InodeCacheEntry(inode, delegate().directoryVerifier(inode)); } else { cacheKey = new InodeCacheEntry(inode, verifier); } DirectoryStream directoryStream = _readdirCache.getIfPresent(cacheKey); if (directoryStream == null) { directoryStream = delegate().list(inode, DirectoryStream.ZERO_VERIFIER, 0L); cacheKey = new InodeCacheEntry(inode, directoryStream.getVerifier()); _readdirCache.put(cacheKey, directoryStream); } return directoryStream.tail(cookie); } VfsCache(VirtualFileSystem inner, VfsCacheConfig cacheConfig); @Override void commit(Inode inode, long offset, int count); @Override Inode symlink(Inode parent, String path, String link, Subject subject, int mode); @Override void remove(Inode parent, String path); @Override Inode parentOf(Inode inode); @Override boolean move(Inode src, String oldName, Inode dest, String newName); @Override Inode mkdir(Inode parent, String path, Subject subject, int mode); @Override Inode link(Inode parent, Inode link, String path, Subject subject); @Override Inode lookup(Inode parent, String path); @Override FsStat getFsStat(); @Override Inode create(Inode parent, Stat.Type type, String path, Subject subject, int mode); @Override Stat getattr(Inode inode); @Override void setattr(Inode inode, Stat stat); @Override boolean getCaseInsensitive(); @Override boolean getCasePreserving(); void invalidateLookupCache(Inode parent, String path); void invalidateStatCache(final Inode inode); @Override DirectoryStream list(Inode inode, byte[] verifier, long cookie); @Override void removeXattr(Inode inode, String attr); @Override void setXattr(Inode inode, String attr, byte[] value, SetXattrMode mode);  }
@Test public void shouldReadThroughOnEmptyReaddirCache() throws IOException { vfsCache.list(root, DirectoryStream.ZERO_VERIFIER, 0L); verify(vfs).list(root, DirectoryStream.ZERO_VERIFIER, 0L); }
@Test public void shouldUseReaddirCache() throws IOException { vfsCache.list(root, DirectoryStream.ZERO_VERIFIER, 0L); vfsCache.list(root, DirectoryStream.ZERO_VERIFIER, 0L); verify(vfs, times(1)).list(root, DirectoryStream.ZERO_VERIFIER, 0L); }
@Test public void shouldUseReaddirCacheWithCookie() throws IOException { DirectoryStream stream = vfsCache.list(root, DirectoryStream.ZERO_VERIFIER, 0L); vfsCache.list(root, stream.getVerifier(), 5L); verify(vfs, times(1)).list(root, DirectoryStream.ZERO_VERIFIER, 0L); }
@Test public void shouldReadThroughReaddirCacheOnUnknownVerifier() throws IOException { vfsCache.list(root, DirectoryStream.ZERO_VERIFIER, 0L); vfsCache.list(root, Arrays.copyOf(new byte[]{ 0x01}, nfs4_prot.NFS4_VERIFIER_SIZE), 0L); verify(vfs, times(2)).list(root, DirectoryStream.ZERO_VERIFIER, 0L); }