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

Updates to connector to fix concurrency #1

Merged
merged 2 commits into from
Oct 20, 2024
Merged
Show file tree
Hide file tree
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
Binary file added duck.db
Binary file not shown.
Binary file added duck.db.wal
Binary file not shown.
9 changes: 5 additions & 4 deletions src/duckduckapi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,9 @@ const connector: Connector<Configuration, State> = {
* @param metrics
*/
tryInitState(_: Configuration, __: unknown): Promise<State> {
const credentials: CredentialSchema = { url: DUCKDB_URL };
const client = new duckdb.Database(credentials.url, DUCKDB_CONFIG);
// const credentials: CredentialSchema = { url: DUCKDB_URL };
// const client = new duckdb.Database(credentials.url, DUCKDB_CONFIG);
const client = db;
return Promise.resolve({ client: client });
},

Expand Down Expand Up @@ -207,7 +208,7 @@ async function createDuckDBFile(schema: string): Promise<void> {

export interface duckduckapi {
dbSchema: string
loaderJob(): void;
loaderJob(db: duckdb.Database): void;
getFunctions(): void;
}

Expand All @@ -218,6 +219,6 @@ export async function makeConnector(dda: duckduckapi): Promise<Connector<Configu
*/
await createDuckDBFile(dda.dbSchema);
// spawn loaderjob execution on a cron
dda.loaderJob();
dda.loaderJob(db);
return Promise.resolve(connector);
}
7 changes: 3 additions & 4 deletions src/handlers/query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -397,8 +397,7 @@ export async function plan_queries(
if (configuration.config === null || configuration.config === undefined) {
throw new Forbidden("Connector is not properly configured", {});
}
let collection_alias: string =
configuration.config.collection_aliases[query.collection];
let collection_alias: string = configuration.config.collection_aliases[query.collection];
let query_plan: SQLQuery[];
if (query.variables) {
let promises = query.variables.map((varSet) => {
Expand Down Expand Up @@ -443,6 +442,7 @@ export async function plan_queries(

async function do_all(con: any, query: SQLQuery): Promise<any[]> {
return new Promise((resolve, reject) => {

con.all(query.sql, ...query.args, function (err: any, res: any) {
if (err) {
reject(err);
Expand All @@ -464,6 +464,7 @@ async function perform_query(
const row_set = JSON.parse(res[0]["data"] as string) as RowSet;
response.push(row_set);
}
con.close();
return response;
}

Expand All @@ -483,8 +484,6 @@ export async function do_query(
state: State,
query: QueryRequest
): Promise<QueryResponse> {
console.log(query);

if (is_query_function(query, configuration)) {
return await perform_query_function(state, query);
} else {
Expand Down
13 changes: 8 additions & 5 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import {start} from "@hasura/ndc-sdk-typescript";
import { makeConnector, duckduckapi, db} from "./duckduckapi";

import * as duckdb from "duckdb";
// get any api credentials from env vars

const calendar: duckduckapi = {
// DROP TABLE IF EXISTS users;
dbSchema: `
CREATE TABLE IF NOT EXISTS users (id int, name string);
`,
Expand All @@ -13,19 +14,21 @@ const calendar: duckduckapi = {
// Implementation for retrieving functions
},

loaderJob: () => {
loaderJob: (db: duckdb.Database) => {
console.log("Running loader job...");

async function insertLoop() {
let id = 100;
while (true) {
const con = db.connect();
// Insert a row into the table
await db.all(`
con.exec('BEGIN TRANSACTION');
con.all(`
INSERT INTO users (id, name) values (?, ?);
`, id, 'name'+id.toString(), (err)=>{console.log(err)});
con.run('COMMIT');
id++;
console.log(`Inserted row ${id}`);

con.close();
// Wait for 1 second before the next insertion
await new Promise(resolve => setTimeout(resolve, 1000));
}
Expand Down
Loading