Skip to content

Commit

Permalink
Graceful write concerns first doc operation, collection preset and th…
Browse files Browse the repository at this point in the history
…en db preset
  • Loading branch information
christkv committed Nov 7, 2012
1 parent 2cd47ab commit ba678af
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 84 deletions.
57 changes: 12 additions & 45 deletions lib/mongodb/collection.js
Expand Up @@ -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
Expand Down
46 changes: 9 additions & 37 deletions lib/mongodb/db.js
Expand Up @@ -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
Expand Down
21 changes: 19 additions & 2 deletions test/write_preferences_test.js
Expand Up @@ -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);
Expand All @@ -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);
Expand All @@ -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
Expand Down

0 comments on commit ba678af

Please sign in to comment.