Skip to content

Commit 1ff3f0c

Browse files
committed
Refactor inventory management to use managed-file abstractions, part 2
* Transition GRIB collection abstractions from java.io/java.nio path APIs with the exception of writing GRIB Index files, which remain local-only for now.
1 parent e0d122d commit 1ff3f0c

26 files changed

+268
-212
lines changed

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 1998-2021 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

@@ -77,7 +77,7 @@ public static List<Object[]> getTestParameters() {
7777
public static void after(String dataDir, String newModel, FeatureCollectionConfig config, String varName, int orgLen,
7878
int remLen) {
7979
// cleanup index files created during test for a given parameter input
80-
cleanUpIndexFiles(GribCdmIndex.getTopIndexFileFromConfig(config).getParent());
80+
cleanUpIndexFiles(GribCdmIndex.getTopIndexFileFromConfig(config).getPath());
8181
}
8282

8383
///////////////////////////////////////
@@ -98,7 +98,7 @@ public TestGribCdmIndexUpdating(String dataDir, String newModel, FeatureCollecti
9898
this.orgLen = orgLen;
9999
this.remLen = remLen;
100100
GribCdmIndex.updateGribCollection(config, CollectionUpdateType.always, logger);
101-
this.indexFile = GribCdmIndex.getTopIndexFileFromConfig(config).getAbsolutePath();
101+
this.indexFile = GribCdmIndex.getTopIndexFileFromConfig(config).getPath();
102102
}
103103

104104
@Test

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
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
*/
5+
56
package ucar.nc2.grib;
67

78
import static com.google.common.truth.Truth.assertThat;
@@ -68,7 +69,7 @@ public void testExtraCoordinates() throws IOException {
6869
config.gribConfig.setExcludeZero(true); // no longer the default
6970

7071
boolean changed = GribCdmIndex.updateGribCollection(config, CollectionUpdateType.always, logger);
71-
String topLevelIndex = GribCdmIndex.getTopIndexFileFromConfig(config).getAbsolutePath();
72+
String topLevelIndex = GribCdmIndex.getTopIndexFileFromConfig(config).getPath();
7273

7374
logger.debug("changed = {}", changed);
7475

@@ -126,7 +127,7 @@ public void testBestReftimeMonotonic() throws IOException {
126127

127128
boolean changed = GribCdmIndex.updateGribCollection(config, updateMode, logger);
128129
logger.debug("changed = {}", changed);
129-
String topLevelIndex = GribCdmIndex.getTopIndexFileFromConfig(config).getAbsolutePath();
130+
String topLevelIndex = GribCdmIndex.getTopIndexFileFromConfig(config).getPath();
130131
boolean ok = true;
131132

132133
try (NetcdfDataset ds = NetcdfDatasets.openDataset(topLevelIndex)) {

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
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 ucar.nc2.grib;
67

78
import static com.google.common.truth.Truth.assertThat;
@@ -80,7 +81,7 @@ private void checkVariableNameAndTimeAxis(FeatureCollectionConfig config, String
8081
String units, double[] values) throws IOException {
8182
final boolean changed = GribCdmIndex.updateGribCollection(config, CollectionUpdateType.always, logger);
8283
assertThat(changed).isTrue();
83-
final String topLevelIndex = GribCdmIndex.getTopIndexFileFromConfig(config).getAbsolutePath();
84+
final String topLevelIndex = GribCdmIndex.getTopIndexFileFromConfig(config).getPath();
8485

8586
try (NetcdfDataset netcdfDataset = NetcdfDatasets.openDataset(topLevelIndex)) {
8687
final Variable variable =

grib/src/main/java/ucar/nc2/grib/GribIndex.java

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

@@ -12,7 +12,6 @@
1212
import ucar.nc2.grib.grib1.Grib1Index;
1313
import ucar.nc2.grib.grib2.Grib2Index;
1414
import ucar.unidata.io.RandomAccessFile;
15-
import java.io.File;
1615
import java.io.IOException;
1716

1817
/**
@@ -33,11 +32,11 @@ public boolean hasChangedSince(MFile file, long when) {
3332
String idxPath = file.getPath();
3433
if (!idxPath.endsWith(GBX9_IDX))
3534
idxPath += GBX9_IDX;
36-
File idxFile = GribIndexCache.getExistingFileOrCache(idxPath);
35+
MFile idxFile = GribIndexCache.getExistingFileOrCache(idxPath);
3736
if (idxFile == null)
3837
return true;
3938

40-
long idxLastModified = idxFile.lastModified();
39+
long idxLastModified = idxFile.getLastModified();
4140
if (idxLastModified < file.getLastModified())
4241
return true;
4342
return 0 < when && when < idxLastModified;
@@ -47,13 +46,13 @@ public boolean hasntChangedSince(MFile file, long when) {
4746
String idxPath = file.getPath();
4847
if (!idxPath.endsWith(GBX9_IDX))
4948
idxPath += GBX9_IDX;
50-
File idxFile = GribIndexCache.getExistingFileOrCache(idxPath);
49+
MFile idxFile = GribIndexCache.getExistingFileOrCache(idxPath);
5150
if (idxFile == null)
5251
return true;
5352

54-
if (idxFile.lastModified() < file.getLastModified())
53+
if (idxFile.getLastModified() < file.getLastModified())
5554
return true;
56-
return 0 < when && idxFile.lastModified() < when;
55+
return 0 < when && idxFile.getLastModified() < when;
5756
}
5857
};
5958
/////////////////////////////////////////////////////////////////////////

grib/src/main/java/ucar/nc2/grib/GribIndexCache.java

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

66
package ucar.nc2.grib;
77

88
import ucar.nc2.grib.collection.Grib;
99
import ucar.nc2.util.DiskCache2;
10+
import thredds.inventory.MFile;
11+
import thredds.inventory.MFiles;
1012
import java.io.File;
1113

1214
/**
@@ -35,11 +37,11 @@ public static synchronized DiskCache2 getDiskCache2() {
3537
* @param fileLocation full path of original index filename
3638
* @return File, possibly in cache, may or may not exist
3739
*/
38-
public static File getFileOrCache(String fileLocation) {
39-
File result = getExistingFileOrCache(fileLocation);
40+
public static MFile getFileOrCache(String fileLocation) {
41+
MFile result = getExistingFileOrCache(fileLocation);
4042
if (result != null)
4143
return result;
42-
return getDiskCache2().getFile(fileLocation);
44+
return MFiles.create(getDiskCache2().getFile(fileLocation).getPath());
4345
}
4446

4547
/**
@@ -48,14 +50,14 @@ public static File getFileOrCache(String fileLocation) {
4850
* @param fileLocation full path of original index filename
4951
* @return existing file if you can find it, else null
5052
*/
51-
public static File getExistingFileOrCache(String fileLocation) {
53+
public static MFile getExistingFileOrCache(String fileLocation) {
5254
File result = getDiskCache2().getExistingFileOrCache(fileLocation);
5355
if (result == null && Grib.debugGbxIndexOnly && fileLocation.endsWith(".gbx9.ncx4")) { // might create only from
5456
// gbx9 for debugging
5557
int length = fileLocation.length();
5658
String maybeIndexAlreadyExists = fileLocation.substring(0, length - 10) + ".ncx4";
5759
result = getDiskCache2().getExistingFileOrCache(maybeIndexAlreadyExists);
5860
}
59-
return result;
61+
return result == null ? null : MFiles.create(result.getPath());
6062
}
6163
}

grib/src/main/java/ucar/nc2/grib/collection/GcMFile.java

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

66
package ucar.nc2.grib.collection;
77

8-
import java.io.FileInputStream;
98
import java.io.FileNotFoundException;
109
import java.io.IOException;
1110
import java.io.OutputStream;
1211
import javax.annotation.Nullable;
13-
import thredds.filesystem.MFileOS;
1412
import thredds.inventory.MFile;
1513
import ucar.nc2.util.IO;
1614
import ucar.unidata.io.RandomAccessFile;
1715
import ucar.unidata.util.StringUtil2;
18-
import java.io.File;
1916
import java.util.ArrayList;
2017
import java.util.List;
2118
import java.util.Set;
@@ -28,7 +25,7 @@
2825
*/
2926
public class GcMFile implements thredds.inventory.MFile {
3027

31-
static List<GcMFile> makeFiles(File directory, List<MFile> files, Set<Integer> allFileSet) {
28+
static List<GcMFile> makeFiles(MFile directory, List<MFile> files, Set<Integer> allFileSet) {
3229
List<GcMFile> result = new ArrayList<>(files.size());
3330
String dirPath = StringUtil2.replace(directory.getPath(), '\\', "/");
3431

@@ -47,12 +44,12 @@ static List<GcMFile> makeFiles(File directory, List<MFile> files, Set<Integer> a
4744
}
4845

4946
////////////////////////////////////////////////////////////////////////////////////////
50-
public final File directory;
47+
public final MFile directory;
5148
public final String name;
5249
public final long lastModified, length;
5350
public final int index;
5451

55-
GcMFile(File directory, String name, long lastModified, long length, int index) {
52+
GcMFile(MFile directory, String name, long lastModified, long length, int index) {
5653
this.directory = directory;
5754
this.name = name;
5855
this.lastModified = lastModified;
@@ -77,8 +74,8 @@ public boolean isDirectory() {
7774

7875
@Override
7976
public String getPath() {
80-
String path = new File(directory, name).getPath();
81-
return StringUtil2.replace(path, '\\', "/");
77+
MFile file = directory.getChild(name);
78+
return file == null ? null : file.getPath();
8279
}
8380

8481
@Override
@@ -87,8 +84,8 @@ public String getName() {
8784
}
8885

8986
@Override
90-
public MFile getParent() {
91-
return new MFileOS(directory);
87+
public MFile getParent() throws IOException {
88+
return directory;
9289
}
9390

9491
@Override
@@ -105,24 +102,28 @@ public Object getAuxInfo() {
105102
@Override
106103
public void setAuxInfo(Object info) {}
107104

108-
public File getDirectory() {
105+
public MFile getDirectory() {
109106
return directory;
110107
}
111108

112109
@Override
113110
public String toString() {
114-
return "GcMFile{" + "directory=" + directory + ", name='" + name + '\'' + ", lastModified=" + lastModified
111+
return "GcMFile{" + "directory=" + directory.getPath() + ", name='" + name + '\'' + ", lastModified=" + lastModified
115112
+ ", length=" + length + ", index=" + index + '}';
116113
}
117114

118115
@Override
119116
public boolean exists() {
120-
return new File(directory, name).exists();
117+
MFile file = directory.getChild(name);
118+
return file != null && file.exists();
121119
}
122120

123121
@Override
124-
public FileInputStream getInputStream() throws FileNotFoundException {
125-
return new FileInputStream(new File(directory, name));
122+
public java.io.InputStream getInputStream() throws FileNotFoundException {
123+
MFile file = directory.getChild(name);
124+
if (file == null)
125+
throw new FileNotFoundException(name);
126+
return file.getInputStream();
126127
}
127128

128129
@Override

grib/src/main/java/ucar/nc2/grib/collection/Grib1CollectionBuilder.java

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

@@ -28,7 +28,6 @@
2828
import ucar.nc2.time.CalendarDateRange;
2929
import ucar.nc2.time.CalendarPeriod;
3030
import ucar.nc2.util.CloseableIterator;
31-
import java.io.File;
3231
import java.io.IOException;
3332
import java.util.*;
3433

@@ -191,7 +190,7 @@ protected boolean writeIndex(String name, String indexFilepath, CoordinateRuntim
191190
List<Grib1CollectionWriter.Group> groups2 = new ArrayList<>();
192191
for (Object g : groups)
193192
groups2.add((Grib1CollectionWriter.Group) g); // why copy ?
194-
File indexFileInCache = GribIndexCache.getFileOrCache(indexFilepath);
193+
MFile indexFileInCache = GribIndexCache.getFileOrCache(indexFilepath);
195194
return writer.writeIndex(name, indexFileInCache, masterRuntime, groups2, files, type, dateRange);
196195
}
197196

grib/src/main/java/ucar/nc2/grib/collection/Grib1CollectionWriter.java

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

@@ -9,6 +9,7 @@
99
import java.nio.charset.StandardCharsets;
1010
import thredds.inventory.MCollection;
1111
import thredds.inventory.MFile;
12+
import thredds.inventory.MFiles;
1213
import ucar.nc2.grib.coord.Coordinate;
1314
import ucar.nc2.grib.coord.CoordinateEns;
1415
import ucar.nc2.grib.coord.CoordinateRuntime;
@@ -90,15 +91,18 @@ public Set<Long> getCoordinateRuntimes() {
9091
*/
9192

9293
// indexFile is in the cache
93-
boolean writeIndex(String name, File idxFile, CoordinateRuntime masterRuntime, List<Group> groups, List<MFile> files,
94+
boolean writeIndex(String name, MFile idxFile, CoordinateRuntime masterRuntime, List<Group> groups, List<MFile> files,
9495
GribCollectionImmutable.Type type, CalendarDateRange dateRange) throws IOException {
9596
Grib1Record first = null; // take global metadata from here
9697
boolean deleteOnClose = false;
9798

9899
if (idxFile.exists()) {
99100
RandomAccessFile.eject(idxFile.getPath());
100-
if (!idxFile.delete()) {
101-
logger.warn(" gc1 cant delete index file {}", idxFile.getPath());
101+
if (idxFile instanceof thredds.filesystem.MFileOS) {
102+
File f = new File(idxFile.getPath());
103+
if (!f.delete()) {
104+
logger.warn(" gc1 cant delete index file {}", idxFile.getPath());
105+
}
102106
}
103107
}
104108
logger.debug(" createIndex for {}", idxFile.getPath());
@@ -186,7 +190,7 @@ boolean writeIndex(String name, File idxFile, CoordinateRuntime masterRuntime, L
186190
indexBuilder.setVersion(currentVersion);
187191

188192
// directory and mfile list
189-
File directory = new File(dcm.getRoot());
193+
MFile directory = MFiles.create(dcm.getRoot());
190194
List<GcMFile> gcmfiles = GcMFile.makeFiles(directory, files, allFileSet);
191195
for (GcMFile gcmfile : gcmfiles) {
192196
GribCollectionProto.MFile.Builder b = GribCollectionProto.MFile.newBuilder();
@@ -230,8 +234,10 @@ boolean writeIndex(String name, File idxFile, CoordinateRuntime masterRuntime, L
230234
} finally {
231235

232236
// remove it on failure
233-
if (deleteOnClose && !idxFile.delete()) {
234-
logger.error(" gc1 cant deleteOnClose index file {}", idxFile.getPath());
237+
if (deleteOnClose && idxFile instanceof thredds.filesystem.MFileOS) {
238+
File f = new File(idxFile.getPath());
239+
if (!f.delete())
240+
logger.error(" gc1 cant deleteOnClose index file {}", idxFile.getPath());
235241
}
236242
}
237243
}

grib/src/main/java/ucar/nc2/grib/collection/Grib1PartitionBuilder.java

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

66
package ucar.nc2.grib.collection;
77

88
import thredds.featurecollection.FeatureCollectionConfig;
9+
import thredds.inventory.MFile;
910
import thredds.inventory.partition.PartitionManager;
10-
import java.io.File;
1111

1212
/**
1313
* Builds Grib1 PartitionCollections (version 2)
@@ -18,7 +18,7 @@
1818
class Grib1PartitionBuilder extends GribPartitionBuilder {
1919
public static final String MAGIC_START = "Grib1Partition2Index"; // was Grib1Partition0Index
2020

21-
Grib1PartitionBuilder(String name, File directory, PartitionManager tpc, org.slf4j.Logger logger) {
21+
Grib1PartitionBuilder(String name, MFile directory, PartitionManager tpc, org.slf4j.Logger logger) {
2222
super(name, tpc, logger);
2323

2424
FeatureCollectionConfig config = null;

0 commit comments

Comments
 (0)