| | 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 | | { |
| 314 | 21 | | return new StoragePageable(this, cancellationToken); |
| | 22 | | } |
| | 23 | |
|
| | 24 | | public AsyncPageable<T> ToAsyncCollection(CancellationToken cancellationToken) |
| | 25 | | { |
| 342 | 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) |
| 314 | 43 | | : base(cancellationToken) |
| | 44 | | { |
| 314 | 45 | | _enumerator = enumerator; |
| 314 | 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) => |
| 296 | 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 | | { |
| 314 | 82 | | Page<T> page = _enumerator.GetNextPageAsync( |
| 314 | 83 | | continuationToken, |
| 314 | 84 | | pageHintSize, |
| 314 | 85 | | async: false, |
| 314 | 86 | | cancellationToken: CancellationToken) |
| 314 | 87 | | .EnsureCompleted(); |
| 308 | 88 | | continuationToken = page.ContinuationToken; |
| 308 | 89 | | yield return page; |
| 296 | 90 | | } while (CanContinue(continuationToken)); |
| 296 | 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) |
| 342 | 132 | | : base(cancellationToken) |
| | 133 | | { |
| 342 | 134 | | _enumerator = enumerator; |
| 342 | 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) => |
| 312 | 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 | | { |
| 48 | 171 | | Page<T> page = await _enumerator.GetNextPageAsync( |
| 48 | 172 | | continuationToken, |
| 48 | 173 | | pageHintSize, |
| 48 | 174 | | async: true, |
| 48 | 175 | | cancellationToken: CancellationToken) |
| 48 | 176 | | .ConfigureAwait(false); |
| 42 | 177 | | continuationToken = page.ContinuationToken; |
| 42 | 178 | | yield return page; |
| 34 | 179 | | } while (CanContinue(continuationToken)); |
| 42 | 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. |
| 294 | 195 | | if (cancellationToken == default) |
| | 196 | | { |
| 294 | 197 | | cancellationToken = CancellationToken; |
| | 198 | | } |
| | 199 | |
|
| 294 | 200 | | string continuationToken = null; |
| | 201 | | do |
| | 202 | | { |
| 294 | 203 | | Page<T> page = await _enumerator.GetNextPageAsync( |
| 294 | 204 | | continuationToken, |
| 294 | 205 | | null, |
| 294 | 206 | | async: true, |
| 294 | 207 | | cancellationToken: cancellationToken) |
| 294 | 208 | | .ConfigureAwait(false); |
| 290 | 209 | | continuationToken = page.ContinuationToken; |
| 11104 | 210 | | foreach (T item in page.Values) |
| | 211 | | { |
| 5268 | 212 | | yield return item; |
| | 213 | | } |
| 278 | 214 | | } while (CanContinue(continuationToken)); |
| 290 | 215 | | } |
| | 216 | | } |
| | 217 | | } |
| | 218 | | } |