Skip to content

refactor(NODE-5764): add commandName to AbstractOperation subclasses #3934

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 18 commits into from
Dec 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
6 changes: 5 additions & 1 deletion src/bulk/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -880,13 +880,17 @@ const executeCommandsAsync = promisify(executeCommands);
* We would like this logic to simply live inside the BulkWriteOperation class
* @internal
*/
class BulkWriteShimOperation extends AbstractOperation {
export class BulkWriteShimOperation extends AbstractOperation {
bulkOperation: BulkOperationBase;
constructor(bulkOperation: BulkOperationBase, options: BulkWriteOptions) {
super(options);
this.bulkOperation = bulkOperation;
}

get commandName(): string {
return 'bulkWrite' as const;
}

execute(_server: Server, session: ClientSession | undefined): Promise<any> {
if (this.options.session == null) {
// An implicit session could have been created by 'executeOperation'
Expand Down
4 changes: 4 additions & 0 deletions src/operations/aggregate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,10 @@ export class AggregateOperation<T = Document> extends CommandOperation<T> {
}
}

override get commandName() {
return 'aggregate' as const;
}

override get canRetryRead(): boolean {
return !this.hasWriteStage;
}
Expand Down
4 changes: 4 additions & 0 deletions src/operations/bulk_write.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ export class BulkWriteOperation extends AbstractOperation<BulkWriteResult> {
this.operations = operations;
}

override get commandName() {
return 'bulkWrite' as const;
}

override async execute(
server: Server,
session: ClientSession | undefined
Expand Down
4 changes: 4 additions & 0 deletions src/operations/collections.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ export class CollectionsOperation extends AbstractOperation<Collection[]> {
this.db = db;
}

override get commandName() {
return 'listCollections' as const;
}

override async execute(
server: Server,
session: ClientSession | undefined
Expand Down
4 changes: 4 additions & 0 deletions src/operations/count.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ export class CountOperation extends CommandOperation<number> {
this.query = filter;
}

override get commandName() {
return 'count' as const;
}

override async execute(server: Server, session: ClientSession | undefined): Promise<number> {
const options = this.options;
const cmd: Document = {
Expand Down
4 changes: 4 additions & 0 deletions src/operations/create_collection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,10 @@ export class CreateCollectionOperation extends CommandOperation<Collection> {
this.name = name;
}

override get commandName() {
return 'create' as const;
}

override async execute(server: Server, session: ClientSession | undefined): Promise<Collection> {
const db = this.db;
const name = this.name;
Expand Down
4 changes: 4 additions & 0 deletions src/operations/delete.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@ export class DeleteOperation extends CommandOperation<DeleteResult> {
this.statements = statements;
}

override get commandName() {
return 'delete' as const;
}

override get canRetryWrite(): boolean {
if (super.canRetryWrite === false) {
return false;
Expand Down
4 changes: 4 additions & 0 deletions src/operations/distinct.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ export class DistinctOperation extends CommandOperation<any[]> {
this.query = query;
}

override get commandName() {
return 'distinct' as const;
}

override async execute(server: Server, session: ClientSession | undefined): Promise<any[]> {
const coll = this.collection;
const key = this.key;
Expand Down
8 changes: 8 additions & 0 deletions src/operations/drop.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ export class DropCollectionOperation extends CommandOperation<boolean> {
this.name = name;
}

override get commandName() {
return 'drop' as const;
}

override async execute(server: Server, session: ClientSession | undefined): Promise<boolean> {
const db = this.db;
const options = this.options;
Expand Down Expand Up @@ -88,6 +92,10 @@ export class DropDatabaseOperation extends CommandOperation<boolean> {
super(db, options);
this.options = options;
}
override get commandName() {
return 'dropDatabase' as const;
}

override async execute(server: Server, session: ClientSession | undefined): Promise<boolean> {
await super.executeCommand(server, session, { dropDatabase: 1 });
return true;
Expand Down
4 changes: 4 additions & 0 deletions src/operations/estimated_document_count.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ export class EstimatedDocumentCountOperation extends CommandOperation<number> {
this.collectionName = collection.collectionName;
}

override get commandName() {
return 'count' as const;
}

override async execute(server: Server, session: ClientSession | undefined): Promise<number> {
const cmd: Document = { count: this.collectionName };

Expand Down
4 changes: 4 additions & 0 deletions src/operations/find.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,10 @@ export class FindOperation extends CommandOperation<Document> {
this.filter = filter != null && filter._bsontype === 'ObjectId' ? { _id: filter } : filter;
}

override get commandName() {
return 'find' as const;
}

override async execute(server: Server, session: ClientSession | undefined): Promise<Document> {
this.server = server;

Expand Down
6 changes: 5 additions & 1 deletion src/operations/find_and_modify.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ function configureFindAndModifyCmdBaseUpdateOpts(
}

/** @internal */
class FindAndModifyOperation extends CommandOperation<Document> {
export class FindAndModifyOperation extends CommandOperation<Document> {
override options: FindOneAndReplaceOptions | FindOneAndUpdateOptions | FindOneAndDeleteOptions;
cmdBase: FindAndModifyCmdBase;
collection: Collection;
Expand Down Expand Up @@ -178,6 +178,10 @@ class FindAndModifyOperation extends CommandOperation<Document> {
this.query = query;
}

override get commandName() {
return 'findAndModify' as const;
}

override async execute(server: Server, session: ClientSession | undefined): Promise<Document> {
const coll = this.collection;
const query = this.query;
Expand Down
3 changes: 3 additions & 0 deletions src/operations/get_more.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ export class GetMoreOperation extends AbstractOperation {
this.server = server;
}

override get commandName() {
return 'getMore' as const;
}
/**
* Although there is a server already associated with the get more operation, the signature
* for execute passes a server so we will just use that one.
Expand Down
29 changes: 29 additions & 0 deletions src/operations/indexes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,10 @@ export class IndexesOperation extends AbstractOperation<Document[]> {
this.collection = collection;
}

override get commandName() {
return 'listIndexes' as const;
}

override async execute(_server: Server, session: ClientSession | undefined): Promise<Document[]> {
const coll = this.collection;
const options = this.options;
Expand Down Expand Up @@ -235,6 +239,10 @@ export class CreateIndexesOperation<
});
}

override get commandName() {
return 'createIndexes';
}

override async execute(server: Server, session: ClientSession | undefined): Promise<T> {
const options = this.options;
const indexes = this.indexes;
Expand Down Expand Up @@ -272,6 +280,7 @@ export class CreateIndexOperation extends CreateIndexesOperation<string> {
) {
super(parent, collectionName, [makeIndexSpec(indexSpec, options)], options);
}

override async execute(server: Server, session: ClientSession | undefined): Promise<string> {
const indexNames = await super.execute(server, session);
return indexNames[0];
Expand All @@ -295,6 +304,10 @@ export class EnsureIndexOperation extends CreateIndexOperation {
this.collectionName = collectionName;
}

override get commandName() {
return 'listIndexes';
}

override async execute(server: Server, session: ClientSession | undefined): Promise<string> {
const indexName = this.indexes[0].name;
const indexes = await this.db
Expand Down Expand Up @@ -328,6 +341,10 @@ export class DropIndexOperation extends CommandOperation<Document> {
this.indexName = indexName;
}

override get commandName() {
return 'dropIndexes' as const;
}

override async execute(server: Server, session: ClientSession | undefined): Promise<Document> {
const cmd = { dropIndexes: this.collection.collectionName, index: this.indexName };
return super.executeCommand(server, session, cmd);
Expand Down Expand Up @@ -360,6 +377,10 @@ export class ListIndexesOperation extends CommandOperation<Document> {
this.collectionNamespace = collection.s.namespace;
}

override get commandName() {
return 'listIndexes' as const;
}

override async execute(server: Server, session: ClientSession | undefined): Promise<Document> {
const serverWireVersion = maxWireVersion(server);

Expand Down Expand Up @@ -394,6 +415,10 @@ export class IndexExistsOperation extends AbstractOperation<boolean> {
this.indexes = indexes;
}

override get commandName() {
return 'listIndexes' as const;
}

override async execute(server: Server, session: ClientSession | undefined): Promise<boolean> {
const coll = this.collection;
const indexes = this.indexes;
Expand Down Expand Up @@ -423,6 +448,10 @@ export class IndexInformationOperation extends AbstractOperation<Document> {
this.name = name;
}

override get commandName() {
return 'listIndexes' as const;
}

override async execute(server: Server, session: ClientSession | undefined): Promise<Document> {
const db = this.db;
const name = this.name;
Expand Down
8 changes: 8 additions & 0 deletions src/operations/insert.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ export class InsertOperation extends CommandOperation<Document> {
this.documents = documents;
}

override get commandName() {
return 'insert' as const;
}

override async execute(server: Server, session: ClientSession | undefined): Promise<Document> {
const options = this.options ?? {};
const ordered = typeof options.ordered === 'boolean' ? options.ordered : true;
Expand Down Expand Up @@ -114,6 +118,10 @@ export class InsertManyOperation extends AbstractOperation<InsertManyResult> {
this.docs = docs;
}

override get commandName() {
return 'insert' as const;
}

override async execute(
server: Server,
session: ClientSession | undefined
Expand Down
4 changes: 4 additions & 0 deletions src/operations/is_capped.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ export class IsCappedOperation extends AbstractOperation<boolean> {
this.collection = collection;
}

override get commandName() {
return 'listCollections' as const;
}

override async execute(server: Server, session: ClientSession | undefined): Promise<boolean> {
const coll = this.collection;
const [collection] = await coll.s.db
Expand Down
4 changes: 4 additions & 0 deletions src/operations/kill_cursors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ export class KillCursorsOperation extends AbstractOperation {
this.server = server;
}

override get commandName() {
return 'killCursors' as const;
}

override async execute(server: Server, session: ClientSession | undefined): Promise<void> {
if (server !== this.server) {
throw new MongoRuntimeError('Killcursor must run on the same server operation began on');
Expand Down
4 changes: 4 additions & 0 deletions src/operations/list_collections.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ export class ListCollectionsOperation extends CommandOperation<Document> {
}
}

override get commandName() {
return 'listCollections' as const;
}

override async execute(server: Server, session: ClientSession | undefined): Promise<Document> {
return super.executeCommand(server, session, this.generateCommand(maxWireVersion(server)));
}
Expand Down
4 changes: 4 additions & 0 deletions src/operations/list_databases.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ export class ListDatabasesOperation extends CommandOperation<ListDatabasesResult
this.ns = new MongoDBNamespace('admin', '$cmd');
}

override get commandName() {
return 'listDatabases' as const;
}

override async execute(
server: Server,
session: ClientSession | undefined
Expand Down
4 changes: 4 additions & 0 deletions src/operations/operation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,10 @@ export abstract class AbstractOperation<TResult = any> {
this.trySecondaryWrite = false;
}

/** Must match the first key of the command object sent to the server.
Command name should be stateless (should not use 'this' keyword) */
abstract get commandName(): string;

abstract execute(server: Server, session: ClientSession | undefined): Promise<TResult>;

hasAspect(aspect: symbol): boolean {
Expand Down
3 changes: 3 additions & 0 deletions src/operations/options_operation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ export class OptionsOperation extends AbstractOperation<Document> {
this.options = options;
this.collection = collection;
}
override get commandName() {
return 'listCollections' as const;
}

override async execute(server: Server, session: ClientSession | undefined): Promise<Document> {
const coll = this.collection;
Expand Down
4 changes: 4 additions & 0 deletions src/operations/profiling_level.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ export class ProfilingLevelOperation extends CommandOperation<string> {
this.options = options;
}

override get commandName() {
return 'profile' as const;
}

override async execute(server: Server, session: ClientSession | undefined): Promise<string> {
const doc = await super.executeCommand(server, session, { profile: -1 });
if (doc.ok === 1) {
Expand Down
4 changes: 4 additions & 0 deletions src/operations/remove_user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ export class RemoveUserOperation extends CommandOperation<boolean> {
this.username = username;
}

override get commandName() {
return 'dropUser' as const;
}

override async execute(server: Server, session: ClientSession | undefined): Promise<boolean> {
await super.executeCommand(server, session, { dropUser: this.username });
return true;
Expand Down
4 changes: 4 additions & 0 deletions src/operations/rename.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ export class RenameOperation extends CommandOperation<Document> {
this.ns = new MongoDBNamespace('admin', '$cmd');
}

override get commandName(): string {
return 'renameCollection' as const;
}

override async execute(server: Server, session: ClientSession | undefined): Promise<Collection> {
// Build the command
const renameCollection = this.collection.namespace;
Expand Down
8 changes: 8 additions & 0 deletions src/operations/run_command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ export class RunCommandOperation<T = Document> extends AbstractOperation<T> {
this.ns = parent.s.namespace.withCollection('$cmd');
}

override get commandName() {
return 'runCommand' as const;
}

override async execute(server: Server, session: ClientSession | undefined): Promise<T> {
this.server = server;
return server.commandAsync(this.ns, this.command, {
Expand All @@ -44,6 +48,10 @@ export class RunAdminCommandOperation<T = Document> extends AbstractOperation<T>
this.ns = new MongoDBNamespace('admin', '$cmd');
}

override get commandName() {
return 'runCommand' as const;
}

override async execute(server: Server, session: ClientSession | undefined): Promise<T> {
this.server = server;
return server.commandAsync(this.ns, this.command, {
Expand Down
Loading