<commit_before>'use strict';

var mongodb = require('mongodb');

var connection = null;

function init (env, cb, forceNewConnection) {
  var MongoClient = mongodb.MongoClient;
  var mongo = {};

  function maybe_connect (cb) {

    if (connection != null && !forceNewConnection) {
      console.log('Reusing MongoDB connection handler');
      // If there is a valid callback, then return the Mongo-object
      mongo.db = connection;

      if (cb && cb.call) {
        cb(null, mongo);
      }
    } else {
      if (!env.storageURI) {
        throw new Error('MongoDB connection string is missing');
      }

      console.log('Setting up new connection to MongoDB');
      var timeout =  30 * 1000;
      var options = { reconnectInterval: 10000, reconnectTries: 500, connectTimeoutMS: timeout, socketTimeoutMS: timeout };

      var connect_with_retry = function(i) {
        return MongoClient.connect(env.storageURI, options, function connected(err, client) {
          if (err) {
            //console.log('err=', err)
            if (err.name && err.name === "MongoNetworkError") {
                if (i>20) {
                   // Abort after retrying for more than 10 minutes
                   throw 'Error connecting to MongoDB, stopping the retry loop and aborting...';
                }
                console.log('Error connecting to MongoDB: %j - retrying in ' + i*3 + ' sec', err);
                setTimeout(connect_with_retry, i*3000, i+1);
            } else if (err.message) {
                throw new Error('MongoDB connection string '+env.storageURI+' seems invalid: '+err.message) ; 
            }
          } else {
            console.log('Successfully established a connected to MongoDB');
            
            var dbName = env.storageURI.split('/').pop().split('?');
            dbName=dbName[0]; // drop Connection Options
            mongo.db = client.db(dbName);
            connection = mongo.db;
            mongo.client = client;
            // If there is a valid callback, then invoke the function to perform the callback

            if (cb && cb.call) {<commit_msg>make error message more clear for users<commit_after>'use strict';

var mongodb = require('mongodb');

var connection = null;

function init (env, cb, forceNewConnection) {
  var MongoClient = mongodb.MongoClient;
  var mongo = {};

  function maybe_connect (cb) {

    if (connection != null && !forceNewConnection) {
      console.log('Reusing MongoDB connection handler');
      // If there is a valid callback, then return the Mongo-object
      mongo.db = connection;

      if (cb && cb.call) {
        cb(null, mongo);
      }
    } else {
      if (!env.storageURI) {
        throw new Error('MongoDB connection string is missing. Please set MONGO_CONNECTION environment variable');
      }

      console.log('Setting up new connection to MongoDB');
      var timeout =  30 * 1000;
      var options = { reconnectInterval: 10000, reconnectTries: 500, connectTimeoutMS: timeout, socketTimeoutMS: timeout };

      var connect_with_retry = function(i) {
        return MongoClient.connect(env.storageURI, options, function connected(err, client) {
          if (err) {
            //console.log('err=', err)
            if (err.name && err.name === "MongoNetworkError") {
                if (i>20) {
                   // Abort after retrying for more than 10 minutes
                   throw 'Error connecting to MongoDB, stopping the retry loop and aborting...';
                }
                console.log('Error connecting to MongoDB: %j - retrying in ' + i*3 + ' sec', err);
                setTimeout(connect_with_retry, i*3000, i+1);
            } else if (err.message) {
                throw new Error('MongoDB connection string '+env.storageURI+' seems invalid: '+err.message) ; 
            }
          } else {
            console.log('Successfully established a connected to MongoDB');
            
            var dbName = env.storageURI.split('/').pop().split('?');
            dbName=dbName[0]; // drop Connection Options
            mongo.db = client.db(dbName);
            connection = mongo.db;
            mongo.client = client;
            // If there is a valid callback, then invoke the function to perform the callback

            if (cb && cb.call) {
{'fraction_non_alphanumeric': 0.11149906890130354, 'fraction_numerical': 0.011638733705772812, 'mean_word_length': 2.1342086068563093, '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': '2947121', 'n_tokens_mistral': 1226, 'n_tokens_neox': 1140, 'n_words': 363}