mirror of
https://github.com/BinaryAlley/Nemera.git
synced 2026-12-02 23:11:10 +00:00
Fixes large library scans hitting the SQLite parameter limit
Bounds path lookups and enrichment resets to 20,000 parameters per query, restores failed scans from their staged metadata, and clears staged data only after the materialized items are saved.
This commit is contained in:
1 parent
800f4ab334
commit
6ef9592f00
22 files changed
+542
-94
No files matched your search
+29
@@ -82,6 +82,35 @@ public class BookRepositoryTests
|
||||
Assert.Equal(MetadataStatus.Enriched, keptBook!.MetadataStatus);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task GetExistingPathsAsync_WhenMorePathsAreRequestedThanTheStorageAccepts_ShouldReturnTheExistingPaths()
|
||||
{
|
||||
// Arrange
|
||||
// A scan of a very large library stages more paths than SQLite accepts as the parameters of one statement, which used to fail the whole scan.
|
||||
using SqliteConnection anchorConnection = new($"Data Source=luminadataccess-bookrepo-existingpaths-large-{Guid.NewGuid()};Mode=Memory;Cache=Shared");
|
||||
anchorConnection.Open();
|
||||
using LuminaDbContext context = new(new DbContextOptionsBuilder<LuminaDbContext>().UseSqlite(anchorConnection.ConnectionString).Options);
|
||||
context.Database.EnsureCreated();
|
||||
BookRepository sut = new(context);
|
||||
|
||||
Guid libraryId = Guid.NewGuid();
|
||||
BookEntity book = _bookEntityFixture.Create();
|
||||
book.LibraryId = libraryId;
|
||||
book.Path = "/books/tolkien/the-hobbit.epub";
|
||||
context.Books.Add(book);
|
||||
await context.SaveChangesAsync();
|
||||
List<string> requestedPaths = [.. Enumerable.Range(0, 120_000).Select(index => $"/books/tolkien/book-{index}.epub")];
|
||||
requestedPaths.Add("/books/tolkien/the-hobbit.epub");
|
||||
|
||||
// Act
|
||||
Result<IReadOnlyCollection<string>> result = await sut.GetExistingPathsAsync(libraryId, requestedPaths, CancellationToken.None);
|
||||
|
||||
// Assert
|
||||
Assert.False(result.IsFailure);
|
||||
string existingPath = Assert.Single(result.Value);
|
||||
Assert.Equal("/books/tolkien/the-hobbit.epub", existingPath);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task ResetMetadataStatusForLibraryAsync_WhenCalled_ShouldResetTheMetadataStatusOfAllBooksOfTheLibrary()
|
||||
{
|
||||
|
||||
+33
-2
@@ -36,6 +36,7 @@ public class LibraryScanSnapshotRepositoryTests : IDisposable
|
||||
private readonly LibraryScanSnapshotRepository _sut;
|
||||
private readonly LibraryScanSnapshotEntityFixture _libraryScanSnapshotEntityFixture = new();
|
||||
private readonly LibraryScanStagingResultsEntityFixture _libraryScanStagingResultsEntityFixture = new();
|
||||
private readonly LibraryScanResultEntityFixture _libraryScanResultEntityFixture = new();
|
||||
private readonly UserEntityFixture _userEntityFixture = new();
|
||||
private readonly LibraryEntityFixture _libraryEntityFixture = new();
|
||||
private readonly LibraryScanEntityFixture _libraryScanEntityFixture = new();
|
||||
@@ -128,7 +129,7 @@ public class LibraryScanSnapshotRepositoryTests : IDisposable
|
||||
await _context.SaveChangesAsync();
|
||||
|
||||
// Act
|
||||
Result<IReadOnlyList<string>> result = await _sut.GetPathsAsync(library.Id, CancellationToken.None);
|
||||
Result<IReadOnlySet<string>> result = await _sut.GetPathsAsync(library.Id, CancellationToken.None);
|
||||
|
||||
// Assert
|
||||
Assert.False(result.IsFailure);
|
||||
@@ -144,7 +145,7 @@ public class LibraryScanSnapshotRepositoryTests : IDisposable
|
||||
(_, LibraryEntity library, _) = await SeedScanGraphAsync();
|
||||
|
||||
// Act
|
||||
Result<IReadOnlyList<string>> result = await _sut.GetPathsAsync(library.Id, CancellationToken.None);
|
||||
Result<IReadOnlySet<string>> result = await _sut.GetPathsAsync(library.Id, CancellationToken.None);
|
||||
|
||||
// Assert
|
||||
Assert.False(result.IsFailure);
|
||||
@@ -224,6 +225,36 @@ public class LibraryScanSnapshotRepositoryTests : IDisposable
|
||||
Assert.False(await _context.LibraryScanStagingResults.AsNoTracking().AnyAsync(staging => staging.LibraryScanId == scanId));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task RollbackSnapshotForScanAsync_WhenCalled_ShouldRemoveOnlyTheSnapshotEntriesTheScanAddedOrModified()
|
||||
{
|
||||
// Arrange
|
||||
(_, LibraryEntity library, Guid scanId) = await SeedScanGraphAsync();
|
||||
LibraryScanSnapshotEntity untouchedSnapshot = _libraryScanSnapshotEntityFixture.Create(libraryId: library.Id, path: "/books/untouched.epub");
|
||||
LibraryScanSnapshotEntity newSnapshot = _libraryScanSnapshotEntityFixture.Create(libraryId: library.Id, path: "/books/new.epub");
|
||||
LibraryScanSnapshotEntity modifiedSnapshot = _libraryScanSnapshotEntityFixture.Create(libraryId: library.Id, path: "/books/modified.epub");
|
||||
_context.LibraryScanSnapshots.AddRange(untouchedSnapshot, newSnapshot, modifiedSnapshot);
|
||||
LibraryScanResultEntity newResult = _libraryScanResultEntityFixture.Create(libraryScanId: scanId, status: LibraryScanFileStatus.New, path: "/books/new.epub");
|
||||
LibraryScanResultEntity modifiedResult = _libraryScanResultEntityFixture.Create(libraryScanId: scanId, status: LibraryScanFileStatus.Modified, path: "/books/modified.epub");
|
||||
LibraryScanResultEntity deletedResult = _libraryScanResultEntityFixture.Create(libraryScanId: scanId, status: LibraryScanFileStatus.Deleted, path: "/books/deleted.epub");
|
||||
_context.LibraryScanResults.AddRange(newResult, modifiedResult, deletedResult);
|
||||
await _context.SaveChangesAsync();
|
||||
|
||||
// Act
|
||||
Result<Deleted> result = await _sut.RollbackSnapshotForScanAsync(library.Id, scanId, CancellationToken.None);
|
||||
|
||||
// Assert
|
||||
Assert.False(result.IsFailure);
|
||||
Assert.Equal(Result.Deleted, result.Value);
|
||||
List<LibraryScanSnapshotEntity> snapshots = await _context.LibraryScanSnapshots.AsNoTracking()
|
||||
.Where(snapshot => snapshot.LibraryId == library.Id)
|
||||
.ToListAsync();
|
||||
LibraryScanSnapshotEntity remainingSnapshot = Assert.Single(snapshots);
|
||||
Assert.Equal("/books/untouched.epub", remainingSnapshot.Path);
|
||||
// The audit results are kept: they describe what the failed scan found, and only the snapshot is rolled back.
|
||||
Assert.Equal(3, await _context.LibraryScanResults.AsNoTracking().CountAsync(scanResult => scanResult.LibraryScanId == scanId));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Seeds the user, media library and media library scan required by the foreign keys of the snapshot and staging tables.
|
||||
/// </summary>
|
||||
|
||||
+33
@@ -99,6 +99,39 @@ public class MusicLibraryScanItemMetadataRepositoryTests : IDisposable
|
||||
Assert.Empty(result.Value);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task GetByLibraryIdAsync_WhenCalled_ShouldReturnAllTheItemsOfTheLibraryOrderedByPath()
|
||||
{
|
||||
// Arrange
|
||||
Guid libraryId = Guid.NewGuid();
|
||||
MusicLibraryScanItemMetadataEntity itemOfFirstScan = _musicLibraryScanItemMetadataEntityFixture.Create(
|
||||
libraryId: libraryId, libraryScanId: Guid.NewGuid(), path: "/music/queen/bohemian-rhapsody.flac");
|
||||
MusicLibraryScanItemMetadataEntity itemOfSecondScan = _musicLibraryScanItemMetadataEntityFixture.Create(
|
||||
libraryId: libraryId, libraryScanId: Guid.NewGuid(), path: "/music/queen/bicycle-race.flac");
|
||||
MusicLibraryScanItemMetadataEntity itemOfAnotherLibrary = _musicLibraryScanItemMetadataEntityFixture.Create(
|
||||
libraryId: Guid.NewGuid(), path: "/music/queen/love-of-my-life.flac");
|
||||
_context.MusicLibraryScanItemMetadata.AddRange(itemOfFirstScan, itemOfSecondScan, itemOfAnotherLibrary);
|
||||
await _context.SaveChangesAsync();
|
||||
|
||||
// Act
|
||||
Result<IReadOnlyList<MusicLibraryScanItemMetadataEntity>> result = await _sut.GetByLibraryIdAsync(libraryId, CancellationToken.None);
|
||||
|
||||
// Assert
|
||||
Assert.False(result.IsFailure);
|
||||
Assert.Equal([itemOfSecondScan.Id, itemOfFirstScan.Id], result.Value.Select(item => item.Id));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task GetByLibraryIdAsync_WhenNoItemsExistForTheLibrary_ShouldReturnAnEmptyCollection()
|
||||
{
|
||||
// Act
|
||||
Result<IReadOnlyList<MusicLibraryScanItemMetadataEntity>> result = await _sut.GetByLibraryIdAsync(Guid.NewGuid(), CancellationToken.None);
|
||||
|
||||
// Assert
|
||||
Assert.False(result.IsFailure);
|
||||
Assert.Empty(result.Value);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task DeleteByScanIdAsync_WhenCalled_ShouldDeleteOnlyTheItemsOfTheScan()
|
||||
{
|
||||
|
||||
+28
@@ -189,6 +189,34 @@ public class TrackRepositoryTests
|
||||
Assert.Equal("/music/Queen/Love-Of-My-Life.flac", existingPath);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task GetExistingPathsAsync_WhenMorePathsAreRequestedThanTheStorageAccepts_ShouldReturnTheExistingPaths()
|
||||
{
|
||||
// Arrange
|
||||
// A scan of a very large library stages more paths than SQLite accepts as the parameters of one statement, which used to fail the whole scan.
|
||||
using SqliteConnection anchorConnection = new($"Data Source=luminadataccess-trackrepo-existingpaths-large-{Guid.NewGuid()};Mode=Memory;Cache=Shared");
|
||||
anchorConnection.Open();
|
||||
using LuminaDbContext context = new(new DbContextOptionsBuilder<LuminaDbContext>().UseSqlite(anchorConnection.ConnectionString).Options);
|
||||
context.Database.EnsureCreated();
|
||||
TrackRepository sut = new(context);
|
||||
|
||||
Guid libraryId = Guid.NewGuid();
|
||||
AlbumEntity album = await SeedAlbumAsync(context, libraryId);
|
||||
TrackEntity track = _trackEntityFixture.Create(albumId: album.Id, libraryId: libraryId, path: "/music/queen/bohemian-rhapsody.flac", includeMetadata: false);
|
||||
context.Tracks.Add(track);
|
||||
await context.SaveChangesAsync();
|
||||
List<string> requestedPaths = [.. Enumerable.Range(0, 120_000).Select(index => $"/music/queen/track-{index}.flac")];
|
||||
requestedPaths.Add("/music/queen/bohemian-rhapsody.flac");
|
||||
|
||||
// Act
|
||||
Result<IReadOnlyCollection<string>> result = await sut.GetExistingPathsAsync(libraryId, requestedPaths, CancellationToken.None);
|
||||
|
||||
// Assert
|
||||
Assert.False(result.IsFailure);
|
||||
string existingPath = Assert.Single(result.Value);
|
||||
Assert.Equal("/music/queen/bohemian-rhapsody.flac", existingPath);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task UpdateAsync_WhenNothingChanged_ShouldNotChangeAnyAuditColumn()
|
||||
{
|
||||
|
||||
Reference in new issue
Block a user