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:
1 parent
e3cbe30b29
commit
03d32f18d3
51 files changed
+4917
-108
No files matched your search
@@ -0,0 +1,179 @@
|
||||
using System.Globalization;
|
||||
using Chronos.Core.Archiving;
|
||||
using Chronos.Core.Diagnostics;
|
||||
using Chronos.Core.Model;
|
||||
|
||||
namespace Chronos.Data.Projects;
|
||||
|
||||
/// <summary>
|
||||
/// Brings the index's projects of one sequence in line with their sidecars (owner review 2, R8), which are the truth: a
|
||||
/// project or version the index lacks is added (an index rebuilt, a sequence restored from the trash), a head the index is
|
||||
/// behind on moves forward, a name, Main flag or deletion recorded since is taken, and a live index row whose folder is gone
|
||||
/// is marked deleted. 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 is moved aside to <c>versions/.stray</c>, never deleted. Runs under the sequence's lease.
|
||||
/// </summary>
|
||||
internal sealed class ProjectIndexer(ChronosDatabase db)
|
||||
{
|
||||
private const string StrayFolder = ".stray";
|
||||
|
||||
/// <summary>Reconciles the projects of <paramref name="sequenceId"/> in <paramref name="folder"/>; the caller holds the sequence's lease.</summary>
|
||||
public async Task ReconcileAsync(SequencePaths folder, Guid sequenceId, CancellationToken cancellationToken)
|
||||
{
|
||||
var seen = new HashSet<Guid>();
|
||||
if (Directory.Exists(folder.ProjectsDirectory))
|
||||
{
|
||||
foreach (var (directory, trashed) in ProjectFolders(folder))
|
||||
{
|
||||
cancellationToken.ThrowIfCancellationRequested();
|
||||
ProjectRecord record;
|
||||
try
|
||||
{
|
||||
record = ProjectSidecars.ReadRecord(Path.Combine(directory, ArchiveLayout.ProjectFile));
|
||||
}
|
||||
catch (Exception ex) when (ex is IOException or InvalidDataException or UnauthorizedAccessException)
|
||||
{
|
||||
Log.Warning("projects", $"The project in {directory} cannot be read", ex);
|
||||
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;
|
||||
if (trashed && record.Deleted is null) record = record with { Deleted = Directory.GetLastWriteTimeUtc(directory) };
|
||||
if (record.Deleted is null) record = RollForward(folder, record, directory);
|
||||
await IndexAsync(record, directory, cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
}
|
||||
foreach (var gone in await db.Projects.ListBySequenceAsync(sequenceId, includeDeleted: false, cancellationToken).ConfigureAwait(false))
|
||||
{
|
||||
if (seen.Contains(gone.Id)) continue;
|
||||
Log.Warning("projects", $"Project {gone.Id} of sequence {sequenceId} has no folder any more; it is marked deleted");
|
||||
await db.Projects.UpdateAsync(gone with { DeletedAt = DateTimeOffset.UtcNow, UpdatedAt = DateTimeOffset.UtcNow }, cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>The project folders of a sequence: the live ones, then those in the projects' trash (<c><id>-<time></c>).</summary>
|
||||
private static IEnumerable<(string Directory, bool Trashed)> ProjectFolders(SequencePaths folder)
|
||||
{
|
||||
foreach (var directory in Directory.EnumerateDirectories(folder.ProjectsDirectory).Order(StringComparer.Ordinal))
|
||||
{
|
||||
if (Guid.TryParseExact(Path.GetFileName(directory), "N", out _)) yield return (directory, false);
|
||||
}
|
||||
if (!Directory.Exists(folder.ProjectsTrashDirectory)) yield break;
|
||||
foreach (var directory in Directory.EnumerateDirectories(folder.ProjectsTrashDirectory).Order(StringComparer.Ordinal))
|
||||
{
|
||||
if (File.Exists(Path.Combine(directory, ArchiveLayout.ProjectFile))) yield return (directory, true);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The record with the version files that follow its head adopted (a push that wrote its version but not the record),
|
||||
/// and the files past the head that do not follow it moved aside; the record is written again when its head moved.
|
||||
/// </summary>
|
||||
private static ProjectRecord RollForward(SequencePaths folder, ProjectRecord record, string directory)
|
||||
{
|
||||
var versions = Path.Combine(directory, ArchiveLayout.ProjectVersionsFolder);
|
||||
var files = ProjectSidecars.ListVersions(versions);
|
||||
var head = record.Head;
|
||||
var moved = false;
|
||||
while (true)
|
||||
{
|
||||
var next = (head?.Number ?? 0) + 1;
|
||||
var candidates = files.Where(f => f.Number == next).ToList();
|
||||
ProjectVersion? follower = null;
|
||||
foreach (var candidate in candidates)
|
||||
{
|
||||
try
|
||||
{
|
||||
var version = ProjectSidecars.ReadVersion(candidate.Path);
|
||||
if (version.ProjectId == record.Id && version.ParentId == head?.Id)
|
||||
{
|
||||
follower = version;
|
||||
break;
|
||||
}
|
||||
}
|
||||
catch (Exception ex) when (ex is IOException or InvalidDataException or UnauthorizedAccessException)
|
||||
{
|
||||
Log.Warning("projects", $"Version file {candidate.Path} cannot be read", ex);
|
||||
}
|
||||
}
|
||||
if (follower is null) break;
|
||||
head = new ProjectHead(follower.Id, follower.Number);
|
||||
moved = true;
|
||||
}
|
||||
foreach (var stray in files.Where(f => f.Number > (head?.Number ?? 0)))
|
||||
{
|
||||
try
|
||||
{
|
||||
var aside = Path.Combine(versions, StrayFolder);
|
||||
Directory.CreateDirectory(aside);
|
||||
File.Move(stray.Path, Path.Combine(aside, Path.GetFileName(stray.Path) + "." + DateTime.UtcNow.ToString("yyyyMMddHHmmssfff", CultureInfo.InvariantCulture)));
|
||||
Log.Warning("projects", $"Version file {stray.Path} does not follow the head of project {record.Id}; moved aside");
|
||||
}
|
||||
catch (Exception ex) when (ex is IOException or UnauthorizedAccessException)
|
||||
{
|
||||
Log.Warning("projects", $"Version file {stray.Path} could not be moved aside", ex);
|
||||
}
|
||||
}
|
||||
if (!moved) return record;
|
||||
var rolled = record with { Head = head, Updated = DateTimeOffset.UtcNow };
|
||||
ProjectSidecars.WriteRecord(folder.ProjectRecordPath(record.Id), rolled);
|
||||
return rolled;
|
||||
}
|
||||
|
||||
/// <summary>Writes what the index lacks of one project's record and versions.</summary>
|
||||
private async Task IndexAsync(ProjectRecord record, string directory, CancellationToken cancellationToken)
|
||||
{
|
||||
var row = await db.Projects.GetAsync(record.Id, cancellationToken).ConfigureAwait(false);
|
||||
if (row is null)
|
||||
{
|
||||
var project = record.ToProject();
|
||||
var live = await db.Projects.ListBySequenceAsync(record.SequenceId, includeDeleted: false, cancellationToken).ConfigureAwait(false);
|
||||
if (project.DeletedAt is null && project.IsMain && live.Any(p => p.IsMain))
|
||||
{
|
||||
Log.Warning("projects", $"Project {record.Id} says it is Main, but sequence {record.SequenceId} has one; indexed as another project");
|
||||
project = project with { IsMain = false };
|
||||
}
|
||||
if (project.DeletedAt is null && live.Any(p => string.Equals(p.Name, project.Name, StringComparison.OrdinalIgnoreCase)))
|
||||
project = project with { Name = project.Name + " (" + project.Id.ToString("N")[..6] + ")" };
|
||||
// The head follows its versions below, so a version the index cannot take never leaves a head without its row.
|
||||
await db.Projects.InsertAsync(project with { HeadVersionId = null, HeadNumber = 0 }, cancellationToken).ConfigureAwait(false);
|
||||
row = project with { HeadVersionId = null, HeadNumber = 0 };
|
||||
}
|
||||
var indexed = await db.Projects.ListVersionIdsAsync(record.Id, cancellationToken).ConfigureAwait(false);
|
||||
var headNumber = row.HeadNumber;
|
||||
Guid? headId = row.HeadVersionId;
|
||||
var limit = record.Head?.Number ?? 0;
|
||||
foreach (var file in ProjectSidecars.ListVersions(Path.Combine(directory, ArchiveLayout.ProjectVersionsFolder)))
|
||||
{
|
||||
if (file.Number > limit || indexed.ContainsKey(file.Number)) continue;
|
||||
ProjectVersion version;
|
||||
try
|
||||
{
|
||||
version = ProjectSidecars.ReadVersion(file.Path);
|
||||
}
|
||||
catch (Exception ex) when (ex is IOException or InvalidDataException or UnauthorizedAccessException)
|
||||
{
|
||||
Log.Warning("projects", $"Version file {file.Path} cannot be read; it is not indexed", ex);
|
||||
continue;
|
||||
}
|
||||
if (version.ProjectId != record.Id || version.Number != file.Number) continue;
|
||||
await db.Projects.InsertVersionAsync(version, cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
if (record.Head is { } head && head.Number > headNumber && await db.Projects.GetVersionAsync(head.Id, cancellationToken).ConfigureAwait(false) is not null)
|
||||
(headId, headNumber) = (head.Id, head.Number);
|
||||
var updated = row with
|
||||
{
|
||||
Name = row.DeletedAt is null && record.Deleted is null && row.Name != record.Name && !await NameTakenAsync(record, cancellationToken).ConfigureAwait(false)
|
||||
? record.Name
|
||||
: row.Name,
|
||||
HeadVersionId = headId,
|
||||
HeadNumber = headNumber,
|
||||
DeletedAt = row.DeletedAt ?? record.Deleted,
|
||||
};
|
||||
if (updated != row) await db.Projects.UpdateAsync(updated, cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
private async Task<bool> NameTakenAsync(ProjectRecord record, CancellationToken cancellationToken) =>
|
||||
(await db.Projects.ListBySequenceAsync(record.SequenceId, includeDeleted: false, cancellationToken).ConfigureAwait(false))
|
||||
.Any(p => p.Id != record.Id && string.Equals(p.Name, record.Name, StringComparison.OrdinalIgnoreCase));
|
||||
}
|
||||
Reference in new issue
Block a user