fix(quick-open): search unloaded tables after cold start
This commit is contained in:
6 files changed
+211
-29
No files matched your search
@@ -1,6 +1,7 @@
|
||||
import { computed, ref, watch } from "vue";
|
||||
import type { ConnectionConfig } from "@/types/database";
|
||||
import type { SqlCompletionTable } from "@/lib/sql/sqlCompletion";
|
||||
import { resolveDefaultDatabase } from "@/lib/database/defaultDatabase";
|
||||
import { useConnectionStore } from "@/stores/connectionStore";
|
||||
import { useSavedSqlStore } from "@/stores/savedSqlStore";
|
||||
import * as api from "@/lib/backend/api";
|
||||
@@ -111,7 +112,7 @@ export function useQuickOpen() {
|
||||
let remoteSearchGeneration = 0;
|
||||
let remoteSearchTimer: ReturnType<typeof setTimeout> | undefined;
|
||||
let activeRemoteRequests = 0;
|
||||
const remoteRequestWaiters: Array<() => void> = [];
|
||||
const remoteRequestWaiters: Array<{ generation: number; resolve: (acquired: boolean) => void }> = [];
|
||||
let sqlFilesLoaded = false;
|
||||
let sqlFilesLoadingPromise: Promise<void> | null = null;
|
||||
let sqlFilesLoadGeneration = 0;
|
||||
@@ -467,12 +468,20 @@ export function useQuickOpen() {
|
||||
|
||||
function remoteSearchContexts(): Array<{ conn: ConnectionConfig; database: string }> {
|
||||
if (typeof connectionStore.listCompletionTables !== "function") return [];
|
||||
const connectedIds = connectionStore.connectedIds;
|
||||
if (!(connectedIds instanceof Set)) return [];
|
||||
|
||||
const databasesByConnection: Array<{ conn: ConnectionConfig; databases: string[] }> = [];
|
||||
for (const conn of connectionStore.connections) {
|
||||
if (!connectedIds.has(conn.id) || REMOTE_SEARCH_UNSUPPORTED_TYPES.has(conn.db_type)) continue;
|
||||
const orderedConnections = [...connectionStore.connections].sort((left, right) => {
|
||||
const priority = (conn: ConnectionConfig) => {
|
||||
if (conn.id === connectionStore.activeConnectionId) return 0;
|
||||
if (connectionStore.connectedIds.has(conn.id)) return 1;
|
||||
return 2;
|
||||
};
|
||||
return priority(left) - priority(right);
|
||||
});
|
||||
for (const conn of orderedConnections) {
|
||||
// listCompletionTables connects on demand. Keeping disconnected connections
|
||||
// out here makes quick-open blind to unloaded tables after a cold start.
|
||||
if (REMOTE_SEARCH_UNSUPPORTED_TYPES.has(conn.db_type)) continue;
|
||||
const databases = new Set<string>();
|
||||
collectConnectionDatabases(connectionStore.treeNodes, conn.id, databases);
|
||||
if (conn.database?.trim()) databases.add(conn.database.trim());
|
||||
@@ -482,6 +491,8 @@ export function useQuickOpen() {
|
||||
for (const database of conn.attached_databases ?? []) {
|
||||
if (database.name.trim()) databases.add(database.name.trim());
|
||||
}
|
||||
const defaultDatabase = resolveDefaultDatabase(conn, [...databases]);
|
||||
if (defaultDatabase) databases.add(defaultDatabase);
|
||||
if (databases.size > 0) databasesByConnection.push({ conn, databases: [...databases] });
|
||||
}
|
||||
|
||||
@@ -500,39 +511,54 @@ export function useQuickOpen() {
|
||||
return contexts;
|
||||
}
|
||||
|
||||
async function acquireRemoteRequestSlot(): Promise<void> {
|
||||
async function acquireRemoteRequestSlot(generation: number): Promise<boolean> {
|
||||
if (generation !== remoteSearchGeneration) return false;
|
||||
if (activeRemoteRequests < REMOTE_SEARCH_CONCURRENCY) {
|
||||
activeRemoteRequests++;
|
||||
return;
|
||||
return true;
|
||||
}
|
||||
return new Promise<boolean>((resolve) => remoteRequestWaiters.push({ generation, resolve }));
|
||||
}
|
||||
|
||||
function cancelStaleRemoteRequestWaiters(generation: number): void {
|
||||
for (let index = remoteRequestWaiters.length - 1; index >= 0; index -= 1) {
|
||||
const waiter = remoteRequestWaiters[index]!;
|
||||
if (waiter.generation === generation) continue;
|
||||
remoteRequestWaiters.splice(index, 1);
|
||||
waiter.resolve(false);
|
||||
}
|
||||
await new Promise<void>((resolve) => remoteRequestWaiters.push(resolve));
|
||||
}
|
||||
|
||||
function releaseRemoteRequestSlot(): void {
|
||||
const next = remoteRequestWaiters.shift();
|
||||
if (next) next();
|
||||
let next = remoteRequestWaiters.shift();
|
||||
while (next && next.generation !== remoteSearchGeneration) {
|
||||
next.resolve(false);
|
||||
next = remoteRequestWaiters.shift();
|
||||
}
|
||||
if (next) next.resolve(true);
|
||||
else activeRemoteRequests--;
|
||||
}
|
||||
|
||||
async function runRemoteSearch(query: string, generation: number, contexts: Array<{ conn: ConnectionConfig; database: string }>): Promise<void> {
|
||||
const groups = await Promise.all(
|
||||
contexts.map(async ({ conn, database }) => {
|
||||
await acquireRemoteRequestSlot();
|
||||
const groups = contexts.map(() => [] as QuickOpenItem[]);
|
||||
await Promise.all(
|
||||
contexts.map(async ({ conn, database }, index) => {
|
||||
const acquired = await acquireRemoteRequestSlot(generation);
|
||||
if (!acquired) return;
|
||||
try {
|
||||
// A newer query may supersede queued work before it reaches the metadata API.
|
||||
if (generation !== remoteSearchGeneration) return [];
|
||||
const tables = await connectionStore.listCompletionTables(conn.id, database, query, REMOTE_SEARCH_RESULTS_PER_REQUEST, undefined, true);
|
||||
return tables.slice(0, REMOTE_SEARCH_RESULTS_PER_REQUEST).map((table) => remoteTableItem(table, conn, database));
|
||||
if (generation !== remoteSearchGeneration) return;
|
||||
const tables = await connectionStore.listCompletionTables(conn.id, database, query, REMOTE_SEARCH_RESULTS_PER_REQUEST, undefined, true, undefined, undefined, { activateConnection: false });
|
||||
if (generation !== remoteSearchGeneration) return;
|
||||
groups[index] = tables.slice(0, REMOTE_SEARCH_RESULTS_PER_REQUEST).map((table) => remoteTableItem(table, conn, database));
|
||||
remoteItems.value = groups.flat().slice(0, REMOTE_SEARCH_MAX_RESULTS);
|
||||
} catch {
|
||||
return [];
|
||||
return;
|
||||
} finally {
|
||||
releaseRemoteRequestSlot();
|
||||
}
|
||||
}),
|
||||
);
|
||||
|
||||
if (generation !== remoteSearchGeneration) return;
|
||||
remoteItems.value = groups.flat().slice(0, REMOTE_SEARCH_MAX_RESULTS);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -550,6 +576,7 @@ export function useQuickOpen() {
|
||||
searchQuery,
|
||||
(query) => {
|
||||
const generation = ++remoteSearchGeneration;
|
||||
cancelStaleRemoteRequestWaiters(generation);
|
||||
if (remoteSearchTimer) clearTimeout(remoteSearchTimer);
|
||||
remoteItems.value = [];
|
||||
|
||||
|
||||
Reference in new issue
Block a user