Skip to content

Commit 6247039

Browse files
bulk write
1 parent ebc7975 commit 6247039

File tree

1 file changed

+23
-67
lines changed

1 file changed

+23
-67
lines changed

src/operations/client_bulk_write/client_bulk_write.ts

Lines changed: 23 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
1-
import { MongoClientBulkWriteExecutionError, ServerType } from '../../beta';
1+
import { type Connection } from '../../cmap/connection';
22
import { ClientBulkWriteCursorResponse } from '../../cmap/wire_protocol/responses';
3-
import type { Server } from '../../sdam/server';
43
import type { ClientSession } from '../../sessions';
5-
import { type TimeoutContext } from '../../timeout';
64
import { MongoDBNamespace } from '../../utils';
7-
import { CommandOperation } from '../command';
5+
import { ModernizedCommandOperation } from '../command';
86
import { Aspect, defineAspects } from '../operation';
9-
import { type ClientBulkWriteCommandBuilder } from './command_builder';
7+
import { type ClientBulkWriteCommand, type ClientBulkWriteCommandBuilder } from './command_builder';
108
import { type ClientBulkWriteOptions } from './common';
119

1210
/**
1311
* Executes a single client bulk write operation within a potential batch.
1412
* @internal
1513
*/
16-
export class ClientBulkWriteOperation extends CommandOperation<ClientBulkWriteCursorResponse> {
14+
export class ClientBulkWriteOperation extends ModernizedCommandOperation<ClientBulkWriteCursorResponse> {
15+
override SERVER_COMMAND_RESPONSE_TYPE = ClientBulkWriteCursorResponse;
16+
1717
commandBuilder: ClientBulkWriteCommandBuilder;
1818
override options: ClientBulkWriteOptions;
1919

@@ -36,72 +36,28 @@ export class ClientBulkWriteOperation extends CommandOperation<ClientBulkWriteCu
3636
return this.commandBuilder.isBatchRetryable;
3737
}
3838

39-
/**
40-
* Execute the command. Superclass will handle write concern, etc.
41-
* @param server - The server.
42-
* @param session - The session.
43-
* @returns The response.
44-
*/
45-
override async execute(
46-
server: Server,
47-
session: ClientSession | undefined,
48-
timeoutContext: TimeoutContext
49-
): Promise<ClientBulkWriteCursorResponse> {
50-
let command;
39+
override handleOk(
40+
response: InstanceType<typeof this.SERVER_COMMAND_RESPONSE_TYPE>
41+
): ClientBulkWriteCursorResponse {
42+
return response;
43+
}
5144

52-
if (server.description.type === ServerType.LoadBalancer) {
53-
if (session) {
54-
let connection;
55-
if (!session.pinnedConnection) {
56-
// Checkout a connection to build the command.
57-
connection = await server.pool.checkOut({ timeoutContext });
58-
// Pin the connection to the session so it get used to execute the command and we do not
59-
// perform a double check-in/check-out.
60-
session.pin(connection);
61-
} else {
62-
connection = session.pinnedConnection;
63-
}
64-
command = this.commandBuilder.buildBatch(
65-
connection.hello?.maxMessageSizeBytes,
66-
connection.hello?.maxWriteBatchSize,
67-
connection.hello?.maxBsonObjectSize
68-
);
69-
} else {
70-
throw new MongoClientBulkWriteExecutionError(
71-
'Session provided to the client bulk write operation must be present.'
72-
);
73-
}
74-
} else {
75-
// At this point we have a server and the auto connect code has already
76-
// run in executeOperation, so the server description will be populated.
77-
// We can use that to build the command.
78-
if (
79-
!server.description.maxWriteBatchSize ||
80-
!server.description.maxMessageSizeBytes ||
81-
!server.description.maxBsonObjectSize
82-
) {
83-
throw new MongoClientBulkWriteExecutionError(
84-
'In order to execute a client bulk write, both maxWriteBatchSize, maxMessageSizeBytes and maxBsonObjectSize must be provided by the servers hello response.'
85-
);
86-
}
87-
command = this.commandBuilder.buildBatch(
88-
server.description.maxMessageSizeBytes,
89-
server.description.maxWriteBatchSize,
90-
server.description.maxBsonObjectSize
91-
);
92-
}
45+
override buildCommandDocument(
46+
connection: Connection,
47+
_session?: ClientSession
48+
): ClientBulkWriteCommand {
49+
const command = this.commandBuilder.buildBatch(
50+
connection.description.maxMessageSizeBytes,
51+
connection.description.maxWriteBatchSize,
52+
connection.description.maxBsonObjectSize
53+
);
9354

94-
// Check after the batch is built if we cannot retry it and override the option.
55+
// Check _after_ the batch is built if we cannot retry it and override the option.
9556
if (!this.canRetryWrite) {
9657
this.options.willRetryWrite = false;
9758
}
98-
return await super.executeCommand(
99-
server,
100-
session,
101-
command,
102-
timeoutContext,
103-
ClientBulkWriteCursorResponse
104-
);
59+
60+
return command;
10561
}
10662
}
10763

0 commit comments

Comments
 (0)