From 933adfc257baa6ff8e296667c5f8468cc776d428 Mon Sep 17 00:00:00 2001 From: Christian Amor Kvalheim Date: Fri, 20 Jan 2012 11:15:45 +0100 Subject: [PATCH] Removed __defineGetter__ and __defineSetter__ --- docs/sphinx-docs/source/api-documentation.rst | 41 +++- lib/mongodb/bson/objectid.js | 14 +- lib/mongodb/collection.js | 20 +- lib/mongodb/connection/repl_set_servers.js | 185 +++++++++--------- lib/mongodb/connection/server.js | 129 +++++++----- lib/mongodb/db.js | 9 +- lib/mongodb/gridfs/chunk.js | 23 ++- lib/mongodb/gridfs/gridstore.js | 62 +++--- test/objectid_test.js | 12 ++ 9 files changed, 302 insertions(+), 193 deletions(-) diff --git a/docs/sphinx-docs/source/api-documentation.rst b/docs/sphinx-docs/source/api-documentation.rst index 004f1575e6..e311c7e1bb 100644 --- a/docs/sphinx-docs/source/api-documentation.rst +++ b/docs/sphinx-docs/source/api-documentation.rst @@ -10,4 +10,43 @@ The Node.JS MongoDB Driver API Get's called in case the request fails. And a lot of other text so we need multiple lines :throws SomeError: For whatever reason in that case. - :returns: Something \ No newline at end of file + :returns: Something + +.. code-block:: javascript + var ensureConnection = function(test, numberOfTries, callback) { + // Replica configuration + var replSet = new ReplSetServers( [ + new Server( RS.host, RS.ports[1], { auto_reconnect: true } ), + new Server( RS.host, RS.ports[0], { auto_reconnect: true } ), + new Server( RS.host, RS.ports[2], { auto_reconnect: true } ) + ], + {rs_name:RS.name} + ); + + if(numberOfTries <= 0) return callback(new Error("could not connect correctly"), null); + + var db = new Db('integration_test_', replSet); + // Print any errors + db.on("error", function(err) { + console.log("============================= ensureConnection caught error") + console.dir(err) + if(err != null && err.stack != null) console.log(err.stack) + db.close(); + }) + + // Open the db + db.open(function(err, p_db) { + // Close connections + db.close(); + // Process result + if(err != null) { + // Wait for a sec and retry + setTimeout(function() { + numberOfTries = numberOfTries - 1; + ensureConnection(test, numberOfTries, callback); + }, 1000); + } else { + return callback(null, p_db); + } + }) + } diff --git a/lib/mongodb/bson/objectid.js b/lib/mongodb/bson/objectid.js index 881a0ce3f6..a8e2aeeabc 100644 --- a/lib/mongodb/bson/objectid.js +++ b/lib/mongodb/bson/objectid.js @@ -27,6 +27,7 @@ var checkForHexRegExp = new RegExp("^[0-9a-fA-F]{24}$"); function ObjectID (id) { this._bsontype = 'ObjectID'; + // Throw an error if it's not a valid setup if(id != null && 'number' != typeof id && (id.length != 12 && id.length != 24)) throw new Error("Argument passed in must be a single String of 12 bytes or a string of 24 hex characters in hex format"); // Generate id based on the input @@ -142,9 +143,16 @@ ObjectID.prototype.equals = function equals (otherID) { * @return {Number} */ -ObjectID.prototype.__defineGetter__("generationTime", function() { - return Math.floor(BinaryParser.decodeInt(this.id.substring(0,4), 32, true, true)); -}); +Object.defineProperty(ObjectID.prototype, "generationTime", { + enumerable: true + , get: function () { + return Math.floor(BinaryParser.decodeInt(this.id.substring(0,4), 32, true, true)); + } + , set: function (value) { + var value = BinaryParser.encodeInt(value, 32, true, true); + this.id = value + this.id.substr(4); + } +}); /** * Returns the Date that his ID was generated. diff --git a/lib/mongodb/collection.js b/lib/mongodb/collection.js index af12089921..a125b79c17 100644 --- a/lib/mongodb/collection.js +++ b/lib/mongodb/collection.js @@ -47,18 +47,18 @@ function Collection (db, collectionName, pkFactory, options) { this.pkFactory = pkFactory == null ? ObjectID : pkFactory; - - Object.defineProperty(this, "hint", { - enumerable: true - , get: function () { - return this.internalHint; - } - , set: function (v) { - this.internalHint = this.normalizeHintField(v); - } - }); }; +Object.defineProperty(Collection.prototype, "hint", { + enumerable: true + , get: function () { + return this.internalHint; + } + , set: function (v) { + this.internalHint = this.normalizeHintField(v); + } +}); + /** * Inserts `docs` into the db. * diff --git a/lib/mongodb/connection/repl_set_servers.js b/lib/mongodb/connection/repl_set_servers.js index c3f3e921b9..21c70ba392 100644 --- a/lib/mongodb/connection/repl_set_servers.js +++ b/lib/mongodb/connection/repl_set_servers.js @@ -115,111 +115,110 @@ var ReplSetServers = exports.ReplSetServers = function(servers, options) { } else { this.servers = servers; } - } - - // Auto Reconnect property - Object.defineProperty(this, "autoReconnect", { enumerable: true - , get: function () { - return true; - } - }); - - // Get Read Preference method - Object.defineProperty(this, "readPreference", { enumerable: true - , get: function () { - if(this._readPreference == null && this.readSecondary) { - return Server.READ_SECONDARY; - } else if(this._readPreference == null && !this.readSecondary) { - return Server.READ_PRIMARY; - } else { - return this._readPreference; - } - } - }); + } +}; - // Db Instances - Object.defineProperty(this, "dbInstances", {enumerable:true - , get: function() { - var servers = this.allServerInstances(); - return servers[0].dbInstances; - } - }) +inherits(ReplSetServers, SimpleEmitter); - // Auto Reconnect property - Object.defineProperty(this, "host", { enumerable: true - , get: function () { - if (this.primary != null) return this.primary.host; +// Auto Reconnect property +Object.defineProperty(ReplSetServers.prototype, "autoReconnect", { enumerable: true + , get: function () { + return true; + } +}); + +// Get Read Preference method +Object.defineProperty(ReplSetServers.prototype, "readPreference", { enumerable: true + , get: function () { + if(this._readPreference == null && this.readSecondary) { + return Server.READ_SECONDARY; + } else if(this._readPreference == null && !this.readSecondary) { + return Server.READ_PRIMARY; + } else { + return this._readPreference; } - }); + } +}); - Object.defineProperty(this, "port", { enumerable: true - , get: function () { - if (this.primary != null) return this.primary.port; - } - }); +// Db Instances +Object.defineProperty(ReplSetServers.prototype, "dbInstances", {enumerable:true + , get: function() { + var servers = this.allServerInstances(); + return servers[0].dbInstances; + } +}) - Object.defineProperty(this, "read", { enumerable: true - , get: function () { - return this.secondaries.length > 0 ? this.secondaries[0] : null; - } - }); +// Auto Reconnect property +Object.defineProperty(ReplSetServers.prototype, "host", { enumerable: true + , get: function () { + if (this.primary != null) return this.primary.host; + } +}); +Object.defineProperty(ReplSetServers.prototype, "port", { enumerable: true + , get: function () { + if (this.primary != null) return this.primary.port; + } +}); - // Get list of secondaries - Object.defineProperty(this, "secondaries", {enumerable: true - , get: function() { - var keys = Object.keys(this._state.secondaries); - var array = new Array(keys.length); - // Convert secondaries to array - for(var i = 0; i < keys.length; i++) { - array[i] = this._state.secondaries[keys[i]]; - } - return array; - } - }); +Object.defineProperty(ReplSetServers.prototype, "read", { enumerable: true + , get: function () { + return this.secondaries.length > 0 ? this.secondaries[0] : null; + } +}); - // Get list of all secondaries including passives - Object.defineProperty(this, "allSecondaries", {enumerable: true - , get: function() { - return this.secondaries.concat(this.passives); - } - }); - - // Get list of arbiters - Object.defineProperty(this, "arbiters", {enumerable: true - , get: function() { - var keys = Object.keys(this._state.arbiters); - var array = new Array(keys.length); - // Convert arbiters to array - for(var i = 0; i < keys.length; i++) { - array[i] = this._state.arbiters[keys[i]]; - } - return array; - } - }); - - // Get list of passives - Object.defineProperty(this, "passives", {enumerable: true - , get: function() { - var keys = Object.keys(this._state.passives); - var array = new Array(keys.length); - // Convert arbiters to array - for(var i = 0; i < keys.length; i++) { - array[i] = this._state.passives[keys[i]]; - } - return array; +// Get list of secondaries +Object.defineProperty(ReplSetServers.prototype, "secondaries", {enumerable: true + , get: function() { + var keys = Object.keys(this._state.secondaries); + var array = new Array(keys.length); + // Convert secondaries to array + for(var i = 0; i < keys.length; i++) { + array[i] = this._state.secondaries[keys[i]]; } - }); + return array; + } +}); - // Master connection property - Object.defineProperty(this, "primary", { enumerable: true - , get: function () { - return this._state != null ? this._state.master : null; +// Get list of all secondaries including passives +Object.defineProperty(ReplSetServers.prototype, "allSecondaries", {enumerable: true + , get: function() { + return this.secondaries.concat(this.passives); + } +}); + +// Get list of arbiters +Object.defineProperty(ReplSetServers.prototype, "arbiters", {enumerable: true + , get: function() { + var keys = Object.keys(this._state.arbiters); + var array = new Array(keys.length); + // Convert arbiters to array + for(var i = 0; i < keys.length; i++) { + array[i] = this._state.arbiters[keys[i]]; } - }); -}; + return array; + } +}); + +// Get list of passives +Object.defineProperty(ReplSetServers.prototype, "passives", {enumerable: true + , get: function() { + var keys = Object.keys(this._state.passives); + var array = new Array(keys.length); + // Convert arbiters to array + for(var i = 0; i < keys.length; i++) { + array[i] = this._state.passives[keys[i]]; + } + return array; + } +}); -inherits(ReplSetServers, SimpleEmitter); +// Master connection property +Object.defineProperty(ReplSetServers.prototype, "primary", { enumerable: true + , get: function () { + return this._state != null ? this._state.master : null; + } +}); // Allow setting the read preference at the replicaset level ReplSetServers.prototype.setReadPreference = function(preference) { diff --git a/lib/mongodb/connection/server.js b/lib/mongodb/connection/server.js index 16e43edc35..a01e517bd5 100644 --- a/lib/mongodb/connection/server.js +++ b/lib/mongodb/connection/server.js @@ -35,27 +35,6 @@ var Server = exports.Server = function(host, port, options) { // Contains the isMaster information returned from the server this.isMasterDoc; - // Setters and getters - this.__defineGetter__("autoReconnect", function() { return self.options['auto_reconnect'] == null ? false : this.options['auto_reconnect']; }); - this.__defineGetter__("connection", function() { return self.internalConnection; }); - this.__defineSetter__("connection", function(connection) { self.internalConnection = connection; }); - this.__defineGetter__("master", function() { return self.internalMaster; }); - this.__defineSetter__("master", function(value) { self.internalMaster = value; }); - this.__defineGetter__("primary", function() { return self; }); - this.__defineGetter__("readPreference", function() { return Server.READ_PRIMARY; }); - - // Get Read Preference method - Object.defineProperty(this, "readPreference", { enumerable: true - , get: function () { - if(this._readPreference == null && this.readSecondary) { - return Server.READ_SECONDARY; - } else if(this._readPreference == null && !this.readSecondary) { - return Server.READ_PRIMARY; - } else { - return this._readPreference; - } - } - }); // Set default connection pool options this.socketOptions = this.options.socketOptions != null ? this.options.socketOptions : {}; @@ -80,9 +59,6 @@ var Server = exports.Server = function(host, port, options) { this._state = {'runtimeStats': {'queryStats':new RunningStats()}}; // Do we record server stats or not this.recordQueryStats = false; - // Getter for query Stats - this.__defineGetter__("queryStats", function() { return this._state.runtimeStats.queryStats; }); - this.__defineGetter__("runtimeStats", function() { return this._state.runtimeStats; }); }; // Inherit simple event emitter @@ -92,6 +68,63 @@ Server.READ_PRIMARY = 'primary'; Server.READ_SECONDARY = 'secondary'; Server.READ_SECONDARY_ONLY = 'secondaryOnly'; +// Setters and getters +Object.defineProperty(Server.prototype, "autoReconnect", { enumerable: true + , get: function () { + return this.options['auto_reconnect'] == null ? false : this.options['auto_reconnect']; + } +}); + +Object.defineProperty(Server.prototype, "connection", { enumerable: true + , get: function () { + return this.internalConnection; + } + , set: function(connection) { + this.internalConnection = connection; + } +}); + +Object.defineProperty(Server.prototype, "master", { enumerable: true + , get: function () { + return this.internalMaster; + } + , set: function(value) { + this.internalMaster = value; + } +}); + +Object.defineProperty(Server.prototype, "primary", { enumerable: true + , get: function () { + return this; + } +}); + +// Getter for query Stats +Object.defineProperty(Server.prototype, "queryStats", { enumerable: true + , get: function () { + return this._state.runtimeStats.queryStats; + } +}); + +Object.defineProperty(Server.prototype, "runtimeStats", { enumerable: true + , get: function () { + return this._state.runtimeStats; + } +}); + +// Get Read Preference method +Object.defineProperty(Server.prototype, "readPreference", { enumerable: true + , get: function () { + if(this._readPreference == null && this.readSecondary) { + return Server.READ_SECONDARY; + } else if(this._readPreference == null && !this.readSecondary) { + return Server.READ_PRIMARY; + } else { + return this._readPreference; + } + } +}); + // Allow setting the read preference at the server level Server.prototype.setReadPreference = function(preference) { // Set read preference @@ -551,33 +584,33 @@ var RunningStats = function() { this.m_oldM = 0.0; this.m_oldS = 0.0; this.m_newM = 0.0; - this.m_newS = 0.0; - - // Define getters - Object.defineProperty(this, "numDataValues", { enumerable: true - , get: function () { return this.m_n; } - }); + this.m_newS = 0.0; +} - Object.defineProperty(this, "mean", { enumerable: true - , get: function () { return (this.m_n > 0) ? this.m_newM : 0.0; } - }); +// Define getters +Object.defineProperty(RunningStats.prototype, "numDataValues", { enumerable: true + , get: function () { return this.m_n; } +}); - Object.defineProperty(this, "variance", { enumerable: true - , get: function () { return ((this.m_n > 1) ? this.m_newS/(this.m_n - 1) : 0.0); } - }); +Object.defineProperty(RunningStats.prototype, "mean", { enumerable: true + , get: function () { return (this.m_n > 0) ? this.m_newM : 0.0; } +}); - Object.defineProperty(this, "standardDeviation", { enumerable: true - , get: function () { return Math.sqrt(this.variance); } - }); - - Object.defineProperty(this, "sScore", { enumerable: true - , get: function () { - var bottom = this.mean + this.standardDeviation; - if(bottom == 0) return 0; - return ((2 * this.mean * this.standardDeviation)/(bottom)); - } - }); -} +Object.defineProperty(RunningStats.prototype, "variance", { enumerable: true + , get: function () { return ((this.m_n > 1) ? this.m_newS/(this.m_n - 1) : 0.0); } +}); + +Object.defineProperty(RunningStats.prototype, "standardDeviation", { enumerable: true + , get: function () { return Math.sqrt(this.variance); } +}); + +Object.defineProperty(RunningStats.prototype, "sScore", { enumerable: true + , get: function () { + var bottom = this.mean + this.standardDeviation; + if(bottom == 0) return 0; + return ((2 * this.mean * this.standardDeviation)/(bottom)); + } +}); RunningStats.prototype.push = function(x) { // Update the number of samples diff --git a/lib/mongodb/db.js b/lib/mongodb/db.js index ebd843b094..55e12904fa 100644 --- a/lib/mongodb/db.js +++ b/lib/mongodb/db.js @@ -50,8 +50,6 @@ var Db = exports.Db = function(databaseName, serverConfig, options) { throw "Native bson parser not compiled, please compile or avoid using native_parser=true"; } - // State of the db connection - this.__defineGetter__("state", function() { return this.serverConfig._serverState; }); // Internal state of the server this._state = 'disconnected'; @@ -163,6 +161,13 @@ function validateDatabaseName(databaseName) { inherits(Db, EventEmitter); +// State of the db connection +Object.defineProperty(Db.prototype, "state", { enumerable: true + , get: function () { + return this.serverConfig._serverState; + } +}); + Db.prototype.open = function(callback) { var self = this; diff --git a/lib/mongodb/gridfs/chunk.js b/lib/mongodb/gridfs/chunk.js index 49f48cc38e..f1dccf5dbe 100644 --- a/lib/mongodb/gridfs/chunk.js +++ b/lib/mongodb/gridfs/chunk.js @@ -45,16 +45,23 @@ var Chunk = exports.Chunk = function(file, mongoObject) { } // Update position this.internalPosition = 0; - /** - * The position of the read/write head - * @name position - * @lends Chunk# - * @field - */ - this.__defineGetter__("position", function() { return this.internalPosition; }); - this.__defineSetter__("position", function(value) { this.internalPosition = value; }); }; +/** + * The position of the read/write head + * @name position + * @lends Chunk# + * @field + */ +Object.defineProperty(Chunk.prototype, "position", { enumerable: true + , get: function () { + return this.internalPosition; + } + , set: function(value) { + this.internalPosition = value; + } +}); + /** * Writes a data to this object and advance the read/write head. * diff --git a/lib/mongodb/gridfs/gridstore.js b/lib/mongodb/gridfs/gridstore.js index a3907c09e5..28d89966ee 100644 --- a/lib/mongodb/gridfs/gridstore.js +++ b/lib/mongodb/gridfs/gridstore.js @@ -62,36 +62,42 @@ var GridStore = exports.GridStore = function(db, fileIdObject, mode, options) { this.root = this.options['root'] == null ? exports.GridStore.DEFAULT_ROOT_COLLECTION : this.options['root']; this.position = 0; // Set default chunk size - this.internalChunkSize = this.options['chunkSize'] == null ? Chunk.DEFAULT_CHUNK_SIZE : this.options['chunkSize']; - - /** - * The chunk size used by this file. - * - * @name chunkSize - * @lends GridStore - * @field - */ - this.__defineGetter__("chunkSize", function() { - return this.internalChunkSize; }); - this.__defineSetter__("chunkSize", function(value) { - if(!(this.mode[0] == "w" && this.position == 0 && this.uploadDate == null)) { - this.internalChunkSize = this.internalChunkSize; - } else { - this.internalChunkSize = value; - } - }); - - /** - * The md5 checksum for this file. - * - * @name md5 - * @lends GridStore - * @field - */ - this.__defineGetter__("md5", function() { return this.internalMd5; }); - this.__defineSetter__("md5", function(value) {}); + this.internalChunkSize = this.options['chunkSize'] == null ? Chunk.DEFAULT_CHUNK_SIZE : this.options['chunkSize']; }; +/** + * The chunk size used by this file. + * + * @name chunkSize + * @lends GridStore + * @field + */ +Object.defineProperty(GridStore.prototype, "chunkSize", { enumerable: true + , get: function () { + return this.internalChunkSize; + } + , set: function(value) { + if(!(this.mode[0] == "w" && this.position == 0 && this.uploadDate == null)) { + this.internalChunkSize = this.internalChunkSize; + } else { + this.internalChunkSize = value; + } + } +}); + +/** + * The md5 checksum for this file. + * + * @name md5 + * @lends GridStore + * @field + */ +Object.defineProperty(GridStore.prototype, "md5", { enumerable: true + , get: function () { + return this.internalMd5; + } +}); + /** * Opens the file from the database and initialize this object. Also creates a * new one if file does not exist. diff --git a/test/objectid_test.js b/test/objectid_test.js index 10c8692f62..fa0e9ce49e 100644 --- a/test/objectid_test.js +++ b/test/objectid_test.js @@ -142,6 +142,18 @@ exports.shouldCorrectlyGenerateObjectIDFromTimestamp = function(test) { test.done(); } +exports.shouldCorrectlyCreateAnObjectIDAndOverrideTheTimestamp = function(test) { + var timestamp = 1000; + var objectID = new ObjectID(); + var id1 = objectID.id; + // Override the timestamp + objectID.generationTime = timestamp + var id2 = objectID.id; + // Check the strings + test.equal(id1.substr(4), id2.substr(4)); + test.done(); +} + exports.shouldCorrectlyInsertWithObjectId = function(test) { client.createCollection('shouldCorrectlyInsertWithObjectId', function(err, collection) { collection.insert({}, {safe:true}, function(err, ids) {