| | 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 System.Threading; |
| | 8 | |
|
| | 9 | | namespace Azure.Storage.Blobs.ChangeFeed |
| | 10 | | { |
| | 11 | | internal class Shard |
| | 12 | | { |
| | 13 | | /// <summary> |
| | 14 | | /// Container Client for listing Chunks. |
| | 15 | | /// </summary> |
| | 16 | | private readonly BlobContainerClient _containerClient; |
| | 17 | |
|
| | 18 | | /// <summary> |
| | 19 | | /// ChunkFactory. |
| | 20 | | /// </summary> |
| | 21 | | private readonly ChunkFactory _chunkFactory; |
| | 22 | |
|
| | 23 | | /// <summary> |
| | 24 | | /// Queue of the paths to Chunks we haven't processed. |
| | 25 | | /// </summary> |
| | 26 | | private readonly Queue<string> _chunks; |
| | 27 | |
|
| | 28 | | /// <summary> |
| | 29 | | /// The Chunk we are currently processing. |
| | 30 | | /// </summary> |
| | 31 | | private Chunk _currentChunk; |
| | 32 | |
|
| | 33 | | /// <summary> |
| | 34 | | /// The index of the Chunk we are processing. |
| | 35 | | /// </summary> |
| | 36 | | private long _chunkIndex; |
| | 37 | |
|
| | 38 | | /// <summary> |
| | 39 | | /// The index of the Chunk we are processing. |
| | 40 | | /// </summary> |
| 164 | 41 | | public virtual string ShardPath { get; } |
| | 42 | |
|
| | 43 | | /// <summary> |
| | 44 | | /// Gets the <see cref="ShardCursor"/> for this Shard. |
| | 45 | | /// </summary> |
| | 46 | | public virtual ShardCursor GetCursor() |
| 400 | 47 | | => _currentChunk == null ? null : new ShardCursor( |
| 400 | 48 | | _currentChunk.ChunkPath, |
| 400 | 49 | | _currentChunk.BlockOffset, |
| 400 | 50 | | _currentChunk.EventIndex); |
| | 51 | |
|
| | 52 | | /// <summary> |
| | 53 | | /// If this Shard has a next event. |
| | 54 | | /// </summary> |
| | 55 | | public virtual bool HasNext() |
| 59420 | 56 | | => _chunks.Count > 0 || (_currentChunk != null && _currentChunk.HasNext()); |
| | 57 | |
|
| | 58 | | /// <summary> |
| | 59 | | /// Gets the next <see cref="BlobChangeFeedEvent"/>. |
| | 60 | | /// </summary> |
| | 61 | | public virtual async Task<BlobChangeFeedEvent> Next( |
| | 62 | | bool async, |
| | 63 | | CancellationToken cancellationToken = default) |
| | 64 | | { |
| 29612 | 65 | | if (!HasNext()) |
| | 66 | | { |
| 0 | 67 | | throw new InvalidOperationException("Shard doesn't have any more events"); |
| | 68 | | } |
| | 69 | |
|
| | 70 | | BlobChangeFeedEvent changeFeedEvent; |
| | 71 | |
|
| 29612 | 72 | | changeFeedEvent = await _currentChunk.Next(async, cancellationToken).ConfigureAwait(false); |
| | 73 | |
|
| | 74 | | // Remove currentChunk if it doesn't have another event. |
| 29612 | 75 | | if (!_currentChunk.HasNext() && _chunks.Count > 0) |
| | 76 | | { |
| 12 | 77 | | _currentChunk = await _chunkFactory.BuildChunk( |
| 12 | 78 | | async, |
| 12 | 79 | | _chunks.Dequeue(), |
| 12 | 80 | | cancellationToken: cancellationToken).ConfigureAwait(false); |
| 12 | 81 | | _chunkIndex++; |
| | 82 | | } |
| 29612 | 83 | | return changeFeedEvent; |
| 29612 | 84 | | } |
| | 85 | |
|
| | 86 | | /// <summary> |
| | 87 | | /// Constructor for use by <see cref="ShardFactory.BuildShard(bool, string, ShardCursor)"/>. |
| | 88 | | /// </summary> |
| 236 | 89 | | public Shard( |
| 236 | 90 | | BlobContainerClient containerClient, |
| 236 | 91 | | ChunkFactory chunkFactory, |
| 236 | 92 | | Queue<string> chunks, |
| 236 | 93 | | Chunk currentChunk, |
| 236 | 94 | | long chunkIndex, |
| 236 | 95 | | string shardPath) |
| | 96 | | { |
| 236 | 97 | | _containerClient = containerClient; |
| 236 | 98 | | _chunkFactory = chunkFactory; |
| 236 | 99 | | _chunks = chunks; |
| 236 | 100 | | _currentChunk = currentChunk; |
| 236 | 101 | | _chunkIndex = chunkIndex; |
| 236 | 102 | | ShardPath = shardPath; |
| 236 | 103 | | } |
| | 104 | |
|
| | 105 | | /// <summary> |
| | 106 | | /// Constructor for mocking. |
| | 107 | | /// </summary> |
| 48 | 108 | | internal Shard() { } |
| | 109 | | } |
| | 110 | | } |