Skip to content

Commit

Permalink
refactor(read-preference): remove duplicate ReadPreference
Browse files Browse the repository at this point in the history
There was a ReadPreference defined in mongodb-core as well as in
this driver, which lead to much confusion, unneccesary code
duplication, and most importantly an inability to confidently use
`instanceof` with `ReadPreference`. We now simply use the one from
mongodb-core, and forward that in our exports.

NODE-1187
  • Loading branch information
mbroadst committed Dec 4, 2017
1 parent 5f7d3ac commit 9e06d5f
Show file tree
Hide file tree
Showing 13 changed files with 94 additions and 243 deletions.
4 changes: 3 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
'use strict';

// Core module
var core = require('mongodb-core'),
Instrumentation = require('./lib/apm');
Expand All @@ -16,7 +18,7 @@ connect.Collection = require('./lib/collection');
connect.Server = require('./lib/topologies/server');
connect.ReplSet = require('./lib/topologies/replset');
connect.Mongos = require('./lib/topologies/mongos');
connect.ReadPreference = require('./lib/read_preference');
connect.ReadPreference = require('mongodb-core').ReadPreference;
connect.GridStore = require('./lib/gridfs/grid_store');
connect.Chunk = require('./lib/gridfs/chunk');
connect.Logger = core.Logger;
Expand Down
23 changes: 11 additions & 12 deletions lib/collection.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@ var checkCollectionName = require('./utils').checkCollectionName,
handleCallback = require('./utils').handleCallback,
decorateCommand = require('./utils').decorateCommand,
formattedOrderClause = require('./utils').formattedOrderClause,
ReadPreference = require('./read_preference'),
CoreReadPreference = require('mongodb-core').ReadPreference,
ReadPreference = require('mongodb-core').ReadPreference,
CommandCursor = require('./command_cursor'),
Define = require('./metadata'),
Cursor = require('./cursor'),
Expand Down Expand Up @@ -297,7 +296,7 @@ Collection.prototype.find = function(query, options, callback) {
newOptions.slaveOk = options.slaveOk != null ? options.slaveOk : this.s.db.slaveOk;

// Add read preference if needed
newOptions = getReadPreference(this, newOptions, this.s.db, this);
newOptions = getReadPreference(this, newOptions, this.s.db);

// Set slave ok to true if read preference different from primary
if (
Expand Down Expand Up @@ -3193,28 +3192,28 @@ var writeConcern = function(target, db, col, options) {

// Figure out the read preference
var getReadPreference = function(self, options, db) {
var r = null;
let r = null;
if (options.readPreference) {
r = options.readPreference;
} else if (self.s.readPreference) {
r = self.s.readPreference;
} else if (db.s.readPreference) {
r = db.s.readPreference;
} else {
return options;
}

if (r instanceof ReadPreference) {
options.readPreference = new CoreReadPreference(r.mode, r.tags, {
maxStalenessSeconds: r.maxStalenessSeconds
});
} else if (typeof r === 'string') {
options.readPreference = new CoreReadPreference(r);
if (typeof r === 'string') {
options.readPreference = new ReadPreference(r);
} else if (r && !(r instanceof ReadPreference) && typeof r === 'object') {
var mode = r.mode || r.preference;
const mode = r.mode || r.preference;
if (mode && typeof mode === 'string') {
options.readPreference = new CoreReadPreference(mode, r.tags, {
options.readPreference = new ReadPreference(mode, r.tags, {
maxStalenessSeconds: r.maxStalenessSeconds
});
}
} else if (!(r instanceof ReadPreference)) {
throw new TypeError('Invalid read preference: ' + r);
}

return options;
Expand Down
28 changes: 14 additions & 14 deletions lib/command_cursor.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
'use strict';

var inherits = require('util').inherits,
ReadPreference = require('./read_preference'),
ReadPreference = require('mongodb-core').ReadPreference,
MongoError = require('mongodb-core').MongoError,
Readable = require('stream').Readable,
Define = require('./metadata'),
CoreCursor = require('./cursor'),
CoreReadPreference = require('mongodb-core').ReadPreference;
CoreCursor = require('./cursor');

/**
* @fileOverview The **CommandCursor** class is an internal class that embodies a
Expand Down Expand Up @@ -162,23 +161,24 @@ var define = (CommandCursor.define = new Define('CommandCursor', CommandCursor,
* @throws {MongoError}
* @return {Cursor}
*/
CommandCursor.prototype.setReadPreference = function(r) {
if (this.s.state === CommandCursor.CLOSED || this.isDead())
CommandCursor.prototype.setReadPreference = function(readPreference) {
if (this.s.state === CommandCursor.CLOSED || this.isDead()) {
throw MongoError.create({ message: 'Cursor is closed', driver: true });
if (this.s.state !== CommandCursor.INIT)
}

if (this.s.state !== CommandCursor.INIT) {
throw MongoError.create({
message: 'cannot change cursor readPreference after cursor has been accessed',
driver: true
});
}

if (r instanceof ReadPreference) {
this.s.options.readPreference = new CoreReadPreference(r.mode, r.tags, {
maxStalenessSeconds: r.maxStalenessSeconds
});
} else if (typeof r === 'string') {
this.s.options.readPreference = new CoreReadPreference(r);
} else if (r instanceof CoreReadPreference) {
this.s.options.readPreference = r;
if (readPreference instanceof ReadPreference) {
this.s.options.readPreference = readPreference;
} else if (typeof readPreference === 'string') {
this.s.options.readPreference = new ReadPreference(readPreference);
} else {
throw new TypeError('Invalid read preference: ' + readPreference);
}

return this;
Expand Down
23 changes: 11 additions & 12 deletions lib/cursor.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,12 @@ var inherits = require('util').inherits,
f = require('util').format,
formattedOrderClause = require('./utils').formattedOrderClause,
handleCallback = require('./utils').handleCallback,
ReadPreference = require('./read_preference'),
ReadPreference = require('mongodb-core').ReadPreference,
MongoError = require('mongodb-core').MongoError,
Readable = require('stream').Readable,
Define = require('./metadata'),
CoreCursor = require('mongodb-core').Cursor,
Map = require('mongodb-core').BSON.Map,
CoreReadPreference = require('mongodb-core').ReadPreference,
executeOperation = require('./utils').executeOperation;

/**
Expand Down Expand Up @@ -867,20 +866,20 @@ define.classMethod('forEach', { callback: true, promise: false });
* @throws {MongoError}
* @return {Cursor}
*/
Cursor.prototype.setReadPreference = function(r) {
if (this.s.state !== Cursor.INIT)
Cursor.prototype.setReadPreference = function(readPreference) {
if (this.s.state !== Cursor.INIT) {
throw MongoError.create({
message: 'cannot change cursor readPreference after cursor has been accessed',
driver: true
});
if (r instanceof ReadPreference) {
this.s.options.readPreference = new CoreReadPreference(r.mode, r.tags, {
maxStalenessSeconds: r.maxStalenessSeconds
});
} else if (typeof r === 'string') {
this.s.options.readPreference = new CoreReadPreference(r);
} else if (r instanceof CoreReadPreference) {
this.s.options.readPreference = r;
}

if (readPreference instanceof ReadPreference) {
this.s.options.readPreference = readPreference;
} else if (typeof readPreference === 'string') {
this.s.options.readPreference = new ReadPreference(readPreference);
} else {
throw new TypeError('Invalid read preference: ' + readPreference);
}

return this;
Expand Down
42 changes: 23 additions & 19 deletions lib/db.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,10 @@ var EventEmitter = require('events').EventEmitter,
handleCallback = require('./utils').handleCallback,
filterOptions = require('./utils').filterOptions,
toError = require('./utils').toError,
ReadPreference = require('./read_preference'),
ReadPreference = require('mongodb-core').ReadPreference,
f = require('util').format,
Admin = require('./admin'),
Code = require('mongodb-core').BSON.Code,
CoreReadPreference = require('mongodb-core').ReadPreference,
MongoError = require('mongodb-core').MongoError,
ObjectID = require('mongodb-core').ObjectID,
Define = require('./metadata'),
Expand Down Expand Up @@ -270,25 +269,30 @@ Object.defineProperty(Db.prototype, 'writeConcern', {
});

/**
* Converts provided read preference to CoreReadPreference
* Ensures provided read preference is properly converted into an object
* @param {(ReadPreference|string|object)} readPreference the user provided read preference
* @return {CoreReadPreference}
* @return {ReadPreference}
*/
var convertReadPreference = function(readPreference) {
if (readPreference && typeof readPreference === 'string') {
return new CoreReadPreference(readPreference);
} else if (readPreference instanceof ReadPreference) {
return new CoreReadPreference(readPreference.mode, readPreference.tags, {
maxStalenessSeconds: readPreference.maxStalenessSeconds
});
} else if (readPreference && typeof readPreference === 'object') {
var mode = readPreference.mode || readPreference.preference;
if (mode && typeof mode === 'string') {
readPreference = new CoreReadPreference(mode, readPreference.tags, {
maxStalenessSeconds: readPreference.maxStalenessSeconds
});
const convertReadPreference = function(readPreference) {
if (readPreference) {
if (typeof readPreference === 'string') {
return new ReadPreference(readPreference);
} else if (
readPreference &&
!(readPreference instanceof ReadPreference) &&
typeof readPreference === 'object'
) {
const mode = readPreference.mode || readPreference.preference;
if (mode && typeof mode === 'string') {
return new ReadPreference(mode, readPreference.tags, {
maxStalenessSeconds: readPreference.maxStalenessSeconds
});
}
} else if (!(readPreference instanceof ReadPreference)) {
throw new TypeError('Invalid read preference: ' + readPreference);
}
}

return readPreference;
};

Expand All @@ -314,7 +318,7 @@ var executeCommand = function(self, command, options, callback) {
if (options.readPreference) {
options.readPreference = convertReadPreference(options.readPreference);
} else {
options.readPreference = CoreReadPreference.primary;
options.readPreference = ReadPreference.primary;
}

// Debug information
Expand Down Expand Up @@ -770,7 +774,7 @@ var evaluate = function(self, code, parameters, options, callback) {
}

// Set primary read preference
options.readPreference = new CoreReadPreference(ReadPreference.PRIMARY);
options.readPreference = new ReadPreference(ReadPreference.PRIMARY);

// Execute the command
self.command(cmd, options, function(err, result) {
Expand Down
6 changes: 3 additions & 3 deletions lib/gridfs/grid_store.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
*/
var Chunk = require('./chunk'),
ObjectID = require('mongodb-core').BSON.ObjectID,
ReadPreference = require('../read_preference'),
ReadPreference = require('mongodb-core').ReadPreference,
Buffer = require('buffer').Buffer,
Collection = require('../collection'),
fs = require('fs'),
Expand Down Expand Up @@ -130,7 +130,7 @@ var GridStore = function GridStore(db, id, filename, mode, options) {
this.options['root'] == null ? GridStore.DEFAULT_ROOT_COLLECTION : this.options['root'];
this.position = 0;
this.readPreference =
this.options.readPreference || db.options.readPreference || ReadPreference.PRIMARY;
this.options.readPreference || db.options.readPreference || ReadPreference.primary;
this.writeConcern = _getWriteConcern(db, this.options);
// Set default chunk size
this.internalChunkSize =
Expand Down Expand Up @@ -1396,7 +1396,7 @@ var list = function(db, rootCollection, options, callback) {
}

// Establish read preference
var readPreference = options.readPreference || ReadPreference.PRIMARY;
var readPreference = options.readPreference || ReadPreference.primary;
// Check if we are returning by id not filename
var byId = options['id'] != null ? options['id'] : false;
// Fetch item
Expand Down
2 changes: 1 addition & 1 deletion lib/mongo_client.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ var parse = require('./url_parser'),
EventEmitter = require('events').EventEmitter,
inherits = require('util').inherits,
Define = require('./metadata'),
ReadPreference = require('./read_preference'),
ReadPreference = require('mongodb-core').ReadPreference,
Logger = require('mongodb-core').Logger,
MongoError = require('mongodb-core').MongoError,
handleCallback = require('./utils').handleCallback,
Expand Down
Loading

0 comments on commit 9e06d5f

Please sign in to comment.