Skip to content
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

refactor: pull CursorStream out of Cursor #2543

Merged
merged 42 commits into from
Oct 13, 2020

Conversation

emadum
Copy link
Contributor

@emadum emadum commented Sep 12, 2020

Cursor is no longer a Readable stream, .stream() must now be called to get a stream.

NODE-2820

Description

Cursor used to extend Readable, doubling as both a readable stream and an iterable cursor. This was a likely source of confusion, because these usages couldn't be mixed on a given cursor, and the resulting errors were not particularly informative. Calling .stream() simplifies both the implementation and usage by making the separation between steaming and iterable cursors more explicit.

What changed?

Are there any files to ignore?

Copy link
Member

@mbroadst mbroadst left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sorry, jumped the gun on reviewing, but since I already did it here are some thoughts

test/functional/spec-runner/index.js Outdated Show resolved Hide resolved
test/functional/operation_generators_example.test.js Outdated Show resolved Hide resolved
test/functional/cursor.test.js Outdated Show resolved Hide resolved
src/cursor/cursor.ts Outdated Show resolved Hide resolved
src/change_stream.ts Outdated Show resolved Hide resolved
src/change_stream.ts Outdated Show resolved Hide resolved
src/change_stream.ts Outdated Show resolved Hide resolved
src/change_stream.ts Outdated Show resolved Hide resolved
src/change_stream.ts Outdated Show resolved Hide resolved
src/change_stream.ts Outdated Show resolved Hide resolved
@@ -72,16 +72,17 @@ describe('Cursor Async Iterator Tests', function () {
}
});

it('should properly stop when cursor is closed', {
// TODO - figure out how to get this test working again
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The for await ... of statement consumes the entire stream before entering the body of the loop, so this test fails with count larger than 1. I'm not sure if that means there's something incorrect with the implementation, or if this is a bad test.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mbroadst This test is still failing if un-skipped, any thoughts on what could be going wrong here?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are a couple of issues here:

  • We removed the Symbol.asyncIterator implementation for cursor because it already existed on Readable, so that actually needs to be reimplemented here because we want people to be able to do for await (const doc of cursor) { ... }

  • If this is currently broken, that means that CursorStream is broken somehow. You might just have a silly bug here where you're creating a new stream on every iteration, in which case this fix should suffice:

const cursorStream = cursor.stream();
for await (const doc of cursorStream) { ... }

Otherwise, this would indicate that when you close the cursor it is not closing the stream which might be just fine! We assume that the stream might consume a bunch of document on that first iteration so this is probably desired behavior.

Let's fix the first problem, and revert the changes to the test here as your validation that the async iterator is implemented properly.

Copy link
Member

@mbroadst mbroadst left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lookin good! I think there's some small misunderstanding about ChangeStream still being an EventEmitter, lmk if you want to chat more about that

src/cursor/cursor.ts Outdated Show resolved Hide resolved
src/cursor/cursor.ts Outdated Show resolved Hide resolved
src/cursor/cursor.ts Outdated Show resolved Hide resolved
src/cursor/cursor.ts Outdated Show resolved Hide resolved
src/cursor/cursor.ts Outdated Show resolved Hide resolved
src/change_stream.ts Outdated Show resolved Hide resolved
src/change_stream.ts Outdated Show resolved Hide resolved
src/change_stream.ts Outdated Show resolved Hide resolved
src/change_stream.ts Outdated Show resolved Hide resolved
src/change_stream.ts Outdated Show resolved Hide resolved
@emadum emadum marked this pull request as ready for review September 28, 2020 14:48
src/cursor/cursor.ts Show resolved Hide resolved
src/cursor/cursor.ts Outdated Show resolved Hide resolved
src/cursor/cursor.ts Outdated Show resolved Hide resolved
src/cursor/cursor.ts Show resolved Hide resolved
src/cursor/cursor.ts Outdated Show resolved Hide resolved
src/change_stream.ts Outdated Show resolved Hide resolved
src/change_stream.ts Outdated Show resolved Hide resolved
src/change_stream.ts Outdated Show resolved Hide resolved
src/change_stream.ts Outdated Show resolved Hide resolved
src/change_stream.ts Outdated Show resolved Hide resolved
Co-authored-by: Matt Broadstone <mbroadst@mongodb.com>
Copy link
Member

@mbroadst mbroadst left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking really great, I have just a few more comments but I think we can merge this shortly

src/change_stream.ts Outdated Show resolved Hide resolved
src/change_stream.ts Outdated Show resolved Hide resolved
src/cursor/cursor.ts Show resolved Hide resolved
src/cursor/cursor.ts Show resolved Hide resolved
test/functional/change_stream.test.js Outdated Show resolved Hide resolved
src/change_stream.ts Outdated Show resolved Hide resolved
Copy link
Member

@mbroadst mbroadst left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Left one

@@ -72,16 +72,17 @@ describe('Cursor Async Iterator Tests', function () {
}
});

it('should properly stop when cursor is closed', {
// TODO - figure out how to get this test working again
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are a couple of issues here:

  • We removed the Symbol.asyncIterator implementation for cursor because it already existed on Readable, so that actually needs to be reimplemented here because we want people to be able to do for await (const doc of cursor) { ... }

  • If this is currently broken, that means that CursorStream is broken somehow. You might just have a silly bug here where you're creating a new stream on every iteration, in which case this fix should suffice:

const cursorStream = cursor.stream();
for await (const doc of cursorStream) { ... }

Otherwise, this would indicate that when you close the cursor it is not closing the stream which might be just fine! We assume that the stream might consume a bunch of document on that first iteration so this is probably desired behavior.

Let's fix the first problem, and revert the changes to the test here as your validation that the async iterator is implemented properly.

@emadum emadum requested a review from mbroadst October 1, 2020 14:37
@emadum emadum requested review from nbbeeken and reggi October 6, 2020 17:51
Copy link
Contributor

@reggi reggi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@emadum This is really cool, a lot in here! LGTM!

One request: could you add a description of the fundamental changes here to this pull? With the intention of bringing those into the body of the commit message, just because this is such a big change it would be really nice to have a plain-english description of the changes here.

test/functional/change_stream.test.js Show resolved Hide resolved
Copy link
Contributor

@nbbeeken nbbeeken left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Every question I had was covered by the proceeding reviewers, excellent work! lgtm

Copy link
Member

@mbroadst mbroadst left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

@emadum emadum merged commit 054838f into master Oct 13, 2020
@emadum emadum deleted the NODE-2820/remove-readable-from-cursor branch October 13, 2020 14:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants