small search refactorings

This commit is contained in:
zadam 2023-02-28 23:23:17 +01:00
parent ee2953a5e1
commit f883fde74a
6 changed files with 30 additions and 23 deletions

View File

@ -124,35 +124,43 @@ function getNoteTitleForPath(notePathArray) {
* Archived (and hidden) notes are also returned, but non-archived paths are preferred if available * 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 * - 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 * - you can check whether returned path is archived using isArchived
*
* @param {BNote} note
* @param {string[]} path
*/ */
function getSomePath(note, path = []) { function getSomePath(note, path = []) {
// first try to find note within hoisted note, otherwise take any existing 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)
return getSomePathInner(note, [...path], true) || getSomePathInner(note, path, false);
|| getSomePathInner(note, [...path], false);
} }
/**
* @param {BNote} note
* @param {string[]} path
* @param {boolean}respectHoisting
* @returns {string[]|false}
*/
function getSomePathInner(note, path, respectHoisting) { function getSomePathInner(note, path, respectHoisting) {
if (note.isRoot()) { if (note.isRoot()) {
path.push(note.noteId); const foundPath = [...path, note.noteId];
path.reverse(); foundPath.reverse();
if (respectHoisting && !path.includes(cls.getHoistedNoteId())) { if (respectHoisting && !foundPath.includes(cls.getHoistedNoteId())) {
return false; return false;
} }
return path; return foundPath;
} }
const parents = note.parents; const parents = note.parents;
if (parents.length === 0) { 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; return false;
} }
for (const parentNote of parents) { for (const parentNote of parents) {
const retPath = getSomePathInner(parentNote, path.concat([note.noteId]), respectHoisting); const retPath = getSomePathInner(parentNote, [...path, note.noteId], respectHoisting);
if (retPath) { if (retPath) {
return retPath; return retPath;

View File

@ -1168,9 +1168,7 @@ class BNote extends AbstractBeccaEntity {
return false; return false;
} else if (parentNote.noteId === '_hidden') { } else if (parentNote.noteId === '_hidden') {
continue; continue;
} } else if (!parentNote.isHiddenCompletely()) {
if (!parentNote.isHiddenCompletely()) {
return false; return false;
} }
} }

View File

@ -73,7 +73,7 @@ class FNote {
this.mime = row.mime; this.mime = row.mime;
} }
addParent(parentNoteId, branchId) { addParent(parentNoteId, branchId, sort = true) {
if (parentNoteId === 'none') { if (parentNoteId === 'none') {
return; return;
} }
@ -83,6 +83,10 @@ class FNote {
} }
this.parentToBranch[parentNoteId] = branchId; this.parentToBranch[parentNoteId] = branchId;
if (sort) {
this.sortParents();
}
} }
addChild(childNoteId, branchId, sort = true) { 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 // 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 // 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) => { this.parents.sort((aNoteId, bNoteId) => {
const aBranchId = this.parentToBranch[aNoteId]; const aBranchId = this.parentToBranch[aNoteId];
@ -197,7 +201,7 @@ class FNote {
return 1; return 1;
} }
const aNote = this.froca.getNoteFromCache([aNoteId]); const aNote = this.froca.getNoteFromCache(aNoteId);
if (aNote.isArchived || aNote.isHiddenCompletely()) { if (aNote.isArchived || aNote.isHiddenCompletely()) {
return 1; return 1;

View File

@ -115,7 +115,7 @@ class Froca {
const childNote = this.notes[branch.noteId]; const childNote = this.notes[branch.noteId];
if (childNote) { if (childNote) {
childNote.addParent(branch.parentNoteId, branch.branchId); childNote.addParent(branch.parentNoteId, branch.branchId, false);
} }
const parentNote = this.notes[branch.parentNoteId]; const parentNote = this.notes[branch.parentNoteId];
@ -152,6 +152,7 @@ class Froca {
// sort all of them at once, this avoids repeated sorts (#1480) // sort all of them at once, this avoids repeated sorts (#1480)
for (const noteId of noteIdsToSort) { for (const noteId of noteIdsToSort) {
this.notes[noteId].sortChildren(); this.notes[noteId].sortChildren();
this.notes[noteId].sortParents();
} }
} }

View File

@ -58,7 +58,7 @@ async function resolveNotePathToSegments(notePath, hoistedNoteId = 'root', logEr
return; return;
} }
child.resortParents(); child.sortParents();
const parents = child.getParentNotes(); const parents = child.getParentNotes();

View File

@ -155,11 +155,8 @@ function findResultsWithExpression(expression, searchContext) {
const noteSet = expression.execute(allNoteSet, executionContext, searchContext); const noteSet = expression.execute(allNoteSet, executionContext, searchContext);
const searchResults = noteSet.notes const searchResults = noteSet.notes
.filter(note => !note.isDeleted)
.map(note => { .map(note => {
if (note.isDeleted) {
return null;
}
const notePathArray = executionContext.noteIdToNotePath[note.noteId] || beccaService.getSomePath(note); const notePathArray = executionContext.noteIdToNotePath[note.noteId] || beccaService.getSomePath(note);
if (!notePathArray) { if (!notePathArray) {
@ -167,8 +164,7 @@ function findResultsWithExpression(expression, searchContext) {
} }
return new SearchResult(notePathArray); return new SearchResult(notePathArray);
}) });
.filter(note => !!note);
for (const res of searchResults) { for (const res of searchResults) {
res.computeScore(searchContext.fulltextQuery, searchContext.highlightedTokens); res.computeScore(searchContext.fulltextQuery, searchContext.highlightedTokens);