feat(data): archive services, ingest and job handlers
ArchiveService facade over indexer (scan, adopt, rebuild), ArchiveIngest (IArchiveIngest with per-sequence group commit, leases and a write-ahead journal), verifier, maintenance (duplicates, orphans, cleanup), mass rename with resumable journal, thumbnails/proxies and card import; job handlers for scan, rebuild, verify, import, thumbs, proxies and maintenance with checkpoints. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_012iLMBNwBE2GnphAsu4cjaC
This commit is contained in:
23 files changed
+3161
No files matched your search
@@ -0,0 +1,92 @@
|
||||
using System.Collections.Concurrent;
|
||||
using Titano.Core.Archiving;
|
||||
using Titano.Core.Imaging;
|
||||
|
||||
namespace Titano.Data.Archiving;
|
||||
|
||||
/// <summary>Settings shared by the archive services.</summary>
|
||||
public sealed record ArchiveServiceOptions
|
||||
{
|
||||
/// <summary>Decoders for thumbnails and proxies beyond embedded previews and JPEG (WIC on Windows).</summary>
|
||||
public ImageDecoder Decoder { get; init; } = new([]);
|
||||
|
||||
/// <summary>Name written into lease files so a waiting writer can say who holds a sequence.</summary>
|
||||
public string Owner { get; init; } = "titano@" + Environment.MachineName;
|
||||
|
||||
/// <summary>How long a writer waits for a sequence held by another process before giving up.</summary>
|
||||
public TimeSpan LeaseWait { get; init; } = TimeSpan.FromMinutes(2);
|
||||
|
||||
public TimeSpan LeaseDuration { get; init; } = LeaseFile.DefaultDuration;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Read-modify-write of sidecars, serialised per sequence: an in-process gate for this process's writers plus a lease
|
||||
/// file for writers in other processes on the same share (server and client). Every edit sees the latest sidecar,
|
||||
/// with an interrupted ingest batch completed first.
|
||||
/// </summary>
|
||||
internal sealed class SequenceWriter(ArchiveServiceOptions options)
|
||||
{
|
||||
private readonly ConcurrentDictionary<Guid, SemaphoreSlim> _gates = new();
|
||||
|
||||
public ArchiveServiceOptions Options => options;
|
||||
|
||||
/// <summary>Holds the sequence exclusively until disposed.</summary>
|
||||
public async Task<IAsyncDisposable> LockAsync(ArchivePaths archive, Guid sequenceId, CancellationToken cancellationToken)
|
||||
{
|
||||
var gate = _gates.GetOrAdd(sequenceId, _ => new SemaphoreSlim(1, 1));
|
||||
await gate.WaitAsync(cancellationToken).ConfigureAwait(false);
|
||||
try
|
||||
{
|
||||
var lease = await LeaseFile.AcquireAsync(archive.LocksDirectory, $"seq-{sequenceId:N}", options.Owner, options.LeaseWait,
|
||||
options.LeaseDuration, cancellationToken).ConfigureAwait(false);
|
||||
return new Held(gate, lease);
|
||||
}
|
||||
catch
|
||||
{
|
||||
gate.Release();
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Reads the sidecar of a locked sequence, completing an interrupted ingest batch first.
|
||||
/// </summary>
|
||||
/// <exception cref="InvalidDataException">The sidecar in the folder belongs to another sequence.</exception>
|
||||
public static SequenceManifest ReadLocked(SequencePaths sequence, Guid sequenceId)
|
||||
{
|
||||
var manifest = ManifestStore.ReadSequence(sequence.ManifestPath);
|
||||
if (manifest.Id != sequenceId)
|
||||
throw new InvalidDataException($"{sequence.ManifestPath} belongs to sequence {manifest.Id}, not {sequenceId}.");
|
||||
if (IngestJournal.Recover(sequence, manifest) is { } recovered)
|
||||
{
|
||||
ManifestStore.WriteSequence(sequence.ManifestPath, recovered);
|
||||
manifest = recovered;
|
||||
}
|
||||
IngestJournal.Delete(sequence);
|
||||
return manifest;
|
||||
}
|
||||
|
||||
/// <summary>Applies <paramref name="edit"/> to the sidecar under the lock; a null result writes nothing.</summary>
|
||||
public async Task<SequenceManifest> UpdateAsync(ArchivePaths archive, Guid sequenceId, string folder,
|
||||
Func<SequenceManifest, SequenceManifest?> edit, CancellationToken cancellationToken)
|
||||
{
|
||||
var sequence = archive.Sequence(folder);
|
||||
await using (await LockAsync(archive, sequenceId, cancellationToken).ConfigureAwait(false))
|
||||
{
|
||||
var manifest = ReadLocked(sequence, sequenceId);
|
||||
if (edit(manifest) is not { } updated) return manifest;
|
||||
ManifestStore.WriteSequence(sequence.ManifestPath, updated);
|
||||
return updated;
|
||||
}
|
||||
}
|
||||
|
||||
private sealed class Held(SemaphoreSlim gate, LeaseFile lease) : IAsyncDisposable
|
||||
{
|
||||
public ValueTask DisposeAsync()
|
||||
{
|
||||
lease.Dispose();
|
||||
gate.Release();
|
||||
return ValueTask.CompletedTask;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user