From f883fde74ab0fb09c672860544b9a917330ebb98 Mon Sep 17 00:00:00 2001 From: zadam Date: Tue, 28 Feb 2023 23:23:17 +0100 Subject: [PATCH] small search refactorings --- src/becca/becca_service.js | 26 +++++++++++++++++--------- src/becca/entities/bnote.js | 4 +--- src/public/app/entities/fnote.js | 10 +++++++--- src/public/app/services/froca.js | 3 ++- src/public/app/services/tree.js | 2 +- src/services/search/services/search.js | 8 ++------ 6 files changed, 30 insertions(+), 23 deletions(-) diff --git a/src/becca/becca_service.js b/src/becca/becca_service.js index 95a8c95ae..80942c564 100644 --- a/src/becca/becca_service.js +++ b/src/becca/becca_service.js @@ -124,35 +124,43 @@ function getNoteTitleForPath(notePathArray) { * Archived (and hidden) notes are also returned, but non-archived paths are preferred if available * - this means that archived paths is returned only if there's no non-archived path * - you can check whether returned path is archived using isArchived + * + * @param {BNote} note + * @param {string[]} path */ function getSomePath(note, path = []) { // first try to find note within hoisted note, otherwise take any existing note path - // each branch needs a separate copy since it's mutable - return getSomePathInner(note, [...path], true) - || getSomePathInner(note, [...path], false); + return getSomePathInner(note, path, true) + || getSomePathInner(note, path, false); } +/** + * @param {BNote} note + * @param {string[]} path + * @param {boolean}respectHoisting + * @returns {string[]|false} + */ function getSomePathInner(note, path, respectHoisting) { if (note.isRoot()) { - path.push(note.noteId); - path.reverse(); + const foundPath = [...path, note.noteId]; + foundPath.reverse(); - if (respectHoisting && !path.includes(cls.getHoistedNoteId())) { + if (respectHoisting && !foundPath.includes(cls.getHoistedNoteId())) { return false; } - return path; + return foundPath; } const parents = note.parents; if (parents.length === 0) { - console.log(`Note ${note.noteId} - "${note.title}" has no parents.`); + console.log(`Note '${note.noteId}' - '${note.title}' has no parents.`); return false; } for (const parentNote of parents) { - const retPath = getSomePathInner(parentNote, path.concat([note.noteId]), respectHoisting); + const retPath = getSomePathInner(parentNote, [...path, note.noteId], respectHoisting); if (retPath) { return retPath; diff --git a/src/becca/entities/bnote.js b/src/becca/entities/bnote.js index 8a752058a..6ef73b28d 100644 --- a/src/becca/entities/bnote.js +++ b/src/becca/entities/bnote.js @@ -1168,9 +1168,7 @@ class BNote extends AbstractBeccaEntity { return false; } else if (parentNote.noteId === '_hidden') { continue; - } - - if (!parentNote.isHiddenCompletely()) { + } else if (!parentNote.isHiddenCompletely()) { return false; } } diff --git a/src/public/app/entities/fnote.js b/src/public/app/entities/fnote.js index 7078cecae..1f44a97bf 100644 --- a/src/public/app/entities/fnote.js +++ b/src/public/app/entities/fnote.js @@ -73,7 +73,7 @@ class FNote { this.mime = row.mime; } - addParent(parentNoteId, branchId) { + addParent(parentNoteId, branchId, sort = true) { if (parentNoteId === 'none') { return; } @@ -83,6 +83,10 @@ class FNote { } this.parentToBranch[parentNoteId] = branchId; + + if (sort) { + this.sortParents(); + } } addChild(childNoteId, branchId, sort = true) { @@ -189,7 +193,7 @@ class FNote { // will sort the parents so that non-search & non-archived are first and archived at the end // this is done so that non-search & non-archived paths are always explored as first when looking for note path - resortParents() { + sortParents() { this.parents.sort((aNoteId, bNoteId) => { const aBranchId = this.parentToBranch[aNoteId]; @@ -197,7 +201,7 @@ class FNote { return 1; } - const aNote = this.froca.getNoteFromCache([aNoteId]); + const aNote = this.froca.getNoteFromCache(aNoteId); if (aNote.isArchived || aNote.isHiddenCompletely()) { return 1; diff --git a/src/public/app/services/froca.js b/src/public/app/services/froca.js index 476700cec..8bedf2fa6 100644 --- a/src/public/app/services/froca.js +++ b/src/public/app/services/froca.js @@ -115,7 +115,7 @@ class Froca { const childNote = this.notes[branch.noteId]; if (childNote) { - childNote.addParent(branch.parentNoteId, branch.branchId); + childNote.addParent(branch.parentNoteId, branch.branchId, false); } const parentNote = this.notes[branch.parentNoteId]; @@ -152,6 +152,7 @@ class Froca { // sort all of them at once, this avoids repeated sorts (#1480) for (const noteId of noteIdsToSort) { this.notes[noteId].sortChildren(); + this.notes[noteId].sortParents(); } } diff --git a/src/public/app/services/tree.js b/src/public/app/services/tree.js index cc0112d49..3c4925f3e 100644 --- a/src/public/app/services/tree.js +++ b/src/public/app/services/tree.js @@ -58,7 +58,7 @@ async function resolveNotePathToSegments(notePath, hoistedNoteId = 'root', logEr return; } - child.resortParents(); + child.sortParents(); const parents = child.getParentNotes(); diff --git a/src/services/search/services/search.js b/src/services/search/services/search.js index 9ca8386ba..06362b28c 100644 --- a/src/services/search/services/search.js +++ b/src/services/search/services/search.js @@ -155,11 +155,8 @@ function findResultsWithExpression(expression, searchContext) { const noteSet = expression.execute(allNoteSet, executionContext, searchContext); const searchResults = noteSet.notes + .filter(note => !note.isDeleted) .map(note => { - if (note.isDeleted) { - return null; - } - const notePathArray = executionContext.noteIdToNotePath[note.noteId] || beccaService.getSomePath(note); if (!notePathArray) { @@ -167,8 +164,7 @@ function findResultsWithExpression(expression, searchContext) { } return new SearchResult(notePathArray); - }) - .filter(note => !!note); + }); for (const res of searchResults) { res.computeScore(searchContext.fulltextQuery, searchContext.highlightedTokens);