Skip to content

Commit 3d3da40

Browse files
authoredSep 25, 2024
feat(NODE-6329): client bulk write happy path (#4206)
1 parent 643a875 commit 3d3da40

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

49 files changed

+2875
-81
lines changed
 

‎src/cmap/command_monitoring_events.ts

+16-2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,12 @@ import {
77
LEGACY_HELLO_COMMAND_CAMEL_CASE
88
} from '../constants';
99
import { calculateDurationInMs, deepCopy } from '../utils';
10-
import { OpMsgRequest, type OpQueryRequest, type WriteProtocolMessageType } from './commands';
10+
import {
11+
DocumentSequence,
12+
OpMsgRequest,
13+
type OpQueryRequest,
14+
type WriteProtocolMessageType
15+
} from './commands';
1116
import type { Connection } from './connection';
1217

1318
/**
@@ -249,7 +254,16 @@ const OP_QUERY_KEYS = [
249254
/** Extract the actual command from the query, possibly up-converting if it's a legacy format */
250255
function extractCommand(command: WriteProtocolMessageType): Document {
251256
if (command instanceof OpMsgRequest) {
252-
return deepCopy(command.command);
257+
const cmd = deepCopy(command.command);
258+
// For OP_MSG with payload type 1 we need to pull the documents
259+
// array out of the document sequence for monitoring.
260+
if (cmd.ops instanceof DocumentSequence) {
261+
cmd.ops = cmd.ops.documents;
262+
}
263+
if (cmd.nsInfo instanceof DocumentSequence) {
264+
cmd.nsInfo = cmd.nsInfo.documents;
265+
}
266+
return cmd;
253267
}
254268

255269
if (command.query?.$query) {

‎src/cmap/commands.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -544,10 +544,10 @@ export class OpMsgRequest {
544544
for (const [key, value] of Object.entries(document)) {
545545
if (value instanceof DocumentSequence) {
546546
// Document sequences starts with type 1 at the first byte.
547-
const buffer = Buffer.allocUnsafe(1 + 4 + key.length);
547+
const buffer = Buffer.allocUnsafe(1 + 4 + key.length + 1);
548548
buffer[0] = 1;
549-
// Third part is the field name at offset 5.
550-
encodeUTF8Into(buffer, key, 5);
549+
// Third part is the field name at offset 5 with trailing null byte.
550+
encodeUTF8Into(buffer, `${key}\0`, 5);
551551
chunks.push(buffer);
552552
// Fourth part are the documents' bytes.
553553
let docsLength = 0;
@@ -557,7 +557,7 @@ export class OpMsgRequest {
557557
chunks.push(docBson);
558558
}
559559
// Second part of the sequence is the length at offset 1;
560-
buffer.writeInt32LE(key.length + docsLength, 1);
560+
buffer.writeInt32LE(4 + key.length + 1 + docsLength, 1);
561561
// Why are we removing the field from the command? This is because it needs to be
562562
// removed in the OP_MSG request first section, and DocumentSequence is not a
563563
// BSON type and is specific to the MongoDB wire protocol so there's nothing

0 commit comments

Comments
 (0)