Ohm-Management - Projektarbeit B-ME
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

kill_cursors.js 1.9KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. 'use strict';
  2. const KillCursor = require('../connection/commands').KillCursor;
  3. const MongoError = require('../error').MongoError;
  4. const MongoNetworkError = require('../error').MongoNetworkError;
  5. const collectionNamespace = require('./shared').collectionNamespace;
  6. const maxWireVersion = require('../utils').maxWireVersion;
  7. const command = require('./command');
  8. function killCursors(server, ns, cursorState, callback) {
  9. callback = typeof callback === 'function' ? callback : () => {};
  10. const cursorId = cursorState.cursorId;
  11. if (maxWireVersion(server) < 4) {
  12. const bson = server.s.bson;
  13. const pool = server.s.pool;
  14. const killCursor = new KillCursor(bson, ns, [cursorId]);
  15. const options = {
  16. immediateRelease: true,
  17. noResponse: true
  18. };
  19. if (typeof cursorState.session === 'object') {
  20. options.session = cursorState.session;
  21. }
  22. if (pool && pool.isConnected()) {
  23. try {
  24. pool.write(killCursor, options, callback);
  25. } catch (err) {
  26. if (typeof callback === 'function') {
  27. callback(err, null);
  28. } else {
  29. console.warn(err);
  30. }
  31. }
  32. }
  33. return;
  34. }
  35. const killCursorCmd = {
  36. killCursors: collectionNamespace(ns),
  37. cursors: [cursorId]
  38. };
  39. const options = {};
  40. if (typeof cursorState.session === 'object') options.session = cursorState.session;
  41. command(server, ns, killCursorCmd, options, (err, result) => {
  42. if (err) {
  43. return callback(err);
  44. }
  45. const response = result.message;
  46. if (response.cursorNotFound) {
  47. return callback(new MongoNetworkError('cursor killed or timed out'), null);
  48. }
  49. if (!Array.isArray(response.documents) || response.documents.length === 0) {
  50. return callback(
  51. new MongoError(`invalid killCursors result returned for cursor id ${cursorId}`)
  52. );
  53. }
  54. callback(null, response.documents[0]);
  55. });
  56. }
  57. module.exports = killCursors;