Skip to content

Commit

Permalink
Fix #118. Inconsistent file size bug.
Browse files Browse the repository at this point in the history
  • Loading branch information
shoestringresearch committed Oct 29, 2023
1 parent 6e2fcb5 commit 2381976
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 3 deletions.
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
27 changes: 25 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

0 comments on commit 2381976

Please sign in to comment.