refactor(wiki): unify category cleaning and push directory listing to SQL

Centralize the wiki category-path normalization (separator/quote cleanup,
type-label stripping, dedup, depth cap) into a single types helper shared by
the service, repository, and taxonomy layers so stored and queried paths are
always cleaned identically and cannot drift.

Flatten category_path into fixed category_l1/l2/l3 columns (kept in sync on
write) and rewrite ListDistinctCategoryPathsByType to group, count, and
paginate directly in cross-dialect SQL, returning the page of child folders
plus the total distinct count. This removes the prior full-column scan and
in-memory pagination (whose maxPaths cap was effectively dead) and makes Total
and later pages correct.

Add the level columns + composite index to the wiki migrations and the repo
test schema, and cover the SQL grouping/counting/pagination with a new test.
This commit is contained in:
wizardchen
2026-06-16 17:38:31 +08:00
committed by lyingbug
parent fe33db9493
commit 88c10da0b0
10 changed files with 289 additions and 235 deletions
+6 -10
View File
@@ -162,19 +162,15 @@ func (h *WikiPageHandler) ListCategories(c *gin.Context) {
pageSize = 2000
}
parentPath := parseWikiCategoryPath(c.Query("parent_path"))
paths, err := h.wikiService.ListDistinctCategoryPathsByType(c.Request.Context(), kbID, pageTypes, parentPath, pageSize)
// Grouping and pagination are pushed to SQL; the service returns just the
// requested page of child folders plus the total distinct count.
paths, total, err := h.wikiService.ListDistinctCategoryPathsByType(c.Request.Context(), kbID, pageTypes, parentPath, page, pageSize)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
total := len(paths)
offset := (page - 1) * pageSize
if offset > total {
offset = total
}
end := offset + pageSize
if end > total {
end = total
if paths == nil {
paths = []types.WikiCategoryPath{}
}
totalPages := 0
if pageSize > 0 {
@@ -182,7 +178,7 @@ func (h *WikiPageHandler) ListCategories(c *gin.Context) {
}
c.JSON(http.StatusOK, types.WikiCategoryPathListResponse{
Paths: paths[offset:end],
Paths: paths,
Total: total,
Page: page,
PageSize: pageSize,