Fix Tring to Mutate Immutable Collections

Fixes two spots where #44665 caused a previously mutable collection to now be read as an immutable one, leading to errors
This commit is contained in:
Armin Braun 2019-07-22 10:47:28 +02:00
parent b9067ba1ba
commit a6adcecd20
2 changed files with 17 additions and 8 deletions

View File

@ -82,19 +82,28 @@ public class RunningStats implements Writeable, Cloneable {
// read doc count // read doc count
docCount = (Long)in.readGenericValue(); docCount = (Long)in.readGenericValue();
// read fieldSum // read fieldSum
fieldSum = (HashMap<String, Double>)in.readGenericValue(); fieldSum = convertIfNeeded((Map<String, Double>)in.readGenericValue());
// counts // counts
counts = (HashMap<String, Long>)in.readGenericValue(); counts = convertIfNeeded((Map<String, Long>)in.readGenericValue());
// means // means
means = (HashMap<String, Double>)in.readGenericValue(); means = convertIfNeeded((Map<String, Double>)in.readGenericValue());
// variances // variances
variances = (HashMap<String, Double>)in.readGenericValue(); variances = convertIfNeeded((Map<String, Double>)in.readGenericValue());
// skewness // skewness
skewness = (HashMap<String, Double>)in.readGenericValue(); skewness = convertIfNeeded((Map<String, Double>)in.readGenericValue());
// kurtosis // kurtosis
kurtosis = (HashMap<String, Double>)in.readGenericValue(); kurtosis = convertIfNeeded((Map<String, Double>)in.readGenericValue());
// read covariances // read covariances
covariances = (HashMap<String, HashMap<String, Double>>)in.readGenericValue(); covariances = convertIfNeeded((Map<String, HashMap<String, Double>>)in.readGenericValue());
}
// Convert Map to HashMap if it isn't
private static <K, V> HashMap<K, V> convertIfNeeded(Map<K,V> map) {
if (map instanceof HashMap) {
return (HashMap<K, V>) map;
} else {
return new HashMap<>(map);
}
} }
@Override @Override

View File

@ -365,7 +365,7 @@ public final class InnerHitBuilder implements Writeable, ToXContentObject {
* Adds a field to load from the docvalue and return. * Adds a field to load from the docvalue and return.
*/ */
public InnerHitBuilder addDocValueField(String field, String format) { public InnerHitBuilder addDocValueField(String field, String format) {
if (docValueFields == null) { if (docValueFields == null || docValueFields.isEmpty() == true) {
docValueFields = new ArrayList<>(); docValueFields = new ArrayList<>();
} }
docValueFields.add(new FieldAndFormat(field, format)); docValueFields.add(new FieldAndFormat(field, format));