-
Notifications
You must be signed in to change notification settings - Fork 79
MONGOSH-296 - Bulk API #283
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
Conversation
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.
Looks great. Left some sneaky comment, there are 3 you may want to check before merging:
- is it ok to log operations?
- set
this._executed
in the same context when is read - there is an
!== null
that would pass withundefined
, is that ok?
export default class Bulk extends ShellApiClass { | ||
_mongo: Mongo; | ||
_collection: any; // to avoid circular ref | ||
_batchCounts: any; |
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.
Maybe is better to add the type for _batchCounts, _batches, _innerBulk, just to make clear what those variables are for, and establish a contract with the different service provider implementations .. and also to get TS to complain a bit more 😄.
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.
for example would be great to have a type BatchCounters
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.
Ah yes, so for _innerBulk it would be a node driver type but the shell-api class can't import that directly. So instead you are saying we should define interfaces in the service-provider-core that indicate what the innerBulk/batches types should be? I can definitely do that
packages/shell-api/src/bulk.ts
Outdated
} | ||
|
||
if (this._innerBulk.s !== undefined && Array.isArray(this._innerBulk.s.batches)) { | ||
this._batches = [...this._innerBulk.s.batches]; |
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.
do we need to keep track of the batches in a class property or we should rather have a _getAllBatches
method so we always use the service provider state as source of truth?
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.
You can only call getOperations after a bulk op is executed, but the bulk op class for the driver deletes the batches attribute after it's executed. So we need to save the batches locally right before we execute. Just a difference from driver to shell api.
async execute(writeConcern?: WriteConcern): Promise<BulkWriteResult> { | ||
if (this._executed) { | ||
throw new MongoshInvalidInputError('A bulk operation cannot be re-executed'); | ||
} |
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.
to be precise in order to avoid race conditions in scripts this._executed
should be set right here in the same "synchronous" context where is checked, or in any case before calling any async operation.
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.
We can get rid of this entirely because the driver will throw the same error. Or we could just set it to false in the constructor?
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 removed the error check but kept it it for modifying the batch count - it wouldn't make sense if calling execute twice changed the batch count. Let me know if you think there's a problem with that
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 think having this._executed
there makes sense. my point was that we should not wait to set it to true though:
async execute(writeConcern?: WriteConcern): Promise<BulkWriteResult> {
if (this._executed) {
throw new MongoshInvalidInputError('A bulk operation cannot be re-executed');
}
this._executed = true;
...
which is different from:
if (this._executed) {
throw new MongoshInvalidInputError('A bulk operation cannot be re-executed');
}
await doSomething(); // <--
this._executed = true;
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.
But shouldn't this._executed
only be set true if the driver successfully executes the bulk op? That's why it's after the await
No description provided.