<commit_before>'use strict';

/*
 * using mongodb driver >= 2.0
 * docs: http://mongodb.github.io/node-mongodb-native/2.0/api-docs/
 */
var MongoClient = require('mongodb').MongoClient,
    Server = require('mongodb').Server,
    ObjectID = require('mongodb').ObjectID,
    promise = require('enterprise-utils').async.Series.promise;

/*
 * Small wrapper around mongo native driver, will get Db, or creates connection if
 */
module.exports = {
    getDb: getDb,
    createMongoUrl: createMongoUrl,
    ObjectID: ObjectID
}


/**
 * creates mongoUrl from connection opts
 * @param {Object} opts connection opts
 * @returns {String}  mongoUrl
 */
function createMongoUrl(opts){
    // opts.connName,
    // opts.host,
    // opts.database,
    // opts.username,
    // opts.password,
    opts.host = opts.host || 'localhost';
    opts.auto_reconnect = opts.auto_reconnect === false ? false : true;
    opts.port = opts.port || 27017; // default mongodb port
    opts.poolSize = opts.poolSize || 5; // default mongo use 5 connections per client
    opts.native_parser = opts.native_parser === false ? false : true;
    
    if(!opts.host || !opts.database) {
        throw new Error('Model datasources mongoConnector: connection host or database missing');
    }
    
    // only connection prefs are copyied to mongoUrl
    var nonPrefs = ['connName', 'host', 'port', 'database', 'collection', 'username', 'password', 'mongoUrl', 'indexes'];
    
    // http://docs.mongodb.org/manual/reference/connection-string/
    // mongodb://[username:password@]host1[:port1][,host2[:port2],...[,hostN[:portN]]][/[database][?options]]
    var mongoUrl = 'mongodb://' +( opts.username ? opts.username+':'+opts.password+'@' :'' ) +
              opts.host+ ':' +opts.port+ '/' + opts.database + '?';
    
    // copy prefs to mongoUrl
    for(var key in opts) if(nonPrefs.indexOf(key)===-1) mongoUrl += key+'='+opts[key]+'&';
    
    return mongoUrl;
}

/**
 * mongo clients
 * @type {Object}
 */
var mongoDbs = {};

/**
 * gets or creates mongo client db
 * @param {String} mongoUrl will be generated if not set
 * @param {Function} callback (err, mongoClient)
 */
function getDb(mongoUrl, callback) { // callback(err, mongoDb)
    if(typeof callback !== 'function') throw new Error('Wrong arguments');
    
    var dbPromise = mongoDbs[mongoUrl];
    if(dbPromise) { // mongo client db for this connection already promised
        if(dbPromise.isPending) {
            dbPromise.then(function(promise, next){
                if(promise.error) callback(new Error('Model datasources mongoConnector: creating mongo client failed').cause(promise.error));
                else callback(null, promise.value);<commit_msg>fixed: mongoldb url<commit_after>'use strict';

/*
 * using mongodb driver >= 2.0
 * docs: http://mongodb.github.io/node-mongodb-native/2.0/api-docs/
 */
var MongoClient = require('mongodb').MongoClient,
    Server = require('mongodb').Server,
    ObjectID = require('mongodb').ObjectID,
    promise = require('enterprise-utils').async.Series.promise;

/*
 * Small wrapper around mongo native driver, will get Db, or creates connection if
 */
module.exports = {
    getDb: getDb,
    createMongoUrl: createMongoUrl,
    ObjectID: ObjectID
}


/**
 * creates mongoUrl from connection opts
 * @param {Object} opts connection opts
 * @returns {String}  mongoUrl
 */
function createMongoUrl(opts){
    // opts.connName,
    // opts.host,
    // opts.database,
    // opts.username,
    // opts.password,
    opts.host = opts.host || 'localhost';
    opts.auto_reconnect = opts.auto_reconnect === false ? false : true;
    opts.port = opts.port || 27017; // default mongodb port
    opts.poolSize = opts.poolSize || 5; // default mongo use 5 connections per client
    opts.native_parser = opts.native_parser === false ? false : true;
    
    if(!opts.host || !opts.database) {
        throw new Error('Model datasources mongoConnector: connection host or database missing');
    }
    
    // only connection prefs are copyied to mongoUrl
    var nonPrefs = ['connName', 'host', 'port', 'database', 'collection', 'username', 'password', 'mongoUrl', 'indexes'];
    
    // http://docs.mongodb.org/manual/reference/connection-string/
    // mongodb://[username:password@]host1[:port1][,host2[:port2],...[,hostN[:portN]]][/[database][?options]]
    var mongoUrl = 'mongodb://' +( opts.username ? opts.username+':'+opts.password+'@' :'' ) +
              opts.host+ ':' +opts.port+ '/' + opts.database + '?';
    
    // copy prefs to mongoUrl
    for(var key in opts) if(nonPrefs.indexOf(key)===-1) mongoUrl += key+'='+opts[key]+'&';
    mongoUrl = mongoUrl.substring(0, mongoUrl.length-1); // remove last "&"
    
    return mongoUrl;
}

/**
 * mongo clients
 * @type {Object}
 */
var mongoDbs = {};

/**
 * gets or creates mongo client db
 * @param {String} mongoUrl will be generated if not set
 * @param {Function} callback (err, mongoClient)
 */
function getDb(mongoUrl, callback) { // callback(err, mongoDb)
    if(typeof callback !== 'function') throw new Error('Wrong arguments');
    
    var dbPromise = mongoDbs[mongoUrl];
    if(dbPromise) { // mongo client db for this connection already promised
        if(dbPromise.isPending) {
            dbPromise.then(function(promise, next){
                if(promise.error) callback(new Error('Model datasources mongoConnector: creating mongo client failed').cause(promise.error));
                else callback(null, promise.value);
{'fraction_non_alphanumeric': 0.15384615384615385, 'fraction_numerical': 0.006197593875318994, 'mean_word_length': 3.2733644859813085, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, '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': '2985328', 'n_tokens_mistral': 1752, 'n_tokens_neox': 1705, 'n_words': 414}