From ba678afafb487e444a835e87d80196c2742d31e5 Mon Sep 17 00:00:00 2001 From: Christian Kvalheim Date: Wed, 7 Nov 2012 17:27:55 +0100 Subject: [PATCH] Graceful write concerns first doc operation, collection preset and then db preset --- lib/mongodb/collection.js | 57 +++++++--------------------------- lib/mongodb/db.js | 46 ++++++--------------------- test/write_preferences_test.js | 21 +++++++++++-- 3 files changed, 40 insertions(+), 84 deletions(-) diff --git a/lib/mongodb/collection.js b/lib/mongodb/collection.js index d23b6ae177..4f6ca5ab1c 100644 --- a/lib/mongodb/collection.js +++ b/lib/mongodb/collection.js @@ -1642,54 +1642,21 @@ var _getWriteConcern = function(self, options, callback) { errorOptions = errorOptions == null && self.opts.safe != null ? self.opts.safe : errorOptions; errorOptions = errorOptions == null && self.db.safe != null ? self.db.safe : errorOptions; - // If we have specified the w value override any other existing ones - if(typeof options.w == 'number') { - errorOptions = {w: options.w}; - if(typeof options.wtimeout == 'number') errorOptions.wtimeout = options.wtimeout; - } else if(typeof self.opts.w == 'number') { - errorOptions = {w: self.opts.w}; - if(typeof self.opts.wtimeout == 'number') errorOptions.wtimeout = self.opts.wtimeout; - } else if(typeof self.db.options.w == 'number') { - errorOptions = {w: self.db.options.w}; - if(typeof self.db.options.wtimeout == 'number') errorOptions.wtimeout = self.db.options.wtimeout; + // Local options first + if(typeof options.w == 'number' + || typeof options.journal == 'boolean' || typeof options.fsync == 'boolean') { + errorOptions = options; + } else if(typeof self.opts.w == 'number' + || typeof self.opts.journal == 'boolean' || typeof self.opts.fsync == 'boolean') { + errorOptions = self.opts; + } else if(typeof self.db.options.w == 'number' + || typeof self.db.options.journal == 'boolean' || typeof self.db.options.fsync == 'boolean') { + errorOptions = self.db.options; } - // Set journal value - var journal = null; - // Verify journal setup - if(typeof options.journal == 'boolean') { - journal = options.journal; - } else if(typeof self.opts.journal == 'boolean') { - journal = self.opts.journal; - } else if(typeof self.db.options.journal == 'boolean') { - journal = self.db.options.journal; - } - - // Set fsync value - var fsync = null; - // Verify journal setup - if(typeof options.fsync == 'boolean') { - fsync = options.fsync; - } else if(typeof self.opts.fsync == 'boolean') { - fsync = self.opts.fsync; - } else if(typeof self.db.options.fsync == 'boolean') { - fsync = self.db.options.fsync; - } - - // If w <= 0 and fsync or jorunal is set throw an error - if(errorOptions.w <= 0 && (fsync == true || journal == true)) + // Check that we have a valid combination + if(typeof errorOptions.w == 'number' && errorOptions.w < 1 && (errorOptions.journal == true || errorOptions.fsync == true)) { throw new Error("No acknowlegement using w < 1 cannot be combined with journal:ture or fsync:true"); - - // Add missing journal write concern if any - if(journal != null) { - if(errorOptions != null && typeof errorOptions != 'object') errorOptions = {}; - errorOptions.journal = journal; - } - - // Add missing fsync write concern if any - if(fsync != null) { - if(errorOptions != null && typeof errorOptions != 'object') errorOptions = {}; - errorOptions.fsync = fsync; } // Verify correct behavior of the error conditions diff --git a/lib/mongodb/db.js b/lib/mongodb/db.js index 98b0f71a36..95a9aaf999 100644 --- a/lib/mongodb/db.js +++ b/lib/mongodb/db.js @@ -2157,46 +2157,18 @@ var _getWriteConcern = function(self, options, callback) { var errorOptions = options.safe != null ? options.safe : null; errorOptions = errorOptions == null && self.safe != null ? self.safe : errorOptions; - // If we have specified the w value override any other existing ones - if(typeof options.w == 'number') { - errorOptions = {w: options.w}; - if(typeof options.wtimeout == 'number') errorOptions.wtimeout = options.wtimeout; - } else if(typeof self.options.w == 'number') { - errorOptions = {w: self.options.w}; - if(typeof self.options.wtimeout == 'number') errorOptions.wtimeout = self.options.wtimeout; + // Local options first + if(typeof options.w == 'number' + || typeof options.journal == 'boolean' || typeof options.fsync == 'boolean') { + errorOptions = options; + } else if(typeof self.options.w == 'number' + || typeof self.options.journal == 'boolean' || typeof self.options.fsync == 'boolean') { + errorOptions = self.opts; } - // Set journal value - var journal = null; - // Verify journal setup - if(typeof options.journal == 'boolean') { - journal = options.journal; - } else if(typeof self.options.journal == 'boolean') { - journal = self.options.journal; - } - - // Set fsync value - var fsync = null; - // Verify journal setup - if(typeof options.fsync == 'boolean') { - fsync = options.fsync; - } else if(typeof self.options.fsync == 'boolean') { - fsync = self.options.fsync; - } - - // If w <= 0 and fsync or jorunal is set throw an error - if(errorOptions.w <= 0 && (fsync == true || journal == true)) + // Check that we have a valid combination + if(typeof errorOptions.w == 'number' && errorOptions.w < 1 && (errorOptions.journal == true || errorOptions.fsync == true)) { throw new Error("No acknowlegement using w < 1 cannot be combined with journal:ture or fsync:true"); - // Add missing journal write concern if any - if(journal != null) { - if(errorOptions != null && typeof errorOptions != 'object') errorOptions = {}; - errorOptions.journal = journal; - } - - // Add missing fsync write concern if any - if(fsync != null) { - if(errorOptions != null && typeof errorOptions != 'object') errorOptions = {}; - errorOptions.fsync = fsync; } // Verify correct behavior of the error conditions diff --git a/test/write_preferences_test.js b/test/write_preferences_test.js index 182ad2c0ed..b655d0afb7 100644 --- a/test/write_preferences_test.js +++ b/test/write_preferences_test.js @@ -109,7 +109,7 @@ exports['insert with journal db level'] = function(test) { exports['insert with journal collection level'] = function(test) { var db = new Db(MONGODB, new Server("127.0.0.1", 27017, {auto_reconnect: true, poolSize: 4, ssl:useSSL}) - , {w:0, native_parser: (process.env['TEST_NATIVE'] != null)}); + , {w:1, native_parser: (process.env['TEST_NATIVE'] != null)}); db.open(function(err, db) { db.collection('insert_with_w_1', {journal:true}).update({a:1}, {a:1}, {upsert:true}, function(err, result) { test.equal(null, err); @@ -123,7 +123,7 @@ exports['insert with journal collection level'] = function(test) { exports['insert with journal collection insert level'] = function(test) { var db = new Db(MONGODB, new Server("127.0.0.1", 27017, {auto_reconnect: true, poolSize: 4, ssl:useSSL}) - , {w:0, native_parser: (process.env['TEST_NATIVE'] != null)}); + , {w:1, native_parser: (process.env['TEST_NATIVE'] != null)}); db.open(function(err, db) { db.collection('insert_with_w_1').update({a:1}, {a:1}, {upsert:true, journal:true}, function(err, result) { test.equal(null, err); @@ -148,6 +148,23 @@ exports['insert with journal and w == 1 at db level'] = function(test) { }); } +exports['throw error when combining w:0 and journal'] = function(test) { + var db = new Db(MONGODB, + new Server("127.0.0.1", 27017, {auto_reconnect: true, poolSize: 4, ssl:useSSL}) + , {w:0, journal:true, wtimeout:1000, native_parser: (process.env['TEST_NATIVE'] != null)}); + db.open(function(err, db) { + test.throws(function() { + db.collection('insert_with_w_1').update({a:1}, {a:1}, {upsert:true}, function(err, result) { + test.equal(null, err); + test.equal(1, result); + }); + }, "No acknowlegement using w < 1 cannot be combined with journal:ture or fsync:true"); + + test.done(); + db.close(); + }); +} + /** * Retrieve the server information for the current * instance of the db client