diff --git a/src/Titano.Data/Archiving/MassRenamer.cs b/src/Titano.Data/Archiving/MassRenamer.cs index ad46a81b..6846e70d 100644 --- a/src/Titano.Data/Archiving/MassRenamer.cs +++ b/src/Titano.Data/Archiving/MassRenamer.cs @@ -93,55 +93,106 @@ public sealed class MassRenamer } } RenameJournal.Write(paths, journal); - for (var i = 0; i < journal.Plan.Steps.Count; i++) + var steps = journal.Plan.Steps; + foreach (var step in steps) { - var step = journal.Plan.Steps[i]; if (completed.Contains(step.SequenceId)) continue; cancellationToken.ThrowIfCancellationRequested(); - try - { - await ApplyStepAsync(archive, paths, step, cancellationToken).ConfigureAwait(false); - } - catch (StaleRenamePlanException) when (resuming) - { - Log.Warning("archive", $"Rename of {step.FromFolder} dropped: the index no longer has that sequence there"); - } + // A folder that moves carries the sequences inside it: they are locked, moved and recorded with it. + IReadOnlyList carried = step.MovesFolder && !step.Carried + ? [.. steps.Where(c => c.Carried && !completed.Contains(c.SequenceId) && Carrier(steps, c) == step)] + : []; + await ApplyStepAsync(archive, paths, step, carried, resuming, cancellationToken).ConfigureAwait(false); completed.Add(step.SequenceId); + foreach (var c in carried) completed.Add(c.SequenceId); RenameJournal.Write(paths, journal with { Completed = [.. completed] }); - progress?.Report(i + 1); + progress?.Report(completed.Count); } RenameJournal.Delete(paths, journal.Plan.Id); } - /// Folder, then sidecar title, then index; each part is idempotent so a resumed step can repeat it. - private async Task ApplyStepAsync(Archive archive, ArchivePaths paths, RenameStep step, CancellationToken cancellationToken) + /// The step whose folder move carries : the nearest moving folder around it (as planned). + private static RenameStep? Carrier(IReadOnlyList steps, RenameStep carried) => + steps.Where(m => !m.Carried && m.MovesFolder && RenamePlanner.IsInside(m.FromFolder, carried.FromFolder)).MaxBy(m => m.FromFolder.Length); + + /// + /// Applies a step, and the steps of the sequences its folder carries, under all their leases: sequence leases, then + /// for a move the thumbnail leases, each by sequence id (the order trash and pack compaction take them in). No + /// writer of a carried sequence (ingest, an editor save, a thumbnail pass) works in the folder while it moves or + /// before its index row follows. + /// + /// A step whose sequence the index no longer has where the plan says is dropped instead of + /// failing the rename: its sidecar still carries the id, so the next scan indexes it wherever it ended up. + private async Task ApplyStepAsync(Archive archive, ArchivePaths paths, RenameStep step, IReadOnlyList carried, bool resuming, + CancellationToken cancellationToken) { - await using (await _writer.LockAsync(paths, step.SequenceId, cancellationToken).ConfigureAwait(false)) + var ids = carried.Select(c => c.SequenceId).Append(step.SequenceId).Order().ToList(); + var locks = new List(ids.Count); + var leases = new List(ids.Count); + try { - var row = await _db.Sequences.GetAsync(step.SequenceId, cancellationToken).ConfigureAwait(false); - if (row is null || (row.Folder != step.FromFolder && row.Folder != step.ToFolder)) throw new StaleRenamePlanException(step.SequenceId); - RenamePlanner.MoveFolder(paths, step); - var sequence = paths.Sequence(step.ToFolder); - var manifest = SequenceWriter.ReadLocked(sequence, step.SequenceId); - if (manifest.Title != step.ToTitle) + foreach (var id in ids) locks.Add(await _writer.LockAsync(paths, id, cancellationToken).ConfigureAwait(false)); + if (step.MovesFolder) { - manifest = manifest with + foreach (var id in ids) { - Title = step.ToTitle, - Versions = manifest.Versions with { Settings = manifest.Versions.Settings + 1 }, - UpdatedAt = DateTimeOffset.UtcNow, - }; - ManifestStore.WriteSequence(sequence.ManifestPath, manifest); + leases.Add(await LeaseFile.AcquireAsync(paths.LocksDirectory, $"thumbs-{id:N}", _writer.Options.Owner, _writer.Options.LeaseWait, + _writer.Options.LeaseDuration, cancellationToken).ConfigureAwait(false)); + } } - await using (var tx = await _db.BeginAsync(cancellationToken).ConfigureAwait(false)) + // Checked before anything moves; a dropped step drops the sequences it would have carried, which stay inside it. + if (!await StandsAsync(step, resuming, cancellationToken).ConfigureAwait(false)) return; + var follow = new List(carried.Count); + foreach (var c in carried) { - var existing = await tx.Sequences.GetAsync(step.SequenceId, cancellationToken).ConfigureAwait(false); - await tx.Sequences.UpsertAsync(SequenceMapping.ToSequence(manifest, archive.Id, sequence, existing), cancellationToken).ConfigureAwait(false); - if (step.MovesFolder) await tx.Frames.RebaseFolderAsync(step.SequenceId, step.FromFolder, step.ToFolder, cancellationToken).ConfigureAwait(false); - await tx.Archives.DeleteFolderStateAsync(archive.Id, step.FromFolder, cancellationToken).ConfigureAwait(false); - await tx.CommitAsync(cancellationToken).ConfigureAwait(false); + if (await StandsAsync(c, resuming, cancellationToken).ConfigureAwait(false)) follow.Add(c); } - await _indexer.RecordStampsAsync(archive, sequence, step.SequenceId, cancellationToken).ConfigureAwait(false); + RenamePlanner.MoveFolder(paths, step); + await RecordAsync(archive, paths, step, cancellationToken).ConfigureAwait(false); + // Moved with their carrier: only their sidecars and index rows follow. + foreach (var c in follow) await RecordAsync(archive, paths, c, cancellationToken).ConfigureAwait(false); + } + finally + { + for (var i = leases.Count - 1; i >= 0; i--) leases[i].Dispose(); + for (var i = locks.Count - 1; i >= 0; i--) await locks[i].DisposeAsync().ConfigureAwait(false); } } + + /// The index still has the step's sequence where the plan says (or where it already took it); otherwise stale. + /// Not resuming, and the sequence moved or vanished since the plan was made. + private async Task StandsAsync(RenameStep step, bool resuming, CancellationToken cancellationToken) + { + var row = await _db.Sequences.GetAsync(step.SequenceId, cancellationToken).ConfigureAwait(false); + if (row is not null && (row.Folder == step.FromFolder || row.Folder == step.ToFolder)) return true; + if (!resuming) throw new StaleRenamePlanException(step.SequenceId); + Log.Warning("archive", $"Rename of {step.FromFolder} dropped: the index no longer has that sequence there"); + return false; + } + + /// Sidecar title, then index (folder, frames' paths); each part is idempotent so a resumed step can repeat it. + private async Task RecordAsync(Archive archive, ArchivePaths paths, RenameStep step, CancellationToken cancellationToken) + { + var sequence = paths.Sequence(step.ToFolder); + var manifest = SequenceWriter.ReadLocked(sequence, step.SequenceId); + if (manifest.Title != step.ToTitle) + { + manifest = manifest with + { + Title = step.ToTitle, + Versions = manifest.Versions with { Settings = manifest.Versions.Settings + 1 }, + UpdatedAt = DateTimeOffset.UtcNow, + }; + ManifestStore.WriteSequence(sequence.ManifestPath, manifest); + } + await using (var tx = await _db.BeginAsync(cancellationToken).ConfigureAwait(false)) + { + var existing = await tx.Sequences.GetAsync(step.SequenceId, cancellationToken).ConfigureAwait(false); + await tx.Sequences.UpsertAsync(SequenceMapping.ToSequence(manifest, archive.Id, sequence, existing), cancellationToken).ConfigureAwait(false); + if (step.MovesFolder) await tx.Frames.RebaseFolderAsync(step.SequenceId, step.FromFolder, step.ToFolder, cancellationToken).ConfigureAwait(false); + await tx.Archives.DeleteFolderStateAsync(archive.Id, step.FromFolder, cancellationToken).ConfigureAwait(false); + await tx.CommitAsync(cancellationToken).ConfigureAwait(false); + } + await _indexer.RecordStampsAsync(archive, sequence, step.SequenceId, cancellationToken).ConfigureAwait(false); + } } diff --git a/tests/Titano.Data.Tests/Archiving/MassRenameTests.cs b/tests/Titano.Data.Tests/Archiving/MassRenameTests.cs index c54ca153..d15b5036 100644 --- a/tests/Titano.Data.Tests/Archiving/MassRenameTests.cs +++ b/tests/Titano.Data.Tests/Archiving/MassRenameTests.cs @@ -136,6 +136,30 @@ public sealed class MassRenameTests(TestContext context) Assert.Empty((await fx.Service.FindOrphansAsync(fx.Archive.Id, fx.Ct)).MissingSequences); } + [Test] + public async Task AFolderMovesOnceTheWritersOfTheSequencesInsideAreDone() + { + await using var fx = await CreateAsync(context); + var ids = await NestedAsync(fx); + var before = await StateAsync(fx, ids); + var plan = await fx.Service.PlanRenameAsync(fx.Archive.Id, [ids[0]], "moved/{date}", null, fx.Ct); + + // An ingest into trips/day2 holds that sequence's lease; a thumbnail pass of trips/day2/dawn holds its pack's. + Task apply; + using (await LeaseFile.AcquireAsync(fx.Paths.LocksDirectory, $"seq-{ids[1]:N}", "ingest", TimeSpan.FromSeconds(5), cancellationToken: fx.Ct)) + using (await LeaseFile.AcquireAsync(fx.Paths.LocksDirectory, $"thumbs-{ids[2]:N}", "thumbnails", TimeSpan.FromSeconds(5), cancellationToken: fx.Ct)) + { + apply = fx.Service.ApplyRenameAsync(plan, null, fx.Ct); + await Task.Delay(500, fx.Ct); + Assert.False(apply.IsCompleted); + Assert.True(Directory.Exists(Path.Combine(fx.Paths.Root, "trips", "day2", "dawn")), "nothing moved while they write"); + } + + await apply; + Assert.SequenceEqual(Expected(plan, before), await StateAsync(fx, ids)); + Assert.Empty(RenameJournal.ListPending(fx.Paths)); + } + [Test] public async Task NestedSequencesMovedTogetherLeaveTheirFolderFirst() {