diff options
author | Florian Dold <florian.dold@gmail.com> | 2019-06-25 12:49:43 +0200 |
---|---|---|
committer | Florian Dold <florian.dold@gmail.com> | 2019-06-25 12:49:43 +0200 |
commit | c84361d3cbf5485ee924272047a3fbd78bc508f6 (patch) | |
tree | 5e366b7e0576250601e086ad902ebbc941876b03 /packages/idb-bridge | |
parent | 859a9e72e1e0431d729e429865d6272e2fb03ff7 (diff) |
idb: memory backend fixes
Diffstat (limited to 'packages/idb-bridge')
-rw-r--r-- | packages/idb-bridge/src/MemoryBackend.test.ts | 37 | ||||
-rw-r--r-- | packages/idb-bridge/src/MemoryBackend.ts | 30 | ||||
-rw-r--r-- | packages/idb-bridge/src/util/cmp.ts | 2 |
3 files changed, 57 insertions, 12 deletions
diff --git a/packages/idb-bridge/src/MemoryBackend.test.ts b/packages/idb-bridge/src/MemoryBackend.test.ts index c882b9760..b5bc5dad2 100644 --- a/packages/idb-bridge/src/MemoryBackend.test.ts +++ b/packages/idb-bridge/src/MemoryBackend.test.ts @@ -151,6 +151,43 @@ test("Spec: Example 1 Part 3", async t => { cursor = request4.result; t.is(cursor.value.isbn, 123456); + cursor.continue(); + + await promiseFromRequest(request4); + + cursor = request4.result; + t.is(cursor.value.isbn, 234567); + + cursor.continue(); + + await promiseFromRequest(request4); + + cursor = request4.result; + t.is(cursor.value.isbn, 345678); + + cursor.continue(); + await promiseFromRequest(request4); + + cursor = request4.result; + + t.is(cursor, null); + + const tx5 = db.transaction("books", "readonly"); + const store5 = tx5.objectStore("books"); + const index5 = store5.index("by_author"); + + const request5 = index5.openCursor(null, "next"); + + await promiseFromRequest(request5); + cursor = request5.result; + t.is(cursor.value.author, "Barney"); + cursor.continue(); + + await promiseFromRequest(request5); + cursor = request5.result; + t.is(cursor.value.author, "Fred"); + cursor.continue(); + db.close(); t.pass(); diff --git a/packages/idb-bridge/src/MemoryBackend.ts b/packages/idb-bridge/src/MemoryBackend.ts index 75e583a89..c72431f28 100644 --- a/packages/idb-bridge/src/MemoryBackend.ts +++ b/packages/idb-bridge/src/MemoryBackend.ts @@ -747,7 +747,11 @@ export class MemoryBackend implements Backend { } } - let indexEntry; + if (indexPos === undefined || indexPos === null) { + indexPos = forward ? indexData.minKey() : indexData.maxKey(); + } + + let indexEntry: IndexRecord | undefined; indexEntry = indexData.get(indexPos); if (!indexEntry) { const res = indexData.nextHigherPair(indexPos); @@ -756,22 +760,19 @@ export class MemoryBackend implements Backend { } } - if (!indexEntry) { - // We're out of luck, no more data! - return { count: 0, primaryKeys: [], indexKeys: [], values: [] }; - } - let primkeySubPos = 0; // Sort out the case where the index key is the same, so we have // to get the prev/next primary key if ( + indexEntry !== undefined && req.lastIndexPosition !== undefined && compareKeys(indexEntry.indexKey, req.lastIndexPosition) === 0 ) { let pos = forward ? 0 : indexEntry.primaryKeys.length - 1; + console.log("number of primary keys", indexEntry.primaryKeys.length); // Advance past the lastObjectStorePosition - while (pos >= 0 && pos < indexEntry.primaryKeys.length) { + do { const cmpResult = compareKeys( req.lastObjectStorePosition, indexEntry.primaryKeys[pos], @@ -780,7 +781,8 @@ export class MemoryBackend implements Backend { break; } pos += forward ? 1 : -1; - } + } while (pos >= 0 && pos < indexEntry.primaryKeys.length); + // Make sure we're at least at advancedPrimaryPos while ( primaryPos !== undefined && @@ -797,10 +799,12 @@ export class MemoryBackend implements Backend { pos += forward ? 1 : -1; } primkeySubPos = pos; - } else { + } else if (indexEntry !== undefined) { primkeySubPos = forward ? 0 : indexEntry.primaryKeys.length - 1; } + console.log("pos=", primkeySubPos); + while (1) { if (req.limit != 0 && numResults == req.limit) { break; @@ -811,14 +815,19 @@ export class MemoryBackend implements Backend { if (!range.includes(indexPos)) { break; } + if (indexEntry === undefined) { + break; + } if ( primkeySubPos < 0 || primkeySubPos >= indexEntry.primaryKeys.length ) { + console.log("moving subkey forward"); primkeySubPos = forward ? 0 : indexEntry.primaryKeys.length - 1; const res = indexData.nextHigherPair(indexPos); if (res) { indexPos = res[1].indexKey; + indexEntry = res[1]; } else { break; } @@ -866,10 +875,11 @@ export class MemoryBackend implements Backend { // Advance store position if we are either still at the last returned // store key, or if we are currently not on a key. const storeEntry = storeData.get(storePos); + console.log("store entry:", storeEntry); if ( !storeEntry || (req.lastObjectStorePosition !== undefined && - compareKeys(req.lastObjectStorePosition, storeEntry.primaryKey)) + compareKeys(req.lastObjectStorePosition, storePos) === 0) ) { storePos = storeData.nextHigherKey(storePos); } diff --git a/packages/idb-bridge/src/util/cmp.ts b/packages/idb-bridge/src/util/cmp.ts index 078c0a9bd..9d0dc99a2 100644 --- a/packages/idb-bridge/src/util/cmp.ts +++ b/packages/idb-bridge/src/util/cmp.ts @@ -39,8 +39,6 @@ const getType = (x: any) => { // https://w3c.github.io/IndexedDB/#compare-two-keys const compareKeys = (first: any, second: any): -1 | 0 | 1 => { - console.log("comparing keys", first, second); - if (second === undefined) { throw new TypeError(); } |