feat(sidebar): add regular expression search

This commit is contained in:
Lan-zk authored and GitHub committed 2026-08-14 02:25:24 +08:00
1 parent 5df8990802
commit 98f5c52f8e
26 files changed
+2203 -54

No files matched your search

@@ -313,6 +313,42 @@ describe("connectionStore metadata loading", () => {
).toBe(false);
}, 15000);
it("preserves loaded Xugu type members when a search projection collapses the type", async () => {
vi.doMock("@/lib/backend/tauriRuntime", () => ({ isTauriRuntime: () => false }));
vi.doMock("@/lib/backend/api", () => ({ checkConnectionHealth: vi.fn().mockResolvedValue(undefined) }));
const { useConnectionStore } = await import("@/stores/connectionStore");
const store = useConnectionStore();
const connection = xuguConnection();
const typeNode: TreeNode = {
id: "xugu-1:app_db:app_schema:type:address_t",
label: "ADDRESS_T",
type: "type",
objectName: "ADDRESS_T",
connectionId: connection.id,
database: "app_db",
schema: "app_schema",
xuguTypeMembersExpandable: true,
isExpanded: true,
children: Array.from({ length: 400 }, (_, index) => ({
id: `xugu-1:app_db:app_schema:type:address_t:attribute:${index}`,
label: `attribute_${index}`,
type: "type-attribute" as const,
connectionId: connection.id,
database: "app_db",
schema: "app_schema",
})),
};
store.connections = [connection];
store.connectedIds = new Set([connection.id]);
store.treeNodes = [typeNode];
await store.loadXuguTypeMembers(typeNode, { preserveCollapsedChildren: true });
expect(typeNode.isExpanded).toBe(false);
expect(typeNode.children).toHaveLength(400);
});
it("loads Oracle package overloads through the shared package member path", async () => {
const completionAssistantSearch = vi.fn().mockResolvedValue({
candidates: [
@@ -802,9 +838,12 @@ describe("connectionStore metadata loading", () => {
const treeCache = decodeSchemaTreeCache<TreeNode[]>(cachedPayloads.get(treeCacheKey));
const indexCache = decodeSchemaTreeCache<TreeNode[]>(cachedPayloads.get(indexCacheKey));
expect(treeCache?.children.map((node) => node.label)).toEqual(["fresh_table"]);
expect(indexCache?.tableSearchIndex?.entries).toEqual([{ name: "indexed_table", tableType: "TABLE" }]);
await expect(store.loadSidebarTableSearchIndex(tablesGroup.id)).resolves.toEqual([{ name: "indexed_table", table_type: "TABLE" }]);
expect(loadSchemaCache).toHaveBeenLastCalledWith(indexCacheKey);
// The local table index preserves comments for regex search matching.
expect(indexCache?.tableSearchIndex?.entries).toEqual([{ name: "indexed_table", tableType: "TABLE", comment: null }]);
await expect(store.loadSidebarTableSearchIndex(tablesGroup.id)).resolves.toEqual([{ name: "indexed_table", table_type: "TABLE", comment: null }]);
// The refreshed index is served from the in-memory cache; refreshing
// additionally registers the scope by reading the sidebar index manifest.
expect(loadSchemaCache).toHaveBeenLastCalledWith("dbx:sidebar-table-search-index-manifest-v1");
});
it("bypasses Oracle object-group caches created before DIP visibility was fixed", async () => {
@@ -52,6 +52,7 @@ describe("connectionStore MongoDB Legacy fallback", () => {
saveConnections: vi.fn().mockResolvedValue(undefined),
saveSidebarLayout: vi.fn().mockResolvedValue(undefined),
deleteSchemaCachePrefix: vi.fn().mockResolvedValue(undefined),
loadSchemaCache: vi.fn().mockResolvedValue(null),
}));
const { useConnectionStore } = await import("@/stores/connectionStore");
@@ -93,6 +94,7 @@ describe("connectionStore MongoDB Legacy fallback", () => {
saveConnections: vi.fn().mockResolvedValue(undefined),
saveSidebarLayout: vi.fn().mockResolvedValue(undefined),
deleteSchemaCachePrefix: vi.fn().mockResolvedValue(undefined),
loadSchemaCache: vi.fn().mockResolvedValue(null),
}));
const { useConnectionStore } = await import("@/stores/connectionStore");
@@ -45,6 +45,7 @@ function installApiMocks(extra: Record<string, unknown> = {}) {
checkConnectionHealth: vi.fn().mockResolvedValue(undefined),
connectDb: vi.fn().mockResolvedValue("pg-1"),
deleteSchemaCachePrefix: vi.fn().mockResolvedValue(undefined),
loadSchemaCache: vi.fn().mockResolvedValue(null),
saveConnections: vi.fn().mockResolvedValue(undefined),
saveSidebarLayout: vi.fn().mockResolvedValue(undefined),
connectionDatabaseInfo: vi.fn().mockResolvedValue(undefined),
+191 -13
View File
@@ -68,6 +68,7 @@ import { connectionObjectTreeNodeSchema, connectionObjectTreeQuerySchema, connec
import { buildDatabaseTreeNodes, buildDuckDbConnectionTreeNodes, compareSidebarNames, sortSidebarDatabases, sortSidebarNames, shouldIncludeDefaultDatabaseNode } from "@/lib/database/databaseTree";
import { buildSqlServerDatabaseTreeNodes } from "@/lib/database/sqlServerTree";
import { collapseExpandedTreeNodes } from "@/lib/sidebar/sidebarTreeCollapse";
import { findNodePathByIdentity, nodeMatchesRegexScopeIdentity, type SidebarRegexScopeIdentity } from "@/lib/sidebar/sidebarSearchTree";
import { findDatabaseTreeNode } from "@/lib/sidebar/treeRefreshTarget";
import { simpleModeEmptyShellNeedsConfirmedLoad, treeNodeLoadedChildrenContentPresent } from "@/lib/sidebar/treeLoadedChildrenMarker";
import { shouldMarkDisconnected } from "@/lib/connection/connectionHealth";
@@ -97,7 +98,7 @@ import {
type DatabaseObjectTreeKind,
} from "@/lib/table/tableTree";
import { hasTreeNodeDatabaseContext, normalizeCataloglessDatabaseNodes, treeNodeSchemaCachePrefix } from "@/lib/sidebar/treeNodeContext";
import { decodeSchemaTreeCache, encodeSchemaTreeCache } from "@/lib/metadata/schemaTreeCache";
import { decodeSchemaTreeCache, decodeTableSearchIndexManifest, encodeSchemaTreeCache, encodeTableSearchIndexManifest, type TableSearchIndexManifestEntry } from "@/lib/metadata/schemaTreeCache";
import { sortSidebarTreeChildrenForParent } from "@/lib/sidebar/sidebarNodeOrdering";
import { connectionSupportsDatabaseUserAdmin } from "@/lib/database/databaseUserAdmin";
import { getTableMetadataCapabilities } from "@/lib/table/tableMetadataCapabilities";
@@ -302,6 +303,7 @@ interface LoadTreeOptions {
expectedSidebarTableSearchQuery?: string;
tableNameFilterScopeKey?: string;
expectedTableNameFilterRevision?: number;
preserveCollapsedChildren?: boolean;
}
interface PersistedTreeChildrenLoadResult {
@@ -381,6 +383,15 @@ export const useConnectionStore = defineStore("connection", () => {
const schemaListCache = ref<Record<string, string[]>>({});
const sidebarSearchQuery = ref("");
const sidebarTableSearchQueries = ref<Record<string, string>>({});
// Local table indexes are immutable snapshots until an explicit refresh.
// Keep both resolved values and in-flight reads here so regex typing does
// not reread SQLite for every keypress or issue duplicate concurrent loads.
const sidebarTableSearchIndexCache = new Map<string, TableInfo[] | null>();
const sidebarTableSearchIndexInFlight = new Map<string, Promise<TableInfo[] | null>>();
const sidebarTableSearchIndexConnectionGenerations = new Map<string, number>();
let sidebarTableSearchIndexManifest: TableSearchIndexManifestEntry[] | null = null;
let sidebarTableSearchIndexManifestInFlight: Promise<TableSearchIndexManifestEntry[]> | null = null;
let sidebarTableSearchIndexManifestWriteQueue: Promise<void> = Promise.resolve();
const sidebarTableNameFilters = ref<Record<string, TableNameFilter>>(loadSidebarTableNameFilters());
const sidebarTableNameFilterRevisions = new Map<string, number>();
const completionTableIndex = new Map<string, { touched: number; tables: SqlCompletionTable[] }>();
@@ -2060,25 +2071,178 @@ export const useConnectionStore = defineStore("connection", () => {
function sidebarTableSearchIndexCacheKey(parent: TreeNode): string | null {
const treeCacheKey = sidebarTableSearchTreeCacheKey(parent);
return treeCacheKey ? `${treeCacheKey}:table-search-index-v1` : null;
if (!treeCacheKey) return null;
// Catalog-capable databases may expose the same database name in several
// catalogs. Their metadata tree cache keys predate catalog support, so use
// the complete scope identity for the table index without changing those
// shared metadata-cache formats. Catalogless scopes keep the v1 key so
// indexes created before the regex-search manifest remain discoverable.
if (parent.catalog) return schemaCacheKey(parent.connectionId || "", parent.catalog, parent.database || "", parent.schema || "", parent.type, "table-search-index-v2");
return `${treeCacheKey}:table-search-index-v1`;
}
async function loadSidebarTableSearchIndex(parentNodeId: string): Promise<TableInfo[] | null> {
const parent = findNode(treeNodes.value, parentNodeId);
const sidebarTableSearchIndexManifestCacheKey = "dbx:sidebar-table-search-index-manifest-v1";
function sidebarTableSearchIndexManifestEntry(parent: TreeNode, cacheKey: string): TableSearchIndexManifestEntry | null {
if (!parent.connectionId || !parent.database || !parent.type) return null;
const identity: SidebarRegexScopeIdentity = {
connectionId: parent.connectionId,
database: parent.database,
schema: parent.schema,
catalog: parent.catalog,
nodeType: parent.type,
};
const findPath = (nodes: TreeNode[], targetId: string, path: TreeNode[] = []): TreeNode[] | null => {
for (const node of nodes) {
const next = [...path, node];
if (node.id === targetId) {
// Same-id nodes can exist in other branches; only the node matching
// the full database context is the index parent we registered.
if (nodeMatchesRegexScopeIdentity(node, targetId, identity)) return next;
continue;
}
if (node.children) {
const found = findPath(node.children, targetId, next);
if (found) return found;
}
}
return null;
};
const path = findPath(treeNodes.value, parent.id)?.map((node) => ({
id: node.id,
label: node.label,
type: node.type,
...(node.connectionId ? { connectionId: node.connectionId } : {}),
...(node.database !== undefined ? { database: node.database } : {}),
...(node.catalog !== undefined ? { catalog: node.catalog } : {}),
...(node.schema !== undefined ? { schema: node.schema } : {}),
...(node.linkedServer !== undefined ? { linkedServer: node.linkedServer } : {}),
...(node.linkedCatalog !== undefined ? { linkedCatalog: node.linkedCatalog } : {}),
...(node.linkedSchema !== undefined ? { linkedSchema: node.linkedSchema } : {}),
}));
return {
cacheKey,
parentNodeId: parent.id,
connectionId: parent.connectionId,
database: parent.database,
...(parent.schema ? { schema: parent.schema } : {}),
...(parent.catalog ? { catalog: parent.catalog } : {}),
nodeType: parent.type,
...(path ? { path } : {}),
};
}
async function loadSidebarTableSearchIndexManifest(): Promise<TableSearchIndexManifestEntry[]> {
if (sidebarTableSearchIndexManifest) return sidebarTableSearchIndexManifest;
if (sidebarTableSearchIndexManifestInFlight) return sidebarTableSearchIndexManifestInFlight;
const read = (async () => {
const payload = await api.loadSchemaCache<unknown>(sidebarTableSearchIndexManifestCacheKey).catch(() => null);
sidebarTableSearchIndexManifest = decodeTableSearchIndexManifest(payload);
return sidebarTableSearchIndexManifest;
})();
sidebarTableSearchIndexManifestInFlight = read;
try {
return await read;
} finally {
sidebarTableSearchIndexManifestInFlight = null;
}
}
function sidebarTableSearchIndexConnectionGeneration(connectionId: string): number {
return sidebarTableSearchIndexConnectionGenerations.get(connectionId) ?? 0;
}
async function invalidateSidebarTableSearchIndexesForConnection(connectionId: string): Promise<void> {
sidebarTableSearchIndexConnectionGenerations.set(connectionId, sidebarTableSearchIndexConnectionGeneration(connectionId) + 1);
const rawPrefix = `${connectionId}:`;
const encodedPrefix = `${schemaCacheKey(connectionId)}:`;
const matchesConnectionCacheKey = (cacheKey: string) => cacheKey.startsWith(rawPrefix) || cacheKey.startsWith(encodedPrefix);
for (const cacheKey of sidebarTableSearchIndexCache.keys()) {
if (matchesConnectionCacheKey(cacheKey)) sidebarTableSearchIndexCache.delete(cacheKey);
}
for (const cacheKey of sidebarTableSearchIndexInFlight.keys()) {
if (matchesConnectionCacheKey(cacheKey)) sidebarTableSearchIndexInFlight.delete(cacheKey);
}
sidebarTableSearchIndexManifestWriteQueue = sidebarTableSearchIndexManifestWriteQueue.then(async () => {
const manifest = await loadSidebarTableSearchIndexManifest();
const nextManifest = manifest.filter((scope) => scope.connectionId !== connectionId);
if (nextManifest.length === manifest.length) return;
sidebarTableSearchIndexManifest = nextManifest;
await api.saveSchemaCache(sidebarTableSearchIndexManifestCacheKey, encodeTableSearchIndexManifest(nextManifest)).catch(() => undefined);
});
await sidebarTableSearchIndexManifestWriteQueue;
}
async function registerSidebarTableSearchIndexScope(parent: TreeNode, cacheKey: string): Promise<void> {
const entry = sidebarTableSearchIndexManifestEntry(parent, cacheKey);
if (!entry) return;
sidebarTableSearchIndexManifestWriteQueue = sidebarTableSearchIndexManifestWriteQueue.then(async () => {
const manifest = await loadSidebarTableSearchIndexManifest();
const matchesEntryScope = (item: TableSearchIndexManifestEntry) => item.parentNodeId === entry.parentNodeId && item.connectionId === entry.connectionId && item.database === entry.database && item.schema === entry.schema && item.catalog === entry.catalog && item.nodeType === entry.nodeType;
const existing = manifest.find((item) => matchesEntryScope(item));
if (existing?.cacheKey === entry.cacheKey) return;
// Re-registering a scope after its cache-key format changes replaces the
// old manifest entry, so stale and refreshed indexes are never merged.
sidebarTableSearchIndexManifest = [...manifest.filter((item) => !matchesEntryScope(item)), entry];
await api.saveSchemaCache(sidebarTableSearchIndexManifestCacheKey, encodeTableSearchIndexManifest(sidebarTableSearchIndexManifest)).catch(() => undefined);
});
await sidebarTableSearchIndexManifestWriteQueue;
}
async function readSidebarTableSearchIndexCache(cacheKey: string, connectionId: string): Promise<TableInfo[] | null> {
if (sidebarTableSearchIndexCache.has(cacheKey)) return sidebarTableSearchIndexCache.get(cacheKey) ?? null;
const pending = sidebarTableSearchIndexInFlight.get(cacheKey);
if (pending) return pending;
const generation = sidebarTableSearchIndexConnectionGeneration(connectionId);
const read = (async () => {
const decoded = decodeSchemaTreeCache<TreeNode[]>(await api.loadSchemaCache<unknown>(cacheKey).catch(() => null));
const index = decoded?.tableSearchIndex;
const entries = index ? index.entries.map((entry) => ({ name: entry.name, table_type: entry.tableType, ...(entry.comment !== undefined ? { comment: entry.comment } : {}) })) : null;
if (generation !== sidebarTableSearchIndexConnectionGeneration(connectionId)) return null;
sidebarTableSearchIndexCache.set(cacheKey, entries);
return entries;
})();
sidebarTableSearchIndexInFlight.set(cacheKey, read);
try {
return await read;
} finally {
if (sidebarTableSearchIndexInFlight.get(cacheKey) === read) sidebarTableSearchIndexInFlight.delete(cacheKey);
}
}
function findSidebarTreeNodeByIdentity(parentNodeId: string, identity: SidebarRegexScopeIdentity): TreeNode | null {
const path = findNodePathByIdentity(treeNodes.value, parentNodeId, identity);
return path?.[path.length - 1] ?? null;
}
async function loadSidebarTableSearchIndex(parentNodeId: string, identity?: SidebarRegexScopeIdentity): Promise<TableInfo[] | null> {
// The identity disambiguates same-id nodes (e.g. database "a:b" vs schema
// "b" under database "a") so the correct cache key is read and registered.
const parent = identity ? findSidebarTreeNodeByIdentity(parentNodeId, identity) : findNode(treeNodes.value, parentNodeId);
if (!parent) return null;
const cacheKey = sidebarTableSearchIndexCacheKey(parent);
if (!cacheKey) return null;
const decoded = decodeSchemaTreeCache<TreeNode[]>(await api.loadSchemaCache<unknown>(cacheKey).catch(() => null));
const index = decoded?.tableSearchIndex;
if (!index) return null;
return index.entries.map((entry) => ({ name: entry.name, table_type: entry.tableType }));
const entries = await readSidebarTableSearchIndexCache(cacheKey, parent.connectionId || "");
if (entries) await registerSidebarTableSearchIndexScope(parent, cacheKey);
return entries;
}
async function refreshSidebarTableSearchIndex(parentNodeId: string): Promise<TableInfo[]> {
const parent = findNode(treeNodes.value, parentNodeId);
async function loadSidebarTableSearchIndexScopes(): Promise<Array<{ scope: TableSearchIndexManifestEntry; entries: TableInfo[] }>> {
const manifest = await loadSidebarTableSearchIndexManifest();
const scopes: Array<{ scope: TableSearchIndexManifestEntry; entries: TableInfo[] }> = [];
for (const scope of manifest) {
const entries = await readSidebarTableSearchIndexCache(scope.cacheKey, scope.connectionId);
if (entries) scopes.push({ scope, entries });
}
return scopes;
}
async function refreshSidebarTableSearchIndex(parentNodeId: string, identity?: SidebarRegexScopeIdentity): Promise<TableInfo[]> {
const parent = identity ? findSidebarTreeNodeByIdentity(parentNodeId, identity) : findNode(treeNodes.value, parentNodeId);
if (!parent?.connectionId || !hasTreeNodeDatabaseContext(parent)) return [];
const cacheKey = sidebarTableSearchIndexCacheKey(parent);
if (!cacheKey) return [];
const generation = sidebarTableSearchIndexConnectionGeneration(parent.connectionId);
await ensureConnected(parent.connectionId);
const config = getConfig(parent.connectionId);
const querySchema = connectionObjectTreeQuerySchema(config, parent.database, parent.schema);
@@ -2086,13 +2250,25 @@ export const useConnectionStore = defineStore("connection", () => {
const pageSize = sidebarObjectGroupPageSize();
const entries: TableInfo[] = [];
for (let offset = 0; ; offset += pageSize) {
if (generation !== sidebarTableSearchIndexConnectionGeneration(parent.connectionId)) return [];
const page = await listTablesWithOptionalTableNameFilter(parent.connectionId, parent.database, querySchema, undefined, pageSize, offset, objectTypes, parent.catalog);
entries.push(...page);
if (page.length < pageSize) break;
}
const deduped = [...new Map(entries.map((entry) => [`${entry.table_type}\0${entry.name}`, entry])).values()];
const tableSearchIndex = { complete: true as const, indexedAt: new Date().toISOString(), entries: deduped.map((entry) => ({ name: entry.name, tableType: entry.table_type })) };
const tableSearchIndex = {
complete: true as const,
indexedAt: new Date().toISOString(),
entries: deduped.map((entry) => ({ name: entry.name, tableType: entry.table_type, ...(entry.comment !== undefined ? { comment: entry.comment } : {}) })),
};
if (generation !== sidebarTableSearchIndexConnectionGeneration(parent.connectionId)) return [];
await api.saveSchemaCache(cacheKey, encodeSchemaTreeCache<TreeNode[]>([], Date.now(), tableSearchIndex));
if (generation !== sidebarTableSearchIndexConnectionGeneration(parent.connectionId)) {
await api.deleteSchemaCachePrefix(cacheKey).catch(() => undefined);
return [];
}
sidebarTableSearchIndexCache.set(cacheKey, deduped);
await registerSidebarTableSearchIndexScope(parent, cacheKey);
return deduped;
}
@@ -2569,6 +2745,7 @@ export const useConnectionStore = defineStore("connection", () => {
clearConnectionHealthCheck(config.id);
invalidateCompletionCache(config.id);
void invalidateObjectDdlCache({ connectionId: config.id });
await invalidateSidebarTableSearchIndexesForConnection(config.id);
clearLoadedChildrenCache(config.id);
const node = findConnectionNode(config.id);
if (node?.isExpanded) {
@@ -5924,14 +6101,14 @@ export const useConnectionStore = defineStore("connection", () => {
}
}
async function loadXuguTypeMembers(node: TreeNode): Promise<void> {
async function loadXuguTypeMembers(node: TreeNode, options?: Pick<LoadTreeOptions, "preserveCollapsedChildren">): Promise<void> {
if (!isXuguTypeMemberContainer(node, getConfig(node.connectionId || "")?.db_type)) return;
const connectionId = node.connectionId;
const database = node.database;
if (!connectionId || !database) return;
if (node.isExpanded) {
node.isExpanded = false;
if (!sidebarSearchQuery.value) releaseCollapsedTreeNodeChildren(node.id);
if (!sidebarSearchQuery.value && !options?.preserveCollapsedChildren) releaseCollapsedTreeNodeChildren(node.id);
return;
}
if (node.children && node.children.length > 0) {
@@ -7743,6 +7920,7 @@ export const useConnectionStore = defineStore("connection", () => {
setSidebarTableSearchQuery,
refreshSidebarTableSearch,
loadSidebarTableSearchIndex,
loadSidebarTableSearchIndexScopes,
refreshSidebarTableSearchIndex,
createConnectionGroup(name: string, parentGroupId?: string | null) {
const result = createGroupOp(sidebarLayout.value, name, parentGroupId);