The sidecars stay the truth of a sequence's projects in the cases round 1's checkers found still open, and no version or setting is lost on the way: - An index-only Main meeting a Main another index made is written back as another project, and Main passes to theirs in one transaction through a free temporary name, so ux_projects_name and ux_projects_main never fail (the SqliteException used to escape ListAsync and leave the index and the sidecars disagreeing). A refusal is logged; the next reconcile settles it. - The reconcile takes Main from the records, in step with the index, when another index passed it on: exactly one Main per sequence, names from the records where free. - A project folder with versions and no record, of a project no index row names (a creation cut before its record), no longer holds the sequence back forever: it is left as it is and indexed once its record arrives. - A record behind the index (a projects folder put back from an older backup) is made whole from the index: its missing versions and the record are written again, at every catch-up and before any append, rename or delete. Only a record ahead of the index or naming another version is refused, with a message that says so. - An index meeting Main walks the chain down to the version the record names as mirrored (a bound, since that record write may lag project.json), so a lagging record never makes an External that reverts a newer push. When the record names a mirrored ancestor, settings written since merge with the unmirrored versions, as for an index that knows its mirror. - The catch-up merge's External + Merge pair is resumable: an External head whose parent is a push no mirror wrote is finished into its Merge version next time, by this index or one rebuilt from the sidecars. - A project deleted at the home has no head there, so a workspace copy of it reads Gone, never In sync; Remove local copy without discard says the project was deleted in the library. - DatabaseExceptions are contained per sequence in the archive scan's project reconcile and in ProjectService reads. Tests: SQLite (PostgreSQL-ready) for each scenario, including a cut between the two catch-up versions, a 40,000-exclusion version through save, push and rebuild, and the App's Remove local copy of a project deleted at home. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
186 lines
10 KiB
C#
186 lines
10 KiB
C#
using Chronos.Core.Archiving;
|
|
using Chronos.Core.Model;
|
|
using Chronos.Core.Project;
|
|
using Chronos.Data.Archiving;
|
|
|
|
namespace Chronos.Data.Projects;
|
|
|
|
/// <summary>A project's home cannot be reached now (its archive offline or busy, its server out of reach): try again later.</summary>
|
|
public sealed class HomeUnavailableException(string message, Exception? inner = null) : IOException(message, inner);
|
|
|
|
/// <summary>The sequence a home keeps projects for, as a workspace's project file names it.</summary>
|
|
public sealed record HomeSequence(BundleArchive Archive, BundleSequence Sequence, BundleHome Home);
|
|
|
|
/// <summary>
|
|
/// Where a sequence's projects live (owner review 2, R8): the side a workspace pushes to and pulls from, "like git". This
|
|
/// PC's archive is one (<see cref="ArchiveProjectHome"/>); a server is the other (phase 5b). Every call may throw
|
|
/// <see cref="HomeUnavailableException"/> when the home cannot be reached, and <see cref="ProjectException"/> when it refuses.
|
|
/// </summary>
|
|
public interface IProjectHome
|
|
{
|
|
/// <summary>The sequence and where it is.</summary>
|
|
Task<HomeSequence> SequenceAsync(CancellationToken cancellationToken);
|
|
|
|
/// <summary>The sequence's live projects, Main first (made when the sequence had none).</summary>
|
|
Task<IReadOnlyList<SequenceProject>> ListAsync(CancellationToken cancellationToken);
|
|
|
|
/// <summary>A new project named <paramref name="name"/> from <paramref name="fromVersionId"/> (a version of any project of the sequence), or from Main's head.</summary>
|
|
Task<SequenceProject> CreateAsync(string name, Guid? fromVersionId, string? author, CancellationToken cancellationToken);
|
|
|
|
/// <summary>
|
|
/// The project's newest version; a project deleted at the home has none (<see cref="ProjectException"/>,
|
|
/// <see cref="ProjectError.NotFound"/>), so a workspace's copy of it reads as gone, never as in step.
|
|
/// </summary>
|
|
Task<ProjectVersion> HeadAsync(Guid projectId, CancellationToken cancellationToken);
|
|
|
|
/// <summary>A version of the project by id; null when the home has none.</summary>
|
|
Task<ProjectVersion?> GetVersionAsync(Guid projectId, Guid versionId, CancellationToken cancellationToken);
|
|
|
|
/// <summary>
|
|
/// Whether the home holds <paramref name="version"/> durably, read back from where it keeps it and matching its content
|
|
/// hash: what a workspace asks before it drops its own copy. A home that keeps no other copy answers from
|
|
/// <see cref="GetVersionAsync"/>.
|
|
/// </summary>
|
|
async Task<bool> HoldsAsync(Guid projectId, ProjectVersion version, CancellationToken cancellationToken)
|
|
{
|
|
ArgumentNullException.ThrowIfNull(version);
|
|
return await GetVersionAsync(projectId, version.Id, cancellationToken).ConfigureAwait(false) is { } held
|
|
&& string.Equals(held.ContentHash, version.ContentHash, StringComparison.OrdinalIgnoreCase) && ProjectContent.Verifies(held.SettingsJson, held.ContentHash);
|
|
}
|
|
|
|
/// <summary>The versions numbered above <paramref name="afterNumber"/>, oldest first, at most <paramref name="limit"/>.</summary>
|
|
Task<IReadOnlyList<ProjectVersion>> VersionsAfterAsync(Guid projectId, int afterNumber, int limit, CancellationToken cancellationToken);
|
|
|
|
/// <summary>One page of the history, newest first: the versions numbered below <paramref name="beforeNumber"/> (from the head when null).</summary>
|
|
Task<IReadOnlyList<ProjectVersion>> HistoryAsync(Guid projectId, int? beforeNumber, int limit, CancellationToken cancellationToken);
|
|
|
|
/// <summary>Stores a version made from the head (<see cref="ProjectService.PushAsync"/>).</summary>
|
|
Task<PushResult> PushAsync(Guid projectId, ProjectVersion version, CancellationToken cancellationToken);
|
|
|
|
/// <summary>The sequence's frames as the home holds them now.</summary>
|
|
Task<HomeFrames> FramesAsync(CancellationToken cancellationToken);
|
|
}
|
|
|
|
/// <summary>A sequence of this PC's archive as its projects' home: the archive's <see cref="ProjectService"/>.</summary>
|
|
public sealed class ArchiveProjectHome(ChronosDatabase db, ProjectService projects, Guid sequenceId) : IProjectHome
|
|
{
|
|
public Guid SequenceId => sequenceId;
|
|
|
|
public async Task<HomeSequence> SequenceAsync(CancellationToken cancellationToken)
|
|
{
|
|
var sequence = await db.Sequences.GetAsync(sequenceId, cancellationToken).ConfigureAwait(false)
|
|
?? throw new ProjectException(ProjectError.SequenceNotFound, $"Sequence {sequenceId} is not in the index.");
|
|
var archive = await db.Archives.GetAsync(sequence.ArchiveId, cancellationToken).ConfigureAwait(false);
|
|
return new HomeSequence(new BundleArchive(sequence.ArchiveId, archive?.Name ?? string.Empty, archive?.RootPath),
|
|
new BundleSequence(sequence.Id, sequence.Folder, sequence.DisplayTitle, sequence.FrameCount),
|
|
new BundleHome(BundleHome.ArchiveKind, sequence.Id, sequence.ArchiveId));
|
|
}
|
|
|
|
public Task<IReadOnlyList<SequenceProject>> ListAsync(CancellationToken cancellationToken) =>
|
|
Reach(async () =>
|
|
{
|
|
// Made at once when the sequence has none, so every list has a Main.
|
|
await projects.MainAsync(sequenceId, cancellationToken).ConfigureAwait(false);
|
|
return await projects.ListAsync(sequenceId, cancellationToken).ConfigureAwait(false);
|
|
});
|
|
|
|
public Task<SequenceProject> CreateAsync(string name, Guid? fromVersionId, string? author, CancellationToken cancellationToken) =>
|
|
Reach(() => projects.CreateAsync(sequenceId, name, fromVersionId, author, cancellationToken));
|
|
|
|
public Task<ProjectVersion> HeadAsync(Guid projectId, CancellationToken cancellationToken) =>
|
|
Reach(async () =>
|
|
{
|
|
await Own(projectId, cancellationToken).ConfigureAwait(false);
|
|
return await projects.HeadAsync(projectId, cancellationToken).ConfigureAwait(false);
|
|
});
|
|
|
|
public Task<ProjectVersion?> GetVersionAsync(Guid projectId, Guid versionId, CancellationToken cancellationToken) =>
|
|
Reach(async () =>
|
|
{
|
|
if (await projects.GetVersionAsync(versionId, cancellationToken).ConfigureAwait(false) is not { } version)
|
|
{
|
|
// A version the index does not know yet may be in the sequence's sidecars (see Own): they are read first.
|
|
await projects.ListAsync(sequenceId, cancellationToken).ConfigureAwait(false);
|
|
version = await projects.GetVersionAsync(versionId, cancellationToken).ConfigureAwait(false);
|
|
}
|
|
return version is not null && version.ProjectId == projectId ? version : null;
|
|
});
|
|
|
|
/// <summary>Read back from the version's file in the archive, never from the index's row (<see cref="ProjectService.StoredAsync"/>).</summary>
|
|
public Task<bool> HoldsAsync(Guid projectId, ProjectVersion version, CancellationToken cancellationToken) =>
|
|
Reach(async () =>
|
|
{
|
|
ArgumentNullException.ThrowIfNull(version);
|
|
var project = await Own(projectId, cancellationToken).ConfigureAwait(false);
|
|
return project.DeletedAt is null && ProjectContent.Verifies(version.SettingsJson, version.ContentHash)
|
|
&& await projects.StoredAsync(projectId, version, cancellationToken).ConfigureAwait(false);
|
|
});
|
|
|
|
public Task<IReadOnlyList<ProjectVersion>> VersionsAfterAsync(Guid projectId, int afterNumber, int limit, CancellationToken cancellationToken) =>
|
|
Reach(async () =>
|
|
{
|
|
await Own(projectId, cancellationToken).ConfigureAwait(false);
|
|
return await projects.VersionsAfterAsync(projectId, afterNumber, limit, cancellationToken).ConfigureAwait(false);
|
|
});
|
|
|
|
public Task<IReadOnlyList<ProjectVersion>> HistoryAsync(Guid projectId, int? beforeNumber, int limit, CancellationToken cancellationToken) =>
|
|
Reach(async () =>
|
|
{
|
|
await Own(projectId, cancellationToken).ConfigureAwait(false);
|
|
return await projects.HistoryAsync(projectId, beforeNumber, limit, cancellationToken).ConfigureAwait(false);
|
|
});
|
|
|
|
public Task<PushResult> PushAsync(Guid projectId, ProjectVersion version, CancellationToken cancellationToken) =>
|
|
Reach(async () =>
|
|
{
|
|
await Own(projectId, cancellationToken).ConfigureAwait(false);
|
|
return await projects.PushAsync(projectId, version, cancellationToken).ConfigureAwait(false);
|
|
});
|
|
|
|
public Task<HomeFrames> FramesAsync(CancellationToken cancellationToken) => projects.FramesAsync(sequenceId, cancellationToken);
|
|
|
|
/// <summary>
|
|
/// The project is one of this sequence's. One the index does not know yet (an index taken back from an older backup, a
|
|
/// sequence back from the trash, a project another program sharing the archive made) is looked for in the sequence's
|
|
/// sidecars first, which every listing brings into the index.
|
|
/// </summary>
|
|
private async Task<SequenceProject> Own(Guid projectId, CancellationToken cancellationToken)
|
|
{
|
|
if (await projects.GetAsync(projectId, cancellationToken).ConfigureAwait(false) is not { } project)
|
|
{
|
|
await projects.ListAsync(sequenceId, cancellationToken).ConfigureAwait(false);
|
|
project = await projects.GetAsync(projectId, cancellationToken).ConfigureAwait(false);
|
|
}
|
|
return project is not null && project.SequenceId == sequenceId
|
|
? project
|
|
: throw new ProjectException(ProjectError.NotFound, $"Project {projectId} is not a project of sequence {sequenceId}.");
|
|
}
|
|
|
|
/// <summary>An archive offline, read-only or busy is a home out of reach for now.</summary>
|
|
private static async Task<T> Reach<T>(Func<Task<T>> call)
|
|
{
|
|
try
|
|
{
|
|
return await call().ConfigureAwait(false);
|
|
}
|
|
catch (ArchiveOfflineException ex)
|
|
{
|
|
throw new HomeUnavailableException(ex.Message, ex);
|
|
}
|
|
catch (LeaseUnavailableException ex)
|
|
{
|
|
throw new HomeUnavailableException(ex.Message, ex);
|
|
}
|
|
catch (SequenceFolderMovedException ex)
|
|
{
|
|
// Another index of the archive moved the sequence: this one finds it at its next scan.
|
|
throw new HomeUnavailableException(ex.Message, ex);
|
|
}
|
|
catch (ProjectsUnreadableException ex)
|
|
{
|
|
// A record or a version this Chronos cannot read now (damaged, held open, of a newer Chronos): nothing was written.
|
|
throw new HomeUnavailableException(ex.Message, ex);
|
|
}
|
|
}
|
|
}
|