Tests write serialized queue and watch-memory bytes directly; the app persists through its coalescing writer. Single-instance hand-over no longer returns an unused flag. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_012iLMBNwBE2GnphAsu4cjaC
48 lines
1.8 KiB
C#
48 lines
1.8 KiB
C#
namespace Titano.Uploader.Watching;
|
|
|
|
/// <summary>Size and last-write time of a file at one check.</summary>
|
|
public readonly record struct FileObservation(string Path, long Size, DateTime ModifiedUtc);
|
|
|
|
/// <summary>
|
|
/// Decides when a file in a watch folder has stopped growing: it is reported once its size and write time were
|
|
/// identical in <see cref="RequiredChecks"/> consecutive checks, then forgotten (the caller enqueues it).
|
|
/// </summary>
|
|
public sealed class StableFileTracker
|
|
{
|
|
public const int RequiredChecks = 2;
|
|
|
|
private readonly Dictionary<string, (long Size, DateTime Modified, int Count)> _seen = new(StringComparer.OrdinalIgnoreCase);
|
|
|
|
/// <summary>Files seen but not yet stable.</summary>
|
|
public int PendingCount => _seen.Count;
|
|
|
|
/// <summary>
|
|
/// Records a complete scan: returns the files that are now stable and forgets files missing from the scan.
|
|
/// </summary>
|
|
public IReadOnlyList<FileObservation> Check(IEnumerable<FileObservation> scan)
|
|
{
|
|
var present = new HashSet<string>(StringComparer.OrdinalIgnoreCase);
|
|
var stable = new List<FileObservation>();
|
|
foreach (var o in scan)
|
|
{
|
|
present.Add(o.Path);
|
|
if (Observe(o)) stable.Add(o);
|
|
}
|
|
foreach (var path in _seen.Keys.Where(p => !present.Contains(p)).ToList()) _seen.Remove(path);
|
|
return stable;
|
|
}
|
|
|
|
/// <summary>Records one observation; true when the file just became stable.</summary>
|
|
public bool Observe(FileObservation o)
|
|
{
|
|
var count = _seen.TryGetValue(o.Path, out var last) && last.Size == o.Size && last.Modified == o.ModifiedUtc ? last.Count + 1 : 1;
|
|
if (count >= RequiredChecks)
|
|
{
|
|
_seen.Remove(o.Path);
|
|
return true;
|
|
}
|
|
_seen[o.Path] = (o.Size, o.ModifiedUtc, count);
|
|
return false;
|
|
}
|
|
}
|