Example #1
0
 private static Task WalkSingleBlockBlobAsync(
     Stream stream,
     SemaphoreSlim?blockActionSemaphore,
     SingleBlockBlobCallbackAsync singleBlockCallback,
     long bytesLeftInBlob)
 {
     return(ReadBlockAsync(
                stream,
                blockActionSemaphore,
                bytesLeftInBlob,
                async(blockBufferHandle, blockLength, blockHash) =>
     {
         try
         {
             var rollingId = new RollingBlobIdentifierWithBlocks();
             var blobIdentifierWithBlocks = rollingId.Finalize(blockHash);
             await singleBlockCallback(blockBufferHandle.Value, blockLength, blobIdentifierWithBlocks).ConfigureAwait(false);
         }
         finally
         {
             blockBufferHandle.Dispose();
             blockActionSemaphore?.Release();
         }
     }));
 }
Example #2
0
 private static void WalkSingleBlockBlob(Stream stream, SemaphoreSlim?blockActionSemaphore, SingleBlockBlobCallback singleBlockCallback, long bytesLeftInBlob)
 {
     ReadBlock(
         stream,
         blockActionSemaphore,
         bytesLeftInBlob,
         (blockBufferHandle, blockLength, blockHash) =>
     {
         try
         {
             var rollingId = new RollingBlobIdentifierWithBlocks();
             var blobIdentifierWithBlocks = rollingId.Finalize(blockHash);
             singleBlockCallback(blockBufferHandle.Value, blockLength, blobIdentifierWithBlocks);
         }
         finally
         {
             blockBufferHandle.Dispose();
             blockActionSemaphore?.Release();
         }
     });
 }
Example #3
0
        private static async Task WalkMultiBlockBlobAsync(
            Stream stream,
            SemaphoreSlim?blockActionSemaphore,
            bool multiBlocksInParallel,
            MultipleBlockBlobCallbackAsync multipleBlockCallback,
            MultipleBlockBlobSealCallbackAsync multipleBlockSealCallback,
            long bytesLeftInBlob)
        {
            var rollingId = new RollingBlobIdentifierWithBlocks();
            BlobIdentifierWithBlocks?blobIdentifierWithBlocks = null;

            var tasks = new List <Task>();

            do
            {
                await ReadBlockAsync(
                    stream,
                    blockActionSemaphore,
                    bytesLeftInBlob,
                    async (blockBufferHandle, blockLength, blockHash) =>
                {
                    bytesLeftInBlob  -= blockLength;
                    bool isFinalBlock = bytesLeftInBlob == 0;

                    try
                    {
                        if (isFinalBlock)
                        {
                            blobIdentifierWithBlocks = rollingId.Finalize(blockHash);
                        }
                        else
                        {
                            rollingId.Update(blockHash);
                        }
                    }
                    catch
                    {
                        CleanupBufferAndSemaphore(blockBufferHandle, blockActionSemaphore);
                        throw;
                    }

                    Task multiBlockTask = Task.Run(async() =>
                    {
                        try
                        {
                            await multipleBlockCallback(blockBufferHandle.Value, blockLength, blockHash, isFinalBlock).ConfigureAwait(false);
                        }
                        finally
                        {
                            CleanupBufferAndSemaphore(blockBufferHandle, blockActionSemaphore);
                        }
                    });
                    tasks.Add(multiBlockTask);

                    if (!multiBlocksInParallel)
                    {
                        await multiBlockTask.ConfigureAwait(false);
                    }
                }).ConfigureAwait(false);
            }while (bytesLeftInBlob > 0);

            await Task.WhenAll(tasks).ConfigureAwait(false);

            await multipleBlockSealCallback(blobIdentifierWithBlocks).ConfigureAwait(false);
        }
Example #4
0
        private static void WalkMultiBlockBlob(
            Stream stream,
            SemaphoreSlim?blockActionSemaphore,
            bool multiBlocksInParallel,
            MultipleBlockBlobCallback multipleBlockCallback,
            MultipleBlockBlobSealCallback multipleBlockSealCallback,
            long bytesLeftInBlob)
        {
            var rollingId = new RollingBlobIdentifierWithBlocks();
            BlobIdentifierWithBlocks?blobIdentifierWithBlocks = null;

            Lazy <List <Task> > tasks = new Lazy <List <Task> >(() => new List <Task>());

            do
            {
                ReadBlock(
                    stream,
                    blockActionSemaphore,
                    bytesLeftInBlob,
                    (blockBufferHandle, blockLength, blockHash) =>
                {
                    bytesLeftInBlob  -= blockLength;
                    bool isFinalBlock = bytesLeftInBlob == 0;

                    try
                    {
                        if (isFinalBlock)
                        {
                            blobIdentifierWithBlocks = rollingId.Finalize(blockHash);
                        }
                        else
                        {
                            rollingId.Update(blockHash);
                        }
                    }
                    catch
                    {
                        CleanupBufferAndSemaphore(blockBufferHandle, blockActionSemaphore);
                        throw;
                    }

                    if (multiBlocksInParallel)
                    {
                        tasks.Value.Add(Task.Run(() =>
                        {
                            try
                            {
                                multipleBlockCallback(blockBufferHandle.Value, blockLength, blockHash, isFinalBlock);
                            }
                            finally
                            {
                                CleanupBufferAndSemaphore(blockBufferHandle, blockActionSemaphore);
                            }
                        }));
                    }
                    else
                    {
                        try
                        {
                            multipleBlockCallback(blockBufferHandle.Value, blockLength, blockHash, isFinalBlock);
                        }
                        finally
                        {
                            CleanupBufferAndSemaphore(blockBufferHandle, blockActionSemaphore);
                        }
                    }
                });
            }while (bytesLeftInBlob > 0);

            if (tasks.IsValueCreated)
            {
                Task.WaitAll(tasks.Value.ToArray());
            }

            multipleBlockSealCallback(blobIdentifierWithBlocks);
        }