|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105 |
- 'use strict';
-
- const common = require('./common');
- const BulkOperationBase = common.BulkOperationBase;
- const Batch = common.Batch;
- const bson = common.bson;
- const utils = require('../utils');
- const toError = utils.toError;
-
-
- function addToOperationsList(bulkOperation, docType, document) {
-
- const bsonSize = bson.calculateObjectSize(document, {
- checkKeys: false,
-
-
-
- ignoreUndefined: 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);
-
- const maxKeySize = bulkOperation.s.maxKeySize;
-
-
- if (
- bulkOperation.s.currentBatchSize + 1 >= bulkOperation.s.maxWriteBatchSize ||
- bulkOperation.s.currentBatchSizeBytes + maxKeySize + bsonSize >=
- 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;
- }
-
- 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');
- }
-
- 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;
- }
-
-
-
- class OrderedBulkOperation extends BulkOperationBase {
- constructor(topology, collection, options) {
- options = options || {};
- options = Object.assign(options, { addToOperationsList });
-
- super(topology, collection, options, true);
- }
- }
-
-
- function initializeOrderedBulkOp(topology, collection, options) {
- return new OrderedBulkOperation(topology, collection, options);
- }
-
- initializeOrderedBulkOp.OrderedBulkOperation = OrderedBulkOperation;
- module.exports = initializeOrderedBulkOp;
- module.exports.Bulk = OrderedBulkOperation;
|