Skip to content

Commit

Permalink
Do fast isValidFile checks first for dynamically loaded iosps to impr…
Browse files Browse the repository at this point in the history
…ove performance
  • Loading branch information
tdrwenski committed Feb 2, 2024
1 parent 2322c54 commit 0b02f41
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 3 deletions.
10 changes: 8 additions & 2 deletions cdm/core/src/main/java/ucar/nc2/NetcdfFiles.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

package ucar.nc2;

import com.google.common.collect.Lists;
import java.io.BufferedInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
Expand All @@ -19,6 +20,7 @@
import java.nio.channels.OverlappingFileLockException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.ServiceLoader;
import java.util.zip.GZIPInputStream;
Expand Down Expand Up @@ -788,8 +790,12 @@ private static IOServiceProvider getIosp(ucar.unidata.io.RandomAccessFile raf) t
return new N3iospNew();

} else {
// look for dynamically loaded IOSPs
for (IOServiceProvider loadedSpi : ServiceLoader.load(IOServiceProvider.class)) {
// look for dynamically loaded IOSPs, and sort before using
final ServiceLoader<IOServiceProvider> iosps = ServiceLoader.load(IOServiceProvider.class);
final List<IOServiceProvider> sortedIosps = Lists.newArrayList(iosps);
Collections.sort(sortedIosps);

for (IOServiceProvider loadedSpi : sortedIosps) {
if (loadedSpi.isValidFile(raf)) {
Class c = loadedSpi.getClass();
try {
Expand Down
26 changes: 25 additions & 1 deletion cdm/core/src/main/java/ucar/nc2/iosp/IOServiceProvider.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
*
* @author caron
*/
public interface IOServiceProvider {
public interface IOServiceProvider extends Comparable<IOServiceProvider> {

/**
* Check if this is a valid file for this IOServiceProvider.
Expand Down Expand Up @@ -243,4 +243,28 @@ long readToOutputStream(Variable v2, Section section, OutputStream out)
*/
String getFileTypeDescription();

/**
* Used to determine the ordering for dynamically loaded IOServiceProviders.
*/
enum SortGroup {
GROUP_1, LAST_GROUP,
}

/**
* Get the SortGroup for this IOServiceProvider. This determines the order in which the dynamically loaded
* IOServiceProviders are checked to see if they can open a file with isValidFile().
*
* @return SortGroup, by default the SortGroup.LAST_GROUP will be used.
*/
default SortGroup getSortGroup() {
return SortGroup.LAST_GROUP;
}

/**
* Used the SortGroup to determine the ordering for dynamically loaded IOServiceProviders.
*/
@Override
default int compareTo(IOServiceProvider other) {
return getSortGroup().compareTo(other.getSortGroup());
}
}
10 changes: 10 additions & 0 deletions cdm/zarr/src/main/java/ucar/nc2/iosp/zarr/ZarrIosp.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,16 @@ public boolean isValidFile(RandomAccessFile raf) {
return raf.isDirectory();
}

/**
* Set the SortGroup to GROUP_1 so this IOSP will be checked first, since `isValidFile()` is a quick check
*
* @return SortGroup.GROUP_1
*/
@Override
public SortGroup getSortGroup() {
return SortGroup.GROUP_1;
}

@Override
public String getFileTypeId() {
return fileTypeId;
Expand Down

0 comments on commit 0b02f41

Please sign in to comment.