feat(copies): pure comparison, detection rules, details payload and comparison sides

Detection, Integrate and Merge need one comparison of two copies of a time-lapse, so
this adds it: CopyComparer classifies every photo (same, converted, same unhashed,
deleted here/there, differs, two versions, only here/there) with strict Differs and
Versions rules so whole-second capture times and bursts never pair, unusable frames
held as nothing and marked for repair, and the score of design 3.2.
CopyRules ports 1.1's same-originals gate exactly for "Link when half shared" and
holds the Decide table, ask-again thresholds, fingerprints, hints and the probe
sample. CopyPayload compares details in portable form (same photo at another index is
no line) and merges the chosen sides into one payload that must parse as a project.
CopySides reads both sides from the index and a server and answers the last-copy
check across every archive. SyncTombstones gains FromDtos for local pairs.

Refs #73

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Alby96andClaude Opus 5.5 committed 2026-11-16 19:58:07 +01:00
1 parent 283d04d9cd
commit f6f81e2c37
9 files changed
+1465

No files matched your search

+74
View File
@@ -0,0 +1,74 @@
using Chronos.Core.Model;
using Chronos.Data.Repositories;
using Chronos.Data.Sync;
using Chronos.Protocol;
namespace Chronos.Data.Copies;
/// <summary>
/// The two sides of a comparison as <see cref="CopyComparer"/> reads them (owner review 6, #73; design §4.2): a local
/// sequence's frames and tombstones, a server sequence's manifest with its frames' capture times, and the last-copy check
/// the planners make before anything goes to a trash.
/// </summary>
public sealed class CopySides(ChronosDatabase db)
{
/// <summary>Frames whose file holds nothing usable: the other side's copy repairs them.</summary>
public const FrameFlags UnusableFlags = FrameFlags.Missing | FrameFlags.Corrupt;
/// <summary>Parameters per statement of <see cref="HeldElsewhereAsync"/>.</summary>
private const int Batch = 500;
private readonly ChronosDatabase _db = db ?? throw new ArgumentNullException(nameof(db));
private readonly DbAccess _access = new(db.Database);
/// <summary>A local sequence: every frame row in index order and its tombstones (<see cref="SyncTombstones.ToDto"/>).</summary>
public async Task<CopySideInput> LocalAsync(Guid id, CancellationToken ct)
{
var frames = await Processing.SequenceFrames.ListAsync(_db, id, ct).ConfigureAwait(false);
var removals = await _db.FrameRemovals.ListBySequenceAsync(id, ct).ConfigureAwait(false);
return new CopySideInput(
[.. frames.Select(f => new CopyPhotoInput(f.Id, f.Index, f.FileName, f.Sha256, f.Size, f.CapturedAt, (f.Flags & UnusableFlags) != 0))],
SyncTombstones.ToDto(removals));
}
/// <summary>
/// A server sequence: its manifest (content, flags, tombstones) with the capture times of its frame rows (the manifest
/// has none). A frame the listing no longer has keeps no capture time.
/// </summary>
public async Task<CopySideInput> RemoteAsync(ISyncRemote remote, Guid id, CancellationToken ct)
{
ArgumentNullException.ThrowIfNull(remote);
var manifest = await remote.GetManifestAsync(id, ct).ConfigureAwait(false);
var rows = await remote.ListFramesAsync(id, ct).ConfigureAwait(false);
var captured = new Dictionary<Guid, DateTime?>(rows.Count);
foreach (var row in rows) captured[row.Id] = row.CapturedAt;
return new CopySideInput(
[.. manifest.Frames.Select(f => new CopyPhotoInput(f.Id, f.Index, f.FileName, f.Sha256.Length == 0 ? null : f.Sha256, f.Size,
captured.GetValueOrDefault(f.Id), (f.Flags & (long)UnusableFlags) != 0))],
manifest.Removed ?? []);
}
/// <summary>
/// The raw SHA-256 (lower-case) of <paramref name="sha256"/> that a usable frame (not Missing or Corrupt) of any sequence
/// of this index other than <paramref name="except"/> holds: what is not the last copy of its photo. Batched.
/// </summary>
public async Task<IReadOnlySet<string>> HeldElsewhereAsync(IReadOnlyCollection<string> sha256, IReadOnlyCollection<Guid> except, CancellationToken ct)
{
ArgumentNullException.ThrowIfNull(sha256);
ArgumentNullException.ThrowIfNull(except);
var held = new HashSet<string>(StringComparer.Ordinal);
var wanted = sha256.Where(s => !string.IsNullOrEmpty(s)).Select(s => s.ToLowerInvariant()).Distinct(StringComparer.Ordinal).ToList();
var excluded = except.Distinct().ToList();
foreach (var chunk in wanted.Chunk(Math.Max(1, Batch - excluded.Count)))
{
var p = new SqlParameters();
var not = excluded.Count == 0 ? string.Empty : $" AND sequence_id NOT IN ({string.Join(", ", excluded.Select(id => p.Add(id)))})";
var hashes = string.Join(", ", chunk.Select(sha => p.Add(sha)));
var rows = await _access.ListAsync(
$"SELECT DISTINCT sha256 FROM frames WHERE sha256 IN ({hashes}) AND (flags & {(int)UnusableFlags}) = 0{not}",
p.ToArray(), r => r.GetString(0), ct).ConfigureAwait(false);
foreach (var sha in rows) held.Add(sha.ToLowerInvariant());
}
return held;
}
}