From 07d033d8cdfe50c387a6e773ec26cc3ec555195c Mon Sep 17 00:00:00 2001 From: Pavel Krymets Date: Mon, 29 Jul 2019 10:52:31 -0700 Subject: [PATCH 1/4] Move async collection to core --- sdk/core/Azure.Core/src/AsyncCollection.cs | 96 +++++++++ sdk/core/Azure.Core/src/Page.cs | 81 ++++++++ .../src/AsyncCollection.cs | 192 ------------------ .../src/StorageAsyncCollection.cs | 2 +- 4 files changed, 178 insertions(+), 193 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..7199557b41bda --- /dev/null +++ b/sdk/core/Azure.Core/src/AsyncCollection.cs @@ -0,0 +1,96 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. See License.txt in the project root for +// license information. + +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..028b8d0b05e91 --- /dev/null +++ b/sdk/core/Azure.Core/src/Page.cs @@ -0,0 +1,81 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. + +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 T[] 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(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/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 From d44d2f886d51bf06f1cbdb351dcdb6a6b6707991 Mon Sep 17 00:00:00 2001 From: Pavel Krymets Date: Mon, 29 Jul 2019 10:58:37 -0700 Subject: [PATCH 2/4] IReadOnlyList --- sdk/core/Azure.Core/src/Page.cs | 5 +++-- sdk/storage/Azure.Storage.Queues/tests/ServiceClientTests.cs | 2 +- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/sdk/core/Azure.Core/src/Page.cs b/sdk/core/Azure.Core/src/Page.cs index 028b8d0b05e91..ec259a8b184f6 100644 --- a/sdk/core/Azure.Core/src/Page.cs +++ b/sdk/core/Azure.Core/src/Page.cs @@ -1,6 +1,7 @@ // Copyright (c) Microsoft Corporation. All rights reserved. // Licensed under the MIT License. +using System.Collections.Generic; using System.ComponentModel; namespace Azure @@ -15,7 +16,7 @@ public readonly struct Page /// /// Gets the values in this . /// - public T[] Values { get; } + public IReadOnlyList Values { get; } /// /// Gets the continuation token used to request the next @@ -47,7 +48,7 @@ public readonly struct Page /// /// The that provided this . /// - public Page(T[] values, string continuationToken, Response response) + public Page(IReadOnlyList values, string continuationToken, Response response) { this.Values = values; this.ContinuationToken = continuationToken; 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); } } From daba226767006319e415f288c2cad239daf32c42 Mon Sep 17 00:00:00 2001 From: Pavel Krymets Date: Mon, 29 Jul 2019 10:58:52 -0700 Subject: [PATCH 3/4] IReadOnlyList --- sdk/storage/Azure.Storage.Blobs/tests/ContainerClientTests.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) 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); } } From ed3c723424570360581ee90aa71adc1a19abfcfc Mon Sep 17 00:00:00 2001 From: Pavel Krymets Date: Mon, 29 Jul 2019 12:28:55 -0700 Subject: [PATCH 4/4] (C) --- sdk/core/Azure.Core/src/AsyncCollection.cs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/sdk/core/Azure.Core/src/AsyncCollection.cs b/sdk/core/Azure.Core/src/AsyncCollection.cs index 7199557b41bda..717c81039c1b5 100644 --- a/sdk/core/Azure.Core/src/AsyncCollection.cs +++ b/sdk/core/Azure.Core/src/AsyncCollection.cs @@ -1,6 +1,5 @@ // Copyright (c) Microsoft Corporation. All rights reserved. -// Licensed under the MIT License. See License.txt in the project root for -// license information. +// Licensed under the MIT License. using System.Collections.Generic; using System.ComponentModel;