Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
c9fc786
chore: reuse parse work
nbbeeken May 9, 2024
91418d8
chore: turn back on cursorResponse
nbbeeken May 9, 2024
414cdfa
wip
nbbeeken May 9, 2024
cd29ba1
wip
nbbeeken May 10, 2024
a342412
feat(NODE-6136): parse cursor responses on demand
nbbeeken May 15, 2024
9eff48a
wip
nbbeeken May 10, 2024
4534ada
fix: FLE
nbbeeken May 17, 2024
0075729
test: needs fail points
nbbeeken May 17, 2024
79acc71
test: undo bench changes
nbbeeken May 17, 2024
dde8250
chore: cleanup
nbbeeken May 17, 2024
0b825f1
wip
nbbeeken Jun 3, 2024
e0c2250
wip
nbbeeken Jun 4, 2024
1c10a1f
chore: fix unit tests
nbbeeken Jun 4, 2024
be871b8
chore: fix wc throw location
nbbeeken Jun 4, 2024
6989a54
fix: add get overload properly
nbbeeken Jun 5, 2024
9e07ea8
chore: use serialize to make empty_v
nbbeeken Jun 6, 2024
6d081f4
docs: add comment about type crime
nbbeeken Jun 6, 2024
241a08e
cruft
nbbeeken Jun 6, 2024
de3c271
chore: type annotation
nbbeeken Jun 6, 2024
4743695
chore: move ExecutionResult and document
nbbeeken Jun 6, 2024
cba9c49
test: add match to expected errors
nbbeeken Jun 6, 2024
acbb323
test: uncomment wc error ctor tests
nbbeeken Jun 6, 2024
1ffa752
fix: super generic
nbbeeken Jun 7, 2024
fb7de90
Merge branch 'main' into NODE-6136-cursor-response
nbbeeken Jun 7, 2024
827e3f7
chore: fix nullish documents
nbbeeken Jun 7, 2024
00b90ea
fix: pass through options
nbbeeken Jun 7, 2024
e28dbbb
Merge branch 'main' into NODE-6136-cursor-response
nbbeeken Jun 7, 2024
e517ab3
refactor: move CountDocument logic into collection API (#4138)
nbbeeken Jun 10, 2024
9d75303
Revert "refactor: move CountDocument logic into collection API" (#4139)
nbbeeken Jun 10, 2024
b9cfb7c
Merge branch 'main' into NODE-6136-cursor-response
nbbeeken Jun 10, 2024
c1c8ba4
chore: only attach encryptedResponse to cursor response
nbbeeken Jun 10, 2024
d5214c8
chore: clean up TS for "required"
nbbeeken Jun 11, 2024
eb0b618
Merge branch 'main' into NODE-6136-cursor-response
baileympearson Jun 13, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
wip
  • Loading branch information
nbbeeken committed Jun 4, 2024
commit cd29ba1375c14d9954907163a09dffdb1aadb438
11 changes: 8 additions & 3 deletions src/cmap/wire_protocol/on_demand/document.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,13 @@ export class OnDemandDocument {

if (name.length !== nameLength) return false;

for (let i = 0; i < name.length; i++) {
if (this.bson[nameOffset + i] !== name.charCodeAt(i)) return false;
const nameEnd = nameOffset + nameLength;
for (
let byteIndex = nameOffset, charIndex = 0;
charIndex < name.length && byteIndex < nameEnd;
charIndex++, byteIndex++
) {
if (this.bson[byteIndex] !== name.charCodeAt(charIndex)) return false;
}

return true;
Expand Down Expand Up @@ -127,7 +132,7 @@ export class OnDemandDocument {
const element = this.elements[index];

// skip this element if it has already been associated with a name
if (!this.indexFound[index] && this.isElementName(name, element)) {
if (!(index in this.indexFound) && this.isElementName(name, element)) {
const cachedElement = { element, value: undefined };
this.cache[name] = cachedElement;
this.indexFound[index] = true;
Expand Down
50 changes: 25 additions & 25 deletions src/cmap/wire_protocol/responses.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
type Document,
Long,
parseToElementsToArray,
pluckBSONSerializeOptions,
type Timestamp
} from '../../bson';
import { MongoUnexpectedServerResponseError } from '../../error';
Expand Down Expand Up @@ -153,13 +154,7 @@ export class MongoDBResponse extends OnDemandDocument {

public override toObject(options?: BSONSerializeOptions): Record<string, any> {
const exactBSONOptions = {
useBigInt64: options?.useBigInt64,
promoteLongs: options?.promoteLongs,
promoteValues: options?.promoteValues,
promoteBuffers: options?.promoteBuffers,
bsonRegExp: options?.bsonRegExp,
raw: options?.raw ?? false,
fieldsAsRaw: options?.fieldsAsRaw ?? {},
...pluckBSONSerializeOptions(options ?? {}),
validation: this.parseBsonSerializationOptions(options)
};
return super.toObject(exactBSONOptions);
Expand Down Expand Up @@ -188,33 +183,38 @@ export class CursorResponse extends MongoDBResponse {
return value instanceof CursorResponse || value === CursorResponse.emptyGetMore;
}

public id: Long;
public ns: MongoDBNamespace | null = null;
public batchSize = 0;

private batch: OnDemandDocument;
private _batch: OnDemandDocument | null = null;
private iterated = 0;

constructor(bytes: Uint8Array, offset?: number, isArray?: boolean) {
super(bytes, offset, isArray);
get cursor() {
return this.get('cursor', BSONType.object, true);
}

const cursor = this.get('cursor', BSONType.object, true);
get id(): Long {
return Long.fromBigInt(this.cursor.get('id', BSONType.long, true));
}

const id = cursor.get('id', BSONType.long, true);
this.id = new Long(Number(id & 0xffff_ffffn), Number((id >> 32n) & 0xffff_ffffn));
get ns() {
const namespace = this.cursor.get('ns', BSONType.string);
if (namespace != null) return ns(namespace);
return null;
}

const namespace = cursor.get('ns', BSONType.string);
if (namespace != null) this.ns = ns(namespace);
get length() {
return Math.max(this.batchSize - this.iterated, 0);
}

if (cursor.has('firstBatch')) this.batch = cursor.get('firstBatch', BSONType.array, true);
else if (cursor.has('nextBatch')) this.batch = cursor.get('nextBatch', BSONType.array, true);
get batch() {
if (this._batch != null) return this._batch;
const cursor = this.cursor;
if (cursor.has('firstBatch')) this._batch = cursor.get('firstBatch', BSONType.array, true);
else if (cursor.has('nextBatch')) this._batch = cursor.get('nextBatch', BSONType.array, true);
else throw new MongoUnexpectedServerResponseError('Cursor document did not contain a batch');

this.batchSize = this.batch.size();
return this._batch;
}

get length() {
return Math.max(this.batchSize - this.iterated, 0);
get batchSize() {
return this.batch?.size();
}

shift(options?: BSONSerializeOptions): any {
Expand Down
6 changes: 5 additions & 1 deletion src/cursor/abstract_cursor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,7 @@ export abstract class AbstractCursor<
return bufferedDocs;
}

async *[Symbol.asyncIterator](): AsyncGenerator<TSchema, void, void> {
private async *asyncIterator() {
if (this.closed) {
return;
}
Expand Down Expand Up @@ -350,6 +350,10 @@ export abstract class AbstractCursor<
}
}

async *[Symbol.asyncIterator](): AsyncGenerator<TSchema, void, void> {
yield* this.asyncIterator();
}

stream(options?: CursorStreamOptions): Readable & AsyncIterable<TSchema> {
if (options?.transform) {
const transform = options.transform;
Expand Down
20 changes: 17 additions & 3 deletions test/benchmarks/driverBench/index.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,26 @@
'use strict';

const MongoBench = require('../mongoBench');
const process = require('node:process');
const os = require('node:os');
const util = require('node:util');

const args = util.parseArgs({
args: process.argv.slice(2),
options: {
grep: {
short: 'g',
type: 'string',
required: false
}
}
});

const Runner = MongoBench.Runner;

let bsonType = 'js-bson';
// TODO(NODE-4606): test against different driver configurations in CI

const { inspect } = require('util');
const { writeFile } = require('fs/promises');
const { makeParallelBenchmarks, makeSingleBench, makeMultiBench } = require('../mongoBench/suites');

Expand All @@ -30,7 +42,9 @@ function average(arr) {
return arr.reduce((x, y) => x + y, 0) / arr.length;
}

const benchmarkRunner = new Runner()
const benchmarkRunner = new Runner({
grep: args.values.grep ?? null
})
.suite('singleBench', suite => makeSingleBench(suite))
.suite('multiBench', suite => makeMultiBench(suite))
.suite('parallel', suite => makeParallelBenchmarks(suite));
Expand Down Expand Up @@ -96,7 +110,7 @@ benchmarkRunner
})
.then(data => {
const results = JSON.stringify(data, undefined, 2);
console.log(inspect(data, { depth: Infinity, colors: true }));
console.log(util.inspect(data, { depth: Infinity, colors: true }));
return writeFile('results.json', results);
})
.catch(err => console.error(err));
7 changes: 7 additions & 0 deletions test/benchmarks/mongoBench/runner.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ class Runner {
console.log.apply(console, arguments);
};
this.children = {};
this.grep = options.grep?.toLowerCase() ?? null;
}

/**
Expand Down Expand Up @@ -124,6 +125,12 @@ class Runner {
const result = {};

for (const [name, benchmark] of benchmarks) {
if (this.grep != null) {
if (!name.toLowerCase().includes(this.grep)) {
result[name] = 0;
continue;
}
}
this.reporter(` Executing Benchmark "${name}"`);
result[name] = await this._runBenchmark(benchmark);
}
Expand Down