@uwdata/mosaic-core
Advanced tools
Comparing version 0.4.0 to 0.5.0
{ | ||
"name": "@uwdata/mosaic-core", | ||
"version": "0.4.0", | ||
"version": "0.5.0", | ||
"description": "Scalable and extensible linked data views.", | ||
@@ -31,7 +31,7 @@ "keywords": [ | ||
"dependencies": { | ||
"@duckdb/duckdb-wasm": "^1.28.0", | ||
"@uwdata/mosaic-sql": "^0.4.0", | ||
"apache-arrow": "^13.0.0" | ||
"@duckdb/duckdb-wasm": "^1.28.1-dev106.0", | ||
"@uwdata/mosaic-sql": "^0.5.0", | ||
"apache-arrow": "^14.0.1" | ||
}, | ||
"gitHead": "4114465543a21b25d6d14647db7d95af875d2519" | ||
"gitHead": "92886dddfb126c1439924c5a0189e4639c3519a7" | ||
} |
# mosaic-core | ||
The core Mosaic components: a central coordinator, parameters and selections for linking scalar values or query predicates (respectively) across Mosaic clients, and filter groups with optimized index management. The Mosaic coordinator can send queries either over the network to a backing server (`socket` and `rest` clients) or to a client-side [DuckDB-WASM](https://github.com/duckdb/duckdb-wasm) instance (`wasm` client). | ||
The core Mosaic components: a central coordinator, parameters (`Param`) and selections (`Selection`) for linking scalar values or query predicates (respectively) across Mosaic clients, and filter groups with optimized index management. The Mosaic coordinator can send queries either over the network to a backing server (`socket` and `rest` clients) or to a client-side [DuckDB-WASM](https://github.com/duckdb/duckdb-wasm) instance (`wasm` client). | ||
The `mosaic-core` facilities are included as part of the [vgplot](https://github.com/uwdata/mosaic/tree/main/packages/vgplot) API. |
@@ -9,2 +9,3 @@ import { asRelation } from '@uwdata/mosaic-sql'; | ||
constructor(coordinator) { | ||
/** @type {import('@uwdata/mosaic-core').Coordinator} */ | ||
this.mc = coordinator; | ||
@@ -11,0 +12,0 @@ this.clear(); |
import * as duckdb from '@duckdb/duckdb-wasm'; | ||
export async function wasmConnector(options) { | ||
const db = await initDatabase(options); | ||
const con = await db.connect(); | ||
export function wasmConnector(options = {}) { | ||
const { duckdb, connection, ...opts } = options; | ||
let db = duckdb; | ||
let con = connection; | ||
let loadPromise; | ||
function load() { | ||
if (!loadPromise) { | ||
// use a loading promise to avoid race conditions | ||
// synchronizes multiple callees on the same load | ||
loadPromise = (db | ||
? Promise.resolve(db) | ||
: initDatabase(opts).then(result => db = result)) | ||
.then(db => db.connect()) | ||
.then(result => con = result); | ||
} | ||
return loadPromise; | ||
} | ||
/** | ||
* Get the backing DuckDB-WASM instance. | ||
* Will lazily initialize DuckDB-WASM if not already loaded. | ||
* @returns {duckdb.AsyncDuckDB} The DuckDB-WASM instance. | ||
*/ | ||
async function getDuckDB() { | ||
if (!db) await load(); | ||
return db; | ||
} | ||
/** | ||
* Get the backing DuckDB-WASM connection. | ||
* Will lazily initialize DuckDB-WASM if not already loaded. | ||
* @returns {duckdb.AsyncDuckDBConnection} The DuckDB-WASM connection. | ||
*/ | ||
async function getConnection() { | ||
if (!con) await load(); | ||
return con; | ||
} | ||
return { | ||
db, | ||
con, | ||
getDuckDB, | ||
getConnection, | ||
/** | ||
* Query the DuckDB-WASM instance. | ||
* @param {object} query | ||
* @param {string} [query.type] The query type: 'exec', 'arrow', or 'json'. | ||
* @param {string} query.sql A SQL query string. | ||
* @returns the query result | ||
*/ | ||
query: async query => { | ||
const { type, sql } = query; | ||
const con = await getConnection(); | ||
const result = await con.query(sql); | ||
@@ -13,0 +56,0 @@ return type === 'exec' ? undefined |
@@ -26,5 +26,9 @@ import { socketConnector } from './connectors/socket.js'; | ||
constructor(db = socketConnector(), options = {}) { | ||
const { | ||
logger = console, | ||
manager = QueryManager() | ||
} = options; | ||
this.catalog = new Catalog(this); | ||
this.manager = options.manager || QueryManager(); | ||
this.logger(options.logger || console); | ||
this.manager = manager; | ||
this.logger(logger); | ||
this.configure(options); | ||
@@ -72,2 +76,3 @@ this.databaseConnector(db); | ||
exec(query, { priority = Priority.Normal } = {}) { | ||
query = Array.isArray(query) ? query.join(';\n') : query; | ||
return this.manager.request({ type: 'exec', query }, priority); | ||
@@ -128,2 +133,3 @@ } | ||
clients.add(client); // mark as connected | ||
client.coordinator = this; | ||
@@ -160,3 +166,4 @@ // retrieve field statistics | ||
filterGroups.get(client.filterBy)?.remove(client); | ||
client.coordinator = null; | ||
} | ||
} |
@@ -1,2 +0,1 @@ | ||
import { coordinator } from './Coordinator.js'; | ||
import { throttle } from './util/throttle.js'; | ||
@@ -17,5 +16,20 @@ | ||
this._requestUpdate = throttle(() => this.requestQuery(), true); | ||
this._coordinator = null; | ||
} | ||
/** | ||
* Return this client's connected coordinator. | ||
*/ | ||
get coordinator() { | ||
return this._coordinator; | ||
} | ||
/** | ||
* Set this client's connected coordinator. | ||
*/ | ||
set coordinator(coordinator) { | ||
this._coordinator = coordinator; | ||
} | ||
/** | ||
* Return this client's filter selection. | ||
@@ -67,3 +81,3 @@ */ | ||
* Called by the coordinator to return a query result. | ||
* | ||
* | ||
* @param {*} data the query result | ||
@@ -91,3 +105,3 @@ * @returns {this} | ||
const q = query || this.query(this.filterBy?.predicate(this)); | ||
return coordinator().requestQuery(this, q); | ||
return this._coordinator.requestQuery(this, q); | ||
} | ||
@@ -94,0 +108,0 @@ |
import { Query, Ref } from '@uwdata/mosaic-sql'; | ||
import { queryResult } from './util/query-result.js'; | ||
function wait(callback) { | ||
const method = typeof requestAnimationFrame !== 'undefined' | ||
? requestAnimationFrame | ||
: typeof setImmediate !== 'undefined' ? setImmediate : setTimeout; | ||
return method(callback); | ||
} | ||
/** | ||
@@ -33,3 +40,3 @@ * Create a consolidator to combine structurally compatible queries. | ||
// only Apache Arrow is supported, so we can project efficiently | ||
id = id || requestAnimationFrame(() => run()); | ||
id = id || wait(() => run()); | ||
pending.push({ entry, priority, index: pending.length }); | ||
@@ -36,0 +43,0 @@ } else { |
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is too big to display
812659
17748
6
+ Added@uwdata/mosaic-sql@0.5.0(transitive)
+ Addedapache-arrow@14.0.2(transitive)
- Removed@uwdata/mosaic-sql@0.4.0(transitive)
- Removedapache-arrow@13.0.0(transitive)
Updated@uwdata/mosaic-sql@^0.5.0
Updatedapache-arrow@^14.0.1