Skip to content

Commit

Permalink
Optimize more: allow replace when better to do so
Browse files Browse the repository at this point in the history
  • Loading branch information
LiquidFenrir committed Dec 18, 2023
1 parent 8b3bfc9 commit ca52c16
Showing 1 changed file with 22 additions and 12 deletions.
34 changes: 22 additions & 12 deletions src/main/java/org/lsc/beans/BeanComparator.java
Original file line number Diff line number Diff line change
Expand Up @@ -321,22 +321,32 @@ private static LscModifications getUpdatedObject(

case REPLACE_VALUES:
if (attrStatus == PolicyType.FORCE) {
multiMi = new ArrayList<LscDatasetModification>(2);

// check if there are any extra values to be added
Set<Object> missingValues = SetUtils.findMissingNeedles(dstAttrValues, toSetAttrValues);
if (missingValues.size() > 0) {
LOGGER.debug("{} Adding values to attribute \"{}\": new values are {}",
new Object[]{logPrefix, attrName, missingValues});
multiMi.add(new LscDatasetModification(LscDatasetModificationType.ADD_VALUES, dstAttr.getID(), missingValues));
}

// check if there are any extra values to be removed
Set<Object> extraValues = SetUtils.findMissingNeedles(toSetAttrValues, dstAttrValues);
if (extraValues.size() > 0) {
LOGGER.debug("{} Removing values from attribute \"{}\": old values are {}",
new Object[]{logPrefix, attrName, extraValues});
multiMi.add(new LscDatasetModification(LscDatasetModificationType.DELETE_VALUES, dstAttr.getID(), extraValues));

if((missingValues.size() + extraValues.size()) >= toSetAttrValues.size()) {
// More things to add and delete than remaining in the final set
// so, replace with the final set directly.
LOGGER.debug("{} Replacing attribute \"{}\": source values are {}, old values were {}, new values are {}",
new Object[]{logPrefix, attrName, srcAttrValues, dstAttrValues, toSetAttrValues});
mi = new LscDatasetModification(operationType, dstAttr.getID(), toSetAttrValues);
} else {
// Adding and deleting the values is less expensive than replacing everything
multiMi = new ArrayList<LscDatasetModification>(2);

if (missingValues.size() > 0) {
LOGGER.debug("{} Adding values to attribute \"{}\": new values are {}",
new Object[]{logPrefix, attrName, missingValues});
multiMi.add(new LscDatasetModification(LscDatasetModificationType.ADD_VALUES, dstAttr.getID(), missingValues));
}

if (extraValues.size() > 0) {
LOGGER.debug("{} Removing values from attribute \"{}\": old values are {}",
new Object[]{logPrefix, attrName, extraValues});
multiMi.add(new LscDatasetModification(LscDatasetModificationType.DELETE_VALUES, dstAttr.getID(), extraValues));
}
}
} else if (attrStatus == PolicyType.MERGE) {
// check if there are any extra values to be added
Expand Down

0 comments on commit ca52c16

Please sign in to comment.