// ----------------------------------------------------------------------------- // InMemoryHlcStateStore.cs // Sprint: SPRINT_20260105_002_001_LB_hlc_core_library // Task: HLC-004 - Implement IHlcStateStore interface and InMemoryHlcStateStore // ----------------------------------------------------------------------------- using System.Collections.Concurrent; namespace StellaOps.HybridLogicalClock; /// /// In-memory implementation of HLC state store for testing and development. /// /// /// This implementation does not survive process restarts. Use PostgresHlcStateStore /// for production deployments requiring persistence. /// public sealed class InMemoryHlcStateStore : IHlcStateStore { private readonly ConcurrentDictionary _states = new(StringComparer.Ordinal); /// public Task LoadAsync(string nodeId, CancellationToken ct = default) { ArgumentException.ThrowIfNullOrWhiteSpace(nodeId); ct.ThrowIfCancellationRequested(); return Task.FromResult( _states.TryGetValue(nodeId, out var timestamp) ? timestamp : (HlcTimestamp?)null); } /// public Task SaveAsync(HlcTimestamp timestamp, CancellationToken ct = default) { ct.ThrowIfCancellationRequested(); _states.AddOrUpdate( timestamp.NodeId, timestamp, (_, existing) => { // Only update if new timestamp is greater (maintain monotonicity) return timestamp > existing ? timestamp : existing; }); return Task.CompletedTask; } /// /// Gets all stored states (for testing/debugging). /// public IReadOnlyDictionary GetAllStates() => new Dictionary(_states); /// /// Clears all stored states (for testing). /// public void Clear() => _states.Clear(); }