Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

Already on GitHub? Sign in to your account

loops ;) #15

Closed
wants to merge 1 commit into
from
Jump to file or symbol
Failed to load files and symbols.
+23 −16
Split
View
@@ -155,8 +155,8 @@ var Row = module.exports.Row = function(row, decoder) {
this.cols = []; // list of hashes of {name, value};
this.colHash = {}; // hash of name->value
- var count = 0;
- for (var i = 0; i < row.columns.length; i++) {
+ var count = 0, i = 0, numColumns = row.columns.length;
+ for (; i < numColumns; i++) {
if (row.columns[i].value && row.columns[i].name != 'KEY') {
// avoid 'KEY' in column[name:value] as it is neat. Also it breaks specificValidators.
var decodedName = decoder.decode(row.columns[i].name, 'comparator');
@@ -265,14 +265,20 @@ Connection.prototype.connect = function(callback) {
amendError(err, self.connectionInfo);
cb(err);
} else {
- for (var i = 0; i < def.cf_defs.length; i++) {
- var validators = {
+ var i = 0,
+ numDefs = def.cf_defs.length,
+ validators,
+ j = 0,
+ numMeta = 0;
@ctavan

ctavan Jan 31, 2012

Contributor

This driver uses the implicit convention to define one variable with a var statement in each line. IMO new patches should stick to that convention.

+ for (; i < numDefs; i++) {
+ validators = {
@ctavan

ctavan Jan 31, 2012

Contributor

Also it uses the convention of late variable definition instead of defining all vars at the beginning of a function-scope.

key: def.cf_defs[i].key_validation_class,
comparator: def.cf_defs[i].comparator_type,
defaultValidator: def.cf_defs[i].default_validation_class,
specificValidators: {}
};
- for (var j = 0; j < def.cf_defs[i].column_metadata.length; j++) {
+ j = 0, numMeta = def.cf_defs[i].column_metadata.length;
+ for (; j < numMeta; j++) {
// todo: verify that the name we use as the key represents the raw-bytes version of the column name, not
// the stringified version.
validators.specificValidators[def.cf_defs[i].column_metadata[j].name] = def.cf_defs[i].column_metadata[j].validation_class;
@@ -381,7 +387,7 @@ Connection.prototype.execute = function(query, args, callback) {
cqlString = cql.toString(),
start, end, diff;
- start = new Date().getTime();
+ start = +new Date;
@ctavan

ctavan Jan 31, 2012

Contributor

I would prefer Date.now() instead of "plus-magic".

self.emit('log', 'cql', {'query': query, 'parameterized_query': cqlString, 'args': args});
// if a connection dies at the right place, execute_cql_query never returns. make sure the callback gets called.
@@ -397,7 +403,7 @@ Connection.prototype.execute = function(query, args, callback) {
clearTimeout(timeoutId);
}
- end = new Date().getTime();
+ end = +new Date;
diff = (end - start);
if (self.connectionInfo.log_time) {
self.emit('log', 'timing', {
@@ -419,8 +425,8 @@ Connection.prototype.execute = function(query, args, callback) {
var decoder = new Decoder(self.validators[cfName], {use_bigints: self.connectionInfo.use_bigints,
select_count: selectCountRe.test(cql)});
// for now, return results.
- var rows = [];
- for (var i = 0; i < res.rows.length; i++) {
+ var rows = [], i = 0, numRows = res.rows.length;
+ for (; i < numRows; i++) {
var row = new Row(res.rows[i], decoder);
rows.push(row);
}
@@ -465,7 +471,7 @@ ConnectionInPool.prototype.connect = function(callback) {
Connection.call(this, this._options);
Connection.prototype.connect.call(this, function(err) {
self.connected = !err;
- self.unhealthyAt = err ? new Date().getTime() : 0;
+ self.unhealthyAt = err ? +new Date : 0;
callback(err);
});
};
@@ -479,7 +485,7 @@ ConnectionInPool.prototype.isHealthy = function() {
* period, it is safe to retry the connection.
*/
ConnectionInPool.prototype.isStaleUnhealthy = function() {
- return !this.isHealthy() && new Date().getTime() - this.unhealthyAt > this._options.staleThreshold;
+ return !this.isHealthy() && +new Date - this.unhealthyAt > this._options.staleThreshold;
}
/**
@@ -524,7 +530,8 @@ var PooledConnection = module.exports.PooledConnection = function(config) {
this.shuttingDown = false;
// Construct a list of nodes from hosts in <host>:<port> form
- for (var i = 0; i < config.hosts.length; i++) {
+ var i = 0, numConfigs = config.hosts.length;
+ for (; i < numConfigs; i++) {
var hostSpec = config.hosts[i];
if (!hostSpec) { continue; }
var host = hostSpec.split(':');
@@ -651,7 +658,7 @@ PooledConnection.prototype.execute = function(query, args, executeCallback) {
recoverableError = err;
}
if (recoverableError) {
- con.unhealthyAt = new Date().getTime();
+ con.unhealthyAt = +new Date;
con.taken = false;
self.emit('log', 'warn', 'setting unhealthy from execute ' + con.connectionInfo.host + ':' + con.connectionInfo.port);
// try again.
@@ -663,7 +670,7 @@ PooledConnection.prototype.execute = function(query, args, executeCallback) {
});
} catch (err) {
// individual connection has failed.
- con.unhealthyAt = new Date().getTime();
+ con.unhealthyAt = +new Date;
con.taken = false;
self.emit('log', 'warn', 'setting unhealthy from catch outside execute ' + con.connectionInfo.host + ':' + con.connectionInfo.port);
// try again.
@@ -676,7 +683,7 @@ PooledConnection.prototype.execute = function(query, args, executeCallback) {
/** gets the next untaken connection. errors when all connections are bad, or loop times out. */
PooledConnection.prototype._getNextCon = function(callback) {
var self = this;
- var tryStart = new Date().getTime();
+ var tryStart = +new Date;
var con = null;
var allBad = false;
var timedOut = false;
@@ -685,7 +692,7 @@ PooledConnection.prototype._getNextCon = function(callback) {
// should the timeout of getting a single connection be the sum of all connections? Think of a scenario where the
// timeout is N, but the first X nodes are unresponsive. You still want to allow access to the subsequent good
// nodes.
- timedOut = (new Date().getTime() - tryStart) >= (self.timeout * self.connections.length)
+ timedOut = (+new Date - tryStart) >= (self.timeout * self.connections.length)
return !allBad && con === null && !timedOut;
}, function tryConnect(callback) {
var c = self.connections[self.current_node];