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 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
42 function onError(e) { | 42 function onError(e) { |
43 var s = "Caught error."; | 43 var s = "Caught error."; |
44 if (e.target && e.target.webkitErrorMessage) | 44 if (e.target && e.target.webkitErrorMessage) |
45 s += "\n" + e.target.webkitErrorMessage; | 45 s += "\n" + e.target.webkitErrorMessage; |
46 console.log(s); | 46 console.log(s); |
47 automation.setStatus(s); | 47 automation.setStatus(s); |
48 e.stopPropagation(); | 48 e.stopPropagation(); |
49 throw new Error(e); | 49 throw new Error(e); |
50 } | 50 } |
51 | 51 |
52 var version = 2; // The version with our object stores. | 52 var baseVersion = 2; // The version with our object stores. |
53 var db; | 53 var curVersion; |
54 | 54 |
55 // Valid options fields: | 55 // Valid options fields: |
56 // indexName: the name of an index to create on each object store | 56 // indexName: the name of an index to create on each object store |
57 // indexKeyPath: likewise | 57 // indexKeyPath: the key path for that index |
58 // indexIsUnique: the "unique" option for IDBIndexParameters | 58 // indexIsUnique: the "unique" option for IDBIndexParameters |
59 // indexIsMultiEntry: the "multiEntry" option for IDBIndexParameters | 59 // indexIsMultiEntry: the "multiEntry" option for IDBIndexParameters |
60 // | 60 // |
61 function createDatabase(name, objectStores, handler, errorHandler, options) { | 61 function createDatabase(name, objectStores, handler, errorHandler, options) { |
62 var openRequest = indexedDB.open(name, version); | 62 var openRequest = indexedDB.open(name, baseVersion); |
63 openRequest.onblocked = errorHandler; | 63 openRequest.onblocked = errorHandler; |
64 function createObjectStores(db) { | 64 function createObjectStores(db) { |
65 for (var store in objectStores) { | 65 for (var store in objectStores) { |
66 var name = objectStores[store]; | 66 var name = objectStores[store]; |
67 assert(!db.objectStoreNames.contains(name)); | 67 assert(!db.objectStoreNames.contains(name)); |
68 var os = db.createObjectStore(name); | 68 var os = db.createObjectStore(name); |
69 if (options && options.indexName) { | 69 if (options && options.indexName) { |
70 assert(options.indexKeyPath || options.indexKeyPath == ""); | 70 assert(options.indexKeyPath || options.indexKeyPath == ""); |
71 os.createIndex(options.indexName, options.indexKeyPath, | 71 os.createIndex(options.indexName, options.indexKeyPath, |
72 { unique: options.indexIsUnique, | 72 { unique: options.indexIsUnique, |
73 multiEntry: options.indexIsMultiEntry }); | 73 multiEntry: options.indexIsMultiEntry }); |
74 } | 74 } |
75 } | 75 } |
76 } | 76 } |
77 openRequest.onupgradeneeded = function(ev) { | 77 openRequest.onupgradeneeded = function(ev) { |
78 // TODO: This is the spec-compliant path, which doesn't yet work in Chrome, | 78 // TODO: This is the spec-compliant path, which doesn't yet work in Chrome, |
79 // and isn't yet tested, as this function won't currently be called. | 79 // and isn't yet tested, as this function won't currently be called. |
80 assert(openRequest == ev.target); | 80 assert(openRequest == ev.target); |
81 db = openRequest.result; | 81 var db = openRequest.result; |
jsbell
2012/07/26 18:52:41
Ignore my comment in the previous review.
| |
82 createObjectStores(db); | 82 createObjectStores(db); |
83 // onsuccess will get called after this exits. | 83 // onsuccess will get called after this exits. |
84 }; | 84 }; |
85 openRequest.onsuccess = function(ev) { | 85 openRequest.onsuccess = function(ev) { |
86 assert(openRequest == ev.target); | 86 assert(openRequest == ev.target); |
87 db = openRequest.result; | 87 var db = openRequest.result; |
88 db.onerror = function(ev) { | 88 db.onerror = function(ev) { |
89 console.log("db error", arguments, openRequest.webkitErrorMessage); | 89 console.log("db error", arguments, openRequest.webkitErrorMessage); |
90 errorHandler(); | 90 errorHandler(); |
91 } | 91 } |
92 if (db.version != version) { | 92 if (db.version != baseVersion) { |
93 // This is the current Chrome path. | 93 // This is the current Chrome path. |
94 var setVersionRequest = db.setVersion(version); | 94 var setVersionRequest = db.setVersion(baseVersion); |
95 setVersionRequest.onfailure = errorHandler; | 95 setVersionRequest.onerror = errorHandler; |
96 setVersionRequest.onsuccess = | 96 setVersionRequest.onsuccess = |
97 function(e) { | 97 function(e) { |
98 curVersion = db.version; | |
98 assert(setVersionRequest == e.target); | 99 assert(setVersionRequest == e.target); |
99 createObjectStores(db); | 100 createObjectStores(db); |
100 var versionTransaction = setVersionRequest.result; | 101 var versionTransaction = setVersionRequest.result; |
101 versionTransaction.oncomplete = function() {handler(db); }; | 102 versionTransaction.oncomplete = function() { handler(db); }; |
102 versionTransaction.onerror = onError; | 103 versionTransaction.onerror = onError; |
103 } | 104 } |
104 } else { | 105 } else { |
105 handler(db); | 106 handler(db); |
106 } | 107 } |
107 } | 108 } |
108 } | 109 } |
109 | 110 |
111 // You must close all database connections before calling this. | |
112 function alterObjectStores( | |
113 name, objectStoreNames, func, handler, errorHandler) { | |
114 var version = curVersion + 1; | |
115 var openRequest = indexedDB.open(name, version); | |
116 openRequest.onblocked = errorHandler; | |
117 // TODO: This won't work in Firefox yet; see above in createDatabase. | |
jsbell
2012/07/26 18:52:41
I wrote an upgradeneeded shim for the current vers
ericu
2012/07/26 21:03:12
I'll consider adding that in a later CL, especiall
| |
118 openRequest.onsuccess = function(ev) { | |
119 assert(openRequest == ev.target); | |
120 var db = openRequest.result; | |
121 db.onerror = function(ev) { | |
122 console.log("error altering db", arguments, | |
123 openRequest.webkitErrorMessage); | |
124 errorHandler(); | |
125 } | |
126 if (db.version != version) { | |
127 var setVersionRequest = db.setVersion(version); | |
128 setVersionRequest.onerror = errorHandler; | |
129 setVersionRequest.onsuccess = | |
130 function(e) { | |
131 curVersion = db.version; | |
132 assert(setVersionRequest == e.target); | |
133 var versionTransaction = setVersionRequest.result; | |
134 versionTransaction.oncomplete = function() { handler(db); }; | |
135 versionTransaction.onerror = onError; | |
136 for (var store in objectStoreNames) { | |
137 func(versionTransaction.objectStore(objectStoreNames[store])); | |
138 } | |
139 } | |
140 } else { | |
141 errorHandler(); | |
142 } | |
143 } | |
144 } | |
145 | |
110 function getTransaction(db, objectStoreNames, mode, opt_handler) { | 146 function getTransaction(db, objectStoreNames, mode, opt_handler) { |
111 var transaction = db.transaction(objectStoreNames, mode); | 147 var transaction = db.transaction(objectStoreNames, mode); |
112 transaction.onerror = onError; | 148 transaction.onerror = onError; |
113 transaction.onabort = onError; | 149 transaction.onabort = onError; |
114 if (opt_handler) { | 150 if (opt_handler) { |
115 transaction.oncomplete = opt_handler; | 151 transaction.oncomplete = opt_handler; |
116 } | 152 } |
117 return transaction; | 153 return transaction; |
118 } | 154 } |
119 | 155 |
(...skipping 14 matching lines...) Expand all Loading... | |
134 var duration = Date.now() - startTime; | 170 var duration = Date.now() - startTime; |
135 // Ignore the cleanup time for this test. | 171 // Ignore the cleanup time for this test. |
136 automation.addResult(testName, duration); | 172 automation.addResult(testName, duration); |
137 automation.setStatus("Deleting database."); | 173 automation.setStatus("Deleting database."); |
138 // TODO: Turn on actual deletion; for now it's way too slow. | 174 // TODO: Turn on actual deletion; for now it's way too slow. |
139 // deleteDatabase(testName, onDeleted); | 175 // deleteDatabase(testName, onDeleted); |
140 onTestComplete(); | 176 onTestComplete(); |
141 } | 177 } |
142 } | 178 } |
143 | 179 |
180 function getDisplayName(args) { | |
181 return getDisplayName.caller.name + "_" + | |
182 Array.prototype.slice.call(args, 0, args.length - 1).join("_"); | |
jsbell
2012/07/26 18:52:41
Cute. :) Maybe a comment that last argument is ass
ericu
2012/07/26 21:03:12
Done.
| |
183 } | |
184 | |
185 function getSimpleKey(i) { | |
186 return "key " + i; | |
187 } | |
188 | |
189 function getSimpleValue(i) { | |
190 return "value " + i; | |
191 } | |
192 | |
193 function putLinearValues( | |
194 transaction, objectStoreNames, numKeys, getKey, getValue) { | |
195 if (!getKey) | |
196 getKey = getSimpleKey; | |
197 if (!getValue) | |
198 getValue = getSimpleValue; | |
199 for (var i in objectStoreNames) { | |
200 var os = transaction.objectStore(objectStoreNames[i]); | |
jsbell
2012/07/26 18:52:41
This will throw if not found, never return null. S
ericu
2012/07/26 21:03:13
Removed. It'll fail quickly enough anyway.
| |
201 assert(os); | |
202 for (var j = 0; j < numKeys; ++j) { | |
203 var request = os.put(getValue(j), getKey(j)); | |
204 request.onerror = onError; | |
205 } | |
206 } | |
207 } | |
208 | |
209 function getRandomValues( | |
210 transaction, objectStoreNames, numReads, numKeys, indexName, getKey) { | |
211 if (!getKey) | |
212 getKey = getSimpleKey; | |
213 for (var i in objectStoreNames) { | |
214 var os = transaction.objectStore(objectStoreNames[i]); | |
215 assert(os); | |
216 var queryObject = os; | |
jsbell
2012/07/26 18:52:41
FYI, in the IDB spec itself this (a store or index
ericu
2012/07/26 21:03:13
Sounds good; changed to "source".
| |
217 if (indexName) | |
218 queryObject = queryObject.index(indexName); | |
219 for (var j = 0; j < numReads; ++j) { | |
220 var rand = Math.floor(Math.random() * numKeys); | |
221 var request = queryObject.get(getKey(rand)); | |
222 request.onerror = onError; | |
223 } | |
224 } | |
225 } | |
226 | |
227 function putRandomValues( | |
228 transaction, objectStoreNames, numPuts, numKeys, getKey, getValue) { | |
229 if (!getKey) | |
230 getKey = getSimpleKey; | |
231 if (!getValue) | |
232 getValue = getSimpleValue; | |
233 for (var i in objectStoreNames) { | |
234 var os = transaction.objectStore(objectStoreNames[i]); | |
235 assert(os); | |
236 for (var j = 0; j < numPuts; ++j) { | |
237 var rand = Math.floor(Math.random() * numKeys); | |
238 var request = os.put(getValue(rand), getKey(rand)); | |
239 request.onerror = onError; | |
240 } | |
241 } | |
242 } | |
243 | |
144 function stringOfLength(n) { | 244 function stringOfLength(n) { |
145 assert(n > 0); | 245 assert(n > 0); |
146 assert(n == Math.floor(n)); | 246 assert(n == Math.floor(n)); |
147 return new Array(n + 1).join('0'); | 247 return new Array(n + 1).join('0'); |
148 } | 248 } |
OLD | NEW |