mirror of
https://github.com/zadam/trilium.git
synced 2025-11-18 03:00:41 +01:00
initial work on note fulltext (schema changes, updating...)
This commit is contained in:
@@ -4,8 +4,8 @@ const build = require('./build');
|
||||
const packageJson = require('../../package');
|
||||
const {TRILIUM_DATA_DIR} = require('./data_dir');
|
||||
|
||||
const APP_DB_VERSION = 126;
|
||||
const SYNC_VERSION = 6;
|
||||
const APP_DB_VERSION = 128;
|
||||
const SYNC_VERSION = 7;
|
||||
|
||||
module.exports = {
|
||||
appVersion: packageJson.version,
|
||||
|
||||
60
src/services/note_fulltext.js
Normal file
60
src/services/note_fulltext.js
Normal file
@@ -0,0 +1,60 @@
|
||||
const sql = require('./sql');
|
||||
const repository = require('./repository');
|
||||
const html2plaintext = require('html2plaintext');
|
||||
|
||||
const noteIdQueue = [];
|
||||
|
||||
async function updateNoteFulltext(note) {
|
||||
if (note.isDeleted || note.isProtected || note.hasLabel('archived')) {
|
||||
await sql.execute(`DELETE
|
||||
FROM note_fulltext
|
||||
WHERE noteId = ?`, [note.noteId]);
|
||||
} else {
|
||||
let content = null;
|
||||
let contentHash = null;
|
||||
|
||||
if (['text', 'code'].includes(note.type)) {
|
||||
const noteContent = await note.getNoteContent();
|
||||
content = noteContent.content;
|
||||
|
||||
if (note.type === 'text' && note.mime === 'text/html') {
|
||||
content = html2plaintext(content);
|
||||
}
|
||||
|
||||
contentHash = noteContent.hash;
|
||||
}
|
||||
|
||||
// optimistically try to update first ...
|
||||
const res = await sql.execute(`UPDATE note_fulltext title = ?, titleHash = ?, content = ?, contentHash = ? WHERE noteId = ?`, [note.title, note.hash, content, contentHash, note.noteId]);
|
||||
|
||||
// ... and insert only when the update did not work
|
||||
if (res.stmt.changes === 0) {
|
||||
await sql.execute(`INSERT INTO note_fulltext (title, titleHash, content, contentHash, noteId)
|
||||
VALUES (?, ?, ?, ?, ?)`, [note.title, note.hash, content, contentHash, note.noteId]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async function triggerNoteFulltextUpdate(noteId) {
|
||||
if (!noteIdQueue.includes(noteId)) {
|
||||
noteIdQueue.push(noteId);
|
||||
}
|
||||
|
||||
while (noteIdQueue.length > 0) {
|
||||
await sql.transactional(async () => {
|
||||
if (noteIdQueue.length === 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
const noteId = noteIdQueue.shift();
|
||||
const note = await repository.getNote(noteId);
|
||||
|
||||
await updateNoteFulltext(note);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
triggerNoteFulltextUpdate,
|
||||
updateNoteFulltext
|
||||
};
|
||||
@@ -126,6 +126,10 @@ async function updateEntity(entity) {
|
||||
await eventService.emit(entity.isDeleted ? eventService.ENTITY_DELETED : eventService.ENTITY_CHANGED, eventPayload);
|
||||
}
|
||||
}
|
||||
|
||||
if (entity.afterSaving) {
|
||||
await entity.afterSaving();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
@@ -3,6 +3,7 @@ const log = require('./log');
|
||||
const eventLogService = require('./event_log');
|
||||
const syncTableService = require('./sync_table');
|
||||
const eventService = require('./events');
|
||||
const noteFulltextService = require('../services/note_fulltext');
|
||||
|
||||
async function updateEntity(sync, entity, sourceId) {
|
||||
const {entityName} = sync;
|
||||
@@ -61,6 +62,8 @@ async function updateNote(entity, sourceId) {
|
||||
await syncTableService.addNoteSync(entity.noteId, sourceId);
|
||||
});
|
||||
|
||||
noteFulltextService.triggerNoteFulltextUpdate(entity.noteId);
|
||||
|
||||
log.info("Update/sync note " + entity.noteId);
|
||||
}
|
||||
}
|
||||
@@ -75,6 +78,8 @@ async function updateNoteContent(entity, sourceId) {
|
||||
await sql.replace("note_contents", entity);
|
||||
|
||||
await syncTableService.addNoteContentSync(entity.noteContentId, sourceId);
|
||||
|
||||
noteFulltextService.triggerNoteFulltextUpdate(entity.noteId);
|
||||
});
|
||||
|
||||
log.info("Update/sync note content " + entity.noteContentId);
|
||||
|
||||
Reference in New Issue
Block a user