public async Task Save(RecordRefRun run, string targetFileName, bool returnBuffer = true) { using var output = new FileStream( path: targetFileName, FileMode.CreateNew, FileAccess.Write, FileShare.None, bufferSize: 1024 * (int)Units.BytesPerKiB); const int capacity = 900 * 1000; ValueTask writeTask = ValueTask.CompletedTask; var buffer1 = new ArrayBufferWriter <byte>(capacity); var buffer2 = new ArrayBufferWriter <byte>(capacity); var buffer = buffer1; foreach (var item in run.Records) { if (buffer.FreeCapacity < item.length) { await writeTask; writeTask = output.WriteAsync(buffer.WrittenMemory); buffer = buffer == buffer1 ? buffer2 : buffer1; buffer.Clear(); } buffer.Write(run.Buffer.AsSpan().Slice(item.offset, item.length)); } await writeTask; await output.WriteAsync(buffer.WrittenMemory); if (returnBuffer) { ReturnBuffer(run.Buffer); } }
public void Sort(RecordRefRun run) { var comparer = new RecordRefComparer(run.Buffer, run.Limit); run.Records.Sort(comparer.Compare); }