Build your personal knowledge base with Trilium Notes
TypeScript electronelectron-appknowledge-baseknowledge-graphknowledge-managementknowledge-management-graphlocal-firstnote-managmentnote-takernote-takingnotebooknotesnotes-apppersonal-knowledge-basepersonal-wikiscriptableself-hostedself-hostingwiki
note cache refactoring WIP
zadam60c204972917
diff --git a/src/services/note_cache/entities/attribute.js b/src/services/note_cache/entities/attribute.jsindex a276d47d6b..beb605fbf3 100644--- a/src/services/note_cache/entities/attribute.js+++ b/src/services/note_cache/entities/attribute.js@@ -1,3 +1,7 @@+"use strict";++const noteCache = require('../note_cache');+class Attribute {constructor(row) {/** @param {string} */@@ -13,11 +17,11 @@ class Attribute {/** @param {boolean} */this.isInheritable = !!row.isInheritable;- notes[this.noteId].ownedAttributes.push(this);+ noteCache.notes[this.noteId].ownedAttributes.push(this);const key = `${this.type-this.name}`;- attributeIndex[key] = attributeIndex[key] || [];- attributeIndex[key].push(this);+ noteCache.attributeIndex[key] = noteCache.attributeIndex[key] || [];+ noteCache.attributeIndex[key].push(this);const targetNote = this.targetNote;@@ -32,12 +36,14 @@ class Attribute {}get note() {- return notes[this.noteId];+ return noteCache.notes[this.noteId];}get targetNote() {if (this.type === 'relation') {- return notes[this.value];+ return noteCache.notes[this.value];}}}++module.exports = Attribute;diff --git a/src/services/note_cache/entities/branch.js b/src/services/note_cache/entities/branch.jsindex aaf075aa88..07d4221f68 100644--- a/src/services/note_cache/entities/branch.js+++ b/src/services/note_cache/entities/branch.js@@ -1,4 +1,8 @@-export default class Branch {+"use strict";++const noteCache = require('../note_cache');++class Branch {constructor(row) {/** @param {string} */this.branchId = row.branchId;@@ -13,7 +17,7 @@ export default class Branch {return;}- const childNote = notes[this.noteId];+ const childNote = noteCache.notes[this.noteId];const parentNote = this.parentNote;if (!childNote) {@@ -26,12 +30,12 @@ export default class Branch {parentNote.children.push(childNote);- childParentToBranch[`${this.noteId}-${this.parentNoteId}`] = this;+ noteCache.childParentToBranch[`${this.noteId}-${this.parentNoteId}`] = this;}/** @return {Note} */get parentNote() {- const note = notes[this.parentNoteId];+ const note = noteCache.notes[this.parentNoteId];if (!note) {console.log(`Cannot find note ${this.parentNoteId}`);@@ -40,3 +44,5 @@ export default class Branch {return note;}}++module.exports = Branch;diff --git a/src/services/note_cache/entities/note.js b/src/services/note_cache/entities/note.jsindex 7f5dfe924e..d86508e809 100644--- a/src/services/note_cache/entities/note.js+++ b/src/services/note_cache/entities/note.js@@ -1,4 +1,8 @@-export default class Note {+"use strict";++const noteCache = require('../note_cache');++class Note {constructor(row) {/** @param {string} */this.noteId = row.noteId;@@ -29,7 +33,7 @@ export default class Note {this.flatTextCache = null;if (protectedSessionService.isProtectedSessionAvailable()) {- decryptProtectedNote(this);+ noteCache.decryptProtectedNote(this);}}@@ -233,4 +237,14 @@ export default class Note {return arr;}++ decrypt() {+ if (this.isProtected && !this.isDecrypted && protectedSessionService.isProtectedSessionAvailable()) {+ this.title = protectedSessionService.decryptString(note.title);++ this.isDecrypted = true;+ }+ }}++module.exports = Note;diff --git a/src/services/note_cache/note_cache.js b/src/services/note_cache/note_cache.jsindex 0d60672353..974377dc57 100644--- a/src/services/note_cache/note_cache.js+++ b/src/services/note_cache/note_cache.js@@ -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 sqlInit = require('../sql_init.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 {constructor() {@@ -22,9 +21,7 @@ class NoteCache {this.attributeIndex = null;this.loaded = false;- this.loadedPromiseResolve;- /** Is resolved after the initial load */- this.loadedPromise = new Promise(res => this.loadedPromiseResolve = res);+ this.loadedPromise = this.load();}/** @return {Attribute[]} */@@ -33,6 +30,8 @@ class NoteCache {}async load() {+ await sqlInit.dbReady;+this.notes = await this.getMappedRows(`SELECT noteId, title, isProtected FROM notes WHERE isDeleted = 0`,row => new Note(row));@@ -45,7 +44,6 @@ class NoteCache {row => new Attribute(row));this.loaded = true;- this.loadedPromiseResolve();}async getMappedRows(query, cb) {@@ -61,19 +59,15 @@ class NoteCache {return map;}- decryptProtectedNote(note) {- if (note.isProtected && !note.isDecrypted && protectedSessionService.isProtectedSessionAvailable()) {- note.title = protectedSessionService.decryptString(note.title);-- note.isDecrypted = true;- }- }-decryptProtectedNotes() {for (const note of Object.values(this.notes)) {- decryptProtectedNote(note);+ note.decrypt();}}++ getBranch(childNoteId, parentNoteId) {+ return this.childParentToBranch[`${childNoteId}-${parentNoteId}`];+ }}const noteCache = new NoteCache();@@ -100,13 +94,13 @@ eventService.subscribe([eventService.ENTITY_CHANGED, eventService.ENTITY_DELETEDnote.isDecrypted = !entity.isProtected || !!entity.isContentAvailable;note.flatTextCache = null;- decryptProtectedNote(note);+ noteCache.decryptProtectedNote(note);}else {const note = new Note(entity);noteCache.notes[noteId] = note;- decryptProtectedNote(note);+ noteCache.decryptProtectedNote(note);}}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, () => {noteCache.loadedPromise.then(() => noteCache.decryptProtectedNotes());});-sqlInit.dbReady.then(() => utils.stopWatch("Note cache load", () => treeCache.load()));--module.exports = {- loadedPromise,- findNotesForAutocomplete,- getNotePath,- getNoteTitleForPath,- isAvailable,- isArchived,- isInAncestor,- load,- findSimilarNotes-};+module.exports = noteCache;diff --git a/src/services/note_cache/note_cache_service.js b/src/services/note_cache/note_cache_service.jsindex c5bebc3055..3b567c2a94 100644--- a/src/services/note_cache/note_cache_service.js+++ b/src/services/note_cache/note_cache_service.js@@ -1,3 +1,8 @@+"use strict";++const noteCache = require('./note_cache');+const hoistedNoteService = require('../hoisted_note');+function isNotePathArchived(notePath) {const noteId = notePath[notePath.length - 1];const note = noteCache.notes[noteId];diff --git a/src/services/note_cache/note_set.js b/src/services/note_cache/note_set.jsindex bd088162ac..da768854db 100644--- a/src/services/note_cache/note_set.js+++ b/src/services/note_cache/note_set.js@@ -1,4 +1,6 @@-export default class NoteSet {+"use strict";++class NoteSet {constructor(notes = []) {this.notes = notes;}@@ -20,3 +22,5 @@ export default class NoteSet {this.notes = this.notes.concat(anotherNoteSet.arr);}}++module.exports = NoteSet;diff --git a/src/services/note_cache/expressions/and.js b/src/services/search/expressions/and.jssimilarity index 84%rename from src/services/note_cache/expressions/and.jsrename to src/services/search/expressions/and.jsindex 2cdb97f8f2..f542d416cf 100644--- a/src/services/note_cache/expressions/and.js+++ b/src/services/search/expressions/and.js@@ -1,4 +1,6 @@-export default class AndExp {+"use strict";++class AndExp {constructor(subExpressions) {this.subExpressions = subExpressions;}@@ -11,3 +13,5 @@ export default class AndExp {return noteSet;}}++module.exports = AndExp;diff --git a/src/services/note_cache/expressions/equals.js b/src/services/search/expressions/equals.jssimilarity index 93%rename from src/services/note_cache/expressions/equals.jsrename to src/services/search/expressions/equals.jsindex 794fbc3f3c..ab6e69b60d 100644--- a/src/services/note_cache/expressions/equals.js+++ b/src/services/search/expressions/equals.js@@ -1,4 +1,6 @@-export default class EqualsExp {+"use strict";++class EqualsExp {constructor(attributeType, attributeName, attributeValue) {this.attributeType = attributeType;this.attributeName = attributeName;@@ -26,3 +28,5 @@ export default class EqualsExp {}}}++module.exports = EqualsExp;diff --git a/src/services/note_cache/expressions/exists.js b/src/services/search/expressions/exists.jssimilarity index 92%rename from src/services/note_cache/expressions/exists.jsrename to src/services/search/expressions/exists.jsindex f79f17af7f..781fd06047 100644--- a/src/services/note_cache/expressions/exists.js+++ b/src/services/search/expressions/exists.js@@ -1,4 +1,6 @@-export default class ExistsExp {+"use strict";++class ExistsExp {constructor(attributeType, attributeName) {this.attributeType = attributeType;this.attributeName = attributeName;@@ -25,3 +27,5 @@ export default class ExistsExp {}}}++module.exports = ExistsExp;diff --git a/src/services/note_cache/expressions/note_cache_fulltext.js b/src/services/search/expressions/note_cache_fulltext.jssimilarity index 97%rename from src/services/note_cache/expressions/note_cache_fulltext.jsrename to src/services/search/expressions/note_cache_fulltext.jsindex 5451d7255f..3d3e1bcff3 100644--- a/src/services/note_cache/expressions/note_cache_fulltext.js+++ b/src/services/search/expressions/note_cache_fulltext.js@@ -1,4 +1,6 @@-export default class NoteCacheFulltextExp {+"use strict";++class NoteCacheFulltextExp {constructor(tokens) {this.tokens = tokens;}@@ -123,3 +125,5 @@ export default class NoteCacheFulltextExp {}}}++module.exports = NoteCacheFulltextExp;diff --git a/src/services/note_cache/expressions/note_content_fulltext.js b/src/services/search/expressions/note_content_fulltext.jssimilarity index 89%rename from src/services/note_cache/expressions/note_content_fulltext.jsrename to src/services/search/expressions/note_content_fulltext.jsindex 9a8db4a408..a7a5a4f7af 100644--- a/src/services/note_cache/expressions/note_content_fulltext.js+++ b/src/services/search/expressions/note_content_fulltext.js@@ -1,4 +1,6 @@-export default class NoteContentFulltextExp {+"use strict";++class NoteContentFulltextExp {constructor(tokens) {this.tokens = tokens;}@@ -24,3 +26,5 @@ export default class NoteContentFulltextExp {return results;}}++module.exports = NoteContentFulltextExp;diff --git a/src/services/note_cache/expressions/or.js b/src/services/search/expressions/or.jssimilarity index 86%rename from src/services/note_cache/expressions/or.jsrename to src/services/search/expressions/or.jsindex f5ba189c04..c17dd2210c 100644--- a/src/services/note_cache/expressions/or.js+++ b/src/services/search/expressions/or.js@@ -1,4 +1,6 @@-export default class OrExp {+"use strict";++class OrExp {constructor(subExpressions) {this.subExpressions = subExpressions;}@@ -13,3 +15,5 @@ export default class OrExp {return resultNoteSet;}}++module.exports = OrExp;diff --git a/src/services/note_cache/search.js b/src/services/search/search.jssimilarity index 97%rename from src/services/note_cache/search.jsrename to src/services/search/search.jsindex 993877f144..09f6f00319 100644--- a/src/services/note_cache/search.js+++ b/src/services/search/search.js@@ -1,3 +1,7 @@+"use strict";++import NoteCacheFulltextExp from "./expressions/note_cache_fulltext.js";+async function findNotesWithExpression(expression) {const hoistedNote = notes[hoistedNoteService.getHoistedNoteId()];diff --git a/src/services/note_cache/search_result.js b/src/services/search/search_result.jssimilarity index 53%rename from src/services/note_cache/search_result.jsrename to src/services/search/search_result.jsindex 3a92f4fbe9..83c395b102 100644--- a/src/services/note_cache/search_result.js+++ b/src/services/search/search_result.js@@ -1,7 +1,11 @@-export default class SearchResult {+"use strict";++const noteCacheService = require('../note_cache/note_cache_service');++class SearchResult {constructor(notePathArray) {this.notePathArray = notePathArray;- this.notePathTitle = getNoteTitleForPath(notePathArray);+ this.notePathTitle = noteCacheService.getNoteTitleForPath(notePathArray);}get notePath() {@@ -12,3 +16,5 @@ export default class SearchResult {return this.notePathArray[this.notePathArray.length - 1];}}++module.exports = SearchResult;