Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
nbbeeken committed May 16, 2024
1 parent cb38894 commit e6de40a
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 32 deletions.
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

0 comments on commit e6de40a

Please sign in to comment.