Skip to content

Commit 28cc882

Browse files
committed
JAVA-2144: Optimize BsonDocumentCodec via use of BsonTypeCodecMap
1 parent 9024d7e commit 28cc882

File tree

2 files changed

+20
-7
lines changed

2 files changed

+20
-7
lines changed

bson/src/main/org/bson/codecs/BsonDocumentCodec.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import java.util.List;
3131
import java.util.Map;
3232

33+
import static org.bson.codecs.BsonValueCodecProvider.getBsonTypeClassMap;
3334
import static org.bson.codecs.configuration.CodecRegistries.fromProviders;
3435

3536
/**
@@ -42,12 +43,13 @@ public class BsonDocumentCodec implements CollectibleCodec<BsonDocument> {
4243
private static final CodecRegistry DEFAULT_REGISTRY = fromProviders(new BsonValueCodecProvider());
4344

4445
private final CodecRegistry codecRegistry;
46+
private final BsonTypeCodecMap bsonTypeCodecMap;
4547

4648
/**
4749
* Creates a new instance with a default codec registry that uses the {@link BsonValueCodecProvider}.
4850
*/
4951
public BsonDocumentCodec() {
50-
codecRegistry = DEFAULT_REGISTRY;
52+
this(DEFAULT_REGISTRY);
5153
}
5254

5355
/**
@@ -60,6 +62,7 @@ public BsonDocumentCodec(final CodecRegistry codecRegistry) {
6062
throw new IllegalArgumentException("Codec registry can not be null");
6163
}
6264
this.codecRegistry = codecRegistry;
65+
this.bsonTypeCodecMap = new BsonTypeCodecMap(getBsonTypeClassMap(), codecRegistry);
6366
}
6467

6568
/**
@@ -95,7 +98,7 @@ public BsonDocument decode(final BsonReader reader, final DecoderContext decoder
9598
* @return the non-null value read from the reader
9699
*/
97100
protected BsonValue readValue(final BsonReader reader, final DecoderContext decoderContext) {
98-
return codecRegistry.get(BsonValueCodecProvider.getClassForBsonType(reader.getCurrentBsonType())).decode(reader, decoderContext);
101+
return (BsonValue) bsonTypeCodecMap.get(reader.getCurrentBsonType()).decode(reader, decoderContext);
99102
}
100103

101104
@Override

bson/src/main/org/bson/codecs/BsonValueCodecProvider.java

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@
4343
import org.bson.codecs.configuration.CodecProvider;
4444
import org.bson.codecs.configuration.CodecRegistry;
4545

46-
import java.util.Collections;
4746
import java.util.HashMap;
4847
import java.util.Map;
4948

@@ -53,7 +52,7 @@
5352
* @since 3.0
5453
*/
5554
public class BsonValueCodecProvider implements CodecProvider {
56-
private static final Map<BsonType, Class<? extends BsonValue>> DEFAULT_BSON_TYPE_CLASS_MAP;
55+
private static final BsonTypeClassMap DEFAULT_BSON_TYPE_CLASS_MAP;
5756

5857
private final Map<Class<?>, Codec<?>> codecs = new HashMap<Class<?>, Codec<?>>();
5958

@@ -69,8 +68,19 @@ public BsonValueCodecProvider() {
6968
* @param bsonType the BsonType
7069
* @return the class associated with the given type
7170
*/
71+
@SuppressWarnings("unchecked")
7272
public static Class<? extends BsonValue> getClassForBsonType(final BsonType bsonType) {
73-
return DEFAULT_BSON_TYPE_CLASS_MAP.get(bsonType);
73+
return (Class<? extends BsonValue>) DEFAULT_BSON_TYPE_CLASS_MAP.get(bsonType);
74+
}
75+
76+
/**
77+
* Gets the BsonTypeClassMap used by this provider.
78+
*
79+
* @return the non-null BsonTypeClassMap
80+
* @since 3.3
81+
*/
82+
public static BsonTypeClassMap getBsonTypeClassMap() {
83+
return DEFAULT_BSON_TYPE_CLASS_MAP;
7484
}
7585

7686
@Override
@@ -132,7 +142,7 @@ private <T extends BsonValue> void addCodec(final Codec<T> codec) {
132142
}
133143

134144
static {
135-
Map<BsonType, Class<? extends BsonValue>> map = new HashMap<BsonType, Class<? extends BsonValue>>();
145+
Map<BsonType, Class<?>> map = new HashMap<BsonType, Class<?>>();
136146

137147
map.put(BsonType.NULL, BsonNull.class);
138148
map.put(BsonType.ARRAY, BsonArray.class);
@@ -155,6 +165,6 @@ private <T extends BsonValue> void addCodec(final Codec<T> codec) {
155165
map.put(BsonType.TIMESTAMP, BsonTimestamp.class);
156166
map.put(BsonType.UNDEFINED, BsonUndefined.class);
157167

158-
DEFAULT_BSON_TYPE_CLASS_MAP = Collections.unmodifiableMap(map);
168+
DEFAULT_BSON_TYPE_CLASS_MAP = new BsonTypeClassMap(map);
159169
}
160170
}

0 commit comments

Comments
 (0)