OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 window.indexedDB = window.indexedDB || window.webkitIndexedDB || | 5 window.indexedDB = window.indexedDB || window.webkitIndexedDB || |
6 window.mozIndexedDB || window.msIndexedDB; | 6 window.mozIndexedDB || window.msIndexedDB; |
7 | 7 |
8 var automation = { | 8 var automation = { |
9 results: {} | 9 results: {} |
10 }; | 10 }; |
(...skipping 27 matching lines...) Expand all Loading... | |
38 throw e; | 38 throw e; |
39 } | 39 } |
40 } | 40 } |
41 | 41 |
42 function onError(e) { | 42 function onError(e) { |
43 console.log(e); | 43 console.log(e); |
44 e.stopPropagation(); | 44 e.stopPropagation(); |
45 throw new Error(e); | 45 throw new Error(e); |
46 } | 46 } |
47 | 47 |
48 var version = 2; // The version with our object stores. | 48 var baseVersion = 2; // The version with our object stores. |
49 var db; | 49 var curVersion; |
50 | 50 |
51 // Valid options fields: | 51 // Valid options fields: |
52 // indexName: the name of an index to create on each object store | 52 // indexName: the name of an index to create on each object store |
53 // indexKeyPath: likewise | 53 // indexKeyPath: the key path for that index |
54 // indexIsUnique: the "unique" option for IDBIndexParameters | 54 // indexIsUnique: the "unique" option for IDBIndexParameters |
55 // indexIsMultiEntry: the "multiEntry" option for IDBIndexParameters | 55 // indexIsMultiEntry: the "multiEntry" option for IDBIndexParameters |
56 // | 56 // |
57 function createDatabase(name, objectStores, handler, errorHandler, options) { | 57 function createDatabase(name, objectStores, handler, errorHandler, options) { |
58 var openRequest = indexedDB.open(name, version); | 58 var openRequest = indexedDB.open(name, baseVersion); |
59 openRequest.onblocked = errorHandler; | 59 openRequest.onblocked = errorHandler; |
60 function createObjectStores(db) { | 60 function createObjectStores(db) { |
61 for (var store in objectStores) { | 61 for (var store in objectStores) { |
62 var name = objectStores[store]; | 62 var name = objectStores[store]; |
63 assert(!db.objectStoreNames.contains(name)); | 63 assert(!db.objectStoreNames.contains(name)); |
64 var os = db.createObjectStore(name); | 64 var os = db.createObjectStore(name); |
65 if (options && options.indexName) { | 65 if (options && options.indexName) { |
66 assert(options.indexKeyPath || options.indexKeyPath == ""); | 66 assert(options.indexKeyPath || options.indexKeyPath == ""); |
67 os.createIndex(options.indexName, options.keyPath, | 67 os.createIndex(options.indexName, options.keyPath, |
68 { unique: options.indexIsUnique, | 68 { unique: options.indexIsUnique, |
69 multiEntry: options.indexIsMultiEntry }); | 69 multiEntry: options.indexIsMultiEntry }); |
70 } | 70 } |
71 } | 71 } |
72 } | 72 } |
73 openRequest.onupgradeneeded = function(ev) { | 73 openRequest.onupgradeneeded = function(ev) { |
74 // TODO: This is the spec-compliant path, which doesn't yet work in Chrome, | 74 // TODO: This is the spec-compliant path, which doesn't yet work in Chrome, |
75 // and isn't yet tested, as this function won't currently be called. | 75 // and isn't yet tested, as this function won't currently be called. |
76 assert(openRequest == ev.target); | 76 assert(openRequest == ev.target); |
77 db = openRequest.result; | 77 var db = openRequest.result; |
78 createObjectStores(db); | 78 createObjectStores(db); |
79 // onsuccess will get called after this exits. | 79 // onsuccess will get called after this exits. |
80 }; | 80 }; |
81 openRequest.onsuccess = function(ev) { | 81 openRequest.onsuccess = function(ev) { |
82 assert(openRequest == ev.target); | 82 assert(openRequest == ev.target); |
83 db = openRequest.result; | 83 var db = openRequest.result; |
84 db.onerror = function(ev) { | 84 db.onerror = function(ev) { |
85 console.log("db error", arguments, openRequest.webkitErrorMessage); | 85 console.log("db error", arguments, openRequest.webkitErrorMessage); |
86 errorHandler(); | 86 errorHandler(); |
87 } | 87 } |
88 if (db.version != version) { | 88 if (db.version != baseVersion) { |
89 // This is the current Chrome path. | 89 // This is the current Chrome path. |
90 var setVersionRequest = db.setVersion(version); | 90 var setVersionRequest = db.setVersion(baseVersion); |
91 setVersionRequest.onfailure = errorHandler; | 91 setVersionRequest.onfailure = errorHandler; |
jsbell
2012/07/19 00:28:57
Should be onerror not onfailure.
ericu
2012/07/24 21:58:07
Whoops! Of course it should. Fixed in 2 places.
| |
92 setVersionRequest.onsuccess = | 92 setVersionRequest.onsuccess = |
93 function(e) { | 93 function(e) { |
94 curVersion = db.version; | |
94 assert(setVersionRequest == e.target); | 95 assert(setVersionRequest == e.target); |
95 createObjectStores(db); | 96 createObjectStores(db); |
96 var versionTransaction = setVersionRequest.result; | 97 var versionTransaction = setVersionRequest.result; |
97 versionTransaction.oncomplete = function() {handler(db); }; | 98 versionTransaction.oncomplete = function() { handler(db); }; |
98 versionTransaction.onerror = onError; | 99 versionTransaction.onerror = onError; |
99 } | 100 } |
100 } else { | 101 } else { |
101 handler(db); | 102 handler(db); |
102 } | 103 } |
103 } | 104 } |
104 } | 105 } |
105 | 106 |
107 // You must close all database connections before calling this. | |
108 function alterObjectStores( | |
109 name, objectStoreNames, func, handler, errorHandler) { | |
110 var version = curVersion + 1; | |
111 var openRequest = indexedDB.open(name, version); | |
112 openRequest.onblocked = errorHandler; | |
113 // TODO: This won't work in Firefox yet; see above in createDatabase. | |
114 openRequest.onsuccess = function(ev) { | |
115 assert(openRequest == ev.target); | |
116 var db = openRequest.result; | |
117 db.onerror = function(ev) { | |
118 console.log("error altering db", arguments, | |
119 openRequest.webkitErrorMessage); | |
120 errorHandler(); | |
121 } | |
122 if (db.version != version) { | |
123 var setVersionRequest = db.setVersion(version); | |
124 setVersionRequest.onfailure = errorHandler; | |
125 setVersionRequest.onsuccess = | |
126 function(e) { | |
127 curVersion = db.version; | |
128 assert(setVersionRequest == e.target); | |
129 var versionTransaction = setVersionRequest.result; | |
130 versionTransaction.oncomplete = function() { handler(db); }; | |
131 versionTransaction.onerror = onError; | |
132 for (var store in objectStoreNames) { | |
133 func(versionTransaction.objectStore(objectStoreNames[store])); | |
134 } | |
135 } | |
136 } else { | |
137 errorHandler(); | |
138 } | |
139 } | |
140 } | |
141 | |
106 function getTransaction(db, objectStoreNames, mode, opt_handler) { | 142 function getTransaction(db, objectStoreNames, mode, opt_handler) { |
107 var transaction = db.transaction(objectStoreNames, mode); | 143 var transaction = db.transaction(objectStoreNames, mode); |
108 transaction.onerror = onError; | 144 transaction.onerror = onError; |
109 transaction.onabort = onError; | 145 transaction.onabort = onError; |
110 if (opt_handler) { | 146 if (opt_handler) { |
111 transaction.oncomplete = opt_handler; | 147 transaction.oncomplete = opt_handler; |
112 } | 148 } |
113 return transaction; | 149 return transaction; |
114 } | 150 } |
115 | 151 |
(...skipping 12 matching lines...) Expand all Loading... | |
128 } | 164 } |
129 return function(db) { | 165 return function(db) { |
130 var duration = Date.now() - startTime; | 166 var duration = Date.now() - startTime; |
131 // Ignore the cleanup time for this test. | 167 // Ignore the cleanup time for this test. |
132 automation.addResult(testName, duration); | 168 automation.addResult(testName, duration); |
133 automation.setStatus("Deleting database."); | 169 automation.setStatus("Deleting database."); |
134 deleteDatabase(db, onDeleted); | 170 deleteDatabase(db, onDeleted); |
135 } | 171 } |
136 } | 172 } |
137 | 173 |
174 function getDisplayName(args) { | |
175 return getDisplayName.caller.name + "_" + | |
176 Array.prototype.slice.call(args, 0, args.length - 1).join("_"); | |
177 } | |
178 | |
179 function getSimpleKey(i) { | |
180 return "key " + i; | |
181 } | |
182 | |
183 function getSimpleValue(i) { | |
184 return "value " + i; | |
185 } | |
186 | |
187 function putLinearValues( | |
188 transaction, objectStoreNames, numKeys, getKey, getValue) { | |
189 if (!getKey) | |
190 getKey = getSimpleKey; | |
191 if (!getValue) | |
192 getValue = getSimpleValue; | |
193 for (var i in objectStoreNames) { | |
194 var os = transaction.objectStore(objectStoreNames[i]); | |
195 assert(os); | |
196 for (var j = 0; j < numKeys; ++j) { | |
197 var request = os.put(getValue(j), getKey(j)); | |
198 request.onerror = onError; | |
199 } | |
200 } | |
201 } | |
202 | |
203 function getRandomValues( | |
204 transaction, objectStoreNames, numReads, numKeys, indexName, getKey) { | |
205 if (!getKey) | |
206 getKey = getSimpleKey; | |
207 for (var i in objectStoreNames) { | |
208 var os = transaction.objectStore(objectStoreNames[i]); | |
209 assert(os); | |
210 var queryObject = os; | |
211 if (indexName) | |
212 queryObject = queryObject.index(indexName); | |
213 for (var j = 0; j < numReads; ++j) { | |
214 var rand = Math.floor(Math.random() * numKeys); | |
215 var request = queryObject.get(getKey(rand)); | |
216 request.onerror = onError; | |
217 } | |
218 } | |
219 } | |
220 | |
221 function putRandomValues( | |
222 transaction, objectStoreNames, numPuts, numKeys, getKey, getValue) { | |
223 if (!getKey) | |
224 getKey = getSimpleKey; | |
225 if (!getValue) | |
226 getValue = getSimpleValue; | |
227 for (var i in objectStoreNames) { | |
228 var os = transaction.objectStore(objectStoreNames[i]); | |
229 assert(os); | |
230 for (var j = 0; j < numPuts; ++j) { | |
231 var rand = Math.floor(Math.random() * numKeys); | |
232 var request = os.put(getValue(rand), getKey(rand)); | |
233 request.onerror = onError; | |
234 } | |
235 } | |
236 } | |
237 | |
138 function stringOfLength(n) { | 238 function stringOfLength(n) { |
139 assert(n > 0); | 239 assert(n > 0); |
140 assert(n == Math.floor(n)); | 240 assert(n == Math.floor(n)); |
141 return new Array(n + 1).join('0'); | 241 return new Array(n + 1).join('0'); |
142 } | 242 } |
OLD | NEW |