To prevent search lag when there are a large number of notes

This commit is contained in:
SiriusXT 2025-04-01 21:07:15 +08:00
parent a7799d32b0
commit adcb803caa
4 changed files with 15 additions and 13 deletions

View File

@ -11,7 +11,8 @@ const SELECTED_NOTE_PATH_KEY = "data-note-path";
const SELECTED_EXTERNAL_LINK_KEY = "data-external-link"; const SELECTED_EXTERNAL_LINK_KEY = "data-external-link";
// To prevent search lag when there are a large number of notes, set a delay based on the number of notes to avoid jitter. // To prevent search lag when there are a large number of notes, set a delay based on the number of notes to avoid jitter.
const notesCount = await server.get<number>(`stats/notesCount`); const notesCount = await server.get<number>(`autocomplete/notesCount`);
console.log(notesCount);
let debounceTimeoutId: ReturnType<typeof setTimeout>; let debounceTimeoutId: ReturnType<typeof setTimeout>;
function getSearchDelay(notesCount: number): number { function getSearchDelay(notesCount: number): number {

View File

@ -8,6 +8,7 @@ import cls from "../../services/cls.js";
import becca from "../../becca/becca.js"; import becca from "../../becca/becca.js";
import type { Request } from "express"; import type { Request } from "express";
import ValidationError from "../../errors/validation_error.js"; import ValidationError from "../../errors/validation_error.js";
import sql from "../../services/sql.js";
function getAutocomplete(req: Request) { function getAutocomplete(req: Request) {
if (typeof req.query.query !== "string") { if (typeof req.query.query !== "string") {
@ -79,6 +80,15 @@ function getRecentNotes(activeNoteId: string) {
}); });
} }
// Get the total number of notes
function getNotesCount(req: Request) {
const notesCount = sql.getRow(
`SELECT COUNT(*) AS count FROM notes WHERE isDeleted = 0;`,
) as { count: number };
return notesCount.count;
}
export default { export default {
getAutocomplete getAutocomplete,
getNotesCount
}; };

View File

@ -48,16 +48,7 @@ function getSubtreeSize(req: Request) {
}; };
} }
// Get the total number of notes
function getNotesCount(req: Request) {
const notesCount = sql.getRow(
`SELECT COUNT(*) AS count FROM notes WHERE isDeleted = 0;`,
) as { count: number };
return notesCount.count;
}
export default { export default {
getNoteSize, getNoteSize,
getSubtreeSize, getSubtreeSize
getNotesCount
}; };

View File

@ -273,6 +273,7 @@ function register(app: express.Application) {
route(PST, "/api/setup/sync-seed", [auth.checkAppNotInitialized], setupApiRoute.saveSyncSeed, apiResultHandler, false); route(PST, "/api/setup/sync-seed", [auth.checkAppNotInitialized], setupApiRoute.saveSyncSeed, apiResultHandler, false);
apiRoute(GET, "/api/autocomplete", autocompleteApiRoute.getAutocomplete); apiRoute(GET, "/api/autocomplete", autocompleteApiRoute.getAutocomplete);
apiRoute(GET, "/api/autocomplete/notesCount", autocompleteApiRoute.getNotesCount);
apiRoute(GET, "/api/quick-search/:searchString", searchRoute.quickSearch); apiRoute(GET, "/api/quick-search/:searchString", searchRoute.quickSearch);
apiRoute(GET, "/api/search-note/:noteId", searchRoute.searchFromNote); apiRoute(GET, "/api/search-note/:noteId", searchRoute.searchFromNote);
apiRoute(PST, "/api/search-and-execute-note/:noteId", searchRoute.searchAndExecute); apiRoute(PST, "/api/search-and-execute-note/:noteId", searchRoute.searchAndExecute);
@ -361,7 +362,6 @@ function register(app: express.Application) {
apiRoute(GET, "/api/similar-notes/:noteId", similarNotesRoute.getSimilarNotes); apiRoute(GET, "/api/similar-notes/:noteId", similarNotesRoute.getSimilarNotes);
apiRoute(GET, "/api/backend-log", backendLogRoute.getBackendLog); apiRoute(GET, "/api/backend-log", backendLogRoute.getBackendLog);
apiRoute(GET, "/api/stats/note-size/:noteId", statsRoute.getNoteSize); apiRoute(GET, "/api/stats/note-size/:noteId", statsRoute.getNoteSize);
apiRoute(GET, "/api/stats/notesCount", statsRoute.getNotesCount);
apiRoute(GET, "/api/stats/subtree-size/:noteId", statsRoute.getSubtreeSize); apiRoute(GET, "/api/stats/subtree-size/:noteId", statsRoute.getSubtreeSize);
apiRoute(PST, "/api/delete-notes-preview", notesApiRoute.getDeleteNotesPreview); apiRoute(PST, "/api/delete-notes-preview", notesApiRoute.getDeleteNotesPreview);
route(GET, "/api/fonts", [auth.checkApiAuthOrElectron], fontsRoute.getFontCss); route(GET, "/api/fonts", [auth.checkApiAuthOrElectron], fontsRoute.getFontCss);