Merge pull request #5890 from Bond-009/filesystem

Reduce string allocations/fs lookups in resolve code
This commit is contained in:
Bond-009 2021-04-30 20:45:43 +02:00 committed by GitHub
commit 360d80c873
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 29 additions and 51 deletions

View File

@ -558,7 +558,6 @@ namespace Emby.Server.Implementations.Library
var args = new ItemResolveArgs(_configurationManager.ApplicationPaths, directoryService) var args = new ItemResolveArgs(_configurationManager.ApplicationPaths, directoryService)
{ {
Parent = parent, Parent = parent,
Path = fullPath,
FileInfo = fileInfo, FileInfo = fileInfo,
CollectionType = collectionType, CollectionType = collectionType,
LibraryOptions = libraryOptions LibraryOptions = libraryOptions

View File

@ -1,3 +1,5 @@
#nullable enable
using System; using System;
using System.IO; using System.IO;
using System.Linq; using System.Linq;
@ -21,8 +23,8 @@ namespace Emby.Server.Implementations.Library
/// <param name="fileSystem">The file system.</param> /// <param name="fileSystem">The file system.</param>
/// <param name="libraryManager">The library manager.</param> /// <param name="libraryManager">The library manager.</param>
/// <param name="directoryService">The directory service.</param> /// <param name="directoryService">The directory service.</param>
/// <exception cref="ArgumentException">Item must have a path</exception> /// <exception cref="ArgumentException">Item must have a path.</exception>
public static void SetInitialItemValues(BaseItem item, Folder parent, IFileSystem fileSystem, ILibraryManager libraryManager, IDirectoryService directoryService) public static void SetInitialItemValues(BaseItem item, Folder? parent, IFileSystem fileSystem, ILibraryManager libraryManager, IDirectoryService directoryService)
{ {
// This version of the below method has no ItemResolveArgs, so we have to require the path already being set // This version of the below method has no ItemResolveArgs, so we have to require the path already being set
if (string.IsNullOrEmpty(item.Path)) if (string.IsNullOrEmpty(item.Path))
@ -43,9 +45,9 @@ namespace Emby.Server.Implementations.Library
// Make sure DateCreated and DateModified have values // Make sure DateCreated and DateModified have values
var fileInfo = directoryService.GetFile(item.Path); var fileInfo = directoryService.GetFile(item.Path);
SetDateCreated(item, fileSystem, fileInfo); SetDateCreated(item, fileInfo);
EnsureName(item, item.Path, fileInfo); EnsureName(item, fileInfo);
} }
/// <summary> /// <summary>
@ -72,9 +74,9 @@ namespace Emby.Server.Implementations.Library
item.Id = libraryManager.GetNewItemId(item.Path, item.GetType()); item.Id = libraryManager.GetNewItemId(item.Path, item.GetType());
// Make sure the item has a name // Make sure the item has a name
EnsureName(item, item.Path, args.FileInfo); EnsureName(item, args.FileInfo);
item.IsLocked = item.Path.IndexOf("[dontfetchmeta]", StringComparison.OrdinalIgnoreCase) != -1 || item.IsLocked = item.Path.Contains("[dontfetchmeta]", StringComparison.OrdinalIgnoreCase) ||
item.GetParents().Any(i => i.IsLocked); item.GetParents().Any(i => i.IsLocked);
// Make sure DateCreated and DateModified have values // Make sure DateCreated and DateModified have values
@ -84,28 +86,15 @@ namespace Emby.Server.Implementations.Library
/// <summary> /// <summary>
/// Ensures the name. /// Ensures the name.
/// </summary> /// </summary>
private static void EnsureName(BaseItem item, string fullPath, FileSystemMetadata fileInfo) private static void EnsureName(BaseItem item, FileSystemMetadata fileInfo)
{ {
// If the subclass didn't supply a name, add it here // If the subclass didn't supply a name, add it here
if (string.IsNullOrEmpty(item.Name) && !string.IsNullOrEmpty(fullPath)) if (string.IsNullOrEmpty(item.Name) && !string.IsNullOrEmpty(item.Path))
{ {
var fileName = fileInfo == null ? Path.GetFileName(fullPath) : fileInfo.Name; item.Name = fileInfo.IsDirectory ? fileInfo.Name : Path.GetFileNameWithoutExtension(fileInfo.Name);
item.Name = GetDisplayName(fileName, fileInfo != null && fileInfo.IsDirectory);
} }
} }
/// <summary>
/// Gets the display name.
/// </summary>
/// <param name="path">The path.</param>
/// <param name="isDirectory">if set to <c>true</c> [is directory].</param>
/// <returns>System.String.</returns>
private static string GetDisplayName(string path, bool isDirectory)
{
return isDirectory ? Path.GetFileName(path) : Path.GetFileNameWithoutExtension(path);
}
/// <summary> /// <summary>
/// Ensures DateCreated and DateModified have values. /// Ensures DateCreated and DateModified have values.
/// </summary> /// </summary>
@ -114,21 +103,6 @@ namespace Emby.Server.Implementations.Library
/// <param name="args">The args.</param> /// <param name="args">The args.</param>
private static void EnsureDates(IFileSystem fileSystem, BaseItem item, ItemResolveArgs args) private static void EnsureDates(IFileSystem fileSystem, BaseItem item, ItemResolveArgs args)
{ {
if (fileSystem == null)
{
throw new ArgumentNullException(nameof(fileSystem));
}
if (item == null)
{
throw new ArgumentNullException(nameof(item));
}
if (args == null)
{
throw new ArgumentNullException(nameof(args));
}
// See if a different path came out of the resolver than what went in // See if a different path came out of the resolver than what went in
if (!fileSystem.AreEqual(args.Path, item.Path)) if (!fileSystem.AreEqual(args.Path, item.Path))
{ {
@ -136,7 +110,7 @@ namespace Emby.Server.Implementations.Library
if (childData != null) if (childData != null)
{ {
SetDateCreated(item, fileSystem, childData); SetDateCreated(item, childData);
} }
else else
{ {
@ -144,17 +118,17 @@ namespace Emby.Server.Implementations.Library
if (fileData.Exists) if (fileData.Exists)
{ {
SetDateCreated(item, fileSystem, fileData); SetDateCreated(item, fileData);
} }
} }
} }
else else
{ {
SetDateCreated(item, fileSystem, args.FileInfo); SetDateCreated(item, args.FileInfo);
} }
} }
private static void SetDateCreated(BaseItem item, IFileSystem fileSystem, FileSystemMetadata info) private static void SetDateCreated(BaseItem item, FileSystemMetadata? info)
{ {
var config = BaseItem.ConfigurationManager.GetMetadataConfiguration(); var config = BaseItem.ConfigurationManager.GetMetadataConfiguration();
@ -163,7 +137,7 @@ namespace Emby.Server.Implementations.Library
// directoryService.getFile may return null // directoryService.getFile may return null
if (info != null) if (info != null)
{ {
var dateCreated = fileSystem.GetCreationTimeUtc(info); var dateCreated = info.CreationTimeUtc;
if (dateCreated.Equals(DateTime.MinValue)) if (dateCreated.Equals(DateTime.MinValue))
{ {

View File

@ -120,8 +120,7 @@ namespace MediaBrowser.Controller.Entities
var args = new ItemResolveArgs(ConfigurationManager.ApplicationPaths, directoryService) var args = new ItemResolveArgs(ConfigurationManager.ApplicationPaths, directoryService)
{ {
FileInfo = FileSystem.GetDirectoryInfo(path), FileInfo = FileSystem.GetDirectoryInfo(path)
Path = path
}; };
// Gather child folder and files // Gather child folder and files

View File

@ -271,7 +271,6 @@ namespace MediaBrowser.Controller.Entities
var args = new ItemResolveArgs(ConfigurationManager.ApplicationPaths, directoryService) var args = new ItemResolveArgs(ConfigurationManager.ApplicationPaths, directoryService)
{ {
FileInfo = FileSystem.GetDirectoryInfo(path), FileInfo = FileSystem.GetDirectoryInfo(path),
Path = path,
Parent = GetParent() as Folder, Parent = GetParent() as Folder,
CollectionType = CollectionType CollectionType = CollectionType
}; };

View File

@ -60,10 +60,10 @@ namespace MediaBrowser.Controller.Library
public FileSystemMetadata FileInfo { get; set; } public FileSystemMetadata FileInfo { get; set; }
/// <summary> /// <summary>
/// Gets or sets the path. /// Gets the path.
/// </summary> /// </summary>
/// <value>The path.</value> /// <value>The path.</value>
public string Path { get; set; } public string Path => FileInfo.FullName;
/// <summary> /// <summary>
/// Gets a value indicating whether this instance is directory. /// Gets a value indicating whether this instance is directory.
@ -87,7 +87,7 @@ namespace MediaBrowser.Controller.Library
return false; return false;
} }
var parentDir = System.IO.Path.GetDirectoryName(Path) ?? string.Empty; var parentDir = FileInfo.DirectoryName ?? string.Empty;
return parentDir.Length > _appPaths.RootFolderPath.Length return parentDir.Length > _appPaths.RootFolderPath.Length
&& parentDir.StartsWith(_appPaths.RootFolderPath, StringComparison.OrdinalIgnoreCase); && parentDir.StartsWith(_appPaths.RootFolderPath, StringComparison.OrdinalIgnoreCase);

View File

@ -6,6 +6,7 @@ using MediaBrowser.Controller.Entities.TV;
using MediaBrowser.Controller.Library; using MediaBrowser.Controller.Library;
using MediaBrowser.Controller.Providers; using MediaBrowser.Controller.Providers;
using MediaBrowser.Model.Entities; using MediaBrowser.Model.Entities;
using MediaBrowser.Model.IO;
using Moq; using Moq;
using Xunit; using Xunit;
@ -28,7 +29,10 @@ namespace Jellyfin.Server.Implementations.Tests.Library
{ {
Parent = parent, Parent = parent,
CollectionType = CollectionType.TvShows, CollectionType = CollectionType.TvShows,
Path = "All My Children/Season 01/Extras/All My Children S01E01 - Behind The Scenes.mkv" FileInfo = new FileSystemMetadata()
{
FullName = "All My Children/Season 01/Extras/All My Children S01E01 - Behind The Scenes.mkv"
}
}; };
Assert.Null(episodeResolver.Resolve(itemResolveArgs)); Assert.Null(episodeResolver.Resolve(itemResolveArgs));
@ -48,7 +52,10 @@ namespace Jellyfin.Server.Implementations.Tests.Library
{ {
Parent = series, Parent = series,
CollectionType = CollectionType.TvShows, CollectionType = CollectionType.TvShows,
Path = "Extras/Extras S01E01.mkv" FileInfo = new FileSystemMetadata()
{
FullName = "Extras/Extras S01E01.mkv"
}
}; };
Assert.NotNull(episodeResolver.Resolve(itemResolveArgs)); Assert.NotNull(episodeResolver.Resolve(itemResolveArgs));
} }