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 */
addEntityChange(isDeleted = false) {
entityChangesService.addEntityChange({
putEntityChange(isDeleted = false) {
entityChangesService.putEntityChange({
entityName: this.constructor.entityName,
entityId: this[this.constructor.primaryKeyName],
hash: this.generateHash(isDeleted),
@ -101,7 +101,7 @@ class AbstractBeccaEntity {
return;
}
this.addEntityChange(false);
this.putEntityChange(false);
if (!cls.isEntityEventsDisabled()) {
const eventPayload = {
@ -219,7 +219,7 @@ class AbstractBeccaEntity {
// access to the decrypted content
const hash = blobService.calculateContentHash(pojo);
entityChangesService.addEntityChange({
entityChangesService.putEntityChange({
entityName: 'blobs',
entityId: newBlobId,
hash: hash,
@ -279,7 +279,7 @@ class AbstractBeccaEntity {
log.info(`Marking ${entityName} ${entityId} as deleted`);
this.addEntityChange(true);
this.putEntityChange(true);
eventService.emit(eventService.ENTITY_DELETED, { entityName, entityId, entity: this });
}
@ -296,7 +296,7 @@ class AbstractBeccaEntity {
log.info(`Marking ${entityName} ${entityId} as deleted`);
this.addEntityChange(true);
this.putEntityChange(true);
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.getAndCheckNote(req.params.parentNoteId);
entityChangesService.addNoteReorderingEntityChange(req.params.parentNoteId, "etapi");
entityChangesService.putNoteReorderingEntityChange(req.params.parentNoteId, "etapi");
res.sendStatus(204);
});

View File

@ -72,7 +72,7 @@ function moveBranchBeforeNote(req) {
treeService.sortNotesIfNeeded(parentNote.noteId);
// 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}`);
@ -123,7 +123,7 @@ function moveBranchAfterNote(req) {
treeService.sortNotesIfNeeded(parentNote.noteId);
// 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}`);

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",
[afterNote.parentNoteId, afterNote.notePosition]);
eventChangesService.addNoteReorderingEntityChange(afterNote.parentNoteId);
eventChangesService.putNoteReorderingEntityChange(afterNote.parentNoteId);
const branch = new BBranch({
noteId: noteId,

View File

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

View File

@ -414,7 +414,7 @@ class ConsistencyChecks {
const hash = utils.hash(utils.randomString(10));
entityChangesService.addEntityChange({
entityChangesService.putEntityChange({
entityName: 'blobs',
entityId: blobId,
hash: hash,
@ -605,7 +605,7 @@ class ConsistencyChecks {
const entityRow = sql.getRow(`SELECT * FROM ${entityName} WHERE ${key} = ?`, [entityId]);
if (this.autoFix) {
entityChangesService.addEntityChange({
entityChangesService.putEntityChange({
entityName,
entityId,
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];
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
entityHashMap[sector] = (entityHashMap[sector] || "") + hash + isErased;
}

View File

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

View File

@ -39,7 +39,7 @@ function setEntityChangesAsErased(entityChanges) {
ec.isErased = true;
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);
entityChangesService.addNoteReorderingEntityChange(params.parentNoteId);
entityChangesService.putNoteReorderingEntityChange(params.parentNoteId);
return retObject;
}

View File

@ -38,7 +38,7 @@ function updateNormalEntity(remoteEC, remoteEntityRow, instanceId) {
return true;
} 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.
entityChangesService.addEntityChangeWithInstanceId(localEC, null);
entityChangesService.putEntityChangeForOtherInstances(localEC);
return false;
}
@ -62,12 +62,12 @@ function updateNormalEntity(remoteEC, remoteEntityRow, instanceId) {
sql.replace(remoteEC.entityName, remoteEntityRow);
entityChangesService.addEntityChangeWithInstanceId(remoteEC, instanceId);
entityChangesService.putEntityChangeWithInstanceId(remoteEC, instanceId);
return true;
} 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
entityChangesService.addEntityChangeWithInstanceId(localEC, null);
entityChangesService.putEntityChangeForOtherInstances(localEC);
return false;
}
@ -80,7 +80,7 @@ function updateNoteReordering(remoteEC, remoteEntityRow, instanceId) {
sql.execute("UPDATE branches SET notePosition = ? WHERE branchId = ?", [remoteEntityRow[key], key]);
}
entityChangesService.addEntityChangeWithInstanceId(remoteEC, instanceId);
entityChangesService.putEntityChangeWithInstanceId(remoteEC, instanceId);
return true;
}
@ -106,7 +106,7 @@ function eraseEntity(entityChange, instanceId) {
sql.execute(`DELETE FROM ${entityName} WHERE ${primaryKeyName} = ?`, [entityId]);
entityChangesService.addEntityChangeWithInstanceId(entityChange, instanceId);
entityChangesService.putEntityChangeWithInstanceId(entityChange, instanceId);
}
module.exports = {

View File

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