<commit_before>var _ = require('lodash');
var uuid = require('node-uuid').v4;

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

var Kuery = require('kuery');

var Cursor = require('../cursor');

var Collection = function (collectionName) {
	EventEmitter.call(this);

	this._documents = [];
	this._name = collectionName;
}

util.inherits(Collection, EventEmitter);


Collection.prototype.count = function (callback) {
	callback(null, this._documents.length);
};

Collection.prototype.insert = function (document, callback) {
	if (_.isArray(document)) {
		return callback(new Error('cannot store array'));
	}
	if (!_.isObject(document)) {
		return callback(new Error('cannot store non-object'));
	}
	if (!_.has(document, '_id')) {
		document['_id'] = uuid();
	}
	if (_.findIndex(this._documents, { _id: document['_id'] }) !== -1) {
		return callback(Error('Unique constraint!'));
	}
	this._documents.push(document);
	this.emit("change", document);
	if (callback) {
		callback(null, document);
	}
};

Collection.prototype.save = function (document, callback) {
	if (!_.has(document, '_id')) {
		return this.insert(document, callback);
	} else {
		var index = _.findIndex(this._documents, function (doc) { return doc['_id'] === document['_id']; });
		if (index != -1) {
			this._documents[index] = document;
			this.emit("change", document);
		} else {
			return callback(new Error('A document with the _id does not exist'));
		}
		if (callback) {
			callback(null, document);
		}
	}
}

Collection.prototype.drop = function (callback) {
	this._documents = [];

	if (callback) {
		callback(null);
	}
}


Collection.prototype.find = function (query, options) {
	return new Cursor(this, query, options, this._getDocuments.bind(this));
}

Collection.prototype.remove = function (query, options, callback) {
	var self = this;
	this.find(query, options).toArray(function (err, docs) {
		var ids = _.map(docs, function (doc) { return doc._id });

		_.forEach(ids, function (id) {
			var index = _.findIndex(self._documents, function (doc) { return doc['_id'] === id; });
			if (index != -1) {
				self._documents.splice(index, 1);
				self.emit("change", {});
			}
		});

		callback(null);
	});
}


Collection.prototype._getDocuments = function (query, callback) {
	var self = this;
	var q = new Kuery(query);
	var documents = q.find(self._documents);
	process.nextTick(function () {
		callback(null, _.cloneDeep(documents));
	});
};

module.exports = Collection;<commit_msg>reverted indentation<commit_after>var _ = require('lodash');
var uuid = require('node-uuid').v4;

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

var Kuery = require('kuery');

var Cursor = require('../cursor');

var Collection = function(collectionName) {
	EventEmitter.call(this);

	this._documents = [];
	this._name = collectionName;
}

util.inherits(Collection, EventEmitter);


Collection.prototype.count = function(callback) {
	callback(null, this._documents.length);
};

Collection.prototype.insert = function(document, callback) {
	if(_.isArray(document)) {
		return callback(new Error('cannot store array'));
	}
	if(!_.isObject(document)) {
		return callback(new Error('cannot store non-object'));
	}
	if(!_.has(document, '_id')) {
		document['_id'] = uuid();
	}
	if(_.findIndex(this._documents, {_id:document['_id']}) !== -1) {
		return callback(Error('Unique constraint!'));
	}
	this._documents.push(document);
	this.emit("change", document);
	if(callback) {
		callback(null, document);
	}
};

Collection.prototype.save = function(document, callback) {
	if(!_.has(document, '_id')) {
		return this.insert(document, callback);
	} else {
		var index = _.findIndex(this._documents, function(doc) {return doc['_id'] === document['_id'];});
		if(index != -1) {
			this._documents[index] = document;
			this.emit("change", document);
		} else {
			return callback(new Error('A document with the _id does not exist'));
		}
		if(callback) {
			callback(null, document);
		}
	}
}

Collection.prototype.drop = function(callback) {
	this._documents = [];

	if(callback) {
		callback(null);
	}
}


Collection.prototype.find = function(query, options){
	return new Cursor(this, query, options, this._getDocuments.bind(this));
}

Collection.prototype.remove = function(query, options, callback) {
	var self = this;
	this.find(query, options).toArray(function(err, docs) {
		var ids = _.map(docs, function(doc) { return doc._id });

		_.forEach(ids, function(id) {
			var index = _.findIndex(self._documents, function(doc) {return doc['_id'] === id;});
			if (index != -1) {
				self._documents.splice(index, 1);
				self.emit("change", {});
			}
		});

		callback(null);
	});
}


Collection.prototype._getDocuments = function(query, callback) {
	var self = this;
	var q = new Kuery(query);
	var documents = q.find(self._documents);
	process.nextTick(function() {
		callback(null, _.cloneDeep(documents));
	});
};

module.exports = Collection;
{'fraction_non_alphanumeric': 0.175594039468385, 'fraction_numerical': 0.002013693113169553, 'mean_word_length': 4.676571428571428, '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': '6313655', 'n_tokens_mistral': 1713, 'n_tokens_neox': 1599, 'n_words': 345}