| | | 1 | | using System; |
| | | 2 | | using System.Collections.Generic; |
| | | 3 | | using System.Linq; |
| | | 4 | | using Jellyfin.Database.Implementations.Entities; |
| | | 5 | | using Jellyfin.Database.Implementations.MatchCriteria; |
| | | 6 | | |
| | | 7 | | namespace Jellyfin.Database.Implementations; |
| | | 8 | | |
| | | 9 | | /// <summary> |
| | | 10 | | /// Provides methods for querying item hierarchies using iterative traversal. |
| | | 11 | | /// Uses AncestorIds and LinkedChildren tables for parent-child traversal. |
| | | 12 | | /// </summary> |
| | | 13 | | public static class DescendantQueryHelper |
| | | 14 | | { |
| | | 15 | | /// <summary> |
| | | 16 | | /// Gets a queryable of all descendant IDs for a parent item. |
| | | 17 | | /// Traverses AncestorIds and LinkedChildren to find all descendants. |
| | | 18 | | /// </summary> |
| | | 19 | | /// <param name="context">Database context.</param> |
| | | 20 | | /// <param name="parentId">Parent item ID.</param> |
| | | 21 | | /// <returns>Queryable of descendant item IDs.</returns> |
| | | 22 | | public static IQueryable<Guid> GetAllDescendantIds(JellyfinDbContext context, Guid parentId) |
| | | 23 | | { |
| | 0 | 24 | | ArgumentNullException.ThrowIfNull(context); |
| | | 25 | | |
| | 0 | 26 | | var descendants = TraverseHierarchyDown(context, [parentId]); |
| | | 27 | | |
| | 0 | 28 | | descendants.Remove(parentId); |
| | | 29 | | |
| | 0 | 30 | | return descendants.AsQueryable(); |
| | | 31 | | } |
| | | 32 | | |
| | | 33 | | /// <summary> |
| | | 34 | | /// Gets a queryable of all owned descendant IDs for a parent item. |
| | | 35 | | /// Traverses only AncestorIds (hierarchical ownership), NOT LinkedChildren (associations). |
| | | 36 | | /// Use this for deletion to avoid destroying items that are merely linked (e.g. movies in a BoxSet). |
| | | 37 | | /// </summary> |
| | | 38 | | /// <param name="context">Database context.</param> |
| | | 39 | | /// <param name="parentId">Parent item ID.</param> |
| | | 40 | | /// <returns>Queryable of owned descendant item IDs.</returns> |
| | | 41 | | public static IQueryable<Guid> GetOwnedDescendantIds(JellyfinDbContext context, Guid parentId) |
| | | 42 | | { |
| | 0 | 43 | | ArgumentNullException.ThrowIfNull(context); |
| | | 44 | | |
| | 0 | 45 | | var descendants = TraverseHierarchyDownOwned(context, [parentId]); |
| | | 46 | | |
| | 0 | 47 | | descendants.Remove(parentId); |
| | | 48 | | |
| | 0 | 49 | | return descendants.AsQueryable(); |
| | | 50 | | } |
| | | 51 | | |
| | | 52 | | /// <summary> |
| | | 53 | | /// Gets all owned descendant IDs for multiple parent items in a single traversal. |
| | | 54 | | /// More efficient than calling <see cref="GetOwnedDescendantIds"/> per parent because |
| | | 55 | | /// it performs one traversal for all seeds instead of N separate traversals. |
| | | 56 | | /// </summary> |
| | | 57 | | /// <param name="context">Database context.</param> |
| | | 58 | | /// <param name="parentIds">Parent item IDs.</param> |
| | | 59 | | /// <returns>Set of all owned descendant item IDs (excluding the parent IDs themselves).</returns> |
| | | 60 | | public static HashSet<Guid> GetOwnedDescendantIdsBatch(JellyfinDbContext context, IReadOnlyList<Guid> parentIds) |
| | | 61 | | { |
| | 1 | 62 | | ArgumentNullException.ThrowIfNull(context); |
| | 1 | 63 | | ArgumentNullException.ThrowIfNull(parentIds); |
| | | 64 | | |
| | 1 | 65 | | if (parentIds.Count == 0) |
| | | 66 | | { |
| | 0 | 67 | | return []; |
| | | 68 | | } |
| | | 69 | | |
| | 1 | 70 | | var seedSet = new HashSet<Guid>(parentIds); |
| | 1 | 71 | | var descendants = TraverseHierarchyDownOwned(context, seedSet); |
| | | 72 | | |
| | | 73 | | // Remove the seed IDs — callers want only descendants |
| | 1 | 74 | | descendants.ExceptWith(seedSet); |
| | | 75 | | |
| | 1 | 76 | | return descendants; |
| | | 77 | | } |
| | | 78 | | |
| | | 79 | | /// <summary> |
| | | 80 | | /// Gets a queryable of all folder IDs that have any descendant matching the specified criteria. |
| | | 81 | | /// Can be used in LINQ .Contains() expressions. |
| | | 82 | | /// </summary> |
| | | 83 | | /// <param name="context">Database context.</param> |
| | | 84 | | /// <param name="criteria">The matching criteria to apply.</param> |
| | | 85 | | /// <returns>Queryable of folder IDs.</returns> |
| | | 86 | | public static IQueryable<Guid> GetFolderIdsMatching(JellyfinDbContext context, FolderMatchCriteria criteria) |
| | | 87 | | { |
| | 0 | 88 | | ArgumentNullException.ThrowIfNull(context); |
| | 0 | 89 | | ArgumentNullException.ThrowIfNull(criteria); |
| | 0 | 90 | | var matchingItemIds = criteria switch |
| | 0 | 91 | | { |
| | 0 | 92 | | HasSubtitles => context.MediaStreamInfos |
| | 0 | 93 | | .Where(ms => ms.StreamType == MediaStreamTypeEntity.Subtitle) |
| | 0 | 94 | | .Select(ms => ms.ItemId) |
| | 0 | 95 | | .Distinct() |
| | 0 | 96 | | .ToHashSet(), |
| | 0 | 97 | | HasChapterImages => context.Chapters |
| | 0 | 98 | | .Where(c => c.ImagePath != null) |
| | 0 | 99 | | .Select(c => c.ItemId) |
| | 0 | 100 | | .Distinct() |
| | 0 | 101 | | .ToHashSet(), |
| | 0 | 102 | | HasMediaStreamType m => GetMatchingMediaStreamItemIds(context, m), |
| | 0 | 103 | | _ => throw new ArgumentOutOfRangeException(nameof(criteria), $"Unknown criteria type: {criteria.GetType().Na |
| | 0 | 104 | | }; |
| | | 105 | | |
| | 0 | 106 | | var ancestors = TraverseHierarchyUp(context, matchingItemIds); |
| | | 107 | | |
| | 0 | 108 | | return ancestors.AsQueryable(); |
| | | 109 | | } |
| | | 110 | | |
| | | 111 | | private static HashSet<Guid> GetMatchingMediaStreamItemIds(JellyfinDbContext context, HasMediaStreamType criteria) |
| | | 112 | | { |
| | 0 | 113 | | var query = context.MediaStreamInfos |
| | 0 | 114 | | .Where(ms => ms.StreamType == criteria.StreamType && ms.Language == criteria.Language); |
| | | 115 | | |
| | 0 | 116 | | if (criteria.IsExternal.HasValue) |
| | | 117 | | { |
| | 0 | 118 | | var isExternal = criteria.IsExternal.Value; |
| | 0 | 119 | | query = query.Where(ms => ms.IsExternal == isExternal); |
| | | 120 | | } |
| | | 121 | | |
| | 0 | 122 | | return query.Select(ms => ms.ItemId).Distinct().ToHashSet(); |
| | | 123 | | } |
| | | 124 | | |
| | | 125 | | /// <summary> |
| | | 126 | | /// Traverses DOWN the hierarchy from parent folders to find all descendants. |
| | | 127 | | /// </summary> |
| | | 128 | | private static HashSet<Guid> TraverseHierarchyDown(JellyfinDbContext context, ICollection<Guid> startIds) |
| | | 129 | | { |
| | 0 | 130 | | var visited = new HashSet<Guid>(startIds); |
| | 0 | 131 | | var folderStack = new HashSet<Guid>(startIds); |
| | | 132 | | |
| | 0 | 133 | | while (folderStack.Count != 0) |
| | | 134 | | { |
| | 0 | 135 | | var currentFolders = folderStack.ToArray(); |
| | 0 | 136 | | folderStack.Clear(); |
| | | 137 | | |
| | 0 | 138 | | var directChildren = context.AncestorIds |
| | 0 | 139 | | .WhereOneOrMany(currentFolders, e => e.ParentItemId) |
| | 0 | 140 | | .Select(e => e.ItemId) |
| | 0 | 141 | | .ToArray(); |
| | | 142 | | |
| | 0 | 143 | | var linkedChildren = context.LinkedChildren |
| | 0 | 144 | | .WhereOneOrMany(currentFolders, e => e.ParentId) |
| | 0 | 145 | | .Select(e => e.ChildId) |
| | 0 | 146 | | .ToArray(); |
| | | 147 | | |
| | 0 | 148 | | var allChildren = directChildren.Concat(linkedChildren).Distinct().ToArray(); |
| | | 149 | | |
| | 0 | 150 | | if (allChildren.Length == 0) |
| | | 151 | | { |
| | | 152 | | break; |
| | | 153 | | } |
| | | 154 | | |
| | 0 | 155 | | var childFolders = context.BaseItems |
| | 0 | 156 | | .WhereOneOrMany(allChildren, e => e.Id) |
| | 0 | 157 | | .Where(e => e.IsFolder) |
| | 0 | 158 | | .Select(e => e.Id) |
| | 0 | 159 | | .ToHashSet(); |
| | | 160 | | |
| | 0 | 161 | | foreach (var childId in allChildren) |
| | | 162 | | { |
| | 0 | 163 | | if (visited.Add(childId) && childFolders.Contains(childId)) |
| | | 164 | | { |
| | 0 | 165 | | folderStack.Add(childId); |
| | | 166 | | } |
| | | 167 | | } |
| | | 168 | | } |
| | | 169 | | |
| | 0 | 170 | | return visited; |
| | | 171 | | } |
| | | 172 | | |
| | | 173 | | /// <summary> |
| | | 174 | | /// Traverses DOWN the hierarchy using only AncestorIds (ownership), not LinkedChildren. |
| | | 175 | | /// </summary> |
| | | 176 | | private static HashSet<Guid> TraverseHierarchyDownOwned(JellyfinDbContext context, ICollection<Guid> startIds) |
| | | 177 | | { |
| | 1 | 178 | | var visited = new HashSet<Guid>(startIds); |
| | 1 | 179 | | var folderStack = new HashSet<Guid>(startIds); |
| | | 180 | | |
| | 1 | 181 | | while (folderStack.Count != 0) |
| | | 182 | | { |
| | 1 | 183 | | var currentFolders = folderStack.ToArray(); |
| | 1 | 184 | | folderStack.Clear(); |
| | | 185 | | |
| | 1 | 186 | | var directChildren = context.AncestorIds |
| | 1 | 187 | | .WhereOneOrMany(currentFolders, e => e.ParentItemId) |
| | 1 | 188 | | .Select(e => e.ItemId) |
| | 1 | 189 | | .ToArray(); |
| | | 190 | | |
| | 1 | 191 | | if (directChildren.Length == 0) |
| | | 192 | | { |
| | | 193 | | break; |
| | | 194 | | } |
| | | 195 | | |
| | 0 | 196 | | var childFolders = context.BaseItems |
| | 0 | 197 | | .WhereOneOrMany(directChildren, e => e.Id) |
| | 0 | 198 | | .Where(e => e.IsFolder) |
| | 0 | 199 | | .Select(e => e.Id) |
| | 0 | 200 | | .ToHashSet(); |
| | | 201 | | |
| | 0 | 202 | | foreach (var childId in directChildren) |
| | | 203 | | { |
| | 0 | 204 | | if (visited.Add(childId) && childFolders.Contains(childId)) |
| | | 205 | | { |
| | 0 | 206 | | folderStack.Add(childId); |
| | | 207 | | } |
| | | 208 | | } |
| | | 209 | | } |
| | | 210 | | |
| | 1 | 211 | | return visited; |
| | | 212 | | } |
| | | 213 | | |
| | | 214 | | /// <summary> |
| | | 215 | | /// Traverses UP the hierarchy from items to find all ancestor folders. |
| | | 216 | | /// </summary> |
| | | 217 | | private static HashSet<Guid> TraverseHierarchyUp(JellyfinDbContext context, ICollection<Guid> startIds) |
| | | 218 | | { |
| | 0 | 219 | | var ancestors = new HashSet<Guid>(); |
| | 0 | 220 | | var itemStack = new HashSet<Guid>(startIds); |
| | | 221 | | |
| | 0 | 222 | | while (itemStack.Count != 0) |
| | | 223 | | { |
| | 0 | 224 | | var currentItems = itemStack.ToArray(); |
| | 0 | 225 | | itemStack.Clear(); |
| | | 226 | | |
| | 0 | 227 | | var ancestorParents = context.AncestorIds |
| | 0 | 228 | | .WhereOneOrMany(currentItems, e => e.ItemId) |
| | 0 | 229 | | .Select(e => e.ParentItemId) |
| | 0 | 230 | | .ToArray(); |
| | | 231 | | |
| | 0 | 232 | | var linkedParents = context.LinkedChildren |
| | 0 | 233 | | .WhereOneOrMany(currentItems, e => e.ChildId) |
| | 0 | 234 | | .Select(e => e.ParentId) |
| | 0 | 235 | | .ToArray(); |
| | | 236 | | |
| | 0 | 237 | | foreach (var parentId in ancestorParents.Concat(linkedParents)) |
| | | 238 | | { |
| | 0 | 239 | | if (ancestors.Add(parentId)) |
| | | 240 | | { |
| | 0 | 241 | | itemStack.Add(parentId); |
| | | 242 | | } |
| | | 243 | | } |
| | | 244 | | } |
| | | 245 | | |
| | 0 | 246 | | return ancestors; |
| | | 247 | | } |
| | | 248 | | } |