Skip to content

Commit 2e59e12

Browse files
committed
Removed debugging log cruft.
This code made the logic harder to follow and it isn't really useful now that the reader works.
1 parent 5718df0 commit 2e59e12

3 files changed

Lines changed: 12 additions & 130 deletions

File tree

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

Lines changed: 1 addition & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,6 @@
1919
import com.fasterxml.jackson.databind.node.TextNode;
2020

2121
final class Decoder {
22-
23-
private final static boolean DEBUG = System.getenv().get(
24-
"MAXMIND_DB_DECODER_DEBUG") != null;
2522
// XXX - This is only for unit testings. We should possibly make a
2623
// constructor to set this
2724
boolean POINTER_TEST_HACK = false;
@@ -91,13 +88,7 @@ Result decode(int offset) throws IOException {
9188

9289
Type type = Type.fromControlByte(ctrlByte);
9390

94-
if (Decoder.DEBUG) {
95-
Log.debug("Offset", String.valueOf(offset));
96-
Log.debugBinary("Control byte", ctrlByte);
97-
Log.debug("Type", type.name());
98-
}
99-
100-
// Pointers are a special case, we don't read the next $size bytes, we
91+
// Pointers are a special case, we don't read the next 'size' bytes, we
10192
// use the size to determine the length of the pointer and then follow
10293
// it.
10394
if (type.equals(Type.POINTER)) {
@@ -115,10 +106,6 @@ Result decode(int offset) throws IOException {
115106
if (type.equals(Type.EXTENDED)) {
116107
int nextByte = buffer.get();
117108

118-
if (Decoder.DEBUG) {
119-
Log.debug("Next byte", nextByte);
120-
}
121-
122109
int typeNum = nextByte + 7;
123110

124111
if (typeNum < 8) {
@@ -136,11 +123,6 @@ Result decode(int offset) throws IOException {
136123
int size = sizeArray[0];
137124
offset = sizeArray[1];
138125

139-
if (Decoder.DEBUG) {
140-
Log.debug("Size", String.valueOf(size));
141-
Log.debug("Offset", offset);
142-
Log.debug("Size", size);
143-
}
144126
return this.decodeByType(type, offset, size);
145127
}
146128

@@ -184,7 +166,6 @@ private Result decodeByType(Type type, int offset, int size)
184166
default:
185167
throw new InvalidDatabaseException(
186168
"Unknown or unexpected type: " + type.name());
187-
188169
}
189170
}
190171

@@ -198,15 +179,6 @@ private Result decodePointer(int ctrlByte, int offset) {
198179
long pointer = packed + this.pointerBase
199180
+ this.pointerValueOffset[pointerSize];
200181

201-
if (Decoder.DEBUG) {
202-
Log.debug("Pointer size", String.valueOf(pointerSize));
203-
Log.debug("Packed pointer", String.valueOf(packed));
204-
Log.debug("Pointer base", this.pointerBase);
205-
Log.debug("Pointer value offset",
206-
this.pointerValueOffset[pointerSize]);
207-
Log.debug("Pointer to", String.valueOf(pointer));
208-
}
209-
210182
return new Result(new LongNode(pointer), offset + pointerSize);
211183
}
212184

@@ -280,11 +252,6 @@ private Result decodeArray(int size, int offset) throws IOException {
280252
array.add(r.getNode());
281253
}
282254

283-
if (Decoder.DEBUG) {
284-
Log.debug("Array size", size);
285-
Log.debug("Decoded array", array.toString());
286-
}
287-
288255
return new Result(array, offset);
289256
}
290257

@@ -303,11 +270,6 @@ private Result decodeMap(int size, int offset) throws IOException {
303270
map.put(key, value);
304271
}
305272

306-
if (Decoder.DEBUG) {
307-
Log.debug("Map size", size);
308-
Log.debug("Decoded map", map.toString());
309-
}
310-
311273
return new Result(map, offset);
312274
}
313275

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

Lines changed: 0 additions & 45 deletions
This file was deleted.

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

Lines changed: 11 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,6 @@ public final class MaxMindDbReader implements Closeable {
1818
(byte) 0xCD, (byte) 0xEF, 'M', 'a', 'x', 'M', 'i', 'n', 'd', '.',
1919
'c', 'o', 'm' };
2020

21-
private final static boolean DEBUG = System.getenv().get(
22-
"MAXMIND_DB_READER_DEBUG") != null;
2321
private final Decoder decoder;
2422
private final Metadata metadata;
2523
private final ThreadBuffer threadBuffer;
@@ -43,7 +41,7 @@ public enum FileMode {
4341
/**
4442
* Constructs a Reader for the MaxMind DB format. The file passed to it must
4543
* be a valid MaxMind DB file such as a GeoIP2 database file.
46-
*
44+
*
4745
* @param database
4846
* the MaxMind DB file to use.
4947
* @throws IOException
@@ -56,7 +54,7 @@ public MaxMindDbReader(File database) throws IOException {
5654
/**
5755
* Constructs a Reader for the MaxMind DB format. The file passed to it must
5856
* be a valid MaxMind DB file such as a GeoIP2 database file.
59-
*
57+
*
6058
* @param database
6159
* the MaxMind DB file to use.
6260
* @param fileMode
@@ -72,15 +70,11 @@ public MaxMindDbReader(File database, FileMode fileMode) throws IOException {
7270
this.metadata = new Metadata(metadataDecoder.decode(start).getNode());
7371
this.decoder = new Decoder(this.threadBuffer,
7472
this.metadata.searchTreeSize + DATA_SECTION_SEPARATOR_SIZE);
75-
76-
if (MaxMindDbReader.DEBUG) {
77-
Log.debug(this.metadata.toString());
78-
}
7973
}
8074

8175
/**
8276
* Looks up the <code>address</code> in the MaxMind DB.
83-
*
77+
*
8478
* @param ipAddress
8579
* the IP address to look up.
8680
* @return the record for the IP address.
@@ -99,13 +93,6 @@ private int findAddressInTree(InetAddress address)
9993
throws InvalidDatabaseException {
10094
byte[] rawAddress = address.getAddress();
10195

102-
if (MaxMindDbReader.DEBUG) {
103-
Log.debugNewLine();
104-
Log.debug("IP address", address);
105-
Log.debug("IP address", rawAddress);
106-
Log.debugNewLine();
107-
}
108-
10996
boolean isIp4AddressInIp6Db = rawAddress.length == 4
11097
&& this.metadata.ipVersion == 6;
11198
int ipStartBit = isIp4AddressInIp6Db ? 96 : 0;
@@ -122,29 +109,14 @@ private int findAddressInTree(InetAddress address)
122109
}
123110
int record = this.readNode(nodeNum, bit);
124111

125-
if (MaxMindDbReader.DEBUG) {
126-
Log.debug("Bit #", i);
127-
Log.debug("Bit value", bit);
128-
Log.debug("Record", bit == 1 ? "right" : "left");
129-
Log.debug("Record value", record);
130-
}
131-
132112
if (record == this.metadata.nodeCount) {
133-
if (MaxMindDbReader.DEBUG) {
134-
Log.debug("Record is empty");
135-
}
113+
// record is empty
136114
return 0;
137115
} else if (record > this.metadata.nodeCount) {
138-
if (MaxMindDbReader.DEBUG) {
139-
Log.debug("Record is a data pointer");
140-
}
116+
// record is a data pointer
141117
return record;
142118
}
143119

144-
if (MaxMindDbReader.DEBUG) {
145-
Log.debug("Record is a node number");
146-
}
147-
148120
nodeNum = record;
149121
}
150122
throw new InvalidDatabaseException("Something bad happened");
@@ -182,13 +154,6 @@ private JsonNode resolveDataPointer(int pointer) throws IOException {
182154
int resolved = (pointer - this.metadata.nodeCount)
183155
+ this.metadata.searchTreeSize;
184156

185-
if (MaxMindDbReader.DEBUG) {
186-
int treeSize = this.metadata.searchTreeSize;
187-
Log.debug("Resolved data pointer", "( " + pointer + " - "
188-
+ this.metadata.nodeCount + " ) + " + treeSize + " = "
189-
+ resolved);
190-
}
191-
192157
// We only want the data from the decoder, not the offset where it was
193158
// found.
194159
return this.decoder.decode(resolved).getNode();
@@ -197,12 +162,13 @@ private JsonNode resolveDataPointer(int pointer) throws IOException {
197162
/*
198163
* Apparently searching a file for a sequence is not a solved problem in
199164
* Java. This searches from the end of the file for metadata start.
200-
*
165+
*
201166
* This is an extremely naive but reasonably readable implementation. There
202167
* are much faster algorithms (e.g., Boyer-Moore) for this if speed is ever
203168
* an issue, but I suspect it won't be.
204169
*/
205-
private int findMetadataStart(String databaseName) throws InvalidDatabaseException {
170+
private int findMetadataStart(String databaseName)
171+
throws InvalidDatabaseException {
206172
ByteBuffer buffer = this.threadBuffer.get();
207173
int fileSize = buffer.capacity();
208174

@@ -217,9 +183,8 @@ private int findMetadataStart(String databaseName) throws InvalidDatabaseExcepti
217183
return fileSize - i;
218184
}
219185
throw new InvalidDatabaseException(
220-
"Could not find a MaxMind DB metadata marker in this file ("
221-
+ databaseName
222-
+ "). Is this a valid MaxMind DB file?");
186+
"Could not find a MaxMind DB metadata marker in this file ("
187+
+ databaseName + "). Is this a valid MaxMind DB file?");
223188
}
224189

225190
Metadata getMetadata() {
@@ -228,7 +193,7 @@ Metadata getMetadata() {
228193

229194
/**
230195
* Closes the MaxMind DB and returns resources to the system.
231-
*
196+
*
232197
* @throws IOException
233198
* if an I/O error occurs.
234199
*/

0 commit comments

Comments
 (0)