kill_cursors.ts 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. import type { Long } from '../bson';
  2. import { MongoRuntimeError } from '../error';
  3. import type { Server } from '../sdam/server';
  4. import type { ClientSession } from '../sessions';
  5. import type { Callback, MongoDBNamespace } from '../utils';
  6. import {
  7. AbstractCallbackOperation,
  8. Aspect,
  9. defineAspects,
  10. type OperationOptions
  11. } from './operation';
  12. /**
  13. * https://www.mongodb.com/docs/manual/reference/command/killCursors/
  14. * @internal
  15. */
  16. interface KillCursorsCommand {
  17. killCursors: string;
  18. cursors: Long[];
  19. comment?: unknown;
  20. }
  21. export class KillCursorsOperation extends AbstractCallbackOperation {
  22. cursorId: Long;
  23. constructor(cursorId: Long, ns: MongoDBNamespace, server: Server, options: OperationOptions) {
  24. super(options);
  25. this.ns = ns;
  26. this.cursorId = cursorId;
  27. this.server = server;
  28. }
  29. executeCallback(
  30. server: Server,
  31. session: ClientSession | undefined,
  32. callback: Callback<void>
  33. ): void {
  34. if (server !== this.server) {
  35. return callback(
  36. new MongoRuntimeError('Killcursor must run on the same server operation began on')
  37. );
  38. }
  39. const killCursors = this.ns.collection;
  40. if (killCursors == null) {
  41. // Cursors should have adopted the namespace returned by MongoDB
  42. // which should always defined a collection name (even a pseudo one, ex. db.aggregate())
  43. return callback(
  44. new MongoRuntimeError('A collection name must be determined before killCursors')
  45. );
  46. }
  47. const killCursorsCommand: KillCursorsCommand = {
  48. killCursors,
  49. cursors: [this.cursorId]
  50. };
  51. server.command(this.ns, killCursorsCommand, { session }, () => callback());
  52. }
  53. }
  54. defineAspects(KillCursorsOperation, [Aspect.MUST_SELECT_SAME_SERVER]);