Thu, 22 Jan 2015 13:21:57 +0100
Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6
michael@0 | 1 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 2 | * License, v. 2.0. If a copy of the MPL was not distributed with this file, |
michael@0 | 3 | * You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 4 | |
michael@0 | 5 | 'use strict'; |
michael@0 | 6 | |
michael@0 | 7 | const {classes: Cc, interfaces: Ci, utils: Cu, results: Cr} = Components; |
michael@0 | 8 | |
michael@0 | 9 | this.EXPORTED_SYMBOLS = ['DataStoreDB']; |
michael@0 | 10 | |
michael@0 | 11 | function debug(s) { |
michael@0 | 12 | //dump('DEBUG DataStoreDB: ' + s + '\n'); |
michael@0 | 13 | } |
michael@0 | 14 | |
michael@0 | 15 | const DATASTOREDB_VERSION = 1; |
michael@0 | 16 | const DATASTOREDB_OBJECTSTORE_NAME = 'DataStoreDB'; |
michael@0 | 17 | const DATASTOREDB_REVISION = 'revision'; |
michael@0 | 18 | const DATASTOREDB_REVISION_INDEX = 'revisionIndex'; |
michael@0 | 19 | |
michael@0 | 20 | Cu.import('resource://gre/modules/IndexedDBHelper.jsm'); |
michael@0 | 21 | Cu.import("resource://gre/modules/XPCOMUtils.jsm"); |
michael@0 | 22 | Cu.importGlobalProperties(["indexedDB"]); |
michael@0 | 23 | |
michael@0 | 24 | XPCOMUtils.defineLazyServiceGetter(this, "uuidgen", |
michael@0 | 25 | "@mozilla.org/uuid-generator;1", |
michael@0 | 26 | "nsIUUIDGenerator"); |
michael@0 | 27 | |
michael@0 | 28 | this.DataStoreDB = function DataStoreDB() {} |
michael@0 | 29 | |
michael@0 | 30 | DataStoreDB.prototype = { |
michael@0 | 31 | |
michael@0 | 32 | __proto__: IndexedDBHelper.prototype, |
michael@0 | 33 | |
michael@0 | 34 | upgradeSchema: function(aTransaction, aDb, aOldVersion, aNewVersion) { |
michael@0 | 35 | debug('updateSchema'); |
michael@0 | 36 | aDb.createObjectStore(DATASTOREDB_OBJECTSTORE_NAME, { autoIncrement: true }); |
michael@0 | 37 | let store = aDb.createObjectStore(DATASTOREDB_REVISION, |
michael@0 | 38 | { autoIncrement: true, |
michael@0 | 39 | keyPath: 'internalRevisionId' }); |
michael@0 | 40 | store.createIndex(DATASTOREDB_REVISION_INDEX, 'revisionId', { unique: true }); |
michael@0 | 41 | }, |
michael@0 | 42 | |
michael@0 | 43 | init: function(aOwner, aName) { |
michael@0 | 44 | let dbName = aName + '|' + aOwner; |
michael@0 | 45 | this.initDBHelper(dbName, DATASTOREDB_VERSION, |
michael@0 | 46 | [DATASTOREDB_OBJECTSTORE_NAME, DATASTOREDB_REVISION]); |
michael@0 | 47 | }, |
michael@0 | 48 | |
michael@0 | 49 | txn: function(aType, aCallback, aErrorCb) { |
michael@0 | 50 | debug('Transaction request'); |
michael@0 | 51 | this.newTxn( |
michael@0 | 52 | aType, |
michael@0 | 53 | aType == 'readonly' |
michael@0 | 54 | ? [ DATASTOREDB_OBJECTSTORE_NAME ] : [ DATASTOREDB_OBJECTSTORE_NAME, DATASTOREDB_REVISION ], |
michael@0 | 55 | function(aTxn, aStores) { |
michael@0 | 56 | aType == 'readonly' ? aCallback(aTxn, aStores[0], null) : aCallback(aTxn, aStores[0], aStores[1]); |
michael@0 | 57 | }, |
michael@0 | 58 | function() {}, |
michael@0 | 59 | aErrorCb |
michael@0 | 60 | ); |
michael@0 | 61 | }, |
michael@0 | 62 | |
michael@0 | 63 | cursorTxn: function(aCallback, aErrorCb) { |
michael@0 | 64 | debug('Cursor transaction request'); |
michael@0 | 65 | this.newTxn( |
michael@0 | 66 | 'readonly', |
michael@0 | 67 | [ DATASTOREDB_OBJECTSTORE_NAME, DATASTOREDB_REVISION ], |
michael@0 | 68 | function(aTxn, aStores) { |
michael@0 | 69 | aCallback(aTxn, aStores[0], aStores[1]); |
michael@0 | 70 | }, |
michael@0 | 71 | function() {}, |
michael@0 | 72 | aErrorCb |
michael@0 | 73 | ); |
michael@0 | 74 | }, |
michael@0 | 75 | |
michael@0 | 76 | revisionTxn: function(aType, aCallback, aErrorCb) { |
michael@0 | 77 | debug("Transaction request"); |
michael@0 | 78 | this.newTxn( |
michael@0 | 79 | aType, |
michael@0 | 80 | DATASTOREDB_REVISION, |
michael@0 | 81 | aCallback, |
michael@0 | 82 | function() {}, |
michael@0 | 83 | aErrorCb |
michael@0 | 84 | ); |
michael@0 | 85 | }, |
michael@0 | 86 | |
michael@0 | 87 | addRevision: function(aStore, aKey, aType, aSuccessCb) { |
michael@0 | 88 | debug("AddRevision: " + aKey + " - " + aType); |
michael@0 | 89 | let revisionId = uuidgen.generateUUID().toString(); |
michael@0 | 90 | let request = aStore.put({ revisionId: revisionId, objectId: aKey, operation: aType }); |
michael@0 | 91 | request.onsuccess = function() { |
michael@0 | 92 | aSuccessCb(revisionId); |
michael@0 | 93 | } |
michael@0 | 94 | }, |
michael@0 | 95 | |
michael@0 | 96 | getInternalRevisionId: function(aRevisionId, aStore, aSuccessCb) { |
michael@0 | 97 | debug('GetInternalRevisionId'); |
michael@0 | 98 | let request = aStore.index(DATASTOREDB_REVISION_INDEX).getKey(aRevisionId); |
michael@0 | 99 | request.onsuccess = function(aEvent) { |
michael@0 | 100 | aSuccessCb(aEvent.target.result); |
michael@0 | 101 | } |
michael@0 | 102 | }, |
michael@0 | 103 | |
michael@0 | 104 | clearRevisions: function(aStore, aSuccessCb) { |
michael@0 | 105 | debug("ClearRevisions"); |
michael@0 | 106 | let request = aStore.clear(); |
michael@0 | 107 | request.onsuccess = function() { |
michael@0 | 108 | aSuccessCb(); |
michael@0 | 109 | } |
michael@0 | 110 | }, |
michael@0 | 111 | |
michael@0 | 112 | delete: function() { |
michael@0 | 113 | debug('delete'); |
michael@0 | 114 | this.close(); |
michael@0 | 115 | indexedDB.deleteDatabase(this.dbName); |
michael@0 | 116 | debug('database deleted'); |
michael@0 | 117 | } |
michael@0 | 118 | } |