| | 1 | | // Copyright (c) Microsoft Corporation. All rights reserved. |
| | 2 | | // Licensed under the MIT License. |
| | 3 | |
|
| | 4 | | using System.Collections.Generic; |
| | 5 | | using System.Threading; |
| | 6 | | using System.Threading.Tasks; |
| | 7 | | using Azure.Core.Pipeline; |
| | 8 | |
|
| | 9 | | namespace Azure.Storage |
| | 10 | | { |
| | 11 | | internal abstract class StorageCollectionEnumerator<T> |
| | 12 | | { |
| | 13 | | public abstract ValueTask<Page<T>> GetNextPageAsync( |
| | 14 | | string continuationToken, |
| | 15 | | int? pageSizeHint, |
| | 16 | | bool async, |
| | 17 | | CancellationToken cancellationToken); |
| | 18 | |
|
| | 19 | | public Pageable<T> ToSyncCollection(CancellationToken cancellationToken) |
| | 20 | | { |
| 18 | 21 | | return new StoragePageable(this, cancellationToken); |
| | 22 | | } |
| | 23 | |
|
| | 24 | | public AsyncPageable<T> ToAsyncCollection(CancellationToken cancellationToken) |
| | 25 | | { |
| 20 | 26 | | return new StorageAsyncPageable(this, cancellationToken); |
| | 27 | | } |
| | 28 | |
|
| | 29 | | /// <summary> |
| | 30 | | /// Abstract the Storage pattern for async iteration |
| | 31 | | /// </summary> |
| | 32 | | private class StoragePageable : Pageable<T> |
| | 33 | | { |
| | 34 | | private StorageCollectionEnumerator<T> _enumerator; |
| | 35 | |
|
| | 36 | | // for mocking |
| | 37 | | protected StoragePageable() |
| 0 | 38 | | : base() |
| | 39 | | { |
| 0 | 40 | | } |
| | 41 | |
|
| | 42 | | public StoragePageable(StorageCollectionEnumerator<T> enumerator, CancellationToken cancellationToken) |
| 18 | 43 | | : base(cancellationToken) |
| | 44 | | { |
| 18 | 45 | | _enumerator = enumerator; |
| 18 | 46 | | } |
| | 47 | |
|
| | 48 | | /// <summary> |
| | 49 | | /// Determine if the iteration can continue. |
| | 50 | | /// </summary> |
| | 51 | | /// <param name="continuationToken"> |
| | 52 | | /// The next continuation token provided with the last |
| | 53 | | /// <see cref="Page{T}"/>. |
| | 54 | | /// </param> |
| | 55 | | /// <returns> |
| | 56 | | /// True if the iteration can continue, false otherwise. |
| | 57 | | /// </returns> |
| | 58 | | protected virtual bool CanContinue(string continuationToken) => |
| 16 | 59 | | !string.IsNullOrEmpty(continuationToken); |
| | 60 | |
|
| | 61 | | /// <summary> |
| | 62 | | /// Enumerate the values a <see cref="Page{T}"/> at a time. This may |
| | 63 | | /// make mutliple service requests. |
| | 64 | | /// </summary> |
| | 65 | | /// <param name="continuationToken"> |
| | 66 | | /// A continuation token indicating where to resume paging or null to |
| | 67 | | /// begin paging from the beginning. |
| | 68 | | /// </param> |
| | 69 | | /// <param name="pageHintSize"> |
| | 70 | | /// The size of <see cref="Page{T}"/>s that should be requested (from |
| | 71 | | /// service operations that support it). |
| | 72 | | /// </param> |
| | 73 | | /// <returns> |
| | 74 | | /// An async sequence of <see cref="Page{T}"/>s. |
| | 75 | | /// </returns> |
| | 76 | | public override IEnumerable<Page<T>> AsPages( |
| | 77 | | string continuationToken = default, |
| | 78 | | int? pageHintSize = default) |
| | 79 | | { |
| | 80 | | do |
| | 81 | | { |
| 18 | 82 | | Page<T> page = _enumerator.GetNextPageAsync( |
| 18 | 83 | | continuationToken, |
| 18 | 84 | | pageHintSize, |
| 18 | 85 | | async: false, |
| 18 | 86 | | cancellationToken: CancellationToken) |
| 18 | 87 | | .EnsureCompleted(); |
| 18 | 88 | | continuationToken = page.ContinuationToken; |
| 18 | 89 | | yield return page; |
| 16 | 90 | | } while (CanContinue(continuationToken)); |
| 16 | 91 | | } |
| | 92 | |
|
| | 93 | | /// <summary> |
| | 94 | | /// Enumerate the values in the collection synchronously. This may |
| | 95 | | /// make mutliple service requests. |
| | 96 | | /// </summary> |
| | 97 | | /// <returns>A sequence of values.</returns> |
| | 98 | | public override IEnumerator<T> GetEnumerator() |
| | 99 | | { |
| 0 | 100 | | string continuationToken = null; |
| | 101 | | do |
| | 102 | | { |
| 0 | 103 | | Page<T> page = _enumerator.GetNextPageAsync( |
| 0 | 104 | | continuationToken, |
| 0 | 105 | | null, |
| 0 | 106 | | async: false, |
| 0 | 107 | | cancellationToken: CancellationToken) |
| 0 | 108 | | .EnsureCompleted(); |
| 0 | 109 | | continuationToken = page.ContinuationToken; |
| 0 | 110 | | foreach (T item in page.Values) |
| | 111 | | { |
| 0 | 112 | | yield return item; |
| | 113 | | } |
| 0 | 114 | | } while (CanContinue(continuationToken)); |
| 0 | 115 | | } |
| | 116 | | } |
| | 117 | |
|
| | 118 | | /// <summary> |
| | 119 | | /// Abstract the Storage pattern for async iteration |
| | 120 | | /// </summary> |
| | 121 | | private class StorageAsyncPageable : AsyncPageable<T> |
| | 122 | | { |
| | 123 | | private StorageCollectionEnumerator<T> _enumerator; |
| | 124 | |
|
| | 125 | | // for mocking |
| | 126 | | protected StorageAsyncPageable() |
| 0 | 127 | | : base() |
| | 128 | | { |
| 0 | 129 | | } |
| | 130 | |
|
| | 131 | | public StorageAsyncPageable(StorageCollectionEnumerator<T> enumerator, CancellationToken cancellationToken) |
| 20 | 132 | | : base(cancellationToken) |
| | 133 | | { |
| 20 | 134 | | _enumerator = enumerator; |
| 20 | 135 | | } |
| | 136 | |
|
| | 137 | | /// <summary> |
| | 138 | | /// Determine if the iteration can continue. |
| | 139 | | /// </summary> |
| | 140 | | /// <param name="continuationToken"> |
| | 141 | | /// The next continuation token provided with the last |
| | 142 | | /// <see cref="Page{T}"/>. |
| | 143 | | /// </param> |
| | 144 | | /// <returns> |
| | 145 | | /// True if the iteration can continue, false otherwise. |
| | 146 | | /// </returns> |
| | 147 | | protected virtual bool CanContinue(string continuationToken) => |
| 14 | 148 | | !string.IsNullOrEmpty(continuationToken); |
| | 149 | |
|
| | 150 | | /// <summary> |
| | 151 | | /// Enumerate the values a <see cref="Page{T}"/> at a time. This may |
| | 152 | | /// make mutliple service requests. |
| | 153 | | /// </summary> |
| | 154 | | /// <param name="continuationToken"> |
| | 155 | | /// A continuation token indicating where to resume paging or null to |
| | 156 | | /// begin paging from the beginning. |
| | 157 | | /// </param> |
| | 158 | | /// <param name="pageHintSize"> |
| | 159 | | /// The size of <see cref="Page{T}"/>s that should be requested (from |
| | 160 | | /// service operations that support it). |
| | 161 | | /// </param> |
| | 162 | | /// <returns> |
| | 163 | | /// An async sequence of <see cref="Page{T}"/>s. |
| | 164 | | /// </returns> |
| | 165 | | public override async IAsyncEnumerable<Page<T>> AsPages( |
| | 166 | | string continuationToken = default, |
| | 167 | | int? pageHintSize = default) |
| | 168 | | { |
| | 169 | | do |
| | 170 | | { |
| 6 | 171 | | Page<T> page = await _enumerator.GetNextPageAsync( |
| 6 | 172 | | continuationToken, |
| 6 | 173 | | pageHintSize, |
| 6 | 174 | | async: true, |
| 6 | 175 | | cancellationToken: CancellationToken) |
| 6 | 176 | | .ConfigureAwait(false); |
| 4 | 177 | | continuationToken = page.ContinuationToken; |
| 4 | 178 | | yield return page; |
| 2 | 179 | | } while (CanContinue(continuationToken)); |
| 4 | 180 | | } |
| | 181 | |
|
| | 182 | | /// <summary> |
| | 183 | | /// Enumerate the values in the collection asynchronously. This may |
| | 184 | | /// make mutliple service requests. |
| | 185 | | /// </summary> |
| | 186 | | /// <param name="cancellationToken"> |
| | 187 | | /// The <see cref="CancellationToken"/> used for requests made while |
| | 188 | | /// enumerating asynchronously. |
| | 189 | | /// </param> |
| | 190 | | /// <returns>An async sequence of values.</returns> |
| | 191 | | public override async IAsyncEnumerator<T> GetAsyncEnumerator(CancellationToken cancellationToken = default) |
| | 192 | | { |
| | 193 | | // This is the only method that takes its own CancellationToken, but |
| | 194 | | // we'll still use the original CancellationToken if one wasn't passed. |
| 14 | 195 | | if (cancellationToken == default) |
| | 196 | | { |
| 14 | 197 | | cancellationToken = CancellationToken; |
| | 198 | | } |
| | 199 | |
|
| 14 | 200 | | string continuationToken = null; |
| | 201 | | do |
| | 202 | | { |
| 14 | 203 | | Page<T> page = await _enumerator.GetNextPageAsync( |
| 14 | 204 | | continuationToken, |
| 14 | 205 | | null, |
| 14 | 206 | | async: true, |
| 14 | 207 | | cancellationToken: cancellationToken) |
| 14 | 208 | | .ConfigureAwait(false); |
| 14 | 209 | | continuationToken = page.ContinuationToken; |
| 62 | 210 | | foreach (T item in page.Values) |
| | 211 | | { |
| 18 | 212 | | yield return item; |
| | 213 | | } |
| 12 | 214 | | } while (CanContinue(continuationToken)); |
| 14 | 215 | | } |
| | 216 | | } |
| | 217 | | } |
| | 218 | | } |