ordered.js 3.0 KB

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