Update Node_modules
This commit is contained in:
+100
-48
@@ -2,18 +2,18 @@
|
||||
|
||||
const Long = require('mongodb-core').BSON.Long;
|
||||
const MongoError = require('mongodb-core').MongoError;
|
||||
const ObjectID = require('mongodb-core').BSON.ObjectID;
|
||||
const BSON = require('mongodb-core').BSON;
|
||||
const MongoWriteConcernError = require('mongodb-core').MongoWriteConcernError;
|
||||
const toError = require('../utils').toError;
|
||||
const handleCallback = require('../utils').handleCallback;
|
||||
const applyRetryableWrites = require('../utils').applyRetryableWrites;
|
||||
const applyWriteConcern = require('../utils').applyWriteConcern;
|
||||
const ObjectID = require('mongodb-core').BSON.ObjectID;
|
||||
const BSON = require('mongodb-core').BSON;
|
||||
const executeOperation = require('../utils').executeOperation;
|
||||
const isPromiseLike = require('../utils').isPromiseLike;
|
||||
|
||||
// Error codes
|
||||
const UNKNOWN_ERROR = 8;
|
||||
const INVALID_BSON_ERROR = 22;
|
||||
const WRITE_CONCERN_ERROR = 64;
|
||||
const MULTIPLE_ERROR = 65;
|
||||
|
||||
// Insert types
|
||||
const INSERT = 1;
|
||||
@@ -54,18 +54,6 @@ class Batch {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Wraps a legacy operation so we can correctly rewrite it's error
|
||||
* @ignore
|
||||
*/
|
||||
class LegacyOp {
|
||||
constructor(batchType, operation, index) {
|
||||
this.batchType = batchType;
|
||||
this.index = index;
|
||||
this.operation = operation;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new BulkWriteResult instance (INTERNAL TYPE, do not instantiate directly)
|
||||
*
|
||||
@@ -352,7 +340,7 @@ class WriteError {
|
||||
* Merges results into shared data structure
|
||||
* @ignore
|
||||
*/
|
||||
function mergeBatchResults(ordered, batch, bulkResult, err, result) {
|
||||
function mergeBatchResults(batch, bulkResult, err, result) {
|
||||
// If we have an error set the result to be the err object
|
||||
if (err) {
|
||||
result = err;
|
||||
@@ -484,6 +472,41 @@ function mergeBatchResults(ordered, batch, bulkResult, err, result) {
|
||||
}
|
||||
}
|
||||
|
||||
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) {
|
||||
// Error is a driver related error not a bulk op error, terminate
|
||||
if (((err && err.driver) || (err && err.message)) && !(err instanceof MongoWriteConcernError)) {
|
||||
return handleCallback(callback, err);
|
||||
}
|
||||
|
||||
// If we have and error
|
||||
if (err) err.ok = 0;
|
||||
if (err instanceof MongoWriteConcernError) {
|
||||
return handleMongoWriteConcernError(batch, bulkOperation.s.bulkResult, err, callback);
|
||||
}
|
||||
|
||||
// Merge the results together
|
||||
const writeResult = new BulkWriteResult(bulkOperation.s.bulkResult);
|
||||
const mergeResult = mergeBatchResults(batch, bulkOperation.s.bulkResult, err, result);
|
||||
if (mergeResult != null) {
|
||||
return handleCallback(callback, null, writeResult);
|
||||
}
|
||||
|
||||
if (bulkOperation.handleWriteError(callback, writeResult)) return;
|
||||
|
||||
// Execute the next command in line
|
||||
executeCommands(bulkOperation, options, callback);
|
||||
}
|
||||
|
||||
bulkOperation.finalOptionsHandler({ options, batch, resultHandler }, callback);
|
||||
}
|
||||
|
||||
/**
|
||||
* handles write concern error
|
||||
*
|
||||
@@ -493,8 +516,8 @@ function mergeBatchResults(ordered, batch, bulkResult, err, result) {
|
||||
* @param {WriteConcernError} err
|
||||
* @param {function} callback
|
||||
*/
|
||||
function handleMongoWriteConcernError(batch, bulkResult, ordered, err, callback) {
|
||||
mergeBatchResults(ordered, batch, bulkResult, null, err.result);
|
||||
function handleMongoWriteConcernError(batch, bulkResult, err, callback) {
|
||||
mergeBatchResults(batch, bulkResult, null, err.result);
|
||||
|
||||
const wrappedWriteConcernError = new WriteConcernError({
|
||||
errmsg: err.result.writeConcernError.errmsg,
|
||||
@@ -702,6 +725,13 @@ class BulkOperationBase {
|
||||
const maxWriteBatchSize =
|
||||
isMaster && isMaster.maxWriteBatchSize ? isMaster.maxWriteBatchSize : 1000;
|
||||
|
||||
// Calculates the largest possible size of an Array key, represented as a BSON string
|
||||
// element. This calculation:
|
||||
// 1 byte for BSON type
|
||||
// # of bytes = length of (string representation of (maxWriteBatchSize - 1))
|
||||
// + 1 bytes for null terminator
|
||||
const maxKeySize = (maxWriteBatchSize - 1).toString(10).length + 2;
|
||||
|
||||
// Final options for retryable writes and write concern
|
||||
let finalOptions = Object.assign({}, options);
|
||||
finalOptions = applyRetryableWrites(finalOptions, collection.s.db);
|
||||
@@ -745,6 +775,7 @@ class BulkOperationBase {
|
||||
// Max batch size options
|
||||
maxBatchSizeBytes: maxBatchSizeBytes,
|
||||
maxWriteBatchSize: maxWriteBatchSize,
|
||||
maxKeySize,
|
||||
// Namespace
|
||||
namespace: namespace,
|
||||
// BSON
|
||||
@@ -898,6 +929,15 @@ class BulkOperationBase {
|
||||
);
|
||||
}
|
||||
|
||||
_handleEarlyError(err, callback) {
|
||||
if (typeof callback === 'function') {
|
||||
callback(err, null);
|
||||
return;
|
||||
}
|
||||
|
||||
return this.s.promiseLibrary.reject(err);
|
||||
}
|
||||
|
||||
/**
|
||||
* Execute next write command in a chain
|
||||
*
|
||||
@@ -911,19 +951,17 @@ class BulkOperationBase {
|
||||
if (typeof options === 'function') (callback = options), (options = {});
|
||||
options = options || {};
|
||||
|
||||
if (this.s.executed) {
|
||||
const executedError = toError('batch cannot be re-executed');
|
||||
return typeof callback === 'function'
|
||||
? callback(executedError, null)
|
||||
: this.s.promiseLibrary.reject(executedError);
|
||||
}
|
||||
|
||||
if (typeof _writeConcern === 'function') {
|
||||
callback = _writeConcern;
|
||||
} else if (_writeConcern && typeof _writeConcern === 'object') {
|
||||
this.s.writeConcern = _writeConcern;
|
||||
}
|
||||
|
||||
if (this.s.executed) {
|
||||
const executedError = toError('batch cannot be re-executed');
|
||||
return this._handleEarlyError(executedError, callback);
|
||||
}
|
||||
|
||||
// If we have current batch
|
||||
if (this.isOrdered) {
|
||||
if (this.s.currentBatch) this.s.batches.push(this.s.currentBatch);
|
||||
@@ -935,13 +973,43 @@ class BulkOperationBase {
|
||||
// If we have no operations in the bulk raise an error
|
||||
if (this.s.batches.length === 0) {
|
||||
const emptyBatchError = toError('Invalid Operation, no operations specified');
|
||||
return typeof callback === 'function'
|
||||
? callback(emptyBatchError, null)
|
||||
: this.s.promiseLibrary.reject(emptyBatchError);
|
||||
return this._handleEarlyError(emptyBatchError, callback);
|
||||
}
|
||||
return { options, callback };
|
||||
}
|
||||
|
||||
/**
|
||||
* The callback format for results
|
||||
* @callback BulkOperationBase~resultCallback
|
||||
* @param {MongoError} error An error instance representing the error during the execution.
|
||||
* @param {BulkWriteResult} result The bulk write result.
|
||||
*/
|
||||
/**
|
||||
* Execute the ordered bulk operation
|
||||
*
|
||||
* @method
|
||||
* @param {object} [options] Optional settings.
|
||||
* @param {(number|string)} [options.w] The write concern.
|
||||
* @param {number} [options.wtimeout] The write concern timeout.
|
||||
* @param {boolean} [options.j=false] Specify a journal write concern.
|
||||
* @param {boolean} [options.fsync=false] Specify a file sync write concern.
|
||||
* @param {BulkOperationBase~resultCallback} [callback] The result callback
|
||||
* @throws {MongoError} Throws error if the bulk object has already been executed
|
||||
* @throws {MongoError} Throws error if the bulk object does not have any operations
|
||||
* @return {Promise} returns Promise if no callback passed
|
||||
*/
|
||||
execute(_writeConcern, options, callback) {
|
||||
const ret = this.bulkExecute(_writeConcern, options, callback);
|
||||
if (!ret || isPromiseLike(ret)) {
|
||||
return ret;
|
||||
}
|
||||
|
||||
options = ret.options;
|
||||
callback = ret.callback;
|
||||
|
||||
return executeOperation(this.s.topology, executeCommands, [this, options, callback]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Handles final options before executing command
|
||||
*
|
||||
@@ -1025,11 +1093,7 @@ class BulkOperationBase {
|
||||
// Force top level error
|
||||
err.ok = 0;
|
||||
// Merge top level error and return
|
||||
handleCallback(
|
||||
callback,
|
||||
null,
|
||||
mergeBatchResults(false, config.batch, this.s.bulkResult, err, null)
|
||||
);
|
||||
handleCallback(callback, null, mergeBatchResults(config.batch, this.s.bulkResult, err, null));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1086,20 +1150,8 @@ Object.defineProperty(BulkOperationBase.prototype, 'length', {
|
||||
module.exports = {
|
||||
Batch,
|
||||
BulkOperationBase,
|
||||
BulkWriteError,
|
||||
BulkWriteResult,
|
||||
bson,
|
||||
FindOperators,
|
||||
handleMongoWriteConcernError,
|
||||
LegacyOp,
|
||||
mergeBatchResults,
|
||||
INVALID_BSON_ERROR: INVALID_BSON_ERROR,
|
||||
MULTIPLE_ERROR: MULTIPLE_ERROR,
|
||||
UNKNOWN_ERROR: UNKNOWN_ERROR,
|
||||
WRITE_CONCERN_ERROR: WRITE_CONCERN_ERROR,
|
||||
INSERT: INSERT,
|
||||
UPDATE: UPDATE,
|
||||
REMOVE: REMOVE,
|
||||
WriteError,
|
||||
WriteConcernError
|
||||
REMOVE: REMOVE
|
||||
};
|
||||
|
||||
+17
-90
@@ -2,16 +2,10 @@
|
||||
|
||||
const common = require('./common');
|
||||
const BulkOperationBase = common.BulkOperationBase;
|
||||
const Batch = common.Batch;
|
||||
const bson = common.bson;
|
||||
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;
|
||||
|
||||
/**
|
||||
* Add to internal list of Operations
|
||||
@@ -24,7 +18,11 @@ const bson = common.bson;
|
||||
function addToOperationsList(bulkOperation, docType, document) {
|
||||
// Get the bsonSize
|
||||
const bsonSize = bson.calculateObjectSize(document, {
|
||||
checkKeys: false
|
||||
checkKeys: false,
|
||||
|
||||
// Since we don't know what the user selected for BSON options here,
|
||||
// err on the safe side, and check the size with ignoreUndefined: false.
|
||||
ignoreUndefined: false
|
||||
});
|
||||
|
||||
// Throw error if the doc is bigger than the max BSON size
|
||||
@@ -35,10 +33,12 @@ function addToOperationsList(bulkOperation, docType, document) {
|
||||
if (bulkOperation.s.currentBatch == null)
|
||||
bulkOperation.s.currentBatch = new Batch(docType, bulkOperation.s.currentIndex);
|
||||
|
||||
const maxKeySize = bulkOperation.s.maxKeySize;
|
||||
|
||||
// Check if we need to create a new batch
|
||||
if (
|
||||
bulkOperation.s.currentBatchSize + 1 >= bulkOperation.s.maxWriteBatchSize ||
|
||||
bulkOperation.s.currentBatchSizeBytes + bulkOperation.s.currentBatchSizeBytes >=
|
||||
bulkOperation.s.currentBatchSizeBytes + maxKeySize + bsonSize >=
|
||||
bulkOperation.s.maxBatchSizeBytes ||
|
||||
bulkOperation.s.currentBatch.batchType !== docType
|
||||
) {
|
||||
@@ -51,10 +51,6 @@ function addToOperationsList(bulkOperation, docType, document) {
|
||||
// Reset the current size trackers
|
||||
bulkOperation.s.currentBatchSize = 0;
|
||||
bulkOperation.s.currentBatchSizeBytes = 0;
|
||||
} else {
|
||||
// Update current batch size
|
||||
bulkOperation.s.currentBatchSize = bulkOperation.s.currentBatchSize + 1;
|
||||
bulkOperation.s.currentBatchSizeBytes = bulkOperation.s.currentBatchSizeBytes + bsonSize;
|
||||
}
|
||||
|
||||
if (docType === common.INSERT) {
|
||||
@@ -67,13 +63,14 @@ function addToOperationsList(bulkOperation, docType, document) {
|
||||
// We have an array of documents
|
||||
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;
|
||||
}
|
||||
|
||||
bulkOperation.s.currentBatch.originalIndexes.push(bulkOperation.s.currentIndex);
|
||||
bulkOperation.s.currentBatch.operations.push(document);
|
||||
bulkOperation.s.currentBatchSize += 1;
|
||||
bulkOperation.s.currentBatchSizeBytes += maxKeySize + bsonSize;
|
||||
bulkOperation.s.currentIndex += 1;
|
||||
|
||||
// Return bulkOperation
|
||||
return bulkOperation;
|
||||
}
|
||||
@@ -81,6 +78,7 @@ function addToOperationsList(bulkOperation, docType, document) {
|
||||
/**
|
||||
* Create a new OrderedBulkOperation instance (INTERNAL TYPE, do not instantiate directly)
|
||||
* @class
|
||||
* @extends BulkOperationBase
|
||||
* @property {number} length Get the number of operations in the bulk.
|
||||
* @return {OrderedBulkOperation} a OrderedBulkOperation instance.
|
||||
*/
|
||||
@@ -92,77 +90,6 @@ class OrderedBulkOperation extends BulkOperationBase {
|
||||
|
||||
super(topology, collection, options, true);
|
||||
}
|
||||
|
||||
/**
|
||||
* The callback format for results
|
||||
* @callback OrderedBulkOperation~resultCallback
|
||||
* @param {MongoError} error An error instance representing the error during the execution.
|
||||
* @param {BulkWriteResult} result The bulk write result.
|
||||
*/
|
||||
|
||||
/**
|
||||
* Execute the ordered bulk operation
|
||||
*
|
||||
* @method
|
||||
* @param {object} [options] Optional settings.
|
||||
* @param {(number|string)} [options.w] The write concern.
|
||||
* @param {number} [options.wtimeout] The write concern timeout.
|
||||
* @param {boolean} [options.j=false] Specify a journal write concern.
|
||||
* @param {boolean} [options.fsync=false] Specify a file sync write concern.
|
||||
* @param {OrderedBulkOperation~resultCallback} [callback] The result callback
|
||||
* @throws {MongoError}
|
||||
* @return {Promise} returns Promise if no callback passed
|
||||
*/
|
||||
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]);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Execute next write command in a chain
|
||||
*
|
||||
* @param {OrderedBulkOperation} bulkOperation
|
||||
* @param {object} options
|
||||
* @param {function} callback
|
||||
*/
|
||||
function executeCommands(bulkOperation, options, callback) {
|
||||
if (bulkOperation.s.batches.length === 0) {
|
||||
return handleCallback(callback, null, new BulkWriteResult(bulkOperation.s.bulkResult));
|
||||
}
|
||||
|
||||
// Ordered execution of the command
|
||||
const batch = bulkOperation.s.batches.shift();
|
||||
|
||||
function resultHandler(err, result) {
|
||||
// Error is a driver related error not a bulk op error, terminate
|
||||
if (((err && err.driver) || (err && err.message)) && !(err instanceof MongoWriteConcernError)) {
|
||||
return handleCallback(callback, err);
|
||||
}
|
||||
|
||||
// If we have and error
|
||||
if (err) err.ok = 0;
|
||||
if (err instanceof MongoWriteConcernError) {
|
||||
return handleMongoWriteConcernError(batch, bulkOperation.s.bulkResult, true, err, callback);
|
||||
}
|
||||
|
||||
// Merge the results together
|
||||
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;
|
||||
|
||||
// Execute the next command in line
|
||||
executeCommands(bulkOperation, options, callback);
|
||||
}
|
||||
|
||||
bulkOperation.finalOptionsHandler({ options, batch, resultHandler }, callback);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
+18
-103
@@ -2,16 +2,10 @@
|
||||
|
||||
const common = require('./common');
|
||||
const BulkOperationBase = common.BulkOperationBase;
|
||||
const Batch = common.Batch;
|
||||
const bson = common.bson;
|
||||
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;
|
||||
|
||||
/**
|
||||
* Add to internal list of Operations
|
||||
@@ -24,7 +18,11 @@ const bson = common.bson;
|
||||
function addToOperationsList(bulkOperation, docType, document) {
|
||||
// Get the bsonSize
|
||||
const bsonSize = bson.calculateObjectSize(document, {
|
||||
checkKeys: false
|
||||
checkKeys: false,
|
||||
|
||||
// Since we don't know what the user selected for BSON options here,
|
||||
// err on the safe side, and check the size with ignoreUndefined: false.
|
||||
ignoreUndefined: false
|
||||
});
|
||||
// Throw error if the doc is bigger than the max BSON size
|
||||
if (bsonSize >= bulkOperation.s.maxBatchSizeBytes)
|
||||
@@ -40,6 +38,8 @@ function addToOperationsList(bulkOperation, docType, document) {
|
||||
bulkOperation.s.currentBatch = bulkOperation.s.currentRemoveBatch;
|
||||
}
|
||||
|
||||
const maxKeySize = bulkOperation.s.maxKeySize;
|
||||
|
||||
// Create a new batch object if we don't have a current one
|
||||
if (bulkOperation.s.currentBatch == null)
|
||||
bulkOperation.s.currentBatch = new Batch(docType, bulkOperation.s.currentIndex);
|
||||
@@ -47,7 +47,8 @@ function addToOperationsList(bulkOperation, docType, document) {
|
||||
// Check if we need to create a new batch
|
||||
if (
|
||||
bulkOperation.s.currentBatch.size + 1 >= bulkOperation.s.maxWriteBatchSize ||
|
||||
bulkOperation.s.currentBatch.sizeBytes + bsonSize >= bulkOperation.s.maxBatchSizeBytes ||
|
||||
bulkOperation.s.currentBatch.sizeBytes + maxKeySize + bsonSize >=
|
||||
bulkOperation.s.maxBatchSizeBytes ||
|
||||
bulkOperation.s.currentBatch.batchType !== docType
|
||||
) {
|
||||
// Save the batch to the execution stack
|
||||
@@ -60,12 +61,12 @@ function addToOperationsList(bulkOperation, docType, document) {
|
||||
// We have an array of documents
|
||||
if (Array.isArray(document)) {
|
||||
throw toError('operation passed in cannot be an Array');
|
||||
} else {
|
||||
bulkOperation.s.currentBatch.operations.push(document);
|
||||
bulkOperation.s.currentBatch.originalIndexes.push(bulkOperation.s.currentIndex);
|
||||
bulkOperation.s.currentIndex = bulkOperation.s.currentIndex + 1;
|
||||
}
|
||||
|
||||
bulkOperation.s.currentBatch.operations.push(document);
|
||||
bulkOperation.s.currentBatch.originalIndexes.push(bulkOperation.s.currentIndex);
|
||||
bulkOperation.s.currentIndex = bulkOperation.s.currentIndex + 1;
|
||||
|
||||
// Save back the current Batch to the right type
|
||||
if (docType === common.INSERT) {
|
||||
bulkOperation.s.currentInsertBatch = bulkOperation.s.currentBatch;
|
||||
@@ -80,8 +81,8 @@ function addToOperationsList(bulkOperation, docType, document) {
|
||||
}
|
||||
|
||||
// Update current batch size
|
||||
bulkOperation.s.currentBatch.size = bulkOperation.s.currentBatch.size + 1;
|
||||
bulkOperation.s.currentBatch.sizeBytes = bulkOperation.s.currentBatch.sizeBytes + bsonSize;
|
||||
bulkOperation.s.currentBatch.size += 1;
|
||||
bulkOperation.s.currentBatch.sizeBytes += maxKeySize + bsonSize;
|
||||
|
||||
// Return bulkOperation
|
||||
return bulkOperation;
|
||||
@@ -90,6 +91,7 @@ function addToOperationsList(bulkOperation, docType, document) {
|
||||
/**
|
||||
* Create a new UnorderedBulkOperation instance (INTERNAL TYPE, do not instantiate directly)
|
||||
* @class
|
||||
* @extends BulkOperationBase
|
||||
* @property {number} length Get the number of operations in the bulk.
|
||||
* @return {UnorderedBulkOperation} a UnorderedBulkOperation instance.
|
||||
*/
|
||||
@@ -100,93 +102,6 @@ class UnorderedBulkOperation extends BulkOperationBase {
|
||||
|
||||
super(topology, collection, options, false);
|
||||
}
|
||||
|
||||
/**
|
||||
* The callback format for results
|
||||
* @callback UnorderedBulkOperation~resultCallback
|
||||
* @param {MongoError} error An error instance representing the error during the execution.
|
||||
* @param {BulkWriteResult} result The bulk write result.
|
||||
*/
|
||||
|
||||
/**
|
||||
* Execute the ordered bulk operation
|
||||
*
|
||||
* @method
|
||||
* @param {object} [options] Optional settings.
|
||||
* @param {(number|string)} [options.w] The write concern.
|
||||
* @param {number} [options.wtimeout] The write concern timeout.
|
||||
* @param {boolean} [options.j=false] Specify a journal write concern.
|
||||
* @param {boolean} [options.fsync=false] Specify a file sync write concern.
|
||||
* @param {UnorderedBulkOperation~resultCallback} [callback] The result callback
|
||||
* @throws {MongoError}
|
||||
* @return {Promise} returns Promise if no callback passed
|
||||
*/
|
||||
execute(_writeConcern, options, callback) {
|
||||
const ret = this.bulkExecute(_writeConcern, options, callback);
|
||||
options = ret.options;
|
||||
callback = ret.callback;
|
||||
|
||||
return executeOperation(this.s.topology, executeBatches, [this, options, callback]);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Execute the command
|
||||
*
|
||||
* @param {UnorderedBulkOperation} bulkOperation
|
||||
* @param {object} batch
|
||||
* @param {object} options
|
||||
* @param {function} callback
|
||||
*/
|
||||
function executeBatch(bulkOperation, batch, options, callback) {
|
||||
function resultHandler(err, result) {
|
||||
// Error is a driver related error not a bulk op error, terminate
|
||||
if (((err && err.driver) || (err && err.message)) && !(err instanceof MongoWriteConcernError)) {
|
||||
return handleCallback(callback, err);
|
||||
}
|
||||
|
||||
// If we have and error
|
||||
if (err) err.ok = 0;
|
||||
if (err instanceof MongoWriteConcernError) {
|
||||
return handleMongoWriteConcernError(batch, bulkOperation.s.bulkResult, false, err, callback);
|
||||
}
|
||||
handleCallback(
|
||||
callback,
|
||||
null,
|
||||
mergeBatchResults(false, batch, bulkOperation.s.bulkResult, err, result)
|
||||
);
|
||||
}
|
||||
|
||||
bulkOperation.finalOptionsHandler({ options, batch, resultHandler }, callback);
|
||||
}
|
||||
|
||||
/**
|
||||
* Execute all the commands
|
||||
*
|
||||
* @param {UnorderedBulkOperation} bulkOperation
|
||||
* @param {object} options
|
||||
* @param {function} callback
|
||||
*/
|
||||
function executeBatches(bulkOperation, options, callback) {
|
||||
let numberOfCommandsToExecute = bulkOperation.s.batches.length;
|
||||
// Execute over all the batches
|
||||
for (let i = 0; i < bulkOperation.s.batches.length; i++) {
|
||||
executeBatch(bulkOperation, bulkOperation.s.batches[i], options, function(err) {
|
||||
// Count down the number of commands left to execute
|
||||
numberOfCommandsToExecute = numberOfCommandsToExecute - 1;
|
||||
|
||||
// Execute
|
||||
if (numberOfCommandsToExecute === 0) {
|
||||
// Driver level error
|
||||
if (err) return handleCallback(callback, err);
|
||||
|
||||
const writeResult = new BulkWriteResult(bulkOperation.s.bulkResult);
|
||||
if (bulkOperation.handleWriteError(callback, writeResult)) return;
|
||||
|
||||
return handleCallback(callback, null, writeResult);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user