Files
Chronos/src/Titano.Core/Project/FramePhotos.cs
T
Claude 991742462b fix(sync): let a photo's hash decide which frames hold it
Telling copies of a photo apart counted a frame not hashed yet with the
same size and capture time as a hashed photo's copy, although it may be
another photo (a RAW of constant size in a burst without sub-second
times). Positions then shifted between copies: an exclusion waited forever
as a pending frame, or landed on the other photo. A hashed photo is now the
frames holding its hash; size and capture time count only when no frame
holds it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_012iLMBNwBE2GnphAsu4cjaC
2026-10-12 19:59:54 +00:00

117 lines
5.6 KiB
C#

namespace Titano.Core.Project;
/// <summary>
/// A photo as every copy of a sequence knows it (BRIEF §14.5: originals are content-addressed): its SHA-256 once hashed,
/// else its size and capture time (a folder adopted and not verified yet). Frame ids and positions differ between a
/// server sequence and a PC's copy of it; the photo does not.
/// </summary>
/// <param name="Sha256">Hex SHA-256; null or empty while the copy has not hashed the file.</param>
public sealed record FramePhoto(string? Sha256, long Size, DateTime? CapturedAt)
{
public bool IsHashed => !string.IsNullOrEmpty(Sha256);
/// <summary>
/// The same photo: the same SHA-256 when both are hashed, otherwise the same size and capture time (to the
/// millisecond, as databases keep it).
/// </summary>
public bool Matches(FramePhoto other)
{
ArgumentNullException.ThrowIfNull(other);
if (IsHashed && other.IsHashed) return string.Equals(Sha256, other.Sha256, StringComparison.OrdinalIgnoreCase);
return Size == other.Size && CapturedAt is { } mine && other.CapturedAt is { } theirs && Millisecond(mine) == Millisecond(theirs);
}
internal static long Millisecond(DateTime time) => time.Ticks / TimeSpan.TicksPerMillisecond;
}
/// <summary>
/// The photos of one copy's frames by frame index (positions in capture order, BRIEF §5), and where a photo is among
/// them. A copy may hold one photo more than once (a folder adopted with 'IMG_0001.JPG' and 'IMG_0001 (1).JPG'); those
/// frames are told apart by their order (<see cref="Occurrence"/>). Immutable once built, so parallel workers may share it.
/// </summary>
public sealed class FramePhotos
{
private readonly FramePhoto?[] _byIndex;
private readonly Dictionary<string, List<int>> _bySha = new(StringComparer.Ordinal);
private readonly Dictionary<(long Size, long Time), List<int>> _bySizeAndTime = [];
/// <param name="frames">Each frame's index and photo, in any order.</param>
public FramePhotos(IEnumerable<(int Index, FramePhoto Photo)> frames)
{
ArgumentNullException.ThrowIfNull(frames);
var list = frames.Where(f => f.Index >= 0).ToList();
_byIndex = new FramePhoto?[list.Count == 0 ? 0 : list.Max(f => f.Index) + 1];
foreach (var (index, photo) in list) _byIndex[index] = photo;
for (var i = 0; i < _byIndex.Length; i++)
{
if (_byIndex[i] is not { } photo) continue;
if (photo.IsHashed)
{
var sha = photo.Sha256!.ToLowerInvariant();
if (!_bySha.TryGetValue(sha, out var copies)) _bySha[sha] = copies = [];
copies.Add(i);
}
if (photo.CapturedAt is not { } time) continue;
var key = (photo.Size, FramePhoto.Millisecond(time));
if (!_bySizeAndTime.TryGetValue(key, out var same)) _bySizeAndTime[key] = same = [];
same.Add(i);
}
}
public int Count => _byIndex.Length;
/// <summary>The photo at <paramref name="index"/>; null when no frame has that index.</summary>
public FramePhoto? this[int index] => (uint)index < (uint)_byIndex.Length ? _byIndex[index] : null;
/// <summary>Some frame is not hashed yet: matching it needs capture times.</summary>
public bool HasUnhashed => _byIndex.Any(p => p is { IsHashed: false });
/// <summary>
/// The index of the first frame holding <paramref name="photo"/> (<see cref="FramePhoto.Matches"/>), or with
/// <paramref name="occurrence"/> n of the (n+1)-th in index order; null when fewer frames hold it.
/// </summary>
public int? IndexOf(FramePhoto photo, int occurrence = 0)
{
ArgumentNullException.ThrowIfNull(photo);
ArgumentOutOfRangeException.ThrowIfNegative(occurrence);
var holding = Holding(photo);
return occurrence < holding.Count ? holding[occurrence] : null;
}
/// <summary>
/// Which of the frames holding the photo at <paramref name="index"/> it is, in index order: 0 for the first (and for
/// a photo held once), 1 for the second, … A position translated to another copy names the same one there.
/// </summary>
public int Occurrence(int index) => this[index] is { } photo ? Math.Max(Holding(photo).BinarySearch(index), 0) : 0;
/// <summary>
/// The indices of the frames holding <paramref name="photo"/>, ascending. A hashed photo is the frames with its SHA-256
/// when there are any: a frame not hashed yet with the same size and capture time may be another photo (a RAW of
/// constant size in a burst without sub-second times), so it counts only when no frame has the hash.
/// </summary>
private List<int> Holding(FramePhoto photo)
{
if (photo.IsHashed && _bySha.TryGetValue(photo.Sha256!.ToLowerInvariant(), out var hashed)) return hashed;
var holding = new List<int>(1);
if (photo.CapturedAt is not { } time || !_bySizeAndTime.TryGetValue((photo.Size, FramePhoto.Millisecond(time)), out var candidates)) return holding;
foreach (var index in candidates)
{
if (_byIndex[index]!.Matches(photo)) holding.Add(index);
}
return holding;
}
/// <summary>Both lists hold the same photos at the same indices: a position names the same photo on both.</summary>
public bool SameAs(FramePhotos other)
{
ArgumentNullException.ThrowIfNull(other);
if (other.Count != Count) return false;
for (var i = 0; i < Count; i++)
{
var (mine, theirs) = (this[i], other[i]);
if (mine is null != theirs is null || (mine is not null && !mine.Matches(theirs!))) return false;
}
return true;
}
}