-
Notifications
You must be signed in to change notification settings - Fork 1.8k
feat(NODE-6329): client bulk write happy path #4206
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
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
ddcae44
feat(NODE-6329): client bulk write happy path
durran a2d9dcc
chore: address comments
durran c7fc4e2
test: error test
durran aa1c1da
chore: comments
durran c18c0ad
test: sync op id test
durran 0f4d3a5
test: index test
durran 373f40b
test: sync to skip serverless
durran bb3bae8
test: sync remaining tests
durran 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 |
---|---|---|
@@ -0,0 +1,73 @@ | ||
import type { Document } from '../bson'; | ||
import { type ClientBulkWriteCursorResponse } from '../cmap/wire_protocol/responses'; | ||
import { MongoBulkWriteCursorError } from '../error'; | ||
import type { MongoClient } from '../mongo_client'; | ||
import { ClientBulkWriteOperation } from '../operations/client_bulk_write/client_bulk_write'; | ||
import { type ClientBulkWriteOptions } from '../operations/client_bulk_write/common'; | ||
import { executeOperation } from '../operations/execute_operation'; | ||
import type { ClientSession } from '../sessions'; | ||
import { mergeOptions, MongoDBNamespace } from '../utils'; | ||
import { | ||
AbstractCursor, | ||
type AbstractCursorOptions, | ||
type InitialCursorResponse | ||
} from './abstract_cursor'; | ||
|
||
/** @public */ | ||
export interface ClientBulkWriteCursorOptions | ||
extends Omit<AbstractCursorOptions, 'maxAwaitTimeMS' | 'tailable' | 'awaitData'>, | ||
ClientBulkWriteOptions {} | ||
|
||
/** | ||
* This is the cursor that handles client bulk write operations. Note this is never | ||
* exposed directly to the user and is always immediately exhausted. | ||
* @internal | ||
*/ | ||
export class ClientBulkWriteCursor extends AbstractCursor { | ||
public readonly command: Document; | ||
/** @internal */ | ||
private cursorResponse?: ClientBulkWriteCursorResponse; | ||
/** @internal */ | ||
private clientBulkWriteOptions: ClientBulkWriteOptions; | ||
|
||
/** @internal */ | ||
constructor(client: MongoClient, command: Document, options: ClientBulkWriteOptions = {}) { | ||
super(client, new MongoDBNamespace('admin', '$cmd'), options); | ||
|
||
this.command = command; | ||
this.clientBulkWriteOptions = options; | ||
} | ||
|
||
/** | ||
* We need a way to get the top level cursor response fields for | ||
* generating the bulk write result, so we expose this here. | ||
*/ | ||
get response(): ClientBulkWriteCursorResponse { | ||
if (this.cursorResponse) return this.cursorResponse; | ||
throw new MongoBulkWriteCursorError( | ||
'No client bulk write cursor response returned from the server.' | ||
); | ||
} | ||
|
||
clone(): ClientBulkWriteCursor { | ||
const clonedOptions = mergeOptions({}, this.clientBulkWriteOptions); | ||
delete clonedOptions.session; | ||
return new ClientBulkWriteCursor(this.client, this.command, { | ||
...clonedOptions | ||
}); | ||
} | ||
|
||
/** @internal */ | ||
async _initialize(session: ClientSession): Promise<InitialCursorResponse> { | ||
const clientBulkWriteOperation = new ClientBulkWriteOperation(this.command, { | ||
...this.clientBulkWriteOptions, | ||
...this.cursorOptions, | ||
session | ||
}); | ||
|
||
const response = await executeOperation(this.client, clientBulkWriteOperation); | ||
this.cursorResponse = response; | ||
|
||
return { server: clientBulkWriteOperation.server, session, response }; | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import { type Document } from 'bson'; | ||
|
||
import { ClientBulkWriteCursorResponse } from '../../cmap/wire_protocol/responses'; | ||
import type { Server } from '../../sdam/server'; | ||
import type { ClientSession } from '../../sessions'; | ||
import { MongoDBNamespace } from '../../utils'; | ||
import { CommandOperation } from '../command'; | ||
import { Aspect, defineAspects } from '../operation'; | ||
import { type ClientBulkWriteOptions } from './common'; | ||
|
||
/** | ||
* Executes a single client bulk write operation within a potential batch. | ||
* @internal | ||
nbbeeken marked this conversation as resolved.
Show resolved
Hide resolved
|
||
*/ | ||
export class ClientBulkWriteOperation extends CommandOperation<ClientBulkWriteCursorResponse> { | ||
command: Document; | ||
override options: ClientBulkWriteOptions; | ||
|
||
override get commandName() { | ||
return 'bulkWrite' as const; | ||
} | ||
|
||
constructor(command: Document, options: ClientBulkWriteOptions) { | ||
super(undefined, options); | ||
this.command = command; | ||
this.options = options; | ||
this.ns = new MongoDBNamespace('admin', '$cmd'); | ||
} | ||
|
||
/** | ||
* Execute the command. Superclass will handle write concern, etc. | ||
* @param server - The server. | ||
* @param session - The session. | ||
* @returns The response. | ||
*/ | ||
override async execute( | ||
server: Server, | ||
session: ClientSession | undefined | ||
): Promise<ClientBulkWriteCursorResponse> { | ||
return await super.executeCommand(server, session, this.command, ClientBulkWriteCursorResponse); | ||
} | ||
} | ||
|
||
// Skipping the collation as it goes on the individual ops. | ||
defineAspects(ClientBulkWriteOperation, [Aspect.WRITE_OPERATION, Aspect.SKIP_COLLATION]); |
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.
Uh oh!
There was an error while loading. Please reload this page.