@uwdata/mosaic-core 0.17.0 → 0.18.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +47 -0
- package/README.md +0 -1
- package/dist/src/Coordinator.d.ts +147 -0
- package/dist/src/Coordinator.d.ts.map +1 -0
- package/dist/src/Coordinator.js +269 -0
- package/dist/src/Coordinator.js.map +1 -0
- package/dist/src/MosaicClient.d.ts +138 -0
- package/dist/src/MosaicClient.d.ts.map +1 -0
- package/dist/src/MosaicClient.js +213 -0
- package/dist/src/MosaicClient.js.map +1 -0
- package/dist/src/Param.d.ts +56 -0
- package/dist/src/Param.d.ts.map +1 -0
- package/dist/src/Param.js +89 -0
- package/dist/src/Param.js.map +1 -0
- package/dist/src/QueryConsolidator.d.ts +11 -0
- package/dist/src/QueryConsolidator.d.ts.map +1 -0
- package/dist/src/QueryConsolidator.js +249 -0
- package/dist/src/QueryConsolidator.js.map +1 -0
- package/dist/src/QueryManager.d.ts +77 -0
- package/dist/src/QueryManager.d.ts.map +1 -0
- package/dist/src/QueryManager.js +174 -0
- package/dist/src/QueryManager.js.map +1 -0
- package/dist/src/Selection.d.ts +222 -0
- package/dist/src/Selection.d.ts.map +1 -0
- package/dist/src/Selection.js +319 -0
- package/dist/src/Selection.js.map +1 -0
- package/dist/src/SelectionClause.d.ts +192 -0
- package/dist/src/SelectionClause.d.ts.map +1 -0
- package/dist/src/SelectionClause.js +126 -0
- package/dist/src/SelectionClause.js.map +1 -0
- package/dist/src/connectors/Connector.d.ts +26 -0
- package/dist/src/connectors/Connector.d.ts.map +1 -0
- package/dist/src/connectors/Connector.js +2 -0
- package/dist/src/connectors/Connector.js.map +1 -0
- package/dist/src/connectors/rest.d.ts +24 -0
- package/dist/src/connectors/rest.d.ts.map +1 -0
- package/dist/src/connectors/rest.js +37 -0
- package/dist/src/connectors/rest.js.map +1 -0
- package/dist/src/connectors/socket.d.ts +40 -0
- package/dist/src/connectors/socket.d.ts.map +1 -0
- package/dist/src/connectors/socket.js +115 -0
- package/dist/src/connectors/socket.js.map +1 -0
- package/dist/src/connectors/wasm.d.ts +53 -0
- package/dist/src/connectors/wasm.d.ts.map +1 -0
- package/dist/src/connectors/wasm.js +113 -0
- package/dist/src/connectors/wasm.js.map +1 -0
- package/dist/src/index.d.ts +28 -0
- package/dist/src/index.d.ts.map +1 -0
- package/dist/src/index.js +25 -0
- package/dist/src/index.js.map +1 -0
- package/dist/src/make-client.d.ts +35 -0
- package/dist/src/make-client.d.ts.map +1 -0
- package/dist/src/make-client.js +52 -0
- package/dist/src/make-client.js.map +1 -0
- package/dist/src/preagg/PreAggregator.d.ts +150 -0
- package/dist/src/preagg/PreAggregator.d.ts.map +1 -0
- package/dist/src/preagg/PreAggregator.js +382 -0
- package/dist/src/preagg/PreAggregator.js.map +1 -0
- package/dist/src/preagg/preagg-columns.d.ts +16 -0
- package/dist/src/preagg/preagg-columns.d.ts.map +1 -0
- package/dist/src/preagg/preagg-columns.js +95 -0
- package/dist/src/preagg/preagg-columns.js.map +1 -0
- package/dist/src/preagg/sufficient-statistics.d.ts +14 -0
- package/dist/src/preagg/sufficient-statistics.d.ts.map +1 -0
- package/dist/src/preagg/sufficient-statistics.js +446 -0
- package/dist/src/preagg/sufficient-statistics.js.map +1 -0
- package/dist/src/types.d.ts +77 -0
- package/dist/src/types.d.ts.map +1 -0
- package/dist/src/types.js +2 -0
- package/dist/src/types.js.map +1 -0
- package/dist/src/util/AsyncDispatch.d.ts +121 -0
- package/dist/src/util/AsyncDispatch.d.ts.map +1 -0
- package/dist/src/util/AsyncDispatch.js +188 -0
- package/dist/src/util/AsyncDispatch.js.map +1 -0
- package/dist/src/util/cache.d.ts +19 -0
- package/dist/src/util/cache.d.ts.map +1 -0
- package/dist/src/util/cache.js +66 -0
- package/dist/src/util/cache.js.map +1 -0
- package/dist/src/util/decode-ipc.d.ts +12 -0
- package/dist/src/util/decode-ipc.d.ts.map +1 -0
- package/{src → dist/src}/util/decode-ipc.js +5 -6
- package/dist/src/util/decode-ipc.js.map +1 -0
- package/dist/src/util/distinct.d.ts +3 -0
- package/dist/src/util/distinct.d.ts.map +1 -0
- package/dist/src/util/distinct.js +16 -0
- package/dist/src/util/distinct.js.map +1 -0
- package/dist/src/util/field-info.d.ts +26 -0
- package/dist/src/util/field-info.d.ts.map +1 -0
- package/dist/src/util/field-info.js +91 -0
- package/dist/src/util/field-info.js.map +1 -0
- package/dist/src/util/hash.d.ts +2 -0
- package/dist/src/util/hash.d.ts.map +1 -0
- package/dist/src/util/hash.js +26 -0
- package/dist/src/util/hash.js.map +1 -0
- package/dist/src/util/is-activatable.d.ts +8 -0
- package/dist/src/util/is-activatable.d.ts.map +1 -0
- package/dist/src/util/is-activatable.js +10 -0
- package/dist/src/util/is-activatable.js.map +1 -0
- package/dist/src/util/is-arrow-table.d.ts +9 -0
- package/dist/src/util/is-arrow-table.d.ts.map +1 -0
- package/dist/src/util/is-arrow-table.js +11 -0
- package/dist/src/util/is-arrow-table.js.map +1 -0
- package/dist/src/util/js-type.d.ts +9 -0
- package/dist/src/util/js-type.d.ts.map +1 -0
- package/dist/src/util/js-type.js +59 -0
- package/dist/src/util/js-type.js.map +1 -0
- package/dist/src/util/priority-queue.d.ts +35 -0
- package/dist/src/util/priority-queue.d.ts.map +1 -0
- package/dist/src/util/priority-queue.js +81 -0
- package/dist/src/util/priority-queue.js.map +1 -0
- package/dist/src/util/query-result.d.ts +47 -0
- package/dist/src/util/query-result.d.ts.map +1 -0
- package/dist/src/util/query-result.js +83 -0
- package/dist/src/util/query-result.js.map +1 -0
- package/dist/src/util/synchronizer.d.ts +36 -0
- package/dist/src/util/synchronizer.d.ts.map +1 -0
- package/dist/src/util/synchronizer.js +52 -0
- package/dist/src/util/synchronizer.js.map +1 -0
- package/dist/src/util/throttle.d.ts +12 -0
- package/dist/src/util/throttle.d.ts.map +1 -0
- package/dist/src/util/throttle.js +51 -0
- package/dist/src/util/throttle.js.map +1 -0
- package/dist/src/util/to-data-columns.d.ts +22 -0
- package/dist/src/util/to-data-columns.d.ts.map +1 -0
- package/dist/src/util/to-data-columns.js +51 -0
- package/dist/src/util/to-data-columns.js.map +1 -0
- package/dist/src/util/void-logger.d.ts +13 -0
- package/dist/src/util/void-logger.d.ts.map +1 -0
- package/dist/src/util/void-logger.js +13 -0
- package/dist/src/util/void-logger.js.map +1 -0
- package/package.json +16 -10
- package/src/Coordinator.ts +367 -0
- package/src/{MosaicClient.js → MosaicClient.ts} +49 -43
- package/src/{Param.js → Param.ts} +29 -28
- package/src/{QueryConsolidator.js → QueryConsolidator.ts} +81 -58
- package/src/{QueryManager.js → QueryManager.ts} +61 -54
- package/src/Selection.ts +388 -0
- package/src/SelectionClause.ts +275 -0
- package/src/connectors/Connector.ts +6 -6
- package/src/connectors/rest.ts +56 -0
- package/src/connectors/{socket.js → socket.ts} +53 -42
- package/src/connectors/{wasm.js → wasm.ts} +46 -62
- package/src/{index.js → index.ts} +13 -1
- package/src/make-client.ts +93 -0
- package/src/preagg/{PreAggregator.js → PreAggregator.ts} +164 -145
- package/src/preagg/{preagg-columns.js → preagg-columns.ts} +27 -24
- package/src/preagg/{sufficient-statistics.js → sufficient-statistics.ts} +160 -110
- package/src/types.ts +24 -9
- package/src/util/{AsyncDispatch.js → AsyncDispatch.ts} +62 -43
- package/src/util/{cache.js → cache.ts} +25 -15
- package/src/util/decode-ipc.ts +15 -0
- package/src/util/{distinct.js → distinct.ts} +3 -3
- package/src/util/{field-info.js → field-info.ts} +31 -32
- package/src/util/{hash.js → hash.ts} +4 -4
- package/src/util/is-activatable.ts +11 -0
- package/src/util/is-arrow-table.ts +12 -0
- package/src/util/{js-type.js → js-type.ts} +7 -5
- package/src/util/{priority-queue.js → priority-queue.ts} +32 -20
- package/src/util/{query-result.js → query-result.ts} +24 -17
- package/src/util/synchronizer.ts +56 -0
- package/src/util/throttle.ts +59 -0
- package/src/util/to-data-columns.ts +65 -0
- package/src/util/void-logger.ts +23 -0
- package/src/Coordinator.js +0 -313
- package/src/Selection.js +0 -380
- package/src/SelectionClause.js +0 -159
- package/src/connectors/rest.js +0 -38
- package/src/index-types.ts +0 -5
- package/src/make-client.js +0 -101
- package/src/util/is-activatable.js +0 -8
- package/src/util/is-arrow-table.js +0 -10
- package/src/util/selection-types.ts +0 -137
- package/src/util/synchronizer.js +0 -47
- package/src/util/throttle.js +0 -54
- package/src/util/to-data-columns.js +0 -60
- package/src/util/void-logger.js +0 -13
- package/tsconfig.json +0 -9
- package/vitest.config.ts +0 -3
|
@@ -5,23 +5,30 @@ export const QueryState = Object.freeze({
|
|
|
5
5
|
done: Symbol('done')
|
|
6
6
|
});
|
|
7
7
|
|
|
8
|
+
type QueryStateType = typeof QueryState[keyof typeof QueryState];
|
|
9
|
+
|
|
8
10
|
/**
|
|
9
11
|
* A query result Promise that can allows external callers
|
|
10
12
|
* to resolve or reject the Promise.
|
|
11
13
|
*/
|
|
12
|
-
export class QueryResult extends Promise {
|
|
14
|
+
export class QueryResult<T = unknown> extends Promise<T> {
|
|
15
|
+
private _resolve!: (value: T | PromiseLike<T>) => void;
|
|
16
|
+
private _reject!: (reason?: unknown) => void;
|
|
17
|
+
private _state: QueryStateType;
|
|
18
|
+
private _value: T | undefined;
|
|
19
|
+
|
|
13
20
|
/**
|
|
14
21
|
* Create a new query result Promise.
|
|
15
22
|
*/
|
|
16
23
|
constructor() {
|
|
17
|
-
let resolve;
|
|
18
|
-
let reject;
|
|
24
|
+
let resolve: (value: T | PromiseLike<T>) => void;
|
|
25
|
+
let reject: (reason?: unknown) => void;
|
|
19
26
|
super((r, e) => {
|
|
20
27
|
resolve = r;
|
|
21
28
|
reject = e;
|
|
22
29
|
});
|
|
23
|
-
this._resolve = resolve
|
|
24
|
-
this._reject = reject
|
|
30
|
+
this._resolve = resolve!;
|
|
31
|
+
this._reject = reject!;
|
|
25
32
|
this._state = QueryState.pending;
|
|
26
33
|
this._value = undefined;
|
|
27
34
|
}
|
|
@@ -29,10 +36,10 @@ export class QueryResult extends Promise {
|
|
|
29
36
|
/**
|
|
30
37
|
* Resolve the result Promise with a prepared value or the provided value.
|
|
31
38
|
* This method will only succeed if either a value is provided or the promise is ready.
|
|
32
|
-
* @param
|
|
33
|
-
* @returns
|
|
39
|
+
* @param value The result value.
|
|
40
|
+
* @returns This QueryResult instance.
|
|
34
41
|
*/
|
|
35
|
-
fulfill(value) {
|
|
42
|
+
fulfill(value?: T): this {
|
|
36
43
|
if (this._value !== undefined) {
|
|
37
44
|
if (value !== undefined) {
|
|
38
45
|
throw Error('Promise is ready and fulfill has a provided value');
|
|
@@ -51,10 +58,10 @@ export class QueryResult extends Promise {
|
|
|
51
58
|
|
|
52
59
|
/**
|
|
53
60
|
* Prepare to resolve with the provided value.
|
|
54
|
-
* @param
|
|
55
|
-
* @returns
|
|
61
|
+
* @param value The result value.
|
|
62
|
+
* @returns This QueryResult instance.
|
|
56
63
|
*/
|
|
57
|
-
ready(value) {
|
|
64
|
+
ready(value: T): this {
|
|
58
65
|
this._state = QueryState.ready;
|
|
59
66
|
this._value = value;
|
|
60
67
|
return this;
|
|
@@ -62,10 +69,10 @@ export class QueryResult extends Promise {
|
|
|
62
69
|
|
|
63
70
|
/**
|
|
64
71
|
* Rejects the result Promise with the provided error.
|
|
65
|
-
* @param
|
|
66
|
-
* @returns
|
|
72
|
+
* @param error The error value.
|
|
73
|
+
* @returns This QueryResult instance.
|
|
67
74
|
*/
|
|
68
|
-
reject(error) {
|
|
75
|
+
reject(error: unknown): this {
|
|
69
76
|
this._state = QueryState.error;
|
|
70
77
|
this._reject(error);
|
|
71
78
|
return this;
|
|
@@ -73,12 +80,12 @@ export class QueryResult extends Promise {
|
|
|
73
80
|
|
|
74
81
|
/**
|
|
75
82
|
* Returns the state of this query result.
|
|
76
|
-
* @returns
|
|
83
|
+
* @returns The current state symbol.
|
|
77
84
|
*/
|
|
78
|
-
get state() {
|
|
85
|
+
get state(): QueryStateType {
|
|
79
86
|
return this._state;
|
|
80
87
|
}
|
|
81
88
|
}
|
|
82
89
|
|
|
83
90
|
// necessary to make Promise subclass act like a Promise
|
|
84
|
-
QueryResult.prototype.constructor = Promise;
|
|
91
|
+
QueryResult.prototype.constructor = Promise;
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Synchronizer class to aid synchronization of updates on multiple pending operations.
|
|
3
|
+
*/
|
|
4
|
+
export class Synchronizer<T = unknown> {
|
|
5
|
+
private _set: Set<T>;
|
|
6
|
+
private _done: () => void;
|
|
7
|
+
private _promise: Promise<void>;
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Create a new synchronizer instance.
|
|
11
|
+
*/
|
|
12
|
+
constructor() {
|
|
13
|
+
this._set = new Set<T>();
|
|
14
|
+
this._done = () => {};
|
|
15
|
+
this._promise = new Promise<void>(resolve => this._done = resolve);
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* Mark an item as pending.
|
|
20
|
+
* @param item An item to synchronize on.
|
|
21
|
+
*/
|
|
22
|
+
pending(item: T): void {
|
|
23
|
+
this._set.add(item);
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* Mark a pending item as ready, indicating it is
|
|
28
|
+
* ready for a synchronized update.
|
|
29
|
+
* @param item An item to synchronize on.
|
|
30
|
+
* @returns True if the synchronizer is ready to
|
|
31
|
+
* resolve, false otherwise.
|
|
32
|
+
*/
|
|
33
|
+
ready(item: T): boolean {
|
|
34
|
+
this._set.delete(item);
|
|
35
|
+
return this._set.size === 0;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* Resolve the current synchronization cycle, causing the synchronize
|
|
40
|
+
* promise to resolve and thereby trigger downstream updates.
|
|
41
|
+
*/
|
|
42
|
+
resolve(): void {
|
|
43
|
+
this._promise = new Promise<void>(resolve => {
|
|
44
|
+
this._done();
|
|
45
|
+
this._done = resolve;
|
|
46
|
+
});
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* The promise for the current synchronization cycle.
|
|
51
|
+
* @returns The synchronization promise.
|
|
52
|
+
*/
|
|
53
|
+
get promise(): Promise<void> {
|
|
54
|
+
return this._promise;
|
|
55
|
+
}
|
|
56
|
+
}
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
const NIL = {};
|
|
2
|
+
|
|
3
|
+
interface QueueItem<E> {
|
|
4
|
+
event: E;
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Throttle invocations of a callback function. The callback must return
|
|
9
|
+
* a Promise. Upon repeated invocation, the callback will not be invoked
|
|
10
|
+
* until a prior Promise resolves. If multiple invocations occurs while
|
|
11
|
+
* waiting, only the most recent invocation will be pending.
|
|
12
|
+
* @param callback The callback function.
|
|
13
|
+
* @param debounce Flag indicating if invocations
|
|
14
|
+
* should also be debounced within the current animation frame.
|
|
15
|
+
* @returns A new function that throttles access to the callback.
|
|
16
|
+
*/
|
|
17
|
+
export function throttle<E, T>(
|
|
18
|
+
callback: (event?: E) => Promise<T> | null,
|
|
19
|
+
debounce: boolean = false
|
|
20
|
+
): (event?: E) => void {
|
|
21
|
+
let curr: Promise<unknown> | null;
|
|
22
|
+
let next: QueueItem<E> | undefined | null;
|
|
23
|
+
let pending: E | undefined | typeof NIL = NIL;
|
|
24
|
+
|
|
25
|
+
function invoke(event?: E): void {
|
|
26
|
+
curr = callback(event)
|
|
27
|
+
?.catch(() => {})
|
|
28
|
+
.finally(() => {
|
|
29
|
+
if (next) {
|
|
30
|
+
const { event: value } = next;
|
|
31
|
+
next = null;
|
|
32
|
+
invoke(value);
|
|
33
|
+
} else {
|
|
34
|
+
curr = null;
|
|
35
|
+
}
|
|
36
|
+
}) || null;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
function enqueue(event: E): void {
|
|
40
|
+
next = { event };
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
function process(event?: E): void {
|
|
44
|
+
if (curr) enqueue(event!); else invoke(event);
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
function delay(event?: E): void {
|
|
48
|
+
if (pending !== event) {
|
|
49
|
+
requestAnimationFrame(() => {
|
|
50
|
+
const e = pending as E;
|
|
51
|
+
pending = NIL;
|
|
52
|
+
process(e);
|
|
53
|
+
});
|
|
54
|
+
}
|
|
55
|
+
pending = event;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
return debounce ? delay : process;
|
|
59
|
+
}
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
import { isArrowTable } from './is-arrow-table.js';
|
|
2
|
+
import type { Table } from '@uwdata/flechette';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* An Array or TypedArray
|
|
6
|
+
*/
|
|
7
|
+
type Arrayish = Array<unknown> | Int8Array | Uint8Array | Uint8ClampedArray
|
|
8
|
+
| Int16Array | Uint16Array | Int32Array | Uint32Array
|
|
9
|
+
| Float32Array | Float64Array;
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Data columns structure with either named columns or values array
|
|
13
|
+
*/
|
|
14
|
+
type DataColumns =
|
|
15
|
+
| { numRows: number; columns: Record<string, Arrayish> }
|
|
16
|
+
| { numRows: number; values: Arrayish };
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* Convert input data to a set of column arrays.
|
|
20
|
+
* @param data The input data.
|
|
21
|
+
* @returns An object with named column arrays.
|
|
22
|
+
*/
|
|
23
|
+
export function toDataColumns(data: unknown): DataColumns {
|
|
24
|
+
if (isArrowTable(data)) {
|
|
25
|
+
return arrowToColumns(data);
|
|
26
|
+
} else if (Array.isArray(data)) {
|
|
27
|
+
return arrayToColumns(data);
|
|
28
|
+
} else {
|
|
29
|
+
throw new Error('Unrecognized data format.');
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* Convert an Arrow table to a set of column arrays.
|
|
35
|
+
* @param data An Arrow Table.
|
|
36
|
+
* @returns An object with named column arrays.
|
|
37
|
+
*/
|
|
38
|
+
function arrowToColumns(data: Table): DataColumns {
|
|
39
|
+
const { numRows } = data;
|
|
40
|
+
return { numRows, columns: data.toColumns() as Record<string, Arrayish> };
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* Convert an array of values to a set of column arrays.
|
|
45
|
+
* If the array values are objects, build out named columns.
|
|
46
|
+
* We use the keys of the first object as the column names.
|
|
47
|
+
* Otherwise, use a special "values" array.
|
|
48
|
+
* @param data An array of data objects.
|
|
49
|
+
* @returns An object with named column arrays.
|
|
50
|
+
*/
|
|
51
|
+
function arrayToColumns(data: Record<string,unknown>[]): DataColumns {
|
|
52
|
+
const numRows = data.length;
|
|
53
|
+
if (typeof data[0] === 'object') {
|
|
54
|
+
const names = numRows ? Object.keys(data[0]!) : [];
|
|
55
|
+
const columns: Record<string, unknown[]> = {};
|
|
56
|
+
if (names.length > 0) {
|
|
57
|
+
names.forEach(name => {
|
|
58
|
+
columns[name] = data.map(d => d[name]);
|
|
59
|
+
});
|
|
60
|
+
}
|
|
61
|
+
return { numRows, columns };
|
|
62
|
+
} else {
|
|
63
|
+
return { numRows, values: data };
|
|
64
|
+
}
|
|
65
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
interface Logger {
|
|
2
|
+
debug(...args: unknown[]): void;
|
|
3
|
+
info(...args: unknown[]): void;
|
|
4
|
+
log(...args: unknown[]): void;
|
|
5
|
+
warn(...args: unknown[]): void;
|
|
6
|
+
error(...args: unknown[]): void;
|
|
7
|
+
group(label?: string): void;
|
|
8
|
+
groupCollapsed(label?: string): void;
|
|
9
|
+
groupEnd(): void;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export function voidLogger(): Logger {
|
|
13
|
+
return {
|
|
14
|
+
debug(): void {},
|
|
15
|
+
info(): void {},
|
|
16
|
+
log(): void {},
|
|
17
|
+
warn(): void {},
|
|
18
|
+
error(): void {},
|
|
19
|
+
group(): void {},
|
|
20
|
+
groupCollapsed(): void {},
|
|
21
|
+
groupEnd(): void {}
|
|
22
|
+
};
|
|
23
|
+
}
|
package/src/Coordinator.js
DELETED
|
@@ -1,313 +0,0 @@
|
|
|
1
|
-
/** @import { Connector } from './connectors/Connector.js' */
|
|
2
|
-
/** @import { PreAggregateOptions } from './preagg/PreAggregator.js' */
|
|
3
|
-
/** @import { QueryResult } from './util/query-result.js' */
|
|
4
|
-
/** @import { SelectionClause } from './util/selection-types.js' */
|
|
5
|
-
/** @import { MosaicClient } from './MosaicClient.js' */
|
|
6
|
-
/** @import { Selection } from './Selection.js' */
|
|
7
|
-
/** @import { Logger, QueryType } from './types.js' */
|
|
8
|
-
import { socketConnector } from './connectors/socket.js';
|
|
9
|
-
import { PreAggregator } from './preagg/PreAggregator.js';
|
|
10
|
-
import { voidLogger } from './util/void-logger.js';
|
|
11
|
-
import { QueryManager, Priority } from './QueryManager.js';
|
|
12
|
-
|
|
13
|
-
/**
|
|
14
|
-
* The singleton Coordinator instance.
|
|
15
|
-
* @type {Coordinator}
|
|
16
|
-
*/
|
|
17
|
-
let _instance;
|
|
18
|
-
|
|
19
|
-
/**
|
|
20
|
-
* Set or retrieve the coordinator instance.
|
|
21
|
-
* @param {Coordinator} [instance] the coordinator instance to set
|
|
22
|
-
* @returns {Coordinator} the coordinator instance
|
|
23
|
-
*/
|
|
24
|
-
export function coordinator(instance) {
|
|
25
|
-
if (instance) {
|
|
26
|
-
_instance = instance;
|
|
27
|
-
} else if (_instance == null) {
|
|
28
|
-
_instance = new Coordinator();
|
|
29
|
-
}
|
|
30
|
-
return _instance;
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
/**
|
|
34
|
-
* A Mosaic Coordinator manages all database communication for clients and
|
|
35
|
-
* handles selection updates. The Coordinator also performs optimizations
|
|
36
|
-
* including query caching, consolidation, and pre-aggregation.
|
|
37
|
-
*/
|
|
38
|
-
export class Coordinator {
|
|
39
|
-
/**
|
|
40
|
-
* @param {Connector} [db] Database connector. Defaults to a web socket connection.
|
|
41
|
-
* @param {object} [options] Coordinator options.
|
|
42
|
-
* @param {Logger} [options.logger=console] The logger to use, defaults to `console`.
|
|
43
|
-
* @param {QueryManager} [options.manager] The query manager to use.
|
|
44
|
-
* @param {boolean} [options.cache=true] Boolean flag to enable/disable query caching.
|
|
45
|
-
* @param {boolean} [options.consolidate=true] Boolean flag to enable/disable query consolidation.
|
|
46
|
-
* @param {PreAggregateOptions} [options.preagg] Options for the Pre-aggregator.
|
|
47
|
-
*/
|
|
48
|
-
constructor(db = socketConnector(), {
|
|
49
|
-
logger = console,
|
|
50
|
-
manager = new QueryManager(),
|
|
51
|
-
cache = true,
|
|
52
|
-
consolidate = true,
|
|
53
|
-
preagg = {}
|
|
54
|
-
} = {}) {
|
|
55
|
-
/** @type {QueryManager} */
|
|
56
|
-
this.manager = manager;
|
|
57
|
-
this.manager.cache(cache);
|
|
58
|
-
this.manager.consolidate(consolidate);
|
|
59
|
-
this.databaseConnector(db);
|
|
60
|
-
this.logger(logger);
|
|
61
|
-
this.clear();
|
|
62
|
-
this.preaggregator = new PreAggregator(this, preagg);
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
/**
|
|
66
|
-
* Clear the coordinator state.
|
|
67
|
-
* @param {object} [options] Options object.
|
|
68
|
-
* @param {boolean} [options.clients=true] If true, disconnect all clients.
|
|
69
|
-
* @param {boolean} [options.cache=true] If true, clear the query cache.
|
|
70
|
-
*/
|
|
71
|
-
clear({ clients = true, cache = true } = {}) {
|
|
72
|
-
this.manager.clear();
|
|
73
|
-
if (clients) {
|
|
74
|
-
this.filterGroups?.forEach(group => group.disconnect());
|
|
75
|
-
this.filterGroups = new Map;
|
|
76
|
-
this.clients?.forEach(client => this.disconnect(client));
|
|
77
|
-
this.clients = new Set;
|
|
78
|
-
}
|
|
79
|
-
if (cache) this.manager.cache().clear();
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
/**
|
|
83
|
-
* Get or set the database connector.
|
|
84
|
-
* @param {Connector} [db] The database connector to use.
|
|
85
|
-
* @returns {Connector} The current database connector.
|
|
86
|
-
*/
|
|
87
|
-
databaseConnector(db) {
|
|
88
|
-
return this.manager.connector(db);
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
/**
|
|
92
|
-
* Get or set the logger.
|
|
93
|
-
* @param {Logger} [logger] The logger to use.
|
|
94
|
-
* @returns {Logger} The current logger
|
|
95
|
-
*/
|
|
96
|
-
logger(logger) {
|
|
97
|
-
if (arguments.length) {
|
|
98
|
-
this._logger = logger || voidLogger();
|
|
99
|
-
this.manager.logger(this._logger);
|
|
100
|
-
}
|
|
101
|
-
return this._logger;
|
|
102
|
-
}
|
|
103
|
-
|
|
104
|
-
// -- Query Management ----
|
|
105
|
-
|
|
106
|
-
/**
|
|
107
|
-
* Cancel previosuly submitted query requests. These queries will be
|
|
108
|
-
* canceled if they are queued but have not yet been submitted.
|
|
109
|
-
* @param {QueryResult[]} requests An array
|
|
110
|
-
* of query result objects, such as those returned by the `query` method.
|
|
111
|
-
*/
|
|
112
|
-
cancel(requests) {
|
|
113
|
-
this.manager.cancel(requests);
|
|
114
|
-
}
|
|
115
|
-
|
|
116
|
-
/**
|
|
117
|
-
* Issue a query for which no result (return value) is needed.
|
|
118
|
-
* @param {QueryType[] | QueryType} query The query or an array of queries.
|
|
119
|
-
* Each query should be either a Query builder object or a SQL string.
|
|
120
|
-
* @param {object} [options] An options object.
|
|
121
|
-
* @param {number} [options.priority] The query priority, defaults to
|
|
122
|
-
* `Priority.Normal`.
|
|
123
|
-
* @returns {QueryResult} A query result promise.
|
|
124
|
-
*/
|
|
125
|
-
exec(query, { priority = Priority.Normal } = {}) {
|
|
126
|
-
query = Array.isArray(query) ? query.filter(x => x).join(';\n') : query;
|
|
127
|
-
return this.manager.request({ type: 'exec', query }, priority);
|
|
128
|
-
}
|
|
129
|
-
|
|
130
|
-
/**
|
|
131
|
-
* Issue a query to the backing database. The submitted query may be
|
|
132
|
-
* consolidate with other queries and its results may be cached.
|
|
133
|
-
* @param {QueryType} query The query as either a Query builder objec
|
|
134
|
-
* or a SQL string.
|
|
135
|
-
* @param {object} [options] An options object.
|
|
136
|
-
* @param {'arrow' | 'json'} [options.type] The query result format type.
|
|
137
|
-
* @param {boolean} [options.cache=true] If true, cache the query result
|
|
138
|
-
* client-side within the QueryManager.
|
|
139
|
-
* @param {boolean} [options.persist] If true, request the database
|
|
140
|
-
* server to persist a cached query server-side.
|
|
141
|
-
* @param {number} [options.priority] The query priority, defaults to
|
|
142
|
-
* `Priority.Normal`.
|
|
143
|
-
* @returns {QueryResult} A query result promise.
|
|
144
|
-
*/
|
|
145
|
-
query(query, {
|
|
146
|
-
type = 'arrow',
|
|
147
|
-
cache = true,
|
|
148
|
-
priority = Priority.Normal,
|
|
149
|
-
...options
|
|
150
|
-
} = {}) {
|
|
151
|
-
return this.manager.request({ type, query, cache, options }, priority);
|
|
152
|
-
}
|
|
153
|
-
|
|
154
|
-
/**
|
|
155
|
-
* Issue a query to prefetch data for later use. The query result is cached
|
|
156
|
-
* for efficient future access.
|
|
157
|
-
* @param {QueryType} query The query as either a Query builder object
|
|
158
|
-
* or a SQL string.
|
|
159
|
-
* @param {object} [options] An options object.
|
|
160
|
-
* @param {'arrow' | 'json'} [options.type] The query result format type.
|
|
161
|
-
* @returns {QueryResult} A query result promise.
|
|
162
|
-
*/
|
|
163
|
-
prefetch(query, options = {}) {
|
|
164
|
-
return this.query(query, { ...options, cache: true, priority: Priority.Low });
|
|
165
|
-
}
|
|
166
|
-
|
|
167
|
-
// -- Client Management ----
|
|
168
|
-
|
|
169
|
-
/**
|
|
170
|
-
* Update client data by submitting the given query and returning the
|
|
171
|
-
* data (or error) to the client.
|
|
172
|
-
* @param {MosaicClient} client A Mosaic client.
|
|
173
|
-
* @param {QueryType} query The data query.
|
|
174
|
-
* @param {number} [priority] The query priority.
|
|
175
|
-
* @returns {Promise} A Promise that resolves upon completion of the update.
|
|
176
|
-
*/
|
|
177
|
-
updateClient(client, query, priority = Priority.Normal) {
|
|
178
|
-
client.queryPending();
|
|
179
|
-
return client._pending = this.query(query, { priority })
|
|
180
|
-
.then(
|
|
181
|
-
data => client.queryResult(data).update(),
|
|
182
|
-
err => { this._logger.error(err); client.queryError(err); }
|
|
183
|
-
)
|
|
184
|
-
.catch(err => this._logger.error(err));
|
|
185
|
-
}
|
|
186
|
-
|
|
187
|
-
/**
|
|
188
|
-
* Issue a query request for a client. If the query is null or undefined,
|
|
189
|
-
* the client is simply updated. Otherwise `updateClient` is called. As a
|
|
190
|
-
* side effect, this method clears the current preaggregator state.
|
|
191
|
-
* @param {MosaicClient} client The client to update.
|
|
192
|
-
* @param {QueryType | null} [query] The query to issue.
|
|
193
|
-
*/
|
|
194
|
-
requestQuery(client, query) {
|
|
195
|
-
this.preaggregator.clear();
|
|
196
|
-
return query
|
|
197
|
-
? this.updateClient(client, query)
|
|
198
|
-
: Promise.resolve(client.update());
|
|
199
|
-
}
|
|
200
|
-
|
|
201
|
-
/**
|
|
202
|
-
* Connect a client to the coordinator.
|
|
203
|
-
* @param {MosaicClient} client The Mosaic client to connect.
|
|
204
|
-
*/
|
|
205
|
-
connect(client) {
|
|
206
|
-
const { clients } = this;
|
|
207
|
-
|
|
208
|
-
if (clients.has(client)) {
|
|
209
|
-
throw new Error('Client already connected.');
|
|
210
|
-
}
|
|
211
|
-
|
|
212
|
-
// add client to client set
|
|
213
|
-
clients.add(client);
|
|
214
|
-
|
|
215
|
-
// register coordinator on client instance
|
|
216
|
-
client.coordinator = this;
|
|
217
|
-
|
|
218
|
-
// initialize client lifecycle
|
|
219
|
-
client.initialize();
|
|
220
|
-
|
|
221
|
-
// connect filter selection
|
|
222
|
-
connectSelection(this, client.filterBy, client);
|
|
223
|
-
}
|
|
224
|
-
|
|
225
|
-
/**
|
|
226
|
-
* Disconnect a client from the coordinator.
|
|
227
|
-
* @param {MosaicClient} client The Mosaic client to disconnect.
|
|
228
|
-
*/
|
|
229
|
-
disconnect(client) {
|
|
230
|
-
const { clients, filterGroups } = this;
|
|
231
|
-
if (!clients.has(client)) return;
|
|
232
|
-
clients.delete(client);
|
|
233
|
-
client.coordinator = null;
|
|
234
|
-
|
|
235
|
-
const group = filterGroups.get(client.filterBy);
|
|
236
|
-
if (group) {
|
|
237
|
-
group.clients.delete(client);
|
|
238
|
-
}
|
|
239
|
-
}
|
|
240
|
-
}
|
|
241
|
-
|
|
242
|
-
/**
|
|
243
|
-
* Connect a selection-client pair to the coordinator to process updates.
|
|
244
|
-
* @param {Coordinator} mc The Mosaic coordinator.
|
|
245
|
-
* @param {Selection} selection A selection.
|
|
246
|
-
* @param {MosaicClient} client A Mosiac client that is filtered by the
|
|
247
|
-
* given selection.
|
|
248
|
-
*/
|
|
249
|
-
function connectSelection(mc, selection, client) {
|
|
250
|
-
if (!selection) return;
|
|
251
|
-
let entry = mc.filterGroups.get(selection);
|
|
252
|
-
if (!entry) {
|
|
253
|
-
const activate = clause => activateSelection(mc, selection, clause);
|
|
254
|
-
const value = () => updateSelection(mc, selection);
|
|
255
|
-
|
|
256
|
-
selection.addEventListener('activate', activate);
|
|
257
|
-
selection.addEventListener('value', value);
|
|
258
|
-
|
|
259
|
-
entry = {
|
|
260
|
-
selection,
|
|
261
|
-
clients: new Set,
|
|
262
|
-
disconnect() {
|
|
263
|
-
selection.removeEventListener('activate', activate);
|
|
264
|
-
selection.removeEventListener('value', value);
|
|
265
|
-
}
|
|
266
|
-
};
|
|
267
|
-
mc.filterGroups.set(selection, entry);
|
|
268
|
-
}
|
|
269
|
-
entry.clients.add(client);
|
|
270
|
-
}
|
|
271
|
-
|
|
272
|
-
/**
|
|
273
|
-
* Activate a selection, providing a clause indicative of potential
|
|
274
|
-
* next updates. Activation provides a preview of likely next events,
|
|
275
|
-
* enabling potential precomputation to optimize updates.
|
|
276
|
-
* @param {Coordinator} mc The Mosaic coordinator.
|
|
277
|
-
* @param {Selection} selection A selection.
|
|
278
|
-
* @param {SelectionClause} clause A selection clause for the activation.
|
|
279
|
-
*/
|
|
280
|
-
function activateSelection(mc, selection, clause) {
|
|
281
|
-
const { preaggregator, filterGroups } = mc;
|
|
282
|
-
const { clients } = filterGroups.get(selection);
|
|
283
|
-
for (const client of clients) {
|
|
284
|
-
if (client.enabled) {
|
|
285
|
-
preaggregator.request(client, selection, clause);
|
|
286
|
-
}
|
|
287
|
-
}
|
|
288
|
-
}
|
|
289
|
-
|
|
290
|
-
/**
|
|
291
|
-
* Process an updated selection value, querying filtered data for any
|
|
292
|
-
* associated clients.
|
|
293
|
-
* @param {Coordinator} mc The Mosaic coordinator.
|
|
294
|
-
* @param {Selection} selection A selection.
|
|
295
|
-
* @returns {Promise} A Promise that resolves when the update completes.
|
|
296
|
-
*/
|
|
297
|
-
function updateSelection(mc, selection) {
|
|
298
|
-
const { preaggregator, filterGroups } = mc;
|
|
299
|
-
const { clients } = filterGroups.get(selection);
|
|
300
|
-
const { active } = selection;
|
|
301
|
-
return Promise.allSettled(Array.from(clients, client => {
|
|
302
|
-
if (!client.enabled) return client.requestQuery();
|
|
303
|
-
const info = preaggregator.request(client, selection, active);
|
|
304
|
-
const filter = info ? null : selection.predicate(client);
|
|
305
|
-
|
|
306
|
-
// skip due to cross-filtering
|
|
307
|
-
if (info?.skip || (!info && !filter)) return;
|
|
308
|
-
|
|
309
|
-
// @ts-ignore
|
|
310
|
-
const query = info?.query(active.predicate) ?? client.query(filter);
|
|
311
|
-
return mc.updateClient(client, query);
|
|
312
|
-
}));
|
|
313
|
-
}
|