Skip to content

Commit

Permalink
Add registerOPFSFileName to API, available in the shell via
Browse files Browse the repository at this point in the history
  • Loading branch information
carlopi committed Jan 16, 2025
1 parent 72e03b4 commit f848df5
Show file tree
Hide file tree
Showing 7 changed files with 57 additions and 0 deletions.
13 changes: 13 additions & 0 deletions packages/duckdb-wasm-shell/crate/src/duckdb/async_duckdb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,11 @@ extern "C" {
conn: ConnectionID,
) -> Result<JsValue, JsValue>;

#[wasm_bindgen(catch, method, js_name = "registerOPFSFileName")]
async fn register_opfs_file_name(
this: &JsAsyncDuckDB,
text: &str,
) -> Result<JsValue, JsValue>;
#[wasm_bindgen(catch, method, js_name = "collectFileStatistics")]
async fn collect_file_statistics(
this: &JsAsyncDuckDB,
Expand Down Expand Up @@ -158,6 +163,14 @@ impl AsyncDuckDB {
Ok(())
}

pub async fn register_opfs_file_name(
&self,
file: &str,
) -> Result<(), js_sys::Error> {
self.bindings.register_opfs_file_name(file).await?;
Ok(())
}

/// Enable file statistics
pub async fn export_file_statistics(
&self,
Expand Down
20 changes: 20 additions & 0 deletions packages/duckdb-wasm-shell/crate/src/shell.rs
Original file line number Diff line number Diff line change
Expand Up @@ -377,6 +377,18 @@ impl Shell {
Shell::with(|s| s.write(&buffer));
}

pub fn register_opfs_file_name(name: &str) {
let db_ptr = Shell::with(|s| s.db.clone());
let name_copy = name.to_string();
spawn_local(async move {
let db = match db_ptr {
Some(ref db) => db.read().unwrap(),
None => return,
};
db.register_opfs_file_name(&name_copy).await.unwrap();
});
}

pub fn collect_file_statistics(name: &str, enable: bool) {
let db_ptr = Shell::with(|s| s.db.clone());
let name_copy = name.to_string();
Expand Down Expand Up @@ -532,6 +544,13 @@ impl Shell {
}
}
}
"register" => {
let filename = args[subcmd.len()..].trim();
db.register_opfs_file_name(filename).await.unwrap();
Shell::with_mut(|s| {
s.writeln(&format!("Registering OPFS file handle for: {}", filename))
});
}
"track" => {
let filename = args[subcmd.len()..].trim();
db.collect_file_statistics(filename, true).await.unwrap();
Expand Down Expand Up @@ -666,6 +685,7 @@ impl Shell {
".files drop Drop all files.\r\n",
".files drop $FILE Drop a single file.\r\n",
".files track $FILE Collect file statistics.\r\n",
".files register $FILE Register OPFS file handle.\r\n",
".files paging $FILE Show file paging.\r\n",
".files reads $FILE Show file reads.\r\n",
".open $FILE Open database file.\r\n",
Expand Down
5 changes: 5 additions & 0 deletions packages/duckdb-wasm-shell/crate/src/shell_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,11 @@ pub fn writeln(text: &str) {
Shell::with(|s| s.writeln(text));
}

#[wasm_bindgen(js_name = "registerOPFSFileName")]
pub fn register_opfs_file_name(name: &str) {
Shell::register_opfs_file_name(name);
}

#[wasm_bindgen(js_name = "collectFileStatistics")]
pub fn collect_file_statistics(name: &str, enable: bool) {
Shell::collect_file_statistics(name, enable);
Expand Down
1 change: 1 addition & 0 deletions packages/duckdb-wasm/src/bindings/bindings_interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ export interface DuckDBBindings {
flushFiles(): void;
copyFileToPath(name: string, path: string): void;
copyFileToBuffer(name: string): Uint8Array;
registerOPFSFileName(file: string): void;
collectFileStatistics(file: string, enable: boolean): void;
exportFileStatistics(file: string): FileStatistics;
}
10 changes: 10 additions & 0 deletions packages/duckdb-wasm/src/parallel/async_bindings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,7 @@ export class AsyncDuckDB implements AsyncDuckDBBindings {
switch (task.type) {
case WorkerRequestType.CLOSE_PREPARED:
case WorkerRequestType.COLLECT_FILE_STATISTICS:
case WorkerRequestType.REGISTER_OPFS_FILE_NAME:
case WorkerRequestType.COPY_FILE_TO_PATH:
case WorkerRequestType.DISCONNECT:
case WorkerRequestType.DROP_FILE:
Expand Down Expand Up @@ -546,6 +547,15 @@ export class AsyncDuckDB implements AsyncDuckDBBindings {
await this.postTask(task, []);
}

/** Enable file statistics */
public async registerOPFSFileName(name: string): Promise<void> {
const task = new WorkerTask<WorkerRequestType.REGISTER_OPFS_FILE_NAME, [string], null>(
WorkerRequestType.REGISTER_OPFS_FILE_NAME,
[name],
);
await this.postTask(task, []);
}

/** Enable file statistics */
public async collectFileStatistics(name: string, enable: boolean): Promise<void> {
const task = new WorkerTask<WorkerRequestType.COLLECT_FILE_STATISTICS, [string, boolean], null>(
Expand Down
5 changes: 5 additions & 0 deletions packages/duckdb-wasm/src/parallel/worker_dispatcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -360,6 +360,11 @@ export abstract class AsyncDuckDBDispatcher implements Logger {
this.sendOK(request);
break;

case WorkerRequestType.REGISTER_OPFS_FILE_NAME:
this._bindings.registerOPFSFileName(request.data[0]);
this.sendOK(request);
break;

case WorkerRequestType.EXPORT_FILE_STATISTICS: {
this.postMessage(
{
Expand Down
3 changes: 3 additions & 0 deletions packages/duckdb-wasm/src/parallel/worker_request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export enum WorkerRequestType {
CANCEL_PENDING_QUERY = 'CANCEL_PENDING_QUERY',
CLOSE_PREPARED = 'CLOSE_PREPARED',
COLLECT_FILE_STATISTICS = 'COLLECT_FILE_STATISTICS',
REGISTER_OPFS_FILE_NAME = 'REGISTER_OPFS_FILE_NAME',
CONNECT = 'CONNECT',
COPY_FILE_TO_BUFFER = 'COPY_FILE_TO_BUFFER',
COPY_FILE_TO_PATH = 'COPY_FILE_TO_PATH',
Expand Down Expand Up @@ -108,6 +109,7 @@ export type WorkerRequestVariant =
| WorkerRequest<WorkerRequestType.CLOSE_PREPARED, [ConnectionID, StatementID]>
| WorkerRequest<WorkerRequestType.CANCEL_PENDING_QUERY, number>
| WorkerRequest<WorkerRequestType.COLLECT_FILE_STATISTICS, [string, boolean]>
| WorkerRequest<WorkerRequestType.REGISTER_OPFS_FILE_NAME, [string]>
| WorkerRequest<WorkerRequestType.CONNECT, null>
| WorkerRequest<WorkerRequestType.COPY_FILE_TO_BUFFER, string>
| WorkerRequest<WorkerRequestType.COPY_FILE_TO_PATH, [string, string]>
Expand Down Expand Up @@ -166,6 +168,7 @@ export type WorkerResponseVariant =

export type WorkerTaskVariant =
| WorkerTask<WorkerRequestType.COLLECT_FILE_STATISTICS, [string, boolean], null>
| WorkerTask<WorkerRequestType.REGISTER_OPFS_FILE_NAME, [string], null>
| WorkerTask<WorkerRequestType.CLOSE_PREPARED, [number, number], null>
| WorkerTask<WorkerRequestType.CONNECT, null, ConnectionID>
| WorkerTask<WorkerRequestType.COPY_FILE_TO_BUFFER, string, Uint8Array>
Expand Down

0 comments on commit f848df5

Please sign in to comment.