forked from mongodb/node-mongodb-native
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient_bulk_write.ts
More file actions
71 lines (60 loc) · 2.23 KB
/
client_bulk_write.ts
File metadata and controls
71 lines (60 loc) · 2.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import { type Connection } from '../../cmap/connection';
import { ClientBulkWriteCursorResponse } from '../../cmap/wire_protocol/responses';
import type { ClientSession } from '../../sessions';
import { MongoDBNamespace } from '../../utils';
import { ModernizedCommandOperation } from '../command';
import { Aspect, defineAspects } from '../operation';
import { type ClientBulkWriteCommand, type ClientBulkWriteCommandBuilder } from './command_builder';
import { type ClientBulkWriteOptions } from './common';
/**
* Executes a single client bulk write operation within a potential batch.
* @internal
*/
export class ClientBulkWriteOperation extends ModernizedCommandOperation<ClientBulkWriteCursorResponse> {
override SERVER_COMMAND_RESPONSE_TYPE = ClientBulkWriteCursorResponse;
commandBuilder: ClientBulkWriteCommandBuilder;
override options: ClientBulkWriteOptions;
override get commandName() {
return 'bulkWrite' as const;
}
constructor(commandBuilder: ClientBulkWriteCommandBuilder, options: ClientBulkWriteOptions) {
super(undefined, options);
this.commandBuilder = commandBuilder;
this.options = options;
this.ns = new MongoDBNamespace('admin', '$cmd');
}
override resetBatch(): boolean {
return this.commandBuilder.resetBatch();
}
override get canRetryWrite(): boolean {
return this.commandBuilder.isBatchRetryable;
}
override handleOk(
response: InstanceType<typeof this.SERVER_COMMAND_RESPONSE_TYPE>
): ClientBulkWriteCursorResponse {
return response;
}
override buildCommandDocument(
connection: Connection,
_session?: ClientSession
): ClientBulkWriteCommand {
const command = this.commandBuilder.buildBatch(
connection.description.maxMessageSizeBytes,
connection.description.maxWriteBatchSize,
connection.description.maxBsonObjectSize
);
// Check _after_ the batch is built if we cannot retry it and override the option.
if (!this.canRetryWrite) {
this.options.willRetryWrite = false;
}
return command;
}
}
// Skipping the collation as it goes on the individual ops.
defineAspects(ClientBulkWriteOperation, [
Aspect.WRITE_OPERATION,
Aspect.SKIP_COLLATION,
Aspect.CURSOR_CREATING,
Aspect.RETRYABLE,
Aspect.COMMAND_BATCHING
]);