using Chronos.Data.Sync; namespace Chronos.Data.Copies; /// Why Upload all local left a sequence out (owner review 6, Q8). public enum UploadSkip { /// In a match nobody answered yet: it may be on the server already. SuspectedCopy, /// The user kept it apart from a match. KeptApart, /// It lies in a server's originals archive: it came from a server. ServerOriginals, /// The server deleted its copy (a 404 for the linked sequence). DeletedOnServer, /// An Integrate or Merge job holds it, or will. Busy, } /// /// Which local-only sequences Upload all local sends (design ยง6.4, owner review 6, Q8): suspected copies, kept-apart ones, /// those the server deleted, those an Integrate or Merge holds and those in any server's originals archive are skipped and /// flagged, never uploaded as new server sequences. Pure. /// public static class UploadAllRule { /// /// Splits into the entries to upload and those skipped with the reason. /// are every pair of the index (any server, and local pairs); /// every known server's originals archive (the current one's too); the targets of pending or /// running Integrate and MergeSequences jobs. An entry without a local sequence is neither. /// public static (IReadOnlyList Upload, IReadOnlyList<(SyncEntry Entry, UploadSkip Why)> Skipped) Select( IEnumerable localOnly, IReadOnlyCollection copies, IReadOnlySet serverOriginalsArchives, IReadOnlySet busy) { ArgumentNullException.ThrowIfNull(localOnly); ArgumentNullException.ThrowIfNull(copies); ArgumentNullException.ThrowIfNull(serverOriginalsArchives); ArgumentNullException.ThrowIfNull(busy); var bySequence = new Dictionary>(); foreach (var copy in copies) { Add(copy.LocalId, copy); if (copy.OtherId is { } other) Add(other, copy); } var upload = new List(); var skipped = new List<(SyncEntry, UploadSkip)>(); foreach (var entry in localOnly) { if (entry.Local is not { } local) continue; // A Kept apart answer given before a server sequence was confirmed deleted there (two 404s apart) holds nothing // back: that sequence is no copy of anything any more, and the local one is this PC's only. A Keep here, answered // once the deletion was shown, is the user's answer about the deletion itself: it still skips. var pairs = (bySequence.GetValueOrDefault(local.Id) ?? []).Where(p => !ApartBeforeDeletion(p)).ToList(); UploadSkip? why = busy.Contains(local.Id) || pairs.Any(p => p.State == CopyState.Integrating) ? UploadSkip.Busy : pairs.Any(p => p.Kind == CopyKind.LocalServer && p.RemoteGoneHits >= 1) ? UploadSkip.DeletedOnServer : pairs.Any(p => p.State == CopyState.KeptApart) ? UploadSkip.KeptApart : pairs.Any(p => p.State == CopyState.Candidate) ? UploadSkip.SuspectedCopy : serverOriginalsArchives.Contains(local.ArchiveId) ? UploadSkip.ServerOriginals : null; if (why is { } reason) skipped.Add((entry, reason)); else upload.Add(entry); } return (upload, skipped); static bool ApartBeforeDeletion(FoundCopy p) => p is { Kind: CopyKind.LocalServer, State: CopyState.KeptApart, RemoteGone: true, AnsweredAt: { } answered, RemoteGoneAt: { } gone } && answered < gone; void Add(Guid id, FoundCopy copy) { if (!bySequence.TryGetValue(id, out var list)) bySequence[id] = list = []; list.Add(copy); } } }