use the proper abstraction of ImmutableOpenMap

we already use the open map / clone trick, might as well use the ImmutableOpenMap here
This commit is contained in:
Shay Banon 2013-11-26 02:48:19 +01:00
parent a03556daa0
commit 0610fc3ad2
2 changed files with 15 additions and 9 deletions

View File

@ -25,6 +25,7 @@ import com.carrotsearch.hppc.predicates.ObjectPredicate;
import com.carrotsearch.hppc.procedures.ObjectObjectProcedure;
import java.util.Iterator;
import java.util.Map;
/**
* An immutable map implementation based on open hash map.
@ -180,6 +181,16 @@ public final class ImmutableOpenMap<KType, VType> implements Iterable<ObjectObje
return new ImmutableOpenMap<KType, VType>(map);
}
/**
* Puts all the entries in the map to the builder.
*/
public Builder<KType, VType> putAll(Map<KType, VType> map) {
for (Map.Entry<KType, VType> entry : map.entrySet()) {
this.map.put(entry.getKey(), entry.getValue());
}
return this;
}
/**
* A put operation that can be used in the fluent pattern.
*/

View File

@ -19,7 +19,6 @@
package org.elasticsearch.index.mapper.object;
import com.carrotsearch.hppc.ObjectObjectOpenHashMap;
import com.carrotsearch.hppc.cursors.ObjectObjectCursor;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
@ -30,7 +29,7 @@ import org.apache.lucene.search.Filter;
import org.apache.lucene.util.BytesRef;
import org.elasticsearch.ElasticSearchIllegalStateException;
import org.elasticsearch.common.Strings;
import org.elasticsearch.common.collect.HppcMaps;
import org.elasticsearch.common.collect.ImmutableOpenMap;
import org.elasticsearch.common.joda.FormatDateTimeFormatter;
import org.elasticsearch.common.xcontent.ToXContent;
import org.elasticsearch.common.xcontent.XContentBuilder;
@ -291,7 +290,7 @@ public class ObjectMapper implements Mapper, AllFieldMapper.IncludeInAll {
private Boolean includeInAll;
private volatile ObjectObjectOpenHashMap<String, Mapper> mappers = HppcMaps.newMap();
private volatile ImmutableOpenMap<String, Mapper> mappers = ImmutableOpenMap.of();
private final Object mutex = new Object();
@ -303,9 +302,7 @@ public class ObjectMapper implements Mapper, AllFieldMapper.IncludeInAll {
this.dynamic = dynamic;
this.pathType = pathType;
if (mappers != null) {
for (Map.Entry<String, Mapper> entry : mappers.entrySet()) {
this.mappers.put(entry.getKey(), entry.getValue());
}
this.mappers = ImmutableOpenMap.builder(this.mappers).putAll(mappers).build();
}
this.nestedTypePathAsString = "__" + fullPath;
this.nestedTypePathAsBytes = new BytesRef(nestedTypePathAsString);
@ -357,9 +354,7 @@ public class ObjectMapper implements Mapper, AllFieldMapper.IncludeInAll {
((AllFieldMapper.IncludeInAll) mapper).includeInAllIfNotSet(includeInAll);
}
synchronized (mutex) {
ObjectObjectOpenHashMap<String, Mapper> mappers = this.mappers.clone();
mappers.put(mapper.name(), mapper);
this.mappers = mappers;
this.mappers = ImmutableOpenMap.builder(this.mappers).fPut(mapper.name(), mapper).build();
}
return this;
}