Skip to content

Commit

Permalink
Use HashMap if CompactHashMap is not available on classpath (#1235)
Browse files Browse the repository at this point in the history
This allows eureka users to exclude compactmap library if they so choose.
  • Loading branch information
spencergibb authored and troshko111 committed Sep 4, 2019
1 parent 7671fda commit 9d07ac4
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,22 @@ public class EurekaJacksonCodec {

public static EurekaJacksonCodec INSTANCE = new EurekaJacksonCodec();

public static final Supplier<? extends Map<String, String>> METADATA_MAP_SUPPLIER;

static {
boolean useCompact = true;
try {
Class.forName("vlsi.utils.CompactHashMap");
} catch (ClassNotFoundException e) {
useCompact = false;
}
if (useCompact) {
METADATA_MAP_SUPPLIER = CompactHashMap::new;
} else {
METADATA_MAP_SUPPLIER = HashMap::new;
}
}

/**
* XStream codec supports character replacement in field names to generate XML friendly
* names. This feature is also configurable, and replacement strings can be provided by a user.
Expand Down Expand Up @@ -622,7 +638,7 @@ public InstanceInfo deserialize(JsonParser jp, DeserializationContext context) t
String key = intern.apply(jp, CacheScope.GLOBAL_SCOPE);
jsonToken = jp.nextToken();
String value = intern.apply(jp, CacheScope.APPLICATION_SCOPE );
metadataMap = Optional.ofNullable(metadataMap).orElseGet(CompactHashMap::new);
metadataMap = Optional.ofNullable(metadataMap).orElseGet(METADATA_MAP_SUPPLIER);
metadataMap.put(key, value);
}
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,11 @@
import com.netflix.appinfo.AmazonInfo.MetaDataKey;
import com.netflix.appinfo.DataCenterInfo.Name;
import com.netflix.discovery.converters.EnumLookup;
import com.netflix.discovery.converters.EurekaJacksonCodec;
import com.netflix.discovery.util.DeserializerStringCache;
import com.netflix.discovery.util.DeserializerStringCache.CacheScope;
import com.netflix.discovery.util.StringCache;

import vlsi.utils.CompactHashMap;

/**
* Amazon instance info builder that is doing key names interning, together with
* value interning for selected keys (see {@link StringInterningAmazonInfoBuilder#VALUE_INTERN_KEYS}).
Expand Down Expand Up @@ -109,7 +108,7 @@ private void skipToEnd(JsonParser jp) throws IOException {
@Override
public AmazonInfo deserialize(JsonParser jp, DeserializationContext context)
throws IOException {
Map<String,String> metadata = new CompactHashMap<>();
Map<String,String> metadata = EurekaJacksonCodec.METADATA_MAP_SUPPLIER.get();
DeserializerStringCache intern = DeserializerStringCache.from(context);

if (skipToMetadata(jp)) {
Expand Down

0 comments on commit 9d07ac4

Please sign in to comment.