Ohm-Management - Projektarbeit B-ME
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ordered.js 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. 'use strict';
  2. const common = require('./common');
  3. const BulkOperationBase = common.BulkOperationBase;
  4. const Batch = common.Batch;
  5. const bson = common.bson;
  6. const utils = require('../utils');
  7. const toError = utils.toError;
  8. /**
  9. * Add to internal list of Operations
  10. *
  11. * @param {OrderedBulkOperation} bulkOperation
  12. * @param {number} docType number indicating the document type
  13. * @param {object} document
  14. * @return {OrderedBulkOperation}
  15. */
  16. function addToOperationsList(bulkOperation, docType, document) {
  17. // Get the bsonSize
  18. const bsonSize = bson.calculateObjectSize(document, {
  19. checkKeys: false,
  20. // Since we don't know what the user selected for BSON options here,
  21. // err on the safe side, and check the size with ignoreUndefined: false.
  22. ignoreUndefined: false
  23. });
  24. // Throw error if the doc is bigger than the max BSON size
  25. if (bsonSize >= bulkOperation.s.maxBatchSizeBytes)
  26. throw toError('document is larger than the maximum size ' + bulkOperation.s.maxBatchSizeBytes);
  27. // Create a new batch object if we don't have a current one
  28. if (bulkOperation.s.currentBatch == null)
  29. bulkOperation.s.currentBatch = new Batch(docType, bulkOperation.s.currentIndex);
  30. const maxKeySize = bulkOperation.s.maxKeySize;
  31. // Check if we need to create a new batch
  32. if (
  33. bulkOperation.s.currentBatchSize + 1 >= bulkOperation.s.maxWriteBatchSize ||
  34. bulkOperation.s.currentBatchSizeBytes + maxKeySize + bsonSize >=
  35. bulkOperation.s.maxBatchSizeBytes ||
  36. bulkOperation.s.currentBatch.batchType !== docType
  37. ) {
  38. // Save the batch to the execution stack
  39. bulkOperation.s.batches.push(bulkOperation.s.currentBatch);
  40. // Create a new batch
  41. bulkOperation.s.currentBatch = new Batch(docType, bulkOperation.s.currentIndex);
  42. // Reset the current size trackers
  43. bulkOperation.s.currentBatchSize = 0;
  44. bulkOperation.s.currentBatchSizeBytes = 0;
  45. }
  46. if (docType === common.INSERT) {
  47. bulkOperation.s.bulkResult.insertedIds.push({
  48. index: bulkOperation.s.currentIndex,
  49. _id: document._id
  50. });
  51. }
  52. // We have an array of documents
  53. if (Array.isArray(document)) {
  54. throw toError('operation passed in cannot be an Array');
  55. }
  56. bulkOperation.s.currentBatch.originalIndexes.push(bulkOperation.s.currentIndex);
  57. bulkOperation.s.currentBatch.operations.push(document);
  58. bulkOperation.s.currentBatchSize += 1;
  59. bulkOperation.s.currentBatchSizeBytes += maxKeySize + bsonSize;
  60. bulkOperation.s.currentIndex += 1;
  61. // Return bulkOperation
  62. return bulkOperation;
  63. }
  64. /**
  65. * Create a new OrderedBulkOperation instance (INTERNAL TYPE, do not instantiate directly)
  66. * @class
  67. * @extends BulkOperationBase
  68. * @property {number} length Get the number of operations in the bulk.
  69. * @return {OrderedBulkOperation} a OrderedBulkOperation instance.
  70. */
  71. class OrderedBulkOperation extends BulkOperationBase {
  72. constructor(topology, collection, options) {
  73. options = options || {};
  74. options = Object.assign(options, { addToOperationsList });
  75. super(topology, collection, options, true);
  76. }
  77. }
  78. /**
  79. * Returns an unordered batch object
  80. * @ignore
  81. */
  82. function initializeOrderedBulkOp(topology, collection, options) {
  83. return new OrderedBulkOperation(topology, collection, options);
  84. }
  85. initializeOrderedBulkOp.OrderedBulkOperation = OrderedBulkOperation;
  86. module.exports = initializeOrderedBulkOp;
  87. module.exports.Bulk = OrderedBulkOperation;