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

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