From 21348142b4c711c3b2d172d070021921a95d0a6a Mon Sep 17 00:00:00 2001 From: Pavel Krymets Date: Mon, 29 Jul 2019 15:50:20 -0700 Subject: [PATCH] Move async collection to core (#7038) --- sdk/core/Azure.Core/src/AsyncCollection.cs | 95 +++++++++ sdk/core/Azure.Core/src/Page.cs | 82 ++++++++ .../tests/ContainerClientTests.cs | 4 +- .../src/AsyncCollection.cs | 192 ------------------ .../src/StorageAsyncCollection.cs | 2 +- .../tests/ServiceClientTests.cs | 2 +- 6 files changed, 181 insertions(+), 196 deletions(-) create mode 100644 sdk/core/Azure.Core/src/AsyncCollection.cs create mode 100644 sdk/core/Azure.Core/src/Page.cs delete mode 100644 sdk/storage/Azure.Storage.Common/src/AsyncCollection.cs diff --git a/sdk/core/Azure.Core/src/AsyncCollection.cs b/sdk/core/Azure.Core/src/AsyncCollection.cs new file mode 100644 index 0000000000000..717c81039c1b5 --- /dev/null +++ b/sdk/core/Azure.Core/src/AsyncCollection.cs @@ -0,0 +1,95 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. + +using System.Collections.Generic; +using System.ComponentModel; +using System.Threading; + +namespace Azure +{ + /// + /// A collection of values that may take multiple service requests to + /// iterate over. + /// + /// The type of the values. + public abstract class AsyncCollection : IAsyncEnumerable> + { + /// + /// Gets a used for requests made while + /// enumerating asynchronously. + /// + protected virtual CancellationToken CancellationToken { get; } + + /// + /// Initializes a new instance of the + /// class for mocking. + /// + protected AsyncCollection() => + this.CancellationToken = CancellationToken.None; + + /// + /// Initializes a new instance of the + /// class. + /// + /// + /// The used for requests made while + /// enumerating asynchronously. + /// + protected AsyncCollection(CancellationToken cancellationToken) => + this.CancellationToken = cancellationToken; + + /// + /// Enumerate the values a at a time. This may + /// make mutliple service requests. + /// + /// + /// A continuation token indicating where to resume paging or null to + /// begin paging from the beginning. + /// + /// + /// The size of s that should be requested (from + /// service operations that support it). + /// + /// + /// An async sequence of s. + /// + public abstract IAsyncEnumerable> ByPage( + string continuationToken = default, + int? pageSizeHint = default); + + /// + /// Enumerate the values in the collection asynchronously. This may + /// make mutliple service requests. + /// + /// + /// The used for requests made while + /// enumerating asynchronously. + /// + /// An async sequence of values. + public abstract IAsyncEnumerator> GetAsyncEnumerator(CancellationToken cancellationToken = default); + + /// + /// Creates a string representation of an . + /// + /// + /// A string representation of an . + /// + [EditorBrowsable(EditorBrowsableState.Never)] + public override string ToString() => base.ToString(); + + /// + /// Check if two instances are equal. + /// + /// The instance to compare to. + /// True if they're equal, false otherwise. + [EditorBrowsable(EditorBrowsableState.Never)] + public override bool Equals(object obj) => base.Equals(obj); + + /// + /// Get a hash code for the . + /// + /// Hash code for the . + [EditorBrowsable(EditorBrowsableState.Never)] + public override int GetHashCode() => base.GetHashCode(); + } +} diff --git a/sdk/core/Azure.Core/src/Page.cs b/sdk/core/Azure.Core/src/Page.cs new file mode 100644 index 0000000000000..ec259a8b184f6 --- /dev/null +++ b/sdk/core/Azure.Core/src/Page.cs @@ -0,0 +1,82 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. + +using System.Collections.Generic; +using System.ComponentModel; + +namespace Azure +{ + /// + /// A single of values from a request that may return + /// zero or more s of values. + /// + /// The type of values. + public readonly struct Page + { + /// + /// Gets the values in this . + /// + public IReadOnlyList Values { get; } + + /// + /// Gets the continuation token used to request the next + /// . The continuation token may be null or + /// empty when there are no more pages. + /// + public string ContinuationToken { get; } + + /// + /// The that provided this . + /// + private readonly Response _response; + + /// + /// Gets the that provided this + /// . + /// + public Response GetRawResponse() => this._response; + + /// + /// Creates a new . + /// + /// + /// The values in this . + /// + /// + /// The continuation token used to request the next . + /// + /// + /// The that provided this . + /// + public Page(IReadOnlyList values, string continuationToken, Response response) + { + this.Values = values; + this.ContinuationToken = continuationToken; + this._response = response; + } + + /// + /// Creates a string representation of an . + /// + /// + /// A string representation of an . + /// + [EditorBrowsable(EditorBrowsableState.Never)] + public override string ToString() => base.ToString(); + + /// + /// Check if two instances are equal. + /// + /// The instance to compare to. + /// True if they're equal, false otherwise. + [EditorBrowsable(EditorBrowsableState.Never)] + public override bool Equals(object obj) => base.Equals(obj); + + /// + /// Get a hash code for the . + /// + /// Hash code for the . + [EditorBrowsable(EditorBrowsableState.Never)] + public override int GetHashCode() => base.GetHashCode(); + } +} diff --git a/sdk/storage/Azure.Storage.Blobs/tests/ContainerClientTests.cs b/sdk/storage/Azure.Storage.Blobs/tests/ContainerClientTests.cs index 47f429c31e915..35f974d0d55f2 100644 --- a/sdk/storage/Azure.Storage.Blobs/tests/ContainerClientTests.cs +++ b/sdk/storage/Azure.Storage.Blobs/tests/ContainerClientTests.cs @@ -1161,7 +1161,7 @@ public async Task ListBlobsFlatSegmentAsync_MaxResults() var page = await container.GetBlobsAsync().ByPage(pageSizeHint: 2).FirstAsync(); // Assert - Assert.AreEqual(2, page.Values.Length); + Assert.AreEqual(2, page.Values.Count); } } @@ -1365,7 +1365,7 @@ public async Task ListBlobsHierarchySegmentAsync_MaxResults() .FirstAsync(); // Assert - Assert.AreEqual(2, page.Values.Length); + Assert.AreEqual(2, page.Values.Count); } } diff --git a/sdk/storage/Azure.Storage.Common/src/AsyncCollection.cs b/sdk/storage/Azure.Storage.Common/src/AsyncCollection.cs deleted file mode 100644 index 06128838dbb8e..0000000000000 --- a/sdk/storage/Azure.Storage.Common/src/AsyncCollection.cs +++ /dev/null @@ -1,192 +0,0 @@ -// Copyright (c) Microsoft Corporation. All rights reserved. -// Licensed under the MIT License. See License.txt in the project root for -// license information. - -using System; -using System.Collections.Generic; -using System.ComponentModel; -using System.Threading; - -// TODO: #6807: Move these types into the Azure.Core package next release? - -namespace Azure -{ - /// - /// A collection of values that may take multiple service requests to - /// iterate over. - /// - /// The type of the values. - public abstract class AsyncCollection : IAsyncEnumerable> - { - /// - /// Gets a used for requests made while - /// enumerating asynchronously. - /// - protected virtual CancellationToken CancellationToken { get; private set; } - - /// - /// Initializes a new instance of the - /// class for mocking. - /// - protected AsyncCollection() => - this.CancellationToken = CancellationToken.None; - - /// - /// Initializes a new instance of the - /// class. - /// - /// - /// The used for requests made while - /// enumerating asynchronously. - /// - protected AsyncCollection(CancellationToken cancellationToken) => - this.CancellationToken = cancellationToken; - - /// - /// Enumerate the values a at a time. This may - /// make mutliple service requests. - /// - /// - /// A continuation token indicating where to resume paging or null to - /// begin paging from the beginning. - /// - /// - /// The size of s that should be requested (from - /// service operations that support it). - /// - /// - /// An async sequence of s. - /// - public abstract IAsyncEnumerable> ByPage( - string continuationToken = default, - int? pageSizeHint = default); - - /// - /// Enumerate the values in the collection asynchronously. This may - /// make mutliple service requests. - /// - /// - /// The used for requests made while - /// enumerating asynchronously. - /// - /// An async sequence of values. - public abstract IAsyncEnumerator> GetAsyncEnumerator(CancellationToken cancellationToken = default); - - /// - /// Enumerate the values in the collection synchronously. This may - /// make mutliple service requests. - /// - /// A sequence of values. - protected abstract IEnumerator> GetEnumerator(); - - /// - /// Creates a string representation of an . - /// - /// - /// A string representation of an . - /// - [EditorBrowsable(EditorBrowsableState.Never)] - public override string ToString() => base.ToString(); - - /// - /// Check if two instances are equal. - /// - /// The instance to compare to. - /// True if they're equal, false otherwise. - [EditorBrowsable(EditorBrowsableState.Never)] - public override bool Equals(object obj) => base.Equals(obj); - - /// - /// Get a hash code for the . - /// - /// Hash code for the . - [EditorBrowsable(EditorBrowsableState.Never)] - public override int GetHashCode() => base.GetHashCode(); - } - -#pragma warning disable CA1815 // Override equals and operator equals on value types -#pragma warning disable CA2231 // Overload operator equals on overriding value type Equals -#pragma warning disable CA1066 // Type {0} should implement IEquatable because it overrides Equals - /// - /// A single of values from a request that may return - /// zero or more s of values. - /// - /// The type of values. - public readonly struct Page -#pragma warning restore CA1066 // Type {0} should implement IEquatable because it overrides Equals -#pragma warning restore CA2231 // Overload operator equals on overriding value type Equals -#pragma warning restore CA1815 // Override equals and operator equals on value types - { -// TODO: Should this be Items instead of Values? -// - Probably not with Response.Values -// TODO: Should it be IEnumerable? - #pragma warning disable CA1819 // Properties should not return arrays - /// - /// Gets the values in this . - /// - public T[] Values { get; } - #pragma warning restore CA1819 // Properties should not return arrays - - // TODO: Should this be object instead of string? - /// - /// Gets the continuation token used to request the next - /// . The continuation token may be null or - /// empty when there are no more pages. - /// - public string ContinuationToken { get; } - - /// - /// The that provided this . - /// - private readonly Response _response; - - /// - /// Gets the that provided this - /// . - /// - public Response GetRawResponse() => this._response; - - /// - /// Creates a new . - /// - /// - /// The values in this . - /// - /// - /// The continuation token used to request the next . - /// - /// - /// The that provided this . - /// - public Page(T[] values, string continuationToken, Response response) - { - this.Values = values; - this.ContinuationToken = continuationToken; - this._response = response; - } - - /// - /// Creates a string representation of an . - /// - /// - /// A string representation of an . - /// - [EditorBrowsable(EditorBrowsableState.Never)] - public override string ToString() => base.ToString(); - - /// - /// Check if two instances are equal. - /// - /// The instance to compare to. - /// True if they're equal, false otherwise. - [EditorBrowsable(EditorBrowsableState.Never)] - public override bool Equals(object obj) => base.Equals(obj); - - /// - /// Get a hash code for the . - /// - /// Hash code for the . - [EditorBrowsable(EditorBrowsableState.Never)] - public override int GetHashCode() => base.GetHashCode(); - } -} diff --git a/sdk/storage/Azure.Storage.Common/src/StorageAsyncCollection.cs b/sdk/storage/Azure.Storage.Common/src/StorageAsyncCollection.cs index e0d01130ce883..e378831a31481 100644 --- a/sdk/storage/Azure.Storage.Common/src/StorageAsyncCollection.cs +++ b/sdk/storage/Azure.Storage.Common/src/StorageAsyncCollection.cs @@ -138,7 +138,7 @@ public override async IAsyncEnumerator> GetAsyncEnumerator(Cancellat /// make mutliple service requests. /// /// A sequence of values. - protected override IEnumerator> GetEnumerator() + protected IEnumerator> GetEnumerator() { string continuationToken = null; do diff --git a/sdk/storage/Azure.Storage.Queues/tests/ServiceClientTests.cs b/sdk/storage/Azure.Storage.Queues/tests/ServiceClientTests.cs index 8ddb2657c03d7..e3b81f989cdbd 100644 --- a/sdk/storage/Azure.Storage.Queues/tests/ServiceClientTests.cs +++ b/sdk/storage/Azure.Storage.Queues/tests/ServiceClientTests.cs @@ -63,7 +63,7 @@ public async Task GetQueuesAsync_MaxResults() service.GetQueuesAsync() .ByPage(pageSizeHint: 1) .FirstAsync(); - Assert.AreEqual(1, page.Values.Length); + Assert.AreEqual(1, page.Values.Count); } }