-
Notifications
You must be signed in to change notification settings - Fork 1.3k
CSHARP-3458: Extend IAsyncCursor and IAsyncCursorSource to support IAsyncEnumerable #1708
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
d34273f
CSHARP-3458: Extend IAsyncCursor and IAsyncCursorSource to support IA…
adelinowona 9926d43
add ToasyncEnumerable method in MongoQueryable
adelinowona 1a25d69
address pr comments
adelinowona f19dc41
update constructor in AsyncCursorEnumerableOneTimeAdapter
adelinowona 9b0c513
address pr comments
adelinowona 302f71f
Update AsyncCursorEnumerator tests
adelinowona 1410505
fix build failure
adelinowona 5ae3d2c
address pr comments
adelinowona a0f4ed8
address minor issues
adelinowona File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,11 +17,12 @@ | |
using System.Collections; | ||
using System.Collections.Generic; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using MongoDB.Driver.Core.Misc; | ||
|
||
namespace MongoDB.Driver.Core.Operations | ||
{ | ||
internal class AsyncCursorEnumerator<TDocument> : IEnumerator<TDocument> | ||
internal sealed class AsyncCursorEnumerator<TDocument> : IEnumerator<TDocument>, IAsyncEnumerator<TDocument> | ||
{ | ||
// private fields | ||
private IEnumerator<TDocument> _batchEnumerator; | ||
|
@@ -72,6 +73,15 @@ public void Dispose() | |
} | ||
} | ||
|
||
public ValueTask DisposeAsync() | ||
{ | ||
// TODO: implement true async disposal (CSHARP-5630) | ||
Dispose(); | ||
|
||
// TODO: convert to ValueTask.CompletedTask once we stop supporting older target frameworks | ||
return default; // Equivalent to ValueTask.CompletedTask which is not available on older target frameworks. | ||
} | ||
|
||
public bool MoveNext() | ||
{ | ||
ThrowIfDisposed(); | ||
|
@@ -82,24 +92,46 @@ public bool MoveNext() | |
return true; | ||
} | ||
|
||
while (true) | ||
while (_cursor.MoveNext(_cancellationToken)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good refactoring |
||
{ | ||
if (_cursor.MoveNext(_cancellationToken)) | ||
_batchEnumerator?.Dispose(); | ||
_batchEnumerator = _cursor.Current.GetEnumerator(); | ||
if (_batchEnumerator.MoveNext()) | ||
{ | ||
_batchEnumerator?.Dispose(); | ||
_batchEnumerator = _cursor.Current.GetEnumerator(); | ||
if (_batchEnumerator.MoveNext()) | ||
{ | ||
return true; | ||
} | ||
return true; | ||
} | ||
else | ||
} | ||
|
||
_batchEnumerator?.Dispose(); | ||
_batchEnumerator = null; | ||
_finished = true; | ||
return false; | ||
} | ||
|
||
public async ValueTask<bool> MoveNextAsync() | ||
{ | ||
ThrowIfDisposed(); | ||
_started = true; | ||
|
||
if (_batchEnumerator != null && _batchEnumerator.MoveNext()) | ||
{ | ||
return true; | ||
} | ||
|
||
while (await _cursor.MoveNextAsync(_cancellationToken).ConfigureAwait(false)) | ||
{ | ||
_batchEnumerator?.Dispose(); | ||
_batchEnumerator = _cursor.Current.GetEnumerator(); | ||
if (_batchEnumerator.MoveNext()) | ||
{ | ||
_batchEnumerator = null; | ||
_finished = true; | ||
return false; | ||
return true; | ||
} | ||
} | ||
|
||
_batchEnumerator?.Dispose(); | ||
_batchEnumerator = null; | ||
sanych-sun marked this conversation as resolved.
Show resolved
Hide resolved
|
||
_finished = true; | ||
return false; | ||
} | ||
|
||
public void Reset() | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
95 changes: 95 additions & 0 deletions
95
src/MongoDB.Driver/Core/Operations/AsyncCursorSourceEnumerator.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
/* Copyright 2010-present MongoDB Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
using System; | ||
using System.Collections.Generic; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using MongoDB.Driver.Core.Misc; | ||
|
||
namespace MongoDB.Driver.Core.Operations | ||
{ | ||
#pragma warning disable CA1001 | ||
sanych-sun marked this conversation as resolved.
Show resolved
Hide resolved
|
||
// we are suppressing this warning as we currently use the old Microsoft.CodeAnalysis.FxCopAnalyzers which doesn't | ||
// have a concept of IAsyncDisposable. | ||
// TODO: remove this suppression once we update our analyzers to use Microsoft.CodeAnalysis.NetAnalyzers | ||
internal sealed class AsyncCursorSourceEnumerator<TDocument> : IAsyncEnumerator<TDocument> | ||
#pragma warning restore CA1001 | ||
{ | ||
private readonly CancellationToken _cancellationToken; | ||
private AsyncCursorEnumerator<TDocument> _cursorEnumerator; | ||
private readonly IAsyncCursorSource<TDocument> _cursorSource; | ||
private bool _disposed; | ||
|
||
public AsyncCursorSourceEnumerator(IAsyncCursorSource<TDocument> cursorSource, CancellationToken cancellationToken) | ||
{ | ||
_cursorSource = Ensure.IsNotNull(cursorSource, nameof(cursorSource)); | ||
_cancellationToken = cancellationToken; | ||
} | ||
|
||
public TDocument Current | ||
{ | ||
get | ||
{ | ||
if (_cursorEnumerator == null) | ||
{ | ||
throw new InvalidOperationException("Enumeration has not started. Call MoveNextAsync."); | ||
} | ||
return _cursorEnumerator.Current; | ||
} | ||
} | ||
|
||
public async ValueTask DisposeAsync() | ||
{ | ||
if (!_disposed) | ||
{ | ||
_disposed = true; | ||
|
||
if (_cursorEnumerator != null) | ||
{ | ||
await _cursorEnumerator.DisposeAsync().ConfigureAwait(false); | ||
} | ||
} | ||
} | ||
|
||
public async ValueTask<bool> MoveNextAsync() | ||
{ | ||
ThrowIfDisposed(); | ||
|
||
if (_cursorEnumerator == null) | ||
{ | ||
var cursor = await _cursorSource.ToCursorAsync(_cancellationToken).ConfigureAwait(false); | ||
_cursorEnumerator = new AsyncCursorEnumerator<TDocument>(cursor, _cancellationToken); | ||
} | ||
|
||
return await _cursorEnumerator.MoveNextAsync().ConfigureAwait(false); | ||
} | ||
|
||
public void Reset() | ||
{ | ||
ThrowIfDisposed(); | ||
throw new NotSupportedException(); | ||
} | ||
|
||
// private methods | ||
private void ThrowIfDisposed() | ||
{ | ||
if (_disposed) | ||
{ | ||
throw new ObjectDisposedException(GetType().Name); | ||
} | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would add a comment like: