Skip to content

Commit

Permalink
immutable ContainerNodes to avoid deep copies
Browse files Browse the repository at this point in the history
Note, this requires Jackson 2.7.x.
  • Loading branch information
phraktle committed Jan 12, 2016
1 parent 3490a7d commit 945605b
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 10 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.6.4</version>
<version>2.7.0</version>
</dependency>
</dependencies>
<properties>
Expand Down
4 changes: 0 additions & 4 deletions src/main/java/com/maxmind/db/CHMCache.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import java.util.concurrent.ConcurrentHashMap;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.ContainerNode;

/**
* A simplistic cache using a {@link ConcurrentHashMap}. There's no eviction
Expand Down Expand Up @@ -42,9 +41,6 @@ public JsonNode get(int key, Loader loader) throws IOException {
}
}
}
if (value instanceof ContainerNode) {
value = value.deepCopy();
}
return value;
}

Expand Down
16 changes: 11 additions & 5 deletions src/main/java/com/maxmind/db/Decoder.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@
import java.nio.charset.CharacterCodingException;
import java.nio.charset.Charset;
import java.nio.charset.CharsetDecoder;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
Expand Down Expand Up @@ -257,26 +262,27 @@ private static BooleanNode decodeBoolean(int size)
}

private JsonNode decodeArray(int size) throws IOException {
ArrayNode array = OBJECT_MAPPER.createArrayNode();

List<JsonNode> array = new ArrayList<JsonNode>(size);
for (int i = 0; i < size; i++) {
JsonNode r = this.decode();
array.add(r);
}

return array;
return new ArrayNode(OBJECT_MAPPER.getNodeFactory(), Collections.unmodifiableList(array));
}

private JsonNode decodeMap(int size) throws IOException {
ObjectNode map = OBJECT_MAPPER.createObjectNode();
int capacity = (int) (size / 0.75F + 1.0F);
Map<String, JsonNode> map = new HashMap<String, JsonNode>(capacity);

for (int i = 0; i < size; i++) {
String key = this.decode().asText();
JsonNode value = this.decode();
map.set(key, value);
map.put(key, value);
}

return map;
return new ObjectNode(OBJECT_MAPPER.getNodeFactory(), Collections.unmodifiableMap(map));
}

private byte[] getByteArray(int length) {
Expand Down

0 comments on commit 945605b

Please sign in to comment.