<commit_before>/*!
 * Module dependencies.
 */

//var Schema = require('./schema');
var gpool = require('generic-pool');
var r = require('rethinkdb');
/**
 * Let's roll out things 
 */

function Thinky () {
    this.models = {};

    this.defaultSettings = {
        host: 'localhost',
        port: 28015,
        db: 'test',
        authKey: '',
        poolMax: 10,
        poolMin: 1,
        enforce: { type: true, missing: false, extra: false },
        timeFormat: 'native'
    };

    this.options = {};
};

Thinky.prototype.init = function(options) {
    var self = this;

    if (!(options instanceof Object)) {
        options = {};
    }
    else if (options === null) {
        options = {};
    }
    else if (Object.prototype.toString.call(options) === '[object Array]') {
        options = {};
    }

    this.setOptions({
        host: options.host || this.defaultSettings.host,
        port: options.port || this.defaultSettings.port,
        db: options.db || this.defaultSettings.db,
        authKey: options.authKey || this.defaultSettings.authKey,
        poolMax: options.poolMax || 10,
        poolMin: options.poolMin || 1,
        enforce: this.options.enforce || this.defaultSettings.enforce,
        timeFormat: this.options.timeFormat || this.defaultSettings.timeFormat
    });

    this.createPool();
    //TODO add option for not using a pool
};

Thinky.prototype.createPool = function () {
    var self = this;
    this.pool = gpool.Pool({
        name: 'rethinkdbPool',
        create: function(cb) {
            r.connect({
                host: self.options.host,
                port: self.options.port,
                authKey: self.options.authKey
            }, function(error, conn) {
                return cb(error, conn);
            })
        },
        destroy: function(connection) {
            connection.close();
        },
        max: self.options.poolMax,
        min: self.options.poolMin,
        idleTimeoutMillis: 30000,
        log: function(what, level) {
            if (level === 'error') {
                require('fs').appendFile('rethinkdb-pool.log', what+'\r\n');
            }
        }
    })
}

Thinky.prototype.getOptions = function () {
    return this.options;
};
Thinky.prototype.setOptions = function (options) {
    var shouldDrain = false;
    for(var key in options) {
        if ((key === 'host') || (key === 'port') || (key === 'poolMax') || (key === 'poolMin')) shouldDrain = true;
        this.setOption(key, options[key], false);
    }
    if (shouldDrain === true) {
        this.disconnect();
        this.createPool();
    }
    return this.options;
};


Thinky.prototype.setOption = function (key, value, drainPool) {
    drainPool = drainPool || true;
    //TODO Update generic pool to be able to change the pool size without creating a new one.
    if (key === 'enforce') {
        this.options.enforce = this.checkEnforce(value, this.defaultSettings.enforce);
    }
    else {
        if (value === null) {
            this.options[key] = this.defaultSettings[key];
        }
        else {
            this.options[key] = value;
        }
        if (drainPool === true) {
            if ((key === 'host') || (key === 'port') || (key === 'poolMin') || (key === 'poolMax')) {
                var stack = new Error().stack
                //console.log( stack )

                this.disconnect();
                this.createPool();
            }
        }
    }
    return this.options;
};

Thinky.prototype.checkEnforce = function(newValue, defaultValue) {
    if (newValue === true) {
        return {missing: true, extra: true, type: true}
    }
    else if (newValue === false) {
        return {missing: false, extra: false, type: false}
    }
    else if ((newValue != null) && (typeof newValue === 'object') && (Object.prototype.toString.call(newValue) === '[object Object]')) {
        var result = {
            extra: defaultValue.extra,
            type: defaultValue.type,
            missing: defaultValue.missing<commit_msg>Cleaning - Array.isArray, init, squash some if statements<commit_after>Array.isArray = Array.isArray || function(obj) {
    return Object.prototype.toString.call(obj) === "[object Array]";
};

var gpool = require('generic-pool');
var r = require('rethinkdb');

function Thinky () {
    this.models = {};

    this.defaultSettings = {
        host: 'localhost',
        port: 28015,
        db: 'test',
        authKey: '',
        poolMax: 10,
        poolMin: 1,
        enforce: { type: true, missing: false, extra: false },
        timeFormat: 'native',
        timeoutConnection: 30000
    };

    this.options = {};
};

Thinky.prototype.init = function(options) {
    var self = this;

    if ((!(options instanceof Object)) || (options === null) || (Array.isArray(options))) {
        options = {};
    }

    this.setOptions({
        host: options.host || this.defaultSettings.host,
        port: options.port || this.defaultSettings.port,
        db: options.db || this.defaultSettings.db,
        authKey: options.authKey || this.defaultSettings.authKey,
        poolMax: options.poolMax || this.defaultSettings.poolMax,
        poolMin: options.poolMin || this.defaultSettings.poolMin,
        enforce: this.options.enforce || this.defaultSettings.enforce,
        timeFormat: this.options.timeFormat || this.defaultSettings.timeFormat
    });

    this.createPool();
    //TODO add option for not using a pool
};

Thinky.prototype.createPool = function () {
    var self = this;

    this.pool = gpool.Pool({
        name: 'rethinkdbPool',
        create: function(cb) {
            r.connect({
                host: self.options.host,
                port: self.options.port,
                authKey: self.options.authKey
            }, function(error, conn) {
                return cb(error, conn);
            })
        },
        destroy: function(connection) {
            connection.close();
        },
        max: self.options.poolMax,
        min: self.options.poolMin,
        idleTimeoutMillis: self.options.timeoutConnection,
        log: function(what, level) {
            if (level === 'error') {
                require('fs').appendFile('rethinkdb-pool.log', what+'\r\n');
            }
        }
    })
}

Thinky.prototype.getOptions = function () {
    return this.options;
};

Thinky.prototype.setOptions = function (options) {
    var shouldDrain = false;
    for(var key in options) {
        if ((key === 'host') || (key === 'port') || (key === 'poolMax') || (key === 'poolMin')) shouldDrain = true;
        this.setOption(key, options[key], false);
    }
    if (shouldDrain === true) {
        this.disconnect();
        this.createPool();
    }
    return this.options;
};


Thinky.prototype.setOption = function (key, value, drainPool) {
    drainPool = drainPool || true;
    //TODO Update generic pool to be able to change the pool size without creating a new one.
    if (key === 'enforce') {
        this.options.enforce = this.checkEnforce(value, this.defaultSettings.enforce);
    }
    else {
        if (value === null) {
            this.options[key] = this.defaultSettings[key];
        }
        else {
            this.options[key] = value;
        }
        if (drainPool === true) {
            if ((key === 'host') || (key === 'port') || (key === 'poolMin') || (key === 'poolMax')) {
                this.disconnect();
                this.createPool();
            }
        }
    }
    return this.options;
};

Thinky.prototype.checkEnforce = function(newValue, defaultValue) {
    if (newValue === true) {
        return {missing: true, extra: true, type: true}
    }
    else if (newValue === false) {
        return {missing: false, extra: false, type: false}
    }
    else if ((newValue != null) && (typeof newValue === 'object') && (Object.prototype.toString.call(newValue) === '[object Object]')) {
        var result = {
            extra: defaultValue.extra,
            type: defaultValue.type,
            missing: defaultValue.missing
{'fraction_non_alphanumeric': 0.14783908332295428, 'fraction_numerical': 0.003611906837713289, 'mean_word_length': 2.1232983274990276, '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': '4280003', 'n_tokens_mistral': 2362, 'n_tokens_neox': 2305, 'n_words': 540}