Skip to content

Commit 3d240f5

Browse files
author
haileyajohnson
authored
Merge pull request #772 from lesserwhirls/fmrcCache
[5.x]: Enable the use of a persisted FMRC cache
2 parents 04f7db0 + e797b6b commit 3d240f5

4 files changed

Lines changed: 92 additions & 20 deletions

File tree

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ default boolean canScan(String location) {
2222
/**
2323
* Creates an instance of
2424
*
25-
* @return An {@link MController} that scans locations to filter and provide a set of {@linik MFile}s defining to
25+
* @return An {@link MController} that scans locations to filter and provide a set of {@link MFile}s defining to
2626
* be used to define a collection.
2727
*/
2828
MController create();

cdm/core/src/main/java/ucar/nc2/ft/fmrc/GridDatasetInv.java

Lines changed: 49 additions & 18 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-2021 John Caron and University Corporation for Atmospheric Research/Unidata
33
* See LICENSE for license information.
44
*/
55

@@ -20,6 +20,7 @@
2020
import ucar.nc2.dt.GridCoordSystem;
2121
import ucar.nc2.dt.grid.GridDataset;
2222
import ucar.nc2.NetcdfFile;
23+
import ucar.nc2.internal.dataset.ft.fmrc.InventoryCacheProvider;
2324
import ucar.nc2.ncml.NcMLReader;
2425
import ucar.nc2.dataset.CoordinateAxis1D;
2526
import ucar.nc2.time.CalendarDate;
@@ -53,8 +54,7 @@ public class GridDatasetInv {
5354
private static final int REQ_VERSION = 2; // minimum required version, else regenerate XML
5455
private static final int CURR_VERSION = 2; // current version
5556

56-
// Cache the GridDatasetInv directly, not persisted to disk.
57-
// TODO: Add persistence if thats shown to be needed.
57+
// Cache the GridDatasetInv directly in memory. Persist to disk if InventoryCacheProvider if found.
5858
private static Cache<String, GridDatasetInv> cache = CacheBuilder.newBuilder().maximumSize(100).build();
5959

6060
public static GridDatasetInv open(MCollection cm, MFile mfile, Element ncml) throws IOException {
@@ -69,6 +69,18 @@ private static class GenerateInv implements Callable<GridDatasetInv> {
6969
private final MCollection cm;
7070
private final MFile mfile;
7171
private final Element ncml;
72+
private static final InventoryCacheProvider persistedCache;
73+
74+
// Look for InventoryCacheProvider, which implements a persistent cache of GridDatasetInv.
75+
// Persistence is needed for the TDS
76+
static {
77+
InventoryCacheProvider icp = null;
78+
for (InventoryCacheProvider provider : ServiceLoader.load(InventoryCacheProvider.class)) {
79+
// first one wins
80+
icp = provider;
81+
}
82+
persistedCache = icp;
83+
}
7284

7385
GenerateInv(MCollection cm, MFile mfile, Element ncml) {
7486
this.cm = cm;
@@ -79,23 +91,33 @@ private static class GenerateInv implements Callable<GridDatasetInv> {
7991
@Override
8092
public GridDatasetInv call() throws Exception {
8193
GridDataset gds = null;
82-
try {
83-
if (ncml == null) {
84-
gds = GridDataset.open(mfile.getPath());
85-
86-
} else {
87-
NetcdfFile nc = NetcdfDataset.acquireFile(DatasetUrl.create(null, mfile.getPath()), null);
88-
NetcdfDataset ncd = NcMLReader.mergeNcML(nc, ncml); // create new dataset
89-
ncd.enhance(); // now that the ncml is added, enhance "in place", ie modify the NetcdfDataset
90-
gds = new GridDataset(ncd);
91-
}
94+
GridDatasetInv inv = null;
95+
if (persistedCache != null) {
96+
inv = persistedCache.get(mfile);
97+
}
9298

93-
return new GridDatasetInv(gds, cm.extractDate(mfile));
94-
} finally {
95-
if (gds != null) {
96-
gds.close();
99+
if (inv == null) {
100+
try {
101+
if (ncml == null) {
102+
gds = GridDataset.open(mfile.getPath());
103+
} else {
104+
NetcdfFile nc = NetcdfDataset.acquireFile(DatasetUrl.create(null, mfile.getPath()), null);
105+
NetcdfDataset ncd = NcMLReader.mergeNcML(nc, ncml); // create new dataset
106+
ncd.enhance(); // now that the ncml is added, enhance "in place", ie modify the NetcdfDataset
107+
gds = new GridDataset(ncd);
108+
}
109+
inv = new GridDatasetInv(gds, cm.extractDate(mfile));
110+
if (persistedCache != null && inv != null) {
111+
// add inventory to persisted cache
112+
persistedCache.put(mfile, inv);
113+
}
114+
} finally {
115+
if (gds != null) {
116+
gds.close();
117+
}
97118
}
98119
}
120+
return inv;
99121
}
100122
}
101123

@@ -171,6 +193,15 @@ public String toString() {
171193
return location;
172194
}
173195

196+
/**
197+
* Check if the GribDatasetInv xml format can be fully understood by this code.
198+
*
199+
* @return true if version can be fully understood, otherwise false.
200+
*/
201+
public boolean isXmlVersionCompatible() {
202+
return this.version >= REQ_VERSION;
203+
}
204+
174205
public String getLocation() {
175206
return location;
176207
}
@@ -457,7 +488,7 @@ Document writeDocument(Date lastModified) {
457488
* @return ForecastModelRun
458489
* @throws IOException on io error
459490
*/
460-
private static GridDatasetInv readXML(byte[] xmlString) throws IOException {
491+
public static GridDatasetInv readXML(byte[] xmlString) throws IOException {
461492
InputStream is = new BufferedInputStream(new ByteArrayInputStream(xmlString));
462493
org.jdom2.Document doc;
463494
try {
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/*
2+
* Copyright (c) 2021 University Corporation for Atmospheric Research/Unidata
3+
* See LICENSE for license information.
4+
*/
5+
6+
package ucar.nc2.internal.dataset.ft.fmrc;
7+
8+
import thredds.inventory.MFile;
9+
import ucar.nc2.ft.fmrc.GridDatasetInv;
10+
11+
import javax.annotation.Nullable;
12+
import java.io.*;
13+
14+
/**
15+
* Service Provider Interface for providing a persisted cache for {@link GridDatasetInv}.
16+
*
17+
* For use by the THREDDS Data Server and not intended to be used publicly.
18+
*
19+
*/
20+
public interface InventoryCacheProvider {
21+
22+
/**
23+
* Get the grid inventory associated with an MFile from the cache.
24+
*
25+
* @param mfile the mfile containing gridded data
26+
* @return grid inventory of the of mfile, null if not found
27+
* @throws IOException
28+
*/
29+
@Nullable
30+
public GridDatasetInv get(MFile mfile) throws IOException;
31+
32+
/**
33+
* Add the grid inventory associated with an MFile to the cache.
34+
*
35+
* @param mfile the mfile containing gridded data
36+
* @param inventory the grid inventory of the of mfile
37+
* @throws IOException
38+
*/
39+
public void put(MFile mfile, GridDatasetInv inventory) throws IOException;
40+
41+
}

cdm/core/src/main/java/ucar/unidata/io/RandomAccessFile.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1456,7 +1456,7 @@ public final String readLine() throws IOException {
14561456
* The charset parameter is an extension not implemented in java.io.RandomAccessFile.
14571457
*
14581458
* @param charset - character encoding to use
1459-
* @return
1459+
* @return the next line of text
14601460
* @throws IOException
14611461
*/
14621462
public String readLine(Charset charset) throws IOException {

0 commit comments

Comments
 (0)