reduce recursive querying

This commit is contained in:
Luke Pulverenti
2016-05-06 00:50:39 -04:00
parent 242fb3c770
commit 5a496a1fc8
10 changed files with 263 additions and 289 deletions

View File

@@ -397,12 +397,6 @@ namespace MediaBrowser.Server.Implementations.Dto
collectionFolder.GetViewType(user);
}
var playlist = item as Playlist;
if (playlist != null)
{
AttachLinkedChildImages(dto, playlist, user, options);
}
if (fields.Contains(ItemFields.CanDelete))
{
dto.CanDelete = user == null
@@ -1564,45 +1558,6 @@ namespace MediaBrowser.Server.Implementations.Dto
}
}
private void AttachLinkedChildImages(BaseItemDto dto, Folder folder, User user, DtoOptions options)
{
List<BaseItem> linkedChildren = null;
var backdropLimit = options.GetImageLimit(ImageType.Backdrop);
if (backdropLimit > 0 && dto.BackdropImageTags.Count == 0)
{
linkedChildren = user == null
? folder.GetRecursiveChildren().ToList()
: folder.GetRecursiveChildren(user).ToList();
var parentWithBackdrop = linkedChildren.FirstOrDefault(i => i.GetImages(ImageType.Backdrop).Any());
if (parentWithBackdrop != null)
{
dto.ParentBackdropItemId = GetDtoId(parentWithBackdrop);
dto.ParentBackdropImageTags = GetBackdropImageTags(parentWithBackdrop, backdropLimit);
}
}
if (!dto.ImageTags.ContainsKey(ImageType.Primary) && options.GetImageLimit(ImageType.Primary) > 0)
{
if (linkedChildren == null)
{
linkedChildren = user == null
? folder.GetRecursiveChildren().ToList()
: folder.GetRecursiveChildren(user).ToList();
}
var parentWithImage = linkedChildren.FirstOrDefault(i => i.GetImages(ImageType.Primary).Any());
if (parentWithImage != null)
{
dto.ParentPrimaryImageItemId = GetDtoId(parentWithImage);
dto.ParentPrimaryImageTag = GetImageCacheTag(parentWithImage, ImageType.Primary);
}
}
}
private string GetMappedPath(IHasMetadata item)
{
var path = item.Path;

View File

@@ -2,7 +2,6 @@
using MediaBrowser.Controller.Library;
using MediaBrowser.Model.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
@@ -35,25 +34,20 @@ namespace MediaBrowser.Server.Implementations.Library.Validators
/// <returns>Task.</returns>
public async Task Run(IProgress<double> progress, CancellationToken cancellationToken)
{
var items = _libraryManager.RootFolder.GetRecursiveChildren()
.SelectMany(i => i.Studios)
.DistinctNames()
.ToList();
var items = _libraryManager.GetItemList(new InternalItemsQuery
{
IncludeItemTypes = new[] { typeof(Studio).Name }
}).ToList();
var numComplete = 0;
var count = items.Count;
var validIds = new List<Guid>();
foreach (var name in items)
foreach (var item in items)
{
try
{
var itemByName = _libraryManager.GetStudio(name);
validIds.Add(itemByName.Id);
await itemByName.RefreshMetadata(cancellationToken).ConfigureAwait(false);
await item.RefreshMetadata(cancellationToken).ConfigureAwait(false);
}
catch (OperationCanceledException)
{
@@ -62,7 +56,7 @@ namespace MediaBrowser.Server.Implementations.Library.Validators
}
catch (Exception ex)
{
_logger.ErrorException("Error refreshing {0}", ex, name);
_logger.ErrorException("Error refreshing {0}", ex, item.Name);
}
numComplete++;
@@ -73,28 +67,6 @@ namespace MediaBrowser.Server.Implementations.Library.Validators
progress.Report(percent);
}
var allIds = _libraryManager.GetItemIds(new InternalItemsQuery
{
IncludeItemTypes = new[] { typeof(Studio).Name }
});
var invalidIds = allIds
.Except(validIds)
.ToList();
foreach (var id in invalidIds)
{
cancellationToken.ThrowIfCancellationRequested();
var item = _libraryManager.GetItemById(id);
await _libraryManager.DeleteItem(item, new DeleteOptions
{
DeleteFileLocation = false
}).ConfigureAwait(false);
}
progress.Report(100);
}
}

View File

@@ -82,7 +82,7 @@ namespace MediaBrowser.Server.Implementations.Persistence
private IDbCommand _updateInheritedRatingCommand;
private IDbCommand _updateInheritedTagsCommand;
public const int LatestSchemaVersion = 69;
public const int LatestSchemaVersion = 71;
/// <summary>
/// Initializes a new instance of the <see cref="SqliteItemRepository"/> class.
@@ -226,6 +226,9 @@ namespace MediaBrowser.Server.Implementations.Persistence
_connection.AddColumn(Logger, "TypedBaseItems", "InheritedTags", "Text");
_connection.AddColumn(Logger, "TypedBaseItems", "CleanName", "Text");
_connection.AddColumn(Logger, "TypedBaseItems", "PresentationUniqueKey", "Text");
_connection.AddColumn(Logger, "TypedBaseItems", "SlugName", "Text");
_connection.AddColumn(Logger, "TypedBaseItems", "OriginalTitle", "Text");
_connection.AddColumn(Logger, "TypedBaseItems", "PrimaryVersionId", "Text");
string[] postQueries =
{
@@ -367,7 +370,9 @@ namespace MediaBrowser.Server.Implementations.Persistence
"Tags",
"SourceType",
"TrailerTypes",
"DateModifiedDuringLastRefresh"
"DateModifiedDuringLastRefresh",
"OriginalTitle",
"PrimaryVersionId"
};
private readonly string[] _mediaStreamSaveColumns =
@@ -476,7 +481,10 @@ namespace MediaBrowser.Server.Implementations.Persistence
"DateModifiedDuringLastRefresh",
"InheritedTags",
"CleanName",
"PresentationUniqueKey"
"PresentationUniqueKey",
"SlugName",
"OriginalTitle",
"PrimaryVersionId"
};
_saveItemCommand = _connection.CreateCommand();
_saveItemCommand.CommandText = "replace into TypedBaseItems (" + string.Join(",", saveColumns.ToArray()) + ") values (";
@@ -810,7 +818,20 @@ namespace MediaBrowser.Server.Implementations.Persistence
{
_saveItemCommand.GetParameter(index++).Value = item.Name.RemoveDiacritics();
}
_saveItemCommand.GetParameter(index++).Value = item.PresentationUniqueKey;
_saveItemCommand.GetParameter(index++).Value = item.SlugName;
_saveItemCommand.GetParameter(index++).Value = item.OriginalTitle;
var video = item as Video;
if (video != null)
{
_saveItemCommand.GetParameter(index++).Value = video.PrimaryVersionId;
}
else
{
_saveItemCommand.GetParameter(index++).Value = null;
}
_saveItemCommand.Transaction = transaction;
@@ -1189,6 +1210,20 @@ namespace MediaBrowser.Server.Implementations.Persistence
item.DateModifiedDuringLastRefresh = reader.GetDateTime(51).ToUniversalTime();
}
if (!reader.IsDBNull(52))
{
item.OriginalTitle = reader.GetString(52);
}
var video = item as Video;
if (video != null)
{
if (!reader.IsDBNull(53))
{
video.PrimaryVersionId = reader.GetString(53);
}
}
return item;
}
@@ -2070,6 +2105,19 @@ namespace MediaBrowser.Server.Implementations.Persistence
cmd.Parameters.Add(cmd, "@PersonName", DbType.String).Value = query.Person;
}
if (!string.IsNullOrWhiteSpace(query.SlugName))
{
if (_config.Configuration.SchemaVersion >= 70)
{
whereClauses.Add("SlugName=@SlugName");
}
else
{
whereClauses.Add("Name=@SlugName");
}
cmd.Parameters.Add(cmd, "@SlugName", DbType.String).Value = query.SlugName;
}
if (!string.IsNullOrWhiteSpace(query.Name))
{
if (_config.Configuration.SchemaVersion >= 66)
@@ -2340,6 +2388,11 @@ namespace MediaBrowser.Server.Implementations.Persistence
private bool EnableGroupByPresentationUniqueKey(InternalItemsQuery query)
{
if (!query.GroupByPresentationUniqueKey)
{
return false;
}
if (!string.IsNullOrWhiteSpace(query.PresentationUniqueKey))
{
return false;