note cache refactoring WIP

This commit is contained in:
zadam 2020-05-17 09:48:24 +02:00
parent dcd371b5b1
commit 60c2049729
14 changed files with 104 additions and 57 deletions

View File

@ -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;

View File

@ -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;

View File

@ -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;

View File

@ -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,18 +59,14 @@ 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)) {
note.decrypt();
}
}
decryptProtectedNotes() {
for (const note of Object.values(this.notes)) {
decryptProtectedNote(note);
}
getBranch(childNoteId, parentNoteId) {
return this.childParentToBranch[`${childNoteId}-${parentNoteId}`];
}
}
@ -100,13 +94,13 @@ eventService.subscribe([eventService.ENTITY_CHANGED, eventService.ENTITY_DELETED
note.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;

View File

@ -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];

View File

@ -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;

View File

@ -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;

View File

@ -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;

View File

@ -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;

View File

@ -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;

View File

@ -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;

View File

@ -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;

View File

@ -1,3 +1,7 @@
"use strict";
import NoteCacheFulltextExp from "./expressions/note_cache_fulltext.js";
async function findNotesWithExpression(expression) {
const hoistedNote = notes[hoistedNoteService.getHoistedNoteId()];

View File

@ -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;