Merge branch 'review2-p5a-fix3-projects' into review2-p5a-int3
This commit is contained in:
commit
87e2640907
5 files changed
+398
-130
No files matched your search
@@ -24,7 +24,8 @@ internal sealed record ProjectReconcile(bool Missing, IReadOnlySet<Guid> Seen, I
|
||||
/// behind on moves forward, a name or deletion recorded since is taken, and a row the index marked deleted is listed again
|
||||
/// when its live record says it is not. Only a record that says so, live or in the projects' trash, deletes a project:
|
||||
/// never a folder that is not there (moved for a moment, a backup put back, a sync tool) nor a record that cannot be read,
|
||||
/// whose project stays as the index has it (<see cref="ProjectReconcile.HeldBack"/>). A version file written past its
|
||||
/// whose project stays as the index has it (<see cref="ProjectReconcile.HeldBack"/>); a record missing from the folder of a
|
||||
/// live project the index holds is written again from the index when the archive takes writes. A version file written past its
|
||||
/// record's head by a push a crash cut short is adopted when it follows the head (its file is complete: versions are
|
||||
/// written atomically, then the record); any other file past the head, or a second file of a number the record's chain
|
||||
/// names another for, is moved aside to <c>versions/.stray</c>, never deleted. Runs under the sequence's lease, or without
|
||||
@@ -34,6 +35,9 @@ internal sealed class ProjectIndexer(ChronosDatabase db)
|
||||
{
|
||||
private const string StrayFolder = ".stray";
|
||||
|
||||
/// <summary>Versions read at once when a project's sidecars are written again from the index.</summary>
|
||||
private const int WriteBackPage = 500;
|
||||
|
||||
/// <summary>Reconciles the projects of <paramref name="sequenceId"/> in <paramref name="folder"/>; the caller holds the sequence's lease.</summary>
|
||||
/// <param name="writable">False for a read-only archive: the sidecars are read as they are and only the index is written
|
||||
/// (a version past its record's head is indexed, not adopted into the record; nothing is moved aside).</param>
|
||||
@@ -46,29 +50,39 @@ internal sealed class ProjectIndexer(ChronosDatabase db)
|
||||
foreach (var (directory, trashed) in ProjectFolders(folder))
|
||||
{
|
||||
cancellationToken.ThrowIfCancellationRequested();
|
||||
ProjectRecord record;
|
||||
ProjectRecord? record = null;
|
||||
var recordPath = Path.Combine(directory, ArchiveLayout.ProjectFile);
|
||||
try
|
||||
{
|
||||
record = ProjectSidecars.ReadRecord(Path.Combine(directory, ArchiveLayout.ProjectFile));
|
||||
record = ProjectSidecars.ReadRecord(recordPath);
|
||||
}
|
||||
catch (Exception ex) when (ex is IOException or InvalidDataException or UnauthorizedAccessException)
|
||||
{
|
||||
// Its folder names it (<id>, or <id>-<time> in the trash).
|
||||
var name = Path.GetFileName(directory);
|
||||
if (!Guid.TryParseExact(trashed && name.Length > 32 ? name[..32] : name, "N", out var named)) continue;
|
||||
if (!trashed && !File.Exists(Path.Combine(directory, ArchiveLayout.ProjectFile))
|
||||
&& await db.Projects.GetAsync(named, cancellationToken).ConfigureAwait(false) is null)
|
||||
if (!trashed && !File.Exists(recordPath))
|
||||
{
|
||||
// Versions with no record, of a project no index row names: a creation a crash or a lost disk cut short before its
|
||||
// record (versions are written first), or one a sync tool is still bringing. No project of the sequence is
|
||||
// hidden in it, so it holds nothing back; it is left as it is, and indexed once its record is there.
|
||||
Log.Info("projects", $"{directory} holds no project record; it is left as it is");
|
||||
var row = await db.Projects.GetAsync(named, cancellationToken).ConfigureAwait(false);
|
||||
if (row is null)
|
||||
{
|
||||
// Versions with no record, of a project no index row names: a creation a crash or a lost disk cut short before
|
||||
// its record (versions are written first), or one a sync tool is still bringing. No project of the sequence is
|
||||
// hidden in it, so it holds nothing back; it is left as it is, and indexed once its record is there.
|
||||
Log.Info("projects", $"{directory} holds no project record; it is left as it is");
|
||||
continue;
|
||||
}
|
||||
// A record lost (a sync tool, a disk) of a live project the index holds: written again from the index.
|
||||
if (writable && row is { DeletedAt: null } && row.SequenceId == sequenceId)
|
||||
record = await RecordFromIndexAsync(folder, row, recordPath, cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
if (record is null)
|
||||
{
|
||||
Log.Warning("projects", $"The project in {directory} cannot be read; it stays as the index has it", ex);
|
||||
// Its row stays as it is, never taken for gone nor written again, and a live one is held back (it may be Main).
|
||||
if (seen.Add(named) && !trashed) heldBack.Add(named);
|
||||
continue;
|
||||
}
|
||||
Log.Warning("projects", $"The project in {directory} cannot be read; it stays as the index has it", ex);
|
||||
// Its row stays as it is, never taken for gone nor written again, and a live one is held back (it may be Main).
|
||||
if (seen.Add(named) && !trashed) heldBack.Add(named);
|
||||
continue;
|
||||
}
|
||||
// A sequence folder copied by hand carries the projects of its source, which stay that sequence's.
|
||||
if (record.SequenceId != sequenceId || !seen.Add(record.Id)) continue;
|
||||
@@ -91,7 +105,7 @@ internal sealed class ProjectIndexer(ChronosDatabase db)
|
||||
}
|
||||
try
|
||||
{
|
||||
await SettleMainAsync(sequenceId, live, heldBack, cancellationToken).ConfigureAwait(false);
|
||||
await SettleMainAsync(folder, sequenceId, live, heldBack, writable, cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
catch (DatabaseException ex) when (ex.IsUniqueViolation || ex.IsForeignKeyViolation)
|
||||
{
|
||||
@@ -115,33 +129,98 @@ internal sealed class ProjectIndexer(ChronosDatabase db)
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The index's Main taken from the records, which say which project is Main (another index of the archive passed it on:
|
||||
/// an index-only Main written back beside the one made into the archive meanwhile): when the index's Main is read and its
|
||||
/// record no longer says it is Main, or the index has none, the oldest project whose record says it is becomes Main, and
|
||||
/// both take their records' names where free. Only records in step with the index count (a record behind it, put back
|
||||
/// from an older backup, says what was): never while the index's Main is held back or its record is not in step, and
|
||||
/// never more than one Main.
|
||||
/// The index's Main taken from the records, which say which project is Main. When several records in step say so (an
|
||||
/// index that met the sequence while Main's record was not there yet, a sync tool still bringing it, made a Main of its
|
||||
/// own; another index passed Main on), the oldest of them that has a version (by the time its record says it was made,
|
||||
/// then by id) is Main, the same in every index, and the others are ordinary projects, every version kept: in the index,
|
||||
/// and in their records too when the archive takes writes, so each index and older Chronos reads them alike. Main and the
|
||||
/// project it passes from take their records' names where free. Only records in step with the index count (a record
|
||||
/// behind it, put back from an older backup, says what was): never while the index's Main is held back or its record is
|
||||
/// not in step, and never more than one Main.
|
||||
/// </summary>
|
||||
private async Task SettleMainAsync(Guid sequenceId, IReadOnlyDictionary<Guid, ProjectRecord> live, IReadOnlySet<Guid> heldBack, CancellationToken cancellationToken)
|
||||
private async Task SettleMainAsync(SequencePaths folder, Guid sequenceId, Dictionary<Guid, ProjectRecord> live, IReadOnlySet<Guid> heldBack, bool writable,
|
||||
CancellationToken cancellationToken)
|
||||
{
|
||||
var rows = await db.Projects.ListBySequenceAsync(sequenceId, includeDeleted: false, cancellationToken).ConfigureAwait(false);
|
||||
ProjectRecord? InStep(SequenceProject row) =>
|
||||
!heldBack.Contains(row.Id) && live.TryGetValue(row.Id, out var record) && record.Head?.Id == row.HeadVersionId ? record : null;
|
||||
var main = rows.FirstOrDefault(p => p.IsMain);
|
||||
var own = main is null ? null : InStep(main);
|
||||
if (main is not null && own is not { IsMain: false }) return;
|
||||
var claimant = rows.Where(p => !p.IsMain && InStep(p) is { IsMain: true }).OrderBy(p => p.CreatedAt).ThenBy(p => p.Id).FirstOrDefault();
|
||||
if (claimant is null) return;
|
||||
var others = rows.Where(p => p.Id != claimant.Id && p.Id != main?.Id).ToList();
|
||||
var promoted = claimant with { IsMain = true, Name = FreeName(others, live[claimant.Id].Name, claimant.Id), MirroredVersionId = null, MirroredSettingsVersion = null };
|
||||
Log.Warning("projects", $"The records of sequence {sequenceId} name project {claimant.Id} as Main{(main is null ? string.Empty : $", not {main.Id}")}; the index takes it");
|
||||
if (main is null)
|
||||
if (main is not null && own is null) return;
|
||||
var claimants = rows.Where(p => p.HeadVersionId is not null && InStep(p) is { IsMain: true })
|
||||
.OrderBy(p => live[p.Id].Created.UtcTicks).ThenBy(p => p.Id).ToList();
|
||||
if (claimants.Count == 0) return;
|
||||
var winner = claimants[0];
|
||||
if (winner.Id != main?.Id)
|
||||
{
|
||||
await db.Projects.UpdateAsync(promoted, cancellationToken).ConfigureAwait(false);
|
||||
return;
|
||||
var others = rows.Where(p => p.Id != winner.Id && p.Id != main?.Id).ToList();
|
||||
var promoted = winner with { IsMain = true, Name = FreeName(others, live[winner.Id].Name, winner.Id), MirroredVersionId = null, MirroredSettingsVersion = null };
|
||||
Log.Warning("projects", $"The records of sequence {sequenceId} name project {winner.Id} as Main{(main is null ? string.Empty : $", not {main.Id}")}; the index takes it");
|
||||
if (main is null)
|
||||
{
|
||||
await db.Projects.UpdateAsync(promoted, cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
else
|
||||
{
|
||||
var demoted = main with { IsMain = false, Name = FreeName([.. others, promoted], own!.Name, main.Id), MirroredVersionId = null, MirroredSettingsVersion = null };
|
||||
await SwapMainAsync(db, demoted, promoted, cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
}
|
||||
if (!writable || claimants.Count < 2) return;
|
||||
// The records that say Main and are not: ordinary projects, as the index names them.
|
||||
foreach (var loser in claimants.Skip(1))
|
||||
{
|
||||
if (await db.Projects.GetAsync(loser.Id, cancellationToken).ConfigureAwait(false) is not { IsMain: false, DeletedAt: null } row) continue;
|
||||
var record = live[loser.Id] with { IsMain = false, Name = row.Name, Mirrored = null, Updated = DateTimeOffset.UtcNow };
|
||||
try
|
||||
{
|
||||
ProjectSidecars.WriteRecord(folder.ProjectRecordPath(loser.Id), record);
|
||||
live[loser.Id] = record;
|
||||
Log.Warning("projects", $"The record of project {loser.Id} of sequence {sequenceId} said it was Main beside project {winner.Id}; it says it is '{row.Name}'");
|
||||
}
|
||||
catch (Exception ex) when (ex is IOException or UnauthorizedAccessException)
|
||||
{
|
||||
Log.Warning("projects", $"The record of project {loser.Id} still says it is Main; tried again next time", ex);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The record of a live project the index holds, written again from the index when its folder has none (a sync tool, a
|
||||
/// disk lost it): the version files the folder lacks first (those there are left as they are), then the record naming
|
||||
/// the index's head and, for Main, the version this index mirrored. The versions the folder holds past that head are
|
||||
/// then adopted as after any push a crash cut short. Null when it cannot be written now (tried again next time).
|
||||
/// </summary>
|
||||
private async Task<ProjectRecord?> RecordFromIndexAsync(SequencePaths folder, SequenceProject row, string path, CancellationToken cancellationToken)
|
||||
{
|
||||
try
|
||||
{
|
||||
await WriteVersionsAsync(db, folder, row.Id, 0, cancellationToken).ConfigureAwait(false);
|
||||
var mirrored = row.IsMain && row.MirroredVersionId is { } id && await db.Projects.GetVersionAsync(id, cancellationToken).ConfigureAwait(false) is { } version
|
||||
? new ProjectHead(version.Id, version.Number)
|
||||
: null;
|
||||
var record = ProjectRecord.Of(row, mirrored);
|
||||
ProjectSidecars.WriteRecord(path, record);
|
||||
Log.Warning("projects", $"Project {row.Id} of sequence {row.SequenceId} had no record in its folder; it is written again from the index");
|
||||
return record;
|
||||
}
|
||||
catch (Exception ex) when (ex is IOException or InvalidDataException or UnauthorizedAccessException)
|
||||
{
|
||||
Log.Warning("projects", $"The record of project {row.Id} could not be written again from the index; tried again next time", ex);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>The version files of a project numbered above <paramref name="after"/> written from the index (immutable: one there already is left).</summary>
|
||||
internal static async Task WriteVersionsAsync(ChronosDatabase db, SequencePaths folder, Guid projectId, int after, CancellationToken cancellationToken)
|
||||
{
|
||||
while (true)
|
||||
{
|
||||
var page = await db.Projects.ListVersionsAfterAsync(projectId, after, WriteBackPage, cancellationToken).ConfigureAwait(false);
|
||||
foreach (var version in page) ProjectSidecars.WriteVersion(folder.VersionPath(projectId, version.Number, version.Id), version);
|
||||
if (page.Count < WriteBackPage) return;
|
||||
after = page[^1].Number;
|
||||
}
|
||||
var demoted = main with { IsMain = false, Name = FreeName([.. others, promoted], own!.Name, main.Id), MirroredVersionId = null, MirroredSettingsVersion = null };
|
||||
await SwapMainAsync(db, demoted, promoted, cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -272,6 +351,8 @@ internal sealed class ProjectIndexer(ChronosDatabase db)
|
||||
row = project with { HeadVersionId = null, HeadNumber = 0 };
|
||||
await db.Projects.InsertAsync(row, cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
// A record behind the index (a projects folder put back from an older backup) says what was: its name is not taken.
|
||||
var behind = row.HeadVersionId is not null && (record.Head?.Number ?? 0) < row.HeadNumber;
|
||||
await IndexVersionsAsync(record, directory, writable, cancellationToken).ConfigureAwait(false);
|
||||
var (headId, headNumber) = (row.HeadVersionId, row.HeadNumber);
|
||||
var reached = true;
|
||||
@@ -295,7 +376,7 @@ internal sealed class ProjectIndexer(ChronosDatabase db)
|
||||
{
|
||||
updated = updated with { DeletedAt = row.DeletedAt ?? record.Deleted };
|
||||
}
|
||||
else if (row.DeletedAt is not null || row.Name != record.Name)
|
||||
else if (row.DeletedAt is not null || (row.Name != record.Name && !behind))
|
||||
{
|
||||
var others = (await db.Projects.ListBySequenceAsync(record.SequenceId, includeDeleted: false, cancellationToken).ConfigureAwait(false))
|
||||
.Where(p => p.Id != record.Id).ToList();
|
||||
@@ -305,12 +386,12 @@ internal sealed class ProjectIndexer(ChronosDatabase db)
|
||||
Log.Warning("projects", $"Project {record.Id} of sequence {record.SequenceId} is live in its sidecars; it is listed again");
|
||||
updated = updated with { DeletedAt = null, IsMain = row.IsMain && !others.Any(p => p.IsMain) };
|
||||
}
|
||||
// The record's name, else (another project has it) the name the index has, else one free.
|
||||
// The record's name (unless it is behind), else (another project has it) the name the index has, else one free.
|
||||
updated = updated with
|
||||
{
|
||||
Name = FreeName(others, record.Name, record.Id) == record.Name ? record.Name
|
||||
Name = !behind && FreeName(others, record.Name, record.Id) == record.Name ? record.Name
|
||||
: FreeName(others, row.Name, record.Id) == row.Name ? row.Name
|
||||
: FreeName(others, record.Name, record.Id),
|
||||
: FreeName(others, behind ? row.Name : record.Name, record.Id),
|
||||
};
|
||||
}
|
||||
if (updated != row) await db.Projects.UpdateAsync(updated, cancellationToken).ConfigureAwait(false);
|
||||
|
||||
@@ -84,7 +84,8 @@ public sealed record HomeFrames(int FramesVersion, IReadOnlyList<Frame> Frames,
|
||||
/// Every operation runs under the sequence's lease and first catches up: the sidecars into the index
|
||||
/// (<see cref="ProjectIndexer"/>), the Main project made when the sequence has none (version 1, "imported" from the
|
||||
/// sequence's settings), and settings another path wrote into the sequence since the last mirror appended to Main as an
|
||||
/// "external" version (or merged with a push whose mirror a crash cut short). A version is written as a sidecar first
|
||||
/// "external" version (or merged with a push whose mirror a crash cut short, as one "merge" version in which the settings
|
||||
/// written meanwhile win where both changed). A version is written as a sidecar first
|
||||
/// (immutable), then the project's record, then the index; a push to Main is also mirrored into the sequence's
|
||||
/// <c>project.json</c> and settings (positions placed on its frames, through the settings version check), so the code
|
||||
/// paths that read the sequence's settings keep working and only Main drives the hero frame the Library shows. Other
|
||||
@@ -108,9 +109,6 @@ public sealed class ProjectService
|
||||
/// <summary>Frames read at once when a sequence's photos are gathered.</summary>
|
||||
private const int PageSize = 1000;
|
||||
|
||||
/// <summary>Versions read at once when a project's sidecars are written again from the index.</summary>
|
||||
private const int WriteBackPage = 500;
|
||||
|
||||
/// <summary>How far back from Main's head an index that knows no mirror looks for the version its sequence's settings hold.</summary>
|
||||
private const int MirrorSearch = 100;
|
||||
|
||||
@@ -548,74 +546,97 @@ public sealed class ProjectService
|
||||
// Only the sequence's settings version moved (settings another path wrote, or only its title or tags): settings that
|
||||
// differ from the head are its newest state.
|
||||
var latest = sameAsHead ? head : await AppendDerivedAsync(context, main, head, current, ProjectVersionKind.External, cancellationToken).ConfigureAwait(false);
|
||||
await _db.Projects.SetMirroredAsync(main.Id, latest.Id, context.Row.SettingsVersion, cancellationToken).ConfigureAwait(false);
|
||||
await MarkMirroredAsync(context, main, latest, frames.Photos, cancellationToken).ConfigureAwait(false);
|
||||
return;
|
||||
}
|
||||
// The head is newer than the mirror: a push whose mirror a crash (or a settings edit at the same moment) cut short.
|
||||
var mirrored = await _db.Projects.GetVersionAsync(main.MirroredVersionId.Value, cancellationToken).ConfigureAwait(false);
|
||||
if (sameAsHead)
|
||||
{
|
||||
// A head that is the external version of a merge a crash stopped before its merge version (its parent a push no
|
||||
// mirror wrote): the merge is finished now, else that push would never be in force. Any other: in force already.
|
||||
if (head.Kind == ProjectVersionKind.External && head.ParentId is { } pushedId && pushedId != main.MirroredVersionId
|
||||
&& mirrored is { } basis && basis.ProjectId == main.Id
|
||||
&& await _db.Projects.GetVersionAsync(pushedId, cancellationToken).ConfigureAwait(false) is { } pushed && pushed.ProjectId == main.Id)
|
||||
{
|
||||
Log.Warning("projects", $"The merge of version {pushed.Number} of Main of sequence {context.Row.Id} stopped after version {head.Number}; it is finished");
|
||||
await MergeIntoMainAsync(context, main, basis, pushed, head.SettingsJson, head, frames.Photos, cancellationToken).ConfigureAwait(false);
|
||||
return;
|
||||
}
|
||||
await _db.Projects.SetMirroredAsync(main.Id, head.Id, context.Row.SettingsVersion, cancellationToken).ConfigureAwait(false);
|
||||
await MarkMirroredAsync(context, main, head, frames.Photos, cancellationToken).ConfigureAwait(false);
|
||||
return;
|
||||
}
|
||||
var mirrored = await _db.Projects.GetVersionAsync(main.MirroredVersionId.Value, cancellationToken).ConfigureAwait(false);
|
||||
if (main.MirroredSettingsVersion == context.Row.SettingsVersion || mirrored is null || ProjectContent.SameChoices(mirrored.SettingsJson, current, frames.Photos))
|
||||
{
|
||||
// Nobody changed the settings since the last mirror: the mirror completes.
|
||||
await MirrorAsync(context, main, head, cancellationToken).ConfigureAwait(false);
|
||||
return;
|
||||
}
|
||||
await MergeIntoMainAsync(context, main, mirrored, head, current, null, frames.Photos, cancellationToken).ConfigureAwait(false);
|
||||
await MergeIntoMainAsync(context, main, mirrored, head, current, frames.Photos, cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Both moved: the settings written meanwhile (<paramref name="theirs"/>) merge with <paramref name="pushed"/>, the head no
|
||||
/// mirror wrote, over <paramref name="basis"/>, the version mirrored last (the version made in a workspace wins where both
|
||||
/// changed), each photo named as the frames hold it now so the same photo merges as one. Where both changed, the settings
|
||||
/// written meanwhile become an external version first, so the values the merge leaves out stay in the history to restore;
|
||||
/// the merge version follows it and is mirrored. <paramref name="external"/> is that external version when a crash
|
||||
/// stopped the pair between its two versions: the merge version is made on it, so a cut anywhere is finished next time.
|
||||
/// Both moved: the settings written meanwhile (<paramref name="theirs"/>, the settings in force) merge with
|
||||
/// <paramref name="head"/>, Main's versions no mirror wrote, over <paramref name="basis"/>, the version mirrored last, each
|
||||
/// photo named as the frames hold it now so the same photo merges as one. Where both changed, the settings written
|
||||
/// meanwhile win: they are the newer write, which older clients made and read, and the head's values stay in its version,
|
||||
/// in the history to restore. The result is one merge version on the head, mirrored, so there is no pair a crash could
|
||||
/// cut in two. A merge that holds nothing the head lacks makes no version: the head is mirrored. That is also how a merge
|
||||
/// whose mirror a crash cut short is finished next time, by this index or by one rebuilt from the sidecars: merged again
|
||||
/// over the same basis, it is the merge version itself.
|
||||
/// </summary>
|
||||
private async Task MergeIntoMainAsync(LeaseContext context, SequenceProject main, ProjectVersion basis, ProjectVersion pushed, string theirs,
|
||||
ProjectVersion? external, FramePhotos photos, CancellationToken cancellationToken)
|
||||
private async Task MergeIntoMainAsync(LeaseContext context, SequenceProject main, ProjectVersion basis, ProjectVersion head, string theirs,
|
||||
FramePhotos photos, CancellationToken cancellationToken)
|
||||
{
|
||||
ProjectMergeResult merged;
|
||||
try
|
||||
{
|
||||
merged = ProjectMerge.MergeWithConflicts(ProjectMerge.Read(ProjectContent.Rebind(basis.SettingsJson, photos)),
|
||||
ProjectMerge.Read(ProjectContent.Rebind(pushed.SettingsJson, photos)), ProjectMerge.Read(ProjectContent.Rebind(theirs, photos)));
|
||||
ProjectMerge.Read(ProjectContent.Rebind(theirs, photos)), ProjectMerge.Read(ProjectContent.Rebind(head.SettingsJson, photos)));
|
||||
}
|
||||
catch (NewerSettingsException ex)
|
||||
{
|
||||
// A newer Chronos's version is never merged with this one's settings: both are left as they are, recorded as seen.
|
||||
Log.Warning("projects", $"Version {pushed.Number} of Main of sequence {context.Row.Id} and its settings written meanwhile are left as they are", ex);
|
||||
Log.Warning("projects", $"Version {head.Number} of Main of sequence {context.Row.Id} and its settings written meanwhile are left as they are", ex);
|
||||
await _db.Projects.SetMirroredAsync(main.Id, main.HeadVersionId, context.Row.SettingsVersion, cancellationToken).ConfigureAwait(false);
|
||||
return;
|
||||
}
|
||||
var parent = external ?? pushed;
|
||||
if (merged.HasConflicts && external is null)
|
||||
var text = ProjectContent.Canonical(merged.Merged.ToJson());
|
||||
if (ProjectContent.SameChoices(head.SettingsJson, text, photos))
|
||||
{
|
||||
parent = await AppendDerivedAsync(context, main, pushed, theirs, ProjectVersionKind.External, cancellationToken).ConfigureAwait(false);
|
||||
main = (await _db.Projects.GetAsync(main.Id, cancellationToken).ConfigureAwait(false))!;
|
||||
Log.Info("projects", $"Version {pushed.Number} of Main of sequence {context.Row.Id} and its settings written meanwhile both changed "
|
||||
+ $"{string.Join(", ", merged.Conflicts.Select(c => c.Path))}: version {pushed.Number}'s values are kept, the others are version {parent.Number}");
|
||||
Step("catchup.external");
|
||||
await MirrorAsync(context, main, head, cancellationToken).ConfigureAwait(false);
|
||||
return;
|
||||
}
|
||||
var version = await AppendDerivedAsync(context, main, parent, ProjectContent.Canonical(merged.Merged.ToJson()), ProjectVersionKind.Merge, cancellationToken)
|
||||
.ConfigureAwait(false);
|
||||
if (merged.HasConflicts)
|
||||
{
|
||||
Log.Info("projects", $"Version {head.Number} of Main of sequence {context.Row.Id} and its settings written meanwhile both changed "
|
||||
+ $"{string.Join(", ", merged.Conflicts.Select(c => c.Path))}: the settings written meanwhile are kept, version {head.Number} holds the others");
|
||||
}
|
||||
var version = await AppendDerivedAsync(context, main, head, text, ProjectVersionKind.Merge, cancellationToken).ConfigureAwait(false);
|
||||
main = (await _db.Projects.GetAsync(main.Id, cancellationToken).ConfigureAwait(false))!;
|
||||
Step("catchup.merge");
|
||||
await MirrorAsync(context, main, version, cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// <paramref name="version"/> recorded as the one the sequence's settings hold (they are its choices): in the index, and
|
||||
/// in Main's record when <c>project.json</c> holds them too, so an index meeting Main later compares <c>project.json</c>
|
||||
/// with it and never takes settings written after it for a merge over an older version.
|
||||
/// </summary>
|
||||
private async Task MarkMirroredAsync(LeaseContext context, SequenceProject main, ProjectVersion version, FramePhotos photos, CancellationToken cancellationToken)
|
||||
{
|
||||
await _db.Projects.SetMirroredAsync(main.Id, version.Id, context.Row.SettingsVersion, cancellationToken).ConfigureAwait(false);
|
||||
if (File.Exists(context.Folder.ProjectPath) && SharedPortable(context, photos) is { } shared && ProjectContent.SameChoices(version.SettingsJson, shared, photos))
|
||||
NameMirrored(context, main, version);
|
||||
}
|
||||
|
||||
/// <summary>Main's record naming <paramref name="version"/> as mirrored (its head and the rest as they are); a record that cannot be read is left.</summary>
|
||||
private static void NameMirrored(LeaseContext context, SequenceProject main, ProjectVersion version)
|
||||
{
|
||||
var path = context.Folder.ProjectRecordPath(main.Id);
|
||||
try
|
||||
{
|
||||
var record = ProjectSidecars.ReadRecord(path);
|
||||
var mirrored = new ProjectHead(version.Id, version.Number);
|
||||
if (record.Mirrored != mirrored) ProjectSidecars.WriteRecord(path, record with { Mirrored = mirrored });
|
||||
}
|
||||
catch (Exception ex) when (ex is IOException or InvalidDataException or UnauthorizedAccessException)
|
||||
{
|
||||
// An index meeting Main then compares project.json with the versions the head descends from.
|
||||
Log.Warning("projects", $"The record of Main of sequence {context.Row.Id} could not name version {version.Number} as mirrored", ex);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Main as this index meets it with no mirror known (an index rebuilt from the sidecars, another PC's index of the archive,
|
||||
/// an index taken back from a backup): the sequence's settings are what its <c>project.json</c> holds, the file every
|
||||
@@ -623,8 +644,8 @@ public sealed class ProjectService
|
||||
/// copy brought up to them. Those of a version the head descends from, down to the one Main's record says was mirrored
|
||||
/// last (a record whose write failed after <c>project.json</c>'s lags it, so it only bounds the search; an older record
|
||||
/// names none: the last versions): a mirror a crash cut short, completed. Neither: settings another path wrote since,
|
||||
/// merged with the versions no mirror wrote when the record names the one mirrored last (as an index that knows its
|
||||
/// mirror merges them), else an external version.
|
||||
/// in force, merged with the versions no mirror wrote when the record names the one mirrored last (as an index that knows
|
||||
/// its mirror merges them: the settings written since win where both changed), else an external version.
|
||||
/// </summary>
|
||||
private async Task MeetMainAsync(LeaseContext context, SequenceProject main, ProjectVersion head, FramePhotos photos, CancellationToken cancellationToken)
|
||||
{
|
||||
@@ -634,23 +655,16 @@ public sealed class ProjectService
|
||||
await _db.Projects.SetMirroredAsync(main.Id, head.Id, context.Row.SettingsVersion, cancellationToken).ConfigureAwait(false);
|
||||
return;
|
||||
}
|
||||
var recorded = await RecordedMirrorAsync(context, main, head, cancellationToken).ConfigureAwait(false);
|
||||
if (ProjectContent.SameChoices(head.SettingsJson, shared, photos))
|
||||
{
|
||||
// The external version of a merge a crash stopped before its merge version (see CatchUpAsync): finished now.
|
||||
if (recorded is not null && head.Kind == ProjectVersionKind.External && head.ParentId is { } pushedId && pushedId != recorded.Id
|
||||
&& await _db.Projects.GetVersionAsync(pushedId, cancellationToken).ConfigureAwait(false) is { } pushed && pushed.ProjectId == main.Id)
|
||||
{
|
||||
await MergeIntoMainAsync(context, main, recorded, pushed, head.SettingsJson, head, photos, cancellationToken).ConfigureAwait(false);
|
||||
return;
|
||||
}
|
||||
if (CurrentPortable(context, photos) is { } copy && ProjectContent.SameChoices(head.SettingsJson, copy, photos))
|
||||
await _db.Projects.SetMirroredAsync(main.Id, head.Id, context.Row.SettingsVersion, cancellationToken).ConfigureAwait(false);
|
||||
await MarkMirroredAsync(context, main, head, photos, cancellationToken).ConfigureAwait(false);
|
||||
else
|
||||
await MirrorAsync(context, main, head, cancellationToken).ConfigureAwait(false);
|
||||
return;
|
||||
}
|
||||
// A record that names the head as mirrored says project.json held it: whatever it holds now was written since.
|
||||
var recorded = await RecordedMirrorAsync(context, main, head, cancellationToken).ConfigureAwait(false);
|
||||
var headMirrored = context.Reconcile.Live.TryGetValue(main.Id, out var record) && record.Mirrored?.Id == head.Id;
|
||||
if (!headMirrored && await MirroredBeforeAsync(main, head, recorded, shared, photos, cancellationToken).ConfigureAwait(false))
|
||||
{
|
||||
@@ -659,7 +673,8 @@ public sealed class ProjectService
|
||||
}
|
||||
if (recorded is not null)
|
||||
{
|
||||
await MergeIntoMainAsync(context, main, recorded, head, shared, null, photos, cancellationToken).ConfigureAwait(false);
|
||||
// The settings written since stay in force where both changed; the versions' values stay in the history.
|
||||
await MergeIntoMainAsync(context, main, recorded, head, shared, photos, cancellationToken).ConfigureAwait(false);
|
||||
return;
|
||||
}
|
||||
var external = await AppendDerivedAsync(context, main, head, shared, ProjectVersionKind.External, cancellationToken).ConfigureAwait(false);
|
||||
@@ -820,16 +835,8 @@ public sealed class ProjectService
|
||||
}
|
||||
|
||||
/// <summary>The version files of a project numbered above <paramref name="after"/> written from the index (immutable: one there already is left).</summary>
|
||||
private async Task WriteVersionsAsync(SequencePaths folder, Guid projectId, int after, CancellationToken cancellationToken)
|
||||
{
|
||||
while (true)
|
||||
{
|
||||
var page = await _db.Projects.ListVersionsAfterAsync(projectId, after, WriteBackPage, cancellationToken).ConfigureAwait(false);
|
||||
foreach (var version in page) ProjectSidecars.WriteVersion(folder.VersionPath(projectId, version.Number, version.Id), version);
|
||||
if (page.Count < WriteBackPage) return;
|
||||
after = page[^1].Number;
|
||||
}
|
||||
}
|
||||
private Task WriteVersionsAsync(SequencePaths folder, Guid projectId, int after, CancellationToken cancellationToken) =>
|
||||
ProjectIndexer.WriteVersionsAsync(_db, folder, projectId, after, cancellationToken);
|
||||
|
||||
/// <summary>The version a Main record names as mirrored: the one this index mirrored when it knows it, else <paramref name="fallback"/>.</summary>
|
||||
private async Task<ProjectHead?> MirroredHeadAsync(SequenceProject project, ProjectHead? fallback, CancellationToken cancellationToken) =>
|
||||
@@ -1030,7 +1037,9 @@ public sealed class ProjectService
|
||||
try
|
||||
{
|
||||
if (IsNewer(version)) throw new InvalidDataException($"Version {version.Number} of Main is of a newer Chronos.");
|
||||
placed = ProjectSettings.FromJson(ProjectContent.Place(version.SettingsJson, frames.Photos));
|
||||
// Read as the other branches read settings (one of a newer Chronos is never refused), and never mirrored when newer.
|
||||
placed = ProjectSettings.FromJson(ProjectContent.Place(version.SettingsJson, frames.Photos), allowNewer: true);
|
||||
if (placed.IsNewerFormat) throw new InvalidDataException($"Version {version.Number} of Main is of a newer Chronos.");
|
||||
}
|
||||
catch (Exception ex) when (ex is JsonException or InvalidDataException)
|
||||
{
|
||||
@@ -1040,16 +1049,7 @@ public sealed class ProjectService
|
||||
var json = placed.ToJson();
|
||||
Directory.CreateDirectory(context.Folder.MetaDirectory);
|
||||
AtomicFile.WriteAllText(context.Folder.ProjectPath, json);
|
||||
try
|
||||
{
|
||||
var record = ProjectSidecars.ReadRecord(context.Folder.ProjectRecordPath(main.Id));
|
||||
ProjectSidecars.WriteRecord(context.Folder.ProjectRecordPath(main.Id), record with { Mirrored = new ProjectHead(version.Id, version.Number) });
|
||||
}
|
||||
catch (Exception ex) when (ex is IOException or InvalidDataException or UnauthorizedAccessException)
|
||||
{
|
||||
// An index meeting Main then compares project.json with the versions the head descends from.
|
||||
Log.Warning("projects", $"The record of Main of sequence {context.Row.Id} could not name version {version.Number} as mirrored", ex);
|
||||
}
|
||||
NameMirrored(context, main, version);
|
||||
var hero = placed.HeroFrame is { } index && (uint)index < (uint)frames.Frames.Count ? frames.Frames[index].Id : Guid.Empty;
|
||||
var result = await _db.Sequences.EditAsync(context.Row.Id, new SequenceEdit { SettingsJson = json, HeroFrameId = hero }, context.Row.SettingsVersion,
|
||||
context.Row.FramesVersion, cancellationToken).ConfigureAwait(false);
|
||||
|
||||
@@ -18,9 +18,10 @@ public sealed class ProjectVersionTooLargeException(string message) : IOExceptio
|
||||
/// The record of a project in its folder (<c>.chronos/projects/<id>/project.json</c>): what the index row is rebuilt
|
||||
/// from. <see cref="Deleted"/> is set when the project was deleted, once its folder is in the projects' trash.
|
||||
/// </summary>
|
||||
/// <param name="Mirrored">Main only: the version whose settings a mirror last wrote into the sequence's <c>project.json</c>
|
||||
/// (any index of the archive). An index that meets Main for the first time compares <c>project.json</c> with it, never
|
||||
/// with a copy of the settings it may have read long ago (<see cref="ProjectService"/>). Older records have none.</param>
|
||||
/// <param name="Mirrored">Main only: the version whose settings the sequence's <c>project.json</c> was last known to hold
|
||||
/// (any index of the archive): written there by a mirror, or written by another path and made that external version. An
|
||||
/// index that meets Main for the first time compares <c>project.json</c> with it, never with a copy of the settings it may
|
||||
/// have read long ago (<see cref="ProjectService"/>). Older records have none.</param>
|
||||
public sealed record ProjectRecord(Guid Id, Guid SequenceId, string Name, bool IsMain, ProjectHead? Head, DateTimeOffset Created, string? CreatedBy,
|
||||
DateTimeOffset Updated, DateTimeOffset? Deleted = null, ProjectHead? Mirrored = null)
|
||||
{
|
||||
|
||||
@@ -134,7 +134,7 @@ public abstract class MainMergeTests(TestContext context) : DatabaseTest(context
|
||||
private async Task<ArchiveFixture> FixtureAsync() => Own(await ArchiveFixture.CreateAsync(Context, database: await NewOptionsAsync(multiConnection: true)));
|
||||
|
||||
[Test]
|
||||
public async Task TheValuesTheMergeLeavesOutStayInMainsHistory()
|
||||
public async Task WhereBothChangedTheSettingsWrittenMeanwhileWinAndThePushStaysInMainsHistory()
|
||||
{
|
||||
var fx = await FixtureAsync();
|
||||
var (sequence, frames) = await ProjectServiceTests.UploadedAsync(fx, 4);
|
||||
@@ -152,20 +152,19 @@ public abstract class MainMergeTests(TestContext context) : DatabaseTest(context
|
||||
await fx.Db.Projects.SetMirroredAsync(main.Id, first.Id, reverted.SettingsVersion, fx.Ct);
|
||||
await ProjectServiceTests.WriteSettingsAsync(fx, sequence.Id, new ProjectSettings { Exposure = new ExposureSettings { ExposureOffsetEv = 1.25 }, ExcludedFrames = [3] });
|
||||
|
||||
// The pushed version wins where both changed; the other writer's settings are the version before the merge.
|
||||
// One merge version on the push: the settings written meanwhile win where both changed, the push's offset stays in its version.
|
||||
var history = await projects.HistoryAsync(main.Id, cancellationToken: fx.Ct);
|
||||
Assert.SequenceEqual([ProjectVersionKind.Merge, ProjectVersionKind.External, ProjectVersionKind.Commit, ProjectVersionKind.Imported], history.Select(v => v.Kind));
|
||||
Assert.Equal((history[1].Id, pushed.Id), (history[0].ParentId, history[1].ParentId));
|
||||
var external = ProjectSettings.FromJson(ProjectContent.Place(history[1].SettingsJson, ProjectServiceTests.Photos(frames)));
|
||||
Assert.Equal((1.25, StackSetting.None), (external.Exposure.ExposureOffsetEv, external.Stack.Mode));
|
||||
Assert.SequenceEqual([3], external.ExcludedFrames);
|
||||
Assert.SequenceEqual([ProjectVersionKind.Merge, ProjectVersionKind.Commit, ProjectVersionKind.Imported], history.Select(v => v.Kind));
|
||||
Assert.Equal((pushed.Id, pushed.Id), (history[0].ParentId, history[1].Id));
|
||||
Assert.Equal(0.5, ProjectSettings.FromJson(history[1].SettingsJson).Exposure.ExposureOffsetEv);
|
||||
var settings = ProjectSettings.FromJson((await fx.Db.Sequences.GetAsync(sequence.Id))!.SettingsJson!);
|
||||
Assert.Equal((0.5, StackSetting.Median), (settings.Exposure.ExposureOffsetEv, settings.Stack.Mode));
|
||||
Assert.Equal((1.25, StackSetting.Median), (settings.Exposure.ExposureOffsetEv, settings.Stack.Mode));
|
||||
Assert.SequenceEqual([3], settings.ExcludedFrames);
|
||||
Assert.SequenceEqual([3], ProjectSettings.FromJson(ProjectContent.Place(history[0].SettingsJson, ProjectServiceTests.Photos(frames))).ExcludedFrames);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task AMergeACrashStopsBetweenItsTwoVersionsIsFinishedNextTime()
|
||||
public async Task AMergeACrashCutBeforeItsMirrorIsFinishedNextTimeWithNoOtherVersion()
|
||||
{
|
||||
foreach (var rebuild in new[] { false, true })
|
||||
{
|
||||
@@ -183,7 +182,7 @@ public abstract class MainMergeTests(TestContext context) : DatabaseTest(context
|
||||
var record = ProjectSidecars.ReadRecord(folder.ProjectRecordPath(main.Id));
|
||||
ProjectSidecars.WriteRecord(folder.ProjectRecordPath(main.Id), record with { Mirrored = new ProjectHead(first.Id, 1) });
|
||||
|
||||
// The push's mirror cut short, another path's settings meanwhile, and the catch-up's merge cut after its external version.
|
||||
// The push's mirror cut short, another path's settings meanwhile, and the catch-up's merge cut before its mirror.
|
||||
var reverted = await ProjectServiceTests.WriteSettingsAsync(fx, sequence.Id, new ProjectSettings());
|
||||
await fx.Db.Projects.SetMirroredAsync(main.Id, first.Id, reverted.SettingsVersion, fx.Ct);
|
||||
var theirs = new ProjectSettings { Exposure = new ExposureSettings { ExposureOffsetEv = 1.25 }, ExcludedFrames = [3] };
|
||||
@@ -191,33 +190,118 @@ public abstract class MainMergeTests(TestContext context) : DatabaseTest(context
|
||||
File.WriteAllText(folder.ProjectPath, theirs.ToJson());
|
||||
projects.Cut = step =>
|
||||
{
|
||||
if (step == "catchup.external") throw new CutException(step);
|
||||
if (step == "catchup.merge") throw new CutException(step);
|
||||
};
|
||||
await Assert.ThrowsAsync<CutException>(() => projects.ListAsync(sequence.Id, fx.Ct));
|
||||
projects.Cut = null;
|
||||
Assert.SequenceEqual([ProjectVersionKind.External, ProjectVersionKind.Commit, ProjectVersionKind.Imported],
|
||||
Assert.SequenceEqual([ProjectVersionKind.Merge, ProjectVersionKind.Commit, ProjectVersionKind.Imported],
|
||||
(await fx.Db.Projects.ListVersionsAsync(main.Id, null, 10, fx.Ct)).Select(v => v.Kind));
|
||||
Assert.Equal(StackSetting.None, ProjectSettings.FromJson(File.ReadAllText(folder.ProjectPath)).Stack.Mode);
|
||||
|
||||
// The next catch-up (by this index, or by one rebuilt from the sidecars) makes the merge version: the push is in force.
|
||||
// The next catch-up (by this index, or by one rebuilt from the sidecars) mirrors that merge version and adds none.
|
||||
if (rebuild)
|
||||
{
|
||||
await fx.ReopenWithEmptyIndexAsync();
|
||||
await fx.ScanAsync(ScanMode.Rebuild);
|
||||
}
|
||||
var history = await fx.Service.Projects.HistoryAsync(main.Id, cancellationToken: fx.Ct);
|
||||
Assert.SequenceEqual([ProjectVersionKind.Merge, ProjectVersionKind.External, ProjectVersionKind.Commit, ProjectVersionKind.Imported],
|
||||
history.Select(v => v.Kind));
|
||||
Assert.Equal((history[1].Id, pushed.Id), (history[0].ParentId, history[1].ParentId));
|
||||
foreach (var _ in new[] { 1, 2 })
|
||||
{
|
||||
var history = await fx.Service.Projects.HistoryAsync(main.Id, cancellationToken: fx.Ct);
|
||||
Assert.SequenceEqual([ProjectVersionKind.Merge, ProjectVersionKind.Commit, ProjectVersionKind.Imported], history.Select(v => v.Kind));
|
||||
Assert.Equal(pushed.Id, history[0].ParentId);
|
||||
}
|
||||
foreach (var settings in new[] { ProjectSettings.FromJson((await fx.Db.Sequences.GetAsync(sequence.Id))!.SettingsJson!),
|
||||
ProjectSettings.FromJson(File.ReadAllText(folder.ProjectPath)) })
|
||||
{
|
||||
Assert.Equal((0.5, StackSetting.Median), (settings.Exposure.ExposureOffsetEv, settings.Stack.Mode));
|
||||
Assert.Equal((1.25, StackSetting.Median), (settings.Exposure.ExposureOffsetEv, settings.Stack.Mode));
|
||||
Assert.SequenceEqual([3], settings.ExcludedFrames);
|
||||
}
|
||||
Assert.Count(4, await fx.Service.Projects.HistoryAsync(main.Id, cancellationToken: fx.Ct));
|
||||
}
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task TwoOutsideEditsInARowAreNoMergeForAnyIndex()
|
||||
{
|
||||
foreach (var rebuild in new[] { true, false })
|
||||
{
|
||||
var fx = await FixtureAsync();
|
||||
var (sequence, frames) = await ProjectServiceTests.UploadedAsync(fx, 3);
|
||||
var projects = fx.Service.Projects;
|
||||
var main = await projects.MainAsync(sequence.Id, fx.Ct);
|
||||
var first = await projects.HeadAsync(main.Id, fx.Ct);
|
||||
var pushed = (await projects.PushAsync(main.Id, ProjectServiceTests.Draft(first, new ProjectSettings { ExcludedFrames = [0] }, frames), fx.Ct)).Version!;
|
||||
var folder = fx.Paths.Sequence(sequence.Folder);
|
||||
// Another PC's index that met Main mirrored at version 2.
|
||||
var (other, _) = await fx.OtherWriterAsync();
|
||||
await other.Projects.ListAsync(sequence.Id, fx.Ct);
|
||||
|
||||
// An older path edits the settings twice, the second time back to what version 2 holds: two external versions.
|
||||
foreach (var excluded in new[] { new[] { 0, 1 }, new[] { 0 } })
|
||||
{
|
||||
var settings = new ProjectSettings { ExcludedFrames = [.. excluded] };
|
||||
await ProjectServiceTests.WriteSettingsAsync(fx, sequence.Id, settings);
|
||||
File.WriteAllText(folder.ProjectPath, settings.ToJson());
|
||||
await projects.ListAsync(sequence.Id, fx.Ct);
|
||||
}
|
||||
var latest = (await projects.HeadAsync(main.Id, fx.Ct)).Id;
|
||||
Assert.Equal(latest, ProjectSidecars.ReadRecord(folder.ProjectRecordPath(main.Id)).Mirrored?.Id);
|
||||
// As an older build leaves the record (naming the push as mirrored): the shape of the history decides nothing.
|
||||
var record = ProjectSidecars.ReadRecord(folder.ProjectRecordPath(main.Id));
|
||||
ProjectSidecars.WriteRecord(folder.ProjectRecordPath(main.Id), record with { Mirrored = new ProjectHead(pushed.Id, pushed.Number) });
|
||||
|
||||
var service = other;
|
||||
if (rebuild)
|
||||
{
|
||||
await fx.ReopenWithEmptyIndexAsync();
|
||||
await fx.ScanAsync(ScanMode.Rebuild);
|
||||
service = fx.Service;
|
||||
}
|
||||
await service.Projects.ListAsync(sequence.Id, fx.Ct);
|
||||
var history = await service.Projects.HistoryAsync(main.Id, cancellationToken: fx.Ct);
|
||||
Assert.SequenceEqual([ProjectVersionKind.External, ProjectVersionKind.External, ProjectVersionKind.Commit, ProjectVersionKind.Imported],
|
||||
history.Select(v => v.Kind));
|
||||
Assert.Equal(latest, history[0].Id);
|
||||
Assert.SequenceEqual([0], ProjectSettings.FromJson(ProjectContent.Place(history[0].SettingsJson, ProjectServiceTests.Photos(frames))).ExcludedFrames);
|
||||
Assert.SequenceEqual([0], ProjectSettings.FromJson(File.ReadAllText(folder.ProjectPath)).ExcludedFrames);
|
||||
}
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task AnOutsideEditAfterAPushWhoseRecordLagsStaysInForce()
|
||||
{
|
||||
var fx = await FixtureAsync();
|
||||
var (sequence, frames) = await ProjectServiceTests.UploadedAsync(fx, 3);
|
||||
var projects = fx.Service.Projects;
|
||||
var main = await projects.MainAsync(sequence.Id, fx.Ct);
|
||||
var first = await projects.HeadAsync(main.Id, fx.Ct);
|
||||
var second = (await projects.PushAsync(main.Id, ProjectServiceTests.Draft(first, new ProjectSettings { ExcludedFrames = [0] }, frames), fx.Ct)).Version!;
|
||||
var third = (await projects.PushAsync(main.Id, ProjectServiceTests.Draft(second, new ProjectSettings
|
||||
{
|
||||
ExcludedFrames = [0],
|
||||
Exposure = new ExposureSettings { ExposureOffsetEv = 0.5 },
|
||||
}, frames), fx.Ct)).Version!;
|
||||
|
||||
// Version 3's mirror wrote project.json but not the record (it still names version 2); then an older path edits project.json.
|
||||
var folder = fx.Paths.Sequence(sequence.Folder);
|
||||
var record = ProjectSidecars.ReadRecord(folder.ProjectRecordPath(main.Id));
|
||||
ProjectSidecars.WriteRecord(folder.ProjectRecordPath(main.Id), record with { Mirrored = new ProjectHead(second.Id, 2) });
|
||||
File.WriteAllText(folder.ProjectPath, new ProjectSettings { ExcludedFrames = [0], Exposure = new ExposureSettings { ExposureOffsetEv = 1.0 } }.ToJson());
|
||||
await fx.ReopenWithEmptyIndexAsync();
|
||||
await fx.ScanAsync(ScanMode.Rebuild);
|
||||
|
||||
// The outside edit stays in force; version 3's offset stays in the history.
|
||||
var history = await fx.Service.Projects.HistoryAsync(main.Id, cancellationToken: fx.Ct);
|
||||
Assert.SequenceEqual([ProjectVersionKind.Merge, ProjectVersionKind.Commit, ProjectVersionKind.Commit, ProjectVersionKind.Imported], history.Select(v => v.Kind));
|
||||
Assert.Equal((third.Id, 0.5), (history[1].Id, ProjectSettings.FromJson(history[1].SettingsJson).Exposure.ExposureOffsetEv));
|
||||
foreach (var settings in new[] { ProjectSettings.FromJson((await fx.Db.Sequences.GetAsync(sequence.Id))!.SettingsJson!),
|
||||
ProjectSettings.FromJson(File.ReadAllText(folder.ProjectPath)) })
|
||||
{
|
||||
Assert.Equal(1.0, settings.Exposure.ExposureOffsetEv);
|
||||
Assert.SequenceEqual([0], settings.ExcludedFrames);
|
||||
}
|
||||
Assert.Count(4, await fx.Service.Projects.HistoryAsync(main.Id, cancellationToken: fx.Ct));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task ANewerChronosHeadOfMainIsNotMergedWithSettingsWrittenMeanwhile()
|
||||
{
|
||||
|
||||
@@ -579,9 +579,111 @@ public abstract class ProjectRecoveryTests(TestContext context) : DatabaseTest(c
|
||||
Assert.FileExists(folder.VersionPath(id, 1, cut.Id));
|
||||
Assert.Null(await fx.Db.Projects.GetAsync(id));
|
||||
|
||||
// Its record there at last (a sync tool bringing another PC's project): indexed as another project, Main stays.
|
||||
// Its record there at last (a sync tool bringing another PC's Main, made before this one): Main, as in every index; the
|
||||
// one made here meanwhile is another project, its version kept.
|
||||
ProjectSidecars.WriteRecord(folder.ProjectRecordPath(id), new ProjectRecord(id, sequence.Id, ProjectService.MainName, true, new ProjectHead(cut.Id, 1), now, null, now));
|
||||
Assert.SequenceEqual([(main.Id, true), (id, false)], (await fx.Service.Projects.ListAsync(sequence.Id, fx.Ct)).Select(p => (p.Id, p.IsMain)));
|
||||
Assert.SequenceEqual([(id, "Main", true), (main.Id, "Main (2)", false)],
|
||||
(await fx.Service.Projects.ListAsync(sequence.Id, fx.Ct)).Select(p => (p.Id, p.Name, p.IsMain)));
|
||||
Assert.Equal(1, MainRecords(folder));
|
||||
Assert.Count(1, await fx.Service.Projects.HistoryAsync(main.Id, cancellationToken: fx.Ct));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task AMainMadeWhileAnotherMainsRecordWasNotThereYetGivesWayInEveryIndex()
|
||||
{
|
||||
var fx = await FixtureAsync();
|
||||
var (sequence, frames) = await UploadedAsync(fx, 3);
|
||||
var projects = fx.Service.Projects;
|
||||
var main = await projects.MainAsync(sequence.Id, fx.Ct);
|
||||
var pushed = (await projects.PushAsync(main.Id, Draft(await projects.HeadAsync(main.Id, fx.Ct), new ProjectSettings { ExcludedFrames = [1] }, frames), fx.Ct))
|
||||
.Version!;
|
||||
var folder = fx.Paths.Sequence(sequence.Folder);
|
||||
|
||||
// Another PC's index meets the sequence while a sync tool is still bringing Main's record: it makes a Main of its own.
|
||||
var record = folder.ProjectRecordPath(main.Id);
|
||||
File.Move(record, record + ".syncing");
|
||||
var (other, _) = await fx.OtherWriterAsync();
|
||||
var theirs = await other.Projects.MainAsync(sequence.Id, fx.Ct);
|
||||
Assert.NotEqual(main.Id, theirs.Id);
|
||||
File.Move(record + ".syncing", record);
|
||||
|
||||
// The record there, both indexes take the older Main, in either order, and the other is an ordinary project with its version.
|
||||
foreach (var service in new[] { other, fx.Service, other })
|
||||
{
|
||||
Assert.SequenceEqual([(main.Id, "Main", true), (theirs.Id, "Main (2)", false)],
|
||||
(await service.Projects.ListAsync(sequence.Id, fx.Ct)).Select(p => (p.Id, p.Name, p.IsMain)));
|
||||
}
|
||||
Assert.Equal(1, MainRecords(folder));
|
||||
Assert.Equal(("Main (2)", false), (ProjectSidecars.ReadRecord(folder.ProjectRecordPath(theirs.Id)).Name, ProjectSidecars.ReadRecord(folder.ProjectRecordPath(theirs.Id)).IsMain));
|
||||
Assert.Count(1, await other.Projects.HistoryAsync(theirs.Id, cancellationToken: fx.Ct));
|
||||
Assert.Equal(pushed.Id, (await other.Projects.HeadAsync(main.Id, fx.Ct)).Id);
|
||||
Assert.Equal(PushOutcome.Stored, (await other.Projects.PushAsync(main.Id, Draft(pushed, new ProjectSettings { ExcludedFrames = [2] }, frames), fx.Ct)).Outcome);
|
||||
Assert.SequenceEqual([2], Settings(fx, sequence).ExcludedFrames);
|
||||
Assert.Equal(main.Id, (await projects.MainAsync(sequence.Id, fx.Ct)).Id);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task AProjectWhoseRecordIsLostIsWrittenAgainFromTheIndex()
|
||||
{
|
||||
var fx = await FixtureAsync();
|
||||
var (sequence, frames) = await UploadedAsync(fx, 3);
|
||||
var projects = fx.Service.Projects;
|
||||
var main = await projects.MainAsync(sequence.Id, fx.Ct);
|
||||
var vertical = await projects.CreateAsync(sequence.Id, "Vertical", cancellationToken: fx.Ct);
|
||||
var first = await projects.HeadAsync(vertical.Id, fx.Ct);
|
||||
var second = (await projects.PushAsync(vertical.Id, Draft(first, new ProjectSettings { ExcludedFrames = [1] }, frames), fx.Ct)).Version!;
|
||||
var mainSecond = (await projects.PushAsync(main.Id, Draft(await projects.HeadAsync(main.Id, fx.Ct), new ProjectSettings { ExcludedFrames = [2] }, frames), fx.Ct))
|
||||
.Version!;
|
||||
var folder = fx.Paths.Sequence(sequence.Folder);
|
||||
|
||||
// Version 3 of Vertical is in its folder (another PC's push), then both records and version 1's file are lost.
|
||||
var third = Draft(second, new ProjectSettings { ExcludedFrames = [0] }, frames) with { StoredAt = DateTimeOffset.UtcNow };
|
||||
ProjectSidecars.WriteVersion(folder.VersionPath(vertical.Id, 3, third.Id), third);
|
||||
File.Delete(folder.ProjectRecordPath(vertical.Id));
|
||||
File.Delete(folder.ProjectRecordPath(main.Id));
|
||||
File.Delete(folder.VersionPath(vertical.Id, 1, first.Id));
|
||||
|
||||
// Nothing is held back: the records are written again from the index, the version on disk past its head adopted.
|
||||
Assert.SequenceEqual([(main.Id, true, 2), (vertical.Id, false, 3)],
|
||||
(await projects.ListAsync(sequence.Id, fx.Ct)).Select(p => (p.Id, p.IsMain, p.HeadNumber)));
|
||||
var verticalRecord = ProjectSidecars.ReadRecord(folder.ProjectRecordPath(vertical.Id));
|
||||
Assert.Equal(("Vertical", third.Id), (verticalRecord.Name, verticalRecord.Head!.Id));
|
||||
var mainRecord = ProjectSidecars.ReadRecord(folder.ProjectRecordPath(main.Id));
|
||||
Assert.Equal((true, mainSecond.Id, mainSecond.Id), (mainRecord.IsMain, mainRecord.Head!.Id, mainRecord.Mirrored?.Id));
|
||||
Assert.FileExists(folder.VersionPath(vertical.Id, 1, first.Id));
|
||||
Assert.Equal(PushOutcome.Stored, (await projects.PushAsync(main.Id, Draft(mainSecond, new ProjectSettings { ExcludedFrames = [0] }, frames), fx.Ct)).Outcome);
|
||||
Assert.SequenceEqual([0], Settings(fx, sequence).ExcludedFrames);
|
||||
Assert.Equal(PushOutcome.Stored, (await projects.PushAsync(vertical.Id, Draft(third, new ProjectSettings(), frames), fx.Ct)).Outcome);
|
||||
|
||||
await fx.ReopenWithEmptyIndexAsync();
|
||||
await fx.ScanAsync(ScanMode.Rebuild);
|
||||
Assert.SequenceEqual([4, 3, 2, 1], (await fx.Service.Projects.HistoryAsync(vertical.Id, cancellationToken: fx.Ct)).Select(v => v.Number));
|
||||
Assert.SequenceEqual([3, 2, 1], (await fx.Service.Projects.HistoryAsync(main.Id, cancellationToken: fx.Ct)).Select(v => v.Number));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task ARenameMadeAfterABackupStaysWhenTheBackupIsPutBack()
|
||||
{
|
||||
var fx = await FixtureAsync();
|
||||
var (sequence, frames) = await UploadedAsync(fx, 3);
|
||||
var projects = fx.Service.Projects;
|
||||
var vertical = await projects.CreateAsync(sequence.Id, "Vertical", cancellationToken: fx.Ct);
|
||||
var folder = fx.Paths.Sequence(sequence.Folder);
|
||||
var backup = folder.ProjectsDirectory + "-backup";
|
||||
CopyDirectory(folder.ProjectsDirectory, backup);
|
||||
await projects.PushAsync(vertical.Id, Draft(await projects.HeadAsync(vertical.Id, fx.Ct), new ProjectSettings { ExcludedFrames = [1] }, frames), fx.Ct);
|
||||
await projects.RenameAsync(vertical.Id, "Square", fx.Ct);
|
||||
|
||||
// The backup made before is put back: its record is behind the index and names the old name.
|
||||
Directory.Delete(folder.ProjectsDirectory, recursive: true);
|
||||
CopyDirectory(backup, folder.ProjectsDirectory);
|
||||
Assert.Equal("Square", (await projects.ListAsync(sequence.Id, fx.Ct)).Single(p => p.Id == vertical.Id).Name);
|
||||
var record = ProjectSidecars.ReadRecord(folder.ProjectRecordPath(vertical.Id));
|
||||
Assert.Equal(("Square", 2), (record.Name, record.Head!.Number));
|
||||
|
||||
await fx.ReopenWithEmptyIndexAsync();
|
||||
await fx.ScanAsync(ScanMode.Rebuild);
|
||||
Assert.Equal("Square", (await fx.Service.Projects.ListAsync(sequence.Id, fx.Ct)).Single(p => p.Id == vertical.Id).Name);
|
||||
}
|
||||
|
||||
[Test]
|
||||
|
||||
Reference in new issue
Block a user