-
Notifications
You must be signed in to change notification settings - Fork 152
fix: respect access mode and file open flags #1962
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
sumimakito
wants to merge
1
commit into
duckdb:main
Choose a base branch
from
sumimakito:fix/db-open
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+249
−52
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
import * as duckdb from '../src/'; | ||
import { tmpdir } from 'os'; | ||
import { randomUUID } from 'crypto'; | ||
import path from 'path'; | ||
import { unlink } from 'fs/promises'; | ||
|
||
export function testNodeFS(db: () => duckdb.AsyncDuckDB): void { | ||
const files: string[] = []; | ||
|
||
afterAll(async () => { | ||
await Promise.all(files.map(file => unlink(file).catch(() => {}))); | ||
await db().flushFiles(); | ||
await db().dropFiles(); | ||
}); | ||
|
||
describe('Node FS', () => { | ||
it('Should not create an empty DB file in read-only mode for non-existent path', async () => { | ||
const tmp = tmpdir(); | ||
const filename = `duckdb_test_${randomUUID().replace(/-/g, '')}`; | ||
files.push(path.join(tmp, filename)); | ||
|
||
await expectAsync( | ||
db().open({ | ||
path: path.join(tmp, filename), | ||
accessMode: duckdb.DuckDBAccessMode.READ_ONLY, | ||
}), | ||
).toBeRejectedWithError(/database does not exist/); | ||
}); | ||
|
||
it('Should create DB file in read-write mode for non-existent path', async () => { | ||
const tmp = tmpdir(); | ||
const filename = `duckdb_test_${randomUUID().replace(/-/g, '')}`; | ||
files.push(path.join(tmp, filename)); | ||
|
||
await expectAsync( | ||
db().open({ | ||
path: path.join(tmp, filename), | ||
accessMode: duckdb.DuckDBAccessMode.READ_WRITE, | ||
}), | ||
).toBeResolved(); | ||
}); | ||
|
||
it('Should create an empty DB file in read-only mode for non-existent path with direct I/O', async () => { | ||
const tmp = tmpdir(); | ||
const filename = `duckdb_test_${randomUUID().replace(/-/g, '')}`; | ||
files.push(path.join(tmp, filename)); | ||
|
||
await expectAsync( | ||
db().open({ | ||
path: path.join(tmp, filename), | ||
accessMode: duckdb.DuckDBAccessMode.READ_ONLY, | ||
useDirectIO: true, | ||
}), | ||
).toBeRejectedWithError(/database does not exist/); | ||
}); | ||
|
||
it('Should create DB file in read-write mode for non-existent path with direct I/O', async () => { | ||
const tmp = tmpdir(); | ||
const filename = `duckdb_test_${randomUUID().replace(/-/g, '')}`; | ||
files.push(path.join(tmp, filename)); | ||
|
||
await expectAsync( | ||
db().open({ | ||
path: path.join(tmp, filename), | ||
accessMode: duckdb.DuckDBAccessMode.READ_WRITE, | ||
useDirectIO: true, | ||
}), | ||
).toBeResolved(); | ||
}); | ||
}); | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Five lines below this, where we call createSyncAccessHandle, we can pass a new optional mode to OPFS to further enforce the access mode: https://developer.mozilla.org/en-US/docs/Web/API/FileSystemFileHandle/createSyncAccessHandle#mode
If the access mode is passed to createSyncAccessHandle, it would allow read-only connections to be opened simultaneously (for example when using the same app in multiple tabs). A big win for duckdb-wasm + OPFS.
The option is only implemented in Chrome and Edge right now, but issues exist to implement in both Webkit and Firefox:
https://bugs.webkit.org/show_bug.cgi?id=283959
https://bugzilla.mozilla.org/show_bug.cgi?id=1949462
And tests have already been added to the Web Platform Tests dashboard:
https://wpt.fyi/results/fs/FileSystemFileHandle-sync-access-handle-lock-modes.https.tentative.worker.html?label=experimental&label=master&aligned
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@amiller-gh
I believe it's fine for this PR to focus only on a single window.
I think we should first handle single-window support before addressing multiple windows.
Because the mode argument of the createSyncAccessHandle method was introduced to specify the file access mode. However, this argument has not yet been enabled in lib.webworker.d.ts (the Web Worker type definition file). As a result, using this argument in TypeScript may cause a type error. To resolve this issue, you need to either wait for the type definition file to be updated or extend the type definitions within your project.
And, I plan to create a PR for handling multiple windows, so this PR needs to be merged for that.