-
Notifications
You must be signed in to change notification settings - Fork 1.8k
feat(NODE-5019): add runCursorCommand API #3655
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 13 commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
514f8b3
feat(NODE-5019): add runCursorCommand API
nbbeeken 3298b88
fix: clone
nbbeeken 3da03ad
sync
nbbeeken e0626ad
rm invalid tests
nbbeeken 4c23490
remove comment
nbbeeken e778767
extract RP
nbbeeken 408bbe7
fix: clusterTime tests
nbbeeken 4ae5514
sync
nbbeeken f3f9de1
test: add unit
nbbeeken a91d792
lb fixes
nbbeeken f7d8653
suite name
nbbeeken bd7bcc3
Merge branch 'main' into NODE-5019-runCommandCursor
nbbeeken f52c98c
run on serverless
nbbeeken ecef528
sync skip to serverless
nbbeeken 6bee14b
comments
nbbeeken 6ace185
refactor test and csot
nbbeeken 482f816
sync
nbbeeken b95b5c1
rename option
nbbeeken 9e94b16
Merge branch 'main' into NODE-5019-runCommandCursor
dariakp d1a3d43
consolidate cs/cursor getting
nbbeeken File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
#! /usr/bin/env bash | ||
|
||
set -o xtrace | ||
set -o errexit | ||
|
||
pushd "$HOME/code/drivers/specifications/source" | ||
make | ||
popd | ||
|
||
SOURCE="$HOME/code/drivers/specifications/source/run-command/tests/unified" | ||
|
||
for file in $SOURCE/*; do | ||
cp "$file" "test/spec/run-command/$(basename "$file")" | ||
done | ||
|
||
# cp "$HOME/code/drivers/specifications/source/unified-test-format/tests/invalid/entity-createRunCursorCommand.yml" test/spec/unified-test-format/invalid/entity-createRunCursorCommand.yml | ||
# cp "$HOME/code/drivers/specifications/source/unified-test-format/tests/invalid/entity-createRunCursorCommand.json" test/spec/unified-test-format/invalid/entity-createRunCursorCommand.json | ||
|
||
cp "$HOME/code/drivers/specifications/source/unified-test-format/tests/valid-pass/entity-commandCursor.yml" test/spec/unified-test-format/valid-pass/entity-commandCursor.yml | ||
cp "$HOME/code/drivers/specifications/source/unified-test-format/tests/valid-pass/entity-commandCursor.json" test/spec/unified-test-format/valid-pass/entity-commandCursor.json | ||
|
||
|
||
if [ -z "$MONGODB_URI" ]; then echo "must set uri" && exit 1; fi | ||
export MONGODB_URI=$MONGODB_URI | ||
npm run check:test -- -g '(RunCommand spec)|(Unified test format runner runCursorCommand)' |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,138 @@ | ||
import { BSONSerializeOptions, Document, Long, resolveBSONOptions } from '../bson'; | ||
import type { Db } from '../db'; | ||
import { MongoAPIError, MongoUnexpectedServerResponseError } from '../error'; | ||
import { executeOperation, ExecutionResult } from '../operations/execute_operation'; | ||
import { GetMoreOperation } from '../operations/get_more'; | ||
import { RunCommandOperation } from '../operations/run_command'; | ||
import type { ReadConcernLike } from '../read_concern'; | ||
import type { ReadPreferenceLike } from '../read_preference'; | ||
import type { ClientSession } from '../sessions'; | ||
import { Callback, ns } from '../utils'; | ||
import { AbstractCursor } from './abstract_cursor'; | ||
|
||
/** @public */ | ||
export type RunCommandCursorOptions = { | ||
dariakp marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
readPreference?: ReadPreferenceLike; | ||
session?: ClientSession; | ||
} & BSONSerializeOptions; | ||
|
||
/** @internal */ | ||
type RunCursorCommandResponse = { | ||
cursor: { id: bigint | Long | number; ns: string; firstBatch: Document[] }; | ||
ok: 1; | ||
}; | ||
|
||
/** @public */ | ||
export class RunCommandCursor extends AbstractCursor { | ||
public readonly command: Readonly<Record<string, any>>; | ||
public readonly getMoreOptions: { | ||
comment?: any; | ||
maxAwaitTimeMS?: number; | ||
batchSize?: number; | ||
} = {}; | ||
|
||
/** | ||
* Controls the `getMore.comment` field | ||
* @param comment - any BSON value | ||
*/ | ||
public setComment(comment: any): this { | ||
this.getMoreOptions.comment = comment; | ||
return this; | ||
} | ||
|
||
/** | ||
* Controls the `getMore.maxTimeMS` field. Only valid when cursor is tailable await | ||
* @param maxTimeMS - the number of milliseconds to wait for new data | ||
*/ | ||
public setMaxTimeMS(maxTimeMS: number): this { | ||
this.getMoreOptions.maxAwaitTimeMS = maxTimeMS; | ||
return this; | ||
} | ||
|
||
/** | ||
* Controls the `getMore.batchSize` field | ||
* @param maxTimeMS - the number documents to return in the `nextBatch` | ||
*/ | ||
public setBatchSize(batchSize: number): this { | ||
this.getMoreOptions.batchSize = batchSize; | ||
return this; | ||
} | ||
|
||
public clone(): RunCommandCursor { | ||
return new RunCommandCursor(this.db, this.command, { | ||
readPreference: this.readPreference, | ||
...resolveBSONOptions(this.cursorOptions) | ||
}); | ||
} | ||
|
||
public override withReadConcern(_: ReadConcernLike): never { | ||
throw new MongoAPIError( | ||
'RunCommandCursor does not support readConcern it must be attached to the command being run' | ||
); | ||
} | ||
|
||
public override addCursorFlag(_: string, __: boolean): never { | ||
throw new MongoAPIError( | ||
'RunCommandCursor does not support cursor flags, they must be attached to the command being run' | ||
); | ||
} | ||
|
||
public override maxTimeMS(_: number): never { | ||
dariakp marked this conversation as resolved.
Show resolved
Hide resolved
|
||
throw new MongoAPIError( | ||
'RunCommandCursor does not support maxTimeMS, it must be attached to the command being run' | ||
); | ||
} | ||
|
||
public override batchSize(_: number): never { | ||
throw new MongoAPIError( | ||
'RunCommandCursor does not support batchSize, it must be attached to the command being run' | ||
); | ||
} | ||
|
||
/** @internal */ | ||
private db: Db; | ||
|
||
/** @internal */ | ||
constructor(db: Db, command: Document, options: RunCommandCursorOptions = {}) { | ||
super(db.s.client, ns(db.namespace), options); | ||
this.db = db; | ||
this.command = Object.freeze({ ...command }); | ||
} | ||
|
||
/** @internal */ | ||
protected _initialize(session: ClientSession, callback: Callback<ExecutionResult>) { | ||
const operation = new RunCommandOperation<RunCursorCommandResponse>(this.db, this.command, { | ||
...this.cursorOptions, | ||
session: session, | ||
readPreference: this.cursorOptions.readPreference | ||
}); | ||
executeOperation(this.client, operation).then( | ||
response => { | ||
if (response.cursor == null) { | ||
callback( | ||
new MongoUnexpectedServerResponseError('Expected server to respond with cursor') | ||
); | ||
return; | ||
} | ||
callback(undefined, { | ||
server: operation.server, | ||
session, | ||
response | ||
}); | ||
}, | ||
err => callback(err) | ||
); | ||
} | ||
|
||
/** @internal */ | ||
override _getMore(_batchSize: number, callback: Callback<Document>) { | ||
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion | ||
const getMoreOperation = new GetMoreOperation(this.namespace, this.id!, this.server!, { | ||
...this.cursorOptions, | ||
session: this.session, | ||
...this.getMoreOptions | ||
}); | ||
|
||
executeOperation(this.client, getMoreOperation, callback); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import { expect } from 'chai'; | ||
|
||
import { CommandStartedEvent, Db, MongoClient } from '../../mongodb'; | ||
|
||
describe('class RunCommandCursor', () => { | ||
dariakp marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
let client: MongoClient; | ||
let db: Db; | ||
let commandsStarted: CommandStartedEvent[]; | ||
|
||
beforeEach(async function () { | ||
client = this.configuration.newClient({}, { monitorCommands: true }); | ||
db = client.db(); | ||
await db.dropDatabase().catch(() => null); | ||
await db | ||
.collection<{ _id: number }>('collection') | ||
.insertMany([{ _id: 0 }, { _id: 1 }, { _id: 2 }]); | ||
commandsStarted = []; | ||
client.on('commandStarted', started => commandsStarted.push(started)); | ||
}); | ||
|
||
afterEach(async function () { | ||
commandsStarted = []; | ||
await client.close(); | ||
}); | ||
|
||
it('should only run init command once', async () => { | ||
dariakp marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
const cursor = db.runCursorCommand({ find: 'collection', filter: {}, batchSize: 1 }); | ||
cursor.setBatchSize(1); | ||
const it0 = cursor[Symbol.asyncIterator](); | ||
const it1 = cursor[Symbol.asyncIterator](); | ||
|
||
const next0it0 = await it0.next(); // find, 1 doc | ||
const next0it1 = await it1.next(); // getMore, 1 doc | ||
|
||
expect(next0it0).to.deep.equal({ value: { _id: 0 }, done: false }); | ||
expect(next0it1).to.deep.equal({ value: { _id: 1 }, done: false }); | ||
expect(commandsStarted.map(c => c.commandName)).to.have.lengthOf(2); | ||
|
||
const next1it0 = await it0.next(); // getMore, 1 doc | ||
const next1it1 = await it1.next(); // getMore, 0 doc & exhausted id | ||
|
||
expect(next1it0).to.deep.equal({ value: { _id: 2 }, done: false }); | ||
expect(next1it1).to.deep.equal({ value: undefined, done: true }); | ||
expect(commandsStarted.map(c => c.commandName)).to.have.lengthOf(4); | ||
|
||
const next2it0 = await it0.next(); | ||
const next2it1 = await it1.next(); | ||
|
||
expect(next2it0).to.deep.equal({ value: undefined, done: true }); | ||
expect(next2it1).to.deep.equal({ value: undefined, done: true }); | ||
expect(commandsStarted.map(c => c.commandName)).to.have.lengthOf(4); | ||
}); | ||
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.