/// <summary> /// Creates a new empty TimeSeriesStreamCollection object. It initializes an underlying MemoryStream and /// inits it with default values for all TimeSeries (one for each given key is created). Every write access /// to any TimeSeries will then be in-place on the already allocated buffer with the fixed size of /// keys.Count * bucketCount. /// </summary> public TimeSeriesStreamCollection(IEnumerable <TKey> keys, int keySize, Action <TKey, Stream> writeKeyAction, TimeSeriesSpan span, int decimalPlaces = 1) : base() { var keyList = keys.ToList(); Metrics = new BinaryStreamMetrics(keySize, span.Count, decimalPlaces); _stream = new CompressableMemoryStream(Metrics.StreamSize(keyList.Count)); _stream.SetLength(Metrics.StreamSize(keyList.Count)); using (var writer = new BinaryWriter(_stream, System.Text.Encoding.UTF8, true)) writer.Write(keyList.Count); foreach (var i in Enumerable.Range(0, keyList.Count)) { writeKeyAction(keyList[i], _stream); var timeseries = new TimeSeriesStream <T>(_stream, Metrics.TimeseriesPosition(i), span, decimalPlaces); foreach (var item in timeseries) { timeseries[item.Key] = null; } _dict.Add(keyList[i], timeseries); } }
/// <summary> /// Initializes a CompressableMemoryStream from a GZipped byte array. /// </summary> public static CompressableMemoryStream FromCompressedByteArray(byte[] byteArray) { var stream = new CompressableMemoryStream((int)(byteArray.Length / _compressionEstimate)); using var compressedStream = new MemoryStream(byteArray, 0, byteArray.Length, false) { Position = 0 }; using var decompressionStream = new GZipStream(compressedStream, CompressionMode.Decompress); decompressionStream.CopyTo(stream); stream.Position = 0; return(stream); }
/// <summary> /// Restores a TimeSeriesStreamCollection from a compressed byte array. /// </summary> public TimeSeriesStreamCollection(byte[] compressedByteArray, int keySize, Func <Stream, TKey> readKeyFunc, TimeSeriesSpan span, int decimalPlaces = 1) : base() { Metrics = new BinaryStreamMetrics(keySize, span.Count, decimalPlaces); _stream = CompressableMemoryStream.FromCompressedByteArray(compressedByteArray); int count = 0; using (var reader = new BinaryReader(_stream, System.Text.Encoding.UTF8, true)) count = reader.ReadInt32(); foreach (var i in Enumerable.Range(0, count)) { _stream.Seek(Metrics.KeyPosition(i), SeekOrigin.Begin); var key = readKeyFunc(_stream); _dict.Add(key, new TimeSeriesStream <T>(_stream, Metrics.TimeseriesPosition(i), span, decimalPlaces)); } }