123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178 |
- 'use strict';
- const common = require('./common');
- const BulkOperationBase = common.BulkOperationBase;
- const utils = require('../utils');
- const toError = utils.toError;
- const handleCallback = utils.handleCallback;
- const BulkWriteResult = common.BulkWriteResult;
- const Batch = common.Batch;
- const mergeBatchResults = common.mergeBatchResults;
- const executeOperation = utils.executeOperation;
- const MongoWriteConcernError = require('mongodb-core').MongoWriteConcernError;
- const handleMongoWriteConcernError = require('./common').handleMongoWriteConcernError;
- const bson = common.bson;
- function addToOperationsList(bulkOperation, docType, document) {
-
- const bsonSize = bson.calculateObjectSize(document, {
- checkKeys: false
- });
-
- if (bsonSize >= bulkOperation.s.maxBatchSizeBytes)
- throw toError('document is larger than the maximum size ' + bulkOperation.s.maxBatchSizeBytes);
-
- if (bulkOperation.s.currentBatch == null)
- bulkOperation.s.currentBatch = new Batch(docType, bulkOperation.s.currentIndex);
-
- if (
- bulkOperation.s.currentBatchSize + 1 >= bulkOperation.s.maxWriteBatchSize ||
- bulkOperation.s.currentBatchSizeBytes + bulkOperation.s.currentBatchSizeBytes >=
- bulkOperation.s.maxBatchSizeBytes ||
- bulkOperation.s.currentBatch.batchType !== docType
- ) {
-
- bulkOperation.s.batches.push(bulkOperation.s.currentBatch);
-
- bulkOperation.s.currentBatch = new Batch(docType, bulkOperation.s.currentIndex);
-
- bulkOperation.s.currentBatchSize = 0;
- bulkOperation.s.currentBatchSizeBytes = 0;
- } else {
-
- bulkOperation.s.currentBatchSize = bulkOperation.s.currentBatchSize + 1;
- bulkOperation.s.currentBatchSizeBytes = bulkOperation.s.currentBatchSizeBytes + bsonSize;
- }
- if (docType === common.INSERT) {
- bulkOperation.s.bulkResult.insertedIds.push({
- index: bulkOperation.s.currentIndex,
- _id: document._id
- });
- }
-
- if (Array.isArray(document)) {
- throw toError('operation passed in cannot be an Array');
- } else {
- bulkOperation.s.currentBatch.originalIndexes.push(bulkOperation.s.currentIndex);
- bulkOperation.s.currentBatch.operations.push(document);
- bulkOperation.s.currentBatchSizeBytes = bulkOperation.s.currentBatchSizeBytes + bsonSize;
- bulkOperation.s.currentIndex = bulkOperation.s.currentIndex + 1;
- }
-
- return bulkOperation;
- }
- class OrderedBulkOperation extends BulkOperationBase {
- constructor(topology, collection, options) {
- options = options || {};
- options = Object.assign(options, { addToOperationsList });
- super(topology, collection, options, true);
- }
-
-
- execute(_writeConcern, options, callback) {
- const ret = this.bulkExecute(_writeConcern, options, callback);
- options = ret.options;
- callback = ret.callback;
- return executeOperation(this.s.topology, executeCommands, [this, options, callback]);
- }
- }
- function executeCommands(bulkOperation, options, callback) {
- if (bulkOperation.s.batches.length === 0) {
- return handleCallback(callback, null, new BulkWriteResult(bulkOperation.s.bulkResult));
- }
-
- const batch = bulkOperation.s.batches.shift();
- function resultHandler(err, result) {
-
- if (((err && err.driver) || (err && err.message)) && !(err instanceof MongoWriteConcernError)) {
- return handleCallback(callback, err);
- }
-
- if (err) err.ok = 0;
- if (err instanceof MongoWriteConcernError) {
- return handleMongoWriteConcernError(batch, bulkOperation.s.bulkResult, true, err, callback);
- }
-
- const writeResult = new BulkWriteResult(bulkOperation.s.bulkResult);
- const mergeResult = mergeBatchResults(true, batch, bulkOperation.s.bulkResult, err, result);
- if (mergeResult != null) {
- return handleCallback(callback, null, writeResult);
- }
- if (bulkOperation.handleWriteError(callback, writeResult)) return;
-
- executeCommands(bulkOperation, options, callback);
- }
- bulkOperation.finalOptionsHandler({ options, batch, resultHandler }, callback);
- }
- function initializeOrderedBulkOp(topology, collection, options) {
- return new OrderedBulkOperation(topology, collection, options);
- }
- initializeOrderedBulkOp.OrderedBulkOperation = OrderedBulkOperation;
- module.exports = initializeOrderedBulkOp;
- module.exports.Bulk = OrderedBulkOperation;
|