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>
162 lines
9.4 KiB
C#
162 lines
9.4 KiB
C#
using System.Text.Json;
|
|
using Chronos.Core.Model;
|
|
using Chronos.Data.Copies;
|
|
using Chronos.Data.Repositories;
|
|
|
|
namespace Chronos.Data.Sync;
|
|
|
|
/// <summary>The <c>sync_links</c> table and the sync columns of local sequences (origin, sync state).</summary>
|
|
internal sealed class SyncLinkStore(ChronosDatabase db)
|
|
{
|
|
private const string Columns =
|
|
"server, remote_id, local_id, title, frame_count, total_bytes, render_count, remote_status, remote_settings_version, " +
|
|
"remote_frames_version, remote_updated_at, base_local_settings, base_remote_settings, diff_local_frames, diff_remote_frames, " +
|
|
"diff_local_renders, diff_remote_renders, missing_remote, missing_local, exports_to_get, exports_to_get_bytes, exports_to_send, " +
|
|
"exports_to_send_bytes, conflict_local_settings, conflict_remote_settings, conflict_remote, proxies_cached, proxies_frames_version, error, synced_at, " +
|
|
"diff_remote_updated_at";
|
|
|
|
private static readonly string UpsertSql = SqlDialect.Upsert("sync_links", Columns.Split(", "), 2);
|
|
|
|
private static readonly JobState[] Active = [JobState.Queued, JobState.Running, JobState.Paused];
|
|
|
|
private readonly DbAccess _db = new(db.Database);
|
|
|
|
public Task<IReadOnlyList<SyncLink>> ListAsync(string server, CancellationToken cancellationToken) =>
|
|
_db.ListAsync($"SELECT {Columns} FROM sync_links WHERE server = $1 ORDER BY remote_id", [server], Map, cancellationToken);
|
|
|
|
public Task<SyncLink?> GetAsync(string server, Guid remoteId, CancellationToken cancellationToken) =>
|
|
_db.SingleAsync($"SELECT {Columns} FROM sync_links WHERE server = $1 AND remote_id = $2", [server, remoteId], Map, cancellationToken);
|
|
|
|
public Task<SyncLink?> FindByLocalAsync(string server, Guid localId, CancellationToken cancellationToken) =>
|
|
_db.SingleAsync($"SELECT {Columns} FROM sync_links WHERE server = $1 AND local_id = $2", [server, localId], Map, cancellationToken);
|
|
|
|
public Task UpsertAsync(SyncLink l, CancellationToken cancellationToken) =>
|
|
_db.ExecuteAsync(UpsertSql,
|
|
[
|
|
l.Server, l.RemoteId, l.LocalId, l.Title, l.FrameCount, l.TotalBytes, l.RenderCount, (int)l.RemoteStatus, l.RemoteSettingsVersion,
|
|
l.RemoteFramesVersion, l.RemoteUpdatedAt, l.BaseLocalSettings, l.BaseRemoteSettings, l.DiffLocalFrames, l.DiffRemoteFrames,
|
|
l.DiffLocalRenders, l.DiffRemoteRenders, l.MissingRemote, l.MissingLocal, l.ExportsToGet, l.ExportsToGetBytes, l.ExportsToSend,
|
|
l.ExportsToSendBytes, l.ConflictLocalSettings, l.ConflictRemoteSettings,
|
|
l.ConflictRemote is { } remote ? JsonSerializer.Serialize(remote, SyncPayloadJson.Default.SyncPayload) : null,
|
|
l.ProxiesCached, l.ProxiesFramesVersion, l.Error, l.SyncedAt, l.DiffRemoteUpdatedAt,
|
|
], cancellationToken);
|
|
|
|
public Task DeleteAsync(string server, Guid remoteId, CancellationToken cancellationToken) =>
|
|
_db.ExecuteAsync("DELETE FROM sync_links WHERE server = $1 AND remote_id = $2", [server, remoteId], cancellationToken);
|
|
|
|
/// <summary>
|
|
/// 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. 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)
|
|
{
|
|
ArgumentException.ThrowIfNullOrWhiteSpace(from);
|
|
ArgumentException.ThrowIfNullOrWhiteSpace(to);
|
|
if (string.Equals(from, to, StringComparison.Ordinal)) return (0, 0);
|
|
return await _db.InTransactionAsync(async session =>
|
|
{
|
|
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);
|
|
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);
|
|
foreach (var link in old)
|
|
{
|
|
var clashes = current.Where(c => c.RemoteId == link.RemoteId || (link.LocalId is { } local && c.LocalId == local)).ToList();
|
|
if (clashes.Any(c => Newer(c, link)))
|
|
{
|
|
await inner.ExecuteAsync("DELETE FROM sync_links WHERE server = $1 AND remote_id = $2", [from, link.RemoteId], cancellationToken).ConfigureAwait(false);
|
|
dropped++;
|
|
continue;
|
|
}
|
|
foreach (var clash in clashes)
|
|
{
|
|
await inner.ExecuteAsync("DELETE FROM sync_links WHERE server = $1 AND remote_id = $2", [to, clash.RemoteId], cancellationToken).ConfigureAwait(false);
|
|
current.Remove(clash);
|
|
dropped++;
|
|
}
|
|
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);
|
|
|
|
// The row already under the new key stays on a tie: it is the one the signed-in session works with.
|
|
static bool Newer(SyncLink kept, SyncLink other) => (kept.SyncedAt ?? DateTimeOffset.MinValue) >= (other.SyncedAt ?? DateTimeOffset.MinValue);
|
|
}
|
|
|
|
/// <summary>Records where a local sequence stands for the Library; touches neither its versions nor its update time.</summary>
|
|
public Task SetLocalStateAsync(Guid localId, SyncState state, CancellationToken cancellationToken) =>
|
|
_db.ExecuteAsync("UPDATE sequences SET sync_state = $2 WHERE id = $1 AND sync_state <> $2", [localId, (int)state], cancellationToken);
|
|
|
|
/// <summary>Marks a local sequence as downloaded from the server.</summary>
|
|
public Task SetOriginAsync(Guid localId, SequenceOrigin origin, CancellationToken cancellationToken) =>
|
|
_db.ExecuteAsync("UPDATE sequences SET origin = $2 WHERE id = $1", [localId, (int)origin], cancellationToken);
|
|
|
|
/// <summary>Upload and download jobs that are waiting, running or paused, by target id (local id for uploads, server id for downloads).</summary>
|
|
public async Task<IReadOnlyDictionary<Guid, IReadOnlyList<Job>>> ActiveTransfersAsync(CancellationToken cancellationToken)
|
|
{
|
|
var jobs = new List<Job>();
|
|
foreach (var kind in new[] { JobKind.UploadCommit, JobKind.Download })
|
|
{
|
|
foreach (var state in Active)
|
|
{
|
|
string? cursor = null;
|
|
do
|
|
{
|
|
var page = await db.Jobs.ListAsync(state, kind, 500, cursor, cancellationToken).ConfigureAwait(false);
|
|
jobs.AddRange(page.Items.Where(j => j.TargetIds.Count > 0));
|
|
cursor = page.NextCursor;
|
|
}
|
|
while (cursor is not null);
|
|
}
|
|
}
|
|
return jobs.GroupBy(j => j.TargetIds[0]).ToDictionary(g => g.Key, g => (IReadOnlyList<Job>)[.. g]);
|
|
}
|
|
|
|
private static SyncLink Map(DbRow r) => new()
|
|
{
|
|
Server = r.GetString(0),
|
|
RemoteId = r.GetGuid(1),
|
|
LocalId = r.GetGuidOrNull(2),
|
|
Title = r.GetString(3),
|
|
FrameCount = r.GetInt32(4),
|
|
TotalBytes = r.GetInt64(5),
|
|
RenderCount = r.GetInt32(6),
|
|
RemoteStatus = (SequenceStatus)r.GetInt32(7),
|
|
RemoteSettingsVersion = r.GetInt32(8),
|
|
RemoteFramesVersion = r.GetInt32(9),
|
|
RemoteUpdatedAt = r.GetDateTimeOffset(10),
|
|
BaseLocalSettings = r.GetInt32OrNull(11),
|
|
BaseRemoteSettings = r.GetInt32OrNull(12),
|
|
DiffLocalFrames = r.GetInt32OrNull(13),
|
|
DiffRemoteFrames = r.GetInt32OrNull(14),
|
|
DiffLocalRenders = r.GetInt32OrNull(15),
|
|
DiffRemoteRenders = r.GetInt32OrNull(16),
|
|
MissingRemote = r.GetInt32(17),
|
|
MissingLocal = r.GetInt32(18),
|
|
ExportsToGet = r.GetInt32(19),
|
|
ExportsToGetBytes = r.GetInt64(20),
|
|
ExportsToSend = r.GetInt32(21),
|
|
ExportsToSendBytes = r.GetInt64(22),
|
|
ConflictLocalSettings = r.GetInt32OrNull(23),
|
|
ConflictRemoteSettings = r.GetInt32OrNull(24),
|
|
ConflictRemote = r.GetStringOrNull(25) is { } json ? JsonSerializer.Deserialize(json, SyncPayloadJson.Default.SyncPayload) : null,
|
|
ProxiesCached = r.GetInt32(26),
|
|
ProxiesFramesVersion = r.GetInt32OrNull(27),
|
|
Error = r.GetStringOrNull(28),
|
|
SyncedAt = r.GetDateTimeOffsetOrNull(29),
|
|
DiffRemoteUpdatedAt = r.GetDateTimeOffsetOrNull(30),
|
|
};
|
|
}
|