<commit_before>var _ = require('lodash');
var Promise = require('bluebird');
var ConcurrencyError = require('tapeworm').ConcurrencyError;
var DuplicateCommitError = require('tapeworm').DuplicateCommitError;

var IdbPartition = function(idb, partitionId) {
	this._persistedCommits = null;
	this._streamIndex = {};
	this._commitIds = [];
	this._commitConcurrencyCheck = [];
	this._idb = idb;
	this._partitionId = partitionId;
}

IdbPartition.prototype._promisify = function(value, callback) {
	return Promise.resolve(value).nodeify(callback);
};

IdbPartition.prototype.open = function() {
	var self = this;
	var request = this._idb.open('tapeworm_'+this._partitionId, 1);
	return new Promise(function(resolve, reject) {
		request.onsuccess = function(event) {
			self._db = event.target.result;
			resolve(self);
		}
		request.onupgradeneeded = function(event) {
			var db = event.target.result;
			self._persistedCommits = db.createObjectStore('commits', {keyPath: 'id', unique: true});
			self._persistedCommits.createIndex('streamId', 'streamId', {unique:false});
			self._persistedCommits.createIndex('streamIdCommitSequence', 'streamIdCommitSequence', {unique:true});
			//db.createObjectStore('commits', {keyPath: 'id'});
		}
		request.onerror = function(event) {
			reject(new Error(event));
		}
		request.onblocked = function(event) {
			reject(new Error(event));
		}
	});
}

IdbPartition.prototype.append = function(commit, callback) {
	var self = this;
	commit.isDispatched = false;
	commit.streamIdCommitSequence = commit.streamId + commit.commitSequence;

	console.log(commit.streamIdCommitSequence);
	//check for duplicates
/*	if(_.contains(this._commitIds, commit.id)) {
		throw new DuplicateCommitError("Duplicate commit of " + commit.id);
	}
	var concurrencyKey = commit.streamId+'-'+commit.commitSequence;
	if(_.contains(this._commitConcurrencyCheck, concurrencyKey)) {
		throw new ConcurrencyError('Concurrency error on stream ' + commit.streamId);
	}
*/	
	//check concurrency
	return new Promise(function(resolve, reject) {

		var txn = self._db.transaction(['commits'], 'readwrite');
		var commits = txn.objectStore('commits');

		txn.oncomplete = txn.onsuccess = function() {
			console.log('txn complete');
		}

		txn.onerror = function(event) {
			console.log(event);
			reject(new DuplicateCommitError(event));
		}

		var request = commits.add(commit);
		request.onsuccess = function() {
			console.log('transaction added');
			console.log(commit);
			process.nextTick(function() {
				resolve(commit)
			});
		};
		request.onerror = function(event) {
			console.log('error');
			console.log(error);
			reject(new Error(event));
		};
	});

/*	this._commits.push(commit);
	this._commitIds.push(commit.id);
	this._commitConcurrencyCheck.push(concurrencyKey);
	var index = this._streamIndex[commit.streamId];
	if(!index) {<commit_msg>fixed bug in error handler<commit_after>var _ = require('lodash');
var Promise = require('bluebird');
var ConcurrencyError = require('tapeworm').ConcurrencyError;
var DuplicateCommitError = require('tapeworm').DuplicateCommitError;

var IdbPartition = function(idb, partitionId) {
	this._persistedCommits = null;
	this._streamIndex = {};
	this._commitIds = [];
	this._commitConcurrencyCheck = [];
	this._idb = idb;
	this._partitionId = partitionId;
}

IdbPartition.prototype._promisify = function(value, callback) {
	return Promise.resolve(value).nodeify(callback);
};

IdbPartition.prototype.open = function() {
	var self = this;
	var request = this._idb.open('tapeworm_'+this._partitionId, 1);
	return new Promise(function(resolve, reject) {
		request.onsuccess = function(event) {
			self._db = event.target.result;
			resolve(self);
		}
		request.onupgradeneeded = function(event) {
			var db = event.target.result;
			self._persistedCommits = db.createObjectStore('commits', {keyPath: 'id', unique: true});
			self._persistedCommits.createIndex('streamId', 'streamId', {unique:false});
			self._persistedCommits.createIndex('streamIdCommitSequence', 'streamIdCommitSequence', {unique:true});
			//db.createObjectStore('commits', {keyPath: 'id'});
		}
		request.onerror = function(event) {
			reject(new Error(event));
		}
		request.onblocked = function(event) {
			reject(new Error(event));
		}
	});
}

IdbPartition.prototype.append = function(commit, callback) {
	var self = this;
	commit.isDispatched = false;
	commit.streamIdCommitSequence = commit.streamId + commit.commitSequence;

	console.log(commit.streamIdCommitSequence);
	//check for duplicates
/*	if(_.contains(this._commitIds, commit.id)) {
		throw new DuplicateCommitError("Duplicate commit of " + commit.id);
	}
	var concurrencyKey = commit.streamId+'-'+commit.commitSequence;
	if(_.contains(this._commitConcurrencyCheck, concurrencyKey)) {
		throw new ConcurrencyError('Concurrency error on stream ' + commit.streamId);
	}
*/	
	//check concurrency
	return new Promise(function(resolve, reject) {

		var txn = self._db.transaction(['commits'], 'readwrite');
		var commits = txn.objectStore('commits');

		txn.oncomplete = txn.onsuccess = function() {
			console.log('txn complete');
		}

		txn.onerror = function(event) {
			console.log(event);
			reject(new DuplicateCommitError(event));
		}

		var request = commits.add(commit);
		request.onsuccess = function() {
			console.log('transaction added');
			console.log(commit);
			process.nextTick(function() {
				resolve(commit)
			});
		};
		request.onerror = function(event) {
			console.log('error');
			console.log(event);
			reject(new Error(event));
		};
	});

/*	this._commits.push(commit);
	this._commitIds.push(commit.id);
	this._commitConcurrencyCheck.push(concurrencyKey);
	var index = this._streamIndex[commit.streamId];
	if(!index) {
{'fraction_non_alphanumeric': 0.1464778884810348, 'fraction_numerical': 0.0003495892326516343, 'mean_word_length': 5.146079484425349, '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': '4852003', 'n_tokens_mistral': 1973, 'n_tokens_neox': 1834, 'n_words': 316}