public SegmentStats Segment(string label, IEnumerable <T> stream, int maxWorkingMemoryBytes = 0, ISafeLogger logger = null, IMetricsHost metrics = null, CancellationToken cancellationToken = default) { var count = 0L; var segments = 0; var sw = new StreamWriter(CreateSegment(label, segments)); try { foreach (var item in stream) { var line = _serialize(item); BuiltInMetrics.LineLength <T>(metrics, line.Length); sw.WriteLine(line); count++; if (maxWorkingMemoryBytes == 0 || sw.BaseStream.Length < maxWorkingMemoryBytes) { continue; } sw.Flush(); sw.Close(); segments++; sw = new StreamWriter(CreateSegment(label, segments)); } } finally { sw.Flush(); sw.Close(); } return(new SegmentStats { RecordCount = count, RecordLength = (int)BuiltInMetrics.GetMeanLineLength <T>(metrics) + 1, SegmentCount = segments }); }