Skip to content

Commit 8e06e72

Browse files
refactor(NODE-7083): index operations to modernized operation (#4611)
Co-authored-by: Bailey Pearson <[email protected]>
1 parent 25dd18e commit 8e06e72

File tree

3 files changed

+49
-53
lines changed

3 files changed

+49
-53
lines changed

src/operations/indexes.ts

Lines changed: 17 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,13 @@
11
import type { Document } from '../bson';
22
import { type Connection } from '../cmap/connection';
3-
import { CursorResponse } from '../cmap/wire_protocol/responses';
3+
import { CursorResponse, MongoDBResponse } from '../cmap/wire_protocol/responses';
44
import type { Collection } from '../collection';
55
import { type AbstractCursorOptions } from '../cursor/abstract_cursor';
66
import { MongoCompatibilityError } from '../error';
77
import { type OneOrMore } from '../mongo_types';
8-
import type { Server } from '../sdam/server';
9-
import type { ClientSession } from '../sessions';
10-
import { type TimeoutContext } from '../timeout';
118
import { isObject, maxWireVersion, type MongoDBNamespace } from '../utils';
129
import {
1310
type CollationOptions,
14-
CommandOperation,
1511
type CommandOperationOptions,
1612
ModernizedCommandOperation,
1713
type OperationParent
@@ -246,7 +242,8 @@ type ResolvedIndexDescription = Omit<IndexDescription, 'key' | 'version'> & {
246242
};
247243

248244
/** @internal */
249-
export class CreateIndexesOperation extends CommandOperation<string[]> {
245+
export class CreateIndexesOperation extends ModernizedCommandOperation<string[]> {
246+
override SERVER_COMMAND_RESPONSE_TYPE = MongoDBResponse;
250247
override options: CreateIndexesOptions;
251248
collectionName: string;
252249
indexes: ReadonlyArray<ResolvedIndexDescription>;
@@ -260,6 +257,8 @@ export class CreateIndexesOperation extends CommandOperation<string[]> {
260257
super(parent, options);
261258

262259
this.options = options ?? {};
260+
// collation is set on each index, it should not be defined at the root
261+
this.options.collation = undefined;
263262
this.collectionName = collectionName;
264263
this.indexes = indexes.map((userIndex: IndexDescription): ResolvedIndexDescription => {
265264
// Ensure the key is a Map to preserve index key ordering
@@ -273,6 +272,7 @@ export class CreateIndexesOperation extends CommandOperation<string[]> {
273272
key
274273
};
275274
});
275+
this.ns = parent.s.namespace;
276276
}
277277

278278
static fromIndexDescriptionArray(
@@ -299,15 +299,11 @@ export class CreateIndexesOperation extends CommandOperation<string[]> {
299299
return 'createIndexes';
300300
}
301301

302-
override async execute(
303-
server: Server,
304-
session: ClientSession | undefined,
305-
timeoutContext: TimeoutContext
306-
): Promise<string[]> {
302+
override buildCommandDocument(connection: Connection): Document {
307303
const options = this.options;
308304
const indexes = this.indexes;
309305

310-
const serverWireVersion = maxWireVersion(server);
306+
const serverWireVersion = maxWireVersion(connection);
311307

312308
const cmd: Document = { createIndexes: this.collectionName, indexes };
313309

@@ -319,13 +315,11 @@ export class CreateIndexesOperation extends CommandOperation<string[]> {
319315
}
320316
cmd.commitQuorum = options.commitQuorum;
321317
}
318+
return cmd;
319+
}
322320

323-
// collation is set on each index, it should not be defined at the root
324-
this.options.collation = undefined;
325-
326-
await super.executeCommand(server, session, cmd, timeoutContext);
327-
328-
const indexNames = indexes.map(index => index.name || '');
321+
override handleOk(_response: InstanceType<typeof this.SERVER_COMMAND_RESPONSE_TYPE>): string[] {
322+
const indexNames = this.indexes.map(index => index.name || '');
329323
return indexNames;
330324
}
331325
}
@@ -334,7 +328,8 @@ export class CreateIndexesOperation extends CommandOperation<string[]> {
334328
export type DropIndexesOptions = CommandOperationOptions;
335329

336330
/** @internal */
337-
export class DropIndexOperation extends CommandOperation<Document> {
331+
export class DropIndexOperation extends ModernizedCommandOperation<Document> {
332+
override SERVER_COMMAND_RESPONSE_TYPE = MongoDBResponse;
338333
override options: DropIndexesOptions;
339334
collection: Collection;
340335
indexName: string;
@@ -345,19 +340,15 @@ export class DropIndexOperation extends CommandOperation<Document> {
345340
this.options = options ?? {};
346341
this.collection = collection;
347342
this.indexName = indexName;
343+
this.ns = collection.fullNamespace;
348344
}
349345

350346
override get commandName() {
351347
return 'dropIndexes' as const;
352348
}
353349

354-
override async execute(
355-
server: Server,
356-
session: ClientSession | undefined,
357-
timeoutContext: TimeoutContext
358-
): Promise<Document> {
359-
const cmd = { dropIndexes: this.collection.collectionName, index: this.indexName };
360-
return await super.executeCommand(server, session, cmd, timeoutContext);
350+
override buildCommandDocument(_connection: Connection): Document {
351+
return { dropIndexes: this.collection.collectionName, index: this.indexName };
361352
}
362353
}
363354

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
1-
import type { Document } from '../../bson';
1+
import { type Document } from '../../bson';
2+
import { type Connection } from '../../cmap/connection';
3+
import { MongoDBResponse } from '../../cmap/wire_protocol/responses';
24
import type { Collection } from '../../collection';
3-
import type { Server } from '../../sdam/server';
5+
import type { ServerCommandOptions } from '../../sdam/server';
46
import type { ClientSession } from '../../sessions';
57
import { type TimeoutContext } from '../../timeout';
6-
import { AbstractOperation } from '../operation';
8+
import { ModernizedOperation } from '../operation';
79

810
/**
911
* @public
@@ -20,37 +22,35 @@ export interface SearchIndexDescription extends Document {
2022
}
2123

2224
/** @internal */
23-
export class CreateSearchIndexesOperation extends AbstractOperation<string[]> {
25+
export class CreateSearchIndexesOperation extends ModernizedOperation<Document> {
26+
override SERVER_COMMAND_RESPONSE_TYPE = MongoDBResponse;
2427
private readonly collection: Collection;
2528
private readonly descriptions: ReadonlyArray<SearchIndexDescription>;
2629

2730
constructor(collection: Collection, descriptions: ReadonlyArray<SearchIndexDescription>) {
2831
super();
2932
this.collection = collection;
3033
this.descriptions = descriptions;
34+
this.ns = collection.fullNamespace;
3135
}
3236

3337
override get commandName() {
3438
return 'createSearchIndexes' as const;
3539
}
3640

37-
override async execute(
38-
server: Server,
39-
session: ClientSession | undefined,
40-
timeoutContext: TimeoutContext
41-
): Promise<string[]> {
41+
override buildCommand(_connection: Connection, _session?: ClientSession): Document {
4242
const namespace = this.collection.fullNamespace;
43-
const command = {
43+
return {
4444
createSearchIndexes: namespace.collection,
4545
indexes: this.descriptions
4646
};
47+
}
4748

48-
const res = await server.command(namespace, command, {
49-
session,
50-
timeoutContext
51-
});
49+
override handleOk(response: InstanceType<typeof this.SERVER_COMMAND_RESPONSE_TYPE>): string[] {
50+
return super.handleOk(response).indexesCreated.map((val: { name: string }) => val.name);
51+
}
5252

53-
const indexesCreated: Array<{ name: string }> = res?.indexesCreated ?? [];
54-
return indexesCreated.map(({ name }) => name);
53+
override buildOptions(timeoutContext: TimeoutContext): ServerCommandOptions {
54+
return { session: this.session, timeoutContext };
5555
}
5656
}
Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
import type { Document } from '../../bson';
2+
import { type Connection } from '../../cmap/connection';
3+
import { MongoDBResponse } from '../../cmap/wire_protocol/responses';
24
import type { Collection } from '../../collection';
3-
import type { Server } from '../../sdam/server';
5+
import type { ServerCommandOptions } from '../../sdam/server';
46
import type { ClientSession } from '../../sessions';
57
import { type TimeoutContext } from '../../timeout';
6-
import { AbstractOperation } from '../operation';
8+
import { ModernizedOperation } from '../operation';
79

810
/** @internal */
9-
export class UpdateSearchIndexOperation extends AbstractOperation<void> {
11+
export class UpdateSearchIndexOperation extends ModernizedOperation<void> {
12+
override SERVER_COMMAND_RESPONSE_TYPE = MongoDBResponse;
1013
private readonly collection: Collection;
1114
private readonly name: string;
1215
private readonly definition: Document;
@@ -16,25 +19,27 @@ export class UpdateSearchIndexOperation extends AbstractOperation<void> {
1619
this.collection = collection;
1720
this.name = name;
1821
this.definition = definition;
22+
this.ns = collection.fullNamespace;
1923
}
2024

2125
override get commandName() {
2226
return 'updateSearchIndex' as const;
2327
}
2428

25-
override async execute(
26-
server: Server,
27-
session: ClientSession | undefined,
28-
timeoutContext: TimeoutContext
29-
): Promise<void> {
29+
override buildCommand(_connection: Connection, _session?: ClientSession): Document {
3030
const namespace = this.collection.fullNamespace;
31-
const command = {
31+
return {
3232
updateSearchIndex: namespace.collection,
3333
name: this.name,
3434
definition: this.definition
3535
};
36+
}
37+
38+
override handleOk(_response: MongoDBResponse): void {
39+
// no response.
40+
}
3641

37-
await server.command(namespace, command, { session, timeoutContext });
38-
return;
42+
override buildOptions(timeoutContext: TimeoutContext): ServerCommandOptions {
43+
return { session: this.session, timeoutContext };
3944
}
4045
}

0 commit comments

Comments
 (0)