-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
perf: only write actually changed values to config (#207)
- Loading branch information
Showing
5 changed files
with
319 additions
and
282 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
90 changes: 90 additions & 0 deletions
90
src/main/java/com/aws/greengrass/logmanager/util/ConfigUtil.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
/* | ||
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package com.aws.greengrass.logmanager.util; | ||
|
||
import com.aws.greengrass.config.CaseInsensitiveString; | ||
import com.aws.greengrass.config.Node; | ||
import com.aws.greengrass.config.Topic; | ||
import com.aws.greengrass.config.Topics; | ||
import com.aws.greengrass.config.UnsupportedInputTypeException; | ||
import com.aws.greengrass.config.UpdateBehaviorTree; | ||
import com.aws.greengrass.logging.api.Logger; | ||
import com.aws.greengrass.logging.impl.LogManager; | ||
import edu.umd.cs.findbugs.annotations.NonNull; | ||
|
||
import java.util.HashSet; | ||
import java.util.Map; | ||
import java.util.Objects; | ||
import java.util.Set; | ||
|
||
public final class ConfigUtil { | ||
private static final Logger logger = LogManager.getLogger(ConfigUtil.class); | ||
|
||
private ConfigUtil() { | ||
} | ||
|
||
/** | ||
* Same as topics.updateFromMap, but only makes the update when the value actually changes, skipping any unnecessary | ||
* timestampUpdated events. Ideally this code would exist in Topics, but it isn't, so we need to do this in order to | ||
* maintain compatibility. | ||
* | ||
* @param topics Topics to update with values from the map | ||
* @param newValues the new value to apply | ||
* @param ubt update behavior tree | ||
*/ | ||
public static void updateFromMapWhenChanged(Topics topics, Map<String, Object> newValues, UpdateBehaviorTree ubt) { | ||
Set<CaseInsensitiveString> childrenToRemove = new HashSet<>(topics.children.keySet()); | ||
|
||
newValues.forEach((okey, value) -> { | ||
CaseInsensitiveString key = new CaseInsensitiveString(okey); | ||
childrenToRemove.remove(key); | ||
updateChild(topics, key, value, ubt); | ||
}); | ||
|
||
childrenToRemove.forEach(childName -> { | ||
UpdateBehaviorTree childMergeBehavior = ubt.getChildBehavior(childName.toString()); | ||
|
||
// remove the existing child if its merge behavior is REPLACE | ||
if (childMergeBehavior.getBehavior() == UpdateBehaviorTree.UpdateBehavior.REPLACE) { | ||
topics.remove(topics.children.get(childName)); | ||
} | ||
}); | ||
} | ||
|
||
private static void updateChild(Topics t, CaseInsensitiveString key, Object value, | ||
@NonNull UpdateBehaviorTree mergeBehavior) { | ||
UpdateBehaviorTree childMergeBehavior = mergeBehavior.getChildBehavior(key.toString()); | ||
|
||
Node existingChild = t.children.get(key); | ||
// if new node is a container node | ||
if (value instanceof Map) { | ||
// if existing child is a container node | ||
if (existingChild == null || existingChild instanceof Topics) { | ||
updateFromMapWhenChanged(t.createInteriorChild(key.toString()), (Map) value, childMergeBehavior); | ||
} else { | ||
t.remove(existingChild); | ||
Topics newNode = t.createInteriorChild(key.toString(), mergeBehavior.getTimestampToUse()); | ||
updateFromMapWhenChanged(newNode, (Map) value, childMergeBehavior); | ||
} | ||
// if new node is a leaf node | ||
} else { | ||
try { | ||
if (existingChild == null || existingChild instanceof Topic) { | ||
Topic node = t.createLeafChild(key.toString()); | ||
if (!Objects.equals(node.getOnce(), value)) { | ||
node.withValueChecked(childMergeBehavior.getTimestampToUse(), value); | ||
} | ||
} else { | ||
t.remove(existingChild); | ||
Topic newNode = t.createLeafChild(key.toString()); | ||
newNode.withValueChecked(childMergeBehavior.getTimestampToUse(), value); | ||
} | ||
} catch (UnsupportedInputTypeException e) { | ||
logger.error("Should never fail in updateChild", e); | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.