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.

commands.js 17KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546
  1. 'use strict';
  2. var retrieveBSON = require('./utils').retrieveBSON;
  3. var BSON = retrieveBSON();
  4. var Long = BSON.Long;
  5. const MongoError = require('../error').MongoError;
  6. const Buffer = require('safe-buffer').Buffer;
  7. // Incrementing request id
  8. var _requestId = 0;
  9. // Wire command operation ids
  10. var opcodes = require('../wireprotocol/shared').opcodes;
  11. // Query flags
  12. var OPTS_TAILABLE_CURSOR = 2;
  13. var OPTS_SLAVE = 4;
  14. var OPTS_OPLOG_REPLAY = 8;
  15. var OPTS_NO_CURSOR_TIMEOUT = 16;
  16. var OPTS_AWAIT_DATA = 32;
  17. var OPTS_EXHAUST = 64;
  18. var OPTS_PARTIAL = 128;
  19. // Response flags
  20. var CURSOR_NOT_FOUND = 1;
  21. var QUERY_FAILURE = 2;
  22. var SHARD_CONFIG_STALE = 4;
  23. var AWAIT_CAPABLE = 8;
  24. /**************************************************************
  25. * QUERY
  26. **************************************************************/
  27. var Query = function(bson, ns, query, options) {
  28. var self = this;
  29. // Basic options needed to be passed in
  30. if (ns == null) throw new Error('ns must be specified for query');
  31. if (query == null) throw new Error('query must be specified for query');
  32. // Validate that we are not passing 0x00 in the collection name
  33. if (ns.indexOf('\x00') !== -1) {
  34. throw new Error('namespace cannot contain a null character');
  35. }
  36. // Basic options
  37. this.bson = bson;
  38. this.ns = ns;
  39. this.query = query;
  40. // Additional options
  41. this.numberToSkip = options.numberToSkip || 0;
  42. this.numberToReturn = options.numberToReturn || 0;
  43. this.returnFieldSelector = options.returnFieldSelector || null;
  44. this.requestId = Query.getRequestId();
  45. // special case for pre-3.2 find commands, delete ASAP
  46. this.pre32Limit = options.pre32Limit;
  47. // Serialization option
  48. this.serializeFunctions =
  49. typeof options.serializeFunctions === 'boolean' ? options.serializeFunctions : false;
  50. this.ignoreUndefined =
  51. typeof options.ignoreUndefined === 'boolean' ? options.ignoreUndefined : false;
  52. this.maxBsonSize = options.maxBsonSize || 1024 * 1024 * 16;
  53. this.checkKeys = typeof options.checkKeys === 'boolean' ? options.checkKeys : true;
  54. this.batchSize = self.numberToReturn;
  55. // Flags
  56. this.tailable = false;
  57. this.slaveOk = typeof options.slaveOk === 'boolean' ? options.slaveOk : false;
  58. this.oplogReplay = false;
  59. this.noCursorTimeout = false;
  60. this.awaitData = false;
  61. this.exhaust = false;
  62. this.partial = false;
  63. };
  64. //
  65. // Assign a new request Id
  66. Query.prototype.incRequestId = function() {
  67. this.requestId = _requestId++;
  68. };
  69. //
  70. // Assign a new request Id
  71. Query.nextRequestId = function() {
  72. return _requestId + 1;
  73. };
  74. //
  75. // Uses a single allocated buffer for the process, avoiding multiple memory allocations
  76. Query.prototype.toBin = function() {
  77. var self = this;
  78. var buffers = [];
  79. var projection = null;
  80. // Set up the flags
  81. var flags = 0;
  82. if (this.tailable) {
  83. flags |= OPTS_TAILABLE_CURSOR;
  84. }
  85. if (this.slaveOk) {
  86. flags |= OPTS_SLAVE;
  87. }
  88. if (this.oplogReplay) {
  89. flags |= OPTS_OPLOG_REPLAY;
  90. }
  91. if (this.noCursorTimeout) {
  92. flags |= OPTS_NO_CURSOR_TIMEOUT;
  93. }
  94. if (this.awaitData) {
  95. flags |= OPTS_AWAIT_DATA;
  96. }
  97. if (this.exhaust) {
  98. flags |= OPTS_EXHAUST;
  99. }
  100. if (this.partial) {
  101. flags |= OPTS_PARTIAL;
  102. }
  103. // If batchSize is different to self.numberToReturn
  104. if (self.batchSize !== self.numberToReturn) self.numberToReturn = self.batchSize;
  105. // Allocate write protocol header buffer
  106. var header = Buffer.alloc(
  107. 4 * 4 + // Header
  108. 4 + // Flags
  109. Buffer.byteLength(self.ns) +
  110. 1 + // namespace
  111. 4 + // numberToSkip
  112. 4 // numberToReturn
  113. );
  114. // Add header to buffers
  115. buffers.push(header);
  116. // Serialize the query
  117. var query = self.bson.serialize(this.query, {
  118. checkKeys: this.checkKeys,
  119. serializeFunctions: this.serializeFunctions,
  120. ignoreUndefined: this.ignoreUndefined
  121. });
  122. // Add query document
  123. buffers.push(query);
  124. if (self.returnFieldSelector && Object.keys(self.returnFieldSelector).length > 0) {
  125. // Serialize the projection document
  126. projection = self.bson.serialize(this.returnFieldSelector, {
  127. checkKeys: this.checkKeys,
  128. serializeFunctions: this.serializeFunctions,
  129. ignoreUndefined: this.ignoreUndefined
  130. });
  131. // Add projection document
  132. buffers.push(projection);
  133. }
  134. // Total message size
  135. var totalLength = header.length + query.length + (projection ? projection.length : 0);
  136. // Set up the index
  137. var index = 4;
  138. // Write total document length
  139. header[3] = (totalLength >> 24) & 0xff;
  140. header[2] = (totalLength >> 16) & 0xff;
  141. header[1] = (totalLength >> 8) & 0xff;
  142. header[0] = totalLength & 0xff;
  143. // Write header information requestId
  144. header[index + 3] = (this.requestId >> 24) & 0xff;
  145. header[index + 2] = (this.requestId >> 16) & 0xff;
  146. header[index + 1] = (this.requestId >> 8) & 0xff;
  147. header[index] = this.requestId & 0xff;
  148. index = index + 4;
  149. // Write header information responseTo
  150. header[index + 3] = (0 >> 24) & 0xff;
  151. header[index + 2] = (0 >> 16) & 0xff;
  152. header[index + 1] = (0 >> 8) & 0xff;
  153. header[index] = 0 & 0xff;
  154. index = index + 4;
  155. // Write header information OP_QUERY
  156. header[index + 3] = (opcodes.OP_QUERY >> 24) & 0xff;
  157. header[index + 2] = (opcodes.OP_QUERY >> 16) & 0xff;
  158. header[index + 1] = (opcodes.OP_QUERY >> 8) & 0xff;
  159. header[index] = opcodes.OP_QUERY & 0xff;
  160. index = index + 4;
  161. // Write header information flags
  162. header[index + 3] = (flags >> 24) & 0xff;
  163. header[index + 2] = (flags >> 16) & 0xff;
  164. header[index + 1] = (flags >> 8) & 0xff;
  165. header[index] = flags & 0xff;
  166. index = index + 4;
  167. // Write collection name
  168. index = index + header.write(this.ns, index, 'utf8') + 1;
  169. header[index - 1] = 0;
  170. // Write header information flags numberToSkip
  171. header[index + 3] = (this.numberToSkip >> 24) & 0xff;
  172. header[index + 2] = (this.numberToSkip >> 16) & 0xff;
  173. header[index + 1] = (this.numberToSkip >> 8) & 0xff;
  174. header[index] = this.numberToSkip & 0xff;
  175. index = index + 4;
  176. // Write header information flags numberToReturn
  177. header[index + 3] = (this.numberToReturn >> 24) & 0xff;
  178. header[index + 2] = (this.numberToReturn >> 16) & 0xff;
  179. header[index + 1] = (this.numberToReturn >> 8) & 0xff;
  180. header[index] = this.numberToReturn & 0xff;
  181. index = index + 4;
  182. // Return the buffers
  183. return buffers;
  184. };
  185. Query.getRequestId = function() {
  186. return ++_requestId;
  187. };
  188. /**************************************************************
  189. * GETMORE
  190. **************************************************************/
  191. var GetMore = function(bson, ns, cursorId, opts) {
  192. opts = opts || {};
  193. this.numberToReturn = opts.numberToReturn || 0;
  194. this.requestId = _requestId++;
  195. this.bson = bson;
  196. this.ns = ns;
  197. this.cursorId = cursorId;
  198. };
  199. //
  200. // Uses a single allocated buffer for the process, avoiding multiple memory allocations
  201. GetMore.prototype.toBin = function() {
  202. var length = 4 + Buffer.byteLength(this.ns) + 1 + 4 + 8 + 4 * 4;
  203. // Create command buffer
  204. var index = 0;
  205. // Allocate buffer
  206. var _buffer = Buffer.alloc(length);
  207. // Write header information
  208. // index = write32bit(index, _buffer, length);
  209. _buffer[index + 3] = (length >> 24) & 0xff;
  210. _buffer[index + 2] = (length >> 16) & 0xff;
  211. _buffer[index + 1] = (length >> 8) & 0xff;
  212. _buffer[index] = length & 0xff;
  213. index = index + 4;
  214. // index = write32bit(index, _buffer, requestId);
  215. _buffer[index + 3] = (this.requestId >> 24) & 0xff;
  216. _buffer[index + 2] = (this.requestId >> 16) & 0xff;
  217. _buffer[index + 1] = (this.requestId >> 8) & 0xff;
  218. _buffer[index] = this.requestId & 0xff;
  219. index = index + 4;
  220. // index = write32bit(index, _buffer, 0);
  221. _buffer[index + 3] = (0 >> 24) & 0xff;
  222. _buffer[index + 2] = (0 >> 16) & 0xff;
  223. _buffer[index + 1] = (0 >> 8) & 0xff;
  224. _buffer[index] = 0 & 0xff;
  225. index = index + 4;
  226. // index = write32bit(index, _buffer, OP_GETMORE);
  227. _buffer[index + 3] = (opcodes.OP_GETMORE >> 24) & 0xff;
  228. _buffer[index + 2] = (opcodes.OP_GETMORE >> 16) & 0xff;
  229. _buffer[index + 1] = (opcodes.OP_GETMORE >> 8) & 0xff;
  230. _buffer[index] = opcodes.OP_GETMORE & 0xff;
  231. index = index + 4;
  232. // index = write32bit(index, _buffer, 0);
  233. _buffer[index + 3] = (0 >> 24) & 0xff;
  234. _buffer[index + 2] = (0 >> 16) & 0xff;
  235. _buffer[index + 1] = (0 >> 8) & 0xff;
  236. _buffer[index] = 0 & 0xff;
  237. index = index + 4;
  238. // Write collection name
  239. index = index + _buffer.write(this.ns, index, 'utf8') + 1;
  240. _buffer[index - 1] = 0;
  241. // Write batch size
  242. // index = write32bit(index, _buffer, numberToReturn);
  243. _buffer[index + 3] = (this.numberToReturn >> 24) & 0xff;
  244. _buffer[index + 2] = (this.numberToReturn >> 16) & 0xff;
  245. _buffer[index + 1] = (this.numberToReturn >> 8) & 0xff;
  246. _buffer[index] = this.numberToReturn & 0xff;
  247. index = index + 4;
  248. // Write cursor id
  249. // index = write32bit(index, _buffer, cursorId.getLowBits());
  250. _buffer[index + 3] = (this.cursorId.getLowBits() >> 24) & 0xff;
  251. _buffer[index + 2] = (this.cursorId.getLowBits() >> 16) & 0xff;
  252. _buffer[index + 1] = (this.cursorId.getLowBits() >> 8) & 0xff;
  253. _buffer[index] = this.cursorId.getLowBits() & 0xff;
  254. index = index + 4;
  255. // index = write32bit(index, _buffer, cursorId.getHighBits());
  256. _buffer[index + 3] = (this.cursorId.getHighBits() >> 24) & 0xff;
  257. _buffer[index + 2] = (this.cursorId.getHighBits() >> 16) & 0xff;
  258. _buffer[index + 1] = (this.cursorId.getHighBits() >> 8) & 0xff;
  259. _buffer[index] = this.cursorId.getHighBits() & 0xff;
  260. index = index + 4;
  261. // Return buffer
  262. return _buffer;
  263. };
  264. /**************************************************************
  265. * KILLCURSOR
  266. **************************************************************/
  267. var KillCursor = function(bson, ns, cursorIds) {
  268. this.ns = ns;
  269. this.requestId = _requestId++;
  270. this.cursorIds = cursorIds;
  271. };
  272. //
  273. // Uses a single allocated buffer for the process, avoiding multiple memory allocations
  274. KillCursor.prototype.toBin = function() {
  275. var length = 4 + 4 + 4 * 4 + this.cursorIds.length * 8;
  276. // Create command buffer
  277. var index = 0;
  278. var _buffer = Buffer.alloc(length);
  279. // Write header information
  280. // index = write32bit(index, _buffer, length);
  281. _buffer[index + 3] = (length >> 24) & 0xff;
  282. _buffer[index + 2] = (length >> 16) & 0xff;
  283. _buffer[index + 1] = (length >> 8) & 0xff;
  284. _buffer[index] = length & 0xff;
  285. index = index + 4;
  286. // index = write32bit(index, _buffer, requestId);
  287. _buffer[index + 3] = (this.requestId >> 24) & 0xff;
  288. _buffer[index + 2] = (this.requestId >> 16) & 0xff;
  289. _buffer[index + 1] = (this.requestId >> 8) & 0xff;
  290. _buffer[index] = this.requestId & 0xff;
  291. index = index + 4;
  292. // index = write32bit(index, _buffer, 0);
  293. _buffer[index + 3] = (0 >> 24) & 0xff;
  294. _buffer[index + 2] = (0 >> 16) & 0xff;
  295. _buffer[index + 1] = (0 >> 8) & 0xff;
  296. _buffer[index] = 0 & 0xff;
  297. index = index + 4;
  298. // index = write32bit(index, _buffer, OP_KILL_CURSORS);
  299. _buffer[index + 3] = (opcodes.OP_KILL_CURSORS >> 24) & 0xff;
  300. _buffer[index + 2] = (opcodes.OP_KILL_CURSORS >> 16) & 0xff;
  301. _buffer[index + 1] = (opcodes.OP_KILL_CURSORS >> 8) & 0xff;
  302. _buffer[index] = opcodes.OP_KILL_CURSORS & 0xff;
  303. index = index + 4;
  304. // index = write32bit(index, _buffer, 0);
  305. _buffer[index + 3] = (0 >> 24) & 0xff;
  306. _buffer[index + 2] = (0 >> 16) & 0xff;
  307. _buffer[index + 1] = (0 >> 8) & 0xff;
  308. _buffer[index] = 0 & 0xff;
  309. index = index + 4;
  310. // Write batch size
  311. // index = write32bit(index, _buffer, this.cursorIds.length);
  312. _buffer[index + 3] = (this.cursorIds.length >> 24) & 0xff;
  313. _buffer[index + 2] = (this.cursorIds.length >> 16) & 0xff;
  314. _buffer[index + 1] = (this.cursorIds.length >> 8) & 0xff;
  315. _buffer[index] = this.cursorIds.length & 0xff;
  316. index = index + 4;
  317. // Write all the cursor ids into the array
  318. for (var i = 0; i < this.cursorIds.length; i++) {
  319. // Write cursor id
  320. // index = write32bit(index, _buffer, cursorIds[i].getLowBits());
  321. _buffer[index + 3] = (this.cursorIds[i].getLowBits() >> 24) & 0xff;
  322. _buffer[index + 2] = (this.cursorIds[i].getLowBits() >> 16) & 0xff;
  323. _buffer[index + 1] = (this.cursorIds[i].getLowBits() >> 8) & 0xff;
  324. _buffer[index] = this.cursorIds[i].getLowBits() & 0xff;
  325. index = index + 4;
  326. // index = write32bit(index, _buffer, cursorIds[i].getHighBits());
  327. _buffer[index + 3] = (this.cursorIds[i].getHighBits() >> 24) & 0xff;
  328. _buffer[index + 2] = (this.cursorIds[i].getHighBits() >> 16) & 0xff;
  329. _buffer[index + 1] = (this.cursorIds[i].getHighBits() >> 8) & 0xff;
  330. _buffer[index] = this.cursorIds[i].getHighBits() & 0xff;
  331. index = index + 4;
  332. }
  333. // Return buffer
  334. return _buffer;
  335. };
  336. var Response = function(bson, message, msgHeader, msgBody, opts) {
  337. opts = opts || { promoteLongs: true, promoteValues: true, promoteBuffers: false };
  338. this.parsed = false;
  339. this.raw = message;
  340. this.data = msgBody;
  341. this.bson = bson;
  342. this.opts = opts;
  343. // Read the message header
  344. this.length = msgHeader.length;
  345. this.requestId = msgHeader.requestId;
  346. this.responseTo = msgHeader.responseTo;
  347. this.opCode = msgHeader.opCode;
  348. this.fromCompressed = msgHeader.fromCompressed;
  349. // Read the message body
  350. this.responseFlags = msgBody.readInt32LE(0);
  351. this.cursorId = new Long(msgBody.readInt32LE(4), msgBody.readInt32LE(8));
  352. this.startingFrom = msgBody.readInt32LE(12);
  353. this.numberReturned = msgBody.readInt32LE(16);
  354. // Preallocate document array
  355. this.documents = new Array(this.numberReturned);
  356. // Flag values
  357. this.cursorNotFound = (this.responseFlags & CURSOR_NOT_FOUND) !== 0;
  358. this.queryFailure = (this.responseFlags & QUERY_FAILURE) !== 0;
  359. this.shardConfigStale = (this.responseFlags & SHARD_CONFIG_STALE) !== 0;
  360. this.awaitCapable = (this.responseFlags & AWAIT_CAPABLE) !== 0;
  361. this.promoteLongs = typeof opts.promoteLongs === 'boolean' ? opts.promoteLongs : true;
  362. this.promoteValues = typeof opts.promoteValues === 'boolean' ? opts.promoteValues : true;
  363. this.promoteBuffers = typeof opts.promoteBuffers === 'boolean' ? opts.promoteBuffers : false;
  364. };
  365. Response.prototype.isParsed = function() {
  366. return this.parsed;
  367. };
  368. Response.prototype.parse = function(options) {
  369. // Don't parse again if not needed
  370. if (this.parsed) return;
  371. options = options || {};
  372. // Allow the return of raw documents instead of parsing
  373. var raw = options.raw || false;
  374. var documentsReturnedIn = options.documentsReturnedIn || null;
  375. var promoteLongs =
  376. typeof options.promoteLongs === 'boolean' ? options.promoteLongs : this.opts.promoteLongs;
  377. var promoteValues =
  378. typeof options.promoteValues === 'boolean' ? options.promoteValues : this.opts.promoteValues;
  379. var promoteBuffers =
  380. typeof options.promoteBuffers === 'boolean' ? options.promoteBuffers : this.opts.promoteBuffers;
  381. var bsonSize, _options;
  382. // Set up the options
  383. _options = {
  384. promoteLongs: promoteLongs,
  385. promoteValues: promoteValues,
  386. promoteBuffers: promoteBuffers
  387. };
  388. // Position within OP_REPLY at which documents start
  389. // (See https://docs.mongodb.com/manual/reference/mongodb-wire-protocol/#wire-op-reply)
  390. this.index = 20;
  391. //
  392. // Single document and documentsReturnedIn set
  393. //
  394. if (this.numberReturned === 1 && documentsReturnedIn != null && raw) {
  395. // Calculate the bson size
  396. bsonSize =
  397. this.data[this.index] |
  398. (this.data[this.index + 1] << 8) |
  399. (this.data[this.index + 2] << 16) |
  400. (this.data[this.index + 3] << 24);
  401. // Slice out the buffer containing the command result document
  402. var document = this.data.slice(this.index, this.index + bsonSize);
  403. // Set up field we wish to keep as raw
  404. var fieldsAsRaw = {};
  405. fieldsAsRaw[documentsReturnedIn] = true;
  406. _options.fieldsAsRaw = fieldsAsRaw;
  407. // Deserialize but keep the array of documents in non-parsed form
  408. var doc = this.bson.deserialize(document, _options);
  409. if (doc instanceof Error) {
  410. throw doc;
  411. }
  412. if (doc.errmsg) {
  413. throw new MongoError(doc.errmsg);
  414. }
  415. if (!doc.cursor) {
  416. throw new MongoError('Cursor not found');
  417. }
  418. // Get the documents
  419. this.documents = doc.cursor[documentsReturnedIn];
  420. this.numberReturned = this.documents.length;
  421. // Ensure we have a Long valie cursor id
  422. this.cursorId =
  423. typeof doc.cursor.id === 'number' ? Long.fromNumber(doc.cursor.id) : doc.cursor.id;
  424. // Adjust the index
  425. this.index = this.index + bsonSize;
  426. // Set as parsed
  427. this.parsed = true;
  428. return;
  429. }
  430. //
  431. // Parse Body
  432. //
  433. for (var i = 0; i < this.numberReturned; i++) {
  434. bsonSize =
  435. this.data[this.index] |
  436. (this.data[this.index + 1] << 8) |
  437. (this.data[this.index + 2] << 16) |
  438. (this.data[this.index + 3] << 24);
  439. // If we have raw results specified slice the return document
  440. if (raw) {
  441. this.documents[i] = this.data.slice(this.index, this.index + bsonSize);
  442. } else {
  443. this.documents[i] = this.bson.deserialize(
  444. this.data.slice(this.index, this.index + bsonSize),
  445. _options
  446. );
  447. }
  448. // Adjust the index
  449. this.index = this.index + bsonSize;
  450. }
  451. // Set parsed
  452. this.parsed = true;
  453. };
  454. module.exports = {
  455. Query: Query,
  456. GetMore: GetMore,
  457. Response: Response,
  458. KillCursor: KillCursor
  459. };