mirror of
https://github.com/zadam/trilium.git
synced 2025-11-13 00:35:50 +01:00
Merge remote-tracking branch 'origin/optimize-tree'
This commit is contained in:
@@ -193,6 +193,10 @@ function duplicateSubtree(req) {
|
||||
return noteService.duplicateSubtree(noteId, parentNoteId);
|
||||
}
|
||||
|
||||
function eraseDeletedNotesNow() {
|
||||
noteService.eraseDeletedNotesNow();
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
getNote,
|
||||
updateNote,
|
||||
@@ -204,5 +208,6 @@ module.exports = {
|
||||
setNoteTypeMime,
|
||||
getRelationMap,
|
||||
changeTitle,
|
||||
duplicateSubtree
|
||||
duplicateSubtree,
|
||||
eraseDeletedNotesNow
|
||||
};
|
||||
|
||||
@@ -23,7 +23,8 @@ function getRecentChanges(req) {
|
||||
note_revisions.dateCreated AS date
|
||||
FROM
|
||||
note_revisions
|
||||
JOIN notes USING(noteId)`);
|
||||
JOIN notes USING(noteId)
|
||||
WHERE note_revisions.isErased = 0`);
|
||||
|
||||
for (const noteRevision of noteRevisions) {
|
||||
if (noteCacheService.isInAncestor(noteRevision.noteId, ancestorNoteId)) {
|
||||
|
||||
@@ -38,6 +38,8 @@ function saveSyncSeed(req) {
|
||||
}]
|
||||
}
|
||||
|
||||
log.info("Saved sync seed.");
|
||||
|
||||
sqlInit.createDatabaseForSync(options);
|
||||
}
|
||||
|
||||
|
||||
@@ -5,14 +5,15 @@ const optionService = require('../../services/options');
|
||||
const treeService = require('../../services/tree');
|
||||
|
||||
function getNotesAndBranchesAndAttributes(noteIds) {
|
||||
noteIds = Array.from(new Set(noteIds));
|
||||
const notes = treeService.getNotes(noteIds);
|
||||
const notes = treeService.getNotesIncludingAscendants(noteIds);
|
||||
|
||||
noteIds = notes.map(note => note.noteId);
|
||||
noteIds = new Set(notes.map(note => note.noteId));
|
||||
|
||||
sql.fillNoteIdList(noteIds);
|
||||
|
||||
// joining child note to filter out not completely synchronised notes which would then cause errors later
|
||||
// cannot do that with parent because of root note's 'none' parent
|
||||
const branches = sql.getManyRows(`
|
||||
const branches = sql.getRows(`
|
||||
SELECT
|
||||
branches.branchId,
|
||||
branches.noteId,
|
||||
@@ -20,28 +21,45 @@ function getNotesAndBranchesAndAttributes(noteIds) {
|
||||
branches.notePosition,
|
||||
branches.prefix,
|
||||
branches.isExpanded
|
||||
FROM branches
|
||||
FROM param_list
|
||||
JOIN branches ON param_list.paramId = branches.noteId OR param_list.paramId = branches.parentNoteId
|
||||
JOIN notes AS child ON child.noteId = branches.noteId
|
||||
WHERE branches.isDeleted = 0
|
||||
AND (branches.noteId IN (???) OR parentNoteId IN (???))`, noteIds);
|
||||
WHERE branches.isDeleted = 0`);
|
||||
|
||||
const attributes = sql.getRows(`
|
||||
SELECT
|
||||
attributes.attributeId,
|
||||
attributes.noteId,
|
||||
attributes.type,
|
||||
attributes.name,
|
||||
attributes.value,
|
||||
attributes.position,
|
||||
attributes.isInheritable
|
||||
FROM param_list
|
||||
JOIN attributes ON attributes.noteId = param_list.paramId
|
||||
OR (attributes.type = 'relation' AND attributes.value = param_list.paramId)
|
||||
WHERE attributes.isDeleted = 0`);
|
||||
|
||||
// we don't really care about the direction of the relation
|
||||
const missingTemplateNoteIds = attributes
|
||||
.filter(attr => attr.type === 'relation'
|
||||
&& attr.name === 'template'
|
||||
&& !noteIds.has(attr.value))
|
||||
.map(attr => attr.value);
|
||||
|
||||
if (missingTemplateNoteIds.length > 0) {
|
||||
const templateData = getNotesAndBranchesAndAttributes(missingTemplateNoteIds);
|
||||
|
||||
// there are going to be duplicates with simple concatenation, however:
|
||||
// 1) shouldn't matter for the frontend which will update the entity twice
|
||||
// 2) there shouldn't be many duplicates. There isn't that many templates
|
||||
addArrays(notes, templateData.notes);
|
||||
addArrays(branches, templateData.branches);
|
||||
addArrays(attributes, templateData.attributes);
|
||||
}
|
||||
|
||||
// sorting in memory is faster
|
||||
branches.sort((a, b) => a.notePosition - b.notePosition < 0 ? -1 : 1);
|
||||
|
||||
const attributes = sql.getManyRows(`
|
||||
SELECT
|
||||
attributeId,
|
||||
noteId,
|
||||
type,
|
||||
name,
|
||||
value,
|
||||
position,
|
||||
isInheritable
|
||||
FROM attributes
|
||||
WHERE isDeleted = 0
|
||||
AND (noteId IN (???) OR (type = 'relation' AND value IN (???)))`, noteIds);
|
||||
|
||||
// sorting in memory is faster
|
||||
attributes.sort((a, b) => a.position - b.position < 0 ? -1 : 1);
|
||||
|
||||
return {
|
||||
@@ -51,6 +69,16 @@ function getNotesAndBranchesAndAttributes(noteIds) {
|
||||
};
|
||||
}
|
||||
|
||||
// should be fast based on https://stackoverflow.com/a/64826145/944162
|
||||
// in this case it is assumed that target is potentially much larger than elementsToAdd
|
||||
function addArrays(target, elementsToAdd) {
|
||||
while (elementsToAdd.length) {
|
||||
target.push(elementsToAdd.shift());
|
||||
}
|
||||
|
||||
return target;
|
||||
}
|
||||
|
||||
function getTree(req) {
|
||||
const subTreeNoteId = req.query.subTreeNoteId || 'root';
|
||||
|
||||
@@ -64,25 +92,8 @@ function getTree(req) {
|
||||
JOIN treeWithDescendants ON branches.parentNoteId = treeWithDescendants.noteId
|
||||
WHERE treeWithDescendants.isExpanded = 1
|
||||
AND branches.isDeleted = 0
|
||||
),
|
||||
treeWithDescendantsAndAscendants AS (
|
||||
SELECT noteId FROM treeWithDescendants
|
||||
UNION
|
||||
SELECT branches.parentNoteId FROM branches
|
||||
JOIN treeWithDescendantsAndAscendants ON branches.noteId = treeWithDescendantsAndAscendants.noteId
|
||||
WHERE branches.isDeleted = 0
|
||||
AND branches.parentNoteId != ?
|
||||
),
|
||||
treeWithDescendantsAscendantsAndTemplates AS (
|
||||
SELECT noteId FROM treeWithDescendantsAndAscendants
|
||||
UNION
|
||||
SELECT attributes.value FROM attributes
|
||||
JOIN treeWithDescendantsAscendantsAndTemplates ON attributes.noteId = treeWithDescendantsAscendantsAndTemplates.noteId
|
||||
WHERE attributes.isDeleted = 0
|
||||
AND attributes.type = 'relation'
|
||||
AND attributes.name = 'template'
|
||||
)
|
||||
SELECT noteId FROM treeWithDescendantsAscendantsAndTemplates`, [subTreeNoteId, subTreeNoteId]);
|
||||
SELECT noteId FROM treeWithDescendants`, [subTreeNoteId]);
|
||||
|
||||
noteIds.push(subTreeNoteId);
|
||||
|
||||
|
||||
@@ -154,6 +154,7 @@ function register(app) {
|
||||
route(GET, '/api/notes/:noteId/revisions/:noteRevisionId/download', [auth.checkApiAuthOrElectron], noteRevisionsApiRoute.downloadNoteRevision);
|
||||
apiRoute(PUT, '/api/notes/:noteId/restore-revision/:noteRevisionId', noteRevisionsApiRoute.restoreNoteRevision);
|
||||
apiRoute(POST, '/api/notes/relation-map', notesApiRoute.getRelationMap);
|
||||
apiRoute(POST, '/api/notes/erase-deleted-notes-now', notesApiRoute.eraseDeletedNotesNow);
|
||||
apiRoute(PUT, '/api/notes/:noteId/change-title', notesApiRoute.changeTitle);
|
||||
apiRoute(POST, '/api/notes/:noteId/duplicate/:parentNoteId', notesApiRoute.duplicateSubtree);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user