idb: memory backend fixes
This commit is contained in:
parent
859a9e72e1
commit
c84361d3cb
@ -151,6 +151,43 @@ test("Spec: Example 1 Part 3", async t => {
|
|||||||
cursor = request4.result;
|
cursor = request4.result;
|
||||||
t.is(cursor.value.isbn, 123456);
|
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();
|
db.close();
|
||||||
|
|
||||||
t.pass();
|
t.pass();
|
||||||
|
@ -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);
|
indexEntry = indexData.get(indexPos);
|
||||||
if (!indexEntry) {
|
if (!indexEntry) {
|
||||||
const res = indexData.nextHigherPair(indexPos);
|
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;
|
let primkeySubPos = 0;
|
||||||
|
|
||||||
// Sort out the case where the index key is the same, so we have
|
// Sort out the case where the index key is the same, so we have
|
||||||
// to get the prev/next primary key
|
// to get the prev/next primary key
|
||||||
if (
|
if (
|
||||||
|
indexEntry !== undefined &&
|
||||||
req.lastIndexPosition !== undefined &&
|
req.lastIndexPosition !== undefined &&
|
||||||
compareKeys(indexEntry.indexKey, req.lastIndexPosition) === 0
|
compareKeys(indexEntry.indexKey, req.lastIndexPosition) === 0
|
||||||
) {
|
) {
|
||||||
let pos = forward ? 0 : indexEntry.primaryKeys.length - 1;
|
let pos = forward ? 0 : indexEntry.primaryKeys.length - 1;
|
||||||
|
console.log("number of primary keys", indexEntry.primaryKeys.length);
|
||||||
// Advance past the lastObjectStorePosition
|
// Advance past the lastObjectStorePosition
|
||||||
while (pos >= 0 && pos < indexEntry.primaryKeys.length) {
|
do {
|
||||||
const cmpResult = compareKeys(
|
const cmpResult = compareKeys(
|
||||||
req.lastObjectStorePosition,
|
req.lastObjectStorePosition,
|
||||||
indexEntry.primaryKeys[pos],
|
indexEntry.primaryKeys[pos],
|
||||||
@ -780,7 +781,8 @@ export class MemoryBackend implements Backend {
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
pos += forward ? 1 : -1;
|
pos += forward ? 1 : -1;
|
||||||
}
|
} while (pos >= 0 && pos < indexEntry.primaryKeys.length);
|
||||||
|
|
||||||
// Make sure we're at least at advancedPrimaryPos
|
// Make sure we're at least at advancedPrimaryPos
|
||||||
while (
|
while (
|
||||||
primaryPos !== undefined &&
|
primaryPos !== undefined &&
|
||||||
@ -797,10 +799,12 @@ export class MemoryBackend implements Backend {
|
|||||||
pos += forward ? 1 : -1;
|
pos += forward ? 1 : -1;
|
||||||
}
|
}
|
||||||
primkeySubPos = pos;
|
primkeySubPos = pos;
|
||||||
} else {
|
} else if (indexEntry !== undefined) {
|
||||||
primkeySubPos = forward ? 0 : indexEntry.primaryKeys.length - 1;
|
primkeySubPos = forward ? 0 : indexEntry.primaryKeys.length - 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
console.log("pos=", primkeySubPos);
|
||||||
|
|
||||||
while (1) {
|
while (1) {
|
||||||
if (req.limit != 0 && numResults == req.limit) {
|
if (req.limit != 0 && numResults == req.limit) {
|
||||||
break;
|
break;
|
||||||
@ -811,14 +815,19 @@ export class MemoryBackend implements Backend {
|
|||||||
if (!range.includes(indexPos)) {
|
if (!range.includes(indexPos)) {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
if (indexEntry === undefined) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
if (
|
if (
|
||||||
primkeySubPos < 0 ||
|
primkeySubPos < 0 ||
|
||||||
primkeySubPos >= indexEntry.primaryKeys.length
|
primkeySubPos >= indexEntry.primaryKeys.length
|
||||||
) {
|
) {
|
||||||
|
console.log("moving subkey forward");
|
||||||
primkeySubPos = forward ? 0 : indexEntry.primaryKeys.length - 1;
|
primkeySubPos = forward ? 0 : indexEntry.primaryKeys.length - 1;
|
||||||
const res = indexData.nextHigherPair(indexPos);
|
const res = indexData.nextHigherPair(indexPos);
|
||||||
if (res) {
|
if (res) {
|
||||||
indexPos = res[1].indexKey;
|
indexPos = res[1].indexKey;
|
||||||
|
indexEntry = res[1];
|
||||||
} else {
|
} else {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@ -866,10 +875,11 @@ export class MemoryBackend implements Backend {
|
|||||||
// Advance store position if we are either still at the last returned
|
// Advance store position if we are either still at the last returned
|
||||||
// store key, or if we are currently not on a key.
|
// store key, or if we are currently not on a key.
|
||||||
const storeEntry = storeData.get(storePos);
|
const storeEntry = storeData.get(storePos);
|
||||||
|
console.log("store entry:", storeEntry);
|
||||||
if (
|
if (
|
||||||
!storeEntry ||
|
!storeEntry ||
|
||||||
(req.lastObjectStorePosition !== undefined &&
|
(req.lastObjectStorePosition !== undefined &&
|
||||||
compareKeys(req.lastObjectStorePosition, storeEntry.primaryKey))
|
compareKeys(req.lastObjectStorePosition, storePos) === 0)
|
||||||
) {
|
) {
|
||||||
storePos = storeData.nextHigherKey(storePos);
|
storePos = storeData.nextHigherKey(storePos);
|
||||||
}
|
}
|
||||||
|
@ -39,8 +39,6 @@ const getType = (x: any) => {
|
|||||||
|
|
||||||
// https://w3c.github.io/IndexedDB/#compare-two-keys
|
// https://w3c.github.io/IndexedDB/#compare-two-keys
|
||||||
const compareKeys = (first: any, second: any): -1 | 0 | 1 => {
|
const compareKeys = (first: any, second: any): -1 | 0 | 1 => {
|
||||||
console.log("comparing keys", first, second);
|
|
||||||
|
|
||||||
if (second === undefined) {
|
if (second === undefined) {
|
||||||
throw new TypeError();
|
throw new TypeError();
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user