| | 1 | | // Copyright (c) Microsoft Corporation. All rights reserved. |
| | 2 | | // Licensed under the MIT License. |
| | 3 | |
|
| | 4 | | using System.Collections.Generic; |
| | 5 | | using System.Linq; |
| | 6 | | using System.Threading; |
| | 7 | | using System.Threading.Tasks; |
| | 8 | | using Azure.Storage.Blobs; |
| | 9 | | using Azure.Storage.Blobs.Models; |
| | 10 | |
|
| | 11 | | namespace Azure.Storage.Files.DataLake.Models |
| | 12 | | { |
| | 13 | | /// <summary> |
| | 14 | | /// This class wraps the DateLakeServiceClient.GetFileSystemsAsync return values |
| | 15 | | /// and maps them into DataLake types. |
| | 16 | | /// </summary> |
| | 17 | | internal class GetFileSystemsAsyncCollection |
| | 18 | | { |
| | 19 | | private readonly BlobServiceClient _client; |
| | 20 | | private readonly FileSystemTraits _traits; |
| | 21 | | private readonly string _prefix; |
| | 22 | |
|
| 32 | 23 | | public GetFileSystemsAsyncCollection( |
| 32 | 24 | | BlobServiceClient client, |
| 32 | 25 | | FileSystemTraits traits, |
| 32 | 26 | | string prefix = default) |
| | 27 | | { |
| 32 | 28 | | _client = client; |
| 32 | 29 | | _traits = traits; |
| 32 | 30 | | _prefix = prefix; |
| 32 | 31 | | } |
| | 32 | |
|
| | 33 | | private static Page<FileSystemItem> ConvertPage(Page<BlobContainerItem> page) |
| | 34 | | { |
| 18 | 35 | | return Page<FileSystemItem>.FromValues( |
| 18 | 36 | | page.Values.Select(ConvertItem).ToArray(), |
| 18 | 37 | | page.ContinuationToken, |
| 18 | 38 | | page.GetRawResponse()); |
| | 39 | | } |
| | 40 | |
|
| | 41 | | private static FileSystemItem ConvertItem(BlobContainerItem item) |
| | 42 | | { |
| 19858 | 43 | | return item.ToFileSystemItem(); |
| | 44 | | } |
| | 45 | |
|
| | 46 | | private static AsyncPageable<BlobContainerItem> ConvertCollection(GetFileSystemsAsyncCollection collection, Canc |
| | 47 | | { |
| 18 | 48 | | return collection._client.GetBlobContainersAsync( |
| 18 | 49 | | (BlobContainerTraits)collection._traits, |
| 18 | 50 | | collection._prefix, |
| 18 | 51 | | cancellationToken); |
| | 52 | | } |
| | 53 | |
|
| | 54 | | public Pageable<FileSystemItem> ToSyncCollection(CancellationToken cancellationToken) |
| | 55 | | { |
| 14 | 56 | | return new StoragePageable(this, cancellationToken); |
| | 57 | | } |
| | 58 | |
|
| | 59 | | public AsyncPageable<FileSystemItem> ToAsyncCollection(CancellationToken cancellationToken) |
| | 60 | | { |
| 18 | 61 | | return new StorageAsyncPageable(this, cancellationToken); |
| | 62 | | } |
| | 63 | |
|
| | 64 | | /// <summary> |
| | 65 | | /// Abstract the Storage pattern for async iteration. |
| | 66 | | /// </summary> |
| | 67 | | private class StoragePageable : Pageable<FileSystemItem> |
| | 68 | | { |
| | 69 | | private GetFileSystemsAsyncCollection _collection; |
| | 70 | |
|
| | 71 | | public StoragePageable(GetFileSystemsAsyncCollection collection, CancellationToken cancellationToken) |
| 14 | 72 | | : base(cancellationToken) |
| | 73 | | { |
| 14 | 74 | | _collection = collection; |
| 14 | 75 | | } |
| | 76 | |
|
| | 77 | | /// <summary> |
| | 78 | | /// Enumerate the values a <see cref="Page{T}"/> at a time. This may |
| | 79 | | /// make mutliple service requests. |
| | 80 | | /// </summary> |
| | 81 | | /// <param name="continuationToken"> |
| | 82 | | /// A continuation token indicating where to resume paging or null to |
| | 83 | | /// begin paging from the beginning. |
| | 84 | | /// </param> |
| | 85 | | /// <param name="pageHintSize"> |
| | 86 | | /// The size of <see cref="Page{T}"/>s that should be requested (from |
| | 87 | | /// service operations that support it). |
| | 88 | | /// </param> |
| | 89 | | /// <returns> |
| | 90 | | /// An async sequence of <see cref="Page{T}"/>s. |
| | 91 | | /// </returns> |
| | 92 | | public override IEnumerable<Page<FileSystemItem>> AsPages( |
| | 93 | | string continuationToken = default, |
| | 94 | | int? pageHintSize = default) |
| | 95 | | { |
| 14 | 96 | | return _collection._client.GetBlobContainers( |
| 14 | 97 | | (BlobContainerTraits)_collection._traits, |
| 14 | 98 | | _collection._prefix, |
| 14 | 99 | | CancellationToken) |
| 14 | 100 | | .AsPages(continuationToken, pageHintSize) |
| 14 | 101 | | .Select(ConvertPage); |
| | 102 | | } |
| | 103 | |
|
| | 104 | | /// <summary> |
| | 105 | | /// Enumerate the values in the collection synchronously. This may |
| | 106 | | /// make multiple service requests. |
| | 107 | | /// </summary> |
| | 108 | | /// <returns>A sequence of values.</returns> |
| | 109 | | public override IEnumerator<FileSystemItem> GetEnumerator() |
| | 110 | | { |
| 0 | 111 | | return _collection._client.GetBlobContainers( |
| 0 | 112 | | (BlobContainerTraits)_collection._traits, |
| 0 | 113 | | _collection._prefix, |
| 0 | 114 | | CancellationToken) |
| 0 | 115 | | .Select(ConvertItem) |
| 0 | 116 | | .GetEnumerator(); |
| | 117 | | } |
| | 118 | | } |
| | 119 | |
|
| | 120 | | /// <summary> |
| | 121 | | /// Abstract the Storage pattern for async iteration. |
| | 122 | | /// </summary> |
| | 123 | | private class StorageAsyncPageable : AsyncPageable<FileSystemItem> |
| | 124 | | { |
| | 125 | | private GetFileSystemsAsyncCollection _collection; |
| | 126 | |
|
| | 127 | | // for mocking |
| | 128 | | protected StorageAsyncPageable() |
| 0 | 129 | | : base() |
| | 130 | | { |
| 0 | 131 | | } |
| | 132 | |
|
| | 133 | | public StorageAsyncPageable(GetFileSystemsAsyncCollection collection, CancellationToken cancellationToken) |
| 18 | 134 | | : base(cancellationToken) |
| | 135 | | { |
| 18 | 136 | | _collection = collection; |
| 18 | 137 | | } |
| | 138 | |
|
| | 139 | | /// <summary> |
| | 140 | | /// Enumerate the values a <see cref="Page{T}"/> at a time. This may |
| | 141 | | /// make mutliple service requests. |
| | 142 | | /// </summary> |
| | 143 | | /// <param name="continuationToken"> |
| | 144 | | /// A continuation token indicating where to resume paging or null to |
| | 145 | | /// begin paging from the beginning. |
| | 146 | | /// </param> |
| | 147 | | /// <param name="pageHintSize"> |
| | 148 | | /// The size of <see cref="Page{T}"/>s that should be requested (from |
| | 149 | | /// service operations that support it). |
| | 150 | | /// </param> |
| | 151 | | /// <returns> |
| | 152 | | /// An async sequence of <see cref="Page{T}"/>s. |
| | 153 | | /// </returns> |
| | 154 | | public override async IAsyncEnumerable<Page<FileSystemItem>> AsPages( |
| | 155 | | string continuationToken = default, |
| | 156 | | int? pageHintSize = default) |
| | 157 | | { |
| 6 | 158 | | IAsyncEnumerable<Page<BlobContainerItem>> pages = |
| 6 | 159 | | ConvertCollection(_collection, CancellationToken) |
| 6 | 160 | | .AsPages(continuationToken, pageHintSize); |
| | 161 | |
|
| 18 | 162 | | await foreach (Page<BlobContainerItem> page in pages.ConfigureAwait(false)) |
| | 163 | | { |
| 4 | 164 | | yield return ConvertPage(page); |
| | 165 | | } |
| 4 | 166 | | } |
| | 167 | |
|
| | 168 | | /// <summary> |
| | 169 | | /// Enumerate the values in the collection asynchronously. This may |
| | 170 | | /// make mutliple service requests. |
| | 171 | | /// </summary> |
| | 172 | | /// <param name="cancellationToken"> |
| | 173 | | /// The <see cref="CancellationToken"/> used for requests made while |
| | 174 | | /// enumerating asynchronously. |
| | 175 | | /// </param> |
| | 176 | | /// <returns>An async sequence of values.</returns> |
| | 177 | | public override async IAsyncEnumerator<FileSystemItem> GetAsyncEnumerator(CancellationToken cancellationToke |
| | 178 | | { |
| | 179 | | // This is the only method that takes its own CancellationToken, but |
| | 180 | | // we'll still use the original CancellationToken if one wasn't passed. |
| 12 | 181 | | if (cancellationToken == default) |
| | 182 | | { |
| 12 | 183 | | cancellationToken = CancellationToken; |
| | 184 | | } |
| | 185 | |
|
| 12 | 186 | | IAsyncEnumerable<Page<BlobContainerItem>> pages = |
| 12 | 187 | | ConvertCollection(_collection, cancellationToken) |
| 12 | 188 | | .AsPages(); |
| | 189 | |
|
| 48 | 190 | | await foreach (Page<BlobContainerItem> page in pages.ConfigureAwait(false)) |
| | 191 | | { |
| 19852 | 192 | | foreach (BlobContainerItem item in page.Values) |
| | 193 | | { |
| 9914 | 194 | | yield return ConvertItem(item); |
| | 195 | | } |
| | 196 | | } |
| 12 | 197 | | } |
| | 198 | | } |
| | 199 | | } |
| | 200 | | } |