Skip to content

Commit 42eb9d8

Browse files
client bulk write
1 parent e7f7045 commit 42eb9d8

File tree

1 file changed

+22
-67
lines changed

1 file changed

+22
-67
lines changed

src/operations/client_bulk_write/client_bulk_write.ts

Lines changed: 22 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,18 @@
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;
1716
commandBuilder: ClientBulkWriteCommandBuilder;
1817
override options: ClientBulkWriteOptions;
1918

@@ -36,72 +35,28 @@ export class ClientBulkWriteOperation extends CommandOperation<ClientBulkWriteCu
3635
return this.commandBuilder.isBatchRetryable;
3736
}
3837

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;
51-
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-
}
38+
override buildCommandDocument(
39+
connection: Connection,
40+
_session?: ClientSession
41+
): ClientBulkWriteCommand {
42+
const command = this.commandBuilder.buildBatch(
43+
connection.description.maxMessageSizeBytes,
44+
connection.description.maxWriteBatchSize,
45+
connection.description.maxBsonObjectSize
46+
);
9347

9448
// Check after the batch is built if we cannot retry it and override the option.
9549
if (!this.canRetryWrite) {
9650
this.options.willRetryWrite = false;
9751
}
98-
return await super.executeCommand(
99-
server,
100-
session,
101-
command,
102-
timeoutContext,
103-
ClientBulkWriteCursorResponse
104-
);
52+
53+
return command;
54+
}
55+
56+
override handleOk(
57+
response: InstanceType<typeof this.SERVER_COMMAND_RESPONSE_TYPE>
58+
): ClientBulkWriteCursorResponse {
59+
return response;
10560
}
10661
}
10762

0 commit comments

Comments
 (0)