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
optimized becca loader with raw queries
zadame003341dff4e
diff --git a/src/becca/becca_loader.js b/src/becca/becca_loader.jsindex 3a3e7b6c54..8432567230 100644--- a/src/becca/becca_loader.js+++ b/src/becca/becca_loader.js@@ -25,16 +25,19 @@ function load() {const start = Date.now();becca.reset();- for (const row of sql.iterateRows(`SELECT noteId, title, type, mime, isProtected, dateCreated, dateModified, utcDateCreated, utcDateModified FROM notes`, [])) {- new Note(row);+ // using raw query and passing arrays to avoid allocating new objects+ // this is worth it for becca load since it happens every run and blocks the app until finished++ for (const row of sql.getRawRows(`SELECT noteId, title, type, mime, isProtected, dateCreated, dateModified, utcDateCreated, utcDateModified FROM notes WHERE isDeleted = 0`, [])) {+ new Note().update(row).init();}- for (const row of sql.iterateRows(`SELECT branchId, noteId, parentNoteId, prefix, notePosition, isExpanded, utcDateModified FROM branches WHERE isDeleted = 0`, [])) {- new Branch(row);+ for (const row of sql.getRawRows(`SELECT branchId, noteId, parentNoteId, prefix, notePosition, isExpanded, utcDateModified FROM branches WHERE isDeleted = 0`, [])) {+ new Branch().update(row).init();}- for (const row of sql.iterateRows(`SELECT attributeId, noteId, type, name, value, isInheritable, position, utcDateModified FROM attributes WHERE isDeleted = 0`, [])) {- new Attribute(row);+ for (const row of sql.getRawRows(`SELECT attributeId, noteId, type, name, value, isInheritable, position, utcDateModified FROM attributes WHERE isDeleted = 0`, [])) {+ new Attribute().update(row).init();}for (const row of sql.getRows(`SELECT name, value, isSynced, utcDateModified FROM options`)) {diff --git a/src/becca/entities/attribute.js b/src/becca/entities/attribute.jsindex 18c0d0e34b..77ad4a1c4f 100644--- a/src/becca/entities/attribute.js+++ b/src/becca/entities/attribute.js@@ -14,23 +14,46 @@ class Attribute extends AbstractEntity {constructor(row) {super();+ if (!row) {+ return;+ }++ this.update([+ row.attributeId,+ row.noteId,+ row.type,+ row.name,+ row.value,+ row.isInheritable,+ row.position,+ row.utcDateModified+ ]);++ this.init();+ }++ update([attributeId, noteId, type, name, value, isInheritable, position, utcDateModified]) {/** @param {string} */- this.attributeId = row.attributeId;+ this.attributeId = attributeId;/** @param {string} */- this.noteId = row.noteId;+ this.noteId = noteId;/** @param {string} */- this.type = row.type;+ this.type = type;/** @param {string} */- this.name = row.name;+ this.name = name;/** @param {int} */- this.position = row.position;+ this.position = position;/** @param {string} */- this.value = row.value;+ this.value = value;/** @param {boolean} */- this.isInheritable = !!row.isInheritable;+ this.isInheritable = !!isInheritable;/** @param {string} */- this.utcDateModified = row.utcDateModified;+ this.utcDateModified = utcDateModified;++ return this;+ }+ init() {if (this.attributeId) {this.becca.attributes[this.attributeId] = this;}diff --git a/src/becca/entities/branch.js b/src/becca/entities/branch.jsindex 683c1dace0..8ddd6b23d9 100644--- a/src/becca/entities/branch.js+++ b/src/becca/entities/branch.js@@ -14,21 +14,43 @@ class Branch extends AbstractEntity {constructor(row) {super();+ if (!row) {+ return;+ }++ this.update([+ row.branchId,+ row.noteId,+ row.parentNoteId,+ row.prefix,+ row.notePosition,+ row.isExpanded,+ row.utcDateModified+ ]);++ this.init();+ }++ update([branchId, noteId, parentNoteId, prefix, notePosition, isExpanded, utcDateModified]) {/** @param {string} */- this.branchId = row.branchId;+ this.branchId = branchId;/** @param {string} */- this.noteId = row.noteId;+ this.noteId = noteId;/** @param {string} */- this.parentNoteId = row.parentNoteId;+ this.parentNoteId = parentNoteId;/** @param {string} */- this.prefix = row.prefix;+ this.prefix = prefix;/** @param {int} */- this.notePosition = row.notePosition;+ this.notePosition = notePosition;/** @param {boolean} */- this.isExpanded = !!row.isExpanded;+ this.isExpanded = !!isExpanded;/** @param {string} */- this.utcDateModified = row.utcDateModified;+ this.utcDateModified = utcDateModified;++ return this;+ }+ init() {if (this.branchId === 'root') {return;}diff --git a/src/becca/entities/note.js b/src/becca/entities/note.jsindex 011d4896d8..9fd0e2c2ba 100644--- a/src/becca/entities/note.js+++ b/src/becca/entities/note.js@@ -20,8 +20,61 @@ class Note extends AbstractEntity {constructor(row) {super();- this.update(row);+ if (!row) {+ return;+ }++ this.update([+ row.noteId,+ row.title,+ row.type,+ row.mime,+ row.isProtected,+ row.dateCreated,+ row.dateModified,+ row.utcDateCreated,+ row.utcDateModified+ ]);++ this.init();+ }++ update([noteId, title, type, mime, isProtected, dateCreated, dateModified, utcDateCreated, utcDateModified]) {+ // ------ Database persisted attributes ------+ /** @param {string} */+ this.noteId = noteId;+ /** @param {string} */+ this.title = title;+ /** @param {boolean} */+ this.isProtected = !!isProtected;+ /** @param {string} */+ this.type = type;+ /** @param {string} */+ this.mime = mime;+ /** @param {string} */+ this.dateCreated = dateCreated || dateUtils.localNowDateTime();+ /** @param {string} */+ this.dateModified = dateModified;+ /** @param {string} */+ this.utcDateCreated = utcDateCreated || dateUtils.utcNowDateTime();+ /** @param {string} */+ this.utcDateModified = utcDateModified;++ // ------ Derived attributes ------++ /** @param {boolean} */+ this.isDecrypted = !this.isProtected;++ this.decrypt();++ /** @param {string|null} */+ this.flatTextCache = null;++ return this;+ }++ init() {/** @param {Branch[]} */this.parentBranches = [];/** @param {Note[]} */@@ -54,39 +107,6 @@ class Note extends AbstractEntity {this.revisionCount = null;}- update(row) {- // ------ Database persisted attributes -------- /** @param {string} */- this.noteId = row.noteId;- /** @param {string} */- this.title = row.title;- /** @param {boolean} */- this.isProtected = !!row.isProtected;- /** @param {string} */- this.type = row.type;- /** @param {string} */- this.mime = row.mime;- /** @param {string} */- this.dateCreated = row.dateCreated || dateUtils.localNowDateTime();- /** @param {string} */- this.dateModified = row.dateModified;- /** @param {string} */- this.utcDateCreated = row.utcDateCreated || dateUtils.utcNowDateTime();- /** @param {string} */- this.utcDateModified = row.utcDateModified;-- // ------ Derived attributes -------- /** @param {boolean} */- this.isDecrypted = !this.isProtected;-- this.decrypt();-- /** @param {string|null} */- this.flatTextCache = null;- }-isContentAvailable() {return !this.noteId // new note which was not encrypted yet|| !this.isProtected