michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this file, michael@0: * You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: 'use strict'; michael@0: michael@0: const {classes: Cc, interfaces: Ci, utils: Cu, results: Cr} = Components; michael@0: michael@0: this.EXPORTED_SYMBOLS = ['DataStoreDB']; michael@0: michael@0: function debug(s) { michael@0: //dump('DEBUG DataStoreDB: ' + s + '\n'); michael@0: } michael@0: michael@0: const DATASTOREDB_VERSION = 1; michael@0: const DATASTOREDB_OBJECTSTORE_NAME = 'DataStoreDB'; michael@0: const DATASTOREDB_REVISION = 'revision'; michael@0: const DATASTOREDB_REVISION_INDEX = 'revisionIndex'; michael@0: michael@0: Cu.import('resource://gre/modules/IndexedDBHelper.jsm'); michael@0: Cu.import("resource://gre/modules/XPCOMUtils.jsm"); michael@0: Cu.importGlobalProperties(["indexedDB"]); michael@0: michael@0: XPCOMUtils.defineLazyServiceGetter(this, "uuidgen", michael@0: "@mozilla.org/uuid-generator;1", michael@0: "nsIUUIDGenerator"); michael@0: michael@0: this.DataStoreDB = function DataStoreDB() {} michael@0: michael@0: DataStoreDB.prototype = { michael@0: michael@0: __proto__: IndexedDBHelper.prototype, michael@0: michael@0: upgradeSchema: function(aTransaction, aDb, aOldVersion, aNewVersion) { michael@0: debug('updateSchema'); michael@0: aDb.createObjectStore(DATASTOREDB_OBJECTSTORE_NAME, { autoIncrement: true }); michael@0: let store = aDb.createObjectStore(DATASTOREDB_REVISION, michael@0: { autoIncrement: true, michael@0: keyPath: 'internalRevisionId' }); michael@0: store.createIndex(DATASTOREDB_REVISION_INDEX, 'revisionId', { unique: true }); michael@0: }, michael@0: michael@0: init: function(aOwner, aName) { michael@0: let dbName = aName + '|' + aOwner; michael@0: this.initDBHelper(dbName, DATASTOREDB_VERSION, michael@0: [DATASTOREDB_OBJECTSTORE_NAME, DATASTOREDB_REVISION]); michael@0: }, michael@0: michael@0: txn: function(aType, aCallback, aErrorCb) { michael@0: debug('Transaction request'); michael@0: this.newTxn( michael@0: aType, michael@0: aType == 'readonly' michael@0: ? [ DATASTOREDB_OBJECTSTORE_NAME ] : [ DATASTOREDB_OBJECTSTORE_NAME, DATASTOREDB_REVISION ], michael@0: function(aTxn, aStores) { michael@0: aType == 'readonly' ? aCallback(aTxn, aStores[0], null) : aCallback(aTxn, aStores[0], aStores[1]); michael@0: }, michael@0: function() {}, michael@0: aErrorCb michael@0: ); michael@0: }, michael@0: michael@0: cursorTxn: function(aCallback, aErrorCb) { michael@0: debug('Cursor transaction request'); michael@0: this.newTxn( michael@0: 'readonly', michael@0: [ DATASTOREDB_OBJECTSTORE_NAME, DATASTOREDB_REVISION ], michael@0: function(aTxn, aStores) { michael@0: aCallback(aTxn, aStores[0], aStores[1]); michael@0: }, michael@0: function() {}, michael@0: aErrorCb michael@0: ); michael@0: }, michael@0: michael@0: revisionTxn: function(aType, aCallback, aErrorCb) { michael@0: debug("Transaction request"); michael@0: this.newTxn( michael@0: aType, michael@0: DATASTOREDB_REVISION, michael@0: aCallback, michael@0: function() {}, michael@0: aErrorCb michael@0: ); michael@0: }, michael@0: michael@0: addRevision: function(aStore, aKey, aType, aSuccessCb) { michael@0: debug("AddRevision: " + aKey + " - " + aType); michael@0: let revisionId = uuidgen.generateUUID().toString(); michael@0: let request = aStore.put({ revisionId: revisionId, objectId: aKey, operation: aType }); michael@0: request.onsuccess = function() { michael@0: aSuccessCb(revisionId); michael@0: } michael@0: }, michael@0: michael@0: getInternalRevisionId: function(aRevisionId, aStore, aSuccessCb) { michael@0: debug('GetInternalRevisionId'); michael@0: let request = aStore.index(DATASTOREDB_REVISION_INDEX).getKey(aRevisionId); michael@0: request.onsuccess = function(aEvent) { michael@0: aSuccessCb(aEvent.target.result); michael@0: } michael@0: }, michael@0: michael@0: clearRevisions: function(aStore, aSuccessCb) { michael@0: debug("ClearRevisions"); michael@0: let request = aStore.clear(); michael@0: request.onsuccess = function() { michael@0: aSuccessCb(); michael@0: } michael@0: }, michael@0: michael@0: delete: function() { michael@0: debug('delete'); michael@0: this.close(); michael@0: indexedDB.deleteDatabase(this.dbName); michael@0: debug('database deleted'); michael@0: } michael@0: }