<commit_before>// Dependencies
var async = require('async');
var _ = require('underscore');
var parley = require('parley');

// Prototype definitions
var Adapter = require('./adapter.js');
var Collection = require('./collection.js');
var Model = require('./model.js');

// Read global config
var config = require('./config.js');

// Util
var buildDictionary = require('./buildDictionary.js');

// Include built-in adapters
var builtInAdapters = buildDictionary(__dirname + '/adapters', /(.+Adapter)\.js$/, /Adapter/);


/**
* Prepare waterline to interact with adapters
*/
module.exports = function (options,cb) {
	// Only tear down waterline once 
	// (if teardown() is called explicitly, don't tear it down when the process exits)
	var tornDown = false;

	var adapters = options.adapters || {};
	var collections = options.collections || {};
	var log = options.log || console.log;
	var $$ = new parley();

	// Merge passed-in adapters with default adapters
	adapters = _.extend(builtInAdapters,adapters);


	// Error aggregator obj
	// var errs;

	// initialize each adapter in series
	// TODO: parallelize this process (would decrease server startup time)
	$$(async).forEach(_.keys(adapters),prepareAdapter);

	// Instantiate special collections
	var transactionsDb = $$(instantiateCollection)(require('./collections/Transaction.js'));

	// Attach transaction collection to each adapter
	$$(function (err,transactionsDb,xcb) {
		_.each(adapters,function(adapter,adapterName){
			adapters[adapterName].transactionCollection = transactionsDb;
		});
		xcb();
	})(transactionsDb);
	

	// TODO: in sails, in the same way ---->
	// set up session adapter
	// set up socket adapter
	// ------->

	// then associate each collection with its adapter and sync its schema
	$$(async).forEach(_.keys(collections),prepareCollection);

	// Now that the others are instantiated, add transaction collection to list
	// (this is so events like teardownCollection() fire properly)
	$$(function (err,transactionsDb,xcb) {
		collections[transactionsDb.identity] = transactionsDb;
		xcb();
	})(transactionsDb);


	// Fire teardown() on process-end and make it public
	// (this logic lives here to avoid assigning multiple events in each adapter and collection)
	$$(function (xcb) {

		// Make teardown() public
		module.exports.teardown = function(options,cb) {
			teardown({
				adapters: adapters,
				collections: collections
			},cb);
		};

		// When process ends, fire teardown
		process.on('SIGINT', process.exit);
		process.on('SIGTERM', process.exit);
		process.on('exit', function() {
			teardown({
				adapters: adapters,
				collections: collections
			});
		});

		xcb();
	})();

	
	// Pass instantiated adapters and models
	$$(function (xcb) {
		cb(null,{
			adapters: adapters,
			collections: collections
		});

		xcb();
	})();


	// Instantiate an adapter object
	function prepareAdapter (adapterName,cb) {
		// Pass waterline config down to adapters
		adapters[adapterName].config = _.extend({
			log: log
		}, config, adapters[adapterName].config);

		// Build actual adapter object from definition
		// and replace the entry in the adapter dictionary
		adapters[adapterName] = new Adapter(adapters[adapterName]);

		// Load adapter data source
		adapters[adapterName].initialize(cb);
	}

	// Instantiate a collection object and store it back in the dictionary
	function prepareCollection (collectionName, cb) {
		var collection = collections[collectionName];
		instantiateCollection(collection,function (err,collection) {
			collections[collectionName] = collection;
			cb(err,collection);
		});
	}

	// Instantiate a collection object
	function instantiateCollection (collection, cb) {

		// If no adapter is specified, default to 'dirty'
		if (!collection.adapter) collection.adapter = 'dirty';

		// Use adapter shortname in model def. to look up actual object
		if (_.isString(collection.adapter)) {
			if (! adapters[collection.adapter]) throw "Unknown adapter! ("+collection.adapter+")  Maybe try installing it?";
			else collection.adapter = adapters[collection.adapter];
		}

		// Then check that a valid adapter object was retrieved (or already existed)
		if (!(_.isObject(collection.adapter) && collection.adapter._isWaterlineAdapter)) {
			throw "Invalid adapter!";
		}

		// Build actual collection object from definition
		collection = new Collection(collection);

		// Call initializeCollection() event on adapter
		collection.adapter.initializeCollection(collection.identity,function (err) {
			if (err) throw err;
			
			// Synchronize schema with data source
			collection.sync(function (err){ 
				if (err) throw err;
				cb(err,collection); 
			});
		});

	}<commit_msg>Well, apparently it is the tests fault.<commit_after>// Dependencies
var async = require('async');
var _ = require('underscore');
var parley = require('parley');

// Prototype definitions
var Adapter = require('./adapter.js');
var Collection = require('./collection.js');
var Model = require('./model.js');

// Read global config
var config = require('./config.js');

// Util
var buildDictionary = require('./buildDictionary.js');

// Include built-in adapters
var builtInAdapters = buildDictionary(__dirname + '/adapters', /(.+Adapter)\.js$/, /Adapter/);

var builtInCollections = buildDictionary(__dirname + '/collections', /(.+)\.js$/);

/**
* Prepare waterline to interact with adapters
*/
module.exports = function (options,cb) {
	// Only tear down waterline once 
	// (if teardown() is called explicitly, don't tear it down when the process exits)
	var tornDown = false;

	var adapters = options.adapters || {};
	var collections = options.collections || {};
	var log = options.log || console.log;
	var $$ = new parley();

	// Merge passed-in adapters + collections with defaults
	adapters = _.extend(builtInAdapters,adapters);
	collections = _.extend(builtInCollections,collections);
	// console.log("----------KEYS----->",_.keys(collections));


	// Error aggregator obj
	// var errs;

	// initialize each adapter in series
	// TODO: parallelize this process (would decrease server startup time)
	$$(async).forEach(_.keys(adapters),prepareAdapter);

	// then associate each collection with its adapter and sync its schema
	$$(async).forEach(_.keys(collections),prepareCollection);

	// Now that everything is instantiated, add transaction collection to each collection's adapter
	$$(function (xcb) {
		_.each(collections,function(collection) {
			collection.adapter.transactionCollection = collections[config.transactionDbIdentity];
			// console.log("ADDED ",collection.adapter);
		});
		xcb();
	})();


	// Fire teardown() on process-end and make it public
	// (this logic lives here to avoid assigning multiple events in each adapter and collection)
	$$(function (xcb) {

		// Make teardown() public
		module.exports.teardown = function(options,cb) {
			teardown({
				adapters: adapters,
				collections: collections
			},cb);
		};

		// When process ends, fire teardown
		process.on('SIGINT', process.exit);
		process.on('SIGTERM', process.exit);
		process.on('exit', function() {
			teardown({
				adapters: adapters,
				collections: collections
			});
		});

		xcb();
	})();

	
	// Pass instantiated adapters and models
	$$(function (xcb) {
		cb(null,{
			adapters: adapters,
			collections: collections
		});

		xcb();
	})();


	// Instantiate an adapter object
	function prepareAdapter (adapterName,cb) {
		// Pass waterline config down to adapters
		adapters[adapterName].config = _.extend({
			log: log
		}, config, adapters[adapterName].config);

		// Build actual adapter object from definition
		// and replace the entry in the adapter dictionary
		adapters[adapterName] = new Adapter(adapters[adapterName]);

		// Load adapter data source
		adapters[adapterName].initialize(cb);
	}

	// Instantiate a collection object and store it back in the dictionary
	function prepareCollection (collectionName, cb) {
		var collection = collections[collectionName];
		instantiateCollection(collection,function (err,collection) {
			collections[collectionName] = collection;
			cb(err,collection);
		});
	}

	// Instantiate a collection object
	function instantiateCollection (definition, cb) {

		// If no adapter is specified, default to 'dirty'
		if (!definition.adapter) definition.adapter = 'dirty';

		// Use adapter shortname in model def. to look up actual object
		if (_.isString(definition.adapter)) {
			if (! adapters[definition.adapter]) throw "Unknown adapter! ("+definition.adapter+")  Did you include a valid adapter with this name?";
			else definition.adapter = adapters[definition.adapter];
		}

		// Then check that a valid adapter object was retrieved (or already existed)
		if (!(_.isObject(definition.adapter) && definition.adapter._isWaterlineAdapter)) {
			throw "Invalid adapter!";
		}

		// Build actual collection object from definition
		var collection = new Collection(definition);

		// Call initializeCollection() event on adapter
		collection.adapter.initializeCollection(collection.identity,function (err) {
			if (err) throw err;

			// Synchronize schema with data source
			collection.sync(function (err) { 
				if (err) throw err;
				cb(err,collection); 
			});
		});

	}
{'fraction_non_alphanumeric': 0.12977845351867942, 'fraction_numerical': 0.0, 'mean_word_length': 4.062671797691039, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 6, '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': '2915348', 'n_tokens_mistral': 2894, 'n_tokens_neox': 2541, 'n_words': 795}