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.

unordered.js 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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 {UnorderedBulkOperation} bulkOperation
  12. * @param {number} docType number indicating the document type
  13. * @param {object} document
  14. * @return {UnorderedBulkOperation}
  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. // Holds the current batch
  28. bulkOperation.s.currentBatch = null;
  29. // Get the right type of batch
  30. if (docType === common.INSERT) {
  31. bulkOperation.s.currentBatch = bulkOperation.s.currentInsertBatch;
  32. } else if (docType === common.UPDATE) {
  33. bulkOperation.s.currentBatch = bulkOperation.s.currentUpdateBatch;
  34. } else if (docType === common.REMOVE) {
  35. bulkOperation.s.currentBatch = bulkOperation.s.currentRemoveBatch;
  36. }
  37. const maxKeySize = bulkOperation.s.maxKeySize;
  38. // Create a new batch object if we don't have a current one
  39. if (bulkOperation.s.currentBatch == null)
  40. bulkOperation.s.currentBatch = new Batch(docType, bulkOperation.s.currentIndex);
  41. // Check if we need to create a new batch
  42. if (
  43. bulkOperation.s.currentBatch.size + 1 >= bulkOperation.s.maxWriteBatchSize ||
  44. bulkOperation.s.currentBatch.sizeBytes + maxKeySize + bsonSize >=
  45. bulkOperation.s.maxBatchSizeBytes ||
  46. bulkOperation.s.currentBatch.batchType !== docType
  47. ) {
  48. // Save the batch to the execution stack
  49. bulkOperation.s.batches.push(bulkOperation.s.currentBatch);
  50. // Create a new batch
  51. bulkOperation.s.currentBatch = new Batch(docType, bulkOperation.s.currentIndex);
  52. }
  53. // We have an array of documents
  54. if (Array.isArray(document)) {
  55. throw toError('operation passed in cannot be an Array');
  56. }
  57. bulkOperation.s.currentBatch.operations.push(document);
  58. bulkOperation.s.currentBatch.originalIndexes.push(bulkOperation.s.currentIndex);
  59. bulkOperation.s.currentIndex = bulkOperation.s.currentIndex + 1;
  60. // Save back the current Batch to the right type
  61. if (docType === common.INSERT) {
  62. bulkOperation.s.currentInsertBatch = bulkOperation.s.currentBatch;
  63. bulkOperation.s.bulkResult.insertedIds.push({
  64. index: bulkOperation.s.bulkResult.insertedIds.length,
  65. _id: document._id
  66. });
  67. } else if (docType === common.UPDATE) {
  68. bulkOperation.s.currentUpdateBatch = bulkOperation.s.currentBatch;
  69. } else if (docType === common.REMOVE) {
  70. bulkOperation.s.currentRemoveBatch = bulkOperation.s.currentBatch;
  71. }
  72. // Update current batch size
  73. bulkOperation.s.currentBatch.size += 1;
  74. bulkOperation.s.currentBatch.sizeBytes += maxKeySize + bsonSize;
  75. // Return bulkOperation
  76. return bulkOperation;
  77. }
  78. /**
  79. * Create a new UnorderedBulkOperation instance (INTERNAL TYPE, do not instantiate directly)
  80. * @class
  81. * @extends BulkOperationBase
  82. * @property {number} length Get the number of operations in the bulk.
  83. * @return {UnorderedBulkOperation} a UnorderedBulkOperation instance.
  84. */
  85. class UnorderedBulkOperation extends BulkOperationBase {
  86. constructor(topology, collection, options) {
  87. options = options || {};
  88. options = Object.assign(options, { addToOperationsList });
  89. super(topology, collection, options, false);
  90. }
  91. }
  92. /**
  93. * Returns an unordered batch object
  94. * @ignore
  95. */
  96. function initializeUnorderedBulkOp(topology, collection, options) {
  97. return new UnorderedBulkOperation(topology, collection, options);
  98. }
  99. initializeUnorderedBulkOp.UnorderedBulkOperation = UnorderedBulkOperation;
  100. module.exports = initializeUnorderedBulkOp;
  101. module.exports.Bulk = UnorderedBulkOperation;