commands.js 17 KB

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