From 6d124bb6901d4205ef51e38223dada55c717beff Mon Sep 17 00:00:00 2001 From: Reinhard Pointner Date: Thu, 28 Jan 2016 16:18:01 +0000 Subject: [PATCH] Cache heavily used Pattern/FileFilter objects --- source/net/filebot/cli/CmdlineOperations.java | 3 +- source/net/filebot/media/MediaDetection.java | 13 +++-- source/net/filebot/media/ReleaseInfo.java | 55 +++++++++++++++---- .../net/filebot/subtitle/MicroDVDReader.java | 7 +-- source/net/filebot/util/StringUtilities.java | 1 + 5 files changed, 58 insertions(+), 21 deletions(-) diff --git a/source/net/filebot/cli/CmdlineOperations.java b/source/net/filebot/cli/CmdlineOperations.java index ed0f360d..beeb7927 100644 --- a/source/net/filebot/cli/CmdlineOperations.java +++ b/source/net/filebot/cli/CmdlineOperations.java @@ -1,5 +1,6 @@ package net.filebot.cli; +import static net.filebot.util.StringUtilities.*; import static java.lang.String.*; import static java.util.Arrays.*; import static java.util.Collections.*; @@ -186,7 +187,7 @@ public class CmdlineOperations implements CmdlineInterface { CLILogger.config("Auto-detected query: " + seriesNames); } else { // use --q option - seriesNames = asList(query.split("[|]")); + seriesNames = asList(PIPE.split(query)); } if (strict && seriesNames.size() > 1) { diff --git a/source/net/filebot/media/MediaDetection.java b/source/net/filebot/media/MediaDetection.java index bab7d18b..dce7960d 100644 --- a/source/net/filebot/media/MediaDetection.java +++ b/source/net/filebot/media/MediaDetection.java @@ -998,10 +998,10 @@ public class MediaDetection { return movies; } - private static final Pattern defaultIgnoreTokens = releaseInfo.getVideoFormatPattern(false); + private static Pattern formatInfoPattern = releaseInfo.getVideoFormatPattern(false); public static String stripFormatInfo(CharSequence name) { - return defaultIgnoreTokens.matcher(name).replaceAll(""); + return formatInfoPattern.matcher(name).replaceAll(""); } public static String stripReleaseInfo(String name, boolean strict) { @@ -1144,11 +1144,16 @@ public class MediaDetection { return releaseInfo.cleanRelease(names, strict); } + private static Pattern blacklistPattern; + public static List stripBlacklistedTerms(Collection names) throws IOException { - Pattern blacklist = releaseInfo.getBlacklistPattern(); + if (blacklistPattern == null) { + blacklistPattern = releaseInfo.getBlacklistPattern(); + } + List acceptables = new ArrayList(names.size()); for (String it : names) { - if (blacklist.matcher(it).replaceAll("").trim().length() > 0) { + if (blacklistPattern.matcher(it).replaceAll("").trim().length() > 0) { acceptables.add(it); } } diff --git a/source/net/filebot/media/ReleaseInfo.java b/source/net/filebot/media/ReleaseInfo.java index 21fddae1..926d401a 100644 --- a/source/net/filebot/media/ReleaseInfo.java +++ b/source/net/filebot/media/ReleaseInfo.java @@ -43,19 +43,32 @@ import org.tukaani.xz.XZInputStream; public class ReleaseInfo { + private String[] videoSources; + private Pattern videoSourcePattern; + public String getVideoSource(String... input) { + if (videoSources == null || videoSourcePattern == null) { + videoSources = PIPE.split(getProperty("pattern.video.source")); + videoSourcePattern = getVideoSourcePattern(); + } + // check parent and itself for group names - return matchLast(getVideoSourcePattern(), getProperty("pattern.video.source").split("[|]"), input); + return matchLast(videoSourcePattern, videoSources, input); } + private Pattern videoTagPattern; + public List getVideoTags(String... input) { - Pattern pattern = getVideoTagPattern(); + if (videoTagPattern == null) { + videoTagPattern = getVideoTagPattern(); + } + List tags = new ArrayList(); for (String s : input) { if (s == null) continue; - Matcher m = pattern.matcher(s); + Matcher m = videoTagPattern.matcher(s); while (m.find()) { tags.add(m.group()); } @@ -89,11 +102,17 @@ public class ReleaseInfo { return match; } + private Map languages; + private Pattern languageSuffix; + public Locale getLanguageSuffix(String name) { // match locale identifier and lookup Locale object - Map languages = getLanguageMap(Locale.ENGLISH, Locale.getDefault()); + if (languages == null || languageSuffix == null) { + languages = getLanguageMap(Locale.ENGLISH, Locale.getDefault()); + languageSuffix = getLanguageSuffixPattern(languages.keySet(), false); + } - String lang = matchLast(getLanguageSuffixPattern(languages.keySet(), false), null, name); + String lang = matchLast(languageSuffix, null, name); if (lang == null) return null; @@ -148,11 +167,12 @@ public class ReleaseInfo { Pattern videoSource = getVideoSourcePattern(); Pattern videoTags = getVideoTagPattern(); Pattern videoFormat = getVideoFormatPattern(strict); + Pattern stereoscopic3d = getStereoscopic3DPattern(); Pattern resolution = getResolutionPattern(); Pattern queryBlacklist = getBlacklistPattern(); - stopwords = new Pattern[] { languageTag, videoSource, videoTags, videoFormat, resolution, languageSuffix }; - blacklist = new Pattern[] { queryBlacklist, languageTag, clutterBracket, releaseGroup, videoSource, videoTags, videoFormat, resolution, languageSuffix }; + stopwords = new Pattern[] { languageTag, videoSource, videoTags, videoFormat, stereoscopic3d, resolution, languageSuffix }; + blacklist = new Pattern[] { queryBlacklist, languageTag, clutterBracket, releaseGroup, videoSource, videoTags, videoFormat, stereoscopic3d, resolution, languageSuffix }; // cache compiled patterns for common usage this.stopwords.put(strict, stopwords); @@ -341,16 +361,31 @@ public class ReleaseInfo { return seriesDirectMappings; } + private static FolderEntryFilter diskFolderFilter; + public FileFilter getDiskFolderFilter() { - return new FolderEntryFilter(compile(getProperty("pattern.diskfolder.entry"))); + if (diskFolderFilter == null) { + diskFolderFilter = new FolderEntryFilter(compile(getProperty("pattern.diskfolder.entry"))); + } + return diskFolderFilter; } + private static RegexFileFilter diskFolderEntryFilter; + public FileFilter getDiskFolderEntryFilter() { - return new RegexFileFilter(compile(getProperty("pattern.diskfolder.entry"))); + if (diskFolderEntryFilter == null) { + diskFolderEntryFilter = new RegexFileFilter(compile(getProperty("pattern.diskfolder.entry"))); + } + return diskFolderEntryFilter; } + private static ClutterFileFilter clutterFileFilter; + public FileFilter getClutterFileFilter() throws IOException { - return new ClutterFileFilter(getExcludePattern(), Long.parseLong(getProperty("number.clutter.maxfilesize"))); // only files smaller than 250 MB may be considered clutter + if (clutterFileFilter == null) { + clutterFileFilter = new ClutterFileFilter(getExcludePattern(), Long.parseLong(getProperty("number.clutter.maxfilesize"))); // only files smaller than 250 MB may be considered clutter + } + return clutterFileFilter; } public List getMediaRoots() { diff --git a/source/net/filebot/subtitle/MicroDVDReader.java b/source/net/filebot/subtitle/MicroDVDReader.java index a8848852..42373d30 100644 --- a/source/net/filebot/subtitle/MicroDVDReader.java +++ b/source/net/filebot/subtitle/MicroDVDReader.java @@ -1,23 +1,18 @@ - package net.filebot.subtitle; - import static net.filebot.util.StringUtilities.*; import java.util.ArrayList; import java.util.List; - public class MicroDVDReader extends SubtitleReader { private double fps = 23.976; - public MicroDVDReader(Readable source) { super(source); } - @Override public SubtitleElement readNext() throws Exception { String line = scanner.nextLine(); @@ -60,7 +55,7 @@ public class MicroDVDReader extends SubtitleReader { } // translate '|' to new lines - String[] lines = text.split("[|]"); + String[] lines = PIPE.split(text); // convert frame interval to time interval return new SubtitleElement(Math.round(startFrame * fps), Math.round(endFrame * fps), join(lines, "\n")); diff --git a/source/net/filebot/util/StringUtilities.java b/source/net/filebot/util/StringUtilities.java index 71bf1c3b..03f968e9 100644 --- a/source/net/filebot/util/StringUtilities.java +++ b/source/net/filebot/util/StringUtilities.java @@ -14,6 +14,7 @@ public final class StringUtilities { public static final Pattern SPACE = Pattern.compile("\\s+"); public static final Pattern DIGIT = Pattern.compile("\\d+"); public static final Pattern NON_DIGIT = Pattern.compile("\\D+"); + public static final Pattern PIPE = Pattern.compile("|", Pattern.LITERAL); public static List matchIntegers(CharSequence s) { if (s == null || s.length() == 0) {