dotnet-cqrs/Svrnty.CQRS.Events.Abstractions/Configuration/RetentionPolicyConfig.cs

55 lines
1.9 KiB
C#

using System;
using Svrnty.CQRS.Events.Abstractions.Storage;
namespace Svrnty.CQRS.Events.Abstractions.Configuration;
/// <summary>
/// Configuration for event stream retention policy.
/// Supports time-based and/or size-based retention.
/// </summary>
public record RetentionPolicyConfig : IRetentionPolicy
{
/// <summary>
/// Stream name this policy applies to.
/// Use "*" for default policy.
/// </summary>
public required string StreamName { get; init; }
/// <summary>
/// Maximum age for events (null = no time-based retention).
/// Example: TimeSpan.FromDays(30) keeps events for 30 days.
/// </summary>
public TimeSpan? MaxAge { get; init; }
/// <summary>
/// Maximum number of events to retain (null = no size-based retention).
/// Example: 1000000 keeps only the last 1 million events.
/// </summary>
public long? MaxEventCount { get; init; }
/// <summary>
/// Whether this policy is enabled.
/// Default: true
/// </summary>
public bool Enabled { get; init; } = true;
/// <summary>
/// Validates the retention policy configuration.
/// </summary>
/// <exception cref="ArgumentException">Thrown when configuration is invalid.</exception>
public void Validate()
{
if (string.IsNullOrWhiteSpace(StreamName))
throw new ArgumentException("StreamName cannot be null or whitespace", nameof(StreamName));
if (MaxAge.HasValue && MaxAge.Value <= TimeSpan.Zero)
throw new ArgumentException("MaxAge must be positive", nameof(MaxAge));
if (MaxEventCount.HasValue && MaxEventCount.Value <= 0)
throw new ArgumentException("MaxEventCount must be positive", nameof(MaxEventCount));
if (!MaxAge.HasValue && !MaxEventCount.HasValue)
throw new ArgumentException("At least one of MaxAge or MaxEventCount must be specified");
}
}