CI / Build and test (push) Successful in 9m43s
CI / Windows tests and installer (push) Skipped
CI / YAML check (push) Successful in 27s
CI / PostgreSQL 16 integration (push) Failing after 8m41s
CI / PostgreSQL 17 integration (push) Successful in 12m37s
CI / Docker image (push) Successful in 56s
Release / Build, test and publish (push) Successful in 14m59s
Release / Server image (push) Successful in 1m51s
Edit opens a sequence as a project of a workspace folder outside the library. Projects have linear version histories kept as sidecars in .chronos/projects/, Main mirrors the sequence's settings, and sync merges setting by setting. Migration 0009, JobKind.WorkspaceCopy = 20, Settings › Workspace, and 241 new strings in 38 languages. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Co-authored-by: Alberto Balbo <alberto.balbo.96@gmail.com>
206 lines
12 KiB
C#
206 lines
12 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 cheap stamp of what <see cref="FramesAsync"/> answers: another value whenever the frames, how their photos are
|
|
/// named (a hash learnt) or the encodings conversions joined may have changed, so a workspace reads the frames again only
|
|
/// then. Null from a home that keeps none: the frames are read every time.
|
|
/// </summary>
|
|
Task<string?> FramesStampAsync(CancellationToken cancellationToken) => Task.FromResult<string?>(null);
|
|
}
|
|
|
|
/// <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 sequence's frames version (frames added, removed, converted or renamed), with how many of its photos the index has
|
|
/// hashed and a checksum of those hashes (a Verify hashes them, or finds other bytes, without another frames version)
|
|
/// and how many removals it keeps (a conversion's links).
|
|
/// </summary>
|
|
public async Task<string?> FramesStampAsync(CancellationToken cancellationToken)
|
|
{
|
|
var row = await db.Sequences.GetAsync(sequenceId, cancellationToken).ConfigureAwait(false)
|
|
?? throw new ProjectException(ProjectError.SequenceNotFound, $"Sequence {sequenceId} is not in the index.");
|
|
var (hashed, checksum, removals) = await db.Frames.CountNamedAsync(sequenceId, cancellationToken).ConfigureAwait(false);
|
|
return string.Create(System.Globalization.CultureInfo.InvariantCulture, $"{row.FramesVersion}:{hashed}:{checksum}:{removals}");
|
|
}
|
|
|
|
/// <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);
|
|
}
|
|
}
|
|
}
|