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.

unordered.js 7.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. 'use strict';
  2. const common = require('./common');
  3. const BulkOperationBase = common.BulkOperationBase;
  4. const utils = require('../utils');
  5. const toError = utils.toError;
  6. const handleCallback = utils.handleCallback;
  7. const BulkWriteResult = common.BulkWriteResult;
  8. const Batch = common.Batch;
  9. const mergeBatchResults = common.mergeBatchResults;
  10. const executeOperation = utils.executeOperation;
  11. const MongoWriteConcernError = require('mongodb-core').MongoWriteConcernError;
  12. const handleMongoWriteConcernError = require('./common').handleMongoWriteConcernError;
  13. const bson = common.bson;
  14. const isPromiseLike = require('../utils').isPromiseLike;
  15. /**
  16. * Add to internal list of Operations
  17. *
  18. * @param {UnorderedBulkOperation} bulkOperation
  19. * @param {number} docType number indicating the document type
  20. * @param {object} document
  21. * @return {UnorderedBulkOperation}
  22. */
  23. function addToOperationsList(bulkOperation, docType, document) {
  24. // Get the bsonSize
  25. const bsonSize = bson.calculateObjectSize(document, {
  26. checkKeys: false
  27. });
  28. // Throw error if the doc is bigger than the max BSON size
  29. if (bsonSize >= bulkOperation.s.maxBatchSizeBytes)
  30. throw toError('document is larger than the maximum size ' + bulkOperation.s.maxBatchSizeBytes);
  31. // Holds the current batch
  32. bulkOperation.s.currentBatch = null;
  33. // Get the right type of batch
  34. if (docType === common.INSERT) {
  35. bulkOperation.s.currentBatch = bulkOperation.s.currentInsertBatch;
  36. } else if (docType === common.UPDATE) {
  37. bulkOperation.s.currentBatch = bulkOperation.s.currentUpdateBatch;
  38. } else if (docType === common.REMOVE) {
  39. bulkOperation.s.currentBatch = bulkOperation.s.currentRemoveBatch;
  40. }
  41. const maxKeySize = bulkOperation.s.maxKeySize;
  42. // Create a new batch object if we don't have a current one
  43. if (bulkOperation.s.currentBatch == null)
  44. bulkOperation.s.currentBatch = new Batch(docType, bulkOperation.s.currentIndex);
  45. // Check if we need to create a new batch
  46. if (
  47. bulkOperation.s.currentBatch.size + 1 >= bulkOperation.s.maxWriteBatchSize ||
  48. bulkOperation.s.currentBatch.sizeBytes + maxKeySize + bsonSize >=
  49. bulkOperation.s.maxBatchSizeBytes ||
  50. bulkOperation.s.currentBatch.batchType !== docType
  51. ) {
  52. // Save the batch to the execution stack
  53. bulkOperation.s.batches.push(bulkOperation.s.currentBatch);
  54. // Create a new batch
  55. bulkOperation.s.currentBatch = new Batch(docType, bulkOperation.s.currentIndex);
  56. }
  57. // We have an array of documents
  58. if (Array.isArray(document)) {
  59. throw toError('operation passed in cannot be an Array');
  60. }
  61. bulkOperation.s.currentBatch.operations.push(document);
  62. bulkOperation.s.currentBatch.originalIndexes.push(bulkOperation.s.currentIndex);
  63. bulkOperation.s.currentIndex = bulkOperation.s.currentIndex + 1;
  64. // Save back the current Batch to the right type
  65. if (docType === common.INSERT) {
  66. bulkOperation.s.currentInsertBatch = bulkOperation.s.currentBatch;
  67. bulkOperation.s.bulkResult.insertedIds.push({
  68. index: bulkOperation.s.bulkResult.insertedIds.length,
  69. _id: document._id
  70. });
  71. } else if (docType === common.UPDATE) {
  72. bulkOperation.s.currentUpdateBatch = bulkOperation.s.currentBatch;
  73. } else if (docType === common.REMOVE) {
  74. bulkOperation.s.currentRemoveBatch = bulkOperation.s.currentBatch;
  75. }
  76. // Update current batch size
  77. bulkOperation.s.currentBatch.size += 1;
  78. bulkOperation.s.currentBatch.sizeBytes += maxKeySize + bsonSize;
  79. // Return bulkOperation
  80. return bulkOperation;
  81. }
  82. /**
  83. * Create a new UnorderedBulkOperation instance (INTERNAL TYPE, do not instantiate directly)
  84. * @class
  85. * @property {number} length Get the number of operations in the bulk.
  86. * @return {UnorderedBulkOperation} a UnorderedBulkOperation instance.
  87. */
  88. class UnorderedBulkOperation extends BulkOperationBase {
  89. constructor(topology, collection, options) {
  90. options = options || {};
  91. options = Object.assign(options, { addToOperationsList });
  92. super(topology, collection, options, false);
  93. }
  94. /**
  95. * The callback format for results
  96. * @callback UnorderedBulkOperation~resultCallback
  97. * @param {MongoError} error An error instance representing the error during the execution.
  98. * @param {BulkWriteResult} result The bulk write result.
  99. */
  100. /**
  101. * Execute the ordered bulk operation
  102. *
  103. * @method
  104. * @param {object} [options] Optional settings.
  105. * @param {(number|string)} [options.w] The write concern.
  106. * @param {number} [options.wtimeout] The write concern timeout.
  107. * @param {boolean} [options.j=false] Specify a journal write concern.
  108. * @param {boolean} [options.fsync=false] Specify a file sync write concern.
  109. * @param {UnorderedBulkOperation~resultCallback} [callback] The result callback
  110. * @throws {MongoError}
  111. * @return {Promise} returns Promise if no callback passed
  112. */
  113. execute(_writeConcern, options, callback) {
  114. const ret = this.bulkExecute(_writeConcern, options, callback);
  115. if (isPromiseLike(ret)) {
  116. return ret;
  117. }
  118. options = ret.options;
  119. callback = ret.callback;
  120. return executeOperation(this.s.topology, executeBatches, [this, options, callback]);
  121. }
  122. }
  123. /**
  124. * Execute the command
  125. *
  126. * @param {UnorderedBulkOperation} bulkOperation
  127. * @param {object} batch
  128. * @param {object} options
  129. * @param {function} callback
  130. */
  131. function executeBatch(bulkOperation, batch, options, callback) {
  132. function resultHandler(err, result) {
  133. // Error is a driver related error not a bulk op error, terminate
  134. if (((err && err.driver) || (err && err.message)) && !(err instanceof MongoWriteConcernError)) {
  135. return handleCallback(callback, err);
  136. }
  137. // If we have and error
  138. if (err) err.ok = 0;
  139. if (err instanceof MongoWriteConcernError) {
  140. return handleMongoWriteConcernError(batch, bulkOperation.s.bulkResult, false, err, callback);
  141. }
  142. handleCallback(
  143. callback,
  144. null,
  145. mergeBatchResults(false, batch, bulkOperation.s.bulkResult, err, result)
  146. );
  147. }
  148. bulkOperation.finalOptionsHandler({ options, batch, resultHandler }, callback);
  149. }
  150. /**
  151. * Execute all the commands
  152. *
  153. * @param {UnorderedBulkOperation} bulkOperation
  154. * @param {object} options
  155. * @param {function} callback
  156. */
  157. function executeBatches(bulkOperation, options, callback) {
  158. let numberOfCommandsToExecute = bulkOperation.s.batches.length;
  159. let hasErrored = false;
  160. // Execute over all the batches
  161. for (let i = 0; i < bulkOperation.s.batches.length; i++) {
  162. executeBatch(bulkOperation, bulkOperation.s.batches[i], options, function(err) {
  163. if (hasErrored) {
  164. return;
  165. }
  166. if (err) {
  167. hasErrored = true;
  168. return handleCallback(callback, err);
  169. }
  170. // Count down the number of commands left to execute
  171. numberOfCommandsToExecute = numberOfCommandsToExecute - 1;
  172. // Execute
  173. if (numberOfCommandsToExecute === 0) {
  174. // Driver level error
  175. if (err) return handleCallback(callback, err);
  176. const writeResult = new BulkWriteResult(bulkOperation.s.bulkResult);
  177. if (bulkOperation.handleWriteError(callback, writeResult)) return;
  178. return handleCallback(callback, null, writeResult);
  179. }
  180. });
  181. }
  182. }
  183. /**
  184. * Returns an unordered batch object
  185. * @ignore
  186. */
  187. function initializeUnorderedBulkOp(topology, collection, options) {
  188. return new UnorderedBulkOperation(topology, collection, options);
  189. }
  190. initializeUnorderedBulkOp.UnorderedBulkOperation = UnorderedBulkOperation;
  191. module.exports = initializeUnorderedBulkOp;
  192. module.exports.Bulk = UnorderedBulkOperation;