123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534 |
- "use strict";
- var common = require('./common')
- , utils = require('../utils')
- , toError = require('../utils').toError
- , handleCallback = require('../utils').handleCallback
- , shallowClone = utils.shallowClone
- , BulkWriteResult = common.BulkWriteResult
- , ObjectID = require('mongodb-core').BSON.ObjectID
- , BSON = require('mongodb-core').BSON
- , Define = require('../metadata')
- , Batch = common.Batch
- , mergeBatchResults = common.mergeBatchResults;
- var bson = new BSON([BSON.Binary, BSON.Code, BSON.DBRef, BSON.Decimal128,
- BSON.Double, BSON.Int32, BSON.Long, BSON.Map, BSON.MaxKey, BSON.MinKey,
- BSON.ObjectId, BSON.BSONRegExp, BSON.Symbol, BSON.Timestamp]);
- var FindOperatorsUnordered = function(self) {
- this.s = self.s;
- }
- FindOperatorsUnordered.prototype.update = function(updateDocument) {
-
- var upsert = typeof this.s.currentOp.upsert == 'boolean' ? this.s.currentOp.upsert : false;
-
- var document = {
- q: this.s.currentOp.selector
- , u: updateDocument
- , multi: true
- , upsert: upsert
- }
-
- this.s.currentOp = null;
-
- return addToOperationsList(this, common.UPDATE, document);
- }
- FindOperatorsUnordered.prototype.updateOne = function(updateDocument) {
-
- var upsert = typeof this.s.currentOp.upsert == 'boolean' ? this.s.currentOp.upsert : false;
-
- var document = {
- q: this.s.currentOp.selector
- , u: updateDocument
- , multi: false
- , upsert: upsert
- }
-
- this.s.currentOp = null;
-
- return addToOperationsList(this, common.UPDATE, document);
- }
- FindOperatorsUnordered.prototype.replaceOne = function(updateDocument) {
- this.updateOne(updateDocument);
- }
- FindOperatorsUnordered.prototype.upsert = function() {
- this.s.currentOp.upsert = true;
- return this;
- }
- FindOperatorsUnordered.prototype.removeOne = function() {
-
- var document = {
- q: this.s.currentOp.selector
- , limit: 1
- }
-
- this.s.currentOp = null;
-
- return addToOperationsList(this, common.REMOVE, document);
- }
- FindOperatorsUnordered.prototype.remove = function() {
-
- var document = {
- q: this.s.currentOp.selector
- , limit: 0
- }
-
- this.s.currentOp = null;
-
- return addToOperationsList(this, common.REMOVE, document);
- }
- var addToOperationsList = function(_self, docType, document) {
-
- var bsonSize = bson.calculateObjectSize(document, {
- checkKeys: false,
- });
-
- if(bsonSize >= _self.s.maxBatchSizeBytes) throw toError("document is larger than the maximum size " + _self.s.maxBatchSizeBytes);
-
- _self.s.currentBatch = null;
-
- if(docType == common.INSERT) {
- _self.s.currentBatch = _self.s.currentInsertBatch;
- } else if(docType == common.UPDATE) {
- _self.s.currentBatch = _self.s.currentUpdateBatch;
- } else if(docType == common.REMOVE) {
- _self.s.currentBatch = _self.s.currentRemoveBatch;
- }
-
- if(_self.s.currentBatch == null) _self.s.currentBatch = new Batch(docType, _self.s.currentIndex);
-
- if(((_self.s.currentBatch.size + 1) >= _self.s.maxWriteBatchSize)
- || ((_self.s.currentBatch.sizeBytes + bsonSize) >= _self.s.maxBatchSizeBytes)
- || (_self.s.currentBatch.batchType != docType)) {
-
- _self.s.batches.push(_self.s.currentBatch);
-
- _self.s.currentBatch = new Batch(docType, _self.s.currentIndex);
- }
-
- if(Array.isArray(document)) {
- throw toError("operation passed in cannot be an Array");
- } else {
- _self.s.currentBatch.operations.push(document);
- _self.s.currentBatch.originalIndexes.push(_self.s.currentIndex);
- _self.s.currentIndex = _self.s.currentIndex + 1;
- }
-
- if(docType == common.INSERT) {
- _self.s.currentInsertBatch = _self.s.currentBatch;
- _self.s.bulkResult.insertedIds.push({index: _self.s.currentIndex, _id: document._id});
- } else if(docType == common.UPDATE) {
- _self.s.currentUpdateBatch = _self.s.currentBatch;
- } else if(docType == common.REMOVE) {
- _self.s.currentRemoveBatch = _self.s.currentBatch;
- }
-
- _self.s.currentBatch.size = _self.s.currentBatch.size + 1;
- _self.s.currentBatch.sizeBytes = _self.s.currentBatch.sizeBytes + bsonSize;
-
- return _self;
- }
- var UnorderedBulkOperation = function(topology, collection, options) {
- options = options == null ? {} : options;
-
- var namespace = collection.collectionName;
-
- var executed = false;
-
-
- var currentOp = null;
-
- var bson = topology.bson;
-
- var maxBatchSizeBytes = topology.isMasterDoc && topology.isMasterDoc.maxBsonObjectSize
- ? topology.isMasterDoc.maxBsonObjectSize : (1024*1025*16);
- var maxWriteBatchSize = topology.isMasterDoc && topology.isMasterDoc.maxWriteBatchSize
- ? topology.isMasterDoc.maxWriteBatchSize : 1000;
-
- var writeConcern = common.writeConcern(shallowClone(options), collection, options);
-
- var promiseLibrary = options.promiseLibrary;
-
- if(!promiseLibrary) {
- promiseLibrary = typeof global.Promise == 'function' ?
- global.Promise : require('es6-promise').Promise;
- }
-
- var bulkResult = {
- ok: 1
- , writeErrors: []
- , writeConcernErrors: []
- , insertedIds: []
- , nInserted: 0
- , nUpserted: 0
- , nMatched: 0
- , nModified: 0
- , nRemoved: 0
- , upserted: []
- };
-
- this.s = {
-
- bulkResult: bulkResult
-
- , currentInsertBatch: null
- , currentUpdateBatch: null
- , currentRemoveBatch: null
- , currentBatch: null
- , currentIndex: 0
- , batches: []
-
- , writeConcern: writeConcern
-
- , maxBatchSizeBytes: maxBatchSizeBytes
- , maxWriteBatchSize: maxWriteBatchSize
-
- , namespace: namespace
-
- , bson: bson
-
- , topology: topology
-
- , options: options
-
- , currentOp: currentOp
-
- , executed: executed
-
- , collection: collection
-
- , promiseLibrary: promiseLibrary
-
- , bypassDocumentValidation: typeof options.bypassDocumentValidation == 'boolean' ? options.bypassDocumentValidation : false
- }
- }
- var define = UnorderedBulkOperation.define = new Define('UnorderedBulkOperation', UnorderedBulkOperation, false);
- UnorderedBulkOperation.prototype.insert = function(document) {
- if(this.s.collection.s.db.options.forceServerObjectId !== true && document._id == null) document._id = new ObjectID();
- return addToOperationsList(this, common.INSERT, document);
- }
- UnorderedBulkOperation.prototype.find = function(selector) {
- if (!selector) {
- throw toError("Bulk find operation must specify a selector");
- }
-
- this.s.currentOp = {
- selector: selector
- }
- return new FindOperatorsUnordered(this);
- }
- Object.defineProperty(UnorderedBulkOperation.prototype, 'length', {
- enumerable: true,
- get: function() {
- return this.s.currentIndex;
- }
- });
- UnorderedBulkOperation.prototype.raw = function(op) {
- var key = Object.keys(op)[0];
-
- var forceServerObjectId = typeof this.s.options.forceServerObjectId == 'boolean'
- ? this.s.options.forceServerObjectId : this.s.collection.s.db.options.forceServerObjectId;
-
- if((op.updateOne && op.updateOne.q)
- || (op.updateMany && op.updateMany.q)
- || (op.replaceOne && op.replaceOne.q)) {
- op[key].multi = op.updateOne || op.replaceOne ? false : true;
- return addToOperationsList(this, common.UPDATE, op[key]);
- }
-
- if(op.updateOne || op.updateMany || op.replaceOne) {
- var multi = op.updateOne || op.replaceOne ? false : true;
- var operation = {q: op[key].filter, u: op[key].update || op[key].replacement, multi: multi}
- if(op[key].upsert) operation.upsert = true;
- return addToOperationsList(this, common.UPDATE, operation);
- }
-
- if(op.removeOne || op.removeMany || (op.deleteOne && op.deleteOne.q) || op.deleteMany && op.deleteMany.q) {
- op[key].limit = op.removeOne ? 1 : 0;
- return addToOperationsList(this, common.REMOVE, op[key]);
- }
-
- if(op.deleteOne || op.deleteMany) {
- var limit = op.deleteOne ? 1 : 0;
- operation = {q: op[key].filter, limit: limit}
- return addToOperationsList(this, common.REMOVE, operation);
- }
-
- if(op.insertOne && op.insertOne.document == null) {
- if(forceServerObjectId !== true && op.insertOne._id == null) op.insertOne._id = new ObjectID();
- return addToOperationsList(this, common.INSERT, op.insertOne);
- } else if(op.insertOne && op.insertOne.document) {
- if(forceServerObjectId !== true && op.insertOne.document._id == null) op.insertOne.document._id = new ObjectID();
- return addToOperationsList(this, common.INSERT, op.insertOne.document);
- }
- if(op.insertMany) {
- for(var i = 0; i < op.insertMany.length; i++) {
- if(forceServerObjectId !== true && op.insertMany[i]._id == null) op.insertMany[i]._id = new ObjectID();
- addToOperationsList(this, common.INSERT, op.insertMany[i]);
- }
- return;
- }
-
- throw toError("bulkWrite only supports insertOne, insertMany, updateOne, updateMany, removeOne, removeMany, deleteOne, deleteMany");
- }
- var executeBatch = function(self, batch, callback) {
- var finalOptions = {ordered: false}
- if(self.s.writeConcern != null) {
- finalOptions.writeConcern = self.s.writeConcern;
- }
- var resultHandler = function(err, result) {
-
- if(err && err.driver || err && err.message) {
- return handleCallback(callback, err);
- }
-
- if(err) err.ok = 0;
- handleCallback(callback, null, mergeBatchResults(false, batch, self.s.bulkResult, err, result));
- }
-
- if(self.operationId) {
- resultHandler.operationId = self.operationId;
- }
-
- if(self.s.options.serializeFunctions) {
- finalOptions.serializeFunctions = true
- }
-
- if(self.s.bypassDocumentValidation == true) {
- finalOptions.bypassDocumentValidation = true;
- }
- try {
- if(batch.batchType == common.INSERT) {
- self.s.topology.insert(self.s.collection.namespace, batch.operations, finalOptions, resultHandler);
- } else if(batch.batchType == common.UPDATE) {
- self.s.topology.update(self.s.collection.namespace, batch.operations, finalOptions, resultHandler);
- } else if(batch.batchType == common.REMOVE) {
- self.s.topology.remove(self.s.collection.namespace, batch.operations, finalOptions, resultHandler);
- }
- } catch(err) {
-
- err.ok = 0;
-
- handleCallback(callback, null, mergeBatchResults(false, batch, self.s.bulkResult, err, null));
- }
- }
- var executeBatches = function(self, callback) {
- var numberOfCommandsToExecute = self.s.batches.length;
-
- for(var i = 0; i < self.s.batches.length; i++) {
- executeBatch(self, self.s.batches[i], function(err) {
-
- if(err) error = err;
-
- numberOfCommandsToExecute = numberOfCommandsToExecute - 1;
-
- if(numberOfCommandsToExecute == 0) {
-
- if(error) return handleCallback(callback, error);
-
- var error = self.s.bulkResult.writeErrors.length > 0 ? toError(self.s.bulkResult.writeErrors[0]) : null;
- handleCallback(callback, error, new BulkWriteResult(self.s.bulkResult));
- }
- });
- }
- }
- UnorderedBulkOperation.prototype.execute = function(_writeConcern, callback) {
- var self = this;
- if(this.s.executed) throw toError("batch cannot be re-executed");
- if(typeof _writeConcern == 'function') {
- callback = _writeConcern;
- } else if(_writeConcern && typeof _writeConcern == 'object') {
- this.s.writeConcern = _writeConcern;
- }
-
- if(this.s.currentInsertBatch) this.s.batches.push(this.s.currentInsertBatch);
- if(this.s.currentUpdateBatch) this.s.batches.push(this.s.currentUpdateBatch);
- if(this.s.currentRemoveBatch) this.s.batches.push(this.s.currentRemoveBatch);
-
- if(this.s.batches.length == 0) {
- throw toError("Invalid Operation, No operations in bulk");
- }
-
- if(typeof callback == 'function') return executeBatches(this, callback);
-
- return new this.s.promiseLibrary(function(resolve, reject) {
- executeBatches(self, function(err, r) {
- if(err) return reject(err);
- resolve(r);
- });
- });
- }
- define.classMethod('execute', {callback: true, promise:false});
- var initializeUnorderedBulkOp = function(topology, collection, options) {
- return new UnorderedBulkOperation(topology, collection, options);
- }
- initializeUnorderedBulkOp.UnorderedBulkOperation = UnorderedBulkOperation;
- module.exports = initializeUnorderedBulkOp;
- module.exports.Bulk = UnorderedBulkOperation;
|