Skip to content
Merged
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
30 changes: 5 additions & 25 deletions tutorials/narrow_vs_wide_performance.qmd
Original file line number Diff line number Diff line change
Expand Up @@ -104,61 +104,41 @@ viewof runBenchmarks = Inputs.button("Run All Benchmarks", {
<div id="error_display" style="padding: 10px; background: #f8d7da; border-radius: 4px; color: #721c24; display: none;">
</div>

```{ojs}
//| echo: false
// Database initialization is lazy - only happens when button is clicked
// This is stored as a mutable to track state
mutable dbState = { narrow: null, wide: null, error: null, initialized: false }
```

```{ojs}
//| echo: false
// Initialize databases only when button is clicked (lazy loading)
// Returns { narrow, wide } or null if not yet clicked
initDatabases = {
// Only initialize when button is clicked
if (runBenchmarks < 1) return null;

// Return cached instances if already initialized
if (dbState.initialized && !dbState.error) {
return { narrow: dbState.narrow, wide: dbState.wide };
}

const loadingDiv = document.getElementById('loading_init');
const errorDiv = document.getElementById('error_display');

if (loadingDiv) loadingDiv.style.display = 'block';
if (errorDiv) errorDiv.style.display = 'none';

try {
// Initialize narrow database
const narrowDb = await DuckDBClient.of();
await narrowDb.query(`CREATE VIEW narrow AS SELECT * FROM read_parquet('${narrowUrl}')`);

// Initialize wide database
const wideDb = await DuckDBClient.of();
await wideDb.query(`CREATE VIEW wide AS SELECT * FROM read_parquet('${wideUrl}')`);

// Cache the instances
mutable dbState = { narrow: narrowDb, wide: wideDb, error: null, initialized: true };

if (loadingDiv) loadingDiv.style.display = 'none';
return { narrow: narrowDb, wide: wideDb };
} catch (e) {
const errorMsg = `Failed to initialize databases: ${e.message}. This may be due to network issues or CORS restrictions.`;
mutable dbState = { narrow: null, wide: null, error: errorMsg, initialized: false };

if (errorDiv) {
errorDiv.textContent = errorMsg;
errorDiv.style.display = 'block';
}
throw e;
} finally {
if (loadingDiv) loadingDiv.style.display = 'none';
return { error: errorMsg };
}
}

// Convenience accessors that wait for initialization
dbNarrow = initDatabases ? initDatabases.narrow : null
dbWide = initDatabases ? initDatabases.wide : null
dbNarrow = initDatabases && !initDatabases.error ? initDatabases.narrow : null
dbWide = initDatabases && !initDatabases.error ? initDatabases.wide : null
```

## Data Validity Check
Expand Down