...
We add a new row in a database sync_parent_object_hashcode table for each object pulled from its Parent instance. We keep only the most current hash code of each object.
PULL:
- At first, when we pull an object from Parent to Child instance (where it does not exist yet):
- calculate hash code of an object pulled from Parent,
- look for an object on Child instance with UUID of pulled object,
- such object on Child instance does not exist, so object is created on Child instance,
- object's hash code is saved in Child's database as the latest version of this object on Parent instance,
- there is NO conflict.
- Object is NOT modified on Parent instance.
- When we pull the same object from Parent the second time (this object already exist on Child instance):
- calculate hash code of an object pulled from Parent,
- look for an object on Child instance with UUID of pulled object,
- such object on Child instance exists, so compare calculated hash code with a hash code that is saved in Child's database,
- they are equal, which means that object on Child instance is up to date with corresponding object on Parent instance,
- there is NO conflict.
- Object is modified both on Parent and/or Child instance.
- When we pull the same object from Parent the third time (this object already exist on Child instance):
- calculate hash code of an object pulled from Parent,
- look for an object on Child instance with UUID of pulled object,
- such object on Child instance exists, so compare calculated hash code with a hash code that is saved in Child's database,
- they are NOT equal, which means that object on Child instance is NOT up to date with corresponding object on Parent instance
- there is a conflict, which is resolved using rule ConflictResolution.RULE 1
PUSH:
...
- In SyncPushServiceImpl class during execution of readAndPushObjectToParent method we call shouldPushObject method, where we pull corresponding object from Parent instance,
- this object does not exist on Parent instance yet, shouldPushObject returns true,
- object is pushed to Parent,
- object's hash code is saved in Child's database as the latest version of this object on Parent instance.
...
- In SyncPushServiceImpl class during execution of readAndPushObjectToParent method we call shouldPushObject method, where we pull corresponding object from Parent instance,
- this object already exists on Parent instance,
- calculate hash code of an object pulled from Parent,
- compare calculated hash code with a hash code that is saved in Child's database,
- they are equal, which means that Child modified the latest version of object on Parent instance,
- object is pushed to Parent,
- object's hash code is updated in Child's database as the last version of this object.
...
Conflict detection is based on hash codes. Every object has its own Sync hash code which can be generated basing on that object. During pull or push operation a child instance compares hash codes of a local object, parent's object and recently saved parent's object. The hash code of recently saved parent's object is persisted in the database. We have to establish some shortcuts before we explain when the conflict is detected:
- #C : a hash code of the child instance object
- #P : a hash code of the parent instance object
- #lastP : a last saved hash code of the parent instance object.
The conflict is detected when (#lastP != #P) && (#C != #P).
...
Conflict Resolution
PULL:
- RULE 1
If hash code of an object on Parent instance is NOT equal to hash code of an object on Child instance* it means that there is a new version of this object on Parent →- if object on Child instance was NOT modified (calculated hash code of modified object is equal to hash code saved on Child's instance*) →
- void current object's fields on Child instance,
- save fields of object from Parent instance to Child.
- if object on Child instance was modified (calculated hash code of modified object is NOT equal to hash code saved on Child's instance*) →
- these two objects have to be merged**, then
- saved on Child and Parent instance;
- hash codes on both Child and Parent instance have to be updated.
- if object on Child instance was NOT modified (calculated hash code of modified object is equal to hash code saved on Child's instance*) →
...