reduce sync db access

This commit is contained in:
Luke Pulverenti
2016-08-03 02:38:19 -04:00
parent 7e1a0baf9b
commit 04d480297d
9 changed files with 145 additions and 96 deletions

View File

@@ -97,8 +97,7 @@ namespace MediaBrowser.Server.Implementations.Dto
throw new ArgumentNullException("options");
}
var syncJobItems = GetSyncedItemProgress(options);
var syncDictionary = GetSyncedItemProgressDictionary(syncJobItems);
var syncDictionary = GetSyncedItemProgress(options);
var list = new List<BaseItemDto>();
var programTuples = new List<Tuple<BaseItem, BaseItemDto>>();
@@ -106,7 +105,7 @@ namespace MediaBrowser.Server.Implementations.Dto
foreach (var item in items)
{
var dto = await GetBaseItemDtoInternal(item, options, syncDictionary, user, owner).ConfigureAwait(false);
var dto = await GetBaseItemDtoInternal(item, options, user, owner).ConfigureAwait(false);
var tvChannel = item as LiveTvChannel;
if (tvChannel != null)
@@ -133,7 +132,7 @@ namespace MediaBrowser.Server.Implementations.Dto
}
}
FillSyncInfo(dto, item, options, user, syncJobItems);
FillSyncInfo(dto, item, options, user, syncDictionary);
list.Add(dto);
}
@@ -151,23 +150,11 @@ namespace MediaBrowser.Server.Implementations.Dto
return list;
}
private Dictionary<string, SyncedItemProgress> GetSyncedItemProgressDictionary(IEnumerable<SyncedItemProgress> items)
{
var dict = new Dictionary<string, SyncedItemProgress>();
foreach (var item in items)
{
dict[item.ItemId] = item;
}
return dict;
}
public BaseItemDto GetBaseItemDto(BaseItem item, DtoOptions options, User user = null, BaseItem owner = null)
{
var syncProgress = GetSyncedItemProgress(options);
var syncDictionary = GetSyncedItemProgress(options);
var dto = GetBaseItemDtoInternal(item, options, GetSyncedItemProgressDictionary(syncProgress), user, owner).Result;
var dto = GetBaseItemDtoInternal(item, options, user, owner).Result;
var tvChannel = item as LiveTvChannel;
if (tvChannel != null)
{
@@ -190,11 +177,11 @@ namespace MediaBrowser.Server.Implementations.Dto
SetItemByNameInfo(item, dto, GetTaggedItems(byName, user), user);
}
FillSyncInfo(dto, item, options, user, syncProgress);
FillSyncInfo(dto, item, options, user, syncDictionary);
return dto;
}
FillSyncInfo(dto, item, options, user, syncProgress);
FillSyncInfo(dto, item, options, user, syncDictionary);
return dto;
}
@@ -210,23 +197,24 @@ namespace MediaBrowser.Server.Implementations.Dto
return items;
}
private SyncedItemProgress[] GetSyncedItemProgress(DtoOptions options)
public Dictionary<string, SyncedItemProgress> GetSyncedItemProgress(DtoOptions options)
{
if (!options.Fields.Contains(ItemFields.SyncInfo))
if (!options.Fields.Contains(ItemFields.BasicSyncInfo) &&
!options.Fields.Contains(ItemFields.SyncInfo))
{
return new SyncedItemProgress[] { };
return new Dictionary<string, SyncedItemProgress>();
}
var deviceId = options.DeviceId;
if (string.IsNullOrWhiteSpace(deviceId))
{
return new SyncedItemProgress[] { };
return new Dictionary<string, SyncedItemProgress>();
}
var caps = _deviceManager().GetCapabilities(deviceId);
if (caps == null || !caps.SupportsSync)
{
return new SyncedItemProgress[] { };
return new Dictionary<string, SyncedItemProgress>();
}
return _syncManager.GetSyncedItemProgresses(new SyncJobItemQuery
@@ -240,12 +228,13 @@ namespace MediaBrowser.Server.Implementations.Dto
SyncJobItemStatus.ReadyToTransfer,
SyncJobItemStatus.Synced
}
}).Items;
});
}
public void FillSyncInfo(IEnumerable<Tuple<BaseItem, BaseItemDto>> tuples, DtoOptions options, User user)
{
if (options.Fields.Contains(ItemFields.SyncInfo))
if (options.Fields.Contains(ItemFields.BasicSyncInfo) ||
options.Fields.Contains(ItemFields.SyncInfo))
{
var syncProgress = GetSyncedItemProgress(options);
@@ -258,37 +247,40 @@ namespace MediaBrowser.Server.Implementations.Dto
}
}
private void FillSyncInfo(IHasSyncInfo dto, BaseItem item, DtoOptions options, User user, SyncedItemProgress[] syncProgress)
private void FillSyncInfo(IHasSyncInfo dto, BaseItem item, DtoOptions options, User user, Dictionary<string, SyncedItemProgress> syncProgress)
{
var hasFullSyncInfo = options.Fields.Contains(ItemFields.SyncInfo);
if (hasFullSyncInfo || options.Fields.Contains(ItemFields.BasicSyncInfo))
if (!options.Fields.Contains(ItemFields.BasicSyncInfo) &&
!hasFullSyncInfo)
{
var userCanSync = user != null && user.Policy.EnableSync;
dto.SupportsSync = userCanSync && _syncManager.SupportsSync(item);
return;
}
if (dto.SupportsSync ?? false)
{
dto.HasSyncJob = syncProgress.Any(i => i.Status != SyncJobItemStatus.Synced && string.Equals(i.ItemId, dto.Id, StringComparison.OrdinalIgnoreCase));
dto.IsSynced = syncProgress.Any(i => i.Status == SyncJobItemStatus.Synced && string.Equals(i.ItemId, dto.Id, StringComparison.OrdinalIgnoreCase));
if (hasFullSyncInfo)
SyncedItemProgress syncStatus;
if (syncProgress.TryGetValue(dto.Id, out syncStatus))
{
if (dto.IsSynced.Value)
if (syncStatus.Status == SyncJobItemStatus.Synced)
{
dto.SyncStatus = SyncJobItemStatus.Synced;
dto.SyncPercent = 100;
}
else
{
dto.SyncPercent = syncStatus.Progress;
}
else if (dto.HasSyncJob.Value)
if (hasFullSyncInfo)
{
dto.SyncStatus = syncProgress.Where(i => string.Equals(i.ItemId, dto.Id, StringComparison.OrdinalIgnoreCase)).Select(i => i.Status).Max();
dto.HasSyncJob = true;
dto.SyncStatus = syncStatus.Status;
}
}
}
}
private async Task<BaseItemDto> GetBaseItemDtoInternal(BaseItem item, DtoOptions options, Dictionary<string, SyncedItemProgress> syncProgress, User user = null, BaseItem owner = null)
private async Task<BaseItemDto> GetBaseItemDtoInternal(BaseItem item, DtoOptions options, User user = null, BaseItem owner = null)
{
var fields = options.Fields;
@@ -337,7 +329,7 @@ namespace MediaBrowser.Server.Implementations.Dto
if (user != null)
{
await AttachUserSpecificInfo(dto, item, user, fields, syncProgress).ConfigureAwait(false);
await AttachUserSpecificInfo(dto, item, user, fields).ConfigureAwait(false);
}
var hasMediaSources = item as IHasMediaSources;
@@ -400,11 +392,9 @@ namespace MediaBrowser.Server.Implementations.Dto
return dto;
}
public BaseItemDto GetItemByNameDto(BaseItem item, DtoOptions options, List<BaseItem> taggedItems, User user = null)
public BaseItemDto GetItemByNameDto(BaseItem item, DtoOptions options, List<BaseItem> taggedItems, Dictionary<string, SyncedItemProgress> syncProgress, User user = null)
{
var syncProgress = GetSyncedItemProgress(options);
var dto = GetBaseItemDtoInternal(item, options, GetSyncedItemProgressDictionary(syncProgress), user).Result;
var dto = GetBaseItemDtoInternal(item, options, user).Result;
if (taggedItems != null && options.Fields.Contains(ItemFields.ItemCounts))
{
@@ -452,28 +442,13 @@ namespace MediaBrowser.Server.Implementations.Dto
/// <param name="item">The item.</param>
/// <param name="user">The user.</param>
/// <param name="fields">The fields.</param>
/// <param name="syncProgress">The synchronize progress.</param>
private async Task AttachUserSpecificInfo(BaseItemDto dto, BaseItem item, User user, List<ItemFields> fields, Dictionary<string, SyncedItemProgress> syncProgress)
private async Task AttachUserSpecificInfo(BaseItemDto dto, BaseItem item, User user, List<ItemFields> fields)
{
if (item.IsFolder)
{
var folder = (Folder)item;
if (item.SourceType == SourceType.Library && folder.SupportsUserDataFromChildren && fields.Contains(ItemFields.SyncInfo))
{
// Skip the user data manager because we've already looped through the recursive tree and don't want to do it twice
// TODO: Improve in future
dto.UserData = GetUserItemDataDto(_userDataRepository.GetUserData(user, item));
await SetSpecialCounts(folder, user, dto, fields, syncProgress).ConfigureAwait(false);
dto.UserData.Played = dto.UserData.PlayedPercentage.HasValue &&
dto.UserData.PlayedPercentage.Value >= 100;
}
else
{
dto.UserData = await _userDataRepository.GetUserDataDto(item, dto, user).ConfigureAwait(false);
}
dto.UserData = await _userDataRepository.GetUserDataDto(item, dto, user).ConfigureAwait(false);
if (item.SourceType == SourceType.Library)
{
@@ -498,6 +473,12 @@ namespace MediaBrowser.Server.Implementations.Dto
dto.PlayAccess = item.GetPlayAccess(user);
if (fields.Contains(ItemFields.BasicSyncInfo) || fields.Contains(ItemFields.SyncInfo))
{
var userCanSync = user != null && user.Policy.EnableSync;
dto.SupportsSync = userCanSync && _syncManager.SupportsSync(item);
}
if (fields.Contains(ItemFields.SeasonUserData))
{
var episode = item as Episode;
@@ -1424,7 +1405,7 @@ namespace MediaBrowser.Server.Implementations.Dto
BaseItem parent = null;
var isFirst = true;
while (((!dto.HasLogo && logoLimit > 0) || (!dto.HasArtImage && artLimit > 0) || (!dto.HasThumb && thumbLimit > 0) || parent is Series) &&
while (((!dto.HasLogo && logoLimit > 0) || (!dto.HasArtImage && artLimit > 0) || (!dto.HasThumb && thumbLimit > 0) || parent is Series) &&
(parent = parent ?? (isFirst ? item.GetParent() ?? owner : parent)) != null)
{
if (parent == null)
@@ -1531,7 +1512,7 @@ namespace MediaBrowser.Server.Implementations.Dto
/// <param name="fields">The fields.</param>
/// <param name="syncProgress">The synchronize progress.</param>
/// <returns>Task.</returns>
private async Task SetSpecialCounts(Folder folder, User user, BaseItemDto dto, List<ItemFields> fields, Dictionary<string, SyncedItemProgress> syncProgress)
private async Task SetSpecialCounts(Folder folder, User user, BaseItemDto dto, List<ItemFields> fields, Dictionary<string, SyncJobItemStatus> syncProgress)
{
var recursiveItemCount = 0;
var unplayed = 0;
@@ -1580,10 +1561,10 @@ namespace MediaBrowser.Server.Implementations.Dto
}
double percent = 0;
SyncedItemProgress syncItemProgress;
SyncJobItemStatus syncItemProgress;
if (syncProgress.TryGetValue(child.Id.ToString("N"), out syncItemProgress))
{
switch (syncItemProgress.Status)
switch (syncItemProgress)
{
case SyncJobItemStatus.Synced:
percent = 100;