Skip to content

Commit

Permalink
Merge pull request #119 from rhashimoto/issue-00118
Browse files Browse the repository at this point in the history
Fix #118. Inconsistent file size bug.
  • Loading branch information
rhashimoto authored Oct 30, 2023
2 parents 6e2fcb5 + b6d8fef commit b3abe74
Show file tree
Hide file tree
Showing 6 changed files with 135 additions and 111 deletions.
68 changes: 34 additions & 34 deletions dist/wa-sqlite-async.mjs

Large diffs are not rendered by default.

Binary file modified dist/wa-sqlite-async.wasm
Binary file not shown.
148 changes: 74 additions & 74 deletions dist/wa-sqlite.mjs

Large diffs are not rendered by default.

Binary file modified dist/wa-sqlite.wasm
Binary file not shown.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "wa-sqlite",
"version": "0.9.6",
"version": "0.9.8",
"type": "module",
"main": "src/sqlite-api.js",
"types": "src/types/index.d.ts",
Expand Down
28 changes: 26 additions & 2 deletions src/examples/IDBBatchAtomicVFS.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ function log(...args) {
* @property {string} path
* @property {number} flags
* @property {FileBlock} block0
* @property {boolean} isMetadataChanged
* @property {WebLocks} locks
*
* @property {Set<number>} [changedPages]
Expand Down Expand Up @@ -94,6 +95,7 @@ export class IDBBatchAtomicVFS extends VFS.Base {
path: url.pathname,
flags,
block0: null,
isMetadataChanged: true,
locks: new WebLocks(url.pathname)
};
this.#mapIdToFile.set(fileId, file);
Expand Down Expand Up @@ -241,12 +243,17 @@ export class IDBBatchAtomicVFS extends VFS.Base {
log(`xWrite ${file.path} ${pData.byteLength} ${iOffset}`);

try {
// Update file size if appending.
const prevFileSize = file.block0.fileSize;
if (file.block0.fileSize < iOffset + pData.byteLength) {
file.block0.fileSize = iOffset + pData.byteLength;
file.isMetadataChanged = true;
}

// Convert the write directly into an IndexedDB object. Our assumption
// is that SQLite will only overwrite data with an xWrite of the same
// offset and size unless the database page size changes, except when
// changing database page size which is handled by #reblockIfNeeded().
const prevFileSize = file.block0.fileSize;
file.block0.fileSize = Math.max(file.block0.fileSize, iOffset + pData.byteLength);
const block = iOffset === 0 ? file.block0 : {
path: file.path,
offset: -iOffset,
Expand All @@ -271,6 +278,9 @@ export class IDBBatchAtomicVFS extends VFS.Base {
// Not a batch atomic write so write through.
this.#idb.run('readwrite', ({blocks}) => blocks.put(block));
}

// Clear dirty flag if page 0 was written.
file.isMetadataChanged = iOffset === 0 ? false : file.isMetadataChanged;
return VFS.SQLITE_OK;
} catch (e) {
console.error(e);
Expand Down Expand Up @@ -485,6 +495,18 @@ export class IDBBatchAtomicVFS extends VFS.Base {
return VFS.SQLITE_IOERR;
}
}

if (file.isMetadataChanged) {
// Metadata has changed so write block 0 to IndexedDB.
try {
this.#idb.run('readwrite', async ({blocks}) => {
await blocks.put(file.block0);
});
} catch (e) {
console.error(e);
return VFS.SQLITE_IOERR;
}
}
return VFS.SQLITE_OK;

case 22: // SQLITE_FCNTL_COMMIT_PHASETWO
Expand Down Expand Up @@ -522,6 +544,7 @@ export class IDBBatchAtomicVFS extends VFS.Base {
block0.data = block0.data.slice();
const changedPages = file.changedPages;
file.changedPages = null;
file.isMetadataChanged = false;
this.#idb.run('readwrite', async ({blocks})=> {
// Write block 0 to commit the new version.
blocks.put(block0);
Expand Down Expand Up @@ -557,6 +580,7 @@ export class IDBBatchAtomicVFS extends VFS.Base {
// be left in IndexedDB to be removed by the next atomic write
// transaction.
file.changedPages = null;
file.isMetadataChanged = false;
file.block0 = await this.#idb.run('readonly', ({blocks}) => {
return blocks.get([file.path, 0, file.block0.version + 1]);
});
Expand Down

0 comments on commit b3abe74

Please sign in to comment.