SOLR-14819: Fix inefficient iterator pattern in JsonSchemaValidator.

This commit is contained in:
Bruno Roustant 2020-09-03 11:49:49 +02:00
parent 4fa4329287
commit 22abecdbc1
No known key found for this signature in database
GPG Key ID: CD28DABB95360525
2 changed files with 11 additions and 17 deletions

View File

@ -181,6 +181,8 @@ Optimizations
* SOLR-14422: Admin UI shows Angular placeholders on first load / refresh. (Colvin Cowie via Eric Pugh) * 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 Bug Fixes
--------------------- ---------------------

View File

@ -17,15 +17,7 @@
package org.apache.solr.common.util; package org.apache.solr.common.util;
import java.util.Arrays; import java.util.*;
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.function.Function; import java.util.function.Function;
/** /**
@ -46,15 +38,16 @@ public class JsonSchemaValidator {
this((Map) Utils.fromJSONString(jsonString)); this((Map) Utils.fromJSONString(jsonString));
} }
public JsonSchemaValidator(Map jsonSchema) { public JsonSchemaValidator(Map<?, ?> jsonSchema) {
this.validators = new LinkedList<>(); 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; if (KNOWN_FNAMES.contains(fname.toString())) continue;
Function<Pair<Map, Object>, Validator> initializeFunction = VALIDATORS.get(fname.toString()); Function<Pair<Map, Object>, Validator> initializeFunction = VALIDATORS.get(fname.toString());
if (initializeFunction == null) throw new RuntimeException("Unknown key : " + fname); 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<Map<String, Map>> {
} }
@Override @Override
@SuppressWarnings({"rawtypes"})
boolean validate(Object o, List<String> errs) { boolean validate(Object o, List<String> errs) {
if (o instanceof Map) { if (o instanceof Map) {
@SuppressWarnings({"rawtypes"}) Map<?, ?> map = (Map) o;
Map map = (Map) o; for (Map.Entry<?,?> entry : map.entrySet()) {
for (Object key : map.keySet()) { Object key = entry.getKey();
JsonSchemaValidator jsonSchema = jsonSchemas.get(key.toString()); JsonSchemaValidator jsonSchema = jsonSchemas.get(key.toString());
if (jsonSchema == null && !additionalProperties) { if (jsonSchema == null && !additionalProperties) {
errs.add("Unknown field '" + key + "' in object : " + Utils.toJSONString(o)); errs.add("Unknown field '" + key + "' in object : " + Utils.toJSONString(o));
return false; return false;
} }
if (jsonSchema != null && !jsonSchema.validate(map.get(key), errs)) { if (jsonSchema != null && !jsonSchema.validate(entry.getValue(), errs)) {
return false; return false;
} }
} }