restore changes

This commit is contained in:
Luke Pulverenti 2015-11-13 15:53:29 -05:00
parent e73b353b20
commit 32cec257ba
2 changed files with 132 additions and 56 deletions

View File

@ -27,6 +27,7 @@ using MediaBrowser.Server.Implementations.Library.Validators;
using MediaBrowser.Server.Implementations.Logging; using MediaBrowser.Server.Implementations.Logging;
using MediaBrowser.Server.Implementations.ScheduledTasks; using MediaBrowser.Server.Implementations.ScheduledTasks;
using System; using System;
using System.Collections;
using System.Collections.Concurrent; using System.Collections.Concurrent;
using System.Collections.Generic; using System.Collections.Generic;
using System.Globalization; using System.Globalization;
@ -36,6 +37,7 @@ using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
using CommonIO; using CommonIO;
using MediaBrowser.Model.Extensions; using MediaBrowser.Model.Extensions;
using MediaBrowser.Model.Library;
using MoreLinq; using MoreLinq;
using SortOrder = MediaBrowser.Model.Entities.SortOrder; using SortOrder = MediaBrowser.Model.Entities.SortOrder;
@ -140,6 +142,7 @@ namespace MediaBrowser.Server.Implementations.Library
private readonly Func<ILibraryMonitor> _libraryMonitorFactory; private readonly Func<ILibraryMonitor> _libraryMonitorFactory;
private readonly Func<IProviderManager> _providerManagerFactory; private readonly Func<IProviderManager> _providerManagerFactory;
private readonly Func<IUserViewManager> _userviewManager;
/// <summary> /// <summary>
/// The _library items cache /// The _library items cache
@ -167,7 +170,7 @@ namespace MediaBrowser.Server.Implementations.Library
/// <param name="userManager">The user manager.</param> /// <param name="userManager">The user manager.</param>
/// <param name="configurationManager">The configuration manager.</param> /// <param name="configurationManager">The configuration manager.</param>
/// <param name="userDataRepository">The user data repository.</param> /// <param name="userDataRepository">The user data repository.</param>
public LibraryManager(ILogger logger, ITaskManager taskManager, IUserManager userManager, IServerConfigurationManager configurationManager, IUserDataManager userDataRepository, Func<ILibraryMonitor> libraryMonitorFactory, IFileSystem fileSystem, Func<IProviderManager> providerManagerFactory) public LibraryManager(ILogger logger, ITaskManager taskManager, IUserManager userManager, IServerConfigurationManager configurationManager, IUserDataManager userDataRepository, Func<ILibraryMonitor> libraryMonitorFactory, IFileSystem fileSystem, Func<IProviderManager> providerManagerFactory, Func<IUserViewManager> userviewManager)
{ {
_logger = logger; _logger = logger;
_taskManager = taskManager; _taskManager = taskManager;
@ -177,6 +180,7 @@ namespace MediaBrowser.Server.Implementations.Library
_libraryMonitorFactory = libraryMonitorFactory; _libraryMonitorFactory = libraryMonitorFactory;
_fileSystem = fileSystem; _fileSystem = fileSystem;
_providerManagerFactory = providerManagerFactory; _providerManagerFactory = providerManagerFactory;
_userviewManager = userviewManager;
ByReferenceItems = new ConcurrentDictionary<Guid, BaseItem>(); ByReferenceItems = new ConcurrentDictionary<Guid, BaseItem>();
_libraryItemsCache = new ConcurrentDictionary<Guid, BaseItem>(); _libraryItemsCache = new ConcurrentDictionary<Guid, BaseItem>();
@ -580,7 +584,7 @@ namespace MediaBrowser.Server.Implementations.Library
}; };
// Return null if ignore rules deem that we should do so // Return null if ignore rules deem that we should do so
if (EntityResolutionIgnoreRules.Any(r => r.ShouldIgnore(args))) if (IgnoreFile(args.FileInfo, args.Parent))
{ {
return null; return null;
} }
@ -599,9 +603,9 @@ namespace MediaBrowser.Server.Implementations.Library
// Example: if \\server\movies exists, then strip out \\server\movies\action // Example: if \\server\movies exists, then strip out \\server\movies\action
if (isPhysicalRoot) if (isPhysicalRoot)
{ {
var paths = NormalizeRootPathList(fileSystemDictionary.Values); var paths = NormalizeRootPathList(fileSystemDictionary.Keys);
fileSystemDictionary = paths.ToDictionary(i => i.FullName); fileSystemDictionary = paths.Select(_fileSystem.GetDirectoryInfo).ToDictionary(i => i.FullName);
} }
args.FileSystemDictionary = fileSystemDictionary; args.FileSystemDictionary = fileSystemDictionary;
@ -616,12 +620,14 @@ namespace MediaBrowser.Server.Implementations.Library
return ResolveItem(args); return ResolveItem(args);
} }
public IEnumerable<FileSystemMetadata> NormalizeRootPathList(IEnumerable<FileSystemMetadata> paths) public bool IgnoreFile(FileSystemMetadata file, BaseItem parent)
{ {
var originalList = paths.ToList(); return EntityResolutionIgnoreRules.Any(r => r.ShouldIgnore(file, parent));
}
var list = originalList.Where(i => i.IsDirectory) public IEnumerable<string> NormalizeRootPathList(IEnumerable<string> paths)
.Select(i => _fileSystem.NormalizePath(i.FullName)) {
var list = paths.Select(_fileSystem.NormalizePath)
.Distinct(StringComparer.OrdinalIgnoreCase) .Distinct(StringComparer.OrdinalIgnoreCase)
.ToList(); .ToList();
@ -633,9 +639,7 @@ namespace MediaBrowser.Server.Implementations.Library
_logger.Info("Found duplicate path: {0}", dupe); _logger.Info("Found duplicate path: {0}", dupe);
} }
var newList = list.Except(dupes, StringComparer.OrdinalIgnoreCase).Select(_fileSystem.GetDirectoryInfo).ToList(); return list.Except(dupes, StringComparer.OrdinalIgnoreCase);
newList.AddRange(originalList.Where(i => !i.IsDirectory));
return newList;
} }
/// <summary> /// <summary>
@ -651,7 +655,7 @@ namespace MediaBrowser.Server.Implementations.Library
public IEnumerable<BaseItem> ResolvePaths(IEnumerable<FileSystemMetadata> files, IDirectoryService directoryService, Folder parent, string collectionType) public IEnumerable<BaseItem> ResolvePaths(IEnumerable<FileSystemMetadata> files, IDirectoryService directoryService, Folder parent, string collectionType)
{ {
var fileList = files.ToList(); var fileList = files.Where(i => !IgnoreFile(i, parent)).ToList();
if (parent != null) if (parent != null)
{ {
@ -732,6 +736,13 @@ namespace MediaBrowser.Server.Implementations.Library
folder = dbItem; folder = dbItem;
} }
if (folder.ParentId != rootFolder.Id)
{
folder.ParentId = rootFolder.Id;
var task = folder.UpdateToRepository(ItemUpdateType.MetadataImport, CancellationToken.None);
Task.WaitAll(task);
}
rootFolder.AddVirtualChild(folder); rootFolder.AddVirtualChild(folder);
RegisterItem(folder); RegisterItem(folder);
@ -1265,6 +1276,11 @@ namespace MediaBrowser.Server.Implementations.Library
public QueryResult<BaseItem> GetItems(InternalItemsQuery query) public QueryResult<BaseItem> GetItems(InternalItemsQuery query)
{ {
if (query.User != null)
{
AddUserToQuery(query, query.User);
}
var result = ItemRepository.GetItemIdsList(query); var result = ItemRepository.GetItemIdsList(query);
var items = result.Select(GetItemById).Where(i => i != null).ToArray(); var items = result.Select(GetItemById).Where(i => i != null).ToArray();
@ -1277,14 +1293,66 @@ namespace MediaBrowser.Server.Implementations.Library
public QueryResult<BaseItem> QueryItems(InternalItemsQuery query) public QueryResult<BaseItem> QueryItems(InternalItemsQuery query)
{ {
if (query.User != null)
{
AddUserToQuery(query, query.User);
}
return ItemRepository.GetItems(query); return ItemRepository.GetItems(query);
} }
public List<Guid> GetItemIds(InternalItemsQuery query) public List<Guid> GetItemIds(InternalItemsQuery query)
{ {
if (query.User != null)
{
AddUserToQuery(query, query.User);
}
return ItemRepository.GetItemIdsList(query); return ItemRepository.GetItemIdsList(query);
} }
public IEnumerable<BaseItem> GetItems(InternalItemsQuery query, IEnumerable<string> parentIds)
{
var parents = parentIds.Select(i => GetItemById(new Guid(i))).ToList();
query.AncestorIds = parents.SelectMany(i => i.GetIdsForAncestorQuery()).Select(i => i.ToString("N")).ToArray();
return GetItemIds(query).Select(GetItemById);
}
public QueryResult<BaseItem> GetItemsResult(InternalItemsQuery query, IEnumerable<string> parentIds)
{
var parents = parentIds.Select(i => GetItemById(new Guid(i))).ToList();
query.AncestorIds = parents.SelectMany(i => i.GetIdsForAncestorQuery()).Select(i => i.ToString("N")).ToArray();
return GetItems(query);
}
private void AddUserToQuery(InternalItemsQuery query, User user)
{
if (query.AncestorIds.Length == 0 && !query.ParentId.HasValue && query.ChannelIds.Length == 0)
{
//var userViews = _userviewManager().GetUserViews(new UserViewQuery
//{
// UserId = user.Id.ToString("N"),
// IncludeHidden = true
//}, CancellationToken.None).Result.ToList();
//query.AncestorIds = userViews.SelectMany(i => i.GetIdsForAncestorQuery()).Distinct().Select(i => i.ToString("N")).ToArray();
}
// TODO: handle blocking by tags
query.MaxParentalRating = user.Policy.MaxParentalRating;
if (user.Policy.MaxParentalRating.HasValue)
{
query.BlockUnratedItems = user.Policy.BlockUnratedItems;
}
}
/// <summary> /// <summary>
/// Gets the intros. /// Gets the intros.
/// </summary> /// </summary>
@ -1577,9 +1645,9 @@ namespace MediaBrowser.Server.Implementations.Library
public IEnumerable<Folder> GetCollectionFolders(BaseItem item) public IEnumerable<Folder> GetCollectionFolders(BaseItem item)
{ {
while (!(item.Parent is AggregateFolder) && item.Parent != null) while (!(item.GetParent() is AggregateFolder) && item.GetParent() != null)
{ {
item = item.Parent; item = item.GetParent();
} }
if (item == null) if (item == null)
@ -1616,7 +1684,7 @@ namespace MediaBrowser.Server.Implementations.Library
return type; return type;
} }
return item.Parents return item.GetParents()
.Select(GetConfiguredContentType) .Select(GetConfiguredContentType)
.LastOrDefault(i => !string.IsNullOrWhiteSpace(i)); .LastOrDefault(i => !string.IsNullOrWhiteSpace(i));
} }
@ -1653,9 +1721,9 @@ namespace MediaBrowser.Server.Implementations.Library
private string GetTopFolderContentType(BaseItem item) private string GetTopFolderContentType(BaseItem item)
{ {
while (!(item.Parent is AggregateFolder) && item.Parent != null) while (!(item.GetParent() is AggregateFolder) && item.GetParent() != null)
{ {
item = item.Parent; item = item.GetParent();
} }
if (item == null) if (item == null)
@ -1719,8 +1787,7 @@ namespace MediaBrowser.Server.Implementations.Library
if (!string.Equals(viewType, item.ViewType, StringComparison.OrdinalIgnoreCase)) if (!string.Equals(viewType, item.ViewType, StringComparison.OrdinalIgnoreCase))
{ {
item.ViewType = viewType; refresh = true;
await item.UpdateToRepository(ItemUpdateType.MetadataEdit, cancellationToken).ConfigureAwait(false);
} }
if (!refresh) if (!refresh)
@ -1816,14 +1883,9 @@ namespace MediaBrowser.Server.Implementations.Library
isNew = true; isNew = true;
} }
if (!item.UserId.HasValue) if (!item.UserId.HasValue || !string.Equals(viewType, item.ViewType, StringComparison.OrdinalIgnoreCase))
{ {
item.UserId = user.Id; item.UserId = user.Id;
await item.UpdateToRepository(ItemUpdateType.MetadataEdit, cancellationToken).ConfigureAwait(false);
}
if (!string.Equals(viewType, item.ViewType, StringComparison.OrdinalIgnoreCase))
{
item.ViewType = viewType; item.ViewType = viewType;
await item.UpdateToRepository(ItemUpdateType.MetadataEdit, cancellationToken).ConfigureAwait(false); await item.UpdateToRepository(ItemUpdateType.MetadataEdit, cancellationToken).ConfigureAwait(false);
} }

View File

@ -12,6 +12,7 @@ using System.Collections.Generic;
using System.Threading; using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
using CommonIO; using CommonIO;
using MediaBrowser.Controller.Channels;
using MediaBrowser.Controller.Entities.Audio; using MediaBrowser.Controller.Entities.Audio;
namespace MediaBrowser.Server.Implementations.Persistence namespace MediaBrowser.Server.Implementations.Persistence
@ -24,6 +25,8 @@ namespace MediaBrowser.Server.Implementations.Persistence
private readonly IServerConfigurationManager _config; private readonly IServerConfigurationManager _config;
private readonly IFileSystem _fileSystem; private readonly IFileSystem _fileSystem;
public const int MigrationVersion = 4;
public CleanDatabaseScheduledTask(ILibraryManager libraryManager, IItemRepository itemRepo, ILogger logger, IServerConfigurationManager config, IFileSystem fileSystem) public CleanDatabaseScheduledTask(ILibraryManager libraryManager, IItemRepository itemRepo, ILogger logger, IServerConfigurationManager config, IFileSystem fileSystem)
{ {
_libraryManager = libraryManager; _libraryManager = libraryManager;
@ -64,6 +67,8 @@ namespace MediaBrowser.Server.Implementations.Persistence
innerProgress.RegisterAction(p => progress.Report(45 + (.55 * p))); innerProgress.RegisterAction(p => progress.Report(45 + (.55 * p)));
await CleanDeletedItems(cancellationToken, innerProgress).ConfigureAwait(false); await CleanDeletedItems(cancellationToken, innerProgress).ConfigureAwait(false);
progress.Report(100); progress.Report(100);
await _itemRepo.UpdateInheritedValues(cancellationToken).ConfigureAwait(false);
} }
private async Task UpdateToLatestSchema(CancellationToken cancellationToken, IProgress<double> progress) private async Task UpdateToLatestSchema(CancellationToken cancellationToken, IProgress<double> progress)
@ -115,9 +120,9 @@ namespace MediaBrowser.Server.Implementations.Persistence
progress.Report(percent * 100); progress.Report(percent * 100);
} }
if (!_config.Configuration.DisableStartupScan) if (_config.Configuration.MigrationVersion < MigrationVersion)
{ {
_config.Configuration.DisableStartupScan = true; _config.Configuration.MigrationVersion = MigrationVersion;
_config.SaveConfiguration(); _config.SaveConfiguration();
} }
@ -144,7 +149,7 @@ namespace MediaBrowser.Server.Implementations.Persistence
if (item != null) if (item != null)
{ {
_logger.Info("Cleaning item {0} type: {1} path: {2}", item.Name, item.GetType().Name, item.Path ?? string.Empty); _logger.Debug("Cleaning item {0} type: {1} path: {2}", item.Name, item.GetType().Name, item.Path ?? string.Empty);
await _libraryManager.DeleteItem(item, new DeleteOptions await _libraryManager.DeleteItem(item, new DeleteOptions
{ {
@ -170,7 +175,18 @@ namespace MediaBrowser.Server.Implementations.Persistence
//Limit = limit, //Limit = limit,
// These have their own cleanup routines // These have their own cleanup routines
ExcludeItemTypes = new[] { typeof(Person).Name, typeof(Genre).Name, typeof(MusicGenre).Name, typeof(GameGenre).Name, typeof(Studio).Name, typeof(Year).Name } ExcludeItemTypes = new[]
{
typeof(Person).Name,
typeof(Genre).Name,
typeof(MusicGenre).Name,
typeof(GameGenre).Name,
typeof(Studio).Name,
typeof(Year).Name,
typeof(Channel).Name,
typeof(AggregateFolder).Name,
typeof(CollectionFolder).Name
}
}); });
var numComplete = 0; var numComplete = 0;
@ -198,8 +214,6 @@ namespace MediaBrowser.Server.Implementations.Persistence
continue; continue;
} }
_logger.Info("Deleting item from database {0} because path no longer exists. type: {1} path: {2}", libraryItem.Name, libraryItem.GetType().Name, libraryItem.Path ?? string.Empty);
await _libraryManager.DeleteItem(libraryItem, new DeleteOptions await _libraryManager.DeleteItem(libraryItem, new DeleteOptions
{ {
DeleteFileLocation = false DeleteFileLocation = false