<commit_before>var util = require('util');

var Db = require('mongodb').Db;
var Connection = require('mongodb').Connection;
var Server = require('mongodb').Server;
var BSON = require('mongodb').BSON;
var ObjectID = require('mongodb').ObjectID;

var EventEmitter = require('events').EventEmitter;

var Provider = function(host, port, database, collection) {
	EventEmitter.call(this);
	var self = this;
	self.db = new Db(database, new Server(host, port, {auto_reconnect: true}, {}), {safe:false});
	self.db.open(function(err, results){
		if( err ) self.emit("error", err);
		self.emit("connect", results);
	});
	self.collection = collection;
};

util.inherits(Provider, EventEmitter);

Provider.prototype.getCollection= function(callback) {
  this.db.collection(this.collection, function(error, collection) {
    if( error ) callback(error);
    else callback(null, collection);
  });
};

Provider.prototype.findAll = function(order, callback) {
	if(typeof order == "Function") {
		callback = order;
		order = {};
	}
	//console.log("Hello");
	this.getCollection(function(error, collection) {
      //console.log("Retrieved Collection");
	  if( error ) callback(error);
      else {
        collection.find().toArray(function(error, results) {
		  //console.log("Documents Retrieved");
          if( error ) callback(error)
          else callback(null, results)
        });
      }
    });
};


Provider.prototype.findById = function(id, callback) {
    this.getCollection(function(error, collection) {
      if( error ) callback(error);
      else {
        collection.findOne({_id: collection.db.bson_serializer.ObjectID.createFromHexString(id)}, function(error, result) {
          if( error ) callback(error)
          else callback(null, result)
        });
      }
    });
};

Provider.prototype.findWithOptions = function(document, options, callback) {
	//console.log(document);
	if(typeof options == "Function") {
		callback = options;
		options = {};
	}
	if (!options)
		options = {};	

	this.getCollection(function(error, collection) {
      if( error ) callback(error);
      else {
        collection.find(document, options).toArray(function(error, results) {
          if( error ) callback(error)
          else {
			callback(null, results);
		  }
        });
      }
    });
};

Provider.prototype.find = function(document, order, callback) {
	//console.log(order);
	if(typeof order == "Function") {
		//console.log("Setting Callback to order");
		callback = order;
		order = {};
	}
	if (!order)
		order = {};	

	this.getCollection(function(error, collection) {
      if( error ) callback(error);
      else {
        collection.find(document).sort(order).toArray(function(error, results) {
          if( error ) callback(error)
          else {
			callback(null, results);
		  }
        });
      }
    });
};

Provider.prototype.save = function(document, callback) {
    this.getCollection(function(error, collection) {
      if( error ) callback(error);
      else {<commit_msg>Fixed issue with parameter positioning<commit_after>var util = require('util');

var Db = require('mongodb').Db;
var Connection = require('mongodb').Connection;
var Server = require('mongodb').Server;
var BSON = require('mongodb').BSON;
var ObjectID = require('mongodb').ObjectID;

var EventEmitter = require('events').EventEmitter;

var Provider = function(host, port, database, collection) {
	EventEmitter.call(this);
	var self = this;
	self.db = new Db(database, new Server(host, port, {auto_reconnect: true}, {}), {safe:false});
	self.db.open(function(err, results){
		if( err ) self.emit("error", err);
		self.emit("connect", results);
	});
	self.collection = collection;
};

util.inherits(Provider, EventEmitter);

Provider.prototype.getCollection= function(callback) {
  this.db.collection(this.collection, function(error, collection) {
    if( error ) callback(error);
    else callback(null, collection);
  });
};

Provider.prototype.findAll = function(order, callback) {
	if(typeof order == "function") {
		callback = order;
		order = {};
	}
	//console.log("Hello");
	this.getCollection(function(error, collection) {
      //console.log("Retrieved Collection");
	  if( error ) callback(error);
      else {
        collection.find().toArray(function(error, results) {
		  //console.log("Documents Retrieved");
          if( error ) callback(error)
          else callback(null, results)
        });
      }
    });
};


Provider.prototype.findById = function(id, callback) {
    this.getCollection(function(error, collection) {
      if( error ) callback(error);
      else {
        collection.findOne({_id: collection.db.bson_serializer.ObjectID.createFromHexString(id)}, function(error, result) {
          if( error ) callback(error)
          else callback(null, result)
        });
      }
    });
};

Provider.prototype.findWithOptions = function(document, options, callback) {
	//console.log(document);
	if(typeof options == "function") {
		callback = options;
		options = {};
	}
	if (!options)
		options = {};	

	this.getCollection(function(error, collection) {
      if( error ) callback(error);
      else {
        collection.find(document, options).toArray(function(error, results) {
          if( error ) callback(error)
          else {
			callback(null, results);
		  }
        });
      }
    });
};

Provider.prototype.find = function(document, order, callback) {
	//console.log(order);
	if(typeof order == "function") {
		//console.log("Setting Callback to order");
		callback = order;
		order = {};
	}
	if (!order)
		order = {};	

	this.getCollection(function(error, collection) {
      if( error ) callback(error);
      else {
        collection.find(document).sort(order).toArray(function(error, results) {
          if( error ) callback(error);
          else {
			callback(null, results);
		  }
        });
      }
    });
};

Provider.prototype.save = function(document, callback) {
    this.getCollection(function(error, collection) {
      if( error ) callback(error);
      else {
{'fraction_non_alphanumeric': 0.15419161676646706, 'fraction_numerical': 0.0, 'mean_word_length': 3.3986832479882954, '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': '4760707', 'n_tokens_mistral': 1846, 'n_tokens_neox': 1780, 'n_words': 364}