<commit_before>var Connection = require('./connection'),
    util = require('util');

/**
 * Creates a connection to a keyspace for each of the servers in the pool;
 * @param {Object} options The options for the connection

 * Example:
 *
 *   var pool = new ConnectionPool({
 *     hosts      : ['host1:9160', 'host2:9170', 'host3', 'host4'],
 *     keyspace   : 'database',
 *     user       : 'mary',
 *     pass       : '<PASSWORD>',
 *     timeout    : 30000
 *   });
 *
 * @constructor
 */
var Pool = function(options){
  this.clients = [];
  this.keyspace = options.keyspace;
  this.user = options.user;
  this.password = <PASSWORD>;
  this.timeout = options.timeout;

  if(!options.hosts && options.host){
    this.hosts = [options.hosts];
  } else {
    this.hosts = options.hosts;
  }

  if(!Array.isArray(this.hosts)){
    throw(new Error('HelenusError: Invalid hosts supplied for connection pool'));
  }
};
util.inherits(Pool, process.EventEmitter);

/**
 * Connects to each of the servers in the connection pool
 *
 * TODO: Implement Retries
 * @param {Function} callback The callback to invoke when all connections have been made
 */
Pool.prototype.connect = function(callback){
  var i = 0, finished = 0, client, self = this, len = this.hosts.length,
      connected = 0;

  function onConnect(err, connection, keyspace){
    finished += 1;

    if (err){
      callback(err);
    } else {
      connected += 1;
      self.clients.push(connection);

      //we only want to callback once, after we get a valid connection
      if(connected === 1){
        //set the keyspaces connection to be the pool
        keyspace.connection = self;
        callback(null, keyspace);
      }
    }

    if(finished === len){
      if(self.clients.length === 0){
        callback(new Error('No Available Connections'));
      }
    }
  }

  function connect(host){
    var connection = new Connection({
      host: host,
      keyspace: self.keyspace,
      user: self.user,
      password: self.password,
      timeout: self.timeout
    });

    connection.on('error', function(err){
      self.emit('error', err);
    });

    connection.connect(function(err, keyspace){
      onConnect(err, connection, keyspace);
    });
  }

  for(; i < len; i += 1){
    connect(this.hosts[i]);
  }
};

/**
 * Executes a command on a single client from the pool
 * @param {String} command The command to execute
 * additional params are supplied to the command to be executed<commit_msg>added 'use' to conneciton pool object<commit_after>var Connection = require('./connection'),
    util = require('util');

/**
 * A No-Operation default for empty callbacks
 * @private
 * @memberOf Pool
 */
var NOOP = function(){};

/**
 * Creates a connection to a keyspace for each of the servers in the pool;
 * @param {Object} options The options for the connection

 * Example:
 *
 *   var pool = new ConnectionPool({
 *     hosts      : ['host1:9160', 'host2:9170', 'host3', 'host4'],
 *     keyspace   : 'database',
 *     user       : 'mary',
 *     pass       : '<PASSWORD>',
 *     timeout    : 30000
 *   });
 *
 * @constructor
 */
var Pool = function(options){
  this.clients = [];
  this.keyspace = options.keyspace;
  this.user = options.user;
  this.password = options.password;
  this.timeout = options.timeout;

  if(!options.hosts && options.host){
    this.hosts = [options.hosts];
  } else {
    this.hosts = options.hosts;
  }

  if(!Array.isArray(this.hosts)){
    throw(new Error('HelenusError: Invalid hosts supplied for connection pool'));
  }
};
util.inherits(Pool, process.EventEmitter);

/**
 * Connects to each of the servers in the connection pool
 *
 * TODO: Implement Retries
 * @param {Function} callback The callback to invoke when all connections have been made
 */
Pool.prototype.connect = function(callback){
  var i = 0, finished = 0, client, self = this, len = this.hosts.length,
      connected = 0;

  function onConnect(err, connection, keyspace){
    finished += 1;

    if (err){
      callback(err);
    } else {
      connected += 1;
      self.clients.push(connection);

      //we only want to callback once, after we get a valid connection
      if(connected === 1){
        //set the keyspaces connection to be the pool
        keyspace.connection = self;
        callback(null, keyspace);
      }
    }

    if(finished === len){
      if(self.clients.length === 0){
        callback(new Error('No Available Connections'));
      }
    }
  }

  function connect(host){
    var connection = new Connection({
      host: host,
      keyspace: self.keyspace,
      user: self.user,
      password: <PASSWORD>,
      timeout: self.timeout
    });

    connection.on('error', function(err){
      self.emit('error', err);
    });

    connection.connect(function(err, keyspace){
      onConnect(err, connection, keyspace);
    });
  }

  for(; i < len; i += 1){
    connect(this.hosts[i]);
  }
};

/**
 * Changes the current keyspace for the connection
 */
Pool.prototype.use = function(keyspace, callback){
  callback = callback || NOOP;

  var self = this, i = 0, len = this.clients.length,
      finished = 0, error, ks;

  function onUse(err, keyspace){
    finished += 1;
    error = err;
    ks = keyspace;

    if(finished === len){
      callback(error, ks);
    }
  }

  for(; i < len; i += 1){
    this.clients[i].use(keyspace, onUse);
  }
};

/**
 * Executes a command on a single client from the pool
 * @param {String} command The command to execute
 * additional params are supplied to the command to be executed
{'fraction_non_alphanumeric': 0.13588033879978376, 'fraction_numerical': 0.009731483150117138, 'mean_word_length': 2.7780803267528933, 'pattern_counts': {'":': 0, '<': 10, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '703512', 'n_tokens_mistral': 1722, 'n_tokens_neox': 1650, 'n_words': 502}