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

57 lines
1.4 KiB
C#
Raw Normal View History

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;
2023-02-16 12:57:06 +00:00
namespace InnovEnergy.Lib.S3.Drivers;
2023-02-16 12:57:06 +00:00
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;
}
}