Skip to content

Commit

Permalink
Ensure transaction state exists in jTruncate(). (#247)
Browse files Browse the repository at this point in the history
Co-authored-by: Roy Hashimoto <[email protected]>
  • Loading branch information
rhashimoto and shoestringresearch authored Feb 2, 2025
1 parent 32d9fc0 commit 91cff2b
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 16 deletions.
9 changes: 5 additions & 4 deletions demo/file/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import * as VFS from "../../src/VFS.js";
import { IDBBatchAtomicVFS } from "../../src/examples/IDBBatchAtomicVFS.js";
import { IDBBatchAtomicVFS as MyVFS } from "../../src/examples/IDBBatchAtomicVFS.js";
// import { IDBMirrorVFS as MyVFS } from "../../src/examples/IDBMirrorVFS.js";

const SEARCH_PARAMS = new URLSearchParams(location.search);
const IDB_NAME = SEARCH_PARAMS.get('idb') ?? 'sqlite-vfs';
Expand Down Expand Up @@ -43,7 +44,7 @@ document.getElementById('file-fetch').addEventListener('click', async () => {
let vfs;
try {
log(`Importing to IndexedDB ${IDB_NAME}, path ${DB_NAME}`);
vfs = await IDBBatchAtomicVFS.create(IDB_NAME, null);
vfs = await MyVFS.create(IDB_NAME, null);

// @ts-ignore
const importURL = document.getElementById('file-url').value;
Expand All @@ -69,7 +70,7 @@ document.getElementById('file-import').addEventListener('change', async event =>
let vfs;
try {
log(`Importing to IndexedDB ${IDB_NAME}, path ${DB_NAME}`);
vfs = await IDBBatchAtomicVFS.create(IDB_NAME, null);
vfs = await MyVFS.create(IDB_NAME, null);
// @ts-ignore
await importDatabase(vfs, DB_NAME, event.target.files[0].stream());
log('Import complete');
Expand All @@ -87,7 +88,7 @@ document.getElementById('file-import').addEventListener('change', async event =>
});

/**
* @param {IDBBatchAtomicVFS} vfs
* @param {MyVFS} vfs
* @param {string} path
* @param {ReadableStream} stream
*/
Expand Down
5 changes: 3 additions & 2 deletions demo/file/service-worker.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import * as VFS from "../../src/VFS.js";
import { IDBBatchAtomicVFS } from "../../src/examples/IDBBatchAtomicVFS.js";
import { IDBBatchAtomicVFS as MyVFS } from "../../src/examples/IDBBatchAtomicVFS.js";
// import { IDBMirrorVFS as MyVFS } from "../../src/examples/IDBMirrorVFS.js";

// Install the service worker as soon as possible.
globalThis.addEventListener('install', (/** @type {ExtendableEvent} */ event) => {
Expand All @@ -26,7 +27,7 @@ globalThis.addEventListener('fetch', async (/** @type {FetchEvent} */ event) =>

return event.respondWith((async () => {
// Create the VFS and streaming source using the request parameters.
const vfs = await IDBBatchAtomicVFS.create(url.searchParams.get('idb'), null);
const vfs = await MyVFS.create(url.searchParams.get('idb'), null);
const path = url.searchParams.get('db');
const source = new DatabaseSource(vfs, path);

Expand Down
5 changes: 3 additions & 2 deletions demo/file/verifier.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import SQLiteESMFactory from '../../dist/wa-sqlite-async.mjs';
import * as SQLite from '../../src/sqlite-api.js';
import { IDBBatchAtomicVFS } from '../../src/examples/IDBBatchAtomicVFS.js';
import { IDBBatchAtomicVFS as MyVFS } from "../../src/examples/IDBBatchAtomicVFS.js";
// import { IDBMirrorVFS as MyVFS } from "../../src/examples/IDBMirrorVFS.js";

const SEARCH_PARAMS = new URLSearchParams(location.search);
const IDB_NAME = SEARCH_PARAMS.get('idb') ?? 'sqlite-vfs';
Expand All @@ -10,7 +11,7 @@ const DB_NAME = SEARCH_PARAMS.get('db') ?? 'sqlite.db';
const module = await SQLiteESMFactory();
const sqlite3 = SQLite.Factory(module);

const vfs = await IDBBatchAtomicVFS.create(IDB_NAME, module);
const vfs = await MyVFS.create(IDB_NAME, module);
// @ts-ignore
sqlite3.vfs_register(vfs, true);

Expand Down
24 changes: 16 additions & 8 deletions src/examples/IDBMirrorVFS.js
Original file line number Diff line number Diff line change
Expand Up @@ -332,14 +332,7 @@ export class IDBMirrorVFS extends FacadeVFS {
const file = this.#mapIdToFile.get(fileId);

if (file.flags & VFS.SQLITE_OPEN_MAIN_DB) {
if (!file.txActive) {
file.txActive = {
path: file.path,
txId: file.viewTx.txId + 1,
blocks: new Map(),
fileSize: file.blockSize * file.blocks.size,
};
}
this.#requireTxActive(file);
file.txActive.blocks.set(iOffset, pData.slice());
file.txActive.fileSize = Math.max(file.txActive.fileSize, iOffset + pData.byteLength);
file.blockSize = pData.byteLength;
Expand Down Expand Up @@ -375,6 +368,7 @@ export class IDBMirrorVFS extends FacadeVFS {
const file = this.#mapIdToFile.get(fileId);

if (file.flags & VFS.SQLITE_OPEN_MAIN_DB) {
this.#requireTxActive(file);
file.txActive.fileSize = iSize;
} else {
// All files that are not main databases are stored in a single
Expand Down Expand Up @@ -717,6 +711,20 @@ export class IDBMirrorVFS extends FacadeVFS {
file.txWriteHint = false;
}

/**
* @param {File} file
*/
#requireTxActive(file) {
if (!file.txActive) {
file.txActive = {
path: file.path,
txId: file.viewTx.txId + 1,
blocks: new Map(),
fileSize: file.blockSize * file.blocks.size,
};
}
}

/**
* @param {string} path
* @returns {Promise}
Expand Down
3 changes: 3 additions & 0 deletions src/examples/OPFSPermutedVFS.js
Original file line number Diff line number Diff line change
Expand Up @@ -463,6 +463,9 @@ export class OPFSPermutedVFS extends FacadeVFS {
const file = this.#mapIdToFile.get(fileId);
if ((file.flags & VFS.SQLITE_OPEN_MAIN_DB) && !file.txIsOverwrite) {
file.abortController.signal.throwIfAborted();
if (!file.txActive) {
this.#beginTx(file);
}
file.txActive.fileSize = iSize;

// Remove now obsolete pages from file.txActive.pages
Expand Down

0 comments on commit 91cff2b

Please sign in to comment.