A merge could still trash photos it never verified, resume against a pair the user had answered meanwhile, and take different unhashed photos for copies; the dialog and the lists worded several facts wrongly or lost them on refresh. - Merge: a retry resumes only while the pair is still the job's; Verify requires the other archive online and its folder in place, stops on an unreadable photo, files a differing unhashed photo as a second version and must cover every frame before Trash; the plan hashes unmatched unhashed photos first, lists "Deleted in the other" and names the relinked servers; second versions stay discoverable (CopySides.VersionsAsync). - Integrate: a retry after a failure before the journal runs again. - Sync: a sequence back after a 404 stays Linked while its link holds; a local cull no longer asks for attention. - Block lines of both planners name the right sequence, archive, server and space through one formatter. - Compare dialog: one Details row per setting with worded values, Projects headings, Choose focuses the kept-sequence radios, the photo selection reaches rows outside the viewport, the Replace confirmation lists the losers per side, plan lines read as queued. - Matches and Library: counts first and grouped, integrating shown as a share, local matches name the other archive, the Library keeps its match and "deleted on server" statuses across a refresh. Refs #73 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
155 lines
6.5 KiB
C#
155 lines
6.5 KiB
C#
using System.Text.Json;
|
|
using System.Text.Json.Serialization;
|
|
using Chronos.Core.Archiving;
|
|
using Chronos.Core.IO;
|
|
using Chronos.Data.Copies;
|
|
using Chronos.Data.Sync;
|
|
|
|
namespace Chronos.Data.Archiving;
|
|
|
|
/// <summary>The stages of a merge, in the order they run (design §5d).</summary>
|
|
internal enum MergeStage
|
|
{
|
|
Copy,
|
|
Renders,
|
|
Verify,
|
|
Replace,
|
|
Details,
|
|
Projects,
|
|
Links,
|
|
Trash,
|
|
Done,
|
|
}
|
|
|
|
/// <summary>A photo of the dropped sequence a merge copies into the kept one: its key, the drop's frame, name, size and recorded SHA-256.</summary>
|
|
internal sealed record MergeCopy(string Key, PhotoAction Action, Guid DropFrameId, string FileName, long Bytes, string? Sha256);
|
|
|
|
/// <summary>A losing version of the kept sequence that goes to its photo trash once the winner (the drop's version) is in place.</summary>
|
|
internal sealed record MergeReplace(string Key, Guid KeepFrameId, string FileName, string WinnerSha256);
|
|
|
|
/// <summary>A drop original the verify found in the kept sequence: its SHA-256 now and the keep frame that holds it.</summary>
|
|
internal sealed record MergeVerified(string Sha256, Guid KeepFrameId);
|
|
|
|
/// <summary>
|
|
/// The journal of a merge (owner review 6, #73; design §5d): <c><keep archive>/.chronos/journal/merge-<job:N>.json</c>,
|
|
/// written atomically (temp + rename) after each step. It keeps what the confirmed plan decided (a resumed run never plans
|
|
/// again: its own steps moved the versions) and how far each stage came, so every stage resumes and nothing is done twice.
|
|
/// </summary>
|
|
internal sealed class MergeJournal
|
|
{
|
|
public Guid JobId { get; set; }
|
|
|
|
public Guid CopyId { get; set; }
|
|
|
|
public Guid KeepId { get; set; }
|
|
|
|
public Guid DropId { get; set; }
|
|
|
|
public string Fingerprint { get; set; } = string.Empty;
|
|
|
|
public MergeStage Stage { get; set; }
|
|
|
|
/// <summary>The kept sequence's settings version the plan read (the details are written only at it).</summary>
|
|
public int KeepSettingsVersion { get; set; }
|
|
|
|
/// <summary>The dropped sequence's frames version the plan read (checked before each photo and at the verify).</summary>
|
|
public int DropFramesVersion { get; set; }
|
|
|
|
/// <summary>The kept sequence's statuses before the merge (what its follow-ups make again).</summary>
|
|
public int KeepStatus { get; set; }
|
|
|
|
public List<MergeCopy> Copies { get; set; } = [];
|
|
|
|
public List<MergeReplace> Replaces { get; set; } = [];
|
|
|
|
/// <summary>Raw SHA-256 of drop originals the plan leaves out of the kept sequence (last copies, losers, tombstoned in keep, converted).</summary>
|
|
public List<string> Absent { get; set; } = [];
|
|
|
|
public List<RenderStep> Renders { get; set; } = [];
|
|
|
|
public SyncPayload? Payload { get; set; }
|
|
|
|
public List<ProjectStep> Projects { get; set; } = [];
|
|
|
|
public List<string> SessionKeys { get; set; } = [];
|
|
|
|
/// <summary>Drop frame → the keep frame it was filed as (or that already held it).</summary>
|
|
public Dictionary<Guid, Guid> Committed { get; set; } = [];
|
|
|
|
/// <summary>Render → its archive-relative path in the kept sequence, written before the copy.</summary>
|
|
public Dictionary<Guid, string> RenderTargets { get; set; } = [];
|
|
|
|
public List<Guid> RendersMoved { get; set; } = [];
|
|
|
|
/// <summary>Drop frame → its SHA-256 as hashed by the verify and the keep frame that holds it.</summary>
|
|
public Dictionary<Guid, MergeVerified> Verified { get; set; } = [];
|
|
|
|
/// <summary>Drop frames the verify found to be another photo than keep's (unhashed, same size and time) and filed as a second version.</summary>
|
|
public List<Guid> SecondVersions { get; set; } = [];
|
|
|
|
/// <summary>Drop frames the verify found nothing of to lose: last copies the plan listed, or photos a third sequence holds.</summary>
|
|
public List<Guid> Settled { get; set; } = [];
|
|
|
|
/// <summary>The dropped sequence's frames version the verify read: the trash refuses another.</summary>
|
|
public int? VerifiedDropFramesVersion { get; set; }
|
|
|
|
public List<string> Replaced { get; set; } = [];
|
|
|
|
/// <summary>Keys of losing versions a refused trash step kept (reported at the end).</summary>
|
|
public List<string> KeptLosers { get; set; } = [];
|
|
|
|
public bool DetailsDone { get; set; }
|
|
|
|
public Dictionary<Guid, TransplantStep> ProjectSteps { get; set; } = [];
|
|
|
|
public List<Guid> ProjectsDone { get; set; } = [];
|
|
|
|
public bool KeepKeysWritten { get; set; }
|
|
|
|
public bool DropKeysRemoved { get; set; }
|
|
|
|
public bool LinksMoved { get; set; }
|
|
|
|
/// <summary>The journal of job <paramref name="jobId"/> in the archive at <paramref name="paths"/>.</summary>
|
|
public static string PathFor(ArchivePaths paths, Guid jobId) => Path.Combine(paths.JournalDirectory, $"merge-{jobId:N}.json");
|
|
|
|
/// <summary>Where the job stages the files it copies.</summary>
|
|
public static string StagingFor(ArchivePaths paths, Guid jobId) => Path.Combine(paths.StagingDirectory, $"merge-{jobId:N}");
|
|
|
|
/// <summary>The journal of job <paramref name="jobId"/>; null when there is none (a first run), or it cannot be read.</summary>
|
|
/// <exception cref="InvalidDataException">The journal is damaged: the merge stops rather than start again over half a merge.</exception>
|
|
public static MergeJournal? Read(ArchivePaths paths, Guid jobId)
|
|
{
|
|
var path = PathFor(paths, jobId);
|
|
if (!File.Exists(path)) return null;
|
|
try
|
|
{
|
|
return JsonSerializer.Deserialize(AtomicFile.ReadAllText(path), MergeJournalJson.Default.MergeJournal);
|
|
}
|
|
catch (JsonException ex)
|
|
{
|
|
throw new InvalidDataException($"The merge journal {path} is damaged.", ex);
|
|
}
|
|
}
|
|
|
|
/// <summary>Writes the journal atomically.</summary>
|
|
public void Save(ArchivePaths paths)
|
|
{
|
|
Directory.CreateDirectory(paths.JournalDirectory);
|
|
AtomicFile.WriteAllText(PathFor(paths, JobId), JsonSerializer.Serialize(this, MergeJournalJson.Default.MergeJournal));
|
|
}
|
|
|
|
/// <summary>Deletes the journal and the staging folder of job <paramref name="jobId"/>.</summary>
|
|
public static void Delete(ArchivePaths paths, Guid jobId)
|
|
{
|
|
var staging = StagingFor(paths, jobId);
|
|
if (Directory.Exists(staging)) Directory.Delete(staging, recursive: true);
|
|
File.Delete(PathFor(paths, jobId));
|
|
}
|
|
}
|
|
|
|
[JsonSourceGenerationOptions(PropertyNamingPolicy = JsonKnownNamingPolicy.CamelCase, UseStringEnumConverter = true,
|
|
DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull)]
|
|
[JsonSerializable(typeof(MergeJournal))]
|
|
internal sealed partial class MergeJournalJson : JsonSerializerContext;
|