Innovenergy_trunk/csharp/lib/S3/Drivers/MemoryDriver.cs

57 lines
1.4 KiB
C#
Raw Normal View History

2023-02-16 12:57:06 +00:00
using InnovEnergy.S3.Drivers.Internal;
using InnovEnergy.S3.Drivers.Internal.Delegates;
using InnovEnergy.S3.Metadata;
using InnovEnergy.S3.Records.Specialized;
using InnovEnergy.Time.Unix;
namespace InnovEnergy.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;
}
}