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.

get_more.js 2.7KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. 'use strict';
  2. const GetMore = require('../connection/commands').GetMore;
  3. const retrieveBSON = require('../connection/utils').retrieveBSON;
  4. const MongoError = require('../error').MongoError;
  5. const MongoNetworkError = require('../error').MongoNetworkError;
  6. const BSON = retrieveBSON();
  7. const Long = BSON.Long;
  8. const collectionNamespace = require('./shared').collectionNamespace;
  9. const maxWireVersion = require('../utils').maxWireVersion;
  10. const applyCommonQueryOptions = require('./shared').applyCommonQueryOptions;
  11. const command = require('./command');
  12. function getMore(server, ns, cursorState, batchSize, options, callback) {
  13. options = options || {};
  14. const wireVersion = maxWireVersion(server);
  15. function queryCallback(err, result) {
  16. if (err) return callback(err);
  17. const response = result.message;
  18. // If we have a timed out query or a cursor that was killed
  19. if (response.cursorNotFound) {
  20. return callback(new MongoNetworkError('cursor killed or timed out'), null);
  21. }
  22. if (wireVersion < 4) {
  23. const cursorId =
  24. typeof response.cursorId === 'number'
  25. ? Long.fromNumber(response.cursorId)
  26. : response.cursorId;
  27. cursorState.documents = response.documents;
  28. cursorState.cursorId = cursorId;
  29. callback(null, null, response.connection);
  30. return;
  31. }
  32. // We have an error detected
  33. if (response.documents[0].ok === 0) {
  34. return callback(new MongoError(response.documents[0]));
  35. }
  36. // Ensure we have a Long valid cursor id
  37. const cursorId =
  38. typeof response.documents[0].cursor.id === 'number'
  39. ? Long.fromNumber(response.documents[0].cursor.id)
  40. : response.documents[0].cursor.id;
  41. cursorState.documents = response.documents[0].cursor.nextBatch;
  42. cursorState.cursorId = cursorId;
  43. callback(null, response.documents[0], response.connection);
  44. }
  45. if (wireVersion < 4) {
  46. const bson = server.s.bson;
  47. const getMoreOp = new GetMore(bson, ns, cursorState.cursorId, { numberToReturn: batchSize });
  48. const queryOptions = applyCommonQueryOptions({}, cursorState);
  49. server.s.pool.write(getMoreOp, queryOptions, queryCallback);
  50. return;
  51. }
  52. const getMoreCmd = {
  53. getMore: cursorState.cursorId,
  54. collection: collectionNamespace(ns),
  55. batchSize: Math.abs(batchSize)
  56. };
  57. if (cursorState.cmd.tailable && typeof cursorState.cmd.maxAwaitTimeMS === 'number') {
  58. getMoreCmd.maxTimeMS = cursorState.cmd.maxAwaitTimeMS;
  59. }
  60. const commandOptions = Object.assign(
  61. {
  62. returnFieldSelector: null,
  63. documentsReturnedIn: 'nextBatch'
  64. },
  65. options
  66. );
  67. command(server, ns, getMoreCmd, commandOptions, queryCallback);
  68. }
  69. module.exports = getMore;