<commit_before>/**
 *	Copyright (C) 2014 3D Repo Ltd
 *
 *	This program is free software: you can redistribute it and/or modify
 *	it under the terms of the GNU Affero General Public License as
 *	published by the Free Software Foundation, either version 3 of the
 *	License, or (at your option) any later version.
 *
 *	This program is distributed in the hope that it will be useful,
 *	but WITHOUT ANY WARRANTY; without even the implied warranty of
 *	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *	GNU Affero General Public License for more details.
 *
 *	You should have received a copy of the GNU Affero General Public License
 *	along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

// Inspired by
// http://stackoverflow.com/questions/12037655/node-js-mongodb-native-driver-connection-sharing

var async	  = require('async');
var config	  = require('./config.js');
var mongo	  = require('mongodb');
var logIFace  = require('./logger.js');
var logger	  = logIFace.logger;
onError		  = logIFace.onError;

var responseCodes = require('./response_codes.js');

// Main DB Object constructor
function MongoDB() {
	this.host	  = config.db.host;
	this.port	  = config.db.port;

	this.userAuth = null;
	this.dbConns  = {};
}

/*******************************************************************************
 * Authenticate User against admin database
 *
 * @param {Error} err - err object
 * @param {string} username - username logging in
 * @param {string} password - corresponding password for username
 * @param {function} callback - has parameters (err, user) where
 *								user is the user object
 ******************************************************************************/
MongoDB.prototype.authenticateUser = function(username, password, callback) {
	var self = this;

	// Create a separate admin database connection to avoid
	// constantly switching between auth user and NodeJS
	// user
	if (!self.userAuth)
	{
		var serv = new mongo.Server(self.host, self.port, {
			auto_reconnect: true,
		});

		var db = new mongo.Db('admin', serv, { safe: false });

		// TODO: Merge with code below
		db.open(function(err, dbConn) {
			if (err) return callback(responseCodes.DB_ERROR(err));

			self.userAuth = dbConn;

			dbConn.on('close', function(err) {
				self.userAuth = null;
			});
			logger.log('info', 'Authenticating user: ' + username);

			return self.authenticateUser(username, password, callback);
		});
	} else {
		logger.log('info', 'Authenticating user: ' + username);

		self.userAuth.authenticate(username, password, function(err) {
			if(err)
				return callback(responseCodes.DB_ERROR(err));

			callback(responseCodes.OK);
		});
	}
};

/*******************************************************************************
 * Open a database connection and pass it to the callback function
 *
 * @param {Error} err - err object
 * @param {string} dbName - Database name which to open
 * @param {function} callback - has parameters (err, dbConn) where
 *								dbConn is the returned database connection.
 ******************************************************************************/
MongoDB.prototype.dbCallback = function(dbName, callback) {
	var self = this;

	// If we already have a connection, return that rather than
	// opening a new connection
	if (self.dbConns[dbName]) {
		return callback(responseCodes.OK, self.dbConns[dbName]);
	}

	// Check if there is an open server connection
	// if there isn't then open one
	logger.log('info', 'Opening server ' + self.host + ' : ' + self.port);

	var serv = new mongo.Server(self.host, self.port, {
		auto_reconnect: true,
	});

	logger.log('info', 'Opening database ' + dbName);

	var db = new mongo.Db(dbName, serv, {
			safe: false
		});

	// Attempt to open the database connection
	db.open(function(err, dbConn) {
		if (err) {
			logger.log('debug', 'Error connecting to database');
			return callback(responseCodes.DB_ERROR(err));
		}

		// Authenticate against the NodeJS database user
		var adminDb = db.admin();

		adminDb.authenticate(config.db.username, config.db.password, function(err) {
			if (err)
				return callback(responseCodes.DB_ERROR(err));

			logger.log('debug', 'Authentication successful');
			logger.log('debug', 'Authorized as ' + config.db.username);
			logger.log('debug', 'DB CONNECTION:' + JSON.stringify(dbConn.serverConfig.auth.toArray()));

			self.dbConns[dbName] = dbConn;

			dbConn.on('close', function(err) {
				logger.log('debug', 'Closing connection to ' + dbName + '. REASON: ' + err);
				delete(self.dbConns[dbName]);
			})

			callback(responseCodes.OK, dbConn);
		});
	});

}<commit_msg>MASTER - Bug fix for API update on Mongo<commit_after>/**
 *	Copyright (C) 2014 3D Repo Ltd
 *
 *	This program is free software: you can redistribute it and/or modify
 *	it under the terms of the GNU Affero General Public License as
 *	published by the Free Software Foundation, either version 3 of the
 *	License, or (at your option) any later version.
 *
 *	This program is distributed in the hope that it will be useful,
 *	but WITHOUT ANY WARRANTY; without even the implied warranty of
 *	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *	GNU Affero General Public License for more details.
 *
 *	You should have received a copy of the GNU Affero General Public License
 *	along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

// Inspired by
// http://stackoverflow.com/questions/12037655/node-js-mongodb-native-driver-connection-sharing

var async	  = require('async');
var config	  = require('./config.js');
var mongo	  = require('mongodb');
var logIFace  = require('./logger.js');
var logger	  = logIFace.logger;
onError		  = logIFace.onError;

var responseCodes = require('./response_codes.js');

// Main DB Object constructor
function MongoDB() {
	this.host	  = config.db.host;
	this.port	  = config.db.port;

	this.userAuth = null;
	this.dbConns  = {};
}

/*******************************************************************************
 * Authenticate User against admin database
 *
 * @param {Error} err - err object
 * @param {string} username - username logging in
 * @param {string} password - corresponding password for username
 * @param {function} callback - has parameters (err, user) where
 *								user is the user object
 ******************************************************************************/
MongoDB.prototype.authenticateUser = function(username, password, callback) {
	var self = this;

	// Create a separate admin database connection to avoid
	// constantly switching between auth user and NodeJS
	// user
	if (!self.userAuth)
	{
		var serv = new mongo.Server(self.host, self.port, {
			auto_reconnect: true,
		});

		var db = new mongo.Db('admin', serv, { safe: false });

		// TODO: Merge with code below
		db.open(function(err, dbConn) {
			if (err) return callback(responseCodes.DB_ERROR(err));

			self.userAuth = dbConn;

			dbConn.on('close', function(err) {
				self.userAuth = null;
			});
			logger.log('info', 'Authenticating user: ' + username);

			return self.authenticateUser(username, password, callback);
		});
	} else {
		logger.log('info', 'Authenticating user: ' + username);

		self.userAuth.authenticate(username, password, function(err) {
			if(err)
				return callback(responseCodes.DB_ERROR(err));

			callback(responseCodes.OK);
		});
	}
};

/*******************************************************************************
 * Open a database connection and pass it to the callback function
 *
 * @param {Error} err - err object
 * @param {string} dbName - Database name which to open
 * @param {function} callback - has parameters (err, dbConn) where
 *								dbConn is the returned database connection.
 ******************************************************************************/
MongoDB.prototype.dbCallback = function(dbName, callback) {
	var self = this;

	// If we already have a connection, return that rather than
	// opening a new connection
	if (self.dbConns[dbName]) {
		return callback(responseCodes.OK, self.dbConns[dbName]);
	}

	// Check if there is an open server connection
	// if there isn't then open one
	logger.log('info', 'Opening server ' + self.host + ' : ' + self.port);

	var serv = new mongo.Server(self.host, self.port, {
		auto_reconnect: true,
	});

	logger.log('info', 'Opening database ' + dbName);

	var db = new mongo.Db(dbName, serv, {
			safe: false
		});

	// Attempt to open the database connection
	db.open(function(err, dbConn) {
		if (err) {
			logger.log('debug', 'Error connecting to database');
			return callback(responseCodes.DB_ERROR(err));
		}

		// Authenticate against the NodeJS database user
		var adminDb = db.admin();

		adminDb.authenticate(config.db.username, config.db.password, function(err) {
			if (err)
				return callback(responseCodes.DB_ERROR(err));

			logger.log('debug', 'Authentication successful');
			logger.log('debug', 'Authorized as ' + config.db.username);
			//logger.log('debug', 'DB CONNECTION:' + JSON.stringify(dbConn.serverConfig.auth.toArray()));

			self.dbConns[dbName] = dbConn;

			dbConn.on('close', function(err) {
				logger.log('debug', 'Closing connection to ' + dbName + '. REASON: ' + err);
				delete(self.dbConns[dbName]);
			})

			callback(responseCodes.OK, dbConn);
		});
	});

}
{'fraction_non_alphanumeric': 0.18559022193631391, 'fraction_numerical': 0.003002037096601265, 'mean_word_length': 4.036717062634989, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '7168510', 'n_tokens_mistral': 2976, 'n_tokens_neox': 2699, 'n_words': 837}