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