Notes/src/becca/becca.js

204 lines
5.5 KiB
JavaScript
Raw Normal View History

2020-05-17 09:48:24 +02:00
"use strict";
2020-05-16 23:12:29 +02:00
2022-01-10 17:09:20 +01:00
const sql = require("../services/sql");
const NoteSet = require("../services/search/note_set");
2021-10-16 22:13:34 +02:00
/**
* Becca is a backend cache of all notes, branches and attributes. There's a similar frontend cache Froca.
*/
2021-04-16 23:00:08 +02:00
class Becca {
2020-05-16 23:12:29 +02:00
constructor() {
2020-05-22 09:38:30 +02:00
this.reset();
}
reset() {
2020-05-16 23:12:29 +02:00
/** @type {Object.<String, Note>} */
2021-04-30 23:10:25 +02:00
this.notes = {};
2020-05-16 23:12:29 +02:00
/** @type {Object.<String, Branch>} */
2021-04-30 23:10:25 +02:00
this.branches = {};
2020-05-16 23:12:29 +02:00
/** @type {Object.<String, Branch>} */
this.childParentToBranch = {};
/** @type {Object.<String, Attribute>} */
2021-04-30 23:10:25 +02:00
this.attributes = {};
/** @type {Object.<String, Attribute[]>} Points from attribute type-name to list of attributes */
2020-05-22 09:38:30 +02:00
this.attributeIndex = {};
2021-04-30 23:10:25 +02:00
/** @type {Object.<String, Option>} */
this.options = {};
2022-01-10 17:09:20 +01:00
/** @type {Object.<String, EtapiToken>} */
this.etapiTokens = {};
2020-05-16 23:12:29 +02:00
this.dirtyNoteSetCache();
2020-05-16 23:12:29 +02:00
this.loaded = false;
}
2022-12-23 15:07:48 +01:00
getRoot() {
return this.getNote('root');
}
/** @returns {Attribute[]} */
2020-05-16 23:12:29 +02:00
findAttributes(type, name) {
name = name.trim().toLowerCase();
if (name.startsWith('#') || name.startsWith('~')) {
name = name.substr(1);
}
return this.attributeIndex[`${type}-${name}`] || [];
2020-05-16 23:12:29 +02:00
}
/** @returns {Attribute[]} */
findAttributesWithPrefix(type, name) {
const resArr = [];
const key = `${type}-${name}`;
for (const idx in this.attributeIndex) {
if (idx.startsWith(key)) {
resArr.push(this.attributeIndex[idx]);
}
}
return resArr.flat();
}
2020-05-16 23:12:29 +02:00
decryptProtectedNotes() {
for (const note of Object.values(this.notes)) {
2020-05-17 09:48:24 +02:00
note.decrypt();
2020-05-16 23:12:29 +02:00
}
}
2020-05-17 09:48:24 +02:00
addNote(noteId, note) {
this.notes[noteId] = note;
this.dirtyNoteSetCache();
}
2022-01-10 17:09:20 +01:00
/** @returns {Note|null} */
getNote(noteId) {
return this.notes[noteId];
}
2022-01-10 17:09:20 +01:00
/** @returns {Note[]} */
getNotes(noteIds, ignoreMissing = false) {
2021-05-08 11:20:20 +02:00
const filteredNotes = [];
for (const noteId of noteIds) {
const note = this.notes[noteId];
if (!note) {
if (ignoreMissing) {
continue;
}
2021-05-08 11:20:20 +02:00
throw new Error(`Note '${noteId}' was not found in becca.`);
}
filteredNotes.push(note);
}
return filteredNotes;
2021-04-26 22:24:55 +02:00
}
2022-01-10 17:09:20 +01:00
/** @returns {Branch|null} */
getBranch(branchId) {
return this.branches[branchId];
}
2022-01-10 17:09:20 +01:00
/** @returns {Attribute|null} */
getAttribute(attributeId) {
return this.attributes[attributeId];
}
2022-01-10 17:09:20 +01:00
/** @returns {Branch|null} */
getBranchFromChildAndParent(childNoteId, parentNoteId) {
2020-05-17 09:48:24 +02:00
return this.childParentToBranch[`${childNoteId}-${parentNoteId}`];
}
2022-01-10 17:09:20 +01:00
/** @returns {NoteRevision|null} */
getNoteRevision(noteRevisionId) {
const row = sql.getRow("SELECT * FROM note_revisions WHERE noteRevisionId = ?", [noteRevisionId]);
2022-01-10 17:09:20 +01:00
const NoteRevision = require("./entities/note_revision"); // avoiding circular dependency problems
return row ? new NoteRevision(row) : null;
}
2021-05-01 21:52:22 +02:00
2022-01-10 17:09:20 +01:00
/** @returns {Option|null} */
2021-05-01 21:52:22 +02:00
getOption(name) {
return this.options[name];
}
2021-05-02 12:02:32 +02:00
2022-01-10 17:09:20 +01:00
/** @returns {EtapiToken[]} */
getEtapiTokens() {
return Object.values(this.etapiTokens);
}
/** @returns {EtapiToken|null} */
getEtapiToken(etapiTokenId) {
return this.etapiTokens[etapiTokenId];
}
2021-05-17 22:05:35 +02:00
getEntity(entityName, entityId) {
2021-05-02 12:02:32 +02:00
if (!entityName || !entityId) {
return null;
}
2021-08-07 21:21:30 +02:00
if (entityName === 'note_revisions') {
return this.getNoteRevision(entityId);
}
2021-05-02 12:02:32 +02:00
const camelCaseEntityName = entityName.toLowerCase().replace(/(_[a-z])/g,
group =>
group
.toUpperCase()
.replace('_', '')
);
return this[camelCaseEntityName][entityId];
}
2021-05-02 19:59:16 +02:00
2022-01-10 17:09:20 +01:00
/** @returns {RecentNote[]} */
2021-05-02 19:59:16 +02:00
getRecentNotesFromQuery(query, params = []) {
const rows = sql.getRows(query, params);
2022-01-10 17:09:20 +01:00
const RecentNote = require("./entities/recent_note"); // avoiding circular dependency problems
2021-05-02 19:59:16 +02:00
return rows.map(row => new RecentNote(row));
}
2022-01-10 17:09:20 +01:00
/** @returns {NoteRevision[]} */
2021-05-02 19:59:16 +02:00
getNoteRevisionsFromQuery(query, params = []) {
const rows = sql.getRows(query, params);
2022-01-10 17:09:20 +01:00
const NoteRevision = require("./entities/note_revision"); // avoiding circular dependency problems
2021-05-02 19:59:16 +02:00
return rows.map(row => new NoteRevision(row));
}
/** Should be called when the set of all non-skeleton notes changes (added/removed) */
dirtyNoteSetCache() {
this.allNoteSetCache = null;
}
getAllNoteSet() {
// caching this since it takes 10s of milliseconds to fill this initial NoteSet for many notes
if (!this.allNoteSetCache) {
const allNotes = [];
for (const noteId in becca.notes) {
const note = becca.notes[noteId];
// in the process of loading data sometimes we create "skeleton" note instances which are expected to be filled later
// in case of inconsistent data this might not work and search will then crash on these
if (note.type !== undefined) {
allNotes.push(note);
}
}
this.allNoteSetCache = new NoteSet(allNotes);
}
return this.allNoteSetCache;
}
2020-05-16 23:12:29 +02:00
}
2021-04-16 23:00:08 +02:00
const becca = new Becca();
2020-05-16 23:12:29 +02:00
2021-04-16 23:00:08 +02:00
module.exports = becca;