renamed remnants of "sync" to "entity_change"

This commit is contained in:
zadam 2020-12-16 22:17:42 +01:00
parent cdc2721ac1
commit 1cc7917b6e
3 changed files with 68 additions and 61 deletions

View file

@ -144,7 +144,7 @@ async function consumeSyncData() {
const allEntityChanges = syncDataQueue;
syncDataQueue = [];
const nonProcessedEntityChanges = allEntityChanges.filter(sync => !processedEntityChangeIds.has(sync.id));
const nonProcessedEntityChanges = allEntityChanges.filter(ec => !processedEntityChangeIds.has(ec.id));
try {
await utils.timeLimit(processEntityChanges(nonProcessedEntityChanges), 30000);
@ -251,33 +251,33 @@ async function processEntityChanges(entityChanges) {
const loadResults = new LoadResults(treeCache);
for (const sync of entityChanges.filter(sync => sync.entityName === 'notes')) {
const note = treeCache.notes[sync.entityId];
for (const ec of entityChanges.filter(ec => ec.entityName === 'notes')) {
const note = treeCache.notes[ec.entityId];
if (note) {
note.update(sync.entity);
loadResults.addNote(sync.entityId, sync.sourceId);
note.update(ec.entity);
loadResults.addNote(ec.entityId, ec.sourceId);
}
}
for (const sync of entityChanges.filter(sync => sync.entityName === 'branches')) {
let branch = treeCache.branches[sync.entityId];
const childNote = treeCache.notes[sync.entity.noteId];
const parentNote = treeCache.notes[sync.entity.parentNoteId];
for (const ec of entityChanges.filter(ec => ec.entityName === 'branches')) {
let branch = treeCache.branches[ec.entityId];
const childNote = treeCache.notes[ec.entity.noteId];
const parentNote = treeCache.notes[ec.entity.parentNoteId];
if (branch) {
branch.update(sync.entity);
loadResults.addBranch(sync.entityId, sync.sourceId);
branch.update(ec.entity);
loadResults.addBranch(ec.entityId, ec.sourceId);
if (sync.entity.isDeleted) {
if (ec.entity.isDeleted) {
if (childNote) {
childNote.parents = childNote.parents.filter(parentNoteId => parentNoteId !== sync.entity.parentNoteId);
delete childNote.parentToBranch[sync.entity.parentNoteId];
childNote.parents = childNote.parents.filter(parentNoteId => parentNoteId !== ec.entity.parentNoteId);
delete childNote.parentToBranch[ec.entity.parentNoteId];
}
if (parentNote) {
parentNote.children = parentNote.children.filter(childNoteId => childNoteId !== sync.entity.noteId);
delete parentNote.childToBranch[sync.entity.noteId];
parentNote.children = parentNote.children.filter(childNoteId => childNoteId !== ec.entity.noteId);
delete parentNote.childToBranch[ec.entity.noteId];
}
}
else {
@ -290,12 +290,12 @@ async function processEntityChanges(entityChanges) {
}
}
}
else if (!sync.entity.isDeleted) {
else if (!ec.entity.isDeleted) {
if (childNote || parentNote) {
branch = new Branch(treeCache, sync.entity);
branch = new Branch(treeCache, ec.entity);
treeCache.branches[branch.branchId] = branch;
loadResults.addBranch(sync.entityId, sync.sourceId);
loadResults.addBranch(ec.entityId, ec.sourceId);
if (childNote) {
childNote.addParent(branch.parentNoteId, branch.branchId);
@ -308,14 +308,14 @@ async function processEntityChanges(entityChanges) {
}
}
for (const sync of entityChanges.filter(sync => sync.entityName === 'note_reordering')) {
for (const ec of entityChanges.filter(ec => ec.entityName === 'note_reordering')) {
const parentNoteIdsToSort = new Set();
for (const branchId in sync.positions) {
for (const branchId in ec.positions) {
const branch = treeCache.branches[branchId];
if (branch) {
branch.notePosition = sync.positions[branchId];
branch.notePosition = ec.positions[branchId];
parentNoteIdsToSort.add(branch.parentNoteId);
}
@ -329,20 +329,20 @@ async function processEntityChanges(entityChanges) {
}
}
loadResults.addNoteReordering(sync.entityId, sync.sourceId);
loadResults.addNoteReordering(ec.entityId, ec.sourceId);
}
// missing reloading the relation target note
for (const sync of entityChanges.filter(sync => sync.entityName === 'attributes')) {
let attribute = treeCache.attributes[sync.entityId];
const sourceNote = treeCache.notes[sync.entity.noteId];
const targetNote = sync.entity.type === 'relation' && treeCache.notes[sync.entity.value];
for (const ec of entityChanges.filter(ec => ec.entityName === 'attributes')) {
let attribute = treeCache.attributes[ec.entityId];
const sourceNote = treeCache.notes[ec.entity.noteId];
const targetNote = ec.entity.type === 'relation' && treeCache.notes[ec.entity.value];
if (attribute) {
attribute.update(sync.entity);
loadResults.addAttribute(sync.entityId, sync.sourceId);
attribute.update(ec.entity);
loadResults.addAttribute(ec.entityId, ec.sourceId);
if (sync.entity.isDeleted) {
if (ec.entity.isDeleted) {
if (sourceNote) {
sourceNote.attributes = sourceNote.attributes.filter(attributeId => attributeId !== attribute.attributeId);
}
@ -352,13 +352,13 @@ async function processEntityChanges(entityChanges) {
}
}
}
else if (!sync.entity.isDeleted) {
else if (!ec.entity.isDeleted) {
if (sourceNote || targetNote) {
attribute = new Attribute(treeCache, sync.entity);
attribute = new Attribute(treeCache, ec.entity);
treeCache.attributes[attribute.attributeId] = attribute;
loadResults.addAttribute(sync.entityId, sync.sourceId);
loadResults.addAttribute(ec.entityId, ec.sourceId);
if (sourceNote && !sourceNote.attributes.includes(attribute.attributeId)) {
sourceNote.attributes.push(attribute.attributeId);
@ -371,24 +371,24 @@ async function processEntityChanges(entityChanges) {
}
}
for (const sync of entityChanges.filter(sync => sync.entityName === 'note_contents')) {
delete treeCache.noteComplementPromises[sync.entityId];
for (const ec of entityChanges.filter(ec => ec.entityName === 'note_contents')) {
delete treeCache.noteComplementPromises[ec.entityId];
loadResults.addNoteContent(sync.entityId, sync.sourceId);
loadResults.addNoteContent(ec.entityId, ec.sourceId);
}
for (const sync of entityChanges.filter(sync => sync.entityName === 'note_revisions')) {
loadResults.addNoteRevision(sync.entityId, sync.noteId, sync.sourceId);
for (const ec of entityChanges.filter(ec => ec.entityName === 'note_revisions')) {
loadResults.addNoteRevision(ec.entityId, ec.noteId, ec.sourceId);
}
for (const sync of entityChanges.filter(sync => sync.entityName === 'options')) {
if (sync.entity.name === 'openTabs') {
for (const ec of entityChanges.filter(ec => ec.entityName === 'options')) {
if (ec.entity.name === 'openTabs') {
continue; // only noise
}
options.set(sync.entity.name, sync.entity.value);
options.set(ec.entity.name, ec.entity.value);
loadResults.addOption(sync.entity.name);
loadResults.addOption(ec.entity.name);
}
if (!loadResults.isEmpty()) {

View file

@ -1,6 +1,7 @@
const sql = require('./sql');
const repository = require('./repository');
const sourceIdService = require('./source_id');
const dateUtils = require('./date_utils');
const log = require('./log');
const cls = require('./cls');
@ -63,8 +64,8 @@ function cleanupEntityChangesForMissingEntities(entityName, entityPrimaryKey) {
FROM entity_changes
WHERE
isErased = 0
AND sync.entityName = '${entityName}'
AND sync.entityId NOT IN (SELECT ${entityPrimaryKey} FROM ${entityName})`);
AND entityName = '${entityName}'
AND entityId NOT IN (SELECT ${entityPrimaryKey} FROM ${entityName})`);
}
function fillEntityChanges(entityName, entityPrimaryKey, condition = '') {
@ -120,7 +121,13 @@ function fillAllEntityChanges() {
}
module.exports = {
addNoteReorderingEntityChange: (parentNoteId, sourceId) => addEntityChange("note_reordering", parentNoteId, '', sourceId),
addNoteReorderingEntityChange: (parentNoteId, sourceId) => addEntityChange({
entityName: "note_reordering",
entityId: parentNoteId,
hash: 'N/A',
isErased: false,
utcDateChanged: dateUtils.utcNowDateTime()
}, sourceId),
moveEntityChangeToTop,
addEntityChange,
fillAllEntityChanges,

View file

@ -71,27 +71,27 @@ function sendMessageToAllClients(message) {
}
}
function fillInAdditionalProperties(sync) {
function fillInAdditionalProperties(entityChange) {
// fill in some extra data needed by the frontend
if (sync.entityName === 'attributes') {
sync.entity = sql.getRow(`SELECT * FROM attributes WHERE attributeId = ?`, [sync.entityId]);
} else if (sync.entityName === 'branches') {
sync.entity = sql.getRow(`SELECT * FROM branches WHERE branchId = ?`, [sync.entityId]);
} else if (sync.entityName === 'notes') {
sync.entity = sql.getRow(`SELECT * FROM notes WHERE noteId = ?`, [sync.entityId]);
if (entityChange.entityName === 'attributes') {
entityChange.entity = sql.getRow(`SELECT * FROM attributes WHERE attributeId = ?`, [entityChange.entityId]);
} else if (entityChange.entityName === 'branches') {
entityChange.entity = sql.getRow(`SELECT * FROM branches WHERE branchId = ?`, [entityChange.entityId]);
} else if (entityChange.entityName === 'notes') {
entityChange.entity = sql.getRow(`SELECT * FROM notes WHERE noteId = ?`, [entityChange.entityId]);
if (sync.entity.isProtected) {
sync.entity.title = protectedSessionService.decryptString(sync.entity.title);
if (entityChange.entity.isProtected) {
entityChange.entity.title = protectedSessionService.decryptString(entityChange.entity.title);
}
} else if (sync.entityName === 'note_revisions') {
sync.noteId = sql.getValue(`SELECT noteId
} else if (entityChange.entityName === 'note_revisions') {
entityChange.noteId = sql.getValue(`SELECT noteId
FROM note_revisions
WHERE noteRevisionId = ?`, [sync.entityId]);
} else if (sync.entityName === 'note_reordering') {
sync.positions = sql.getMap(`SELECT branchId, notePosition FROM branches WHERE isDeleted = 0 AND parentNoteId = ?`, [sync.entityId]);
WHERE noteRevisionId = ?`, [entityChange.entityId]);
} else if (entityChange.entityName === 'note_reordering') {
entityChange.positions = sql.getMap(`SELECT branchId, notePosition FROM branches WHERE isDeleted = 0 AND parentNoteId = ?`, [entityChange.entityId]);
}
else if (sync.entityName === 'options') {
sync.entity = sql.getRow(`SELECT * FROM options WHERE name = ?`, [sync.entityId]);
else if (entityChange.entityName === 'options') {
entityChange.entity = sql.getRow(`SELECT * FROM options WHERE name = ?`, [entityChange.entityId]);
}
}