mirror of
				https://github.com/zadam/trilium.git
				synced 2025-11-03 20:06:08 +01:00 
			
		
		
		
	small refactorings and defensive programming in note cache
This commit is contained in:
		@@ -10,9 +10,7 @@ const stringSimilarity = require('string-similarity');
 | 
				
			|||||||
let loaded = false;
 | 
					let loaded = false;
 | 
				
			||||||
let loadedPromiseResolve;
 | 
					let loadedPromiseResolve;
 | 
				
			||||||
/** Is resolved after the initial load */
 | 
					/** Is resolved after the initial load */
 | 
				
			||||||
let loadedPromise = new Promise(res => {
 | 
					let loadedPromise = new Promise(res => loadedPromiseResolve = res);
 | 
				
			||||||
    loadedPromiseResolve = res;
 | 
					 | 
				
			||||||
});
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
let noteTitles = {};
 | 
					let noteTitles = {};
 | 
				
			||||||
let protectedNoteTitles = {};
 | 
					let protectedNoteTitles = {};
 | 
				
			||||||
@@ -30,9 +28,9 @@ async function load() {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
    prefixes = await sql.getMap(`SELECT noteId || '-' || parentNoteId, prefix FROM branches WHERE prefix IS NOT NULL AND prefix != ''`);
 | 
					    prefixes = await sql.getMap(`SELECT noteId || '-' || parentNoteId, prefix FROM branches WHERE prefix IS NOT NULL AND prefix != ''`);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    const relations = await sql.getRows(`SELECT branchId, noteId, parentNoteId FROM branches WHERE isDeleted = 0`);
 | 
					    const branches = await sql.getRows(`SELECT branchId, noteId, parentNoteId FROM branches WHERE isDeleted = 0`);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    for (const rel of relations) {
 | 
					    for (const rel of branches) {
 | 
				
			||||||
        childToParent[rel.noteId] = childToParent[rel.noteId] || [];
 | 
					        childToParent[rel.noteId] = childToParent[rel.noteId] || [];
 | 
				
			||||||
        childToParent[rel.noteId].push(rel.parentNoteId);
 | 
					        childToParent[rel.noteId].push(rel.parentNoteId);
 | 
				
			||||||
        childParentToBranchId[`${rel.noteId}-${rel.parentNoteId}`] = rel.branchId;
 | 
					        childParentToBranchId[`${rel.noteId}-${rel.parentNoteId}`] = rel.branchId;
 | 
				
			||||||
@@ -105,7 +103,7 @@ async function findNotes(query) {
 | 
				
			|||||||
    let noteIds = Object.keys(noteTitles);
 | 
					    let noteIds = Object.keys(noteTitles);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    if (protectedSessionService.isProtectedSessionAvailable()) {
 | 
					    if (protectedSessionService.isProtectedSessionAvailable()) {
 | 
				
			||||||
        noteIds = noteIds.concat(Object.keys(protectedNoteTitles));
 | 
					        noteIds = [...new Set(noteIds.concat(Object.keys(protectedNoteTitles)))];
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    for (const noteId of noteIds) {
 | 
					    for (const noteId of noteIds) {
 | 
				
			||||||
@@ -176,34 +174,6 @@ async function findNotes(query) {
 | 
				
			|||||||
    return apiResults;
 | 
					    return apiResults;
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
function isNotePathArchived(notePath) {
 | 
					 | 
				
			||||||
    // if the note is archived directly
 | 
					 | 
				
			||||||
    if (archived[notePath[notePath.length - 1]] !== undefined) {
 | 
					 | 
				
			||||||
        return true;
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    for (let i = 0; i < notePath.length - 1; i++) {
 | 
					 | 
				
			||||||
        // this is going through parents so archived must be inheritable
 | 
					 | 
				
			||||||
        if (archived[notePath[i]] === 1) {
 | 
					 | 
				
			||||||
            return true;
 | 
					 | 
				
			||||||
        }
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    return false;
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
/**
 | 
					 | 
				
			||||||
 * This assumes that note is available. "archived" note means that there isn't a single non-archived note-path
 | 
					 | 
				
			||||||
 * leading to this note.
 | 
					 | 
				
			||||||
 *
 | 
					 | 
				
			||||||
 * @param noteId
 | 
					 | 
				
			||||||
 */
 | 
					 | 
				
			||||||
function isArchived(noteId) {
 | 
					 | 
				
			||||||
    const notePath = getSomePath(noteId);
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    return isNotePathArchived(notePath);
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
function search(noteId, tokens, path, results) {
 | 
					function search(noteId, tokens, path, results) {
 | 
				
			||||||
    if (tokens.length === 0) {
 | 
					    if (tokens.length === 0) {
 | 
				
			||||||
        const retPath = getSomePath(noteId, path);
 | 
					        const retPath = getSomePath(noteId, path);
 | 
				
			||||||
@@ -254,6 +224,34 @@ function search(noteId, tokens, path, results) {
 | 
				
			|||||||
    }
 | 
					    }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					function isNotePathArchived(notePath) {
 | 
				
			||||||
 | 
					    // if the note is archived directly
 | 
				
			||||||
 | 
					    if (archived[notePath[notePath.length - 1]] !== undefined) {
 | 
				
			||||||
 | 
					        return true;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    for (let i = 0; i < notePath.length - 1; i++) {
 | 
				
			||||||
 | 
					        // this is going through parents so archived must be inheritable
 | 
				
			||||||
 | 
					        if (archived[notePath[i]] === 1) {
 | 
				
			||||||
 | 
					            return true;
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    return false;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/**
 | 
				
			||||||
 | 
					 * This assumes that note is available. "archived" note means that there isn't a single non-archived note-path
 | 
				
			||||||
 | 
					 * leading to this note.
 | 
				
			||||||
 | 
					 *
 | 
				
			||||||
 | 
					 * @param noteId
 | 
				
			||||||
 | 
					 */
 | 
				
			||||||
 | 
					function isArchived(noteId) {
 | 
				
			||||||
 | 
					    const notePath = getSomePath(noteId);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    return isNotePathArchived(notePath);
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
function getNoteTitle(noteId, parentNoteId) {
 | 
					function getNoteTitle(noteId, parentNoteId) {
 | 
				
			||||||
    const prefix = prefixes[noteId + '-' + parentNoteId];
 | 
					    const prefix = prefixes[noteId + '-' + parentNoteId];
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -456,7 +454,10 @@ eventService.subscribe([eventService.ENTITY_CHANGED, eventService.ENTITY_DELETED
 | 
				
			|||||||
                prefixes[branch.noteId + '-' + branch.parentNoteId] = branch.prefix;
 | 
					                prefixes[branch.noteId + '-' + branch.parentNoteId] = branch.prefix;
 | 
				
			||||||
            }
 | 
					            }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            childToParent[branch.noteId].push(branch.parentNoteId);
 | 
					            if (!childToParent[branch.noteId].includes(branch.parentNoteId)) {
 | 
				
			||||||
 | 
					                childToParent[branch.noteId].push(branch.parentNoteId);
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            resortChildToParent(branch.noteId);
 | 
					            resortChildToParent(branch.noteId);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            childParentToBranchId[branch.noteId + '-' + branch.parentNoteId] = branch.branchId;
 | 
					            childParentToBranchId[branch.noteId + '-' + branch.parentNoteId] = branch.branchId;
 | 
				
			||||||
@@ -483,7 +484,7 @@ eventService.subscribe([eventService.ENTITY_CHANGED, eventService.ENTITY_DELETED
 | 
				
			|||||||
// will sort the childs so that non-archived are first and archived at the end
 | 
					// will sort the childs so that non-archived are first and archived at the end
 | 
				
			||||||
// this is done so that non-archived paths are always explored as first when searching for note path
 | 
					// this is done so that non-archived paths are always explored as first when searching for note path
 | 
				
			||||||
function resortChildToParent(noteId) {
 | 
					function resortChildToParent(noteId) {
 | 
				
			||||||
    if (!childToParent[noteId]) {
 | 
					    if (!(noteId in childToParent)) {
 | 
				
			||||||
        return;
 | 
					        return;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -501,9 +502,7 @@ function isAvailable(noteId) {
 | 
				
			|||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
eventService.subscribe(eventService.ENTER_PROTECTED_SESSION, () => {
 | 
					eventService.subscribe(eventService.ENTER_PROTECTED_SESSION, () => {
 | 
				
			||||||
    if (loaded) {
 | 
					    loadedPromise.then(() => loadProtectedNotes());
 | 
				
			||||||
        loadProtectedNotes();
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
});
 | 
					});
 | 
				
			||||||
 | 
					
 | 
				
			||||||
sqlInit.dbReady.then(() => utils.stopWatch("Note cache load", load));
 | 
					sqlInit.dbReady.then(() => utils.stopWatch("Note cache load", load));
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user