1 | 'use strict';
|
2 |
|
3 | const common = require('./common');
|
4 | const BulkOperationBase = common.BulkOperationBase;
|
5 | const Batch = common.Batch;
|
6 | const bson = common.bson;
|
7 | const utils = require('../utils');
|
8 | const toError = utils.toError;
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 | function addToOperationsList(bulkOperation, docType, document) {
|
20 |
|
21 | const bsonSize = bson.calculateObjectSize(document, {
|
22 | checkKeys: false,
|
23 |
|
24 |
|
25 |
|
26 | ignoreUndefined: false
|
27 | });
|
28 |
|
29 |
|
30 | if (bsonSize >= bulkOperation.s.maxBsonObjectSize)
|
31 | throw toError('document is larger than the maximum size ' + bulkOperation.s.maxBsonObjectSize);
|
32 |
|
33 |
|
34 | if (bulkOperation.s.currentBatch == null)
|
35 | bulkOperation.s.currentBatch = new Batch(docType, bulkOperation.s.currentIndex);
|
36 |
|
37 | const maxKeySize = bulkOperation.s.maxKeySize;
|
38 |
|
39 |
|
40 | if (
|
41 |
|
42 | bulkOperation.s.currentBatchSize + 1 >= bulkOperation.s.maxWriteBatchSize ||
|
43 |
|
44 |
|
45 | (bulkOperation.s.currentBatchSize > 0 &&
|
46 | bulkOperation.s.currentBatchSizeBytes + maxKeySize + bsonSize >=
|
47 | bulkOperation.s.maxBatchSizeBytes) ||
|
48 |
|
49 | bulkOperation.s.currentBatch.batchType !== docType
|
50 | ) {
|
51 |
|
52 | bulkOperation.s.batches.push(bulkOperation.s.currentBatch);
|
53 |
|
54 |
|
55 | bulkOperation.s.currentBatch = new Batch(docType, bulkOperation.s.currentIndex);
|
56 |
|
57 |
|
58 | bulkOperation.s.currentBatchSize = 0;
|
59 | bulkOperation.s.currentBatchSizeBytes = 0;
|
60 | }
|
61 |
|
62 | if (docType === common.INSERT) {
|
63 | bulkOperation.s.bulkResult.insertedIds.push({
|
64 | index: bulkOperation.s.currentIndex,
|
65 | _id: document._id
|
66 | });
|
67 | }
|
68 |
|
69 |
|
70 | if (Array.isArray(document)) {
|
71 | throw toError('operation passed in cannot be an Array');
|
72 | }
|
73 |
|
74 | bulkOperation.s.currentBatch.originalIndexes.push(bulkOperation.s.currentIndex);
|
75 | bulkOperation.s.currentBatch.operations.push(document);
|
76 | bulkOperation.s.currentBatchSize += 1;
|
77 | bulkOperation.s.currentBatchSizeBytes += maxKeySize + bsonSize;
|
78 | bulkOperation.s.currentIndex += 1;
|
79 |
|
80 |
|
81 | return bulkOperation;
|
82 | }
|
83 |
|
84 |
|
85 |
|
86 |
|
87 |
|
88 |
|
89 |
|
90 |
|
91 | class OrderedBulkOperation extends BulkOperationBase {
|
92 | constructor(topology, collection, options) {
|
93 | options = options || {};
|
94 | options = Object.assign(options, { addToOperationsList });
|
95 |
|
96 | super(topology, collection, options, true);
|
97 | }
|
98 | }
|
99 |
|
100 |
|
101 |
|
102 |
|
103 |
|
104 | function initializeOrderedBulkOp(topology, collection, options) {
|
105 | return new OrderedBulkOperation(topology, collection, options);
|
106 | }
|
107 |
|
108 | initializeOrderedBulkOp.OrderedBulkOperation = OrderedBulkOperation;
|
109 | module.exports = initializeOrderedBulkOp;
|
110 | module.exports.Bulk = OrderedBulkOperation;
|