sync fixes and refactorings

This commit is contained in:
zadam 2023-07-29 23:25:02 +02:00
parent 04b125afc0
commit e8b52f9e6c
12 changed files with 36 additions and 42 deletions

View File

@ -61,8 +61,8 @@ class AbstractBeccaEntity {
} }
/** @protected */ /** @protected */
addEntityChange(isDeleted = false) { putEntityChange(isDeleted = false) {
entityChangesService.addEntityChange({ entityChangesService.putEntityChange({
entityName: this.constructor.entityName, entityName: this.constructor.entityName,
entityId: this[this.constructor.primaryKeyName], entityId: this[this.constructor.primaryKeyName],
hash: this.generateHash(isDeleted), hash: this.generateHash(isDeleted),
@ -101,7 +101,7 @@ class AbstractBeccaEntity {
return; return;
} }
this.addEntityChange(false); this.putEntityChange(false);
if (!cls.isEntityEventsDisabled()) { if (!cls.isEntityEventsDisabled()) {
const eventPayload = { const eventPayload = {
@ -219,7 +219,7 @@ class AbstractBeccaEntity {
// access to the decrypted content // access to the decrypted content
const hash = blobService.calculateContentHash(pojo); const hash = blobService.calculateContentHash(pojo);
entityChangesService.addEntityChange({ entityChangesService.putEntityChange({
entityName: 'blobs', entityName: 'blobs',
entityId: newBlobId, entityId: newBlobId,
hash: hash, hash: hash,
@ -279,7 +279,7 @@ class AbstractBeccaEntity {
log.info(`Marking ${entityName} ${entityId} as deleted`); log.info(`Marking ${entityName} ${entityId} as deleted`);
this.addEntityChange(true); this.putEntityChange(true);
eventService.emit(eventService.ENTITY_DELETED, { entityName, entityId, entity: this }); eventService.emit(eventService.ENTITY_DELETED, { entityName, entityId, entity: this });
} }
@ -296,7 +296,7 @@ class AbstractBeccaEntity {
log.info(`Marking ${entityName} ${entityId} as deleted`); log.info(`Marking ${entityName} ${entityId} as deleted`);
this.addEntityChange(true); this.putEntityChange(true);
eventService.emit(eventService.ENTITY_DELETED, { entityName, entityId, entity: this }); eventService.emit(eventService.ENTITY_DELETED, { entityName, entityId, entity: this });
} }

View File

@ -75,7 +75,7 @@ function register(router) {
eu.route(router, 'post' ,'/etapi/refresh-note-ordering/:parentNoteId', (req, res, next) => { eu.route(router, 'post' ,'/etapi/refresh-note-ordering/:parentNoteId', (req, res, next) => {
eu.getAndCheckNote(req.params.parentNoteId); eu.getAndCheckNote(req.params.parentNoteId);
entityChangesService.addNoteReorderingEntityChange(req.params.parentNoteId, "etapi"); entityChangesService.putNoteReorderingEntityChange(req.params.parentNoteId, "etapi");
res.sendStatus(204); res.sendStatus(204);
}); });

View File

@ -72,7 +72,7 @@ function moveBranchBeforeNote(req) {
treeService.sortNotesIfNeeded(parentNote.noteId); treeService.sortNotesIfNeeded(parentNote.noteId);
// if sorting is not needed, then still the ordering might have changed above manually // if sorting is not needed, then still the ordering might have changed above manually
entityChangesService.addNoteReorderingEntityChange(parentNote.noteId); entityChangesService.putNoteReorderingEntityChange(parentNote.noteId);
log.info(`Moved note ${branchToMove.noteId}, branch ${branchId} before note ${beforeBranch.noteId}, branch ${beforeBranchId}`); log.info(`Moved note ${branchToMove.noteId}, branch ${branchId} before note ${beforeBranch.noteId}, branch ${beforeBranchId}`);
@ -123,7 +123,7 @@ function moveBranchAfterNote(req) {
treeService.sortNotesIfNeeded(parentNote.noteId); treeService.sortNotesIfNeeded(parentNote.noteId);
// if sorting is not needed, then still the ordering might have changed above manually // if sorting is not needed, then still the ordering might have changed above manually
entityChangesService.addNoteReorderingEntityChange(parentNote.noteId); entityChangesService.putNoteReorderingEntityChange(parentNote.noteId);
log.info(`Moved note ${branchToMove.noteId}, branch ${branchId} after note ${afterNote.noteId}, branch ${afterBranchId}`); log.info(`Moved note ${branchToMove.noteId}, branch ${branchId} after note ${afterNote.noteId}, branch ${afterBranchId}`);

View File

@ -161,7 +161,7 @@ function cloneNoteAfter(noteId, afterBranchId) {
sql.execute("UPDATE branches SET notePosition = notePosition + 10 WHERE parentNoteId = ? AND notePosition > ? AND isDeleted = 0", sql.execute("UPDATE branches SET notePosition = notePosition + 10 WHERE parentNoteId = ? AND notePosition > ? AND isDeleted = 0",
[afterNote.parentNoteId, afterNote.notePosition]); [afterNote.parentNoteId, afterNote.notePosition]);
eventChangesService.addNoteReorderingEntityChange(afterNote.parentNoteId); eventChangesService.putNoteReorderingEntityChange(afterNote.parentNoteId);
const branch = new BBranch({ const branch = new BBranch({
noteId: noteId, noteId: noteId,

View File

@ -56,7 +56,7 @@ function getAndClearEntityChangeIds() {
return entityChangeIds; return entityChangeIds;
} }
function addEntityChange(entityChange) { function putEntityChange(entityChange) {
if (namespace.get('ignoreEntityChangeIds')) { if (namespace.get('ignoreEntityChangeIds')) {
return; return;
} }
@ -91,6 +91,6 @@ module.exports = {
isEntityEventsDisabled, isEntityEventsDisabled,
reset, reset,
getAndClearEntityChangeIds, getAndClearEntityChangeIds,
addEntityChange, putEntityChange,
ignoreEntityChangeIds, ignoreEntityChangeIds,
}; };

View File

@ -414,7 +414,7 @@ class ConsistencyChecks {
const hash = utils.hash(utils.randomString(10)); const hash = utils.hash(utils.randomString(10));
entityChangesService.addEntityChange({ entityChangesService.putEntityChange({
entityName: 'blobs', entityName: 'blobs',
entityId: blobId, entityId: blobId,
hash: hash, hash: hash,
@ -605,7 +605,7 @@ class ConsistencyChecks {
const entityRow = sql.getRow(`SELECT * FROM ${entityName} WHERE ${key} = ?`, [entityId]); const entityRow = sql.getRow(`SELECT * FROM ${entityName} WHERE ${key} = ?`, [entityId]);
if (this.autoFix) { if (this.autoFix) {
entityChangesService.addEntityChange({ entityChangesService.putEntityChange({
entityName, entityName,
entityId, entityId,
hash: utils.randomString(10), // doesn't matter, will force sync, but that's OK hash: utils.randomString(10), // doesn't matter, will force sync, but that's OK

View File

@ -31,10 +31,6 @@ function getEntityHashes() {
const sector = entityId[0]; const sector = entityId[0];
if (entityName === 'revisions' && sector === '5') {
console.log(entityId, hash, isErased);
}
// if the entity is erased, its hash is not updated, so it has to be added extra // if the entity is erased, its hash is not updated, so it has to be added extra
entityHashMap[sector] = (entityHashMap[sector] || "") + hash + isErased; entityHashMap[sector] = (entityHashMap[sector] || "") + hash + isErased;
} }

View File

@ -9,13 +9,13 @@ const blobService = require("../services/blob");
let maxEntityChangeId = 0; let maxEntityChangeId = 0;
function addEntityChangeWithInstanceId(origEntityChange, instanceId) { function putEntityChangeWithInstanceId(origEntityChange, instanceId) {
const ec = {...origEntityChange, instanceId}; const ec = {...origEntityChange, instanceId};
return addEntityChange(ec); return putEntityChange(ec);
} }
function addEntityChange(origEntityChange) { function putEntityChange(origEntityChange) {
const ec = {...origEntityChange}; const ec = {...origEntityChange};
delete ec.id; delete ec.id;
@ -32,11 +32,11 @@ function addEntityChange(origEntityChange) {
maxEntityChangeId = Math.max(maxEntityChangeId, ec.id); maxEntityChangeId = Math.max(maxEntityChangeId, ec.id);
cls.addEntityChange(ec); cls.putEntityChange(ec);
} }
function addNoteReorderingEntityChange(parentNoteId, componentId) { function putNoteReorderingEntityChange(parentNoteId, componentId) {
addEntityChange({ putEntityChange({
entityName: "note_reordering", entityName: "note_reordering",
entityId: parentNoteId, entityId: parentNoteId,
hash: 'N/A', hash: 'N/A',
@ -55,10 +55,8 @@ function addNoteReorderingEntityChange(parentNoteId, componentId) {
}); });
} }
function moveEntityChangeToTop(entityName, entityId) { function putEntityChangeForOtherInstances(ec) {
const ec = sql.getRow(`SELECT * FROM entity_changes WHERE entityName = ? AND entityId = ?`, [entityName, entityId]); putEntityChangeWithInstanceId(ec, null);
addEntityChange(ec);
} }
function addEntityChangesForSector(entityName, sector) { function addEntityChangesForSector(entityName, sector) {
@ -66,7 +64,7 @@ function addEntityChangesForSector(entityName, sector) {
sql.transactional(() => { sql.transactional(() => {
for (const ec of entityChanges) { for (const ec of entityChanges) {
addEntityChange(ec); putEntityChange(ec);
} }
}); });
@ -128,7 +126,7 @@ function fillEntityChanges(entityName, entityPrimaryKey, condition = '') {
} }
} }
addEntityChange(ec); putEntityChange(ec);
} }
if (createdCount > 0) { if (createdCount > 0) {
@ -157,10 +155,10 @@ function recalculateMaxEntityChangeId() {
} }
module.exports = { module.exports = {
addNoteReorderingEntityChange, putNoteReorderingEntityChange,
moveEntityChangeToTop, putEntityChangeForOtherInstances,
addEntityChange, putEntityChange,
addEntityChangeWithInstanceId, putEntityChangeWithInstanceId,
fillAllEntityChanges, fillAllEntityChanges,
addEntityChangesForSector, addEntityChangesForSector,
getMaxEntityChangeId: () => maxEntityChangeId, getMaxEntityChangeId: () => maxEntityChangeId,

View File

@ -39,7 +39,7 @@ function setEntityChangesAsErased(entityChanges) {
ec.isErased = true; ec.isErased = true;
ec.utcDateChanged = dateUtils.utcNowDateTime(); ec.utcDateChanged = dateUtils.utcNowDateTime();
entityChangesService.addEntityChange(ec); entityChangesService.putEntityChange(ec);
} }
} }

View File

@ -265,7 +265,7 @@ function createNewNoteWithTarget(target, targetBranchId, params) {
const retObject = createNewNote(params); const retObject = createNewNote(params);
entityChangesService.addNoteReorderingEntityChange(params.parentNoteId); entityChangesService.putNoteReorderingEntityChange(params.parentNoteId);
return retObject; return retObject;
} }

View File

@ -38,7 +38,7 @@ function updateNormalEntity(remoteEC, remoteEntityRow, instanceId) {
return true; return true;
} else if (localEC?.isErased && !remoteEC.isErased) { } else if (localEC?.isErased && !remoteEC.isErased) {
// on this side, we can't unerase the entity, so force the entity to be erased on the other side. // on this side, we can't unerase the entity, so force the entity to be erased on the other side.
entityChangesService.addEntityChangeWithInstanceId(localEC, null); entityChangesService.putEntityChangeForOtherInstances(localEC);
return false; return false;
} }
@ -62,12 +62,12 @@ function updateNormalEntity(remoteEC, remoteEntityRow, instanceId) {
sql.replace(remoteEC.entityName, remoteEntityRow); sql.replace(remoteEC.entityName, remoteEntityRow);
entityChangesService.addEntityChangeWithInstanceId(remoteEC, instanceId); entityChangesService.putEntityChangeWithInstanceId(remoteEC, instanceId);
return true; return true;
} else if (localEC.hash !== remoteEC.hash && localEC.utcDateChanged > remoteEC.utcDateChanged) { } else if (localEC.hash !== remoteEC.hash && localEC.utcDateChanged > remoteEC.utcDateChanged) {
// the change on our side is newer than on the other side, so the other side should update // the change on our side is newer than on the other side, so the other side should update
entityChangesService.addEntityChangeWithInstanceId(localEC, null); entityChangesService.putEntityChangeForOtherInstances(localEC);
return false; return false;
} }
@ -80,7 +80,7 @@ function updateNoteReordering(remoteEC, remoteEntityRow, instanceId) {
sql.execute("UPDATE branches SET notePosition = ? WHERE branchId = ?", [remoteEntityRow[key], key]); sql.execute("UPDATE branches SET notePosition = ? WHERE branchId = ?", [remoteEntityRow[key], key]);
} }
entityChangesService.addEntityChangeWithInstanceId(remoteEC, instanceId); entityChangesService.putEntityChangeWithInstanceId(remoteEC, instanceId);
return true; return true;
} }
@ -106,7 +106,7 @@ function eraseEntity(entityChange, instanceId) {
sql.execute(`DELETE FROM ${entityName} WHERE ${primaryKeyName} = ?`, [entityId]); sql.execute(`DELETE FROM ${entityName} WHERE ${primaryKeyName} = ?`, [entityId]);
entityChangesService.addEntityChangeWithInstanceId(entityChange, instanceId); entityChangesService.putEntityChangeWithInstanceId(entityChange, instanceId);
} }
module.exports = { module.exports = {

View File

@ -165,7 +165,7 @@ function sortNotes(parentNoteId, customSortBy = 'title', reverse = false, folder
} }
if (someBranchUpdated) { if (someBranchUpdated) {
entityChangesService.addNoteReorderingEntityChange(parentNoteId); entityChangesService.putNoteReorderingEntityChange(parentNoteId);
} }
}); });
} }