more audit work
This commit is contained in:
@@ -4,8 +4,6 @@
|
||||
// Task: HLC-004 - Implement IHlcStateStore interface and InMemoryHlcStateStore
|
||||
// -----------------------------------------------------------------------------
|
||||
|
||||
using System.Collections.Concurrent;
|
||||
|
||||
namespace StellaOps.HybridLogicalClock;
|
||||
|
||||
/// <summary>
|
||||
@@ -17,7 +15,8 @@ namespace StellaOps.HybridLogicalClock;
|
||||
/// </remarks>
|
||||
public sealed class InMemoryHlcStateStore : IHlcStateStore
|
||||
{
|
||||
private readonly ConcurrentDictionary<string, HlcTimestamp> _states = new(StringComparer.Ordinal);
|
||||
private readonly Dictionary<string, HlcTimestamp> _states = new(StringComparer.Ordinal);
|
||||
private readonly object _lock = new();
|
||||
|
||||
/// <inheritdoc/>
|
||||
public Task<HlcTimestamp?> LoadAsync(string nodeId, CancellationToken ct = default)
|
||||
@@ -25,10 +24,13 @@ public sealed class InMemoryHlcStateStore : IHlcStateStore
|
||||
ArgumentException.ThrowIfNullOrWhiteSpace(nodeId);
|
||||
ct.ThrowIfCancellationRequested();
|
||||
|
||||
return Task.FromResult(
|
||||
_states.TryGetValue(nodeId, out var timestamp)
|
||||
? timestamp
|
||||
: (HlcTimestamp?)null);
|
||||
lock (_lock)
|
||||
{
|
||||
return Task.FromResult(
|
||||
_states.TryGetValue(nodeId, out var timestamp)
|
||||
? timestamp
|
||||
: (HlcTimestamp?)null);
|
||||
}
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
@@ -36,14 +38,21 @@ public sealed class InMemoryHlcStateStore : IHlcStateStore
|
||||
{
|
||||
ct.ThrowIfCancellationRequested();
|
||||
|
||||
_states.AddOrUpdate(
|
||||
timestamp.NodeId,
|
||||
timestamp,
|
||||
(_, existing) =>
|
||||
lock (_lock)
|
||||
{
|
||||
if (_states.TryGetValue(timestamp.NodeId, out var existing))
|
||||
{
|
||||
// Only update if new timestamp is greater (maintain monotonicity)
|
||||
return timestamp > existing ? timestamp : existing;
|
||||
});
|
||||
if (timestamp > existing)
|
||||
{
|
||||
_states[timestamp.NodeId] = timestamp;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
_states[timestamp.NodeId] = timestamp;
|
||||
}
|
||||
}
|
||||
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
@@ -52,10 +61,24 @@ public sealed class InMemoryHlcStateStore : IHlcStateStore
|
||||
/// Gets all stored states (for testing/debugging).
|
||||
/// </summary>
|
||||
public IReadOnlyDictionary<string, HlcTimestamp> GetAllStates() =>
|
||||
new Dictionary<string, HlcTimestamp>(_states);
|
||||
GetAllStatesSnapshot();
|
||||
|
||||
/// <summary>
|
||||
/// Clears all stored states (for testing).
|
||||
/// </summary>
|
||||
public void Clear() => _states.Clear();
|
||||
public void Clear()
|
||||
{
|
||||
lock (_lock)
|
||||
{
|
||||
_states.Clear();
|
||||
}
|
||||
}
|
||||
|
||||
private IReadOnlyDictionary<string, HlcTimestamp> GetAllStatesSnapshot()
|
||||
{
|
||||
lock (_lock)
|
||||
{
|
||||
return new Dictionary<string, HlcTimestamp>(_states);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user