Cache heavily used Pattern/FileFilter objects

This commit is contained in:
Reinhard Pointner 2016-01-28 16:18:01 +00:00
parent 57699dad21
commit 6d124bb690
5 changed files with 58 additions and 21 deletions

View File

@ -1,5 +1,6 @@
package net.filebot.cli; package net.filebot.cli;
import static net.filebot.util.StringUtilities.*;
import static java.lang.String.*; import static java.lang.String.*;
import static java.util.Arrays.*; import static java.util.Arrays.*;
import static java.util.Collections.*; import static java.util.Collections.*;
@ -186,7 +187,7 @@ public class CmdlineOperations implements CmdlineInterface {
CLILogger.config("Auto-detected query: " + seriesNames); CLILogger.config("Auto-detected query: " + seriesNames);
} else { } else {
// use --q option // use --q option
seriesNames = asList(query.split("[|]")); seriesNames = asList(PIPE.split(query));
} }
if (strict && seriesNames.size() > 1) { if (strict && seriesNames.size() > 1) {

View File

@ -998,10 +998,10 @@ public class MediaDetection {
return movies; return movies;
} }
private static final Pattern defaultIgnoreTokens = releaseInfo.getVideoFormatPattern(false); private static Pattern formatInfoPattern = releaseInfo.getVideoFormatPattern(false);
public static String stripFormatInfo(CharSequence name) { public static String stripFormatInfo(CharSequence name) {
return defaultIgnoreTokens.matcher(name).replaceAll(""); return formatInfoPattern.matcher(name).replaceAll("");
} }
public static String stripReleaseInfo(String name, boolean strict) { public static String stripReleaseInfo(String name, boolean strict) {
@ -1144,11 +1144,16 @@ public class MediaDetection {
return releaseInfo.cleanRelease(names, strict); return releaseInfo.cleanRelease(names, strict);
} }
private static Pattern blacklistPattern;
public static List<String> stripBlacklistedTerms(Collection<String> names) throws IOException { public static List<String> stripBlacklistedTerms(Collection<String> names) throws IOException {
Pattern blacklist = releaseInfo.getBlacklistPattern(); if (blacklistPattern == null) {
blacklistPattern = releaseInfo.getBlacklistPattern();
}
List<String> acceptables = new ArrayList<String>(names.size()); List<String> acceptables = new ArrayList<String>(names.size());
for (String it : names) { for (String it : names) {
if (blacklist.matcher(it).replaceAll("").trim().length() > 0) { if (blacklistPattern.matcher(it).replaceAll("").trim().length() > 0) {
acceptables.add(it); acceptables.add(it);
} }
} }

View File

@ -43,19 +43,32 @@ import org.tukaani.xz.XZInputStream;
public class ReleaseInfo { public class ReleaseInfo {
private String[] videoSources;
private Pattern videoSourcePattern;
public String getVideoSource(String... input) { 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 // 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<String> getVideoTags(String... input) { public List<String> getVideoTags(String... input) {
Pattern pattern = getVideoTagPattern(); if (videoTagPattern == null) {
videoTagPattern = getVideoTagPattern();
}
List<String> tags = new ArrayList<String>(); List<String> tags = new ArrayList<String>();
for (String s : input) { for (String s : input) {
if (s == null) if (s == null)
continue; continue;
Matcher m = pattern.matcher(s); Matcher m = videoTagPattern.matcher(s);
while (m.find()) { while (m.find()) {
tags.add(m.group()); tags.add(m.group());
} }
@ -89,11 +102,17 @@ public class ReleaseInfo {
return match; return match;
} }
private Map<String, Locale> languages;
private Pattern languageSuffix;
public Locale getLanguageSuffix(String name) { public Locale getLanguageSuffix(String name) {
// match locale identifier and lookup Locale object // match locale identifier and lookup Locale object
Map<String, Locale> 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) if (lang == null)
return null; return null;
@ -148,11 +167,12 @@ public class ReleaseInfo {
Pattern videoSource = getVideoSourcePattern(); Pattern videoSource = getVideoSourcePattern();
Pattern videoTags = getVideoTagPattern(); Pattern videoTags = getVideoTagPattern();
Pattern videoFormat = getVideoFormatPattern(strict); Pattern videoFormat = getVideoFormatPattern(strict);
Pattern stereoscopic3d = getStereoscopic3DPattern();
Pattern resolution = getResolutionPattern(); Pattern resolution = getResolutionPattern();
Pattern queryBlacklist = getBlacklistPattern(); Pattern queryBlacklist = getBlacklistPattern();
stopwords = new Pattern[] { languageTag, 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, resolution, languageSuffix }; blacklist = new Pattern[] { queryBlacklist, languageTag, clutterBracket, releaseGroup, videoSource, videoTags, videoFormat, stereoscopic3d, resolution, languageSuffix };
// cache compiled patterns for common usage // cache compiled patterns for common usage
this.stopwords.put(strict, stopwords); this.stopwords.put(strict, stopwords);
@ -341,16 +361,31 @@ public class ReleaseInfo {
return seriesDirectMappings; return seriesDirectMappings;
} }
private static FolderEntryFilter diskFolderFilter;
public FileFilter getDiskFolderFilter() { 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() { 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 { 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<File> getMediaRoots() { public List<File> getMediaRoots() {

View File

@ -1,23 +1,18 @@
package net.filebot.subtitle; package net.filebot.subtitle;
import static net.filebot.util.StringUtilities.*; import static net.filebot.util.StringUtilities.*;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
public class MicroDVDReader extends SubtitleReader { public class MicroDVDReader extends SubtitleReader {
private double fps = 23.976; private double fps = 23.976;
public MicroDVDReader(Readable source) { public MicroDVDReader(Readable source) {
super(source); super(source);
} }
@Override @Override
public SubtitleElement readNext() throws Exception { public SubtitleElement readNext() throws Exception {
String line = scanner.nextLine(); String line = scanner.nextLine();
@ -60,7 +55,7 @@ public class MicroDVDReader extends SubtitleReader {
} }
// translate '|' to new lines // translate '|' to new lines
String[] lines = text.split("[|]"); String[] lines = PIPE.split(text);
// convert frame interval to time interval // convert frame interval to time interval
return new SubtitleElement(Math.round(startFrame * fps), Math.round(endFrame * fps), join(lines, "\n")); return new SubtitleElement(Math.round(startFrame * fps), Math.round(endFrame * fps), join(lines, "\n"));

View File

@ -14,6 +14,7 @@ public final class StringUtilities {
public static final Pattern SPACE = Pattern.compile("\\s+"); public static final Pattern SPACE = Pattern.compile("\\s+");
public static final Pattern DIGIT = Pattern.compile("\\d+"); public static final Pattern DIGIT = Pattern.compile("\\d+");
public static final Pattern NON_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<Integer> matchIntegers(CharSequence s) { public static List<Integer> matchIntegers(CharSequence s) {
if (s == null || s.length() == 0) { if (s == null || s.length() == 0) {