mirror of
				https://github.com/zadam/trilium.git
				synced 2025-10-31 03:29:02 +01:00 
			
		
		
		
	note cache refactoring WIP
This commit is contained in:
		
							parent
							
								
									dcd371b5b1
								
							
						
					
					
						commit
						60c2049729
					
				| @ -1,3 +1,7 @@ | |||||||
|  | "use strict"; | ||||||
|  | 
 | ||||||
|  | const noteCache = require('../note_cache'); | ||||||
|  | 
 | ||||||
| class Attribute { | class Attribute { | ||||||
|     constructor(row) { |     constructor(row) { | ||||||
|         /** @param {string} */ |         /** @param {string} */ | ||||||
| @ -13,11 +17,11 @@ class Attribute { | |||||||
|         /** @param {boolean} */ |         /** @param {boolean} */ | ||||||
|         this.isInheritable = !!row.isInheritable; |         this.isInheritable = !!row.isInheritable; | ||||||
| 
 | 
 | ||||||
|         notes[this.noteId].ownedAttributes.push(this); |         noteCache.notes[this.noteId].ownedAttributes.push(this); | ||||||
| 
 | 
 | ||||||
|         const key = `${this.type-this.name}`; |         const key = `${this.type-this.name}`; | ||||||
|         attributeIndex[key] = attributeIndex[key] || []; |         noteCache.attributeIndex[key] = noteCache.attributeIndex[key] || []; | ||||||
|         attributeIndex[key].push(this); |         noteCache.attributeIndex[key].push(this); | ||||||
| 
 | 
 | ||||||
|         const targetNote = this.targetNote; |         const targetNote = this.targetNote; | ||||||
| 
 | 
 | ||||||
| @ -32,12 +36,14 @@ class Attribute { | |||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     get note() { |     get note() { | ||||||
|         return notes[this.noteId]; |         return noteCache.notes[this.noteId]; | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     get targetNote() { |     get targetNote() { | ||||||
|         if (this.type === 'relation') { |         if (this.type === 'relation') { | ||||||
|             return notes[this.value]; |             return noteCache.notes[this.value]; | ||||||
|         } |         } | ||||||
|     } |     } | ||||||
| } | } | ||||||
|  | 
 | ||||||
|  | module.exports = Attribute; | ||||||
|  | |||||||
| @ -1,4 +1,8 @@ | |||||||
| export default class Branch { | "use strict"; | ||||||
|  | 
 | ||||||
|  | const noteCache = require('../note_cache'); | ||||||
|  | 
 | ||||||
|  | class Branch { | ||||||
|     constructor(row) { |     constructor(row) { | ||||||
|         /** @param {string} */ |         /** @param {string} */ | ||||||
|         this.branchId = row.branchId; |         this.branchId = row.branchId; | ||||||
| @ -13,7 +17,7 @@ export default class Branch { | |||||||
|             return; |             return; | ||||||
|         } |         } | ||||||
| 
 | 
 | ||||||
|         const childNote = notes[this.noteId]; |         const childNote = noteCache.notes[this.noteId]; | ||||||
|         const parentNote = this.parentNote; |         const parentNote = this.parentNote; | ||||||
| 
 | 
 | ||||||
|         if (!childNote) { |         if (!childNote) { | ||||||
| @ -26,12 +30,12 @@ export default class Branch { | |||||||
| 
 | 
 | ||||||
|         parentNote.children.push(childNote); |         parentNote.children.push(childNote); | ||||||
| 
 | 
 | ||||||
|         childParentToBranch[`${this.noteId}-${this.parentNoteId}`] = this; |         noteCache.childParentToBranch[`${this.noteId}-${this.parentNoteId}`] = this; | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     /** @return {Note} */ |     /** @return {Note} */ | ||||||
|     get parentNote() { |     get parentNote() { | ||||||
|         const note = notes[this.parentNoteId]; |         const note = noteCache.notes[this.parentNoteId]; | ||||||
| 
 | 
 | ||||||
|         if (!note) { |         if (!note) { | ||||||
|             console.log(`Cannot find note ${this.parentNoteId}`); |             console.log(`Cannot find note ${this.parentNoteId}`); | ||||||
| @ -40,3 +44,5 @@ export default class Branch { | |||||||
|         return note; |         return note; | ||||||
|     } |     } | ||||||
| } | } | ||||||
|  | 
 | ||||||
|  | module.exports = Branch; | ||||||
|  | |||||||
| @ -1,4 +1,8 @@ | |||||||
| export default class Note { | "use strict"; | ||||||
|  | 
 | ||||||
|  | const noteCache = require('../note_cache'); | ||||||
|  | 
 | ||||||
|  | class Note { | ||||||
|     constructor(row) { |     constructor(row) { | ||||||
|         /** @param {string} */ |         /** @param {string} */ | ||||||
|         this.noteId = row.noteId; |         this.noteId = row.noteId; | ||||||
| @ -29,7 +33,7 @@ export default class Note { | |||||||
|         this.flatTextCache = null; |         this.flatTextCache = null; | ||||||
| 
 | 
 | ||||||
|         if (protectedSessionService.isProtectedSessionAvailable()) { |         if (protectedSessionService.isProtectedSessionAvailable()) { | ||||||
|             decryptProtectedNote(this); |             noteCache.decryptProtectedNote(this); | ||||||
|         } |         } | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
| @ -233,4 +237,14 @@ export default class Note { | |||||||
| 
 | 
 | ||||||
|         return arr; |         return arr; | ||||||
|     } |     } | ||||||
|  | 
 | ||||||
|  |     decrypt() { | ||||||
|  |         if (this.isProtected && !this.isDecrypted && protectedSessionService.isProtectedSessionAvailable()) { | ||||||
|  |             this.title = protectedSessionService.decryptString(note.title); | ||||||
|  | 
 | ||||||
|  |             this.isDecrypted = true; | ||||||
|  |         } | ||||||
|  |     } | ||||||
| } | } | ||||||
|  | 
 | ||||||
|  | module.exports = Note; | ||||||
|  | |||||||
| @ -1,12 +1,11 @@ | |||||||
| import treeCache from "../../public/app/services/tree_cache.js"; | "use strict"; | ||||||
| 
 | 
 | ||||||
|  | const Note = require('./entities/note'); | ||||||
|  | const Branch = require('./entities/branch'); | ||||||
|  | const Attribute = require('./entities/attribute'); | ||||||
| const sql = require('../sql.js'); | const sql = require('../sql.js'); | ||||||
| const sqlInit = require('../sql_init.js'); | const sqlInit = require('../sql_init.js'); | ||||||
| const eventService = require('../events.js'); | const eventService = require('../events.js'); | ||||||
| const protectedSessionService = require('../protected_session.js'); |  | ||||||
| const utils = require('../utils.js'); |  | ||||||
| const hoistedNoteService = require('../hoisted_note.js'); |  | ||||||
| const stringSimilarity = require('string-similarity'); |  | ||||||
| 
 | 
 | ||||||
| class NoteCache { | class NoteCache { | ||||||
|     constructor() { |     constructor() { | ||||||
| @ -22,9 +21,7 @@ class NoteCache { | |||||||
|         this.attributeIndex = null; |         this.attributeIndex = null; | ||||||
| 
 | 
 | ||||||
|         this.loaded = false; |         this.loaded = false; | ||||||
|         this.loadedPromiseResolve; |         this.loadedPromise = this.load(); | ||||||
|         /** Is resolved after the initial load */ |  | ||||||
|         this.loadedPromise = new Promise(res => this.loadedPromiseResolve = res); |  | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     /** @return {Attribute[]} */ |     /** @return {Attribute[]} */ | ||||||
| @ -33,6 +30,8 @@ class NoteCache { | |||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     async load() { |     async load() { | ||||||
|  |         await sqlInit.dbReady; | ||||||
|  | 
 | ||||||
|         this.notes = await this.getMappedRows(`SELECT noteId, title, isProtected FROM notes WHERE isDeleted = 0`, |         this.notes = await this.getMappedRows(`SELECT noteId, title, isProtected FROM notes WHERE isDeleted = 0`, | ||||||
|             row => new Note(row)); |             row => new Note(row)); | ||||||
| 
 | 
 | ||||||
| @ -45,7 +44,6 @@ class NoteCache { | |||||||
|             row => new Attribute(row)); |             row => new Attribute(row)); | ||||||
| 
 | 
 | ||||||
|         this.loaded = true; |         this.loaded = true; | ||||||
|         this.loadedPromiseResolve(); |  | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     async getMappedRows(query, cb) { |     async getMappedRows(query, cb) { | ||||||
| @ -61,18 +59,14 @@ class NoteCache { | |||||||
|         return map; |         return map; | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     decryptProtectedNote(note) { |     decryptProtectedNotes() { | ||||||
|         if (note.isProtected && !note.isDecrypted && protectedSessionService.isProtectedSessionAvailable()) { |         for (const note of Object.values(this.notes)) { | ||||||
|             note.title = protectedSessionService.decryptString(note.title); |             note.decrypt(); | ||||||
| 
 |  | ||||||
|             note.isDecrypted = true; |  | ||||||
|         } |         } | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     decryptProtectedNotes() { |     getBranch(childNoteId, parentNoteId) { | ||||||
|         for (const note of Object.values(this.notes)) { |         return this.childParentToBranch[`${childNoteId}-${parentNoteId}`]; | ||||||
|             decryptProtectedNote(note); |  | ||||||
|         } |  | ||||||
|     } |     } | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| @ -100,13 +94,13 @@ eventService.subscribe([eventService.ENTITY_CHANGED, eventService.ENTITY_DELETED | |||||||
|             note.isDecrypted = !entity.isProtected || !!entity.isContentAvailable; |             note.isDecrypted = !entity.isProtected || !!entity.isContentAvailable; | ||||||
|             note.flatTextCache = null; |             note.flatTextCache = null; | ||||||
| 
 | 
 | ||||||
|             decryptProtectedNote(note); |             noteCache.decryptProtectedNote(note); | ||||||
|         } |         } | ||||||
|         else { |         else { | ||||||
|             const note = new Note(entity); |             const note = new Note(entity); | ||||||
|             noteCache.notes[noteId] = note; |             noteCache.notes[noteId] = note; | ||||||
| 
 | 
 | ||||||
|             decryptProtectedNote(note); |             noteCache.decryptProtectedNote(note); | ||||||
|         } |         } | ||||||
|     } |     } | ||||||
|     else if (entityName === 'branches') { |     else if (entityName === 'branches') { | ||||||
| @ -201,24 +195,8 @@ eventService.subscribe([eventService.ENTITY_CHANGED, eventService.ENTITY_DELETED | |||||||
|     } |     } | ||||||
| }); | }); | ||||||
| 
 | 
 | ||||||
| function getBranch(childNoteId, parentNoteId) { |  | ||||||
|     return noteCache.childParentToBranch[`${childNoteId}-${parentNoteId}`]; |  | ||||||
| } |  | ||||||
| 
 |  | ||||||
| eventService.subscribe(eventService.ENTER_PROTECTED_SESSION, () => { | eventService.subscribe(eventService.ENTER_PROTECTED_SESSION, () => { | ||||||
|     noteCache.loadedPromise.then(() => noteCache.decryptProtectedNotes()); |     noteCache.loadedPromise.then(() => noteCache.decryptProtectedNotes()); | ||||||
| }); | }); | ||||||
| 
 | 
 | ||||||
| sqlInit.dbReady.then(() => utils.stopWatch("Note cache load", () => treeCache.load())); | module.exports = noteCache; | ||||||
| 
 |  | ||||||
| module.exports = { |  | ||||||
|     loadedPromise, |  | ||||||
|     findNotesForAutocomplete, |  | ||||||
|     getNotePath, |  | ||||||
|     getNoteTitleForPath, |  | ||||||
|     isAvailable, |  | ||||||
|     isArchived, |  | ||||||
|     isInAncestor, |  | ||||||
|     load, |  | ||||||
|     findSimilarNotes |  | ||||||
| }; |  | ||||||
|  | |||||||
| @ -1,3 +1,8 @@ | |||||||
|  | "use strict"; | ||||||
|  | 
 | ||||||
|  | const noteCache = require('./note_cache'); | ||||||
|  | const hoistedNoteService = require('../hoisted_note'); | ||||||
|  | 
 | ||||||
| function isNotePathArchived(notePath) { | function isNotePathArchived(notePath) { | ||||||
|     const noteId = notePath[notePath.length - 1]; |     const noteId = notePath[notePath.length - 1]; | ||||||
|     const note = noteCache.notes[noteId]; |     const note = noteCache.notes[noteId]; | ||||||
|  | |||||||
| @ -1,4 +1,6 @@ | |||||||
| export default class NoteSet { | "use strict"; | ||||||
|  | 
 | ||||||
|  | class NoteSet { | ||||||
|     constructor(notes = []) { |     constructor(notes = []) { | ||||||
|         this.notes = notes; |         this.notes = notes; | ||||||
|     } |     } | ||||||
| @ -20,3 +22,5 @@ export default class NoteSet { | |||||||
|         this.notes = this.notes.concat(anotherNoteSet.arr); |         this.notes = this.notes.concat(anotherNoteSet.arr); | ||||||
|     } |     } | ||||||
| } | } | ||||||
|  | 
 | ||||||
|  | module.exports = NoteSet; | ||||||
|  | |||||||
| @ -1,4 +1,6 @@ | |||||||
| export default class AndExp { | "use strict"; | ||||||
|  | 
 | ||||||
|  | class AndExp { | ||||||
|     constructor(subExpressions) { |     constructor(subExpressions) { | ||||||
|         this.subExpressions = subExpressions; |         this.subExpressions = subExpressions; | ||||||
|     } |     } | ||||||
| @ -11,3 +13,5 @@ export default class AndExp { | |||||||
|         return noteSet; |         return noteSet; | ||||||
|     } |     } | ||||||
| } | } | ||||||
|  | 
 | ||||||
|  | module.exports = AndExp; | ||||||
| @ -1,4 +1,6 @@ | |||||||
| export default class EqualsExp { | "use strict"; | ||||||
|  | 
 | ||||||
|  | class EqualsExp { | ||||||
|     constructor(attributeType, attributeName, attributeValue) { |     constructor(attributeType, attributeName, attributeValue) { | ||||||
|         this.attributeType = attributeType; |         this.attributeType = attributeType; | ||||||
|         this.attributeName = attributeName; |         this.attributeName = attributeName; | ||||||
| @ -26,3 +28,5 @@ export default class EqualsExp { | |||||||
|         } |         } | ||||||
|     } |     } | ||||||
| } | } | ||||||
|  | 
 | ||||||
|  | module.exports = EqualsExp; | ||||||
| @ -1,4 +1,6 @@ | |||||||
| export default class ExistsExp { | "use strict"; | ||||||
|  | 
 | ||||||
|  | class ExistsExp { | ||||||
|     constructor(attributeType, attributeName) { |     constructor(attributeType, attributeName) { | ||||||
|         this.attributeType = attributeType; |         this.attributeType = attributeType; | ||||||
|         this.attributeName = attributeName; |         this.attributeName = attributeName; | ||||||
| @ -25,3 +27,5 @@ export default class ExistsExp { | |||||||
|         } |         } | ||||||
|     } |     } | ||||||
| } | } | ||||||
|  | 
 | ||||||
|  | module.exports = ExistsExp; | ||||||
| @ -1,4 +1,6 @@ | |||||||
| export default class NoteCacheFulltextExp { | "use strict"; | ||||||
|  | 
 | ||||||
|  | class NoteCacheFulltextExp { | ||||||
|     constructor(tokens) { |     constructor(tokens) { | ||||||
|         this.tokens = tokens; |         this.tokens = tokens; | ||||||
|     } |     } | ||||||
| @ -123,3 +125,5 @@ export default class NoteCacheFulltextExp { | |||||||
|         } |         } | ||||||
|     } |     } | ||||||
| } | } | ||||||
|  | 
 | ||||||
|  | module.exports = NoteCacheFulltextExp; | ||||||
| @ -1,4 +1,6 @@ | |||||||
| export default class NoteContentFulltextExp { | "use strict"; | ||||||
|  | 
 | ||||||
|  | class NoteContentFulltextExp { | ||||||
|     constructor(tokens) { |     constructor(tokens) { | ||||||
|         this.tokens = tokens; |         this.tokens = tokens; | ||||||
|     } |     } | ||||||
| @ -24,3 +26,5 @@ export default class NoteContentFulltextExp { | |||||||
|         return results; |         return results; | ||||||
|     } |     } | ||||||
| } | } | ||||||
|  | 
 | ||||||
|  | module.exports = NoteContentFulltextExp; | ||||||
| @ -1,4 +1,6 @@ | |||||||
| export default class OrExp { | "use strict"; | ||||||
|  | 
 | ||||||
|  | class OrExp { | ||||||
|     constructor(subExpressions) { |     constructor(subExpressions) { | ||||||
|         this.subExpressions = subExpressions; |         this.subExpressions = subExpressions; | ||||||
|     } |     } | ||||||
| @ -13,3 +15,5 @@ export default class OrExp { | |||||||
|         return resultNoteSet; |         return resultNoteSet; | ||||||
|     } |     } | ||||||
| } | } | ||||||
|  | 
 | ||||||
|  | module.exports = OrExp; | ||||||
| @ -1,3 +1,7 @@ | |||||||
|  | "use strict"; | ||||||
|  | 
 | ||||||
|  | import NoteCacheFulltextExp from "./expressions/note_cache_fulltext.js"; | ||||||
|  | 
 | ||||||
| async function findNotesWithExpression(expression) { | async function findNotesWithExpression(expression) { | ||||||
| 
 | 
 | ||||||
|     const hoistedNote = notes[hoistedNoteService.getHoistedNoteId()]; |     const hoistedNote = notes[hoistedNoteService.getHoistedNoteId()]; | ||||||
| @ -1,7 +1,11 @@ | |||||||
| export default class SearchResult { | "use strict"; | ||||||
|  | 
 | ||||||
|  | const noteCacheService = require('../note_cache/note_cache_service'); | ||||||
|  | 
 | ||||||
|  | class SearchResult { | ||||||
|     constructor(notePathArray) { |     constructor(notePathArray) { | ||||||
|         this.notePathArray = notePathArray; |         this.notePathArray = notePathArray; | ||||||
|         this.notePathTitle = getNoteTitleForPath(notePathArray); |         this.notePathTitle = noteCacheService.getNoteTitleForPath(notePathArray); | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     get notePath() { |     get notePath() { | ||||||
| @ -12,3 +16,5 @@ export default class SearchResult { | |||||||
|         return this.notePathArray[this.notePathArray.length - 1]; |         return this.notePathArray[this.notePathArray.length - 1]; | ||||||
|     } |     } | ||||||
| } | } | ||||||
|  | 
 | ||||||
|  | module.exports = SearchResult; | ||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user
	 zadam
						zadam