From 9029d939f3a23fc949538e53117fd85051e0ea1d Mon Sep 17 00:00:00 2001 From: LukePulverenti Luke Pulverenti luke pulverenti Date: Mon, 20 Aug 2012 23:56:28 -0400 Subject: [PATCH] Further reduced disk access in ItemController --- .../Events/ItemResolveEventArgs.cs | 55 +++++++++---------- .../FFMpeg/FFProbeResult.cs | 3 +- MediaBrowser.Controller/IO/FileData.cs | 24 ++++++++ .../Library/ItemController.cs | 36 ++++++------ .../Providers/FolderProviderFromXml.cs | 4 +- .../ImageFromMediaLocationProvider.cs | 14 +---- .../Providers/LocalTrailerProvider.cs | 2 +- .../Resolvers/BaseItemResolver.cs | 8 +-- .../Resolvers/VideoResolver.cs | 4 +- MediaBrowser.Controller/Xml/XmlExtensions.cs | 1 - .../Providers/MovieProviderFromXml.cs | 4 +- .../Resolvers/MovieResolver.cs | 11 ++-- MediaBrowser.TV/Metadata/EpisodeXmlParser.cs | 1 - MediaBrowser.TV/Metadata/SeriesXmlParser.cs | 1 - .../Providers/SeriesProviderFromXml.cs | 4 +- MediaBrowser.TV/Resolvers/SeriesResolver.cs | 2 +- MediaBrowser.TV/TVUtils.cs | 12 ++-- 17 files changed, 93 insertions(+), 93 deletions(-) diff --git a/MediaBrowser.Controller/Events/ItemResolveEventArgs.cs b/MediaBrowser.Controller/Events/ItemResolveEventArgs.cs index ecb164455d..18df73b51d 100644 --- a/MediaBrowser.Controller/Events/ItemResolveEventArgs.cs +++ b/MediaBrowser.Controller/Events/ItemResolveEventArgs.cs @@ -1,5 +1,4 @@ using System; -using System.Collections.Generic; using System.IO; using MediaBrowser.Controller.IO; using MediaBrowser.Model.Entities; @@ -11,55 +10,55 @@ namespace MediaBrowser.Controller.Events /// public class ItemResolveEventArgs : PreBeginResolveEventArgs { - public KeyValuePair[] FileSystemChildren { get; set; } + public LazyFileInfo[] FileSystemChildren { get; set; } - public KeyValuePair? GetFileSystemEntry(string path, bool? isFolder) + public LazyFileInfo? GetFileSystemEntry(string path, bool? isFolder = null) { for (int i = 0; i < FileSystemChildren.Length; i++) { - KeyValuePair entry = FileSystemChildren[i]; + LazyFileInfo entry = FileSystemChildren[i]; - if (isFolder.HasValue) + if (entry.Path.Equals(path, StringComparison.OrdinalIgnoreCase)) { - if (isFolder.Value && !entry.Value.IsDirectory) + if (isFolder.HasValue) { - continue; + if (isFolder.Value && !entry.FileInfo.IsDirectory) + { + continue; + } + else if (!isFolder.Value && entry.FileInfo.IsDirectory) + { + continue; + } } - else if (!isFolder.Value && entry.Value.IsDirectory) - { - continue; - } - } - - if (entry.Key.Equals(path, StringComparison.OrdinalIgnoreCase)) - { + return entry; } } return null; } - - public KeyValuePair? GetFileSystemEntryByName(string name, bool? isFolder) + + public LazyFileInfo? GetFileSystemEntryByName(string name, bool? isFolder = null) { for (int i = 0; i < FileSystemChildren.Length; i++) { - KeyValuePair entry = FileSystemChildren[i]; + LazyFileInfo entry = FileSystemChildren[i]; - if (isFolder.HasValue) + if (System.IO.Path.GetFileName(entry.Path).Equals(name, StringComparison.OrdinalIgnoreCase)) { - if (isFolder.Value && !entry.Value.IsDirectory) - { - continue; - } - else if (!isFolder.Value && entry.Value.IsDirectory) + if (isFolder.HasValue) { - continue; + if (isFolder.Value && !entry.FileInfo.IsDirectory) + { + continue; + } + else if (!isFolder.Value && entry.FileInfo.IsDirectory) + { + continue; + } } - } - if (System.IO.Path.GetFileName(entry.Key).Equals(name, StringComparison.OrdinalIgnoreCase)) - { return entry; } } diff --git a/MediaBrowser.Controller/FFMpeg/FFProbeResult.cs b/MediaBrowser.Controller/FFMpeg/FFProbeResult.cs index 3bb7dce131..c7ad1c3ed8 100644 --- a/MediaBrowser.Controller/FFMpeg/FFProbeResult.cs +++ b/MediaBrowser.Controller/FFMpeg/FFProbeResult.cs @@ -1,5 +1,4 @@ -using System; -using System.Collections.Generic; +using System.Collections.Generic; namespace MediaBrowser.Controller.FFMpeg { diff --git a/MediaBrowser.Controller/IO/FileData.cs b/MediaBrowser.Controller/IO/FileData.cs index 843a7e8fe3..92b4a69861 100644 --- a/MediaBrowser.Controller/IO/FileData.cs +++ b/MediaBrowser.Controller/IO/FileData.cs @@ -93,4 +93,28 @@ namespace MediaBrowser.Controller.IO } } + public struct LazyFileInfo + { + public string Path { get; set; } + + private WIN32_FIND_DATA? _FileInfo { get; set; } + + public WIN32_FIND_DATA FileInfo + { + get + { + if (_FileInfo == null) + { + _FileInfo = FileData.GetFileData(Path); + } + + return _FileInfo.Value; + } + set + { + _FileInfo = value; + } + } + } + } diff --git a/MediaBrowser.Controller/Library/ItemController.cs b/MediaBrowser.Controller/Library/ItemController.cs index 208be8f0d5..e99fa94a0c 100644 --- a/MediaBrowser.Controller/Library/ItemController.cs +++ b/MediaBrowser.Controller/Library/ItemController.cs @@ -84,7 +84,7 @@ namespace MediaBrowser.Controller.Library return null; } - KeyValuePair[] fileSystemChildren; + LazyFileInfo[] fileSystemChildren; // Gather child folder and files if (fileData.IsDirectory) @@ -96,7 +96,7 @@ namespace MediaBrowser.Controller.Library } else { - fileSystemChildren = new KeyValuePair[] { }; + fileSystemChildren = new LazyFileInfo[] { }; } ItemResolveEventArgs args = new ItemResolveEventArgs() @@ -135,7 +135,7 @@ namespace MediaBrowser.Controller.Library /// /// Finds child BaseItems for a given Folder /// - private async Task AttachChildren(Folder folder, KeyValuePair[] fileSystemChildren) + private async Task AttachChildren(Folder folder, LazyFileInfo[] fileSystemChildren) { int count = fileSystemChildren.Length; @@ -145,7 +145,7 @@ namespace MediaBrowser.Controller.Library { var child = fileSystemChildren[i]; - tasks[i] = GetItem(child.Key, folder, child.Value); + tasks[i] = GetItem(child.Path, folder, child.FileInfo); } BaseItem[] baseItemChildren = await Task.WhenAll(tasks).ConfigureAwait(false); @@ -161,23 +161,19 @@ namespace MediaBrowser.Controller.Library /// /// Transforms shortcuts into their actual paths /// - private KeyValuePair[] FilterChildFileSystemEntries(KeyValuePair[] fileSystemChildren, bool flattenShortcuts) + private LazyFileInfo[] FilterChildFileSystemEntries(LazyFileInfo[] fileSystemChildren, bool flattenShortcuts) { - KeyValuePair[] returnArray = new KeyValuePair[fileSystemChildren.Length]; - List> resolvedShortcuts = new List>(); + LazyFileInfo[] returnArray = new LazyFileInfo[fileSystemChildren.Length]; + List resolvedShortcuts = new List(); for (int i = 0; i < fileSystemChildren.Length; i++) { - KeyValuePair file = fileSystemChildren[i]; + LazyFileInfo file = fileSystemChildren[i]; - if (file.Value.IsDirectory) - { - returnArray[i] = file; - } // If it's a shortcut, resolve it - else if (Shortcut.IsShortcut(file.Key)) + if (Shortcut.IsShortcut(file.Path)) { - string newPath = Shortcut.ResolveShortcut(file.Key); + string newPath = Shortcut.ResolveShortcut(file.Path); WIN32_FIND_DATA newPathData = FileData.GetFileData(newPath); // Find out if the shortcut is pointing to a directory or file @@ -188,18 +184,18 @@ namespace MediaBrowser.Controller.Library if (flattenShortcuts) { returnArray[i] = file; - KeyValuePair[] newChildren = ConvertFileSystemEntries(Directory.GetFileSystemEntries(newPath, "*", SearchOption.TopDirectoryOnly)); + LazyFileInfo[] newChildren = ConvertFileSystemEntries(Directory.GetFileSystemEntries(newPath, "*", SearchOption.TopDirectoryOnly)); resolvedShortcuts.AddRange(FilterChildFileSystemEntries(newChildren, false)); } else { - returnArray[i] = new KeyValuePair(newPath, newPathData); + returnArray[i] = new LazyFileInfo() { Path = newPath, FileInfo = newPathData }; } } else { - returnArray[i] = new KeyValuePair(newPath, newPathData); + returnArray[i] = new LazyFileInfo() { Path = newPath, FileInfo = newPathData }; } } else @@ -309,15 +305,15 @@ namespace MediaBrowser.Controller.Library return item; } - private KeyValuePair[] ConvertFileSystemEntries(string[] files) + private LazyFileInfo[] ConvertFileSystemEntries(string[] files) { - KeyValuePair[] items = new KeyValuePair[files.Length]; + LazyFileInfo[] items = new LazyFileInfo[files.Length]; for (int i = 0; i < files.Length; i++) { string file = files[i]; - items[i] = new KeyValuePair(file, FileData.GetFileData(file)); + items[i] = new LazyFileInfo() { Path = file }; } return items; diff --git a/MediaBrowser.Controller/Providers/FolderProviderFromXml.cs b/MediaBrowser.Controller/Providers/FolderProviderFromXml.cs index b23e8908d4..cba41f780a 100644 --- a/MediaBrowser.Controller/Providers/FolderProviderFromXml.cs +++ b/MediaBrowser.Controller/Providers/FolderProviderFromXml.cs @@ -21,11 +21,11 @@ namespace MediaBrowser.Controller.Providers public async override Task Fetch(BaseEntity item, ItemResolveEventArgs args) { - var metadataFile = args.GetFileSystemEntryByName("folder.xml", false); + var metadataFile = args.GetFileSystemEntryByName("folder.xml"); if (metadataFile.HasValue) { - await Task.Run(() => { new FolderXmlParser().Fetch(item as Folder, metadataFile.Value.Key); }).ConfigureAwait(false); + await Task.Run(() => { new FolderXmlParser().Fetch(item as Folder, metadataFile.Value.Path); }).ConfigureAwait(false); } } } diff --git a/MediaBrowser.Controller/Providers/ImageFromMediaLocationProvider.cs b/MediaBrowser.Controller/Providers/ImageFromMediaLocationProvider.cs index 7785a558a4..8e5364893c 100644 --- a/MediaBrowser.Controller/Providers/ImageFromMediaLocationProvider.cs +++ b/MediaBrowser.Controller/Providers/ImageFromMediaLocationProvider.cs @@ -50,12 +50,7 @@ namespace MediaBrowser.Controller.Providers { var file = args.FileSystemChildren[i]; - if (file.Value.IsDirectory) - { - continue; - } - - string filePath = file.Key; + string filePath = file.Path; string ext = Path.GetExtension(filePath); @@ -85,12 +80,7 @@ namespace MediaBrowser.Controller.Providers { var file = args.FileSystemChildren[i]; - if (file.Value.IsDirectory) - { - continue; - } - - string filePath = file.Key; + string filePath = file.Path; string ext = Path.GetExtension(filePath); diff --git a/MediaBrowser.Controller/Providers/LocalTrailerProvider.cs b/MediaBrowser.Controller/Providers/LocalTrailerProvider.cs index 2476df7cd9..c023edb9af 100644 --- a/MediaBrowser.Controller/Providers/LocalTrailerProvider.cs +++ b/MediaBrowser.Controller/Providers/LocalTrailerProvider.cs @@ -28,7 +28,7 @@ namespace MediaBrowser.Controller.Providers if (trailerPath.HasValue) { - string[] allFiles = Directory.GetFileSystemEntries(trailerPath.Value.Key, "*", SearchOption.TopDirectoryOnly); + string[] allFiles = Directory.GetFileSystemEntries(trailerPath.Value.Path, "*", SearchOption.TopDirectoryOnly); List