Skip to content

Commit

Permalink
sync fixes and refactorings
Browse files Browse the repository at this point in the history
  • Loading branch information
zadam committed Jul 29, 2023
1 parent 04b125a commit e8b52f9
Show file tree
Hide file tree
Showing 12 changed files with 36 additions and 42 deletions.
12 changes: 6 additions & 6 deletions src/becca/entities/abstract_becca_entity.js
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down Expand Up @@ -101,7 +101,7 @@ class AbstractBeccaEntity {
return;
}

this.addEntityChange(false);
this.putEntityChange(false);

if (!cls.isEntityEventsDisabled()) {
const eventPayload = {
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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 });
}
Expand All @@ -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 });
}
Expand Down
2 changes: 1 addition & 1 deletion src/etapi/branches.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
Expand Down
4 changes: 2 additions & 2 deletions src/routes/api/branches.js
Original file line number Diff line number Diff line change
Expand Up @@ -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}`);

Expand Down Expand Up @@ -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}`);

Expand Down
2 changes: 1 addition & 1 deletion src/services/cloning.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
4 changes: 2 additions & 2 deletions src/services/cls.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ function getAndClearEntityChangeIds() {
return entityChangeIds;
}

function addEntityChange(entityChange) {
function putEntityChange(entityChange) {
if (namespace.get('ignoreEntityChangeIds')) {
return;
}
Expand Down Expand Up @@ -91,6 +91,6 @@ module.exports = {
isEntityEventsDisabled,
reset,
getAndClearEntityChangeIds,
addEntityChange,
putEntityChange,
ignoreEntityChangeIds,
};
4 changes: 2 additions & 2 deletions src/services/consistency_checks.js
Original file line number Diff line number Diff line change
Expand Up @@ -414,7 +414,7 @@ class ConsistencyChecks {

const hash = utils.hash(utils.randomString(10));

entityChangesService.addEntityChange({
entityChangesService.putEntityChange({
entityName: 'blobs',
entityId: blobId,
hash: hash,
Expand Down Expand Up @@ -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
Expand Down
4 changes: 0 additions & 4 deletions src/services/content_hash.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
30 changes: 14 additions & 16 deletions src/services/entity_changes.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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',
Expand All @@ -55,18 +55,16 @@ 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) {
const entityChanges = sql.getRows(`SELECT * FROM entity_changes WHERE entityName = ? AND SUBSTR(entityId, 1, 1) = ?`, [entityName, sector]);

sql.transactional(() => {
for (const ec of entityChanges) {
addEntityChange(ec);
putEntityChange(ec);
}
});

Expand Down Expand Up @@ -128,7 +126,7 @@ function fillEntityChanges(entityName, entityPrimaryKey, condition = '') {
}
}

addEntityChange(ec);
putEntityChange(ec);
}

if (createdCount > 0) {
Expand Down Expand Up @@ -157,10 +155,10 @@ function recalculateMaxEntityChangeId() {
}

module.exports = {
addNoteReorderingEntityChange,
moveEntityChangeToTop,
addEntityChange,
addEntityChangeWithInstanceId,
putNoteReorderingEntityChange,
putEntityChangeForOtherInstances,
putEntityChange,
putEntityChangeWithInstanceId,
fillAllEntityChanges,
addEntityChangesForSector,
getMaxEntityChangeId: () => maxEntityChangeId,
Expand Down
2 changes: 1 addition & 1 deletion src/services/erase.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ function setEntityChangesAsErased(entityChanges) {
ec.isErased = true;
ec.utcDateChanged = dateUtils.utcNowDateTime();

entityChangesService.addEntityChange(ec);
entityChangesService.putEntityChange(ec);
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/services/notes.js
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@ function createNewNoteWithTarget(target, targetBranchId, params) {

const retObject = createNewNote(params);

entityChangesService.addNoteReorderingEntityChange(params.parentNoteId);
entityChangesService.putNoteReorderingEntityChange(params.parentNoteId);

return retObject;
}
Expand Down
10 changes: 5 additions & 5 deletions src/services/sync_update.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -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;
}
Expand All @@ -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;
}
Expand All @@ -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 = {
Expand Down
2 changes: 1 addition & 1 deletion src/services/tree.js
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ function sortNotes(parentNoteId, customSortBy = 'title', reverse = false, folder
}

if (someBranchUpdated) {
entityChangesService.addNoteReorderingEntityChange(parentNoteId);
entityChangesService.putNoteReorderingEntityChange(parentNoteId);
}
});
}
Expand Down

0 comments on commit e8b52f9

Please sign in to comment.