-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(NODE-6329): client bulk write happy path
- Loading branch information
Showing
22 changed files
with
2,408 additions
and
5 deletions.
There are no files selected for viewing
This file contains 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 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,64 @@ | ||
import type { Document } from '../bson'; | ||
import { type ClientBulkWriteCursorResponse } from '../cmap/wire_protocol/responses'; | ||
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 AbstractCursorOptions, | ||
ClientBulkWriteOptions {} | ||
|
||
/** | ||
* @public | ||
*/ | ||
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'), options); | ||
|
||
this.command = command; | ||
this.clientBulkWriteOptions = options; | ||
} | ||
|
||
get response(): ClientBulkWriteCursorResponse { | ||
if (this.cursorResponse) return this.response; | ||
throw new Error('no cursor response'); | ||
} | ||
|
||
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 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 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 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,41 @@ | ||
import { type Document } from 'bson'; | ||
|
||
import { ClientBulkWriteCursorResponse } from '../../cmap/wire_protocol/responses'; | ||
import type { Server } from '../../sdam/server'; | ||
import type { ClientSession } from '../../sessions'; | ||
import { AbstractOperation, Aspect, defineAspects } from '../operation'; | ||
import { type ClientBulkWriteOptions } from './common'; | ||
|
||
export class ClientBulkWriteOperation extends AbstractOperation<ClientBulkWriteCursorResponse> { | ||
command: Document; | ||
override options: ClientBulkWriteOptions; | ||
|
||
override get commandName() { | ||
return 'bulkWrite' as const; | ||
} | ||
|
||
constructor(command: Document, options: ClientBulkWriteOptions) { | ||
super(options); | ||
this.command = command; | ||
this.options = options; | ||
} | ||
|
||
override async execute( | ||
server: Server, | ||
session: ClientSession | undefined | ||
): Promise<ClientBulkWriteCursorResponse> { | ||
return await server.command( | ||
this.ns, | ||
this.command, | ||
{ | ||
...this.options, | ||
...this.bsonOptions, | ||
documentsReturnedIn: 'firstBatch', | ||
session | ||
}, | ||
ClientBulkWriteCursorResponse | ||
); | ||
} | ||
} | ||
|
||
defineAspects(ClientBulkWriteOperation, [Aspect.WRITE_OPERATION]); |
This file contains 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 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,42 @@ | ||
import { ClientBulkWriteCursor } from '../../cursor/client_bulk_write_cursor'; | ||
import { type MongoClient } from '../../mongo_client'; | ||
import { ClientBulkWriteCommandBuilder } from './command_builder'; | ||
import { | ||
type AnyClientBulkWriteModel, | ||
type ClientBulkWriteOptions, | ||
type ClientBulkWriteResult | ||
} from './common'; | ||
import { ClientBulkWriteResultsMerger } from './results_merger'; | ||
|
||
/** | ||
* Responsible for executing a client bulk write. | ||
* @internal | ||
*/ | ||
export class ClientBulkWriteExecutor { | ||
client: MongoClient; | ||
options: ClientBulkWriteOptions; | ||
operations: AnyClientBulkWriteModel[]; | ||
|
||
constructor( | ||
client: MongoClient, | ||
operations: AnyClientBulkWriteModel[], | ||
options?: ClientBulkWriteOptions | ||
) { | ||
this.client = client; | ||
this.options = options || {}; | ||
this.operations = operations; | ||
} | ||
|
||
async execute(): Promise<ClientBulkWriteResult> { | ||
const commmandBuilder = new ClientBulkWriteCommandBuilder(this.operations, this.options); | ||
const commands = commmandBuilder.buildCommands(); | ||
const resultsMerger = new ClientBulkWriteResultsMerger(this.options); | ||
for (const command of commands) { | ||
const cursor = new ClientBulkWriteCursor(this.client, command, this.options); | ||
for (const docs of await cursor.toArray()) { | ||
resultsMerger.merge(cursor.response, docs); | ||
} | ||
} | ||
return resultsMerger.result; | ||
} | ||
} |
This file contains 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,52 @@ | ||
import { type Document } from '../../bson'; | ||
import { type ClientBulkWriteCursorResponse } from '../../cmap/wire_protocol/responses'; | ||
import { | ||
type ClientBulkWriteOptions, | ||
type ClientBulkWriteResult, | ||
type VerboseClientBulkWriteResult | ||
} from './common'; | ||
|
||
/** | ||
* Merges client bulk write cursor responses together into a single result. | ||
* @internal | ||
*/ | ||
export class ClientBulkWriteResultsMerger { | ||
result: ClientBulkWriteResult | VerboseClientBulkWriteResult; | ||
options: ClientBulkWriteOptions; | ||
|
||
/** | ||
* Instantiate the merger. | ||
* @param options - The options. | ||
*/ | ||
constructor(options: ClientBulkWriteOptions) { | ||
this.options = options; | ||
this.result = { | ||
insertedCount: 0, | ||
upsertedCount: 0, | ||
matchedCount: 0, | ||
modifiedCount: 0, | ||
deletedCount: 0 | ||
}; | ||
} | ||
|
||
/** | ||
* Merge the results in the cursor to the existing result. | ||
* @param response - The cursor response. | ||
* @param documents - The documents in the cursor. | ||
* @returns The current result. | ||
*/ | ||
merge(response: ClientBulkWriteCursorResponse, documents: Document[]): ClientBulkWriteResult { | ||
// Update the counts from the cursor response. | ||
this.result.insertedCount += response.insertedCount; | ||
this.result.upsertedCount += response.upsertedCount; | ||
this.result.matchedCount += response.matchedCount; | ||
this.result.modifiedCount += response.modifiedCount; | ||
this.result.deletedCount += response.deletedCount; | ||
|
||
// Iterate all the documents in the cursor and update the result. | ||
for (const document of documents) { | ||
document; | ||
} | ||
return this.result; | ||
} | ||
} |
This file contains 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 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.