| | | 1 | | using System; |
| | | 2 | | using System.Collections.Generic; |
| | | 3 | | using System.Linq; |
| | | 4 | | using System.Linq.Expressions; |
| | | 5 | | using Jellyfin.Database.Implementations.Entities; |
| | | 6 | | using Jellyfin.Database.Implementations.MatchCriteria; |
| | | 7 | | |
| | | 8 | | namespace Jellyfin.Database.Implementations; |
| | | 9 | | |
| | | 10 | | /// <summary> |
| | | 11 | | /// Provides methods for querying item hierarchies. |
| | | 12 | | /// Uses AncestorIds and LinkedChildren tables for parent-child traversal. |
| | | 13 | | /// </summary> |
| | | 14 | | public static class DescendantQueryHelper |
| | | 15 | | { |
| | | 16 | | /// <summary> |
| | | 17 | | /// Gets the predicate identifying items that count toward played/total aggregation: |
| | | 18 | | /// real leaf media, i.e. neither folders nor virtual items (missing or unaired episodes). |
| | | 19 | | /// Shared by the per-item and batched count paths so they cannot diverge. |
| | | 20 | | /// </summary> |
| | | 21 | | public static Expression<Func<BaseItemEntity, bool>> IsCountableLeaf { get; } = |
| | 2 | 22 | | b => !b.IsFolder && !b.IsVirtualItem; |
| | | 23 | | |
| | | 24 | | /// <summary> |
| | | 25 | | /// Gets the predicate identifying the items that stand on their own in a library. An alternate |
| | | 26 | | /// version is a second file for the item that links it rather than an item beside it, and an owned |
| | | 27 | | /// item belongs to its owner unless it is an extra (a trailer and the like, which carries both an |
| | | 28 | | /// owner and an extra type). Nothing here turns on who is asking, so a count that applies it |
| | | 29 | | /// answers the same with a user and without one. |
| | | 30 | | /// </summary> |
| | | 31 | | public static Expression<Func<BaseItemEntity, bool>> IsDistinctLibraryItem { get; } = |
| | 2 | 32 | | b => !b.PrimaryVersionId.HasValue && (!b.OwnerId.HasValue || b.ExtraType != null); |
| | | 33 | | |
| | | 34 | | /// <summary> |
| | | 35 | | /// Builds the predicate identifying the items a user has played, counting a multi-version item as |
| | | 36 | | /// played when any of its alternate versions is. Mirrors the aggregation |
| | | 37 | | /// <c>VersionResumeData.ApplyTo</c> performs on the played flag a single item reports, so that a |
| | | 38 | | /// folder's unplayed count cannot disagree with the watched state its members render with. |
| | | 39 | | /// </summary> |
| | | 40 | | /// <param name="userId">The id of the user whose played state to test.</param> |
| | | 41 | | /// <returns>The predicate matching the items that user has played.</returns> |
| | | 42 | | public static Expression<Func<BaseItemEntity, bool>> IsPlayedBy(Guid userId) => |
| | 16 | 43 | | b => b.UserData!.Any(u => u.UserId.Equals(userId) && u.Played) |
| | 16 | 44 | | || b.LinkedChildEntities!.Any(lc => |
| | 16 | 45 | | (lc.ChildType == LinkedChildType.LocalAlternateVersion || lc.ChildType == LinkedChildType.LinkedAlternat |
| | 16 | 46 | | && lc.Child!.UserData!.Any(u => u.UserId.Equals(userId) && u.Played)); |
| | | 47 | | |
| | | 48 | | /// <summary> |
| | | 49 | | /// Builds the projection pairing an item's id with <see cref="IsPlayedBy"/> evaluated on that same |
| | | 50 | | /// row. A caller that needs the flag alongside the id composes it rather than testing membership of |
| | | 51 | | /// the played set: as a sub-select the set is unbounded by whatever the caller joins it to, so the |
| | | 52 | | /// database builds it from the whole table once per place it appears. |
| | | 53 | | /// </summary> |
| | | 54 | | /// <param name="userId">The id of the user whose played state to test.</param> |
| | | 55 | | /// <returns>The projection of each item onto its id and that user's played state.</returns> |
| | | 56 | | public static Expression<Func<BaseItemEntity, LeafPlayedState>> PlayedStateBy(Guid userId) |
| | | 57 | | { |
| | 3 | 58 | | var played = IsPlayedBy(userId); |
| | 3 | 59 | | var item = played.Parameters[0]; |
| | | 60 | | |
| | | 61 | | // Named members, as the compiler emits for an anonymous type: without them the query provider |
| | | 62 | | // cannot read a later `x.Id` back to the column it was built from and gives up translating. |
| | 3 | 63 | | return Expression.Lambda<Func<BaseItemEntity, LeafPlayedState>>( |
| | 3 | 64 | | Expression.New( |
| | 3 | 65 | | typeof(LeafPlayedState).GetConstructor([typeof(Guid), typeof(bool)])!, |
| | 3 | 66 | | [Expression.Property(item, nameof(BaseItemEntity.Id)), played.Body], |
| | 3 | 67 | | [typeof(LeafPlayedState).GetProperty(nameof(LeafPlayedState.Id))!, typeof(LeafPlayedState).GetProperty(n |
| | 3 | 68 | | item); |
| | | 69 | | } |
| | | 70 | | |
| | | 71 | | /// <summary> |
| | | 72 | | /// Builds the negation of <see cref="IsPlayedBy"/>, so a caller filtering for unplayed items reads |
| | | 73 | | /// the same definition of played as one filtering for played items. |
| | | 74 | | /// </summary> |
| | | 75 | | /// <param name="userId">The id of the user whose played state to test.</param> |
| | | 76 | | /// <returns>The predicate matching the items that user has not played.</returns> |
| | | 77 | | public static Expression<Func<BaseItemEntity, bool>> IsUnplayedBy(Guid userId) |
| | | 78 | | { |
| | 1 | 79 | | var played = IsPlayedBy(userId); |
| | 1 | 80 | | return Expression.Lambda<Func<BaseItemEntity, bool>>(Expression.Not(played.Body), played.Parameters); |
| | | 81 | | } |
| | | 82 | | |
| | | 83 | | /// <summary> |
| | | 84 | | /// Gets a queryable of all descendant IDs for a parent item. |
| | | 85 | | /// Traverses AncestorIds and LinkedChildren to find all descendants. |
| | | 86 | | /// </summary> |
| | | 87 | | /// <param name="context">Database context.</param> |
| | | 88 | | /// <param name="parentId">Parent item ID.</param> |
| | | 89 | | /// <returns>Queryable of descendant item IDs.</returns> |
| | | 90 | | public static IQueryable<Guid> GetAllDescendantIds(JellyfinDbContext context, Guid parentId) |
| | | 91 | | { |
| | 15 | 92 | | ArgumentNullException.ThrowIfNull(context); |
| | | 93 | | |
| | 15 | 94 | | return AllDescendants(context, [parentId]) |
| | 15 | 95 | | .Where(e => !e.Equals(parentId)) |
| | 15 | 96 | | .Distinct(); |
| | | 97 | | } |
| | | 98 | | |
| | | 99 | | /// <summary> |
| | | 100 | | /// Gets all descendant IDs for multiple parent items in a single traversal. |
| | | 101 | | /// Traverses AncestorIds and LinkedChildren, like <see cref="GetAllDescendantIds"/>, but resolves |
| | | 102 | | /// the roots once for all seeds instead of once per seed. |
| | | 103 | | /// </summary> |
| | | 104 | | /// <param name="context">Database context.</param> |
| | | 105 | | /// <param name="parentIds">Parent item IDs.</param> |
| | | 106 | | /// <returns>Set of all descendant item IDs (excluding the parent IDs themselves).</returns> |
| | | 107 | | public static HashSet<Guid> GetAllDescendantIdsBatch(JellyfinDbContext context, IReadOnlyList<Guid> parentIds) |
| | | 108 | | { |
| | 14 | 109 | | ArgumentNullException.ThrowIfNull(context); |
| | 14 | 110 | | ArgumentNullException.ThrowIfNull(parentIds); |
| | | 111 | | |
| | 14 | 112 | | if (parentIds.Count == 0) |
| | | 113 | | { |
| | 0 | 114 | | return []; |
| | | 115 | | } |
| | | 116 | | |
| | 14 | 117 | | var descendants = AllDescendants(context, parentIds) |
| | 14 | 118 | | .Distinct() |
| | 14 | 119 | | .ToHashSet(); |
| | | 120 | | |
| | 14 | 121 | | descendants.ExceptWith(parentIds); |
| | | 122 | | |
| | 14 | 123 | | return descendants; |
| | | 124 | | } |
| | | 125 | | |
| | | 126 | | /// <summary> |
| | | 127 | | /// Gets a queryable of all owned descendant IDs for a parent item. |
| | | 128 | | /// Traverses only AncestorIds (hierarchical ownership), NOT LinkedChildren (associations). |
| | | 129 | | /// Use this for deletion to avoid destroying items that are merely linked (e.g. movies in a BoxSet). |
| | | 130 | | /// </summary> |
| | | 131 | | /// <param name="context">Database context.</param> |
| | | 132 | | /// <param name="parentId">Parent item ID.</param> |
| | | 133 | | /// <returns>Queryable of owned descendant item IDs.</returns> |
| | | 134 | | public static IQueryable<Guid> GetOwnedDescendantIds(JellyfinDbContext context, Guid parentId) |
| | | 135 | | { |
| | 2 | 136 | | ArgumentNullException.ThrowIfNull(context); |
| | | 137 | | |
| | 2 | 138 | | return ClosureDescendants(context, [parentId]) |
| | 2 | 139 | | .Where(e => !e.Equals(parentId)) |
| | 2 | 140 | | .Distinct(); |
| | | 141 | | } |
| | | 142 | | |
| | | 143 | | /// <summary> |
| | | 144 | | /// Gets all owned descendant IDs for multiple parent items in a single traversal. |
| | | 145 | | /// More efficient than calling <see cref="GetOwnedDescendantIds"/> per parent because |
| | | 146 | | /// it performs one traversal for all seeds instead of N separate traversals. |
| | | 147 | | /// </summary> |
| | | 148 | | /// <param name="context">Database context.</param> |
| | | 149 | | /// <param name="parentIds">Parent item IDs.</param> |
| | | 150 | | /// <returns>Set of all owned descendant item IDs (excluding the parent IDs themselves).</returns> |
| | | 151 | | public static HashSet<Guid> GetOwnedDescendantIdsBatch(JellyfinDbContext context, IReadOnlyList<Guid> parentIds) |
| | | 152 | | { |
| | 14 | 153 | | ArgumentNullException.ThrowIfNull(context); |
| | 14 | 154 | | ArgumentNullException.ThrowIfNull(parentIds); |
| | | 155 | | |
| | 14 | 156 | | if (parentIds.Count == 0) |
| | | 157 | | { |
| | 0 | 158 | | return []; |
| | | 159 | | } |
| | | 160 | | |
| | 14 | 161 | | var descendants = ClosureDescendants(context, parentIds) |
| | 14 | 162 | | .Distinct() |
| | 14 | 163 | | .ToHashSet(); |
| | | 164 | | |
| | 14 | 165 | | descendants.ExceptWith(parentIds); |
| | | 166 | | |
| | 14 | 167 | | return descendants; |
| | | 168 | | } |
| | | 169 | | |
| | | 170 | | /// <summary> |
| | | 171 | | /// Gets a queryable of all folder IDs that have any descendant matching the specified criteria. |
| | | 172 | | /// Can be used in LINQ .Contains() expressions. |
| | | 173 | | /// </summary> |
| | | 174 | | /// <param name="context">Database context.</param> |
| | | 175 | | /// <param name="criteria">The matching criteria to apply.</param> |
| | | 176 | | /// <returns>Queryable of folder IDs.</returns> |
| | | 177 | | public static IQueryable<Guid> GetFolderIdsMatching(JellyfinDbContext context, FolderMatchCriteria criteria) |
| | | 178 | | { |
| | 24 | 179 | | ArgumentNullException.ThrowIfNull(context); |
| | 24 | 180 | | ArgumentNullException.ThrowIfNull(criteria); |
| | | 181 | | |
| | | 182 | | // Both sides of a version group can hold a folder a caller would see as matching: the |
| | | 183 | | // alternate carries its own AncestorIds rows and may sit in a different library than the |
| | | 184 | | // primary it is reported against, and the primary is the item that becomes visible. |
| | 24 | 185 | | var reportedItemIds = MatchingMediaOwnerIds(context, criteria) |
| | 24 | 186 | | .Concat(GetPrimaryVersionIdsMatching(context, criteria)) |
| | 24 | 187 | | .Distinct(); |
| | | 188 | | |
| | | 189 | | // One hop up the closure covers every ancestor level. |
| | 24 | 190 | | var hierarchyAncestors = context.AncestorIds |
| | 24 | 191 | | .Where(e => reportedItemIds.Contains(e.ItemId)) |
| | 24 | 192 | | .Select(e => e.ParentItemId); |
| | | 193 | | |
| | 24 | 194 | | var linkParents = ResolveLinkParents(context, reportedItemIds, hierarchyAncestors); |
| | | 195 | | |
| | | 196 | | // Read back as a sub-select so the result stays composable. Off the primary key, which is one |
| | | 197 | | // row per id: LinkedChildren would yield one row per link and lean on the outer Distinct. |
| | 24 | 198 | | var linkedParents = context.BaseItems |
| | 24 | 199 | | .WhereOneOrMany(linkParents, e => e.Id) |
| | 24 | 200 | | .Select(e => e.Id); |
| | | 201 | | |
| | 24 | 202 | | var linkedParentAncestors = context.AncestorIds |
| | 24 | 203 | | .WhereOneOrMany(linkParents, e => e.ItemId) |
| | 24 | 204 | | .Select(e => e.ParentItemId); |
| | | 205 | | |
| | | 206 | | // The chain an item carries stops at its collection folders, so this hop crosses that seam to |
| | | 207 | | // the UserRootFolder above them. One statement for both sides beats a sub-select per side. |
| | 24 | 208 | | var seamAncestors = context.AncestorIds |
| | 24 | 209 | | .Where(e => hierarchyAncestors.Contains(e.ItemId) || linkedParentAncestors.Contains(e.ItemId)) |
| | 24 | 210 | | .Select(e => e.ParentItemId); |
| | | 211 | | |
| | 24 | 212 | | return hierarchyAncestors |
| | 24 | 213 | | .Concat(linkedParents) |
| | 24 | 214 | | .Concat(linkedParentAncestors) |
| | 24 | 215 | | .Concat(seamAncestors) |
| | 24 | 216 | | .Distinct(); |
| | | 217 | | } |
| | | 218 | | |
| | | 219 | | /// <summary> |
| | | 220 | | /// Gets a queryable of the IDs of the primary versions whose alternate version's media matches the |
| | | 221 | | /// criteria. |
| | | 222 | | /// </summary> |
| | | 223 | | /// <param name="context">Database context.</param> |
| | | 224 | | /// <param name="criteria">The matching criteria to apply.</param> |
| | | 225 | | /// <returns>Queryable of primary version item IDs.</returns> |
| | | 226 | | /// <remarks> |
| | | 227 | | /// For callers that already test an item's own media with their own indexed predicate: this covers |
| | | 228 | | /// exactly what such a predicate misses, and the filtered PrimaryVersionId index keeps it to the few |
| | | 229 | | /// items that have versions at all. |
| | | 230 | | /// </remarks> |
| | | 231 | | public static IQueryable<Guid> GetPrimaryVersionIdsMatching(JellyfinDbContext context, FolderMatchCriteria criteria) |
| | | 232 | | { |
| | 42 | 233 | | ArgumentNullException.ThrowIfNull(context); |
| | 42 | 234 | | ArgumentNullException.ThrowIfNull(criteria); |
| | | 235 | | |
| | | 236 | | // Anchored on the alternates rather than on the matches: "has a primary version" is served by |
| | | 237 | | // the partial PrimaryVersionId index, which holds only the few items that are second files, so |
| | | 238 | | // this costs a seek each into the stream index instead of a second pass over every stream row. |
| | 42 | 239 | | var alternates = context.BaseItems.Where(v => v.PrimaryVersionId.HasValue); |
| | | 240 | | |
| | 42 | 241 | | if (criteria is HasChapterImages) |
| | | 242 | | { |
| | 4 | 243 | | return alternates |
| | 4 | 244 | | .Where(v => context.Chapters.Any(c => c.ItemId.Equals(v.Id) && c.ImagePath != null)) |
| | 4 | 245 | | .Select(v => v.PrimaryVersionId!.Value); |
| | | 246 | | } |
| | | 247 | | |
| | 38 | 248 | | var matchingStreams = MatchingMediaStreams(context, criteria); |
| | | 249 | | |
| | 38 | 250 | | return alternates |
| | 38 | 251 | | .Where(v => matchingStreams.Any(ms => ms.ItemId.Equals(v.Id))) |
| | 38 | 252 | | .Select(v => v.PrimaryVersionId!.Value); |
| | | 253 | | } |
| | | 254 | | |
| | | 255 | | // The ids of the items whose own media matches. Kept to the stream and chapter tables so their |
| | | 256 | | // covering indexes answer this outright: projecting the BaseItems navigation instead would add a |
| | | 257 | | // primary-key lookup per stream row rather than one per matching item, and the leading key of both |
| | | 258 | | // indexes leaves the ids already grouped, so the Distinct costs no sort. |
| | | 259 | | private static IQueryable<Guid> MatchingMediaOwnerIds(JellyfinDbContext context, FolderMatchCriteria criteria) |
| | 24 | 260 | | => criteria is HasChapterImages |
| | 24 | 261 | | ? context.Chapters |
| | 24 | 262 | | .Where(c => c.ImagePath != null) |
| | 24 | 263 | | .Select(c => c.ItemId) |
| | 24 | 264 | | .Distinct() |
| | 24 | 265 | | : MatchingMediaStreams(context, criteria) |
| | 24 | 266 | | .Select(ms => ms.ItemId) |
| | 24 | 267 | | .Distinct(); |
| | | 268 | | |
| | | 269 | | // The stream rows a criteria matches. One definition, so the owner projection and the alternate |
| | | 270 | | // projection cannot drift apart despite reading it from opposite ends. |
| | | 271 | | private static IQueryable<MediaStreamInfo> MatchingMediaStreams(JellyfinDbContext context, FolderMatchCriteria crite |
| | 60 | 272 | | => criteria switch |
| | 60 | 273 | | { |
| | 26 | 274 | | HasSubtitles => context.MediaStreamInfos |
| | 26 | 275 | | .Where(ms => ms.StreamType == MediaStreamTypeEntity.Subtitle), |
| | 34 | 276 | | HasMediaStreamType m => GetMatchingMediaStreams(context, m), |
| | 0 | 277 | | _ => throw new ArgumentOutOfRangeException(nameof(criteria), $"Unknown criteria type: {criteria.GetType().Na |
| | 60 | 278 | | }; |
| | | 279 | | |
| | | 280 | | private static IQueryable<MediaStreamInfo> GetMatchingMediaStreams(JellyfinDbContext context, HasMediaStreamType cri |
| | | 281 | | { |
| | 34 | 282 | | var query = context.MediaStreamInfos |
| | 34 | 283 | | .Where(ms => ms.StreamType == criteria.StreamType |
| | 34 | 284 | | && (criteria.Language.Contains(ms.Language) |
| | 34 | 285 | | || (criteria.Language.Contains("und") && string.IsNullOrEmpty(ms.Language)))); // und = undetermi |
| | | 286 | | |
| | 34 | 287 | | if (criteria.IsExternal.HasValue) |
| | | 288 | | { |
| | 0 | 289 | | var isExternal = criteria.IsExternal.Value; |
| | 0 | 290 | | query = query.Where(ms => ms.IsExternal == isExternal); |
| | | 291 | | } |
| | | 292 | | |
| | 34 | 293 | | return query; |
| | | 294 | | } |
| | | 295 | | |
| | | 296 | | private static IQueryable<Guid> AllDescendants(JellyfinDbContext context, IReadOnlyList<Guid> parentIds) |
| | | 297 | | { |
| | 29 | 298 | | var (closureRoots, linkRoots) = ResolveLinkedRoots(context, parentIds); |
| | | 299 | | |
| | 29 | 300 | | var linkedDescendants = context.LinkedChildren |
| | 29 | 301 | | .WhereOneOrMany(linkRoots, e => e.ParentId) |
| | 29 | 302 | | .Select(e => e.ChildId); |
| | | 303 | | |
| | 29 | 304 | | return ClosureDescendants(context, closureRoots) |
| | 29 | 305 | | .Concat(linkedDescendants); |
| | | 306 | | } |
| | | 307 | | |
| | | 308 | | private static IQueryable<Guid> ClosureDescendants(JellyfinDbContext context, IReadOnlyList<Guid> roots) |
| | | 309 | | { |
| | 80 | 310 | | var direct = context.AncestorIds |
| | 80 | 311 | | .WhereOneOrMany(roots, e => e.ParentItemId) |
| | 80 | 312 | | .Select(e => e.ItemId); |
| | | 313 | | |
| | | 314 | | // An item carries its own chain plus its collection folders, never the UserRootFolder. |
| | 80 | 315 | | var indirect = context.AncestorIds |
| | 80 | 316 | | .Where(e => direct.Contains(e.ParentItemId)) |
| | 80 | 317 | | .Select(e => e.ItemId); |
| | | 318 | | |
| | 80 | 319 | | return direct.Concat(indirect); |
| | | 320 | | } |
| | | 321 | | |
| | | 322 | | // Resolves the folders whose linked children lead, at any depth, to a matching item. |
| | | 323 | | private static List<Guid> ResolveLinkParents(JellyfinDbContext context, IQueryable<Guid> matchingItemIds, IQueryable |
| | | 324 | | { |
| | | 325 | | // An alternate version is a second file for the item that links it, not a child of it, so that |
| | | 326 | | // edge is not walked. It is also the one link a non-folder owns, and there is one per remuxed |
| | | 327 | | // movie: walking it would swell this list from the BoxSet and Playlist count to the item count, |
| | | 328 | | // and the list is bound into every statement the returned queryable is embedded in. |
| | 24 | 329 | | var containerLinks = context.LinkedChildren |
| | 24 | 330 | | .Where(e => e.ChildType != LinkedChildType.LocalAlternateVersion |
| | 24 | 331 | | && e.ChildType != LinkedChildType.LinkedAlternateVersion); |
| | | 332 | | |
| | | 333 | | // A link sits above the closure and above another link alike, so the hop repeats until nothing |
| | | 334 | | // new turns up. |
| | 24 | 335 | | var resolved = containerLinks |
| | 24 | 336 | | .Where(e => matchingItemIds.Contains(e.ChildId) || ancestorsOfMatches.Contains(e.ChildId)) |
| | 24 | 337 | | .Select(e => e.ParentId) |
| | 24 | 338 | | .Distinct() |
| | 24 | 339 | | .ToHashSet(); |
| | | 340 | | |
| | 24 | 341 | | var frontier = resolved.ToList(); |
| | | 342 | | |
| | 38 | 343 | | while (frontier.Count != 0) |
| | | 344 | | { |
| | 14 | 345 | | var containingFolders = context.AncestorIds |
| | 14 | 346 | | .WhereOneOrMany(frontier, e => e.ItemId) |
| | 14 | 347 | | .Select(e => e.ParentItemId); |
| | | 348 | | |
| | 14 | 349 | | var directLinkParents = containerLinks |
| | 14 | 350 | | .WhereOneOrMany(frontier, e => e.ChildId) |
| | 14 | 351 | | .Select(e => e.ParentId); |
| | | 352 | | |
| | 14 | 353 | | var indirectLinkParents = containerLinks |
| | 14 | 354 | | .Where(e => containingFolders.Contains(e.ChildId)) |
| | 14 | 355 | | .Select(e => e.ParentId); |
| | | 356 | | |
| | 14 | 357 | | var next = directLinkParents |
| | 14 | 358 | | .Concat(indirectLinkParents) |
| | 14 | 359 | | .Distinct() |
| | 14 | 360 | | .ToArray(); |
| | | 361 | | |
| | 14 | 362 | | frontier = []; |
| | 32 | 363 | | foreach (var id in next) |
| | | 364 | | { |
| | | 365 | | // Cyclic links terminate on the resolved set. |
| | 2 | 366 | | if (resolved.Add(id)) |
| | | 367 | | { |
| | 1 | 368 | | frontier.Add(id); |
| | | 369 | | } |
| | | 370 | | } |
| | | 371 | | } |
| | | 372 | | |
| | 24 | 373 | | return [.. resolved]; |
| | | 374 | | } |
| | | 375 | | |
| | | 376 | | // Resolves the roots the descendant sub-selects are anchored on: those contributing their closure, |
| | | 377 | | // and those contributing their linked children. |
| | | 378 | | private static (List<Guid> ClosureRoots, List<Guid> LinkRoots) ResolveLinkedRoots(JellyfinDbContext context, IReadOn |
| | | 379 | | { |
| | 29 | 380 | | var visited = new HashSet<Guid>(parentIds); |
| | 29 | 381 | | var closureRoots = visited.ToList(); |
| | 29 | 382 | | var linkRoots = visited.ToList(); |
| | 29 | 383 | | var frontier = visited.ToList(); |
| | | 384 | | |
| | 64 | 385 | | while (frontier.Count != 0) |
| | | 386 | | { |
| | 35 | 387 | | var closureIds = ClosureDescendants(context, frontier); |
| | | 388 | | |
| | 35 | 389 | | var linkedIds = context.LinkedChildren |
| | 35 | 390 | | .WhereOneOrMany(frontier, e => e.ParentId) |
| | 35 | 391 | | .Select(e => e.ChildId); |
| | | 392 | | |
| | 35 | 393 | | var linkedFolders = context.BaseItems |
| | 35 | 394 | | .Where(e => e.IsFolder && linkedIds.Contains(e.Id)) |
| | 35 | 395 | | .Select(e => e.Id) |
| | 35 | 396 | | .ToHashSet(); |
| | | 397 | | |
| | | 398 | | // Folders whose own links have to be followed. Driven off LinkedChildren because owning a |
| | | 399 | | // link is the rare property, so the folder check only reaches rows that can qualify. That |
| | | 400 | | // check stays: a non-folder owns links too (a movie and its alternate versions). |
| | 35 | 401 | | var linkOwners = context.LinkedChildren |
| | 35 | 402 | | .Where(e => (closureIds.Contains(e.ParentId) || linkedIds.Contains(e.ParentId)) |
| | 35 | 403 | | && e.Parent!.IsFolder) |
| | 35 | 404 | | .Select(e => e.ParentId) |
| | 35 | 405 | | .Distinct() |
| | 35 | 406 | | .ToArray(); |
| | | 407 | | |
| | 35 | 408 | | frontier = []; |
| | 88 | 409 | | foreach (var id in linkOwners.Concat(linkedFolders)) |
| | | 410 | | { |
| | 9 | 411 | | if (!visited.Add(id)) |
| | | 412 | | { |
| | | 413 | | continue; |
| | | 414 | | } |
| | | 415 | | |
| | 6 | 416 | | frontier.Add(id); |
| | 6 | 417 | | linkRoots.Add(id); |
| | | 418 | | |
| | | 419 | | // Only a folder reached through a link adds a closure the roots so far do not cover. |
| | 6 | 420 | | if (linkedFolders.Contains(id)) |
| | | 421 | | { |
| | 5 | 422 | | closureRoots.Add(id); |
| | | 423 | | } |
| | | 424 | | } |
| | | 425 | | } |
| | | 426 | | |
| | 29 | 427 | | return (closureRoots, linkRoots); |
| | | 428 | | } |
| | | 429 | | } |