Skip to content
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

serialize DuckDBClient.sql #1728

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 23 additions & 4 deletions src/client/stdlib/duckdb.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,24 @@ export function registerTable(name, source) {
}
}

export async function sql(strings, ...args) {
return (await getDefaultClient()).query(strings.join("?"), args);
function queue(f) {
let current;
let version = 0;
return async function () {
const v = ++version;
try {
await current;
} catch {
// ignore errors
}
if (version !== v) throw new Error("invalidated");
return (current = f.apply(this, arguments));
};
}

export async function sql() {
const db = await getDefaultClient();
return db.sql.apply(db, arguments);
}

export async function getDefaultClient() {
Expand All @@ -70,7 +86,8 @@ export async function getDefaultClient() {
export class DuckDBClient {
constructor(db) {
Object.defineProperties(this, {
_db: {value: db}
_db: {value: db},
_queue: {value: new WeakMap()}
});
}

Expand Down Expand Up @@ -133,7 +150,9 @@ export class DuckDBClient {
}

async sql(strings, ...args) {
return await this.query(strings.join("?"), args);
let sql = this._queue.get(strings);
if (!sql) this._queue.set(strings, (sql = queue((args) => this.query(strings.join("?"), args))));
return sql(args);
}

queryTag(strings, ...params) {
Expand Down