<commit_before>/*
 * promiseddb
 * https://github.com/goliatone/promiseddb
 * Created with gbase.
 * Copyright (c) 2014 goliatone
 * Licensed under the MIT license.
 */
/* jshint strict: false, plusplus: true */
/*global define: false, require: false, module: false, exports: false */
(function(root, name, deps, factory) {
    "use strict";
    // Node
    if (typeof deps === 'function') {
        factory = deps;
        deps = [];
    }

    if (typeof exports === 'object') {
        module.exports = factory.apply(root, deps.map(require));
    } else if (typeof define === 'function' && 'amd' in define) {
        //require js, here we assume the file is named as the lower
        //case module name.
        define(name.toLowerCase(), deps, factory);
    } else {
        // Browser
        var d, i = 0,
            global = root,
            old = global[name],
            mod;
        while ((d = deps[i]) !== undefined) deps[i++] = root[d];
        global[name] = mod = factory.apply(global, deps);
        //Export no 'conflict module', aliases the module.
        mod.noConflict = function() {
            global[name] = old;
            return mod;
        };
    }
}(this, 'promiseddb', ['extend'], function(extend) {

    /**
     * Extend method.
     * @param  {Object} target Source object
     * @return {Object}        Resulting object from
     *                         meging target to params.
     */
    var _extend = extend;

    /**
     * Shim console, make sure that if no console
     * available calls do not generate errors.
     * @return {Object} Console shim.
     */
    var _shimConsole = function(con) {

        if (con) return con;

        con = {};
        var empty = {},
            noop = function() {},
            properties = 'memory'.split(','),
            methods = ('assert,clear,count,debug,dir,dirxml,error,exception,group,' +
                'groupCollapsed,groupEnd,info,log,markTimeline,profile,profileEnd,' +
                'table,time,timeEnd,timeStamp,trace,warn').split(','),
            prop,
            method;

        while (method = methods.pop()) con[method] = noop;
        while (prop = properties.pop()) con[prop] = empty;

        return con;
    };



    ///////////////////////////////////////////////////
    // CONSTRUCTOR
    ///////////////////////////////////////////////////

    var OPTIONS = {
        autoconnect:true,
        autoinitialize: true,
        delay: 200,
        maxTries: 5,
        version: 1.0,
        storeId:'_GST_',
        database: '_gstore_default_',
        resultNamespace:'result',
        defineSchema:function() {},
        getDriver:function(){
            return indexedDB || mozIndexedDB || webkitIndexedDB || msIndexedDB;
        }
    };

    /**
     * PromisedDB constructor
     *
     * @param  {object} config Configuration object.
     */
    var PromisedDB = function(config) {
        config = config || {};

        config = _extend({}, this.constructor.DEFAULTS, config);

        if (config.autoinitialize) this.init(config);
    };

    PromisedDB.name = PromisedDB.prototype.name = 'PromisedDB';

    PromisedDB.VERSION = '0.0.0';

    /**
     * Make default options available so we
     * can override.
     */
    PromisedDB.DEFAULTS = OPTIONS;


    ///////////////////////////////////////////////////
    // PRIVATE METHODS
    ///////////////////////////////////////////////////

    PromisedDB.prototype.init = function(config) {
        if (this.initialized) return this.logger.warn('Already initialized');
        this.initialized = true;

        console.log('PromisedDB: Init!');
        _extend(this, config);

        this.tries = 0;
        this.queue = [];

        // Use a vendor specific IndexedDB object.
        this.db = this.getDriver();

        if (config.autoconnect) this.connect();

        return this;
    };

    PromisedDB.prototype.connect = function() {

        var req = this.db.open(this.database, this.version);
        /**
         * Since this is an event, e is an Event object
         * that holds the indexed PromisedDB object.
         */
        req.onsuccess = function(e) {
            this.tries = 0;
            this.connection = e.target.result;
            this.onConnected();
            this.flushQueue();
        };

        // If connection cannot be made to database.
        req.onerror = function(e) {
            if (++this.tries < this.maxTries) {
                setTimeout(this.connect.bind(this), this.delay);
            } else {
                this.onError(e);
            }
        };

        // This will run if our database is new.
        req.onupgradeneeded = function(e) {
            var connection = e.target.result;
            this.defineSchema.apply(connection);
        };

        req.onerror = req.onerror.bind(this);
        req.onsuccess = req.onsuccess.bind(this);
        req.onupgradeneeded = req.onupgradeneeded.bind(this);

    };

    PromisedDB.prototype.flushQueue = function() {
        var reduce = function(last, args) {
            this.resolveTransaction.apply(this, args);
        }.bind(this);
        var out = this.queue.reduce(reduce, []);
        this.queue = [];
    };

    /**
     * Resolve transaction and provide results to caller
     */
    PromisedDB.prototype.resolveTransaction = function(storeId, commit, resolve, reject) {
        var commands = [],
            defaultNamespace = this.resultNamespace,
            transaction = this.connection.transaction(storeId, 'readwrite');

        /*
         * We pass the results to the resolve method.
         * Available as an argument in the `then` method!
         * `query` is actually the IDBRequest returned by
         *  `indexedbd.transaction`
         */
        var execute = function(request, namespace) {
            namespace || (namespace = defaultNamespace);<commit_msg>SRC: PromisedDB added supported method.<commit_after>/*
 * promiseddb
 * https://github.com/goliatone/promiseddb
 * Created with gbase.
 * Copyright (c) 2014 goliatone
 * Licensed under the MIT license.
 */
/* jshint strict: false, plusplus: true */
/*global define: false, require: false, module: false, exports: false */
(function(root, name, deps, factory) {
    "use strict";
    // Node
    if (typeof deps === 'function') {
        factory = deps;
        deps = [];
    }

    if (typeof exports === 'object') {
        module.exports = factory.apply(root, deps.map(require));
    } else if (typeof define === 'function' && 'amd' in define) {
        //require js, here we assume the file is named as the lower
        //case module name.
        define(name.toLowerCase(), deps, factory);
    } else {
        // Browser
        var d, i = 0,
            global = root,
            old = global[name],
            mod;
        while ((d = deps[i]) !== undefined) deps[i++] = root[d];
        global[name] = mod = factory.apply(global, deps);
        //Export no 'conflict module', aliases the module.
        mod.noConflict = function() {
            global[name] = old;
            return mod;
        };
    }
}(this, 'promiseddb', ['extend'], function(extend) {

    /**
     * Extend method.
     * @param  {Object} target Source object
     * @return {Object}        Resulting object from
     *                         meging target to params.
     */
    var _extend = extend;

    /**
     * Shim console, make sure that if no console
     * available calls do not generate errors.
     * @return {Object} Console shim.
     */
    var _shimConsole = function(con) {

        if (con) return con;

        con = {};
        var empty = {},
            noop = function() {},
            properties = 'memory'.split(','),
            methods = ('assert,clear,count,debug,dir,dirxml,error,exception,group,' +
                'groupCollapsed,groupEnd,info,log,markTimeline,profile,profileEnd,' +
                'table,time,timeEnd,timeStamp,trace,warn').split(','),
            prop,
            method;

        while (method = methods.pop()) con[method] = noop;
        while (prop = properties.pop()) con[prop] = empty;

        return con;
    };



    ///////////////////////////////////////////////////
    // CONSTRUCTOR
    ///////////////////////////////////////////////////

    var OPTIONS = {
        autoconnect:true,
        autoinitialize: true,
        delay: 200,
        maxTries: 5,
        version: 1.0,
        storeId:'_GST_',
        database: '_gstore_default_',
        resultNamespace:'result',
        defineSchema:function() {},
        getDriver:function(){
            return indexedDB || mozIndexedDB || webkitIndexedDB || msIndexedDB;
        }
    };

    /**
     * PromisedDB constructor
     *
     * @param  {object} config Configuration object.
     */
    var PromisedDB = function(config) {
        config = config || {};

        config = _extend({}, this.constructor.DEFAULTS, config);

        if (config.autoinitialize) this.init(config);
    };

    PromisedDB.name = PromisedDB.prototype.name = 'PromisedDB';

    PromisedDB.VERSION = '0.0.0';

    /**
     * Make default options available so we
     * can override.
     */
    PromisedDB.DEFAULTS = OPTIONS;

    PromisedDB.supported = function() {
        return !!(indexedDB || mozIndexedDB || webkitIndexedDB || msIndexedDB);
    };
    ///////////////////////////////////////////////////
    // PRIVATE METHODS
    ///////////////////////////////////////////////////

    PromisedDB.prototype.init = function(config) {
        if (this.initialized) return this.logger.warn('Already initialized');
        this.initialized = true;

        console.log('PromisedDB: Init!');
        _extend(this, config);

        this.tries = 0;
        this.queue = [];

        // Use a vendor specific IndexedDB object.
        this.db = this.getDriver();

        if (config.autoconnect) this.connect();

        return this;
    };

    PromisedDB.prototype.connect = function() {

        var req = this.db.open(this.database, this.version);
        /**
         * Since this is an event, e is an Event object
         * that holds the indexed PromisedDB object.
         */
        req.onsuccess = function(e) {
            this.tries = 0;
            this.connection = e.target.result;
            this.onConnected();
            this.flushQueue();
        };

        // If connection cannot be made to database.
        req.onerror = function(e) {
            if (++this.tries < this.maxTries) {
                setTimeout(this.connect.bind(this), this.delay);
            } else {
                this.onError(e);
            }
        };

        // This will run if our database is new.
        req.onupgradeneeded = function(e) {
            var connection = e.target.result;
            //TODO: Should we ensure we notify that
            //we had to create the DB?!
            this.defineSchema.apply(connection);
        };

        req.onerror = req.onerror.bind(this);
        req.onsuccess = req.onsuccess.bind(this);
        req.onupgradeneeded = req.onupgradeneeded.bind(this);
    };

    PromisedDB.prototype.flushQueue = function() {
        var reduce = function(last, args) {
            this.resolveTransaction.apply(this, args);
        }.bind(this);
        var out = this.queue.reduce(reduce, []);
        this.queue = [];
    };

    /**
     * Resolve transaction and provide results to caller
     */
    PromisedDB.prototype.resolveTransaction = function(storeId, commit, resolve, reject) {
        var commands = [],
            defaultNamespace = this.resultNamespace,
            transaction = this.connection.transaction(storeId, 'readwrite');

        /*
         * We pass the results to the resolve method.
         * Available as an argument in the `then` method!
         * `query` is actually the IDBRequest returned by
         *  `indexedbd.transaction`
         */
        var execute = function(request, namespace) {
            namespace || (namespace = defaultNamespace);
{'fraction_non_alphanumeric': 0.15421144713821544, 'fraction_numerical': 0.00266600016662501, 'mean_word_length': 1.7400136955033096, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 2}, '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': '7012479', 'n_tokens_mistral': 3366, 'n_tokens_neox': 3241, 'n_words': 825}