Skip to content

Database governance built for humans and agents — controlling changes and access across every major database.

perf: optimize for the tree build (#18129)

ecmadao60d32cf65385
diff --git a/frontend/src/views/sql-editor/AsidePanel/WorksheetPane/SheetList/SheetTree.vue b/frontend/src/views/sql-editor/AsidePanel/WorksheetPane/SheetList/SheetTree.vue
index 60f5168639..e4ac456d0a 100644
--- a/frontend/src/views/sql-editor/AsidePanel/WorksheetPane/SheetList/SheetTree.vue
+++ b/frontend/src/views/sql-editor/AsidePanel/WorksheetPane/SheetList/SheetTree.vue
@@ -350,7 +350,7 @@ const handleRenameNode = useDebounceFn(async () => {
const editing = editingNode.value;
const moveFolder = async () => {
await updateWorksheetFolders(editing.node, editing.node.key, newKey);
- replaceExpandedKeys(editing.node.key, newKey);
+ replaceExpandedKeys({ oldKey: editing.node.key, newKey });
folderContext.moveFolder(editing.node.key, newKey);
cleanup();
};
@@ -804,35 +804,32 @@ const updateWorksheetFolders = async (
};
}
});
- await batchUpdateWorksheetFolders(
- worksheets.map((worksheet) => ({
- name: worksheet.name,
- folders: worksheet.folders,
- }))
- );
+ await batchUpdateWorksheetFolders(worksheets);
};
-const replaceExpandedKeys = (oldKey: string, newKey: string) => {
- const updates: Array<{ oldPath: string; newPath: string }> = [];
+const replaceExpandedKeys = ({
+ oldKey,
+ newKey,
+}: {
+ oldKey: string;
+ newKey?: string;
+}) => {
+ const newSet = new Set<string>();
for (const path of expandedKeys.value) {
if (
path === oldKey ||
folderContext.isSubFolder({ parent: oldKey, path, dig: true })
) {
- updates.push({
- oldPath: path,
- newPath: newKey ? path.replace(oldKey, newKey) : "",
- });
+ if (newKey) {
+ newSet.add(path.replace(oldKey, newKey));
+ }
+ } else {
+ newSet.add(path);
}
}
- for (const { oldPath, newPath } of updates) {
- expandedKeys.value.delete(oldPath);
- if (newPath) {
- expandedKeys.value.add(newPath);
- }
- }
+ expandedKeys.value = newSet;
};
const handleDuplicateFolderName = (
@@ -842,7 +839,7 @@ const handleDuplicateFolderName = (
const sameNode = parentNode?.children.find((child) => child.key === key);
const _defer = defer<boolean>();
- if (!!sameNode) {
+ if (sameNode) {
const dialogInstance = $dialog.create({
title: t("sheet.hint-tips.duplicate-folder-name-title"),
content: t("sheet.hint-tips.duplicate-folder-name-content", {
@@ -915,12 +912,12 @@ const handleDrop = async ({ node, dragNode }: TreeDropInfo) => {
nextTick(() => {
// Update expanded keys for the moved folder and all its subfolders
- replaceExpandedKeys(draggedNode.key, newKey);
+ replaceExpandedKeys({ oldKey: draggedNode.key, newKey });
// Ensure new parent folder is expanded to show the moved item
expandedKeys.value.add(parentNode.key);
// Close old parent folder if it's now empty
if (shouldCloseOldParent) {
- replaceExpandedKeys(oldParentNode.key, "");
+ replaceExpandedKeys({ oldKey: oldParentNode.key });
}
});
};
diff --git a/frontend/src/views/sql-editor/AsidePanel/WorksheetPane/SheetList/dropdown.ts b/frontend/src/views/sql-editor/AsidePanel/WorksheetPane/SheetList/dropdown.ts
index a0e4bf6505..ec553de934 100644
--- a/frontend/src/views/sql-editor/AsidePanel/WorksheetPane/SheetList/dropdown.ts
+++ b/frontend/src/views/sql-editor/AsidePanel/WorksheetPane/SheetList/dropdown.ts
@@ -98,6 +98,10 @@ export const useDropdown = (
return sheetStore.getWorksheetByName(context.node.worksheet.name);
});
+ const allowCreateNew = computed(
+ () => !filter.value.keyword && !filter.value.onlyShowStarred
+ );
+
const options = computed((): WorksheetDropdown[] => {
if (viewMode === "draft" || !context.node) {
return [];
@@ -147,7 +151,7 @@ export const useDropdown = (
);
}
} else {
- if (!filter.value.keyword) {
+ if (allowCreateNew.value) {
items.push({
icon: FolderIcon,
key: "add-folder",
@@ -155,7 +159,7 @@ export const useDropdown = (
});
}
if (viewMode === "my") {
- if (!filter.value.keyword) {
+ if (allowCreateNew.value) {
items.push({
icon: FileCodeIcon,
key: "add-worksheet",
diff --git a/frontend/src/views/sql-editor/Sheet/context.ts b/frontend/src/views/sql-editor/Sheet/context.ts
index 3b1eb1fc70..74cecfd50f 100644
--- a/frontend/src/views/sql-editor/Sheet/context.ts
+++ b/frontend/src/views/sql-editor/Sheet/context.ts
@@ -195,9 +195,10 @@ const useSheetTreeByView = (
const buildTree = (
parent: WorksheetFolderNode,
- worksheetsByFolder: Map<string, WorksheetLikeItem[]>
+ worksheetsByFolder: Map<string, WorksheetLikeItem[]>,
+ hideEmpty: boolean
) => {
- parent.children = folderContext
+ const subfolders = folderContext
.listSubFolders(parent.key)
.map((folder) => ({
isLeaf: false,
@@ -208,8 +209,11 @@ const useSheetTreeByView = (
}));
let empty = true;
- for (const childNode of parent.children) {
- const subtree = buildTree(childNode, worksheetsByFolder);
+ for (const childNode of subfolders) {
+ const subtree = buildTree(childNode, worksheetsByFolder, hideEmpty);
+ if (!subtree.empty || !hideEmpty) {
+ parent.children.push(subtree);
+ }
if (!subtree.empty) {
empty = false;
}
@@ -238,19 +242,19 @@ const useSheetTreeByView = (
return parent;
};
- const folderTree = computed(() => buildTree(getRootTreeNode(), new Map()));
+ const folderTree = computed(() =>
+ buildTree(getRootTreeNode(), new Map(), false)
+ );
const rebuildTree = useDebounceFn(() => {
- const folderPathes = new Set<string>([]);
+ const folderPaths = new Set<string>([]);
+ const worksheetsByFolder = new Map<string, WorksheetLikeItem[]>();
+
for (const worksheet of sheetLikeItemList.value) {
for (const path of getPathesForWorksheet(worksheet)) {
- folderPathes.add(path);
+ folderPaths.add(path);
}
- }
- folderContext.mergeFolders(folderPathes);
- const worksheetsByFolder = new Map<string, WorksheetLikeItem[]>();
- for (const worksheet of sheetLikeItemList.value) {
const pwd = getPwdForWorksheet(worksheet);
if (!worksheetsByFolder.has(pwd)) {
worksheetsByFolder.set(pwd, []);
@@ -258,7 +262,12 @@ const useSheetTreeByView = (
worksheetsByFolder.get(pwd)!.push(worksheet);
}
- sheetTree.value = buildTree(getRootTreeNode(), worksheetsByFolder);
+ folderContext.mergeFolders(folderPaths);
+ sheetTree.value = buildTree(
+ getRootTreeNode(),
+ worksheetsByFolder,
+ filter.value.onlyShowStarred
+ );
events.emit("on-built", { viewMode });
}, DEBOUNCE_SEARCH_DELAY);
@@ -380,32 +389,29 @@ export const revealWorksheets = <T>(
});
};
+const INITIAL_FILTER: WorksheetFilter = {
+ keyword: "",
+ showShared: true,
+ showMine: true,
+ showDraft: true,
+ onlyShowStarred: false,
+};
+
export const provideSheetContext = () => {
const me = useCurrentUserV1();
const tabStore = useSQLEditorTabStore();
const worksheetV1Store = useWorkSheetStore();
const { project } = storeToRefs(useSQLEditorStore());
- const initialFilter = computed(
- (): WorksheetFilter => ({
- keyword: "",
- showShared: true,
- showMine: true,
- showDraft: true,
- onlyShowStarred: false,
- })
- );
const filter = useDynamicLocalStorage<WorksheetFilter>(
computed(
() => `bb.sql-editor.${project.value}.worksheet-filter.${me.value.name}`
),
{
- ...initialFilter.value,
+ ...INITIAL_FILTER,
}
);
- const filterChanged = computed(
- () => !isEqual(filter.value, initialFilter.value)
- );
+ const filterChanged = computed(() => !isEqual(filter.value, INITIAL_FILTER));
const viewContexts = {
my: useSheetTreeByView(
diff --git a/frontend/src/views/sql-editor/Sheet/folder.ts b/frontend/src/views/sql-editor/Sheet/folder.ts
index 5f3a72b09c..13ab58bdc2 100644
--- a/frontend/src/views/sql-editor/Sheet/folder.ts
+++ b/frontend/src/views/sql-editor/Sheet/folder.ts
@@ -87,30 +87,23 @@ export const useFolderByView = (
const fromPath = ensureFolderPath(from);
const toPath = ensureFolderPath(to);
- const pendingUpdatePathes = [];
+ const newSet = new Set<string>();
for (const path of localCache.value.values()) {
if (
path === fromPath ||
isSubFolder({ parent: fromPath, path, dig: true })
) {
- pendingUpdatePathes.push({
- old: path,
- new: path.replace(fromPath, toPath),
- });
+ newSet.add(path.replace(fromPath, toPath));
+ } else {
+ newSet.add(path);
}
}
-
- const newSet = new Set(localCache.value);
- for (const item of pendingUpdatePathes) {
- newSet.delete(item.old);
- newSet.add(item.new);
- }
localCache.value = newSet;
};
- const mergeFolders = (pathes: Set<string>) => {
+ const mergeFolders = (paths: Set<string>) => {
const newSet = new Set(localCache.value);
- for (const folder of pathes.values()) {
+ for (const folder of paths.values()) {
const validPath = ensureFolderPath(folder);
newSet.add(validPath);
}