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