| 14 | } |
| 15 | |
| 16 | addToOperationsList( |
| 17 | batchType: BatchType, |
| 18 | document: Document | UpdateStatement | DeleteStatement |
| 19 | ): this { |
| 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 | } as any); |
| 27 | |
| 28 | // Throw error if the doc is bigger than the max BSON size |
| 29 | if (bsonSize >= this.s.maxBsonObjectSize) |
| 30 | // TODO(NODE-3483): Change this to MongoBSONError |
| 31 | throw new MongoInvalidArgumentError( |
| 32 | `Document is larger than the maximum size ${this.s.maxBsonObjectSize}` |
| 33 | ); |
| 34 | |
| 35 | // Create a new batch object if we don't have a current one |
| 36 | if (this.s.currentBatch == null) { |
| 37 | this.s.currentBatch = new Batch(batchType, this.s.currentIndex); |
| 38 | } |
| 39 | |
| 40 | const maxKeySize = this.s.maxKeySize; |
| 41 | |
| 42 | // Check if we need to create a new batch |
| 43 | if ( |
| 44 | // New batch if we exceed the max batch op size |
| 45 | this.s.currentBatchSize + 1 >= this.s.maxWriteBatchSize || |
| 46 | // New batch if we exceed the maxBatchSizeBytes. Only matters if batch already has a doc, |
| 47 | // since we can't sent an empty batch |
| 48 | (this.s.currentBatchSize > 0 && |
| 49 | this.s.currentBatchSizeBytes + maxKeySize + bsonSize >= this.s.maxBatchSizeBytes) || |
| 50 | // New batch if the new op does not have the same op type as the current batch |
| 51 | this.s.currentBatch.batchType !== batchType |
| 52 | ) { |
| 53 | // Save the batch to the execution stack |
| 54 | this.s.batches.push(this.s.currentBatch); |
| 55 | |
| 56 | // Create a new batch |
| 57 | this.s.currentBatch = new Batch(batchType, this.s.currentIndex); |
| 58 | |
| 59 | // Reset the current size trackers |
| 60 | this.s.currentBatchSize = 0; |
| 61 | this.s.currentBatchSizeBytes = 0; |
| 62 | } |
| 63 | |
| 64 | if (batchType === BatchType.INSERT) { |
| 65 | this.s.bulkResult.insertedIds.push({ |
| 66 | index: this.s.currentIndex, |
| 67 | _id: (document as Document)._id |
| 68 | }); |
| 69 | } |
| 70 | |
| 71 | // We have an array of documents |
| 72 | if (Array.isArray(document)) { |
| 73 | throw new MongoInvalidArgumentError('Operation passed in cannot be an Array'); |