PageRenderTime 43ms CodeModel.GetById 13ms app.highlight 24ms RepoModel.GetById 2ms app.codeStats 0ms

/node_modules/mongoose/node_modules/mongodb/lib/bulk/ordered.js

https://bitbucket.org/coleman333/smartsite
JavaScript | 617 lines | 371 code | 75 blank | 171 comment | 84 complexity | 94ed0b9c8fe92554f00453d28d5aaeca MD5 | raw file
  1'use strict';
  2
  3var common = require('./common'),
  4  utils = require('../utils'),
  5  toError = require('../utils').toError,
  6  handleCallback = require('../utils').handleCallback,
  7  shallowClone = utils.shallowClone,
  8  BulkWriteResult = common.BulkWriteResult,
  9  ObjectID = require('mongodb-core').BSON.ObjectID,
 10  Define = require('../metadata'),
 11  BSON = require('mongodb-core').BSON,
 12  Batch = common.Batch,
 13  mergeBatchResults = common.mergeBatchResults,
 14  executeOperation = require('../utils').executeOperation,
 15  BulkWriteError = require('./common').BulkWriteError;
 16
 17var bson = new BSON([
 18  BSON.Binary,
 19  BSON.Code,
 20  BSON.DBRef,
 21  BSON.Decimal128,
 22  BSON.Double,
 23  BSON.Int32,
 24  BSON.Long,
 25  BSON.Map,
 26  BSON.MaxKey,
 27  BSON.MinKey,
 28  BSON.ObjectId,
 29  BSON.BSONRegExp,
 30  BSON.Symbol,
 31  BSON.Timestamp
 32]);
 33
 34/**
 35 * Create a FindOperatorsOrdered instance (INTERNAL TYPE, do not instantiate directly)
 36 * @class
 37 * @return {FindOperatorsOrdered} a FindOperatorsOrdered instance.
 38 */
 39var FindOperatorsOrdered = function(self) {
 40  this.s = self.s;
 41};
 42
 43/**
 44 * Add a single update document to the bulk operation
 45 *
 46 * @method
 47 * @param {object} doc update operations
 48 * @throws {MongoError}
 49 * @return {OrderedBulkOperation}
 50 */
 51FindOperatorsOrdered.prototype.update = function(updateDocument) {
 52  // Perform upsert
 53  var upsert = typeof this.s.currentOp.upsert === 'boolean' ? this.s.currentOp.upsert : false;
 54
 55  // Establish the update command
 56  var document = {
 57    q: this.s.currentOp.selector,
 58    u: updateDocument,
 59    multi: true,
 60    upsert: upsert
 61  };
 62
 63  // Clear out current Op
 64  this.s.currentOp = null;
 65  // Add the update document to the list
 66  return addToOperationsList(this, common.UPDATE, document);
 67};
 68
 69/**
 70 * Add a single update one document to the bulk operation
 71 *
 72 * @method
 73 * @param {object} doc update operations
 74 * @throws {MongoError}
 75 * @return {OrderedBulkOperation}
 76 */
 77FindOperatorsOrdered.prototype.updateOne = function(updateDocument) {
 78  // Perform upsert
 79  var upsert = typeof this.s.currentOp.upsert === 'boolean' ? this.s.currentOp.upsert : false;
 80
 81  // Establish the update command
 82  var document = {
 83    q: this.s.currentOp.selector,
 84    u: updateDocument,
 85    multi: false,
 86    upsert: upsert
 87  };
 88
 89  // Clear out current Op
 90  this.s.currentOp = null;
 91  // Add the update document to the list
 92  return addToOperationsList(this, common.UPDATE, document);
 93};
 94
 95/**
 96 * Add a replace one operation to the bulk operation
 97 *
 98 * @method
 99 * @param {object} doc the new document to replace the existing one with
100 * @throws {MongoError}
101 * @return {OrderedBulkOperation}
102 */
103FindOperatorsOrdered.prototype.replaceOne = function(updateDocument) {
104  this.updateOne(updateDocument);
105};
106
107/**
108 * Upsert modifier for update bulk operation
109 *
110 * @method
111 * @throws {MongoError}
112 * @return {FindOperatorsOrdered}
113 */
114FindOperatorsOrdered.prototype.upsert = function() {
115  this.s.currentOp.upsert = true;
116  return this;
117};
118
119/**
120 * Add a remove one operation to the bulk operation
121 *
122 * @method
123 * @throws {MongoError}
124 * @return {OrderedBulkOperation}
125 */
126FindOperatorsOrdered.prototype.deleteOne = function() {
127  // Establish the update command
128  var document = {
129    q: this.s.currentOp.selector,
130    limit: 1
131  };
132
133  // Clear out current Op
134  this.s.currentOp = null;
135  // Add the remove document to the list
136  return addToOperationsList(this, common.REMOVE, document);
137};
138
139// Backward compatibility
140FindOperatorsOrdered.prototype.removeOne = FindOperatorsOrdered.prototype.deleteOne;
141
142/**
143 * Add a remove operation to the bulk operation
144 *
145 * @method
146 * @throws {MongoError}
147 * @return {OrderedBulkOperation}
148 */
149FindOperatorsOrdered.prototype.delete = function() {
150  // Establish the update command
151  var document = {
152    q: this.s.currentOp.selector,
153    limit: 0
154  };
155
156  // Clear out current Op
157  this.s.currentOp = null;
158  // Add the remove document to the list
159  return addToOperationsList(this, common.REMOVE, document);
160};
161
162// Backward compatibility
163FindOperatorsOrdered.prototype.remove = FindOperatorsOrdered.prototype.delete;
164
165// Add to internal list of documents
166var addToOperationsList = function(_self, docType, document) {
167  // Get the bsonSize
168  var bsonSize = bson.calculateObjectSize(document, {
169    checkKeys: false
170  });
171
172  // Throw error if the doc is bigger than the max BSON size
173  if (bsonSize >= _self.s.maxBatchSizeBytes) {
174    throw toError('document is larger than the maximum size ' + _self.s.maxBatchSizeBytes);
175  }
176
177  // Create a new batch object if we don't have a current one
178  if (_self.s.currentBatch == null) _self.s.currentBatch = new Batch(docType, _self.s.currentIndex);
179
180  // Check if we need to create a new batch
181  if (
182    _self.s.currentBatchSize + 1 >= _self.s.maxWriteBatchSize ||
183    _self.s.currentBatchSizeBytes + _self.s.currentBatchSizeBytes >= _self.s.maxBatchSizeBytes ||
184    _self.s.currentBatch.batchType !== docType
185  ) {
186    // Save the batch to the execution stack
187    _self.s.batches.push(_self.s.currentBatch);
188
189    // Create a new batch
190    _self.s.currentBatch = new Batch(docType, _self.s.currentIndex);
191
192    // Reset the current size trackers
193    _self.s.currentBatchSize = 0;
194    _self.s.currentBatchSizeBytes = 0;
195  } else {
196    // Update current batch size
197    _self.s.currentBatchSize = _self.s.currentBatchSize + 1;
198    _self.s.currentBatchSizeBytes = _self.s.currentBatchSizeBytes + bsonSize;
199  }
200
201  if (docType === common.INSERT) {
202    _self.s.bulkResult.insertedIds.push({ index: _self.s.currentIndex, _id: document._id });
203  }
204
205  // We have an array of documents
206  if (Array.isArray(document)) {
207    throw toError('operation passed in cannot be an Array');
208  } else {
209    _self.s.currentBatch.originalIndexes.push(_self.s.currentIndex);
210    _self.s.currentBatch.operations.push(document);
211    _self.s.currentBatchSizeBytes = _self.s.currentBatchSizeBytes + bsonSize;
212    _self.s.currentIndex = _self.s.currentIndex + 1;
213  }
214
215  // Return self
216  return _self;
217};
218
219/**
220 * Create a new OrderedBulkOperation instance (INTERNAL TYPE, do not instantiate directly)
221 * @class
222 * @property {number} length Get the number of operations in the bulk.
223 * @return {OrderedBulkOperation} a OrderedBulkOperation instance.
224 */
225function OrderedBulkOperation(topology, collection, options) {
226  options = options == null ? {} : options;
227  // TODO Bring from driver information in isMaster
228  var executed = false;
229
230  // Current item
231  var currentOp = null;
232
233  // Handle to the bson serializer, used to calculate running sizes
234  var bson = topology.bson;
235
236  // Namespace for the operation
237  var namespace = collection.collectionName;
238
239  // Set max byte size
240  var maxBatchSizeBytes =
241    topology.isMasterDoc && topology.isMasterDoc.maxBsonObjectSize
242      ? topology.isMasterDoc.maxBsonObjectSize
243      : 1024 * 1025 * 16;
244  var maxWriteBatchSize =
245    topology.isMasterDoc && topology.isMasterDoc.maxWriteBatchSize
246      ? topology.isMasterDoc.maxWriteBatchSize
247      : 1000;
248
249  // Get the write concern
250  var writeConcern = common.writeConcern(shallowClone(options), collection, options);
251
252  // Get the promiseLibrary
253  var promiseLibrary = options.promiseLibrary || Promise;
254
255  // Final results
256  var bulkResult = {
257    ok: 1,
258    writeErrors: [],
259    writeConcernErrors: [],
260    insertedIds: [],
261    nInserted: 0,
262    nUpserted: 0,
263    nMatched: 0,
264    nModified: 0,
265    nRemoved: 0,
266    upserted: []
267  };
268
269  // Internal state
270  this.s = {
271    // Final result
272    bulkResult: bulkResult,
273    // Current batch state
274    currentBatch: null,
275    currentIndex: 0,
276    currentBatchSize: 0,
277    currentBatchSizeBytes: 0,
278    batches: [],
279    // Write concern
280    writeConcern: writeConcern,
281    // Max batch size options
282    maxBatchSizeBytes: maxBatchSizeBytes,
283    maxWriteBatchSize: maxWriteBatchSize,
284    // Namespace
285    namespace: namespace,
286    // BSON
287    bson: bson,
288    // Topology
289    topology: topology,
290    // Options
291    options: options,
292    // Current operation
293    currentOp: currentOp,
294    // Executed
295    executed: executed,
296    // Collection
297    collection: collection,
298    // Promise Library
299    promiseLibrary: promiseLibrary,
300    // Fundamental error
301    err: null,
302    // Bypass validation
303    bypassDocumentValidation:
304      typeof options.bypassDocumentValidation === 'boolean'
305        ? options.bypassDocumentValidation
306        : false,
307    // check keys
308    checkKeys: typeof options.checkKeys === 'boolean' ? options.checkKeys : true
309  };
310}
311
312var define = (OrderedBulkOperation.define = new Define(
313  'OrderedBulkOperation',
314  OrderedBulkOperation,
315  false
316));
317
318OrderedBulkOperation.prototype.raw = function(op) {
319  var key = Object.keys(op)[0];
320
321  // Set up the force server object id
322  var forceServerObjectId =
323    typeof this.s.options.forceServerObjectId === 'boolean'
324      ? this.s.options.forceServerObjectId
325      : this.s.collection.s.db.options.forceServerObjectId;
326
327  // Update operations
328  if (
329    (op.updateOne && op.updateOne.q) ||
330    (op.updateMany && op.updateMany.q) ||
331    (op.replaceOne && op.replaceOne.q)
332  ) {
333    op[key].multi = op.updateOne || op.replaceOne ? false : true;
334    return addToOperationsList(this, common.UPDATE, op[key]);
335  }
336
337  // Crud spec update format
338  if (op.updateOne || op.updateMany || op.replaceOne) {
339    var multi = op.updateOne || op.replaceOne ? false : true;
340    var operation = { q: op[key].filter, u: op[key].update || op[key].replacement, multi: multi };
341    operation.upsert = op[key].upsert ? true : false;
342    if (op.collation) operation.collation = op.collation;
343    if (op[key].arrayFilters) operation.arrayFilters = op[key].arrayFilters;
344    return addToOperationsList(this, common.UPDATE, operation);
345  }
346
347  // Remove operations
348  if (
349    op.removeOne ||
350    op.removeMany ||
351    (op.deleteOne && op.deleteOne.q) ||
352    (op.deleteMany && op.deleteMany.q)
353  ) {
354    op[key].limit = op.removeOne ? 1 : 0;
355    return addToOperationsList(this, common.REMOVE, op[key]);
356  }
357
358  // Crud spec delete operations, less efficient
359  if (op.deleteOne || op.deleteMany) {
360    var limit = op.deleteOne ? 1 : 0;
361    operation = { q: op[key].filter, limit: limit };
362    if (op.collation) operation.collation = op.collation;
363    return addToOperationsList(this, common.REMOVE, operation);
364  }
365
366  // Insert operations
367  if (op.insertOne && op.insertOne.document == null) {
368    if (forceServerObjectId !== true && op.insertOne._id == null) op.insertOne._id = new ObjectID();
369    return addToOperationsList(this, common.INSERT, op.insertOne);
370  } else if (op.insertOne && op.insertOne.document) {
371    if (forceServerObjectId !== true && op.insertOne.document._id == null)
372      op.insertOne.document._id = new ObjectID();
373    return addToOperationsList(this, common.INSERT, op.insertOne.document);
374  }
375
376  if (op.insertMany) {
377    for (var i = 0; i < op.insertMany.length; i++) {
378      if (forceServerObjectId !== true && op.insertMany[i]._id == null)
379        op.insertMany[i]._id = new ObjectID();
380      addToOperationsList(this, common.INSERT, op.insertMany[i]);
381    }
382
383    return;
384  }
385
386  // No valid type of operation
387  throw toError(
388    'bulkWrite only supports insertOne, insertMany, updateOne, updateMany, removeOne, removeMany, deleteOne, deleteMany'
389  );
390};
391
392/**
393 * Add a single insert document to the bulk operation
394 *
395 * @param {object} doc the document to insert
396 * @throws {MongoError}
397 * @return {OrderedBulkOperation}
398 */
399OrderedBulkOperation.prototype.insert = function(document) {
400  if (this.s.collection.s.db.options.forceServerObjectId !== true && document._id == null)
401    document._id = new ObjectID();
402  return addToOperationsList(this, common.INSERT, document);
403};
404
405/**
406 * Initiate a find operation for an update/updateOne/remove/removeOne/replaceOne
407 *
408 * @method
409 * @param {object} selector The selector for the bulk operation.
410 * @throws {MongoError}
411 * @return {FindOperatorsOrdered}
412 */
413OrderedBulkOperation.prototype.find = function(selector) {
414  if (!selector) {
415    throw toError('Bulk find operation must specify a selector');
416  }
417
418  // Save a current selector
419  this.s.currentOp = {
420    selector: selector
421  };
422
423  return new FindOperatorsOrdered(this);
424};
425
426Object.defineProperty(OrderedBulkOperation.prototype, 'length', {
427  enumerable: true,
428  get: function() {
429    return this.s.currentIndex;
430  }
431});
432
433//
434// Execute next write command in a chain
435var executeCommands = function(self, options, callback) {
436  if (self.s.batches.length === 0) {
437    return handleCallback(callback, null, new BulkWriteResult(self.s.bulkResult));
438  }
439
440  // Ordered execution of the command
441  var batch = self.s.batches.shift();
442
443  var resultHandler = function(err, result) {
444    // Error is a driver related error not a bulk op error, terminate
445    if ((err && err.driver) || (err && err.message)) {
446      return handleCallback(callback, err);
447    }
448
449    // If we have and error
450    if (err) err.ok = 0;
451    // Merge the results together
452    var mergeResult = mergeBatchResults(true, batch, self.s.bulkResult, err, result);
453    const writeResult = new BulkWriteResult(self.s.bulkResult);
454    if (mergeResult != null) {
455      return handleCallback(callback, null, writeResult);
456    }
457
458    // If we are ordered and have errors and they are
459    // not all replication errors terminate the operation
460    if (self.s.bulkResult.writeErrors.length > 0) {
461      if (self.s.bulkResult.writeErrors.length === 1) {
462        return handleCallback(
463          callback,
464          new BulkWriteError(toError(self.s.bulkResult.writeErrors[0]), writeResult),
465          null
466        );
467      }
468
469      return handleCallback(
470        callback,
471        new BulkWriteError(
472          toError({
473            message: 'write operation failed',
474            code: self.s.bulkResult.writeErrors[0].code,
475            writeErrors: self.s.bulkResult.writeErrors
476          }),
477          writeResult
478        ),
479        null
480      );
481    } else if (writeResult.getWriteConcernError()) {
482      return handleCallback(
483        callback,
484        new BulkWriteError(toError(writeResult.getWriteConcernError()), writeResult),
485        null
486      );
487    }
488
489    // Execute the next command in line
490    executeCommands(self, options, callback);
491  };
492
493  var finalOptions = Object.assign({ ordered: true }, options);
494  if (self.s.writeConcern != null) {
495    finalOptions.writeConcern = self.s.writeConcern;
496  }
497
498  // Set an operationIf if provided
499  if (self.operationId) {
500    resultHandler.operationId = self.operationId;
501  }
502
503  // Serialize functions
504  if (self.s.options.serializeFunctions) {
505    finalOptions.serializeFunctions = true;
506  }
507
508  // Ignore undefined
509  if (self.s.options.ignoreUndefined) {
510    finalOptions.ignoreUndefined = true;
511  }
512
513  // Is the bypassDocumentValidation options specific
514  if (self.s.bypassDocumentValidation === true) {
515    finalOptions.bypassDocumentValidation = true;
516  }
517
518  // Is the checkKeys option disabled
519  if (self.s.checkKeys === false) {
520    finalOptions.checkKeys = false;
521  }
522
523  try {
524    if (batch.batchType === common.INSERT) {
525      self.s.topology.insert(
526        self.s.collection.namespace,
527        batch.operations,
528        finalOptions,
529        resultHandler
530      );
531    } else if (batch.batchType === common.UPDATE) {
532      self.s.topology.update(
533        self.s.collection.namespace,
534        batch.operations,
535        finalOptions,
536        resultHandler
537      );
538    } else if (batch.batchType === common.REMOVE) {
539      self.s.topology.remove(
540        self.s.collection.namespace,
541        batch.operations,
542        finalOptions,
543        resultHandler
544      );
545    }
546  } catch (err) {
547    // Force top level error
548    err.ok = 0;
549    // Merge top level error and return
550    handleCallback(callback, null, mergeBatchResults(false, batch, self.s.bulkResult, err, null));
551  }
552};
553
554/**
555 * The callback format for results
556 * @callback OrderedBulkOperation~resultCallback
557 * @param {MongoError} error An error instance representing the error during the execution.
558 * @param {BulkWriteResult} result The bulk write result.
559 */
560
561/**
562 * Execute the ordered bulk operation
563 *
564 * @method
565 * @param {object} [options=null] Optional settings.
566 * @param {(number|string)} [options.w=null] The write concern.
567 * @param {number} [options.wtimeout=null] The write concern timeout.
568 * @param {boolean} [options.j=false] Specify a journal write concern.
569 * @param {boolean} [options.fsync=false] Specify a file sync write concern.
570 * @param {OrderedBulkOperation~resultCallback} [callback] The result callback
571 * @throws {MongoError}
572 * @return {Promise} returns Promise if no callback passed
573 */
574OrderedBulkOperation.prototype.execute = function(_writeConcern, options, callback) {
575  if (typeof options === 'function') (callback = options), (options = {});
576  options = options || {};
577
578  if (this.s.executed) {
579    var executedError = toError('batch cannot be re-executed');
580    return typeof callback === 'function'
581      ? callback(executedError, null)
582      : this.s.promiseLibrary.reject(executedError);
583  }
584
585  if (typeof _writeConcern === 'function') {
586    callback = _writeConcern;
587  } else if (_writeConcern && typeof _writeConcern === 'object') {
588    this.s.writeConcern = _writeConcern;
589  }
590
591  // If we have current batch
592  if (this.s.currentBatch) this.s.batches.push(this.s.currentBatch);
593
594  // If we have no operations in the bulk raise an error
595  if (this.s.batches.length === 0) {
596    var emptyBatchError = toError('Invalid Operation, no operations specified');
597    return typeof callback === 'function'
598      ? callback(emptyBatchError, null)
599      : this.s.promiseLibrary.reject(emptyBatchError);
600  }
601
602  return executeOperation(this.s.topology, executeCommands, [this, options, callback]);
603};
604
605define.classMethod('execute', { callback: true, promise: false });
606
607/**
608 * Returns an unordered batch object
609 * @ignore
610 */
611var initializeOrderedBulkOp = function(topology, collection, options) {
612  return new OrderedBulkOperation(topology, collection, options);
613};
614
615initializeOrderedBulkOp.OrderedBulkOperation = OrderedBulkOperation;
616module.exports = initializeOrderedBulkOp;
617module.exports.Bulk = OrderedBulkOperation;