-
Notifications
You must be signed in to change notification settings - Fork 120
/
async_bindings.ts
633 lines (591 loc) · 24.4 KB
/
async_bindings.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
import {
WorkerRequestType,
WorkerResponseType,
WorkerResponseVariant,
WorkerTaskVariant,
WorkerTask,
ConnectionID,
WorkerTaskReturnType,
} from './worker_request';
import { AsyncDuckDBBindings } from './async_bindings_interface';
import { Logger } from '../log';
import { AsyncDuckDBConnection } from './async_connection';
import { CSVInsertOptions, JSONInsertOptions, ArrowInsertOptions } from '../bindings/insert_options';
import { ScriptTokens } from '../bindings/tokens';
import { FileStatistics } from '../bindings/file_stats';
import { DuckDBConfig } from '../bindings/config';
import { InstantiationProgress } from '../bindings/progress';
import { arrowToSQLField } from '../json_typedef';
import { WebFile } from '../bindings/web_file';
import { DuckDBDataProtocol } from '../bindings';
const TEXT_ENCODER = new TextEncoder();
export class AsyncDuckDB implements AsyncDuckDBBindings {
/** The message handler */
protected readonly _onMessageHandler: (event: MessageEvent) => void;
/** The error handler */
protected readonly _onErrorHandler: (event: ErrorEvent) => void;
/** The close handler */
protected readonly _onCloseHandler: () => void;
/** Instantiate the module */
protected _onInstantiationProgress: ((p: InstantiationProgress) => void)[] = [];
/** The logger */
protected readonly _logger: Logger;
/** The worker */
protected _worker: Worker | null = null;
/** The promise for the worker shutdown */
protected _workerShutdownPromise: Promise<null> | null = null;
/** Make the worker as terminated */
protected _workerShutdownResolver: (value: PromiseLike<null> | null) => void = () => {};
/** The next message id */
protected _nextMessageId = 0;
/** The pending requests */
protected _pendingRequests: Map<number, WorkerTaskVariant> = new Map();
constructor(logger: Logger, worker: Worker | null = null) {
this._logger = logger;
this._onMessageHandler = this.onMessage.bind(this);
this._onErrorHandler = this.onError.bind(this);
this._onCloseHandler = this.onClose.bind(this);
if (worker != null) this.attach(worker);
}
/** Get the logger */
public get logger(): Logger {
return this._logger;
}
/** Attach to worker */
protected attach(worker: Worker): void {
this._worker = worker;
this._worker.addEventListener('message', this._onMessageHandler);
this._worker.addEventListener('error', this._onErrorHandler);
this._worker.addEventListener('close', this._onCloseHandler);
this._workerShutdownPromise = new Promise<null>(
(resolve: (value: PromiseLike<null> | null) => void, _reject: (reason?: void) => void) => {
this._workerShutdownResolver = resolve;
},
);
}
/** Detach from worker */
public detach(): void {
if (!this._worker) return;
this._worker.removeEventListener('message', this._onMessageHandler);
this._worker.removeEventListener('error', this._onErrorHandler);
this._worker.removeEventListener('close', this._onCloseHandler);
this._worker = null;
this._workerShutdownResolver(null);
this._workerShutdownPromise = null;
this._workerShutdownResolver = () => {};
}
/** Kill the worker */
public async terminate(): Promise<void> {
if (!this._worker) return;
this._worker.terminate();
//await this._workerShutdownPromise; TODO deadlocking in karma?
this._worker = null;
this._workerShutdownPromise = null;
this._workerShutdownResolver = () => {};
}
/** Post a task */
protected async postTask<W extends WorkerTaskVariant>(
task: W,
transfer: ArrayBuffer[] = [],
): Promise<WorkerTaskReturnType<W>> {
if (!this._worker) {
console.error('cannot send a message since the worker is not set!');
return undefined as any;
}
const mid = this._nextMessageId++;
this._pendingRequests.set(mid, task);
this._worker.postMessage(
{
messageId: mid,
type: task.type,
data: task.data,
},
transfer,
);
return (await task.promise) as WorkerTaskReturnType<W>;
}
/** Received a message */
protected onMessage(event: MessageEvent): void {
// Unassociated responses?
const response = event.data as WorkerResponseVariant;
switch (response.type) {
// Request failed?
case WorkerResponseType.LOG: {
this._logger.log(response.data);
return;
}
// Call progress callback
case WorkerResponseType.INSTANTIATE_PROGRESS: {
for (const p of this._onInstantiationProgress) {
p(response.data);
}
return;
}
}
// Get associated task
const task = this._pendingRequests.get(response.requestId);
if (!task) {
console.warn(`unassociated response: [${response.requestId}, ${response.type.toString()}]`);
return;
}
this._pendingRequests.delete(response.requestId);
// Request failed?
if (response.type == WorkerResponseType.ERROR) {
// Workaround for Firefox not being able to perform structured-clone on Native Errors
// https://bugzilla.mozilla.org/show_bug.cgi?id=1556604
const e = new Error(response.data.message);
e.name = response.data.name;
if (Object.getOwnPropertyDescriptor(e, 'stack')?.writable) {
e.stack = response.data.stack;
}
task.promiseRejecter(e);
return;
}
// Otherwise differentiate between the tasks first
switch (task.type) {
case WorkerRequestType.CLOSE_PREPARED:
case WorkerRequestType.COLLECT_FILE_STATISTICS:
case WorkerRequestType.COPY_FILE_TO_PATH:
case WorkerRequestType.DISCONNECT:
case WorkerRequestType.DROP_FILE:
case WorkerRequestType.DROP_FILES:
case WorkerRequestType.FLUSH_FILES:
case WorkerRequestType.INSERT_ARROW_FROM_IPC_STREAM:
case WorkerRequestType.INSERT_CSV_FROM_PATH:
case WorkerRequestType.INSERT_JSON_FROM_PATH:
case WorkerRequestType.OPEN:
case WorkerRequestType.PING:
case WorkerRequestType.REGISTER_FILE_BUFFER:
case WorkerRequestType.REGISTER_FILE_HANDLE:
case WorkerRequestType.REGISTER_FILE_URL:
case WorkerRequestType.RESET:
if (response.type == WorkerResponseType.OK) {
task.promiseResolver(response.data);
return;
}
break;
case WorkerRequestType.INSTANTIATE:
this._onInstantiationProgress = [];
if (response.type == WorkerResponseType.OK) {
task.promiseResolver(response.data);
return;
}
break;
case WorkerRequestType.GLOB_FILE_INFOS:
if (response.type == WorkerResponseType.FILE_INFOS) {
task.promiseResolver(response.data);
return;
}
break;
case WorkerRequestType.GET_VERSION:
if (response.type == WorkerResponseType.VERSION_STRING) {
task.promiseResolver(response.data);
return;
}
break;
case WorkerRequestType.GET_FEATURE_FLAGS:
if (response.type == WorkerResponseType.FEATURE_FLAGS) {
task.promiseResolver(response.data);
return;
}
break;
case WorkerRequestType.GET_TABLE_NAMES:
if (response.type == WorkerResponseType.TABLE_NAMES) {
task.promiseResolver(response.data);
return;
}
break;
case WorkerRequestType.TOKENIZE:
if (response.type == WorkerResponseType.SCRIPT_TOKENS) {
task.promiseResolver(response.data);
return;
}
break;
case WorkerRequestType.COPY_FILE_TO_BUFFER:
if (response.type == WorkerResponseType.FILE_BUFFER) {
task.promiseResolver(response.data);
return;
}
break;
case WorkerRequestType.EXPORT_FILE_STATISTICS:
if (response.type == WorkerResponseType.FILE_STATISTICS) {
task.promiseResolver(response.data);
return;
}
break;
case WorkerRequestType.CONNECT:
if (response.type == WorkerResponseType.CONNECTION_INFO) {
task.promiseResolver(response.data);
return;
}
break;
case WorkerRequestType.RUN_PREPARED:
case WorkerRequestType.RUN_QUERY:
if (response.type == WorkerResponseType.QUERY_RESULT) {
task.promiseResolver(response.data);
return;
}
break;
case WorkerRequestType.SEND_PREPARED:
if (response.type == WorkerResponseType.QUERY_RESULT_HEADER) {
task.promiseResolver(response.data);
return;
}
break;
case WorkerRequestType.START_PENDING_QUERY:
if (response.type == WorkerResponseType.QUERY_RESULT_HEADER_OR_NULL) {
task.promiseResolver(response.data);
return;
}
break;
case WorkerRequestType.POLL_PENDING_QUERY:
if (response.type == WorkerResponseType.QUERY_RESULT_HEADER_OR_NULL) {
task.promiseResolver(response.data);
return;
}
break;
case WorkerRequestType.CANCEL_PENDING_QUERY:
this._onInstantiationProgress = [];
if (response.type == WorkerResponseType.SUCCESS) {
task.promiseResolver(response.data);
return;
}
break;
case WorkerRequestType.FETCH_QUERY_RESULTS:
if (response.type == WorkerResponseType.QUERY_RESULT_CHUNK) {
task.promiseResolver(response.data);
return;
}
break;
case WorkerRequestType.CREATE_PREPARED:
if (response.type == WorkerResponseType.PREPARED_STATEMENT_ID) {
task.promiseResolver(response.data);
return;
}
break;
}
task.promiseRejecter(new Error(`unexpected response type: ${response.type.toString()}`));
}
/** Received an error */
protected onError(event: ErrorEvent): void {
console.error(event);
console.error(`error in duckdb worker: ${event.message}`);
this._pendingRequests.clear();
}
/** The worker was closed */
protected onClose(): void {
this._workerShutdownResolver(null);
if (this._pendingRequests.size != 0) {
console.warn(`worker terminated with ${this._pendingRequests.size} pending requests`);
return;
}
this._pendingRequests.clear();
}
/** Reset the duckdb */
public async reset(): Promise<null> {
const task = new WorkerTask<WorkerRequestType.RESET, null, null>(WorkerRequestType.RESET, null);
return await this.postTask(task);
}
/** Ping the worker thread */
public async ping(): Promise<any> {
const task = new WorkerTask<WorkerRequestType.PING, null, null>(WorkerRequestType.PING, null);
await this.postTask(task);
}
/** Try to drop a file */
public async dropFile(name: string): Promise<null> {
const task = new WorkerTask<WorkerRequestType.DROP_FILE, string, null>(WorkerRequestType.DROP_FILE, name);
return await this.postTask(task);
}
/** Try to drop files */
public async dropFiles(): Promise<null> {
const task = new WorkerTask<WorkerRequestType.DROP_FILES, null, null>(WorkerRequestType.DROP_FILES, null);
return await this.postTask(task);
}
/** Flush all files */
public async flushFiles(): Promise<null> {
const task = new WorkerTask<WorkerRequestType.FLUSH_FILES, null, null>(WorkerRequestType.FLUSH_FILES, null);
return await this.postTask(task);
}
/** Open the database */
public async instantiate(
mainModuleURL: string,
pthreadWorkerURL: string | null = null,
progress: (progress: InstantiationProgress) => void = _p => {},
): Promise<null> {
this._onInstantiationProgress.push(progress);
const task = new WorkerTask<WorkerRequestType.INSTANTIATE, [string, string | null], null>(
WorkerRequestType.INSTANTIATE,
[mainModuleURL, pthreadWorkerURL],
);
return await this.postTask(task);
}
/** Get the version */
public async getVersion(): Promise<string> {
const task = new WorkerTask<WorkerRequestType.GET_VERSION, null, string>(WorkerRequestType.GET_VERSION, null);
const version = await this.postTask(task);
return version;
}
/** Get the feature flags */
public async getFeatureFlags(): Promise<number> {
const task = new WorkerTask<WorkerRequestType.GET_FEATURE_FLAGS, null, number>(
WorkerRequestType.GET_FEATURE_FLAGS,
null,
);
const feature = await this.postTask(task);
return feature;
}
/** Open a new database */
public async open(config: DuckDBConfig): Promise<void> {
const task = new WorkerTask<WorkerRequestType.OPEN, DuckDBConfig, null>(WorkerRequestType.OPEN, config);
await this.postTask(task);
}
/** Tokenize a script text */
public async tokenize(text: string): Promise<ScriptTokens> {
const task = new WorkerTask<WorkerRequestType.TOKENIZE, string, ScriptTokens>(WorkerRequestType.TOKENIZE, text);
const tokens = await this.postTask(task);
return tokens;
}
/** Connect to the database */
public async connectInternal(): Promise<number> {
const task = new WorkerTask<WorkerRequestType.CONNECT, null, ConnectionID>(WorkerRequestType.CONNECT, null);
return await this.postTask(task);
}
/** Connect to the database */
public async connect(): Promise<AsyncDuckDBConnection> {
const cid = await this.connectInternal();
return new AsyncDuckDBConnection(this, cid);
}
/** Disconnect from the database */
public async disconnect(conn: ConnectionID): Promise<void> {
const task = new WorkerTask<WorkerRequestType.DISCONNECT, ConnectionID, null>(
WorkerRequestType.DISCONNECT,
conn,
);
await this.postTask(task);
}
/** Run a query */
public async runQuery(conn: ConnectionID, text: string): Promise<Uint8Array> {
const task = new WorkerTask<WorkerRequestType.RUN_QUERY, [ConnectionID, string], Uint8Array>(
WorkerRequestType.RUN_QUERY,
[conn, text],
);
return await this.postTask(task);
}
/** Start a pending query */
public async startPendingQuery(conn: ConnectionID, text: string): Promise<Uint8Array | null> {
const task = new WorkerTask<WorkerRequestType.START_PENDING_QUERY, [ConnectionID, string], Uint8Array | null>(
WorkerRequestType.START_PENDING_QUERY,
[conn, text],
);
return await this.postTask(task);
}
/** Poll a pending query */
public async pollPendingQuery(conn: ConnectionID): Promise<Uint8Array | null> {
const task = new WorkerTask<WorkerRequestType.POLL_PENDING_QUERY, ConnectionID, Uint8Array | null>(
WorkerRequestType.POLL_PENDING_QUERY,
conn,
);
return await this.postTask(task);
}
/** Cancel a pending query */
public async cancelPendingQuery(conn: ConnectionID): Promise<boolean> {
const task = new WorkerTask<WorkerRequestType.CANCEL_PENDING_QUERY, ConnectionID, boolean>(
WorkerRequestType.CANCEL_PENDING_QUERY,
conn,
);
return await this.postTask(task);
}
/** Fetch query results */
public async fetchQueryResults(conn: ConnectionID): Promise<Uint8Array> {
const task = new WorkerTask<WorkerRequestType.FETCH_QUERY_RESULTS, ConnectionID, Uint8Array>(
WorkerRequestType.FETCH_QUERY_RESULTS,
conn,
);
return await this.postTask(task);
}
/** Get table names */
public async getTableNames(conn: number, text: string): Promise<string[]> {
const task = new WorkerTask<WorkerRequestType.GET_TABLE_NAMES, [number, string], string[]>(
WorkerRequestType.GET_TABLE_NAMES,
[conn, text],
);
return await this.postTask(task);
}
/** Prepare a statement and return its identifier */
public async createPrepared(conn: number, text: string): Promise<number> {
const task = new WorkerTask<WorkerRequestType.CREATE_PREPARED, [number, string], number>(
WorkerRequestType.CREATE_PREPARED,
[conn, text],
);
return await this.postTask(task);
}
/** Close a prepared statement */
public async closePrepared(conn: number, statement: number): Promise<void> {
const task = new WorkerTask<WorkerRequestType.CLOSE_PREPARED, [number, number], null>(
WorkerRequestType.CLOSE_PREPARED,
[conn, statement],
);
await this.postTask(task);
}
/** Execute a prepared statement and return the full result */
public async runPrepared(conn: number, statement: number, params: any[]): Promise<Uint8Array> {
const task = new WorkerTask<WorkerRequestType.RUN_PREPARED, [ConnectionID, number, any[]], Uint8Array>(
WorkerRequestType.RUN_PREPARED,
[conn, statement, params],
);
return await this.postTask(task);
}
/** Execute a prepared statement and stream the result */
public async sendPrepared(conn: number, statement: number, params: any[]): Promise<Uint8Array> {
const task = new WorkerTask<WorkerRequestType.SEND_PREPARED, [ConnectionID, number, any[]], Uint8Array>(
WorkerRequestType.SEND_PREPARED,
[conn, statement, params],
);
return await this.postTask(task);
}
/** Glob file infos */
public async globFiles(path: string): Promise<WebFile[]> {
const task = new WorkerTask<WorkerRequestType.GLOB_FILE_INFOS, string, WebFile[]>(
WorkerRequestType.GLOB_FILE_INFOS,
path,
);
return await this.postTask(task);
}
/** Register file text */
public async registerFileText(name: string, text: string): Promise<void> {
const buffer = TEXT_ENCODER.encode(text);
await this.registerFileBuffer(name, buffer);
}
/** Register a file path. */
public async registerFileURL(
name: string,
url: string,
proto: DuckDBDataProtocol,
directIO: boolean,
): Promise<void> {
if (url === undefined) {
url = name;
}
const task = new WorkerTask<
WorkerRequestType.REGISTER_FILE_URL,
[string, string, DuckDBDataProtocol, boolean],
null
>(WorkerRequestType.REGISTER_FILE_URL, [name, url, proto, directIO]);
await this.postTask(task);
}
/** Register an empty file buffer. */
public async registerEmptyFileBuffer(name: string): Promise<void> {
const task = new WorkerTask<WorkerRequestType.REGISTER_FILE_BUFFER, [string, Uint8Array], null>(
WorkerRequestType.REGISTER_FILE_BUFFER,
[name, new Uint8Array()],
);
await this.postTask(task);
}
/** Register a file buffer. */
public async registerFileBuffer(name: string, buffer: Uint8Array): Promise<void> {
const task = new WorkerTask<WorkerRequestType.REGISTER_FILE_BUFFER, [string, Uint8Array], null>(
WorkerRequestType.REGISTER_FILE_BUFFER,
[name, buffer],
);
await this.postTask(task, [buffer.buffer]);
}
/** Register a file handle. */
public async registerFileHandle<HandleType>(
name: string,
handle: HandleType,
protocol: DuckDBDataProtocol,
directIO: boolean,
): Promise<void> {
const task = new WorkerTask<
WorkerRequestType.REGISTER_FILE_HANDLE,
[string, any, DuckDBDataProtocol, boolean],
null
>(WorkerRequestType.REGISTER_FILE_HANDLE, [name, handle, protocol, directIO]);
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>(
WorkerRequestType.COLLECT_FILE_STATISTICS,
[name, enable],
);
await this.postTask(task, []);
}
/** Export file statistics */
public async exportFileStatistics(name: string): Promise<FileStatistics> {
const task = new WorkerTask<WorkerRequestType.EXPORT_FILE_STATISTICS, string, FileStatistics>(
WorkerRequestType.EXPORT_FILE_STATISTICS,
name,
);
return await this.postTask(task, []);
}
/** Copy a file to a buffer. */
public async copyFileToBuffer(name: string): Promise<Uint8Array> {
const task = new WorkerTask<WorkerRequestType.COPY_FILE_TO_BUFFER, string, Uint8Array>(
WorkerRequestType.COPY_FILE_TO_BUFFER,
name,
);
return await this.postTask(task);
}
/** Copy a file to a path. */
public async copyFileToPath(name: string, path: string): Promise<void> {
const task = new WorkerTask<WorkerRequestType.COPY_FILE_TO_PATH, [string, string], null>(
WorkerRequestType.COPY_FILE_TO_PATH,
[name, path],
);
await this.postTask(task);
}
/** Insert arrow from an ipc stream */
public async insertArrowFromIPCStream(
conn: ConnectionID,
buffer: Uint8Array,
options?: ArrowInsertOptions,
): Promise<void> {
if (buffer.length == 0) return;
// Pass to the worker
const task = new WorkerTask<
WorkerRequestType.INSERT_ARROW_FROM_IPC_STREAM,
[number, Uint8Array, ArrowInsertOptions | undefined],
null
>(WorkerRequestType.INSERT_ARROW_FROM_IPC_STREAM, [conn, buffer, options]);
await this.postTask(task, [buffer.buffer]);
}
/** Insert a csv file */
public async insertCSVFromPath(conn: ConnectionID, path: string, options: CSVInsertOptions): Promise<void> {
// Flatten the table options
if (options.columns !== undefined) {
const out = [];
for (const k in options.columns) {
const type = options.columns[k];
out.push(arrowToSQLField(k, type));
}
options.columnsFlat = out;
delete options.columns;
}
// Pass to the worker
const task = new WorkerTask<WorkerRequestType.INSERT_CSV_FROM_PATH, [number, string, CSVInsertOptions], null>(
WorkerRequestType.INSERT_CSV_FROM_PATH,
[conn, path, options],
);
await this.postTask(task);
}
/** Insert a json file */
public async insertJSONFromPath(conn: ConnectionID, path: string, options: JSONInsertOptions): Promise<void> {
// Flatten the table options
if (options.columns !== undefined) {
const out = [];
for (const k in options.columns) {
const type = options.columns[k];
out.push(arrowToSQLField(k, type));
}
options.columnsFlat = out;
delete options.columns;
}
// Pass to the worker
const task = new WorkerTask<WorkerRequestType.INSERT_JSON_FROM_PATH, [number, string, JSONInsertOptions], null>(
WorkerRequestType.INSERT_JSON_FROM_PATH,
[conn, path, options],
);
await this.postTask(task);
}
}