Innovenergy_trunk/csharp/lib/S3/Drivers/Internal/Util/Aggregator.cs

60 lines
1.3 KiB
C#
Raw Normal View History

2023-02-16 12:57:06 +00:00
using InnovEnergy.S3.Metadata;
using InnovEnergy.S3.Records;
using InnovEnergy.S3.Records.Operations;
using InnovEnergy.Lib.Utils;
namespace InnovEnergy.S3.Drivers.Internal.Util;
public class Aggregator
{
public AggregationLevel AggregationLevel { get; }
private Record[] Buffer { get; }
private UInt32 Index { get; set; }
public Aggregator(AggregationLevel thisLevel,
AggregationLevel levelBelow,
IEnumerable<Record> initialRecords)
{
var ratio = thisLevel.SamplePeriod / levelBelow.SamplePeriod;
AggregationLevel = thisLevel;
Index = 0;
Buffer = new Record[ratio];
Clear();
foreach (var record in initialRecords)
Aggregate(record);
}
public Record? Aggregate(Record r)
{
Buffer[Index++] = r;
return IsFull
? ForceAggregation()
: null;
}
public Record? ForceAggregation()
{
if (IsEmpty)
return null; // nothing to aggregate
var aggregated = Buffer.Aggregate();
Clear();
return aggregated;
}
private void Clear()
{
Buffer.Fill(Record.Empty);
Index = 0;
}
private Boolean IsFull => Index == Buffer.Length;
private Boolean IsEmpty => Index == 0;
}