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; using Memory = Dictionary; 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 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; } }