Skip to content

Commit

Permalink
MID-7008: Added early exit to diff* methods in case they are used via…
Browse files Browse the repository at this point in the history
… equals

In order to determine if objects are equals, we do not need full diff of objects
only to know that they are different.
Adds optional exit of diff methods on first occurence of different value.
  • Loading branch information
tonydamage committed Apr 27, 2021
1 parent 12a5711 commit 25c88d9
Show file tree
Hide file tree
Showing 4 changed files with 191 additions and 27 deletions.
Expand Up @@ -586,8 +586,18 @@ public ItemDelta<V,D> diff(Item<V,D> other, @NotNull ParameterizedEquivalenceStr

void diffInternal(Item<V, D> other, Collection<? extends ItemDelta> deltas, boolean rootValuesOnly,
ParameterizedEquivalenceStrategy strategy) {
diffInternal(other, deltas, rootValuesOnly, strategy, false);
}

boolean diffInternal(Item<V, D> other, Collection<? extends ItemDelta> deltas, boolean rootValuesOnly,
ParameterizedEquivalenceStrategy strategy, boolean exitOnDiff) {
ItemDelta delta = createDelta();
if (other == null) {
// Early exit for equals use case
if (exitOnDiff) {
return true;
}

if (delta.getDefinition() == null && this.getDefinition() != null) {
delta.setDefinition(this.getDefinition().clone());
}
Expand All @@ -611,7 +621,11 @@ void diffInternal(Item<V, D> other, Collection<? extends ItemDelta> deltas, bool
if (!rootValuesOnly && thisValue.representsSameValue(otherValue, true)) {
found = true;
// Matching IDs, look inside to figure out internal deltas
((PrismValueImpl) thisValue).diffMatchingRepresentation(otherValue, deltas, strategy);
boolean different = ((PrismValueImpl) thisValue).diffMatchingRepresentation(otherValue, deltas, strategy, exitOnDiff);
if (exitOnDiff && different) {
return true;
}

// No need to process this value again
iterator.remove();
break;
Expand All @@ -624,12 +638,18 @@ void diffInternal(Item<V, D> other, Collection<? extends ItemDelta> deltas, bool
}
}
if (!found) {
if (exitOnDiff) {
return true;
}
// We have the value and the other does not, this is delete of the entire value
delta.addValueToDelete(thisValue.clone());
}
}
// outstandingOtherValues are those values that the other has and we could not
// match them to any of our values. These must be new values to add
if (exitOnDiff && !outstandingOtherValues.isEmpty()) {
return true;
}
for (PrismValue outstandingOtherValue : outstandingOtherValues) {
delta.addValueToAdd(outstandingOtherValue.clone());
}
Expand All @@ -640,6 +660,7 @@ void diffInternal(Item<V, D> other, Collection<? extends ItemDelta> deltas, bool
if (delta != null && !delta.isEmpty()) {
((Collection)deltas).add(delta);
}
return !delta.isEmpty();
}

protected ItemDelta<V,D> fixupDelta(ItemDelta<V, D> delta, Item<V, D> other) {
Expand Down

0 comments on commit 25c88d9

Please sign in to comment.