Skip to content

Commit 3330e7e

Browse files
committed
refactor(NODE-7083): index operations to modernized operation
1 parent a8338ad commit 3330e7e

File tree

3 files changed

+53
-49
lines changed

3 files changed

+53
-49
lines changed

src/operations/indexes.ts

Lines changed: 19 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import type { Document } from '../bson';
2-
import { CursorResponse } from '../cmap/wire_protocol/responses';
2+
import { type Connection } from '../cmap/connection';
3+
import { CursorResponse, MongoDBResponse } from '../cmap/wire_protocol/responses';
34
import type { Collection } from '../collection';
45
import { type AbstractCursorOptions } from '../cursor/abstract_cursor';
56
import { MongoCompatibilityError } from '../error';
@@ -12,6 +13,7 @@ import {
1213
type CollationOptions,
1314
CommandOperation,
1415
type CommandOperationOptions,
16+
ModernizedCommandOperation,
1517
type OperationParent
1618
} from './command';
1719
import { Aspect, defineAspects } from './operation';
@@ -244,7 +246,8 @@ type ResolvedIndexDescription = Omit<IndexDescription, 'key' | 'version'> & {
244246
};
245247

246248
/** @internal */
247-
export class CreateIndexesOperation extends CommandOperation<string[]> {
249+
export class CreateIndexesOperation extends ModernizedCommandOperation<string[]> {
250+
override SERVER_COMMAND_RESPONSE_TYPE = MongoDBResponse;
248251
override options: CreateIndexesOptions;
249252
collectionName: string;
250253
indexes: ReadonlyArray<ResolvedIndexDescription>;
@@ -258,6 +261,8 @@ export class CreateIndexesOperation extends CommandOperation<string[]> {
258261
super(parent, options);
259262

260263
this.options = options ?? {};
264+
// collation is set on each index, it should not be defined at the root
265+
this.options.collation = undefined;
261266
this.collectionName = collectionName;
262267
this.indexes = indexes.map((userIndex: IndexDescription): ResolvedIndexDescription => {
263268
// Ensure the key is a Map to preserve index key ordering
@@ -271,6 +276,7 @@ export class CreateIndexesOperation extends CommandOperation<string[]> {
271276
key
272277
};
273278
});
279+
this.ns = parent.s.namespace;
274280
}
275281

276282
static fromIndexDescriptionArray(
@@ -297,15 +303,11 @@ export class CreateIndexesOperation extends CommandOperation<string[]> {
297303
return 'createIndexes';
298304
}
299305

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

308-
const serverWireVersion = maxWireVersion(server);
310+
const serverWireVersion = maxWireVersion(connection);
309311

310312
const cmd: Document = { createIndexes: this.collectionName, indexes };
311313

@@ -317,13 +319,11 @@ export class CreateIndexesOperation extends CommandOperation<string[]> {
317319
}
318320
cmd.commitQuorum = options.commitQuorum;
319321
}
322+
return cmd;
323+
}
320324

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

334334
/** @internal */
335-
export class DropIndexOperation extends CommandOperation<Document> {
335+
export class DropIndexOperation extends ModernizedCommandOperation<Document> {
336+
override SERVER_COMMAND_RESPONSE_TYPE = MongoDBResponse;
336337
override options: DropIndexesOptions;
337338
collection: Collection;
338339
indexName: string;
@@ -343,19 +344,15 @@ export class DropIndexOperation extends CommandOperation<Document> {
343344
this.options = options ?? {};
344345
this.collection = collection;
345346
this.indexName = indexName;
347+
this.ns = collection.fullNamespace;
346348
}
347349

348350
override get commandName() {
349351
return 'dropIndexes' as const;
350352
}
351353

352-
override async execute(
353-
server: Server,
354-
session: ClientSession | undefined,
355-
timeoutContext: TimeoutContext
356-
): Promise<Document> {
357-
const cmd = { dropIndexes: this.collection.collectionName, index: this.indexName };
358-
return await super.executeCommand(server, session, cmd, timeoutContext);
354+
override buildCommandDocument(_connection: Connection): Document {
355+
return { dropIndexes: this.collection.collectionName, index: this.indexName };
359356
}
360357
}
361358

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
1-
import type { Document } from '../../bson';
1+
import { BSONType, 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,37 @@ export interface SearchIndexDescription extends Document {
2022
}
2123

2224
/** @internal */
23-
export class CreateSearchIndexesOperation extends AbstractOperation<string[]> {
25+
export class CreateSearchIndexesOperation extends ModernizedOperation<string[]> {
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: MongoDBResponse): string[] {
50+
const indexesCreated = response.get('indexesCreated', BSONType.array, true);
51+
const doc = indexesCreated.toObject();
52+
return Object.entries(doc).map(([_key, val]) => val.name);
53+
}
5254

53-
const indexesCreated: Array<{ name: string }> = res?.indexesCreated ?? [];
54-
return indexesCreated.map(({ name }) => name);
55+
override buildOptions(timeoutContext: TimeoutContext): ServerCommandOptions {
56+
return { session: this.session, timeoutContext };
5557
}
5658
}
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)