public async Task<Stream> DownloadValuesAsync() { var stream = CreateMemoryStream(); await _blob.DownloadRangeToStreamAsync(stream, 0, _commitPosition.ToLinearAddress()); stream.Seek(0, SeekOrigin.Begin); var sj = new StreamJoiner(); sj.Append(stream, 0, _commitPosition.ToLinearAddress()); return sj; }
async Task<PreparedPages> PreparePages(int position, Stream stream) { int count = (int)stream.Length; int copied = 0; int currentPosition = position; int firstPageNumber = position / PageSize; int lastPageNumber = ((position + count) / PageSize); Page firstPage = Retrieve_Page_At(position); Page lastPage = new Page(position + count); Task lastFilling = null; if (firstPageNumber == lastPageNumber) lastPage = firstPage; else if (lastPage.GetBaseAddress() < _commitPosition.ToLinearAddress()) lastFilling = lastPage.FillFromBlobAsync(_blob); var bufferedPages = new StreamJoiner(); if (firstPage.Is_empty_and_can_contain_all_data___or___Is_not_empty(count)) { copied = firstPage.Append(stream); bufferedPages.Append(firstPage.ToStream()); currentPosition = position + copied; } if (copied < count) { int rem; int fullPages = Math.DivRem(count - copied, PageSize, out rem); if (fullPages > 0) { bufferedPages.Append(stream, copied, count - copied - rem); copied += fullPages * PageSize; currentPosition = position + copied; stream.Seek(copied, SeekOrigin.Begin); } if (copied < count) { Debug.Assert(rem > 0); if (lastFilling != null) await lastFilling; else lastPage = new Page(position + copied); lastPage.Fill_From(stream); bufferedPages.Append(lastPage.ToStream()); copied += rem; currentPosition = position + copied; } } Debug.Assert(position + count == currentPosition); Debug.Assert(copied == count); return new PreparedPages { Position = position, CurrentPosition = currentPosition, Pages = bufferedPages, LastPage = lastPage }; }