1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192 |
- "use strict";
- Object.defineProperty(exports, "__esModule", { value: true });
- exports.UnorderedBulkOperation = void 0;
- const BSON = require("../bson");
- const error_1 = require("../error");
- const common_1 = require("./common");
- class UnorderedBulkOperation extends common_1.BulkOperationBase {
-
- constructor(collection, options) {
- super(collection, options, false);
- }
- handleWriteError(callback, writeResult) {
- if (this.s.batches.length) {
- return false;
- }
- return super.handleWriteError(callback, writeResult);
- }
- addToOperationsList(batchType, document) {
-
- const bsonSize = BSON.calculateObjectSize(document, {
- checkKeys: false,
-
-
- ignoreUndefined: false
- });
-
- if (bsonSize >= this.s.maxBsonObjectSize) {
-
- throw new error_1.MongoInvalidArgumentError(`Document is larger than the maximum size ${this.s.maxBsonObjectSize}`);
- }
-
- this.s.currentBatch = undefined;
-
- if (batchType === common_1.BatchType.INSERT) {
- this.s.currentBatch = this.s.currentInsertBatch;
- }
- else if (batchType === common_1.BatchType.UPDATE) {
- this.s.currentBatch = this.s.currentUpdateBatch;
- }
- else if (batchType === common_1.BatchType.DELETE) {
- this.s.currentBatch = this.s.currentRemoveBatch;
- }
- const maxKeySize = this.s.maxKeySize;
-
- if (this.s.currentBatch == null) {
- this.s.currentBatch = new common_1.Batch(batchType, this.s.currentIndex);
- }
-
- if (
-
- this.s.currentBatch.size + 1 >= this.s.maxWriteBatchSize ||
-
-
- (this.s.currentBatch.size > 0 &&
- this.s.currentBatch.sizeBytes + maxKeySize + bsonSize >= this.s.maxBatchSizeBytes) ||
-
- this.s.currentBatch.batchType !== batchType) {
-
- this.s.batches.push(this.s.currentBatch);
-
- this.s.currentBatch = new common_1.Batch(batchType, this.s.currentIndex);
- }
-
- if (Array.isArray(document)) {
- throw new error_1.MongoInvalidArgumentError('Operation passed in cannot be an Array');
- }
- this.s.currentBatch.operations.push(document);
- this.s.currentBatch.originalIndexes.push(this.s.currentIndex);
- this.s.currentIndex = this.s.currentIndex + 1;
-
- if (batchType === common_1.BatchType.INSERT) {
- this.s.currentInsertBatch = this.s.currentBatch;
- this.s.bulkResult.insertedIds.push({
- index: this.s.bulkResult.insertedIds.length,
- _id: document._id
- });
- }
- else if (batchType === common_1.BatchType.UPDATE) {
- this.s.currentUpdateBatch = this.s.currentBatch;
- }
- else if (batchType === common_1.BatchType.DELETE) {
- this.s.currentRemoveBatch = this.s.currentBatch;
- }
-
- this.s.currentBatch.size += 1;
- this.s.currentBatch.sizeBytes += maxKeySize + bsonSize;
- return this;
- }
- }
- exports.UnorderedBulkOperation = UnorderedBulkOperation;
|