Skip to content

Commit

Permalink
feat: Support filtered DirectoryStream (#381)
Browse files Browse the repository at this point in the history
* Add support for filtered DirectoryStreams.
* Add unit test

---------

Co-authored-by: Marcel Hekman <[email protected]>
  • Loading branch information
ennoruijters and Marcel Hekman authored Sep 28, 2023
1 parent 71640bd commit f6668ba
Show file tree
Hide file tree
Showing 3 changed files with 131 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -512,7 +512,7 @@ public void close()
@Override
public Iterator<Path> iterator()
{
return new S3Iterator(s3Path);
return new S3FilteredIterator(s3Path, filter);
}
};
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package org.carlspring.cloud.storage.s3fs;

import java.io.IOException;
import java.nio.file.DirectoryStream.Filter;
import java.nio.file.Path;
import java.util.Iterator;
import java.util.NoSuchElementException;

public class S3FilteredIterator implements Iterator<Path>
{

private final S3Iterator s3iterator;

private final Filter<? super Path> filter;

private Path cursor;

private boolean cursorIsCurrent;

public S3FilteredIterator(S3Path s3Path,
Filter<? super Path> filter)
{
this.filter = filter;
this.s3iterator = new S3Iterator(s3Path);
clearCursor();
}

@Override
public boolean hasNext()
{
if (!cursorIsCurrent)
{
findNextFiltered();
}
return cursorIsCurrent;
}

@Override
public Path next()
{
if (!hasNext())
{
throw new NoSuchElementException();
}
Path next = cursor;
clearCursor();
return next;
}

private void findNextFiltered()
{
try
{
while (s3iterator.hasNext())
{
S3Path next = s3iterator.next();
if (filter.accept(next))
{
cursor = next;
cursorIsCurrent = true;
return;
}
}
clearCursor();
}
catch (IOException e)
{
throw new RuntimeException(e);
}
}

private void clearCursor()
{
cursor = null;
cursorIsCurrent = false;
}

@Override
public void remove()
{
throw new UnsupportedOperationException();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,35 @@ void removeIteratorStreamDirectoryReader()
}
}

@Test
public void directoryStreamWithFilter()
throws IOException
{
// fixtures
S3ClientMock client = S3MockFactory.getS3ClientMock();
client.bucket("bucketA")
.dir("dir1") //
.file("dir/file1.txt", "content".getBytes()) //
.file("dir/file2.sql", "content".getBytes()) //
.file("dir/file3.txt", "content".getBytes()) //
.file("dir/file4.sql", "content".getBytes()) //
.file("dir/tmp_file.txt", "content".getBytes());

// act
Path base = createNewS3FileSystem().getPath("/bucketA", "dir");
DirectoryStream.Filter<Path> filter = entry -> {
String filename = entry.getFileName().toString();
return filename.startsWith("tmp_") || filename.endsWith(".sql");
};

// act
try (DirectoryStream<Path> dirStream = Files.newDirectoryStream(base, filter))
{
// assert
assertDirectoryStream(dirStream, "file2.sql", "file4.sql", "tmp_file.txt");
}
}

@Test
void list999Paths()
throws IOException
Expand Down Expand Up @@ -146,21 +175,27 @@ private void assertNewDirectoryStream(Path base,
{
try (DirectoryStream<Path> dir = Files.newDirectoryStream(base))
{
assertNotNull(dir);
assertNotNull(dir.iterator());
assertTrue(dir.iterator().hasNext());
assertDirectoryStream(dir, files);
}
}

Set<String> filesNamesExpected = new HashSet<>(Arrays.asList(files));
Set<String> filesNamesActual = new HashSet<>();
private void assertDirectoryStream(DirectoryStream<Path> dir,
final String... files)
{
assertNotNull(dir);
assertNotNull(dir.iterator());
assertTrue(dir.iterator().hasNext());

for (Path path : dir)
{
String fileName = path.getFileName().toString();
filesNamesActual.add(fileName);
}
Set<String> filesNamesExpected = new HashSet<>(Arrays.asList(files));
Set<String> filesNamesActual = new HashSet<>();

assertEquals(filesNamesExpected, filesNamesActual);
for (Path path : dir)
{
String fileName = path.getFileName().toString();
filesNamesActual.add(fileName);
}

assertEquals(filesNamesExpected, filesNamesActual);
}

/**
Expand Down

0 comments on commit f6668ba

Please sign in to comment.