Skip to content

Commit

Permalink
add-option-to-map-on-full-dataset
Browse files Browse the repository at this point in the history
  • Loading branch information
bmschmidt committed Dec 7, 2023
1 parent 100762d commit 29f99a6
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 2 deletions.
46 changes: 46 additions & 0 deletions src/Dataset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,52 @@ export abstract class Dataset<T extends Tile> {
}
}

/**
* Invoke a function on all tiles in the dataset that have been downloaded.
* The general architecture here is taken from the
* d3 quadtree functions. That's why, for example, it doesn't
* recurse.
* @param callback The function to invoke on each tile.
* @param after Whether to execute the visit in bottom-up order. Default false.
* @param filter
*/

async visit_full(
callback: (tile: T) => Promise<void>,
after = false,
starting_tile : T | null = null,
filter: (t: T) => boolean = (x) => true,
updateFunction: (tile: T, completed, total) => Promise<void>
) {

// Visit all children with a callback function.
// In general recursing quadtrees isn't that fast, but
// we rarely have more than ten tiles deep and the
// code is much cleaner this way than an async queue.

let seen = 0;
const start = starting_tile || this.root_tile;
await start.download();
const total = JSON.parse(start.record_batch.schema.metadata.get("total_points"))

async function resolve(tile: T) {
await tile.download();
if (after) {
await Promise.all(tile.children.map(resolve))
await callback(tile);
seen += tile.record_batch.numRows
void updateFunction(tile, seen, total)
} else {
await callback(tile);
seen += tile.record_batch.numRows
void updateFunction(tile, seen, total)
await Promise.all(tile.children.map(resolve))
}
}
await resolve(start);
}

async schema() {
await this.ready;
if (this._schema) {
Expand Down
4 changes: 2 additions & 2 deletions src/tile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ export abstract class Tile {
return this._children;
}

download() {
download() : Promise<void> {
throw new Error('Not implemented');
}

Expand Down Expand Up @@ -253,7 +253,7 @@ export abstract class Tile {
}

async schema() {
this.download();
await this.download();
await this.promise;
return this._schema;
}
Expand Down

0 comments on commit 29f99a6

Please sign in to comment.