Changeset 3923db0
- Timestamp:
- Nov 20, 2018 11:03:47 AM (2 years ago)
- Branches:
- master
- Children:
- 21ca75d
- Parents:
- 98de1ae
- Location:
- router/java/src/com/maxmind/db
- Files:
-
- 6 edited
Legend:
- Unmodified
- Added
- Removed
-
router/java/src/com/maxmind/db/CHMCache.java
r98de1ae r3923db0 3 3 import java.io.IOException; 4 4 import java.util.concurrent.ConcurrentHashMap; 5 6 import com.fasterxml.jackson.databind.JsonNode;7 5 8 6 /** … … 16 14 17 15 private final int capacity; 18 private final ConcurrentHashMap<Integer, JsonNode> cache;16 private final ConcurrentHashMap<Integer, Object> cache; 19 17 private boolean cacheFull = false; 20 18 … … 25 23 public CHMCache(int capacity) { 26 24 this.capacity = capacity; 27 this.cache = new ConcurrentHashMap<Integer, JsonNode>(capacity);25 this.cache = new ConcurrentHashMap<Integer, Object>(capacity); 28 26 } 29 27 30 28 @Override 31 public JsonNodeget(int key, Loader loader) throws IOException {29 public Object get(int key, Loader loader) throws IOException { 32 30 Integer k = key; 33 JsonNodevalue = cache.get(k);31 Object value = cache.get(k); 34 32 if (value == null) { 35 33 value = loader.load(key); -
router/java/src/com/maxmind/db/Decoder.java
r98de1ae r3923db0 13 13 import java.util.Map; 14 14 15 import com.fasterxml.jackson.databind.JsonNode;16 import com.fasterxml.jackson.databind.ObjectMapper;17 import com.fasterxml.jackson.databind.node.*;18 19 15 /* 20 16 * Decoder for MaxMind DB data. … … 26 22 private static final Charset UTF_8 = Charset.forName("UTF-8"); 27 23 28 private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper();29 30 24 private static final int[] POINTER_VALUE_OFFSETS = { 0, 0, 1 << 11, (1 << 19) + ((1) << 11), 0 }; 31 25 … … 72 66 private final NodeCache.Loader cacheLoader = new NodeCache.Loader() { 73 67 @Override 74 public JsonNodeload(int key) throws IOException {68 public Object load(int key) throws IOException { 75 69 return decode(key); 76 70 } 77 71 }; 78 72 79 JsonNodedecode(int offset) throws IOException {73 Object decode(int offset) throws IOException { 80 74 if (offset >= this.buffer.capacity()) { 81 75 throw new InvalidDatabaseException( … … 88 82 } 89 83 90 JsonNodedecode() throws IOException {84 Object decode() throws IOException { 91 85 int ctrlByte = 0xFF & this.buffer.get(); 92 86 … … 104 98 // for unit testing 105 99 if (this.POINTER_TEST_HACK) { 106 return new LongNode(pointer);100 return Long.valueOf(pointer); 107 101 } 108 102 109 103 int targetOffset = (int) pointer; 110 104 int position = buffer.position(); 111 JsonNodenode = cache.get(targetOffset, cacheLoader);105 Object node = cache.get(targetOffset, cacheLoader); 112 106 buffer.position(position); 113 107 return node; … … 148 142 } 149 143 150 private JsonNodedecodeByType(Type type, int size)144 private Object decodeByType(Type type, int size) 151 145 throws IOException { 152 146 switch (type) { … … 158 152 return Decoder.decodeBoolean(size); 159 153 case UTF8_STRING: 160 return new TextNode(this.decodeString(size));154 return this.decodeString(size); 161 155 case DOUBLE: 162 156 return this.decodeDouble(size); … … 164 158 return this.decodeFloat(size); 165 159 case BYTES: 166 return new BinaryNode(this.getByteArray(size));160 return this.getByteArray(size); 167 161 case UINT16: 168 162 return this.decodeUint16(size); … … 189 183 } 190 184 191 private Int NodedecodeUint16(int size) {192 return new IntNode(this.decodeInteger(size));193 } 194 195 private Int NodedecodeInt32(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)); 197 191 } 198 192 … … 205 199 } 206 200 207 private Long NodedecodeUint32(int size) {208 return new LongNode(this.decodeLong(size));201 private Long decodeUint32(int size) { 202 return Long.valueOf(this.decodeLong(size)); 209 203 } 210 204 … … 225 219 } 226 220 227 private BigInteger NodedecodeBigInteger(int size) {221 private BigInteger decodeBigInteger(int size) { 228 222 byte[] bytes = this.getByteArray(size); 229 return new BigInteger Node(new BigInteger(1, bytes));230 } 231 232 private Double NodedecodeDouble(int size) throws InvalidDatabaseException {223 return new BigInteger(1, bytes); 224 } 225 226 private Double decodeDouble(int size) throws InvalidDatabaseException { 233 227 if (size != 8) { 234 228 throw new InvalidDatabaseException( … … 236 230 + "invalid size of double."); 237 231 } 238 return new DoubleNode(this.buffer.getDouble());239 } 240 241 private Float NodedecodeFloat(int size) throws InvalidDatabaseException {232 return Double.valueOf(this.buffer.getDouble()); 233 } 234 235 private Float decodeFloat(int size) throws InvalidDatabaseException { 242 236 if (size != 4) { 243 237 throw new InvalidDatabaseException( … … 245 239 + "invalid size of float."); 246 240 } 247 return new FloatNode(this.buffer.getFloat());248 } 249 250 private static Boolean NodedecodeBoolean(int size)241 return Float.valueOf(this.buffer.getFloat()); 242 } 243 244 private static Boolean decodeBoolean(int size) 251 245 throws InvalidDatabaseException { 252 246 switch (size) { 253 247 case 0: 254 return Boolean Node.FALSE;248 return Boolean.FALSE; 255 249 case 1: 256 return Boolean Node.TRUE;250 return Boolean.TRUE; 257 251 default: 258 252 throw new InvalidDatabaseException( … … 262 256 } 263 257 264 private JsonNodedecodeArray(int size) throws IOException {265 266 List< JsonNode> array = new ArrayList<JsonNode>(size);267 for (int i = 0; i < size; i++) { 268 JsonNoder = 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(); 269 263 array.add(r); 270 264 } 271 265 272 return new ArrayNode(OBJECT_MAPPER.getNodeFactory(), Collections.unmodifiableList(array));273 } 274 275 private JsonNodedecodeMap(int size) throws IOException {266 return Collections.unmodifiableList(array); 267 } 268 269 private Map<String, Object> decodeMap(int size) throws IOException { 276 270 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 JsonNodevalue = 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(); 282 276 map.put(key, value); 283 277 } 284 278 285 return new ObjectNode(OBJECT_MAPPER.getNodeFactory(), Collections.unmodifiableMap(map));279 return Collections.unmodifiableMap(map); 286 280 } 287 281 -
router/java/src/com/maxmind/db/Metadata.java
r98de1ae r3923db0 6 6 import java.util.List; 7 7 import 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;12 8 13 9 public final class Metadata { … … 19 15 private final String databaseType; 20 16 21 private final JsonNodedescription;17 private final Map description; 22 18 23 19 private final int ipVersion; 24 20 25 private final JsonNodelanguages;21 private final List languages; 26 22 27 23 private final int nodeByteSize; … … 33 29 private final int searchTreeSize; 34 30 35 Metadata( JsonNodemetadata) {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"); 47 43 this.nodeByteSize = this.recordSize / 4; 48 44 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); 49 66 } 50 67 … … 83 100 */ 84 101 public Map<String, String> getDescription() { 85 return new ObjectMapper().convertValue(this.description, 86 new TypeReference<HashMap<String, String>>() { 87 }); 102 return this.description; 88 103 } 89 104 … … 100 115 */ 101 116 public List<String> getLanguages() { 102 return new ObjectMapper().convertValue(this.languages, 103 new TypeReference<ArrayList<String>>() { 104 }); 117 return this.languages; 105 118 } 106 119 -
router/java/src/com/maxmind/db/NoCache.java
r98de1ae r3923db0 2 2 3 3 import java.io.IOException; 4 5 import com.fasterxml.jackson.databind.JsonNode;6 4 7 5 /** … … 16 14 17 15 @Override 18 public JsonNodeget(int key, Loader loader) throws IOException {16 public Object get(int key, Loader loader) throws IOException { 19 17 return loader.load(key); 20 18 } -
router/java/src/com/maxmind/db/NodeCache.java
r98de1ae r3923db0 3 3 import java.io.IOException; 4 4 5 import com.fasterxml.jackson.databind.JsonNode;6 7 5 public interface NodeCache { 8 6 9 7 public interface Loader { 10 JsonNodeload(int key) throws IOException;8 Object load(int key) throws IOException; 11 9 } 12 10 13 public JsonNodeget(int key, Loader loader) throws IOException;11 public Object get(int key, Loader loader) throws IOException; 14 12 15 13 } -
router/java/src/com/maxmind/db/Reader.java
r98de1ae r3923db0 7 7 import java.net.InetAddress; 8 8 import java.nio.ByteBuffer; 9 import java.util.Map; 9 10 import java.util.concurrent.atomic.AtomicReference; 10 11 import com.fasterxml.jackson.databind.JsonNode;12 11 13 12 /** … … 130 129 131 130 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)); 133 132 134 133 this.ipV4Start = this.findIpV4StartNode(buffer); … … 142 141 * @throws IOException if a file I/O error occurs. 143 142 */ 144 public JsonNodeget(InetAddress ipAddress) throws IOException {143 public Object get(InetAddress ipAddress) throws IOException { 145 144 ByteBuffer buffer = this.getBufferHolder().get(); 146 145 int pointer = this.findAddressInTree(buffer, ipAddress); … … 235 234 } 236 235 237 private JsonNoderesolveDataPointer(ByteBuffer buffer, int pointer)236 private Object resolveDataPointer(ByteBuffer buffer, int pointer) 238 237 throws IOException { 239 238 int resolved = (pointer - this.metadata.getNodeCount())
Note: See TracChangeset
for help on using the changeset viewer.