1 /* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
3 * You can obtain one at http://mozilla.org/MPL/2.0/. */
7 const {classes: Cc, interfaces: Ci, utils: Cu, results: Cr} = Components;
9 this.EXPORTED_SYMBOLS = ['DataStoreDB'];
12 //dump('DEBUG DataStoreDB: ' + s + '\n');
15 const DATASTOREDB_VERSION = 1;
16 const DATASTOREDB_OBJECTSTORE_NAME = 'DataStoreDB';
17 const DATASTOREDB_REVISION = 'revision';
18 const DATASTOREDB_REVISION_INDEX = 'revisionIndex';
20 Cu.import('resource://gre/modules/IndexedDBHelper.jsm');
21 Cu.import("resource://gre/modules/XPCOMUtils.jsm");
22 Cu.importGlobalProperties(["indexedDB"]);
24 XPCOMUtils.defineLazyServiceGetter(this, "uuidgen",
25 "@mozilla.org/uuid-generator;1",
28 this.DataStoreDB = function DataStoreDB() {}
30 DataStoreDB.prototype = {
32 __proto__: IndexedDBHelper.prototype,
34 upgradeSchema: function(aTransaction, aDb, aOldVersion, aNewVersion) {
35 debug('updateSchema');
36 aDb.createObjectStore(DATASTOREDB_OBJECTSTORE_NAME, { autoIncrement: true });
37 let store = aDb.createObjectStore(DATASTOREDB_REVISION,
38 { autoIncrement: true,
39 keyPath: 'internalRevisionId' });
40 store.createIndex(DATASTOREDB_REVISION_INDEX, 'revisionId', { unique: true });
43 init: function(aOwner, aName) {
44 let dbName = aName + '|' + aOwner;
45 this.initDBHelper(dbName, DATASTOREDB_VERSION,
46 [DATASTOREDB_OBJECTSTORE_NAME, DATASTOREDB_REVISION]);
49 txn: function(aType, aCallback, aErrorCb) {
50 debug('Transaction request');
54 ? [ DATASTOREDB_OBJECTSTORE_NAME ] : [ DATASTOREDB_OBJECTSTORE_NAME, DATASTOREDB_REVISION ],
55 function(aTxn, aStores) {
56 aType == 'readonly' ? aCallback(aTxn, aStores[0], null) : aCallback(aTxn, aStores[0], aStores[1]);
63 cursorTxn: function(aCallback, aErrorCb) {
64 debug('Cursor transaction request');
67 [ DATASTOREDB_OBJECTSTORE_NAME, DATASTOREDB_REVISION ],
68 function(aTxn, aStores) {
69 aCallback(aTxn, aStores[0], aStores[1]);
76 revisionTxn: function(aType, aCallback, aErrorCb) {
77 debug("Transaction request");
87 addRevision: function(aStore, aKey, aType, aSuccessCb) {
88 debug("AddRevision: " + aKey + " - " + aType);
89 let revisionId = uuidgen.generateUUID().toString();
90 let request = aStore.put({ revisionId: revisionId, objectId: aKey, operation: aType });
91 request.onsuccess = function() {
92 aSuccessCb(revisionId);
96 getInternalRevisionId: function(aRevisionId, aStore, aSuccessCb) {
97 debug('GetInternalRevisionId');
98 let request = aStore.index(DATASTOREDB_REVISION_INDEX).getKey(aRevisionId);
99 request.onsuccess = function(aEvent) {
100 aSuccessCb(aEvent.target.result);
104 clearRevisions: function(aStore, aSuccessCb) {
105 debug("ClearRevisions");
106 let request = aStore.clear();
107 request.onsuccess = function() {
115 indexedDB.deleteDatabase(this.dbName);
116 debug('database deleted');