MongoDB speed boost using bulkWrite()

This commit is contained in:
Ylian Saint-Hilaire 2021-01-10 04:53:30 -08:00
parent 3249d727b8
commit e41ac08221
2 changed files with 28 additions and 2 deletions

28
db.js
View File

@ -38,6 +38,8 @@ module.exports.CreateDB = function (parent, func) {
obj.dbRecordsDecryptKey = null;
obj.changeStream = false;
obj.pluginsActive = ((parent.config) && (parent.config.settings) && (parent.config.settings.plugins != null) && (parent.config.settings.plugins != false) && ((typeof parent.config.settings.plugins != 'object') || (parent.config.settings.plugins.enabled != false)));
obj.pendingSet = false;
obj.pendingSets = null;
obj.SetupDatabase = function (func) {
// Check if the database unique identifier is present
@ -1029,7 +1031,17 @@ module.exports.CreateDB = function (parent, func) {
}
} else if (obj.databaseType == 3) {
// Database actions on the main collection (MongoDB)
obj.Set = function (data, func) { data = common.escapeLinksFieldNameEx(data); obj.file.replaceOne({ _id: data._id }, performTypedRecordEncrypt(data), { upsert: true }, func); };
obj.Set = function (data) { // Fast Set operation using bulkWrite(), this is much faster then using replaceOne()
if (obj.pendingSet == false) {
// Perform the operation now
obj.pendingSet = true; obj.pendingSets = null;
obj.file.bulkWrite([{ replaceOne: { filter: { _id: data._id }, replacement: performTypedRecordEncrypt(common.escapeLinksFieldNameEx(data)), upsert: true } }], bulkWriteCompleted);
} else {
// Add this operation to the pending list
if (obj.pendingSets == null) { obj.pendingSets = {} }
obj.pendingSets[data._id] = data;
}
};
obj.Get = function (id, func) {
if (arguments.length > 2) {
var parms = [func];
@ -1408,6 +1420,20 @@ module.exports.CreateDB = function (parent, func) {
}
}
// MongoDB pending bulk write operation, perform fast bulk document replacement.
function bulkWriteCompleted() {
if (obj.pendingSets != null) {
// Perform pending operations
var ops = [], c = 0;
for (var i in obj.pendingSets) { c++; ops.push({ replaceOne: { filter: { _id: i }, replacement: performTypedRecordEncrypt(common.escapeLinksFieldNameEx(obj.pendingSets[i])), upsert: true } }); }
obj.file.bulkWrite(ops, bulkWriteCompleted);
obj.pendingSets = null;
} else {
// All done, no pending operations.
obj.pendingSet = false;
}
}
// Perform a server backup
obj.performingBackup = false;
obj.performBackup = function (func) {

View File

@ -4972,7 +4972,7 @@ module.exports.CreateWebServer = function (parent, db, args, certificates) {
// Add HTTP security headers to all responses
obj.app.use(function (req, res, next) {
// Useful for debugging reverse proxy issues
parent.debug('httpheaders', req.headers);
parent.debug('httpheaders', req.method, req.url, req.headers);
// Set the real IP address of the request
// If a trusted reverse-proxy is sending us the remote IP address, use it.