| | | 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; |
| | | 7 | | using System.Threading.Tasks; |
| | | 8 | | using Azure.Storage.Blobs.Models; |
| | | 9 | | |
| | | 10 | | namespace Azure.Storage.Blobs.ChangeFeed |
| | | 11 | | { |
| | | 12 | | internal class Segment |
| | | 13 | | { |
| | | 14 | | /// <summary> |
| | | 15 | | /// The time (to the nearest hour) associated with this Segment. |
| | | 16 | | /// </summary> |
| | 696 | 17 | | public DateTimeOffset DateTime { get; private set; } |
| | | 18 | | |
| | | 19 | | /// <summary> |
| | | 20 | | /// The path of manifest associated with this Segment. |
| | | 21 | | /// </summary> |
| | 264 | 22 | | public string ManifestPath { get; private set; } |
| | | 23 | | |
| | | 24 | | /// <summary> |
| | | 25 | | /// The Shards associated with this Segment. |
| | | 26 | | /// </summary> |
| | | 27 | | private readonly List<Shard> _shards; |
| | | 28 | | |
| | | 29 | | /// <summary> |
| | | 30 | | /// The Shards we have finished reading from. |
| | | 31 | | /// </summary> |
| | | 32 | | private readonly HashSet<int> _finishedShards; |
| | | 33 | | |
| | | 34 | | /// <summary> |
| | | 35 | | /// The index of the Shard we will return the next event from. |
| | | 36 | | /// </summary> |
| | | 37 | | private int _shardIndex; |
| | | 38 | | |
| | 128 | 39 | | public Segment( |
| | 128 | 40 | | List<Shard> shards, |
| | 128 | 41 | | int shardIndex, |
| | 128 | 42 | | DateTimeOffset dateTime, |
| | 128 | 43 | | string manifestPath) |
| | | 44 | | { |
| | 128 | 45 | | _shards = shards; |
| | 128 | 46 | | _shardIndex = shardIndex; |
| | 128 | 47 | | DateTime = dateTime; |
| | 128 | 48 | | ManifestPath = manifestPath; |
| | 128 | 49 | | _finishedShards = new HashSet<int>(); |
| | 128 | 50 | | } |
| | | 51 | | |
| | | 52 | | public virtual SegmentCursor GetCursor() |
| | | 53 | | { |
| | 136 | 54 | | List<ShardCursor> shardCursors = new List<ShardCursor>(); |
| | 688 | 55 | | foreach (Shard shard in _shards) |
| | | 56 | | { |
| | 208 | 57 | | var shardCursor = shard.GetCursor(); |
| | 208 | 58 | | if (shardCursor != null) |
| | | 59 | | { |
| | 208 | 60 | | shardCursors.Add(shard.GetCursor()); |
| | | 61 | | } |
| | | 62 | | } |
| | 136 | 63 | | return new SegmentCursor( |
| | 136 | 64 | | segmentPath: ManifestPath, |
| | 136 | 65 | | shardCursors: shardCursors, |
| | 136 | 66 | | currentShardPath: _shards[_shardIndex].ShardPath); |
| | | 67 | | } |
| | | 68 | | |
| | | 69 | | public virtual async Task<List<BlobChangeFeedEvent>> GetPage( |
| | | 70 | | bool async, |
| | | 71 | | int? pageSize, |
| | | 72 | | CancellationToken cancellationToken = default) |
| | | 73 | | { |
| | 148 | 74 | | List<BlobChangeFeedEvent> changeFeedEventList = new List<BlobChangeFeedEvent>(); |
| | | 75 | | |
| | 148 | 76 | | if (!HasNext()) |
| | | 77 | | { |
| | 0 | 78 | | throw new InvalidOperationException("Segment doesn't have any more events"); |
| | | 79 | | } |
| | | 80 | | |
| | 148 | 81 | | int i = 0; |
| | 29900 | 82 | | while (i < pageSize && _shards.Count > 0) |
| | | 83 | | { |
| | | 84 | | // If this Shard is finished, skip it. |
| | 29816 | 85 | | if (_finishedShards.Contains(_shardIndex)) |
| | | 86 | | { |
| | 216 | 87 | | _shardIndex++; |
| | | 88 | | |
| | 216 | 89 | | if (_shardIndex == _shards.Count) |
| | | 90 | | { |
| | 0 | 91 | | _shardIndex = 0; |
| | | 92 | | } |
| | | 93 | | |
| | 0 | 94 | | continue; |
| | | 95 | | } |
| | | 96 | | |
| | 29600 | 97 | | Shard currentShard = _shards[_shardIndex]; |
| | | 98 | | |
| | 29600 | 99 | | BlobChangeFeedEvent changeFeedEvent = await currentShard.Next(async, cancellationToken).ConfigureAwait(f |
| | | 100 | | |
| | 29600 | 101 | | changeFeedEventList.Add(changeFeedEvent); |
| | | 102 | | |
| | | 103 | | // If the current shard is completed, remove it from _shards |
| | 29600 | 104 | | if (!currentShard.HasNext()) |
| | | 105 | | { |
| | 88 | 106 | | _finishedShards.Add(_shardIndex); |
| | | 107 | | } |
| | | 108 | | |
| | 29600 | 109 | | i++; |
| | 29600 | 110 | | _shardIndex++; |
| | 29600 | 111 | | if (_shardIndex >= _shards.Count) |
| | | 112 | | { |
| | 26776 | 113 | | _shardIndex = 0; |
| | | 114 | | } |
| | | 115 | | |
| | | 116 | | // If all the Shards are finished, we need to break out early. |
| | 29600 | 117 | | if (_finishedShards.Count == _shards.Count) |
| | | 118 | | { |
| | | 119 | | break; |
| | | 120 | | } |
| | 29536 | 121 | | } |
| | | 122 | | |
| | 148 | 123 | | return changeFeedEventList; |
| | 148 | 124 | | } |
| | | 125 | | |
| | | 126 | | public virtual bool HasNext() |
| | 436 | 127 | | => _finishedShards.Count < _shards.Count; |
| | | 128 | | |
| | | 129 | | /// <summary> |
| | | 130 | | /// Constructor for mocking. |
| | | 131 | | /// </summary> |
| | 56 | 132 | | public Segment() { } |
| | | 133 | | } |
| | | 134 | | } |