From ae71d809ea522778f229ee89d97f298b7002eb38 Mon Sep 17 00:00:00 2001 From: Reinhard Pointner Date: Fri, 6 Jul 2012 03:10:26 +0000 Subject: [PATCH] * pre-load MediaTypes, MediaDetection and Groovy classes and resources on a low-priority background thread right after startup --- source/net/sourceforge/filebot/Main.java | 39 +++++++++++++++++-- .../filebot/format/MediaBindingBean.java | 6 +-- .../filebot/media/MediaDetection.java | 8 ++-- .../filebot/media/ReleaseInfo.java | 8 ++-- website/data/query-blacklist.txt | 6 +-- 5 files changed, 49 insertions(+), 18 deletions(-) diff --git a/source/net/sourceforge/filebot/Main.java b/source/net/sourceforge/filebot/Main.java index 13a911d8..4fe5c737 100644 --- a/source/net/sourceforge/filebot/Main.java +++ b/source/net/sourceforge/filebot/Main.java @@ -22,6 +22,8 @@ import java.security.PermissionCollection; import java.security.Permissions; import java.security.Policy; import java.security.ProtectionDomain; +import java.util.Collections; +import java.util.List; import java.util.Properties; import java.util.logging.Level; import java.util.logging.Logger; @@ -45,6 +47,7 @@ import net.sourceforge.filebot.cli.ArgumentBean; import net.sourceforge.filebot.cli.ArgumentProcessor; import net.sourceforge.filebot.cli.CmdlineOperations; import net.sourceforge.filebot.format.ExpressionFormat; +import net.sourceforge.filebot.media.MediaDetection; import net.sourceforge.filebot.ui.MainFrame; import net.sourceforge.filebot.ui.SinglePanelFrame; import net.sourceforge.filebot.ui.sfv.SfvPanelBuilder; @@ -108,7 +111,7 @@ public class Main { // GUI mode => start user interface try { - SwingUtilities.invokeLater(new Runnable() { + SwingUtilities.invokeAndWait(new Runnable() { @Override public void run() { @@ -123,8 +126,8 @@ public class Main { } }); - // pre-load media.types (when loaded during DnD it will freeze the UI for a few hundred milliseconds) - MediaTypes.getDefault(); + // pre-load certain classes and resources in the background + warmupCachedResources(); // check for application updates (only when installed, i.e. not running via fatjar or webstart) if (!"skip".equals(System.getProperty("application.update"))) { @@ -247,6 +250,36 @@ public class Main { } + private static void warmupCachedResources() { + Thread warmup = new Thread("warmup") { + + @Override + public void run() { + try { + // pre-load media.types (when loaded during DnD it will freeze the UI for a few hundred milliseconds) + MediaTypes.getDefault(); + + // pre-load movie/series index + List dummy = Collections.singletonList(""); + MediaDetection.stripReleaseInfo(dummy, true); + MediaDetection.matchSeriesByName(dummy, 0); + MediaDetection.matchMovieName(dummy, true); + + // pre-load Groovy script engine + new ExpressionFormat("").format(""); + } catch (Exception e) { + Logger.getLogger(getClass().getName()).log(Level.WARNING, e.getMessage(), e); + } + } + }; + + // start background thread + warmup.setDaemon(true); + warmup.setPriority(Thread.MIN_PRIORITY); + warmup.start(); + } + + private static void restoreWindowBounds(final JFrame window, final Settings settings) { // store bounds on close window.addWindowListener(new WindowAdapter() { diff --git a/source/net/sourceforge/filebot/format/MediaBindingBean.java b/source/net/sourceforge/filebot/format/MediaBindingBean.java index 7bd253c3..ed6c412c 100644 --- a/source/net/sourceforge/filebot/format/MediaBindingBean.java +++ b/source/net/sourceforge/filebot/format/MediaBindingBean.java @@ -6,6 +6,7 @@ import static java.util.Arrays.*; import static net.sourceforge.filebot.MediaTypes.*; import static net.sourceforge.filebot.format.Define.*; import static net.sourceforge.filebot.hash.VerificationUtilities.*; +import static net.sourceforge.filebot.media.MediaDetection.*; import static net.sourceforge.filebot.similarity.Normalization.*; import static net.sourceforge.filebot.web.EpisodeFormat.*; import static net.sourceforge.tuned.FileUtilities.*; @@ -24,7 +25,6 @@ import net.sf.ehcache.CacheManager; import net.sf.ehcache.Element; import net.sourceforge.filebot.WebServices; import net.sourceforge.filebot.hash.HashType; -import net.sourceforge.filebot.media.ReleaseInfo; import net.sourceforge.filebot.mediainfo.MediaInfo; import net.sourceforge.filebot.mediainfo.MediaInfo.StreamKind; import net.sourceforge.filebot.web.Date; @@ -287,7 +287,6 @@ public class MediaBindingBean { public String getVideoSource() { // use inferred media file File inferredMediaFile = getInferredMediaFile(); - ReleaseInfo releaseInfo = new ReleaseInfo(); // look for video source patterns in media file and it's parent folder return releaseInfo.getVideoSource(inferredMediaFile); @@ -298,7 +297,6 @@ public class MediaBindingBean { public String getReleaseGroup() throws IOException { // use inferred media file File inferredMediaFile = getInferredMediaFile(); - ReleaseInfo releaseInfo = new ReleaseInfo(); // look for release group names in media file and it's parent folder return releaseInfo.getReleaseGroup(inferredMediaFile); @@ -310,7 +308,7 @@ public class MediaBindingBean { // make sure media file is defined checkMediaFile(); - Locale languageSuffix = new ReleaseInfo().getLanguageSuffix(FileUtilities.getName(mediaFile)); + Locale languageSuffix = releaseInfo.getLanguageSuffix(FileUtilities.getName(mediaFile)); if (languageSuffix != null) return new Locale(languageSuffix.getISO3Language()); // force ISO3 letter-code diff --git a/source/net/sourceforge/filebot/media/MediaDetection.java b/source/net/sourceforge/filebot/media/MediaDetection.java index 329bd828..3bf49e91 100644 --- a/source/net/sourceforge/filebot/media/MediaDetection.java +++ b/source/net/sourceforge/filebot/media/MediaDetection.java @@ -56,7 +56,7 @@ import net.sourceforge.filebot.web.TheTVDBClient.TheTVDBSearchResult; public class MediaDetection { - private static final ReleaseInfo releaseInfo = new ReleaseInfo(); + public static final ReleaseInfo releaseInfo = new ReleaseInfo(); public static final FileFilter DISK_FOLDERS = releaseInfo.getDiskFolderFilter(); public static final FileFilter NON_CLUTTER_FILES = not(releaseInfo.getClutterFileFilter()); @@ -319,7 +319,7 @@ public class MediaDetection { terms.add(reduceMovieName(getName(movieFolder))); } - List movieNameMatches = matchMovieName(terms, locale, strict); + List movieNameMatches = matchMovieName(terms, strict); // skip further queries if collected matches are already sufficient if (options.size() > 0 && movieNameMatches.size() > 0) { @@ -329,7 +329,7 @@ public class MediaDetection { // if matching name+year failed, try matching only by name if (movieNameMatches.isEmpty() && strict) { - movieNameMatches = matchMovieName(terms, locale, false); + movieNameMatches = matchMovieName(terms, false); } // assume name without spacing will mess up any lookup @@ -406,7 +406,7 @@ public class MediaDetection { } - public static List matchMovieName(final List files, final Locale locale, final boolean strict) throws Exception { + public static List matchMovieName(final List files, final boolean strict) throws Exception { // cross-reference file / folder name with movie list final HighPerformanceMatcher nameMatcher = new HighPerformanceMatcher(3); final Map matchMap = new HashMap(); diff --git a/source/net/sourceforge/filebot/media/ReleaseInfo.java b/source/net/sourceforge/filebot/media/ReleaseInfo.java index bb1d8256..87745f02 100644 --- a/source/net/sourceforge/filebot/media/ReleaseInfo.java +++ b/source/net/sourceforge/filebot/media/ReleaseInfo.java @@ -195,24 +195,24 @@ public class ReleaseInfo { } - public synchronized Pattern getReleaseGroupPattern(boolean strict) throws IOException { + public Pattern getReleaseGroupPattern(boolean strict) throws IOException { // pattern matching any release group name enclosed in separators return compile("(?