<commit_before>/*
 * IDBWrapper - A cross-browser wrapper for IndexedDB
 * Copyright (c) 2011 <NAME>
 * http://jensarps.de/
 *
 * Licensed under the MIT (X11) license
 */

(function(name, definition, global){
	if (typeof define === 'function'){
		define(definition);
	} else {
		global[name] = definition();
	}
})('IDBStore', function(){

	var IDBStore;
	
	var defaults = {
		dbName: 'IDB',
		storeName: 'Store',
		dbVersion: '1.0',
		keyPath: 'id',
		autoIncrement: true,
		onStoreReady: function(){}
	};
	
	IDBStore = function(kwArgs, onStoreReady){
		mixin(this, defaults);
		mixin(this, kwArgs);
		onStoreReady && (this.onStoreReady = onStoreReady);
		this.idb = window.indexedDB || window.webkitIndexedDB || window.mozIndexedDB || window.msIndexedDB;
		this.consts = window.IDBTransaction || window.webkitIDBTransaction || window.msIndexedDB;
		this.openDB();
	};
	
	IDBStore.prototype = {
		
		db: null,
		
		dbName: null,
		
		dbDescription: null,
		
		dbVersion: null,
		
		store: null,
		
		storeName: null,
		
		keyPath: null,
		
		autoIncrement: null,
		
		features: null,
		
		onStoreReady: null,
		
		openDB: function(){
			var openRequest = this.idb.open(this.dbName, this.dbDescription);
			openRequest.onerror = function(error){ console.error('Could not open database.', error); };
			openRequest.onsuccess = hitch(this, function(event){
				this.db = event.target.result;
				this.checkVersion(hitch(this, function(){
					this.getObjectStore(hitch(this, function(){
						this.testFeatures(this.onStoreReady)
					}));
				}));
			});
		},
		
		testFeatures: function(callback){
			var features = this.features = {};
			
			// In Chrome, there's no getAll method (getAll is not part of the spec, but handy)
			var getAllTransaction = this.db.transaction([this.storeName], this.consts.READ_ONLY);
			features.hasGetAll = !!getAllTransaction.objectStore(this.storeName).getAll;
			getAllTransaction.abort();
			
			// In FF, autoIncrement doesn't work.
			// We won't test for that, as testing
			// sometimes fails in Chrome (it's a long
			// story).
			features.hasAutoIncrement = !window.mozIndexedDB;
			
			callback && callback();
		},


		/**************
		 * versioning *
		 **************/
		
		checkVersion: function(onSuccess, onError){
			if(this.getVersion() != this.dbVersion){
				this.setVersion(onSuccess, onError);
			}else{
				onSuccess && onSuccess();
			}
		},
		
		getVersion: function(){
			return this.db.version;
		},
		
		setVersion: function(onSuccess, onError){
			onError || (onError = function(error){ console.error('Failed to set version.', error); });
			var versionRequest = this.db.setVersion(this.dbVersion);
			versionRequest.onerror = onError;
			versionRequest.onblocked = onError;
			versionRequest.onsuccess = onSuccess;
		},


		/*************************
		 * object store handling *
		 *************************/ 


		getObjectStore: function(onSuccess, onError){
			if(this.hasObjectStore()){
				this.openExistingObjectStore(onSuccess, onError);
			}else{
				this.createNewObjectStore(onSuccess, onError);
			}
		},
		
		hasObjectStore: function(){
			return this.db.objectStoreNames.contains(this.storeName);
		},
		
		createNewObjectStore: function(onSuccess, onError){
			this.setVersion(hitch(this, function(){
				this.store = this.db.createObjectStore(this.storeName, { keyPath: this.keyPath, autoIncrement: this.autoIncrement});
				onSuccess && onSuccess(this.store);
			}), onError);
		},
		
		openExistingObjectStore: function(onSuccess, onError){
			var emptyTransaction = this.db.transaction([], this.consts.READ_ONLY, 0);
			this.store = emptyTransaction.objectStore(this.storeName);
			onSuccess && onSuccess(this.store);
		},
		
		deleteObjectStore: function(onSuccess, onError){
			onError || (onError = function(error){ console.error('Failed to delete objectStore.', error); });
			this.setVersion(hitch(this, function(){
				this.db.deleteObjectStore(this.storeName);
				var success = !this.hasObjectStore();
				onSuccess && success && onSuccess();
				onError && !success && onError();
			}), onError);
		},


		/*********************
		 * data manipulation *
		 *********************/


		put: function(dataObj, onSuccess, onError){
			onError || (onError = function(error) { console.error('Could not write data.', error); });
			onSuccess || (onSuccess = noop);
			if(typeof dataObj[this.keyPath] == 'undefined' && !this.features.hasAutoIncrement){
				dataObj[this.keyPath] = this._getUID();
			}
			var putTransaction = this.db.transaction([this.storeName], this.consts.READ_WRITE, 1000);
			var putRequest = putTransaction.objectStore(this.storeName).put(dataObj);
			putRequest.onsuccess = function(event){ onSuccess(event.target.result); };
			putRequest.onerror = onError;
		},
		
		get: function(key, onSuccess, onError){
			onError || (onError = function(error) { console.error('Could not read data.', error); });
			onSuccess || (onSuccess = noop);
			var getTransaction = this.db.transaction([this.storeName], this.consts.READ_ONLY, 1000);
			var getRequest = getTransaction.objectStore(this.storeName).get(key);
			getRequest.onsuccess = function(event){ onSuccess(event.target.result); };
			getRequest.onerror = onError;
		},
		
		remove: function(key, onSuccess, onError){
			onError || (onError = function(error) { console.error('Could not remove data.', error); });
			onSuccess || (onSuccess = noop);
			var removeTransaction = this.db.transaction([this.storeName], this.consts.READ_WRITE);
			var deleteRequest = removeTransaction.objectStore(this.storeName).delete(key);
			deleteRequest.onsuccess = function(event){ onSuccess(event.target.result); };
			deleteRequest.onerror = onError;
		},
		
		getAll: function(onSuccess, onError){
			onError || (onError = function(error) { console.error('Could not read data.', error); });
			onSuccess || (onSuccess = noop);
			var getAllTransaction = this.db.transaction([this.storeName], this.consts.READ_ONLY);
			if(this.features.hasGetAll){<commit_msg>Timeouts have been removed from spec.<commit_after>/*
 * IDBWrapper - A cross-browser wrapper for IndexedDB
 * Copyright (c) 2011 <NAME>
 * http://jensarps.de/
 *
 * Licensed under the MIT (X11) license
 */

(function(name, definition, global){
	if (typeof define === 'function'){
		define(definition);
	} else {
		global[name] = definition();
	}
})('IDBStore', function(){

	var IDBStore;
	
	var defaults = {
		dbName: 'IDB',
		storeName: 'Store',
		dbVersion: '1.0',
		keyPath: 'id',
		autoIncrement: true,
		onStoreReady: function(){}
	};
	
	IDBStore = function(kwArgs, onStoreReady){
		mixin(this, defaults);
		mixin(this, kwArgs);
		onStoreReady && (this.onStoreReady = onStoreReady);
		this.idb = window.indexedDB || window.webkitIndexedDB || window.mozIndexedDB || window.msIndexedDB;
		this.consts = window.IDBTransaction || window.webkitIDBTransaction || window.msIndexedDB;
		this.openDB();
	};
	
	IDBStore.prototype = {
		
		db: null,
		
		dbName: null,
		
		dbDescription: null,
		
		dbVersion: null,
		
		store: null,
		
		storeName: null,
		
		keyPath: null,
		
		autoIncrement: null,
		
		features: null,
		
		onStoreReady: null,
		
		openDB: function(){
			var openRequest = this.idb.open(this.dbName, this.dbDescription);
			openRequest.onerror = function(error){ console.error('Could not open database.', error); };
			openRequest.onsuccess = hitch(this, function(event){
				this.db = event.target.result;
				this.checkVersion(hitch(this, function(){
					this.getObjectStore(hitch(this, function(){
						this.testFeatures(this.onStoreReady)
					}));
				}));
			});
		},
		
		testFeatures: function(callback){
			var features = this.features = {};
			
			// In Chrome, there's no getAll method (getAll is not part of the spec, but handy)
			var getAllTransaction = this.db.transaction([this.storeName], this.consts.READ_ONLY);
			features.hasGetAll = !!getAllTransaction.objectStore(this.storeName).getAll;
			getAllTransaction.abort();
			
			// In FF, autoIncrement doesn't work.
			// We won't test for that, as testing
			// sometimes fails in Chrome (it's a long
			// story).
			features.hasAutoIncrement = !window.mozIndexedDB;
			
			callback && callback();
		},


		/**************
		 * versioning *
		 **************/
		
		checkVersion: function(onSuccess, onError){
			if(this.getVersion() != this.dbVersion){
				this.setVersion(onSuccess, onError);
			}else{
				onSuccess && onSuccess();
			}
		},
		
		getVersion: function(){
			return this.db.version;
		},
		
		setVersion: function(onSuccess, onError){
			onError || (onError = function(error){ console.error('Failed to set version.', error); });
			var versionRequest = this.db.setVersion(this.dbVersion);
			versionRequest.onerror = onError;
			versionRequest.onblocked = onError;
			versionRequest.onsuccess = onSuccess;
		},


		/*************************
		 * object store handling *
		 *************************/ 


		getObjectStore: function(onSuccess, onError){
			if(this.hasObjectStore()){
				this.openExistingObjectStore(onSuccess, onError);
			}else{
				this.createNewObjectStore(onSuccess, onError);
			}
		},
		
		hasObjectStore: function(){
			return this.db.objectStoreNames.contains(this.storeName);
		},
		
		createNewObjectStore: function(onSuccess, onError){
			this.setVersion(hitch(this, function(){
				this.store = this.db.createObjectStore(this.storeName, { keyPath: this.keyPath, autoIncrement: this.autoIncrement});
				onSuccess && onSuccess(this.store);
			}), onError);
		},
		
		openExistingObjectStore: function(onSuccess, onError){
			var emptyTransaction = this.db.transaction([], this.consts.READ_ONLY);
			this.store = emptyTransaction.objectStore(this.storeName);
			onSuccess && onSuccess(this.store);
		},
		
		deleteObjectStore: function(onSuccess, onError){
			onError || (onError = function(error){ console.error('Failed to delete objectStore.', error); });
			this.setVersion(hitch(this, function(){
				this.db.deleteObjectStore(this.storeName);
				var success = !this.hasObjectStore();
				onSuccess && success && onSuccess();
				onError && !success && onError();
			}), onError);
		},


		/*********************
		 * data manipulation *
		 *********************/


		put: function(dataObj, onSuccess, onError){
			onError || (onError = function(error) { console.error('Could not write data.', error); });
			onSuccess || (onSuccess = noop);
			if(typeof dataObj[this.keyPath] == 'undefined' && !this.features.hasAutoIncrement){
				dataObj[this.keyPath] = this._getUID();
			}
			var putTransaction = this.db.transaction([this.storeName], this.consts.READ_WRITE);
			var putRequest = putTransaction.objectStore(this.storeName).put(dataObj);
			putRequest.onsuccess = function(event){ onSuccess(event.target.result); };
			putRequest.onerror = onError;
		},
		
		get: function(key, onSuccess, onError){
			onError || (onError = function(error) { console.error('Could not read data.', error); });
			onSuccess || (onSuccess = noop);
			var getTransaction = this.db.transaction([this.storeName], this.consts.READ_ONLY);
			var getRequest = getTransaction.objectStore(this.storeName).get(key);
			getRequest.onsuccess = function(event){ onSuccess(event.target.result); };
			getRequest.onerror = onError;
		},
		
		remove: function(key, onSuccess, onError){
			onError || (onError = function(error) { console.error('Could not remove data.', error); });
			onSuccess || (onSuccess = noop);
			var removeTransaction = this.db.transaction([this.storeName], this.consts.READ_WRITE);
			var deleteRequest = removeTransaction.objectStore(this.storeName).delete(key);
			deleteRequest.onsuccess = function(event){ onSuccess(event.target.result); };
			deleteRequest.onerror = onError;
		},
		
		getAll: function(onSuccess, onError){
			onError || (onError = function(error) { console.error('Could not read data.', error); });
			onSuccess || (onSuccess = noop);
			var getAllTransaction = this.db.transaction([this.storeName], this.consts.READ_ONLY);
			if(this.features.hasGetAll){
{'fraction_non_alphanumeric': 0.1608257720802464, 'fraction_numerical': 0.0020810788312661285, 'mean_word_length': 4.116695059625213, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, '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': '853531', 'n_tokens_mistral': 4370, 'n_tokens_neox': 3674, 'n_words': 747}