MES-ETL/MesETL.App/Services/ProcessContext.cs

78 lines
2.4 KiB
C#
Raw Normal View History

2024-01-29 09:29:16 +08:00
using System.Collections.Concurrent;
namespace MesETL.App.Services;
2023-12-29 16:16:05 +08:00
2024-01-04 09:00:44 +08:00
/// <summary>
/// 处理上下文类,标识处理进度
/// </summary>
2023-12-29 16:16:05 +08:00
public class ProcessContext
{
2024-01-29 09:29:16 +08:00
private bool _hasException;
private long _inputCount;
private long _transformCount;
private long _outputCount;
2024-02-10 17:12:26 +08:00
private readonly ConcurrentDictionary<string, (long input, long output)> _tableProgress = new();
2024-01-29 09:29:16 +08:00
public bool HasException => _hasException;
2023-12-29 16:16:05 +08:00
public bool IsInputCompleted { get; private set; }
public bool IsTransformCompleted { get; private set; }
public bool IsOutputCompleted { get; private set; }
2024-01-29 09:29:16 +08:00
public long InputCount
2023-12-29 16:16:05 +08:00
{
get => _inputCount;
2024-01-29 09:29:16 +08:00
set => Interlocked.Exchange(ref _inputCount, value);
2023-12-29 16:16:05 +08:00
}
2024-01-29 09:29:16 +08:00
public long TransformCount
2023-12-29 16:16:05 +08:00
{
get => _transformCount;
2024-01-29 09:29:16 +08:00
set => Interlocked.Exchange(ref _transformCount, value);
2023-12-29 16:16:05 +08:00
}
2024-01-29 09:29:16 +08:00
public long OutputCount
2023-12-29 16:16:05 +08:00
{
get => _outputCount;
2024-01-29 09:29:16 +08:00
set => Interlocked.Exchange(ref _outputCount, value);
2023-12-29 16:16:05 +08:00
}
2024-01-29 09:29:16 +08:00
2024-12-10 14:03:09 +08:00
public long MaxMemoryUsage { get; set; }
2024-01-29 09:29:16 +08:00
// TableName -> Count
2024-02-10 17:12:26 +08:00
public IReadOnlyDictionary<string, (long input, long output)> TableProgress => _tableProgress;
2024-01-29 09:29:16 +08:00
2023-12-29 16:16:05 +08:00
public void CompleteInput() => IsInputCompleted = true;
public void CompleteTransform() => IsTransformCompleted = true;
public void CompleteOutput() => IsOutputCompleted = true;
2024-01-29 09:29:16 +08:00
public bool AddException(Exception e) => _hasException = true;
2023-12-29 16:16:05 +08:00
public void AddInput() => Interlocked.Increment(ref _inputCount);
public void AddInput(int count) => Interlocked.Add(ref _inputCount, count);
public void AddTransform() => Interlocked.Increment(ref _transformCount);
public void AddTransform(int count) => Interlocked.Add(ref _transformCount, count);
public void AddOutput() => Interlocked.Increment(ref _outputCount);
public void AddOutput(int count) => Interlocked.Add(ref _outputCount, count);
2024-01-29 09:29:16 +08:00
2024-02-10 17:12:26 +08:00
public void AddTableInput(string table, int count)
2024-01-29 09:29:16 +08:00
{
if (!_tableProgress.TryAdd(table, (input: count, output: 0)))
2024-02-10 17:12:26 +08:00
{
var tuple = _tableProgress[table];
2024-02-10 17:12:26 +08:00
tuple.input += count;
_tableProgress[table] = tuple;
}
2024-01-29 09:29:16 +08:00
}
2024-02-10 17:12:26 +08:00
public void AddTableOutput(string table, int count)
2024-01-29 09:29:16 +08:00
{
2024-02-10 17:12:26 +08:00
_tableProgress.AddOrUpdate(table, (input:0, output:count), (k, tuple) =>
{
tuple.output += count;
return tuple;
});
2024-01-29 09:29:16 +08:00
}
2023-12-29 16:16:05 +08:00
}