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.

shared.js 3.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. 'use strict';
  2. const ReadPreference = require('../topologies/read_preference');
  3. const MongoError = require('../error').MongoError;
  4. const ServerType = require('../sdam/server_description').ServerType;
  5. const TopologyDescription = require('../sdam/topology_description').TopologyDescription;
  6. const MESSAGE_HEADER_SIZE = 16;
  7. const COMPRESSION_DETAILS_SIZE = 9; // originalOpcode + uncompressedSize, compressorID
  8. // OPCODE Numbers
  9. // Defined at https://docs.mongodb.com/manual/reference/mongodb-wire-protocol/#request-opcodes
  10. var opcodes = {
  11. OP_REPLY: 1,
  12. OP_UPDATE: 2001,
  13. OP_INSERT: 2002,
  14. OP_QUERY: 2004,
  15. OP_GETMORE: 2005,
  16. OP_DELETE: 2006,
  17. OP_KILL_CURSORS: 2007,
  18. OP_COMPRESSED: 2012,
  19. OP_MSG: 2013
  20. };
  21. var getReadPreference = function(cmd, options) {
  22. // Default to command version of the readPreference
  23. var readPreference = cmd.readPreference || new ReadPreference('primary');
  24. // If we have an option readPreference override the command one
  25. if (options.readPreference) {
  26. readPreference = options.readPreference;
  27. }
  28. if (typeof readPreference === 'string') {
  29. readPreference = new ReadPreference(readPreference);
  30. }
  31. if (!(readPreference instanceof ReadPreference)) {
  32. throw new MongoError('read preference must be a ReadPreference instance');
  33. }
  34. return readPreference;
  35. };
  36. // Parses the header of a wire protocol message
  37. var parseHeader = function(message) {
  38. return {
  39. length: message.readInt32LE(0),
  40. requestId: message.readInt32LE(4),
  41. responseTo: message.readInt32LE(8),
  42. opCode: message.readInt32LE(12)
  43. };
  44. };
  45. function applyCommonQueryOptions(queryOptions, options) {
  46. Object.assign(queryOptions, {
  47. raw: typeof options.raw === 'boolean' ? options.raw : false,
  48. promoteLongs: typeof options.promoteLongs === 'boolean' ? options.promoteLongs : true,
  49. promoteValues: typeof options.promoteValues === 'boolean' ? options.promoteValues : true,
  50. promoteBuffers: typeof options.promoteBuffers === 'boolean' ? options.promoteBuffers : false,
  51. monitoring: typeof options.monitoring === 'boolean' ? options.monitoring : false,
  52. fullResult: typeof options.fullResult === 'boolean' ? options.fullResult : false
  53. });
  54. if (typeof options.socketTimeout === 'number') {
  55. queryOptions.socketTimeout = options.socketTimeout;
  56. }
  57. if (options.session) {
  58. queryOptions.session = options.session;
  59. }
  60. if (typeof options.documentsReturnedIn === 'string') {
  61. queryOptions.documentsReturnedIn = options.documentsReturnedIn;
  62. }
  63. return queryOptions;
  64. }
  65. function isSharded(topologyOrServer) {
  66. if (topologyOrServer.type === 'mongos') return true;
  67. if (topologyOrServer.description && topologyOrServer.description.type === ServerType.Mongos) {
  68. return true;
  69. }
  70. // NOTE: This is incredibly inefficient, and should be removed once command construction
  71. // happens based on `Server` not `Topology`.
  72. if (topologyOrServer.description && topologyOrServer.description instanceof TopologyDescription) {
  73. const servers = Array.from(topologyOrServer.description.servers.values());
  74. return servers.some(server => server.type === ServerType.Mongos);
  75. }
  76. return false;
  77. }
  78. function databaseNamespace(ns) {
  79. return ns.split('.')[0];
  80. }
  81. function collectionNamespace(ns) {
  82. return ns
  83. .split('.')
  84. .slice(1)
  85. .join('.');
  86. }
  87. module.exports = {
  88. getReadPreference,
  89. MESSAGE_HEADER_SIZE,
  90. COMPRESSION_DETAILS_SIZE,
  91. opcodes,
  92. parseHeader,
  93. applyCommonQueryOptions,
  94. isSharded,
  95. databaseNamespace,
  96. collectionNamespace
  97. };