<commit_before>/* jslint node: true */
/**
 * PUSH Notification server
 * (c) Telefonica Digital, 2012 - All rights reserved
 * License: GNU Affero V3 (see LICENSE file)
 * <NAME> <<EMAIL>>
 * <NAME> <<EMAIL>>
 */

var mongodb = require('mongodb'),
    log = require('./logger.js'),
    events = require('events'),
    util = require('util'),
    ddbbsettings = require('../config.js').ddbbsettings,
    helpers = require('../common/helpers.js'),
    connectionstate = require('../common/constants.js').connectionstate;

var DataStore = function() {
  this.callbackReady = function(callback) {
    if (this.ready) {
      callback(true);
      return;
    }
    if (!this.callbacks) {
      this.callbacks = [];
    }
    this.callbacks.push(helpers.checkCallback(callback));
  },

  /*
   * MongoDB.Server => https://github.com/mongodb/node-mongodb-native/blob/1.3-dev/lib/mongodb/connection/server.js#L16
   * MongoDB.Db => https://github.com/mongodb/node-mongodb-native/blob/1.3-dev/lib/mongodb/db.js#L37
   * MongoDB.ReplSet => https://github.com/mongodb/node-mongodb-native/blob/1.3-dev/lib/mongodb/connection/repl_set.js#L24
   */
  this.init = function() {
    log.info('datastore::init --> MongoDB data store loading.');
    events.EventEmitter.call(this);

    var mongourl = (function() {
      var url = 'mongodb://';
      var servers = (ddbbsettings.machines).map(function(e) { return e[0] + ':' + e[1]}).toString();
      url += servers;
      var db = ddbbsettings.ddbbname || '';
      url += '/' + db;
      log.info('datastore::init --> Going to connect to -- ' + url);
      return url;
    })();

    var options = {
      db: {},
      server: {
        socketOptions: {
          keepAlive: 1
        }
      },
      replSet: {
        socketOptions: {
          keepAlive: 1
        }
      },
      mongos: {
        socketOptions: {
          keepAlive: 1
        }
      }
    };

    var self = this;
    mongodb.MongoClient.connect(mongourl, options, function(err, db) {
      if (err) {
        log.critical(log.messages.CRITICAL_DBCONNECTIONERROR, {
          "class": 'datastore',
          "method": 'starting',
          "error": err
        });
        self.close();
        return;
      }
      self.db = db;
      log.info('datastore::init --> Connected to MongoDB on ' + ddbbsettings.machines +
               '. Database Name: ' + ddbbsettings.ddbbname);
      self.emit('ddbbconnected');
      self.ready = true;
      var callbacks = self.callbacks || [];
      callbacks.forEach(function(elem) {
        elem(true);
      });
    });<commit_msg>OWD-29078.  Mongos: Once mongos is stopped in any frontend application must not work, in fact, it should be stopped<commit_after>/* jslint node: true */
/**
 * PUSH Notification server
 * (c) Telefonica Digital, 2012 - All rights reserved
 * License: GNU Affero V3 (see LICENSE file)
 * <NAME> <<EMAIL>>
 * <NAME> <<EMAIL>>
 */

var mongodb = require('mongodb'),
    log = require('./logger.js'),
    events = require('events'),
    util = require('util'),
    ddbbsettings = require('../config.js').ddbbsettings,
    helpers = require('../common/helpers.js'),
    connectionstate = require('../common/constants.js').connectionstate;

var DataStore = function() {
  this.callbackReady = function(callback) {
    if (this.ready) {
      callback(true);
      return;
    }
    if (!this.callbacks) {
      this.callbacks = [];
    }
    this.callbacks.push(helpers.checkCallback(callback));
  },

  /*
   * MongoDB.Server => https://github.com/mongodb/node-mongodb-native/blob/1.3-dev/lib/mongodb/connection/server.js#L16
   * MongoDB.Db => https://github.com/mongodb/node-mongodb-native/blob/1.3-dev/lib/mongodb/db.js#L37
   * MongoDB.ReplSet => https://github.com/mongodb/node-mongodb-native/blob/1.3-dev/lib/mongodb/connection/repl_set.js#L24
   */
  this.init = function() {
    log.info('datastore::init --> MongoDB data store loading.');
    events.EventEmitter.call(this);

    var mongourl = (function() {
      var url = 'mongodb://';
      var servers = (ddbbsettings.machines).map(
        function(e) { return e[0] + ':' + e[1]; }
      ).toString();
      url += servers;
      var db = ddbbsettings.ddbbname || '';
      url += '/' + db;
      log.info('datastore::init --> Going to connect to -- ' + url);
      return url;
    })();

    var options = {
      db: {},
      server: {
        socketOptions: {
          keepAlive: 1
        }
      },
      replSet: {
        socketOptions: {
          keepAlive: 1
        }
      },
      mongos: {
        socketOptions: {
          keepAlive: 1
        }
      }
    };

    var self = this;
    mongodb.MongoClient.connect(mongourl, options, function(err, db) {
      if (err) {
        log.critical(log.messages.CRITICAL_DBCONNECTIONERROR, {
          "class": 'datastore',
          "method": 'starting',
          "error": err
        });
        self.close();
        return;
      }
      self.db = db;
      log.info('datastore::init --> Connected to MongoDB on ' + ddbbsettings.machines +
               '. Database Name: ' + ddbbsettings.ddbbname);

      var events = ['close', 'error'];
      events.forEach(function(e) {
        db.on(e, function() {
          self.emit('ddbbdisconnected')
        });
      });

      self.emit('ddbbconnected');
      self.ready = true;
      var callbacks = self.callbacks || [];
      callbacks.forEach(function(elem) {
        elem(true);
      });
    });
{'fraction_non_alphanumeric': 0.16379941434846265, 'fraction_numerical': 0.00896778916544656, 'mean_word_length': 2.251041046995836, 'pattern_counts': {'":': 6, '<': 15, '<?xml version=': 0, '>': 27, 'https://': 6, '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': '6402675', 'n_tokens_mistral': 1833, 'n_tokens_neox': 1762, 'n_words': 320}