1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/dom/datastore/DataStoreDB.jsm Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,118 @@ 1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this file, 1.6 + * You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.7 + 1.8 +'use strict'; 1.9 + 1.10 +const {classes: Cc, interfaces: Ci, utils: Cu, results: Cr} = Components; 1.11 + 1.12 +this.EXPORTED_SYMBOLS = ['DataStoreDB']; 1.13 + 1.14 +function debug(s) { 1.15 + //dump('DEBUG DataStoreDB: ' + s + '\n'); 1.16 +} 1.17 + 1.18 +const DATASTOREDB_VERSION = 1; 1.19 +const DATASTOREDB_OBJECTSTORE_NAME = 'DataStoreDB'; 1.20 +const DATASTOREDB_REVISION = 'revision'; 1.21 +const DATASTOREDB_REVISION_INDEX = 'revisionIndex'; 1.22 + 1.23 +Cu.import('resource://gre/modules/IndexedDBHelper.jsm'); 1.24 +Cu.import("resource://gre/modules/XPCOMUtils.jsm"); 1.25 +Cu.importGlobalProperties(["indexedDB"]); 1.26 + 1.27 +XPCOMUtils.defineLazyServiceGetter(this, "uuidgen", 1.28 + "@mozilla.org/uuid-generator;1", 1.29 + "nsIUUIDGenerator"); 1.30 + 1.31 +this.DataStoreDB = function DataStoreDB() {} 1.32 + 1.33 +DataStoreDB.prototype = { 1.34 + 1.35 + __proto__: IndexedDBHelper.prototype, 1.36 + 1.37 + upgradeSchema: function(aTransaction, aDb, aOldVersion, aNewVersion) { 1.38 + debug('updateSchema'); 1.39 + aDb.createObjectStore(DATASTOREDB_OBJECTSTORE_NAME, { autoIncrement: true }); 1.40 + let store = aDb.createObjectStore(DATASTOREDB_REVISION, 1.41 + { autoIncrement: true, 1.42 + keyPath: 'internalRevisionId' }); 1.43 + store.createIndex(DATASTOREDB_REVISION_INDEX, 'revisionId', { unique: true }); 1.44 + }, 1.45 + 1.46 + init: function(aOwner, aName) { 1.47 + let dbName = aName + '|' + aOwner; 1.48 + this.initDBHelper(dbName, DATASTOREDB_VERSION, 1.49 + [DATASTOREDB_OBJECTSTORE_NAME, DATASTOREDB_REVISION]); 1.50 + }, 1.51 + 1.52 + txn: function(aType, aCallback, aErrorCb) { 1.53 + debug('Transaction request'); 1.54 + this.newTxn( 1.55 + aType, 1.56 + aType == 'readonly' 1.57 + ? [ DATASTOREDB_OBJECTSTORE_NAME ] : [ DATASTOREDB_OBJECTSTORE_NAME, DATASTOREDB_REVISION ], 1.58 + function(aTxn, aStores) { 1.59 + aType == 'readonly' ? aCallback(aTxn, aStores[0], null) : aCallback(aTxn, aStores[0], aStores[1]); 1.60 + }, 1.61 + function() {}, 1.62 + aErrorCb 1.63 + ); 1.64 + }, 1.65 + 1.66 + cursorTxn: function(aCallback, aErrorCb) { 1.67 + debug('Cursor transaction request'); 1.68 + this.newTxn( 1.69 + 'readonly', 1.70 + [ DATASTOREDB_OBJECTSTORE_NAME, DATASTOREDB_REVISION ], 1.71 + function(aTxn, aStores) { 1.72 + aCallback(aTxn, aStores[0], aStores[1]); 1.73 + }, 1.74 + function() {}, 1.75 + aErrorCb 1.76 + ); 1.77 + }, 1.78 + 1.79 + revisionTxn: function(aType, aCallback, aErrorCb) { 1.80 + debug("Transaction request"); 1.81 + this.newTxn( 1.82 + aType, 1.83 + DATASTOREDB_REVISION, 1.84 + aCallback, 1.85 + function() {}, 1.86 + aErrorCb 1.87 + ); 1.88 + }, 1.89 + 1.90 + addRevision: function(aStore, aKey, aType, aSuccessCb) { 1.91 + debug("AddRevision: " + aKey + " - " + aType); 1.92 + let revisionId = uuidgen.generateUUID().toString(); 1.93 + let request = aStore.put({ revisionId: revisionId, objectId: aKey, operation: aType }); 1.94 + request.onsuccess = function() { 1.95 + aSuccessCb(revisionId); 1.96 + } 1.97 + }, 1.98 + 1.99 + getInternalRevisionId: function(aRevisionId, aStore, aSuccessCb) { 1.100 + debug('GetInternalRevisionId'); 1.101 + let request = aStore.index(DATASTOREDB_REVISION_INDEX).getKey(aRevisionId); 1.102 + request.onsuccess = function(aEvent) { 1.103 + aSuccessCb(aEvent.target.result); 1.104 + } 1.105 + }, 1.106 + 1.107 + clearRevisions: function(aStore, aSuccessCb) { 1.108 + debug("ClearRevisions"); 1.109 + let request = aStore.clear(); 1.110 + request.onsuccess = function() { 1.111 + aSuccessCb(); 1.112 + } 1.113 + }, 1.114 + 1.115 + delete: function() { 1.116 + debug('delete'); 1.117 + this.close(); 1.118 + indexedDB.deleteDatabase(this.dbName); 1.119 + debug('database deleted'); 1.120 + } 1.121 +}