opusdev/vector-similarity-api
1
1import type { Long } from '../bson';2import { type Connection } from '../cmap/connection';3import { MongoDBResponse } from '../cmap/wire_protocol/responses';4import { type MongoError, MongoRuntimeError } from '../error';5import type { Server, ServerCommandOptions } from '../sdam/server';6import type { ClientSession } from '../sessions';7import { type TimeoutContext } from '../timeout';8import { type MongoDBNamespace } from '../utils';9import { AbstractOperation, Aspect, defineAspects, type OperationOptions } from './operation';10 11/**12 * https://www.mongodb.com/docs/manual/reference/command/killCursors/13 * @internal14 */15interface KillCursorsCommand {16 killCursors: string;17 cursors: Long[];18 comment?: unknown;19}20 21export class KillCursorsOperation extends AbstractOperation<void> {22 override SERVER_COMMAND_RESPONSE_TYPE = MongoDBResponse;23 cursorId: Long;24 25 constructor(cursorId: Long, ns: MongoDBNamespace, server: Server, options: OperationOptions) {26 super(options);27 this.ns = ns;28 this.cursorId = cursorId;29 this.server = server;30 }31 32 override get commandName() {33 return 'killCursors' as const;34 }35 36 override buildCommand(_connection: Connection, _session?: ClientSession): KillCursorsCommand {37 const killCursors = this.ns.collection;38 if (killCursors == null) {39 // Cursors should have adopted the namespace returned by MongoDB40 // which should always defined a collection name (even a pseudo one, ex. db.aggregate())41 throw new MongoRuntimeError('A collection name must be determined before killCursors');42 }43 44 const killCursorsCommand: KillCursorsCommand = {45 killCursors,46 cursors: [this.cursorId]47 };48 49 return killCursorsCommand;50 }51 52 override buildOptions(timeoutContext: TimeoutContext): ServerCommandOptions {53 return {54 session: this.session,55 timeoutContext56 };57 }58 59 override handleError(_error: MongoError): void {60 // The driver should never emit errors from killCursors, this is spec-ed behavior61 }62}63 64defineAspects(KillCursorsOperation, [Aspect.MUST_SELECT_SAME_SERVER]);65 