unordered.js 4.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.UnorderedBulkOperation = void 0;
  4. const BSON = require("../bson");
  5. const error_1 = require("../error");
  6. const common_1 = require("./common");
  7. /** @public */
  8. class UnorderedBulkOperation extends common_1.BulkOperationBase {
  9. constructor(collection, options) {
  10. super(collection, options, false);
  11. }
  12. handleWriteError(callback, writeResult) {
  13. if (this.s.batches.length) {
  14. return false;
  15. }
  16. return super.handleWriteError(callback, writeResult);
  17. }
  18. addToOperationsList(batchType, document) {
  19. // Get the bsonSize
  20. const bsonSize = BSON.calculateObjectSize(document, {
  21. checkKeys: false,
  22. // Since we don't know what the user selected for BSON options here,
  23. // err on the safe side, and check the size with ignoreUndefined: false.
  24. ignoreUndefined: false
  25. });
  26. // Throw error if the doc is bigger than the max BSON size
  27. if (bsonSize >= this.s.maxBsonObjectSize) {
  28. // TODO(NODE-3483): Change this to MongoBSONError
  29. throw new error_1.MongoInvalidArgumentError(`Document is larger than the maximum size ${this.s.maxBsonObjectSize}`);
  30. }
  31. // Holds the current batch
  32. this.s.currentBatch = undefined;
  33. // Get the right type of batch
  34. if (batchType === common_1.BatchType.INSERT) {
  35. this.s.currentBatch = this.s.currentInsertBatch;
  36. }
  37. else if (batchType === common_1.BatchType.UPDATE) {
  38. this.s.currentBatch = this.s.currentUpdateBatch;
  39. }
  40. else if (batchType === common_1.BatchType.DELETE) {
  41. this.s.currentBatch = this.s.currentRemoveBatch;
  42. }
  43. const maxKeySize = this.s.maxKeySize;
  44. // Create a new batch object if we don't have a current one
  45. if (this.s.currentBatch == null) {
  46. this.s.currentBatch = new common_1.Batch(batchType, this.s.currentIndex);
  47. }
  48. // Check if we need to create a new batch
  49. if (
  50. // New batch if we exceed the max batch op size
  51. this.s.currentBatch.size + 1 >= this.s.maxWriteBatchSize ||
  52. // New batch if we exceed the maxBatchSizeBytes. Only matters if batch already has a doc,
  53. // since we can't sent an empty batch
  54. (this.s.currentBatch.size > 0 &&
  55. this.s.currentBatch.sizeBytes + maxKeySize + bsonSize >= this.s.maxBatchSizeBytes) ||
  56. // New batch if the new op does not have the same op type as the current batch
  57. this.s.currentBatch.batchType !== batchType) {
  58. // Save the batch to the execution stack
  59. this.s.batches.push(this.s.currentBatch);
  60. // Create a new batch
  61. this.s.currentBatch = new common_1.Batch(batchType, this.s.currentIndex);
  62. }
  63. // We have an array of documents
  64. if (Array.isArray(document)) {
  65. throw new error_1.MongoInvalidArgumentError('Operation passed in cannot be an Array');
  66. }
  67. this.s.currentBatch.operations.push(document);
  68. this.s.currentBatch.originalIndexes.push(this.s.currentIndex);
  69. this.s.currentIndex = this.s.currentIndex + 1;
  70. // Save back the current Batch to the right type
  71. if (batchType === common_1.BatchType.INSERT) {
  72. this.s.currentInsertBatch = this.s.currentBatch;
  73. this.s.bulkResult.insertedIds.push({
  74. index: this.s.bulkResult.insertedIds.length,
  75. _id: document._id
  76. });
  77. }
  78. else if (batchType === common_1.BatchType.UPDATE) {
  79. this.s.currentUpdateBatch = this.s.currentBatch;
  80. }
  81. else if (batchType === common_1.BatchType.DELETE) {
  82. this.s.currentRemoveBatch = this.s.currentBatch;
  83. }
  84. // Update current batch size
  85. this.s.currentBatch.size += 1;
  86. this.s.currentBatch.sizeBytes += maxKeySize + bsonSize;
  87. return this;
  88. }
  89. }
  90. exports.UnorderedBulkOperation = UnorderedBulkOperation;
  91. //# sourceMappingURL=unordered.js.map