<commit_before>var mysql = require('mysql'); // https://github.com/felixge/node-mysql
var log = require('tinylogger');
var misc = require('./misc.js');
var conf = require('../enode.config.js').config.mysql;

var sql = {

    connections: [],
    current: 0,
    connectionsCount: 0,

    def: function(err, sender) { // default error handler
        sender = sender ? (sender+': ') : '';
        if (!err) { return; }
        log.error(JSON.stringify(err));
        if (err.fatal) {
            log.panic(sender+'MySQL '+err);
            process.exit();
        }
        else { log.error(sender+'MySQL '+err); }
        return false;
    },

    esc: function(value) { return sql.connections[sql.current].escape(value); },

    query: function(str, values, callback) {
        if (callback == undefined) { callback = sql.def; }
        var q = sql.connections[sql.current].query(str, values, function(err, rows) {
            if (err && err.code == 'ER_LOCK_DEADLOCK') {
                log.warn('Deadlock detected. Query: '+str);
                setTimeout(function(){sql.query(str, values, callback);}, conf.deadlockDelay);
                return;
            }
            callback(err, rows);
        });
        if (conf.log) {
            log.sql('['+sql.current+'] '+(conf.fullLog ? q.sql : q.sql.substring(0,80)));
        }
        sql.current = (sql.current+1) % sql.connectionsCount; // round robin
    },

    setErrorListener: function(sqlConnection) {
        sqlConnection.on('error', function(err) {
            if (!err.fatal) { return; }
            if (err.code !== 'PROTOCOL_sql_LOST') { sql.def(err); return; }
            log.warn('MySQL connection Lost. Re-connecting: ' + err.stack);
            sqlConnection = mysql.createConnection(sqlConnection.config);
            sql.setErrorListener(sqlConnection);
            sqlConnection.connect();
        });
    },

}

exports.init = function(callback) {<commit_msg>reconnect on mysql connection timeout<commit_after>var mysql = require('mysql'); // https://github.com/felixge/node-mysql
var log = require('tinylogger');
var misc = require('./misc.js');
var conf = require('../enode.config.js').config.mysql;

var sql = {

    connections: [],
    current: 0,
    connectionsCount: 0,

    def: function(err, sender) { // default error handler
        sender = sender ? (sender+': ') : '';
        if (!err) { return; }
        log.error(JSON.stringify(err));
        if (err.fatal) {
            log.panic(sender+'MySQL '+err);
            process.exit();
        }
        else { log.error(sender+'MySQL '+err); }
        return false;
    },

    esc: function(value) { return sql.connections[sql.current].escape(value); },

    query: function(str, values, callback) {
        if (callback == undefined) { callback = sql.def; }
        var q = sql.connections[sql.current].query(str, values, function(err, rows) {
            if (err && err.code == 'ER_LOCK_DEADLOCK') {
                log.warn('Deadlock detected. Query: '+str);
                setTimeout(function(){sql.query(str, values, callback);}, conf.deadlockDelay);
                return;
            }
            callback(err, rows);
        });
        if (conf.log) {
            log.sql('['+sql.current+'] '+(conf.fullLog ? q.sql : q.sql.substring(0,80)));
        }
        sql.current = (sql.current+1) % sql.connectionsCount; // round robin
    },

    setErrorListener: function(sqlConnection) {
        sqlConnection.on('error', function(err) {
            if (err.code == 'PROTOCOL_CONNECTION_LOST') {
                log.warn(JSON.stringify(err));
                log.warn('MySQL connection Lost. Re-connecting.');
                sqlConnection = mysql.createConnection(sqlConnection.config);
                sql.setErrorListener(sqlConnection);
                sqlConnection.connect();
            }
            else {
                sql.def(err, 'ErrorListener');
            }
        });
    },

}

exports.init = function(callback) {
{'fraction_non_alphanumeric': 0.1586466165413534, 'fraction_numerical': 0.0030075187969924814, 'mean_word_length': 2.172496025437202, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 2, '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': '2446531', 'n_tokens_mistral': 1183, 'n_tokens_neox': 1148, 'n_words': 224}