Skip to content

Commit 96cc85d

Browse files
committed
Misc. cleanup
1 parent 1756317 commit 96cc85d

8 files changed

Lines changed: 78 additions & 86 deletions

File tree

pom.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,8 @@
7777
<artifactId>maven-compiler-plugin</artifactId>
7878
<version>3.1</version>
7979
<configuration>
80-
<source>1.7</source>
81-
<target>1.7</target>
80+
<source>1.6</source>
81+
<target>1.6</target>
8282
</configuration>
8383
</plugin>
8484
</plugins>

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

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
public class Decoder {
2222
private final FileChannel in;
2323

24-
private final boolean DEBUG = false;
24+
private final boolean DEBUG;
2525
// XXX - This is only for unit testings. We should possibly make a
2626
// constructor to set this
2727
boolean POINTER_TEST_HACK = false;
@@ -53,15 +53,13 @@ void setOffset(long offset) {
5353
this.offset = offset;
5454
}
5555

56-
Type getType() {
57-
return this.type;
58-
}
5956
}
6057

6158
public Decoder(FileChannel in, long pointerBase) {
6259
this.in = in;
6360
this.pointerBase = pointerBase;
6461
this.objectMapper = new ObjectMapper();
62+
this.DEBUG = System.getenv().get("MAXMIND_DB_DECODER_DEBUG") != null;
6563
}
6664

6765
// FIXME - Move most of this method to a switch statement
@@ -160,13 +158,13 @@ public Result decode(long offset) throws MaxMindDbException, IOException {
160158
long new_offset = offset + size;
161159
switch (type) {
162160
case UTF8_STRING:
163-
TextNode s = new TextNode(this.decodeString(bytes));
161+
TextNode s = new TextNode(Decoder.decodeString(bytes));
164162
return new Result(s, type, new_offset);
165163
case DOUBLE:
166-
DoubleNode d = this.decodeDouble(bytes);
164+
DoubleNode d = Decoder.decodeDouble(bytes);
167165
return new Result(d, type, new_offset);
168166
case BYTES:
169-
BinaryNode b = new BinaryNode(this.decodeBytes(bytes));
167+
BinaryNode b = new BinaryNode(Decoder.decodeBytes(bytes));
170168
return new Result(b, type, new_offset);
171169
case UINT16:
172170
IntNode i = Decoder.decodeUint16(bytes);
@@ -232,12 +230,12 @@ private Result decodePointer(int ctrlByte, long offset) throws IOException {
232230
+ pointerSize);
233231
}
234232

235-
private String decodeString(byte[] bytes) {
233+
private static String decodeString(byte[] bytes) {
236234
return new String(bytes, Charset.forName("UTF-8"));
237235
}
238236

239237
// XXX - nop
240-
private byte[] decodeBytes(byte[] bytes) {
238+
private static byte[] decodeBytes(byte[] bytes) {
241239
return bytes;
242240
}
243241

@@ -261,8 +259,8 @@ static BigIntegerNode decodeUint128(byte[] bytes) {
261259
return new BigIntegerNode(new BigInteger(1, bytes));
262260
}
263261

264-
private DoubleNode decodeDouble(byte[] bytes) {
265-
return new DoubleNode(new Double(new String(bytes,
262+
private static DoubleNode decodeDouble(byte[] bytes) {
263+
return new DoubleNode(Double.parseDouble(new String(bytes,
266264
Charset.forName("US-ASCII"))));
267265
}
268266

@@ -315,7 +313,7 @@ private Result decodeMap(long size, long offset) throws MaxMindDbException,
315313
offset = valueResult.getOffset();
316314

317315
if (this.DEBUG) {
318-
Log.debug("Key " + i, key.toString());
316+
Log.debug("Key " + i, key);
319317
Log.debug("Value " + i, value.toString());
320318
}
321319
map.put(key, value);

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

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,6 @@ static void debugBinary(String name, int value) {
3434
Log.debug(name, Integer.toBinaryString(value));
3535
}
3636

37-
private static void debug(String string, byte b) {
38-
Log.debug(string, Integer.toBinaryString(b & 0xFF));
39-
}
40-
4137
static void debug(String name, String value) {
4238
debug(name + ": " + value);
4339
}

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

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,17 @@
66

77
// XXX - if we make this public, add getters
88
class Metadata {
9-
Integer binaryFormatMajorVersion;
10-
Integer binaryFormatMinorVersion;
11-
BigInteger buildEpoch;
12-
String databaseType;
13-
JsonNode description;
14-
Integer ipVersion;
15-
Long nodeCount;
16-
Long recordSize;
17-
int nodeByteSize;
18-
long searchTreeSize;
19-
JsonNode languages;
9+
final int binaryFormatMajorVersion;
10+
final int binaryFormatMinorVersion;
11+
private final BigInteger buildEpoch;
12+
final String databaseType;
13+
final JsonNode description;
14+
final int ipVersion;
15+
final long nodeCount;
16+
final int recordSize;
17+
final int nodeByteSize;
18+
final long searchTreeSize;
19+
final JsonNode languages;
2020

2121
// XXX - think about how I want to construct this. Maybe look at how JSON
2222
// parsers deal with types
@@ -31,8 +31,8 @@ public Metadata(JsonNode metadata) {
3131
this.description = metadata.get("description");
3232
this.ipVersion = metadata.get("ip_version").asInt();
3333
this.nodeCount = metadata.get("node_count").asLong();
34-
this.recordSize = metadata.get("record_size").asLong();
35-
this.nodeByteSize = (int) (this.recordSize / 4);
34+
this.recordSize = metadata.get("record_size").asInt();
35+
this.nodeByteSize = this.recordSize / 4;
3636
this.searchTreeSize = this.nodeCount * this.nodeByteSize;
3737

3838
}

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

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9,20 +9,20 @@
99
import java.util.Arrays;
1010

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

16-
private static final boolean DEBUG = false;
16+
private final boolean DEBUG;
1717
private final Decoder decoder;
1818
private final Metadata metadata;
19-
private final long dataSectionEnd;
2019
private final FileChannel fc;
20+
private final RandomAccessFile raf;
2121

2222
public Reader(File database) throws MaxMindDbException, IOException {
23-
24-
RandomAccessFile raf = new RandomAccessFile(database, "r");
25-
this.fc = raf.getChannel();
23+
this.DEBUG = System.getenv().get("MAXMIND_DB_READER_DEBUG") != null;
24+
this.raf = new RandomAccessFile(database, "r");
25+
this.fc = this.raf.getChannel();
2626
// XXX - we will want
2727
// MappedByteBuffer in = fc.map(FileChannel.MapMode.READ_ONLY, 0,
2828
// fc.size());
@@ -50,9 +50,6 @@ public Reader(File database) throws MaxMindDbException, IOException {
5050
+ "). Is this a valid MaxMind DB file?");
5151
}
5252

53-
// XXX - right?
54-
this.dataSectionEnd = start - METADATE_START_MARKER.length;
55-
5653
Decoder metadataDecoder = new Decoder(this.fc, 0);
5754

5855
this.metadata = new Metadata(metadataDecoder.decode(start).getObject());
@@ -159,7 +156,7 @@ private long[] readNode(long nodeNumber) throws IOException,
159156
private long[] splitNodeIntoRecords(ByteBuffer bytes)
160157
throws MaxMindDbException {
161158
long[] nodes = new long[2];
162-
switch (this.metadata.recordSize.intValue()) {
159+
switch (this.metadata.recordSize) {
163160
case 24:
164161
nodes[0] = Util.decodeLong(Arrays.copyOfRange(bytes.array(), 0,
165162
3));
@@ -215,11 +212,11 @@ private Object resolveDataPointer(long pointer) throws MaxMindDbException,
215212
private long findMetadataStart() throws IOException {
216213
long fileSize = this.fc.size();
217214

218-
FILE: for (long i = 0; i < fileSize - METADATE_START_MARKER.length + 1; i++) {
219-
for (int j = 0; j < METADATE_START_MARKER.length; j++) {
215+
FILE: for (long i = 0; i < fileSize - METADATA_START_MARKER.length + 1; i++) {
216+
for (int j = 0; j < METADATA_START_MARKER.length; j++) {
220217
ByteBuffer b = ByteBuffer.wrap(new byte[1]);
221218
this.fc.read(b, fileSize - i - j - 1);
222-
if (b.get(0) != METADATE_START_MARKER[METADATE_START_MARKER.length
219+
if (b.get(0) != METADATA_START_MARKER[METADATA_START_MARKER.length
223220
- j - 1]) {
224221
continue FILE;
225222
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ public static Type get(int i) {
88
return Type.values()[i];
99
}
1010

11-
public static Type get(byte b) {
11+
private static Type get(byte b) {
1212
// bytes are signed, but we want to treat them as unsigned here
1313
// XXX - Type.values() might be expensive. Consider caching it.
1414
return Type.get(b & 0xFF);

0 commit comments

Comments
 (0)