Skip to content

Commit

Permalink
Merge pull request #1171 from tdrwenski/fix-controllers3-filtering-su…
Browse files Browse the repository at this point in the history
…bdirs

Apply filters to S3Controller getSubdirs
  • Loading branch information
haileyajohnson committed Apr 26, 2023
2 parents 3524a0e + 5cc767a commit 3c42435
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ public Iterator<MFile> getSubdirs(CollectionConfig mc, boolean recheck) {
logger.error("Error creating MFile for {} bucket {}", commonPrefix, initialUri.getBucket(), e);
}
}
return mFiles.iterator();
return new FilteredIterator(mc, mFiles.iterator(), true);
}

@Override
Expand Down
55 changes: 52 additions & 3 deletions cdm/s3/src/test/java/thredds/filesystem/s3/TestControllerS3.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
import software.amazon.awssdk.regions.Region;
import thredds.inventory.CollectionConfig;
import thredds.inventory.MFile;
import thredds.inventory.MFileFilter;
import thredds.inventory.filter.WildcardMatchOnName;
import ucar.unidata.io.s3.CdmS3Uri;
import ucar.unidata.io.s3.S3TestsCommon;
import ucar.unidata.io.s3.TestS3Read;
Expand Down Expand Up @@ -275,6 +277,41 @@ public void testGetSubdirsWithoutDelimiterOsdc() throws URISyntaxException {
checkSubdirsCount(uri, 0);
}

@Test
public void shouldFilterTopFiles() throws URISyntaxException {
final CdmS3Uri uri = new CdmS3Uri(S3TestsCommon.THREDDS_TEST_BUCKET + "?test-dataset-scan/" + DELIMITER_FRAGMENT);

final CollectionConfig noFilter = new CollectionConfig(uri.getBucket(), uri.toString(), true, null, null);
assertThat(topInventoryCount(noFilter)).isEqualTo(3);

final MFileFilter filter = new WildcardMatchOnName("*.nc$");
final CollectionConfig withFilter = new CollectionConfig(uri.getBucket(), uri.toString(), true, filter, null);
assertThat(topInventoryCount(withFilter)).isEqualTo(2);
}

@Test
public void shouldFilterAllFiles() throws URISyntaxException {
final CdmS3Uri uri = new CdmS3Uri(S3TestsCommon.THREDDS_TEST_BUCKET + "?test-dataset-scan/" + DELIMITER_FRAGMENT);

final CollectionConfig noFilter = new CollectionConfig(uri.getBucket(), uri.toString(), true, null, null);
checkInventoryAllCount(noFilter, 8);

final MFileFilter filter = new WildcardMatchOnName("*.nc$");
final CollectionConfig withFilter = new CollectionConfig(uri.getBucket(), uri.toString(), true, filter, null);
checkInventoryAllCount(withFilter, 4);
}

@Test
public void shouldFilterSubDirs() throws URISyntaxException {
final CdmS3Uri uri = new CdmS3Uri(S3TestsCommon.THREDDS_TEST_BUCKET + "?test-dataset-scan/" + DELIMITER_FRAGMENT);
final CollectionConfig noFilter = new CollectionConfig(uri.getBucket(), uri.toString(), true, null, null);
checkSubdirsCount(noFilter, 2);

final MFileFilter filter = new WildcardMatchOnName("sub-dir");
final CollectionConfig withFilter = new CollectionConfig(uri.getBucket(), uri.toString(), true, filter, null);
checkSubdirsCount(withFilter, 1);
}

@AfterClass
public static void teardown() {
System.clearProperty(AWS_REGION_PROP_NAME);
Expand All @@ -298,25 +335,37 @@ private void checkInventoryTopCountAtMost(CdmS3Uri uri, int expectedMaximumCount

private int topInventoryCount(CdmS3Uri uri) {
logger.debug("getInventoryTop: {}", uri);
return topInventoryCount(getCollectionConfig(uri));
}

private int topInventoryCount(CollectionConfig collectionConfig) {
ControllerS3 controller = new ControllerS3();
controller.limit = true;
Iterator<MFile> it = controller.getInventoryTop(getCollectionConfig(uri), false);
Iterator<MFile> it = controller.getInventoryTop(collectionConfig, false);
return countObjects(it);
}

private void checkInventoryAllCount(CdmS3Uri uri, int expectedCount) {
logger.debug("getInventoryAll: {}", uri);
checkInventoryAllCount(getCollectionConfig(uri), expectedCount);
}

private void checkInventoryAllCount(CollectionConfig collectionConfig, int expectedCount) {
ControllerS3 controller = new ControllerS3();
controller.limit = true;
Iterator<MFile> it = controller.getInventoryAll(getCollectionConfig(uri), false);
Iterator<MFile> it = controller.getInventoryAll(collectionConfig, false);
assertThat(countObjects(it)).isEqualTo(expectedCount);
}

private void checkSubdirsCount(CdmS3Uri uri, int expectedCount) {
logger.debug("getSubdirs: {}", uri);
checkSubdirsCount(getCollectionConfig(uri), expectedCount);
}

private void checkSubdirsCount(CollectionConfig collectionConfig, int expectedCount) {
ControllerS3 controller = new ControllerS3();
controller.limit = true;
Iterator<MFile> it = controller.getSubdirs(getCollectionConfig(uri), false);
Iterator<MFile> it = controller.getSubdirs(collectionConfig, false);
assertThat(countObjects(it)).isEqualTo(expectedCount);
}

Expand Down
1 change: 1 addition & 0 deletions cdm/s3/src/test/java/ucar/unidata/io/s3/S3TestsCommon.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,5 @@ public class S3TestsCommon {
public static final String TOP_LEVEL_GCS_BUCKET = "cdms3://storage.googleapis.com/gcp-public-data-goes-16";
public static final String TOP_LEVEL_OSDC_BUCKET =
"cdms3://griffin-objstore.opensciencedatacloud.org/noaa-goes16-hurricane-archive-2017";
public static final String THREDDS_TEST_BUCKET = "cdms3:thredds-test-data";
}

0 comments on commit 3c42435

Please sign in to comment.