46 lines
1.8 KiB
C#
46 lines
1.8 KiB
C#
using MesETL.App.HostedServices.Abstractions;
|
|
using MesETL.App.Options;
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
using Microsoft.Extensions.Logging;
|
|
using Microsoft.Extensions.Options;
|
|
|
|
namespace MesETL.App.Services.ETL;
|
|
|
|
public class DataReaderFactory
|
|
{
|
|
private readonly ILogger<DataReaderFactory> _logger;
|
|
private readonly IOptions<DataInputOptions> _options;
|
|
|
|
public DataReaderFactory(ILogger<DataReaderFactory> logger, IOptions<DataInputOptions> options)
|
|
{
|
|
_logger = logger;
|
|
_options = options;
|
|
}
|
|
|
|
public IDataReader CreateReader(string filePath, string tableName, string[] headers)
|
|
{
|
|
if (_options.Value.UseMock)
|
|
{
|
|
if (_options.Value.TableMockConfig is null)
|
|
throw new ApplicationException("未配置表模拟数据量级");
|
|
_logger.LogDebug("***** Using {Type} data source *****", "ZSTD mock");
|
|
var mockConfig = _options.Value.TableMockConfig.GetValueOrDefault(tableName,
|
|
new TableMockConfig { MockCount = 1, UseDeepCopy = false });
|
|
mockConfig.MockCount = (long)Math.Ceiling(mockConfig.MockCount * _options.Value.MockCountMultiplier);
|
|
return new ZstMockReader(mockConfig, filePath,
|
|
tableName, headers, _options.Value.Delimiter, _options.Value.QuoteChar, _logger);
|
|
}
|
|
|
|
_logger.LogDebug("***** Using {Type} data source *****", "ZSTD");
|
|
return new ZstReader(filePath, tableName, headers, _options.Value.Delimiter, _options.Value.QuoteChar, _logger);
|
|
}
|
|
}
|
|
|
|
public static class DataSourceFactoryExtensions
|
|
{
|
|
public static IServiceCollection AddDataSourceFactory(this IServiceCollection services)
|
|
{
|
|
services.AddSingleton<DataReaderFactory>();
|
|
return services;
|
|
}
|
|
} |