Compute hash only when one is not computed in DB, small optimizations here and there

This commit is contained in:
Vasily 2020-05-19 14:50:14 +03:00
parent f18293bf76
commit a226a4ee03
5 changed files with 37 additions and 68 deletions

View File

@ -1141,20 +1141,10 @@ namespace Emby.Server.Implementations.Data
public string ToValueString(ItemImageInfo image) public string ToValueString(ItemImageInfo image)
{ {
var delimeter = "*"; const string delimeter = "*";
var path = image.Path; var path = image.Path ?? string.Empty;
var hash = image.Hash; var hash = image.Hash ?? string.Empty;
if (path == null)
{
path = string.Empty;
}
if (hash == null)
{
hash = string.Empty;
}
return GetPathToSave(path) + return GetPathToSave(path) +
delimeter + delimeter +

View File

@ -1825,17 +1825,26 @@ namespace Emby.Server.Implementations.Library
public void UpdateImages(BaseItem item) public void UpdateImages(BaseItem item)
{ {
item.ImageInfos if (item == null)
.Where(i => (i.Width == 0 || i.Height == 0)) {
.ToList() throw new ArgumentNullException(nameof(item));
.ForEach(x => }
{
string blurhash = ImageProcessor.GetImageHash(x.Path); var outdated = item.ImageInfos
ImageDimensions size = ImageProcessor.GetImageDimensions(item, x); .Where(i => (i.Width == 0 || i.Height == 0 || string.IsNullOrEmpty(i.Hash)))
x.Width = size.Width; .ToList();
x.Height = size.Height; if (outdated.Count == 0)
x.Hash = blurhash; {
}); return;
}
outdated.ForEach(img =>
{
ImageDimensions size = ImageProcessor.GetImageDimensions(item, img);
img.Width = size.Width;
img.Height = size.Height;
img.Hash = ImageProcessor.GetImageHash(img.Path);
});
_itemRepository.SaveImages(item); _itemRepository.SaveImages(item);
@ -1905,34 +1914,6 @@ namespace Emby.Server.Implementations.Library
UpdateItems(new[] { item }, parent, updateReason, cancellationToken); UpdateItems(new[] { item }, parent, updateReason, cancellationToken);
} }
/// <summary>
/// Updates everything in the database.
/// </summary>
public void UpdateAll()
{
Task.Run(() =>
{
var items = _itemRepository.GetItemList(new InternalItemsQuery {
Recursive = true
});
foreach (var item in items)
{
_logger.LogDebug("Updating item {Name} ({ItemId})",
item.Name,
item.Id);
try
{
item.UpdateToRepository(ItemUpdateType.MetadataEdit, CancellationToken.None);
}
catch (Exception ex)
{
_logger.LogError(ex, "Updating item {ItemId} failed", item.Id);
}
}
_logger.LogDebug("All items have been updated");
});
}
/// <summary> /// <summary>
/// Reports the item removed. /// Reports the item removed.
/// </summary> /// </summary>

View File

@ -3,6 +3,7 @@ using System.Collections.Generic;
using System.Globalization; using System.Globalization;
using System.IO; using System.IO;
using System.Linq; using System.Linq;
using System.Runtime.CompilerServices;
using System.Threading; using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
using MediaBrowser.Common.Extensions; using MediaBrowser.Common.Extensions;
@ -280,9 +281,16 @@ namespace MediaBrowser.Api.Images
public List<ImageInfo> GetItemImageInfos(BaseItem item) public List<ImageInfo> GetItemImageInfos(BaseItem item)
{ {
var list = new List<ImageInfo>(); var list = new List<ImageInfo>();
var itemImages = item.ImageInfos; var itemImages = item.ImageInfos;
if (itemImages.Length == 0)
{
// short-circuit
return list;
}
_libraryManager.UpdateImages(item); // this makes sure dimensions and hashes are correct
foreach (var image in itemImages) foreach (var image in itemImages)
{ {
if (!item.AllowsMultipleImages(image.Type)) if (!item.AllowsMultipleImages(image.Type))
@ -323,7 +331,7 @@ namespace MediaBrowser.Api.Images
{ {
int? width = null; int? width = null;
int? height = null; int? height = null;
string? blurhash = null; string blurhash = null;
long length = 0; long length = 0;
try try
@ -333,13 +341,9 @@ namespace MediaBrowser.Api.Images
var fileInfo = _fileSystem.GetFileInfo(info.Path); var fileInfo = _fileSystem.GetFileInfo(info.Path);
length = fileInfo.Length; length = fileInfo.Length;
blurhash = _imageProcessor.GetImageHash(info.Path); blurhash = info.Hash;
info.Hash = blurhash; // TODO: this doesn't seem like the right thing to do width = info.Width;
height = info.Height;
ImageDimensions size = _imageProcessor.GetImageDimensions(item, info);
_libraryManager.UpdateImages(item);
width = size.Width;
height = size.Height;
if (width <= 0 || height <= 0) if (width <= 0 || height <= 0)
{ {

View File

@ -198,12 +198,6 @@ namespace MediaBrowser.Api
public void Post(UpdateItem request) public void Post(UpdateItem request)
{ {
if (request.ItemId == "*")
{
// Special case: Refresh everything in database. Probably not a great idea to run often.
_libraryManager.UpdateAll();
return;
}
var item = _libraryManager.GetItemById(request.ItemId); var item = _libraryManager.GetItemById(request.ItemId);
var newLockData = request.LockData ?? false; var newLockData = request.LockData ?? false;

View File

@ -195,8 +195,8 @@ namespace MediaBrowser.Controller.Library
/// Updates the item. /// Updates the item.
/// </summary> /// </summary>
void UpdateItems(IEnumerable<BaseItem> items, BaseItem parent, ItemUpdateType updateReason, CancellationToken cancellationToken); void UpdateItems(IEnumerable<BaseItem> items, BaseItem parent, ItemUpdateType updateReason, CancellationToken cancellationToken);
void UpdateItem(BaseItem item, BaseItem parent, ItemUpdateType updateReason, CancellationToken cancellationToken); void UpdateItem(BaseItem item, BaseItem parent, ItemUpdateType updateReason, CancellationToken cancellationToken);
void UpdateAll();
/// <summary> /// <summary>
/// Retrieves the item. /// Retrieves the item.