Source: lib/bulk/unordered.js

  1. 'use strict';
  2. const common = require('./common');
  3. const BulkOperationBase = common.BulkOperationBase;
  4. const Batch = common.Batch;
  5. const bson = common.bson;
  6. const utils = require('../utils');
  7. const toError = utils.toError;
  8. /**
  9. * Add to internal list of Operations
  10. *
  11. * @ignore
  12. * @param {UnorderedBulkOperation} bulkOperation
  13. * @param {number} docType number indicating the document type
  14. * @param {object} document
  15. * @return {UnorderedBulkOperation}
  16. */
  17. function addToOperationsList(bulkOperation, docType, document) {
  18. // Get the bsonSize
  19. const bsonSize = bson.calculateObjectSize(document, {
  20. checkKeys: false,
  21. // Since we don't know what the user selected for BSON options here,
  22. // err on the safe side, and check the size with ignoreUndefined: false.
  23. ignoreUndefined: false
  24. });
  25. // Throw error if the doc is bigger than the max BSON size
  26. if (bsonSize >= bulkOperation.s.maxBatchSizeBytes)
  27. throw toError('document is larger than the maximum size ' + bulkOperation.s.maxBatchSizeBytes);
  28. // Holds the current batch
  29. bulkOperation.s.currentBatch = null;
  30. // Get the right type of batch
  31. if (docType === common.INSERT) {
  32. bulkOperation.s.currentBatch = bulkOperation.s.currentInsertBatch;
  33. } else if (docType === common.UPDATE) {
  34. bulkOperation.s.currentBatch = bulkOperation.s.currentUpdateBatch;
  35. } else if (docType === common.REMOVE) {
  36. bulkOperation.s.currentBatch = bulkOperation.s.currentRemoveBatch;
  37. }
  38. const maxKeySize = bulkOperation.s.maxKeySize;
  39. // Create a new batch object if we don't have a current one
  40. if (bulkOperation.s.currentBatch == null)
  41. bulkOperation.s.currentBatch = new Batch(docType, bulkOperation.s.currentIndex);
  42. // Check if we need to create a new batch
  43. if (
  44. bulkOperation.s.currentBatch.size + 1 >= bulkOperation.s.maxWriteBatchSize ||
  45. bulkOperation.s.currentBatch.sizeBytes + maxKeySize + bsonSize >=
  46. bulkOperation.s.maxBatchSizeBytes ||
  47. bulkOperation.s.currentBatch.batchType !== docType
  48. ) {
  49. // Save the batch to the execution stack
  50. bulkOperation.s.batches.push(bulkOperation.s.currentBatch);
  51. // Create a new batch
  52. bulkOperation.s.currentBatch = new Batch(docType, bulkOperation.s.currentIndex);
  53. }
  54. // We have an array of documents
  55. if (Array.isArray(document)) {
  56. throw toError('operation passed in cannot be an Array');
  57. }
  58. bulkOperation.s.currentBatch.operations.push(document);
  59. bulkOperation.s.currentBatch.originalIndexes.push(bulkOperation.s.currentIndex);
  60. bulkOperation.s.currentIndex = bulkOperation.s.currentIndex + 1;
  61. // Save back the current Batch to the right type
  62. if (docType === common.INSERT) {
  63. bulkOperation.s.currentInsertBatch = bulkOperation.s.currentBatch;
  64. bulkOperation.s.bulkResult.insertedIds.push({
  65. index: bulkOperation.s.bulkResult.insertedIds.length,
  66. _id: document._id
  67. });
  68. } else if (docType === common.UPDATE) {
  69. bulkOperation.s.currentUpdateBatch = bulkOperation.s.currentBatch;
  70. } else if (docType === common.REMOVE) {
  71. bulkOperation.s.currentRemoveBatch = bulkOperation.s.currentBatch;
  72. }
  73. // Update current batch size
  74. bulkOperation.s.currentBatch.size += 1;
  75. bulkOperation.s.currentBatch.sizeBytes += maxKeySize + bsonSize;
  76. // Return bulkOperation
  77. return bulkOperation;
  78. }
  79. /**
  80. * Create a new UnorderedBulkOperation instance (INTERNAL TYPE, do not instantiate directly)
  81. * @class
  82. * @extends BulkOperationBase
  83. * @property {number} length Get the number of operations in the bulk.
  84. * @return {UnorderedBulkOperation} a UnorderedBulkOperation instance.
  85. */
  86. class UnorderedBulkOperation extends BulkOperationBase {
  87. constructor(topology, collection, options) {
  88. options = options || {};
  89. options = Object.assign(options, { addToOperationsList });
  90. super(topology, collection, options, false);
  91. }
  92. }
  93. /**
  94. * Returns an unordered batch object
  95. * @ignore
  96. */
  97. function initializeUnorderedBulkOp(topology, collection, options) {
  98. return new UnorderedBulkOperation(topology, collection, options);
  99. }
  100. initializeUnorderedBulkOp.UnorderedBulkOperation = UnorderedBulkOperation;
  101. module.exports = initializeUnorderedBulkOp;
  102. module.exports.Bulk = UnorderedBulkOperation;