<commit_before>var M = process.mono;
var mongo = require('mongodb');
var Server = mongo.Server;
var MongoClient = mongo.MongoClient;
var serverOptions = {
    native_parser: true,
    server: {
        poolSize: 3
    }
};

var clients = {};
var cbBuffer = {};

// connected dbs
var dbs = {};

exports.connect =  connect;
exports.model = model;

// model constructor??
function model () {}

// TODO test callback buffering
function connect (config, callback) {
    
    // check config
    if (!config) {
        return callback(new Error('Invalid connection string.'));
    }
    
    // check store cache
    if (dbs[config.database]) {
        return callback(null, dbs[config.database]);
    }
    
    // buffer callbacks when db is connecting
    if (cbBuffer[config.database] instanceof Array) {
        return cbBuffer[config.database].push(callback);
    }
    
    // save callback in buffer
    (cbBuffer[config.database] = cbBuffer[config.database] || []).push(callback);
    
    // get and authenticate db with cached client
    if (clients[config.client]) {
        return getAndAuthenticateDb(clients[config.client], config);
    } 
    
    // get client
    clients[config.client] = new MongoClient(new Server(config.host, config.port, serverOptions));
    
    // open client connection
    clients[config.client].open(function (err, client) {
            
        if (err) {
            return callbacks(config.database, err, db);
        }
        
        // get and authenticate db
        getAndAuthenticateDb(client, config);
    });
}

function getAndAuthenticateDb (client, config) {
    
    // get db
    var db = client.db(config.database);
    
    // authenticate
    db.authenticate(config.user, config.pass, function(err) {
        
        // handle error
        if (err) {
            return callbacks(config.database, err);
        }
        
        dbs[config.database] = db;
        callbacks(config.database, null, db);
    });
} <commit_msg>get auth infos from connect parameters<commit_after>var M = process.mono;
var mongo = require('mongodb');
var Server = mongo.Server;
var MongoClient = mongo.MongoClient;
var serverOptions = {
    native_parser: true,
    server: {
        poolSize: 3
    }
};

var clients = {};
var cbBuffer = {};

// connected dbs
var dbs = {};

exports.connect =  connect;
exports.model = model;

// model constructor??
function model () {}

// TODO test callback buffering
function connect (user, pass, config, callback) {
    
    // check config
    if (!config) {
        return callback(new Error('Invalid connection string.'));
    }
    
    // check store cache
    if (dbs[config.database]) {
        return callback(null, dbs[config.database]);
    }
    
    // buffer callbacks when db is connecting
    if (cbBuffer[config.database] instanceof Array) {
        return cbBuffer[config.database].push(callback);
    }
    
    // save callback in buffer
    (cbBuffer[config.database] = cbBuffer[config.database] || []).push(callback);
    
    // get and authenticate db with cached client
    if (clients[config.client]) {
        return getAndAuthenticateDb(user, pass, clients[config.client], config);
    } 
    
    // get client
    clients[config.client] = new MongoClient(new Server(config.host, config.port, serverOptions));
    
    // open client connection
    clients[config.client].open(function (err, client) {
            
        if (err) {
            return callbacks(config.database, err, db);
        }
        
        // get and authenticate db
        getAndAuthenticateDb(user, pass, client, config);
    });
}

function getAndAuthenticateDb (user, pass, client, config) {
    
    // get db
    var db = client.db(config.database);
    
    // authenticate
    db.authenticate(user, pass, function(err) {
        
        // handle error
        if (err) {
            return callbacks(config.database, err);
        }
        
        dbs[config.database] = db;
        callbacks(config.database, null, db);
    });
} 
{'fraction_non_alphanumeric': 0.12018872609883288, 'fraction_numerical': 0.0004966476285075739, 'mean_word_length': 2.35108153078203, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '2256948', 'n_tokens_mistral': 1183, 'n_tokens_neox': 1168, 'n_words': 297}