ordered.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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 {OrderedBulkOperation} bulkOperation
  13. * @param {number} docType number indicating the document type
  14. * @param {object} document
  15. * @return {OrderedBulkOperation}
  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. // Create a new batch object if we don't have a current one
  29. if (bulkOperation.s.currentBatch == null)
  30. bulkOperation.s.currentBatch = new Batch(docType, bulkOperation.s.currentIndex);
  31. const maxKeySize = bulkOperation.s.maxKeySize;
  32. // Check if we need to create a new batch
  33. if (
  34. // New batch if we exceed the max batch op size
  35. bulkOperation.s.currentBatchSize + 1 >= bulkOperation.s.maxWriteBatchSize ||
  36. // New batch if we exceed the maxBatchSizeBytes. Only matters if batch already has a doc,
  37. // since we can't sent an empty batch
  38. (bulkOperation.s.currentBatchSize > 0 &&
  39. bulkOperation.s.currentBatchSizeBytes + maxKeySize + bsonSize >=
  40. bulkOperation.s.maxBatchSizeBytes) ||
  41. // New batch if the new op does not have the same op type as the current batch
  42. bulkOperation.s.currentBatch.batchType !== docType
  43. ) {
  44. // Save the batch to the execution stack
  45. bulkOperation.s.batches.push(bulkOperation.s.currentBatch);
  46. // Create a new batch
  47. bulkOperation.s.currentBatch = new Batch(docType, bulkOperation.s.currentIndex);
  48. // Reset the current size trackers
  49. bulkOperation.s.currentBatchSize = 0;
  50. bulkOperation.s.currentBatchSizeBytes = 0;
  51. }
  52. if (docType === common.INSERT) {
  53. bulkOperation.s.bulkResult.insertedIds.push({
  54. index: bulkOperation.s.currentIndex,
  55. _id: document._id
  56. });
  57. }
  58. // We have an array of documents
  59. if (Array.isArray(document)) {
  60. throw toError('operation passed in cannot be an Array');
  61. }
  62. bulkOperation.s.currentBatch.originalIndexes.push(bulkOperation.s.currentIndex);
  63. bulkOperation.s.currentBatch.operations.push(document);
  64. bulkOperation.s.currentBatchSize += 1;
  65. bulkOperation.s.currentBatchSizeBytes += maxKeySize + bsonSize;
  66. bulkOperation.s.currentIndex += 1;
  67. // Return bulkOperation
  68. return bulkOperation;
  69. }
  70. /**
  71. * Create a new OrderedBulkOperation instance (INTERNAL TYPE, do not instantiate directly)
  72. * @class
  73. * @extends BulkOperationBase
  74. * @property {number} length Get the number of operations in the bulk.
  75. * @return {OrderedBulkOperation} a OrderedBulkOperation instance.
  76. */
  77. class OrderedBulkOperation extends BulkOperationBase {
  78. constructor(topology, collection, options) {
  79. options = options || {};
  80. options = Object.assign(options, { addToOperationsList });
  81. super(topology, collection, options, true);
  82. }
  83. }
  84. /**
  85. * Returns an unordered batch object
  86. * @ignore
  87. */
  88. function initializeOrderedBulkOp(topology, collection, options) {
  89. return new OrderedBulkOperation(topology, collection, options);
  90. }
  91. initializeOrderedBulkOp.OrderedBulkOperation = OrderedBulkOperation;
  92. module.exports = initializeOrderedBulkOp;
  93. module.exports.Bulk = OrderedBulkOperation;