From d7c94ff3e179625c308b7e815976fe10a0d977a3 Mon Sep 17 00:00:00 2001 From: MagicCodingMan Date: Mon, 30 Nov 2026 23:54:32 -0500 Subject: [PATCH 1/2] Start indexed terminal reads inside their Dexie transaction --- .../Magic.IndexedDb.Cli.csproj | 2 +- Magic.IndexedDb/Magic.IndexedDb.csproj | 4 +- .../wwwroot/magicLinqToIndexedDb.js | 17 +++---- tests/migrations/ProtectedWasm/App/App.razor | 14 ++++++ tests/migrations/run.mjs | 49 ++++++++++++++++++- 5 files changed, 72 insertions(+), 14 deletions(-) diff --git a/Magic.IndexedDb.Cli/Magic.IndexedDb.Cli.csproj b/Magic.IndexedDb.Cli/Magic.IndexedDb.Cli.csproj index 7be280d6..abcd3ded 100644 --- a/Magic.IndexedDb.Cli/Magic.IndexedDb.Cli.csproj +++ b/Magic.IndexedDb.Cli/Magic.IndexedDb.Cli.csproj @@ -7,7 +7,7 @@ true magicdb Magic.IndexedDb.Cli - 4.1.0 + 4.1.1 Magic Man Code-first, language-independent migration planning, validation, and static publishing for Magic IndexedDB. https://github.com/magiccodingman/Magic.IndexedDb diff --git a/Magic.IndexedDb/Magic.IndexedDb.csproj b/Magic.IndexedDb/Magic.IndexedDb.csproj index 8462594a..4f2c8562 100644 --- a/Magic.IndexedDb/Magic.IndexedDb.csproj +++ b/Magic.IndexedDb/Magic.IndexedDb.csproj @@ -15,8 +15,8 @@ 4.0.0.0 README.md LICENSE.txt - 4.1.0.0 - 4.1.0 + 4.1.1.0 + 4.1.1 diff --git a/Magic.IndexedDb/wwwroot/magicLinqToIndexedDb.js b/Magic.IndexedDb/wwwroot/magicLinqToIndexedDb.js index 9b851f67..580a9208 100644 --- a/Magic.IndexedDb/wwwroot/magicLinqToIndexedDb.js +++ b/Magic.IndexedDb/wwwroot/magicLinqToIndexedDb.js @@ -242,18 +242,15 @@ async function runIndexedQueries(db, table, universalQueries, return await table.toArray(); } - let queries = []; - - for (let query of universalQueries) { - const coverage = coveredIndex(table, query, queryAdditions, projection); - queries.push({ q: runIndexedQuery(table, query, queryAdditions, Boolean(coverage)), coverage }); - } - - let finalResults = []; + const finalResults = []; await Promise.all( - queries.map(async ({ q, coverage }) => { - await db.transaction('r', table, async () => { + universalQueries.map(query => { + const coverage = coveredIndex(table, query, queryAdditions, projection); + return db.transaction('r', table, async () => { + // First and Last start their IndexedDB request immediately. + // Create that request in the transaction that awaits it. + const q = runIndexedQuery(table, query, queryAdditions, Boolean(coverage)); if (coverage && !(q instanceof Promise)) { await q.eachKey((key, cursor) => { const pk = cursor.primaryKey; diff --git a/tests/migrations/ProtectedWasm/App/App.razor b/tests/migrations/ProtectedWasm/App/App.razor index f9ba7f51..94e872d6 100644 --- a/tests/migrations/ProtectedWasm/App/App.razor +++ b/tests/migrations/ProtectedWasm/App/App.razor @@ -188,6 +188,20 @@ "The cross-store batch insert failed."); Require((await references.ToListAsync()).Single().Target == "document", "The protected batch failed to persist the typed value."); + foreach (var (id, size) in new[] { (20, 400 * 1024), (21, 400 * 1024), (22, 2800 * 1024) }) + { + var secret = new string((char)('a' + id - 20), size); + Require(await table.InsertProtectedIfCurrentAsync(new RotationRow { Id = id, Secret = secret }, + table, advancedFence, row => row.Generation) == MagicConditionalMutationResult.Applied, + "The guarded setup for the protected terminal read failed."); + var first = await table.FirstOrDefaultAsync(row => row.Id == id); + var last = await table.LastOrDefaultAsync(row => row.Id == id); + Require(first?.Id == id && first.Secret == secret && last?.Id == id && last.Secret == secret, + "Indexed terminal reads did not round-trip the protected record."); + } + Require(await table.FirstOrDefaultAsync(row => row.Id == -1) is null && + await table.LastOrDefaultAsync(row => row.Id == -1) is null, + "An absent indexed key did not return the default value."); await Contexts.DeactivateAsync(); } diff --git a/tests/migrations/run.mjs b/tests/migrations/run.mjs index b5b72f73..171f4d84 100644 --- a/tests/migrations/run.mjs +++ b/tests/migrations/run.mjs @@ -1759,10 +1759,57 @@ test('rotation checkpoints preserve compound keys across protected and public st indexedKeys: ids.map(id => ['catalog', id]), publicRow: { id: 1, label: 'Public configuration' }, oldKeyRejected: true }); }); +test('indexed first and last start their requests inside the native read transaction', async ({ page }) => { + const result = await page.evaluate(async () => { + const { default: Dexie } = await import('/engine/dexie/dexie.js'); + const { magicQueryAsync } = await import('/engine/magicLinqToIndexedDb.js'); + const db = new Dexie('indexed-terminal-transaction'); + db.version(1).stores({ records: 'id, category' }); + await db.open(); + try { + const table = db.table('records'); + await table.bulkAdd([{ id: 1, category: 7 }, { id: 2, category: 7 }]); + const prototype = Object.getPrototypeOf(table.toCollection()); + let terminalCalls = 0; + for (const method of ['first', 'last']) { + const execute = prototype[method]; + prototype[method] = function (...args) { + terminalCalls++; + if (!Dexie.currentTransaction) { + // Let a request started prematurely resolve on a later task. + // An unrelated empty read transaction cannot stay alive for it. + return execute.apply(this, args).then(async row => { + await new Promise(resolve => setTimeout(resolve, 20)); + return row; + }); + } + return execute.apply(this, args); + }; + } + const query = async (property, value, additionFunction) => + (await magicQueryAsync(db, table, { + nodeType: 1, condition: { property, operation: 'Equal', value } + }, [{ additionFunction, intValue: 0 }])).map(row => row.id); + return { + first: await query('category', 7, 'first'), + last: await query('category', 7, 'last'), + present: await query('id', 2, 'first'), + missing: await query('id', 3, 'last'), + terminalCalls + }; + } catch (error) { + throw new Error(`${error.name}: ${error.message}`); + } finally { + db.close(); + } + }); + assert.deepEqual(result, { first: [1], last: [2], present: [2], missing: [], terminalCalls: 4 }); +}); + registerProtectedEngineTests({ test, schema, bundle, copy, inspect, read, loadPage, recoveryPackage, appendRevision, jsonAsset, assets, cliFixture, getOrigin: () => origin }); registerProtectedRegistrationTests({ test, schema, bundle, jsonAsset, copy, registrationStore, inspect, appendRevision, assets }); -const expectedScenarioCount = (cliFixture ? 143 : 139) + (process.env.MAGIC_ROTATION_BENCHMARK === "1" ? 9 : 0); +const expectedScenarioCount = (cliFixture ? 144 : 140) + (process.env.MAGIC_ROTATION_BENCHMARK === "1" ? 9 : 0); assert.equal(tests.length, expectedScenarioCount, 'Unexpected migration scenario count.'); if (listOnly) { From fda432a7f62eac680f7937d61f7f02d7780b3867 Mon Sep 17 00:00:00 2001 From: MagicCodingMan Date: Mon, 30 Nov 2026 23:55:14 -0500 Subject: [PATCH 2/2] Assert indexed terminal calls without pinning Dexie delegation --- tests/migrations/run.mjs | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/tests/migrations/run.mjs b/tests/migrations/run.mjs index 171f4d84..293559c7 100644 --- a/tests/migrations/run.mjs +++ b/tests/migrations/run.mjs @@ -1786,16 +1786,19 @@ test('indexed first and last start their requests inside the native read transac return execute.apply(this, args); }; } - const query = async (property, value, additionFunction) => - (await magicQueryAsync(db, table, { + const query = async (property, value, additionFunction) => { + const before = terminalCalls; + const rows = await magicQueryAsync(db, table, { nodeType: 1, condition: { property, operation: 'Equal', value } - }, [{ additionFunction, intValue: 0 }])).map(row => row.id); + }, [{ additionFunction, intValue: 0 }]); + if (terminalCalls === before) throw new Error("The query did not use the indexed terminal path."); + return rows.map(row => row.id); + }; return { first: await query('category', 7, 'first'), last: await query('category', 7, 'last'), present: await query('id', 2, 'first'), - missing: await query('id', 3, 'last'), - terminalCalls + missing: await query('id', 3, 'last') }; } catch (error) { throw new Error(`${error.name}: ${error.message}`); @@ -1803,7 +1806,7 @@ test('indexed first and last start their requests inside the native read transac db.close(); } }); - assert.deepEqual(result, { first: [1], last: [2], present: [2], missing: [], terminalCalls: 4 }); + assert.deepEqual(result, { first: [1], last: [2], present: [2], missing: [] }); }); registerProtectedEngineTests({ test, schema, bundle, copy, inspect, read, loadPage, recoveryPackage,