unordered.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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.maxBsonObjectSize)
  27. throw toError('document is larger than the maximum size ' + bulkOperation.s.maxBsonObjectSize);
  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. // New batch if we exceed the max batch op size
  45. bulkOperation.s.currentBatch.size + 1 >= bulkOperation.s.maxWriteBatchSize ||
  46. // New batch if we exceed the maxBatchSizeBytes. Only matters if batch already has a doc,
  47. // since we can't sent an empty batch
  48. (bulkOperation.s.currentBatch.size > 0 &&
  49. bulkOperation.s.currentBatch.sizeBytes + maxKeySize + bsonSize >=
  50. bulkOperation.s.maxBatchSizeBytes) ||
  51. // New batch if the new op does not have the same op type as the current batch
  52. bulkOperation.s.currentBatch.batchType !== docType
  53. ) {
  54. // Save the batch to the execution stack
  55. bulkOperation.s.batches.push(bulkOperation.s.currentBatch);
  56. // Create a new batch
  57. bulkOperation.s.currentBatch = new Batch(docType, bulkOperation.s.currentIndex);
  58. }
  59. // We have an array of documents
  60. if (Array.isArray(document)) {
  61. throw toError('operation passed in cannot be an Array');
  62. }
  63. bulkOperation.s.currentBatch.operations.push(document);
  64. bulkOperation.s.currentBatch.originalIndexes.push(bulkOperation.s.currentIndex);
  65. bulkOperation.s.currentIndex = bulkOperation.s.currentIndex + 1;
  66. // Save back the current Batch to the right type
  67. if (docType === common.INSERT) {
  68. bulkOperation.s.currentInsertBatch = bulkOperation.s.currentBatch;
  69. bulkOperation.s.bulkResult.insertedIds.push({
  70. index: bulkOperation.s.bulkResult.insertedIds.length,
  71. _id: document._id
  72. });
  73. } else if (docType === common.UPDATE) {
  74. bulkOperation.s.currentUpdateBatch = bulkOperation.s.currentBatch;
  75. } else if (docType === common.REMOVE) {
  76. bulkOperation.s.currentRemoveBatch = bulkOperation.s.currentBatch;
  77. }
  78. // Update current batch size
  79. bulkOperation.s.currentBatch.size += 1;
  80. bulkOperation.s.currentBatch.sizeBytes += maxKeySize + bsonSize;
  81. // Return bulkOperation
  82. return bulkOperation;
  83. }
  84. /**
  85. * Create a new UnorderedBulkOperation instance (INTERNAL TYPE, do not instantiate directly)
  86. * @class
  87. * @extends BulkOperationBase
  88. * @property {number} length Get the number of operations in the bulk.
  89. * @return {UnorderedBulkOperation} a UnorderedBulkOperation instance.
  90. */
  91. class UnorderedBulkOperation extends BulkOperationBase {
  92. constructor(topology, collection, options) {
  93. options = options || {};
  94. options = Object.assign(options, { addToOperationsList });
  95. super(topology, collection, options, false);
  96. }
  97. handleWriteError(callback, writeResult) {
  98. if (this.s.batches.length) {
  99. return false;
  100. }
  101. return super.handleWriteError(callback, writeResult);
  102. }
  103. }
  104. /**
  105. * Returns an unordered batch object
  106. * @ignore
  107. */
  108. function initializeUnorderedBulkOp(topology, collection, options) {
  109. return new UnorderedBulkOperation(topology, collection, options);
  110. }
  111. initializeUnorderedBulkOp.UnorderedBulkOperation = UnorderedBulkOperation;
  112. module.exports = initializeUnorderedBulkOp;
  113. module.exports.Bulk = UnorderedBulkOperation;