57 lines
1.4 KiB
C#
57 lines
1.4 KiB
C#
using InnovEnergy.Lib.S3.Drivers.Internal;
|
|
using InnovEnergy.Lib.S3.Drivers.Internal.Delegates;
|
|
using InnovEnergy.Lib.S3.Metadata;
|
|
using InnovEnergy.Lib.S3.Records.Specialized;
|
|
using InnovEnergy.Lib.Time.Unix;
|
|
|
|
namespace InnovEnergy.Lib.S3.Drivers;
|
|
|
|
using Levels = IReadOnlyList<AggregationLevel>;
|
|
using Memory = Dictionary<String, TimeStampedRecord>;
|
|
|
|
public class MemoryDriver : Writer
|
|
{
|
|
|
|
public MemoryDriver(Levels levels, UnixTime startTime, Memory memory) :
|
|
base(
|
|
startTime,
|
|
levels,
|
|
ReadRecord(memory),
|
|
WriteRecord(memory)
|
|
)
|
|
{
|
|
}
|
|
|
|
private static String GetKey(AggregationLevel level, UInt32 index)
|
|
{
|
|
return $"{level}/{index}";
|
|
}
|
|
|
|
private static ReadRecord ReadRecord(Memory memory)
|
|
{
|
|
Task<TimeStampedRecord> Read(AggregationLevel level, UInt32 index)
|
|
{
|
|
var key = GetKey(level, index);
|
|
|
|
var result = memory.TryGetValue(key, out var tsRecord)
|
|
? tsRecord
|
|
: TimeStampedRecord.Empty();
|
|
|
|
return Task.FromResult(result);
|
|
}
|
|
|
|
return Read;
|
|
}
|
|
|
|
private static WriteRecord WriteRecord(Memory memory)
|
|
{
|
|
Task Write(TimeStampedRecord record, AggregationLevel level, UInt32 index)
|
|
{
|
|
var key = GetKey(level, index);
|
|
memory[key] = record;
|
|
return Task.CompletedTask;
|
|
}
|
|
|
|
return Write;
|
|
}
|
|
} |