diff --git a/colormipsearch-api/src/main/java/org/janelia/colormipsearch/mips/FileDataUtils.java b/colormipsearch-api/src/main/java/org/janelia/colormipsearch/mips/FileDataUtils.java index 96eaca6c..75f2eae5 100644 --- a/colormipsearch-api/src/main/java/org/janelia/colormipsearch/mips/FileDataUtils.java +++ b/colormipsearch-api/src/main/java/org/janelia/colormipsearch/mips/FileDataUtils.java @@ -5,7 +5,6 @@ import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; -import java.util.Arrays; import java.util.HashMap; import java.util.List; import java.util.Map; @@ -14,19 +13,17 @@ import java.util.regex.Matcher; import java.util.regex.Pattern; import java.util.stream.Collectors; -import java.util.stream.IntStream; import java.util.stream.Stream; import java.util.zip.ZipEntry; import java.util.zip.ZipFile; import org.apache.commons.collections4.CollectionUtils; -import org.apache.commons.lang3.RegExUtils; import org.apache.commons.lang3.StringUtils; import org.janelia.colormipsearch.model.FileData; public class FileDataUtils { - private static final Map>> ARCHIVE_ENTRIES_CACHE = new HashMap<>(); + private static final Map>> FILE_NAMES_CACHE = new HashMap<>(); public static FileData lookupVariantFileData(List variantLocations, Pattern variantPattern, String sourceCDMName, String variantSuffix, Function variantSuffixMapping) { if (CollectionUtils.isEmpty(variantLocations)) { @@ -50,26 +47,17 @@ public static FileData lookupVariantFileData(List variantLocations, Patt } } - private static FileData lookupVariantFileDataInDir(Path variantPath, - Pattern variantPattern) { - try (Stream s = Files.find(variantPath, 1, (p, a) -> { - Matcher variantMatcher = variantPattern.matcher(p.getFileName().toString()); - return variantMatcher.find(); - })) { - return s.map(p -> { - try { - return p.toRealPath(); - } catch (IOException e) { - throw new UncheckedIOException(e); - } - }) - .filter(Files::isRegularFile) - .findFirst() - .map(p -> FileData.fromString(p.toString())) - .orElse(null); - } catch (IOException e) { - throw new UncheckedIOException(e); - } + private static FileData lookupVariantFileDataInDir(Path variantPath, Pattern variantPattern) { + Map> variantDirEntries = getDirEntryNames(variantPath); + return variantDirEntries.entrySet().stream() + .filter(e -> { + Matcher variantMatcher = variantPattern.matcher(e.getKey()); + return variantMatcher.find(); + }) + .flatMap(e -> e.getValue().stream()) + .findFirst() + .map(FileData::fromString) + .orElse(null); } private static FileData lookupVariantFileDataInArchive(Path variantPath, Pattern variantPattern) { @@ -86,10 +74,10 @@ private static FileData lookupVariantFileDataInArchive(Path variantPath, Pattern } private static Map> getZipEntryNames(Path zipPath) { - if (ARCHIVE_ENTRIES_CACHE.get(zipPath) == null) { + if (FILE_NAMES_CACHE.get(zipPath) == null) { return cacheZipEntryNames(zipPath); } else { - return ARCHIVE_ENTRIES_CACHE.get(zipPath); + return FILE_NAMES_CACHE.get(zipPath); } } @@ -105,7 +93,7 @@ private static Map> cacheZipEntryNames(Path zipPath) { .filter(ze -> !ze.isDirectory()) .map(ZipEntry::getName) .collect(Collectors.groupingBy(zen -> Paths.get(zen).getFileName().toString(), Collectors.toList())); - ARCHIVE_ENTRIES_CACHE.put(zipPath, zipEntryNames); + FILE_NAMES_CACHE.put(zipPath, zipEntryNames); return zipEntryNames; } finally { try { @@ -115,8 +103,29 @@ private static Map> cacheZipEntryNames(Path zipPath) { } } - private static String createMIPName(String name, String suffix, String ext) { - return name + StringUtils.defaultIfEmpty(suffix, "") + ext; + private static Map> getDirEntryNames(Path dirPath) { + if (FILE_NAMES_CACHE.get(dirPath) == null) { + return cacheDirEntryNames(dirPath); + } else { + return FILE_NAMES_CACHE.get(dirPath); + } + } + + private static Map> cacheDirEntryNames(Path dirPath) { + try (Stream s = Files.find(dirPath, 1, (p, a) -> !a.isDirectory())) { + Map> dirEntryNames = + s.map(p -> { + try { + return p.toRealPath().toString(); + } catch (IOException e) { + throw new UncheckedIOException(e); + } + }).collect(Collectors.groupingBy(f -> Paths.get(f).getFileName().toString(), Collectors.toList())); + FILE_NAMES_CACHE.put(dirPath, dirEntryNames); + return dirEntryNames; + } catch (IOException e) { + throw new UncheckedIOException(e); + } } }