1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
|
using System;
using System.Collections.Generic;
using System.Linq;
using Jellyfin.Database.Implementations;
using Jellyfin.Database.Implementations.Entities;
using Jellyfin.Database.Implementations.MatchCriteria;
using Microsoft.EntityFrameworkCore;
using Xunit;
namespace Jellyfin.Server.Implementations.Tests.Item;
/// <summary>
/// Verifies the descendant traversals against the SQLite provider: the sets they resolve, and that
/// they stay sub-selects instead of inlining every descendant id into the statement.
/// </summary>
public sealed class DescendantQueryHelperTests : SqliteDbTestFixture
{
private const string FolderType = "MediaBrowser.Controller.Entities.Folder";
private const string BoxSetType = "MediaBrowser.Controller.Entities.Movies.BoxSet";
private const string MovieType = "MediaBrowser.Controller.Entities.Movies.Movie";
private readonly Dictionary<Guid, int> _linkCounters = new();
public DescendantQueryHelperTests()
{
}
[Fact]
public void GetAllDescendantIds_Hierarchy_ReturnsEveryLevelWithoutTheParent()
{
var library = Guid.NewGuid();
var series = Guid.NewGuid();
var season = Guid.NewGuid();
var episode = Guid.NewGuid();
using (var ctx = CreateDbContext())
{
AddFolder(ctx, library);
AddFolder(ctx, series);
AddFolder(ctx, season);
AddItem(ctx, episode, MovieType);
// AncestorIds is a closure: production writes one row per ancestor, not just the parent.
AddAncestors(ctx, series, library);
AddAncestors(ctx, season, series, library);
AddAncestors(ctx, episode, season, series, library);
ctx.SaveChanges();
}
using (var ctx = CreateDbContext())
{
var descendants = DescendantQueryHelper.GetAllDescendantIds(ctx, library).ToHashSet();
Assert.Equal(new[] { series, season, episode }.Order(), descendants.Order());
Assert.DoesNotContain(library, descendants);
}
}
[Fact]
public void GetAllDescendantIds_LinkedFolder_IncludesItsOwnDescendants()
{
var boxSet = Guid.NewGuid();
var series = Guid.NewGuid();
var episode = Guid.NewGuid();
using (var ctx = CreateDbContext())
{
AddItem(ctx, boxSet, BoxSetType, isFolder: true);
AddFolder(ctx, series);
AddItem(ctx, episode, MovieType);
AddAncestors(ctx, episode, series);
AddLink(ctx, boxSet, series);
ctx.SaveChanges();
}
using (var ctx = CreateDbContext())
{
var descendants = DescendantQueryHelper.GetAllDescendantIds(ctx, boxSet).ToHashSet();
Assert.Contains(series, descendants);
Assert.Contains(episode, descendants);
}
}
// Timeout so that a missing termination guard fails the test instead of hanging the run.
[Fact(Timeout = 30000)]
public void GetAllDescendantIds_NestedLinks_AreFollowedAndCyclesTerminate()
{
var outer = Guid.NewGuid();
var inner = Guid.NewGuid();
var movie = Guid.NewGuid();
using (var ctx = CreateDbContext())
{
AddItem(ctx, outer, BoxSetType, isFolder: true);
AddItem(ctx, inner, BoxSetType, isFolder: true);
AddItem(ctx, movie, MovieType);
AddLink(ctx, outer, inner);
AddLink(ctx, inner, movie);
// The traversal must not spin on this cycle.
AddLink(ctx, inner, outer);
ctx.SaveChanges();
}
using (var ctx = CreateDbContext())
{
var descendants = DescendantQueryHelper.GetAllDescendantIds(ctx, outer).ToHashSet();
Assert.Contains(inner, descendants);
Assert.Contains(movie, descendants);
Assert.DoesNotContain(outer, descendants);
}
}
[Fact]
public void GetAllDescendantIds_LinksOfNonFolders_AreNotFollowed()
{
var library = Guid.NewGuid();
var movie = Guid.NewGuid();
var alternateVersion = Guid.NewGuid();
using (var ctx = CreateDbContext())
{
AddFolder(ctx, library);
AddItem(ctx, movie, MovieType);
AddItem(ctx, alternateVersion, MovieType);
AddAncestors(ctx, movie, library);
// An alternate version hangs off the movie by link, and the movie is not a folder.
AddLink(ctx, movie, alternateVersion);
ctx.SaveChanges();
}
using (var ctx = CreateDbContext())
{
var descendants = DescendantQueryHelper.GetAllDescendantIds(ctx, library).ToHashSet();
Assert.Contains(movie, descendants);
Assert.DoesNotContain(alternateVersion, descendants);
}
}
[Fact]
public void GetAllDescendantIds_ClosureSeamAboveTheCollectionFolder_IsCrossed()
{
var userRoot = Guid.NewGuid();
var collectionFolder = Guid.NewGuid();
var series = Guid.NewGuid();
var episode = Guid.NewGuid();
var boxSet = Guid.NewGuid();
var linkedMovie = Guid.NewGuid();
using (var ctx = CreateDbContext())
{
AddFolder(ctx, userRoot);
AddFolder(ctx, collectionFolder);
AddFolder(ctx, series);
AddItem(ctx, episode, MovieType);
AddItem(ctx, boxSet, BoxSetType, isFolder: true);
AddItem(ctx, linkedMovie, MovieType);
// An item carries its own chain plus its collection folder, but not the user root above
// it, so one hop from the user root stops at the collection folder.
AddAncestors(ctx, collectionFolder, userRoot);
AddAncestors(ctx, series, collectionFolder);
AddAncestors(ctx, episode, series, collectionFolder);
AddAncestors(ctx, boxSet, collectionFolder);
// The box set is only reachable across the seam, and its links have to be followed too.
AddLink(ctx, boxSet, linkedMovie);
ctx.SaveChanges();
}
using (var ctx = CreateDbContext())
{
var descendants = DescendantQueryHelper.GetAllDescendantIds(ctx, userRoot).ToHashSet();
Assert.Equal(
new[] { collectionFolder, series, episode, boxSet, linkedMovie }.Order(),
descendants.Order());
}
}
[Fact]
public void GetOwnedDescendantIds_ClosureSeamAboveTheCollectionFolder_IsCrossed()
{
var userRoot = Guid.NewGuid();
var collectionFolder = Guid.NewGuid();
var series = Guid.NewGuid();
var episode = Guid.NewGuid();
var boxSet = Guid.NewGuid();
var linkedMovie = Guid.NewGuid();
using (var ctx = CreateDbContext())
{
AddFolder(ctx, userRoot);
AddFolder(ctx, collectionFolder);
AddFolder(ctx, series);
AddItem(ctx, episode, MovieType);
AddItem(ctx, boxSet, BoxSetType, isFolder: true);
AddItem(ctx, linkedMovie, MovieType);
AddAncestors(ctx, collectionFolder, userRoot);
AddAncestors(ctx, series, collectionFolder);
AddAncestors(ctx, episode, series, collectionFolder);
AddAncestors(ctx, boxSet, collectionFolder);
AddLink(ctx, boxSet, linkedMovie);
ctx.SaveChanges();
}
using (var ctx = CreateDbContext())
{
// Owned only: the linked movie stays out, or deleting a library would delete it.
var expected = new[] { collectionFolder, series, episode, boxSet }.Order();
Assert.Equal(expected, DescendantQueryHelper.GetOwnedDescendantIds(ctx, userRoot).ToHashSet().Order());
Assert.Equal(expected, DescendantQueryHelper.GetOwnedDescendantIdsBatch(ctx, [userRoot]).Order());
}
}
[Fact]
public void GetFolderIdsMatching_LinkAboveAClosure_ReturnsTheLinkingFolder()
{
var collections = Guid.NewGuid();
var boxSet = Guid.NewGuid();
var library = Guid.NewGuid();
var series = Guid.NewGuid();
var episode = Guid.NewGuid();
var otherLibrary = Guid.NewGuid();
var otherBoxSet = Guid.NewGuid();
var silentMovie = Guid.NewGuid();
using (var ctx = CreateDbContext())
{
AddFolder(ctx, collections);
AddItem(ctx, boxSet, BoxSetType, isFolder: true);
AddFolder(ctx, library);
AddFolder(ctx, series);
AddItem(ctx, episode, MovieType);
AddAncestors(ctx, boxSet, collections);
AddAncestors(ctx, series, library);
AddAncestors(ctx, episode, series, library);
// The link lands on the series, not on the episode that carries the subtitles.
AddLink(ctx, boxSet, series);
AddStream(ctx, episode, MediaStreamTypeEntity.Subtitle);
AddFolder(ctx, otherLibrary);
AddItem(ctx, otherBoxSet, BoxSetType, isFolder: true);
AddItem(ctx, silentMovie, MovieType);
AddAncestors(ctx, otherBoxSet, collections);
AddAncestors(ctx, silentMovie, otherLibrary);
AddLink(ctx, otherBoxSet, silentMovie);
// A stream of another type: the criteria, not the mere presence of a stream, decides.
AddStream(ctx, silentMovie, MediaStreamTypeEntity.Video);
ctx.SaveChanges();
}
using (var ctx = CreateDbContext())
{
var folders = DescendantQueryHelper.GetFolderIdsMatching(ctx, new HasSubtitles()).ToHashSet();
Assert.Equal(new[] { library, series, boxSet, collections }.Order(), folders.Order());
}
}
[Fact(Timeout = 30000)]
public void GetFolderIdsMatching_NestedLinks_AreFollowedAndCyclesTerminate()
{
var outer = Guid.NewGuid();
var inner = Guid.NewGuid();
var movie = Guid.NewGuid();
var silentSet = Guid.NewGuid();
var silentMovie = Guid.NewGuid();
using (var ctx = CreateDbContext())
{
AddItem(ctx, outer, BoxSetType, isFolder: true);
AddItem(ctx, inner, BoxSetType, isFolder: true);
AddItem(ctx, movie, MovieType);
AddLink(ctx, outer, inner);
AddLink(ctx, inner, movie);
// Resolving the link parents must not spin on this cycle.
AddLink(ctx, inner, outer);
AddStream(ctx, movie, MediaStreamTypeEntity.Subtitle);
AddItem(ctx, silentSet, BoxSetType, isFolder: true);
AddItem(ctx, silentMovie, MovieType);
AddLink(ctx, silentSet, silentMovie);
AddStream(ctx, silentMovie, MediaStreamTypeEntity.Video);
ctx.SaveChanges();
}
using (var ctx = CreateDbContext())
{
var folders = DescendantQueryHelper.GetFolderIdsMatching(ctx, new HasSubtitles()).ToHashSet();
Assert.Equal(new[] { inner, outer }.Order(), folders.Order());
}
}
[Fact]
public void GetFolderIdsMatching_ClosureSeamAboveTheCollectionFolder_IsCrossed()
{
var userRoot = Guid.NewGuid();
var collectionFolder = Guid.NewGuid();
var series = Guid.NewGuid();
var episode = Guid.NewGuid();
using (var ctx = CreateDbContext())
{
AddFolder(ctx, userRoot);
AddFolder(ctx, collectionFolder);
AddFolder(ctx, series);
AddItem(ctx, episode, MovieType);
// The closure is not transitive at this seam: no item records the user root.
AddAncestors(ctx, episode, series, collectionFolder);
AddAncestors(ctx, series, collectionFolder);
AddAncestors(ctx, collectionFolder, userRoot);
AddStream(ctx, episode, MediaStreamTypeEntity.Subtitle);
ctx.SaveChanges();
}
using (var ctx = CreateDbContext())
{
var folders = DescendantQueryHelper.GetFolderIdsMatching(ctx, new HasSubtitles()).ToHashSet();
Assert.Equal(new[] { series, collectionFolder, userRoot }.Order(), folders.Order());
}
}
[Fact]
public void GetFolderIdsMatching_LinkedFolder_MatchesOnLanguageOnly()
{
var boxSet = Guid.NewGuid();
var series = Guid.NewGuid();
var episode = Guid.NewGuid();
using (var ctx = CreateDbContext())
{
AddItem(ctx, boxSet, BoxSetType, isFolder: true);
AddFolder(ctx, series);
AddItem(ctx, episode, MovieType);
AddAncestors(ctx, episode, series);
AddLink(ctx, boxSet, series);
AddStream(ctx, episode, MediaStreamTypeEntity.Subtitle, "ger");
ctx.SaveChanges();
}
using (var ctx = CreateDbContext())
{
var german = new HasMediaStreamType(MediaStreamTypeEntity.Subtitle, ["ger"]);
var french = new HasMediaStreamType(MediaStreamTypeEntity.Subtitle, ["fre"]);
Assert.Equal(new[] { series, boxSet }.Order(), DescendantQueryHelper.GetFolderIdsMatching(ctx, german).ToHashSet().Order());
Assert.Empty(DescendantQueryHelper.GetFolderIdsMatching(ctx, french).ToArray());
}
}
[Fact]
public void GetFolderIdsMatching_AlternateVersionLinks_AreNotWalked()
{
var collections = Guid.NewGuid();
var boxSet = Guid.NewGuid();
var library = Guid.NewGuid();
var movie = Guid.NewGuid();
var alternateVersion = Guid.NewGuid();
using (var ctx = CreateDbContext())
{
AddFolder(ctx, collections);
AddItem(ctx, boxSet, BoxSetType, isFolder: true);
AddFolder(ctx, library);
AddItem(ctx, movie, MovieType);
AddItem(ctx, alternateVersion, MovieType);
AddAncestors(ctx, boxSet, collections);
AddAncestors(ctx, movie, library);
AddAncestors(ctx, alternateVersion, library);
// Only the second file carries the subtitles, and it hangs off the movie by an alternate
// version link. The movie is not a folder, so that link is not a parent-child edge.
AddLink(ctx, movie, alternateVersion, LinkedChildType.LocalAlternateVersion);
AddLink(ctx, boxSet, movie);
AddStream(ctx, alternateVersion, MediaStreamTypeEntity.Subtitle);
ctx.SaveChanges();
}
using (var ctx = CreateDbContext())
{
var folders = DescendantQueryHelper.GetFolderIdsMatching(ctx, new HasSubtitles()).ToHashSet();
// The library still matches: the alternate version carries its own closure. The box set does
// not, matching the descendant side, which does not follow a non-folder's links either.
Assert.Equal([library], folders);
}
}
[Fact]
public void GetOwnedDescendantIds_IgnoresLinkedChildren()
{
var boxSet = Guid.NewGuid();
var owned = Guid.NewGuid();
var linked = Guid.NewGuid();
using (var ctx = CreateDbContext())
{
AddItem(ctx, boxSet, BoxSetType, isFolder: true);
AddItem(ctx, owned, MovieType);
AddItem(ctx, linked, MovieType);
AddAncestors(ctx, owned, boxSet);
AddLink(ctx, boxSet, linked);
ctx.SaveChanges();
}
using (var ctx = CreateDbContext())
{
Assert.Equal([owned], DescendantQueryHelper.GetOwnedDescendantIds(ctx, boxSet).ToArray());
Assert.Equal([owned], DescendantQueryHelper.GetOwnedDescendantIdsBatch(ctx, [boxSet]).ToArray());
}
}
[Fact]
public void GetAllDescendantIds_StatementSizeDoesNotGrowWithTheLibrary()
{
var small = SeedLibrary(10);
var large = SeedLibrary(500);
using var ctx = CreateDbContext();
var smallSql = CountingQuery(ctx, small).ToQueryString();
var largeSql = CountingQuery(ctx, large).ToQueryString();
// Reading the ids into memory and handing them back as AsQueryable() makes EF inline one
// literal per descendant, which is what allocated megabytes per call.
Assert.Equal(smallSql.Length, largeSql.Length);
Assert.Contains("AncestorIds", smallSql, StringComparison.Ordinal);
Assert.Equal(10, CountingQuery(ctx, small).Count());
Assert.Equal(500, CountingQuery(ctx, large).Count());
}
private static IQueryable<BaseItemEntity> CountingQuery(JellyfinDbContext context, Guid libraryId)
{
var descendantIds = DescendantQueryHelper.GetAllDescendantIds(context, libraryId);
return context.BaseItems
.AsNoTracking()
.Where(b => descendantIds.Contains(b.Id))
.Where(DescendantQueryHelper.IsCountableLeaf);
}
private Guid SeedLibrary(int childCount)
{
var library = Guid.NewGuid();
using var ctx = CreateDbContext();
AddFolder(ctx, library);
for (var i = 0; i < childCount; i++)
{
var child = Guid.NewGuid();
AddItem(ctx, child, MovieType);
AddAncestors(ctx, child, library);
}
ctx.SaveChanges();
return library;
}
private static void AddFolder(JellyfinDbContext context, Guid id)
=> AddItem(context, id, FolderType, isFolder: true);
private static void AddItem(JellyfinDbContext context, Guid id, string type, bool isFolder = false)
=> context.BaseItems.Add(new BaseItemEntity
{
Id = id,
Type = type,
Name = type + " " + id,
IsFolder = isFolder
});
private static void AddStream(JellyfinDbContext context, Guid itemId, MediaStreamTypeEntity type, string? language = null)
=> context.MediaStreamInfos.Add(new MediaStreamInfo
{
ItemId = itemId,
StreamIndex = 0,
StreamType = type,
Language = language,
Item = null!
});
private static void AddAncestors(JellyfinDbContext context, Guid itemId, params Guid[] ancestorIds)
{
foreach (var ancestorId in ancestorIds)
{
context.AncestorIds.Add(new AncestorId
{
ItemId = itemId,
ParentItemId = ancestorId,
Item = null!,
ParentItem = null!
});
}
}
// LinkedChildren is keyed on (ParentId, SortOrder), so every link of a parent needs its own slot.
private void AddLink(JellyfinDbContext context, Guid parentId, Guid childId, LinkedChildType childType = LinkedChildType.Manual)
{
_linkCounters.TryGetValue(parentId, out var sortOrder);
_linkCounters[parentId] = sortOrder + 1;
context.LinkedChildren.Add(new LinkedChildEntity
{
ParentId = parentId,
ChildId = childId,
ChildType = childType,
SortOrder = sortOrder
});
}
}
|