feat(projects): workspace projects with linear versions, the local archive as their home

Owner review 2, phase 5a (R8): editing moves out of the library into a workspace on a fast disk, and a
sequence gets several projects, each with a linear version history pushed and pulled like git.

- Core: ProjectSettings keeps members a newer Chronos wrote (Extra) and reads a newer settings format
  read-only; ProjectContent is the portable form of a version (every frame position parked by photo,
  canonical text, SHA-256 content hash); ProjectBundle format 2 adds the workspace section, and a file
  without it is still written as format 1. JobKind.WorkspaceCopy = 20 (19 kept for Convert).
- Data: migration 0009 (projects, project_versions, the render's project columns); ProjectService on
  ArchiveService.Projects keeps projects as sidecars first (.chronos/projects/<id>/), then the index,
  under the sequence lease: create, rename, delete into the projects' trash, history, push with
  fast-forward, idempotent repeat, divergence and hash checks. Main is made from the sequence's settings
  and mirrored both ways, so every older code path keeps working; outside writes become "external"
  versions, a mirror a crash cut short is completed or merged. Full scans and rebuilds take the projects
  back from their sidecars.
- ProjectMerge moves from the App to Data/Projects, merges pending frames photo by photo and replays
  unpushed commits on a moved head.
- Workspaces: WorkspaceStore, WorkspaceSync (checkout, save, commit, push, pull with replay and merge,
  conflicts settled by keep mine / take server / save both, restore, status) and the WorkspaceCopy job
  (proxies copied hash-verified and resumable; originals refused until phase 5c).
- Renders of a project version place its positions on the frames and record the project and version.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
(cherry picked from commit c4c1802884e403db8759ed0501f770a67a047f20)
This commit is contained in:
Alby96 committed 2026-10-27 22:51:32 +01:00
1 parent e3cbe30b29
commit 03d32f18d3
51 files changed
+4917 -108

No files matched your search

+588
View File
@@ -0,0 +1,588 @@
using System.Text.Json;
using System.Text.Json.Nodes;
using Chronos.Core.Archiving;
using Chronos.Core.Diagnostics;
using Chronos.Core.IO;
using Chronos.Core.Model;
using Chronos.Core.Project;
using Chronos.Data.Archiving;
using Chronos.Data.Repositories;
namespace Chronos.Data.Projects;
/// <summary>Why a project operation was refused (<see cref="ProjectException.Reason"/>).</summary>
public enum ProjectError
{
/// <summary>The sequence is not in the index.</summary>
SequenceNotFound,
/// <summary>The project (or the version named) does not exist, or was deleted.</summary>
NotFound,
/// <summary>The name is blank or longer than <see cref="ProjectService.MaxNameLength"/>.</summary>
InvalidName,
/// <summary>Another project of the sequence has that name (case does not count).</summary>
NameTaken,
/// <summary>The Main project cannot be deleted: it holds the sequence's own settings.</summary>
MainCannotBeDeleted,
}
/// <summary>A project operation was refused; nothing was written.</summary>
public sealed class ProjectException(ProjectError reason, string message) : InvalidOperationException(message)
{
public ProjectError Reason { get; } = reason;
}
/// <summary>What became of a version pushed to its home (<see cref="ProjectService.PushAsync"/>).</summary>
public enum PushOutcome
{
/// <summary>Stored as the project's new head (its parent was the head: a fast-forward).</summary>
Stored,
/// <summary>A version with that id is stored already (a push repeated after its answer was lost): nothing changed.</summary>
AlreadyStored,
/// <summary>The head moved since the version's parent: pull the head, replay the version on it and push again.</summary>
Diverged,
/// <summary>The settings do not match the content hash sent with them: nothing was stored.</summary>
HashMismatch,
/// <summary>The settings are of a newer format than this home reads: nothing was stored.</summary>
NewerFormat,
/// <summary>Not a version of this project: settings that are not portable (they name frames by position), or an id another project holds.</summary>
Invalid,
}
/// <summary>The answer to a push: the outcome, the version as stored (<see cref="PushOutcome.Stored"/>, <see cref="PushOutcome.AlreadyStored"/>) and the project's head.</summary>
public sealed record PushResult(PushOutcome Outcome, ProjectVersion? Version, ProjectVersion? Head);
/// <summary>
/// The frames of a sequence at its home, as project settings name them: their frames version, rows in index order and
/// photos (<see cref="FramePhotos"/>, for <see cref="ProjectContent"/>).
/// </summary>
public sealed record HomeFrames(int FramesVersion, IReadOnlyList<Frame> Frames, FramePhotos Photos);
/// <summary>
/// The projects of the sequences of this PC's archives (owner review 2, R8): the home side of a workspace's push and pull.
/// 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
/// (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
/// projects stay on this home: copies of a sequence linked to a server sync only its settings, i.e. Main.
/// </summary>
public sealed class ProjectService
{
/// <summary>The name of the project made for every sequence (the desktop shows it in the UI language).</summary>
public const string MainName = "Main";
public const int MaxNameLength = 60;
/// <summary>Frames read at once when a sequence's photos are gathered.</summary>
private const int PageSize = 1000;
/// <summary>How long a read waits for a sequence another writer holds before it answers from the index alone.</summary>
private static readonly TimeSpan ReadWait = TimeSpan.FromSeconds(3);
private readonly ChronosDatabase _db;
private readonly SequenceWriter _writer;
private readonly ProjectIndexer _indexer;
internal ProjectService(ChronosDatabase db, SequenceWriter writer)
{
_db = db;
_writer = writer;
_indexer = new ProjectIndexer(db);
}
/// <summary>The client name versions made here carry (the archive engine's lease owner).</summary>
public string ClientName => _writer.Options.Owner;
/// <summary>The live projects of a sequence, Main first; read from the index alone while the archive is offline.</summary>
/// <exception cref="ProjectException">The sequence is not in the index.</exception>
public async Task<IReadOnlyList<SequenceProject>> ListAsync(Guid sequenceId, CancellationToken cancellationToken = default)
{
await ReadAsync(sequenceId, cancellationToken).ConfigureAwait(false);
return await _db.Projects.ListBySequenceAsync(sequenceId, includeDeleted: false, cancellationToken).ConfigureAwait(false);
}
/// <summary>The sequence's Main project, made now (from its settings) when it has none.</summary>
/// <exception cref="ProjectException">The sequence is not in the index.</exception>
/// <exception cref="ArchiveOfflineException">The archive is offline and the index has no Main project yet.</exception>
public async Task<SequenceProject> MainAsync(Guid sequenceId, CancellationToken cancellationToken = default)
{
await ReadAsync(sequenceId, cancellationToken).ConfigureAwait(false);
if (await _db.Projects.GetMainAsync(sequenceId, cancellationToken).ConfigureAwait(false) is { } main) return main;
// The catch-up could not run (the archive offline, the sequence busy): it runs now, or says why it cannot.
return await UnderLeaseAsync(sequenceId, async (_, ct) => (await _db.Projects.GetMainAsync(sequenceId, ct).ConfigureAwait(false))!, cancellationToken)
.ConfigureAwait(false);
}
public Task<SequenceProject?> GetAsync(Guid projectId, CancellationToken cancellationToken = default) => _db.Projects.GetAsync(projectId, cancellationToken);
/// <summary>
/// A new project of the sequence named <paramref name="name"/>, whose first version (kind "imported") holds the settings of
/// <paramref name="fromVersionId"/> (a version of any project of the sequence), or of Main's head.
/// </summary>
/// <exception cref="ProjectException">Unknown sequence or version, or the name is invalid or taken.</exception>
public Task<SequenceProject> CreateAsync(Guid sequenceId, string name, Guid? fromVersionId = null, string? author = null,
CancellationToken cancellationToken = default)
{
var trimmed = ValidName(name);
return UnderLeaseAsync(sequenceId, async (context, ct) =>
{
await EnsureNameFreeAsync(sequenceId, null, trimmed, ct).ConfigureAwait(false);
ProjectVersion source;
if (fromVersionId is { } from)
{
source = await _db.Projects.GetVersionAsync(from, ct).ConfigureAwait(false) is { } version
&& await _db.Projects.GetAsync(version.ProjectId, ct).ConfigureAwait(false) is { SequenceId: var owner } && owner == sequenceId
? version
: throw new ProjectException(ProjectError.NotFound, $"Version {from} is not a version of sequence {sequenceId}.");
}
else
{
var main = (await _db.Projects.GetMainAsync(sequenceId, ct).ConfigureAwait(false))!;
source = (await _db.Projects.GetVersionAsync(main.HeadVersionId!.Value, ct).ConfigureAwait(false))!;
}
var now = DateTimeOffset.UtcNow;
var project = new SequenceProject
{
Id = Guid.CreateVersion7(),
SequenceId = sequenceId,
Name = trimmed,
CreatedBy = author,
CreatedAt = now,
UpdatedAt = now,
};
var first = new ProjectVersion
{
Id = Guid.CreateVersion7(),
ProjectId = project.Id,
Number = 1,
Kind = ProjectVersionKind.Imported,
Author = author,
ClientName = ClientName,
SettingsJson = source.SettingsJson,
ContentHash = source.ContentHash,
SettingsFormat = source.SettingsFormat,
FramesVersion = source.FramesVersion,
CreatedAt = now,
StoredAt = now,
};
return await AddProjectAsync(context.Folder, project, first, ct).ConfigureAwait(false);
}, cancellationToken);
}
/// <summary>Gives a project another name.</summary>
/// <exception cref="ProjectException">Unknown project, or the name is invalid or taken.</exception>
public async Task<SequenceProject> RenameAsync(Guid projectId, string name, CancellationToken cancellationToken = default)
{
var trimmed = ValidName(name);
var project = await LiveAsync(projectId, cancellationToken).ConfigureAwait(false);
return await UnderLeaseAsync(project.SequenceId, async (context, ct) =>
{
var current = await LiveAsync(projectId, ct).ConfigureAwait(false);
if (current.Name == trimmed) return current;
await EnsureNameFreeAsync(current.SequenceId, projectId, trimmed, ct).ConfigureAwait(false);
var renamed = current with { Name = trimmed, UpdatedAt = DateTimeOffset.UtcNow };
ProjectSidecars.WriteRecord(context.Folder.ProjectRecordPath(projectId), ProjectRecord.Of(renamed));
await _db.Projects.UpdateAsync(renamed, ct).ConfigureAwait(false);
return renamed;
}, cancellationToken).ConfigureAwait(false);
}
/// <summary>
/// Deletes a project: its record says so, its folder moves to <c>.chronos/projects/.trash</c> (kept, never erased by
/// Chronos) and it leaves the listing.
/// </summary>
/// <exception cref="ProjectException">Unknown project, or the Main project.</exception>
public async Task DeleteAsync(Guid projectId, CancellationToken cancellationToken = default)
{
var project = await LiveAsync(projectId, cancellationToken).ConfigureAwait(false);
if (project.IsMain) throw new ProjectException(ProjectError.MainCannotBeDeleted, "The Main project cannot be deleted.");
await UnderLeaseAsync(project.SequenceId, async (context, ct) =>
{
var current = await LiveAsync(projectId, ct).ConfigureAwait(false);
var now = DateTimeOffset.UtcNow;
var deleted = current with { DeletedAt = now, UpdatedAt = now };
ProjectSidecars.WriteRecord(context.Folder.ProjectRecordPath(projectId), ProjectRecord.Of(deleted));
Directory.CreateDirectory(context.Folder.ProjectsTrashDirectory);
Directory.Move(context.Folder.ProjectDirectory(projectId),
Path.Combine(context.Folder.ProjectsTrashDirectory, projectId.ToString("N") + "-" + now.UtcTicks.ToString(System.Globalization.CultureInfo.InvariantCulture)));
await _db.Projects.UpdateAsync(deleted, ct).ConfigureAwait(false);
return true;
}, cancellationToken).ConfigureAwait(false);
}
/// <summary>One page of a project's history, newest first: the versions numbered below <paramref name="beforeNumber"/> (from the head when null).</summary>
/// <exception cref="ProjectException">Unknown project.</exception>
public async Task<IReadOnlyList<ProjectVersion>> HistoryAsync(Guid projectId, int? beforeNumber = null, int limit = 50, CancellationToken cancellationToken = default)
{
var project = await KnownAsync(projectId, cancellationToken).ConfigureAwait(false);
await ReadAsync(project.SequenceId, cancellationToken).ConfigureAwait(false);
return await _db.Projects.ListVersionsAsync(projectId, beforeNumber, limit, cancellationToken).ConfigureAwait(false);
}
/// <summary>The versions numbered above <paramref name="afterNumber"/>, oldest first: what a workspace pulls.</summary>
/// <exception cref="ProjectException">Unknown project.</exception>
public async Task<IReadOnlyList<ProjectVersion>> VersionsAfterAsync(Guid projectId, int afterNumber, int limit = 100, CancellationToken cancellationToken = default)
{
var project = await KnownAsync(projectId, cancellationToken).ConfigureAwait(false);
await ReadAsync(project.SequenceId, cancellationToken).ConfigureAwait(false);
return await _db.Projects.ListVersionsAfterAsync(projectId, afterNumber, limit, cancellationToken).ConfigureAwait(false);
}
/// <summary>A version by id; null when the index has none.</summary>
public Task<ProjectVersion?> GetVersionAsync(Guid versionId, CancellationToken cancellationToken = default) =>
_db.Projects.GetVersionAsync(versionId, cancellationToken);
/// <summary>The newest version of a project, after catching up.</summary>
/// <exception cref="ProjectException">Unknown project.</exception>
public async Task<ProjectVersion> HeadAsync(Guid projectId, CancellationToken cancellationToken = default)
{
var project = await KnownAsync(projectId, cancellationToken).ConfigureAwait(false);
await ReadAsync(project.SequenceId, cancellationToken).ConfigureAwait(false);
project = await KnownAsync(projectId, cancellationToken).ConfigureAwait(false);
return await _db.Projects.GetVersionAsync(project.HeadVersionId ?? Guid.Empty, cancellationToken).ConfigureAwait(false)
?? throw new ProjectException(ProjectError.NotFound, $"Project {projectId} has no version.");
}
/// <summary>
/// Stores <paramref name="version"/> (made in a workspace from its <see cref="ProjectVersion.ParentId"/>) as the
/// project's new head when its parent is the head: its number is the head's plus one, whatever it says. Repeated with
/// the id of a stored version, it answers as the first time. Its settings must be portable (positions parked by photo)
/// and match their content hash. A version of Main is mirrored into the sequence's settings.
/// </summary>
/// <exception cref="ProjectException">Unknown project.</exception>
public async Task<PushResult> PushAsync(Guid projectId, ProjectVersion version, CancellationToken cancellationToken = default)
{
ArgumentNullException.ThrowIfNull(version);
var project = await LiveAsync(projectId, cancellationToken).ConfigureAwait(false);
return await UnderLeaseAsync(project.SequenceId, async (context, ct) =>
{
var current = await LiveAsync(projectId, ct).ConfigureAwait(false);
var head = await _db.Projects.GetVersionAsync(current.HeadVersionId ?? Guid.Empty, ct).ConfigureAwait(false);
if (await _db.Projects.GetVersionAsync(version.Id, ct).ConfigureAwait(false) is { } existing)
{
return existing.ProjectId == projectId && string.Equals(existing.ContentHash, version.ContentHash, StringComparison.OrdinalIgnoreCase)
? new PushResult(PushOutcome.AlreadyStored, existing, head)
: new PushResult(PushOutcome.Invalid, null, head);
}
if (!ProjectContent.Verifies(version.SettingsJson, version.ContentHash)) return new PushResult(PushOutcome.HashMismatch, null, head);
var format = ProjectContent.FormatOf(version.SettingsJson);
if (format > ProjectSettings.CurrentVersion) return new PushResult(PushOutcome.NewerFormat, null, head);
if (!IsPortable(version.SettingsJson)) return new PushResult(PushOutcome.Invalid, null, head);
if (version.ParentId != current.HeadVersionId) return new PushResult(PushOutcome.Diverged, null, head);
var stored = version with
{
ProjectId = projectId,
Number = current.HeadNumber + 1,
ContentHash = version.ContentHash.ToLowerInvariant(),
SettingsFormat = format,
StoredAt = DateTimeOffset.UtcNow,
};
current = await AppendAsync(context.Folder, current, stored, ct).ConfigureAwait(false);
if (current.IsMain) await MirrorAsync(context, current, stored, ct).ConfigureAwait(false);
return new PushResult(PushOutcome.Stored, stored, stored);
}, cancellationToken).ConfigureAwait(false);
}
/// <summary>The frames of a sequence as they are indexed now, with its frames version.</summary>
/// <exception cref="ProjectException">The sequence is not in the index.</exception>
public async Task<HomeFrames> FramesAsync(Guid sequenceId, CancellationToken cancellationToken = default)
{
for (var attempt = 0; ; attempt++)
{
var before = await _db.Sequences.GetAsync(sequenceId, cancellationToken).ConfigureAwait(false) ?? throw SequenceMissing(sequenceId);
var frames = await LoadFramesAsync(sequenceId, cancellationToken).ConfigureAwait(false);
var after = await _db.Sequences.GetAsync(sequenceId, cancellationToken).ConfigureAwait(false) ?? throw SequenceMissing(sequenceId);
if (after.FramesVersion == before.FramesVersion || attempt == 2) return new HomeFrames(after.FramesVersion, frames.Frames, frames.Photos);
}
}
/// <summary>
/// Catches the sequence's projects up under its lease (see the class remarks) before a read; the index alone answers while
/// the archive is offline or the sequence busy for longer than the lease wait.
/// </summary>
private async Task ReadAsync(Guid sequenceId, CancellationToken cancellationToken)
{
try
{
await UnderLeaseAsync(sequenceId, (_, _) => Task.FromResult(true), cancellationToken, ReadWait).ConfigureAwait(false);
}
catch (Exception ex) when (ex is ArchiveOfflineException or LeaseUnavailableException)
{
Log.Info("projects", $"The projects of sequence {sequenceId} are read from the index: {ex.Message}");
}
}
/// <summary>Runs <paramref name="work"/> under the sequence's lease, after catching its projects up.</summary>
private async Task<T> UnderLeaseAsync<T>(Guid sequenceId, Func<LeaseContext, CancellationToken, Task<T>> work, CancellationToken cancellationToken,
TimeSpan? wait = null)
{
var sequence = await _db.Sequences.GetAsync(sequenceId, cancellationToken).ConfigureAwait(false) ?? throw SequenceMissing(sequenceId);
var archive = await _db.Archives.GetAsync(sequence.ArchiveId, cancellationToken).ConfigureAwait(false) ?? throw SequenceMissing(sequenceId);
var paths = new ArchivePaths(archive.RootPath);
ArchiveVolume.EnsureWritable(paths);
await using (await _writer.LockAsync(paths, sequenceId, wait, cancellationToken).ConfigureAwait(false))
{
var row = await _db.Sequences.GetAsync(sequenceId, cancellationToken).ConfigureAwait(false) ?? throw SequenceMissing(sequenceId);
var context = new LeaseContext(paths.Sequence(row.Folder), row);
await CatchUpAsync(context, cancellationToken).ConfigureAwait(false);
return await work(context, cancellationToken).ConfigureAwait(false);
}
}
/// <summary>The sidecars into the index, the Main project made, and Main in step with the sequence's settings.</summary>
private async Task CatchUpAsync(LeaseContext context, CancellationToken cancellationToken)
{
await _indexer.ReconcileAsync(context.Folder, context.Row.Id, cancellationToken).ConfigureAwait(false);
var main = await _db.Projects.GetMainAsync(context.Row.Id, cancellationToken).ConfigureAwait(false);
if (main?.HeadVersionId is null)
{
await MakeMainAsync(context, main, cancellationToken).ConfigureAwait(false);
return;
}
if (main.MirroredVersionId == main.HeadVersionId && main.MirroredSettingsVersion == context.Row.SettingsVersion) return;
var head = (await _db.Projects.GetVersionAsync(main.HeadVersionId.Value, cancellationToken).ConfigureAwait(false))!;
var frames = await LoadFramesAsync(context.Row.Id, cancellationToken).ConfigureAwait(false);
if (CurrentPortable(context, frames.Photos) is not { } current)
{
// Settings no Chronos can read (or of a newer one): left as they are, recorded as seen.
await _db.Projects.SetMirroredAsync(main.Id, main.HeadVersionId, context.Row.SettingsVersion, cancellationToken).ConfigureAwait(false);
return;
}
var sameAsHead = ProjectContent.Hash(current) == head.ContentHash;
if (main.MirroredVersionId == main.HeadVersionId || main.MirroredVersionId is null)
{
// Only the sequence's settings version moved (settings another path wrote, or only its title or tags), or the index
// was rebuilt and knows no mirror: 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);
return;
}
// The head is newer than the mirror: a push whose mirror a crash (or a settings edit at the same moment) cut short.
if (sameAsHead)
{
await _db.Projects.SetMirroredAsync(main.Id, head.Id, context.Row.SettingsVersion, 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.Hash(current) == mirrored.ContentHash)
{
// Nobody changed the settings since the last mirror: the mirror completes.
await MirrorAsync(context, main, head, cancellationToken).ConfigureAwait(false);
return;
}
// Both moved: the settings written meanwhile merge with the head (the version made in a workspace wins where both changed).
var merged = ProjectMerge.Merge(ProjectMerge.Read(mirrored.SettingsJson), ProjectMerge.Read(head.SettingsJson), ProjectMerge.Read(current));
var version = await AppendDerivedAsync(context, main, head, ProjectContent.Canonical(merged.ToJson()), ProjectVersionKind.Merge, cancellationToken)
.ConfigureAwait(false);
main = (await _db.Projects.GetAsync(main.Id, cancellationToken).ConfigureAwait(false))!;
await MirrorAsync(context, main, version, cancellationToken).ConfigureAwait(false);
}
/// <summary>The Main project of a sequence that has none (or one without versions): version 1 from the sequence's settings.</summary>
private async Task MakeMainAsync(LeaseContext context, SequenceProject? existing, CancellationToken cancellationToken)
{
var frames = await LoadFramesAsync(context.Row.Id, cancellationToken).ConfigureAwait(false);
var portable = CurrentPortable(context, frames.Photos) ?? ProjectContent.ToPortable(new ProjectSettings(), frames.Photos);
var now = DateTimeOffset.UtcNow;
var project = existing ?? new SequenceProject
{
Id = Guid.CreateVersion7(),
SequenceId = context.Row.Id,
Name = await FreeMainNameAsync(context.Row.Id, cancellationToken).ConfigureAwait(false),
IsMain = true,
CreatedAt = now,
UpdatedAt = now,
};
var first = NewVersion(project.Id, null, project.HeadNumber + 1, ProjectVersionKind.Imported, portable, context.Row.FramesVersion, now);
if (existing is null)
{
await AddProjectAsync(context.Folder, project with { MirroredSettingsVersion = context.Row.SettingsVersion, MirroredVersionId = first.Id }, first,
cancellationToken).ConfigureAwait(false);
}
else
{
var updated = await AppendAsync(context.Folder, existing, first, cancellationToken).ConfigureAwait(false);
await _db.Projects.SetMirroredAsync(updated.Id, first.Id, context.Row.SettingsVersion, cancellationToken).ConfigureAwait(false);
}
}
/// <summary>"Main", or "Main (2)"… when a project of that name exists already (one made before the sequence had its Main).</summary>
private async Task<string> FreeMainNameAsync(Guid sequenceId, CancellationToken cancellationToken)
{
var names = (await _db.Projects.ListBySequenceAsync(sequenceId, includeDeleted: false, cancellationToken).ConfigureAwait(false))
.Select(p => p.Name).ToHashSet(StringComparer.OrdinalIgnoreCase);
var name = MainName;
for (var n = 2; names.Contains(name); n++) name = $"{MainName} ({n})";
return name;
}
/// <summary>The portable form of the sequence's settings in force (index, else <c>project.json</c>, else defaults); null when unreadable or newer.</summary>
private static string? CurrentPortable(LeaseContext context, FramePhotos photos)
{
var json = context.Row.SettingsJson ?? (File.Exists(context.Folder.ProjectPath) ? AtomicFile.ReadAllText(context.Folder.ProjectPath) : null)
?? new ProjectSettings().ToJson();
try
{
if (ProjectContent.FormatOf(json) > ProjectSettings.CurrentVersion) return null;
return ProjectContent.ToPortable(json, photos);
}
catch (JsonException ex)
{
Log.Warning("projects", $"The settings of sequence {context.Row.Id} cannot be read", ex);
return null;
}
}
/// <summary>A version made here (catch-up or merge) appended to Main.</summary>
private async Task<ProjectVersion> AppendDerivedAsync(LeaseContext context, SequenceProject main, ProjectVersion head, string portable, ProjectVersionKind kind,
CancellationToken cancellationToken)
{
var version = NewVersion(main.Id, head.Id, main.HeadNumber + 1, kind, portable, context.Row.FramesVersion, DateTimeOffset.UtcNow);
await AppendAsync(context.Folder, main, version, cancellationToken).ConfigureAwait(false);
return version;
}
private ProjectVersion NewVersion(Guid projectId, Guid? parentId, int number, ProjectVersionKind kind, string portable, int framesVersion, DateTimeOffset now) => new()
{
Id = Guid.CreateVersion7(),
ProjectId = projectId,
ParentId = parentId,
Number = number,
Kind = kind,
ClientName = ClientName,
SettingsJson = portable,
ContentHash = ProjectContent.Hash(portable),
SettingsFormat = ProjectContent.FormatOf(portable),
FramesVersion = framesVersion,
CreatedAt = now,
StoredAt = now,
};
/// <summary>A new project with its first version: sidecars (version, then record), then both rows in one transaction.</summary>
private async Task<SequenceProject> AddProjectAsync(SequencePaths folder, SequenceProject project, ProjectVersion first, CancellationToken cancellationToken)
{
var withHead = project with { HeadVersionId = first.Id, HeadNumber = first.Number };
ProjectSidecars.WriteVersion(folder.VersionPath(project.Id, first.Number, first.Id), first);
ProjectSidecars.WriteRecord(folder.ProjectRecordPath(project.Id), ProjectRecord.Of(withHead));
await using var tx = await _db.BeginAsync(cancellationToken).ConfigureAwait(false);
await tx.Projects.InsertAsync(withHead with { HeadVersionId = null, HeadNumber = 0 }, cancellationToken).ConfigureAwait(false);
await tx.Projects.InsertVersionAsync(first, cancellationToken).ConfigureAwait(false);
await tx.Projects.UpdateAsync(withHead, cancellationToken).ConfigureAwait(false);
await tx.CommitAsync(cancellationToken).ConfigureAwait(false);
return withHead;
}
/// <summary>A version appended to a project: its sidecar, the record's head, then the version row and the head in one transaction.</summary>
private async Task<SequenceProject> AppendAsync(SequencePaths folder, SequenceProject project, ProjectVersion version, CancellationToken cancellationToken)
{
var now = DateTimeOffset.UtcNow;
var advanced = project with { HeadVersionId = version.Id, HeadNumber = version.Number, UpdatedAt = now };
ProjectSidecars.WriteVersion(folder.VersionPath(project.Id, version.Number, version.Id), version);
ProjectSidecars.WriteRecord(folder.ProjectRecordPath(project.Id), ProjectRecord.Of(advanced));
await using var tx = await _db.BeginAsync(cancellationToken).ConfigureAwait(false);
await tx.Projects.InsertVersionAsync(version, cancellationToken).ConfigureAwait(false);
if (!await tx.Projects.AdvanceHeadAsync(project.Id, project.HeadNumber, version.Id, version.Number, now, cancellationToken).ConfigureAwait(false))
throw new IOException($"The head of project {project.Id} moved in the index while its lease was held.");
await tx.CommitAsync(cancellationToken).ConfigureAwait(false);
return advanced;
}
/// <summary>
/// Writes Main's <paramref name="version"/> into the sequence: its positions placed on the sequence's frames, then
/// <c>project.json</c> and the index's settings (and hero frame) with the settings version as the check. Recorded as
/// mirrored when the index took it; a settings edit that won the race is caught up by the next operation (merged).
/// </summary>
private async Task MirrorAsync(LeaseContext context, SequenceProject main, ProjectVersion version, CancellationToken cancellationToken)
{
var frames = await LoadFramesAsync(context.Row.Id, cancellationToken).ConfigureAwait(false);
var placed = ProjectSettings.FromJson(ProjectContent.Place(version.SettingsJson, frames.Photos));
var json = placed.ToJson();
Directory.CreateDirectory(context.Folder.MetaDirectory);
AtomicFile.WriteAllText(context.Folder.ProjectPath, json);
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);
if (result is { Outcome: SequenceEditOutcome.Updated, Sequence: { } updated })
{
context.Row = updated;
await _db.Projects.SetMirroredAsync(main.Id, version.Id, updated.SettingsVersion, cancellationToken).ConfigureAwait(false);
}
else
{
Log.Info("projects", $"The settings of sequence {context.Row.Id} changed while version {version.Number} of Main was mirrored; merged next time");
}
}
/// <summary>Whether settings name no frame by position (every position parked by photo).</summary>
private static bool IsPortable(string json)
{
try
{
return JsonNode.Parse(json, new JsonNodeOptions { PropertyNameCaseInsensitive = true }) is JsonObject project
&& project["excludedFrames"] is null or JsonArray { Count: 0 }
&& project["heroFrame"] is null
&& (project["camera"] is not JsonObject camera || camera["keyframes"] is null or JsonArray { Count: 0 });
}
catch (Exception ex) when (ex is JsonException or ArgumentException)
{
return false;
}
}
private async Task<(IReadOnlyList<Frame> Frames, FramePhotos Photos)> LoadFramesAsync(Guid sequenceId, CancellationToken cancellationToken)
{
var frames = new List<Frame>();
for (var after = -1; ;)
{
var page = await _db.Frames.ListBySequenceAsync(sequenceId, after, PageSize, cancellationToken).ConfigureAwait(false);
frames.AddRange(page);
if (page.Count < PageSize) break;
after = page[^1].Index;
}
return (frames, new FramePhotos(frames.Select(f => (f.Index, new FramePhoto(f.Sha256, f.Size, f.CapturedAt)))));
}
private async Task EnsureNameFreeAsync(Guid sequenceId, Guid? projectId, string name, CancellationToken cancellationToken)
{
var projects = await _db.Projects.ListBySequenceAsync(sequenceId, includeDeleted: false, cancellationToken).ConfigureAwait(false);
if (projects.Any(p => p.Id != projectId && string.Equals(p.Name, name, StringComparison.OrdinalIgnoreCase)))
throw new ProjectException(ProjectError.NameTaken, $"Sequence {sequenceId} has a project named '{name}'.");
}
private static string ValidName(string name)
{
var trimmed = name?.Trim() ?? string.Empty;
return trimmed.Length is > 0 and <= MaxNameLength && !trimmed.Any(char.IsControl)
? trimmed
: throw new ProjectException(ProjectError.InvalidName, "A project name has 1 to 60 characters.");
}
private async Task<SequenceProject> KnownAsync(Guid projectId, CancellationToken cancellationToken) =>
await _db.Projects.GetAsync(projectId, cancellationToken).ConfigureAwait(false)
?? throw new ProjectException(ProjectError.NotFound, $"Project {projectId} does not exist.");
private async Task<SequenceProject> LiveAsync(Guid projectId, CancellationToken cancellationToken) =>
await KnownAsync(projectId, cancellationToken).ConfigureAwait(false) is { DeletedAt: null } project
? project
: throw new ProjectException(ProjectError.NotFound, $"Project {projectId} was deleted.");
private static ProjectException SequenceMissing(Guid sequenceId) => new(ProjectError.SequenceNotFound, $"Sequence {sequenceId} is not in the index.");
/// <summary>The sequence as read under its lease (the mirror moves its settings version) and its folder.</summary>
private sealed class LeaseContext(SequencePaths folder, Sequence row)
{
public SequencePaths Folder { get; } = folder;
public Sequence Row { get; set; } = row;
}
}