diff options
author | Harald Musum <musum@yahoo-inc.com> | 2017-01-26 15:12:05 +0100 |
---|---|---|
committer | Harald Musum <musum@yahoo-inc.com> | 2017-01-26 15:12:05 +0100 |
commit | 7f075f6761b514e25411b38e180e837be752807f (patch) | |
tree | f9a69e8d0c4ef49efd23d6f40abda353b620081c | |
parent | 3412a4094942b9fe9237261762d3c2f4e2d919db (diff) |
Simplify
-rw-r--r-- | config-application-package/src/main/java/com/yahoo/config/application/OverrideProcessor.java | 45 |
1 files changed, 8 insertions, 37 deletions
diff --git a/config-application-package/src/main/java/com/yahoo/config/application/OverrideProcessor.java b/config-application-package/src/main/java/com/yahoo/config/application/OverrideProcessor.java index bd913393b9d..1e77ad1a8f7 100644 --- a/config-application-package/src/main/java/com/yahoo/config/application/OverrideProcessor.java +++ b/config-application-package/src/main/java/com/yahoo/config/application/OverrideProcessor.java @@ -12,8 +12,6 @@ import org.w3c.dom.NamedNodeMap; import javax.xml.transform.TransformerException; import java.util.*; import java.util.logging.Logger; -import java.util.stream.Collectors; -import java.util.stream.IntStream; /** * Handles overrides in a XML document according to the rules defined for multi environment application packages. @@ -37,8 +35,6 @@ class OverrideProcessor implements PreProcessor { private static final String ATTR_ID = "id"; private static final String ATTR_ENV = "environment"; private static final String ATTR_REG = "region"; - private static final String ATTR_ENV_FULL_NAME = "deploy:" + ATTR_ENV; - private static final String ATTR_REG_FULL_NAME = "deploy:" + ATTR_REG; public OverrideProcessor(Environment environment, RegionName region) { this.environment = environment; @@ -135,17 +131,11 @@ class OverrideProcessor implements PreProcessor { * Find the most specific element and remove all others. */ private void retainMostSpecificEnvironmentAndRegion(Element parent, List<Element> children, Context context) { - // Put elements with same attributes in a map with a key that is the concatenation of attribute names - // (except the override attribute names) and process values for each key - Map<String, List<Element>> elementsByEqualAttributeSet = elementsByEqualAttributeSet(children); - // Keep track of elements with highest number of matches (might be more than one element with same tag, need a list) List<Element> bestMatches = new ArrayList<>(); int bestMatch = 0; - for (Map.Entry<String, List<Element>> entry : elementsByEqualAttributeSet.entrySet()) { - for (Element child : entry.getValue()) { - bestMatch = findBestMatches(bestMatches, child, bestMatch, context); - } + for (Element child : children) { + bestMatch = updateBestMatches(bestMatches, child, bestMatch, context); } if (bestMatch > 0) { // there was a region/environment specific override doElementSpecificProcessingOnOverride(bestMatches); @@ -157,13 +147,13 @@ class OverrideProcessor implements PreProcessor { } } - private int findBestMatches(List<Element> bestMatchElements, Element child, int bestMatch, Context context) { + private int updateBestMatches(List<Element> bestMatches, Element child, int bestMatch, Context context) { int overrideCount = getNumberOfOverrides(child, context); if (overrideCount >= bestMatch) { - if (bestMatch != overrideCount) - bestMatchElements.clear(); + if (overrideCount > bestMatch) + bestMatches.clear(); - bestMatchElements.add(child); + bestMatches.add(child); return overrideCount; } else { return bestMatch; @@ -182,9 +172,9 @@ class OverrideProcessor implements PreProcessor { } /** Called on each element which is selected by matching some override condition */ - private void doElementSpecificProcessingOnOverride(List<Element> bestMatchElements) { + private void doElementSpecificProcessingOnOverride(List<Element> elements) { // if node capacity is specified explicitly for some env/region we should require that capacity - bestMatchElements.forEach(element -> { + elements.forEach(element -> { if (element.getTagName().equals("nodes")) if (element.getChildNodes().getLength() == 0) // specifies capacity, not a list of nodes element.setAttribute("required", "true"); @@ -250,25 +240,6 @@ class OverrideProcessor implements PreProcessor { return elementsByTagName; } - private Map<String, List<Element>> elementsByEqualAttributeSet(List<Element> children) { - Map<String, List<Element>> elementsByEqualAttributeSet = new LinkedHashMap<>(); - // Index by a concatenation of tag name + attribute names (except override attribute names) - for (Element child : children) { - NamedNodeMap attributes = child.getAttributes(); - String attributeNames = IntStream.range(0, attributes.getLength()) - .mapToObj(i -> attributes.item(i).getNodeName()) - .filter(nodeName -> !(nodeName.equals(ATTR_ENV_FULL_NAME) || nodeName.equals(ATTR_REG_FULL_NAME))) - .sorted() - .collect(Collectors.joining()); - String key = child.getTagName() + attributeNames; - if ( ! elementsByEqualAttributeSet.containsKey(key)) { - elementsByEqualAttributeSet.put(key, new ArrayList<>()); - } - elementsByEqualAttributeSet.get(key).add(child); - } - return elementsByEqualAttributeSet; - } - // For debugging private static String getPrintableElement(Element element) { StringBuilder sb = new StringBuilder(element.getTagName()); |