Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unlock enhanced datasets #1303

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -1657,8 +1657,7 @@ private Builder<?> addLocalFieldsToBuilder(Builder<? extends Builder<?>> b) {
this.coordSys.forEach(sys -> b.coords.addCoordinateSystem(sys.toBuilder()));
this.coordTransforms.forEach(trans -> b.coords.addCoordinateTransform(trans.toBuilder()));

b.setOrgFile(this.orgFile).setConventionUsed(this.convUsed).setEnhanceMode(this.enhanceMode)
.setAggregation(this.agg);
b.setOrgFile(this).setConventionUsed(this.convUsed).setEnhanceMode(this.enhanceMode).setAggregation(this.agg);

return (Builder<?>) super.addLocalFieldsToBuilder(b);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
package ucar.nc2.dataset;

import static com.google.common.truth.Truth.assertThat;
import static com.google.common.truth.Truth.assertWithMessage;

import java.io.IOException;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import ucar.nc2.NetcdfFile;
import ucar.nc2.ncml.TestNcmlRead;
import ucar.nc2.util.cache.FileCacheIF;
import ucar.unidata.util.test.TestDir;

public class TestNetcdfFileCache {
@BeforeClass
public static void setupCaches() {
NetcdfDatasets.initNetcdfFileCache(1, 10, 15, -1);
}

@AfterClass
public static void shutdownCaches() {
NetcdfDatasets.shutdown();
}

@After
public void cleanupAfterEach() {
NetcdfDatasets.getNetcdfFileCache().clearCache(true);
}

@Test
public void shouldReleaseLockOnNetcdfFileUsingBuilder() throws IOException {
final String filename = "file:./" + TestDir.cdmLocalTestDataDir + "jan.nc";
final DatasetUrl durl = DatasetUrl.findDatasetUrl(filename);

final NetcdfFile netcdfFile = NetcdfDatasets.acquireFile(durl, null);
final NetcdfDataset netcdfDatasetFromBuilder = NetcdfDataset.builder(netcdfFile).build();
// Closing the builder NetcdfDataset should close the original NetcdfFile acquired from cache
netcdfDatasetFromBuilder.close();

assertNoFilesAreLocked();
}

@Test
public void shouldReleaseLockOnNetcdfDatasetUsingBuilder() throws IOException {
final String filename = "file:./" + TestDir.cdmLocalTestDataDir + "jan.nc";
final DatasetUrl durl = DatasetUrl.findDatasetUrl(filename);

final NetcdfDataset netcdfDataset = NetcdfDatasets.acquireDataset(durl, null);
final NetcdfDataset netcdfDatasetFromBuilder = netcdfDataset.toBuilder().build();
// Closing the builder NetcdfDataset should close the original NetcdfDataset acquired from cache
netcdfDatasetFromBuilder.close();

assertNoFilesAreLocked();
}

@Test
public void shouldReleaseLockOnDataset() throws IOException {
final String filename = "file:./" + TestDir.cdmLocalTestDataDir + "jan.nc";
assertLockIsReleasedOnDataset(filename);
}

@Test
public void shouldReleaseLockOnAggregation() throws IOException {
final String filename = "file:./" + TestNcmlRead.topDir + "aggExisting.xml";
assertLockIsReleasedOnDataset(filename);
}

private static void assertLockIsReleasedOnDataset(String filename) throws IOException {
final DatasetUrl durl = DatasetUrl.findDatasetUrl(filename);
final NetcdfFile netcdfFile = NetcdfDatasets.acquireFile(durl, null);
final NetcdfDataset netcdfDataset = NetcdfDatasets.enhance(netcdfFile, NetcdfDataset.getDefaultEnhanceMode(), null);
// Closing the netcdf dataset should close the "wrapped" NetcdfFile acquired from cache
netcdfDataset.close();

assertNoFilesAreLocked();
}

private static void assertNoFilesAreLocked() {
FileCacheIF cache = NetcdfDatasets.getNetcdfFileCache();
boolean isAnyFileLocked = cache.showCache().stream().anyMatch(entry -> entry.startsWith("true"));
assertWithMessage(cache.showCache().toString()).that(isAnyFileLocked).isFalse();
}
}
Loading