<commit_before>var _ = require('underscore');
var IO = require('./IO');

/////////////
// Modules //
/////////////

module.exports = function(tenant){}

module.exports.getAvailableModules = function(callback) {
    IO.getFileListForFolder("node_modules", function(modules) {
        var finalModules = [];
        for (var m = 0; m < modules.length; m++) {
            if (modules[m].substring(0, 4) === "oae-") {
                finalModules.push(modules[m]);
            }
        }
        callback(finalModules);
    });
};

//////////////////////
// Cassandra access //
//////////////////////

var CassandraSystem = require('cassandra-client').System;
var CassandraTypes = require('cassandra-client/lib/gen-nodejs/cassandra_types');
var Connection = require('cassandra-client').Connection;

var sys = new CassandraSystem('127.0.0.1:9160');
var KEYSPACE = "oae";

module.exports.getConnection = function() {
    return new Connection({hosts: 'localhost', 'port': 9160, keyspace: KEYSPACE, 'user': '', 'pass': ''});;
};

module.exports.initializeKeySpace = function(callback) {
    var ksDef = new CassandraTypes.KsDef({
        name: KEYSPACE, 
        strategy_class: 'org.apache.cassandra.locator.SimpleStrategy', 
        strategy_options: {
            'replication_factor': '1'
        }, 
        cf_defs: []
    });
    sys.addKeyspace(ksDef, function(err) {
        if (err) {
            console.log("The keyspace could not be created");
            console.log(err);
        } else {
            console.log("The keyspace has been created");
        }
        callback();
    });
};

/**
 * Run a single Cassandra query.
 * @param  {String}   query      The CQL query
 * @param  {array}    parameters An array of values that can be interpreted by node-cassandra-client. If an element is detected as an array the query and parameters will be fixed.
 * @param  {Function} callback   Callback method.
 */
module.exports.runQuery = function(query, parameters, callback) {
    var fixed = fixQuery(query, parameters);
    executeQuery(fixed.query, fixed.parameters, callback);
};

/**
 * Run a batched Cassandra query.
 * @param  {Object[]}   queries  An array of simple hashes. Each hash should contain a query key and parameters key.
 * @param  {Function}   callback Callback method
 */
module.exports.runBatchQuery = function(queries, callback) {
    var query = 'BEGIN BATCH USING CONSISTENCY ONE \n';
    var parameters = [];
    for (var i = 0; i < queries.length;i++) {
        var fixed = fixQuery(queries[i].query, queries[i].parameters);
        query += fixed.query + "; \n";
        parameters = parameters.concat(fixed.parameters);
    }
    query += 'APPLY BATCH;';
    executeQuery(query, parameters, callback);
};


/**
 * Adds the ability to pass in array parameters. If you pass in a parameter that is an array, the apropriate number of question marks will be added in the query.
 * @param  {String}    query      The query
 * @param  {Object[]}  parameters The parameters for this query.
 */
var fixQuery = function(query, parameters) {
    var fixedQuery = "";
    var fixedParameters = [];
    var q = query.indexOf('?');
    var from = 0;
    var i = 0;
    while (q >= 0) {
        fixedQuery += query.slice(from, q);

        // If we passed in an array add n-1 more question marks.
        if (Array.isArray(parameters[i])) {
            for (var c = 0; c < parameters[i].length - 1;c++) {
                fixedQuery += "?, ";
                fixedParameters.push(parameters[i][c]);
            }
            fixedParameters.push(parameters[i][parameters[i].length - 1]);
        }
        else {
            fixedParameters.push(parameters[i]);
        }
        fixedQuery += '?';


        // Next one.
        from = q+1;
        q = query.indexOf('?', from);
        i++;
    }

    // Append the request of the query.
    fixedQuery += query.slice(from);
    return {'query': fixedQuery, 'parameters': fixedParameters};
};


/**
 * Actually executes a query
 * @param  {String}    query       The query
 * @param  {Object[]}  parameters  The parameters that match this query
 * @param  {Function}  callback    Callback
 */
var executeQuery = function(query, parameters, callback) {
    var con = module.exports.getConnection();
    con.connect(function(err){
        if (err) {
            console.log("Failed to connect to Cassandra");
            console.log(err);
        } else {
            con.execute(query, parameters, function(err, rows) {
                con.close(function() {
                    if (callback)
                        callback(err, rows);
                });
            });
        }
    });
};<commit_msg>Fix if statement<commit_after>var _ = require('underscore');
var IO = require('./IO');

/////////////
// Modules //
/////////////

module.exports = function(tenant){}

module.exports.getAvailableModules = function(callback) {
    IO.getFileListForFolder("node_modules", function(modules) {
        var finalModules = [];
        for (var m = 0; m < modules.length; m++) {
            if (modules[m].substring(0, 4) === "oae-") {
                finalModules.push(modules[m]);
            }
        }
        callback(finalModules);
    });
};

//////////////////////
// Cassandra access //
//////////////////////

var CassandraSystem = require('cassandra-client').System;
var CassandraTypes = require('cassandra-client/lib/gen-nodejs/cassandra_types');
var Connection = require('cassandra-client').Connection;

var sys = new CassandraSystem('127.0.0.1:9160');
var KEYSPACE = "oae";

module.exports.getConnection = function() {
    return new Connection({hosts: 'localhost', 'port': 9160, keyspace: KEYSPACE, 'user': '', 'pass': ''});;
};

module.exports.initializeKeySpace = function(callback) {
    var ksDef = new CassandraTypes.KsDef({
        name: KEYSPACE, 
        strategy_class: 'org.apache.cassandra.locator.SimpleStrategy', 
        strategy_options: {
            'replication_factor': '1'
        }, 
        cf_defs: []
    });
    sys.addKeyspace(ksDef, function(err) {
        if (err) {
            console.log("The keyspace could not be created");
            console.log(err);
        } else {
            console.log("The keyspace has been created");
        }
        callback();
    });
};

/**
 * Run a single Cassandra query.
 * @param  {String}   query      The CQL query
 * @param  {array}    parameters An array of values that can be interpreted by node-cassandra-client. If an element is detected as an array the query and parameters will be fixed.
 * @param  {Function} callback   Callback method.
 */
module.exports.runQuery = function(query, parameters, callback) {
    var fixed = fixQuery(query, parameters);
    executeQuery(fixed.query, fixed.parameters, callback);
};

/**
 * Run a batched Cassandra query.
 * @param  {Object[]}   queries  An array of simple hashes. Each hash should contain a query key and parameters key.
 * @param  {Function}   callback Callback method
 */
module.exports.runBatchQuery = function(queries, callback) {
    var query = 'BEGIN BATCH USING CONSISTENCY ONE \n';
    var parameters = [];
    for (var i = 0; i < queries.length;i++) {
        var fixed = fixQuery(queries[i].query, queries[i].parameters);
        query += fixed.query + "; \n";
        parameters = parameters.concat(fixed.parameters);
    }
    query += 'APPLY BATCH;';
    executeQuery(query, parameters, callback);
};


/**
 * Adds the ability to pass in array parameters. If you pass in a parameter that is an array, the apropriate number of question marks will be added in the query.
 * @param  {String}    query      The query
 * @param  {Object[]}  parameters The parameters for this query.
 */
var fixQuery = function(query, parameters) {
    var fixedQuery = "";
    var fixedParameters = [];
    var q = query.indexOf('?');
    var from = 0;
    var i = 0;
    while (q >= 0) {
        fixedQuery += query.slice(from, q);

        // If we passed in an array add n-1 more question marks.
        if (Array.isArray(parameters[i])) {
            for (var c = 0; c < parameters[i].length - 1;c++) {
                fixedQuery += "?, ";
                fixedParameters.push(parameters[i][c]);
            }
            fixedParameters.push(parameters[i][parameters[i].length - 1]);
        }
        else {
            fixedParameters.push(parameters[i]);
        }
        fixedQuery += '?';


        // Next one.
        from = q+1;
        q = query.indexOf('?', from);
        i++;
    }

    // Append the request of the query.
    fixedQuery += query.slice(from);
    return {'query': fixedQuery, 'parameters': fixedParameters};
};


/**
 * Actually executes a query
 * @param  {String}    query       The query
 * @param  {Object[]}  parameters  The parameters that match this query
 * @param  {Function}  callback    Callback
 */
var executeQuery = function(query, parameters, callback) {
    var con = module.exports.getConnection();
    con.connect(function(err){
        if (err) {
            console.log("Failed to connect to Cassandra");
            console.log(err);
        } else {
            con.execute(query, parameters, function(err, rows) {
                con.close(function() {
                    if (callback) {
                        callback(err, rows);
                    }
                });
            });
        }
    });
};
{'fraction_non_alphanumeric': 0.14652874053535245, 'fraction_numerical': 0.005758771462088088, 'mean_word_length': 2.277874868926949, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 2, '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': '5607249', 'n_tokens_mistral': 2696, 'n_tokens_neox': 2642, 'n_words': 740}