Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 var kPrefetchThreshold = 2; | |
|
jsbell
2011/11/28 20:29:16
Include a comment that this must match the value i
hans
2011/11/29 14:44:34
Done.
| |
| 6 var kMinPrefetchAmount = 5; | |
| 7 var kNumberOfItems = 100; | |
|
jsbell
2011/11/28 20:29:16
Since this is == kMaxPrefetchAmount in renderer_we
hans
2011/11/29 14:44:34
Done. Bumping to 200.
| |
| 8 | |
| 9 function test() { | |
| 10 request = webkitIndexedDB.open('cursor-prefetch'); | |
|
jsbell
2011/11/28 20:29:16
Can you add the indexedDB = indexedDB || webkitInd
hans
2011/11/29 14:44:34
Done.
| |
| 11 request.onsuccess = openSuccess; | |
| 12 request.onerror = unexpectedErrorCallback; | |
| 13 } | |
| 14 | |
| 15 function openSuccess() { | |
| 16 window.db = event.target.result; | |
| 17 | |
| 18 request = db.setVersion('new version'); | |
| 19 request.onsuccess = setVersionSuccess; | |
| 20 request.onerror = unexpectedErrorCallback; | |
| 21 } | |
| 22 | |
| 23 function setVersionSuccess() { | |
| 24 debug("setVersionSuccess():"); | |
| 25 window.trans = event.target.result; | |
| 26 shouldBeTrue("trans !== null"); | |
| 27 trans.onabort = unexpectedAbortCallback; | |
| 28 trans.oncomplete = fillObjectStore; | |
| 29 | |
| 30 deleteAllObjectStores(db); | |
| 31 var store = db.createObjectStore('store'); | |
| 32 store.createIndex('index', ''); | |
| 33 } | |
| 34 | |
| 35 function fillObjectStore() { | |
| 36 debug("fillObjectStore()"); | |
| 37 var trans = db.transaction(['store'], webkitIDBTransaction.READ_WRITE); | |
| 38 trans.onabort = unexpectedAbortCallback; | |
| 39 trans.oncomplete = firstTest; | |
| 40 | |
| 41 var store = trans.objectStore('store'); | |
| 42 debug("Storing " + kNumberOfItems + " object in the object store."); | |
| 43 for (var i = 0; i < kNumberOfItems; ++i) { | |
| 44 var req = store.put(i, i); | |
| 45 req.onerror = unexpectedErrorCallback; | |
| 46 } | |
| 47 | |
| 48 // Let the transaction finish. | |
| 49 } | |
| 50 | |
| 51 function firstTest() { | |
| 52 debug("firstTest()"); | |
| 53 | |
| 54 // Test iterating straight through the object store. | |
| 55 | |
| 56 var trans = db.transaction(['store'], webkitIDBTransaction.READ_WRITE); | |
| 57 trans.onabort = unexpectedAbortCallback; | |
| 58 trans.oncomplete = secondTest; | |
| 59 | |
| 60 var store = trans.objectStore('store'); | |
| 61 var cursorReq = store.openCursor(); | |
| 62 cursorReq.onerror = unexpectedErrorCallback; | |
| 63 | |
| 64 count = 0; | |
| 65 cursorReq.onsuccess = function() { | |
| 66 cursor = event.target.result; | |
| 67 if (cursor === null) { | |
| 68 shouldBe("count", "kNumberOfItems"); | |
| 69 return; // Let the transaction finish. | |
| 70 } | |
| 71 | |
| 72 if (cursor.key !== count) | |
| 73 shouldBe("cursor.key", "count"); | |
| 74 if (cursor.value !== count) | |
| 75 shouldBe("cursor.value", "count"); | |
| 76 | |
| 77 ++count; | |
| 78 | |
| 79 cursor.continue(); | |
| 80 } | |
| 81 } | |
| 82 | |
| 83 function secondTest() { | |
| 84 debug("secondTest()"); | |
| 85 | |
| 86 // Test iterating through the object store, intermixed with | |
| 87 // continue calls to specific keys. | |
| 88 | |
| 89 var trans = db.transaction(['store'], webkitIDBTransaction.READ_WRITE); | |
| 90 trans.onabort = unexpectedAbortCallback; | |
| 91 trans.oncomplete = thirdTest; | |
| 92 | |
| 93 var store = trans.objectStore('store'); | |
| 94 var cursorReq = store.openCursor(); | |
| 95 cursorReq.onerror = unexpectedErrorCallback; | |
| 96 | |
| 97 var jumpTable = [{from: 5, to: 17}, | |
| 98 {from: 25, to: 30}, | |
| 99 {from: 31, to: 35}, | |
| 100 {from: 70, to: 80}, | |
| 101 {from: 98, to: 99}]; | |
| 102 | |
| 103 count = 0; | |
| 104 expectedKey = 0; | |
| 105 | |
| 106 cursorReq.onsuccess = function() { | |
| 107 cursor = event.target.result; | |
| 108 if (cursor === null) { | |
| 109 debug("Finished iterating after " + count + " steps."); | |
| 110 return; // Let the transaction finish. | |
| 111 } | |
| 112 | |
| 113 if (cursor.key !== expectedKey) | |
| 114 shouldBe("cursor.key", "expectedKey"); | |
| 115 if (cursor.value !== expectedKey) | |
| 116 shouldBe("cursor.value", "expectedKey"); | |
| 117 | |
| 118 ++count; | |
| 119 | |
| 120 for (var i = 0; i < jumpTable.length; ++i) { | |
| 121 if (jumpTable[i].from === cursor.key) { | |
| 122 expectedKey = jumpTable[i].to; | |
| 123 debug("Jumping from "+ cursor.key + " to " + expectedKey); | |
| 124 cursor.continue(expectedKey); | |
| 125 return; | |
| 126 } | |
| 127 } | |
| 128 | |
| 129 ++expectedKey; | |
| 130 cursor.continue(); | |
| 131 } | |
| 132 } | |
| 133 | |
| 134 function thirdTest() { | |
| 135 debug("thirdTest()"); | |
| 136 | |
| 137 // Test iterating straight through the object store in reverse. | |
| 138 | |
| 139 var trans = db.transaction(['store'], webkitIDBTransaction.READ_WRITE); | |
| 140 trans.onabort = unexpectedAbortCallback; | |
| 141 trans.oncomplete = fourthTest; | |
| 142 | |
| 143 var store = trans.objectStore('store'); | |
| 144 var cursorReq = store.openCursor( | |
| 145 webkitIDBKeyRange.upperBound(kNumberOfItems-1), webkitIDBCursor.PREV); | |
| 146 cursorReq.onerror = unexpectedErrorCallback; | |
| 147 | |
| 148 count = 0; | |
| 149 cursorReq.onsuccess = function() { | |
| 150 cursor = event.target.result; | |
| 151 if (cursor === null) { | |
| 152 shouldBe("count", "kNumberOfItems"); | |
| 153 return; // Let the transaction finish. | |
| 154 } | |
| 155 | |
| 156 expectedKey = kNumberOfItems - count - 1; | |
| 157 | |
| 158 if (cursor.key !== expectedKey) | |
| 159 shouldBe("cursor.key", "expectedKey"); | |
| 160 if (cursor.value !== expectedKey) | |
| 161 shouldBe("cursor.value", "expectedKey"); | |
| 162 | |
| 163 ++count; | |
| 164 | |
| 165 cursor.continue(); | |
| 166 } | |
| 167 } | |
| 168 | |
| 169 function fourthTest() { | |
| 170 debug("fourthTest()"); | |
| 171 | |
| 172 // Test iterating, and then stopping before reaching the end. | |
| 173 // Make sure transaction terminates anyway. | |
| 174 | |
| 175 var trans = db.transaction(['store'], webkitIDBTransaction.READ_WRITE); | |
| 176 trans.onabort = unexpectedAbortCallback; | |
| 177 trans.oncomplete = function() { | |
| 178 debug("fourthTest() transaction completed"); | |
| 179 fifthTest(); | |
| 180 } | |
| 181 | |
| 182 var store = trans.objectStore('store'); | |
| 183 var cursorReq = store.openCursor(); | |
| 184 cursorReq.onerror = unexpectedErrorCallback; | |
| 185 | |
| 186 count = 0; | |
| 187 cursorReq.onsuccess = function() { | |
| 188 cursor = event.target.result; | |
| 189 | |
| 190 if (cursor.key !== count) | |
| 191 shouldBe("cursor.key", "count"); | |
| 192 if (cursor.value !== count) | |
| 193 shouldBe("cursor.value", "count"); | |
| 194 | |
| 195 ++count; | |
| 196 | |
| 197 if (count === 25) { | |
| 198 // Schedule some other request. | |
| 199 var otherReq = store.get(42); | |
| 200 otherReq.onerror = unexpectedErrorCallback; | |
| 201 otherReq.onsuccess = function() { | |
| 202 if (count === 25) { | |
| 203 debug("Other request fired before continue, as expected."); | |
| 204 } else { | |
| 205 debug("Other request fired out-of-order!"); | |
| 206 fail(); | |
| 207 } | |
| 208 } | |
| 209 | |
| 210 cursor.continue(); | |
| 211 return; | |
| 212 } | |
| 213 | |
| 214 if (count === 30) { | |
| 215 // Do a continue first, then another request. | |
| 216 cursor.continue(); | |
| 217 | |
| 218 var otherReq = store.get(42); | |
| 219 otherReq.onerror = unexpectedErrorCallback; | |
| 220 otherReq.onsuccess = function() { | |
| 221 if (count === 31) { | |
| 222 debug("Other request fired right after continue as expected."); | |
| 223 } else { | |
| 224 debug("Other request didn't fire right after continue as expected."); | |
| 225 fail(); | |
| 226 } | |
| 227 } | |
| 228 | |
| 229 return; | |
| 230 } | |
| 231 | |
| 232 if (count === 75) { | |
| 233 return; // Sudden stop. | |
| 234 } | |
| 235 | |
| 236 cursor.continue(); | |
| 237 } | |
| 238 } | |
| 239 | |
| 240 function fifthTest() { | |
| 241 debug("fifthTest()"); | |
| 242 | |
| 243 // Test iterating over the pre-fetch threshold, but make sure the | |
| 244 // cursor is positioned so that it is actually at the last element | |
| 245 // in the range when pre-fetch fires, and make sure a null cursor | |
| 246 // is the result as expected. | |
| 247 | |
| 248 var trans = db.transaction(['store'], webkitIDBTransaction.READ_WRITE); | |
| 249 trans.onabort = unexpectedAbortCallback; | |
| 250 trans.oncomplete = sixthTest; | |
| 251 | |
| 252 var store = trans.objectStore('store'); | |
| 253 | |
| 254 var startKey = kNumberOfItems - 1 - kPrefetchThreshold; | |
| 255 var cursorReq = store.openCursor(webkitIDBKeyRange.lowerBound(startKey)); | |
| 256 cursorReq.onerror = unexpectedErrorCallback; | |
| 257 | |
| 258 count = 0; | |
| 259 cursorReq.onsuccess = function() { | |
| 260 cursor = event.target.result; | |
| 261 | |
| 262 if (cursor === null) { | |
| 263 debug("cursor is null"); | |
| 264 shouldBe("count", "kPrefetchThreshold + 1"); | |
| 265 return; | |
| 266 } | |
| 267 | |
| 268 debug("count: " + count); | |
| 269 ++count; | |
| 270 cursor.continue(); | |
| 271 } | |
| 272 } | |
| 273 | |
| 274 function sixthTest() { | |
| 275 debug("sixthTest()"); | |
| 276 | |
| 277 // Test stepping two cursors simultaneously. First cursor1 steps | |
| 278 // for a while, then cursor2, then back to cursor1, etc. | |
| 279 | |
| 280 var trans = db.transaction(['store'], webkitIDBTransaction.READ_WRITE); | |
| 281 trans.onabort = unexpectedAbortCallback; | |
| 282 trans.oncomplete = seventhTest; | |
| 283 var store = trans.objectStore('store'); | |
| 284 | |
| 285 cursor1 = null; | |
| 286 cursor2 = null; | |
| 287 | |
| 288 count1 = 0; | |
| 289 count2 = 0; | |
| 290 | |
| 291 var cursor1func = function() { | |
| 292 var cursor = event.target.result; | |
| 293 if (cursor === null) { | |
| 294 shouldBe("count1", "kNumberOfItems"); | |
| 295 cursor2.continue(); | |
| 296 return; | |
| 297 } | |
| 298 | |
| 299 if (cursor1 === null) { | |
| 300 cursor1 = cursor; | |
| 301 } | |
| 302 | |
| 303 if (cursor1.key !== count1) | |
| 304 shouldBe("cursor1.key", "count1"); | |
| 305 if (cursor1.value !== count1) | |
| 306 shouldBe("cursor1.value", "count1"); | |
| 307 | |
| 308 ++count1; | |
| 309 | |
| 310 if (count1 % 20 === 0) { | |
| 311 if (cursor2 !== null) { | |
| 312 cursor2.continue(); | |
| 313 } else { | |
| 314 var req = store.openCursor(); | |
| 315 req.onerror = unexpectedErrorCallback; | |
| 316 req.onsuccess = cursor2func; | |
| 317 } | |
| 318 } else { | |
| 319 cursor1.continue(); | |
| 320 } | |
| 321 } | |
| 322 | |
| 323 var cursor2func = function() { | |
| 324 var cursor = event.target.result; | |
| 325 if (cursor === null) { | |
| 326 shouldBe("count2", "kNumberOfItems"); | |
| 327 return; | |
| 328 } | |
| 329 | |
| 330 if (cursor2 === null) { | |
| 331 cursor2 = cursor; | |
| 332 } | |
| 333 | |
| 334 if (cursor2.key !== count2) | |
| 335 shouldBe("cursor2.key", "count2"); | |
| 336 if (cursor2.value !== count2) | |
| 337 shouldBe("cursor2.value", "count2"); | |
| 338 | |
| 339 ++count2; | |
| 340 | |
| 341 if (count2 % 20 === 0) { | |
| 342 cursor1.continue(); | |
| 343 } else { | |
| 344 cursor2.continue(); | |
| 345 } | |
| 346 } | |
| 347 | |
| 348 var req = store.openCursor(); | |
| 349 req.onerror = unexpectedErrorCallback; | |
| 350 req.onsuccess = cursor1func; | |
| 351 } | |
| 352 | |
| 353 function seventhTest() { | |
| 354 debug("seventhTest()"); | |
| 355 | |
|
jsbell
2011/11/28 20:29:16
Add a comment about what this test is for.
hans
2011/11/29 14:44:34
Done.
| |
| 356 var trans = db.transaction(['store'], webkitIDBTransaction.READ_WRITE); | |
| 357 trans.onabort = unexpectedAbortCallback; | |
| 358 trans.oncomplete = done; | |
| 359 | |
| 360 var store = trans.objectStore('store'); | |
| 361 var index = store.index('index'); | |
| 362 | |
| 363 var cursorReq = index.openCursor(); | |
| 364 cursorReq.onerror = unexpectedErrorCallback; | |
| 365 count = 0; | |
| 366 | |
| 367 cursorReq.onsuccess = function() { | |
| 368 cursor = event.target.result; | |
| 369 if (cursor === null) { | |
| 370 shouldBe("count", "kNumberOfItems"); | |
| 371 return; | |
| 372 } | |
| 373 | |
| 374 if (cursor.key !== count) | |
| 375 shouldBe("cursor.key", "count"); | |
| 376 if (cursor.primaryKey !== count) | |
| 377 shouldBe("cursor.primaryKey", "count"); | |
| 378 if (cursor.value !== count) | |
| 379 shouldBe("cursor.value", "count"); | |
| 380 | |
| 381 ++count; | |
| 382 cursor.continue(); | |
| 383 } | |
| 384 } | |
| OLD | NEW |