initial work on note fulltext (schema changes, updating...)

This commit is contained in:
zadam
2019-03-10 17:02:23 +01:00
parent 05374becfd
commit d25a1e3ed9
11 changed files with 469 additions and 142 deletions

View File

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

View 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
};

View File

@@ -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();
}
});
}

View File

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