Skip to content

Commit 3c0c0ec

Browse files
committed
Refactor GRIB collection handling to use MFile abstractions
* replace several file/path-based lookups with managed MFile-based access * update inventory controllers to expose a DirectoryStream<MFile> interface * handle GRIB index creation, caching, and temporary file naming with shared helpers
1 parent 1ff3f0c commit 3c0c0ec

File tree

31 files changed

+422
-78
lines changed

31 files changed

+422
-78
lines changed

cdm-test/src/test/java/ucar/nc2/grib/TestGribIndexLocationS3.java

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
/*
2+
* Copyright (c) 2023-2026 University Corporation for Atmospheric Research/Unidata
3+
* See LICENSE for license information.
4+
*/
5+
16
package ucar.nc2.grib;
27

38
import static com.google.common.truth.Truth.assertThat;
@@ -51,7 +56,7 @@ public static List<Object[]> getTestParameters() {
5156

5257
public TestGribIndexLocationS3(String filename, String fragment) {
5358
this.filename = filename + fragment;
54-
this.indexFilename = filename + GribIndex.GBX9_IDX;
59+
this.indexFilename = GribIndex.makeIndexFileName(filename);
5560
this.isGrib1 = filename.endsWith(".grib1");
5661
}
5762

@@ -86,7 +91,7 @@ public void shouldCreateIndexInDefaultCache() throws IOException {
8691
assertThat(index).isNotNull();
8792
assertThat(index.getNRecords()).isNotEqualTo(0);
8893

89-
assertThat(diskCache.getCacheFile(s3File.getPath() + GribIndex.GBX9_IDX).exists()).isTrue();
94+
assertThat(diskCache.getCacheFile(GribIndex.makeIndexFileName(s3File.getPath())).exists()).isTrue();
9095
}
9196

9297
@Test
@@ -98,13 +103,13 @@ public void shouldReuseCachedIndex() throws IOException {
98103
final GribIndex index =
99104
GribIndex.readOrCreateIndexFromSingleFile(isGrib1, s3File, CollectionUpdateType.always, logger);
100105
assertThat(index).isNotNull();
101-
assertThat(diskCache.getCacheFile(s3File.getPath() + GribIndex.GBX9_IDX).exists()).isTrue();
102-
final long cacheLastModified = diskCache.getCacheFile(s3File.getPath() + GribIndex.GBX9_IDX).lastModified();
106+
assertThat(diskCache.getCacheFile(GribIndex.makeIndexFileName(s3File.getPath())).exists()).isTrue();
107+
final long cacheLastModified = diskCache.getCacheFile(GribIndex.makeIndexFileName(s3File.getPath())).lastModified();
103108

104109
final GribIndex rereadIndex =
105110
GribIndex.readOrCreateIndexFromSingleFile(isGrib1, s3File, CollectionUpdateType.never, logger);
106111
assertThat(rereadIndex).isNotNull();
107-
final File rereadCachedIndex = diskCache.getCacheFile(s3File.getPath() + GribIndex.GBX9_IDX);
112+
final File rereadCachedIndex = diskCache.getCacheFile(GribIndex.makeIndexFileName(s3File.getPath()));
108113
assertThat(rereadCachedIndex.lastModified()).isEqualTo(cacheLastModified);
109114
}
110115
}

cdm/core/src/main/java/thredds/filesystem/ControllerOS.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,20 @@ public DirectoryStream<MFile> getSubdirs(CollectionConfig mc, boolean recheck) {
7878
return new MFileDirectoryStream(new FilteredIterator(mc, new MFileIterator(cd), true)); // return only subdirs
7979
}
8080

81+
@Nullable
82+
@Override
83+
public DirectoryStream<MFile> getFullInventoryAtLocation(String location) {
84+
if (location.startsWith("file:")) {
85+
location = location.substring(5);
86+
}
87+
88+
File cd = new File(location);
89+
if (!cd.exists())
90+
return null;
91+
if (!cd.isDirectory())
92+
return null;
93+
return new MFileDirectoryStream(new MFileIterator(cd));
94+
}
8195

8296
public void close() {} // NOOP
8397

cdm/core/src/main/java/thredds/filesystem/ControllerOS7.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
package thredds.filesystem;
77

8+
import javax.annotation.Nullable;
89
import thredds.inventory.CollectionConfig;
910
import thredds.inventory.MController;
1011
import thredds.inventory.MFile;
@@ -57,6 +58,27 @@ public DirectoryStream<MFile> getSubdirs(CollectionConfig mc, boolean recheck) {
5758
return null;
5859
}
5960

61+
@Nullable
62+
@Override
63+
public DirectoryStream<MFile> getFullInventoryAtLocation(String location) {
64+
if (location.startsWith("file:")) {
65+
location = location.substring(5);
66+
}
67+
68+
Path cd = Paths.get(location);
69+
if (!Files.exists(cd))
70+
return null;
71+
if (!Files.isDirectory(cd))
72+
return null;
73+
74+
MFileDirectoryStream stream = null;
75+
try {
76+
stream = new MFileDirectoryStream(new MFileIterator(cd, null));
77+
} catch (IOException ioe) {
78+
logger.warn(ioe.getMessage(), ioe);
79+
}
80+
return stream;
81+
}
6082

6183
public void close() {} // NOOP
6284

cdm/core/src/main/java/thredds/inventory/CollectionAbstract.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,11 @@ public String getCollectionName() {
121121

122122
@Override
123123
public String getIndexFilename(String suffix) {
124-
return getRoot() + "/" + collectionName + suffix;
124+
MFile rootMFile = MFiles.create(getRoot());
125+
MFile mfile = rootMFile.getChild(collectionName + suffix);
126+
if (mfile == null)
127+
throw new IllegalStateException("Cannot determine location of collection index file");
128+
return mfile.getPath();
125129
}
126130

127131
public void setStreamFilter(MFileFilter filter) {

cdm/core/src/main/java/thredds/inventory/CollectionList.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@ public CollectionList(String collectionName, String list, Logger logger) {
4242

4343
Collections.sort(mfiles);
4444
this.lastModified = lastModified;
45-
this.root = System.getProperty("user.dir");
4645
}
4746

4847
public CollectionList(String collectionName, String root, List<MFile> mfiles, Logger logger) {

cdm/core/src/main/java/thredds/inventory/CollectionManagerCatalog.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 1998-2018 University Corporation for Atmospheric Research/Unidata
2+
* Copyright (c) 1998-2026 University Corporation for Atmospheric Research/Unidata
33
* See LICENSE for license information.
44
*/
55

@@ -18,6 +18,7 @@
1818
import java.util.Date;
1919
import java.util.Formatter;
2020
import java.util.List;
21+
import ucar.nc2.util.DiskCache2;
2122

2223
/**
2324
* CollectionManager of datasets from a catalog.
@@ -45,7 +46,7 @@ public CollectionManagerCatalog(String collectionName, String collectionSpec, St
4546
}
4647

4748
this.catalogUrl = collectionSpec;
48-
this.root = System.getProperty("user.dir");
49+
this.root = DiskCache2.getDefault().getRootDirectory();
4950
}
5051

5152
@Override

cdm/core/src/main/java/thredds/inventory/CollectionSingleFile.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55

66
package thredds.inventory;
77

8+
import ucar.nc2.util.DiskCache2;
9+
810
/**
911
* A CollectionManager consisting of a single file
1012
*
@@ -18,9 +20,9 @@ public CollectionSingleFile(MFile file, org.slf4j.Logger logger) {
1820
mfiles.add(file);
1921
try {
2022
MFile p = file.getParent();
21-
this.root = p != null ? p.getPath() : System.getProperty("user.dir");
23+
this.root = p != null ? p.getPath() : DiskCache2.getDefault().getRootDirectory();
2224
} catch (java.io.IOException e) {
23-
this.root = System.getProperty("user.dir");
25+
this.root = DiskCache2.getDefault().getRootDirectory();
2426
}
2527

2628
this.lastModified = file.getLastModified();

cdm/core/src/main/java/thredds/inventory/CollectionSpecParser.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
/*
2-
* Copyright (c) 1998-2022 University Corporation for Atmospheric Research/Unidata
2+
* Copyright (c) 1998-2026 University Corporation for Atmospheric Research/Unidata
33
* See LICENSE for license information.
44
*/
5+
56
package thredds.inventory;
67

78
import java.io.File;
89
import java.util.Formatter;
10+
import ucar.nc2.util.DiskCache2;
911

1012
/**
1113
* Parses the collection specification string for local files.
@@ -39,7 +41,7 @@
3941
public class CollectionSpecParser extends CollectionSpecParserAbstract {
4042
private final static String DELIMITER = "/";
4143
private final static String FRAGMENT = "";
42-
private final static String DEFAULT_DIR = System.getProperty("user.dir");
44+
private final static String DEFAULT_DIR = DiskCache2.getDefault().getRootDirectory();
4345

4446
/**
4547
* Single spec : "/topdir/** /#dateFormatMark#regExp"

cdm/core/src/main/java/thredds/inventory/MController.java

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -48,15 +48,13 @@ public interface MController extends Closeable {
4848
DirectoryStream<MFile> getSubdirs(CollectionConfig mc, boolean recheck);
4949

5050
/**
51-
* Get an MFile for a specific location.
52-
*
53-
* @param location the location
54-
* @return MFile or null
51+
* Returns all subdirectories and leaves in a given location, not recursing into subdirectories.
52+
*
53+
* @param location defines the location to scan
54+
* @return DirectoryStream over Mfiles, or null if the location does not exist
5555
*/
5656
@Nullable
57-
default MFile getMFile(String location) {
58-
return MFiles.create(location);
59-
}
57+
DirectoryStream<MFile> getFullInventoryAtLocation(String location);
6058

6159
@Override
6260
void close();

cdm/core/src/main/java/thredds/inventory/MControllers.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,7 @@ public static MController create(String location) {
4343
*/
4444
public static DirectoryStream<MFile> newDirectoryStream(String location) throws IOException {
4545
MController controller = create(location);
46-
CollectionConfig config = new CollectionConfig(location, location, false, null, null);
47-
DirectoryStream<MFile> stream = controller.getInventoryTop(config, true);
46+
DirectoryStream<MFile> stream = controller.getFullInventoryAtLocation(location);
4847
controller.close();
4948
if (stream == null) {
5049
throw new IOException("Could not create DirectoryStream for " + location);

0 commit comments

Comments
 (0)