/// <summary> /// Upload a single block. This can happen on parallel threads. /// </summary> /// <param name="blockIdSequenceNumber">The block sequence prefix value.</param> /// <param name="setResult">The set result.</param> /// <returns>A <see cref="TaskSequence"/> that dispenses a block stream.</returns> private TaskSequence DispenseBlockStream(long blockIdSequenceNumber, Action <SmallBlockMemoryStream, string, string> setResult) { int currentCallIndex = this.dispenserCallCount++; TraceHelper.WriteLine("Staring dispensBlockStream for id {0}", currentCallIndex); SmallBlockMemoryStream memoryStream = new SmallBlockMemoryStream(Constants.DefaultBufferSize); var md5Check = MD5.Create(); int totalCopied = 0, numRead = 0; do { byte[] buffer = new byte[Constants.DefaultBufferSize]; var numToRead = (int)Math.Min(buffer.Length, this.blockSize - totalCopied); var readTask = this.sourceStream.ReadAsync(buffer, 0, numToRead); yield return(readTask); numRead = readTask.Result; if (numRead != 0) { // Verify the content StreamUtilities.ComputeHash(buffer, 0, numRead, md5Check); StreamUtilities.ComputeHash(buffer, 0, numRead, this.blobHash); var writeTask = memoryStream.WriteAsync(buffer, 0, numRead); yield return(writeTask); // Materialize any exceptions var scratch = writeTask.Result; Console.WriteLine(scratch); totalCopied += numRead; } }while (numRead != 0 && totalCopied < this.blockSize); // No locking necessary as only once active Dispense Task this.dispensizedStreamSize += totalCopied; if (totalCopied != 0) { string hashVal = StreamUtilities.GetHashValue(md5Check); string blockId = Utilities.GenerateBlockIDWithHash(hashVal, blockIdSequenceNumber); this.blockList.Add(blockId); memoryStream.Position = 0; setResult(memoryStream, blockId, hashVal); } else { memoryStream.Close(); setResult(null, null, null); } TraceHelper.WriteLine("Ending dispensBlockStream for id {0}", currentCallIndex); }
/// <summary> /// As a final step upload the block list to commit the blob. /// </summary> /// <returns>A <see cref="TaskSequence"/> that commits the blob.</returns> private TaskSequence CommitBlob() { string hashValue = StreamUtilities.GetHashValue(this.blobHash); this.blob.Properties.ContentMD5 = hashValue; // At the convenience layer we always upload uncommitted blocks List <PutBlockListItem> putBlockList = new List <PutBlockListItem>(); foreach (var id in this.blockList) { putBlockList.Add(new PutBlockListItem(id, BlockSearchMode.Uncommitted)); } return(this.blob.UploadBlockList(putBlockList, this.options)); }
/// <summary> /// Sets the MD5 of the blob. /// </summary> private void SetBlobMD5() { var hashValue = StreamUtilities.GetHashValue(this.blobHash); this.Blob.Properties.ContentMD5 = hashValue; }
/// <summary> /// Reads the data from the service starting at the specified location. Verifies the block's signature (if required) and adds it to the buffered data. /// </summary> /// <param name="startPosition">The starting position of the read-ahead.</param> /// <param name="length">The number of bytes to read ahead.</param> /// <returns> An TaskSequence that represents the asynchronous read action. </returns> private TaskSequence ReadAheadImpl(long startPosition, long length) { var webResponseTask = new InvokeTaskSequenceTask <Stream>((result) => { return(this.Blob.GetStreamImpl(options, startPosition, length, result)); }); yield return(webResponseTask); using (var stream = webResponseTask.Result) { this.LockToEtag(); if (this.IntegrityControlVerificationEnabled && this.Blob.Properties.BlobType == BlobType.BlockBlob) { long blockStartPosition = 0; foreach (var block in this.blockList) { var blockSize = block.Size; // Find the starting block if (blockStartPosition < startPosition) { blockStartPosition += blockSize; continue; } // Start creating blocks var memoryStream = new SmallBlockMemoryStream(Constants.DefaultBufferSize); var md5Check = MD5.Create(); int totalCopied = 0, numRead = 0; do { byte[] buffer = new byte[Constants.DefaultBufferSize]; var numToRead = (int)Math.Min(buffer.Length, blockSize - totalCopied); var readTask = stream.ReadAsync(buffer, 0, numToRead); yield return(readTask); numRead = readTask.Result; if (numRead != 0) { // Verify the content StreamUtilities.ComputeHash(buffer, 0, numRead, md5Check); var writeTask = memoryStream.WriteAsync(buffer, 0, numRead); yield return(writeTask); var scratch = writeTask.Result; // Materialize any exceptions totalCopied += numRead; } }while (numRead != 0 && totalCopied < blockSize); // If we read something, act on it if (totalCopied != 0) { // Verify the hash string blockNameMD5Value = Utilities.ExtractMD5ValueFromBlockID(block.Name); if (blockNameMD5Value != StreamUtilities.GetHashValue(md5Check)) { throw new InvalidDataException("Blob data corrupted (integrity check failed)"); } memoryStream.Position = 0; // Rewind the stream to allow for reading this.downloadedBlocksList.Add(new DownloadedBlock(startPosition, memoryStream)); startPosition += blockSize; blockStartPosition += blockSize; } else { break; } } } else { var memoryStream = new SmallBlockMemoryStream(Constants.DefaultBufferSize); var copyTask = new InvokeTaskSequenceTask(() => { return(stream.WriteTo(memoryStream)); }); yield return(copyTask); var scratch = copyTask.Result; // Materialize any errors memoryStream.Position = 0; // Rewind the stream to allow for reading this.downloadedBlocksList.Add(new DownloadedBlock(startPosition, memoryStream)); } } }
/// <summary> /// Generates a blockID using the block's hash and a prefix. /// </summary> /// <returns>The base64 encoded blockID.</returns> private string GetBlockID() { this.blockIdSequenceNumber += 1; return(Utilities.GenerateBlockIDWithHash(StreamUtilities.GetHashValue(this.blockHash), this.blockIdSequenceNumber)); }