ordered.js 3.1 KB

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