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
2020import ucar .nc2 .dt .GridCoordSystem ;
2121import ucar .nc2 .dt .grid .GridDataset ;
2222import ucar .nc2 .NetcdfFile ;
23+ import ucar .nc2 .internal .dataset .ft .fmrc .InventoryCacheProvider ;
2324import ucar .nc2 .ncml .NcMLReader ;
2425import ucar .nc2 .dataset .CoordinateAxis1D ;
2526import 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 {
0 commit comments