dom/datastore/DataStoreDB.jsm

branch
TOR_BUG_3246
changeset 7
129ffea94266
equal deleted inserted replaced
-1:000000000000 0:357ddd76651b
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/. */
4
5 'use strict';
6
7 const {classes: Cc, interfaces: Ci, utils: Cu, results: Cr} = Components;
8
9 this.EXPORTED_SYMBOLS = ['DataStoreDB'];
10
11 function debug(s) {
12 //dump('DEBUG DataStoreDB: ' + s + '\n');
13 }
14
15 const DATASTOREDB_VERSION = 1;
16 const DATASTOREDB_OBJECTSTORE_NAME = 'DataStoreDB';
17 const DATASTOREDB_REVISION = 'revision';
18 const DATASTOREDB_REVISION_INDEX = 'revisionIndex';
19
20 Cu.import('resource://gre/modules/IndexedDBHelper.jsm');
21 Cu.import("resource://gre/modules/XPCOMUtils.jsm");
22 Cu.importGlobalProperties(["indexedDB"]);
23
24 XPCOMUtils.defineLazyServiceGetter(this, "uuidgen",
25 "@mozilla.org/uuid-generator;1",
26 "nsIUUIDGenerator");
27
28 this.DataStoreDB = function DataStoreDB() {}
29
30 DataStoreDB.prototype = {
31
32 __proto__: IndexedDBHelper.prototype,
33
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 });
41 },
42
43 init: function(aOwner, aName) {
44 let dbName = aName + '|' + aOwner;
45 this.initDBHelper(dbName, DATASTOREDB_VERSION,
46 [DATASTOREDB_OBJECTSTORE_NAME, DATASTOREDB_REVISION]);
47 },
48
49 txn: function(aType, aCallback, aErrorCb) {
50 debug('Transaction request');
51 this.newTxn(
52 aType,
53 aType == 'readonly'
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]);
57 },
58 function() {},
59 aErrorCb
60 );
61 },
62
63 cursorTxn: function(aCallback, aErrorCb) {
64 debug('Cursor transaction request');
65 this.newTxn(
66 'readonly',
67 [ DATASTOREDB_OBJECTSTORE_NAME, DATASTOREDB_REVISION ],
68 function(aTxn, aStores) {
69 aCallback(aTxn, aStores[0], aStores[1]);
70 },
71 function() {},
72 aErrorCb
73 );
74 },
75
76 revisionTxn: function(aType, aCallback, aErrorCb) {
77 debug("Transaction request");
78 this.newTxn(
79 aType,
80 DATASTOREDB_REVISION,
81 aCallback,
82 function() {},
83 aErrorCb
84 );
85 },
86
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);
93 }
94 },
95
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);
101 }
102 },
103
104 clearRevisions: function(aStore, aSuccessCb) {
105 debug("ClearRevisions");
106 let request = aStore.clear();
107 request.onsuccess = function() {
108 aSuccessCb();
109 }
110 },
111
112 delete: function() {
113 debug('delete');
114 this.close();
115 indexedDB.deleteDatabase(this.dbName);
116 debug('database deleted');
117 }
118 }

mercurial