unordered.js 4.1 KB

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