diff options
Diffstat (limited to 'Jellyfin.Server.Implementations')
8 files changed, 112 insertions, 43 deletions
diff --git a/Jellyfin.Server.Implementations/Item/BaseItemRepository.QueryBuilding.cs b/Jellyfin.Server.Implementations/Item/BaseItemRepository.QueryBuilding.cs index 4cd02bcd8d..0f8f8dc2dd 100644 --- a/Jellyfin.Server.Implementations/Item/BaseItemRepository.QueryBuilding.cs +++ b/Jellyfin.Server.Implementations/Item/BaseItemRepository.QueryBuilding.cs @@ -227,20 +227,23 @@ public sealed partial class BaseItemRepository { if (!string.IsNullOrWhiteSpace(filter.NameStartsWith)) { - var nameStartsWithLower = filter.NameStartsWith.ToLowerInvariant(); - dbQuery = dbQuery.Where(e => e.SortName!.ToLower().StartsWith(nameStartsWithLower)); + var paddedPrefix = BaseItem.NormalizeSortNameFilter(filter.NameStartsWith).ToLowerInvariant(); + var rawPrefix = filter.NameStartsWith.ToLowerInvariant(); + dbQuery = dbQuery.Where(e => + e.SortName!.ToLower().StartsWith(paddedPrefix) + || e.SortName!.ToLower().TrimStart('0').StartsWith(rawPrefix)); } if (!string.IsNullOrWhiteSpace(filter.NameStartsWithOrGreater)) { - var startsOrGreaterLower = filter.NameStartsWithOrGreater.ToLowerInvariant(); - dbQuery = dbQuery.Where(e => e.SortName!.ToLower().CompareTo(startsOrGreaterLower) >= 0); + var paddedValue = BaseItem.NormalizeSortNameFilter(filter.NameStartsWithOrGreater).ToLowerInvariant(); + dbQuery = dbQuery.Where(e => e.SortName!.ToLower().CompareTo(paddedValue) >= 0); } if (!string.IsNullOrWhiteSpace(filter.NameLessThan)) { - var lessThanLower = filter.NameLessThan.ToLowerInvariant(); - dbQuery = dbQuery.Where(e => e.SortName!.ToLower().CompareTo(lessThanLower) < 0); + var paddedValue = BaseItem.NormalizeSortNameFilter(filter.NameLessThan).ToLowerInvariant(); + dbQuery = dbQuery.Where(e => e.SortName!.ToLower().CompareTo(paddedValue) < 0); } return dbQuery; diff --git a/Jellyfin.Server.Implementations/Item/BaseItemRepository.Querying.cs b/Jellyfin.Server.Implementations/Item/BaseItemRepository.Querying.cs index 8d573569e9..a46cd9ab99 100644 --- a/Jellyfin.Server.Implementations/Item/BaseItemRepository.Querying.cs +++ b/Jellyfin.Server.Implementations/Item/BaseItemRepository.Querying.cs @@ -600,7 +600,7 @@ public sealed partial class BaseItemRepository } /// <inheritdoc /> - public QueryFiltersLegacy GetQueryFiltersLegacy(InternalItemsQuery filter) + public IReadOnlyList<int> GetYears(InternalItemsQuery filter) { ArgumentNullException.ThrowIfNull(filter); PrepareFilterQuery(filter); @@ -608,22 +608,34 @@ public sealed partial class BaseItemRepository using var context = _dbProvider.CreateDbContext(); var baseQuery = PrepareItemQuery(context, filter); baseQuery = TranslateQuery(baseQuery, context, filter); + return GetYears(baseQuery); + } - var matchingItemIds = baseQuery.Select(e => e.Id); + /// <inheritdoc /> + public IReadOnlyList<string> GetOfficialRatings(InternalItemsQuery filter) + { + ArgumentNullException.ThrowIfNull(filter); + PrepareFilterQuery(filter); - var years = baseQuery - .Where(e => e.ProductionYear != null && e.ProductionYear > 0) - .Select(e => e.ProductionYear!.Value) - .Distinct() - .OrderBy(y => y) - .ToArray(); + using var context = _dbProvider.CreateDbContext(); + var baseQuery = PrepareItemQuery(context, filter); + baseQuery = TranslateQuery(baseQuery, context, filter); + return GetOfficialRatings(baseQuery); + } - var officialRatings = baseQuery - .Where(e => e.OfficialRating != null && e.OfficialRating != string.Empty) - .Select(e => e.OfficialRating!) - .Distinct() - .OrderBy(r => r) - .ToArray(); + /// <inheritdoc /> + public QueryFiltersLegacy GetQueryFiltersLegacy(InternalItemsQuery filter) + { + ArgumentNullException.ThrowIfNull(filter); + PrepareFilterQuery(filter); + + using var context = _dbProvider.CreateDbContext(); + var baseQuery = PrepareItemQuery(context, filter); + baseQuery = TranslateQuery(baseQuery, context, filter); + + var matchingItemIds = baseQuery.Select(e => e.Id); + var years = GetYears(baseQuery); + var officialRatings = GetOfficialRatings(baseQuery); var tags = context.ItemValuesMap .Join( @@ -659,4 +671,24 @@ public sealed partial class BaseItemRepository Genres = genres }; } + + private static IReadOnlyList<int> GetYears(IQueryable<BaseItemEntity> query) + { + return query + .Where(e => e.ProductionYear != null && e.ProductionYear > 0) + .Select(e => e.ProductionYear!.Value) + .Distinct() + .OrderBy(y => y) + .ToArray(); + } + + private static IReadOnlyList<string> GetOfficialRatings(IQueryable<BaseItemEntity> query) + { + return query + .Where(e => e.OfficialRating != null && e.OfficialRating != string.Empty) + .Select(e => e.OfficialRating!) + .Distinct() + .OrderBy(r => r) + .ToArray(); + } } diff --git a/Jellyfin.Server.Implementations/Item/BaseItemRepository.TranslateQuery.cs b/Jellyfin.Server.Implementations/Item/BaseItemRepository.TranslateQuery.cs index e66f18bc6e..76889cb095 100644 --- a/Jellyfin.Server.Implementations/Item/BaseItemRepository.TranslateQuery.cs +++ b/Jellyfin.Server.Implementations/Item/BaseItemRepository.TranslateQuery.cs @@ -18,6 +18,7 @@ using Jellyfin.Extensions; using Jellyfin.Server.Implementations.Extensions; using MediaBrowser.Controller.Entities; using MediaBrowser.Model.Entities; +using MediaBrowser.Model.Globalization; using Microsoft.EntityFrameworkCore; using BaseItemEntity = Jellyfin.Database.Implementations.Entities.BaseItemEntity; @@ -70,6 +71,9 @@ public sealed partial class BaseItemRepository private static string NormalizeLanguage(string language) => string.Equals(language, "und", StringComparison.OrdinalIgnoreCase) ? "und" : language; + private IReadOnlyList<string> GetStreamLanguageCodes(string language) + => _localizationManager.GetMediaStreamLanguageCodes(NormalizeLanguage(language)); + // The primary versions whose alternate version satisfies a dimension bound. Anchored on // PrimaryVersionId so the filtered index carries it rather than a scan of every item. private static IQueryable<Guid> VersionsMatchingDimension(JellyfinDbContext context, Expression<Func<BaseItemEntity, bool>> bound) @@ -825,8 +829,8 @@ public sealed partial class BaseItemRepository if (!string.IsNullOrWhiteSpace(filter.HasNoAudioTrackWithLanguage)) { - var lang = NormalizeLanguage(filter.HasNoAudioTrackWithLanguage); - var undetermined = string.Equals(lang, "und", StringComparison.Ordinal); + var lang = GetStreamLanguageCodes(filter.HasNoAudioTrackWithLanguage); + var undetermined = lang.Contains("und", StringComparer.Ordinal); var criteria = new HasMediaStreamType(MediaStreamTypeEntity.Audio, lang); // A track only an alternate version carries still belongs to the item a caller sees, so the // item's own streams alone do not decide this. Same for every stream filter below. @@ -837,15 +841,15 @@ public sealed partial class BaseItemRepository .Where(e => (!e.IsFolder && !e.MediaStreams!.Any(ms => ms.StreamType == MediaStreamTypeEntity.Audio - && (ms.Language == lang || (undetermined && string.IsNullOrEmpty(ms.Language)))) + && (lang.Contains(ms.Language) || (undetermined && string.IsNullOrEmpty(ms.Language)))) && !versionsWithAudio.Contains(e.Id)) || (e.IsFolder && !foldersWithAudio.Contains(e.Id))); } if (!string.IsNullOrWhiteSpace(filter.HasNoInternalSubtitleTrackWithLanguage)) { - var lang = NormalizeLanguage(filter.HasNoInternalSubtitleTrackWithLanguage); - var undetermined = string.Equals(lang, "und", StringComparison.Ordinal); + var lang = GetStreamLanguageCodes(filter.HasNoInternalSubtitleTrackWithLanguage); + var undetermined = lang.Contains("und", StringComparer.Ordinal); var criteria = new HasMediaStreamType(MediaStreamTypeEntity.Subtitle, lang, IsExternal: false); var versionsWithSubtitles = DescendantQueryHelper.GetPrimaryVersionIdsMatching(context, criteria); var foldersWithSubtitles = DescendantQueryHelper.GetFolderIdsMatching(context, criteria); @@ -854,15 +858,15 @@ public sealed partial class BaseItemRepository .Where(e => (!e.IsFolder && !e.MediaStreams!.Any(ms => ms.StreamType == MediaStreamTypeEntity.Subtitle && !ms.IsExternal - && (ms.Language == lang || (undetermined && string.IsNullOrEmpty(ms.Language)))) + && (lang.Contains(ms.Language) || (undetermined && string.IsNullOrEmpty(ms.Language)))) && !versionsWithSubtitles.Contains(e.Id)) || (e.IsFolder && !foldersWithSubtitles.Contains(e.Id))); } if (!string.IsNullOrWhiteSpace(filter.HasNoExternalSubtitleTrackWithLanguage)) { - var lang = NormalizeLanguage(filter.HasNoExternalSubtitleTrackWithLanguage); - var undetermined = string.Equals(lang, "und", StringComparison.Ordinal); + var lang = GetStreamLanguageCodes(filter.HasNoExternalSubtitleTrackWithLanguage); + var undetermined = lang.Contains("und", StringComparer.Ordinal); var criteria = new HasMediaStreamType(MediaStreamTypeEntity.Subtitle, lang, IsExternal: true); var versionsWithSubtitles = DescendantQueryHelper.GetPrimaryVersionIdsMatching(context, criteria); var foldersWithSubtitles = DescendantQueryHelper.GetFolderIdsMatching(context, criteria); @@ -871,15 +875,15 @@ public sealed partial class BaseItemRepository .Where(e => (!e.IsFolder && !e.MediaStreams!.Any(ms => ms.StreamType == MediaStreamTypeEntity.Subtitle && ms.IsExternal - && (ms.Language == lang || (undetermined && string.IsNullOrEmpty(ms.Language)))) + && (lang.Contains(ms.Language) || (undetermined && string.IsNullOrEmpty(ms.Language)))) && !versionsWithSubtitles.Contains(e.Id)) || (e.IsFolder && !foldersWithSubtitles.Contains(e.Id))); } if (!string.IsNullOrWhiteSpace(filter.HasNoSubtitleTrackWithLanguage)) { - var lang = NormalizeLanguage(filter.HasNoSubtitleTrackWithLanguage); - var undetermined = string.Equals(lang, "und", StringComparison.Ordinal); + var lang = GetStreamLanguageCodes(filter.HasNoSubtitleTrackWithLanguage); + var undetermined = lang.Contains("und", StringComparer.Ordinal); var criteria = new HasMediaStreamType(MediaStreamTypeEntity.Subtitle, lang); var versionsWithSubtitles = DescendantQueryHelper.GetPrimaryVersionIdsMatching(context, criteria); var foldersWithSubtitles = DescendantQueryHelper.GetFolderIdsMatching(context, criteria); @@ -888,7 +892,7 @@ public sealed partial class BaseItemRepository .Where(e => (!e.IsFolder && !e.MediaStreams!.Any(ms => ms.StreamType == MediaStreamTypeEntity.Subtitle - && (ms.Language == lang || (undetermined && string.IsNullOrEmpty(ms.Language)))) + && (lang.Contains(ms.Language) || (undetermined && string.IsNullOrEmpty(ms.Language)))) && !versionsWithSubtitles.Contains(e.Id)) || (e.IsFolder && !foldersWithSubtitles.Contains(e.Id))); } @@ -1094,8 +1098,10 @@ public sealed partial class BaseItemRepository if (filter.AncestorIds.Length > 0) { - var ancestorFilter = filter.AncestorIds.OneOrManyExpressionBuilder<AncestorId, Guid>(f => f.ParentItemId); - baseQuery = baseQuery.Where(e => e.Parents!.AsQueryable().Any(ancestorFilter)); + var descendantIds = context.AncestorIds + .WhereOneOrMany(filter.AncestorIds, a => a.ParentItemId) + .Select(a => a.ItemId); + baseQuery = baseQuery.Where(e => descendantIds.Contains(e.Id)); } if (filter.DescendantOfId.HasValue) @@ -1115,8 +1121,14 @@ public sealed partial class BaseItemRepository if (!string.IsNullOrWhiteSpace(filter.AncestorWithPresentationUniqueKey)) { - baseQuery = baseQuery - .Where(e => context.BaseItems.Where(e => e.Id != EF.Constant(PlaceholderId)).Where(f => f.PresentationUniqueKey == filter.AncestorWithPresentationUniqueKey).Any(f => f.Children!.Any(w => w.ItemId == e.Id))); + var ancestorKey = filter.AncestorWithPresentationUniqueKey; + var keyedAncestorIds = context.BaseItems + .Where(f => f.Id != EF.Constant(PlaceholderId) && f.PresentationUniqueKey == ancestorKey) + .Select(f => f.Id); + var descendantIds = context.AncestorIds + .Where(a => keyedAncestorIds.Contains(a.ParentItemId)) + .Select(a => a.ItemId); + baseQuery = baseQuery.Where(e => descendantIds.Contains(e.Id)); } if (!string.IsNullOrWhiteSpace(filter.SeriesPresentationUniqueKey)) diff --git a/Jellyfin.Server.Implementations/Item/BaseItemRepository.cs b/Jellyfin.Server.Implementations/Item/BaseItemRepository.cs index 1d2aa21853..e51957ffec 100644 --- a/Jellyfin.Server.Implementations/Item/BaseItemRepository.cs +++ b/Jellyfin.Server.Implementations/Item/BaseItemRepository.cs @@ -10,6 +10,7 @@ using MediaBrowser.Controller.Channels; using MediaBrowser.Controller.Configuration; using MediaBrowser.Controller.Entities; using MediaBrowser.Controller.Persistence; +using MediaBrowser.Model.Globalization; using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.Logging; using BaseItemDto = MediaBrowser.Controller.Entities.BaseItem; @@ -38,6 +39,7 @@ public sealed partial class BaseItemRepository private readonly IServerApplicationHost _appHost; private readonly IItemTypeLookup _itemTypeLookup; private readonly IServerConfigurationManager _serverConfigurationManager; + private readonly ILocalizationManager _localizationManager; private readonly ILogger<BaseItemRepository> _logger; private static readonly IReadOnlyList<ItemValueType> _getAllArtistsValueTypes = [ItemValueType.Artist, ItemValueType.AlbumArtist]; @@ -78,18 +80,21 @@ public sealed partial class BaseItemRepository /// <param name="appHost">The Application host.</param> /// <param name="itemTypeLookup">The static type lookup.</param> /// <param name="serverConfigurationManager">The server Configuration manager.</param> + /// <param name="localizationManager">The localization manager.</param> /// <param name="logger">System logger.</param> public BaseItemRepository( IDbContextFactory<JellyfinDbContext> dbProvider, IServerApplicationHost appHost, IItemTypeLookup itemTypeLookup, IServerConfigurationManager serverConfigurationManager, + ILocalizationManager localizationManager, ILogger<BaseItemRepository> logger) { _dbProvider = dbProvider; _appHost = appHost; _itemTypeLookup = itemTypeLookup; _serverConfigurationManager = serverConfigurationManager; + _localizationManager = localizationManager; _logger = logger; } diff --git a/Jellyfin.Server.Implementations/Item/ItemCountBuilder.cs b/Jellyfin.Server.Implementations/Item/ItemCountBuilder.cs index 0f8a1b9dfe..0fb6721d74 100644 --- a/Jellyfin.Server.Implementations/Item/ItemCountBuilder.cs +++ b/Jellyfin.Server.Implementations/Item/ItemCountBuilder.cs @@ -73,6 +73,10 @@ internal static class ItemCountBuilder { result.BookCount += count; } + else if (string.Equals(type, lookup[BaseItemKind.AudioBook], StringComparison.Ordinal)) + { + result.AudioBookCount += count; + } } result.ItemCount = result.TotalItemCount(); diff --git a/Jellyfin.Server.Implementations/Item/ItemCountService.cs b/Jellyfin.Server.Implementations/Item/ItemCountService.cs index 942161a176..b0af08479d 100644 --- a/Jellyfin.Server.Implementations/Item/ItemCountService.cs +++ b/Jellyfin.Server.Implementations/Item/ItemCountService.cs @@ -7,7 +7,6 @@ using System.Linq; using Jellyfin.Data.Enums; using Jellyfin.Database.Implementations; using Jellyfin.Database.Implementations.Entities; -using Jellyfin.Database.Implementations.Enums; using Jellyfin.Extensions; using MediaBrowser.Controller.Entities; using MediaBrowser.Controller.Persistence; @@ -72,6 +71,7 @@ public class ItemCountService : IItemCountService { ItemCount = counts.Sum(c => c.Count) }; + foreach (var count in counts) { if (string.Equals(count.Key, lookup[BaseItemKind.MusicAlbum], StringComparison.Ordinal)) @@ -118,6 +118,10 @@ public class ItemCountService : IItemCountService { result.BookCount = count.Count; } + else if (string.Equals(count.Key, lookup[BaseItemKind.AudioBook], StringComparison.Ordinal)) + { + result.AudioBookCount = count.Count; + } } return result; diff --git a/Jellyfin.Server.Implementations/Item/ItemPersistenceService.cs b/Jellyfin.Server.Implementations/Item/ItemPersistenceService.cs index 3bf33b969e..4e7e5a5331 100644 --- a/Jellyfin.Server.Implementations/Item/ItemPersistenceService.cs +++ b/Jellyfin.Server.Implementations/Item/ItemPersistenceService.cs @@ -374,7 +374,8 @@ public class ItemPersistenceService : IItemPersistenceService var allListedItemValuesSet = allListedItemValues.ToHashSet(); var existingValues = context.ItemValues - .Where(e => types.Contains(e.Type) && values.Contains(e.Value)) + .WhereOneOrMany(types, e => e.Type) + .WhereOneOrMany(values, e => e.Value) .AsEnumerable() .Where(e => allListedItemValuesSet.Contains((e.Type, e.Value))) .ToArray(); @@ -428,7 +429,7 @@ public class ItemPersistenceService : IItemPersistenceService var allExistingAncestorIds = itemsWithAncestors.Count > 0 ? context.AncestorIds - .Where(e => itemsWithAncestors.Contains(e.ItemId)) + .WhereOneOrMany(itemsWithAncestors, e => e.ItemId) .ToList() .GroupBy(e => e.ItemId) .ToDictionary(g => g.Key, g => g.ToList()) @@ -442,7 +443,7 @@ public class ItemPersistenceService : IItemPersistenceService var validAncestorIdsSet = allRequestedAncestorIds.Count > 0 ? context.BaseItems - .Where(e => allRequestedAncestorIds.Contains(e.Id)) + .WhereOneOrMany(allRequestedAncestorIds, e => e.Id) .Select(f => f.Id) .ToHashSet() : new HashSet<Guid>(); @@ -502,7 +503,7 @@ public class ItemPersistenceService : IItemPersistenceService { var allParentIds = folderIds.Concat(videoIds).Distinct().ToList(); var allLinkedChildren = context.LinkedChildren - .Where(e => allParentIds.Contains(e.ParentId)) + .WhereOneOrMany(allParentIds, e => e.ParentId) .ToList(); allLinkedChildrenByParent = allLinkedChildren @@ -572,7 +573,7 @@ public class ItemPersistenceService : IItemPersistenceService var pathToIdMap = pathsToResolve.Count > 0 ? context.BaseItems - .Where(e => e.Path != null && pathsToResolve.Contains(e.Path)) + .WhereOneOrMany(pathsToResolve, e => e.Path!) .Select(e => new { e.Path, e.Id }) .GroupBy(e => e.Path!) .ToDictionary(g => g.Key, g => g.First().Id) diff --git a/Jellyfin.Server.Implementations/Item/PeopleRepository.cs b/Jellyfin.Server.Implementations/Item/PeopleRepository.cs index 802138ed5e..3eb66a328f 100644 --- a/Jellyfin.Server.Implementations/Item/PeopleRepository.cs +++ b/Jellyfin.Server.Implementations/Item/PeopleRepository.cs @@ -441,7 +441,15 @@ public class PeopleRepository(IDbContextFactory<JellyfinDbContext> dbProvider, I if (filter.ParentId != null) { - query = query.Where(e => e.BaseItems!.Any(w => context.AncestorIds.Any(i => i.ParentItemId == filter.ParentId && i.ItemId == w.ItemId))); + // Seek the credits through IX_AncestorIds_ParentItemId_ItemId; a correlated EXISTS walks every person. + var parentId = filter.ParentId.Value; + var descendantIds = context.AncestorIds + .Where(a => a.ParentItemId == parentId) + .Select(a => a.ItemId); + var creditedPeopleIds = context.PeopleBaseItemMap + .Where(m => descendantIds.Contains(m.ItemId)) + .Select(m => m.PeopleId); + query = query.Where(e => creditedPeopleIds.Contains(e.Id)); } if (!filter.AppearsInItemId.IsEmpty()) |
