| | | 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.Text.Json; |
| | | 7 | | using System.Threading.Tasks; |
| | | 8 | | using Azure.Storage.Blobs.Models; |
| | | 9 | | |
| | | 10 | | namespace Azure.Storage.Blobs.ChangeFeed |
| | | 11 | | { |
| | | 12 | | internal class SegmentFactory |
| | | 13 | | { |
| | | 14 | | private readonly BlobContainerClient _containerClient; |
| | | 15 | | private readonly ShardFactory _shardFactory; |
| | | 16 | | |
| | | 17 | | /// <summary> |
| | | 18 | | /// Constructor for mocking. |
| | | 19 | | /// </summary> |
| | 40 | 20 | | public SegmentFactory() { } |
| | | 21 | | |
| | 84 | 22 | | public SegmentFactory( |
| | 84 | 23 | | BlobContainerClient containerClient, |
| | 84 | 24 | | ShardFactory shardFactory) |
| | | 25 | | { |
| | 84 | 26 | | _containerClient = containerClient; |
| | 84 | 27 | | _shardFactory = shardFactory; |
| | 84 | 28 | | } |
| | | 29 | | |
| | | 30 | | #pragma warning disable CA1822 // Does not acces instance data can be marked static. |
| | | 31 | | public virtual async Task<Segment> BuildSegment( |
| | | 32 | | #pragma warning restore CA1822 // Can't mock static methods in MOQ. |
| | | 33 | | bool async, |
| | | 34 | | string manifestPath, |
| | | 35 | | SegmentCursor cursor = default) |
| | | 36 | | { |
| | | 37 | | // Models we need for later |
| | | 38 | | List<Shard> shards = new List<Shard>(); |
| | | 39 | | DateTimeOffset dateTime = BlobChangeFeedExtensions.ToDateTimeOffset(manifestPath).Value; |
| | | 40 | | |
| | | 41 | | // Download segment manifest |
| | | 42 | | BlobClient blobClient = _containerClient.GetBlobClient(manifestPath); |
| | | 43 | | BlobDownloadInfo blobDownloadInfo; |
| | | 44 | | |
| | | 45 | | if (async) |
| | | 46 | | { |
| | | 47 | | blobDownloadInfo = await blobClient.DownloadAsync().ConfigureAwait(false); |
| | | 48 | | } |
| | | 49 | | else |
| | | 50 | | { |
| | | 51 | | blobDownloadInfo = blobClient.Download(); |
| | | 52 | | } |
| | | 53 | | |
| | | 54 | | // Parse segment manifest |
| | | 55 | | JsonDocument jsonManifest; |
| | | 56 | | |
| | | 57 | | if (async) |
| | | 58 | | { |
| | | 59 | | jsonManifest = await JsonDocument.ParseAsync(blobDownloadInfo.Content).ConfigureAwait(false); |
| | | 60 | | } |
| | | 61 | | else |
| | | 62 | | { |
| | | 63 | | jsonManifest = JsonDocument.Parse(blobDownloadInfo.Content); |
| | | 64 | | } |
| | | 65 | | |
| | | 66 | | foreach (JsonElement shardJsonElement in jsonManifest.RootElement.GetProperty("chunkFilePaths").EnumerateArr |
| | | 67 | | { |
| | | 68 | | string shardPath = shardJsonElement.ToString().Substring("$blobchangefeed/".Length); |
| | 352 | 69 | | var shardCursor = cursor?.ShardCursors?.Find(x => x.CurrentChunkPath.StartsWith(shardPath, StringCompari |
| | | 70 | | Shard shard = await _shardFactory.BuildShard( |
| | | 71 | | async, |
| | | 72 | | shardPath, |
| | | 73 | | shardCursor) |
| | | 74 | | .ConfigureAwait(false); |
| | | 75 | | if (shard.HasNext()) |
| | | 76 | | { |
| | | 77 | | shards.Add(shard); |
| | | 78 | | } |
| | | 79 | | } |
| | | 80 | | |
| | | 81 | | int shardIndex = 0; |
| | | 82 | | string currentShardPath = cursor?.CurrentShardPath; |
| | | 83 | | if (!string.IsNullOrWhiteSpace(currentShardPath)) |
| | | 84 | | { |
| | 72 | 85 | | shardIndex = shards.FindIndex(s => s.ShardPath == currentShardPath); |
| | | 86 | | if (shardIndex < 0) |
| | | 87 | | { |
| | | 88 | | throw new ArgumentException($"Shard {currentShardPath} not found."); |
| | | 89 | | } |
| | | 90 | | } |
| | | 91 | | return new Segment( |
| | | 92 | | shards, |
| | | 93 | | shardIndex, |
| | | 94 | | dateTime, |
| | | 95 | | manifestPath); |
| | | 96 | | } |
| | | 97 | | } |
| | | 98 | | } |