| | | 1 | | // Copyright (c) Microsoft Corporation. All rights reserved. |
| | | 2 | | // Licensed under the MIT License. |
| | | 3 | | |
| | | 4 | | using System; |
| | | 5 | | using System.Collections.Generic; |
| | | 6 | | using System.Threading.Tasks; |
| | | 7 | | using Azure.Storage.Blobs.Models; |
| | | 8 | | |
| | | 9 | | namespace Azure.Storage.Blobs.ChangeFeed |
| | | 10 | | { |
| | | 11 | | /// <summary> |
| | | 12 | | /// Builds a Shard. |
| | | 13 | | /// </summary> |
| | | 14 | | internal class ShardFactory |
| | | 15 | | { |
| | | 16 | | private readonly ChunkFactory _chunkFactory; |
| | | 17 | | private readonly BlobContainerClient _containerClient; |
| | | 18 | | |
| | 96 | 19 | | public ShardFactory( |
| | 96 | 20 | | BlobContainerClient containerClient, |
| | 96 | 21 | | ChunkFactory chunkFactory) |
| | | 22 | | { |
| | 96 | 23 | | _containerClient = containerClient; |
| | 96 | 24 | | _chunkFactory = chunkFactory; |
| | 96 | 25 | | } |
| | | 26 | | |
| | | 27 | | /// <summary> |
| | | 28 | | /// Constructor for mocking. |
| | | 29 | | /// </summary> |
| | 16 | 30 | | public ShardFactory() { } |
| | | 31 | | |
| | | 32 | | #pragma warning disable CA1822 // Does not acces instance data can be marked static. |
| | | 33 | | public virtual async Task<Shard> BuildShard( |
| | | 34 | | #pragma warning restore CA1822 // Can't mock static methods in MOQ. |
| | | 35 | | bool async, |
| | | 36 | | string shardPath, |
| | | 37 | | ShardCursor shardCursor = default) |
| | | 38 | | { |
| | | 39 | | // Models we'll need later |
| | 236 | 40 | | Queue<string> chunks = new Queue<string>(); |
| | 236 | 41 | | long blockOffset = shardCursor?.BlockOffset ?? 0; |
| | 236 | 42 | | long eventIndex = shardCursor?.EventIndex ?? 0; |
| | | 43 | | |
| | | 44 | | // Get Chunks |
| | 236 | 45 | | if (async) |
| | | 46 | | { |
| | 1004 | 47 | | await foreach (BlobHierarchyItem blobHierarchyItem in _containerClient.GetBlobsByHierarchyAsync( |
| | 226 | 48 | | prefix: shardPath).ConfigureAwait(false)) |
| | | 49 | | { |
| | 276 | 50 | | if (blobHierarchyItem.IsPrefix) |
| | | 51 | | continue; |
| | | 52 | | |
| | | 53 | | //Chunk chunk = new Chunk(_containerClient, blobHierarchyItem.Blob.Name); |
| | 276 | 54 | | chunks.Enqueue(blobHierarchyItem.Blob.Name); |
| | | 55 | | } |
| | | 56 | | } |
| | | 57 | | else |
| | | 58 | | { |
| | 140 | 59 | | foreach (BlobHierarchyItem blobHierarchyItem in _containerClient.GetBlobsByHierarchy( |
| | 10 | 60 | | prefix: shardPath)) |
| | | 61 | | { |
| | 60 | 62 | | if (blobHierarchyItem.IsPrefix) |
| | | 63 | | continue; |
| | | 64 | | |
| | 60 | 65 | | chunks.Enqueue(blobHierarchyItem.Blob.Name); |
| | | 66 | | } |
| | | 67 | | } |
| | | 68 | | |
| | 236 | 69 | | long chunkIndex = 0; |
| | 236 | 70 | | string currentChunkPath = shardCursor?.CurrentChunkPath; |
| | 236 | 71 | | Chunk currentChunk = null; |
| | 236 | 72 | | if (chunks.Count > 0) // Chunks can be empty right after hour flips. |
| | | 73 | | { |
| | | 74 | | // Fast forward to current Chunk |
| | 236 | 75 | | if (!string.IsNullOrWhiteSpace(currentChunkPath)) |
| | | 76 | | { |
| | 132 | 77 | | while (chunks.Count > 0) |
| | | 78 | | { |
| | 132 | 79 | | if (chunks.Peek() == currentChunkPath) |
| | | 80 | | { |
| | | 81 | | break; |
| | | 82 | | } |
| | | 83 | | else |
| | | 84 | | { |
| | 64 | 85 | | chunks.Dequeue(); |
| | 64 | 86 | | chunkIndex++; |
| | | 87 | | } |
| | | 88 | | } |
| | 68 | 89 | | if (chunks.Count == 0) |
| | | 90 | | { |
| | 0 | 91 | | throw new ArgumentException($"Chunk {currentChunkPath} not found."); |
| | | 92 | | } |
| | | 93 | | } |
| | | 94 | | |
| | 236 | 95 | | currentChunk = await _chunkFactory.BuildChunk( |
| | 236 | 96 | | async, |
| | 236 | 97 | | chunks.Dequeue(), |
| | 236 | 98 | | blockOffset, |
| | 236 | 99 | | eventIndex).ConfigureAwait(false); |
| | | 100 | | } |
| | | 101 | | |
| | 236 | 102 | | return new Shard( |
| | 236 | 103 | | _containerClient, |
| | 236 | 104 | | _chunkFactory, |
| | 236 | 105 | | chunks, |
| | 236 | 106 | | currentChunk, |
| | 236 | 107 | | chunkIndex, |
| | 236 | 108 | | shardPath); |
| | 236 | 109 | | } |
| | | 110 | | } |
| | | 111 | | } |