From 22abecdbc13b758aae6d92622c3f70472dd1a112 Mon Sep 17 00:00:00 2001 From: Bruno Roustant Date: Thu, 3 Sep 2020 11:49:49 +0200 Subject: [PATCH] SOLR-14819: Fix inefficient iterator pattern in JsonSchemaValidator. --- solr/CHANGES.txt | 2 ++ .../solr/common/util/JsonSchemaValidator.java | 26 +++++++------------ 2 files changed, 11 insertions(+), 17 deletions(-) diff --git a/solr/CHANGES.txt b/solr/CHANGES.txt index 2a4a01a2154..69a398d64a4 100644 --- a/solr/CHANGES.txt +++ b/solr/CHANGES.txt @@ -181,6 +181,8 @@ Optimizations * SOLR-14422: Admin UI shows Angular placeholders on first load / refresh. (Colvin Cowie via Eric Pugh) +* SOLR-14819: Fix inefficient iterator pattern in JsonSchemaValidator. (Thomas DuBuisson via Bruno Roustant) + Bug Fixes --------------------- diff --git a/solr/solrj/src/java/org/apache/solr/common/util/JsonSchemaValidator.java b/solr/solrj/src/java/org/apache/solr/common/util/JsonSchemaValidator.java index 1a2d1c70b72..d67e6eb88d6 100644 --- a/solr/solrj/src/java/org/apache/solr/common/util/JsonSchemaValidator.java +++ b/solr/solrj/src/java/org/apache/solr/common/util/JsonSchemaValidator.java @@ -17,15 +17,7 @@ package org.apache.solr.common.util; -import java.util.Arrays; -import java.util.Collections; -import java.util.HashMap; -import java.util.HashSet; -import java.util.LinkedList; -import java.util.List; -import java.util.Locale; -import java.util.Map; -import java.util.Set; +import java.util.*; import java.util.function.Function; /** @@ -46,15 +38,16 @@ public class JsonSchemaValidator { this((Map) Utils.fromJSONString(jsonString)); } - public JsonSchemaValidator(Map jsonSchema) { + public JsonSchemaValidator(Map jsonSchema) { this.validators = new LinkedList<>(); - for (Object fname : jsonSchema.keySet()) { + for (Map.Entry entry : jsonSchema.entrySet()) { + Object fname = entry.getKey(); if (KNOWN_FNAMES.contains(fname.toString())) continue; Function, Validator> initializeFunction = VALIDATORS.get(fname.toString()); if (initializeFunction == null) throw new RuntimeException("Unknown key : " + fname); - this.validators.add(initializeFunction.apply(new Pair<>(jsonSchema, jsonSchema.get(fname)))); + this.validators.add(initializeFunction.apply(new Pair<>(jsonSchema, entry.getValue()))); } } @@ -276,18 +269,17 @@ class PropertiesValidator extends Validator> { } @Override - @SuppressWarnings({"rawtypes"}) boolean validate(Object o, List errs) { if (o instanceof Map) { - @SuppressWarnings({"rawtypes"}) - Map map = (Map) o; - for (Object key : map.keySet()) { + Map map = (Map) o; + for (Map.Entry entry : map.entrySet()) { + Object key = entry.getKey(); JsonSchemaValidator jsonSchema = jsonSchemas.get(key.toString()); if (jsonSchema == null && !additionalProperties) { errs.add("Unknown field '" + key + "' in object : " + Utils.toJSONString(o)); return false; } - if (jsonSchema != null && !jsonSchema.validate(map.get(key), errs)) { + if (jsonSchema != null && !jsonSchema.validate(entry.getValue(), errs)) { return false; } }