note cache fixes after refactoring

This commit is contained in:
zadam 2020-05-17 10:11:19 +02:00
parent 60c2049729
commit 32eaafd024
13 changed files with 69 additions and 40 deletions

View File

@ -1,6 +1,7 @@
"use strict"; "use strict";
const noteCacheService = require('../../services/note_cache/note_cache.js'); const noteCacheService = require('../../services/note_cache/note_cache_service');
const searchService = require('../../services/search/search');
const repository = require('../../services/repository'); const repository = require('../../services/repository');
const log = require('../../services/log'); const log = require('../../services/log');
const utils = require('../../services/utils'); const utils = require('../../services/utils');
@ -18,7 +19,7 @@ async function getAutocomplete(req) {
results = await getRecentNotes(activeNoteId); results = await getRecentNotes(activeNoteId);
} }
else { else {
results = await noteCacheService.findNotesForAutocomplete(query); results = await searchService.searchNotesForAutocomplete(query);
} }
const msTaken = Date.now() - timestampStarted; const msTaken = Date.now() - timestampStarted;

View File

@ -1,6 +1,6 @@
"use strict"; "use strict";
const noteCacheService = require('../../services/note_cache/note_cache.js'); const noteCacheService = require('../../services/note_cache/note_cache_service');
const repository = require('../../services/repository'); const repository = require('../../services/repository');
async function getSimilarNotes(req) { async function getSimilarNotes(req) {

View File

@ -1,9 +1,9 @@
"use strict"; "use strict";
const noteCache = require('../note_cache');
class Attribute { class Attribute {
constructor(row) { constructor(noteCache, row) {
/** @param {NoteCache} */
this.noteCache = noteCache;
/** @param {string} */ /** @param {string} */
this.attributeId = row.attributeId; this.attributeId = row.attributeId;
/** @param {string} */ /** @param {string} */
@ -17,11 +17,11 @@ class Attribute {
/** @param {boolean} */ /** @param {boolean} */
this.isInheritable = !!row.isInheritable; this.isInheritable = !!row.isInheritable;
noteCache.notes[this.noteId].ownedAttributes.push(this); this.noteCache.notes[this.noteId].ownedAttributes.push(this);
const key = `${this.type-this.name}`; const key = `${this.type-this.name}`;
noteCache.attributeIndex[key] = noteCache.attributeIndex[key] || []; this.noteCache.attributeIndex[key] = this.noteCache.attributeIndex[key] || [];
noteCache.attributeIndex[key].push(this); this.noteCache.attributeIndex[key].push(this);
const targetNote = this.targetNote; const targetNote = this.targetNote;
@ -36,12 +36,12 @@ class Attribute {
} }
get note() { get note() {
return noteCache.notes[this.noteId]; return this.noteCache.notes[this.noteId];
} }
get targetNote() { get targetNote() {
if (this.type === 'relation') { if (this.type === 'relation') {
return noteCache.notes[this.value]; return this.noteCache.notes[this.value];
} }
} }
} }

View File

@ -1,9 +1,9 @@
"use strict"; "use strict";
const noteCache = require('../note_cache');
class Branch { class Branch {
constructor(row) { constructor(noteCache, row) {
/** @param {NoteCache} */
this.noteCache = noteCache;
/** @param {string} */ /** @param {string} */
this.branchId = row.branchId; this.branchId = row.branchId;
/** @param {string} */ /** @param {string} */
@ -17,7 +17,7 @@ class Branch {
return; return;
} }
const childNote = noteCache.notes[this.noteId]; const childNote = this.noteCache.notes[this.noteId];
const parentNote = this.parentNote; const parentNote = this.parentNote;
if (!childNote) { if (!childNote) {
@ -30,12 +30,12 @@ class Branch {
parentNote.children.push(childNote); parentNote.children.push(childNote);
noteCache.childParentToBranch[`${this.noteId}-${this.parentNoteId}`] = this; this.noteCache.childParentToBranch[`${this.noteId}-${this.parentNoteId}`] = this;
} }
/** @return {Note} */ /** @return {Note} */
get parentNote() { get parentNote() {
const note = noteCache.notes[this.parentNoteId]; const note = this.noteCache.notes[this.parentNoteId];
if (!note) { if (!note) {
console.log(`Cannot find note ${this.parentNoteId}`); console.log(`Cannot find note ${this.parentNoteId}`);

View File

@ -1,9 +1,12 @@
"use strict"; "use strict";
const noteCache = require('../note_cache'); const noteCache = require('../note_cache');
const protectedSessionService = require('../../protected_session');
class Note { class Note {
constructor(row) { constructor(noteCache, row) {
/** @param {NoteCache} */
this.noteCache = noteCache;
/** @param {string} */ /** @param {string} */
this.noteId = row.noteId; this.noteId = row.noteId;
/** @param {string} */ /** @param {string} */
@ -33,7 +36,7 @@ class Note {
this.flatTextCache = null; this.flatTextCache = null;
if (protectedSessionService.isProtectedSessionAvailable()) { if (protectedSessionService.isProtectedSessionAvailable()) {
noteCache.decryptProtectedNote(this); this.decrypt();
} }
} }
@ -52,7 +55,7 @@ class Note {
for (const ownedAttr of parentAttributes) { // parentAttributes so we process also inherited templates for (const ownedAttr of parentAttributes) { // parentAttributes so we process also inherited templates
if (ownedAttr.type === 'relation' && ownedAttr.name === 'template') { if (ownedAttr.type === 'relation' && ownedAttr.name === 'template') {
const templateNote = notes[ownedAttr.value]; const templateNote = this.noteCache.notes[ownedAttr.value];
if (templateNote) { if (templateNote) {
templateAttributes.push(...templateNote.attributes); templateAttributes.push(...templateNote.attributes);

View File

@ -33,15 +33,15 @@ class NoteCache {
await sqlInit.dbReady; 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(this, row));
this.branches = await this.getMappedRows(`SELECT branchId, noteId, parentNoteId, prefix FROM branches WHERE isDeleted = 0`, this.branches = await this.getMappedRows(`SELECT branchId, noteId, parentNoteId, prefix FROM branches WHERE isDeleted = 0`,
row => new Branch(row)); row => new Branch(this, row));
this.attributeIndex = []; this.attributeIndex = [];
this.attributes = await this.getMappedRows(`SELECT attributeId, noteId, type, name, value, isInheritable FROM attributes WHERE isDeleted = 0`, this.attributes = await this.getMappedRows(`SELECT attributeId, noteId, type, name, value, isInheritable FROM attributes WHERE isDeleted = 0`,
row => new Attribute(row)); row => new Attribute(this, row));
this.loaded = true; this.loaded = true;
} }
@ -94,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;
noteCache.decryptProtectedNote(note); note.decrypt();
} }
else { else {
const note = new Note(entity); const note = new Note(entity);
noteCache.notes[noteId] = note; noteCache.notes[noteId] = note;
noteCache.decryptProtectedNote(note); note.decrypt();
} }
} }
else if (entityName === 'branches') { else if (entityName === 'branches') {

View File

@ -2,6 +2,7 @@
const noteCache = require('./note_cache'); const noteCache = require('./note_cache');
const hoistedNoteService = require('../hoisted_note'); const hoistedNoteService = require('../hoisted_note');
const stringSimilarity = require('string-similarity');
function isNotePathArchived(notePath) { function isNotePathArchived(notePath) {
const noteId = notePath[notePath.length - 1]; const noteId = notePath[notePath.length - 1];
@ -69,7 +70,7 @@ function getNoteTitle(childNoteId, parentNoteId) {
title = childNote.title; title = childNote.title;
} }
const branch = parentNote ? getBranch(childNote.noteId, parentNote.noteId) : null; const branch = parentNote ? noteCache.getBranch(childNote.noteId, parentNote.noteId) : null;
return ((branch && branch.prefix) ? `${branch.prefix} - ` : '') + title; return ((branch && branch.prefix) ? `${branch.prefix} - ` : '') + title;
} }
@ -199,7 +200,7 @@ async function findSimilarNotes(noteId) {
return []; return [];
} }
for (const note of Object.values(notes)) { for (const note of Object.values(noteCache.notes)) {
if (note.isProtected && !note.isDecrypted) { if (note.isProtected && !note.isDecrypted) {
continue; continue;
} }
@ -229,7 +230,9 @@ function isAvailable(noteId) {
} }
module.exports = { module.exports = {
getSomePath,
getNotePath, getNotePath,
getNoteTitle,
getNoteTitleForPath, getNoteTitleForPath,
isAvailable, isAvailable,
isArchived, isArchived,

View File

@ -1,5 +1,8 @@
"use strict"; "use strict";
const NoteSet = require('../note_set');
const noteCache = require('../../note_cache/note_cache');
class EqualsExp { class EqualsExp {
constructor(attributeType, attributeName, attributeValue) { constructor(attributeType, attributeName, attributeValue) {
this.attributeType = attributeType; this.attributeType = attributeType;
@ -8,7 +11,7 @@ class EqualsExp {
} }
execute(noteSet) { execute(noteSet) {
const attrs = findAttributes(this.attributeType, this.attributeName); const attrs = noteCache.findAttributes(this.attributeType, this.attributeName);
const resultNoteSet = new NoteSet(); const resultNoteSet = new NoteSet();
for (const attr of attrs) { for (const attr of attrs) {

View File

@ -1,5 +1,8 @@
"use strict"; "use strict";
const NoteSet = require('../note_set');
const noteCache = require('../../note_cache/note_cache');
class ExistsExp { class ExistsExp {
constructor(attributeType, attributeName) { constructor(attributeType, attributeName) {
this.attributeType = attributeType; this.attributeType = attributeType;
@ -7,7 +10,7 @@ class ExistsExp {
} }
execute(noteSet) { execute(noteSet) {
const attrs = findAttributes(this.attributeType, this.attributeName); const attrs = noteCache.findAttributes(this.attributeType, this.attributeName);
const resultNoteSet = new NoteSet(); const resultNoteSet = new NoteSet();
for (const attr of attrs) { for (const attr of attrs) {

View File

@ -1,5 +1,9 @@
"use strict"; "use strict";
const NoteSet = require('../note_set');
const noteCache = require('../../note_cache/note_cache');
const noteCacheService = require('../../note_cache/note_cache_service');
class NoteCacheFulltextExp { class NoteCacheFulltextExp {
constructor(tokens) { constructor(tokens) {
this.tokens = tokens; this.tokens = tokens;
@ -34,7 +38,7 @@ class NoteCacheFulltextExp {
} }
for (const parentNote of note.parents) { for (const parentNote of note.parents) {
const title = getNoteTitle(note.noteId, parentNote.noteId).toLowerCase(); const title = noteCacheService.getNoteTitle(note.noteId, parentNote.noteId).toLowerCase();
const foundTokens = foundAttrTokens.slice(); const foundTokens = foundAttrTokens.slice();
for (const token of this.tokens) { for (const token of this.tokens) {
@ -77,13 +81,13 @@ class NoteCacheFulltextExp {
searchDownThePath(note, tokens, path, resultNoteSet, searchContext) { searchDownThePath(note, tokens, path, resultNoteSet, searchContext) {
if (tokens.length === 0) { if (tokens.length === 0) {
const retPath = getSomePath(note, path); const retPath = noteCacheService.getSomePath(note, path);
if (retPath) { if (retPath) {
const noteId = retPath[retPath.length - 1]; const noteId = retPath[retPath.length - 1];
searchContext.noteIdToNotePath[noteId] = retPath; searchContext.noteIdToNotePath[noteId] = retPath;
resultNoteSet.add(notes[noteId]); resultNoteSet.add(noteCache.notes[noteId]);
} }
return; return;
@ -105,7 +109,7 @@ class NoteCacheFulltextExp {
} }
for (const parentNote of note.parents) { for (const parentNote of note.parents) {
const title = getNoteTitle(note.noteId, parentNote.noteId).toLowerCase(); const title = noteCacheService.getNoteTitle(note.noteId, parentNote.noteId).toLowerCase();
const foundTokens = foundAttrTokens.slice(); const foundTokens = foundAttrTokens.slice();
for (const token of tokens) { for (const token of tokens) {

View File

@ -1,5 +1,8 @@
"use strict"; "use strict";
const NoteSet = require('../note_set');
const noteCache = require('../../note_cache/note_cache');
class NoteContentFulltextExp { class NoteContentFulltextExp {
constructor(tokens) { constructor(tokens) {
this.tokens = tokens; this.tokens = tokens;
@ -18,8 +21,8 @@ class NoteContentFulltextExp {
const results = []; const results = [];
for (const noteId of noteIds) { for (const noteId of noteIds) {
if (noteSet.hasNoteId(noteId) && noteId in notes) { if (noteSet.hasNoteId(noteId) && noteId in noteCache.notes) {
resultNoteSet.add(notes[noteId]); resultNoteSet.add(noteCache.notes[noteId]);
} }
} }

View File

@ -1,13 +1,18 @@
"use strict"; "use strict";
import NoteCacheFulltextExp from "./expressions/note_cache_fulltext.js"; const NoteCacheFulltextExp = require("./expressions/note_cache_fulltext");
const NoteSet = require("./note_set");
const SearchResult = require("./search_result");
const noteCache = require('../note_cache/note_cache');
const hoistedNoteService = require('../hoisted_note');
const utils = require('../utils');
async function findNotesWithExpression(expression) { async function findNotesWithExpression(expression) {
const hoistedNote = notes[hoistedNoteService.getHoistedNoteId()]; const hoistedNote = noteCache.notes[hoistedNoteService.getHoistedNoteId()];
const allNotes = (hoistedNote && hoistedNote.noteId !== 'root') const allNotes = (hoistedNote && hoistedNote.noteId !== 'root')
? hoistedNote.subtreeNotes ? hoistedNote.subtreeNotes
: Object.values(notes); : Object.values(noteCache.notes);
const allNoteSet = new NoteSet(allNotes); const allNoteSet = new NoteSet(allNotes);
@ -35,7 +40,7 @@ async function findNotesWithExpression(expression) {
return searchResults; return searchResults;
} }
async function findNotesForAutocomplete(query) { async function searchNotesForAutocomplete(query) {
if (!query.trim().length) { if (!query.trim().length) {
return []; return [];
} }
@ -73,7 +78,7 @@ function highlightSearchResults(searchResults, tokens) {
tokens.sort((a, b) => a.length > b.length ? -1 : 1); tokens.sort((a, b) => a.length > b.length ? -1 : 1);
for (const result of searchResults) { for (const result of searchResults) {
const note = notes[result.noteId]; const note = noteCache.notes[result.noteId];
result.highlightedNotePathTitle = result.notePathTitle; result.highlightedNotePathTitle = result.notePathTitle;
@ -115,3 +120,7 @@ function formatAttribute(attr) {
return label; return label;
} }
} }
module.exports = {
searchNotesForAutocomplete
};