Skip to content

Commit a5273a0

Browse files
committed
More refactoring
1 parent d72120f commit a5273a0

5 files changed

Lines changed: 175 additions & 143 deletions

File tree

src/main/java/com/maxmind/maxminddb/Decoder.java

Lines changed: 16 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,8 @@ public class Decoder {
3232
class Result {
3333
private final JsonNode obj;
3434
private long offset;
35-
private final Type type;
3635

37-
Result(JsonNode obj, Type t, long offset) {
38-
this.type = t;
36+
Result(JsonNode obj, long offset) {
3937
this.obj = obj;
4038
this.offset = offset;
4139

@@ -159,28 +157,28 @@ public Result decode(long offset) throws MaxMindDbException, IOException {
159157
switch (type) {
160158
case UTF8_STRING:
161159
TextNode s = new TextNode(Decoder.decodeString(bytes));
162-
return new Result(s, type, new_offset);
160+
return new Result(s, new_offset);
163161
case DOUBLE:
164162
DoubleNode d = Decoder.decodeDouble(bytes);
165-
return new Result(d, type, new_offset);
163+
return new Result(d, new_offset);
166164
case BYTES:
167165
BinaryNode b = new BinaryNode(Decoder.decodeBytes(bytes));
168-
return new Result(b, type, new_offset);
166+
return new Result(b, new_offset);
169167
case UINT16:
170168
IntNode i = Decoder.decodeUint16(bytes);
171-
return new Result(i, type, new_offset);
169+
return new Result(i, new_offset);
172170
case UINT32:
173171
LongNode l = Decoder.decodeUint32(bytes);
174-
return new Result(l, type, new_offset);
172+
return new Result(l, new_offset);
175173
case INT32:
176174
IntNode int32 = Decoder.decodeInt32(bytes);
177-
return new Result(int32, type, new_offset);
175+
return new Result(int32, new_offset);
178176
case UINT64:
179177
BigIntegerNode bi = Decoder.decodeUint64(bytes);
180-
return new Result(bi, type, new_offset);
178+
return new Result(bi, new_offset);
181179
case UINT128:
182180
BigIntegerNode uint128 = Decoder.decodeUint128(bytes);
183-
return new Result(uint128, type, new_offset);
181+
return new Result(uint128, new_offset);
184182
default:
185183
throw new MaxMindDbException("Unknown or unexpected type: "
186184
+ type.name());
@@ -207,8 +205,7 @@ private Result decodePointer(int ctrlByte, long offset) throws IOException {
207205
Log.debug("Buffer", buffer);
208206
}
209207

210-
buffer.put(0, pointerSize == 4 ? (byte) 0
211-
: (byte) (ctrlByte & 0x7));
208+
buffer.put(0, pointerSize == 4 ? (byte) 0 : (byte) (ctrlByte & 0x7));
212209

213210
long packed = Util.decodeLong(buffer.array());
214211

@@ -226,8 +223,7 @@ private Result decodePointer(int ctrlByte, long offset) throws IOException {
226223
Log.debug("Pointer to", String.valueOf(pointer));
227224
}
228225

229-
return new Result(new LongNode(pointer), Type.POINTER, offset
230-
+ pointerSize);
226+
return new Result(new LongNode(pointer), offset + pointerSize);
231227
}
232228

233229
private static String decodeString(byte[] bytes) {
@@ -260,14 +256,14 @@ static BigIntegerNode decodeUint128(byte[] bytes) {
260256
}
261257

262258
private static DoubleNode decodeDouble(byte[] bytes) {
263-
return new DoubleNode(Double.parseDouble(new String(bytes,
264-
Charset.forName("US-ASCII"))));
259+
return new DoubleNode(Double.parseDouble(new String(bytes, Charset
260+
.forName("US-ASCII"))));
265261
}
266262

267263
private Result decodeBoolean(long size, long offset) {
268264
BooleanNode b = size == 0 ? BooleanNode.FALSE : BooleanNode.TRUE;
269265

270-
return new Result(b, Type.BOOLEAN, offset);
266+
return new Result(b, offset);
271267
}
272268

273269
private Result decodeArray(long size, long offset)
@@ -292,7 +288,7 @@ private Result decodeArray(long size, long offset)
292288
Log.debug("Decoded array", array.toString());
293289
}
294290

295-
return new Result(array, Type.ARRAY, offset);
291+
return new Result(array, offset);
296292
}
297293

298294
private Result decodeMap(long size, long offset) throws MaxMindDbException,
@@ -323,7 +319,7 @@ private Result decodeMap(long size, long offset) throws MaxMindDbException,
323319
Log.debug("Decoded map", map.toString());
324320
}
325321

326-
return new Result(map, Type.MAP, offset);
322+
return new Result(map, offset);
327323

328324
}
329325

src/main/java/com/maxmind/maxminddb/Reader.java

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,9 @@
1010

1111
public class Reader {
1212
private static final int DATA_SECTION_SEPARATOR_SIZE = 16;
13-
private static final byte[] METADATA_START_MARKER = { (byte) 0xAB, (byte) 0xCD,
14-
(byte) 0xEF, 'M', 'a', 'x', 'M', 'i', 'n', 'd', '.', 'c', 'o', 'm' };
13+
private static final byte[] METADATA_START_MARKER = { (byte) 0xAB,
14+
(byte) 0xCD, (byte) 0xEF, 'M', 'a', 'x', 'M', 'i', 'n', 'd', '.',
15+
'c', 'o', 'm' };
1516

1617
private final boolean DEBUG;
1718
private final Decoder decoder;
@@ -57,7 +58,7 @@ public Reader(File database) throws MaxMindDbException, IOException {
5758
this.decoder = new Decoder(this.fc, this.metadata.searchTreeSize
5859
+ DATA_SECTION_SEPARATOR_SIZE);
5960

60-
if (DEBUG) {
61+
if (this.DEBUG) {
6162
Log.debug(this.metadata.toString());
6263
}
6364
}
@@ -87,7 +88,7 @@ long findAddressInTree(InetAddress address) throws MaxMindDbException,
8788
rawAddress = newAddress;
8889
}
8990

90-
if (DEBUG) {
91+
if (this.DEBUG) {
9192
Log.debugNewLine();
9293
Log.debug("IP address", address);
9394
Log.debug("IP address", rawAddress);
@@ -105,7 +106,7 @@ long findAddressInTree(InetAddress address) throws MaxMindDbException,
105106

106107
long record = nodes[bit];
107108

108-
if (DEBUG) {
109+
if (this.DEBUG) {
109110
Log.debug("Nodes", Arrays.toString(nodes));
110111
Log.debug("Bit #", i);
111112
Log.debug("Bit value", bit);
@@ -115,20 +116,20 @@ long record = nodes[bit];
115116
}
116117

117118
if (record == this.metadata.nodeCount) {
118-
if (DEBUG) {
119+
if (this.DEBUG) {
119120
Log.debug("Record is empty");
120121
}
121122
return 0;
122123
}
123124

124125
if (record >= this.metadata.nodeCount) {
125-
if (DEBUG) {
126+
if (this.DEBUG) {
126127
Log.debug("Record is a data pointer");
127128
}
128129
return record;
129130
}
130131

131-
if (DEBUG) {
132+
if (this.DEBUG) {
132133
Log.debug("Record is a node number");
133134
}
134135

@@ -147,7 +148,7 @@ private long[] readNode(long nodeNumber) throws IOException,
147148

148149
this.fc.read(buffer);
149150

150-
if (DEBUG) {
151+
if (this.DEBUG) {
151152
Log.debug("Node bytes", buffer);
152153
}
153154
return this.splitNodeIntoRecords(buffer);
@@ -188,7 +189,7 @@ private Object resolveDataPointer(long pointer) throws MaxMindDbException,
188189
long resolved = (pointer - this.metadata.nodeCount)
189190
+ this.metadata.searchTreeSize;
190191

191-
if (DEBUG) {
192+
if (this.DEBUG) {
192193
long treeSize = this.metadata.searchTreeSize;
193194

194195
Log.debug("Resolved data pointer", "( " + pointer + " - "

src/main/java/com/maxmind/maxminddb/package-info.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,6 @@
33
*/
44
/**
55
* @author greg
6-
*
6+
*
77
*/
88
package com.maxmind.maxminddb;

0 commit comments

Comments
 (0)