fix(sync): keep the copies of a photo apart when positions cross copies
A server folder adopted with the same photo twice ('IMG_0001.JPG' and
'IMG_0001 (1).JPG') holds two frames where the PC keeps one. Translation
by photo sent every position of that photo to its first copy, so the
server's exclusion of the duplicate excluded the PC's only copy, a push
gave the server back one exclusion instead of two, and keyframes of both
copies piled on one frame.
FramePhotos now lists every frame holding a photo and tells them apart by
their order (Occurrence). A position on the n-th copy goes to the n-th
copy of the receiving copy, or waits as a pending frame with its
occurrence when that copy holds fewer; occurrence 0 is not written, so
older projects read the same.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_012iLMBNwBE2GnphAsu4cjaC
This commit is contained in:
3 files changed
+128
-24
No files matched your search
@@ -26,12 +26,13 @@ public sealed record FramePhoto(string? Sha256, long Size, DateTime? CapturedAt)
|
||||
|
||||
/// <summary>
|
||||
/// The photos of one copy's frames by frame index (positions in capture order, BRIEF §5), and where a photo is among
|
||||
/// them. Immutable once built, so parallel workers may share it.
|
||||
/// 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, int> _bySha = new(StringComparer.Ordinal);
|
||||
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>
|
||||
@@ -44,7 +45,12 @@ public sealed class FramePhotos
|
||||
for (var i = 0; i < _byIndex.Length; i++)
|
||||
{
|
||||
if (_byIndex[i] is not { } photo) continue;
|
||||
if (photo.IsHashed) _bySha.TryAdd(photo.Sha256!.ToLowerInvariant(), i);
|
||||
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 = [];
|
||||
@@ -60,17 +66,38 @@ public sealed class FramePhotos
|
||||
/// <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"/>); null when none does.</summary>
|
||||
public int? IndexOf(FramePhoto photo)
|
||||
/// <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);
|
||||
if (photo.IsHashed && _bySha.TryGetValue(photo.Sha256!.ToLowerInvariant(), out var hashed)) return hashed;
|
||||
if (photo.CapturedAt is not { } time || !_bySizeAndTime.TryGetValue((photo.Size, FramePhoto.Millisecond(time)), out var candidates)) return null;
|
||||
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.</summary>
|
||||
private List<int> Holding(FramePhoto photo)
|
||||
{
|
||||
var holding = new List<int>(1);
|
||||
if (photo.IsHashed && _bySha.TryGetValue(photo.Sha256!.ToLowerInvariant(), out var hashed)) holding.AddRange(hashed);
|
||||
if (photo.CapturedAt is not { } time || !_bySizeAndTime.TryGetValue((photo.Size, FramePhoto.Millisecond(time)), out var candidates)) return holding;
|
||||
// Frames not hashed on one side match by size and capture time; a hashed photo's hashed frames are listed already.
|
||||
foreach (var index in candidates)
|
||||
{
|
||||
if (_byIndex[index]!.Matches(photo)) return index;
|
||||
var frame = _byIndex[index]!;
|
||||
if (!(photo.IsHashed && frame.IsHashed) && frame.Matches(photo)) holding.Add(index);
|
||||
}
|
||||
return null;
|
||||
if (holding.Count > 1) holding.Sort();
|
||||
return holding;
|
||||
}
|
||||
|
||||
/// <summary>Both lists hold the same photos at the same indices: a position names the same photo on both.</summary>
|
||||
|
||||
Reference in new issue
Block a user