using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
using ZB.MOM.WW.CBDDC.Core.Storage;
using ZB.MOM.WW.CBDDC.Persistence.Surreal;
namespace ZB.MOM.WW.CBDDC.Persistence.Lmdb;
///
/// Extension methods for adding the LMDB oplog provider and migration feature flags.
///
public static class CBDDCLmdbOplogExtensions
{
///
/// Registers LMDB oplog services and replaces with a feature-flag migration router.
///
/// The service collection.
/// Factory creating LMDB environment options.
/// Optional migration feature-flag configuration.
/// The service collection.
public static IServiceCollection AddCBDDCLmdbOplog(
this IServiceCollection services,
Func optionsFactory,
Action? configureFlags = null)
{
if (services == null) throw new ArgumentNullException(nameof(services));
if (optionsFactory == null) throw new ArgumentNullException(nameof(optionsFactory));
services.TryAddSingleton(optionsFactory);
var flags = new LmdbOplogFeatureFlags
{
UseLmdbOplog = true,
DualWriteOplog = true,
PreferLmdbReads = false,
EnableReadShadowValidation = false
};
configureFlags?.Invoke(flags);
services.TryAddSingleton(flags);
services.TryAddSingleton();
services.TryAddSingleton();
bool surrealRegistered = services.Any(descriptor => descriptor.ServiceType == typeof(SurrealOplogStore));
if (surrealRegistered)
{
services.TryAddSingleton();
services.Replace(ServiceDescriptor.Singleton());
}
else
services.Replace(ServiceDescriptor.Singleton(sp => sp.GetRequiredService()));
return services;
}
}