mirror of
				https://github.com/zadam/trilium.git
				synced 2025-10-31 18:36:30 +01:00 
			
		
		
		
	repository has now first level cache
This commit is contained in:
		| @@ -50,7 +50,7 @@ | |||||||
|     "imagemin-pngquant": "8.0.0", |     "imagemin-pngquant": "8.0.0", | ||||||
|     "ini": "1.3.5", |     "ini": "1.3.5", | ||||||
|     "is-svg": "4.2.1", |     "is-svg": "4.2.1", | ||||||
|     "jimp": "0.10.0", |     "jimp": "0.10.1", | ||||||
|     "mime-types": "2.1.26", |     "mime-types": "2.1.26", | ||||||
|     "multer": "1.4.2", |     "multer": "1.4.2", | ||||||
|     "node-abi": "2.15.0", |     "node-abi": "2.15.0", | ||||||
| @@ -77,7 +77,7 @@ | |||||||
|     "yazl": "^2.5.1" |     "yazl": "^2.5.1" | ||||||
|   }, |   }, | ||||||
|   "devDependencies": { |   "devDependencies": { | ||||||
|     "electron": "9.0.0-beta.13", |     "electron": "9.0.0-beta.14", | ||||||
|     "electron-builder": "22.4.1", |     "electron-builder": "22.4.1", | ||||||
|     "electron-packager": "14.2.1", |     "electron-packager": "14.2.1", | ||||||
|     "electron-rebuild": "1.10.1", |     "electron-rebuild": "1.10.1", | ||||||
|   | |||||||
| @@ -45,11 +45,7 @@ class Attribute extends Entity { | |||||||
|      * @returns {Promise<Note|null>} |      * @returns {Promise<Note|null>} | ||||||
|      */ |      */ | ||||||
|     async getNote() { |     async getNote() { | ||||||
|         if (!this.__note) { |         return await repository.getNote(this.noteId); | ||||||
|             this.__note = await repository.getEntity("SELECT * FROM notes WHERE noteId = ?", [this.noteId]); |  | ||||||
|         } |  | ||||||
|  |  | ||||||
|         return this.__note; |  | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     /** |     /** | ||||||
| @@ -64,11 +60,7 @@ class Attribute extends Entity { | |||||||
|             return null; |             return null; | ||||||
|         } |         } | ||||||
|  |  | ||||||
|         if (!this.__targetNote) { |         return await repository.getNote(this.value); | ||||||
|             this.__targetNote = await repository.getEntity("SELECT * FROM notes WHERE noteId = ?", [this.value]); |  | ||||||
|         } |  | ||||||
|  |  | ||||||
|         return this.__targetNote; |  | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     /** |     /** | ||||||
|   | |||||||
| @@ -28,9 +28,14 @@ class Branch extends Entity { | |||||||
|     // notePosition is not part of hash because it would produce a lot of updates in case of reordering |     // notePosition is not part of hash because it would produce a lot of updates in case of reordering | ||||||
|     static get hashedProperties() { return ["branchId", "noteId", "parentNoteId", "isDeleted", "deleteId", "prefix"]; } |     static get hashedProperties() { return ["branchId", "noteId", "parentNoteId", "isDeleted", "deleteId", "prefix"]; } | ||||||
|  |  | ||||||
|     /** @returns {Note|null} */ |     /** @returns {Promise<Note|null>} */ | ||||||
|     async getNote() { |     async getNote() { | ||||||
|         return await repository.getEntity("SELECT * FROM notes WHERE noteId = ?", [this.noteId]); |         return await repository.getNote(this.noteId); | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     /** @returns {Promise<Note|null>} */ | ||||||
|  |     async getParentNote() { | ||||||
|  |         return await repository.getNote(this.parentNoteId); | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     async beforeSaving() { |     async beforeSaving() { | ||||||
|   | |||||||
| @@ -48,7 +48,7 @@ class NoteRevision extends Entity { | |||||||
|     } |     } | ||||||
|  |  | ||||||
|     async getNote() { |     async getNote() { | ||||||
|         return await repository.getEntity("SELECT * FROM notes WHERE noteId = ?", [this.noteId]); |         return await repository.getNote(this.noteId); | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     /** @returns {boolean} true if the note has string content (not binary) */ |     /** @returns {boolean} true if the note has string content (not binary) */ | ||||||
|   | |||||||
| @@ -37,6 +37,14 @@ function reset() { | |||||||
|     clsHooked.reset(); |     clsHooked.reset(); | ||||||
| } | } | ||||||
|  |  | ||||||
|  | function getEntityFromCache(entityName, entityId) { | ||||||
|  |     return namespace.get(entityName + '-' + entityId); | ||||||
|  | } | ||||||
|  |  | ||||||
|  | function setEntityToCache(entityName, entityId, entity) { | ||||||
|  |     return namespace.set(entityName + '-' + entityId, entity); | ||||||
|  | } | ||||||
|  |  | ||||||
| module.exports = { | module.exports = { | ||||||
|     init, |     init, | ||||||
|     wrap, |     wrap, | ||||||
| @@ -46,5 +54,7 @@ module.exports = { | |||||||
|     isEntityEventsDisabled, |     isEntityEventsDisabled, | ||||||
|     reset, |     reset, | ||||||
|     getSyncRows, |     getSyncRows, | ||||||
|     addSyncRow |     addSyncRow, | ||||||
|  |     getEntityFromCache, | ||||||
|  |     setEntityToCache | ||||||
| }; | }; | ||||||
| @@ -544,16 +544,22 @@ async function deleteBranch(branch, deleteId, taskContext) { | |||||||
|         note.deleteId = deleteId; |         note.deleteId = deleteId; | ||||||
|         await note.save(); |         await note.save(); | ||||||
|  |  | ||||||
|  |         console.log("Deleting note", note.noteId); | ||||||
|  |  | ||||||
|         for (const attribute of await note.getOwnedAttributes()) { |         for (const attribute of await note.getOwnedAttributes()) { | ||||||
|             attribute.isDeleted = true; |             attribute.isDeleted = true; | ||||||
|             attribute.deleteId = deleteId; |             attribute.deleteId = deleteId; | ||||||
|             await attribute.save(); |             await attribute.save(); | ||||||
|  |  | ||||||
|  |             console.log("Deleting note's", note.noteId, "attribute", attribute.attributeId); | ||||||
|         } |         } | ||||||
|  |  | ||||||
|         for (const relation of await note.getTargetRelations()) { |         for (const relation of await note.getTargetRelations()) { | ||||||
|             relation.isDeleted = true; |             relation.isDeleted = true; | ||||||
|             relation.deleteId = deleteId; |             relation.deleteId = deleteId; | ||||||
|             await relation.save(); |             await relation.save(); | ||||||
|  |  | ||||||
|  |             console.log("Deleting note's", note.noteId, "target relation", relation.attributeId); | ||||||
|         } |         } | ||||||
|  |  | ||||||
|         return true; |         return true; | ||||||
|   | |||||||
| @@ -37,9 +37,21 @@ async function getEntity(query, params = []) { | |||||||
|     return entityConstructor.createEntityFromRow(row); |     return entityConstructor.createEntityFromRow(row); | ||||||
| } | } | ||||||
|  |  | ||||||
|  | async function getCachedEntity(entityName, entityId, query) { | ||||||
|  |     let entity = cls.getEntityFromCache(entityName, entityId); | ||||||
|  |  | ||||||
|  |     if (!entity) { | ||||||
|  |         entity = await getEntity(query, [entityId]); | ||||||
|  |  | ||||||
|  |         cls.setEntityToCache(entityName, entityId, entity); | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     return entity; | ||||||
|  | } | ||||||
|  |  | ||||||
| /** @returns {Promise<Note|null>} */ | /** @returns {Promise<Note|null>} */ | ||||||
| async function getNote(noteId) { | async function getNote(noteId) { | ||||||
|     return await getEntity("SELECT * FROM notes WHERE noteId = ?", [noteId]); |     return await getCachedEntity('notes', noteId, "SELECT * FROM notes WHERE noteId = ?"); | ||||||
| } | } | ||||||
|  |  | ||||||
| /** @returns {Promise<Note[]>} */ | /** @returns {Promise<Note[]>} */ | ||||||
| @@ -59,17 +71,17 @@ async function getNotes(noteIds) { | |||||||
|  |  | ||||||
| /** @returns {Promise<NoteRevision|null>} */ | /** @returns {Promise<NoteRevision|null>} */ | ||||||
| async function getNoteRevision(noteRevisionId) { | async function getNoteRevision(noteRevisionId) { | ||||||
|     return await getEntity("SELECT * FROM note_revisions WHERE noteRevisionId = ?", [noteRevisionId]); |     return await getCachedEntity('note_revisions', noteRevisionId, "SELECT * FROM note_revisions WHERE noteRevisionId = ?"); | ||||||
| } | } | ||||||
|  |  | ||||||
| /** @returns {Promise<Branch|null>} */ | /** @returns {Promise<Branch|null>} */ | ||||||
| async function getBranch(branchId) { | async function getBranch(branchId) { | ||||||
|     return await getEntity("SELECT * FROM branches WHERE branchId = ?", [branchId]); |     return await getCachedEntity('branches', branchId, "SELECT * FROM branches WHERE branchId = ?", [branchId]); | ||||||
| } | } | ||||||
|  |  | ||||||
| /** @returns {Promise<Attribute|null>} */ | /** @returns {Promise<Attribute|null>} */ | ||||||
| async function getAttribute(attributeId) { | async function getAttribute(attributeId) { | ||||||
|     return await getEntity("SELECT * FROM attributes WHERE attributeId = ?", [attributeId]); |     return await getCachedEntity('attributes', attributeId, "SELECT * FROM attributes WHERE attributeId = ?"); | ||||||
| } | } | ||||||
|  |  | ||||||
| /** @returns {Promise<Option|null>} */ | /** @returns {Promise<Option|null>} */ | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user