diff --git a/.idea/dataSources.xml b/.idea/dataSources.xml index 70caa7f2a..fa5ae48bf 100644 --- a/.idea/dataSources.xml +++ b/.idea/dataSources.xml @@ -1,7 +1,7 @@ - + sqlite.xerial true org.sqlite.JDBC diff --git a/src/services/note_cache.js b/src/services/note_cache.js index 4285eccd9..de4ee09f0 100644 --- a/src/services/note_cache.js +++ b/src/services/note_cache.js @@ -7,14 +7,14 @@ const utils = require('./utils'); const hoistedNoteService = require('./hoisted_note'); const stringSimilarity = require('string-similarity'); -/** @var {Object.} */ +/** @type {Object.} */ let notes; -/** @var {Object.} */ +/** @type {Object.} */ let branches -/** @var {Object.} */ +/** @type {Object.} */ let attributes; -/** @var {Object.} */ +/** @type {Object.} */ let noteAttributeCache = {}; let childParentToBranch = {}; @@ -109,6 +109,59 @@ class Attribute { } } +class FulltextReference { + /** + * @param type - attributeName, attributeValue, title + * @param id - attributeId, noteId + */ + constructor(type, id) { + this.type = type; + this.id = id; + } +} + +/** @type {Object.} */ +let fulltext = {}; + +/** @type {Object.} */ +let attributeMetas = {}; + +class AttributeMeta { + constructor(attribute) { + this.type = attribute.type; + this.name = attribute.name; + this.isInheritable = attribute.isInheritable; + this.attributeIds = new Set(attribute.attributeId); + } + + addAttribute(attribute) { + this.attributeIds.add(attribute.attributeId); + this.isInheritable = this.isInheritable || attribute.isInheritable; + } + + updateAttribute(attribute) { + if (attribute.isDeleted) { + this.attributeIds.delete(attribute.attributeId); + } + else { + this.attributeIds.add(attribute.attributeId); + } + + this.isInheritable = !!this.attributeIds.find(attributeId => attributes[attributeId].isInheritable); + } +} + +function addToAttributeMeta(attribute) { + const key = `${attribute.type}-${attribute.name}`; + + if (!(key in attributeMetas)) { + attributeMetas[key] = new AttributeMeta(attribute); + } + else { + attributeMetas[key].addAttribute(attribute); + } +} + let loaded = false; let loadedPromiseResolve; /** Is resolved after the initial load */ @@ -140,12 +193,27 @@ async function load() { notes = await getMappedRows(`SELECT noteId, title, isProtected FROM notes WHERE isDeleted = 0`, row => new Note(row)); + for (const note of notes) { + fulltext[note.title] = fulltext[note.title] || []; + fulltext[note.title].push(new FulltextReference('note', note.noteId)); + } + branches = await getMappedRows(`SELECT branchId, noteId, parentNoteId, prefix FROM branches WHERE isDeleted = 0`, row => new Branch(row)); attributes = await getMappedRows(`SELECT attributeId, noteId, type, name, value, isInheritable FROM attributes WHERE isDeleted = 0`, row => new Attribute(row)); + for (const attr of attributes) { + addToAttributeMeta(attributes); + + fulltext[attr.name] = fulltext[attr.name] || []; + fulltext[attr.name].push(new FulltextReference('aName', attr.attributeId)); + + fulltext[attr.value] = fulltext[attr.value] || []; + fulltext[attr.value].push(new FulltextReference('aVal', attr.attributeId)); + } + for (const branch of branches) { const childNote = notes[branch.noteId]; @@ -654,4 +722,4 @@ module.exports = { isInAncestor, load, findSimilarNotes -}; \ No newline at end of file +};