feat: found copies foundation (contracts, migration 0012, store, wire, trash and job hooks, keys)
Owner review 6 asks Chronos to recognise the same time-lapse held under different ids on the server and on this PC, compare the copies, and keep them apart or integrate and sync them. This lays the shared foundation the later parcels build on, without any UI or detection yet. - Migration 0012 adds found_copies (local-server and local-local pairs with evidence, scores, answer, policy and the 404 record) and copy_probes; every existing link with a local sequence becomes a Linked pair, so a server deletion and the per-link policy have a place. - FoundCopyStore keeps the pairs: upserts that never touch the answer, a compare-and-set of the state with an Integrating backstop (a failed or cancelled job reads as the previous state), link promotion and demotion, the once-per-refresh 404 count, the merge's links stage (session keys, links reset, pairs moved, refused when both are linked on one server), the local overlap queries and the probes. SyncLinkStore.RekeyAsync moves pairs and probes with the links in the same transaction. - Contracts for detection, review, plans, the Integrate and MergeSequences jobs (kinds 25/26, High level, new detail steps) and the Upload all rule that keeps suspected, kept-apart, deleted, busy and server-born sequences here. - Wire: POST photos/lookup, uploads into a target (IntoTarget/Copied, trailing and optional), frames/delete on the client, capabilities photo-lookup and upload-into, cached per remote. - The trash plans and deletes accept the job that holds the sequence (its own hold is not Busy) and a frames version checked under the lease; a Background job never interrupts a running one. - Settings for linking, the photo policy and projects; the en and it keys of design section 9. Refs #73 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
43719bb5c5
commit
283d04d9cd
57 files changed
+3302
-26
No files matched your search
@@ -0,0 +1,43 @@
|
||||
using Chronos.Data.Copies;
|
||||
using Chronos.Protocol;
|
||||
|
||||
namespace Chronos.Data.Sync;
|
||||
|
||||
/// <summary>Where a photo of a pair stands (design §4.1); each photo lands in exactly one class.</summary>
|
||||
public enum PhotoClass
|
||||
{
|
||||
Same,
|
||||
SameUnhashed,
|
||||
OnlyHere,
|
||||
OnlyThere,
|
||||
Differs,
|
||||
Converted,
|
||||
DeletedHere,
|
||||
DeletedThere,
|
||||
Versions,
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// One side's frame as the comparison reads it (local frames, or the server's manifest and frames). Unusable: the frame is
|
||||
/// flagged Missing or Corrupt (it holds nothing; the other side's copy repairs it).
|
||||
/// </summary>
|
||||
public sealed record CopyPhotoInput(Guid FrameId, int Index, string FileName, string? Sha256, long Size, DateTime? CapturedAt, bool Unusable);
|
||||
|
||||
/// <summary>One side of a comparison: its frames and its tombstones.</summary>
|
||||
public sealed record CopySideInput(IReadOnlyList<CopyPhotoInput> Photos, IReadOnlyList<RemovedPhotoDto> Removed);
|
||||
|
||||
/// <summary>
|
||||
/// A compared photo. Key: canonical SHA-256, or "u:<size>:<capture ms ticks>" while unhashed. Occurrences count
|
||||
/// the copies of the photo inside each side; Bytes is its size on the side that holds it (Differs: here's).
|
||||
/// </summary>
|
||||
public sealed record ComparedPhoto(PhotoClass Class, string Key, CopyPhotoInput? Here, CopyPhotoInput? There,
|
||||
int HereOccurrences, int ThereOccurrences, long Bytes, bool RepairsHere = false, bool RepairsThere = false);
|
||||
|
||||
/// <summary>The count and bytes of one class on each side.</summary>
|
||||
public sealed record PhotoClassTotal(PhotoClass Class, int Count, long HereBytes, long ThereBytes);
|
||||
|
||||
/// <summary>The outcome of comparing two sides photo by photo.</summary>
|
||||
public sealed record CopyComparisonResult(IReadOnlyList<ComparedPhoto> Photos, IReadOnlyList<PhotoClassTotal> Totals, CopyScore Score)
|
||||
{
|
||||
public IEnumerable<ComparedPhoto> Of(PhotoClass c) => Photos.Where(p => p.Class == c);
|
||||
}
|
||||
@@ -1,5 +1,6 @@
|
||||
using System.Text.Json;
|
||||
using Chronos.Core.Model;
|
||||
using Chronos.Data.Copies;
|
||||
using Chronos.Data.Repositories;
|
||||
|
||||
namespace Chronos.Data.Sync;
|
||||
@@ -47,7 +48,8 @@ internal sealed class SyncLinkStore(ChronosDatabase db)
|
||||
/// Moves every link of <paramref name="from"/> to <paramref name="to"/> in one transaction (owner review 5, #66: an address
|
||||
/// key becomes the server's id key). Where <paramref name="to"/> already links the same server sequence, or the same local
|
||||
/// copy, the row compared last (<see cref="SyncLink.SyncedAt"/>, never compared counting oldest) is kept and the other
|
||||
/// deleted: link rows are recomputed by the next comparison, so nothing is lost.
|
||||
/// deleted: link rows are recomputed by the next comparison, so nothing is lost. The server's found copies and probes move
|
||||
/// in the same transaction (<see cref="FoundCopyStore.RekeyAsync"/>), the pair of the surviving link winning a clash.
|
||||
/// </summary>
|
||||
public async Task<(int Moved, int Dropped)> RekeyAsync(string from, string to, CancellationToken cancellationToken)
|
||||
{
|
||||
@@ -59,7 +61,7 @@ internal sealed class SyncLinkStore(ChronosDatabase db)
|
||||
var inner = new DbAccess(session);
|
||||
var old = await inner.ListAsync($"SELECT {Columns} FROM sync_links WHERE server = $1 ORDER BY remote_id", [from], Map, cancellationToken)
|
||||
.ConfigureAwait(false);
|
||||
if (old.Count == 0) return (0, 0);
|
||||
var surviving = new HashSet<Guid>();
|
||||
var current = (await inner.ListAsync($"SELECT {Columns} FROM sync_links WHERE server = $1 ORDER BY remote_id", [to], Map, cancellationToken)
|
||||
.ConfigureAwait(false)).ToList();
|
||||
var (moved, dropped) = (0, 0);
|
||||
@@ -81,8 +83,11 @@ internal sealed class SyncLinkStore(ChronosDatabase db)
|
||||
await inner.ExecuteAsync("UPDATE sync_links SET server = $3 WHERE server = $1 AND remote_id = $2", [from, link.RemoteId, to], cancellationToken)
|
||||
.ConfigureAwait(false);
|
||||
current.Add(link with { Server = to });
|
||||
surviving.Add(link.RemoteId);
|
||||
moved++;
|
||||
}
|
||||
// The pairs and probes of the server move with its links, in this transaction (owner review 6, #73).
|
||||
await new FoundCopyStore(db).RekeyAsync(session, from, to, surviving, cancellationToken).ConfigureAwait(false);
|
||||
return (moved, dropped);
|
||||
}, cancellationToken).ConfigureAwait(false);
|
||||
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
using Chronos.Core.Model;
|
||||
using Chronos.Data.Copies;
|
||||
|
||||
namespace Chronos.Data.Sync;
|
||||
|
||||
@@ -172,7 +173,9 @@ public sealed record SyncSnapshot(string Server, IReadOnlyList<SyncEntry> Entrie
|
||||
/// <param name="AutoSyncSettings">Send and take settings changed on one side only (Server.html "Sync projects automatically").</param>
|
||||
/// <param name="KeepProxiesOffline">Keep the proxy caches of server sequences this PC opened with proxies up to date.</param>
|
||||
/// <param name="CanEdit">The account may change server sequences; without it this PC's edits are never sent.</param>
|
||||
public sealed record SyncPolicy(bool AutoSyncSettings = true, bool KeepProxiesOffline = false, bool CanEdit = true);
|
||||
/// <param name="Linking">What a refresh does with a match it finds (Settings › Server › Link matches, owner review 6, Q1).</param>
|
||||
public sealed record SyncPolicy(bool AutoSyncSettings = true, bool KeepProxiesOffline = false, bool CanEdit = true,
|
||||
CopyLinking Linking = CopyLinking.AskAlways);
|
||||
|
||||
/// <summary>
|
||||
/// A conflict could not be settled as the user chose because one side changed again after it was shown; the
|
||||
|
||||
@@ -92,6 +92,36 @@ public interface ISyncRemote : IDisposable
|
||||
/// then fail every render of them. A remote that cannot say reads this Chronos's.
|
||||
/// </summary>
|
||||
Task<int> SettingsFormatAsync(CancellationToken cancellationToken) => Task.FromResult(Core.Project.ProjectSettings.CurrentVersion);
|
||||
|
||||
/// <summary>
|
||||
/// The capabilities the server announces (<see cref="ServerInfo.Capabilities"/>, names of <see cref="ServerCapabilities"/>).
|
||||
/// A remote that cannot say announces none: callers take the path of an older server.
|
||||
/// </summary>
|
||||
Task<IReadOnlySet<string>> CapabilitiesAsync(CancellationToken cancellationToken) =>
|
||||
Task.FromResult<IReadOnlySet<string>>(new HashSet<string>(StringComparer.Ordinal));
|
||||
|
||||
/// <summary>
|
||||
/// Which server sequences hold sampled photos of local sequences (owner review 6, #73; <see cref="ServerCapabilities.PhotoLookup"/>).
|
||||
/// See <see cref="ChronosClient.LookupPhotosAsync"/>. A remote without the call throws <see cref="NotSupportedException"/>.
|
||||
/// </summary>
|
||||
Task<PhotoLookupResponse> LookupPhotosAsync(PhotoLookupRequest request, CancellationToken cancellationToken) => throw new NotSupportedException();
|
||||
|
||||
/// <summary>
|
||||
/// Upload of one original into <paramref name="sequenceId"/> that the server files there even when another of its
|
||||
/// sequences holds the photo (<see cref="StartUploadRequest.IntoTarget"/>, owner review 6; <see cref="ServerCapabilities.UploadInto"/>).
|
||||
/// Null when the target deleted the photo. A remote without the call throws <see cref="NotSupportedException"/>.
|
||||
/// </summary>
|
||||
Task<CompleteUploadResponse?> UploadFileIntoAsync(string path, Guid sequenceId, string sha256, DateTime? capturedAt,
|
||||
CancellationToken cancellationToken) => throw new NotSupportedException();
|
||||
|
||||
/// <summary>
|
||||
/// Moves frames of a server sequence into the server's trash, or with <see cref="FrameDeleteRequest.DryRun"/> answers the
|
||||
/// plan only (owner review 6, #73). A 409 <see cref="ChronosApiException"/> means the frames moved since
|
||||
/// <see cref="FrameDeleteRequest.ExpectedFramesVersion"/>, 403 that the account may not. A remote without the call throws
|
||||
/// <see cref="NotSupportedException"/>.
|
||||
/// </summary>
|
||||
Task<FrameDeleteResultDto> DeleteFramesAsync(Guid sequenceId, FrameDeleteRequest request, CancellationToken cancellationToken) =>
|
||||
throw new NotSupportedException();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -141,6 +171,34 @@ public sealed class ChronosSyncRemote(ChronosClient client, bool ownsClient, str
|
||||
/// <summary>The settings format the server announced, and when it was read (a <see cref="System.Diagnostics.Stopwatch"/> timestamp).</summary>
|
||||
private sealed record KnownFormat(int Format, long Read);
|
||||
|
||||
private Task<IReadOnlySet<string>>? _capabilities;
|
||||
|
||||
/// <summary>Read from <c>server/info</c> once per remote (a remote lives for one session; an update of the server ends it).</summary>
|
||||
public Task<IReadOnlySet<string>> CapabilitiesAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
var known = Volatile.Read(ref _capabilities);
|
||||
if (known is { IsFaulted: false, IsCanceled: false }) return known;
|
||||
var read = ReadCapabilitiesAsync(cancellationToken);
|
||||
Volatile.Write(ref _capabilities, read);
|
||||
return read;
|
||||
}
|
||||
|
||||
private async Task<IReadOnlySet<string>> ReadCapabilitiesAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
var info = await client.GetInfoAsync(cancellationToken).ConfigureAwait(false);
|
||||
return new HashSet<string>(info.Capabilities ?? [], StringComparer.Ordinal);
|
||||
}
|
||||
|
||||
public Task<PhotoLookupResponse> LookupPhotosAsync(PhotoLookupRequest request, CancellationToken cancellationToken) =>
|
||||
client.LookupPhotosAsync(request, cancellationToken);
|
||||
|
||||
public Task<CompleteUploadResponse?> UploadFileIntoAsync(string path, Guid sequenceId, string sha256, DateTime? capturedAt,
|
||||
CancellationToken cancellationToken) =>
|
||||
client.UploadFileIntoAsync(path, sequenceId, capturedAt, sha256, null, cancellationToken);
|
||||
|
||||
public Task<FrameDeleteResultDto> DeleteFramesAsync(Guid sequenceId, FrameDeleteRequest request, CancellationToken cancellationToken) =>
|
||||
client.DeleteFramesAsync(sequenceId, request, cancellationToken);
|
||||
|
||||
public string Server { get; } = string.IsNullOrWhiteSpace(server) ? SyncService.ServerKey(client.BaseAddress) : server;
|
||||
|
||||
public async Task<IReadOnlyList<SequenceDto>> ListSequencesAsync(CancellationToken cancellationToken)
|
||||
|
||||
Reference in new issue
Block a user