Notes/src/becca/becca_loader.ts

293 lines
9.1 KiB
TypeScript
Raw Normal View History

2020-05-20 00:03:33 +02:00
"use strict";
2024-02-17 20:45:31 +02:00
import sql = require('../services/sql');
import eventService = require('../services/events');
import becca = require('./becca');
import sqlInit = require('../services/sql_init');
import log = require('../services/log');
import BNote = require('./entities/bnote');
import BBranch = require('./entities/bbranch');
import BAttribute = require('./entities/battribute');
import BOption = require('./entities/boption');
import BEtapiToken = require('./entities/betapi_token');
import cls = require('../services/cls');
import entityConstructor = require('../becca/entity_constructor');
import { AttributeRow, BranchRow, EtapiTokenRow, NoteRow, OptionRow } from './entities/rows';
const beccaLoaded = new Promise<void>((res, rej) => {
2021-05-01 21:52:22 +02:00
sqlInit.dbReady.then(() => {
2023-10-20 09:36:57 +02:00
cls.init(() => {
load();
2021-05-01 21:52:22 +02:00
2024-03-30 10:49:40 +02:00
require('../services/options_init.js').initStartupOptions();
2021-05-01 21:52:22 +02:00
2023-10-20 09:36:57 +02:00
res();
});
2021-05-01 21:52:22 +02:00
});
2020-06-20 21:42:41 +02:00
});
2020-06-20 12:31:38 +02:00
function load() {
const start = Date.now();
2021-04-16 23:00:08 +02:00
becca.reset();
2020-05-20 00:03:33 +02:00
2023-10-20 09:36:57 +02:00
// we know this is slow and the total becca load time is logged
sql.disableSlowQueryLogging(() => {
// using a raw query and passing arrays to avoid allocating new objects,
// this is worth it for the becca load since it happens every run and blocks the app until finished
2023-10-20 09:36:57 +02:00
for (const row of sql.getRawRows(`SELECT noteId, title, type, mime, isProtected, blobId, dateCreated, dateModified, utcDateCreated, utcDateModified FROM notes WHERE isDeleted = 0`)) {
new BNote().update(row).init();
}
2020-05-20 00:03:33 +02:00
2024-02-17 23:32:32 +02:00
const branchRows = sql.getRawRows<BranchRow>(`SELECT branchId, noteId, parentNoteId, prefix, notePosition, isExpanded, utcDateModified FROM branches WHERE isDeleted = 0`);
2023-10-20 09:36:57 +02:00
// in-memory sort is faster than in the DB
2024-02-17 23:32:32 +02:00
branchRows.sort((a, b) => (a.notePosition || 0) - (b.notePosition || 0));
2023-10-20 09:36:57 +02:00
for (const row of branchRows) {
new BBranch().update(row).init();
}
2020-05-20 00:03:33 +02:00
2024-02-17 20:45:31 +02:00
for (const row of sql.getRawRows<AttributeRow>(`SELECT attributeId, noteId, type, name, value, isInheritable, position, utcDateModified FROM attributes WHERE isDeleted = 0`)) {
2023-10-20 09:36:57 +02:00
new BAttribute().update(row).init();
}
2021-04-30 23:10:25 +02:00
2024-02-17 20:45:31 +02:00
for (const row of sql.getRows<OptionRow>(`SELECT name, value, isSynced, utcDateModified FROM options`)) {
2023-10-20 09:36:57 +02:00
new BOption(row);
}
2020-05-20 00:03:33 +02:00
2024-02-17 20:45:31 +02:00
for (const row of sql.getRows<EtapiTokenRow>(`SELECT etapiTokenId, name, tokenHash, utcDateCreated, utcDateModified FROM etapi_tokens WHERE isDeleted = 0`)) {
2023-10-20 09:36:57 +02:00
new BEtapiToken(row);
}
});
2022-01-10 17:09:20 +01:00
for (const noteId in becca.notes) {
becca.notes[noteId].sortParents();
}
2021-04-16 23:00:08 +02:00
becca.loaded = true;
2021-04-16 23:00:08 +02:00
log.info(`Becca (note cache) load took ${Date.now() - start}ms`);
2020-05-20 00:03:33 +02:00
}
2024-02-17 20:45:31 +02:00
function reload(reason: string) {
load();
require('../services/ws').reloadFrontend(reason || "becca reloaded");
}
2024-03-30 10:49:40 +02:00
eventService.subscribeBeccaLoader([eventService.ENTITY_CHANGE_SYNCED], ({ entityName, entityRow }) => {
2021-08-07 21:21:30 +02:00
if (!becca.loaded) {
return;
}
if (["notes", "branches", "attributes", "etapi_tokens", "options"].includes(entityName)) {
2021-08-07 21:21:30 +02:00
const EntityClass = entityConstructor.getEntityFromEntityName(entityName);
const primaryKeyName = EntityClass.primaryKeyName;
2021-08-26 22:10:59 +02:00
let beccaEntity = becca.getEntity(entityName, entityRow[primaryKeyName]);
2021-08-07 21:21:30 +02:00
2021-08-14 10:13:59 +02:00
if (beccaEntity) {
2021-08-26 22:10:59 +02:00
beccaEntity.updateFromRow(entityRow);
2021-08-07 21:21:30 +02:00
} else {
2021-08-14 10:13:59 +02:00
beccaEntity = new EntityClass();
2021-08-26 22:10:59 +02:00
beccaEntity.updateFromRow(entityRow);
2021-08-14 10:13:59 +02:00
beccaEntity.init();
2021-08-07 21:21:30 +02:00
}
}
2021-08-26 22:10:59 +02:00
postProcessEntityUpdate(entityName, entityRow);
2021-08-07 21:21:30 +02:00
});
2024-03-30 10:49:40 +02:00
eventService.subscribeBeccaLoader(eventService.ENTITY_CHANGED, ({ entityName, entity }) => {
2021-08-07 21:21:30 +02:00
if (!becca.loaded) {
return;
}
postProcessEntityUpdate(entityName, entity);
2021-05-01 11:38:20 +02:00
});
2020-05-20 00:03:33 +02:00
/**
* This gets run on entity being created or updated.
*
* @param entityName
* @param entityRow - can be a becca entity (change comes from this trilium instance) or just a row (from sync).
2023-06-30 11:18:34 +02:00
* It should be therefore treated as a row.
*/
2024-02-17 20:45:31 +02:00
function postProcessEntityUpdate(entityName: string, entityRow: any) {
if (entityName === 'notes') {
noteUpdated(entityRow);
} else if (entityName === 'branches') {
branchUpdated(entityRow);
} else if (entityName === 'attributes') {
attributeUpdated(entityRow);
} else if (entityName === 'note_reordering') {
noteReorderingUpdated(entityRow);
}
}
2024-03-30 10:49:40 +02:00
eventService.subscribeBeccaLoader([eventService.ENTITY_DELETED, eventService.ENTITY_DELETE_SYNCED], ({ entityName, entityId }) => {
2021-05-01 11:38:20 +02:00
if (!becca.loaded) {
return;
2020-05-20 00:03:33 +02:00
}
2021-05-01 11:38:20 +02:00
if (entityName === 'notes') {
noteDeleted(entityId);
2021-05-01 11:38:20 +02:00
} else if (entityName === 'branches') {
branchDeleted(entityId);
2021-05-01 11:38:20 +02:00
} else if (entityName === 'attributes') {
attributeDeleted(entityId);
2022-01-10 17:09:20 +01:00
} else if (entityName === 'etapi_tokens') {
etapiTokenDeleted(entityId);
2021-05-01 11:38:20 +02:00
}
});
2024-02-17 20:45:31 +02:00
function noteDeleted(noteId: string) {
delete becca.notes[noteId];
becca.dirtyNoteSetCache();
2021-04-30 23:10:25 +02:00
}
2020-05-20 00:03:33 +02:00
2024-02-17 20:45:31 +02:00
function branchDeleted(branchId: string) {
const branch = becca.branches[branchId];
if (!branch) {
return;
}
2021-05-08 23:31:20 +02:00
const childNote = becca.notes[branch.noteId];
2020-05-20 00:03:33 +02:00
2021-05-01 11:38:20 +02:00
if (childNote) {
childNote.parents = childNote.parents.filter(parent => parent.noteId !== branch.parentNoteId);
childNote.parentBranches = childNote.parentBranches
.filter(parentBranch => parentBranch.branchId !== branch.branchId);
2020-05-20 00:03:33 +02:00
2021-05-01 11:38:20 +02:00
if (childNote.parents.length > 0) {
// subtree notes might lose some inherited attributes
2021-05-01 11:38:20 +02:00
childNote.invalidateSubTree();
2021-04-30 23:10:25 +02:00
}
2021-05-01 11:38:20 +02:00
}
2021-04-30 23:10:25 +02:00
2021-05-01 11:38:20 +02:00
const parentNote = becca.notes[branch.parentNoteId];
2020-05-20 00:03:33 +02:00
2021-05-01 11:38:20 +02:00
if (parentNote) {
parentNote.children = parentNote.children.filter(child => child.noteId !== branch.noteId);
}
2020-05-20 00:03:33 +02:00
2021-05-01 11:38:20 +02:00
delete becca.childParentToBranch[`${branch.noteId}-${branch.parentNoteId}`];
2024-02-17 20:45:31 +02:00
if (branch.branchId) {
delete becca.branches[branch.branchId];
}
2021-05-01 11:38:20 +02:00
}
2021-04-30 23:10:25 +02:00
2024-02-17 20:45:31 +02:00
function noteUpdated(entityRow: NoteRow) {
2023-06-01 00:07:57 +02:00
const note = becca.notes[entityRow.noteId];
if (note) {
2024-02-18 20:41:30 +02:00
// TODO, this wouldn't have worked in the original implementation since the variable was named __flatTextCache.
// type / mime could have been changed, and they are present in flatTextCache
2024-02-17 20:45:31 +02:00
note.__flatTextCache = null;
}
}
2024-02-17 20:45:31 +02:00
function branchUpdated(branchRow: BranchRow) {
2023-06-01 00:07:57 +02:00
const childNote = becca.notes[branchRow.noteId];
2020-05-20 00:03:33 +02:00
2021-05-01 11:38:20 +02:00
if (childNote) {
2024-02-17 20:45:31 +02:00
childNote.__flatTextCache = null;
childNote.sortParents();
// notes in the subtree can get new inherited attributes
2023-06-30 11:18:34 +02:00
// this is in theory needed upon branch creation, but there's no "create" event for sync changes
childNote.invalidateSubTree();
2020-05-20 00:03:33 +02:00
}
2023-06-01 00:07:57 +02:00
const parentNote = becca.notes[branchRow.parentNoteId];
if (parentNote) {
parentNote.sortChildren();
}
2021-04-30 23:10:25 +02:00
}
2024-02-17 20:45:31 +02:00
function attributeDeleted(attributeId: string) {
const attribute = becca.attributes[attributeId];
if (!attribute) {
return;
}
2021-05-01 11:38:20 +02:00
const note = becca.notes[attribute.noteId];
2020-05-20 00:03:33 +02:00
2021-05-01 11:38:20 +02:00
if (note) {
// first invalidate and only then remove the attribute (otherwise invalidation wouldn't be complete)
2023-01-06 20:31:55 +01:00
if (attribute.isAffectingSubtree || note.isInherited()) {
2021-05-01 11:38:20 +02:00
note.invalidateSubTree();
} else {
note.invalidateThisCache();
2021-04-30 23:10:25 +02:00
}
2020-05-20 00:03:33 +02:00
2021-05-08 23:31:20 +02:00
note.ownedAttributes = note.ownedAttributes.filter(attr => attr.attributeId !== attribute.attributeId);
2021-05-01 11:38:20 +02:00
const targetNote = attribute.targetNote;
2021-05-01 11:38:20 +02:00
if (targetNote) {
2021-05-08 23:31:20 +02:00
targetNote.targetRelations = targetNote.targetRelations.filter(rel => rel.attributeId !== attribute.attributeId);
2020-05-20 00:03:33 +02:00
}
2021-05-01 11:38:20 +02:00
}
2021-05-08 23:31:20 +02:00
delete becca.attributes[attribute.attributeId];
2021-05-01 11:38:20 +02:00
const key = `${attribute.type}-${attribute.name.toLowerCase()}`;
if (key in becca.attributeIndex) {
2021-05-08 23:31:20 +02:00
becca.attributeIndex[key] = becca.attributeIndex[key].filter(attr => attr.attributeId !== attribute.attributeId);
2021-05-01 11:38:20 +02:00
}
}
2020-05-20 00:03:33 +02:00
2024-02-17 20:45:31 +02:00
function attributeUpdated(attributeRow: BAttribute) {
2023-06-01 00:07:57 +02:00
const attribute = becca.attributes[attributeRow.attributeId];
const note = becca.notes[attributeRow.noteId];
2021-04-30 23:10:25 +02:00
2021-05-01 11:38:20 +02:00
if (note) {
2023-01-06 20:31:55 +01:00
if (attribute.isAffectingSubtree || note.isInherited()) {
2021-05-01 11:38:20 +02:00
note.invalidateSubTree();
2021-04-30 23:10:25 +02:00
} else {
note.invalidateThisCache();
}
2020-05-20 00:03:33 +02:00
}
2021-04-30 23:10:25 +02:00
}
2024-02-17 20:45:31 +02:00
function noteReorderingUpdated(branchIdList: number[]) {
2021-04-30 23:10:25 +02:00
const parentNoteIds = new Set();
2021-05-01 11:38:20 +02:00
for (const branchId in branchIdList) {
2021-04-30 23:10:25 +02:00
const branch = becca.branches[branchId];
2021-04-30 23:10:25 +02:00
if (branch) {
2021-05-01 11:38:20 +02:00
branch.notePosition = branchIdList[branchId];
2021-04-30 23:10:25 +02:00
parentNoteIds.add(branch.parentNoteId);
}
}
2021-04-30 23:10:25 +02:00
}
2024-02-17 20:45:31 +02:00
function etapiTokenDeleted(etapiTokenId: string) {
2022-01-10 17:09:20 +01:00
delete becca.etapiTokens[etapiTokenId];
}
eventService.subscribeBeccaLoader(eventService.ENTER_PROTECTED_SESSION, () => {
try {
2021-04-16 23:00:08 +02:00
becca.decryptProtectedNotes();
}
2024-02-17 20:45:31 +02:00
catch (e: any) {
log.error(`Could not decrypt protected notes: ${e.message} ${e.stack}`);
}
2020-05-20 00:03:33 +02:00
});
2020-07-27 23:40:14 +02:00
eventService.subscribeBeccaLoader(eventService.LEAVE_PROTECTED_SESSION, load);
2024-02-17 20:45:31 +02:00
export = {
2021-05-01 21:52:22 +02:00
load,
reload,
2021-05-01 21:52:22 +02:00
beccaLoaded
2020-07-27 23:40:14 +02:00
};