Changeset 3923db0 for router


Ignore:
Timestamp:
Nov 20, 2018 11:03:47 AM (20 months ago)
Author:
zzz <zzz@…>
Branches:
master
Children:
21ca75d
Parents:
98de1ae
Message:

Modify MaxMind?-DB-Reader-java
to remove the dependency on the large com.fasterxml.jackson.databind JSON package,
and use POJOs instead.

Location:
router/java/src/com/maxmind/db
Files:
6 edited

Legend:

Unmodified
Added
Removed
  • router/java/src/com/maxmind/db/CHMCache.java

    r98de1ae r3923db0  
    33import java.io.IOException;
    44import java.util.concurrent.ConcurrentHashMap;
    5 
    6 import com.fasterxml.jackson.databind.JsonNode;
    75
    86/**
     
    1614
    1715    private final int capacity;
    18     private final ConcurrentHashMap<Integer, JsonNode> cache;
     16    private final ConcurrentHashMap<Integer, Object> cache;
    1917    private boolean cacheFull = false;
    2018
     
    2523    public CHMCache(int capacity) {
    2624        this.capacity = capacity;
    27         this.cache = new ConcurrentHashMap<Integer, JsonNode>(capacity);
     25        this.cache = new ConcurrentHashMap<Integer, Object>(capacity);
    2826    }
    2927
    3028    @Override
    31     public JsonNode get(int key, Loader loader) throws IOException {
     29    public Object get(int key, Loader loader) throws IOException {
    3230        Integer k = key;
    33         JsonNode value = cache.get(k);
     31        Object value = cache.get(k);
    3432        if (value == null) {
    3533            value = loader.load(key);
  • router/java/src/com/maxmind/db/Decoder.java

    r98de1ae r3923db0  
    1313import java.util.Map;
    1414
    15 import com.fasterxml.jackson.databind.JsonNode;
    16 import com.fasterxml.jackson.databind.ObjectMapper;
    17 import com.fasterxml.jackson.databind.node.*;
    18 
    1915/*
    2016 * Decoder for MaxMind DB data.
     
    2622    private static final Charset UTF_8 = Charset.forName("UTF-8");
    2723
    28     private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper();
    29 
    3024    private static final int[] POINTER_VALUE_OFFSETS = { 0, 0, 1 << 11, (1 << 19) + ((1) << 11), 0 };
    3125
     
    7266    private final NodeCache.Loader cacheLoader = new NodeCache.Loader() {
    7367        @Override
    74         public JsonNode load(int key) throws IOException {
     68        public Object load(int key) throws IOException {
    7569            return decode(key);
    7670        }
    7771    };
    7872
    79     JsonNode decode(int offset) throws IOException {
     73    Object decode(int offset) throws IOException {
    8074        if (offset >= this.buffer.capacity()) {
    8175            throw new InvalidDatabaseException(
     
    8882    }
    8983
    90     JsonNode decode() throws IOException {
     84    Object decode() throws IOException {
    9185        int ctrlByte = 0xFF & this.buffer.get();
    9286
     
    10498            // for unit testing
    10599            if (this.POINTER_TEST_HACK) {
    106                 return new LongNode(pointer);
     100                return Long.valueOf(pointer);
    107101            }
    108102
    109103            int targetOffset = (int) pointer;
    110104            int position = buffer.position();
    111             JsonNode node = cache.get(targetOffset, cacheLoader);
     105            Object node = cache.get(targetOffset, cacheLoader);
    112106            buffer.position(position);
    113107            return node;
     
    148142    }
    149143
    150     private JsonNode decodeByType(Type type, int size)
     144    private Object decodeByType(Type type, int size)
    151145            throws IOException {
    152146        switch (type) {
     
    158152                return Decoder.decodeBoolean(size);
    159153            case UTF8_STRING:
    160                 return new TextNode(this.decodeString(size));
     154                return this.decodeString(size);
    161155            case DOUBLE:
    162156                return this.decodeDouble(size);
     
    164158                return this.decodeFloat(size);
    165159            case BYTES:
    166                 return new BinaryNode(this.getByteArray(size));
     160                return this.getByteArray(size);
    167161            case UINT16:
    168162                return this.decodeUint16(size);
     
    189183    }
    190184
    191     private IntNode decodeUint16(int size) {
    192         return new IntNode(this.decodeInteger(size));
    193     }
    194 
    195     private IntNode decodeInt32(int size) {
    196         return new IntNode(this.decodeInteger(size));
     185    private Integer decodeUint16(int size) {
     186        return Integer.valueOf(this.decodeInteger(size));
     187    }
     188
     189    private Integer decodeInt32(int size) {
     190        return Integer.valueOf(this.decodeInteger(size));
    197191    }
    198192
     
    205199    }
    206200
    207     private LongNode decodeUint32(int size) {
    208         return new LongNode(this.decodeLong(size));
     201    private Long decodeUint32(int size) {
     202        return Long.valueOf(this.decodeLong(size));
    209203    }
    210204
     
    225219    }
    226220
    227     private BigIntegerNode decodeBigInteger(int size) {
     221    private BigInteger decodeBigInteger(int size) {
    228222        byte[] bytes = this.getByteArray(size);
    229         return new BigIntegerNode(new BigInteger(1, bytes));
    230     }
    231 
    232     private DoubleNode decodeDouble(int size) throws InvalidDatabaseException {
     223        return new BigInteger(1, bytes);
     224    }
     225
     226    private Double decodeDouble(int size) throws InvalidDatabaseException {
    233227        if (size != 8) {
    234228            throw new InvalidDatabaseException(
     
    236230                            + "invalid size of double.");
    237231        }
    238         return new DoubleNode(this.buffer.getDouble());
    239     }
    240 
    241     private FloatNode decodeFloat(int size) throws InvalidDatabaseException {
     232        return Double.valueOf(this.buffer.getDouble());
     233    }
     234
     235    private Float decodeFloat(int size) throws InvalidDatabaseException {
    242236        if (size != 4) {
    243237            throw new InvalidDatabaseException(
     
    245239                            + "invalid size of float.");
    246240        }
    247         return new FloatNode(this.buffer.getFloat());
    248     }
    249 
    250     private static BooleanNode decodeBoolean(int size)
     241        return Float.valueOf(this.buffer.getFloat());
     242    }
     243
     244    private static Boolean decodeBoolean(int size)
    251245            throws InvalidDatabaseException {
    252246        switch (size) {
    253247            case 0:
    254                 return BooleanNode.FALSE;
     248                return Boolean.FALSE;
    255249            case 1:
    256                 return BooleanNode.TRUE;
     250                return Boolean.TRUE;
    257251            default:
    258252                throw new InvalidDatabaseException(
     
    262256    }
    263257
    264     private JsonNode decodeArray(int size) throws IOException {
    265 
    266         List<JsonNode> array = new ArrayList<JsonNode>(size);
    267         for (int i = 0; i < size; i++) {
    268             JsonNode r = this.decode();
     258    private List<Object> decodeArray(int size) throws IOException {
     259
     260        List<Object> array = new ArrayList<Object>(size);
     261        for (int i = 0; i < size; i++) {
     262            Object r = this.decode();
    269263            array.add(r);
    270264        }
    271265
    272         return new ArrayNode(OBJECT_MAPPER.getNodeFactory(), Collections.unmodifiableList(array));
    273     }
    274 
    275     private JsonNode decodeMap(int size) throws IOException {
     266        return Collections.unmodifiableList(array);
     267    }
     268
     269    private Map<String, Object> decodeMap(int size) throws IOException {
    276270        int capacity = (int) (size / 0.75F + 1.0F);
    277         Map<String, JsonNode> map = new HashMap<String, JsonNode>(capacity);
    278 
    279         for (int i = 0; i < size; i++) {
    280             String key = this.decode().asText();
    281             JsonNode value = this.decode();
     271        Map<String, Object> map = new HashMap<String, Object>(capacity);
     272
     273        for (int i = 0; i < size; i++) {
     274            String key = (String) this.decode();
     275            Object value = this.decode();
    282276            map.put(key, value);
    283277        }
    284278
    285         return new ObjectNode(OBJECT_MAPPER.getNodeFactory(), Collections.unmodifiableMap(map));
     279        return Collections.unmodifiableMap(map);
    286280    }
    287281
  • router/java/src/com/maxmind/db/Metadata.java

    r98de1ae r3923db0  
    66import java.util.List;
    77import java.util.Map;
    8 
    9 import com.fasterxml.jackson.core.type.TypeReference;
    10 import com.fasterxml.jackson.databind.JsonNode;
    11 import com.fasterxml.jackson.databind.ObjectMapper;
    128
    139public final class Metadata {
     
    1915    private final String databaseType;
    2016
    21     private final JsonNode description;
     17    private final Map description;
    2218
    2319    private final int ipVersion;
    2420
    25     private final JsonNode languages;
     21    private final List languages;
    2622
    2723    private final int nodeByteSize;
     
    3329    private final int searchTreeSize;
    3430
    35     Metadata(JsonNode metadata) {
    36         this.binaryFormatMajorVersion = metadata.get(
    37                 "binary_format_major_version").asInt();
    38         this.binaryFormatMinorVersion = metadata.get(
    39                 "binary_format_minor_version").asInt();
    40         this.buildEpoch = metadata.get("build_epoch").asLong();
    41         this.databaseType = metadata.get("database_type").asText();
    42         this.languages = metadata.get("languages");
    43         this.description = metadata.get("description");
    44         this.ipVersion = metadata.get("ip_version").asInt();
    45         this.nodeCount = metadata.get("node_count").asInt();
    46         this.recordSize = metadata.get("record_size").asInt();
     31    Metadata(Map metadata) {
     32        this.binaryFormatMajorVersion = getInt(metadata,
     33                "binary_format_major_version");
     34        this.binaryFormatMinorVersion = getInt(metadata,
     35                "binary_format_minor_version");
     36        this.buildEpoch = getLong(metadata, "build_epoch");
     37        this.databaseType = getString(metadata, "database_type");
     38        this.languages = (List) metadata.get("languages");
     39        this.description = (Map) metadata.get("description");
     40        this.ipVersion = getInt(metadata, "ip_version");
     41        this.nodeCount = getInt(metadata, "node_count");
     42        this.recordSize = getInt(metadata, "record_size");
    4743        this.nodeByteSize = this.recordSize / 4;
    4844        this.searchTreeSize = this.nodeCount * this.nodeByteSize;
     45    }
     46
     47    private static int getInt(Object m, String key) {
     48        Map map = (Map) m;
     49        Number i = (Number) map.get(key);
     50        if (i != null)
     51            return i.intValue();
     52        return 0;
     53    }
     54
     55    private static long getLong(Object m, String key) {
     56        Map map = (Map) m;
     57        Number i = (Number) map.get(key);
     58        if (i != null)
     59            return i.longValue();
     60        return 0;
     61    }
     62
     63    private static String getString(Object m, String key) {
     64        Map map = (Map) m;
     65        return (String) map.get(key);
    4966    }
    5067
     
    83100     */
    84101    public Map<String, String> getDescription() {
    85         return new ObjectMapper().convertValue(this.description,
    86                 new TypeReference<HashMap<String, String>>() {
    87                 });
     102        return this.description;
    88103    }
    89104
     
    100115     */
    101116    public List<String> getLanguages() {
    102         return new ObjectMapper().convertValue(this.languages,
    103                 new TypeReference<ArrayList<String>>() {
    104                 });
     117        return this.languages;
    105118    }
    106119
  • router/java/src/com/maxmind/db/NoCache.java

    r98de1ae r3923db0  
    22
    33import java.io.IOException;
    4 
    5 import com.fasterxml.jackson.databind.JsonNode;
    64
    75/**
     
    1614
    1715    @Override
    18     public JsonNode get(int key, Loader loader) throws IOException {
     16    public Object get(int key, Loader loader) throws IOException {
    1917        return loader.load(key);
    2018    }
  • router/java/src/com/maxmind/db/NodeCache.java

    r98de1ae r3923db0  
    33import java.io.IOException;
    44
    5 import com.fasterxml.jackson.databind.JsonNode;
    6 
    75public interface NodeCache {
    86
    97    public interface Loader {
    10         JsonNode load(int key) throws IOException;
     8        Object load(int key) throws IOException;
    119    }
    1210
    13     public JsonNode get(int key, Loader loader) throws IOException;
     11    public Object get(int key, Loader loader) throws IOException;
    1412
    1513}
  • router/java/src/com/maxmind/db/Reader.java

    r98de1ae r3923db0  
    77import java.net.InetAddress;
    88import java.nio.ByteBuffer;
     9import java.util.Map;
    910import java.util.concurrent.atomic.AtomicReference;
    10 
    11 import com.fasterxml.jackson.databind.JsonNode;
    1211
    1312/**
     
    130129
    131130        Decoder metadataDecoder = new Decoder(this.cache, buffer, start);
    132         this.metadata = new Metadata(metadataDecoder.decode(start));
     131        this.metadata = new Metadata((Map) metadataDecoder.decode(start));
    133132
    134133        this.ipV4Start = this.findIpV4StartNode(buffer);
     
    142141     * @throws IOException if a file I/O error occurs.
    143142     */
    144     public JsonNode get(InetAddress ipAddress) throws IOException {
     143    public Object get(InetAddress ipAddress) throws IOException {
    145144        ByteBuffer buffer = this.getBufferHolder().get();
    146145        int pointer = this.findAddressInTree(buffer, ipAddress);
     
    235234    }
    236235
    237     private JsonNode resolveDataPointer(ByteBuffer buffer, int pointer)
     236    private Object resolveDataPointer(ByteBuffer buffer, int pointer)
    238237            throws IOException {
    239238        int resolved = (pointer - this.metadata.getNodeCount())
Note: See TracChangeset for help on using the changeset viewer.