<commit_before>
module.exports = function(Promise, drive) {
  var QL = {
    drivers: {},
    cache: {},
    tables: {},
    promises: [],
    broadcasts: {},
    externals: {},

    loadConfig: function() {
      var self = this;
      var promises = [];

      // custom.json lists the required transports, connections and bases
      var custom = require("../custom.json");

      Object.keys(custom.cache).forEach(function(tableName) {
        self.cache[tableName] = { "__type": custom.cache[tableName], "__status": "empty" };
      });

      // Loop on the transports to initialize them
      Object.keys(custom.transports).forEach(function(_transport) {
        var connections = custom.transports[_transport];

        var path = "../transports/" + _transport + ".js";

        var TransportFactory = require(path);
        var transport = TransportFactory(Promise);

        // Loop on the connections and drive them
        connections.forEach(function(params) {
          id = params.id;

          var driver = drive(Promise, transport, params);

          console.log("Testing connection");

          promises.push(
            driver.connect().then(function(handler) {
              self.drivers[params.id] = driver;
              console.log("Connection OK, closing");
              driver.close();
            })
          );
        });
      });

      return promises;
    },

    qualify: function() {
      console.log("Configuration terminée. Pool de connections actives :")
      console.log("++ < " + Object.keys(this.drivers).length + " > ++");

      var self = this;
      var promises = [];

      // Loop on the drivers
      Object.keys(this.drivers).forEach(function(_driver) {
        var driver = self.drivers[_driver];

        // Get the broadcasts
        if (driver.broadcasts !== undefined) {
          driver.broadcasts.forEach(function(table) {
            self.broadcasts[table] = driver;
          });
        }

        promises.push(
          driver.getTables().then(function(tables) {
            tables.forEach(function(table) {
              if (self.tables[table] === undefined) {
                self.tables[table] = [];
              }
              self.tables[table].push(driver);

              if (driver.external !== undefined && driver.external) {
                if (self.externals[table] === undefined) {
                  self.externals[table] = [];
                }
                self.externals[table].push(driver);
              }
            });
          })
        );
      });

      return promises;
    },

    initCache: function() {
      var self = this;
      var promises = [];

      Object.keys(this.cache).forEach(function(tableName) {
        var type = self.cache[tableName]["__type"];

        if (type === "complete") {
          promises.push(
            self.selectFrom(tableName).send()
          );
        }
      });

      return promises;
    },

    _getFastestFor: function(table) {
      var fastest;

      this.tables[table].forEach(function(driver) {
        if (fastest === undefined || driver.slow < fastest.slow) {
          fastest = driver;
        }
      });

      return fastest;
    },

    _selectNoCache: function(obj) {
      var self = this;

      if (this.broadcasts[obj.params.table] !== undefined) {
        obj.source = this.broadcasts[obj.params.table];
        console.log("Base <" + obj.source.params.id + "> selected for being broadcaster");
      } else {
        obj.source = this._getFastestFor(obj.params.table);
        console.log("Base <" + obj.source.params.id + "> selected for being fastest");
      }
<commit_msg>Resolved bug in db selection<commit_after>
module.exports = function(Promise, drive) {
  var QL = {
    drivers: {},
    cache: {},
    tables: {},
    promises: [],
    broadcasts: {},
    externals: {},

    loadConfig: function() {
      var self = this;
      var promises = [];

      // custom.json lists the required transports, connections and bases
      var custom = require("../custom.json");

      Object.keys(custom.cache).forEach(function(tableName) {
        self.cache[tableName] = { "__type": custom.cache[tableName], "__status": "empty" };
      });

      // Loop on the transports to initialize them
      Object.keys(custom.transports).forEach(function(_transport) {
        var connections = custom.transports[_transport];

        var path = "../transports/" + _transport + ".js";

        var TransportFactory = require(path);
        var transport = TransportFactory(Promise);

        // Loop on the connections and drive them
        connections.forEach(function(params) {
          id = params.id;

          var driver = drive(Promise, transport, params);

          console.log("Testing connection");

          promises.push(
            driver.connect().then(function(handler) {
              self.drivers[params.id] = driver;
              console.log("Connection OK, closing");
              driver.close();
            })
          );
        });
      });

      return promises;
    },

    qualify: function() {
      console.log("Configuration terminée. Pool de connections actives :")
      console.log("++ < " + Object.keys(this.drivers).length + " > ++");

      var self = this;
      var promises = [];

      // Loop on the drivers
      Object.keys(this.drivers).forEach(function(_driver) {
        var driver = self.drivers[_driver];

        // Get the broadcasts
        if (driver.broadcasts !== undefined) {
          driver.broadcasts.forEach(function(table) {
            self.broadcasts[table] = driver;
          });
        }

        promises.push(
          driver.getTables().then(function(tables) {
            tables.forEach(function(table) {
              if (self.tables[table] === undefined) {
                self.tables[table] = [];
              }
              self.tables[table].push(driver);

              if (driver.external !== undefined && driver.external) {
                if (self.externals[table] === undefined) {
                  self.externals[table] = [];
                }
                self.externals[table].push(driver);
              }
            });
          })
        );
      });

      return promises;
    },

    initCache: function() {
      var self = this;
      var promises = [];

      Object.keys(this.cache).forEach(function(tableName) {
        var type = self.cache[tableName]["__type"];

        if (type === "complete") {
          promises.push(
            self.selectFrom(tableName).send()
          );
        }
      });

      return promises;
    },

    _getFastestFor: function(table) {
      var fastest;

      this.tables[table].forEach(function(driver) {
        if (fastest === undefined || driver.params.slow < fastest.params.slow) {
          fastest = driver;
        }
      });

      return fastest;
    },

    _selectNoCache: function(obj) {
      var self = this;

      if (this.broadcasts[obj.params.table] !== undefined) {
        obj.source = this.broadcasts[obj.params.table];
        console.log("Base <" + obj.source.params.id + "> selected for being broadcaster");
      } else {
        obj.source = this._getFastestFor(obj.params.table);
        console.log("Base <" + obj.source.params.id + "> selected for being fastest");
      }

{'fraction_non_alphanumeric': 0.1384678410487505, 'fraction_numerical': 0.0, 'mean_word_length': 1.902893380895759, 'pattern_counts': {'":': 4, '<': 11, '<?xml version=': 0, '>': 9, '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': '6358574', 'n_tokens_mistral': 2028, 'n_tokens_neox': 1939, 'n_words': 390}